16477eba65f5e571478d69190fa802ab2a1b2342
[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 killed 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-cached-mark ?*
424   "*Mark used for articles that are in the cache."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-saved-mark ?S
429   "*Mark used for articles that have been saved to."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-no-mark ?  ;Whitespace
434   "*Mark used for articles that have no other secondary mark."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-ancient-mark ?O
439   "*Mark used for ancient articles."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-sparse-mark ?Q
444   "*Mark used for sparsely reffed articles."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-canceled-mark ?G
449   "*Mark used for canceled articles."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-duplicate-mark ?M
454   "*Mark used for duplicate articles."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-undownloaded-mark ?@
459   "*Mark used for articles that weren't downloaded."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-downloadable-mark ?%
464   "*Mark used for articles that are to be downloaded."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-unsendable-mark ?=
469   "*Mark used for articles that won't be sent."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-score-over-mark ?+
474   "*Score mark used for articles with high scores."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-score-below-mark ?-
479   "*Score mark used for articles with low scores."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-empty-thread-mark ?  ;Whitespace
484   "*There is no thread under the article."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-not-empty-thread-mark ?=
489   "*There is a thread under the article."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-view-pseudo-asynchronously nil
494   "*If non-nil, Gnus will view pseudo-articles asynchronously."
495   :group 'gnus-extract-view
496   :type 'boolean)
497
498 (defcustom gnus-auto-expirable-marks
499   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
500         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
501         gnus-souped-mark gnus-duplicate-mark)
502   "*The list of marks converted into expiration if a group is auto-expirable."
503   :version "21.1"
504   :group 'gnus-summary
505   :type '(repeat character))
506
507 (defcustom gnus-inhibit-user-auto-expire t
508   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
509   :version "21.1"
510   :group 'gnus-summary
511   :type 'boolean)
512
513 (defcustom gnus-view-pseudos nil
514   "*If `automatic', pseudo-articles will be viewed automatically.
515 If `not-confirm', pseudos will be viewed automatically, and the user
516 will not be asked to confirm the command."
517   :group 'gnus-extract-view
518   :type '(choice (const :tag "off" nil)
519                  (const automatic)
520                  (const not-confirm)))
521
522 (defcustom gnus-view-pseudos-separately t
523   "*If non-nil, one pseudo-article will be created for each file to be viewed.
524 If nil, all files that use the same viewing command will be given as a
525 list of parameters to that command."
526   :group 'gnus-extract-view
527   :type 'boolean)
528
529 (defcustom gnus-insert-pseudo-articles t
530   "*If non-nil, insert pseudo-articles when decoding articles."
531   :group 'gnus-extract-view
532   :type 'boolean)
533
534 (defcustom gnus-summary-dummy-line-format
535   "  %(:                          :%) %S\n"
536   "*The format specification for the dummy roots in the summary buffer.
537 It works along the same lines as a normal formatting string,
538 with some simple extensions.
539
540 %S  The subject"
541   :group 'gnus-threading
542   :type 'string)
543
544 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
545   "*The format specification for the summary mode line.
546 It works along the same lines as a normal formatting string,
547 with some simple extensions:
548
549 %G  Group name
550 %p  Unprefixed group name
551 %A  Current article number
552 %z  Current article score
553 %V  Gnus version
554 %U  Number of unread articles in the group
555 %e  Number of unselected articles in the group
556 %Z  A string with unread/unselected article counts
557 %g  Shortish group name
558 %S  Subject of the current article
559 %u  User-defined spec
560 %s  Current score file name
561 %d  Number of dormant articles
562 %r  Number of articles that have been marked as read in this session
563 %E  Number of articles expunged by the score files"
564   :group 'gnus-summary-format
565   :type 'string)
566
567 (defcustom gnus-list-identifiers nil
568   "Regexp that matches list identifiers to be removed from subject.
569 This can also be a list of regexps."
570   :version "21.1"
571   :group 'gnus-summary-format
572   :group 'gnus-article-hiding
573   :type '(choice (const :tag "none" nil)
574                  (regexp :value ".*")
575                  (repeat :value (".*") regexp)))
576
577 (defcustom gnus-summary-mark-below 0
578   "*Mark all articles with a score below this variable as read.
579 This variable is local to each summary buffer and usually set by the
580 score file."
581   :group 'gnus-score-default
582   :type 'integer)
583
584 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
585   "*List of functions used for sorting articles in the summary buffer.
586
587 Each function takes two articles and returns non-nil if the first
588 article should be sorted before the other.  If you use more than one
589 function, the primary sort function should be the last.  You should
590 probably always include `gnus-article-sort-by-number' in the list of
591 sorting functions -- preferably first.  Also note that sorting by date
592 is often much slower than sorting by number, and the sorting order is
593 very similar.  (Sorting by date means sorting by the time the message
594 was sent, sorting by number means sorting by arrival time.)
595
596 Ready-made functions include `gnus-article-sort-by-number',
597 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
598 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
599
600 When threading is turned on, the variable `gnus-thread-sort-functions'
601 controls how articles are sorted."
602   :group 'gnus-summary-sort
603   :type '(repeat (choice (function-item gnus-article-sort-by-number)
604                          (function-item gnus-article-sort-by-author)
605                          (function-item gnus-article-sort-by-subject)
606                          (function-item gnus-article-sort-by-date)
607                          (function-item gnus-article-sort-by-score)
608                          (function :tag "other"))))
609
610 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
611   "*List of functions used for sorting threads in the summary buffer.
612 By default, threads are sorted by article number.
613
614 Each function takes two threads and returns non-nil if the first
615 thread should be sorted before the other.  If you use more than one
616 function, the primary sort function should be the last.  You should
617 probably always include `gnus-thread-sort-by-number' in the list of
618 sorting functions -- preferably first.  Also note that sorting by date
619 is often much slower than sorting by number, and the sorting order is
620 very similar.  (Sorting by date means sorting by the time the message
621 was sent, sorting by number means sorting by arrival time.)
622
623 Ready-made functions include `gnus-thread-sort-by-number',
624 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
625 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
626 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
627
628 When threading is turned off, the variable
629 `gnus-article-sort-functions' controls how articles are sorted."
630   :group 'gnus-summary-sort
631   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
632                          (function-item gnus-thread-sort-by-author)
633                          (function-item gnus-thread-sort-by-subject)
634                          (function-item gnus-thread-sort-by-date)
635                          (function-item gnus-thread-sort-by-score)
636                          (function-item gnus-thread-sort-by-total-score)
637                          (function :tag "other"))))
638
639 (defcustom gnus-thread-score-function '+
640   "*Function used for calculating the total score of a thread.
641
642 The function is called with the scores of the article and each
643 subthread and should then return the score of the thread.
644
645 Some functions you can use are `+', `max', or `min'."
646   :group 'gnus-summary-sort
647   :type 'function)
648
649 (defcustom gnus-summary-expunge-below nil
650   "All articles that have a score less than this variable will be expunged.
651 This variable is local to the summary buffers."
652   :group 'gnus-score-default
653   :type '(choice (const :tag "off" nil)
654                  integer))
655
656 (defcustom gnus-thread-expunge-below nil
657   "All threads that have a total score less than this variable will be expunged.
658 See `gnus-thread-score-function' for en explanation of what a
659 \"thread score\" is.
660
661 This variable is local to the summary buffers."
662   :group 'gnus-threading
663   :group 'gnus-score-default
664   :type '(choice (const :tag "off" nil)
665                  integer))
666
667 (defcustom gnus-summary-mode-hook nil
668   "*A hook for Gnus summary mode.
669 This hook is run before any variables are set in the summary buffer."
670   :options '(turn-on-gnus-mailing-list-mode)
671   :group 'gnus-summary-various
672   :type 'hook)
673
674 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
675 (when (featurep 'xemacs)
676   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
677   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
678   (add-hook 'gnus-summary-mode-hook
679             'gnus-xmas-switch-horizontal-scrollbar-off))
680
681 (defcustom gnus-summary-menu-hook nil
682   "*Hook run after the creation of the summary mode menu."
683   :group 'gnus-summary-visual
684   :type 'hook)
685
686 (defcustom gnus-summary-exit-hook nil
687   "*A hook called on exit from the summary buffer.
688 It will be called with point in the group buffer."
689   :group 'gnus-summary-exit
690   :type 'hook)
691
692 (defcustom gnus-summary-prepare-hook nil
693   "*A hook called after the summary buffer has been generated.
694 If you want to modify the summary buffer, you can use this hook."
695   :group 'gnus-summary-various
696   :type 'hook)
697
698 (defcustom gnus-summary-prepared-hook nil
699   "*A hook called as the last thing after the summary buffer has been generated."
700   :group 'gnus-summary-various
701   :type 'hook)
702
703 (defcustom gnus-summary-generate-hook nil
704   "*A hook run just before generating the summary buffer.
705 This hook is commonly used to customize threading variables and the
706 like."
707   :group 'gnus-summary-various
708   :type 'hook)
709
710 (defcustom gnus-select-group-hook nil
711   "*A hook called when a newsgroup is selected.
712
713 If you'd like to simplify subjects like the
714 `gnus-summary-next-same-subject' command does, you can use the
715 following hook:
716
717  (setq gnus-select-group-hook
718       (list
719         (lambda ()
720           (mapcar (lambda (header)
721                      (mail-header-set-subject
722                       header
723                       (gnus-simplify-subject
724                        (mail-header-subject header) 're-only)))
725                   gnus-newsgroup-headers))))"
726   :group 'gnus-group-select
727   :type 'hook)
728
729 (defcustom gnus-select-article-hook nil
730   "*A hook called when an article is selected."
731   :group 'gnus-summary-choose
732   :type 'hook)
733
734 (defcustom gnus-visual-mark-article-hook
735   (list 'gnus-highlight-selected-summary)
736   "*Hook run after selecting an article in the summary buffer.
737 It is meant to be used for highlighting the article in some way.  It
738 is not run if `gnus-visual' is nil."
739   :group 'gnus-summary-visual
740   :type 'hook)
741
742 (defcustom gnus-parse-headers-hook nil
743   "*A hook called before parsing the headers."
744   :group 'gnus-various
745   :type 'hook)
746
747 (defcustom gnus-exit-group-hook nil
748   "*A hook called when exiting summary mode.
749 This hook is not called from the non-updating exit commands like `Q'."
750   :group 'gnus-various
751   :type 'hook)
752
753 (defcustom gnus-summary-update-hook
754   (list 'gnus-summary-highlight-line)
755   "*A hook called when a summary line is changed.
756 The hook will not be called if `gnus-visual' is nil.
757
758 The default function `gnus-summary-highlight-line' will
759 highlight the line according to the `gnus-summary-highlight'
760 variable."
761   :group 'gnus-summary-visual
762   :type 'hook)
763
764 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
765   "*A hook called when an article is selected for the first time.
766 The hook is intended to mark an article as read (or unread)
767 automatically when it is selected."
768   :group 'gnus-summary-choose
769   :type 'hook)
770
771 (defcustom gnus-group-no-more-groups-hook nil
772   "*A hook run when returning to group mode having no more (unread) groups."
773   :group 'gnus-group-select
774   :type 'hook)
775
776 (defcustom gnus-ps-print-hook nil
777   "*A hook run before ps-printing something from Gnus."
778   :group 'gnus-summary
779   :type 'hook)
780
781 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
782   "Face used for highlighting the current article in the summary buffer."
783   :group 'gnus-summary-visual
784   :type 'face)
785
786 (defcustom gnus-summary-highlight
787   '(((= mark gnus-canceled-mark)
788      . gnus-summary-cancelled-face)
789     ((and (> score default)
790           (or (= mark gnus-dormant-mark)
791               (= mark gnus-ticked-mark)))
792      . gnus-summary-high-ticked-face)
793     ((and (< score default)
794           (or (= mark gnus-dormant-mark)
795               (= mark gnus-ticked-mark)))
796      . gnus-summary-low-ticked-face)
797     ((or (= mark gnus-dormant-mark)
798          (= mark gnus-ticked-mark))
799      . gnus-summary-normal-ticked-face)
800     ((and (> score default) (= mark gnus-ancient-mark))
801      . gnus-summary-high-ancient-face)
802     ((and (< score default) (= mark gnus-ancient-mark))
803      . gnus-summary-low-ancient-face)
804     ((= mark gnus-ancient-mark)
805      . gnus-summary-normal-ancient-face)
806     ((and (> score default) (= mark gnus-unread-mark))
807      . gnus-summary-high-unread-face)
808     ((and (< score default) (= mark gnus-unread-mark))
809      . gnus-summary-low-unread-face)
810     ((= mark gnus-unread-mark)
811      . gnus-summary-normal-unread-face)
812     ((and (> score default) (memq mark (list gnus-downloadable-mark
813                                              gnus-undownloaded-mark)))
814      . gnus-summary-high-unread-face)
815     ((and (< score default) (memq mark (list gnus-downloadable-mark
816                                              gnus-undownloaded-mark)))
817      . gnus-summary-low-unread-face)
818     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
819      . gnus-summary-normal-unread-face)
820     ((> score default)
821      . gnus-summary-high-read-face)
822     ((< score default)
823      . gnus-summary-low-read-face)
824     (t
825      . gnus-summary-normal-read-face))
826   "*Controls the highlighting of summary buffer lines.
827
828 A list of (FORM . FACE) pairs.  When deciding how a a particular
829 summary line should be displayed, each form is evaluated.  The content
830 of the face field after the first true form is used.  You can change
831 how those summary lines are displayed, by editing the face field.
832
833 You can use the following variables in the FORM field.
834
835 score:   The articles score
836 default: The default article score.
837 below:   The score below which articles are automatically marked as read.
838 mark:    The articles mark."
839   :group 'gnus-summary-visual
840   :type '(repeat (cons (sexp :tag "Form" nil)
841                        face)))
842
843 (defcustom gnus-alter-header-function nil
844   "Function called to allow alteration of article header structures.
845 The function is called with one parameter, the article header vector,
846 which it may alter in any way.")
847
848 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
849   "Variable that says which function should be used to decode a string with encoded words.")
850
851 (defcustom gnus-extra-headers nil
852   "*Extra headers to parse."
853   :version "21.1"
854   :group 'gnus-summary
855   :type '(repeat symbol))
856
857 (defcustom gnus-ignored-from-addresses
858   (and user-mail-address (regexp-quote user-mail-address))
859   "*Regexp of From headers that may be suppressed in favor of To headers."
860   :version "21.1"
861   :group 'gnus-summary
862   :type 'regexp)
863
864 (gnus-define-group-parameter
865  charset
866  :function-document
867  "Return the default charset of GROUP."
868  :variable gnus-group-charset-alist
869  :variable-default 
870  '(("\\(^\\|:\\)hk\\>\\|\\(^\\|:\\)tw\\>\\|\\<big5\\>" cn-big5)
871    ("\\(^\\|:\\)cn\\>\\|\\<chinese\\>" cn-gb-2312)
872    ("\\(^\\|:\\)fj\\>\\|\\(^\\|:\\)japan\\>" iso-2022-jp-2)
873    ("\\(^\\|:\\)tnn\\>\\|\\(^\\|:\\)pin\\>\\|\\(^\\|:\\)sci.lang.japan" iso-2022-7bit)
874    ("\\(^\\|:\\)relcom\\>" koi8-r)
875    ("\\(^\\|:\\)fido7\\>" koi8-r)
876    ("\\(^\\|:\\)\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
877    ("\\(^\\|:\\)israel\\>" iso-8859-1)
878    ("\\(^\\|:\\)han\\>" euc-kr)
879    ("\\(^\\|:\\)alt.chinese.text.big5\\>" chinese-big5)
880    ("\\(^\\|:\\)soc.culture.vietnamese\\>" vietnamese-viqr)
881    ("\\(^\\|:\\)\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
882    (".*" iso-8859-1))
883  :variable-document
884   "Alist of regexps (to match group names) and default charsets to be used when reading."
885   :variable-group gnus-charset
886   :variable-type '(repeat (list (regexp :tag "Group")
887                                 (symbol :tag "Charset")))
888   :parameter-type '(symbol :tag "Charset")
889   :parameter-document "\
890 The default charset to use in the group.")
891
892 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
893   "List of charsets that should be ignored.
894 When these charsets are used in the \"charset\" parameter, the
895 default charset will be used instead."
896   :version "21.1"
897   :type '(repeat symbol)
898   :group 'gnus-charset)
899
900 (gnus-define-group-parameter
901  ignored-charsets
902  :type list
903  :function-document
904  "Return the ignored charsets of GROUP."
905  :variable gnus-group-ignored-charsets-alist
906  :variable-default 
907  '(("alt\\.chinese\\.text" iso-8859-1))
908  :variable-document
909  "Alist of regexps (to match group names) and charsets that should be ignored.
910 When these charsets are used in the \"charset\" parameter, the
911 default charset will be used instead."
912  :variable-group gnus-charset
913  :variable-type '(repeat (cons (regexp :tag "Group")
914                                (repeat symbol)))
915  :parameter-type '(choice :tag "Ignored charsets" 
916                           :value nil
917                           (repeat (symbol)))
918  :parameter-document       "\
919 List of charsets that should be ignored.
920
921 When these charsets are used in the \"charset\" parameter, the
922 default charset will be used instead.")
923
924 (defcustom gnus-group-highlight-words-alist nil
925   "Alist of group regexps and highlight regexps.
926 This variable uses the same syntax as `gnus-emphasis-alist'."
927   :version "21.1"
928   :type '(repeat (cons (regexp :tag "Group")
929                        (repeat (list (regexp :tag "Highlight regexp")
930                                      (number :tag "Group for entire word" 0)
931                                      (number :tag "Group for displayed part" 0)
932                                      (symbol :tag "Face"
933                                              gnus-emphasis-highlight-words)))))
934   :group 'gnus-summary-visual)
935
936 (defcustom gnus-summary-show-article-charset-alist
937   nil
938   "Alist of number and charset.
939 The article will be shown with the charset corresponding to the
940 numbered argument.
941 For example: ((1 . cn-gb-2312) (2 . big5))."
942   :version "21.1"
943   :type '(repeat (cons (number :tag "Argument" 1)
944                        (symbol :tag "Charset")))
945   :group 'gnus-charset)
946
947 (defcustom gnus-preserve-marks t
948   "Whether marks are preserved when moving, copying and respooling messages."
949   :version "21.1"
950   :type 'boolean
951   :group 'gnus-summary-marks)
952
953 (defcustom gnus-alter-articles-to-read-function nil
954   "Function to be called to alter the list of articles to be selected."
955   :type 'function
956   :group 'gnus-summary)
957
958 (defcustom gnus-orphan-score nil
959   "*All orphans get this score added.  Set in the score file."
960   :group 'gnus-score-default
961   :type '(choice (const nil)
962                  integer))
963
964 (defcustom gnus-summary-save-parts-default-mime "image/.*"
965   "*A regexp to match MIME parts when saving multiple parts of a message
966 with gnus-summary-save-parts (X m). This regexp will be used by default
967 when prompting the user for which type of files to save."
968   :group 'gnus-summary
969   :type 'regexp)
970
971
972 (defcustom gnus-summary-save-parts-default-mime "image/.*"
973   "*A regexp to match MIME parts when saving multiple parts of a message
974 with gnus-summary-save-parts (X m). This regexp will be used by default
975 when prompting the user for which type of files to save."
976   :group 'gnus-summary
977   :type 'regexp)
978
979
980 ;;; Internal variables
981
982 (defvar gnus-article-mime-handles nil)
983 (defvar gnus-article-decoded-p nil)
984 (defvar gnus-article-charset nil)
985 (defvar gnus-article-ignored-charsets nil)
986 (defvar gnus-scores-exclude-files nil)
987 (defvar gnus-page-broken nil)
988 (defvar gnus-inhibit-mime-unbuttonizing nil)
989
990 (defvar gnus-original-article nil)
991 (defvar gnus-article-internal-prepare-hook nil)
992 (defvar gnus-newsgroup-process-stack nil)
993
994 (defvar gnus-thread-indent-array nil)
995 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
996 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
997   "Function called to sort the articles within a thread after it has been gathered together.")
998
999 (defvar gnus-summary-save-parts-type-history nil)
1000 (defvar gnus-summary-save-parts-last-directory nil)
1001
1002 (defvar gnus-summary-save-parts-type-history nil)
1003 (defvar gnus-summary-save-parts-last-directory nil)
1004
1005 ;; Avoid highlighting in kill files.
1006 (defvar gnus-summary-inhibit-highlight nil)
1007 (defvar gnus-newsgroup-selected-overlay nil)
1008 (defvar gnus-inhibit-limiting nil)
1009 (defvar gnus-newsgroup-adaptive-score-file nil)
1010 (defvar gnus-current-score-file nil)
1011 (defvar gnus-current-move-group nil)
1012 (defvar gnus-current-copy-group nil)
1013 (defvar gnus-current-crosspost-group nil)
1014
1015 (defvar gnus-newsgroup-dependencies nil)
1016 (defvar gnus-newsgroup-adaptive nil)
1017 (defvar gnus-summary-display-article-function nil)
1018 (defvar gnus-summary-highlight-line-function nil
1019   "Function called after highlighting a summary line.")
1020
1021 (defvar gnus-summary-line-format-alist
1022   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1023     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1024     (?s gnus-tmp-subject-or-nil ?s)
1025     (?n gnus-tmp-name ?s)
1026     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1027         ?s)
1028     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1029             gnus-tmp-from) ?s)
1030     (?F gnus-tmp-from ?s)
1031     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1032     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1033     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1034     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1035     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1036     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1037     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1038     (?L gnus-tmp-lines ?d)
1039     (?I gnus-tmp-indentation ?s)
1040     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1041     (?R gnus-tmp-replied ?c)
1042     (?\[ gnus-tmp-opening-bracket ?c)
1043     (?\] gnus-tmp-closing-bracket ?c)
1044     (?\> (make-string gnus-tmp-level ? ) ?s)
1045     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1046     (?i gnus-tmp-score ?d)
1047     (?z gnus-tmp-score-char ?c)
1048     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1049     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1050     (?U gnus-tmp-unread ?c)
1051     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1052     (?t (gnus-summary-number-of-articles-in-thread
1053          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1054         ?d)
1055     (?e (gnus-summary-number-of-articles-in-thread
1056          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1057         ?c)
1058     (?u gnus-tmp-user-defined ?s)
1059     (?P (gnus-pick-line-number) ?d))
1060   "An alist of format specifications that can appear in summary lines.
1061 These are paired with what variables they correspond with, along with
1062 the type of the variable (string, integer, character, etc).")
1063
1064 (defvar gnus-summary-dummy-line-format-alist
1065   `((?S gnus-tmp-subject ?s)
1066     (?N gnus-tmp-number ?d)
1067     (?u gnus-tmp-user-defined ?s)))
1068
1069 (defvar gnus-summary-mode-line-format-alist
1070   `((?G gnus-tmp-group-name ?s)
1071     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1072     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1073     (?A gnus-tmp-article-number ?d)
1074     (?Z gnus-tmp-unread-and-unselected ?s)
1075     (?V gnus-version ?s)
1076     (?U gnus-tmp-unread-and-unticked ?d)
1077     (?S gnus-tmp-subject ?s)
1078     (?e gnus-tmp-unselected ?d)
1079     (?u gnus-tmp-user-defined ?s)
1080     (?d (length gnus-newsgroup-dormant) ?d)
1081     (?t (length gnus-newsgroup-marked) ?d)
1082     (?r (length gnus-newsgroup-reads) ?d)
1083     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1084     (?E gnus-newsgroup-expunged-tally ?d)
1085     (?s (gnus-current-score-file-nondirectory) ?s)))
1086
1087 (defvar gnus-last-search-regexp nil
1088   "Default regexp for article search command.")
1089
1090 (defvar gnus-last-shell-command nil
1091   "Default shell command on article.")
1092
1093 (defvar gnus-newsgroup-begin nil)
1094 (defvar gnus-newsgroup-end nil)
1095 (defvar gnus-newsgroup-last-rmail nil)
1096 (defvar gnus-newsgroup-last-mail nil)
1097 (defvar gnus-newsgroup-last-folder nil)
1098 (defvar gnus-newsgroup-last-file nil)
1099 (defvar gnus-newsgroup-auto-expire nil)
1100 (defvar gnus-newsgroup-active nil)
1101
1102 (defvar gnus-newsgroup-data nil)
1103 (defvar gnus-newsgroup-data-reverse nil)
1104 (defvar gnus-newsgroup-limit nil)
1105 (defvar gnus-newsgroup-limits nil)
1106
1107 (defvar gnus-newsgroup-unreads nil
1108   "List of unread articles in the current newsgroup.")
1109
1110 (defvar gnus-newsgroup-unselected nil
1111   "List of unselected unread articles in the current newsgroup.")
1112
1113 (defvar gnus-newsgroup-reads nil
1114   "Alist of read articles and article marks in the current newsgroup.")
1115
1116 (defvar gnus-newsgroup-expunged-tally nil)
1117
1118 (defvar gnus-newsgroup-marked nil
1119   "List of ticked articles in the current newsgroup (a subset of unread art).")
1120
1121 (defvar gnus-newsgroup-killed nil
1122   "List of ranges of articles that have been through the scoring process.")
1123
1124 (defvar gnus-newsgroup-cached nil
1125   "List of articles that come from the article cache.")
1126
1127 (defvar gnus-newsgroup-saved nil
1128   "List of articles that have been saved.")
1129
1130 (defvar gnus-newsgroup-kill-headers nil)
1131
1132 (defvar gnus-newsgroup-replied nil
1133   "List of articles that have been replied to in the current newsgroup.")
1134
1135 (defvar gnus-newsgroup-expirable nil
1136   "List of articles in the current newsgroup that can be expired.")
1137
1138 (defvar gnus-newsgroup-processable nil
1139   "List of articles in the current newsgroup that can be processed.")
1140
1141 (defvar gnus-newsgroup-downloadable nil
1142   "List of articles in the current newsgroup that can be processed.")
1143
1144 (defvar gnus-newsgroup-undownloaded nil
1145   "List of articles in the current newsgroup that haven't been downloaded..")
1146
1147 (defvar gnus-newsgroup-unsendable nil
1148   "List of articles in the current newsgroup that won't be sent.")
1149
1150 (defvar gnus-newsgroup-bookmarks nil
1151   "List of articles in the current newsgroup that have bookmarks.")
1152
1153 (defvar gnus-newsgroup-dormant nil
1154   "List of dormant articles in the current newsgroup.")
1155
1156 (defvar gnus-newsgroup-scored nil
1157   "List of scored articles in the current newsgroup.")
1158
1159 (defvar gnus-newsgroup-headers nil
1160   "List of article headers in the current newsgroup.")
1161
1162 (defvar gnus-newsgroup-threads nil)
1163
1164 (defvar gnus-newsgroup-prepared nil
1165   "Whether the current group has been prepared properly.")
1166
1167 (defvar gnus-newsgroup-ancient nil
1168   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1169
1170 (defvar gnus-newsgroup-sparse nil)
1171
1172 (defvar gnus-current-article nil)
1173 (defvar gnus-article-current nil)
1174 (defvar gnus-current-headers nil)
1175 (defvar gnus-have-all-headers nil)
1176 (defvar gnus-last-article nil)
1177 (defvar gnus-newsgroup-history nil)
1178 (defvar gnus-newsgroup-charset nil)
1179 (defvar gnus-newsgroup-ephemeral-charset nil)
1180 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1181
1182 (defvar gnus-article-before-search nil)
1183
1184 (defconst gnus-summary-local-variables
1185   '(gnus-newsgroup-name
1186     gnus-newsgroup-begin gnus-newsgroup-end
1187     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1188     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1189     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1190     gnus-newsgroup-unselected gnus-newsgroup-marked
1191     gnus-newsgroup-reads gnus-newsgroup-saved
1192     gnus-newsgroup-replied gnus-newsgroup-expirable
1193     gnus-newsgroup-processable gnus-newsgroup-killed
1194     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1195     gnus-newsgroup-unsendable
1196     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1197     gnus-newsgroup-headers gnus-newsgroup-threads
1198     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1199     gnus-current-article gnus-current-headers gnus-have-all-headers
1200     gnus-last-article gnus-article-internal-prepare-hook
1201     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1202     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1203     gnus-thread-expunge-below
1204     gnus-score-alist gnus-current-score-file
1205     (gnus-summary-expunge-below . global)
1206     (gnus-summary-mark-below . global)
1207     (gnus-orphan-score . global)
1208     gnus-newsgroup-active gnus-scores-exclude-files
1209     gnus-newsgroup-history gnus-newsgroup-ancient
1210     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1211     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1212     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1213     (gnus-newsgroup-expunged-tally . 0)
1214     gnus-cache-removable-articles gnus-newsgroup-cached
1215     gnus-newsgroup-data gnus-newsgroup-data-reverse
1216     gnus-newsgroup-limit gnus-newsgroup-limits
1217     gnus-newsgroup-charset)
1218   "Variables that are buffer-local to the summary buffers.")
1219
1220 (defvar gnus-newsgroup-variables nil
1221   "Variables that have separate values in the newsgroups.")
1222
1223 ;; Byte-compiler warning.
1224 (eval-when-compile (defvar gnus-article-mode-map))
1225
1226 ;; MIME stuff.
1227
1228 (defvar gnus-decode-encoded-word-methods
1229   '(mail-decode-encoded-word-string)
1230   "List of methods used to decode encoded words.
1231
1232 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1233 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1234 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1235 whose names match REGEXP.
1236
1237 For example:
1238 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1239  mail-decode-encoded-word-string
1240  (\"chinese\" . rfc1843-decode-string))")
1241
1242 (defvar gnus-decode-encoded-word-methods-cache nil)
1243
1244 (defun gnus-multi-decode-encoded-word-string (string)
1245   "Apply the functions from `gnus-encoded-word-methods' that match."
1246   (unless (and gnus-decode-encoded-word-methods-cache
1247                (eq gnus-newsgroup-name
1248                    (car gnus-decode-encoded-word-methods-cache)))
1249     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1250     (mapcar (lambda (x)
1251               (if (symbolp x)
1252                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1253                 (if (and gnus-newsgroup-name
1254                          (string-match (car x) gnus-newsgroup-name))
1255                     (nconc gnus-decode-encoded-word-methods-cache
1256                            (list (cdr x))))))
1257           gnus-decode-encoded-word-methods))
1258   (let ((xlist gnus-decode-encoded-word-methods-cache))
1259     (pop xlist)
1260     (while xlist
1261       (setq string (funcall (pop xlist) string))))
1262   string)
1263
1264 ;; Subject simplification.
1265
1266 (defun gnus-simplify-whitespace (str)
1267   "Remove excessive whitespace from STR."
1268   (let ((mystr str))
1269     ;; Multiple spaces.
1270     (while (string-match "[ \t][ \t]+" mystr)
1271       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1272                           " "
1273                           (substring mystr (match-end 0)))))
1274     ;; Leading spaces.
1275     (when (string-match "^[ \t]+" mystr)
1276       (setq mystr (substring mystr (match-end 0))))
1277     ;; Trailing spaces.
1278     (when (string-match "[ \t]+$" mystr)
1279       (setq mystr (substring mystr 0 (match-beginning 0))))
1280     mystr))
1281
1282 (defsubst gnus-simplify-subject-re (subject)
1283   "Remove \"Re:\" from subject lines."
1284   (if (string-match message-subject-re-regexp subject)
1285       (substring subject (match-end 0))
1286     subject))
1287
1288 (defun gnus-simplify-subject (subject &optional re-only)
1289   "Remove `Re:' and words in parentheses.
1290 If RE-ONLY is non-nil, strip leading `Re:'s only."
1291   (let ((case-fold-search t))           ;Ignore case.
1292     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1293     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1294       (setq subject (substring subject (match-end 0))))
1295     ;; Remove uninteresting prefixes.
1296     (when (and (not re-only)
1297                gnus-simplify-ignored-prefixes
1298                (string-match gnus-simplify-ignored-prefixes subject))
1299       (setq subject (substring subject (match-end 0))))
1300     ;; Remove words in parentheses from end.
1301     (unless re-only
1302       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1303         (setq subject (substring subject 0 (match-beginning 0)))))
1304     ;; Return subject string.
1305     subject))
1306
1307 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1308 ;; all whitespace.
1309 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1310   (goto-char (point-min))
1311   (while (re-search-forward regexp nil t)
1312     (replace-match (or newtext ""))))
1313
1314 (defun gnus-simplify-buffer-fuzzy ()
1315   "Simplify string in the buffer fuzzily.
1316 The string in the accessible portion of the current buffer is simplified.
1317 It is assumed to be a single-line subject.
1318 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1319 matter is removed.  Additional things can be deleted by setting
1320 `gnus-simplify-subject-fuzzy-regexp'."
1321   (let ((case-fold-search t)
1322         (modified-tick))
1323     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1324
1325     (while (not (eq modified-tick (buffer-modified-tick)))
1326       (setq modified-tick (buffer-modified-tick))
1327       (cond
1328        ((listp gnus-simplify-subject-fuzzy-regexp)
1329         (mapcar 'gnus-simplify-buffer-fuzzy-step
1330                 gnus-simplify-subject-fuzzy-regexp))
1331        (gnus-simplify-subject-fuzzy-regexp
1332         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1333       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1334       (gnus-simplify-buffer-fuzzy-step
1335        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1336       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1337
1338     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1339     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1340     (gnus-simplify-buffer-fuzzy-step " $")
1341     (gnus-simplify-buffer-fuzzy-step "^ +")))
1342
1343 (defun gnus-simplify-subject-fuzzy (subject)
1344   "Simplify a subject string fuzzily.
1345 See `gnus-simplify-buffer-fuzzy' for details."
1346   (save-excursion
1347     (gnus-set-work-buffer)
1348     (let ((case-fold-search t))
1349       ;; Remove uninteresting prefixes.
1350       (when (and gnus-simplify-ignored-prefixes
1351                  (string-match gnus-simplify-ignored-prefixes subject))
1352         (setq subject (substring subject (match-end 0))))
1353       (insert subject)
1354       (inline (gnus-simplify-buffer-fuzzy))
1355       (buffer-string))))
1356
1357 (defsubst gnus-simplify-subject-fully (subject)
1358   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1359   (cond
1360    (gnus-simplify-subject-functions
1361     (gnus-map-function gnus-simplify-subject-functions subject))
1362    ((null gnus-summary-gather-subject-limit)
1363     (gnus-simplify-subject-re subject))
1364    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1365     (gnus-simplify-subject-fuzzy subject))
1366    ((numberp gnus-summary-gather-subject-limit)
1367     (gnus-limit-string (gnus-simplify-subject-re subject)
1368                        gnus-summary-gather-subject-limit))
1369    (t
1370     subject)))
1371
1372 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1373   "Check whether two subjects are equal.
1374 If optional argument simple-first is t, first argument is already
1375 simplified."
1376   (cond
1377    ((null simple-first)
1378     (equal (gnus-simplify-subject-fully s1)
1379            (gnus-simplify-subject-fully s2)))
1380    (t
1381     (equal s1
1382            (gnus-simplify-subject-fully s2)))))
1383
1384 (defun gnus-summary-bubble-group ()
1385   "Increase the score of the current group.
1386 This is a handy function to add to `gnus-summary-exit-hook' to
1387 increase the score of each group you read."
1388   (gnus-group-add-score gnus-newsgroup-name))
1389
1390 \f
1391 ;;;
1392 ;;; Gnus summary mode
1393 ;;;
1394
1395 (put 'gnus-summary-mode 'mode-class 'special)
1396
1397 (defvar gnus-article-commands-menu)
1398
1399 (when t
1400   ;; Non-orthogonal keys
1401
1402   (gnus-define-keys gnus-summary-mode-map
1403     " " gnus-summary-next-page
1404     "\177" gnus-summary-prev-page
1405     [delete] gnus-summary-prev-page
1406     [backspace] gnus-summary-prev-page
1407     "\r" gnus-summary-scroll-up
1408     "\M-\r" gnus-summary-scroll-down
1409     "n" gnus-summary-next-unread-article
1410     "p" gnus-summary-prev-unread-article
1411     "N" gnus-summary-next-article
1412     "P" gnus-summary-prev-article
1413     "\M-\C-n" gnus-summary-next-same-subject
1414     "\M-\C-p" gnus-summary-prev-same-subject
1415     "\M-n" gnus-summary-next-unread-subject
1416     "\M-p" gnus-summary-prev-unread-subject
1417     "." gnus-summary-first-unread-article
1418     "," gnus-summary-best-unread-article
1419     "\M-s" gnus-summary-search-article-forward
1420     "\M-r" gnus-summary-search-article-backward
1421     "<" gnus-summary-beginning-of-article
1422     ">" gnus-summary-end-of-article
1423     "j" gnus-summary-goto-article
1424     "^" gnus-summary-refer-parent-article
1425     "\M-^" gnus-summary-refer-article
1426     "u" gnus-summary-tick-article-forward
1427     "!" gnus-summary-tick-article-forward
1428     "U" gnus-summary-tick-article-backward
1429     "d" gnus-summary-mark-as-read-forward
1430     "D" gnus-summary-mark-as-read-backward
1431     "E" gnus-summary-mark-as-expirable
1432     "\M-u" gnus-summary-clear-mark-forward
1433     "\M-U" gnus-summary-clear-mark-backward
1434     "k" gnus-summary-kill-same-subject-and-select
1435     "\C-k" gnus-summary-kill-same-subject
1436     "\M-\C-k" gnus-summary-kill-thread
1437     "\M-\C-l" gnus-summary-lower-thread
1438     "e" gnus-summary-edit-article
1439     "#" gnus-summary-mark-as-processable
1440     "\M-#" gnus-summary-unmark-as-processable
1441     "\M-\C-t" gnus-summary-toggle-threads
1442     "\M-\C-s" gnus-summary-show-thread
1443     "\M-\C-h" gnus-summary-hide-thread
1444     "\M-\C-f" gnus-summary-next-thread
1445     "\M-\C-b" gnus-summary-prev-thread
1446     [(meta down)] gnus-summary-next-thread
1447     [(meta up)] gnus-summary-prev-thread
1448     "\M-\C-u" gnus-summary-up-thread
1449     "\M-\C-d" gnus-summary-down-thread
1450     "&" gnus-summary-execute-command
1451     "c" gnus-summary-catchup-and-exit
1452     "\C-w" gnus-summary-mark-region-as-read
1453     "\C-t" gnus-summary-toggle-truncation
1454     "?" gnus-summary-mark-as-dormant
1455     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1456     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1457     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1458     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1459     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1460     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1461     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1462     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1463     "=" gnus-summary-expand-window
1464     "\C-x\C-s" gnus-summary-reselect-current-group
1465     "\M-g" gnus-summary-rescan-group
1466     "w" gnus-summary-stop-page-breaking
1467     "\C-c\C-r" gnus-summary-caesar-message
1468     "f" gnus-summary-followup
1469     "F" gnus-summary-followup-with-original
1470     "C" gnus-summary-cancel-article
1471     "r" gnus-summary-reply
1472     "R" gnus-summary-reply-with-original
1473     "\C-c\C-f" gnus-summary-mail-forward
1474     "o" gnus-summary-save-article
1475     "\C-o" gnus-summary-save-article-mail
1476     "|" gnus-summary-pipe-output
1477     "\M-k" gnus-summary-edit-local-kill
1478     "\M-K" gnus-summary-edit-global-kill
1479     ;; "V" gnus-version
1480     "\C-c\C-d" gnus-summary-describe-group
1481     "q" gnus-summary-exit
1482     "Q" gnus-summary-exit-no-update
1483     "\C-c\C-i" gnus-info-find-node
1484     gnus-mouse-2 gnus-mouse-pick-article
1485     "m" gnus-summary-mail-other-window
1486     "a" gnus-summary-post-news
1487     "x" gnus-summary-limit-to-unread
1488     "s" gnus-summary-isearch-article
1489     "t" gnus-summary-toggle-header
1490     "g" gnus-summary-show-article
1491     "l" gnus-summary-goto-last-article
1492     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1493     "\C-d" gnus-summary-enter-digest-group
1494     "\M-\C-d" gnus-summary-read-document
1495     "\M-\C-e" gnus-summary-edit-parameters
1496     "\M-\C-a" gnus-summary-customize-parameters
1497     "\C-c\C-b" gnus-bug
1498     "*" gnus-cache-enter-article
1499     "\M-*" gnus-cache-remove-article
1500     "\M-&" gnus-summary-universal-argument
1501     "\C-l" gnus-recenter
1502     "I" gnus-summary-increase-score
1503     "L" gnus-summary-lower-score
1504     "\M-i" gnus-symbolic-argument
1505     "h" gnus-summary-select-article-buffer
1506
1507     "b" gnus-article-view-part
1508     "\M-t" gnus-summary-toggle-display-buttonized
1509
1510     "V" gnus-summary-score-map
1511     "X" gnus-uu-extract-map
1512     "S" gnus-summary-send-map)
1513
1514   ;; Sort of orthogonal keymap
1515   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1516     "t" gnus-summary-tick-article-forward
1517     "!" gnus-summary-tick-article-forward
1518     "d" gnus-summary-mark-as-read-forward
1519     "r" gnus-summary-mark-as-read-forward
1520     "c" gnus-summary-clear-mark-forward
1521     " " gnus-summary-clear-mark-forward
1522     "e" gnus-summary-mark-as-expirable
1523     "x" gnus-summary-mark-as-expirable
1524     "?" gnus-summary-mark-as-dormant
1525     "b" gnus-summary-set-bookmark
1526     "B" gnus-summary-remove-bookmark
1527     "#" gnus-summary-mark-as-processable
1528     "\M-#" gnus-summary-unmark-as-processable
1529     "S" gnus-summary-limit-include-expunged
1530     "C" gnus-summary-catchup
1531     "H" gnus-summary-catchup-to-here
1532     "\C-c" gnus-summary-catchup-all
1533     "k" gnus-summary-kill-same-subject-and-select
1534     "K" gnus-summary-kill-same-subject
1535     "P" gnus-uu-mark-map)
1536
1537   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1538     "c" gnus-summary-clear-above
1539     "u" gnus-summary-tick-above
1540     "m" gnus-summary-mark-above
1541     "k" gnus-summary-kill-below)
1542
1543   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1544     "/" gnus-summary-limit-to-subject
1545     "n" gnus-summary-limit-to-articles
1546     "w" gnus-summary-pop-limit
1547     "s" gnus-summary-limit-to-subject
1548     "a" gnus-summary-limit-to-author
1549     "u" gnus-summary-limit-to-unread
1550     "m" gnus-summary-limit-to-marks
1551     "M" gnus-summary-limit-exclude-marks
1552     "v" gnus-summary-limit-to-score
1553     "*" gnus-summary-limit-include-cached
1554     "D" gnus-summary-limit-include-dormant
1555     "T" gnus-summary-limit-include-thread
1556     "d" gnus-summary-limit-exclude-dormant
1557     "t" gnus-summary-limit-to-age
1558     "x" gnus-summary-limit-to-extra
1559     "E" gnus-summary-limit-include-expunged
1560     "c" gnus-summary-limit-exclude-childless-dormant
1561     "C" gnus-summary-limit-mark-excluded-as-read
1562     "o" gnus-summary-insert-old-articles
1563     "N" gnus-summary-insert-new-articles)
1564
1565   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1566     "n" gnus-summary-next-unread-article
1567     "p" gnus-summary-prev-unread-article
1568     "N" gnus-summary-next-article
1569     "P" gnus-summary-prev-article
1570     "\C-n" gnus-summary-next-same-subject
1571     "\C-p" gnus-summary-prev-same-subject
1572     "\M-n" gnus-summary-next-unread-subject
1573     "\M-p" gnus-summary-prev-unread-subject
1574     "f" gnus-summary-first-unread-article
1575     "b" gnus-summary-best-unread-article
1576     "j" gnus-summary-goto-article
1577     "g" gnus-summary-goto-subject
1578     "l" gnus-summary-goto-last-article
1579     "o" gnus-summary-pop-article)
1580
1581   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1582     "k" gnus-summary-kill-thread
1583     "l" gnus-summary-lower-thread
1584     "i" gnus-summary-raise-thread
1585     "T" gnus-summary-toggle-threads
1586     "t" gnus-summary-rethread-current
1587     "^" gnus-summary-reparent-thread
1588     "s" gnus-summary-show-thread
1589     "S" gnus-summary-show-all-threads
1590     "h" gnus-summary-hide-thread
1591     "H" gnus-summary-hide-all-threads
1592     "n" gnus-summary-next-thread
1593     "p" gnus-summary-prev-thread
1594     "u" gnus-summary-up-thread
1595     "o" gnus-summary-top-thread
1596     "d" gnus-summary-down-thread
1597     "#" gnus-uu-mark-thread
1598     "\M-#" gnus-uu-unmark-thread)
1599
1600   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1601     "g" gnus-summary-prepare
1602     "c" gnus-summary-insert-cached-articles)
1603
1604   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1605     "c" gnus-summary-catchup-and-exit
1606     "C" gnus-summary-catchup-all-and-exit
1607     "E" gnus-summary-exit-no-update
1608     "Q" gnus-summary-exit
1609     "Z" gnus-summary-exit
1610     "n" gnus-summary-catchup-and-goto-next-group
1611     "R" gnus-summary-reselect-current-group
1612     "G" gnus-summary-rescan-group
1613     "N" gnus-summary-next-group
1614     "s" gnus-summary-save-newsrc
1615     "P" gnus-summary-prev-group)
1616
1617   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1618     " " gnus-summary-next-page
1619     "n" gnus-summary-next-page
1620     "\177" gnus-summary-prev-page
1621     [delete] gnus-summary-prev-page
1622     "p" gnus-summary-prev-page
1623     "\r" gnus-summary-scroll-up
1624     "\M-\r" gnus-summary-scroll-down
1625     "<" gnus-summary-beginning-of-article
1626     ">" gnus-summary-end-of-article
1627     "b" gnus-summary-beginning-of-article
1628     "e" gnus-summary-end-of-article
1629     "^" gnus-summary-refer-parent-article
1630     "r" gnus-summary-refer-parent-article
1631     "D" gnus-summary-enter-digest-group
1632     "R" gnus-summary-refer-references
1633     "T" gnus-summary-refer-thread
1634     "g" gnus-summary-show-article
1635     "s" gnus-summary-isearch-article
1636     "P" gnus-summary-print-article
1637     "M" gnus-mailing-list-insinuate
1638     "t" gnus-article-babel)
1639
1640   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1641     "b" gnus-article-add-buttons
1642     "B" gnus-article-add-buttons-to-head
1643     "o" gnus-article-treat-overstrike
1644     "e" gnus-article-emphasize
1645     "w" gnus-article-fill-cited-article
1646     "Q" gnus-article-fill-long-lines
1647     "C" gnus-article-capitalize-sentences
1648     "c" gnus-article-remove-cr
1649     "q" gnus-article-de-quoted-unreadable
1650     "6" gnus-article-de-base64-unreadable
1651     "Z" gnus-article-decode-HZ
1652     "h" gnus-article-wash-html
1653     "s" gnus-summary-force-verify-and-decrypt
1654     "f" gnus-article-display-x-face
1655     "l" gnus-summary-stop-page-breaking
1656     "r" gnus-summary-caesar-message
1657     "t" gnus-summary-toggle-header
1658     "v" gnus-summary-verbose-headers
1659     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1660     "p" gnus-article-verify-x-pgp-sig
1661     "d" gnus-article-treat-dumbquotes)
1662
1663   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1664     "a" gnus-article-hide
1665     "h" gnus-article-hide-headers
1666     "b" gnus-article-hide-boring-headers
1667     "s" gnus-article-hide-signature
1668     "c" gnus-article-hide-citation
1669     "C" gnus-article-hide-citation-in-followups
1670     "l" gnus-article-hide-list-identifiers
1671     "p" gnus-article-hide-pgp
1672     "B" gnus-article-strip-banner
1673     "P" gnus-article-hide-pem
1674     "\C-c" gnus-article-hide-citation-maybe)
1675
1676   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1677     "a" gnus-article-highlight
1678     "h" gnus-article-highlight-headers
1679     "c" gnus-article-highlight-citation
1680     "s" gnus-article-highlight-signature)
1681
1682   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1683     "w" gnus-article-decode-mime-words
1684     "c" gnus-article-decode-charset
1685     "v" gnus-mime-view-all-parts
1686     "b" gnus-article-view-part)
1687
1688   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1689     "z" gnus-article-date-ut
1690     "u" gnus-article-date-ut
1691     "l" gnus-article-date-local
1692     "p" gnus-article-date-english
1693     "e" gnus-article-date-lapsed
1694     "o" gnus-article-date-original
1695     "i" gnus-article-date-iso8601
1696     "s" gnus-article-date-user)
1697
1698   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1699     "t" gnus-article-remove-trailing-blank-lines
1700     "l" gnus-article-strip-leading-blank-lines
1701     "m" gnus-article-strip-multiple-blank-lines
1702     "a" gnus-article-strip-blank-lines
1703     "A" gnus-article-strip-all-blank-lines
1704     "s" gnus-article-strip-leading-space
1705     "e" gnus-article-strip-trailing-space
1706     "w" gnus-article-remove-leading-whitespace)
1707
1708   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1709     "v" gnus-version
1710     "f" gnus-summary-fetch-faq
1711     "d" gnus-summary-describe-group
1712     "h" gnus-summary-describe-briefly
1713     "i" gnus-info-find-node)
1714
1715   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1716     "e" gnus-summary-expire-articles
1717     "\M-\C-e" gnus-summary-expire-articles-now
1718     "\177" gnus-summary-delete-article
1719     [delete] gnus-summary-delete-article
1720     [backspace] gnus-summary-delete-article
1721     "m" gnus-summary-move-article
1722     "r" gnus-summary-respool-article
1723     "w" gnus-summary-edit-article
1724     "c" gnus-summary-copy-article
1725     "B" gnus-summary-crosspost-article
1726     "q" gnus-summary-respool-query
1727     "t" gnus-summary-respool-trace
1728     "i" gnus-summary-import-article
1729     "p" gnus-summary-article-posted-p)
1730
1731   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1732     "o" gnus-summary-save-article
1733     "m" gnus-summary-save-article-mail
1734     "F" gnus-summary-write-article-file
1735     "r" gnus-summary-save-article-rmail
1736     "f" gnus-summary-save-article-file
1737     "b" gnus-summary-save-article-body-file
1738     "h" gnus-summary-save-article-folder
1739     "v" gnus-summary-save-article-vm
1740     "p" gnus-summary-pipe-output
1741     "s" gnus-soup-add-article)
1742
1743   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1744     "b" gnus-summary-display-buttonized
1745     "m" gnus-summary-repair-multipart
1746     "v" gnus-article-view-part
1747     "o" gnus-article-save-part
1748     "c" gnus-article-copy-part
1749     "C" gnus-article-view-part-as-charset
1750     "e" gnus-article-externalize-part
1751     "E" gnus-article-encrypt-body
1752     "i" gnus-article-inline-part
1753     "|" gnus-article-pipe-part))
1754
1755 (defun gnus-summary-make-menu-bar ()
1756   (gnus-turn-off-edit-menu 'summary)
1757
1758   (unless (boundp 'gnus-summary-misc-menu)
1759
1760     (easy-menu-define
1761      gnus-summary-kill-menu gnus-summary-mode-map ""
1762      (cons
1763       "Score"
1764       (nconc
1765        (list
1766         ["Customize" gnus-score-customize t])
1767        (gnus-make-score-map 'increase)
1768        (gnus-make-score-map 'lower)
1769        '(("Mark"
1770           ["Kill below" gnus-summary-kill-below t]
1771           ["Mark above" gnus-summary-mark-above t]
1772           ["Tick above" gnus-summary-tick-above t]
1773           ["Clear above" gnus-summary-clear-above t])
1774          ["Current score" gnus-summary-current-score t]
1775          ["Set score" gnus-summary-set-score t]
1776          ["Switch current score file..." gnus-score-change-score-file t]
1777          ["Set mark below..." gnus-score-set-mark-below t]
1778          ["Set expunge below..." gnus-score-set-expunge-below t]
1779          ["Edit current score file" gnus-score-edit-current-scores t]
1780          ["Edit score file" gnus-score-edit-file t]
1781          ["Trace score" gnus-score-find-trace t]
1782          ["Find words" gnus-score-find-favourite-words t]
1783          ["Rescore buffer" gnus-summary-rescore t]
1784          ["Increase score..." gnus-summary-increase-score t]
1785          ["Lower score..." gnus-summary-lower-score t]))))
1786
1787     ;; Define both the Article menu in the summary buffer and the equivalent
1788     ;; Commands menu in the article buffer here for consistency.
1789     (let ((innards
1790            `(("Hide"
1791               ["All" gnus-article-hide t]
1792               ["Headers" gnus-article-hide-headers t]
1793               ["Signature" gnus-article-hide-signature t]
1794               ["Citation" gnus-article-hide-citation t]
1795               ["List identifiers" gnus-article-hide-list-identifiers t]
1796               ["PGP" gnus-article-hide-pgp t]
1797               ["Banner" gnus-article-strip-banner t]
1798               ["Boring headers" gnus-article-hide-boring-headers t])
1799              ("Highlight"
1800               ["All" gnus-article-highlight t]
1801               ["Headers" gnus-article-highlight-headers t]
1802               ["Signature" gnus-article-highlight-signature t]
1803               ["Citation" gnus-article-highlight-citation t])
1804              ("MIME"
1805               ["Words" gnus-article-decode-mime-words t]
1806               ["Charset" gnus-article-decode-charset t]
1807               ["QP" gnus-article-de-quoted-unreadable t]
1808               ["Base64" gnus-article-de-base64-unreadable t]
1809               ["View all" gnus-mime-view-all-parts t]
1810               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1811               ["Encrypt body" gnus-article-encrypt-body t])
1812              ("Date"
1813               ["Local" gnus-article-date-local t]
1814               ["ISO8601" gnus-article-date-iso8601 t]
1815               ["UT" gnus-article-date-ut t]
1816               ["Original" gnus-article-date-original t]
1817               ["Lapsed" gnus-article-date-lapsed t]
1818               ["User-defined" gnus-article-date-user t])
1819              ("Washing"
1820               ("Remove Blanks"
1821                ["Leading" gnus-article-strip-leading-blank-lines t]
1822                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1823                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1824                ["All of the above" gnus-article-strip-blank-lines t]
1825                ["All" gnus-article-strip-all-blank-lines t]
1826                ["Leading space" gnus-article-strip-leading-space t]
1827                ["Trailing space" gnus-article-strip-trailing-space t]
1828                ["Leading space in headers" 
1829                 gnus-article-remove-leading-whitespace t])
1830               ["Overstrike" gnus-article-treat-overstrike t]
1831               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1832               ["Emphasis" gnus-article-emphasize t]
1833               ["Word wrap" gnus-article-fill-cited-article t]
1834               ["Fill long lines" gnus-article-fill-long-lines t]
1835               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1836               ["CR" gnus-article-remove-cr t]
1837               ["Show X-Face" gnus-article-display-x-face t]
1838               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1839               ["Base64" gnus-article-de-base64-unreadable t]
1840               ["Rot 13" gnus-summary-caesar-message
1841                ,@(if (featurep 'xemacs) '(t)
1842                    '(:help "\"Caesar rotate\" article by 13"))]
1843               ["Unix pipe" gnus-summary-pipe-message t]
1844               ["Add buttons" gnus-article-add-buttons t]
1845               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1846               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1847               ["Verbose header" gnus-summary-verbose-headers t]
1848               ["Toggle header" gnus-summary-toggle-header t]
1849               ["Html" gnus-article-wash-html t]
1850               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1851               ["HZ" gnus-article-decode-HZ t])
1852              ("Output"
1853               ["Save in default format" gnus-summary-save-article
1854                ,@(if (featurep 'xemacs) '(t)
1855                    '(:help "Save article using default method"))]
1856               ["Save in file" gnus-summary-save-article-file
1857                ,@(if (featurep 'xemacs) '(t)
1858                    '(:help "Save article in file"))]
1859               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1860               ["Save in MH folder" gnus-summary-save-article-folder t]
1861               ["Save in VM folder" gnus-summary-save-article-vm t]
1862               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1863               ["Save body in file" gnus-summary-save-article-body-file t]
1864               ["Pipe through a filter" gnus-summary-pipe-output t]
1865               ["Add to SOUP packet" gnus-soup-add-article t]
1866               ["Print" gnus-summary-print-article t])
1867              ("Backend"
1868               ["Respool article..." gnus-summary-respool-article t]
1869               ["Move article..." gnus-summary-move-article
1870                (gnus-check-backend-function
1871                 'request-move-article gnus-newsgroup-name)]
1872               ["Copy article..." gnus-summary-copy-article t]
1873               ["Crosspost article..." gnus-summary-crosspost-article
1874                (gnus-check-backend-function
1875                 'request-replace-article gnus-newsgroup-name)]
1876               ["Import file..." gnus-summary-import-article t]
1877               ["Check if posted" gnus-summary-article-posted-p t]
1878               ["Edit article" gnus-summary-edit-article
1879                (not (gnus-group-read-only-p))]
1880               ["Delete article" gnus-summary-delete-article
1881                (gnus-check-backend-function
1882                 'request-expire-articles gnus-newsgroup-name)]
1883               ["Query respool" gnus-summary-respool-query t]
1884               ["Trace respool" gnus-summary-respool-trace t]
1885               ["Delete expirable articles" gnus-summary-expire-articles-now
1886                (gnus-check-backend-function
1887                 'request-expire-articles gnus-newsgroup-name)])
1888              ("Extract"
1889               ["Uudecode" gnus-uu-decode-uu
1890                ,@(if (featurep 'xemacs) '(t)
1891                    '(:help "Decode uuencoded article(s)"))]
1892               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1893               ["Unshar" gnus-uu-decode-unshar t]
1894               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1895               ["Save" gnus-uu-decode-save t]
1896               ["Binhex" gnus-uu-decode-binhex t]
1897               ["Postscript" gnus-uu-decode-postscript t])
1898              ("Cache"
1899               ["Enter article" gnus-cache-enter-article t]
1900               ["Remove article" gnus-cache-remove-article t])
1901              ["Translate" gnus-article-babel t]
1902              ["Select article buffer" gnus-summary-select-article-buffer t]
1903              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1904              ["Isearch article..." gnus-summary-isearch-article t]
1905              ["Beginning of the article" gnus-summary-beginning-of-article t]
1906              ["End of the article" gnus-summary-end-of-article t]
1907              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1908              ["Fetch referenced articles" gnus-summary-refer-references t]
1909              ["Fetch current thread" gnus-summary-refer-thread t]
1910              ["Fetch article with id..." gnus-summary-refer-article t]
1911              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1912              ["Redisplay" gnus-summary-show-article t])))
1913       (easy-menu-define
1914        gnus-summary-article-menu gnus-summary-mode-map ""
1915        (cons "Article" innards))
1916
1917       (if (not (keymapp gnus-summary-article-menu))
1918           (easy-menu-define
1919             gnus-article-commands-menu gnus-article-mode-map ""
1920             (cons "Commands" innards))
1921         ;; in Emacs, don't share menu.
1922         (setq gnus-article-commands-menu 
1923               (copy-keymap gnus-summary-article-menu))
1924         (define-key gnus-article-mode-map [menu-bar commands]
1925           (cons "Commands" gnus-article-commands-menu))))
1926
1927     (easy-menu-define
1928      gnus-summary-thread-menu gnus-summary-mode-map ""
1929      '("Threads"
1930        ["Toggle threading" gnus-summary-toggle-threads t]
1931        ["Hide threads" gnus-summary-hide-all-threads t]
1932        ["Show threads" gnus-summary-show-all-threads t]
1933        ["Hide thread" gnus-summary-hide-thread t]
1934        ["Show thread" gnus-summary-show-thread t]
1935        ["Go to next thread" gnus-summary-next-thread t]
1936        ["Go to previous thread" gnus-summary-prev-thread t]
1937        ["Go down thread" gnus-summary-down-thread t]
1938        ["Go up thread" gnus-summary-up-thread t]
1939        ["Top of thread" gnus-summary-top-thread t]
1940        ["Mark thread as read" gnus-summary-kill-thread t]
1941        ["Lower thread score" gnus-summary-lower-thread t]
1942        ["Raise thread score" gnus-summary-raise-thread t]
1943        ["Rethread current" gnus-summary-rethread-current t]))
1944
1945     (easy-menu-define
1946      gnus-summary-post-menu gnus-summary-mode-map ""
1947      `("Post"
1948        ["Post an article" gnus-summary-post-news
1949         ,@(if (featurep 'xemacs) '(t)
1950             '(:help "Post an article"))]
1951        ["Followup" gnus-summary-followup
1952         ,@(if (featurep 'xemacs) '(t)
1953             '(:help "Post followup to this article"))]
1954        ["Followup and yank" gnus-summary-followup-with-original
1955         ,@(if (featurep 'xemacs) '(t)
1956             '(:help "Post followup to this article, quoting its contents"))]
1957        ["Supersede article" gnus-summary-supersede-article t]
1958        ["Cancel article" gnus-summary-cancel-article
1959         ,@(if (featurep 'xemacs) '(t)
1960             '(:help "Cancel an article you posted"))]
1961        ["Reply" gnus-summary-reply t]
1962        ["Reply and yank" gnus-summary-reply-with-original t]
1963        ["Wide reply" gnus-summary-wide-reply t]
1964        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1965         ,@(if (featurep 'xemacs) '(t)
1966             '(:help "Mail a reply, quoting this article"))]
1967        ["Mail forward" gnus-summary-mail-forward t]
1968        ["Post forward" gnus-summary-post-forward t]
1969        ["Digest and mail" gnus-uu-digest-mail-forward t]
1970        ["Digest and post" gnus-uu-digest-post-forward t]
1971        ["Resend message" gnus-summary-resend-message t]
1972        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1973        ["Send a mail" gnus-summary-mail-other-window t]
1974        ["Uuencode and post" gnus-uu-post-news
1975         ,@(if (featurep 'xemacs) '(t)
1976             '(:help "Post a uuencoded article"))]
1977        ["Followup via news" gnus-summary-followup-to-mail t]
1978        ["Followup via news and yank"
1979         gnus-summary-followup-to-mail-with-original t]
1980        ;;("Draft"
1981        ;;["Send" gnus-summary-send-draft t]
1982        ;;["Send bounced" gnus-resend-bounced-mail t])
1983        ))
1984
1985     (easy-menu-define
1986      gnus-summary-misc-menu gnus-summary-mode-map ""
1987      `("Misc"
1988        ("Mark Read"
1989         ["Mark as read" gnus-summary-mark-as-read-forward t]
1990         ["Mark same subject and select"
1991          gnus-summary-kill-same-subject-and-select t]
1992         ["Mark same subject" gnus-summary-kill-same-subject t]
1993         ["Catchup" gnus-summary-catchup
1994          ,@(if (featurep 'xemacs) '(t)
1995              '(:help "Mark unread articles in this group as read"))]
1996         ["Catchup all" gnus-summary-catchup-all t]
1997         ["Catchup to here" gnus-summary-catchup-to-here t]
1998         ["Catchup region" gnus-summary-mark-region-as-read t]
1999         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2000        ("Mark Various"
2001         ["Tick" gnus-summary-tick-article-forward t]
2002         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2003         ["Remove marks" gnus-summary-clear-mark-forward t]
2004         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2005         ["Set bookmark" gnus-summary-set-bookmark t]
2006         ["Remove bookmark" gnus-summary-remove-bookmark t])
2007        ("Mark Limit"
2008         ["Marks..." gnus-summary-limit-to-marks t]
2009         ["Subject..." gnus-summary-limit-to-subject t]
2010         ["Author..." gnus-summary-limit-to-author t]
2011         ["Age..." gnus-summary-limit-to-age t]
2012         ["Extra..." gnus-summary-limit-to-extra t]
2013         ["Score" gnus-summary-limit-to-score t]
2014         ["Unread" gnus-summary-limit-to-unread t]
2015         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2016         ["Articles" gnus-summary-limit-to-articles t]
2017         ["Pop limit" gnus-summary-pop-limit t]
2018         ["Show dormant" gnus-summary-limit-include-dormant t]
2019         ["Hide childless dormant"
2020          gnus-summary-limit-exclude-childless-dormant t]
2021         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2022         ["Hide marked" gnus-summary-limit-exclude-marks t]
2023         ["Show expunged" gnus-summary-show-all-expunged t])
2024        ("Process Mark"
2025         ["Set mark" gnus-summary-mark-as-processable t]
2026         ["Remove mark" gnus-summary-unmark-as-processable t]
2027         ["Remove all marks" gnus-summary-unmark-all-processable t]
2028         ["Mark above" gnus-uu-mark-over t]
2029         ["Mark series" gnus-uu-mark-series t]
2030         ["Mark region" gnus-uu-mark-region t]
2031         ["Unmark region" gnus-uu-unmark-region t]
2032         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2033         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2034         ["Mark all" gnus-uu-mark-all t]
2035         ["Mark buffer" gnus-uu-mark-buffer t]
2036         ["Mark sparse" gnus-uu-mark-sparse t]
2037         ["Mark thread" gnus-uu-mark-thread t]
2038         ["Unmark thread" gnus-uu-unmark-thread t]
2039         ("Process Mark Sets"
2040          ["Kill" gnus-summary-kill-process-mark t]
2041          ["Yank" gnus-summary-yank-process-mark
2042           gnus-newsgroup-process-stack]
2043          ["Save" gnus-summary-save-process-mark t]))
2044        ("Scroll article"
2045         ["Page forward" gnus-summary-next-page
2046          ,@(if (featurep 'xemacs) '(t)
2047              '(:help "Show next page of article"))]
2048         ["Page backward" gnus-summary-prev-page
2049          ,@(if (featurep 'xemacs) '(t)
2050              '(:help "Show previous page of article"))]
2051         ["Line forward" gnus-summary-scroll-up t])
2052        ("Move"
2053         ["Next unread article" gnus-summary-next-unread-article t]
2054         ["Previous unread article" gnus-summary-prev-unread-article t]
2055         ["Next article" gnus-summary-next-article t]
2056         ["Previous article" gnus-summary-prev-article t]
2057         ["Next unread subject" gnus-summary-next-unread-subject t]
2058         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2059         ["Next article same subject" gnus-summary-next-same-subject t]
2060         ["Previous article same subject" gnus-summary-prev-same-subject t]
2061         ["First unread article" gnus-summary-first-unread-article t]
2062         ["Best unread article" gnus-summary-best-unread-article t]
2063         ["Go to subject number..." gnus-summary-goto-subject t]
2064         ["Go to article number..." gnus-summary-goto-article t]
2065         ["Go to the last article" gnus-summary-goto-last-article t]
2066         ["Pop article off history" gnus-summary-pop-article t])
2067        ("Sort"
2068         ["Sort by number" gnus-summary-sort-by-number t]
2069         ["Sort by author" gnus-summary-sort-by-author t]
2070         ["Sort by subject" gnus-summary-sort-by-subject t]
2071         ["Sort by date" gnus-summary-sort-by-date t]
2072         ["Sort by score" gnus-summary-sort-by-score t]
2073         ["Sort by lines" gnus-summary-sort-by-lines t]
2074         ["Sort by characters" gnus-summary-sort-by-chars t])
2075        ("Help"
2076         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2077         ["Describe group" gnus-summary-describe-group t]
2078         ["Read manual" gnus-info-find-node t])
2079        ("Modes"
2080         ["Pick and read" gnus-pick-mode t]
2081         ["Binary" gnus-binary-mode t])
2082        ("Regeneration"
2083         ["Regenerate" gnus-summary-prepare t]
2084         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2085         ["Toggle threading" gnus-summary-toggle-threads t])
2086        ["See old articles" gnus-summary-insert-old-articles t]
2087        ["See new articles" gnus-summary-insert-new-articles t]
2088        ["Filter articles..." gnus-summary-execute-command t]
2089        ["Run command on subjects..." gnus-summary-universal-argument t]
2090        ["Search articles forward..." gnus-summary-search-article-forward t]
2091        ["Search articles backward..." gnus-summary-search-article-backward t]
2092        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2093        ["Expand window" gnus-summary-expand-window t]
2094        ["Expire expirable articles" gnus-summary-expire-articles
2095         (gnus-check-backend-function
2096          'request-expire-articles gnus-newsgroup-name)]
2097        ["Edit local kill file" gnus-summary-edit-local-kill t]
2098        ["Edit main kill file" gnus-summary-edit-global-kill t]
2099        ["Edit group parameters" gnus-summary-edit-parameters t]
2100        ["Customize group parameters" gnus-summary-customize-parameters t]
2101        ["Send a bug report" gnus-bug t]
2102        ("Exit"
2103         ["Catchup and exit" gnus-summary-catchup-and-exit
2104          ,@(if (featurep 'xemacs) '(t)
2105              '(:help "Mark unread articles in this group as read, then exit"))]
2106         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2107         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2108         ["Exit group" gnus-summary-exit
2109          ,@(if (featurep 'xemacs) '(t)
2110              '(:help "Exit current group, return to group selection mode"))]
2111         ["Exit group without updating" gnus-summary-exit-no-update t]
2112         ["Exit and goto next group" gnus-summary-next-group t]
2113         ["Exit and goto prev group" gnus-summary-prev-group t]
2114         ["Reselect group" gnus-summary-reselect-current-group t]
2115         ["Rescan group" gnus-summary-rescan-group t]
2116         ["Update dribble" gnus-summary-save-newsrc t])))
2117
2118     (gnus-run-hooks 'gnus-summary-menu-hook)))
2119
2120 (defvar gnus-summary-tool-bar-map nil)
2121
2122 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2123 (defun gnus-summary-make-tool-bar ()
2124   (if (and (fboundp 'tool-bar-add-item-from-menu)
2125            (default-value 'tool-bar-mode)
2126            (not gnus-summary-tool-bar-map))
2127       (setq gnus-summary-tool-bar-map
2128             (let ((tool-bar-map (make-sparse-keymap))
2129                   (load-path (mm-image-load-path)))
2130               (tool-bar-add-item-from-menu
2131                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2132               (tool-bar-add-item-from-menu
2133                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2134               (tool-bar-add-item-from-menu
2135                'gnus-summary-post-news "post" gnus-summary-mode-map)
2136               (tool-bar-add-item-from-menu
2137                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2138               (tool-bar-add-item-from-menu
2139                'gnus-summary-followup "followup" gnus-summary-mode-map)
2140               (tool-bar-add-item-from-menu
2141                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2142               (tool-bar-add-item-from-menu
2143                'gnus-summary-reply "reply" gnus-summary-mode-map)
2144               (tool-bar-add-item-from-menu
2145                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2146               (tool-bar-add-item-from-menu
2147                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2148               (tool-bar-add-item-from-menu
2149                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2150               (tool-bar-add-item-from-menu
2151                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2152               (tool-bar-add-item-from-menu
2153                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2154               (tool-bar-add-item-from-menu
2155                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2156               (tool-bar-add-item-from-menu
2157                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2158               (tool-bar-add-item-from-menu
2159                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2160               tool-bar-map)))
2161   (if gnus-summary-tool-bar-map
2162       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2163
2164 (defun gnus-score-set-default (var value)
2165   "A version of set that updates the GNU Emacs menu-bar."
2166   (set var value)
2167   ;; It is the message that forces the active status to be updated.
2168   (message ""))
2169
2170 (defun gnus-make-score-map (type)
2171   "Make a summary score map of type TYPE."
2172   (if t
2173       nil
2174     (let ((headers '(("author" "from" string)
2175                      ("subject" "subject" string)
2176                      ("article body" "body" string)
2177                      ("article head" "head" string)
2178                      ("xref" "xref" string)
2179                      ("extra header" "extra" string)
2180                      ("lines" "lines" number)
2181                      ("followups to author" "followup" string)))
2182           (types '((number ("less than" <)
2183                            ("greater than" >)
2184                            ("equal" =))
2185                    (string ("substring" s)
2186                            ("exact string" e)
2187                            ("fuzzy string" f)
2188                            ("regexp" r))))
2189           (perms '(("temporary" (current-time-string))
2190                    ("permanent" nil)
2191                    ("immediate" now)))
2192           header)
2193       (list
2194        (apply
2195         'nconc
2196         (list
2197          (if (eq type 'lower)
2198              "Lower score"
2199            "Increase score"))
2200         (let (outh)
2201           (while headers
2202             (setq header (car headers))
2203             (setq outh
2204                   (cons
2205                    (apply
2206                     'nconc
2207                     (list (car header))
2208                     (let ((ts (cdr (assoc (nth 2 header) types)))
2209                           outt)
2210                       (while ts
2211                         (setq outt
2212                               (cons
2213                                (apply
2214                                 'nconc
2215                                 (list (caar ts))
2216                                 (let ((ps perms)
2217                                       outp)
2218                                   (while ps
2219                                     (setq outp
2220                                           (cons
2221                                            (vector
2222                                             (caar ps)
2223                                             (list
2224                                              'gnus-summary-score-entry
2225                                              (nth 1 header)
2226                                              (if (or (string= (nth 1 header)
2227                                                               "head")
2228                                                      (string= (nth 1 header)
2229                                                               "body"))
2230                                                  ""
2231                                                (list 'gnus-summary-header
2232                                                      (nth 1 header)))
2233                                              (list 'quote (nth 1 (car ts)))
2234                                              (list 'gnus-score-delta-default
2235                                                    nil)
2236                                              (nth 1 (car ps))
2237                                              t)
2238                                             t)
2239                                            outp))
2240                                     (setq ps (cdr ps)))
2241                                   (list (nreverse outp))))
2242                                outt))
2243                         (setq ts (cdr ts)))
2244                       (list (nreverse outt))))
2245                    outh))
2246             (setq headers (cdr headers)))
2247           (list (nreverse outh))))))))
2248
2249 \f
2250
2251 (defun gnus-summary-mode (&optional group)
2252   "Major mode for reading articles.
2253
2254 All normal editing commands are switched off.
2255 \\<gnus-summary-mode-map>
2256 Each line in this buffer represents one article.  To read an
2257 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2258 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2259 respectively.
2260
2261 You can also post articles and send mail from this buffer.  To
2262 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2263 of an article, type `\\[gnus-summary-reply]'.
2264
2265 There are approx. one gazillion commands you can execute in this
2266 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2267
2268 The following commands are available:
2269
2270 \\{gnus-summary-mode-map}"
2271   (interactive)
2272   (kill-all-local-variables)
2273   (when (gnus-visual-p 'summary-menu 'menu)
2274     (gnus-summary-make-menu-bar)
2275     (gnus-summary-make-tool-bar))
2276   (gnus-summary-make-local-variables)
2277   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2278     (gnus-summary-make-local-variables))
2279   (gnus-make-thread-indent-array)
2280   (gnus-simplify-mode-line)
2281   (setq major-mode 'gnus-summary-mode)
2282   (setq mode-name "Summary")
2283   (make-local-variable 'minor-mode-alist)
2284   (use-local-map gnus-summary-mode-map)
2285   (buffer-disable-undo)
2286   (setq buffer-read-only t)             ;Disable modification
2287   (setq truncate-lines t)
2288   (setq selective-display t)
2289   (setq selective-display-ellipses t)   ;Display `...'
2290   (gnus-summary-set-display-table)
2291   (gnus-set-default-directory)
2292   (setq gnus-newsgroup-name group)
2293   (make-local-variable 'gnus-summary-line-format)
2294   (make-local-variable 'gnus-summary-line-format-spec)
2295   (make-local-variable 'gnus-summary-dummy-line-format)
2296   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2297   (make-local-variable 'gnus-summary-mark-positions)
2298   (make-local-hook 'pre-command-hook)
2299   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2300   (gnus-run-hooks 'gnus-summary-mode-hook)
2301   (turn-on-gnus-mailing-list-mode)
2302   (mm-enable-multibyte-mule4)
2303   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2304   (gnus-update-summary-mark-positions))
2305
2306 (defun gnus-summary-make-local-variables ()
2307   "Make all the local summary buffer variables."
2308   (let (global)
2309     (dolist (local gnus-summary-local-variables)
2310       (if (consp local)
2311           (progn
2312             (if (eq (cdr local) 'global)
2313                 ;; Copy the global value of the variable.
2314                 (setq global (symbol-value (car local)))
2315               ;; Use the value from the list.
2316               (setq global (eval (cdr local))))
2317             (set (make-local-variable (car local)) global))
2318         ;; Simple nil-valued local variable.
2319         (set (make-local-variable local) nil)))))
2320
2321 (defun gnus-summary-clear-local-variables ()
2322   (let ((locals gnus-summary-local-variables))
2323     (while locals
2324       (if (consp (car locals))
2325           (and (vectorp (caar locals))
2326                (set (caar locals) nil))
2327         (and (vectorp (car locals))
2328              (set (car locals) nil)))
2329       (setq locals (cdr locals)))))
2330
2331 ;; Summary data functions.
2332
2333 (defmacro gnus-data-number (data)
2334   `(car ,data))
2335
2336 (defmacro gnus-data-set-number (data number)
2337   `(setcar ,data ,number))
2338
2339 (defmacro gnus-data-mark (data)
2340   `(nth 1 ,data))
2341
2342 (defmacro gnus-data-set-mark (data mark)
2343   `(setcar (nthcdr 1 ,data) ,mark))
2344
2345 (defmacro gnus-data-pos (data)
2346   `(nth 2 ,data))
2347
2348 (defmacro gnus-data-set-pos (data pos)
2349   `(setcar (nthcdr 2 ,data) ,pos))
2350
2351 (defmacro gnus-data-header (data)
2352   `(nth 3 ,data))
2353
2354 (defmacro gnus-data-set-header (data header)
2355   `(setf (nth 3 ,data) ,header))
2356
2357 (defmacro gnus-data-level (data)
2358   `(nth 4 ,data))
2359
2360 (defmacro gnus-data-unread-p (data)
2361   `(= (nth 1 ,data) gnus-unread-mark))
2362
2363 (defmacro gnus-data-read-p (data)
2364   `(/= (nth 1 ,data) gnus-unread-mark))
2365
2366 (defmacro gnus-data-pseudo-p (data)
2367   `(consp (nth 3 ,data)))
2368
2369 (defmacro gnus-data-find (number)
2370   `(assq ,number gnus-newsgroup-data))
2371
2372 (defmacro gnus-data-find-list (number &optional data)
2373   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2374      (memq (assq ,number bdata)
2375            bdata)))
2376
2377 (defmacro gnus-data-make (number mark pos header level)
2378   `(list ,number ,mark ,pos ,header ,level))
2379
2380 (defun gnus-data-enter (after-article number mark pos header level offset)
2381   (let ((data (gnus-data-find-list after-article)))
2382     (unless data
2383       (error "No such article: %d" after-article))
2384     (setcdr data (cons (gnus-data-make number mark pos header level)
2385                        (cdr data)))
2386     (setq gnus-newsgroup-data-reverse nil)
2387     (gnus-data-update-list (cddr data) offset)))
2388
2389 (defun gnus-data-enter-list (after-article list &optional offset)
2390   (when list
2391     (let ((data (and after-article (gnus-data-find-list after-article)))
2392           (ilist list))
2393       (if (not (or data
2394                    after-article))
2395           (let ((odata gnus-newsgroup-data))
2396             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2397             (when offset
2398               (gnus-data-update-list odata offset)))
2399         ;; Find the last element in the list to be spliced into the main
2400         ;; list.
2401         (while (cdr list)
2402           (setq list (cdr list)))
2403         (if (not data)
2404             (progn
2405               (setcdr list gnus-newsgroup-data)
2406               (setq gnus-newsgroup-data ilist)
2407               (when offset
2408                 (gnus-data-update-list (cdr list) offset)))
2409           (setcdr list (cdr data))
2410           (setcdr data ilist)
2411           (when offset
2412             (gnus-data-update-list (cdr list) offset))))
2413       (setq gnus-newsgroup-data-reverse nil))))
2414
2415 (defun gnus-data-remove (article &optional offset)
2416   (let ((data gnus-newsgroup-data))
2417     (if (= (gnus-data-number (car data)) article)
2418         (progn
2419           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2420                 gnus-newsgroup-data-reverse nil)
2421           (when offset
2422             (gnus-data-update-list gnus-newsgroup-data offset)))
2423       (while (cdr data)
2424         (when (= (gnus-data-number (cadr data)) article)
2425           (setcdr data (cddr data))
2426           (when offset
2427             (gnus-data-update-list (cdr data) offset))
2428           (setq data nil
2429                 gnus-newsgroup-data-reverse nil))
2430         (setq data (cdr data))))))
2431
2432 (defmacro gnus-data-list (backward)
2433   `(if ,backward
2434        (or gnus-newsgroup-data-reverse
2435            (setq gnus-newsgroup-data-reverse
2436                  (reverse gnus-newsgroup-data)))
2437      gnus-newsgroup-data))
2438
2439 (defun gnus-data-update-list (data offset)
2440   "Add OFFSET to the POS of all data entries in DATA."
2441   (setq gnus-newsgroup-data-reverse nil)
2442   (while data
2443     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2444     (setq data (cdr data))))
2445
2446 (defun gnus-summary-article-pseudo-p (article)
2447   "Say whether this article is a pseudo article or not."
2448   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2449
2450 (defmacro gnus-summary-article-sparse-p (article)
2451   "Say whether this article is a sparse article or not."
2452   `(memq ,article gnus-newsgroup-sparse))
2453
2454 (defmacro gnus-summary-article-ancient-p (article)
2455   "Say whether this article is a sparse article or not."
2456   `(memq ,article gnus-newsgroup-ancient))
2457
2458 (defun gnus-article-parent-p (number)
2459   "Say whether this article is a parent or not."
2460   (let ((data (gnus-data-find-list number)))
2461     (and (cdr data)                     ; There has to be an article after...
2462          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2463             (gnus-data-level (nth 1 data))))))
2464
2465 (defun gnus-article-children (number)
2466   "Return a list of all children to NUMBER."
2467   (let* ((data (gnus-data-find-list number))
2468          (level (gnus-data-level (car data)))
2469          children)
2470     (setq data (cdr data))
2471     (while (and data
2472                 (= (gnus-data-level (car data)) (1+ level)))
2473       (push (gnus-data-number (car data)) children)
2474       (setq data (cdr data)))
2475     children))
2476
2477 (defmacro gnus-summary-skip-intangible ()
2478   "If the current article is intangible, then jump to a different article."
2479   '(let ((to (get-text-property (point) 'gnus-intangible)))
2480      (and to (gnus-summary-goto-subject to))))
2481
2482 (defmacro gnus-summary-article-intangible-p ()
2483   "Say whether this article is intangible or not."
2484   '(get-text-property (point) 'gnus-intangible))
2485
2486 (defun gnus-article-read-p (article)
2487   "Say whether ARTICLE is read or not."
2488   (not (or (memq article gnus-newsgroup-marked)
2489            (memq article gnus-newsgroup-unreads)
2490            (memq article gnus-newsgroup-unselected)
2491            (memq article gnus-newsgroup-dormant))))
2492
2493 ;; Some summary mode macros.
2494
2495 (defmacro gnus-summary-article-number ()
2496   "The article number of the article on the current line.
2497 If there isn's an article number here, then we return the current
2498 article number."
2499   '(progn
2500      (gnus-summary-skip-intangible)
2501      (or (get-text-property (point) 'gnus-number)
2502          (gnus-summary-last-subject))))
2503
2504 (defmacro gnus-summary-article-header (&optional number)
2505   "Return the header of article NUMBER."
2506   `(gnus-data-header (gnus-data-find
2507                       ,(or number '(gnus-summary-article-number)))))
2508
2509 (defmacro gnus-summary-thread-level (&optional number)
2510   "Return the level of thread that starts with article NUMBER."
2511   `(if (and (eq gnus-summary-make-false-root 'dummy)
2512             (get-text-property (point) 'gnus-intangible))
2513        0
2514      (gnus-data-level (gnus-data-find
2515                        ,(or number '(gnus-summary-article-number))))))
2516
2517 (defmacro gnus-summary-article-mark (&optional number)
2518   "Return the mark of article NUMBER."
2519   `(gnus-data-mark (gnus-data-find
2520                     ,(or number '(gnus-summary-article-number)))))
2521
2522 (defmacro gnus-summary-article-pos (&optional number)
2523   "Return the position of the line of article NUMBER."
2524   `(gnus-data-pos (gnus-data-find
2525                    ,(or number '(gnus-summary-article-number)))))
2526
2527 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2528 (defmacro gnus-summary-article-subject (&optional number)
2529   "Return current subject string or nil if nothing."
2530   `(let ((headers
2531           ,(if number
2532                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2533              '(gnus-data-header (assq (gnus-summary-article-number)
2534                                       gnus-newsgroup-data)))))
2535      (and headers
2536           (vectorp headers)
2537           (mail-header-subject headers))))
2538
2539 (defmacro gnus-summary-article-score (&optional number)
2540   "Return current article score."
2541   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2542                   gnus-newsgroup-scored))
2543        gnus-summary-default-score 0))
2544
2545 (defun gnus-summary-article-children (&optional number)
2546   "Return a list of article numbers that are children of article NUMBER."
2547   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2548          (level (gnus-data-level (car data)))
2549          l children)
2550     (while (and (setq data (cdr data))
2551                 (> (setq l (gnus-data-level (car data))) level))
2552       (and (= (1+ level) l)
2553            (push (gnus-data-number (car data))
2554                  children)))
2555     (nreverse children)))
2556
2557 (defun gnus-summary-article-parent (&optional number)
2558   "Return the article number of the parent of article NUMBER."
2559   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2560                                     (gnus-data-list t)))
2561          (level (gnus-data-level (car data))))
2562     (if (zerop level)
2563         ()                              ; This is a root.
2564       ;; We search until we find an article with a level less than
2565       ;; this one.  That function has to be the parent.
2566       (while (and (setq data (cdr data))
2567                   (not (< (gnus-data-level (car data)) level))))
2568       (and data (gnus-data-number (car data))))))
2569
2570 (defun gnus-unread-mark-p (mark)
2571   "Say whether MARK is the unread mark."
2572   (= mark gnus-unread-mark))
2573
2574 (defun gnus-read-mark-p (mark)
2575   "Say whether MARK is one of the marks that mark as read.
2576 This is all marks except unread, ticked, dormant, and expirable."
2577   (not (or (= mark gnus-unread-mark)
2578            (= mark gnus-ticked-mark)
2579            (= mark gnus-dormant-mark)
2580            (= mark gnus-expirable-mark))))
2581
2582 (defmacro gnus-article-mark (number)
2583   "Return the MARK of article NUMBER.
2584 This macro should only be used when computing the mark the \"first\"
2585 time; i.e., when generating the summary lines.  After that,
2586 `gnus-summary-article-mark' should be used to examine the
2587 marks of articles."
2588   `(cond
2589     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2590     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2591     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2592     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2593     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2594     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2595     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2596     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2597            gnus-ancient-mark))))
2598
2599 ;; Saving hidden threads.
2600
2601 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2602 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2603
2604 (defmacro gnus-save-hidden-threads (&rest forms)
2605   "Save hidden threads, eval FORMS, and restore the hidden threads."
2606   (let ((config (make-symbol "config")))
2607     `(let ((,config (gnus-hidden-threads-configuration)))
2608        (unwind-protect
2609            (save-excursion
2610              ,@forms)
2611          (gnus-restore-hidden-threads-configuration ,config)))))
2612
2613 (defun gnus-data-compute-positions ()
2614   "Compute the positions of all articles."
2615   (setq gnus-newsgroup-data-reverse nil)
2616   (let ((data gnus-newsgroup-data))
2617     (save-excursion
2618       (gnus-save-hidden-threads
2619         (gnus-summary-show-all-threads)
2620         (goto-char (point-min))
2621         (while data
2622           (while (get-text-property (point) 'gnus-intangible)
2623             (forward-line 1))
2624           (gnus-data-set-pos (car data) (+ (point) 3))
2625           (setq data (cdr data))
2626           (forward-line 1))))))
2627
2628 (defun gnus-hidden-threads-configuration ()
2629   "Return the current hidden threads configuration."
2630   (save-excursion
2631     (let (config)
2632       (goto-char (point-min))
2633       (while (search-forward "\r" nil t)
2634         (push (1- (point)) config))
2635       config)))
2636
2637 (defun gnus-restore-hidden-threads-configuration (config)
2638   "Restore hidden threads configuration from CONFIG."
2639   (save-excursion
2640     (let (point buffer-read-only)
2641       (while (setq point (pop config))
2642         (when (and (< point (point-max))
2643                    (goto-char point)
2644                    (eq (char-after) ?\n))
2645           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2646
2647 ;; Various summary mode internalish functions.
2648
2649 (defun gnus-mouse-pick-article (e)
2650   (interactive "e")
2651   (mouse-set-point e)
2652   (gnus-summary-next-page nil t))
2653
2654 (defun gnus-summary-set-display-table ()
2655   "Change the display table.
2656 Odd characters have a tendency to mess
2657 up nicely formatted displays - we make all possible glyphs
2658 display only a single character."
2659
2660   ;; We start from the standard display table, if any.
2661   (let ((table (or (copy-sequence standard-display-table)
2662                    (make-display-table)))
2663         (i 32))
2664     ;; Nix out all the control chars...
2665     (while (>= (setq i (1- i)) 0)
2666       (aset table i [??]))
2667     ;; ... but not newline and cr, of course.  (cr is necessary for the
2668     ;; selective display).
2669     (aset table ?\n nil)
2670     (aset table ?\r nil)
2671     ;; We keep TAB as well.
2672     (aset table ?\t nil)
2673     ;; We nix out any glyphs over 126 that are not set already.
2674     (let ((i 256))
2675       (while (>= (setq i (1- i)) 127)
2676         ;; Only modify if the entry is nil.
2677         (unless (aref table i)
2678           (aset table i [??]))))
2679     (setq buffer-display-table table)))
2680
2681 (defun gnus-summary-buffer-name (group)
2682   "Return the summary buffer name of GROUP."
2683   (concat "*Summary " group "*"))
2684
2685 (defun gnus-summary-setup-buffer (group)
2686   "Initialize summary buffer."
2687   (let ((buffer (gnus-summary-buffer-name group)))
2688     (if (get-buffer buffer)
2689         (progn
2690           (set-buffer buffer)
2691           (setq gnus-summary-buffer (current-buffer))
2692           (not gnus-newsgroup-prepared))
2693       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2694       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2695       (gnus-summary-mode group)
2696       (when gnus-carpal
2697         (gnus-carpal-setup-buffer 'summary))
2698       (unless gnus-single-article-buffer
2699         (make-local-variable 'gnus-article-buffer)
2700         (make-local-variable 'gnus-article-current)
2701         (make-local-variable 'gnus-original-article-buffer))
2702       (setq gnus-newsgroup-name group)
2703       ;; Set any local variables in the group parameters.
2704       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2705       t)))
2706
2707 (defun gnus-set-global-variables ()
2708   "Set the global equivalents of the buffer-local variables.
2709 They are set to the latest values they had.  These reflect the summary
2710 buffer that was in action when the last article was fetched."
2711   (when (eq major-mode 'gnus-summary-mode)
2712     (setq gnus-summary-buffer (current-buffer))
2713     (let ((name gnus-newsgroup-name)
2714           (marked gnus-newsgroup-marked)
2715           (unread gnus-newsgroup-unreads)
2716           (headers gnus-current-headers)
2717           (data gnus-newsgroup-data)
2718           (summary gnus-summary-buffer)
2719           (article-buffer gnus-article-buffer)
2720           (original gnus-original-article-buffer)
2721           (gac gnus-article-current)
2722           (reffed gnus-reffed-article-number)
2723           (score-file gnus-current-score-file)
2724           (default-charset gnus-newsgroup-charset)
2725           vlist)
2726       (let ((locals gnus-newsgroup-variables))
2727         (while locals
2728           (if (consp (car locals))
2729               (push (eval (caar locals)) vlist)
2730             (push (eval (car locals)) vlist))
2731           (setq locals (cdr locals)))
2732         (setq vlist (nreverse vlist)))
2733       (save-excursion
2734         (set-buffer gnus-group-buffer)
2735         (setq gnus-newsgroup-name name
2736               gnus-newsgroup-marked marked
2737               gnus-newsgroup-unreads unread
2738               gnus-current-headers headers
2739               gnus-newsgroup-data data
2740               gnus-article-current gac
2741               gnus-summary-buffer summary
2742               gnus-article-buffer article-buffer
2743               gnus-original-article-buffer original
2744               gnus-reffed-article-number reffed
2745               gnus-current-score-file score-file
2746               gnus-newsgroup-charset default-charset)
2747         (let ((locals gnus-newsgroup-variables))
2748           (while locals
2749             (if (consp (car locals))
2750                 (set (caar locals) (pop vlist))
2751               (set (car locals) (pop vlist)))
2752             (setq locals (cdr locals))))
2753         ;; The article buffer also has local variables.
2754         (when (gnus-buffer-live-p gnus-article-buffer)
2755           (set-buffer gnus-article-buffer)
2756           (setq gnus-summary-buffer summary))))))
2757
2758 (defun gnus-summary-article-unread-p (article)
2759   "Say whether ARTICLE is unread or not."
2760   (memq article gnus-newsgroup-unreads))
2761
2762 (defun gnus-summary-first-article-p (&optional article)
2763   "Return whether ARTICLE is the first article in the buffer."
2764   (if (not (setq article (or article (gnus-summary-article-number))))
2765       nil
2766     (eq article (caar gnus-newsgroup-data))))
2767
2768 (defun gnus-summary-last-article-p (&optional article)
2769   "Return whether ARTICLE is the last article in the buffer."
2770   (if (not (setq article (or article (gnus-summary-article-number))))
2771       ;; All non-existent numbers are the last article.  :-)
2772       t
2773     (not (cdr (gnus-data-find-list article)))))
2774
2775 (defun gnus-make-thread-indent-array ()
2776   (let ((n 200))
2777     (unless (and gnus-thread-indent-array
2778                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2779       (setq gnus-thread-indent-array (make-vector 201 "")
2780             gnus-thread-indent-array-level gnus-thread-indent-level)
2781       (while (>= n 0)
2782         (aset gnus-thread-indent-array n
2783               (make-string (* n gnus-thread-indent-level) ? ))
2784         (setq n (1- n))))))
2785
2786 (defun gnus-update-summary-mark-positions ()
2787   "Compute where the summary marks are to go."
2788   (save-excursion
2789     (when (gnus-buffer-exists-p gnus-summary-buffer)
2790       (set-buffer gnus-summary-buffer))
2791     (let ((gnus-replied-mark 129)
2792           (gnus-score-below-mark 130)
2793           (gnus-score-over-mark 130)
2794           (gnus-download-mark 131)
2795           (spec gnus-summary-line-format-spec)
2796           gnus-visual pos)
2797       (save-excursion
2798         (gnus-set-work-buffer)
2799         (let ((gnus-summary-line-format-spec spec)
2800               (gnus-newsgroup-downloadable '((0 . t))))
2801           (gnus-summary-insert-line
2802            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2803           (goto-char (point-min))
2804           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2805                                              (- (point) 2)))))
2806           (goto-char (point-min))
2807           (push (cons 'replied (and (search-forward "\201" nil t)
2808                                     (- (point) 2)))
2809                 pos)
2810           (goto-char (point-min))
2811           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2812                 pos)
2813           (goto-char (point-min))
2814           (push (cons 'download
2815                       (and (search-forward "\203" nil t) (- (point) 2)))
2816                 pos)))
2817       (setq gnus-summary-mark-positions pos))))
2818
2819 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2820   "Insert a dummy root in the summary buffer."
2821   (beginning-of-line)
2822   (gnus-add-text-properties
2823    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2824    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2825
2826 (defun gnus-summary-from-or-to-or-newsgroups (header)
2827   (let ((to (cdr (assq 'To (mail-header-extra header))))
2828         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2829         (mail-parse-charset gnus-newsgroup-charset)
2830         (mail-parse-ignored-charsets
2831          (save-excursion (set-buffer gnus-summary-buffer)
2832                          gnus-newsgroup-ignored-charsets)))
2833     (cond
2834      ((and to
2835            gnus-ignored-from-addresses
2836            (string-match gnus-ignored-from-addresses
2837                          (mail-header-from header)))
2838       (concat "-> "
2839               (or (car (funcall gnus-extract-address-components
2840                                 (funcall
2841                                  gnus-decode-encoded-word-function to)))
2842                   (funcall gnus-decode-encoded-word-function to))))
2843      ((and newsgroups
2844            gnus-ignored-from-addresses
2845            (string-match gnus-ignored-from-addresses
2846                          (mail-header-from header)))
2847       (concat "=> " newsgroups))
2848      (t
2849       (or (car (funcall gnus-extract-address-components
2850                         (mail-header-from header)))
2851           (mail-header-from header))))))
2852
2853 (defun gnus-summary-insert-line (gnus-tmp-header
2854                                  gnus-tmp-level gnus-tmp-current
2855                                  gnus-tmp-unread gnus-tmp-replied
2856                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2857                                  &optional gnus-tmp-dummy gnus-tmp-score
2858                                  gnus-tmp-process)
2859   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2860          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2861          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2862          (gnus-tmp-score-char
2863           (if (or (null gnus-summary-default-score)
2864                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2865                       gnus-summary-zcore-fuzz))
2866               ?  ;Whitespace
2867             (if (< gnus-tmp-score gnus-summary-default-score)
2868                 gnus-score-below-mark gnus-score-over-mark)))
2869          (gnus-tmp-replied
2870           (cond (gnus-tmp-process gnus-process-mark)
2871                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2872                  gnus-cached-mark)
2873                 (gnus-tmp-replied gnus-replied-mark)
2874                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2875                  gnus-saved-mark)
2876                 (t gnus-no-mark)))
2877          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2878          (gnus-tmp-name
2879           (cond
2880            ((string-match "<[^>]+> *$" gnus-tmp-from)
2881             (let ((beg (match-beginning 0)))
2882               (or (and (string-match "^\".+\"" gnus-tmp-from)
2883                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2884                   (substring gnus-tmp-from 0 beg))))
2885            ((string-match "(.+)" gnus-tmp-from)
2886             (substring gnus-tmp-from
2887                        (1+ (match-beginning 0)) (1- (match-end 0))))
2888            (t gnus-tmp-from)))
2889          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2890          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2891          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2892          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2893          (buffer-read-only nil))
2894     (when (string= gnus-tmp-name "")
2895       (setq gnus-tmp-name gnus-tmp-from))
2896     (unless (numberp gnus-tmp-lines)
2897       (setq gnus-tmp-lines 0))
2898     (gnus-put-text-property
2899      (point)
2900      (progn (eval gnus-summary-line-format-spec) (point))
2901      'gnus-number gnus-tmp-number)
2902     (when (gnus-visual-p 'summary-highlight 'highlight)
2903       (forward-line -1)
2904       (gnus-run-hooks 'gnus-summary-update-hook)
2905       (forward-line 1))))
2906
2907 (defun gnus-summary-update-line (&optional dont-update)
2908   "Update summary line after change."
2909   (when (and gnus-summary-default-score
2910              (not gnus-summary-inhibit-highlight))
2911     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2912            (article (gnus-summary-article-number))
2913            (score (gnus-summary-article-score article)))
2914       (unless dont-update
2915         (if (and gnus-summary-mark-below
2916                  (< (gnus-summary-article-score)
2917                     gnus-summary-mark-below))
2918             ;; This article has a low score, so we mark it as read.
2919             (when (memq article gnus-newsgroup-unreads)
2920               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2921           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2922             ;; This article was previously marked as read on account
2923             ;; of a low score, but now it has risen, so we mark it as
2924             ;; unread.
2925             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2926         (gnus-summary-update-mark
2927          (if (or (null gnus-summary-default-score)
2928                  (<= (abs (- score gnus-summary-default-score))
2929                      gnus-summary-zcore-fuzz))
2930              ?  ;Whitespace
2931            (if (< score gnus-summary-default-score)
2932                gnus-score-below-mark gnus-score-over-mark))
2933          'score))
2934       ;; Do visual highlighting.
2935       (when (gnus-visual-p 'summary-highlight 'highlight)
2936         (gnus-run-hooks 'gnus-summary-update-hook)))))
2937
2938 (defvar gnus-tmp-new-adopts nil)
2939
2940 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2941   "Return the number of articles in THREAD.
2942 This may be 0 in some cases -- if none of the articles in
2943 the thread are to be displayed."
2944   (let* ((number
2945           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2946           (cond
2947            ((not (listp thread))
2948             1)
2949            ((and (consp thread) (cdr thread))
2950             (apply
2951              '+ 1 (mapcar
2952                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2953            ((null thread)
2954             1)
2955            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2956             1)
2957            (t 0))))
2958     (when (and level (zerop level) gnus-tmp-new-adopts)
2959       (incf number
2960             (apply '+ (mapcar
2961                        'gnus-summary-number-of-articles-in-thread
2962                        gnus-tmp-new-adopts))))
2963     (if char
2964         (if (> number 1) gnus-not-empty-thread-mark
2965           gnus-empty-thread-mark)
2966       number)))
2967
2968 (defun gnus-summary-set-local-parameters (group)
2969   "Go through the local params of GROUP and set all variable specs in that list."
2970   (let ((params (gnus-group-find-parameter group))
2971         elem)
2972     (while params
2973       (setq elem (car params)
2974             params (cdr params))
2975       (and (consp elem)                 ; Has to be a cons.
2976            (consp (cdr elem))           ; The cdr has to be a list.
2977            (symbolp (car elem))         ; Has to be a symbol in there.
2978            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2979            (ignore-errors               ; So we set it.
2980              (make-local-variable (car elem))
2981              (set (car elem) (eval (nth 1 elem))))))))
2982
2983 (defun gnus-summary-read-group (group &optional show-all no-article
2984                                       kill-buffer no-display backward
2985                                       select-articles)
2986   "Start reading news in newsgroup GROUP.
2987 If SHOW-ALL is non-nil, already read articles are also listed.
2988 If NO-ARTICLE is non-nil, no article is selected initially.
2989 If NO-DISPLAY, don't generate a summary buffer."
2990   (let (result)
2991     (while (and group
2992                 (null (setq result
2993                             (let ((gnus-auto-select-next nil))
2994                               (or (gnus-summary-read-group-1
2995                                    group show-all no-article
2996                                    kill-buffer no-display
2997                                    select-articles)
2998                                   (setq show-all nil
2999                                         select-articles nil)))))
3000                 (eq gnus-auto-select-next 'quietly))
3001       (set-buffer gnus-group-buffer)
3002       ;; The entry function called above goes to the next
3003       ;; group automatically, so we go two groups back
3004       ;; if we are searching for the previous group.
3005       (when backward
3006         (gnus-group-prev-unread-group 2))
3007       (if (not (equal group (gnus-group-group-name)))
3008           (setq group (gnus-group-group-name))
3009         (setq group nil)))
3010     result))
3011
3012 (defun gnus-summary-read-group-1 (group show-all no-article
3013                                         kill-buffer no-display
3014                                         &optional select-articles)
3015   ;; Killed foreign groups can't be entered.
3016   ;;  (when (and (not (gnus-group-native-p group))
3017   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3018   ;;    (error "Dead non-native groups can't be entered"))
3019   (gnus-message 5 "Retrieving newsgroup: %s..." 
3020                 (gnus-group-decoded-name group))
3021   (let* ((new-group (gnus-summary-setup-buffer group))
3022          (quit-config (gnus-group-quit-config group))
3023          (did-select (and new-group (gnus-select-newsgroup
3024                                      group show-all select-articles))))
3025     (cond
3026      ;; This summary buffer exists already, so we just select it.
3027      ((not new-group)
3028       (gnus-set-global-variables)
3029       (when kill-buffer
3030         (gnus-kill-or-deaden-summary kill-buffer))
3031       (gnus-configure-windows 'summary 'force)
3032       (gnus-set-mode-line 'summary)
3033       (gnus-summary-position-point)
3034       (message "")
3035       t)
3036      ;; We couldn't select this group.
3037      ((null did-select)
3038       (when (and (eq major-mode 'gnus-summary-mode)
3039                  (not (equal (current-buffer) kill-buffer)))
3040         (kill-buffer (current-buffer))
3041         (if (not quit-config)
3042             (progn
3043               ;; Update the info -- marks might need to be removed,
3044               ;; for instance.
3045               (gnus-summary-update-info)
3046               (set-buffer gnus-group-buffer)
3047               (gnus-group-jump-to-group group)
3048               (gnus-group-next-unread-group 1))
3049           (gnus-handle-ephemeral-exit quit-config)))
3050       (let ((grpinfo (gnus-get-info group)))
3051         (if (null (gnus-info-read grpinfo))
3052             (gnus-message 3 "Group %s contains no messages" 
3053                           (gnus-group-decoded-name group))
3054           (gnus-message 3 "Can't select group")))
3055       nil)
3056      ;; The user did a `C-g' while prompting for number of articles,
3057      ;; so we exit this group.
3058      ((eq did-select 'quit)
3059       (and (eq major-mode 'gnus-summary-mode)
3060            (not (equal (current-buffer) kill-buffer))
3061            (kill-buffer (current-buffer)))
3062       (when kill-buffer
3063         (gnus-kill-or-deaden-summary kill-buffer))
3064       (if (not quit-config)
3065           (progn
3066             (set-buffer gnus-group-buffer)
3067             (gnus-group-jump-to-group group)
3068             (gnus-group-next-unread-group 1)
3069             (gnus-configure-windows 'group 'force))
3070         (gnus-handle-ephemeral-exit quit-config))
3071       ;; Finally signal the quit.
3072       (signal 'quit nil))
3073      ;; The group was successfully selected.
3074      (t
3075       (gnus-set-global-variables)
3076       ;; Save the active value in effect when the group was entered.
3077       (setq gnus-newsgroup-active
3078             (gnus-copy-sequence
3079              (gnus-active gnus-newsgroup-name)))
3080       ;; You can change the summary buffer in some way with this hook.
3081       (gnus-run-hooks 'gnus-select-group-hook)
3082       (gnus-update-format-specifications
3083        nil 'summary 'summary-mode 'summary-dummy)
3084       (gnus-update-summary-mark-positions)
3085       ;; Do score processing.
3086       (when gnus-use-scoring
3087         (gnus-possibly-score-headers))
3088       ;; Check whether to fill in the gaps in the threads.
3089       (when gnus-build-sparse-threads
3090         (gnus-build-sparse-threads))
3091       ;; Find the initial limit.
3092       (if gnus-show-threads
3093           (if show-all
3094               (let ((gnus-newsgroup-dormant nil))
3095                 (gnus-summary-initial-limit show-all))
3096             (gnus-summary-initial-limit show-all))
3097         ;; When untreaded, all articles are always shown.
3098         (setq gnus-newsgroup-limit
3099               (mapcar
3100                (lambda (header) (mail-header-number header))
3101                gnus-newsgroup-headers)))
3102       ;; Generate the summary buffer.
3103       (unless no-display
3104         (gnus-summary-prepare))
3105       (when gnus-use-trees
3106         (gnus-tree-open group)
3107         (setq gnus-summary-highlight-line-function
3108               'gnus-tree-highlight-article))
3109       ;; If the summary buffer is empty, but there are some low-scored
3110       ;; articles or some excluded dormants, we include these in the
3111       ;; buffer.
3112       (when (and (zerop (buffer-size))
3113                  (not no-display))
3114         (cond (gnus-newsgroup-dormant
3115                (gnus-summary-limit-include-dormant))
3116               ((and gnus-newsgroup-scored show-all)
3117                (gnus-summary-limit-include-expunged t))))
3118       ;; Function `gnus-apply-kill-file' must be called in this hook.
3119       (gnus-run-hooks 'gnus-apply-kill-hook)
3120       (if (and (zerop (buffer-size))
3121                (not no-display))
3122           (progn
3123             ;; This newsgroup is empty.
3124             (gnus-summary-catchup-and-exit nil t)
3125             (gnus-message 6 "No unread news")
3126             (when kill-buffer
3127               (gnus-kill-or-deaden-summary kill-buffer))
3128             ;; Return nil from this function.
3129             nil)
3130         ;; Hide conversation thread subtrees.  We cannot do this in
3131         ;; gnus-summary-prepare-hook since kill processing may not
3132         ;; work with hidden articles.
3133         (and gnus-show-threads
3134              gnus-thread-hide-subtree
3135              (gnus-summary-hide-all-threads))
3136         (when kill-buffer
3137           (gnus-kill-or-deaden-summary kill-buffer))
3138         ;; Show first unread article if requested.
3139         (if (and (not no-article)
3140                  (not no-display)
3141                  gnus-newsgroup-unreads
3142                  gnus-auto-select-first)
3143             (progn
3144               (gnus-configure-windows 'summary)
3145               (cond
3146                ((eq gnus-auto-select-first 'best)
3147                 (gnus-summary-best-unread-article))
3148                ((eq gnus-auto-select-first t)
3149                 (gnus-summary-first-unread-article))
3150                ((gnus-functionp gnus-auto-select-first)
3151                 (funcall gnus-auto-select-first))))
3152           ;; Don't select any articles, just move point to the first
3153           ;; article in the group.
3154           (goto-char (point-min))
3155           (gnus-summary-position-point)
3156           (gnus-configure-windows 'summary 'force)
3157           (gnus-set-mode-line 'summary))
3158         (when (get-buffer-window gnus-group-buffer t)
3159           ;; Gotta use windows, because recenter does weird stuff if
3160           ;; the current buffer ain't the displayed window.
3161           (let ((owin (selected-window)))
3162             (select-window (get-buffer-window gnus-group-buffer t))
3163             (when (gnus-group-goto-group group)
3164               (recenter))
3165             (select-window owin)))
3166         ;; Mark this buffer as "prepared".
3167         (setq gnus-newsgroup-prepared t)
3168         (gnus-run-hooks 'gnus-summary-prepared-hook)
3169         t)))))
3170
3171 (defun gnus-summary-prepare ()
3172   "Generate the summary buffer."
3173   (interactive)
3174   (let ((buffer-read-only nil))
3175     (erase-buffer)
3176     (setq gnus-newsgroup-data nil
3177           gnus-newsgroup-data-reverse nil)
3178     (gnus-run-hooks 'gnus-summary-generate-hook)
3179     ;; Generate the buffer, either with threads or without.
3180     (when gnus-newsgroup-headers
3181       (gnus-summary-prepare-threads
3182        (if gnus-show-threads
3183            (gnus-sort-gathered-threads
3184             (funcall gnus-summary-thread-gathering-function
3185                      (gnus-sort-threads
3186                       (gnus-cut-threads (gnus-make-threads)))))
3187          ;; Unthreaded display.
3188          (gnus-sort-articles gnus-newsgroup-headers))))
3189     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3190     ;; Call hooks for modifying summary buffer.
3191     (goto-char (point-min))
3192     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3193
3194 (defsubst gnus-general-simplify-subject (subject)
3195   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3196   (setq subject
3197         (cond
3198          ;; Truncate the subject.
3199          (gnus-simplify-subject-functions
3200           (gnus-map-function gnus-simplify-subject-functions subject))
3201          ((numberp gnus-summary-gather-subject-limit)
3202           (setq subject (gnus-simplify-subject-re subject))
3203           (if (> (length subject) gnus-summary-gather-subject-limit)
3204               (substring subject 0 gnus-summary-gather-subject-limit)
3205             subject))
3206          ;; Fuzzily simplify it.
3207          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3208           (gnus-simplify-subject-fuzzy subject))
3209          ;; Just remove the leading "Re:".
3210          (t
3211           (gnus-simplify-subject-re subject))))
3212
3213   (if (and gnus-summary-gather-exclude-subject
3214            (string-match gnus-summary-gather-exclude-subject subject))
3215       nil                               ; This article shouldn't be gathered
3216     subject))
3217
3218 (defun gnus-summary-simplify-subject-query ()
3219   "Query where the respool algorithm would put this article."
3220   (interactive)
3221   (gnus-summary-select-article)
3222   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3223
3224 (defun gnus-gather-threads-by-subject (threads)
3225   "Gather threads by looking at Subject headers."
3226   (if (not gnus-summary-make-false-root)
3227       threads
3228     (let ((hashtb (gnus-make-hashtable 1024))
3229           (prev threads)
3230           (result threads)
3231           subject hthread whole-subject)
3232       (while threads
3233         (setq subject (gnus-general-simplify-subject
3234                        (setq whole-subject (mail-header-subject
3235                                             (caar threads)))))
3236         (when subject
3237           (if (setq hthread (gnus-gethash subject hashtb))
3238               (progn
3239                 ;; We enter a dummy root into the thread, if we
3240                 ;; haven't done that already.
3241                 (unless (stringp (caar hthread))
3242                   (setcar hthread (list whole-subject (car hthread))))
3243                 ;; We add this new gathered thread to this gathered
3244                 ;; thread.
3245                 (setcdr (car hthread)
3246                         (nconc (cdar hthread) (list (car threads))))
3247                 ;; Remove it from the list of threads.
3248                 (setcdr prev (cdr threads))
3249                 (setq threads prev))
3250             ;; Enter this thread into the hash table.
3251             (gnus-sethash subject threads hashtb)))
3252         (setq prev threads)
3253         (setq threads (cdr threads)))
3254       result)))
3255
3256 (defun gnus-gather-threads-by-references (threads)
3257   "Gather threads by looking at References headers."
3258   (let ((idhashtb (gnus-make-hashtable 1024))
3259         (thhashtb (gnus-make-hashtable 1024))
3260         (prev threads)
3261         (result threads)
3262         ids references id gthread gid entered ref)
3263     (while threads
3264       (when (setq references (mail-header-references (caar threads)))
3265         (setq id (mail-header-id (caar threads))
3266               ids (gnus-split-references references)
3267               entered nil)
3268         (while (setq ref (pop ids))
3269           (setq ids (delete ref ids))
3270           (if (not (setq gid (gnus-gethash ref idhashtb)))
3271               (progn
3272                 (gnus-sethash ref id idhashtb)
3273                 (gnus-sethash id threads thhashtb))
3274             (setq gthread (gnus-gethash gid thhashtb))
3275             (unless entered
3276               ;; We enter a dummy root into the thread, if we
3277               ;; haven't done that already.
3278               (unless (stringp (caar gthread))
3279                 (setcar gthread (list (mail-header-subject (caar gthread))
3280                                       (car gthread))))
3281               ;; We add this new gathered thread to this gathered
3282               ;; thread.
3283               (setcdr (car gthread)
3284                       (nconc (cdar gthread) (list (car threads)))))
3285             ;; Add it into the thread hash table.
3286             (gnus-sethash id gthread thhashtb)
3287             (setq entered t)
3288             ;; Remove it from the list of threads.
3289             (setcdr prev (cdr threads))
3290             (setq threads prev))))
3291       (setq prev threads)
3292       (setq threads (cdr threads)))
3293     result))
3294
3295 (defun gnus-sort-gathered-threads (threads)
3296   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3297   (let ((result threads))
3298     (while threads
3299       (when (stringp (caar threads))
3300         (setcdr (car threads)
3301                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3302       (setq threads (cdr threads)))
3303     result))
3304
3305 (defun gnus-thread-loop-p (root thread)
3306   "Say whether ROOT is in THREAD."
3307   (let ((stack (list thread))
3308         (infloop 0)
3309         th)
3310     (while (setq thread (pop stack))
3311       (setq th (cdr thread))
3312       (while (and th
3313                   (not (eq (caar th) root)))
3314         (pop th))
3315       (if th
3316           ;; We have found a loop.
3317           (let (ref-dep)
3318             (setcdr thread (delq (car th) (cdr thread)))
3319             (if (boundp (setq ref-dep (intern "none"
3320                                               gnus-newsgroup-dependencies)))
3321                 (setcdr (symbol-value ref-dep)
3322                         (nconc (cdr (symbol-value ref-dep))
3323                                (list (car th))))
3324               (set ref-dep (list nil (car th))))
3325             (setq infloop 1
3326                   stack nil))
3327         ;; Push all the subthreads onto the stack.
3328         (push (cdr thread) stack)))
3329     infloop))
3330
3331 (defun gnus-make-threads ()
3332   "Go through the dependency hashtb and find the roots.  Return all threads."
3333   (let (threads)
3334     (while (catch 'infloop
3335              (mapatoms
3336               (lambda (refs)
3337                 ;; Deal with self-referencing References loops.
3338                 (when (and (car (symbol-value refs))
3339                            (not (zerop
3340                                  (apply
3341                                   '+
3342                                   (mapcar
3343                                    (lambda (thread)
3344                                      (gnus-thread-loop-p
3345                                       (car (symbol-value refs)) thread))
3346                                    (cdr (symbol-value refs)))))))
3347                   (setq threads nil)
3348                   (throw 'infloop t))
3349                 (unless (car (symbol-value refs))
3350                   ;; These threads do not refer back to any other articles,
3351                   ;; so they're roots.
3352                   (setq threads (append (cdr (symbol-value refs)) threads))))
3353               gnus-newsgroup-dependencies)))
3354     threads))
3355
3356 ;; Build the thread tree.
3357 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3358   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3359
3360 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3361 if it was already present.
3362
3363 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3364 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3365 Message-IDs will be renamed be renamed to a unique Message-ID before
3366 being entered.
3367
3368 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3369   (let* ((id (mail-header-id header))
3370          (id-dep (and id (intern id dependencies)))
3371          ref ref-dep ref-header)
3372     ;; Enter this `header' in the `dependencies' table.
3373     (cond
3374      ((not id-dep)
3375       (setq header nil))
3376      ;; The first two cases do the normal part: enter a new `header'
3377      ;; in the `dependencies' table.
3378      ((not (boundp id-dep))
3379       (set id-dep (list header)))
3380      ((null (car (symbol-value id-dep)))
3381       (setcar (symbol-value id-dep) header))
3382
3383      ;; From here the `header' was already present in the
3384      ;; `dependencies' table.
3385      (force-new
3386       ;; Overrides an existing entry;
3387       ;; just set the header part of the entry.
3388       (setcar (symbol-value id-dep) header))
3389
3390      ;; Renames the existing `header' to a unique Message-ID.
3391      ((not gnus-summary-ignore-duplicates)
3392       ;; An article with this Message-ID has already been seen.
3393       ;; We rename the Message-ID.
3394       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3395            (list header))
3396       (mail-header-set-id header id))
3397
3398      ;; The last case ignores an existing entry, except it adds any
3399      ;; additional Xrefs (in case the two articles came from different
3400      ;; servers.
3401      ;; Also sets `header' to `nil' meaning that the `dependencies'
3402      ;; table was *not* modified.
3403      (t
3404       (mail-header-set-xref
3405        (car (symbol-value id-dep))
3406        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3407                    "")
3408                (or (mail-header-xref header) "")))
3409       (setq header nil)))
3410
3411     (when header
3412       ;; First check if that we are not creating a References loop.
3413       (setq ref (gnus-parent-id (mail-header-references header)))
3414       (while (and ref
3415                   (setq ref-dep (intern-soft ref dependencies))
3416                   (boundp ref-dep)
3417                   (setq ref-header (car (symbol-value ref-dep))))
3418         (if (string= id ref)
3419             ;; Yuk!  This is a reference loop.  Make the article be a
3420             ;; root article.
3421             (progn
3422               (mail-header-set-references (car (symbol-value id-dep)) "none")
3423               (setq ref nil))
3424           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3425       (setq ref (gnus-parent-id (mail-header-references header)))
3426       (setq ref-dep (intern (or ref "none") dependencies))
3427       (if (boundp ref-dep)
3428           (setcdr (symbol-value ref-dep)
3429                   (nconc (cdr (symbol-value ref-dep))
3430                          (list (symbol-value id-dep))))
3431         (set ref-dep (list nil (symbol-value id-dep)))))
3432     header))
3433
3434 (defun gnus-build-sparse-threads ()
3435   (let ((headers gnus-newsgroup-headers)
3436         (mail-parse-charset gnus-newsgroup-charset)
3437         (gnus-summary-ignore-duplicates t)
3438         header references generation relations
3439         subject child end new-child date)
3440     ;; First we create an alist of generations/relations, where
3441     ;; generations is how much we trust the relation, and the relation
3442     ;; is parent/child.
3443     (gnus-message 7 "Making sparse threads...")
3444     (save-excursion
3445       (nnheader-set-temp-buffer " *gnus sparse threads*")
3446       (while (setq header (pop headers))
3447         (when (and (setq references (mail-header-references header))
3448                    (not (string= references "")))
3449           (insert references)
3450           (setq child (mail-header-id header)
3451                 subject (mail-header-subject header)
3452                 date (mail-header-date header)
3453                 generation 0)
3454           (while (search-backward ">" nil t)
3455             (setq end (1+ (point)))
3456             (when (search-backward "<" nil t)
3457               (setq new-child (buffer-substring (point) end))
3458               (push (list (incf generation)
3459                           child (setq child new-child)
3460                           subject date)
3461                     relations)))
3462           (when child
3463             (push (list (1+ generation) child nil subject) relations))
3464           (erase-buffer)))
3465       (kill-buffer (current-buffer)))
3466     ;; Sort over trustworthiness.
3467     (mapcar
3468      (lambda (relation)
3469        (when (gnus-dependencies-add-header
3470               (make-full-mail-header
3471                gnus-reffed-article-number
3472                (nth 3 relation) "" (or (nth 4 relation) "")
3473                (nth 1 relation)
3474                (or (nth 2 relation) "") 0 0 "")
3475               gnus-newsgroup-dependencies nil)
3476          (push gnus-reffed-article-number gnus-newsgroup-limit)
3477          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3478          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3479                gnus-newsgroup-reads)
3480          (decf gnus-reffed-article-number)))
3481      (sort relations 'car-less-than-car))
3482     (gnus-message 7 "Making sparse threads...done")))
3483
3484 (defun gnus-build-old-threads ()
3485   ;; Look at all the articles that refer back to old articles, and
3486   ;; fetch the headers for the articles that aren't there.  This will
3487   ;; build complete threads - if the roots haven't been expired by the
3488   ;; server, that is.
3489   (let ((mail-parse-charset gnus-newsgroup-charset)
3490         id heads)
3491     (mapatoms
3492      (lambda (refs)
3493        (when (not (car (symbol-value refs)))
3494          (setq heads (cdr (symbol-value refs)))
3495          (while heads
3496            (if (memq (mail-header-number (caar heads))
3497                      gnus-newsgroup-dormant)
3498                (setq heads (cdr heads))
3499              (setq id (symbol-name refs))
3500              (while (and (setq id (gnus-build-get-header id))
3501                          (not (car (gnus-id-to-thread id)))))
3502              (setq heads nil)))))
3503      gnus-newsgroup-dependencies)))
3504
3505 ;; This function has to be called with point after the article number
3506 ;; on the beginning of the line.
3507 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3508   (let ((eol (gnus-point-at-eol))
3509         (buffer (current-buffer))
3510         header)
3511
3512     ;; overview: [num subject from date id refs chars lines misc]
3513     (unwind-protect
3514         (progn
3515           (narrow-to-region (point) eol)
3516           (unless (eobp)
3517             (forward-char))
3518
3519           (setq header
3520                 (make-full-mail-header
3521                  number                 ; number
3522                  (funcall gnus-decode-encoded-word-function
3523                           (nnheader-nov-field)) ; subject
3524                  (funcall gnus-decode-encoded-word-function
3525                           (nnheader-nov-field)) ; from
3526                  (nnheader-nov-field)   ; date
3527                  (nnheader-nov-read-message-id) ; id
3528                  (nnheader-nov-field)   ; refs
3529                  (nnheader-nov-read-integer) ; chars
3530                  (nnheader-nov-read-integer) ; lines
3531                  (unless (eobp)
3532                    (if (looking-at "Xref: ")
3533                        (goto-char (match-end 0)))
3534                    (nnheader-nov-field)) ; Xref
3535                  (nnheader-nov-parse-extra)))) ; extra
3536
3537       (widen))
3538
3539     (when gnus-alter-header-function
3540       (funcall gnus-alter-header-function header))
3541     (gnus-dependencies-add-header header dependencies force-new)))
3542
3543 (defun gnus-build-get-header (id)
3544   "Look through the buffer of NOV lines and find the header to ID.
3545 Enter this line into the dependencies hash table, and return
3546 the id of the parent article (if any)."
3547   (let ((deps gnus-newsgroup-dependencies)
3548         found header)
3549     (prog1
3550         (save-excursion
3551           (set-buffer nntp-server-buffer)
3552           (let ((case-fold-search nil))
3553             (goto-char (point-min))
3554             (while (and (not found)
3555                         (search-forward id nil t))
3556               (beginning-of-line)
3557               (setq found (looking-at
3558                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3559                                    (regexp-quote id))))
3560               (or found (beginning-of-line 2)))
3561             (when found
3562               (beginning-of-line)
3563               (and
3564                (setq header (gnus-nov-parse-line
3565                              (read (current-buffer)) deps))
3566                (gnus-parent-id (mail-header-references header))))))
3567       (when header
3568         (let ((number (mail-header-number header)))
3569           (push number gnus-newsgroup-limit)
3570           (push header gnus-newsgroup-headers)
3571           (if (memq number gnus-newsgroup-unselected)
3572               (progn
3573                 (push number gnus-newsgroup-unreads)
3574                 (setq gnus-newsgroup-unselected
3575                       (delq number gnus-newsgroup-unselected)))
3576             (push number gnus-newsgroup-ancient)))))))
3577
3578 (defun gnus-build-all-threads ()
3579   "Read all the headers."
3580   (let ((gnus-summary-ignore-duplicates t)
3581         (mail-parse-charset gnus-newsgroup-charset)
3582         (dependencies gnus-newsgroup-dependencies)
3583         header article)
3584     (save-excursion
3585       (set-buffer nntp-server-buffer)
3586       (let ((case-fold-search nil))
3587         (goto-char (point-min))
3588         (while (not (eobp))
3589           (ignore-errors
3590             (setq article (read (current-buffer))
3591                   header (gnus-nov-parse-line article dependencies)))
3592           (when header
3593             (save-excursion
3594               (set-buffer gnus-summary-buffer)
3595               (push header gnus-newsgroup-headers)
3596               (if (memq (setq article (mail-header-number header))
3597                         gnus-newsgroup-unselected)
3598                   (progn
3599                     (push article gnus-newsgroup-unreads)
3600                     (setq gnus-newsgroup-unselected
3601                           (delq article gnus-newsgroup-unselected)))
3602                 (push article gnus-newsgroup-ancient)))
3603             (forward-line 1)))))))
3604
3605 (defun gnus-summary-update-article-line (article header)
3606   "Update the line for ARTICLE using HEADERS."
3607   (let* ((id (mail-header-id header))
3608          (thread (gnus-id-to-thread id)))
3609     (unless thread
3610       (error "Article in no thread"))
3611     ;; Update the thread.
3612     (setcar thread header)
3613     (gnus-summary-goto-subject article)
3614     (let* ((datal (gnus-data-find-list article))
3615            (data (car datal))
3616            (length (when (cdr datal)
3617                      (- (gnus-data-pos data)
3618                         (gnus-data-pos (cadr datal)))))
3619            (buffer-read-only nil)
3620            (level (gnus-summary-thread-level)))
3621       (gnus-delete-line)
3622       (gnus-summary-insert-line
3623        header level nil (gnus-article-mark article)
3624        (memq article gnus-newsgroup-replied)
3625        (memq article gnus-newsgroup-expirable)
3626        ;; Only insert the Subject string when it's different
3627        ;; from the previous Subject string.
3628        (if (and
3629             gnus-show-threads
3630             (gnus-subject-equal
3631              (condition-case ()
3632                  (mail-header-subject
3633                   (gnus-data-header
3634                    (cadr
3635                     (gnus-data-find-list
3636                      article
3637                      (gnus-data-list t)))))
3638                ;; Error on the side of excessive subjects.
3639                (error ""))
3640              (mail-header-subject header)))
3641            ""
3642          (mail-header-subject header))
3643        nil (cdr (assq article gnus-newsgroup-scored))
3644        (memq article gnus-newsgroup-processable))
3645       (when length
3646         (gnus-data-update-list
3647          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3648
3649 (defun gnus-summary-update-article (article &optional iheader)
3650   "Update ARTICLE in the summary buffer."
3651   (set-buffer gnus-summary-buffer)
3652   (let* ((header (gnus-summary-article-header article))
3653          (id (mail-header-id header))
3654          (data (gnus-data-find article))
3655          (thread (gnus-id-to-thread id))
3656          (references (mail-header-references header))
3657          (parent
3658           (gnus-id-to-thread
3659            (or (gnus-parent-id
3660                 (when (and references
3661                            (not (equal "" references)))
3662                   references))
3663                "none")))
3664          (buffer-read-only nil)
3665          (old (car thread)))
3666     (when thread
3667       (unless iheader
3668         (setcar thread nil)
3669         (when parent
3670           (delq thread parent)))
3671       (if (gnus-summary-insert-subject id header)
3672           ;; Set the (possibly) new article number in the data structure.
3673           (gnus-data-set-number data (gnus-id-to-article id))
3674         (setcar thread old)
3675         nil))))
3676
3677 (defun gnus-rebuild-thread (id &optional line)
3678   "Rebuild the thread containing ID.
3679 If LINE, insert the rebuilt thread starting on line LINE."
3680   (let ((buffer-read-only nil)
3681         old-pos current thread data)
3682     (if (not gnus-show-threads)
3683         (setq thread (list (car (gnus-id-to-thread id))))
3684       ;; Get the thread this article is part of.
3685       (setq thread (gnus-remove-thread id)))
3686     (setq old-pos (gnus-point-at-bol))
3687     (setq current (save-excursion
3688                     (and (zerop (forward-line -1))
3689                          (gnus-summary-article-number))))
3690     ;; If this is a gathered thread, we have to go some re-gathering.
3691     (when (stringp (car thread))
3692       (let ((subject (car thread))
3693             roots thr)
3694         (setq thread (cdr thread))
3695         (while thread
3696           (unless (memq (setq thr (gnus-id-to-thread
3697                                    (gnus-root-id
3698                                     (mail-header-id (caar thread)))))
3699                         roots)
3700             (push thr roots))
3701           (setq thread (cdr thread)))
3702         ;; We now have all (unique) roots.
3703         (if (= (length roots) 1)
3704             ;; All the loose roots are now one solid root.
3705             (setq thread (car roots))
3706           (setq thread (cons subject (gnus-sort-threads roots))))))
3707     (let (threads)
3708       ;; We then insert this thread into the summary buffer.
3709       (when line
3710         (goto-char (point-min))
3711         (forward-line (1- line)))
3712       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3713         (if gnus-show-threads
3714             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3715           (gnus-summary-prepare-unthreaded thread))
3716         (setq data (nreverse gnus-newsgroup-data))
3717         (setq threads gnus-newsgroup-threads))
3718       ;; We splice the new data into the data structure.
3719       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3720       ;;!!! then we want to insert at the beginning of the buffer.
3721       ;;!!! That happens to be true with Gnus now, but that may
3722       ;;!!! change in the future.  Perhaps.
3723       (gnus-data-enter-list
3724        (if line nil current) data (- (point) old-pos))
3725       (setq gnus-newsgroup-threads
3726             (nconc threads gnus-newsgroup-threads))
3727       (gnus-data-compute-positions))))
3728
3729 (defun gnus-number-to-header (number)
3730   "Return the header for article NUMBER."
3731   (let ((headers gnus-newsgroup-headers))
3732     (while (and headers
3733                 (not (= number (mail-header-number (car headers)))))
3734       (pop headers))
3735     (when headers
3736       (car headers))))
3737
3738 (defun gnus-parent-headers (in-headers &optional generation)
3739   "Return the headers of the GENERATIONeth parent of HEADERS."
3740   (unless generation
3741     (setq generation 1))
3742   (let ((parent t)
3743         (headers in-headers)
3744         references)
3745     (while (and parent
3746                 (not (zerop generation))
3747                 (setq references (mail-header-references headers)))
3748       (setq headers (if (and references
3749                              (setq parent (gnus-parent-id references)))
3750                         (car (gnus-id-to-thread parent))
3751                       nil))
3752       (decf generation))
3753     (and (not (eq headers in-headers))
3754          headers)))
3755
3756 (defun gnus-id-to-thread (id)
3757   "Return the (sub-)thread where ID appears."
3758   (gnus-gethash id gnus-newsgroup-dependencies))
3759
3760 (defun gnus-id-to-article (id)
3761   "Return the article number of ID."
3762   (let ((thread (gnus-id-to-thread id)))
3763     (when (and thread
3764                (car thread))
3765       (mail-header-number (car thread)))))
3766
3767 (defun gnus-id-to-header (id)
3768   "Return the article headers of ID."
3769   (car (gnus-id-to-thread id)))
3770
3771 (defun gnus-article-displayed-root-p (article)
3772   "Say whether ARTICLE is a root(ish) article."
3773   (let ((level (gnus-summary-thread-level article))
3774         (refs (mail-header-references  (gnus-summary-article-header article)))
3775         particle)
3776     (cond
3777      ((null level) nil)
3778      ((zerop level) t)
3779      ((null refs) t)
3780      ((null (gnus-parent-id refs)) t)
3781      ((and (= 1 level)
3782            (null (setq particle (gnus-id-to-article
3783                                  (gnus-parent-id refs))))
3784            (null (gnus-summary-thread-level particle)))))))
3785
3786 (defun gnus-root-id (id)
3787   "Return the id of the root of the thread where ID appears."
3788   (let (last-id prev)
3789     (while (and id (setq prev (car (gnus-id-to-thread id))))
3790       (setq last-id id
3791             id (gnus-parent-id (mail-header-references prev))))
3792     last-id))
3793
3794 (defun gnus-articles-in-thread (thread)
3795   "Return the list of articles in THREAD."
3796   (cons (mail-header-number (car thread))
3797         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3798
3799 (defun gnus-remove-thread (id &optional dont-remove)
3800   "Remove the thread that has ID in it."
3801   (let (headers thread last-id)
3802     ;; First go up in this thread until we find the root.
3803     (setq last-id (gnus-root-id id)
3804           headers (message-flatten-list (gnus-id-to-thread last-id)))
3805     ;; We have now found the real root of this thread.  It might have
3806     ;; been gathered into some loose thread, so we have to search
3807     ;; through the threads to find the thread we wanted.
3808     (let ((threads gnus-newsgroup-threads)
3809           sub)
3810       (while threads
3811         (setq sub (car threads))
3812         (if (stringp (car sub))
3813             ;; This is a gathered thread, so we look at the roots
3814             ;; below it to find whether this article is in this
3815             ;; gathered root.
3816             (progn
3817               (setq sub (cdr sub))
3818               (while sub
3819                 (when (member (caar sub) headers)
3820                   (setq thread (car threads)
3821                         threads nil
3822                         sub nil))
3823                 (setq sub (cdr sub))))
3824           ;; It's an ordinary thread, so we check it.
3825           (when (eq (car sub) (car headers))
3826             (setq thread sub
3827                   threads nil)))
3828         (setq threads (cdr threads)))
3829       ;; If this article is in no thread, then it's a root.
3830       (if thread
3831           (unless dont-remove
3832             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3833         (setq thread (gnus-id-to-thread last-id)))
3834       (when thread
3835         (prog1
3836             thread                      ; We return this thread.
3837           (unless dont-remove
3838             (if (stringp (car thread))
3839                 (progn
3840                   ;; If we use dummy roots, then we have to remove the
3841                   ;; dummy root as well.
3842                   (when (eq gnus-summary-make-false-root 'dummy)
3843                     ;; We go to the dummy root by going to
3844                     ;; the first sub-"thread", and then one line up.
3845                     (gnus-summary-goto-article
3846                      (mail-header-number (caadr thread)))
3847                     (forward-line -1)
3848                     (gnus-delete-line)
3849                     (gnus-data-compute-positions))
3850                   (setq thread (cdr thread))
3851                   (while thread
3852                     (gnus-remove-thread-1 (car thread))
3853                     (setq thread (cdr thread))))
3854               (gnus-remove-thread-1 thread))))))))
3855
3856 (defun gnus-remove-thread-1 (thread)
3857   "Remove the thread THREAD recursively."
3858   (let ((number (mail-header-number (pop thread)))
3859         d)
3860     (setq thread (reverse thread))
3861     (while thread
3862       (gnus-remove-thread-1 (pop thread)))
3863     (when (setq d (gnus-data-find number))
3864       (goto-char (gnus-data-pos d))
3865       (gnus-summary-show-thread)
3866       (gnus-data-remove
3867        number
3868        (- (gnus-point-at-bol)
3869           (prog1
3870               (1+ (gnus-point-at-eol))
3871             (gnus-delete-line)))))))
3872
3873 (defun gnus-sort-threads-1 (threads func)
3874   (sort (mapcar (lambda (thread)
3875                   (cons (car thread)
3876                         (and (cdr thread)
3877                              (gnus-sort-threads-1 (cdr thread) func))))
3878                 threads) func))
3879
3880 (defun gnus-sort-threads (threads)
3881   "Sort THREADS."
3882   (if (not gnus-thread-sort-functions)
3883       threads
3884     (gnus-message 8 "Sorting threads...")
3885     (prog1
3886         (gnus-sort-threads-1
3887          threads
3888          (gnus-make-sort-function gnus-thread-sort-functions))
3889       (gnus-message 8 "Sorting threads...done"))))
3890
3891 (defun gnus-sort-articles (articles)
3892   "Sort ARTICLES."
3893   (when gnus-article-sort-functions
3894     (gnus-message 7 "Sorting articles...")
3895     (prog1
3896         (setq gnus-newsgroup-headers
3897               (sort articles (gnus-make-sort-function
3898                               gnus-article-sort-functions)))
3899       (gnus-message 7 "Sorting articles...done"))))
3900
3901 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3902 (defmacro gnus-thread-header (thread)
3903   "Return header of first article in THREAD.
3904 Note that THREAD must never, ever be anything else than a variable -
3905 using some other form will lead to serious barfage."
3906   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3907   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3908   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3909         (vector thread) 2))
3910
3911 (defsubst gnus-article-sort-by-number (h1 h2)
3912   "Sort articles by article number."
3913   (< (mail-header-number h1)
3914      (mail-header-number h2)))
3915
3916 (defun gnus-thread-sort-by-number (h1 h2)
3917   "Sort threads by root article number."
3918   (gnus-article-sort-by-number
3919    (gnus-thread-header h1) (gnus-thread-header h2)))
3920
3921 (defsubst gnus-article-sort-by-lines (h1 h2)
3922   "Sort articles by article Lines header."
3923   (< (mail-header-lines h1)
3924      (mail-header-lines h2)))
3925
3926 (defun gnus-thread-sort-by-lines (h1 h2)
3927   "Sort threads by root article Lines header."
3928   (gnus-article-sort-by-lines
3929    (gnus-thread-header h1) (gnus-thread-header h2)))
3930
3931 (defsubst gnus-article-sort-by-chars (h1 h2)
3932   "Sort articles by octet length."
3933   (< (mail-header-chars h1)
3934      (mail-header-chars h2)))
3935
3936 (defun gnus-thread-sort-by-chars (h1 h2)
3937   "Sort threads by root article octet length."
3938   (gnus-article-sort-by-chars
3939    (gnus-thread-header h1) (gnus-thread-header h2)))
3940
3941 (defsubst gnus-article-sort-by-author (h1 h2)
3942   "Sort articles by root author."
3943   (string-lessp
3944    (let ((extract (funcall
3945                    gnus-extract-address-components
3946                    (mail-header-from h1))))
3947      (or (car extract) (cadr extract) ""))
3948    (let ((extract (funcall
3949                    gnus-extract-address-components
3950                    (mail-header-from h2))))
3951      (or (car extract) (cadr extract) ""))))
3952
3953 (defun gnus-thread-sort-by-author (h1 h2)
3954   "Sort threads by root author."
3955   (gnus-article-sort-by-author
3956    (gnus-thread-header h1)  (gnus-thread-header h2)))
3957
3958 (defsubst gnus-article-sort-by-subject (h1 h2)
3959   "Sort articles by root subject."
3960   (string-lessp
3961    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3962    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3963
3964 (defun gnus-thread-sort-by-subject (h1 h2)
3965   "Sort threads by root subject."
3966   (gnus-article-sort-by-subject
3967    (gnus-thread-header h1) (gnus-thread-header h2)))
3968
3969 (defsubst gnus-article-sort-by-date (h1 h2)
3970   "Sort articles by root article date."
3971   (time-less-p
3972    (gnus-date-get-time (mail-header-date h1))
3973    (gnus-date-get-time (mail-header-date h2))))
3974
3975 (defun gnus-thread-sort-by-date (h1 h2)
3976   "Sort threads by root article date."
3977   (gnus-article-sort-by-date
3978    (gnus-thread-header h1) (gnus-thread-header h2)))
3979
3980 (defsubst gnus-article-sort-by-score (h1 h2)
3981   "Sort articles by root article score.
3982 Unscored articles will be counted as having a score of zero."
3983   (> (or (cdr (assq (mail-header-number h1)
3984                     gnus-newsgroup-scored))
3985          gnus-summary-default-score 0)
3986      (or (cdr (assq (mail-header-number h2)
3987                     gnus-newsgroup-scored))
3988          gnus-summary-default-score 0)))
3989
3990 (defun gnus-thread-sort-by-score (h1 h2)
3991   "Sort threads by root article score."
3992   (gnus-article-sort-by-score
3993    (gnus-thread-header h1) (gnus-thread-header h2)))
3994
3995 (defun gnus-thread-sort-by-total-score (h1 h2)
3996   "Sort threads by the sum of all scores in the thread.
3997 Unscored articles will be counted as having a score of zero."
3998   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3999
4000 (defun gnus-thread-total-score (thread)
4001   ;; This function find the total score of THREAD.
4002   (cond ((null thread)
4003          0)
4004         ((consp thread)
4005          (if (stringp (car thread))
4006              (apply gnus-thread-score-function 0
4007                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4008            (gnus-thread-total-score-1 thread)))
4009         (t
4010          (gnus-thread-total-score-1 (list thread)))))
4011
4012 (defun gnus-thread-total-score-1 (root)
4013   ;; This function find the total score of the thread below ROOT.
4014   (setq root (car root))
4015   (apply gnus-thread-score-function
4016          (or (append
4017               (mapcar 'gnus-thread-total-score
4018                       (cdr (gnus-id-to-thread (mail-header-id root))))
4019               (when (> (mail-header-number root) 0)
4020                 (list (or (cdr (assq (mail-header-number root)
4021                                      gnus-newsgroup-scored))
4022                           gnus-summary-default-score 0))))
4023              (list gnus-summary-default-score)
4024              '(0))))
4025
4026 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4027 (defvar gnus-tmp-prev-subject nil)
4028 (defvar gnus-tmp-false-parent nil)
4029 (defvar gnus-tmp-root-expunged nil)
4030 (defvar gnus-tmp-dummy-line nil)
4031
4032 (eval-when-compile (defvar gnus-tmp-header))
4033 (defun gnus-extra-header (type &optional header)
4034   "Return the extra header of TYPE."
4035   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4036       ""))
4037
4038 (defun gnus-summary-prepare-threads (threads)
4039   "Prepare summary buffer from THREADS and indentation LEVEL.
4040 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4041 or a straight list of headers."
4042   (gnus-message 7 "Generating summary...")
4043
4044   (setq gnus-newsgroup-threads threads)
4045   (beginning-of-line)
4046
4047   (let ((gnus-tmp-level 0)
4048         (default-score (or gnus-summary-default-score 0))
4049         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4050         thread number subject stack state gnus-tmp-gathered beg-match
4051         new-roots gnus-tmp-new-adopts thread-end
4052         gnus-tmp-header gnus-tmp-unread
4053         gnus-tmp-replied gnus-tmp-subject-or-nil
4054         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4055         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4056         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4057
4058     (setq gnus-tmp-prev-subject nil)
4059
4060     (if (vectorp (car threads))
4061         ;; If this is a straight (sic) list of headers, then a
4062         ;; threaded summary display isn't required, so we just create
4063         ;; an unthreaded one.
4064         (gnus-summary-prepare-unthreaded threads)
4065
4066       ;; Do the threaded display.
4067
4068       (while (or threads stack gnus-tmp-new-adopts new-roots)
4069
4070         (if (and (= gnus-tmp-level 0)
4071                  (or (not stack)
4072                      (= (caar stack) 0))
4073                  (not gnus-tmp-false-parent)
4074                  (or gnus-tmp-new-adopts new-roots))
4075             (if gnus-tmp-new-adopts
4076                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4077                       thread (list (car gnus-tmp-new-adopts))
4078                       gnus-tmp-header (caar thread)
4079                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4080               (when new-roots
4081                 (setq thread (list (car new-roots))
4082                       gnus-tmp-header (caar thread)
4083                       new-roots (cdr new-roots))))
4084
4085           (if threads
4086               ;; If there are some threads, we do them before the
4087               ;; threads on the stack.
4088               (setq thread threads
4089                     gnus-tmp-header (caar thread))
4090             ;; There were no current threads, so we pop something off
4091             ;; the stack.
4092             (setq state (car stack)
4093                   gnus-tmp-level (car state)
4094                   thread (cdr state)
4095                   stack (cdr stack)
4096                   gnus-tmp-header (caar thread))))
4097
4098         (setq gnus-tmp-false-parent nil)
4099         (setq gnus-tmp-root-expunged nil)
4100         (setq thread-end nil)
4101
4102         (if (stringp gnus-tmp-header)
4103             ;; The header is a dummy root.
4104             (cond
4105              ((eq gnus-summary-make-false-root 'adopt)
4106               ;; We let the first article adopt the rest.
4107               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4108                                                (cddar thread)))
4109               (setq gnus-tmp-gathered
4110                     (nconc (mapcar
4111                             (lambda (h) (mail-header-number (car h)))
4112                             (cddar thread))
4113                            gnus-tmp-gathered))
4114               (setq thread (cons (list (caar thread)
4115                                        (cadar thread))
4116                                  (cdr thread)))
4117               (setq gnus-tmp-level -1
4118                     gnus-tmp-false-parent t))
4119              ((eq gnus-summary-make-false-root 'empty)
4120               ;; We print adopted articles with empty subject fields.
4121               (setq gnus-tmp-gathered
4122                     (nconc (mapcar
4123                             (lambda (h) (mail-header-number (car h)))
4124                             (cddar thread))
4125                            gnus-tmp-gathered))
4126               (setq gnus-tmp-level -1))
4127              ((eq gnus-summary-make-false-root 'dummy)
4128               ;; We remember that we probably want to output a dummy
4129               ;; root.
4130               (setq gnus-tmp-dummy-line gnus-tmp-header)
4131               (setq gnus-tmp-prev-subject gnus-tmp-header))
4132              (t
4133               ;; We do not make a root for the gathered
4134               ;; sub-threads at all.
4135               (setq gnus-tmp-level -1)))
4136
4137           (setq number (mail-header-number gnus-tmp-header)
4138                 subject (mail-header-subject gnus-tmp-header))
4139
4140           (cond
4141            ;; If the thread has changed subject, we might want to make
4142            ;; this subthread into a root.
4143            ((and (null gnus-thread-ignore-subject)
4144                  (not (zerop gnus-tmp-level))
4145                  gnus-tmp-prev-subject
4146                  (not (inline
4147                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4148             (setq new-roots (nconc new-roots (list (car thread)))
4149                   thread-end t
4150                   gnus-tmp-header nil))
4151            ;; If the article lies outside the current limit,
4152            ;; then we do not display it.
4153            ((not (memq number gnus-newsgroup-limit))
4154             (setq gnus-tmp-gathered
4155                   (nconc (mapcar
4156                           (lambda (h) (mail-header-number (car h)))
4157                           (cdar thread))
4158                          gnus-tmp-gathered))
4159             (setq gnus-tmp-new-adopts (if (cdar thread)
4160                                           (append gnus-tmp-new-adopts
4161                                                   (cdar thread))
4162                                         gnus-tmp-new-adopts)
4163                   thread-end t
4164                   gnus-tmp-header nil)
4165             (when (zerop gnus-tmp-level)
4166               (setq gnus-tmp-root-expunged t)))
4167            ;; Perhaps this article is to be marked as read?
4168            ((and gnus-summary-mark-below
4169                  (< (or (cdr (assq number gnus-newsgroup-scored))
4170                         default-score)
4171                     gnus-summary-mark-below)
4172                  ;; Don't touch sparse articles.
4173                  (not (gnus-summary-article-sparse-p number))
4174                  (not (gnus-summary-article-ancient-p number)))
4175             (setq gnus-newsgroup-unreads
4176                   (delq number gnus-newsgroup-unreads))
4177             (if gnus-newsgroup-auto-expire
4178                 (push number gnus-newsgroup-expirable)
4179               (push (cons number gnus-low-score-mark)
4180                     gnus-newsgroup-reads))))
4181
4182           (when gnus-tmp-header
4183             ;; We may have an old dummy line to output before this
4184             ;; article.
4185             (when (and gnus-tmp-dummy-line
4186                        (gnus-subject-equal
4187                         gnus-tmp-dummy-line
4188                         (mail-header-subject gnus-tmp-header)))
4189               (gnus-summary-insert-dummy-line
4190                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4191               (setq gnus-tmp-dummy-line nil))
4192
4193             ;; Compute the mark.
4194             (setq gnus-tmp-unread (gnus-article-mark number))
4195
4196             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4197                                   gnus-tmp-header gnus-tmp-level)
4198                   gnus-newsgroup-data)
4199
4200             ;; Actually insert the line.
4201             (setq
4202              gnus-tmp-subject-or-nil
4203              (cond
4204               ((and gnus-thread-ignore-subject
4205                     gnus-tmp-prev-subject
4206                     (not (inline (gnus-subject-equal
4207                                   gnus-tmp-prev-subject subject))))
4208                subject)
4209               ((zerop gnus-tmp-level)
4210                (if (and (eq gnus-summary-make-false-root 'empty)
4211                         (memq number gnus-tmp-gathered)
4212                         gnus-tmp-prev-subject
4213                         (inline (gnus-subject-equal
4214                                  gnus-tmp-prev-subject subject)))
4215                    gnus-summary-same-subject
4216                  subject))
4217               (t gnus-summary-same-subject)))
4218             (if (and (eq gnus-summary-make-false-root 'adopt)
4219                      (= gnus-tmp-level 1)
4220                      (memq number gnus-tmp-gathered))
4221                 (setq gnus-tmp-opening-bracket ?\<
4222                       gnus-tmp-closing-bracket ?\>)
4223               (setq gnus-tmp-opening-bracket ?\[
4224                     gnus-tmp-closing-bracket ?\]))
4225             (setq
4226              gnus-tmp-indentation
4227              (aref gnus-thread-indent-array gnus-tmp-level)
4228              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4229              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4230                                 gnus-summary-default-score 0)
4231              gnus-tmp-score-char
4232              (if (or (null gnus-summary-default-score)
4233                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4234                          gnus-summary-zcore-fuzz))
4235                  ?  ;Whitespace
4236                (if (< gnus-tmp-score gnus-summary-default-score)
4237                    gnus-score-below-mark gnus-score-over-mark))
4238              gnus-tmp-replied
4239              (cond ((memq number gnus-newsgroup-processable)
4240                     gnus-process-mark)
4241                    ((memq number gnus-newsgroup-cached)
4242                     gnus-cached-mark)
4243                    ((memq number gnus-newsgroup-replied)
4244                     gnus-replied-mark)
4245                    ((memq number gnus-newsgroup-saved)
4246                     gnus-saved-mark)
4247                    (t gnus-no-mark))
4248              gnus-tmp-from (mail-header-from gnus-tmp-header)
4249              gnus-tmp-name
4250              (cond
4251               ((string-match "<[^>]+> *$" gnus-tmp-from)
4252                (setq beg-match (match-beginning 0))
4253                (or (and (string-match "^\".+\"" gnus-tmp-from)
4254                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4255                    (substring gnus-tmp-from 0 beg-match)))
4256               ((string-match "(.+)" gnus-tmp-from)
4257                (substring gnus-tmp-from
4258                           (1+ (match-beginning 0)) (1- (match-end 0))))
4259               (t gnus-tmp-from)))
4260             (when (string= gnus-tmp-name "")
4261               (setq gnus-tmp-name gnus-tmp-from))
4262             (unless (numberp gnus-tmp-lines)
4263               (setq gnus-tmp-lines 0))
4264             (gnus-put-text-property
4265              (point)
4266              (progn (eval gnus-summary-line-format-spec) (point))
4267              'gnus-number number)
4268             (when gnus-visual-p
4269               (forward-line -1)
4270               (gnus-run-hooks 'gnus-summary-update-hook)
4271               (forward-line 1))
4272
4273             (setq gnus-tmp-prev-subject subject)))
4274
4275         (when (nth 1 thread)
4276           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4277         (incf gnus-tmp-level)
4278         (setq threads (if thread-end nil (cdar thread)))
4279         (unless threads
4280           (setq gnus-tmp-level 0)))))
4281   (gnus-message 7 "Generating summary...done"))
4282
4283 (defun gnus-summary-prepare-unthreaded (headers)
4284   "Generate an unthreaded summary buffer based on HEADERS."
4285   (let (header number mark)
4286
4287     (beginning-of-line)
4288
4289     (while headers
4290       ;; We may have to root out some bad articles...
4291       (when (memq (setq number (mail-header-number
4292                                 (setq header (pop headers))))
4293                   gnus-newsgroup-limit)
4294         ;; Mark article as read when it has a low score.
4295         (when (and gnus-summary-mark-below
4296                    (< (or (cdr (assq number gnus-newsgroup-scored))
4297                           gnus-summary-default-score 0)
4298                       gnus-summary-mark-below)
4299                    (not (gnus-summary-article-ancient-p number)))
4300           (setq gnus-newsgroup-unreads
4301                 (delq number gnus-newsgroup-unreads))
4302           (if gnus-newsgroup-auto-expire
4303               (push number gnus-newsgroup-expirable)
4304             (push (cons number gnus-low-score-mark)
4305                   gnus-newsgroup-reads)))
4306
4307         (setq mark (gnus-article-mark number))
4308         (push (gnus-data-make number mark (1+ (point)) header 0)
4309               gnus-newsgroup-data)
4310         (gnus-summary-insert-line
4311          header 0 number
4312          mark (memq number gnus-newsgroup-replied)
4313          (memq number gnus-newsgroup-expirable)
4314          (mail-header-subject header) nil
4315          (cdr (assq number gnus-newsgroup-scored))
4316          (memq number gnus-newsgroup-processable))))))
4317
4318 (defun gnus-summary-remove-list-identifiers ()
4319   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4320   (let ((regexp (if (consp gnus-list-identifiers)
4321                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4322                   gnus-list-identifiers))
4323         changed subject)
4324     (when regexp
4325       (dolist (header gnus-newsgroup-headers)
4326         (setq subject (mail-header-subject header)
4327               changed nil)
4328         (while (string-match
4329                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4330                 subject)
4331           (setq subject
4332                 (concat (substring subject 0 (match-beginning 2))
4333                         (substring subject (match-end 0)))
4334                 changed t))
4335         (when (and changed
4336                    (string-match
4337                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4338           (setq subject
4339                 (concat (substring subject 0 (match-beginning 1))
4340                         (substring subject (match-end 1)))))
4341         (when changed
4342           (mail-header-set-subject header subject))))))
4343
4344 (defun gnus-fetch-headers (articles)
4345   "Fetch headers of ARTICLES."
4346   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4347     (gnus-message 5 "Fetching headers for %s..." name)
4348     (prog1
4349         (if (eq 'nov
4350                 (setq gnus-headers-retrieved-by
4351                       (gnus-retrieve-headers
4352                        articles gnus-newsgroup-name
4353                        ;; We might want to fetch old headers, but
4354                        ;; not if there is only 1 article.
4355                        (and (or (and
4356                                  (not (eq gnus-fetch-old-headers 'some))
4357                                  (not (numberp gnus-fetch-old-headers)))
4358                                 (> (length articles) 1))
4359                           gnus-fetch-old-headers))))
4360             (gnus-get-newsgroup-headers-xover
4361              articles nil nil gnus-newsgroup-name t)
4362           (gnus-get-newsgroup-headers))
4363       (gnus-message 5 "Fetching headers for %s...done" name))))
4364
4365 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4366   "Select newsgroup GROUP.
4367 If READ-ALL is non-nil, all articles in the group are selected.
4368 If SELECT-ARTICLES, only select those articles from GROUP."
4369   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4370          ;;!!! Dirty hack; should be removed.
4371          (gnus-summary-ignore-duplicates
4372           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4373               t
4374             gnus-summary-ignore-duplicates))
4375          (info (nth 2 entry))
4376          articles fetched-articles cached)
4377
4378     (unless (gnus-check-server
4379              (setq gnus-current-select-method
4380                    (gnus-find-method-for-group group)))
4381       (error "Couldn't open server"))
4382
4383     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4384         (gnus-activate-group group)     ; Or we can activate it...
4385         (progn                          ; Or we bug out.
4386           (when (equal major-mode 'gnus-summary-mode)
4387             (kill-buffer (current-buffer)))
4388           (error "Couldn't activate group %s: %s"
4389                  group (gnus-status-message group))))
4390
4391     (unless (gnus-request-group group t)
4392       (when (equal major-mode 'gnus-summary-mode)
4393         (kill-buffer (current-buffer)))
4394       (error "Couldn't request group %s: %s"
4395              group (gnus-status-message group)))
4396
4397     (setq gnus-newsgroup-name group)
4398     (setq gnus-newsgroup-unselected nil)
4399     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4400     (gnus-summary-setup-default-charset)
4401
4402     ;; Adjust and set lists of article marks.
4403     (when info
4404       (gnus-adjust-marked-articles info))
4405
4406     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4407     (when (gnus-virtual-group-p group)
4408       (setq cached gnus-newsgroup-cached))
4409
4410     (setq gnus-newsgroup-unreads
4411           (gnus-set-difference
4412            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4413            gnus-newsgroup-dormant))
4414
4415     (setq gnus-newsgroup-processable nil)
4416
4417     (gnus-update-read-articles group gnus-newsgroup-unreads)
4418
4419     (if (setq articles select-articles)
4420         (setq gnus-newsgroup-unselected
4421               (gnus-sorted-intersection
4422                gnus-newsgroup-unreads
4423                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4424       (setq articles (gnus-articles-to-read group read-all)))
4425
4426     (cond
4427      ((null articles)
4428       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4429       'quit)
4430      ((eq articles 0) nil)
4431      (t
4432       ;; Init the dependencies hash table.
4433       (setq gnus-newsgroup-dependencies
4434             (gnus-make-hashtable (length articles)))
4435       (gnus-set-global-variables)
4436       ;; Retrieve the headers and read them in.
4437       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4438
4439       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4440       (when cached
4441         (setq gnus-newsgroup-cached cached))
4442
4443       ;; Suppress duplicates?
4444       (when gnus-suppress-duplicates
4445         (gnus-dup-suppress-articles))
4446
4447       ;; Set the initial limit.
4448       (setq gnus-newsgroup-limit (copy-sequence articles))
4449       ;; Remove canceled articles from the list of unread articles.
4450       (setq gnus-newsgroup-unreads
4451             (gnus-set-sorted-intersection
4452              gnus-newsgroup-unreads
4453              (setq fetched-articles
4454                    (mapcar (lambda (headers) (mail-header-number headers))
4455                            gnus-newsgroup-headers))))
4456       ;; Removed marked articles that do not exist.
4457       (gnus-update-missing-marks
4458        (gnus-sorted-complement fetched-articles articles))
4459       ;; We might want to build some more threads first.
4460       (when (and gnus-fetch-old-headers
4461                  (eq gnus-headers-retrieved-by 'nov))
4462         (if (eq gnus-fetch-old-headers 'invisible)
4463             (gnus-build-all-threads)
4464           (gnus-build-old-threads)))
4465       ;; Let the Gnus agent mark articles as read.
4466       (when gnus-agent
4467         (gnus-agent-get-undownloaded-list))
4468       ;; Remove list identifiers from subject
4469       (when gnus-list-identifiers
4470         (gnus-summary-remove-list-identifiers))
4471       ;; Check whether auto-expire is to be done in this group.
4472       (setq gnus-newsgroup-auto-expire
4473             (gnus-group-auto-expirable-p group))
4474       ;; Set up the article buffer now, if necessary.
4475       (unless gnus-single-article-buffer
4476         (gnus-article-setup-buffer))
4477       ;; First and last article in this newsgroup.
4478       (when gnus-newsgroup-headers
4479         (setq gnus-newsgroup-begin
4480               (mail-header-number (car gnus-newsgroup-headers))
4481               gnus-newsgroup-end
4482               (mail-header-number
4483                (gnus-last-element gnus-newsgroup-headers))))
4484       ;; GROUP is successfully selected.
4485       (or gnus-newsgroup-headers t)))))
4486
4487 (defun gnus-articles-to-read (group &optional read-all)
4488   "Find out what articles the user wants to read."
4489   (let* ((articles
4490           ;; Select all articles if `read-all' is non-nil, or if there
4491           ;; are no unread articles.
4492           (if (or read-all
4493                   (and (zerop (length gnus-newsgroup-marked))
4494                        (zerop (length gnus-newsgroup-unreads)))
4495                   (eq (gnus-group-find-parameter group 'display)
4496                       'all))
4497               (or
4498                (gnus-uncompress-range (gnus-active group))
4499                (gnus-cache-articles-in-group group))
4500             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4501                           (copy-sequence gnus-newsgroup-unreads))
4502                   '<)))
4503          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4504          (scored (length scored-list))
4505          (number (length articles))
4506          (marked (+ (length gnus-newsgroup-marked)
4507                     (length gnus-newsgroup-dormant)))
4508          (select
4509           (cond
4510            ((numberp read-all)
4511             read-all)
4512            (t
4513             (condition-case ()
4514                 (cond
4515                  ((and (or (<= scored marked) (= scored number))
4516                        (numberp gnus-large-newsgroup)
4517                        (> number gnus-large-newsgroup))
4518                   (let ((input
4519                          (read-string
4520                           (format
4521                            "How many articles from %s (default %d): "
4522                            (gnus-limit-string gnus-newsgroup-name 35)
4523                            number))))
4524                     (if (string-match "^[ \t]*$" input) number input)))
4525                  ((and (> scored marked) (< scored number)
4526                        (> (- scored number) 20))
4527                   (let ((input
4528                          (read-string
4529                           (format "%s %s (%d scored, %d total): "
4530                                   "How many articles from"
4531                                   group scored number))))
4532                     (if (string-match "^[ \t]*$" input)
4533                         number input)))
4534                  (t number))
4535               (quit
4536                (message "Quit getting the articles to read")
4537                nil))))))
4538     (setq select (if (stringp select) (string-to-number select) select))
4539     (if (or (null select) (zerop select))
4540         select
4541       (if (and (not (zerop scored)) (<= (abs select) scored))
4542           (progn
4543             (setq articles (sort scored-list '<))
4544             (setq number (length articles)))
4545         (setq articles (copy-sequence articles)))
4546
4547       (when (< (abs select) number)
4548         (if (< select 0)
4549             ;; Select the N oldest articles.
4550             (setcdr (nthcdr (1- (abs select)) articles) nil)
4551           ;; Select the N most recent articles.
4552           (setq articles (nthcdr (- number select) articles))))
4553       (setq gnus-newsgroup-unselected
4554             (gnus-sorted-intersection
4555              gnus-newsgroup-unreads
4556              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4557       (when gnus-alter-articles-to-read-function
4558         (setq gnus-newsgroup-unreads
4559               (sort
4560                (funcall gnus-alter-articles-to-read-function
4561                         gnus-newsgroup-name gnus-newsgroup-unreads)
4562                '<)))
4563       articles)))
4564
4565 (defun gnus-killed-articles (killed articles)
4566   (let (out)
4567     (while articles
4568       (when (inline (gnus-member-of-range (car articles) killed))
4569         (push (car articles) out))
4570       (setq articles (cdr articles)))
4571     out))
4572
4573 (defun gnus-uncompress-marks (marks)
4574   "Uncompress the mark ranges in MARKS."
4575   (let ((uncompressed '(score bookmark))
4576         out)
4577     (while marks
4578       (if (memq (caar marks) uncompressed)
4579           (push (car marks) out)
4580         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4581       (setq marks (cdr marks)))
4582     out))
4583
4584 (defun gnus-adjust-marked-articles (info)
4585   "Set all article lists and remove all marks that are no longer valid."
4586   (let* ((marked-lists (gnus-info-marks info))
4587          (active (gnus-active (gnus-info-group info)))
4588          (min (car active))
4589          (max (cdr active))
4590          (types gnus-article-mark-lists)
4591          (uncompressed '(score bookmark killed))
4592          marks var articles article mark)
4593
4594     (while marked-lists
4595       (setq marks (pop marked-lists))
4596       (set (setq var (intern (format "gnus-newsgroup-%s"
4597                                      (car (rassq (setq mark (car marks))
4598                                                  types)))))
4599            (if (memq (car marks) uncompressed) (cdr marks)
4600              (gnus-uncompress-range (cdr marks))))
4601
4602       (setq articles (symbol-value var))
4603
4604       ;; All articles have to be subsets of the active articles.
4605       (cond
4606        ;; Adjust "simple" lists.
4607        ((memq mark '(tick dormant expire reply save))
4608         (while articles
4609           (when (or (< (setq article (pop articles)) min) (> article max))
4610             (set var (delq article (symbol-value var))))))
4611        ;; Adjust assocs.
4612        ((memq mark uncompressed)
4613         (when (not (listp (cdr (symbol-value var))))
4614           (set var (list (symbol-value var))))
4615         (when (not (listp (cdr articles)))
4616           (setq articles (list articles)))
4617         (while articles
4618           (when (or (not (consp (setq article (pop articles))))
4619                     (< (car article) min)
4620                     (> (car article) max))
4621             (set var (delq article (symbol-value var))))))))))
4622
4623 (defun gnus-update-missing-marks (missing)
4624   "Go through the list of MISSING articles and remove them from the mark lists."
4625   (when missing
4626     (let ((types gnus-article-mark-lists)
4627           var m)
4628       ;; Go through all types.
4629       (while types
4630         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4631         (when (symbol-value var)
4632           ;; This list has articles.  So we delete all missing articles
4633           ;; from it.
4634           (setq m missing)
4635           (while m
4636             (set var (delq (pop m) (symbol-value var)))))))))
4637
4638 (defun gnus-update-marks ()
4639   "Enter the various lists of marked articles into the newsgroup info list."
4640   (let ((types gnus-article-mark-lists)
4641         (info (gnus-get-info gnus-newsgroup-name))
4642         (uncompressed '(score bookmark killed))
4643         type list newmarked symbol delta-marks)
4644     (when info
4645       ;; Add all marks lists to the list of marks lists.
4646       (while (setq type (pop types))
4647         (setq list (symbol-value
4648                     (setq symbol
4649                           (intern (format "gnus-newsgroup-%s"
4650                                           (car type))))))
4651
4652         (when list
4653           ;; Get rid of the entries of the articles that have the
4654           ;; default score.
4655           (when (and (eq (cdr type) 'score)
4656                      gnus-save-score
4657                      list)
4658             (let* ((arts list)
4659                    (prev (cons nil list))
4660                    (all prev))
4661               (while arts
4662                 (if (or (not (consp (car arts)))
4663                         (= (cdar arts) gnus-summary-default-score))
4664                     (setcdr prev (cdr arts))
4665                   (setq prev arts))
4666                 (setq arts (cdr arts)))
4667               (setq list (cdr all)))))
4668
4669         (unless (memq (cdr type) uncompressed)
4670           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4671
4672         (when (gnus-check-backend-function
4673                'request-set-mark gnus-newsgroup-name)
4674           ;; propagate flags to server, with the following exceptions:
4675           ;; uncompressed:s are not proper flags (they are cons cells)
4676           ;; cache is a internal gnus flag
4677           ;; download are local to one gnus installation (well)
4678           ;; unsend are for nndraft groups only
4679           ;; xxx: generality of this?  this suits nnimap anyway
4680           (unless (memq (cdr type) (append '(cache download unsend)
4681                                            uncompressed))
4682             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4683                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4684                    (add (gnus-remove-from-range
4685                          (gnus-copy-sequence list) old)))
4686               (when add
4687                 (push (list add 'add (list (cdr type))) delta-marks))
4688               (when del
4689                 (push (list del 'del (list (cdr type))) delta-marks)))))
4690
4691         (when list
4692           (push (cons (cdr type) list) newmarked)))
4693
4694       (when delta-marks
4695         (unless (gnus-check-group gnus-newsgroup-name)
4696           (error "Can't open server for %s" gnus-newsgroup-name))
4697         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4698
4699       ;; Enter these new marks into the info of the group.
4700       (if (nthcdr 3 info)
4701           (setcar (nthcdr 3 info) newmarked)
4702         ;; Add the marks lists to the end of the info.
4703         (when newmarked
4704           (setcdr (nthcdr 2 info) (list newmarked))))
4705
4706       ;; Cut off the end of the info if there's nothing else there.
4707       (let ((i 5))
4708         (while (and (> i 2)
4709                     (not (nth i info)))
4710           (when (nthcdr (decf i) info)
4711             (setcdr (nthcdr i info) nil)))))))
4712
4713 (defun gnus-set-mode-line (where)
4714   "Set the mode line of the article or summary buffers.
4715 If WHERE is `summary', the summary mode line format will be used."
4716   ;; Is this mode line one we keep updated?
4717   (when (and (memq where gnus-updated-mode-lines)
4718              (symbol-value
4719               (intern (format "gnus-%s-mode-line-format-spec" where))))
4720     (let (mode-string)
4721       (save-excursion
4722         ;; We evaluate this in the summary buffer since these
4723         ;; variables are buffer-local to that buffer.
4724         (set-buffer gnus-summary-buffer)
4725         ;; We bind all these variables that are used in the `eval' form
4726         ;; below.
4727         (let* ((mformat (symbol-value
4728                          (intern
4729                           (format "gnus-%s-mode-line-format-spec" where))))
4730                (gnus-tmp-group-name (gnus-group-decoded-name 
4731                                      gnus-newsgroup-name))
4732                (gnus-tmp-article-number (or gnus-current-article 0))
4733                (gnus-tmp-unread gnus-newsgroup-unreads)
4734                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4735                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4736                (gnus-tmp-unread-and-unselected
4737                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4738                             (zerop gnus-tmp-unselected))
4739                        "")
4740                       ((zerop gnus-tmp-unselected)
4741                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4742                       (t (format "{%d(+%d) more}"
4743                                  gnus-tmp-unread-and-unticked
4744                                  gnus-tmp-unselected))))
4745                (gnus-tmp-subject
4746                 (if (and gnus-current-headers
4747                          (vectorp gnus-current-headers))
4748                     (gnus-mode-string-quote
4749                      (mail-header-subject gnus-current-headers))
4750                   ""))
4751                bufname-length max-len
4752                gnus-tmp-header);; passed as argument to any user-format-funcs
4753           (setq mode-string (eval mformat))
4754           (setq bufname-length (if (string-match "%b" mode-string)
4755                                    (- (length
4756                                        (buffer-name
4757                                         (if (eq where 'summary)
4758                                             nil
4759                                           (get-buffer gnus-article-buffer))))
4760                                       2)
4761                                  0))
4762           (setq max-len (max 4 (if gnus-mode-non-string-length
4763                                    (- (window-width)
4764                                       gnus-mode-non-string-length
4765                                       bufname-length)
4766                                  (length mode-string))))
4767           ;; We might have to chop a bit of the string off...
4768           (when (> (length mode-string) max-len)
4769             (setq mode-string
4770                   (concat (truncate-string-to-width mode-string (- max-len 3))
4771                           "...")))
4772           ;; Pad the mode string a bit.
4773           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4774       ;; Update the mode line.
4775       (setq mode-line-buffer-identification
4776             (gnus-mode-line-buffer-identification (list mode-string)))
4777       (set-buffer-modified-p t))))
4778
4779 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4780   "Go through the HEADERS list and add all Xrefs to a hash table.
4781 The resulting hash table is returned, or nil if no Xrefs were found."
4782   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4783          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4784          (xref-hashtb (gnus-make-hashtable))
4785          start group entry number xrefs header)
4786     (while headers
4787       (setq header (pop headers))
4788       (when (and (setq xrefs (mail-header-xref header))
4789                  (not (memq (setq number (mail-header-number header))
4790                             unreads)))
4791         (setq start 0)
4792         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4793           (setq start (match-end 0))
4794           (setq group (if prefix
4795                           (concat prefix (substring xrefs (match-beginning 1)
4796                                                     (match-end 1)))
4797                         (substring xrefs (match-beginning 1) (match-end 1))))
4798           (setq number
4799                 (string-to-int (substring xrefs (match-beginning 2)
4800                                           (match-end 2))))
4801           (if (setq entry (gnus-gethash group xref-hashtb))
4802               (setcdr entry (cons number (cdr entry)))
4803             (gnus-sethash group (cons number nil) xref-hashtb)))))
4804     (and start xref-hashtb)))
4805
4806 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4807   "Look through all the headers and mark the Xrefs as read."
4808   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4809         name entry info xref-hashtb idlist method nth4)
4810     (save-excursion
4811       (set-buffer gnus-group-buffer)
4812       (when (setq xref-hashtb
4813                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4814         (mapatoms
4815          (lambda (group)
4816            (unless (string= from-newsgroup (setq name (symbol-name group)))
4817              (setq idlist (symbol-value group))
4818              ;; Dead groups are not updated.
4819              (and (prog1
4820                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4821                             info (nth 2 entry))
4822                     (when (stringp (setq nth4 (gnus-info-method info)))
4823                       (setq nth4 (gnus-server-to-method nth4))))
4824                   ;; Only do the xrefs if the group has the same
4825                   ;; select method as the group we have just read.
4826                   (or (gnus-methods-equal-p
4827                        nth4 (gnus-find-method-for-group from-newsgroup))
4828                       virtual
4829                       (equal nth4 (setq method (gnus-find-method-for-group
4830                                                 from-newsgroup)))
4831                       (and (equal (car nth4) (car method))
4832                            (equal (nth 1 nth4) (nth 1 method))))
4833                   gnus-use-cross-reference
4834                   (or (not (eq gnus-use-cross-reference t))
4835                       virtual
4836                       ;; Only do cross-references on subscribed
4837                       ;; groups, if that is what is wanted.
4838                       (<= (gnus-info-level info) gnus-level-subscribed))
4839                   (gnus-group-make-articles-read name idlist))))
4840          xref-hashtb)))))
4841
4842 (defun gnus-compute-read-articles (group articles)
4843   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4844          (info (nth 2 entry))
4845          (active (gnus-active group))
4846          ninfo)
4847     (when entry
4848       ;; First peel off all invalid article numbers.
4849       (when active
4850         (let ((ids articles)
4851               id first)
4852           (while (setq id (pop ids))
4853             (when (and first (> id (cdr active)))
4854               ;; We'll end up in this situation in one particular
4855               ;; obscure situation.  If you re-scan a group and get
4856               ;; a new article that is cross-posted to a different
4857               ;; group that has not been re-scanned, you might get
4858               ;; crossposted article that has a higher number than
4859               ;; Gnus believes possible.  So we re-activate this
4860               ;; group as well.  This might mean doing the
4861               ;; crossposting thingy will *increase* the number
4862               ;; of articles in some groups.  Tsk, tsk.
4863               (setq active (or (gnus-activate-group group) active)))
4864             (when (or (> id (cdr active))
4865                       (< id (car active)))
4866               (setq articles (delq id articles))))))
4867       ;; If the read list is nil, we init it.
4868       (if (and active
4869                (null (gnus-info-read info))
4870                (> (car active) 1))
4871           (setq ninfo (cons 1 (1- (car active))))
4872         (setq ninfo (gnus-info-read info)))
4873       ;; Then we add the read articles to the range.
4874       (gnus-add-to-range
4875        ninfo (setq articles (sort articles '<))))))
4876
4877 (defun gnus-group-make-articles-read (group articles)
4878   "Update the info of GROUP to say that ARTICLES are read."
4879   (let* ((num 0)
4880          (entry (gnus-gethash group gnus-newsrc-hashtb))
4881          (info (nth 2 entry))
4882          (active (gnus-active group))
4883          range)
4884     (when entry
4885       (setq range (gnus-compute-read-articles group articles))
4886       (save-excursion
4887         (set-buffer gnus-group-buffer)
4888         (gnus-undo-register
4889           `(progn
4890              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4891              (gnus-info-set-read ',info ',(gnus-info-read info))
4892              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4893              (gnus-group-update-group ,group t))))
4894       ;; Add the read articles to the range.
4895       (gnus-info-set-read info range)
4896       ;; Then we have to re-compute how many unread
4897       ;; articles there are in this group.
4898       (when active
4899         (cond
4900          ((not range)
4901           (setq num (- (1+ (cdr active)) (car active))))
4902          ((not (listp (cdr range)))
4903           (setq num (- (cdr active) (- (1+ (cdr range))
4904                                        (car range)))))
4905          (t
4906           (while range
4907             (if (numberp (car range))
4908                 (setq num (1+ num))
4909               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4910             (setq range (cdr range)))
4911           (setq num (- (cdr active) num))))
4912         ;; Update the number of unread articles.
4913         (setcar entry num)
4914         ;; Update the group buffer.
4915         (gnus-group-update-group group t)))))
4916
4917 (defvar gnus-newsgroup-none-id 0)
4918
4919 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4920   (let ((cur nntp-server-buffer)
4921         (dependencies
4922          (or dependencies
4923              (save-excursion (set-buffer gnus-summary-buffer)
4924                              gnus-newsgroup-dependencies)))
4925         headers id end ref
4926         (mail-parse-charset gnus-newsgroup-charset)
4927         (mail-parse-ignored-charsets
4928          (save-excursion (condition-case nil
4929                              (set-buffer gnus-summary-buffer)
4930                            (error))
4931                          gnus-newsgroup-ignored-charsets)))
4932     (save-excursion
4933       (set-buffer nntp-server-buffer)
4934       ;; Translate all TAB characters into SPACE characters.
4935       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4936       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4937       (gnus-run-hooks 'gnus-parse-headers-hook)
4938       (let ((case-fold-search t)
4939             in-reply-to header p lines chars)
4940         (goto-char (point-min))
4941         ;; Search to the beginning of the next header.  Error messages
4942         ;; do not begin with 2 or 3.
4943         (while (re-search-forward "^[23][0-9]+ " nil t)
4944           (setq id nil
4945                 ref nil)
4946           ;; This implementation of this function, with nine
4947           ;; search-forwards instead of the one re-search-forward and
4948           ;; a case (which basically was the old function) is actually
4949           ;; about twice as fast, even though it looks messier.  You
4950           ;; can't have everything, I guess.  Speed and elegance
4951           ;; doesn't always go hand in hand.
4952           (setq
4953            header
4954            (vector
4955             ;; Number.
4956             (prog1
4957                 (read cur)
4958               (end-of-line)
4959               (setq p (point))
4960               (narrow-to-region (point)
4961                                 (or (and (search-forward "\n.\n" nil t)
4962                                          (- (point) 2))
4963                                     (point))))
4964             ;; Subject.
4965             (progn
4966               (goto-char p)
4967               (if (search-forward "\nsubject: " nil t)
4968                   (funcall gnus-decode-encoded-word-function
4969                            (nnheader-header-value))
4970                 "(none)"))
4971             ;; From.
4972             (progn
4973               (goto-char p)
4974               (if (or (search-forward "\nfrom: " nil t)
4975                       (search-forward "\nfrom:" nil t))
4976                   (funcall gnus-decode-encoded-word-function
4977                            (nnheader-header-value))
4978                 "(nobody)"))
4979             ;; Date.
4980             (progn
4981               (goto-char p)
4982               (if (search-forward "\ndate: " nil t)
4983                   (nnheader-header-value) ""))
4984             ;; Message-ID.
4985             (progn
4986               (goto-char p)
4987               (setq id (if (re-search-forward
4988                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4989                            ;; We do it this way to make sure the Message-ID
4990                            ;; is (somewhat) syntactically valid.
4991                            (buffer-substring (match-beginning 1)
4992                                              (match-end 1))
4993                          ;; If there was no message-id, we just fake one
4994                          ;; to make subsequent routines simpler.
4995                          (nnheader-generate-fake-message-id))))
4996             ;; References.
4997             (progn
4998               (goto-char p)
4999               (if (search-forward "\nreferences: " nil t)
5000                   (progn
5001                     (setq end (point))
5002                     (prog1
5003                         (nnheader-header-value)
5004                       (setq ref
5005                             (buffer-substring
5006                              (progn
5007                                (end-of-line)
5008                                (search-backward ">" end t)
5009                                (1+ (point)))
5010                              (progn
5011                                (search-backward "<" end t)
5012                                (point))))))
5013                 ;; Get the references from the in-reply-to header if there
5014                 ;; were no references and the in-reply-to header looks
5015                 ;; promising.
5016                 (if (and (search-forward "\nin-reply-to: " nil t)
5017                          (setq in-reply-to (nnheader-header-value))
5018                          (string-match "<[^>]+>" in-reply-to))
5019                     (let (ref2)
5020                       (setq ref (substring in-reply-to (match-beginning 0)
5021                                            (match-end 0)))
5022                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5023                         (setq ref2 (substring in-reply-to (match-beginning 0)
5024                                               (match-end 0)))
5025                         (when (> (length ref2) (length ref))
5026                           (setq ref ref2)))
5027                       ref)
5028                   (setq ref nil))))
5029             ;; Chars.
5030             (progn
5031               (goto-char p)
5032               (if (search-forward "\nchars: " nil t)
5033                   (if (numberp (setq chars (ignore-errors (read cur))))
5034                       chars 0)
5035                 0))
5036             ;; Lines.
5037             (progn
5038               (goto-char p)
5039               (if (search-forward "\nlines: " nil t)
5040                   (if (numberp (setq lines (ignore-errors (read cur))))
5041                       lines 0)
5042                 0))
5043             ;; Xref.
5044             (progn
5045               (goto-char p)
5046               (and (search-forward "\nxref: " nil t)
5047                    (nnheader-header-value)))
5048             ;; Extra.
5049             (when gnus-extra-headers
5050               (let ((extra gnus-extra-headers)
5051                     out)
5052                 (while extra
5053                   (goto-char p)
5054                   (when (search-forward
5055                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5056                     (push (cons (car extra) (nnheader-header-value))
5057                           out))
5058                   (pop extra))
5059                 out))))
5060           (when (equal id ref)
5061             (setq ref nil))
5062
5063           (when gnus-alter-header-function
5064             (funcall gnus-alter-header-function header)
5065             (setq id (mail-header-id header)
5066                   ref (gnus-parent-id (mail-header-references header))))
5067
5068           (when (setq header
5069                       (gnus-dependencies-add-header
5070                        header dependencies force-new))
5071             (push header headers))
5072           (goto-char (point-max))
5073           (widen))
5074         (nreverse headers)))))
5075
5076 ;; Goes through the xover lines and returns a list of vectors
5077 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5078                                                   force-new dependencies
5079                                                   group also-fetch-heads)
5080   "Parse the news overview data in the server buffer.
5081 Return a list of headers that match SEQUENCE (see
5082 `nntp-retrieve-headers')."
5083   ;; Get the Xref when the users reads the articles since most/some
5084   ;; NNTP servers do not include Xrefs when using XOVER.
5085   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5086   (let ((mail-parse-charset gnus-newsgroup-charset)
5087         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5088         (cur nntp-server-buffer)
5089         (dependencies (or dependencies gnus-newsgroup-dependencies))
5090         number headers header)
5091     (save-excursion
5092       (set-buffer nntp-server-buffer)
5093       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5094       ;; Allow the user to mangle the headers before parsing them.
5095       (gnus-run-hooks 'gnus-parse-headers-hook)
5096       (goto-char (point-min))
5097       (while (not (eobp))
5098         (condition-case ()
5099             (while (and sequence (not (eobp)))
5100               (setq number (read cur))
5101               (while (and sequence
5102                           (< (car sequence) number))
5103                 (setq sequence (cdr sequence)))
5104               (and sequence
5105                    (eq number (car sequence))
5106                    (progn
5107                      (setq sequence (cdr sequence))
5108                      (setq header (inline
5109                                     (gnus-nov-parse-line
5110                                      number dependencies force-new))))
5111                    (push header headers))
5112               (forward-line 1))
5113           (error
5114            (gnus-error 4 "Strange nov line (%d)"
5115                        (count-lines (point-min) (point)))))
5116         (forward-line 1))
5117       ;; A common bug in inn is that if you have posted an article and
5118       ;; then retrieves the active file, it will answer correctly --
5119       ;; the new article is included.  However, a NOV entry for the
5120       ;; article may not have been generated yet, so this may fail.
5121       ;; We work around this problem by retrieving the last few
5122       ;; headers using HEAD.
5123       (if (or (not also-fetch-heads)
5124               (not sequence))
5125           ;; We (probably) got all the headers.
5126           (nreverse headers)
5127         (let ((gnus-nov-is-evil t))
5128           (nconc
5129            (nreverse headers)
5130            (when (gnus-retrieve-headers sequence group)
5131              (gnus-get-newsgroup-headers))))))))
5132
5133 (defun gnus-article-get-xrefs ()
5134   "Fill in the Xref value in `gnus-current-headers', if necessary.
5135 This is meant to be called in `gnus-article-internal-prepare-hook'."
5136   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5137                                  gnus-current-headers)))
5138     (or (not gnus-use-cross-reference)
5139         (not headers)
5140         (and (mail-header-xref headers)
5141              (not (string= (mail-header-xref headers) "")))
5142         (let ((case-fold-search t)
5143               xref)
5144           (save-restriction
5145             (nnheader-narrow-to-headers)
5146             (goto-char (point-min))
5147             (when (or (and (not (eobp))
5148                            (eq (downcase (char-after)) ?x)
5149                            (looking-at "Xref:"))
5150                       (search-forward "\nXref:" nil t))
5151               (goto-char (1+ (match-end 0)))
5152               (setq xref (buffer-substring (point)
5153                                            (progn (end-of-line) (point))))
5154               (mail-header-set-xref headers xref)))))))
5155
5156 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5157   "Find article ID and insert the summary line for that article.
5158 OLD-HEADER can either be a header or a line number to insert
5159 the subject line on."
5160   (let* ((line (and (numberp old-header) old-header))
5161          (old-header (and (vectorp old-header) old-header))
5162          (header (cond ((and old-header use-old-header)
5163                         old-header)
5164                        ((and (numberp id)
5165                              (gnus-number-to-header id))
5166                         (gnus-number-to-header id))
5167                        (t
5168                         (gnus-read-header id))))
5169          (number (and (numberp id) id))
5170          d)
5171     (when header
5172       ;; Rebuild the thread that this article is part of and go to the
5173       ;; article we have fetched.
5174       (when (and (not gnus-show-threads)
5175                  old-header)
5176         (when (and number
5177                    (setq d (gnus-data-find (mail-header-number old-header))))
5178           (goto-char (gnus-data-pos d))
5179           (gnus-data-remove
5180            number
5181            (- (gnus-point-at-bol)
5182               (prog1
5183                   (1+ (gnus-point-at-eol))
5184                 (gnus-delete-line))))))
5185       (when old-header
5186         (mail-header-set-number header (mail-header-number old-header)))
5187       (setq gnus-newsgroup-sparse
5188             (delq (setq number (mail-header-number header))
5189                   gnus-newsgroup-sparse))
5190       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5191       (push number gnus-newsgroup-limit)
5192       (gnus-rebuild-thread (mail-header-id header) line)
5193       (gnus-summary-goto-subject number nil t))
5194     (when (and (numberp number)
5195                (> number 0))
5196       ;; We have to update the boundaries even if we can't fetch the
5197       ;; article if ID is a number -- so that the next `P' or `N'
5198       ;; command will fetch the previous (or next) article even
5199       ;; if the one we tried to fetch this time has been canceled.
5200       (when (> number gnus-newsgroup-end)
5201         (setq gnus-newsgroup-end number))
5202       (when (< number gnus-newsgroup-begin)
5203         (setq gnus-newsgroup-begin number))
5204       (setq gnus-newsgroup-unselected
5205             (delq number gnus-newsgroup-unselected)))
5206     ;; Report back a success?
5207     (and header (mail-header-number header))))
5208
5209 ;;; Process/prefix in the summary buffer
5210
5211 (defun gnus-summary-work-articles (n)
5212   "Return a list of articles to be worked upon.
5213 The prefix argument, the list of process marked articles, and the
5214 current article will be taken into consideration."
5215   (save-excursion
5216     (set-buffer gnus-summary-buffer)
5217     (cond
5218      (n
5219       ;; A numerical prefix has been given.
5220       (setq n (prefix-numeric-value n))
5221       (let ((backward (< n 0))
5222             (n (abs (prefix-numeric-value n)))
5223             articles article)
5224         (save-excursion
5225           (while
5226               (and (> n 0)
5227                    (push (setq article (gnus-summary-article-number))
5228                          articles)
5229                    (if backward
5230                        (gnus-summary-find-prev nil article)
5231                      (gnus-summary-find-next nil article)))
5232             (decf n)))
5233         (nreverse articles)))
5234      ((and (gnus-region-active-p) (mark))
5235       (message "region active")
5236       ;; Work on the region between point and mark.
5237       (let ((max (max (point) (mark)))
5238             articles article)
5239         (save-excursion
5240           (goto-char (min (min (point) (mark))))
5241           (while
5242               (and
5243                (push (setq article (gnus-summary-article-number)) articles)
5244                (gnus-summary-find-next nil article)
5245                (< (point) max)))
5246           (nreverse articles))))
5247      (gnus-newsgroup-processable
5248       ;; There are process-marked articles present.
5249       ;; Save current state.
5250       (gnus-summary-save-process-mark)
5251       ;; Return the list.
5252       (reverse gnus-newsgroup-processable))
5253      (t
5254       ;; Just return the current article.
5255       (list (gnus-summary-article-number))))))
5256
5257 (defmacro gnus-summary-iterate (arg &rest forms)
5258   "Iterate over the process/prefixed articles and do FORMS.
5259 ARG is the interactive prefix given to the command.  FORMS will be
5260 executed with point over the summary line of the articles."
5261   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5262     `(let ((,articles (gnus-summary-work-articles ,arg)))
5263        (while ,articles
5264          (gnus-summary-goto-subject (car ,articles))
5265          ,@forms
5266          (pop ,articles)))))
5267
5268 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5269 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5270
5271 (defun gnus-summary-save-process-mark ()
5272   "Push the current set of process marked articles on the stack."
5273   (interactive)
5274   (push (copy-sequence gnus-newsgroup-processable)
5275         gnus-newsgroup-process-stack))
5276
5277 (defun gnus-summary-kill-process-mark ()
5278   "Push the current set of process marked articles on the stack and unmark."
5279   (interactive)
5280   (gnus-summary-save-process-mark)
5281   (gnus-summary-unmark-all-processable))
5282
5283 (defun gnus-summary-yank-process-mark ()
5284   "Pop the last process mark state off the stack and restore it."
5285   (interactive)
5286   (unless gnus-newsgroup-process-stack
5287     (error "Empty mark stack"))
5288   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5289
5290 (defun gnus-summary-process-mark-set (set)
5291   "Make SET into the current process marked articles."
5292   (gnus-summary-unmark-all-processable)
5293   (while set
5294     (gnus-summary-set-process-mark (pop set))))
5295
5296 ;;; Searching and stuff
5297
5298 (defun gnus-summary-search-group (&optional backward use-level)
5299   "Search for next unread newsgroup.
5300 If optional argument BACKWARD is non-nil, search backward instead."
5301   (save-excursion
5302     (set-buffer gnus-group-buffer)
5303     (when (gnus-group-search-forward
5304            backward nil (if use-level (gnus-group-group-level) nil))
5305       (gnus-group-group-name))))
5306
5307 (defun gnus-summary-best-group (&optional exclude-group)
5308   "Find the name of the best unread group.
5309 If EXCLUDE-GROUP, do not go to this group."
5310   (save-excursion
5311     (set-buffer gnus-group-buffer)
5312     (save-excursion
5313       (gnus-group-best-unread-group exclude-group))))
5314
5315 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5316   (if backward (gnus-summary-find-prev)
5317     (let* ((dummy (gnus-summary-article-intangible-p))
5318            (article (or article (gnus-summary-article-number)))
5319            (arts (gnus-data-find-list article))
5320            result)
5321       (when (and (not dummy)
5322                  (or (not gnus-summary-check-current)
5323                      (not unread)
5324                      (not (gnus-data-unread-p (car arts)))))
5325         (setq arts (cdr arts)))
5326       (when (setq result
5327                   (if unread
5328                       (progn
5329                         (while arts
5330                           (when (or (and undownloaded
5331                                          (eq gnus-undownloaded-mark
5332                                              (gnus-data-mark (car arts))))
5333                                     (gnus-data-unread-p (car arts)))
5334                             (setq result (car arts)
5335                                   arts nil))
5336                           (setq arts (cdr arts)))
5337                         result)
5338                     (car arts)))
5339         (goto-char (gnus-data-pos result))
5340         (gnus-data-number result)))))
5341
5342 (defun gnus-summary-find-prev (&optional unread article)
5343   (let* ((eobp (eobp))
5344          (article (or article (gnus-summary-article-number)))
5345          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5346          result)
5347     (when (and (not eobp)
5348                (or (not gnus-summary-check-current)
5349                    (not unread)
5350                    (not (gnus-data-unread-p (car arts)))))
5351       (setq arts (cdr arts)))
5352     (when (setq result
5353                 (if unread
5354                     (progn
5355                       (while arts
5356                         (when (gnus-data-unread-p (car arts))
5357                           (setq result (car arts)
5358                                 arts nil))
5359                         (setq arts (cdr arts)))
5360                       result)
5361                   (car arts)))
5362       (goto-char (gnus-data-pos result))
5363       (gnus-data-number result))))
5364
5365 (defun gnus-summary-find-subject (subject &optional unread backward article)
5366   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5367          (article (or article (gnus-summary-article-number)))
5368          (articles (gnus-data-list backward))
5369          (arts (gnus-data-find-list article articles))
5370          result)
5371     (when (or (not gnus-summary-check-current)
5372               (not unread)
5373               (not (gnus-data-unread-p (car arts))))
5374       (setq arts (cdr arts)))
5375     (while arts
5376       (and (or (not unread)
5377                (gnus-data-unread-p (car arts)))
5378            (vectorp (gnus-data-header (car arts)))
5379            (gnus-subject-equal
5380             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5381            (setq result (car arts)
5382                  arts nil))
5383       (setq arts (cdr arts)))
5384     (and result
5385          (goto-char (gnus-data-pos result))
5386          (gnus-data-number result))))
5387
5388 (defun gnus-summary-search-forward (&optional unread subject backward)
5389   "Search forward for an article.
5390 If UNREAD, look for unread articles.  If SUBJECT, look for
5391 articles with that subject.  If BACKWARD, search backward instead."
5392   (cond (subject (gnus-summary-find-subject subject unread backward))
5393         (backward (gnus-summary-find-prev unread))
5394         (t (gnus-summary-find-next unread))))
5395
5396 (defun gnus-recenter (&optional n)
5397   "Center point in window and redisplay frame.
5398 Also do horizontal recentering."
5399   (interactive "P")
5400   (when (and gnus-auto-center-summary
5401              (not (eq gnus-auto-center-summary 'vertical)))
5402     (gnus-horizontal-recenter))
5403   (recenter n))
5404
5405 (defun gnus-summary-recenter ()
5406   "Center point in the summary window.
5407 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5408 displayed, no centering will be performed."
5409   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5410   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5411   (interactive)
5412   (let* ((top (cond ((< (window-height) 4) 0)
5413                     ((< (window-height) 7) 1)
5414                     (t (if (numberp gnus-auto-center-summary)
5415                            gnus-auto-center-summary
5416                          2))))
5417          (height (1- (window-height)))
5418          (bottom (save-excursion (goto-char (point-max))
5419                                  (forward-line (- height))
5420                                  (point)))
5421          (window (get-buffer-window (current-buffer))))
5422     ;; The user has to want it.
5423     (when gnus-auto-center-summary
5424       (when (get-buffer-window gnus-article-buffer)
5425         ;; Only do recentering when the article buffer is displayed,
5426         ;; Set the window start to either `bottom', which is the biggest
5427         ;; possible valid number, or the second line from the top,
5428         ;; whichever is the least.
5429         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5430           (if (> bottom top-pos)
5431               ;; Keep the second line from the top visible
5432               (set-window-start window top-pos t)
5433             ;; Try to keep the bottom line visible; if it's partially
5434             ;; obscured, either scroll one more line to make it fully
5435             ;; visible, or revert to using TOP-POS.
5436             (save-excursion
5437               (goto-char (point-max))
5438               (forward-line -1)
5439               (let ((last-line-start (point)))
5440                 (goto-char bottom)
5441                 (set-window-start window (point) t)
5442                 (when (not (pos-visible-in-window-p last-line-start window))
5443                   (forward-line 1)
5444                   (set-window-start window (min (point) top-pos) t)))))))
5445       ;; Do horizontal recentering while we're at it.
5446       (when (and (get-buffer-window (current-buffer) t)
5447                  (not (eq gnus-auto-center-summary 'vertical)))
5448         (let ((selected (selected-window)))
5449           (select-window (get-buffer-window (current-buffer) t))
5450           (gnus-summary-position-point)
5451           (gnus-horizontal-recenter)
5452           (select-window selected))))))
5453
5454 (defun gnus-summary-jump-to-group (newsgroup)
5455   "Move point to NEWSGROUP in group mode buffer."
5456   ;; Keep update point of group mode buffer if visible.
5457   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5458       (save-window-excursion
5459         ;; Take care of tree window mode.
5460         (when (get-buffer-window gnus-group-buffer)
5461           (pop-to-buffer gnus-group-buffer))
5462         (gnus-group-jump-to-group newsgroup))
5463     (save-excursion
5464       ;; Take care of tree window mode.
5465       (if (get-buffer-window gnus-group-buffer)
5466           (pop-to-buffer gnus-group-buffer)
5467         (set-buffer gnus-group-buffer))
5468       (gnus-group-jump-to-group newsgroup))))
5469
5470 ;; This function returns a list of article numbers based on the
5471 ;; difference between the ranges of read articles in this group and
5472 ;; the range of active articles.
5473 (defun gnus-list-of-unread-articles (group)
5474   (let* ((read (gnus-info-read (gnus-get-info group)))
5475          (active (or (gnus-active group) (gnus-activate-group group)))
5476          (last (cdr active))
5477          first nlast unread)
5478     ;; If none are read, then all are unread.
5479     (if (not read)
5480         (setq first (car active))
5481       ;; If the range of read articles is a single range, then the
5482       ;; first unread article is the article after the last read
5483       ;; article.  Sounds logical, doesn't it?
5484       (if (and (not (listp (cdr read)))
5485                (or (< (car read) (car active))
5486                    (progn (setq read (list read))
5487                           nil)))
5488           (setq first (max (car active) (1+ (cdr read))))
5489         ;; `read' is a list of ranges.
5490         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5491                                   (caar read)))
5492                   1)
5493           (setq first (car active)))
5494         (while read
5495           (when first
5496             (while (< first nlast)
5497               (push first unread)
5498               (setq first (1+ first))))
5499           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5500           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5501           (setq read (cdr read)))))
5502     ;; And add the last unread articles.
5503     (while (<= first last)
5504       (push first unread)
5505       (setq first (1+ first)))
5506     ;; Return the list of unread articles.
5507     (delq 0 (nreverse unread))))
5508
5509 (defun gnus-list-of-read-articles (group)
5510   "Return a list of unread, unticked and non-dormant articles."
5511   (let* ((info (gnus-get-info group))
5512          (marked (gnus-info-marks info))
5513          (active (gnus-active group)))
5514     (and info active
5515          (gnus-set-difference
5516           (gnus-sorted-complement
5517            (gnus-uncompress-range active)
5518            (gnus-list-of-unread-articles group))
5519           (append
5520            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5521            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5522
5523 ;; Various summary commands
5524
5525 (defun gnus-summary-select-article-buffer ()
5526   "Reconfigure windows to show article buffer."
5527   (interactive)
5528   (if (not (gnus-buffer-live-p gnus-article-buffer))
5529       (error "There is no article buffer for this summary buffer")
5530     (gnus-configure-windows 'article)
5531     (select-window (get-buffer-window gnus-article-buffer))))
5532
5533 (defun gnus-summary-universal-argument (arg)
5534   "Perform any operation on all articles that are process/prefixed."
5535   (interactive "P")
5536   (let ((articles (gnus-summary-work-articles arg))
5537         func article)
5538     (if (eq
5539          (setq
5540           func
5541           (key-binding
5542            (read-key-sequence
5543             (substitute-command-keys
5544              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5545          'undefined)
5546         (gnus-error 1 "Undefined key")
5547       (save-excursion
5548         (while articles
5549           (gnus-summary-goto-subject (setq article (pop articles)))
5550           (let (gnus-newsgroup-processable)
5551             (command-execute func))
5552           (gnus-summary-remove-process-mark article)))))
5553   (gnus-summary-position-point))
5554
5555 (defun gnus-summary-toggle-truncation (&optional arg)
5556   "Toggle truncation of summary lines.
5557 With arg, turn line truncation on iff arg is positive."
5558   (interactive "P")
5559   (setq truncate-lines
5560         (if (null arg) (not truncate-lines)
5561           (> (prefix-numeric-value arg) 0)))
5562   (redraw-display))
5563
5564 (defun gnus-summary-reselect-current-group (&optional all rescan)
5565   "Exit and then reselect the current newsgroup.
5566 The prefix argument ALL means to select all articles."
5567   (interactive "P")
5568   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5569     (error "Ephemeral groups can't be reselected"))
5570   (let ((current-subject (gnus-summary-article-number))
5571         (group gnus-newsgroup-name))
5572     (setq gnus-newsgroup-begin nil)
5573     (gnus-summary-exit)
5574     ;; We have to adjust the point of group mode buffer because
5575     ;; point was moved to the next unread newsgroup by exiting.
5576     (gnus-summary-jump-to-group group)
5577     (when rescan
5578       (save-excursion
5579         (gnus-group-get-new-news-this-group 1)))
5580     (gnus-group-read-group all t)
5581     (gnus-summary-goto-subject current-subject nil t)))
5582
5583 (defun gnus-summary-rescan-group (&optional all)
5584   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5585   (interactive "P")
5586   (gnus-summary-reselect-current-group all t))
5587
5588 (defun gnus-summary-update-info (&optional non-destructive)
5589   (save-excursion
5590     (let ((group gnus-newsgroup-name))
5591       (when group
5592         (when gnus-newsgroup-kill-headers
5593           (setq gnus-newsgroup-killed
5594                 (gnus-compress-sequence
5595                  (nconc
5596                   (gnus-set-sorted-intersection
5597                    (gnus-uncompress-range gnus-newsgroup-killed)
5598                    (setq gnus-newsgroup-unselected
5599                          (sort gnus-newsgroup-unselected '<)))
5600                   (setq gnus-newsgroup-unreads
5601                         (sort gnus-newsgroup-unreads '<)))
5602                  t)))
5603         (unless (listp (cdr gnus-newsgroup-killed))
5604           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5605         (let ((headers gnus-newsgroup-headers))
5606           ;; Set the new ranges of read articles.
5607           (save-excursion
5608             (set-buffer gnus-group-buffer)
5609             (gnus-undo-force-boundary))
5610           (gnus-update-read-articles
5611            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5612           ;; Set the current article marks.
5613           (let ((gnus-newsgroup-scored
5614                  (if (and (not gnus-save-score)
5615                           (not non-destructive))
5616                      nil
5617                    gnus-newsgroup-scored)))
5618             (save-excursion
5619               (gnus-update-marks)))
5620           ;; Do the cross-ref thing.
5621           (when gnus-use-cross-reference
5622             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5623           ;; Do not switch windows but change the buffer to work.
5624           (set-buffer gnus-group-buffer)
5625           (unless (gnus-ephemeral-group-p group)
5626             (gnus-group-update-group group)))))))
5627
5628 (defun gnus-summary-save-newsrc (&optional force)
5629   "Save the current number of read/marked articles in the dribble buffer.
5630 The dribble buffer will then be saved.
5631 If FORCE (the prefix), also save the .newsrc file(s)."
5632   (interactive "P")
5633   (gnus-summary-update-info t)
5634   (if force
5635       (gnus-save-newsrc-file)
5636     (gnus-dribble-save)))
5637
5638 (defun gnus-summary-exit (&optional temporary)
5639   "Exit reading current newsgroup, and then return to group selection mode.
5640 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5641   (interactive)
5642   (gnus-set-global-variables)
5643   (when (gnus-buffer-live-p gnus-article-buffer)
5644     (save-excursion
5645       (set-buffer gnus-article-buffer)
5646       (mm-destroy-parts gnus-article-mime-handles)
5647       ;; Set it to nil for safety reason.
5648       (setq gnus-article-mime-handle-alist nil)
5649       (setq gnus-article-mime-handles nil)))
5650   (gnus-kill-save-kill-buffer)
5651   (gnus-async-halt-prefetch)
5652   (let* ((group gnus-newsgroup-name)
5653          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5654          (mode major-mode)
5655          (group-point nil)
5656          (buf (current-buffer)))
5657     (unless quit-config
5658       ;; Do adaptive scoring, and possibly save score files.
5659       (when gnus-newsgroup-adaptive
5660         (gnus-score-adaptive))
5661       (when gnus-use-scoring
5662         (gnus-score-save)))
5663     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5664     ;; If we have several article buffers, we kill them at exit.
5665     (unless gnus-single-article-buffer
5666       (gnus-kill-buffer gnus-original-article-buffer)
5667       (setq gnus-article-current nil))
5668     (when gnus-use-cache
5669       (gnus-cache-possibly-remove-articles)
5670       (gnus-cache-save-buffers))
5671     (gnus-async-prefetch-remove-group group)
5672     (when gnus-suppress-duplicates
5673       (gnus-dup-enter-articles))
5674     (when gnus-use-trees
5675       (gnus-tree-close group))
5676     (when gnus-use-cache
5677       (gnus-cache-write-active))
5678     ;; Remove entries for this group.
5679     (nnmail-purge-split-history (gnus-group-real-name group))
5680     ;; Make all changes in this group permanent.
5681     (unless quit-config
5682       (gnus-run-hooks 'gnus-exit-group-hook)
5683       (gnus-summary-update-info))
5684     (gnus-close-group group)
5685     ;; Make sure where we were, and go to next newsgroup.
5686     (set-buffer gnus-group-buffer)
5687     (unless quit-config
5688       (gnus-group-jump-to-group group))
5689     (gnus-run-hooks 'gnus-summary-exit-hook)
5690     (unless (or quit-config
5691                 ;; If this group has disappeared from the summary
5692                 ;; buffer, don't skip forwards.
5693                 (not (string= group (gnus-group-group-name))))
5694       (gnus-group-next-unread-group 1))
5695     (setq group-point (point))
5696     (if temporary
5697         nil                             ;Nothing to do.
5698       ;; If we have several article buffers, we kill them at exit.
5699       (unless gnus-single-article-buffer
5700         (gnus-kill-buffer gnus-article-buffer)
5701         (gnus-kill-buffer gnus-original-article-buffer)
5702         (setq gnus-article-current nil))
5703       (set-buffer buf)
5704       (if (not gnus-kill-summary-on-exit)
5705           (gnus-deaden-summary)
5706         ;; We set all buffer-local variables to nil.  It is unclear why
5707         ;; this is needed, but if we don't, buffer-local variables are
5708         ;; not garbage-collected, it seems.  This would the lead to en
5709         ;; ever-growing Emacs.
5710         (gnus-summary-clear-local-variables)
5711         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5712           (gnus-summary-clear-local-variables))
5713         (when (get-buffer gnus-article-buffer)
5714           (bury-buffer gnus-article-buffer))
5715         ;; We clear the global counterparts of the buffer-local
5716         ;; variables as well, just to be on the safe side.
5717         (set-buffer gnus-group-buffer)
5718         (gnus-summary-clear-local-variables)
5719         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5720           (gnus-summary-clear-local-variables))
5721         ;; Return to group mode buffer.
5722         (when (eq mode 'gnus-summary-mode)
5723           (gnus-kill-buffer buf)))
5724       (setq gnus-current-select-method gnus-select-method)
5725       (pop-to-buffer gnus-group-buffer)
5726       (if (not quit-config)
5727           (progn
5728             (goto-char group-point)
5729             (gnus-configure-windows 'group 'force))
5730         (gnus-handle-ephemeral-exit quit-config))
5731       ;; Clear the current group name.
5732       (unless quit-config
5733         (setq gnus-newsgroup-name nil)))))
5734
5735 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5736 (defun gnus-summary-exit-no-update (&optional no-questions)
5737   "Quit reading current newsgroup without updating read article info."
5738   (interactive)
5739   (let* ((group gnus-newsgroup-name)
5740          (quit-config (gnus-group-quit-config group)))
5741     (when (or no-questions
5742               gnus-expert-user
5743               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5744       (gnus-async-halt-prefetch)
5745       (mapcar 'funcall
5746               (delq 'gnus-summary-expire-articles
5747                     (copy-sequence gnus-summary-prepare-exit-hook)))
5748       (when (gnus-buffer-live-p gnus-article-buffer)
5749         (save-excursion
5750           (set-buffer gnus-article-buffer)
5751           (mm-destroy-parts gnus-article-mime-handles)
5752           ;; Set it to nil for safety reason.
5753           (setq gnus-article-mime-handle-alist nil)
5754           (setq gnus-article-mime-handles nil)))
5755       ;; If we have several article buffers, we kill them at exit.
5756       (unless gnus-single-article-buffer
5757         (gnus-kill-buffer gnus-article-buffer)
5758         (gnus-kill-buffer gnus-original-article-buffer)
5759         (setq gnus-article-current nil))
5760       (if (not gnus-kill-summary-on-exit)
5761           (gnus-deaden-summary)
5762         (gnus-close-group group)
5763         (gnus-summary-clear-local-variables)
5764         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5765           (gnus-summary-clear-local-variables))
5766         (set-buffer gnus-group-buffer)
5767         (gnus-summary-clear-local-variables)
5768         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5769           (gnus-summary-clear-local-variables))
5770         (when (get-buffer gnus-summary-buffer)
5771           (kill-buffer gnus-summary-buffer)))
5772       (unless gnus-single-article-buffer
5773         (setq gnus-article-current nil))
5774       (when gnus-use-trees
5775         (gnus-tree-close group))
5776       (gnus-async-prefetch-remove-group group)
5777       (when (get-buffer gnus-article-buffer)
5778         (bury-buffer gnus-article-buffer))
5779       ;; Return to the group buffer.
5780       (gnus-configure-windows 'group 'force)
5781       ;; Clear the current group name.
5782       (setq gnus-newsgroup-name nil)
5783       (when (equal (gnus-group-group-name) group)
5784         (gnus-group-next-unread-group 1))
5785       (when quit-config
5786         (gnus-handle-ephemeral-exit quit-config)))))
5787
5788 (defun gnus-handle-ephemeral-exit (quit-config)
5789   "Handle movement when leaving an ephemeral group.
5790 The state which existed when entering the ephemeral is reset."
5791   (if (not (buffer-name (car quit-config)))
5792       (gnus-configure-windows 'group 'force)
5793     (set-buffer (car quit-config))
5794     (cond ((eq major-mode 'gnus-summary-mode)
5795            (gnus-set-global-variables))
5796           ((eq major-mode 'gnus-article-mode)
5797            (save-excursion
5798              ;; The `gnus-summary-buffer' variable may point
5799              ;; to the old summary buffer when using a single
5800              ;; article buffer.
5801              (unless (gnus-buffer-live-p gnus-summary-buffer)
5802                (set-buffer gnus-group-buffer))
5803              (set-buffer gnus-summary-buffer)
5804              (gnus-set-global-variables))))
5805     (if (or (eq (cdr quit-config) 'article)
5806             (eq (cdr quit-config) 'pick))
5807         (progn
5808           ;; The current article may be from the ephemeral group
5809           ;; thus it is best that we reload this article
5810           (gnus-summary-show-article)
5811           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5812               (gnus-configure-windows 'pick 'force)
5813             (gnus-configure-windows (cdr quit-config) 'force)))
5814       (gnus-configure-windows (cdr quit-config) 'force))
5815     (when (eq major-mode 'gnus-summary-mode)
5816       (gnus-summary-next-subject 1 nil t)
5817       (gnus-summary-recenter)
5818       (gnus-summary-position-point))))
5819
5820 ;;; Dead summaries.
5821
5822 (defvar gnus-dead-summary-mode-map nil)
5823
5824 (unless gnus-dead-summary-mode-map
5825   (setq gnus-dead-summary-mode-map (make-keymap))
5826   (suppress-keymap gnus-dead-summary-mode-map)
5827   (substitute-key-definition
5828    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5829   (let ((keys '("\C-d" "\r" "\177" [delete])))
5830     (while keys
5831       (define-key gnus-dead-summary-mode-map
5832         (pop keys) 'gnus-summary-wake-up-the-dead))))
5833
5834 (defvar gnus-dead-summary-mode nil
5835   "Minor mode for Gnus summary buffers.")
5836
5837 (defun gnus-dead-summary-mode (&optional arg)
5838   "Minor mode for Gnus summary buffers."
5839   (interactive "P")
5840   (when (eq major-mode 'gnus-summary-mode)
5841     (make-local-variable 'gnus-dead-summary-mode)
5842     (setq gnus-dead-summary-mode
5843           (if (null arg) (not gnus-dead-summary-mode)
5844             (> (prefix-numeric-value arg) 0)))
5845     (when gnus-dead-summary-mode
5846       (gnus-add-minor-mode
5847        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5848
5849 (defun gnus-deaden-summary ()
5850   "Make the current summary buffer into a dead summary buffer."
5851   ;; Kill any previous dead summary buffer.
5852   (when (and gnus-dead-summary
5853              (buffer-name gnus-dead-summary))
5854     (save-excursion
5855       (set-buffer gnus-dead-summary)
5856       (when gnus-dead-summary-mode
5857         (kill-buffer (current-buffer)))))
5858   ;; Make this the current dead summary.
5859   (setq gnus-dead-summary (current-buffer))
5860   (gnus-dead-summary-mode 1)
5861   (let ((name (buffer-name)))
5862     (when (string-match "Summary" name)
5863       (rename-buffer
5864        (concat (substring name 0 (match-beginning 0)) "Dead "
5865                (substring name (match-beginning 0)))
5866        t)
5867       (bury-buffer))))
5868
5869 (defun gnus-kill-or-deaden-summary (buffer)
5870   "Kill or deaden the summary BUFFER."
5871   (save-excursion
5872     (when (and (buffer-name buffer)
5873                (not gnus-single-article-buffer))
5874       (save-excursion
5875         (set-buffer buffer)
5876         (gnus-kill-buffer gnus-article-buffer)
5877         (gnus-kill-buffer gnus-original-article-buffer)))
5878     (cond (gnus-kill-summary-on-exit
5879            (when (and gnus-use-trees
5880                       (gnus-buffer-exists-p buffer))
5881              (save-excursion
5882                (set-buffer buffer)
5883                (gnus-tree-close gnus-newsgroup-name)))
5884            (gnus-kill-buffer buffer))
5885           ((gnus-buffer-exists-p buffer)
5886            (save-excursion
5887              (set-buffer buffer)
5888              (gnus-deaden-summary))))))
5889
5890 (defun gnus-summary-wake-up-the-dead (&rest args)
5891   "Wake up the dead summary buffer."
5892   (interactive)
5893   (gnus-dead-summary-mode -1)
5894   (let ((name (buffer-name)))
5895     (when (string-match "Dead " name)
5896       (rename-buffer
5897        (concat (substring name 0 (match-beginning 0))
5898                (substring name (match-end 0)))
5899        t)))
5900   (gnus-message 3 "This dead summary is now alive again"))
5901
5902 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5903 (defun gnus-summary-fetch-faq (&optional faq-dir)
5904   "Fetch the FAQ for the current group.
5905 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5906 in."
5907   (interactive
5908    (list
5909     (when current-prefix-arg
5910       (completing-read
5911        "Faq dir: " (and (listp gnus-group-faq-directory)
5912                         (mapcar (lambda (file) (list file))
5913                                 gnus-group-faq-directory))))))
5914   (let (gnus-faq-buffer)
5915     (when (setq gnus-faq-buffer
5916                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5917       (gnus-configure-windows 'summary-faq))))
5918
5919 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5920 (defun gnus-summary-describe-group (&optional force)
5921   "Describe the current newsgroup."
5922   (interactive "P")
5923   (gnus-group-describe-group force gnus-newsgroup-name))
5924
5925 (defun gnus-summary-describe-briefly ()
5926   "Describe summary mode commands briefly."
5927   (interactive)
5928   (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")))
5929
5930 ;; Walking around group mode buffer from summary mode.
5931
5932 (defun gnus-summary-next-group (&optional no-article target-group backward)
5933   "Exit current newsgroup and then select next unread newsgroup.
5934 If prefix argument NO-ARTICLE is non-nil, no article is selected
5935 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5936 previous group instead."
5937   (interactive "P")
5938   ;; Stop pre-fetching.
5939   (gnus-async-halt-prefetch)
5940   (let ((current-group gnus-newsgroup-name)
5941         (current-buffer (current-buffer))
5942         entered)
5943     ;; First we semi-exit this group to update Xrefs and all variables.
5944     ;; We can't do a real exit, because the window conf must remain
5945     ;; the same in case the user is prompted for info, and we don't
5946     ;; want the window conf to change before that...
5947     (gnus-summary-exit t)
5948     (while (not entered)
5949       ;; Then we find what group we are supposed to enter.
5950       (set-buffer gnus-group-buffer)
5951       (gnus-group-jump-to-group current-group)
5952       (setq target-group
5953             (or target-group
5954                 (if (eq gnus-keep-same-level 'best)
5955                     (gnus-summary-best-group gnus-newsgroup-name)
5956                   (gnus-summary-search-group backward gnus-keep-same-level))))
5957       (if (not target-group)
5958           ;; There are no further groups, so we return to the group
5959           ;; buffer.
5960           (progn
5961             (gnus-message 5 "Returning to the group buffer")
5962             (setq entered t)
5963             (when (gnus-buffer-live-p current-buffer)
5964               (set-buffer current-buffer)
5965               (gnus-summary-exit))
5966             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5967         ;; We try to enter the target group.
5968         (gnus-group-jump-to-group target-group)
5969         (let ((unreads (gnus-group-group-unread)))
5970           (if (and (or (eq t unreads)
5971                        (and unreads (not (zerop unreads))))
5972                    (gnus-summary-read-group
5973                     target-group nil no-article
5974                     (and (buffer-name current-buffer) current-buffer)
5975                     nil backward))
5976               (setq entered t)
5977             (setq current-group target-group
5978                   target-group nil)))))))
5979
5980 (defun gnus-summary-prev-group (&optional no-article)
5981   "Exit current newsgroup and then select previous unread newsgroup.
5982 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5983   (interactive "P")
5984   (gnus-summary-next-group no-article nil t))
5985
5986 ;; Walking around summary lines.
5987
5988 (defun gnus-summary-first-subject (&optional unread undownloaded)
5989   "Go to the first unread subject.
5990 If UNREAD is non-nil, go to the first unread article.
5991 Returns the article selected or nil if there are no unread articles."
5992   (interactive "P")
5993   (prog1
5994       (cond
5995        ;; Empty summary.
5996        ((null gnus-newsgroup-data)
5997         (gnus-message 3 "No articles in the group")
5998         nil)
5999        ;; Pick the first article.
6000        ((not unread)
6001         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6002         (gnus-data-number (car gnus-newsgroup-data)))
6003        ;; No unread articles.
6004        ((null gnus-newsgroup-unreads)
6005         (gnus-message 3 "No more unread articles")
6006         nil)
6007        ;; Find the first unread article.
6008        (t
6009         (let ((data gnus-newsgroup-data))
6010           (while (and data
6011                       (and (not (and undownloaded
6012                                      (eq gnus-undownloaded-mark
6013                                          (gnus-data-mark (car data)))))
6014                            (not (gnus-data-unread-p (car data)))))
6015             (setq data (cdr data)))
6016           (when data
6017             (goto-char (gnus-data-pos (car data)))
6018             (gnus-data-number (car data))))))
6019     (gnus-summary-position-point)))
6020
6021 (defun gnus-summary-next-subject (n &optional unread dont-display)
6022   "Go to next N'th summary line.
6023 If N is negative, go to the previous N'th subject line.
6024 If UNREAD is non-nil, only unread articles are selected.
6025 The difference between N and the actual number of steps taken is
6026 returned."
6027   (interactive "p")
6028   (let ((backward (< n 0))
6029         (n (abs n)))
6030     (while (and (> n 0)
6031                 (if backward
6032                     (gnus-summary-find-prev unread)
6033                   (gnus-summary-find-next unread)))
6034       (unless (zerop (setq n (1- n)))
6035         (gnus-summary-show-thread)))
6036     (when (/= 0 n)
6037       (gnus-message 7 "No more%s articles"
6038                     (if unread " unread" "")))
6039     (unless dont-display
6040       (gnus-summary-recenter)
6041       (gnus-summary-position-point))
6042     n))
6043
6044 (defun gnus-summary-next-unread-subject (n)
6045   "Go to next N'th unread summary line."
6046   (interactive "p")
6047   (gnus-summary-next-subject n t))
6048
6049 (defun gnus-summary-prev-subject (n &optional unread)
6050   "Go to previous N'th summary line.
6051 If optional argument UNREAD is non-nil, only unread article is selected."
6052   (interactive "p")
6053   (gnus-summary-next-subject (- n) unread))
6054
6055 (defun gnus-summary-prev-unread-subject (n)
6056   "Go to previous N'th unread summary line."
6057   (interactive "p")
6058   (gnus-summary-next-subject (- n) t))
6059
6060 (defun gnus-summary-goto-subject (article &optional force silent)
6061   "Go the subject line of ARTICLE.
6062 If FORCE, also allow jumping to articles not currently shown."
6063   (interactive "nArticle number: ")
6064   (let ((b (point))
6065         (data (gnus-data-find article)))
6066     ;; We read in the article if we have to.
6067     (and (not data)
6068          force
6069          (gnus-summary-insert-subject
6070           article
6071           (if (or (numberp force) (vectorp force)) force)
6072           t)
6073          (setq data (gnus-data-find article)))
6074     (goto-char b)
6075     (if (not data)
6076         (progn
6077           (unless silent
6078             (gnus-message 3 "Can't find article %d" article))
6079           nil)
6080       (goto-char (gnus-data-pos data))
6081       (gnus-summary-position-point)
6082       article)))
6083
6084 ;; Walking around summary lines with displaying articles.
6085
6086 (defun gnus-summary-expand-window (&optional arg)
6087   "Make the summary buffer take up the entire Emacs frame.
6088 Given a prefix, will force an `article' buffer configuration."
6089   (interactive "P")
6090   (if arg
6091       (gnus-configure-windows 'article 'force)
6092     (gnus-configure-windows 'summary 'force)))
6093
6094 (defun gnus-summary-display-article (article &optional all-header)
6095   "Display ARTICLE in article buffer."
6096   (when (gnus-buffer-live-p gnus-article-buffer)
6097     (with-current-buffer gnus-article-buffer
6098       (mm-enable-multibyte-mule4)))
6099   (gnus-set-global-variables)
6100   (when (gnus-buffer-live-p gnus-article-buffer)
6101     (with-current-buffer gnus-article-buffer
6102       (setq gnus-article-charset gnus-newsgroup-charset)
6103       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6104       (mm-enable-multibyte-mule4)))
6105   (if (null article)
6106       nil
6107     (prog1
6108         (if gnus-summary-display-article-function
6109             (funcall gnus-summary-display-article-function article all-header)
6110           (gnus-article-prepare article all-header))
6111       (gnus-run-hooks 'gnus-select-article-hook)
6112       (when (and gnus-current-article
6113                  (not (zerop gnus-current-article)))
6114         (gnus-summary-goto-subject gnus-current-article))
6115       (gnus-summary-recenter)
6116       (when (and gnus-use-trees gnus-show-threads)
6117         (gnus-possibly-generate-tree article)
6118         (gnus-highlight-selected-tree article))
6119       ;; Successfully display article.
6120       (gnus-article-set-window-start
6121        (cdr (assq article gnus-newsgroup-bookmarks))))))
6122
6123 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6124   "Select the current article.
6125 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6126 non-nil, the article will be re-fetched even if it already present in
6127 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6128 be displayed."
6129   ;; Make sure we are in the summary buffer to work around bbdb bug.
6130   (unless (eq major-mode 'gnus-summary-mode)
6131     (set-buffer gnus-summary-buffer))
6132   (let ((article (or article (gnus-summary-article-number)))
6133         (all-headers (not (not all-headers))) ;Must be T or NIL.
6134         gnus-summary-display-article-function)
6135     (and (not pseudo)
6136          (gnus-summary-article-pseudo-p article)
6137          (error "This is a pseudo-article"))
6138     (save-excursion
6139       (set-buffer gnus-summary-buffer)
6140       (if (or (and gnus-single-article-buffer
6141                    (or (null gnus-current-article)
6142                        (null gnus-article-current)
6143                        (null (get-buffer gnus-article-buffer))
6144                        (not (eq article (cdr gnus-article-current)))
6145                        (not (equal (car gnus-article-current)
6146                                    gnus-newsgroup-name))))
6147               (and (not gnus-single-article-buffer)
6148                    (or (null gnus-current-article)
6149                        (not (eq gnus-current-article article))))
6150               force)
6151           ;; The requested article is different from the current article.
6152           (progn
6153             (gnus-summary-display-article article all-headers)
6154             (when (gnus-buffer-live-p gnus-article-buffer)
6155                (with-current-buffer gnus-article-buffer
6156                 (if (not gnus-article-decoded-p) ;; a local variable
6157                     (mm-disable-multibyte-mule4))))
6158             (when (or all-headers gnus-show-all-headers)
6159               (gnus-article-show-all-headers))
6160             (gnus-article-set-window-start
6161              (cdr (assq article gnus-newsgroup-bookmarks)))
6162             article)
6163         (when (or all-headers gnus-show-all-headers)
6164           (gnus-article-show-all-headers))
6165         'old))))
6166
6167 (defun gnus-summary-force-verify-and-decrypt ()
6168   (interactive)
6169   (let ((mm-verify-option 'known)
6170         (mm-decrypt-option 'known))
6171     (gnus-summary-select-article nil 'force)))
6172
6173 (defun gnus-summary-set-current-mark (&optional current-mark)
6174   "Obsolete function."
6175   nil)
6176
6177 (defun gnus-summary-next-article (&optional unread subject backward push)
6178   "Select the next article.
6179 If UNREAD, only unread articles are selected.
6180 If SUBJECT, only articles with SUBJECT are selected.
6181 If BACKWARD, the previous article is selected instead of the next."
6182   (interactive "P")
6183   (cond
6184    ;; Is there such an article?
6185    ((and (gnus-summary-search-forward unread subject backward)
6186          (or (gnus-summary-display-article (gnus-summary-article-number))
6187              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6188     (gnus-summary-position-point))
6189    ;; If not, we try the first unread, if that is wanted.
6190    ((and subject
6191          gnus-auto-select-same
6192          (gnus-summary-first-unread-article))
6193     (gnus-summary-position-point)
6194     (gnus-message 6 "Wrapped"))
6195    ;; Try to get next/previous article not displayed in this group.
6196    ((and gnus-auto-extend-newsgroup
6197          (not unread) (not subject))
6198     (gnus-summary-goto-article
6199      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6200      nil (count-lines (point-min) (point))))
6201    ;; Go to next/previous group.
6202    (t
6203     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6204       (gnus-summary-jump-to-group gnus-newsgroup-name))
6205     (let ((cmd last-command-char)
6206           (point
6207            (save-excursion
6208              (set-buffer gnus-group-buffer)
6209              (point)))
6210           (group
6211            (if (eq gnus-keep-same-level 'best)
6212                (gnus-summary-best-group gnus-newsgroup-name)
6213              (gnus-summary-search-group backward gnus-keep-same-level))))
6214       ;; For some reason, the group window gets selected.  We change
6215       ;; it back.
6216       (select-window (get-buffer-window (current-buffer)))
6217       ;; Select next unread newsgroup automagically.
6218       (cond
6219        ((or (not gnus-auto-select-next)
6220             (not cmd))
6221         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6222        ((or (eq gnus-auto-select-next 'quietly)
6223             (and (eq gnus-auto-select-next 'slightly-quietly)
6224                  push)
6225             (and (eq gnus-auto-select-next 'almost-quietly)
6226                  (gnus-summary-last-article-p)))
6227         ;; Select quietly.
6228         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6229             (gnus-summary-exit)
6230           (gnus-message 7 "No more%s articles (%s)..."
6231                         (if unread " unread" "")
6232                         (if group (concat "selecting " group)
6233                           "exiting"))
6234           (gnus-summary-next-group nil group backward)))
6235        (t
6236         (when (gnus-key-press-event-p last-input-event)
6237           (gnus-summary-walk-group-buffer
6238            gnus-newsgroup-name cmd unread backward point))))))))
6239
6240 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6241   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6242                       (?\C-p (gnus-group-prev-unread-group 1))))
6243         (cursor-in-echo-area t)
6244         keve key group ended)
6245     (save-excursion
6246       (set-buffer gnus-group-buffer)
6247       (goto-char start)
6248       (setq group
6249             (if (eq gnus-keep-same-level 'best)
6250                 (gnus-summary-best-group gnus-newsgroup-name)
6251               (gnus-summary-search-group backward gnus-keep-same-level))))
6252     (while (not ended)
6253       (gnus-message
6254        5 "No more%s articles%s" (if unread " unread" "")
6255        (if (and group
6256                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6257            (format " (Type %s for %s [%s])"
6258                    (single-key-description cmd) group
6259                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6260          (format " (Type %s to exit %s)"
6261                  (single-key-description cmd)
6262                  gnus-newsgroup-name)))
6263       ;; Confirm auto selection.
6264       (setq key (car (setq keve (gnus-read-event-char))))
6265       (setq ended t)
6266       (cond
6267        ((assq key keystrokes)
6268         (let ((obuf (current-buffer)))
6269           (switch-to-buffer gnus-group-buffer)
6270           (when group
6271             (gnus-group-jump-to-group group))
6272           (eval (cadr (assq key keystrokes)))
6273           (setq group (gnus-group-group-name))
6274           (switch-to-buffer obuf))
6275         (setq ended nil))
6276        ((equal key cmd)
6277         (if (or (not group)
6278                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6279             (gnus-summary-exit)
6280           (gnus-summary-next-group nil group backward)))
6281        (t
6282         (push (cdr keve) unread-command-events))))))
6283
6284 (defun gnus-summary-next-unread-article ()
6285   "Select unread article after current one."
6286   (interactive)
6287   (gnus-summary-next-article
6288    (or (not (eq gnus-summary-goto-unread 'never))
6289        (gnus-summary-last-article-p (gnus-summary-article-number)))
6290    (and gnus-auto-select-same
6291         (gnus-summary-article-subject))))
6292
6293 (defun gnus-summary-prev-article (&optional unread subject)
6294   "Select the article after the current one.
6295 If UNREAD is non-nil, only unread articles are selected."
6296   (interactive "P")
6297   (gnus-summary-next-article unread subject t))
6298
6299 (defun gnus-summary-prev-unread-article ()
6300   "Select unread article before current one."
6301   (interactive)
6302   (gnus-summary-prev-article
6303    (or (not (eq gnus-summary-goto-unread 'never))
6304        (gnus-summary-first-article-p (gnus-summary-article-number)))
6305    (and gnus-auto-select-same
6306         (gnus-summary-article-subject))))
6307
6308 (defun gnus-summary-next-page (&optional lines circular)
6309   "Show next page of the selected article.
6310 If at the end of the current article, select the next article.
6311 LINES says how many lines should be scrolled up.
6312
6313 If CIRCULAR is non-nil, go to the start of the article instead of
6314 selecting the next article when reaching the end of the current
6315 article."
6316   (interactive "P")
6317   (setq gnus-summary-buffer (current-buffer))
6318   (gnus-set-global-variables)
6319   (let ((article (gnus-summary-article-number))
6320         (article-window (get-buffer-window gnus-article-buffer t))
6321         endp)
6322     ;; If the buffer is empty, we have no article.
6323     (unless article
6324       (error "No article to select"))
6325     (gnus-configure-windows 'article)
6326     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6327         (if (and (eq gnus-summary-goto-unread 'never)
6328                  (not (gnus-summary-last-article-p article)))
6329             (gnus-summary-next-article)
6330           (gnus-summary-next-unread-article))
6331       (if (or (null gnus-current-article)
6332               (null gnus-article-current)
6333               (/= article (cdr gnus-article-current))
6334               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6335           ;; Selected subject is different from current article's.
6336           (gnus-summary-display-article article)
6337         (when article-window
6338           (gnus-eval-in-buffer-window gnus-article-buffer
6339             (setq endp (gnus-article-next-page lines)))
6340           (when endp
6341             (cond (circular
6342                    (gnus-summary-beginning-of-article))
6343                   (lines
6344                    (gnus-message 3 "End of message"))
6345                   ((null lines)
6346                    (if (and (eq gnus-summary-goto-unread 'never)
6347                             (not (gnus-summary-last-article-p article)))
6348                        (gnus-summary-next-article)
6349                      (gnus-summary-next-unread-article))))))))
6350     (gnus-summary-recenter)
6351     (gnus-summary-position-point)))
6352
6353 (defun gnus-summary-prev-page (&optional lines move)
6354   "Show previous page of selected article.
6355 Argument LINES specifies lines to be scrolled down.
6356 If MOVE, move to the previous unread article if point is at
6357 the beginning of the buffer."
6358   (interactive "P")
6359   (let ((article (gnus-summary-article-number))
6360         (article-window (get-buffer-window gnus-article-buffer t))
6361         endp)
6362     (gnus-configure-windows 'article)
6363     (if (or (null gnus-current-article)
6364             (null gnus-article-current)
6365             (/= article (cdr gnus-article-current))
6366             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6367         ;; Selected subject is different from current article's.
6368         (gnus-summary-display-article article)
6369       (gnus-summary-recenter)
6370       (when article-window
6371         (gnus-eval-in-buffer-window gnus-article-buffer
6372           (setq endp (gnus-article-prev-page lines)))
6373         (when (and move endp)
6374           (cond (lines
6375                  (gnus-message 3 "Beginning of message"))
6376                 ((null lines)
6377                  (if (and (eq gnus-summary-goto-unread 'never)
6378                           (not (gnus-summary-first-article-p article)))
6379                      (gnus-summary-prev-article)
6380                    (gnus-summary-prev-unread-article))))))))
6381   (gnus-summary-position-point))
6382
6383 (defun gnus-summary-prev-page-or-article (&optional lines)
6384   "Show previous page of selected article.
6385 Argument LINES specifies lines to be scrolled down.
6386 If at the beginning of the article, go to the next article."
6387   (interactive "P")
6388   (gnus-summary-prev-page lines t))
6389
6390 (defun gnus-summary-scroll-up (lines)
6391   "Scroll up (or down) one line current article.
6392 Argument LINES specifies lines to be scrolled up (or down if negative)."
6393   (interactive "p")
6394   (gnus-configure-windows 'article)
6395   (gnus-summary-show-thread)
6396   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6397     (gnus-eval-in-buffer-window gnus-article-buffer
6398       (cond ((> lines 0)
6399              (when (gnus-article-next-page lines)
6400                (gnus-message 3 "End of message")))
6401             ((< lines 0)
6402              (gnus-article-prev-page (- lines))))))
6403   (gnus-summary-recenter)
6404   (gnus-summary-position-point))
6405
6406 (defun gnus-summary-scroll-down (lines)
6407   "Scroll down (or up) one line current article.
6408 Argument LINES specifies lines to be scrolled down (or up if negative)."
6409   (interactive "p")
6410   (gnus-summary-scroll-up (- lines)))
6411
6412 (defun gnus-summary-next-same-subject ()
6413   "Select next article which has the same subject as current one."
6414   (interactive)
6415   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6416
6417 (defun gnus-summary-prev-same-subject ()
6418   "Select previous article which has the same subject as current one."
6419   (interactive)
6420   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6421
6422 (defun gnus-summary-next-unread-same-subject ()
6423   "Select next unread article which has the same subject as current one."
6424   (interactive)
6425   (gnus-summary-next-article t (gnus-summary-article-subject)))
6426
6427 (defun gnus-summary-prev-unread-same-subject ()
6428   "Select previous unread article which has the same subject as current one."
6429   (interactive)
6430   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6431
6432 (defun gnus-summary-first-unread-article ()
6433   "Select the first unread article.
6434 Return nil if there are no unread articles."
6435   (interactive)
6436   (prog1
6437       (when (gnus-summary-first-subject t)
6438         (gnus-summary-show-thread)
6439         (gnus-summary-first-subject t)
6440         (gnus-summary-display-article (gnus-summary-article-number)))
6441     (gnus-summary-position-point)))
6442
6443 (defun gnus-summary-first-unread-subject ()
6444   "Place the point on the subject line of the first unread article.
6445 Return nil if there are no unread articles."
6446   (interactive)
6447   (prog1
6448       (when (gnus-summary-first-subject t)
6449         (gnus-summary-show-thread)
6450         (gnus-summary-first-subject t))
6451     (gnus-summary-position-point)))
6452
6453 (defun gnus-summary-first-article ()
6454   "Select the first article.
6455 Return nil if there are no articles."
6456   (interactive)
6457   (prog1
6458       (when (gnus-summary-first-subject)
6459         (gnus-summary-show-thread)
6460         (gnus-summary-first-subject)
6461         (gnus-summary-display-article (gnus-summary-article-number)))
6462     (gnus-summary-position-point)))
6463
6464 (defun gnus-summary-best-unread-article ()
6465   "Select the unread article with the highest score."
6466   (interactive)
6467   (let ((best -1000000)
6468         (data gnus-newsgroup-data)
6469         article score)
6470     (while data
6471       (and (gnus-data-unread-p (car data))
6472            (> (setq score
6473                     (gnus-summary-article-score (gnus-data-number (car data))))
6474               best)
6475            (setq best score
6476                  article (gnus-data-number (car data))))
6477       (setq data (cdr data)))
6478     (prog1
6479         (if article
6480             (gnus-summary-goto-article article)
6481           (error "No unread articles"))
6482       (gnus-summary-position-point))))
6483
6484 (defun gnus-summary-last-subject ()
6485   "Go to the last displayed subject line in the group."
6486   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6487     (when article
6488       (gnus-summary-goto-subject article))))
6489
6490 (defun gnus-summary-goto-article (article &optional all-headers force)
6491   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6492 If ALL-HEADERS is non-nil, no header lines are hidden.
6493 If FORCE, go to the article even if it isn't displayed.  If FORCE
6494 is a number, it is the line the article is to be displayed on."
6495   (interactive
6496    (list
6497     (completing-read
6498      "Article number or Message-ID: "
6499      (mapcar (lambda (number) (list (int-to-string number)))
6500              gnus-newsgroup-limit))
6501     current-prefix-arg
6502     t))
6503   (prog1
6504       (if (and (stringp article)
6505                (string-match "@" article))
6506           (gnus-summary-refer-article article)
6507         (when (stringp article)
6508           (setq article (string-to-number article)))
6509         (if (gnus-summary-goto-subject article force)
6510             (gnus-summary-display-article article all-headers)
6511           (gnus-message 4 "Couldn't go to article %s" article) nil))
6512     (gnus-summary-position-point)))
6513
6514 (defun gnus-summary-goto-last-article ()
6515   "Go to the previously read article."
6516   (interactive)
6517   (prog1
6518       (when gnus-last-article
6519         (gnus-summary-goto-article gnus-last-article nil t))
6520     (gnus-summary-position-point)))
6521
6522 (defun gnus-summary-pop-article (number)
6523   "Pop one article off the history and go to the previous.
6524 NUMBER articles will be popped off."
6525   (interactive "p")
6526   (let (to)
6527     (setq gnus-newsgroup-history
6528           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6529     (if to
6530         (gnus-summary-goto-article (car to) nil t)
6531       (error "Article history empty")))
6532   (gnus-summary-position-point))
6533
6534 ;; Summary commands and functions for limiting the summary buffer.
6535
6536 (defun gnus-summary-limit-to-articles (n)
6537   "Limit the summary buffer to the next N articles.
6538 If not given a prefix, use the process marked articles instead."
6539   (interactive "P")
6540   (prog1
6541       (let ((articles (gnus-summary-work-articles n)))
6542         (setq gnus-newsgroup-processable nil)
6543         (gnus-summary-limit articles))
6544     (gnus-summary-position-point)))
6545
6546 (defun gnus-summary-pop-limit (&optional total)
6547   "Restore the previous limit.
6548 If given a prefix, remove all limits."
6549   (interactive "P")
6550   (when total
6551     (setq gnus-newsgroup-limits
6552           (list (mapcar (lambda (h) (mail-header-number h))
6553                         gnus-newsgroup-headers))))
6554   (unless gnus-newsgroup-limits
6555     (error "No limit to pop"))
6556   (prog1
6557       (gnus-summary-limit nil 'pop)
6558     (gnus-summary-position-point)))
6559
6560 (defun gnus-summary-limit-to-subject (subject &optional header)
6561   "Limit the summary buffer to articles that have subjects that match a regexp."
6562   (interactive "sLimit to subject (regexp): ")
6563   (unless header
6564     (setq header "subject"))
6565   (when (not (equal "" subject))
6566     (prog1
6567         (let ((articles (gnus-summary-find-matching
6568                          (or header "subject") subject 'all)))
6569           (unless articles
6570             (error "Found no matches for \"%s\"" subject))
6571           (gnus-summary-limit articles))
6572       (gnus-summary-position-point))))
6573
6574 (defun gnus-summary-limit-to-author (from)
6575   "Limit the summary buffer to articles that have authors that match a regexp."
6576   (interactive "sLimit to author (regexp): ")
6577   (gnus-summary-limit-to-subject from "from"))
6578
6579 (defun gnus-summary-limit-to-age (age &optional younger-p)
6580   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6581 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6582 articles that are younger than AGE days."
6583   (interactive
6584    (let ((younger current-prefix-arg)
6585          (days-got nil)
6586          days)
6587      (while (not days-got)
6588        (setq days (if younger
6589                       (read-string "Limit to articles within (in days): ")
6590                     (read-string "Limit to articles older than (in days): ")))
6591        (when (> (length days) 0)
6592          (setq days (read days)))
6593        (if (numberp days)
6594            (setq days-got t)
6595          (message "Please enter a number.")
6596          (sleep-for 1)))
6597      (list days younger)))
6598   (prog1
6599       (let ((data gnus-newsgroup-data)
6600             (cutoff (days-to-time age))
6601             articles d date is-younger)
6602         (while (setq d (pop data))
6603           (when (and (vectorp (gnus-data-header d))
6604                      (setq date (mail-header-date (gnus-data-header d))))
6605             (setq is-younger (time-less-p
6606                               (time-since (condition-case ()
6607                                               (date-to-time date)
6608                                             (error '(0 0))))
6609                               cutoff))
6610             (when (if younger-p
6611                       is-younger
6612                     (not is-younger))
6613               (push (gnus-data-number d) articles))))
6614         (gnus-summary-limit (nreverse articles)))
6615     (gnus-summary-position-point)))
6616
6617 (defun gnus-summary-limit-to-extra (header regexp)
6618   "Limit the summary buffer to articles that match an 'extra' header."
6619   (interactive
6620    (let ((header
6621           (intern
6622            (gnus-completing-read
6623             (symbol-name (car gnus-extra-headers))
6624             "Limit extra header:"
6625             (mapcar (lambda (x)
6626                       (cons (symbol-name x) x))
6627                     gnus-extra-headers)
6628             nil
6629             t))))
6630      (list header
6631            (read-string (format "Limit to header %s (regexp): " header)))))
6632   (when (not (equal "" regexp))
6633     (prog1
6634         (let ((articles (gnus-summary-find-matching
6635                          (cons 'extra header) regexp 'all)))
6636           (unless articles
6637             (error "Found no matches for \"%s\"" regexp))
6638           (gnus-summary-limit articles))
6639       (gnus-summary-position-point))))
6640
6641 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6642 (make-obsolete
6643  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6644
6645 (defun gnus-summary-limit-to-unread (&optional all)
6646   "Limit the summary buffer to articles that are not marked as read.
6647 If ALL is non-nil, limit strictly to unread articles."
6648   (interactive "P")
6649   (if all
6650       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6651     (gnus-summary-limit-to-marks
6652      ;; Concat all the marks that say that an article is read and have
6653      ;; those removed.
6654      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6655            gnus-killed-mark gnus-kill-file-mark
6656            gnus-low-score-mark gnus-expirable-mark
6657            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6658            gnus-duplicate-mark gnus-souped-mark)
6659      'reverse)))
6660
6661 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6662 (make-obsolete 'gnus-summary-delete-marked-with
6663                'gnus-summary-limit-exlude-marks)
6664
6665 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6666   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6667 If REVERSE, limit the summary buffer to articles that are marked
6668 with MARKS.  MARKS can either be a string of marks or a list of marks.
6669 Returns how many articles were removed."
6670   (interactive "sMarks: ")
6671   (gnus-summary-limit-to-marks marks t))
6672
6673 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6674   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6675 If REVERSE (the prefix), limit the summary buffer to articles that are
6676 not marked with MARKS.  MARKS can either be a string of marks or a
6677 list of marks.
6678 Returns how many articles were removed."
6679   (interactive "sMarks: \nP")
6680   (prog1
6681       (let ((data gnus-newsgroup-data)
6682             (marks (if (listp marks) marks
6683                      (append marks nil))) ; Transform to list.
6684             articles)
6685         (while data
6686           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6687                   (memq (gnus-data-mark (car data)) marks))
6688             (push (gnus-data-number (car data)) articles))
6689           (setq data (cdr data)))
6690         (gnus-summary-limit articles))
6691     (gnus-summary-position-point)))
6692
6693 (defun gnus-summary-limit-to-score (&optional score)
6694   "Limit to articles with score at or above SCORE."
6695   (interactive "P")
6696   (setq score (if score
6697                   (prefix-numeric-value score)
6698                 (or gnus-summary-default-score 0)))
6699   (let ((data gnus-newsgroup-data)
6700         articles)
6701     (while data
6702       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6703                 score)
6704         (push (gnus-data-number (car data)) articles))
6705       (setq data (cdr data)))
6706     (prog1
6707         (gnus-summary-limit articles)
6708       (gnus-summary-position-point))))
6709
6710 (defun gnus-summary-limit-include-thread (id)
6711   "Display all the hidden articles that in the current thread."
6712   (interactive (list (mail-header-id (gnus-summary-article-header))))
6713   (let ((articles (gnus-articles-in-thread
6714                    (gnus-id-to-thread (gnus-root-id id)))))
6715     (prog1
6716         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6717       (gnus-summary-position-point))))
6718
6719 (defun gnus-summary-limit-include-dormant ()
6720   "Display all the hidden articles that are marked as dormant.
6721 Note that this command only works on a subset of the articles currently
6722 fetched for this group."
6723   (interactive)
6724   (unless gnus-newsgroup-dormant
6725     (error "There are no dormant articles in this group"))
6726   (prog1
6727       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6728     (gnus-summary-position-point)))
6729
6730 (defun gnus-summary-limit-exclude-dormant ()
6731   "Hide all dormant articles."
6732   (interactive)
6733   (prog1
6734       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6735     (gnus-summary-position-point)))
6736
6737 (defun gnus-summary-limit-exclude-childless-dormant ()
6738   "Hide all dormant articles that have no children."
6739   (interactive)
6740   (let ((data (gnus-data-list t))
6741         articles d children)
6742     ;; Find all articles that are either not dormant or have
6743     ;; children.
6744     (while (setq d (pop data))
6745       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6746                 (and (setq children
6747                            (gnus-article-children (gnus-data-number d)))
6748                      (let (found)
6749                        (while children
6750                          (when (memq (car children) articles)
6751                            (setq children nil
6752                                  found t))
6753                          (pop children))
6754                        found)))
6755         (push (gnus-data-number d) articles)))
6756     ;; Do the limiting.
6757     (prog1
6758         (gnus-summary-limit articles)
6759       (gnus-summary-position-point))))
6760
6761 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6762   "Mark all unread excluded articles as read.
6763 If ALL, mark even excluded ticked and dormants as read."
6764   (interactive "P")
6765   (let ((articles (gnus-sorted-complement
6766                    (sort
6767                     (mapcar (lambda (h) (mail-header-number h))
6768                             gnus-newsgroup-headers)
6769                     '<)
6770                    (sort gnus-newsgroup-limit '<)))
6771         article)
6772     (setq gnus-newsgroup-unreads
6773           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6774     (if all
6775         (setq gnus-newsgroup-dormant nil
6776               gnus-newsgroup-marked nil
6777               gnus-newsgroup-reads
6778               (nconc
6779                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6780                gnus-newsgroup-reads))
6781       (while (setq article (pop articles))
6782         (unless (or (memq article gnus-newsgroup-dormant)
6783                     (memq article gnus-newsgroup-marked))
6784           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6785
6786 (defun gnus-summary-limit (articles &optional pop)
6787   (if pop
6788       ;; We pop the previous limit off the stack and use that.
6789       (setq articles (car gnus-newsgroup-limits)
6790             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6791     ;; We use the new limit, so we push the old limit on the stack.
6792     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6793   ;; Set the limit.
6794   (setq gnus-newsgroup-limit articles)
6795   (let ((total (length gnus-newsgroup-data))
6796         (data (gnus-data-find-list (gnus-summary-article-number)))
6797         (gnus-summary-mark-below nil)   ; Inhibit this.
6798         found)
6799     ;; This will do all the work of generating the new summary buffer
6800     ;; according to the new limit.
6801     (gnus-summary-prepare)
6802     ;; Hide any threads, possibly.
6803     (and gnus-show-threads
6804          gnus-thread-hide-subtree
6805          (gnus-summary-hide-all-threads))
6806     ;; Try to return to the article you were at, or one in the
6807     ;; neighborhood.
6808     (when data
6809       ;; We try to find some article after the current one.
6810       (while data
6811         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6812           (setq data nil
6813                 found t))
6814         (setq data (cdr data))))
6815     (unless found
6816       ;; If there is no data, that means that we were after the last
6817       ;; article.  The same goes when we can't find any articles
6818       ;; after the current one.
6819       (goto-char (point-max))
6820       (gnus-summary-find-prev))
6821     (gnus-set-mode-line 'summary)
6822     ;; We return how many articles were removed from the summary
6823     ;; buffer as a result of the new limit.
6824     (- total (length gnus-newsgroup-data))))
6825
6826 (defsubst gnus-invisible-cut-children (threads)
6827   (let ((num 0))
6828     (while threads
6829       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6830         (incf num))
6831       (pop threads))
6832     (< num 2)))
6833
6834 (defsubst gnus-cut-thread (thread)
6835   "Go forwards in the thread until we find an article that we want to display."
6836   (when (or (eq gnus-fetch-old-headers 'some)
6837             (eq gnus-fetch-old-headers 'invisible)
6838             (numberp gnus-fetch-old-headers)
6839             (eq gnus-build-sparse-threads 'some)
6840             (eq gnus-build-sparse-threads 'more))
6841     ;; Deal with old-fetched headers and sparse threads.
6842     (while (and
6843             thread
6844             (or
6845              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6846              (gnus-summary-article-ancient-p
6847               (mail-header-number (car thread))))
6848             (if (or (<= (length (cdr thread)) 1)
6849                     (eq gnus-fetch-old-headers 'invisible))
6850                 (setq gnus-newsgroup-limit
6851                       (delq (mail-header-number (car thread))
6852                             gnus-newsgroup-limit)
6853                       thread (cadr thread))
6854               (when (gnus-invisible-cut-children (cdr thread))
6855                 (let ((th (cdr thread)))
6856                   (while th
6857                     (if (memq (mail-header-number (caar th))
6858                               gnus-newsgroup-limit)
6859                         (setq thread (car th)
6860                               th nil)
6861                       (setq th (cdr th))))))))))
6862   thread)
6863
6864 (defun gnus-cut-threads (threads)
6865   "Cut off all uninteresting articles from the beginning of threads."
6866   (when (or (eq gnus-fetch-old-headers 'some)
6867             (eq gnus-fetch-old-headers 'invisible)
6868             (numberp gnus-fetch-old-headers)
6869             (eq gnus-build-sparse-threads 'some)
6870             (eq gnus-build-sparse-threads 'more))
6871     (let ((th threads))
6872       (while th
6873         (setcar th (gnus-cut-thread (car th)))
6874         (setq th (cdr th)))))
6875   ;; Remove nixed out threads.
6876   (delq nil threads))
6877
6878 (defun gnus-summary-initial-limit (&optional show-if-empty)
6879   "Figure out what the initial limit is supposed to be on group entry.
6880 This entails weeding out unwanted dormants, low-scored articles,
6881 fetch-old-headers verbiage, and so on."
6882   ;; Most groups have nothing to remove.
6883   (if (or gnus-inhibit-limiting
6884           (and (null gnus-newsgroup-dormant)
6885                (not (eq gnus-fetch-old-headers 'some))
6886                (not (numberp gnus-fetch-old-headers))
6887                (not (eq gnus-fetch-old-headers 'invisible))
6888                (null gnus-summary-expunge-below)
6889                (not (eq gnus-build-sparse-threads 'some))
6890                (not (eq gnus-build-sparse-threads 'more))
6891                (null gnus-thread-expunge-below)
6892                (not gnus-use-nocem)))
6893       ()                                ; Do nothing.
6894     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6895     (setq gnus-newsgroup-limit nil)
6896     (mapatoms
6897      (lambda (node)
6898        (unless (car (symbol-value node))
6899          ;; These threads have no parents -- they are roots.
6900          (let ((nodes (cdr (symbol-value node)))
6901                thread)
6902            (while nodes
6903              (if (and gnus-thread-expunge-below
6904                       (< (gnus-thread-total-score (car nodes))
6905                          gnus-thread-expunge-below))
6906                  (gnus-expunge-thread (pop nodes))
6907                (setq thread (pop nodes))
6908                (gnus-summary-limit-children thread))))))
6909      gnus-newsgroup-dependencies)
6910     ;; If this limitation resulted in an empty group, we might
6911     ;; pop the previous limit and use it instead.
6912     (when (and (not gnus-newsgroup-limit)
6913                show-if-empty)
6914       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6915     gnus-newsgroup-limit))
6916
6917 (defun gnus-summary-limit-children (thread)
6918   "Return 1 if this subthread is visible and 0 if it is not."
6919   ;; First we get the number of visible children to this thread.  This
6920   ;; is done by recursing down the thread using this function, so this
6921   ;; will really go down to a leaf article first, before slowly
6922   ;; working its way up towards the root.
6923   (when thread
6924     (let ((children
6925            (if (cdr thread)
6926                (apply '+ (mapcar 'gnus-summary-limit-children
6927                                  (cdr thread)))
6928              0))
6929           (number (mail-header-number (car thread)))
6930           score)
6931       (if (and
6932            (not (memq number gnus-newsgroup-marked))
6933            (or
6934             ;; If this article is dormant and has absolutely no visible
6935             ;; children, then this article isn't visible.
6936             (and (memq number gnus-newsgroup-dormant)
6937                  (zerop children))
6938             ;; If this is "fetch-old-headered" and there is no
6939             ;; visible children, then we don't want this article.
6940             (and (or (eq gnus-fetch-old-headers 'some)
6941                      (numberp gnus-fetch-old-headers))
6942                  (gnus-summary-article-ancient-p number)
6943                  (zerop children))
6944             ;; If this is "fetch-old-headered" and `invisible', then
6945             ;; we don't want this article.
6946             (and (eq gnus-fetch-old-headers 'invisible)
6947                  (gnus-summary-article-ancient-p number))
6948             ;; If this is a sparsely inserted article with no children,
6949             ;; we don't want it.
6950             (and (eq gnus-build-sparse-threads 'some)
6951                  (gnus-summary-article-sparse-p number)
6952                  (zerop children))
6953             ;; If we use expunging, and this article is really
6954             ;; low-scored, then we don't want this article.
6955             (when (and gnus-summary-expunge-below
6956                        (< (setq score
6957                                 (or (cdr (assq number gnus-newsgroup-scored))
6958                                     gnus-summary-default-score))
6959                           gnus-summary-expunge-below))
6960               ;; We increase the expunge-tally here, but that has
6961               ;; nothing to do with the limits, really.
6962               (incf gnus-newsgroup-expunged-tally)
6963               ;; We also mark as read here, if that's wanted.
6964               (when (and gnus-summary-mark-below
6965                          (< score gnus-summary-mark-below))
6966                 (setq gnus-newsgroup-unreads
6967                       (delq number gnus-newsgroup-unreads))
6968                 (if gnus-newsgroup-auto-expire
6969                     (push number gnus-newsgroup-expirable)
6970                   (push (cons number gnus-low-score-mark)
6971                         gnus-newsgroup-reads)))
6972               t)
6973             ;; Check NoCeM things.
6974             (if (and gnus-use-nocem
6975                      (gnus-nocem-unwanted-article-p
6976                       (mail-header-id (car thread))))
6977                 (progn
6978                   (setq gnus-newsgroup-unreads
6979                         (delq number gnus-newsgroup-unreads))
6980                   t))))
6981           ;; Nope, invisible article.
6982           0
6983         ;; Ok, this article is to be visible, so we add it to the limit
6984         ;; and return 1.
6985         (push number gnus-newsgroup-limit)
6986         1))))
6987
6988 (defun gnus-expunge-thread (thread)
6989   "Mark all articles in THREAD as read."
6990   (let* ((number (mail-header-number (car thread))))
6991     (incf gnus-newsgroup-expunged-tally)
6992     ;; We also mark as read here, if that's wanted.
6993     (setq gnus-newsgroup-unreads
6994           (delq number gnus-newsgroup-unreads))
6995     (if gnus-newsgroup-auto-expire
6996         (push number gnus-newsgroup-expirable)
6997       (push (cons number gnus-low-score-mark)
6998             gnus-newsgroup-reads)))
6999   ;; Go recursively through all subthreads.
7000   (mapcar 'gnus-expunge-thread (cdr thread)))
7001
7002 ;; Summary article oriented commands
7003
7004 (defun gnus-summary-refer-parent-article (n)
7005   "Refer parent article N times.
7006 If N is negative, go to ancestor -N instead.
7007 The difference between N and the number of articles fetched is returned."
7008   (interactive "p")
7009   (let ((skip 1)
7010         error header ref)
7011     (when (not (natnump n))
7012       (setq skip (abs n)
7013             n 1))
7014     (while (and (> n 0)
7015                 (not error))
7016       (setq header (gnus-summary-article-header))
7017       (if (and (eq (mail-header-number header)
7018                    (cdr gnus-article-current))
7019                (equal gnus-newsgroup-name
7020                       (car gnus-article-current)))
7021           ;; If we try to find the parent of the currently
7022           ;; displayed article, then we take a look at the actual
7023           ;; References header, since this is slightly more
7024           ;; reliable than the References field we got from the
7025           ;; server.
7026           (save-excursion
7027             (set-buffer gnus-original-article-buffer)
7028             (nnheader-narrow-to-headers)
7029             (unless (setq ref (message-fetch-field "references"))
7030               (setq ref (message-fetch-field "in-reply-to")))
7031             (widen))
7032         (setq ref
7033               ;; It's not the current article, so we take a bet on
7034               ;; the value we got from the server.
7035               (mail-header-references header)))
7036       (if (and ref
7037                (not (equal ref "")))
7038           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7039             (gnus-message 1 "Couldn't find parent"))
7040         (gnus-message 1 "No references in article %d"
7041                       (gnus-summary-article-number))
7042         (setq error t))
7043       (decf n))
7044     (gnus-summary-position-point)
7045     n))
7046
7047 (defun gnus-summary-refer-references ()
7048   "Fetch all articles mentioned in the References header.
7049 Return the number of articles fetched."
7050   (interactive)
7051   (let ((ref (mail-header-references (gnus-summary-article-header)))
7052         (current (gnus-summary-article-number))
7053         (n 0))
7054     (if (or (not ref)
7055             (equal ref ""))
7056         (error "No References in the current article")
7057       ;; For each Message-ID in the References header...
7058       (while (string-match "<[^>]*>" ref)
7059         (incf n)
7060         ;; ... fetch that article.
7061         (gnus-summary-refer-article
7062          (prog1 (match-string 0 ref)
7063            (setq ref (substring ref (match-end 0))))))
7064       (gnus-summary-goto-subject current)
7065       (gnus-summary-position-point)
7066       n)))
7067
7068 (defun gnus-summary-refer-thread (&optional limit)
7069   "Fetch all articles in the current thread.
7070 If LIMIT (the numerical prefix), fetch that many old headers instead
7071 of what's specified by the `gnus-refer-thread-limit' variable."
7072   (interactive "P")
7073   (let ((id (mail-header-id (gnus-summary-article-header)))
7074         (limit (if limit (prefix-numeric-value limit)
7075                  gnus-refer-thread-limit)))
7076     ;; We want to fetch LIMIT *old* headers, but we also have to
7077     ;; re-fetch all the headers in the current buffer, because many of
7078     ;; them may be undisplayed.  So we adjust LIMIT.
7079     (when (numberp limit)
7080       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7081     (unless (eq gnus-fetch-old-headers 'invisible)
7082       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7083       ;; Retrieve the headers and read them in.
7084       (if (eq (gnus-retrieve-headers
7085                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7086               'nov)
7087           (gnus-build-all-threads)
7088         (error "Can't fetch thread from backends that don't support NOV"))
7089       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7090     (gnus-summary-limit-include-thread id)))
7091
7092 (defun gnus-summary-refer-article (message-id)
7093   "Fetch an article specified by MESSAGE-ID."
7094   (interactive "sMessage-ID: ")
7095   (when (and (stringp message-id)
7096              (not (zerop (length message-id))))
7097     ;; Construct the correct Message-ID if necessary.
7098     ;; Suggested by tale@pawl.rpi.edu.
7099     (unless (string-match "^<" message-id)
7100       (setq message-id (concat "<" message-id)))
7101     (unless (string-match ">$" message-id)
7102       (setq message-id (concat message-id ">")))
7103     (let* ((header (gnus-id-to-header message-id))
7104            (sparse (and header
7105                         (gnus-summary-article-sparse-p
7106                          (mail-header-number header))
7107                         (memq (mail-header-number header)
7108                               gnus-newsgroup-limit)))
7109            number)
7110       (cond
7111        ;; If the article is present in the buffer we just go to it.
7112        ((and header
7113              (or (not (gnus-summary-article-sparse-p
7114                        (mail-header-number header)))
7115                  sparse))
7116         (prog1
7117             (gnus-summary-goto-article
7118              (mail-header-number header) nil t)
7119           (when sparse
7120             (gnus-summary-update-article (mail-header-number header)))))
7121        (t
7122         ;; We fetch the article.
7123         (catch 'found
7124           (dolist (gnus-override-method (gnus-refer-article-methods))
7125             (gnus-check-server gnus-override-method)
7126             ;; Fetch the header, and display the article.
7127             (when (setq number (gnus-summary-insert-subject message-id))
7128               (gnus-summary-select-article nil nil nil number)
7129               (throw 'found t)))
7130           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7131
7132 (defun gnus-refer-article-methods ()
7133   "Return a list of referrable methods."
7134   (cond
7135    ;; No method, so we default to current and native.
7136    ((null gnus-refer-article-method)
7137     (list gnus-current-select-method gnus-select-method))
7138    ;; Current.
7139    ((eq 'current gnus-refer-article-method)
7140     (list gnus-current-select-method))
7141    ;; List of select methods.
7142    ((not (and (symbolp (car gnus-refer-article-method))
7143               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7144     (let (out)
7145       (dolist (method gnus-refer-article-method)
7146         (push (if (eq 'current method)
7147                   gnus-current-select-method
7148                 method)
7149               out))
7150       (nreverse out)))
7151    ;; One single select method.
7152    (t
7153     (list gnus-refer-article-method))))
7154
7155 (defun gnus-summary-edit-parameters ()
7156   "Edit the group parameters of the current group."
7157   (interactive)
7158   (gnus-group-edit-group gnus-newsgroup-name 'params))
7159
7160 (defun gnus-summary-customize-parameters ()
7161   "Customize the group parameters of the current group."
7162   (interactive)
7163   (gnus-group-customize gnus-newsgroup-name))
7164
7165 (defun gnus-summary-enter-digest-group (&optional force)
7166   "Enter an nndoc group based on the current article.
7167 If FORCE, force a digest interpretation.  If not, try
7168 to guess what the document format is."
7169   (interactive "P")
7170   (let ((conf gnus-current-window-configuration))
7171     (save-excursion
7172       (gnus-summary-select-article))
7173     (setq gnus-current-window-configuration conf)
7174     (let* ((name (format "%s-%d"
7175                          (gnus-group-prefixed-name
7176                           gnus-newsgroup-name (list 'nndoc ""))
7177                          (save-excursion
7178                            (set-buffer gnus-summary-buffer)
7179                            gnus-current-article)))
7180            (ogroup gnus-newsgroup-name)
7181            (params (append (gnus-info-params (gnus-get-info ogroup))
7182                            (list (cons 'to-group ogroup))
7183                            (list (cons 'save-article-group ogroup))))
7184            (case-fold-search t)
7185            (buf (current-buffer))
7186            dig to-address)
7187       (save-excursion
7188         (set-buffer gnus-original-article-buffer)
7189         ;; Have the digest group inherit the main mail address of
7190         ;; the parent article.
7191         (when (setq to-address (or (message-fetch-field "reply-to")
7192                                    (message-fetch-field "from")))
7193           (setq params (append
7194                         (list (cons 'to-address
7195                                     (funcall gnus-decode-encoded-word-function
7196                                              to-address))))))
7197         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7198         (insert-buffer-substring gnus-original-article-buffer)
7199         ;; Remove lines that may lead nndoc to misinterpret the
7200         ;; document type.
7201         (narrow-to-region
7202          (goto-char (point-min))
7203          (or (search-forward "\n\n" nil t) (point)))
7204         (goto-char (point-min))
7205         (delete-matching-lines "^Path:\\|^From ")
7206         (widen))
7207       (unwind-protect
7208           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7209                     (gnus-newsgroup-ephemeral-ignored-charsets
7210                      gnus-newsgroup-ignored-charsets))
7211                 (gnus-group-read-ephemeral-group
7212                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7213                               (nndoc-article-type
7214                                ,(if force 'mbox 'guess))) t))
7215               ;; Make all postings to this group go to the parent group.
7216               (nconc (gnus-info-params (gnus-get-info name))
7217                      params)
7218             ;; Couldn't select this doc group.
7219             (switch-to-buffer buf)
7220             (gnus-set-global-variables)
7221             (gnus-configure-windows 'summary)
7222             (gnus-message 3 "Article couldn't be entered?"))
7223         (kill-buffer dig)))))
7224
7225 (defun gnus-summary-read-document (n)
7226   "Open a new group based on the current article(s).
7227 This will allow you to read digests and other similar
7228 documents as newsgroups.
7229 Obeys the standard process/prefix convention."
7230   (interactive "P")
7231   (let* ((articles (gnus-summary-work-articles n))
7232          (ogroup gnus-newsgroup-name)
7233          (params (append (gnus-info-params (gnus-get-info ogroup))
7234                          (list (cons 'to-group ogroup))))
7235          article group egroup groups vgroup)
7236     (while (setq article (pop articles))
7237       (setq group (format "%s-%d" gnus-newsgroup-name article))
7238       (gnus-summary-remove-process-mark article)
7239       (when (gnus-summary-display-article article)
7240         (save-excursion
7241           (with-temp-buffer
7242             (insert-buffer-substring gnus-original-article-buffer)
7243             ;; Remove some headers that may lead nndoc to make
7244             ;; the wrong guess.
7245             (message-narrow-to-head)
7246             (goto-char (point-min))
7247             (delete-matching-lines "^\\(Path\\):\\|^From ")
7248             (widen)
7249             (if (setq egroup
7250                       (gnus-group-read-ephemeral-group
7251                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7252                                      (nndoc-article-type guess))
7253                        t nil t))
7254                 (progn
7255                   ;; Make all postings to this group go to the parent group.
7256                   (nconc (gnus-info-params (gnus-get-info egroup))
7257                          params)
7258                   (push egroup groups))
7259               ;; Couldn't select this doc group.
7260               (gnus-error 3 "Article couldn't be entered"))))))
7261     ;; Now we have selected all the documents.
7262     (cond
7263      ((not groups)
7264       (error "None of the articles could be interpreted as documents"))
7265      ((gnus-group-read-ephemeral-group
7266        (setq vgroup (format
7267                      "nnvirtual:%s-%s" gnus-newsgroup-name
7268                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7269        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7270        t
7271        (cons (current-buffer) 'summary)))
7272      (t
7273       (error "Couldn't select virtual nndoc group")))))
7274
7275 (defun gnus-summary-isearch-article (&optional regexp-p)
7276   "Do incremental search forward on the current article.
7277 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7278   (interactive "P")
7279   (gnus-summary-select-article)
7280   (gnus-configure-windows 'article)
7281   (gnus-eval-in-buffer-window gnus-article-buffer
7282     (save-restriction
7283       (widen)
7284       (isearch-forward regexp-p))))
7285
7286 (defun gnus-summary-search-article-forward (regexp &optional backward)
7287   "Search for an article containing REGEXP forward.
7288 If BACKWARD, search backward instead."
7289   (interactive
7290    (list (read-string
7291           (format "Search article %s (regexp%s): "
7292                   (if current-prefix-arg "backward" "forward")
7293                   (if gnus-last-search-regexp
7294                       (concat ", default " gnus-last-search-regexp)
7295                     "")))
7296          current-prefix-arg))
7297   (if (string-equal regexp "")
7298       (setq regexp (or gnus-last-search-regexp ""))
7299     (setq gnus-last-search-regexp regexp)
7300     (setq gnus-article-before-search gnus-current-article))
7301   ;; Intentionally set gnus-last-article.
7302   (setq gnus-last-article gnus-article-before-search)
7303   (let ((gnus-last-article gnus-last-article))
7304     (if (gnus-summary-search-article regexp backward)
7305         (gnus-summary-show-thread)
7306       (error "Search failed: \"%s\"" regexp))))
7307
7308 (defun gnus-summary-search-article-backward (regexp)
7309   "Search for an article containing REGEXP backward."
7310   (interactive
7311    (list (read-string
7312           (format "Search article backward (regexp%s): "
7313                   (if gnus-last-search-regexp
7314                       (concat ", default " gnus-last-search-regexp)
7315                     "")))))
7316   (gnus-summary-search-article-forward regexp 'backward))
7317
7318 (defun gnus-summary-search-article (regexp &optional backward)
7319   "Search for an article containing REGEXP.
7320 Optional argument BACKWARD means do search for backward.
7321 `gnus-select-article-hook' is not called during the search."
7322   ;; We have to require this here to make sure that the following
7323   ;; dynamic binding isn't shadowed by autoloading.
7324   (require 'gnus-async)
7325   (require 'gnus-art)
7326   (let ((gnus-select-article-hook nil)  ;Disable hook.
7327         (gnus-article-prepare-hook nil)
7328         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7329         (gnus-use-article-prefetch nil)
7330         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7331         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7332         (sum (current-buffer))
7333         (gnus-display-mime-function nil)
7334         (found nil)
7335         point)
7336     (gnus-save-hidden-threads
7337       (gnus-summary-select-article)
7338       (set-buffer gnus-article-buffer)
7339       (goto-char (window-point (get-buffer-window (current-buffer))))
7340       (when backward
7341         (forward-line -1))
7342       (while (not found)
7343         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7344         (if (if backward
7345                 (re-search-backward regexp nil t)
7346               (re-search-forward regexp nil t))
7347             ;; We found the regexp.
7348             (progn
7349               (setq found 'found)
7350               (beginning-of-line)
7351               (set-window-start
7352                (get-buffer-window (current-buffer))
7353                (point))
7354               (forward-line 1)
7355               (set-window-point
7356                (get-buffer-window (current-buffer))
7357                (point))
7358               (set-buffer sum)
7359               (setq point (point)))
7360           ;; We didn't find it, so we go to the next article.
7361           (set-buffer sum)
7362           (setq found 'not)
7363           (while (eq found 'not)
7364             (if (not (if backward (gnus-summary-find-prev)
7365                        (gnus-summary-find-next)))
7366                 ;; No more articles.
7367                 (setq found t)
7368               ;; Select the next article and adjust point.
7369               (unless (gnus-summary-article-sparse-p
7370                        (gnus-summary-article-number))
7371                 (setq found nil)
7372                 (gnus-summary-select-article)
7373                 (set-buffer gnus-article-buffer)
7374                 (widen)
7375                 (goto-char (if backward (point-max) (point-min))))))))
7376       (gnus-message 7 ""))
7377     ;; Return whether we found the regexp.
7378     (when (eq found 'found)
7379       (goto-char point)
7380       (gnus-summary-show-thread)
7381       (gnus-summary-goto-subject gnus-current-article)
7382       (gnus-summary-position-point)
7383       t)))
7384
7385 (defun gnus-summary-find-matching (header regexp &optional backward unread
7386                                           not-case-fold)
7387   "Return a list of all articles that match REGEXP on HEADER.
7388 The search stars on the current article and goes forwards unless
7389 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7390 If UNREAD is non-nil, only unread articles will
7391 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7392 in the comparisons."
7393   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7394                 (gnus-data-find-list
7395                  (gnus-summary-article-number) (gnus-data-list backward))))
7396         (case-fold-search (not not-case-fold))
7397         articles d func)
7398     (if (consp header)
7399         (if (eq (car header) 'extra)
7400             (setq func
7401                   `(lambda (h)
7402                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7403                          "")))
7404           (error "%s is an invalid header" header))
7405       (unless (fboundp (intern (concat "mail-header-" header)))
7406         (error "%s is not a valid header" header))
7407       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7408     (while data
7409       (setq d (car data))
7410       (and (or (not unread)             ; We want all articles...
7411                (gnus-data-unread-p d))  ; Or just unreads.
7412            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7413            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7414            (push (gnus-data-number d) articles)) ; Success!
7415       (setq data (cdr data)))
7416     (nreverse articles)))
7417
7418 (defun gnus-summary-execute-command (header regexp command &optional backward)
7419   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7420 If HEADER is an empty string (or nil), the match is done on the entire
7421 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7422   (interactive
7423    (list (let ((completion-ignore-case t))
7424            (completing-read
7425             "Header name: "
7426             (mapcar (lambda (string) (list string))
7427                     '("Number" "Subject" "From" "Lines" "Date"
7428                       "Message-ID" "Xref" "References" "Body"))
7429             nil 'require-match))
7430          (read-string "Regexp: ")
7431          (read-key-sequence "Command: ")
7432          current-prefix-arg))
7433   (when (equal header "Body")
7434     (setq header ""))
7435   ;; Hidden thread subtrees must be searched as well.
7436   (gnus-summary-show-all-threads)
7437   ;; We don't want to change current point nor window configuration.
7438   (save-excursion
7439     (save-window-excursion
7440       (gnus-message 6 "Executing %s..." (key-description command))
7441       ;; We'd like to execute COMMAND interactively so as to give arguments.
7442       (gnus-execute header regexp
7443                     `(call-interactively ',(key-binding command))
7444                     backward)
7445       (gnus-message 6 "Executing %s...done" (key-description command)))))
7446
7447 (defun gnus-summary-beginning-of-article ()
7448   "Scroll the article back to the beginning."
7449   (interactive)
7450   (gnus-summary-select-article)
7451   (gnus-configure-windows 'article)
7452   (gnus-eval-in-buffer-window gnus-article-buffer
7453     (widen)
7454     (goto-char (point-min))
7455     (when gnus-page-broken
7456       (gnus-narrow-to-page))))
7457
7458 (defun gnus-summary-end-of-article ()
7459   "Scroll to the end of the article."
7460   (interactive)
7461   (gnus-summary-select-article)
7462   (gnus-configure-windows 'article)
7463   (gnus-eval-in-buffer-window gnus-article-buffer
7464     (widen)
7465     (goto-char (point-max))
7466     (recenter -3)
7467     (when gnus-page-broken
7468       (gnus-narrow-to-page))))
7469
7470 (defun gnus-summary-print-article (&optional filename n)
7471   "Generate and print a PostScript image of the N next (mail) articles.
7472
7473 If N is negative, print the N previous articles.  If N is nil and articles
7474 have been marked with the process mark, print these instead.
7475
7476 If the optional first argument FILENAME is nil, send the image to the
7477 printer.  If FILENAME is a string, save the PostScript image in a file with
7478 that name.  If FILENAME is a number, prompt the user for the name of the file
7479 to save in."
7480   (interactive (list (ps-print-preprint current-prefix-arg)))
7481   (dolist (article (gnus-summary-work-articles n))
7482     (gnus-summary-select-article nil nil 'pseudo article)
7483     (gnus-eval-in-buffer-window gnus-article-buffer
7484       (let ((buffer (generate-new-buffer " *print*")))
7485         (unwind-protect
7486             (progn
7487               (copy-to-buffer buffer (point-min) (point-max))
7488               (set-buffer buffer)
7489               (gnus-article-delete-invisible-text)
7490               (when (gnus-visual-p 'article-highlight 'highlight)
7491                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7492                 ;; highlight.
7493                 (let ((gnus-article-buffer buffer))
7494                   (gnus-article-highlight-citation t)
7495                   (gnus-article-highlight-signature)))
7496               (let ((ps-left-header
7497                      (list
7498                       (concat "("
7499                               (mail-header-subject gnus-current-headers) ")")
7500                       (concat "("
7501                               (mail-header-from gnus-current-headers) ")")))
7502                     (ps-right-header
7503                      (list
7504                       "/pagenumberstring load"
7505                       (concat "("
7506                               (mail-header-date gnus-current-headers) ")"))))
7507                 (gnus-run-hooks 'gnus-ps-print-hook)
7508                 (save-excursion
7509                   (ps-spool-buffer-with-faces))))
7510           (kill-buffer buffer))))
7511     (gnus-summary-remove-process-mark article))
7512   (ps-despool filename))
7513
7514 (defun gnus-summary-show-article (&optional arg)
7515   "Force re-fetching of the current article.
7516 If ARG (the prefix) is a number, show the article with the charset
7517 defined in `gnus-summary-show-article-charset-alist', or the charset
7518 inputed.
7519 If ARG (the prefix) is non-nil and not a number, show the raw article
7520 without any article massaging functions being run."
7521   (interactive "P")
7522   (cond
7523    ((numberp arg)
7524     (let ((gnus-newsgroup-charset
7525            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7526                (read-coding-system "Charset: ")))
7527           (gnus-newsgroup-ignored-charsets 'gnus-all))
7528       (gnus-summary-select-article nil 'force)
7529       (let ((deps gnus-newsgroup-dependencies)
7530             head header)
7531         (save-excursion
7532           (set-buffer gnus-original-article-buffer)
7533           (save-restriction
7534             (message-narrow-to-head)
7535             (setq head (buffer-string)))
7536           (with-temp-buffer
7537             (insert (format "211 %d Article retrieved.\n"
7538                             (cdr gnus-article-current)))
7539             (insert head)
7540             (insert ".\n")
7541             (let ((nntp-server-buffer (current-buffer)))
7542               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7543         (gnus-data-set-header
7544          (gnus-data-find (cdr gnus-article-current))
7545          header)
7546         (gnus-summary-update-article-line
7547          (cdr gnus-article-current) header))))
7548    ((not arg)
7549     ;; Select the article the normal way.
7550     (gnus-summary-select-article nil 'force))
7551    (t
7552     ;; We have to require this here to make sure that the following
7553     ;; dynamic binding isn't shadowed by autoloading.
7554     (require 'gnus-async)
7555     (require 'gnus-art)
7556     ;; Bind the article treatment functions to nil.
7557     (let ((gnus-have-all-headers t)
7558           gnus-article-prepare-hook
7559           gnus-article-decode-hook
7560           gnus-display-mime-function
7561           gnus-break-pages)
7562       ;; Destroy any MIME parts.
7563       (when (gnus-buffer-live-p gnus-article-buffer)
7564         (save-excursion
7565           (set-buffer gnus-article-buffer)
7566           (mm-destroy-parts gnus-article-mime-handles)
7567           ;; Set it to nil for safety reason.
7568           (setq gnus-article-mime-handle-alist nil)
7569           (setq gnus-article-mime-handles nil)))
7570       (gnus-summary-select-article nil 'force))))
7571   (gnus-summary-goto-subject gnus-current-article)
7572   (gnus-summary-position-point))
7573
7574 (defun gnus-summary-verbose-headers (&optional arg)
7575   "Toggle permanent full header display.
7576 If ARG is a positive number, turn header display on.
7577 If ARG is a negative number, turn header display off."
7578   (interactive "P")
7579   (setq gnus-show-all-headers
7580         (cond ((or (not (numberp arg))
7581                    (zerop arg))
7582                (not gnus-show-all-headers))
7583               ((natnump arg)
7584                t)))
7585   (gnus-summary-show-article))
7586
7587 (defun gnus-summary-toggle-header (&optional arg)
7588   "Show the headers if they are hidden, or hide them if they are shown.
7589 If ARG is a positive number, show the entire header.
7590 If ARG is a negative number, hide the unwanted header lines."
7591   (interactive "P")
7592   (save-excursion
7593     (set-buffer gnus-article-buffer)
7594     (save-restriction
7595       (let* ((buffer-read-only nil)
7596              (inhibit-point-motion-hooks t)
7597              hidden e)
7598         (setq hidden
7599               (if (numberp arg)
7600                   (>= arg 0)
7601                 (save-restriction
7602                   (article-narrow-to-head)
7603                   (gnus-article-hidden-text-p 'headers))))
7604         (goto-char (point-min))
7605         (when (search-forward "\n\n" nil t)
7606           (delete-region (point-min) (1- (point))))
7607         (goto-char (point-min))
7608         (save-excursion
7609           (set-buffer gnus-original-article-buffer)
7610           (goto-char (point-min))
7611           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7612         (insert-buffer-substring gnus-original-article-buffer 1 e)
7613         (save-restriction
7614           (narrow-to-region (point-min) (point))
7615           (article-decode-encoded-words)
7616           (if  hidden
7617               (let ((gnus-treat-hide-headers nil)
7618                     (gnus-treat-hide-boring-headers nil))
7619                 (setq gnus-article-wash-types
7620                       (delq 'headers gnus-article-wash-types))
7621                 (gnus-treat-article 'head))
7622             (gnus-treat-article 'head)))
7623         (gnus-set-mode-line 'article)))))
7624
7625 (defun gnus-summary-show-all-headers ()
7626   "Make all header lines visible."
7627   (interactive)
7628   (gnus-article-show-all-headers))
7629
7630 (defun gnus-summary-caesar-message (&optional arg)
7631   "Caesar rotate the current article by 13.
7632 The numerical prefix specifies how many places to rotate each letter
7633 forward."
7634   (interactive "P")
7635   (gnus-summary-select-article)
7636   (let ((mail-header-separator ""))
7637     (gnus-eval-in-buffer-window gnus-article-buffer
7638       (save-restriction
7639         (widen)
7640         (let ((start (window-start))
7641               buffer-read-only)
7642           (message-caesar-buffer-body arg)
7643           (set-window-start (get-buffer-window (current-buffer)) start))))))
7644
7645 (defun gnus-summary-stop-page-breaking ()
7646   "Stop page breaking in the current article."
7647   (interactive)
7648   (gnus-summary-select-article)
7649   (gnus-eval-in-buffer-window gnus-article-buffer
7650     (widen)
7651     (when (gnus-visual-p 'page-marker)
7652       (let ((buffer-read-only nil))
7653         (gnus-remove-text-with-property 'gnus-prev)
7654         (gnus-remove-text-with-property 'gnus-next))
7655       (setq gnus-page-broken nil))))
7656
7657 (defun gnus-summary-move-article (&optional n to-newsgroup
7658                                             select-method action)
7659   "Move the current article to a different newsgroup.
7660 If N is a positive number, move the N next articles.
7661 If N is a negative number, move the N previous articles.
7662 If N is nil and any articles have been marked with the process mark,
7663 move those articles instead.
7664 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7665 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7666 re-spool using this method.
7667
7668 For this function to work, both the current newsgroup and the
7669 newsgroup that you want to move to have to support the `request-move'
7670 and `request-accept' functions.
7671
7672 ACTION can be either `move' (the default), `crosspost' or `copy'."
7673   (interactive "P")
7674   (unless action
7675     (setq action 'move))
7676   ;; Disable marking as read.
7677   (let (gnus-mark-article-hook)
7678     (save-window-excursion
7679       (gnus-summary-select-article)))
7680   ;; Check whether the source group supports the required functions.
7681   (cond ((and (eq action 'move)
7682               (not (gnus-check-backend-function
7683                     'request-move-article gnus-newsgroup-name)))
7684          (error "The current group does not support article moving"))
7685         ((and (eq action 'crosspost)
7686               (not (gnus-check-backend-function
7687                     'request-replace-article gnus-newsgroup-name)))
7688          (error "The current group does not support article editing")))
7689   (let ((articles (gnus-summary-work-articles n))
7690         (prefix (if (gnus-check-backend-function
7691                     'request-move-article gnus-newsgroup-name)
7692                     (gnus-group-real-prefix gnus-newsgroup-name)
7693                   ""))
7694         (names '((move "Move" "Moving")
7695                  (copy "Copy" "Copying")
7696                  (crosspost "Crosspost" "Crossposting")))
7697         (copy-buf (save-excursion
7698                     (nnheader-set-temp-buffer " *copy article*")))
7699         art-group to-method new-xref article to-groups)
7700     (unless (assq action names)
7701       (error "Unknown action %s" action))
7702     ;; Read the newsgroup name.
7703     (when (and (not to-newsgroup)
7704                (not select-method))
7705       (setq to-newsgroup
7706             (gnus-read-move-group-name
7707              (cadr (assq action names))
7708              (symbol-value (intern (format "gnus-current-%s-group" action)))
7709              articles prefix))
7710       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7711     (setq to-method (or select-method
7712                         (gnus-server-to-method
7713                          (gnus-group-method to-newsgroup))))
7714     ;; Check the method we are to move this article to...
7715     (unless (gnus-check-backend-function
7716              'request-accept-article (car to-method))
7717       (error "%s does not support article copying" (car to-method)))
7718     (unless (gnus-check-server to-method)
7719       (error "Can't open server %s" (car to-method)))
7720     (gnus-message 6 "%s to %s: %s..."
7721                   (caddr (assq action names))
7722                   (or (car select-method) to-newsgroup) articles)
7723     (while articles
7724       (setq article (pop articles))
7725       (setq
7726        art-group
7727        (cond
7728         ;; Move the article.
7729         ((eq action 'move)
7730          ;; Remove this article from future suppression.
7731          (gnus-dup-unsuppress-article article)
7732          (gnus-request-move-article
7733           article                       ; Article to move
7734           gnus-newsgroup-name           ; From newsgroup
7735           (nth 1 (gnus-find-method-for-group
7736                   gnus-newsgroup-name)) ; Server
7737           (list 'gnus-request-accept-article
7738                 to-newsgroup (list 'quote select-method)
7739                 (not articles) t)       ; Accept form
7740           (not articles)))              ; Only save nov last time
7741         ;; Copy the article.
7742         ((eq action 'copy)
7743          (save-excursion
7744            (set-buffer copy-buf)
7745            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7746              (gnus-request-accept-article
7747               to-newsgroup select-method (not articles) t))))
7748         ;; Crosspost the article.
7749         ((eq action 'crosspost)
7750          (let ((xref (message-tokenize-header
7751                       (mail-header-xref (gnus-summary-article-header article))
7752                       " ")))
7753            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7754                                   ":" article))
7755            (unless xref
7756              (setq xref (list (system-name))))
7757            (setq new-xref
7758                  (concat
7759                   (mapconcat 'identity
7760                              (delete "Xref:" (delete new-xref xref))
7761                              " ")
7762                   " " new-xref))
7763            (save-excursion
7764              (set-buffer copy-buf)
7765              ;; First put the article in the destination group.
7766              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7767              (when (consp (setq art-group
7768                                 (gnus-request-accept-article
7769                                  to-newsgroup select-method (not articles))))
7770                (setq new-xref (concat new-xref " " (car art-group)
7771                                       ":" (cdr art-group)))
7772                ;; Now we have the new Xrefs header, so we insert
7773                ;; it and replace the new article.
7774                (nnheader-replace-header "Xref" new-xref)
7775                (gnus-request-replace-article
7776                 (cdr art-group) to-newsgroup (current-buffer))
7777                art-group))))))
7778       (cond
7779        ((not art-group)
7780         (gnus-message 1 "Couldn't %s article %s: %s"
7781                       (cadr (assq action names)) article
7782                       (nnheader-get-report (car to-method))))
7783        ((eq art-group 'junk)
7784         (when (eq action 'move)
7785           (gnus-summary-mark-article article gnus-canceled-mark)
7786           (gnus-message 4 "Deleted article %s" article)))
7787        (t
7788         (let* ((pto-group (gnus-group-prefixed-name
7789                            (car art-group) to-method))
7790                (entry
7791                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7792                (info (nth 2 entry))
7793                (to-group (gnus-info-group info))
7794                to-marks)
7795           ;; Update the group that has been moved to.
7796           (when (and info
7797                      (memq action '(move copy)))
7798             (unless (member to-group to-groups)
7799               (push to-group to-groups))
7800
7801             (unless (memq article gnus-newsgroup-unreads)
7802               (push 'read to-marks)
7803               (gnus-info-set-read
7804                info (gnus-add-to-range (gnus-info-read info)
7805                                        (list (cdr art-group)))))
7806
7807             ;; See whether the article is to be put in the cache.
7808             (let ((marks gnus-article-mark-lists)
7809                   (to-article (cdr art-group)))
7810
7811               ;; Enter the article into the cache in the new group,
7812               ;; if that is required.
7813               (when gnus-use-cache
7814                 (gnus-cache-possibly-enter-article
7815                  to-group to-article
7816                  (memq article gnus-newsgroup-marked)
7817                  (memq article gnus-newsgroup-dormant)
7818                  (memq article gnus-newsgroup-unreads)))
7819
7820               (when gnus-preserve-marks
7821                 ;; Copy any marks over to the new group.
7822                 (when (and (equal to-group gnus-newsgroup-name)
7823                            (not (memq article gnus-newsgroup-unreads)))
7824                   ;; Mark this article as read in this group.
7825                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7826                   (setcdr (gnus-active to-group) to-article)
7827                   (setcdr gnus-newsgroup-active to-article))
7828
7829                 (while marks
7830                   (when (memq article (symbol-value
7831                                        (intern (format "gnus-newsgroup-%s"
7832                                                        (caar marks)))))
7833                     (push (cdar marks) to-marks)
7834                     ;; If the other group is the same as this group,
7835                     ;; then we have to add the mark to the list.
7836                     (when (equal to-group gnus-newsgroup-name)
7837                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7838                            (cons to-article
7839                                  (symbol-value
7840                                   (intern (format "gnus-newsgroup-%s"
7841                                                   (caar marks)))))))
7842                     ;; Copy the marks to other group.
7843                     (gnus-add-marked-articles
7844                      to-group (cdar marks) (list to-article) info))
7845                   (setq marks (cdr marks)))
7846
7847                 (gnus-request-set-mark to-group (list (list (list to-article)
7848                                                             'set
7849                                                             to-marks))))
7850
7851               (gnus-dribble-enter
7852                (concat "(gnus-group-set-info '"
7853                        (gnus-prin1-to-string (gnus-get-info to-group))
7854                        ")"))))
7855
7856           ;; Update the Xref header in this article to point to
7857           ;; the new crossposted article we have just created.
7858           (when (eq action 'crosspost)
7859             (save-excursion
7860               (set-buffer copy-buf)
7861               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7862               (nnheader-replace-header "Xref" new-xref)
7863               (gnus-request-replace-article
7864                article gnus-newsgroup-name (current-buffer)))))
7865
7866         ;;;!!!Why is this necessary?
7867         (set-buffer gnus-summary-buffer)
7868
7869         (gnus-summary-goto-subject article)
7870         (when (eq action 'move)
7871           (gnus-summary-mark-article article gnus-canceled-mark))))
7872       (gnus-summary-remove-process-mark article))
7873     ;; Re-activate all groups that have been moved to.
7874     (while to-groups
7875       (save-excursion
7876         (set-buffer gnus-group-buffer)
7877         (when (gnus-group-goto-group (car to-groups) t)
7878           (gnus-group-get-new-news-this-group 1 t))
7879         (pop to-groups)))
7880
7881     (gnus-kill-buffer copy-buf)
7882     (gnus-summary-position-point)
7883     (gnus-set-mode-line 'summary)))
7884
7885 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7886   "Move the current article to a different newsgroup.
7887 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7888 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7889 re-spool using this method."
7890   (interactive "P")
7891   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7892
7893 (defun gnus-summary-crosspost-article (&optional n)
7894   "Crosspost the current article to some other group."
7895   (interactive "P")
7896   (gnus-summary-move-article n nil nil 'crosspost))
7897
7898 (defcustom gnus-summary-respool-default-method nil
7899   "Default method for respooling an article.
7900 If nil, use to the current newsgroup method."
7901   :type '(choice (gnus-select-method :value (nnml ""))
7902                  (const nil))
7903   :group 'gnus-summary-mail)
7904
7905 (defun gnus-summary-respool-article (&optional n method)
7906   "Respool the current article.
7907 The article will be squeezed through the mail spooling process again,
7908 which means that it will be put in some mail newsgroup or other
7909 depending on `nnmail-split-methods'.
7910 If N is a positive number, respool the N next articles.
7911 If N is a negative number, respool the N previous articles.
7912 If N is nil and any articles have been marked with the process mark,
7913 respool those articles instead.
7914
7915 Respooling can be done both from mail groups and \"real\" newsgroups.
7916 In the former case, the articles in question will be moved from the
7917 current group into whatever groups they are destined to.  In the
7918 latter case, they will be copied into the relevant groups."
7919   (interactive
7920    (list current-prefix-arg
7921          (let* ((methods (gnus-methods-using 'respool))
7922                 (methname
7923                  (symbol-name (or gnus-summary-respool-default-method
7924                                   (car (gnus-find-method-for-group
7925                                         gnus-newsgroup-name)))))
7926                 (method
7927                  (gnus-completing-read
7928                   methname "What backend do you want to use when respooling?"
7929                   methods nil t nil 'gnus-mail-method-history))
7930                 ms)
7931            (cond
7932             ((zerop (length (setq ms (gnus-servers-using-backend
7933                                       (intern method)))))
7934              (list (intern method) ""))
7935             ((= 1 (length ms))
7936              (car ms))
7937             (t
7938              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7939                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7940                            ms-alist))))))))
7941   (unless method
7942     (error "No method given for respooling"))
7943   (if (assoc (symbol-name
7944               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7945              (gnus-methods-using 'respool))
7946       (gnus-summary-move-article n nil method)
7947     (gnus-summary-copy-article n nil method)))
7948
7949 (defun gnus-summary-import-article (file)
7950   "Import an arbitrary file into a mail newsgroup."
7951   (interactive "fImport file: ")
7952   (let ((group gnus-newsgroup-name)
7953         (now (current-time))
7954         atts lines)
7955     (unless (gnus-check-backend-function 'request-accept-article group)
7956       (error "%s does not support article importing" group))
7957     (or (file-readable-p file)
7958         (not (file-regular-p file))
7959         (error "Can't read %s" file))
7960     (save-excursion
7961       (set-buffer (gnus-get-buffer-create " *import file*"))
7962       (erase-buffer)
7963       (nnheader-insert-file-contents file)
7964       (goto-char (point-min))
7965       (unless (nnheader-article-p)
7966         ;; This doesn't look like an article, so we fudge some headers.
7967         (setq atts (file-attributes file)
7968               lines (count-lines (point-min) (point-max)))
7969         (insert "From: " (read-string "From: ") "\n"
7970                 "Subject: " (read-string "Subject: ") "\n"
7971                 "Date: " (message-make-date (nth 5 atts))
7972                 "\n"
7973                 "Message-ID: " (message-make-message-id) "\n"
7974                 "Lines: " (int-to-string lines) "\n"
7975                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7976       (gnus-request-accept-article group nil t)
7977       (kill-buffer (current-buffer)))))
7978
7979 (defun gnus-summary-article-posted-p ()
7980   "Say whether the current (mail) article is available from news as well.
7981 This will be the case if the article has both been mailed and posted."
7982   (interactive)
7983   (let ((id (mail-header-references (gnus-summary-article-header)))
7984         (gnus-override-method (car (gnus-refer-article-methods))))
7985     (if (gnus-request-head id "")
7986         (gnus-message 2 "The current message was found on %s"
7987                       gnus-override-method)
7988       (gnus-message 2 "The current message couldn't be found on %s"
7989                     gnus-override-method)
7990       nil)))
7991
7992 (defun gnus-summary-expire-articles (&optional now)
7993   "Expire all articles that are marked as expirable in the current group."
7994   (interactive)
7995   (when (gnus-check-backend-function
7996          'request-expire-articles gnus-newsgroup-name)
7997     ;; This backend supports expiry.
7998     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7999            (expirable (if total
8000                           (progn
8001                             ;; We need to update the info for
8002                             ;; this group for `gnus-list-of-read-articles'
8003                             ;; to give us the right answer.
8004                             (gnus-run-hooks 'gnus-exit-group-hook)
8005                             (gnus-summary-update-info)
8006                             (gnus-list-of-read-articles gnus-newsgroup-name))
8007                         (setq gnus-newsgroup-expirable
8008                               (sort gnus-newsgroup-expirable '<))))
8009            (expiry-wait (if now 'immediate
8010                           (gnus-group-find-parameter
8011                            gnus-newsgroup-name 'expiry-wait)))
8012            (nnmail-expiry-target
8013             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8014                 nnmail-expiry-target))
8015            es)
8016       (when expirable
8017         ;; There are expirable articles in this group, so we run them
8018         ;; through the expiry process.
8019         (gnus-message 6 "Expiring articles...")
8020         (unless (gnus-check-group gnus-newsgroup-name)
8021           (error "Can't open server for %s" gnus-newsgroup-name))
8022         ;; The list of articles that weren't expired is returned.
8023         (save-excursion
8024           (if expiry-wait
8025               (let ((nnmail-expiry-wait-function nil)
8026                     (nnmail-expiry-wait expiry-wait))
8027                 (setq es (gnus-request-expire-articles
8028                           expirable gnus-newsgroup-name)))
8029             (setq es (gnus-request-expire-articles
8030                       expirable gnus-newsgroup-name)))
8031           (unless total
8032             (setq gnus-newsgroup-expirable es))
8033           ;; We go through the old list of expirable, and mark all
8034           ;; really expired articles as nonexistent.
8035           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8036             (let ((gnus-use-cache nil))
8037               (while expirable
8038                 (unless (memq (car expirable) es)
8039                   (when (gnus-data-find (car expirable))
8040                     (gnus-summary-mark-article
8041                      (car expirable) gnus-canceled-mark)))
8042                 (setq expirable (cdr expirable))))))
8043         (gnus-message 6 "Expiring articles...done")))))
8044
8045 (defun gnus-summary-expire-articles-now ()
8046   "Expunge all expirable articles in the current group.
8047 This means that *all* articles that are marked as expirable will be
8048 deleted forever, right now."
8049   (interactive)
8050   (or gnus-expert-user
8051       (gnus-yes-or-no-p
8052        "Are you really, really, really sure you want to delete all these messages? ")
8053       (error "Phew!"))
8054   (gnus-summary-expire-articles t))
8055
8056 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8057 (defun gnus-summary-delete-article (&optional n)
8058   "Delete the N next (mail) articles.
8059 This command actually deletes articles.  This is not a marking
8060 command.  The article will disappear forever from your life, never to
8061 return.
8062 If N is negative, delete backwards.
8063 If N is nil and articles have been marked with the process mark,
8064 delete these instead."
8065   (interactive "P")
8066   (unless (gnus-check-backend-function 'request-expire-articles
8067                                        gnus-newsgroup-name)
8068     (error "The current newsgroup does not support article deletion"))
8069   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8070     (error "Couldn't open server"))
8071   ;; Compute the list of articles to delete.
8072   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8073         not-deleted)
8074     (if (and gnus-novice-user
8075              (not (gnus-yes-or-no-p
8076                    (format "Do you really want to delete %s forever? "
8077                            (if (> (length articles) 1)
8078                                (format "these %s articles" (length articles))
8079                              "this article")))))
8080         ()
8081       ;; Delete the articles.
8082       (setq not-deleted (gnus-request-expire-articles
8083                          articles gnus-newsgroup-name 'force))
8084       (while articles
8085         (gnus-summary-remove-process-mark (car articles))
8086         ;; The backend might not have been able to delete the article
8087         ;; after all.
8088         (unless (memq (car articles) not-deleted)
8089           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8090         (setq articles (cdr articles)))
8091       (when not-deleted
8092         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8093     (gnus-summary-position-point)
8094     (gnus-set-mode-line 'summary)
8095     not-deleted))
8096
8097 (defun gnus-summary-edit-article (&optional arg)
8098   "Edit the current article.
8099 This will have permanent effect only in mail groups.
8100 If ARG is nil, edit the decoded articles.
8101 If ARG is 1, edit the raw articles.
8102 If ARG is 2, edit the raw articles even in read-only groups.
8103 If ARG is 3, edit the articles with the current handles.
8104 Otherwise, allow editing of articles even in read-only
8105 groups."
8106   (interactive "P")
8107   (let (force raw current-handles)
8108     (cond
8109      ((null arg))
8110      ((eq arg 1) (setq raw t))
8111      ((eq arg 2) (setq raw t
8112                        force t))
8113      ((eq arg 3) (setq current-handles
8114                        (and (gnus-buffer-live-p gnus-article-buffer)
8115                             (with-current-buffer gnus-article-buffer
8116                               (prog1
8117                                   gnus-article-mime-handles
8118                                   (setq gnus-article-mime-handles nil))))))
8119      (t (setq force t)))
8120     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8121         (error "Can't edit the raw article in group nndraft:drafts."))
8122     (save-excursion
8123       (set-buffer gnus-summary-buffer)
8124       (let ((mail-parse-charset gnus-newsgroup-charset)
8125             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8126         (gnus-set-global-variables)
8127         (when (and (not force)
8128                    (gnus-group-read-only-p))
8129           (error "The current newsgroup does not support article editing"))
8130         (gnus-summary-show-article t)
8131         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8132           (with-current-buffer gnus-article-buffer
8133             (mm-enable-multibyte-mule4)))
8134         (if (equal gnus-newsgroup-name "nndraft:drafts")
8135             (setq raw t))
8136         (gnus-article-edit-article
8137          (if raw 'ignore
8138            `(lambda ()
8139               (let ((mbl mml-buffer-list))
8140                 (setq mml-buffer-list nil)
8141                 (mime-to-mml ,'current-handles)
8142                 (make-local-hook 'kill-buffer-hook)
8143                 (let ((mbl1 mml-buffer-list))
8144                   (setq mml-buffer-list mbl)
8145                   (set (make-local-variable 'mml-buffer-list) mbl1))
8146                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8147          `(lambda (no-highlight)
8148             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8149                   (message-options message-options)
8150                   (message-options-set-recipient)
8151                   (mail-parse-ignored-charsets
8152                    ',gnus-newsgroup-ignored-charsets))
8153               ,(if (not raw) '(progn
8154                                 (mml-to-mime)
8155                                 (mml-destroy-buffers)
8156                                 (remove-hook 'kill-buffer-hook
8157                                              'mml-destroy-buffers t)
8158                                 (kill-local-variable 'mml-buffer-list)))
8159               (gnus-summary-edit-article-done
8160                ,(or (mail-header-references gnus-current-headers) "")
8161                ,(gnus-group-read-only-p)
8162                ,gnus-summary-buffer no-highlight))))))))
8163
8164 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8165
8166 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8167                                                  no-highlight)
8168   "Make edits to the current article permanent."
8169   (interactive)
8170   (save-excursion
8171     ;; The buffer restriction contains the entire article if it exists.
8172     (when (article-goto-body)
8173       (let ((lines (count-lines (point) (point-max)))
8174             (length (- (point-max) (point)))
8175             (case-fold-search t)
8176             (body (copy-marker (point))))
8177         (goto-char (point-min))
8178         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8179           (delete-region (match-beginning 1) (match-end 1))
8180           (insert (number-to-string length)))
8181         (goto-char (point-min))
8182         (when (re-search-forward
8183                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8184           (delete-region (match-beginning 1) (match-end 1))
8185           (insert (number-to-string length)))
8186         (goto-char (point-min))
8187         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8188           (delete-region (match-beginning 1) (match-end 1))
8189           (insert (number-to-string lines))))))
8190   ;; Replace the article.
8191   (let ((buf (current-buffer)))
8192     (with-temp-buffer
8193       (insert-buffer-substring buf)
8194
8195       (if (and (not read-only)
8196                (not (gnus-request-replace-article
8197                      (cdr gnus-article-current) (car gnus-article-current)
8198                      (current-buffer) t)))
8199           (error "Couldn't replace article")
8200         ;; Update the summary buffer.
8201         (if (and references
8202                  (equal (message-tokenize-header references " ")
8203                         (message-tokenize-header
8204                          (or (message-fetch-field "references") "") " ")))
8205             ;; We only have to update this line.
8206             (save-excursion
8207               (save-restriction
8208                 (message-narrow-to-head)
8209                 (let ((head (buffer-string))
8210                       header)
8211                   (with-temp-buffer
8212                     (insert (format "211 %d Article retrieved.\n"
8213                                     (cdr gnus-article-current)))
8214                     (insert head)
8215                     (insert ".\n")
8216                     (let ((nntp-server-buffer (current-buffer)))
8217                       (setq header (car (gnus-get-newsgroup-headers
8218                                          (save-excursion
8219                                            (set-buffer gnus-summary-buffer)
8220                                            gnus-newsgroup-dependencies)
8221                                          t))))
8222                     (save-excursion
8223                       (set-buffer gnus-summary-buffer)
8224                       (gnus-data-set-header
8225                        (gnus-data-find (cdr gnus-article-current))
8226                        header)
8227                       (gnus-summary-update-article-line
8228                        (cdr gnus-article-current) header))))))
8229           ;; Update threads.
8230           (set-buffer (or buffer gnus-summary-buffer))
8231           (gnus-summary-update-article (cdr gnus-article-current)))
8232         ;; Prettify the article buffer again.
8233         (unless no-highlight
8234           (save-excursion
8235             (set-buffer gnus-article-buffer)
8236             ;;;!!! Fix this -- article should be rehighlighted.
8237             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8238             (set-buffer gnus-original-article-buffer)
8239             (gnus-request-article
8240              (cdr gnus-article-current)
8241              (car gnus-article-current) (current-buffer))))
8242         ;; Prettify the summary buffer line.
8243         (when (gnus-visual-p 'summary-highlight 'highlight)
8244           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8245
8246 (defun gnus-summary-edit-wash (key)
8247   "Perform editing command KEY in the article buffer."
8248   (interactive
8249    (list
8250     (progn
8251       (message "%s" (concat (this-command-keys) "- "))
8252       (read-char))))
8253   (message "")
8254   (gnus-summary-edit-article)
8255   (execute-kbd-macro (concat (this-command-keys) key))
8256   (gnus-article-edit-done))
8257
8258 ;;; Respooling
8259
8260 (defun gnus-summary-respool-query (&optional silent trace)
8261   "Query where the respool algorithm would put this article."
8262   (interactive)
8263   (let (gnus-mark-article-hook)
8264     (gnus-summary-select-article)
8265     (save-excursion
8266       (set-buffer gnus-original-article-buffer)
8267       (save-restriction
8268         (message-narrow-to-head)
8269         (let ((groups (nnmail-article-group 'identity trace)))
8270           (unless silent
8271             (if groups
8272                 (message "This message would go to %s"
8273                          (mapconcat 'car groups ", "))
8274               (message "This message would go to no groups"))
8275             groups))))))
8276
8277 (defun gnus-summary-respool-trace ()
8278   "Trace where the respool algorithm would put this article.
8279 Display a buffer showing all fancy splitting patterns which matched."
8280   (interactive)
8281   (gnus-summary-respool-query nil t))
8282
8283 ;; Summary marking commands.
8284
8285 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8286   "Mark articles which has the same subject as read, and then select the next.
8287 If UNMARK is positive, remove any kind of mark.
8288 If UNMARK is negative, tick articles."
8289   (interactive "P")
8290   (when unmark
8291     (setq unmark (prefix-numeric-value unmark)))
8292   (let ((count
8293          (gnus-summary-mark-same-subject
8294           (gnus-summary-article-subject) unmark)))
8295     ;; Select next unread article.  If auto-select-same mode, should
8296     ;; select the first unread article.
8297     (gnus-summary-next-article t (and gnus-auto-select-same
8298                                       (gnus-summary-article-subject)))
8299     (gnus-message 7 "%d article%s marked as %s"
8300                   count (if (= count 1) " is" "s are")
8301                   (if unmark "unread" "read"))))
8302
8303 (defun gnus-summary-kill-same-subject (&optional unmark)
8304   "Mark articles which has the same subject as read.
8305 If UNMARK is positive, remove any kind of mark.
8306 If UNMARK is negative, tick articles."
8307   (interactive "P")
8308   (when unmark
8309     (setq unmark (prefix-numeric-value unmark)))
8310   (let ((count
8311          (gnus-summary-mark-same-subject
8312           (gnus-summary-article-subject) unmark)))
8313     ;; If marked as read, go to next unread subject.
8314     (when (null unmark)
8315       ;; Go to next unread subject.
8316       (gnus-summary-next-subject 1 t))
8317     (gnus-message 7 "%d articles are marked as %s"
8318                   count (if unmark "unread" "read"))))
8319
8320 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8321   "Mark articles with same SUBJECT as read, and return marked number.
8322 If optional argument UNMARK is positive, remove any kinds of marks.
8323 If optional argument UNMARK is negative, mark articles as unread instead."
8324   (let ((count 1))
8325     (save-excursion
8326       (cond
8327        ((null unmark)                   ; Mark as read.
8328         (while (and
8329                 (progn
8330                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8331                   (gnus-summary-show-thread) t)
8332                 (gnus-summary-find-subject subject))
8333           (setq count (1+ count))))
8334        ((> unmark 0)                    ; Tick.
8335         (while (and
8336                 (progn
8337                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8338                   (gnus-summary-show-thread) t)
8339                 (gnus-summary-find-subject subject))
8340           (setq count (1+ count))))
8341        (t                               ; Mark as unread.
8342         (while (and
8343                 (progn
8344                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8345                   (gnus-summary-show-thread) t)
8346                 (gnus-summary-find-subject subject))
8347           (setq count (1+ count)))))
8348       (gnus-set-mode-line 'summary)
8349       ;; Return the number of marked articles.
8350       count)))
8351
8352 (defun gnus-summary-mark-as-processable (n &optional unmark)
8353   "Set the process mark on the next N articles.
8354 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8355 the process mark instead.  The difference between N and the actual
8356 number of articles marked is returned."
8357   (interactive "P")
8358   (if (and (null n) (gnus-region-active-p))
8359       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8360     (setq n (prefix-numeric-value n))
8361     (let ((backward (< n 0))
8362           (n (abs n)))
8363       (while (and
8364               (> n 0)
8365               (if unmark
8366                 (gnus-summary-remove-process-mark
8367                  (gnus-summary-article-number))
8368                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8369               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8370         (setq n (1- n)))
8371       (when (/= 0 n)
8372         (gnus-message 7 "No more articles"))
8373       (gnus-summary-recenter)
8374       (gnus-summary-position-point)
8375       n)))
8376
8377 (defun gnus-summary-unmark-as-processable (n)
8378   "Remove the process mark from the next N articles.
8379 If N is negative, unmark backward instead.  The difference between N and
8380 the actual number of articles unmarked is returned."
8381   (interactive "P")
8382   (gnus-summary-mark-as-processable n t))
8383
8384 (defun gnus-summary-unmark-all-processable ()
8385   "Remove the process mark from all articles."
8386   (interactive)
8387   (save-excursion
8388     (while gnus-newsgroup-processable
8389       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8390   (gnus-summary-position-point))
8391
8392 (defun gnus-summary-add-mark (article type)
8393   "Mark ARTICLE with a mark of TYPE."
8394   (let ((vtype (car (assq type gnus-article-mark-lists)))
8395         var)
8396     (if (not vtype)
8397         (error "No such mark type: %s" type)
8398       (setq var (intern (format "gnus-newsgroup-%s" type)))
8399       (set var (cons article (symbol-value var)))
8400       (if (memq type '(processable cached replied saved))
8401           (gnus-summary-update-secondary-mark article)
8402         ;;; !!! This is bobus.  We should find out what primary
8403         ;;; !!! mark we want to set.
8404         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8405
8406 (defun gnus-summary-mark-as-expirable (n)
8407   "Mark N articles forward as expirable.
8408 If N is negative, mark backward instead.  The difference between N and
8409 the actual number of articles marked is returned."
8410   (interactive "p")
8411   (gnus-summary-mark-forward n gnus-expirable-mark))
8412
8413 (defun gnus-summary-mark-article-as-replied (article)
8414   "Mark ARTICLE replied and update the summary line."
8415   (push article gnus-newsgroup-replied)
8416   (let ((buffer-read-only nil))
8417     (when (gnus-summary-goto-subject article nil t)
8418       (gnus-summary-update-secondary-mark article))))
8419
8420 (defun gnus-summary-set-bookmark (article)
8421   "Set a bookmark in current article."
8422   (interactive (list (gnus-summary-article-number)))
8423   (when (or (not (get-buffer gnus-article-buffer))
8424             (not gnus-current-article)
8425             (not gnus-article-current)
8426             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8427     (error "No current article selected"))
8428   ;; Remove old bookmark, if one exists.
8429   (let ((old (assq article gnus-newsgroup-bookmarks)))
8430     (when old
8431       (setq gnus-newsgroup-bookmarks
8432             (delq old gnus-newsgroup-bookmarks))))
8433   ;; Set the new bookmark, which is on the form
8434   ;; (article-number . line-number-in-body).
8435   (push
8436    (cons article
8437          (save-excursion
8438            (set-buffer gnus-article-buffer)
8439            (count-lines
8440             (min (point)
8441                  (save-excursion
8442                    (goto-char (point-min))
8443                    (search-forward "\n\n" nil t)
8444                    (point)))
8445             (point))))
8446    gnus-newsgroup-bookmarks)
8447   (gnus-message 6 "A bookmark has been added to the current article."))
8448
8449 (defun gnus-summary-remove-bookmark (article)
8450   "Remove the bookmark from the current article."
8451   (interactive (list (gnus-summary-article-number)))
8452   ;; Remove old bookmark, if one exists.
8453   (let ((old (assq article gnus-newsgroup-bookmarks)))
8454     (if old
8455         (progn
8456           (setq gnus-newsgroup-bookmarks
8457                 (delq old gnus-newsgroup-bookmarks))
8458           (gnus-message 6 "Removed bookmark."))
8459       (gnus-message 6 "No bookmark in current article."))))
8460
8461 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8462 (defun gnus-summary-mark-as-dormant (n)
8463   "Mark N articles forward as dormant.
8464 If N is negative, mark backward instead.  The difference between N and
8465 the actual number of articles marked is returned."
8466   (interactive "p")
8467   (gnus-summary-mark-forward n gnus-dormant-mark))
8468
8469 (defun gnus-summary-set-process-mark (article)
8470   "Set the process mark on ARTICLE and update the summary line."
8471   (setq gnus-newsgroup-processable
8472         (cons article
8473               (delq article gnus-newsgroup-processable)))
8474   (when (gnus-summary-goto-subject article)
8475     (gnus-summary-show-thread)
8476     (gnus-summary-goto-subject article)
8477     (gnus-summary-update-secondary-mark article)))
8478
8479 (defun gnus-summary-remove-process-mark (article)
8480   "Remove the process mark from ARTICLE and update the summary line."
8481   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8482   (when (gnus-summary-goto-subject article)
8483     (gnus-summary-show-thread)
8484     (gnus-summary-goto-subject article)
8485     (gnus-summary-update-secondary-mark article)))
8486
8487 (defun gnus-summary-set-saved-mark (article)
8488   "Set the process mark on ARTICLE and update the summary line."
8489   (push article gnus-newsgroup-saved)
8490   (when (gnus-summary-goto-subject article)
8491     (gnus-summary-update-secondary-mark article)))
8492
8493 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8494   "Mark N articles as read forwards.
8495 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8496 The difference between N and the actual number of articles marked is
8497 returned.
8498 Iff NO-EXPIRE, auto-expiry will be inhibited."
8499   (interactive "p")
8500   (gnus-summary-show-thread)
8501   (let ((backward (< n 0))
8502         (gnus-summary-goto-unread
8503          (and gnus-summary-goto-unread
8504               (not (eq gnus-summary-goto-unread 'never))
8505               (not (memq mark (list gnus-unread-mark
8506                                     gnus-ticked-mark gnus-dormant-mark)))))
8507         (n (abs n))
8508         (mark (or mark gnus-del-mark)))
8509     (while (and (> n 0)
8510                 (gnus-summary-mark-article nil mark no-expire)
8511                 (zerop (gnus-summary-next-subject
8512                         (if backward -1 1)
8513                         (and gnus-summary-goto-unread
8514                              (not (eq gnus-summary-goto-unread 'never)))
8515                         t)))
8516       (setq n (1- n)))
8517     (when (/= 0 n)
8518       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8519     (gnus-summary-recenter)
8520     (gnus-summary-position-point)
8521     (gnus-set-mode-line 'summary)
8522     n))
8523
8524 (defun gnus-summary-mark-article-as-read (mark)
8525   "Mark the current article quickly as read with MARK."
8526   (let ((article (gnus-summary-article-number)))
8527     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8528     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8529     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8530     (push (cons article mark) gnus-newsgroup-reads)
8531     ;; Possibly remove from cache, if that is used.
8532     (when gnus-use-cache
8533       (gnus-cache-enter-remove-article article))
8534     ;; Allow the backend to change the mark.
8535     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8536     ;; Check for auto-expiry.
8537     (when (and gnus-newsgroup-auto-expire
8538                (memq mark gnus-auto-expirable-marks))
8539       (setq mark gnus-expirable-mark)
8540       ;; Let the backend know about the mark change.
8541       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8542       (push article gnus-newsgroup-expirable))
8543     ;; Set the mark in the buffer.
8544     (gnus-summary-update-mark mark 'unread)
8545     t))
8546
8547 (defun gnus-summary-mark-article-as-unread (mark)
8548   "Mark the current article quickly as unread with MARK."
8549   (let* ((article (gnus-summary-article-number))
8550          (old-mark (gnus-summary-article-mark article)))
8551     ;; Allow the backend to change the mark.
8552     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8553     (if (eq mark old-mark)
8554         t
8555       (if (<= article 0)
8556           (progn
8557             (gnus-error 1 "Can't mark negative article numbers")
8558             nil)
8559         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8560         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8561         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8562         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8563         (cond ((= mark gnus-ticked-mark)
8564                (push article gnus-newsgroup-marked))
8565               ((= mark gnus-dormant-mark)
8566                (push article gnus-newsgroup-dormant))
8567               (t
8568                (push article gnus-newsgroup-unreads)))
8569         (gnus-pull article gnus-newsgroup-reads)
8570
8571         ;; See whether the article is to be put in the cache.
8572         (and gnus-use-cache
8573              (vectorp (gnus-summary-article-header article))
8574              (save-excursion
8575                (gnus-cache-possibly-enter-article
8576                 gnus-newsgroup-name article
8577                 (= mark gnus-ticked-mark)
8578                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8579
8580         ;; Fix the mark.
8581         (gnus-summary-update-mark mark 'unread)
8582         t))))
8583
8584 (defun gnus-summary-mark-article (&optional article mark no-expire)
8585   "Mark ARTICLE with MARK.  MARK can be any character.
8586 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8587 `??' (dormant) and `?E' (expirable).
8588 If MARK is nil, then the default character `?r' is used.
8589 If ARTICLE is nil, then the article on the current line will be
8590 marked.
8591 Iff NO-EXPIRE, auto-expiry will be inhibited."
8592   ;; The mark might be a string.
8593   (when (stringp mark)
8594     (setq mark (aref mark 0)))
8595   ;; If no mark is given, then we check auto-expiring.
8596   (when (null mark)
8597     (setq mark gnus-del-mark))
8598   (when (and (not no-expire)
8599              gnus-newsgroup-auto-expire
8600              (memq mark gnus-auto-expirable-marks))
8601     (setq mark gnus-expirable-mark))
8602   (let ((article (or article (gnus-summary-article-number)))
8603         (old-mark (gnus-summary-article-mark article)))
8604     ;; Allow the backend to change the mark.
8605     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8606     (if (eq mark old-mark)
8607         t
8608       (unless article
8609         (error "No article on current line"))
8610       (if (not (if (or (= mark gnus-unread-mark)
8611                        (= mark gnus-ticked-mark)
8612                        (= mark gnus-dormant-mark))
8613                    (gnus-mark-article-as-unread article mark)
8614                  (gnus-mark-article-as-read article mark)))
8615           t
8616         ;; See whether the article is to be put in the cache.
8617         (and gnus-use-cache
8618              (not (= mark gnus-canceled-mark))
8619              (vectorp (gnus-summary-article-header article))
8620              (save-excursion
8621                (gnus-cache-possibly-enter-article
8622                 gnus-newsgroup-name article
8623                 (= mark gnus-ticked-mark)
8624                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8625
8626         (when (gnus-summary-goto-subject article nil t)
8627           (let ((buffer-read-only nil))
8628             (gnus-summary-show-thread)
8629             ;; Fix the mark.
8630             (gnus-summary-update-mark mark 'unread)
8631             t))))))
8632
8633 (defun gnus-summary-update-secondary-mark (article)
8634   "Update the secondary (read, process, cache) mark."
8635   (gnus-summary-update-mark
8636    (cond ((memq article gnus-newsgroup-processable)
8637           gnus-process-mark)
8638          ((memq article gnus-newsgroup-cached)
8639           gnus-cached-mark)
8640          ((memq article gnus-newsgroup-replied)
8641           gnus-replied-mark)
8642          ((memq article gnus-newsgroup-saved)
8643           gnus-saved-mark)
8644          (t gnus-no-mark))
8645    'replied)
8646   (when (gnus-visual-p 'summary-highlight 'highlight)
8647     (gnus-run-hooks 'gnus-summary-update-hook))
8648   t)
8649
8650 (defun gnus-summary-update-mark (mark type)
8651   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8652         (buffer-read-only nil))
8653     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8654     (when forward
8655       (when (looking-at "\r")
8656         (incf forward))
8657       (when (<= (+ forward (point)) (point-max))
8658         ;; Go to the right position on the line.
8659         (goto-char (+ forward (point)))
8660         ;; Replace the old mark with the new mark.
8661         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8662         ;; Optionally update the marks by some user rule.
8663         (when (eq type 'unread)
8664           (gnus-data-set-mark
8665            (gnus-data-find (gnus-summary-article-number)) mark)
8666           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8667
8668 (defun gnus-mark-article-as-read (article &optional mark)
8669   "Enter ARTICLE in the pertinent lists and remove it from others."
8670   ;; Make the article expirable.
8671   (let ((mark (or mark gnus-del-mark)))
8672     (if (= mark gnus-expirable-mark)
8673         (push article gnus-newsgroup-expirable)
8674       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8675     ;; Remove from unread and marked lists.
8676     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8677     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8678     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8679     (push (cons article mark) gnus-newsgroup-reads)
8680     ;; Possibly remove from cache, if that is used.
8681     (when gnus-use-cache
8682       (gnus-cache-enter-remove-article article))
8683     t))
8684
8685 (defun gnus-mark-article-as-unread (article &optional mark)
8686   "Enter ARTICLE in the pertinent lists and remove it from others."
8687   (let ((mark (or mark gnus-ticked-mark)))
8688     (if (<= article 0)
8689         (progn
8690           (gnus-error 1 "Can't mark negative article numbers")
8691           nil)
8692       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8693             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8694             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8695             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8696
8697       ;; Unsuppress duplicates?
8698       (when gnus-suppress-duplicates
8699         (gnus-dup-unsuppress-article article))
8700
8701       (cond ((= mark gnus-ticked-mark)
8702              (push article gnus-newsgroup-marked))
8703             ((= mark gnus-dormant-mark)
8704              (push article gnus-newsgroup-dormant))
8705             (t
8706              (push article gnus-newsgroup-unreads)))
8707       (gnus-pull article gnus-newsgroup-reads)
8708       t)))
8709
8710 (defalias 'gnus-summary-mark-as-unread-forward
8711   'gnus-summary-tick-article-forward)
8712 (make-obsolete 'gnus-summary-mark-as-unread-forward
8713                'gnus-summary-tick-article-forward)
8714 (defun gnus-summary-tick-article-forward (n)
8715   "Tick N articles forwards.
8716 If N is negative, tick backwards instead.
8717 The difference between N and the number of articles ticked is returned."
8718   (interactive "p")
8719   (gnus-summary-mark-forward n gnus-ticked-mark))
8720
8721 (defalias 'gnus-summary-mark-as-unread-backward
8722   'gnus-summary-tick-article-backward)
8723 (make-obsolete 'gnus-summary-mark-as-unread-backward
8724                'gnus-summary-tick-article-backward)
8725 (defun gnus-summary-tick-article-backward (n)
8726   "Tick N articles backwards.
8727 The difference between N and the number of articles ticked is returned."
8728   (interactive "p")
8729   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8730
8731 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8732 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8733 (defun gnus-summary-tick-article (&optional article clear-mark)
8734   "Mark current article as unread.
8735 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8736 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8737   (interactive)
8738   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8739                                        gnus-ticked-mark)))
8740
8741 (defun gnus-summary-mark-as-read-forward (n)
8742   "Mark N articles as read forwards.
8743 If N is negative, mark backwards instead.
8744 The difference between N and the actual number of articles marked is
8745 returned."
8746   (interactive "p")
8747   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8748
8749 (defun gnus-summary-mark-as-read-backward (n)
8750   "Mark the N articles as read backwards.
8751 The difference between N and the actual number of articles marked is
8752 returned."
8753   (interactive "p")
8754   (gnus-summary-mark-forward
8755    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8756
8757 (defun gnus-summary-mark-as-read (&optional article mark)
8758   "Mark current article as read.
8759 ARTICLE specifies the article to be marked as read.
8760 MARK specifies a string to be inserted at the beginning of the line."
8761   (gnus-summary-mark-article article mark))
8762
8763 (defun gnus-summary-clear-mark-forward (n)
8764   "Clear marks from N articles forward.
8765 If N is negative, clear backward instead.
8766 The difference between N and the number of marks cleared is returned."
8767   (interactive "p")
8768   (gnus-summary-mark-forward n gnus-unread-mark))
8769
8770 (defun gnus-summary-clear-mark-backward (n)
8771   "Clear marks from N articles backward.
8772 The difference between N and the number of marks cleared is returned."
8773   (interactive "p")
8774   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8775
8776 (defun gnus-summary-mark-unread-as-read ()
8777   "Intended to be used by `gnus-summary-mark-article-hook'."
8778   (when (memq gnus-current-article gnus-newsgroup-unreads)
8779     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8780
8781 (defun gnus-summary-mark-read-and-unread-as-read ()
8782   "Intended to be used by `gnus-summary-mark-article-hook'."
8783   (let ((mark (gnus-summary-article-mark)))
8784     (when (or (gnus-unread-mark-p mark)
8785               (gnus-read-mark-p mark))
8786       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8787
8788 (defun gnus-summary-mark-unread-as-ticked ()
8789    "Intended to be used by `gnus-summary-mark-article-hook'."
8790   (when (memq gnus-current-article gnus-newsgroup-unreads)
8791     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8792
8793 (defun gnus-summary-mark-region-as-read (point mark all)
8794   "Mark all unread articles between point and mark as read.
8795 If given a prefix, mark all articles between point and mark as read,
8796 even ticked and dormant ones."
8797   (interactive "r\nP")
8798   (save-excursion
8799     (let (article)
8800       (goto-char point)
8801       (beginning-of-line)
8802       (while (and
8803               (< (point) mark)
8804               (progn
8805                 (when (or all
8806                           (memq (setq article (gnus-summary-article-number))
8807                                 gnus-newsgroup-unreads))
8808                   (gnus-summary-mark-article article gnus-del-mark))
8809                 t)
8810               (gnus-summary-find-next))))))
8811
8812 (defun gnus-summary-mark-below (score mark)
8813   "Mark articles with score less than SCORE with MARK."
8814   (interactive "P\ncMark: ")
8815   (setq score (if score
8816                   (prefix-numeric-value score)
8817                 (or gnus-summary-default-score 0)))
8818   (save-excursion
8819     (set-buffer gnus-summary-buffer)
8820     (goto-char (point-min))
8821     (while
8822         (progn
8823           (and (< (gnus-summary-article-score) score)
8824                (gnus-summary-mark-article nil mark))
8825           (gnus-summary-find-next)))))
8826
8827 (defun gnus-summary-kill-below (&optional score)
8828   "Mark articles with score below SCORE as read."
8829   (interactive "P")
8830   (gnus-summary-mark-below score gnus-killed-mark))
8831
8832 (defun gnus-summary-clear-above (&optional score)
8833   "Clear all marks from articles with score above SCORE."
8834   (interactive "P")
8835   (gnus-summary-mark-above score gnus-unread-mark))
8836
8837 (defun gnus-summary-tick-above (&optional score)
8838   "Tick all articles with score above SCORE."
8839   (interactive "P")
8840   (gnus-summary-mark-above score gnus-ticked-mark))
8841
8842 (defun gnus-summary-mark-above (score mark)
8843   "Mark articles with score over SCORE with MARK."
8844   (interactive "P\ncMark: ")
8845   (setq score (if score
8846                   (prefix-numeric-value score)
8847                 (or gnus-summary-default-score 0)))
8848   (save-excursion
8849     (set-buffer gnus-summary-buffer)
8850     (goto-char (point-min))
8851     (while (and (progn
8852                   (when (> (gnus-summary-article-score) score)
8853                     (gnus-summary-mark-article nil mark))
8854                   t)
8855                 (gnus-summary-find-next)))))
8856
8857 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8858 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8859 (defun gnus-summary-limit-include-expunged (&optional no-error)
8860   "Display all the hidden articles that were expunged for low scores."
8861   (interactive)
8862   (let ((buffer-read-only nil))
8863     (let ((scored gnus-newsgroup-scored)
8864           headers h)
8865       (while scored
8866         (unless (gnus-number-to-header (caar scored))
8867           (and (setq h (gnus-summary-article-header (caar scored)))
8868                (< (cdar scored) gnus-summary-expunge-below)
8869                (push h headers)))
8870         (setq scored (cdr scored)))
8871       (if (not headers)
8872           (when (not no-error)
8873             (error "No expunged articles hidden"))
8874         (goto-char (point-min))
8875         (gnus-summary-prepare-unthreaded (nreverse headers))
8876         (goto-char (point-min))
8877         (gnus-summary-position-point)
8878         t))))
8879
8880 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8881   "Mark all unread articles in this newsgroup as read.
8882 If prefix argument ALL is non-nil, ticked and dormant articles will
8883 also be marked as read.
8884 If QUIETLY is non-nil, no questions will be asked.
8885 If TO-HERE is non-nil, it should be a point in the buffer.  All
8886 articles before this point will be marked as read.
8887 Note that this function will only catch up the unread article
8888 in the current summary buffer limitation.
8889 The number of articles marked as read is returned."
8890   (interactive "P")
8891   (prog1
8892       (save-excursion
8893         (when (or quietly
8894                   (not gnus-interactive-catchup) ;Without confirmation?
8895                   gnus-expert-user
8896                   (gnus-y-or-n-p
8897                    (if all
8898                        "Mark absolutely all articles as read? "
8899                      "Mark all unread articles as read? ")))
8900           (if (and not-mark
8901                    (not gnus-newsgroup-adaptive)
8902                    (not gnus-newsgroup-auto-expire)
8903                    (not gnus-suppress-duplicates)
8904                    (or (not gnus-use-cache)
8905                        (eq gnus-use-cache 'passive)))
8906               (progn
8907                 (when all
8908                   (setq gnus-newsgroup-marked nil
8909                         gnus-newsgroup-dormant nil))
8910                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8911             ;; We actually mark all articles as canceled, which we
8912             ;; have to do when using auto-expiry or adaptive scoring.
8913             (gnus-summary-show-all-threads)
8914             (when (gnus-summary-first-subject (not all) t)
8915               (while (and
8916                       (if to-here (< (point) to-here) t)
8917                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8918                       (gnus-summary-find-next (not all) nil nil t))))
8919             (gnus-set-mode-line 'summary))
8920           t))
8921     (gnus-summary-position-point)))
8922
8923 (defun gnus-summary-catchup-to-here (&optional all)
8924   "Mark all unticked articles before the current one as read.
8925 If ALL is non-nil, also mark ticked and dormant articles as read."
8926   (interactive "P")
8927   (save-excursion
8928     (gnus-save-hidden-threads
8929       (let ((beg (point)))
8930         ;; We check that there are unread articles.
8931         (when (or all (gnus-summary-find-prev))
8932           (gnus-summary-catchup all t beg)))))
8933   (gnus-summary-position-point))
8934
8935 (defun gnus-summary-catchup-all (&optional quietly)
8936   "Mark all articles in this newsgroup as read."
8937   (interactive "P")
8938   (gnus-summary-catchup t quietly))
8939
8940 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8941   "Mark all unread articles in this group as read, then exit.
8942 If prefix argument ALL is non-nil, all articles are marked as read."
8943   (interactive "P")
8944   (when (gnus-summary-catchup all quietly nil 'fast)
8945     ;; Select next newsgroup or exit.
8946     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8947              (eq gnus-auto-select-next 'quietly))
8948         (gnus-summary-next-group nil)
8949       (gnus-summary-exit))))
8950
8951 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8952   "Mark all articles in this newsgroup as read, and then exit."
8953   (interactive "P")
8954   (gnus-summary-catchup-and-exit t quietly))
8955
8956 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8957   "Mark all articles in this group as read and select the next group.
8958 If given a prefix, mark all articles, unread as well as ticked, as
8959 read."
8960   (interactive "P")
8961   (save-excursion
8962     (gnus-summary-catchup all))
8963   (gnus-summary-next-group))
8964
8965 ;;;
8966 ;;; with article
8967 ;;;
8968
8969 (defmacro gnus-with-article (article &rest forms)
8970   "Select ARTICLE and perform FORMS in the original article buffer.
8971 Then replace the article with the result."
8972   `(progn
8973      ;; We don't want the article to be marked as read.
8974      (let (gnus-mark-article-hook)
8975        (gnus-summary-select-article t t nil ,article))
8976      (set-buffer gnus-original-article-buffer)
8977      ,@forms
8978      (if (not (gnus-check-backend-function
8979                'request-replace-article (car gnus-article-current)))
8980          (gnus-message 5 "Read-only group; not replacing")
8981        (unless (gnus-request-replace-article
8982                 ,article (car gnus-article-current)
8983                 (current-buffer) t)
8984          (error "Couldn't replace article")))
8985      ;; The cache and backlog have to be flushed somewhat.
8986      (when gnus-keep-backlog
8987        (gnus-backlog-remove-article
8988         (car gnus-article-current) (cdr gnus-article-current)))
8989      (when gnus-use-cache
8990        (gnus-cache-update-article
8991         (car gnus-article-current) (cdr gnus-article-current)))))
8992
8993 (put 'gnus-with-article 'lisp-indent-function 1)
8994 (put 'gnus-with-article 'edebug-form-spec '(form body))
8995
8996 ;; Thread-based commands.
8997
8998 (defun gnus-summary-articles-in-thread (&optional article)
8999   "Return a list of all articles in the current thread.
9000 If ARTICLE is non-nil, return all articles in the thread that starts
9001 with that article."
9002   (let* ((article (or article (gnus-summary-article-number)))
9003          (data (gnus-data-find-list article))
9004          (top-level (gnus-data-level (car data)))
9005          (top-subject
9006           (cond ((null gnus-thread-operation-ignore-subject)
9007                  (gnus-simplify-subject-re
9008                   (mail-header-subject (gnus-data-header (car data)))))
9009                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9010                  (gnus-simplify-subject-fuzzy
9011                   (mail-header-subject (gnus-data-header (car data)))))
9012                 (t nil)))
9013          (end-point (save-excursion
9014                       (if (gnus-summary-go-to-next-thread)
9015                           (point) (point-max))))
9016          articles)
9017     (while (and data
9018                 (< (gnus-data-pos (car data)) end-point))
9019       (when (or (not top-subject)
9020                 (string= top-subject
9021                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9022                              (gnus-simplify-subject-fuzzy
9023                               (mail-header-subject
9024                                (gnus-data-header (car data))))
9025                            (gnus-simplify-subject-re
9026                             (mail-header-subject
9027                              (gnus-data-header (car data)))))))
9028         (push (gnus-data-number (car data)) articles))
9029       (unless (and (setq data (cdr data))
9030                    (> (gnus-data-level (car data)) top-level))
9031         (setq data nil)))
9032     ;; Return the list of articles.
9033     (nreverse articles)))
9034
9035 (defun gnus-summary-rethread-current ()
9036   "Rethread the thread the current article is part of."
9037   (interactive)
9038   (let* ((gnus-show-threads t)
9039          (article (gnus-summary-article-number))
9040          (id (mail-header-id (gnus-summary-article-header)))
9041          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9042     (unless id
9043       (error "No article on the current line"))
9044     (gnus-rebuild-thread id)
9045     (gnus-summary-goto-subject article)))
9046
9047 (defun gnus-summary-reparent-thread ()
9048   "Make the current article child of the marked (or previous) article.
9049
9050 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9051 is non-nil or the Subject: of both articles are the same."
9052   (interactive)
9053   (unless (not (gnus-group-read-only-p))
9054     (error "The current newsgroup does not support article editing"))
9055   (unless (<= (length gnus-newsgroup-processable) 1)
9056     (error "No more than one article may be marked"))
9057   (save-window-excursion
9058     (let ((gnus-article-buffer " *reparent*")
9059           (current-article (gnus-summary-article-number))
9060           ;; First grab the marked article, otherwise one line up.
9061           (parent-article (if (not (null gnus-newsgroup-processable))
9062                               (car gnus-newsgroup-processable)
9063                             (save-excursion
9064                               (if (eq (forward-line -1) 0)
9065                                   (gnus-summary-article-number)
9066                                 (error "Beginning of summary buffer"))))))
9067       (unless (not (eq current-article parent-article))
9068         (error "An article may not be self-referential"))
9069       (let ((message-id (mail-header-id
9070                          (gnus-summary-article-header parent-article))))
9071         (unless (and message-id (not (equal message-id "")))
9072           (error "No message-id in desired parent"))
9073         (gnus-with-article current-article
9074           (save-restriction
9075             (goto-char (point-min))
9076             (message-narrow-to-head)
9077             (if (re-search-forward "^References: " nil t)
9078                 (progn
9079                   (re-search-forward "^[^ \t]" nil t)
9080                   (forward-line -1)
9081                   (end-of-line)
9082                   (insert " " message-id))
9083               (insert "References: " message-id "\n"))))
9084         (set-buffer gnus-summary-buffer)
9085         (gnus-summary-unmark-all-processable)
9086         (gnus-summary-update-article current-article)
9087         (gnus-summary-rethread-current)
9088         (gnus-message 3 "Article %d is now the child of article %d"
9089                       current-article parent-article)))))
9090
9091 (defun gnus-summary-toggle-threads (&optional arg)
9092   "Toggle showing conversation threads.
9093 If ARG is positive number, turn showing conversation threads on."
9094   (interactive "P")
9095   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9096     (setq gnus-show-threads
9097           (if (null arg) (not gnus-show-threads)
9098             (> (prefix-numeric-value arg) 0)))
9099     (gnus-summary-prepare)
9100     (gnus-summary-goto-subject current)
9101     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9102     (gnus-summary-position-point)))
9103
9104 (defun gnus-summary-show-all-threads ()
9105   "Show all threads."
9106   (interactive)
9107   (save-excursion
9108     (let ((buffer-read-only nil))
9109       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9110   (gnus-summary-position-point))
9111
9112 (defun gnus-summary-show-thread ()
9113   "Show thread subtrees.
9114 Returns nil if no thread was there to be shown."
9115   (interactive)
9116   (let ((buffer-read-only nil)
9117         (orig (point))
9118         ;; first goto end then to beg, to have point at beg after let
9119         (end (progn (end-of-line) (point)))
9120         (beg (progn (beginning-of-line) (point))))
9121     (prog1
9122         ;; Any hidden lines here?
9123         (search-forward "\r" end t)
9124       (subst-char-in-region beg end ?\^M ?\n t)
9125       (goto-char orig)
9126       (gnus-summary-position-point))))
9127
9128 (defun gnus-summary-hide-all-threads ()
9129   "Hide all thread subtrees."
9130   (interactive)
9131   (save-excursion
9132     (goto-char (point-min))
9133     (gnus-summary-hide-thread)
9134     (while (zerop (gnus-summary-next-thread 1 t))
9135       (gnus-summary-hide-thread)))
9136   (gnus-summary-position-point))
9137
9138 (defun gnus-summary-hide-thread ()
9139   "Hide thread subtrees.
9140 Returns nil if no threads were there to be hidden."
9141   (interactive)
9142   (let ((buffer-read-only nil)
9143         (start (point))
9144         (article (gnus-summary-article-number)))
9145     (goto-char start)
9146     ;; Go forward until either the buffer ends or the subthread
9147     ;; ends.
9148     (when (and (not (eobp))
9149                (or (zerop (gnus-summary-next-thread 1 t))
9150                    (goto-char (point-max))))
9151       (prog1
9152           (if (and (> (point) start)
9153                    (search-backward "\n" start t))
9154               (progn
9155                 (subst-char-in-region start (point) ?\n ?\^M)
9156                 (gnus-summary-goto-subject article))
9157             (goto-char start)
9158             nil)))))
9159
9160 (defun gnus-summary-go-to-next-thread (&optional previous)
9161   "Go to the same level (or less) next thread.
9162 If PREVIOUS is non-nil, go to previous thread instead.
9163 Return the article number moved to, or nil if moving was impossible."
9164   (let ((level (gnus-summary-thread-level))
9165         (way (if previous -1 1))
9166         (beg (point)))
9167     (forward-line way)
9168     (while (and (not (eobp))
9169                 (< level (gnus-summary-thread-level)))
9170       (forward-line way))
9171     (if (eobp)
9172         (progn
9173           (goto-char beg)
9174           nil)
9175       (setq beg (point))
9176       (prog1
9177           (gnus-summary-article-number)
9178         (goto-char beg)))))
9179
9180 (defun gnus-summary-next-thread (n &optional silent)
9181   "Go to the same level next N'th thread.
9182 If N is negative, search backward instead.
9183 Returns the difference between N and the number of skips actually
9184 done.
9185
9186 If SILENT, don't output messages."
9187   (interactive "p")
9188   (let ((backward (< n 0))
9189         (n (abs n)))
9190     (while (and (> n 0)
9191                 (gnus-summary-go-to-next-thread backward))
9192       (decf n))
9193     (unless silent
9194       (gnus-summary-position-point))
9195     (when (and (not silent) (/= 0 n))
9196       (gnus-message 7 "No more threads"))
9197     n))
9198
9199 (defun gnus-summary-prev-thread (n)
9200   "Go to the same level previous N'th thread.
9201 Returns the difference between N and the number of skips actually
9202 done."
9203   (interactive "p")
9204   (gnus-summary-next-thread (- n)))
9205
9206 (defun gnus-summary-go-down-thread ()
9207   "Go down one level in the current thread."
9208   (let ((children (gnus-summary-article-children)))
9209     (when children
9210       (gnus-summary-goto-subject (car children)))))
9211
9212 (defun gnus-summary-go-up-thread ()
9213   "Go up one level in the current thread."
9214   (let ((parent (gnus-summary-article-parent)))
9215     (when parent
9216       (gnus-summary-goto-subject parent))))
9217
9218 (defun gnus-summary-down-thread (n)
9219   "Go down thread N steps.
9220 If N is negative, go up instead.
9221 Returns the difference between N and how many steps down that were
9222 taken."
9223   (interactive "p")
9224   (let ((up (< n 0))
9225         (n (abs n)))
9226     (while (and (> n 0)
9227                 (if up (gnus-summary-go-up-thread)
9228                   (gnus-summary-go-down-thread)))
9229       (setq n (1- n)))
9230     (gnus-summary-position-point)
9231     (when (/= 0 n)
9232       (gnus-message 7 "Can't go further"))
9233     n))
9234
9235 (defun gnus-summary-up-thread (n)
9236   "Go up thread N steps.
9237 If N is negative, go up instead.
9238 Returns the difference between N and how many steps down that were
9239 taken."
9240   (interactive "p")
9241   (gnus-summary-down-thread (- n)))
9242
9243 (defun gnus-summary-top-thread ()
9244   "Go to the top of the thread."
9245   (interactive)
9246   (while (gnus-summary-go-up-thread))
9247   (gnus-summary-article-number))
9248
9249 (defun gnus-summary-kill-thread (&optional unmark)
9250   "Mark articles under current thread as read.
9251 If the prefix argument is positive, remove any kinds of marks.
9252 If the prefix argument is negative, tick articles instead."
9253   (interactive "P")
9254   (when unmark
9255     (setq unmark (prefix-numeric-value unmark)))
9256   (let ((articles (gnus-summary-articles-in-thread)))
9257     (save-excursion
9258       ;; Expand the thread.
9259       (gnus-summary-show-thread)
9260       ;; Mark all the articles.
9261       (while articles
9262         (gnus-summary-goto-subject (car articles))
9263         (cond ((null unmark)
9264                (gnus-summary-mark-article-as-read gnus-killed-mark))
9265               ((> unmark 0)
9266                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9267               (t
9268                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9269         (setq articles (cdr articles))))
9270     ;; Hide killed subtrees.
9271     (and (null unmark)
9272          gnus-thread-hide-killed
9273          (gnus-summary-hide-thread))
9274     ;; If marked as read, go to next unread subject.
9275     (when (null unmark)
9276       ;; Go to next unread subject.
9277       (gnus-summary-next-subject 1 t)))
9278   (gnus-set-mode-line 'summary))
9279
9280 ;; Summary sorting commands
9281
9282 (defun gnus-summary-sort-by-number (&optional reverse)
9283   "Sort the summary buffer by article number.
9284 Argument REVERSE means reverse order."
9285   (interactive "P")
9286   (gnus-summary-sort 'number reverse))
9287
9288 (defun gnus-summary-sort-by-author (&optional reverse)
9289   "Sort the summary buffer by author name alphabetically.
9290 If `case-fold-search' is non-nil, case of letters is ignored.
9291 Argument REVERSE means reverse order."
9292   (interactive "P")
9293   (gnus-summary-sort 'author reverse))
9294
9295 (defun gnus-summary-sort-by-subject (&optional reverse)
9296   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9297 If `case-fold-search' is non-nil, case of letters is ignored.
9298 Argument REVERSE means reverse order."
9299   (interactive "P")
9300   (gnus-summary-sort 'subject reverse))
9301
9302 (defun gnus-summary-sort-by-date (&optional reverse)
9303   "Sort the summary buffer by date.
9304 Argument REVERSE means reverse order."
9305   (interactive "P")
9306   (gnus-summary-sort 'date reverse))
9307
9308 (defun gnus-summary-sort-by-score (&optional reverse)
9309   "Sort the summary buffer by score.
9310 Argument REVERSE means reverse order."
9311   (interactive "P")
9312   (gnus-summary-sort 'score reverse))
9313
9314 (defun gnus-summary-sort-by-lines (&optional reverse)
9315   "Sort the summary buffer by the number of lines.
9316 Argument REVERSE means reverse order."
9317   (interactive "P")
9318   (gnus-summary-sort 'lines reverse))
9319
9320 (defun gnus-summary-sort-by-chars (&optional reverse)
9321   "Sort the summary buffer by article length.
9322 Argument REVERSE means reverse order."
9323   (interactive "P")
9324   (gnus-summary-sort 'chars reverse))
9325
9326 (defun gnus-summary-sort (predicate reverse)
9327   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9328   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9329          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9330          (gnus-thread-sort-functions
9331           (if (not reverse)
9332               thread
9333             `(lambda (t1 t2)
9334                (,thread t2 t1))))
9335          (gnus-sort-gathered-threads-function
9336           gnus-thread-sort-functions)
9337          (gnus-article-sort-functions
9338           (if (not reverse)
9339               article
9340             `(lambda (t1 t2)
9341                (,article t2 t1))))
9342          (buffer-read-only)
9343          (gnus-summary-prepare-hook nil))
9344     ;; We do the sorting by regenerating the threads.
9345     (gnus-summary-prepare)
9346     ;; Hide subthreads if needed.
9347     (when (and gnus-show-threads gnus-thread-hide-subtree)
9348       (gnus-summary-hide-all-threads))))
9349
9350 ;; Summary saving commands.
9351
9352 (defun gnus-summary-save-article (&optional n not-saved)
9353   "Save the current article using the default saver function.
9354 If N is a positive number, save the N next articles.
9355 If N is a negative number, save the N previous articles.
9356 If N is nil and any articles have been marked with the process mark,
9357 save those articles instead.
9358 The variable `gnus-default-article-saver' specifies the saver function."
9359   (interactive "P")
9360   (let* ((articles (gnus-summary-work-articles n))
9361          (save-buffer (save-excursion
9362                         (nnheader-set-temp-buffer " *Gnus Save*")))
9363          (num (length articles))
9364          header file)
9365     (dolist (article articles)
9366       (setq header (gnus-summary-article-header article))
9367       (if (not (vectorp header))
9368           ;; This is a pseudo-article.
9369           (if (assq 'name header)
9370               (gnus-copy-file (cdr (assq 'name header)))
9371             (gnus-message 1 "Article %d is unsaveable" article))
9372         ;; This is a real article.
9373         (save-window-excursion
9374           (gnus-summary-select-article t nil nil article))
9375         (save-excursion
9376           (set-buffer save-buffer)
9377           (erase-buffer)
9378           (insert-buffer-substring gnus-original-article-buffer))
9379         (setq file (gnus-article-save save-buffer file num))
9380         (gnus-summary-remove-process-mark article)
9381         (unless not-saved
9382           (gnus-summary-set-saved-mark article))))
9383     (gnus-kill-buffer save-buffer)
9384     (gnus-summary-position-point)
9385     (gnus-set-mode-line 'summary)
9386     n))
9387
9388 (defun gnus-summary-pipe-output (&optional arg)
9389   "Pipe the current article to a subprocess.
9390 If N is a positive number, pipe the N next articles.
9391 If N is a negative number, pipe the N previous articles.
9392 If N is nil and any articles have been marked with the process mark,
9393 pipe those articles instead."
9394   (interactive "P")
9395   (require 'gnus-art)
9396   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9397     (gnus-summary-save-article arg t))
9398   (gnus-configure-windows 'pipe))
9399
9400 (defun gnus-summary-save-article-mail (&optional arg)
9401   "Append the current article to an mail file.
9402 If N is a positive number, save the N next articles.
9403 If N is a negative number, save the N previous articles.
9404 If N is nil and any articles have been marked with the process mark,
9405 save those articles instead."
9406   (interactive "P")
9407   (require 'gnus-art)
9408   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9409     (gnus-summary-save-article arg)))
9410
9411 (defun gnus-summary-save-article-rmail (&optional arg)
9412   "Append the current article to an rmail file.
9413 If N is a positive number, save the N next articles.
9414 If N is a negative number, save the N previous articles.
9415 If N is nil and any articles have been marked with the process mark,
9416 save those articles instead."
9417   (interactive "P")
9418   (require 'gnus-art)
9419   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9420     (gnus-summary-save-article arg)))
9421
9422 (defun gnus-summary-save-article-file (&optional arg)
9423   "Append the current article to a file.
9424 If N is a positive number, save the N next articles.
9425 If N is a negative number, save the N previous articles.
9426 If N is nil and any articles have been marked with the process mark,
9427 save those articles instead."
9428   (interactive "P")
9429   (require 'gnus-art)
9430   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9431     (gnus-summary-save-article arg)))
9432
9433 (defun gnus-summary-write-article-file (&optional arg)
9434   "Write the current article to a file, deleting the previous file.
9435 If N is a positive number, save the N next articles.
9436 If N is a negative number, save the N previous articles.
9437 If N is nil and any articles have been marked with the process mark,
9438 save those articles instead."
9439   (interactive "P")
9440   (require 'gnus-art)
9441   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9442     (gnus-summary-save-article arg)))
9443
9444 (defun gnus-summary-save-article-body-file (&optional arg)
9445   "Append the current article body to a file.
9446 If N is a positive number, save the N next articles.
9447 If N is a negative number, save the N previous articles.
9448 If N is nil and any articles have been marked with the process mark,
9449 save those articles instead."
9450   (interactive "P")
9451   (require 'gnus-art)
9452   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9453     (gnus-summary-save-article arg)))
9454
9455 (defun gnus-summary-pipe-message (program)
9456   "Pipe the current article through PROGRAM."
9457   (interactive "sProgram: ")
9458   (gnus-summary-select-article)
9459   (let ((mail-header-separator ""))
9460     (gnus-eval-in-buffer-window gnus-article-buffer
9461       (save-restriction
9462         (widen)
9463         (let ((start (window-start))
9464               buffer-read-only)
9465           (message-pipe-buffer-body program)
9466           (set-window-start (get-buffer-window (current-buffer)) start))))))
9467
9468 (defun gnus-get-split-value (methods)
9469   "Return a value based on the split METHODS."
9470   (let (split-name method result match)
9471     (when methods
9472       (save-excursion
9473         (set-buffer gnus-original-article-buffer)
9474         (save-restriction
9475           (nnheader-narrow-to-headers)
9476           (while (and methods (not split-name))
9477             (goto-char (point-min))
9478             (setq method (pop methods))
9479             (setq match (car method))
9480             (when (cond
9481                    ((stringp match)
9482                     ;; Regular expression.
9483                     (ignore-errors
9484                       (re-search-forward match nil t)))
9485                    ((gnus-functionp match)
9486                     ;; Function.
9487                     (save-restriction
9488                       (widen)
9489                       (setq result (funcall match gnus-newsgroup-name))))
9490                    ((consp match)
9491                     ;; Form.
9492                     (save-restriction
9493                       (widen)
9494                       (setq result (eval match)))))
9495               (setq split-name (cdr method))
9496               (cond ((stringp result)
9497                      (push (expand-file-name
9498                             result gnus-article-save-directory)
9499                            split-name))
9500                     ((consp result)
9501                      (setq split-name (append result split-name)))))))))
9502     (nreverse split-name)))
9503
9504 (defun gnus-valid-move-group-p (group)
9505   (and (boundp group)
9506        (symbol-name group)
9507        (symbol-value group)
9508        (gnus-get-function (gnus-find-method-for-group
9509                            (symbol-name group)) 'request-accept-article t)))
9510
9511 (defun gnus-read-move-group-name (prompt default articles prefix)
9512   "Read a group name."
9513   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9514          (minibuffer-confirm-incomplete nil) ; XEmacs
9515          (prom
9516           (format "%s %s to:"
9517                   prompt
9518                   (if (> (length articles) 1)
9519                       (format "these %d articles" (length articles))
9520                     "this article")))
9521          (to-newsgroup
9522           (cond
9523            ((null split-name)
9524             (gnus-completing-read default prom
9525                                   gnus-active-hashtb
9526                                   'gnus-valid-move-group-p
9527                                   nil prefix
9528                                   'gnus-group-history))
9529            ((= 1 (length split-name))
9530             (gnus-completing-read (car split-name) prom
9531                                   gnus-active-hashtb
9532                                   'gnus-valid-move-group-p
9533                                   nil nil
9534                                   'gnus-group-history))
9535            (t
9536             (gnus-completing-read nil prom
9537                                   (mapcar (lambda (el) (list el))
9538                                           (nreverse split-name))
9539                                   nil nil nil
9540                                   'gnus-group-history))))
9541          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9542     (when to-newsgroup
9543       (if (or (string= to-newsgroup "")
9544               (string= to-newsgroup prefix))
9545           (setq to-newsgroup default))
9546       (unless to-newsgroup
9547         (error "No group name entered"))
9548       (or (gnus-active to-newsgroup)
9549           (gnus-activate-group to-newsgroup nil nil to-method)
9550           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9551                                      to-newsgroup))
9552               (or (and (gnus-request-create-group to-newsgroup to-method)
9553                        (gnus-activate-group
9554                         to-newsgroup nil nil to-method)
9555                        (gnus-subscribe-group to-newsgroup))
9556                   (error "Couldn't create group %s" to-newsgroup)))
9557           (error "No such group: %s" to-newsgroup)))
9558     to-newsgroup))
9559
9560 (defun gnus-summary-save-parts (type dir n &optional reverse)
9561   "Save parts matching TYPE to DIR.
9562 If REVERSE, save parts that do not match TYPE."
9563   (interactive
9564    (list (read-string "Save parts of type: "
9565                       (or (car gnus-summary-save-parts-type-history)
9566                           gnus-summary-save-parts-default-mime)
9567                       'gnus-summary-save-parts-type-history)
9568          (setq gnus-summary-save-parts-last-directory
9569                (read-file-name "Save to directory: "
9570                                gnus-summary-save-parts-last-directory
9571                                nil t))
9572          current-prefix-arg))
9573   (gnus-summary-iterate n
9574     (let ((gnus-display-mime-function nil)
9575           (gnus-inhibit-treatment t))
9576       (gnus-summary-select-article))
9577     (save-excursion
9578       (set-buffer gnus-article-buffer)
9579       (let ((handles (or gnus-article-mime-handles
9580                          (mm-dissect-buffer) (mm-uu-dissect))))
9581         (when handles
9582           (gnus-summary-save-parts-1 type dir handles reverse)
9583           (unless gnus-article-mime-handles ;; Don't destroy this case.
9584             (mm-destroy-parts handles)))))))
9585
9586 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9587   (if (stringp (car handle))
9588       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9589               (cdr handle))
9590     (when (if reverse
9591               (not (string-match type (mm-handle-media-type handle)))
9592             (string-match type (mm-handle-media-type handle)))
9593       (let ((file (expand-file-name
9594                    (file-name-nondirectory
9595                     (or
9596                      (mail-content-type-get
9597                       (mm-handle-disposition handle) 'filename)
9598                      (concat gnus-newsgroup-name
9599                              "." (number-to-string
9600                                   (cdr gnus-article-current)))))
9601                    dir)))
9602         (unless (file-exists-p file)
9603           (mm-save-part-to-file handle file))))))
9604
9605 ;; Summary extract commands
9606
9607 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9608   (let ((buffer-read-only nil)
9609         (article (gnus-summary-article-number))
9610         after-article b e)
9611     (unless (gnus-summary-goto-subject article)
9612       (error "No such article: %d" article))
9613     (gnus-summary-position-point)
9614     ;; If all commands are to be bunched up on one line, we collect
9615     ;; them here.
9616     (unless gnus-view-pseudos-separately
9617       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9618             files action)
9619         (while ps
9620           (setq action (cdr (assq 'action (car ps))))
9621           (setq files (list (cdr (assq 'name (car ps)))))
9622           (while (and ps (cdr ps)
9623                       (string= (or action "1")
9624                                (or (cdr (assq 'action (cadr ps))) "2")))
9625             (push (cdr (assq 'name (cadr ps))) files)
9626             (setcdr ps (cddr ps)))
9627           (when files
9628             (when (not (string-match "%s" action))
9629               (push " " files))
9630             (push " " files)
9631             (when (assq 'execute (car ps))
9632               (setcdr (assq 'execute (car ps))
9633                       (funcall (if (string-match "%s" action)
9634                                    'format 'concat)
9635                                action
9636                                (mapconcat
9637                                 (lambda (f)
9638                                   (if (equal f " ")
9639                                       f
9640                                     (mm-quote-arg f)))
9641                                 files " ")))))
9642           (setq ps (cdr ps)))))
9643     (if (and gnus-view-pseudos (not not-view))
9644         (while pslist
9645           (when (assq 'execute (car pslist))
9646             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9647                                   (eq gnus-view-pseudos 'not-confirm)))
9648           (setq pslist (cdr pslist)))
9649       (save-excursion
9650         (while pslist
9651           (setq after-article (or (cdr (assq 'article (car pslist)))
9652                                   (gnus-summary-article-number)))
9653           (gnus-summary-goto-subject after-article)
9654           (forward-line 1)
9655           (setq b (point))
9656           (insert "    " (file-name-nondirectory
9657                           (cdr (assq 'name (car pslist))))
9658                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9659           (setq e (point))
9660           (forward-line -1)             ; back to `b'
9661           (gnus-add-text-properties
9662            b (1- e) (list 'gnus-number gnus-reffed-article-number
9663                           gnus-mouse-face-prop gnus-mouse-face))
9664           (gnus-data-enter
9665            after-article gnus-reffed-article-number
9666            gnus-unread-mark b (car pslist) 0 (- e b))
9667           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9668           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9669           (setq pslist (cdr pslist)))))))
9670
9671 (defun gnus-pseudos< (p1 p2)
9672   (let ((c1 (cdr (assq 'action p1)))
9673         (c2 (cdr (assq 'action p2))))
9674     (and c1 c2 (string< c1 c2))))
9675
9676 (defun gnus-request-pseudo-article (props)
9677   (cond ((assq 'execute props)
9678          (gnus-execute-command (cdr (assq 'execute props)))))
9679   (let ((gnus-current-article (gnus-summary-article-number)))
9680     (gnus-run-hooks 'gnus-mark-article-hook)))
9681
9682 (defun gnus-execute-command (command &optional automatic)
9683   (save-excursion
9684     (gnus-article-setup-buffer)
9685     (set-buffer gnus-article-buffer)
9686     (setq buffer-read-only nil)
9687     (let ((command (if automatic command
9688                      (read-string "Command: " (cons command 0)))))
9689       (erase-buffer)
9690       (insert "$ " command "\n\n")
9691       (if gnus-view-pseudo-asynchronously
9692           (start-process "gnus-execute" (current-buffer) shell-file-name
9693                          shell-command-switch command)
9694         (call-process shell-file-name nil t nil
9695                       shell-command-switch command)))))
9696
9697 ;; Summary kill commands.
9698
9699 (defun gnus-summary-edit-global-kill (article)
9700   "Edit the \"global\" kill file."
9701   (interactive (list (gnus-summary-article-number)))
9702   (gnus-group-edit-global-kill article))
9703
9704 (defun gnus-summary-edit-local-kill ()
9705   "Edit a local kill file applied to the current newsgroup."
9706   (interactive)
9707   (setq gnus-current-headers (gnus-summary-article-header))
9708   (gnus-group-edit-local-kill
9709    (gnus-summary-article-number) gnus-newsgroup-name))
9710
9711 ;;; Header reading.
9712
9713 (defun gnus-read-header (id &optional header)
9714   "Read the headers of article ID and enter them into the Gnus system."
9715   (let ((group gnus-newsgroup-name)
9716         (gnus-override-method
9717          (or
9718           gnus-override-method
9719           (and (gnus-news-group-p gnus-newsgroup-name)
9720                (car (gnus-refer-article-methods)))))
9721         where)
9722     ;; First we check to see whether the header in question is already
9723     ;; fetched.
9724     (if (stringp id)
9725         ;; This is a Message-ID.
9726         (setq header (or header (gnus-id-to-header id)))
9727       ;; This is an article number.
9728       (setq header (or header (gnus-summary-article-header id))))
9729     (if (and header
9730              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9731         ;; We have found the header.
9732         header
9733       ;; If this is a sparse article, we have to nix out its
9734       ;; previous entry in the thread hashtb.
9735       (when (and header
9736                  (gnus-summary-article-sparse-p (mail-header-number header)))
9737         (let* ((parent (gnus-parent-id (mail-header-references header)))
9738                (thread (and parent (gnus-id-to-thread parent))))
9739           (when thread
9740             (delq (assq header thread) thread))))
9741       ;; We have to really fetch the header to this article.
9742       (save-excursion
9743         (set-buffer nntp-server-buffer)
9744         (when (setq where (gnus-request-head id group))
9745           (nnheader-fold-continuation-lines)
9746           (goto-char (point-max))
9747           (insert ".\n")
9748           (goto-char (point-min))
9749           (insert "211 ")
9750           (princ (cond
9751                   ((numberp id) id)
9752                   ((cdr where) (cdr where))
9753                   (header (mail-header-number header))
9754                   (t gnus-reffed-article-number))
9755                  (current-buffer))
9756           (insert " Article retrieved.\n"))
9757         (if (or (not where)
9758                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9759             ()                          ; Malformed head.
9760           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9761             (when (and (stringp id)
9762                        (not (string= (gnus-group-real-name group)
9763                                      (car where))))
9764               ;; If we fetched by Message-ID and the article came
9765               ;; from a different group, we fudge some bogus article
9766               ;; numbers for this article.
9767               (mail-header-set-number header gnus-reffed-article-number))
9768             (save-excursion
9769               (set-buffer gnus-summary-buffer)
9770               (decf gnus-reffed-article-number)
9771               (gnus-remove-header (mail-header-number header))
9772               (push header gnus-newsgroup-headers)
9773               (setq gnus-current-headers header)
9774               (push (mail-header-number header) gnus-newsgroup-limit)))
9775           header)))))
9776
9777 (defun gnus-remove-header (number)
9778   "Remove header NUMBER from `gnus-newsgroup-headers'."
9779   (if (and gnus-newsgroup-headers
9780            (= number (mail-header-number (car gnus-newsgroup-headers))))
9781       (pop gnus-newsgroup-headers)
9782     (let ((headers gnus-newsgroup-headers))
9783       (while (and (cdr headers)
9784                   (not (= number (mail-header-number (cadr headers)))))
9785         (pop headers))
9786       (when (cdr headers)
9787         (setcdr headers (cddr headers))))))
9788
9789 ;;;
9790 ;;; summary highlights
9791 ;;;
9792
9793 (defun gnus-highlight-selected-summary ()
9794   "Highlight selected article in summary buffer."
9795   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9796   (when gnus-summary-selected-face
9797     (save-excursion
9798       (let* ((beg (progn (beginning-of-line) (point)))
9799              (end (progn (end-of-line) (point)))
9800              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9801              (from (if (get-text-property beg gnus-mouse-face-prop)
9802                        beg
9803                      (or (next-single-property-change
9804                           beg gnus-mouse-face-prop nil end)
9805                          beg)))
9806              (to
9807               (if (= from end)
9808                   (- from 2)
9809                 (or (next-single-property-change
9810                      from gnus-mouse-face-prop nil end)
9811                     end))))
9812         ;; If no mouse-face prop on line we will have to = from = end,
9813         ;; so we highlight the entire line instead.
9814         (when (= (+ to 2) from)
9815           (setq from beg)
9816           (setq to end))
9817         (if gnus-newsgroup-selected-overlay
9818             ;; Move old overlay.
9819             (gnus-move-overlay
9820              gnus-newsgroup-selected-overlay from to (current-buffer))
9821           ;; Create new overlay.
9822           (gnus-overlay-put
9823            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9824            'face gnus-summary-selected-face))))))
9825
9826 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9827 (defun gnus-summary-highlight-line ()
9828   "Highlight current line according to `gnus-summary-highlight'."
9829   (let* ((list gnus-summary-highlight)
9830          (p (point))
9831          (end (progn (end-of-line) (point)))
9832          ;; now find out where the line starts and leave point there.
9833          (beg (progn (beginning-of-line) (point)))
9834          (article (gnus-summary-article-number))
9835          (score (or (cdr (assq (or article gnus-current-article)
9836                                gnus-newsgroup-scored))
9837                     gnus-summary-default-score 0))
9838          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9839          (inhibit-read-only t))
9840     ;; Eval the cars of the lists until we find a match.
9841     (let ((default gnus-summary-default-score))
9842       (while (and list
9843                   (not (eval (caar list))))
9844         (setq list (cdr list))))
9845     (let ((face (cdar list)))
9846       (unless (eq face (get-text-property beg 'face))
9847         (gnus-put-text-property-excluding-characters-with-faces
9848          beg end 'face
9849          (setq face (if (boundp face) (symbol-value face) face)))
9850         (when gnus-summary-highlight-line-function
9851           (funcall gnus-summary-highlight-line-function article face))))
9852     (goto-char p)))
9853
9854 (defun gnus-update-read-articles (group unread &optional compute)
9855   "Update the list of read articles in GROUP."
9856   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9857          (entry (gnus-gethash group gnus-newsrc-hashtb))
9858          (info (nth 2 entry))
9859          (prev 1)
9860          (unread (sort (copy-sequence unread) '<))
9861          read)
9862     (if (or (not info) (not active))
9863         ;; There is no info on this group if it was, in fact,
9864         ;; killed.  Gnus stores no information on killed groups, so
9865         ;; there's nothing to be done.
9866         ;; One could store the information somewhere temporarily,
9867         ;; perhaps...  Hmmm...
9868         ()
9869       ;; Remove any negative articles numbers.
9870       (while (and unread (< (car unread) 0))
9871         (setq unread (cdr unread)))
9872       ;; Remove any expired article numbers
9873       (while (and unread (< (car unread) (car active)))
9874         (setq unread (cdr unread)))
9875       ;; Compute the ranges of read articles by looking at the list of
9876       ;; unread articles.
9877       (while unread
9878         (when (/= (car unread) prev)
9879           (push (if (= prev (1- (car unread))) prev
9880                   (cons prev (1- (car unread))))
9881                 read))
9882         (setq prev (1+ (car unread)))
9883         (setq unread (cdr unread)))
9884       (when (<= prev (cdr active))
9885         (push (cons prev (cdr active)) read))
9886       (setq read (if (> (length read) 1) (nreverse read) read))
9887       (if compute
9888           read
9889         (save-excursion
9890           (let (setmarkundo)
9891             ;; Propagate the read marks to the backend.
9892             (when (gnus-check-backend-function 'request-set-mark group)
9893               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9894                     (add (gnus-remove-from-range read (gnus-info-read info))))
9895                 (when (or add del)
9896                   (unless (gnus-check-group group)
9897                     (error "Can't open server for %s" group))
9898                   (gnus-request-set-mark
9899                    group (delq nil (list (if add (list add 'add '(read)))
9900                                          (if del (list del 'del '(read))))))
9901                   (setq setmarkundo
9902                         `(gnus-request-set-mark
9903                           ,group
9904                           ',(delq nil (list
9905                                        (if del (list del 'add '(read)))
9906                                        (if add (list add 'del '(read))))))))))
9907             (set-buffer gnus-group-buffer)
9908             (gnus-undo-register
9909               `(progn
9910                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9911                  (gnus-info-set-read ',info ',(gnus-info-read info))
9912                  (gnus-get-unread-articles-in-group ',info
9913                                                     (gnus-active ,group))
9914                  (gnus-group-update-group ,group t)
9915                  ,setmarkundo))))
9916         ;; Enter this list into the group info.
9917         (gnus-info-set-read info read)
9918         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9919         (gnus-get-unread-articles-in-group info (gnus-active group))
9920         t))))
9921
9922 (defun gnus-offer-save-summaries ()
9923   "Offer to save all active summary buffers."
9924   (save-excursion
9925     (let ((buflist (buffer-list))
9926           buffers bufname)
9927       ;; Go through all buffers and find all summaries.
9928       (while buflist
9929         (and (setq bufname (buffer-name (car buflist)))
9930              (string-match "Summary" bufname)
9931              (save-excursion
9932                (set-buffer bufname)
9933                ;; We check that this is, indeed, a summary buffer.
9934                (and (eq major-mode 'gnus-summary-mode)
9935                     ;; Also make sure this isn't bogus.
9936                     gnus-newsgroup-prepared
9937                     ;; Also make sure that this isn't a dead summary buffer.
9938                     (not gnus-dead-summary-mode)))
9939              (push bufname buffers))
9940         (setq buflist (cdr buflist)))
9941       ;; Go through all these summary buffers and offer to save them.
9942       (when buffers
9943         (map-y-or-n-p
9944          "Update summary buffer %s? "
9945          (lambda (buf)
9946            (switch-to-buffer buf)
9947            (gnus-summary-exit))
9948          buffers)))))
9949
9950 (defun gnus-summary-setup-default-charset ()
9951   "Setup newsgroup default charset."
9952   (if (equal gnus-newsgroup-name "nndraft:drafts")
9953       (setq gnus-newsgroup-charset nil)
9954     (let* ((ignored-charsets
9955             (or gnus-newsgroup-ephemeral-ignored-charsets
9956                 (append
9957                  (and gnus-newsgroup-name
9958                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
9959                  gnus-newsgroup-ignored-charsets))))
9960       (setq gnus-newsgroup-charset
9961             (or gnus-newsgroup-ephemeral-charset
9962                 (and gnus-newsgroup-name
9963                      (gnus-parameter-charset gnus-newsgroup-name))
9964                 gnus-default-charset))
9965       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9966            ignored-charsets))))
9967
9968 ;;;
9969 ;;; Mime Commands
9970 ;;;
9971
9972 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9973   "Display the current article buffer fully MIME-buttonized.
9974 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9975 treated as multipart/mixed."
9976   (interactive "P")
9977   (require 'gnus-art)
9978   (let ((gnus-unbuttonized-mime-types nil)
9979         (gnus-mime-display-multipart-as-mixed show-all-parts))
9980     (gnus-summary-show-article)))
9981
9982 (defun gnus-summary-repair-multipart (article)
9983   "Add a Content-Type header to a multipart article without one."
9984   (interactive (list (gnus-summary-article-number)))
9985   (gnus-with-article article
9986     (message-narrow-to-head)
9987     (message-remove-header "Mime-Version")
9988     (goto-char (point-max))
9989     (insert "Mime-Version: 1.0\n")
9990     (widen)
9991     (when (search-forward "\n--" nil t)
9992       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9993         (message-narrow-to-head)
9994         (message-remove-header "Content-Type")
9995         (goto-char (point-max))
9996         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9997                         separator))
9998         (widen))))
9999   (let (gnus-mark-article-hook)
10000     (gnus-summary-select-article t t nil article)))
10001
10002 (defun gnus-summary-toggle-display-buttonized ()
10003   "Toggle the buttonizing of the article buffer."
10004   (interactive)
10005   (require 'gnus-art)
10006   (if (setq gnus-inhibit-mime-unbuttonizing
10007             (not gnus-inhibit-mime-unbuttonizing))
10008       (let ((gnus-unbuttonized-mime-types nil))
10009         (gnus-summary-show-article))
10010     (gnus-summary-show-article)))
10011
10012 ;;;
10013 ;;; Generic summary marking commands
10014 ;;;
10015
10016 (defvar gnus-summary-marking-alist
10017   '((read gnus-del-mark "d")
10018     (unread gnus-unread-mark "u")
10019     (ticked gnus-ticked-mark "!")
10020     (dormant gnus-dormant-mark "?")
10021     (expirable gnus-expirable-mark "e"))
10022   "An alist of names/marks/keystrokes.")
10023
10024 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10025 (defvar gnus-summary-mark-map)
10026
10027 (defun gnus-summary-make-all-marking-commands ()
10028   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10029   (dolist (elem gnus-summary-marking-alist)
10030     (apply 'gnus-summary-make-marking-command elem)))
10031
10032 (defun gnus-summary-make-marking-command (name mark keystroke)
10033   (let ((map (make-sparse-keymap)))
10034     (define-key gnus-summary-generic-mark-map keystroke map)
10035     (dolist (lway `((next "next" next nil "n")
10036                     (next-unread "next unread" next t "N")
10037                     (prev "previous" prev nil "p")
10038                     (prev-unread "previous unread" prev t "P")
10039                     (nomove "" nil nil ,keystroke)))
10040       (let ((func (gnus-summary-make-marking-command-1
10041                    mark (car lway) lway name)))
10042         (setq func (eval func))
10043         (define-key map (nth 4 lway) func)))))
10044
10045 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10046   `(defun ,(intern
10047             (format "gnus-summary-put-mark-as-%s%s"
10048                     name (if (eq way 'nomove)
10049                              ""
10050                            (concat "-" (symbol-name way)))))
10051      (n)
10052      ,(format
10053        "Mark the current article as %s%s.
10054 If N, the prefix, then repeat N times.
10055 If N is negative, move in reverse order.
10056 The difference between N and the actual number of articles marked is
10057 returned."
10058        name (cadr lway))
10059      (interactive "p")
10060      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10061
10062 (defun gnus-summary-generic-mark (n mark move unread)
10063   "Mark N articles with MARK."
10064   (unless (eq major-mode 'gnus-summary-mode)
10065     (error "This command can only be used in the summary buffer"))
10066   (gnus-summary-show-thread)
10067   (let ((nummove
10068          (cond
10069           ((eq move 'next) 1)
10070           ((eq move 'prev) -1)
10071           (t 0))))
10072     (if (zerop nummove)
10073         (setq n 1)
10074       (when (< n 0)
10075         (setq n (abs n)
10076               nummove (* -1 nummove))))
10077     (while (and (> n 0)
10078                 (gnus-summary-mark-article nil mark)
10079                 (zerop (gnus-summary-next-subject nummove unread t)))
10080       (setq n (1- n)))
10081     (when (/= 0 n)
10082       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10083     (gnus-summary-recenter)
10084     (gnus-summary-position-point)
10085     (gnus-set-mode-line 'summary)
10086     n))
10087
10088 (defun gnus-summary-insert-articles (articles)
10089   (when (setq articles
10090               (gnus-set-difference articles
10091                                    (mapcar (lambda (h) (mail-header-number h))
10092                                            gnus-newsgroup-headers)))
10093     (setq gnus-newsgroup-headers 
10094           (merge 'list
10095                  gnus-newsgroup-headers
10096                  (gnus-fetch-headers articles)
10097                  'gnus-article-sort-by-number))
10098     ;; Suppress duplicates?
10099     (when gnus-suppress-duplicates
10100       (gnus-dup-suppress-articles))
10101     
10102     ;; We might want to build some more threads first.
10103     (when (and gnus-fetch-old-headers
10104                (eq gnus-headers-retrieved-by 'nov))
10105       (if (eq gnus-fetch-old-headers 'invisible)
10106         (gnus-build-all-threads)
10107         (gnus-build-old-threads)))
10108     ;; Let the Gnus agent mark articles as read.
10109     (when gnus-agent
10110       (gnus-agent-get-undownloaded-list))
10111     ;; Remove list identifiers from subject
10112     (when gnus-list-identifiers
10113       (gnus-summary-remove-list-identifiers))
10114     ;; First and last article in this newsgroup.
10115     (when gnus-newsgroup-headers
10116       (setq gnus-newsgroup-begin
10117             (mail-header-number (car gnus-newsgroup-headers))
10118             gnus-newsgroup-end
10119             (mail-header-number
10120              (gnus-last-element gnus-newsgroup-headers))))
10121     (when gnus-use-scoring
10122       (gnus-possibly-score-headers))))
10123
10124 (defun gnus-summary-insert-old-articles (&optional all)
10125   "Insert all old articles in this group.
10126 If ALL is non-nil, already read articles become readable.
10127 If ALL is a number, fetch this number of articles."
10128   (interactive "P")
10129   (prog1
10130       (let ((old (mapcar 'car gnus-newsgroup-data))
10131             (i (car gnus-newsgroup-active))
10132             older len)
10133         (while (<= i (cdr gnus-newsgroup-active))
10134           (or (memq i old) (push i older))
10135           (incf i))
10136         (setq len (length older))
10137         (cond 
10138          ((null older) nil)
10139          ((numberp all) 
10140           (if (< all len)
10141               (setq older (subseq older 0 all))))
10142          (all nil)
10143          (t
10144           (if (and (numberp gnus-large-newsgroup)
10145                    (> len gnus-large-newsgroup))
10146               (let ((input
10147                      (read-string
10148                       (format
10149                        "How many articles from %s (default %d): "
10150                        (gnus-limit-string 
10151                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10152                        len))))
10153                 (unless (string-match "^[ \t]*$" input) 
10154                   (setq all (string-to-number input))
10155                   (if (< all len)
10156                       (setq older (subseq older 0 all))))))))
10157         (if (not older)
10158             (message "No old news.")
10159           (gnus-summary-insert-articles older)
10160           (gnus-summary-limit (gnus-union older old))))
10161     (gnus-summary-position-point)))
10162
10163 (defun gnus-summary-insert-new-articles ()
10164   "Insert all new articles in this group."
10165   (interactive)
10166   (prog1
10167       (let ((old (mapcar 'car gnus-newsgroup-data))
10168             (old-active gnus-newsgroup-active)
10169             (nnmail-fetched-sources (list t))
10170             i new)
10171         (setq gnus-newsgroup-active 
10172               (gnus-activate-group gnus-newsgroup-name 'scan))
10173         (setq i (1+ (cdr old-active)))
10174         (while (<= i (cdr gnus-newsgroup-active))
10175           (push i new)
10176           (incf i))
10177         (if (not new)
10178             (message "No gnus is bad news.")
10179           (gnus-summary-insert-articles new)
10180           (setq gnus-newsgroup-unreads
10181                 (append gnus-newsgroup-unreads new))
10182           (gnus-summary-limit (gnus-union old new))))
10183     (gnus-summary-position-point)))
10184
10185 (gnus-summary-make-all-marking-commands)
10186
10187 (gnus-ems-redefine)
10188
10189 (provide 'gnus-sum)
10190
10191 (run-hooks 'gnus-sum-load-hook)
10192
10193 ;;; gnus-sum.el ends here