2001-02-14 15:00:00 ShengHuo ZHU <zsh@cs.rochester.edu>
[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 This variable is only used when not using a threaded display."
587   :group 'gnus-summary-sort
588   :type '(repeat (choice (function-item gnus-article-sort-by-number)
589                          (function-item gnus-article-sort-by-author)
590                          (function-item gnus-article-sort-by-subject)
591                          (function-item gnus-article-sort-by-date)
592                          (function-item gnus-article-sort-by-score)
593                          (function :tag "other"))))
594
595 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
596   "*List of functions used for sorting threads in the summary buffer.
597 By default, threads are sorted by article number.
598
599 Each function takes two threads and return non-nil if the first thread
600 should be sorted before the other.  If you use more than one function,
601 the primary sort function should be the last.  You should probably
602 always include `gnus-thread-sort-by-number' in the list of sorting
603 functions -- preferably first.
604
605 Ready-made functions include `gnus-thread-sort-by-number',
606 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
607 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
608 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
609   :group 'gnus-summary-sort
610   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
611                          (function-item gnus-thread-sort-by-author)
612                          (function-item gnus-thread-sort-by-subject)
613                          (function-item gnus-thread-sort-by-date)
614                          (function-item gnus-thread-sort-by-score)
615                          (function-item gnus-thread-sort-by-total-score)
616                          (function :tag "other"))))
617
618 (defcustom gnus-thread-score-function '+
619   "*Function used for calculating the total score of a thread.
620
621 The function is called with the scores of the article and each
622 subthread and should then return the score of the thread.
623
624 Some functions you can use are `+', `max', or `min'."
625   :group 'gnus-summary-sort
626   :type 'function)
627
628 (defcustom gnus-summary-expunge-below nil
629   "All articles that have a score less than this variable will be expunged.
630 This variable is local to the summary buffers."
631   :group 'gnus-score-default
632   :type '(choice (const :tag "off" nil)
633                  integer))
634
635 (defcustom gnus-thread-expunge-below nil
636   "All threads that have a total score less than this variable will be expunged.
637 See `gnus-thread-score-function' for en explanation of what a
638 \"thread score\" is.
639
640 This variable is local to the summary buffers."
641   :group 'gnus-threading
642   :group 'gnus-score-default
643   :type '(choice (const :tag "off" nil)
644                  integer))
645
646 (defcustom gnus-summary-mode-hook nil
647   "*A hook for Gnus summary mode.
648 This hook is run before any variables are set in the summary buffer."
649   :options '(turn-on-gnus-mailing-list-mode)
650   :group 'gnus-summary-various
651   :type 'hook)
652
653 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
654 (when (featurep 'xemacs)
655   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
656   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
657   (add-hook 'gnus-summary-mode-hook
658             'gnus-xmas-switch-horizontal-scrollbar-off))
659
660 (defcustom gnus-summary-menu-hook nil
661   "*Hook run after the creation of the summary mode menu."
662   :group 'gnus-summary-visual
663   :type 'hook)
664
665 (defcustom gnus-summary-exit-hook nil
666   "*A hook called on exit from the summary buffer.
667 It will be called with point in the group buffer."
668   :group 'gnus-summary-exit
669   :type 'hook)
670
671 (defcustom gnus-summary-prepare-hook nil
672   "*A hook called after the summary buffer has been generated.
673 If you want to modify the summary buffer, you can use this hook."
674   :group 'gnus-summary-various
675   :type 'hook)
676
677 (defcustom gnus-summary-prepared-hook nil
678   "*A hook called as the last thing after the summary buffer has been generated."
679   :group 'gnus-summary-various
680   :type 'hook)
681
682 (defcustom gnus-summary-generate-hook nil
683   "*A hook run just before generating the summary buffer.
684 This hook is commonly used to customize threading variables and the
685 like."
686   :group 'gnus-summary-various
687   :type 'hook)
688
689 (defcustom gnus-select-group-hook nil
690   "*A hook called when a newsgroup is selected.
691
692 If you'd like to simplify subjects like the
693 `gnus-summary-next-same-subject' command does, you can use the
694 following hook:
695
696  (setq gnus-select-group-hook
697       (list
698         (lambda ()
699           (mapcar (lambda (header)
700                      (mail-header-set-subject
701                       header
702                       (gnus-simplify-subject
703                        (mail-header-subject header) 're-only)))
704                   gnus-newsgroup-headers))))"
705   :group 'gnus-group-select
706   :type 'hook)
707
708 (defcustom gnus-select-article-hook nil
709   "*A hook called when an article is selected."
710   :group 'gnus-summary-choose
711   :type 'hook)
712
713 (defcustom gnus-visual-mark-article-hook
714   (list 'gnus-highlight-selected-summary)
715   "*Hook run after selecting an article in the summary buffer.
716 It is meant to be used for highlighting the article in some way.  It
717 is not run if `gnus-visual' is nil."
718   :group 'gnus-summary-visual
719   :type 'hook)
720
721 (defcustom gnus-parse-headers-hook nil
722   "*A hook called before parsing the headers."
723   :group 'gnus-various
724   :type 'hook)
725
726 (defcustom gnus-exit-group-hook nil
727   "*A hook called when exiting summary mode.
728 This hook is not called from the non-updating exit commands like `Q'."
729   :group 'gnus-various
730   :type 'hook)
731
732 (defcustom gnus-summary-update-hook
733   (list 'gnus-summary-highlight-line)
734   "*A hook called when a summary line is changed.
735 The hook will not be called if `gnus-visual' is nil.
736
737 The default function `gnus-summary-highlight-line' will
738 highlight the line according to the `gnus-summary-highlight'
739 variable."
740   :group 'gnus-summary-visual
741   :type 'hook)
742
743 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
744   "*A hook called when an article is selected for the first time.
745 The hook is intended to mark an article as read (or unread)
746 automatically when it is selected."
747   :group 'gnus-summary-choose
748   :type 'hook)
749
750 (defcustom gnus-group-no-more-groups-hook nil
751   "*A hook run when returning to group mode having no more (unread) groups."
752   :group 'gnus-group-select
753   :type 'hook)
754
755 (defcustom gnus-ps-print-hook nil
756   "*A hook run before ps-printing something from Gnus."
757   :group 'gnus-summary
758   :type 'hook)
759
760 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
761   "Face used for highlighting the current article in the summary buffer."
762   :group 'gnus-summary-visual
763   :type 'face)
764
765 (defcustom gnus-summary-highlight
766   '(((= mark gnus-canceled-mark)
767      . gnus-summary-cancelled-face)
768     ((and (> score default)
769           (or (= mark gnus-dormant-mark)
770               (= mark gnus-ticked-mark)))
771      . gnus-summary-high-ticked-face)
772     ((and (< score default)
773           (or (= mark gnus-dormant-mark)
774               (= mark gnus-ticked-mark)))
775      . gnus-summary-low-ticked-face)
776     ((or (= mark gnus-dormant-mark)
777          (= mark gnus-ticked-mark))
778      . gnus-summary-normal-ticked-face)
779     ((and (> score default) (= mark gnus-ancient-mark))
780      . gnus-summary-high-ancient-face)
781     ((and (< score default) (= mark gnus-ancient-mark))
782      . gnus-summary-low-ancient-face)
783     ((= mark gnus-ancient-mark)
784      . gnus-summary-normal-ancient-face)
785     ((and (> score default) (= mark gnus-unread-mark))
786      . gnus-summary-high-unread-face)
787     ((and (< score default) (= mark gnus-unread-mark))
788      . gnus-summary-low-unread-face)
789     ((= mark gnus-unread-mark)
790      . gnus-summary-normal-unread-face)
791     ((and (> score default) (memq mark (list gnus-downloadable-mark
792                                              gnus-undownloaded-mark)))
793      . gnus-summary-high-unread-face)
794     ((and (< score default) (memq mark (list gnus-downloadable-mark
795                                              gnus-undownloaded-mark)))
796      . gnus-summary-low-unread-face)
797     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
798      . gnus-summary-normal-unread-face)
799     ((> score default)
800      . gnus-summary-high-read-face)
801     ((< score default)
802      . gnus-summary-low-read-face)
803     (t
804      . gnus-summary-normal-read-face))
805   "*Controls the highlighting of summary buffer lines.
806
807 A list of (FORM . FACE) pairs.  When deciding how a a particular
808 summary line should be displayed, each form is evaluated.  The content
809 of the face field after the first true form is used.  You can change
810 how those summary lines are displayed, by editing the face field.
811
812 You can use the following variables in the FORM field.
813
814 score:   The articles score
815 default: The default article score.
816 below:   The score below which articles are automatically marked as read.
817 mark:    The articles mark."
818   :group 'gnus-summary-visual
819   :type '(repeat (cons (sexp :tag "Form" nil)
820                        face)))
821
822 (defcustom gnus-alter-header-function nil
823   "Function called to allow alteration of article header structures.
824 The function is called with one parameter, the article header vector,
825 which it may alter in any way.")
826
827 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
828   "Variable that says which function should be used to decode a string with encoded words.")
829
830 (defcustom gnus-extra-headers nil
831   "*Extra headers to parse."
832   :version "21.1"
833   :group 'gnus-summary
834   :type '(repeat symbol))
835
836 (defcustom gnus-ignored-from-addresses
837   (and user-mail-address (regexp-quote user-mail-address))
838   "*Regexp of From headers that may be suppressed in favor of To headers."
839   :version "21.1"
840   :group 'gnus-summary
841   :type 'regexp)
842
843 (gnus-define-group-parameter
844  charset
845  :function-document
846  "Return the default charset of GROUP."
847  :variable gnus-group-charset-alist
848  :variable-default 
849  '(("\\(^\\|:\\)hk\\>\\|\\(^\\|:\\)tw\\>\\|\\<big5\\>" cn-big5)
850    ("\\(^\\|:\\)cn\\>\\|\\<chinese\\>" cn-gb-2312)
851    ("\\(^\\|:\\)fj\\>\\|\\(^\\|:\\)japan\\>" iso-2022-jp-2)
852    ("\\(^\\|:\\)tnn\\>\\|\\(^\\|:\\)pin\\>\\|\\(^\\|:\\)sci.lang.japan" iso-2022-7bit)
853    ("\\(^\\|:\\)relcom\\>" koi8-r)
854    ("\\(^\\|:\\)fido7\\>" koi8-r)
855    ("\\(^\\|:\\)\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
856    ("\\(^\\|:\\)israel\\>" iso-8859-1)
857    ("\\(^\\|:\\)han\\>" euc-kr)
858    ("\\(^\\|:\\)alt.chinese.text.big5\\>" chinese-big5)
859    ("\\(^\\|:\\)soc.culture.vietnamese\\>" vietnamese-viqr)
860    ("\\(^\\|:\\)\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
861    (".*" iso-8859-1))
862  :variable-document
863   "Alist of regexps (to match group names) and default charsets to be used when reading."
864   :variable-group gnus-charset
865   :variable-type '(repeat (list (regexp :tag "Group")
866                                 (symbol :tag "Charset")))
867   :parameter-type '(symbol :tag "Charset")
868   :parameter-document "\
869 The default charset to use in the group.")
870
871 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
872   "List of charsets that should be ignored.
873 When these charsets are used in the \"charset\" parameter, the
874 default charset will be used instead."
875   :version "21.1"
876   :type '(repeat symbol)
877   :group 'gnus-charset)
878
879 (gnus-define-group-parameter
880  ignored-charsets
881  :type list
882  :function-document
883  "Return the ignored charsets of GROUP."
884  :variable gnus-group-ignored-charsets-alist
885  :variable-default 
886  '(("alt\\.chinese\\.text" iso-8859-1))
887  :variable-document
888  "Alist of regexps (to match group names) and charsets that should be ignored.
889 When these charsets are used in the \"charset\" parameter, the
890 default charset will be used instead."
891  :variable-group gnus-charset
892  :variable-type '(repeat (cons (regexp :tag "Group")
893                                (repeat symbol)))
894  :parameter-type '(choice :tag "Ignored charsets" 
895                           :value nil
896                           (repeat (symbol)))
897  :parameter-document       "\
898 List of charsets that should be ignored.
899
900 When these charsets are used in the \"charset\" parameter, the
901 default charset will be used instead.")
902
903 (defcustom gnus-group-highlight-words-alist nil
904   "Alist of group regexps and highlight regexps.
905 This variable uses the same syntax as `gnus-emphasis-alist'."
906   :version "21.1"
907   :type '(repeat (cons (regexp :tag "Group")
908                        (repeat (list (regexp :tag "Highlight regexp")
909                                      (number :tag "Group for entire word" 0)
910                                      (number :tag "Group for displayed part" 0)
911                                      (symbol :tag "Face"
912                                              gnus-emphasis-highlight-words)))))
913   :group 'gnus-summary-visual)
914
915 (defcustom gnus-summary-show-article-charset-alist
916   nil
917   "Alist of number and charset.
918 The article will be shown with the charset corresponding to the
919 numbered argument.
920 For example: ((1 . cn-gb-2312) (2 . big5))."
921   :version "21.1"
922   :type '(repeat (cons (number :tag "Argument" 1)
923                        (symbol :tag "Charset")))
924   :group 'gnus-charset)
925
926 (defcustom gnus-preserve-marks t
927   "Whether marks are preserved when moving, copying and respooling messages."
928   :version "21.1"
929   :type 'boolean
930   :group 'gnus-summary-marks)
931
932 (defcustom gnus-alter-articles-to-read-function nil
933   "Function to be called to alter the list of articles to be selected."
934   :type 'function
935   :group 'gnus-summary)
936
937 (defcustom gnus-orphan-score nil
938   "*All orphans get this score added.  Set in the score file."
939   :group 'gnus-score-default
940   :type '(choice (const nil)
941                  integer))
942
943 (defcustom gnus-summary-save-parts-default-mime "image/.*"
944   "*A regexp to match MIME parts when saving multiple parts of a message
945 with gnus-summary-save-parts (X m). This regexp will be used by default
946 when prompting the user for which type of files to save."
947   :group 'gnus-summary
948   :type 'regexp)
949
950
951 (defcustom gnus-summary-save-parts-default-mime "image/.*"
952   "*A regexp to match MIME parts when saving multiple parts of a message
953 with gnus-summary-save-parts (X m). This regexp will be used by default
954 when prompting the user for which type of files to save."
955   :group 'gnus-summary
956   :type 'regexp)
957
958
959 ;;; Internal variables
960
961 (defvar gnus-article-mime-handles nil)
962 (defvar gnus-article-decoded-p nil)
963 (defvar gnus-article-charset nil)
964 (defvar gnus-article-ignored-charsets nil)
965 (defvar gnus-scores-exclude-files nil)
966 (defvar gnus-page-broken nil)
967 (defvar gnus-inhibit-mime-unbuttonizing nil)
968
969 (defvar gnus-original-article nil)
970 (defvar gnus-article-internal-prepare-hook nil)
971 (defvar gnus-newsgroup-process-stack nil)
972
973 (defvar gnus-thread-indent-array nil)
974 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
975 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
976   "Function called to sort the articles within a thread after it has been gathered together.")
977
978 (defvar gnus-summary-save-parts-type-history nil)
979 (defvar gnus-summary-save-parts-last-directory nil)
980
981 (defvar gnus-summary-save-parts-type-history nil)
982 (defvar gnus-summary-save-parts-last-directory nil)
983
984 ;; Avoid highlighting in kill files.
985 (defvar gnus-summary-inhibit-highlight nil)
986 (defvar gnus-newsgroup-selected-overlay nil)
987 (defvar gnus-inhibit-limiting nil)
988 (defvar gnus-newsgroup-adaptive-score-file nil)
989 (defvar gnus-current-score-file nil)
990 (defvar gnus-current-move-group nil)
991 (defvar gnus-current-copy-group nil)
992 (defvar gnus-current-crosspost-group nil)
993
994 (defvar gnus-newsgroup-dependencies nil)
995 (defvar gnus-newsgroup-adaptive nil)
996 (defvar gnus-summary-display-article-function nil)
997 (defvar gnus-summary-highlight-line-function nil
998   "Function called after highlighting a summary line.")
999
1000 (defvar gnus-summary-line-format-alist
1001   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1002     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1003     (?s gnus-tmp-subject-or-nil ?s)
1004     (?n gnus-tmp-name ?s)
1005     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1006         ?s)
1007     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1008             gnus-tmp-from) ?s)
1009     (?F gnus-tmp-from ?s)
1010     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1011     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1012     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1013     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1014     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1015     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1016     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1017     (?L gnus-tmp-lines ?d)
1018     (?I gnus-tmp-indentation ?s)
1019     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1020     (?R gnus-tmp-replied ?c)
1021     (?\[ gnus-tmp-opening-bracket ?c)
1022     (?\] gnus-tmp-closing-bracket ?c)
1023     (?\> (make-string gnus-tmp-level ? ) ?s)
1024     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1025     (?i gnus-tmp-score ?d)
1026     (?z gnus-tmp-score-char ?c)
1027     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1028     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1029     (?U gnus-tmp-unread ?c)
1030     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1031     (?t (gnus-summary-number-of-articles-in-thread
1032          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1033         ?d)
1034     (?e (gnus-summary-number-of-articles-in-thread
1035          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1036         ?c)
1037     (?u gnus-tmp-user-defined ?s)
1038     (?P (gnus-pick-line-number) ?d))
1039   "An alist of format specifications that can appear in summary lines.
1040 These are paired with what variables they correspond with, along with
1041 the type of the variable (string, integer, character, etc).")
1042
1043 (defvar gnus-summary-dummy-line-format-alist
1044   `((?S gnus-tmp-subject ?s)
1045     (?N gnus-tmp-number ?d)
1046     (?u gnus-tmp-user-defined ?s)))
1047
1048 (defvar gnus-summary-mode-line-format-alist
1049   `((?G gnus-tmp-group-name ?s)
1050     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1051     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1052     (?A gnus-tmp-article-number ?d)
1053     (?Z gnus-tmp-unread-and-unselected ?s)
1054     (?V gnus-version ?s)
1055     (?U gnus-tmp-unread-and-unticked ?d)
1056     (?S gnus-tmp-subject ?s)
1057     (?e gnus-tmp-unselected ?d)
1058     (?u gnus-tmp-user-defined ?s)
1059     (?d (length gnus-newsgroup-dormant) ?d)
1060     (?t (length gnus-newsgroup-marked) ?d)
1061     (?r (length gnus-newsgroup-reads) ?d)
1062     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1063     (?E gnus-newsgroup-expunged-tally ?d)
1064     (?s (gnus-current-score-file-nondirectory) ?s)))
1065
1066 (defvar gnus-last-search-regexp nil
1067   "Default regexp for article search command.")
1068
1069 (defvar gnus-last-shell-command nil
1070   "Default shell command on article.")
1071
1072 (defvar gnus-newsgroup-begin nil)
1073 (defvar gnus-newsgroup-end nil)
1074 (defvar gnus-newsgroup-last-rmail nil)
1075 (defvar gnus-newsgroup-last-mail nil)
1076 (defvar gnus-newsgroup-last-folder nil)
1077 (defvar gnus-newsgroup-last-file nil)
1078 (defvar gnus-newsgroup-auto-expire nil)
1079 (defvar gnus-newsgroup-active nil)
1080
1081 (defvar gnus-newsgroup-data nil)
1082 (defvar gnus-newsgroup-data-reverse nil)
1083 (defvar gnus-newsgroup-limit nil)
1084 (defvar gnus-newsgroup-limits nil)
1085
1086 (defvar gnus-newsgroup-unreads nil
1087   "List of unread articles in the current newsgroup.")
1088
1089 (defvar gnus-newsgroup-unselected nil
1090   "List of unselected unread articles in the current newsgroup.")
1091
1092 (defvar gnus-newsgroup-reads nil
1093   "Alist of read articles and article marks in the current newsgroup.")
1094
1095 (defvar gnus-newsgroup-expunged-tally nil)
1096
1097 (defvar gnus-newsgroup-marked nil
1098   "List of ticked articles in the current newsgroup (a subset of unread art).")
1099
1100 (defvar gnus-newsgroup-killed nil
1101   "List of ranges of articles that have been through the scoring process.")
1102
1103 (defvar gnus-newsgroup-cached nil
1104   "List of articles that come from the article cache.")
1105
1106 (defvar gnus-newsgroup-saved nil
1107   "List of articles that have been saved.")
1108
1109 (defvar gnus-newsgroup-kill-headers nil)
1110
1111 (defvar gnus-newsgroup-replied nil
1112   "List of articles that have been replied to in the current newsgroup.")
1113
1114 (defvar gnus-newsgroup-expirable nil
1115   "List of articles in the current newsgroup that can be expired.")
1116
1117 (defvar gnus-newsgroup-processable nil
1118   "List of articles in the current newsgroup that can be processed.")
1119
1120 (defvar gnus-newsgroup-downloadable nil
1121   "List of articles in the current newsgroup that can be processed.")
1122
1123 (defvar gnus-newsgroup-undownloaded nil
1124   "List of articles in the current newsgroup that haven't been downloaded..")
1125
1126 (defvar gnus-newsgroup-unsendable nil
1127   "List of articles in the current newsgroup that won't be sent.")
1128
1129 (defvar gnus-newsgroup-bookmarks nil
1130   "List of articles in the current newsgroup that have bookmarks.")
1131
1132 (defvar gnus-newsgroup-dormant nil
1133   "List of dormant articles in the current newsgroup.")
1134
1135 (defvar gnus-newsgroup-scored nil
1136   "List of scored articles in the current newsgroup.")
1137
1138 (defvar gnus-newsgroup-headers nil
1139   "List of article headers in the current newsgroup.")
1140
1141 (defvar gnus-newsgroup-threads nil)
1142
1143 (defvar gnus-newsgroup-prepared nil
1144   "Whether the current group has been prepared properly.")
1145
1146 (defvar gnus-newsgroup-ancient nil
1147   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1148
1149 (defvar gnus-newsgroup-sparse nil)
1150
1151 (defvar gnus-current-article nil)
1152 (defvar gnus-article-current nil)
1153 (defvar gnus-current-headers nil)
1154 (defvar gnus-have-all-headers nil)
1155 (defvar gnus-last-article nil)
1156 (defvar gnus-newsgroup-history nil)
1157 (defvar gnus-newsgroup-charset nil)
1158 (defvar gnus-newsgroup-ephemeral-charset nil)
1159 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1160
1161 (defvar gnus-article-before-search nil)
1162
1163 (defconst gnus-summary-local-variables
1164   '(gnus-newsgroup-name
1165     gnus-newsgroup-begin gnus-newsgroup-end
1166     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1167     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1168     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1169     gnus-newsgroup-unselected gnus-newsgroup-marked
1170     gnus-newsgroup-reads gnus-newsgroup-saved
1171     gnus-newsgroup-replied gnus-newsgroup-expirable
1172     gnus-newsgroup-processable gnus-newsgroup-killed
1173     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1174     gnus-newsgroup-unsendable
1175     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1176     gnus-newsgroup-headers gnus-newsgroup-threads
1177     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1178     gnus-current-article gnus-current-headers gnus-have-all-headers
1179     gnus-last-article gnus-article-internal-prepare-hook
1180     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1181     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1182     gnus-thread-expunge-below
1183     gnus-score-alist gnus-current-score-file
1184     (gnus-summary-expunge-below . global)
1185     (gnus-summary-mark-below . global)
1186     (gnus-orphan-score . global)
1187     gnus-newsgroup-active gnus-scores-exclude-files
1188     gnus-newsgroup-history gnus-newsgroup-ancient
1189     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1190     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1191     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1192     (gnus-newsgroup-expunged-tally . 0)
1193     gnus-cache-removable-articles gnus-newsgroup-cached
1194     gnus-newsgroup-data gnus-newsgroup-data-reverse
1195     gnus-newsgroup-limit gnus-newsgroup-limits
1196     gnus-newsgroup-charset)
1197   "Variables that are buffer-local to the summary buffers.")
1198
1199 (defvar gnus-newsgroup-variables nil
1200   "Variables that have separate values in the newsgroups.")
1201
1202 ;; Byte-compiler warning.
1203 (eval-when-compile (defvar gnus-article-mode-map))
1204
1205 ;; MIME stuff.
1206
1207 (defvar gnus-decode-encoded-word-methods
1208   '(mail-decode-encoded-word-string)
1209   "List of methods used to decode encoded words.
1210
1211 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1212 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1213 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1214 whose names match REGEXP.
1215
1216 For example:
1217 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1218  mail-decode-encoded-word-string
1219  (\"chinese\" . rfc1843-decode-string))")
1220
1221 (defvar gnus-decode-encoded-word-methods-cache nil)
1222
1223 (defun gnus-multi-decode-encoded-word-string (string)
1224   "Apply the functions from `gnus-encoded-word-methods' that match."
1225   (unless (and gnus-decode-encoded-word-methods-cache
1226                (eq gnus-newsgroup-name
1227                    (car gnus-decode-encoded-word-methods-cache)))
1228     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1229     (mapcar (lambda (x)
1230               (if (symbolp x)
1231                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1232                 (if (and gnus-newsgroup-name
1233                          (string-match (car x) gnus-newsgroup-name))
1234                     (nconc gnus-decode-encoded-word-methods-cache
1235                            (list (cdr x))))))
1236           gnus-decode-encoded-word-methods))
1237   (let ((xlist gnus-decode-encoded-word-methods-cache))
1238     (pop xlist)
1239     (while xlist
1240       (setq string (funcall (pop xlist) string))))
1241   string)
1242
1243 ;; Subject simplification.
1244
1245 (defun gnus-simplify-whitespace (str)
1246   "Remove excessive whitespace from STR."
1247   (let ((mystr str))
1248     ;; Multiple spaces.
1249     (while (string-match "[ \t][ \t]+" mystr)
1250       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1251                           " "
1252                           (substring mystr (match-end 0)))))
1253     ;; Leading spaces.
1254     (when (string-match "^[ \t]+" mystr)
1255       (setq mystr (substring mystr (match-end 0))))
1256     ;; Trailing spaces.
1257     (when (string-match "[ \t]+$" mystr)
1258       (setq mystr (substring mystr 0 (match-beginning 0))))
1259     mystr))
1260
1261 (defsubst gnus-simplify-subject-re (subject)
1262   "Remove \"Re:\" from subject lines."
1263   (if (string-match message-subject-re-regexp subject)
1264       (substring subject (match-end 0))
1265     subject))
1266
1267 (defun gnus-simplify-subject (subject &optional re-only)
1268   "Remove `Re:' and words in parentheses.
1269 If RE-ONLY is non-nil, strip leading `Re:'s only."
1270   (let ((case-fold-search t))           ;Ignore case.
1271     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1272     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1273       (setq subject (substring subject (match-end 0))))
1274     ;; Remove uninteresting prefixes.
1275     (when (and (not re-only)
1276                gnus-simplify-ignored-prefixes
1277                (string-match gnus-simplify-ignored-prefixes subject))
1278       (setq subject (substring subject (match-end 0))))
1279     ;; Remove words in parentheses from end.
1280     (unless re-only
1281       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1282         (setq subject (substring subject 0 (match-beginning 0)))))
1283     ;; Return subject string.
1284     subject))
1285
1286 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1287 ;; all whitespace.
1288 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1289   (goto-char (point-min))
1290   (while (re-search-forward regexp nil t)
1291     (replace-match (or newtext ""))))
1292
1293 (defun gnus-simplify-buffer-fuzzy ()
1294   "Simplify string in the buffer fuzzily.
1295 The string in the accessible portion of the current buffer is simplified.
1296 It is assumed to be a single-line subject.
1297 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1298 matter is removed.  Additional things can be deleted by setting
1299 `gnus-simplify-subject-fuzzy-regexp'."
1300   (let ((case-fold-search t)
1301         (modified-tick))
1302     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1303
1304     (while (not (eq modified-tick (buffer-modified-tick)))
1305       (setq modified-tick (buffer-modified-tick))
1306       (cond
1307        ((listp gnus-simplify-subject-fuzzy-regexp)
1308         (mapcar 'gnus-simplify-buffer-fuzzy-step
1309                 gnus-simplify-subject-fuzzy-regexp))
1310        (gnus-simplify-subject-fuzzy-regexp
1311         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1312       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1313       (gnus-simplify-buffer-fuzzy-step
1314        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1315       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1316
1317     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1318     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1319     (gnus-simplify-buffer-fuzzy-step " $")
1320     (gnus-simplify-buffer-fuzzy-step "^ +")))
1321
1322 (defun gnus-simplify-subject-fuzzy (subject)
1323   "Simplify a subject string fuzzily.
1324 See `gnus-simplify-buffer-fuzzy' for details."
1325   (save-excursion
1326     (gnus-set-work-buffer)
1327     (let ((case-fold-search t))
1328       ;; Remove uninteresting prefixes.
1329       (when (and gnus-simplify-ignored-prefixes
1330                  (string-match gnus-simplify-ignored-prefixes subject))
1331         (setq subject (substring subject (match-end 0))))
1332       (insert subject)
1333       (inline (gnus-simplify-buffer-fuzzy))
1334       (buffer-string))))
1335
1336 (defsubst gnus-simplify-subject-fully (subject)
1337   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1338   (cond
1339    (gnus-simplify-subject-functions
1340     (gnus-map-function gnus-simplify-subject-functions subject))
1341    ((null gnus-summary-gather-subject-limit)
1342     (gnus-simplify-subject-re subject))
1343    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1344     (gnus-simplify-subject-fuzzy subject))
1345    ((numberp gnus-summary-gather-subject-limit)
1346     (gnus-limit-string (gnus-simplify-subject-re subject)
1347                        gnus-summary-gather-subject-limit))
1348    (t
1349     subject)))
1350
1351 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1352   "Check whether two subjects are equal.
1353 If optional argument simple-first is t, first argument is already
1354 simplified."
1355   (cond
1356    ((null simple-first)
1357     (equal (gnus-simplify-subject-fully s1)
1358            (gnus-simplify-subject-fully s2)))
1359    (t
1360     (equal s1
1361            (gnus-simplify-subject-fully s2)))))
1362
1363 (defun gnus-summary-bubble-group ()
1364   "Increase the score of the current group.
1365 This is a handy function to add to `gnus-summary-exit-hook' to
1366 increase the score of each group you read."
1367   (gnus-group-add-score gnus-newsgroup-name))
1368
1369 \f
1370 ;;;
1371 ;;; Gnus summary mode
1372 ;;;
1373
1374 (put 'gnus-summary-mode 'mode-class 'special)
1375
1376 (defvar gnus-article-commands-menu)
1377
1378 (when t
1379   ;; Non-orthogonal keys
1380
1381   (gnus-define-keys gnus-summary-mode-map
1382     " " gnus-summary-next-page
1383     "\177" gnus-summary-prev-page
1384     [delete] gnus-summary-prev-page
1385     [backspace] gnus-summary-prev-page
1386     "\r" gnus-summary-scroll-up
1387     "\M-\r" gnus-summary-scroll-down
1388     "n" gnus-summary-next-unread-article
1389     "p" gnus-summary-prev-unread-article
1390     "N" gnus-summary-next-article
1391     "P" gnus-summary-prev-article
1392     "\M-\C-n" gnus-summary-next-same-subject
1393     "\M-\C-p" gnus-summary-prev-same-subject
1394     "\M-n" gnus-summary-next-unread-subject
1395     "\M-p" gnus-summary-prev-unread-subject
1396     "." gnus-summary-first-unread-article
1397     "," gnus-summary-best-unread-article
1398     "\M-s" gnus-summary-search-article-forward
1399     "\M-r" gnus-summary-search-article-backward
1400     "<" gnus-summary-beginning-of-article
1401     ">" gnus-summary-end-of-article
1402     "j" gnus-summary-goto-article
1403     "^" gnus-summary-refer-parent-article
1404     "\M-^" gnus-summary-refer-article
1405     "u" gnus-summary-tick-article-forward
1406     "!" gnus-summary-tick-article-forward
1407     "U" gnus-summary-tick-article-backward
1408     "d" gnus-summary-mark-as-read-forward
1409     "D" gnus-summary-mark-as-read-backward
1410     "E" gnus-summary-mark-as-expirable
1411     "\M-u" gnus-summary-clear-mark-forward
1412     "\M-U" gnus-summary-clear-mark-backward
1413     "k" gnus-summary-kill-same-subject-and-select
1414     "\C-k" gnus-summary-kill-same-subject
1415     "\M-\C-k" gnus-summary-kill-thread
1416     "\M-\C-l" gnus-summary-lower-thread
1417     "e" gnus-summary-edit-article
1418     "#" gnus-summary-mark-as-processable
1419     "\M-#" gnus-summary-unmark-as-processable
1420     "\M-\C-t" gnus-summary-toggle-threads
1421     "\M-\C-s" gnus-summary-show-thread
1422     "\M-\C-h" gnus-summary-hide-thread
1423     "\M-\C-f" gnus-summary-next-thread
1424     "\M-\C-b" gnus-summary-prev-thread
1425     [(meta down)] gnus-summary-next-thread
1426     [(meta up)] gnus-summary-prev-thread
1427     "\M-\C-u" gnus-summary-up-thread
1428     "\M-\C-d" gnus-summary-down-thread
1429     "&" gnus-summary-execute-command
1430     "c" gnus-summary-catchup-and-exit
1431     "\C-w" gnus-summary-mark-region-as-read
1432     "\C-t" gnus-summary-toggle-truncation
1433     "?" gnus-summary-mark-as-dormant
1434     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1435     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1436     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1437     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1438     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1439     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1440     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1441     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1442     "=" gnus-summary-expand-window
1443     "\C-x\C-s" gnus-summary-reselect-current-group
1444     "\M-g" gnus-summary-rescan-group
1445     "w" gnus-summary-stop-page-breaking
1446     "\C-c\C-r" gnus-summary-caesar-message
1447     "f" gnus-summary-followup
1448     "F" gnus-summary-followup-with-original
1449     "C" gnus-summary-cancel-article
1450     "r" gnus-summary-reply
1451     "R" gnus-summary-reply-with-original
1452     "\C-c\C-f" gnus-summary-mail-forward
1453     "o" gnus-summary-save-article
1454     "\C-o" gnus-summary-save-article-mail
1455     "|" gnus-summary-pipe-output
1456     "\M-k" gnus-summary-edit-local-kill
1457     "\M-K" gnus-summary-edit-global-kill
1458     ;; "V" gnus-version
1459     "\C-c\C-d" gnus-summary-describe-group
1460     "q" gnus-summary-exit
1461     "Q" gnus-summary-exit-no-update
1462     "\C-c\C-i" gnus-info-find-node
1463     gnus-mouse-2 gnus-mouse-pick-article
1464     "m" gnus-summary-mail-other-window
1465     "a" gnus-summary-post-news
1466     "x" gnus-summary-limit-to-unread
1467     "s" gnus-summary-isearch-article
1468     "t" gnus-summary-toggle-header
1469     "g" gnus-summary-show-article
1470     "l" gnus-summary-goto-last-article
1471     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1472     "\C-d" gnus-summary-enter-digest-group
1473     "\M-\C-d" gnus-summary-read-document
1474     "\M-\C-e" gnus-summary-edit-parameters
1475     "\M-\C-a" gnus-summary-customize-parameters
1476     "\C-c\C-b" gnus-bug
1477     "*" gnus-cache-enter-article
1478     "\M-*" gnus-cache-remove-article
1479     "\M-&" gnus-summary-universal-argument
1480     "\C-l" gnus-recenter
1481     "I" gnus-summary-increase-score
1482     "L" gnus-summary-lower-score
1483     "\M-i" gnus-symbolic-argument
1484     "h" gnus-summary-select-article-buffer
1485
1486     "b" gnus-article-view-part
1487     "\M-t" gnus-summary-toggle-display-buttonized
1488
1489     "V" gnus-summary-score-map
1490     "X" gnus-uu-extract-map
1491     "S" gnus-summary-send-map)
1492
1493   ;; Sort of orthogonal keymap
1494   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1495     "t" gnus-summary-tick-article-forward
1496     "!" gnus-summary-tick-article-forward
1497     "d" gnus-summary-mark-as-read-forward
1498     "r" gnus-summary-mark-as-read-forward
1499     "c" gnus-summary-clear-mark-forward
1500     " " gnus-summary-clear-mark-forward
1501     "e" gnus-summary-mark-as-expirable
1502     "x" gnus-summary-mark-as-expirable
1503     "?" gnus-summary-mark-as-dormant
1504     "b" gnus-summary-set-bookmark
1505     "B" gnus-summary-remove-bookmark
1506     "#" gnus-summary-mark-as-processable
1507     "\M-#" gnus-summary-unmark-as-processable
1508     "S" gnus-summary-limit-include-expunged
1509     "C" gnus-summary-catchup
1510     "H" gnus-summary-catchup-to-here
1511     "\C-c" gnus-summary-catchup-all
1512     "k" gnus-summary-kill-same-subject-and-select
1513     "K" gnus-summary-kill-same-subject
1514     "P" gnus-uu-mark-map)
1515
1516   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1517     "c" gnus-summary-clear-above
1518     "u" gnus-summary-tick-above
1519     "m" gnus-summary-mark-above
1520     "k" gnus-summary-kill-below)
1521
1522   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1523     "/" gnus-summary-limit-to-subject
1524     "n" gnus-summary-limit-to-articles
1525     "w" gnus-summary-pop-limit
1526     "s" gnus-summary-limit-to-subject
1527     "a" gnus-summary-limit-to-author
1528     "u" gnus-summary-limit-to-unread
1529     "m" gnus-summary-limit-to-marks
1530     "M" gnus-summary-limit-exclude-marks
1531     "v" gnus-summary-limit-to-score
1532     "*" gnus-summary-limit-include-cached
1533     "D" gnus-summary-limit-include-dormant
1534     "T" gnus-summary-limit-include-thread
1535     "d" gnus-summary-limit-exclude-dormant
1536     "t" gnus-summary-limit-to-age
1537     "x" gnus-summary-limit-to-extra
1538     "E" gnus-summary-limit-include-expunged
1539     "c" gnus-summary-limit-exclude-childless-dormant
1540     "C" gnus-summary-limit-mark-excluded-as-read)
1541
1542   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1543     "n" gnus-summary-next-unread-article
1544     "p" gnus-summary-prev-unread-article
1545     "N" gnus-summary-next-article
1546     "P" gnus-summary-prev-article
1547     "\C-n" gnus-summary-next-same-subject
1548     "\C-p" gnus-summary-prev-same-subject
1549     "\M-n" gnus-summary-next-unread-subject
1550     "\M-p" gnus-summary-prev-unread-subject
1551     "f" gnus-summary-first-unread-article
1552     "b" gnus-summary-best-unread-article
1553     "j" gnus-summary-goto-article
1554     "g" gnus-summary-goto-subject
1555     "l" gnus-summary-goto-last-article
1556     "o" gnus-summary-pop-article)
1557
1558   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1559     "k" gnus-summary-kill-thread
1560     "l" gnus-summary-lower-thread
1561     "i" gnus-summary-raise-thread
1562     "T" gnus-summary-toggle-threads
1563     "t" gnus-summary-rethread-current
1564     "^" gnus-summary-reparent-thread
1565     "s" gnus-summary-show-thread
1566     "S" gnus-summary-show-all-threads
1567     "h" gnus-summary-hide-thread
1568     "H" gnus-summary-hide-all-threads
1569     "n" gnus-summary-next-thread
1570     "p" gnus-summary-prev-thread
1571     "u" gnus-summary-up-thread
1572     "o" gnus-summary-top-thread
1573     "d" gnus-summary-down-thread
1574     "#" gnus-uu-mark-thread
1575     "\M-#" gnus-uu-unmark-thread)
1576
1577   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1578     "g" gnus-summary-prepare
1579     "c" gnus-summary-insert-cached-articles)
1580
1581   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1582     "c" gnus-summary-catchup-and-exit
1583     "C" gnus-summary-catchup-all-and-exit
1584     "E" gnus-summary-exit-no-update
1585     "Q" gnus-summary-exit
1586     "Z" gnus-summary-exit
1587     "n" gnus-summary-catchup-and-goto-next-group
1588     "R" gnus-summary-reselect-current-group
1589     "G" gnus-summary-rescan-group
1590     "N" gnus-summary-next-group
1591     "s" gnus-summary-save-newsrc
1592     "P" gnus-summary-prev-group)
1593
1594   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1595     " " gnus-summary-next-page
1596     "n" gnus-summary-next-page
1597     "\177" gnus-summary-prev-page
1598     [delete] gnus-summary-prev-page
1599     "p" gnus-summary-prev-page
1600     "\r" gnus-summary-scroll-up
1601     "\M-\r" gnus-summary-scroll-down
1602     "<" gnus-summary-beginning-of-article
1603     ">" gnus-summary-end-of-article
1604     "b" gnus-summary-beginning-of-article
1605     "e" gnus-summary-end-of-article
1606     "^" gnus-summary-refer-parent-article
1607     "r" gnus-summary-refer-parent-article
1608     "D" gnus-summary-enter-digest-group
1609     "R" gnus-summary-refer-references
1610     "T" gnus-summary-refer-thread
1611     "g" gnus-summary-show-article
1612     "s" gnus-summary-isearch-article
1613     "P" gnus-summary-print-article
1614     "M" gnus-mailing-list-insinuate
1615     "t" gnus-article-babel)
1616
1617   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1618     "b" gnus-article-add-buttons
1619     "B" gnus-article-add-buttons-to-head
1620     "o" gnus-article-treat-overstrike
1621     "e" gnus-article-emphasize
1622     "w" gnus-article-fill-cited-article
1623     "Q" gnus-article-fill-long-lines
1624     "C" gnus-article-capitalize-sentences
1625     "c" gnus-article-remove-cr
1626     "q" gnus-article-de-quoted-unreadable
1627     "6" gnus-article-de-base64-unreadable
1628     "Z" gnus-article-decode-HZ
1629     "h" gnus-article-wash-html
1630     "s" gnus-summary-force-verify-and-decrypt
1631     "f" gnus-article-display-x-face
1632     "l" gnus-summary-stop-page-breaking
1633     "r" gnus-summary-caesar-message
1634     "t" gnus-summary-toggle-header
1635     "v" gnus-summary-verbose-headers
1636     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1637     "p" gnus-article-verify-x-pgp-sig
1638     "d" gnus-article-treat-dumbquotes)
1639
1640   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1641     "a" gnus-article-hide
1642     "h" gnus-article-hide-headers
1643     "b" gnus-article-hide-boring-headers
1644     "s" gnus-article-hide-signature
1645     "c" gnus-article-hide-citation
1646     "C" gnus-article-hide-citation-in-followups
1647     "l" gnus-article-hide-list-identifiers
1648     "p" gnus-article-hide-pgp
1649     "B" gnus-article-strip-banner
1650     "P" gnus-article-hide-pem
1651     "\C-c" gnus-article-hide-citation-maybe)
1652
1653   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1654     "a" gnus-article-highlight
1655     "h" gnus-article-highlight-headers
1656     "c" gnus-article-highlight-citation
1657     "s" gnus-article-highlight-signature)
1658
1659   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1660     "w" gnus-article-decode-mime-words
1661     "c" gnus-article-decode-charset
1662     "v" gnus-mime-view-all-parts
1663     "b" gnus-article-view-part)
1664
1665   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1666     "z" gnus-article-date-ut
1667     "u" gnus-article-date-ut
1668     "l" gnus-article-date-local
1669     "p" gnus-article-date-english
1670     "e" gnus-article-date-lapsed
1671     "o" gnus-article-date-original
1672     "i" gnus-article-date-iso8601
1673     "s" gnus-article-date-user)
1674
1675   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1676     "t" gnus-article-remove-trailing-blank-lines
1677     "l" gnus-article-strip-leading-blank-lines
1678     "m" gnus-article-strip-multiple-blank-lines
1679     "a" gnus-article-strip-blank-lines
1680     "A" gnus-article-strip-all-blank-lines
1681     "s" gnus-article-strip-leading-space
1682     "e" gnus-article-strip-trailing-space
1683     "w" gnus-article-remove-leading-whitespace)
1684
1685   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1686     "v" gnus-version
1687     "f" gnus-summary-fetch-faq
1688     "d" gnus-summary-describe-group
1689     "h" gnus-summary-describe-briefly
1690     "i" gnus-info-find-node)
1691
1692   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1693     "e" gnus-summary-expire-articles
1694     "\M-\C-e" gnus-summary-expire-articles-now
1695     "\177" gnus-summary-delete-article
1696     [delete] gnus-summary-delete-article
1697     [backspace] gnus-summary-delete-article
1698     "m" gnus-summary-move-article
1699     "r" gnus-summary-respool-article
1700     "w" gnus-summary-edit-article
1701     "c" gnus-summary-copy-article
1702     "B" gnus-summary-crosspost-article
1703     "q" gnus-summary-respool-query
1704     "t" gnus-summary-respool-trace
1705     "i" gnus-summary-import-article
1706     "p" gnus-summary-article-posted-p)
1707
1708   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1709     "o" gnus-summary-save-article
1710     "m" gnus-summary-save-article-mail
1711     "F" gnus-summary-write-article-file
1712     "r" gnus-summary-save-article-rmail
1713     "f" gnus-summary-save-article-file
1714     "b" gnus-summary-save-article-body-file
1715     "h" gnus-summary-save-article-folder
1716     "v" gnus-summary-save-article-vm
1717     "p" gnus-summary-pipe-output
1718     "s" gnus-soup-add-article)
1719
1720   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1721     "b" gnus-summary-display-buttonized
1722     "m" gnus-summary-repair-multipart
1723     "v" gnus-article-view-part
1724     "o" gnus-article-save-part
1725     "c" gnus-article-copy-part
1726     "C" gnus-article-view-part-as-charset
1727     "e" gnus-article-externalize-part
1728     "E" gnus-article-encrypt-body
1729     "i" gnus-article-inline-part
1730     "|" gnus-article-pipe-part))
1731
1732 (defun gnus-summary-make-menu-bar ()
1733   (gnus-turn-off-edit-menu 'summary)
1734
1735   (unless (boundp 'gnus-summary-misc-menu)
1736
1737     (easy-menu-define
1738      gnus-summary-kill-menu gnus-summary-mode-map ""
1739      (cons
1740       "Score"
1741       (nconc
1742        (list
1743         ["Customize" gnus-score-customize t])
1744        (gnus-make-score-map 'increase)
1745        (gnus-make-score-map 'lower)
1746        '(("Mark"
1747           ["Kill below" gnus-summary-kill-below t]
1748           ["Mark above" gnus-summary-mark-above t]
1749           ["Tick above" gnus-summary-tick-above t]
1750           ["Clear above" gnus-summary-clear-above t])
1751          ["Current score" gnus-summary-current-score t]
1752          ["Set score" gnus-summary-set-score t]
1753          ["Switch current score file..." gnus-score-change-score-file t]
1754          ["Set mark below..." gnus-score-set-mark-below t]
1755          ["Set expunge below..." gnus-score-set-expunge-below t]
1756          ["Edit current score file" gnus-score-edit-current-scores t]
1757          ["Edit score file" gnus-score-edit-file t]
1758          ["Trace score" gnus-score-find-trace t]
1759          ["Find words" gnus-score-find-favourite-words t]
1760          ["Rescore buffer" gnus-summary-rescore t]
1761          ["Increase score..." gnus-summary-increase-score t]
1762          ["Lower score..." gnus-summary-lower-score t]))))
1763
1764     ;; Define both the Article menu in the summary buffer and the equivalent
1765     ;; Commands menu in the article buffer here for consistency.
1766     (let ((innards
1767            `(("Hide"
1768               ["All" gnus-article-hide t]
1769               ["Headers" gnus-article-hide-headers t]
1770               ["Signature" gnus-article-hide-signature t]
1771               ["Citation" gnus-article-hide-citation t]
1772               ["List identifiers" gnus-article-hide-list-identifiers t]
1773               ["PGP" gnus-article-hide-pgp t]
1774               ["Banner" gnus-article-strip-banner t]
1775               ["Boring headers" gnus-article-hide-boring-headers t])
1776              ("Highlight"
1777               ["All" gnus-article-highlight t]
1778               ["Headers" gnus-article-highlight-headers t]
1779               ["Signature" gnus-article-highlight-signature t]
1780               ["Citation" gnus-article-highlight-citation t])
1781              ("MIME"
1782               ["Words" gnus-article-decode-mime-words t]
1783               ["Charset" gnus-article-decode-charset t]
1784               ["QP" gnus-article-de-quoted-unreadable t]
1785               ["Base64" gnus-article-de-base64-unreadable t]
1786               ["View all" gnus-mime-view-all-parts t]
1787               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1788               ["Encrypt body" gnus-article-encrypt-body t])
1789              ("Date"
1790               ["Local" gnus-article-date-local t]
1791               ["ISO8601" gnus-article-date-iso8601 t]
1792               ["UT" gnus-article-date-ut t]
1793               ["Original" gnus-article-date-original t]
1794               ["Lapsed" gnus-article-date-lapsed t]
1795               ["User-defined" gnus-article-date-user t])
1796              ("Washing"
1797               ("Remove Blanks"
1798                ["Leading" gnus-article-strip-leading-blank-lines t]
1799                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1800                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1801                ["All of the above" gnus-article-strip-blank-lines t]
1802                ["All" gnus-article-strip-all-blank-lines t]
1803                ["Leading space" gnus-article-strip-leading-space t]
1804                ["Trailing space" gnus-article-strip-trailing-space t]
1805                ["Leading space in headers" 
1806                 gnus-article-remove-leading-whitespace t])
1807               ["Overstrike" gnus-article-treat-overstrike t]
1808               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1809               ["Emphasis" gnus-article-emphasize t]
1810               ["Word wrap" gnus-article-fill-cited-article t]
1811               ["Fill long lines" gnus-article-fill-long-lines t]
1812               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1813               ["CR" gnus-article-remove-cr t]
1814               ["Show X-Face" gnus-article-display-x-face t]
1815               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1816               ["Base64" gnus-article-de-base64-unreadable t]
1817               ["Rot 13" gnus-summary-caesar-message
1818                ,@(if (featurep 'xemacs) '(t)
1819                    '(:help "\"Caesar rotate\" article by 13"))]
1820               ["Unix pipe" gnus-summary-pipe-message t]
1821               ["Add buttons" gnus-article-add-buttons t]
1822               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1823               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1824               ["Verbose header" gnus-summary-verbose-headers t]
1825               ["Toggle header" gnus-summary-toggle-header t]
1826               ["Html" gnus-article-wash-html t]
1827               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1828               ["HZ" gnus-article-decode-HZ t])
1829              ("Output"
1830               ["Save in default format" gnus-summary-save-article
1831                ,@(if (featurep 'xemacs) '(t)
1832                    '(:help "Save article using default method"))]
1833               ["Save in file" gnus-summary-save-article-file
1834                ,@(if (featurep 'xemacs) '(t)
1835                    '(:help "Save article in file"))]
1836               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1837               ["Save in MH folder" gnus-summary-save-article-folder t]
1838               ["Save in VM folder" gnus-summary-save-article-vm t]
1839               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1840               ["Save body in file" gnus-summary-save-article-body-file t]
1841               ["Pipe through a filter" gnus-summary-pipe-output t]
1842               ["Add to SOUP packet" gnus-soup-add-article t]
1843               ["Print" gnus-summary-print-article t])
1844              ("Backend"
1845               ["Respool article..." gnus-summary-respool-article t]
1846               ["Move article..." gnus-summary-move-article
1847                (gnus-check-backend-function
1848                 'request-move-article gnus-newsgroup-name)]
1849               ["Copy article..." gnus-summary-copy-article t]
1850               ["Crosspost article..." gnus-summary-crosspost-article
1851                (gnus-check-backend-function
1852                 'request-replace-article gnus-newsgroup-name)]
1853               ["Import file..." gnus-summary-import-article t]
1854               ["Check if posted" gnus-summary-article-posted-p t]
1855               ["Edit article" gnus-summary-edit-article
1856                (not (gnus-group-read-only-p))]
1857               ["Delete article" gnus-summary-delete-article
1858                (gnus-check-backend-function
1859                 'request-expire-articles gnus-newsgroup-name)]
1860               ["Query respool" gnus-summary-respool-query t]
1861               ["Trace respool" gnus-summary-respool-trace t]
1862               ["Delete expirable articles" gnus-summary-expire-articles-now
1863                (gnus-check-backend-function
1864                 'request-expire-articles gnus-newsgroup-name)])
1865              ("Extract"
1866               ["Uudecode" gnus-uu-decode-uu
1867                ,@(if (featurep 'xemacs) '(t)
1868                    '(:help "Decode uuencoded article(s)"))]
1869               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1870               ["Unshar" gnus-uu-decode-unshar t]
1871               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1872               ["Save" gnus-uu-decode-save t]
1873               ["Binhex" gnus-uu-decode-binhex t]
1874               ["Postscript" gnus-uu-decode-postscript t])
1875              ("Cache"
1876               ["Enter article" gnus-cache-enter-article t]
1877               ["Remove article" gnus-cache-remove-article t])
1878              ["Translate" gnus-article-babel t]
1879              ["Select article buffer" gnus-summary-select-article-buffer t]
1880              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1881              ["Isearch article..." gnus-summary-isearch-article t]
1882              ["Beginning of the article" gnus-summary-beginning-of-article t]
1883              ["End of the article" gnus-summary-end-of-article t]
1884              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1885              ["Fetch referenced articles" gnus-summary-refer-references t]
1886              ["Fetch current thread" gnus-summary-refer-thread t]
1887              ["Fetch article with id..." gnus-summary-refer-article t]
1888              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1889              ["Redisplay" gnus-summary-show-article t])))
1890       (easy-menu-define
1891        gnus-summary-article-menu gnus-summary-mode-map ""
1892        (cons "Article" innards))
1893
1894       (if (not (keymapp gnus-summary-article-menu))
1895           (easy-menu-define
1896             gnus-article-commands-menu gnus-article-mode-map ""
1897             (cons "Commands" innards))
1898         ;; in Emacs, don't share menu.
1899         (setq gnus-article-commands-menu 
1900               (copy-keymap gnus-summary-article-menu))
1901         (define-key gnus-article-mode-map [menu-bar commands]
1902           (cons "Commands" gnus-article-commands-menu))))
1903
1904     (easy-menu-define
1905      gnus-summary-thread-menu gnus-summary-mode-map ""
1906      '("Threads"
1907        ["Toggle threading" gnus-summary-toggle-threads t]
1908        ["Hide threads" gnus-summary-hide-all-threads t]
1909        ["Show threads" gnus-summary-show-all-threads t]
1910        ["Hide thread" gnus-summary-hide-thread t]
1911        ["Show thread" gnus-summary-show-thread t]
1912        ["Go to next thread" gnus-summary-next-thread t]
1913        ["Go to previous thread" gnus-summary-prev-thread t]
1914        ["Go down thread" gnus-summary-down-thread t]
1915        ["Go up thread" gnus-summary-up-thread t]
1916        ["Top of thread" gnus-summary-top-thread t]
1917        ["Mark thread as read" gnus-summary-kill-thread t]
1918        ["Lower thread score" gnus-summary-lower-thread t]
1919        ["Raise thread score" gnus-summary-raise-thread t]
1920        ["Rethread current" gnus-summary-rethread-current t]))
1921
1922     (easy-menu-define
1923      gnus-summary-post-menu gnus-summary-mode-map ""
1924      `("Post"
1925        ["Post an article" gnus-summary-post-news
1926         ,@(if (featurep 'xemacs) '(t)
1927             '(:help "Post an article"))]
1928        ["Followup" gnus-summary-followup
1929         ,@(if (featurep 'xemacs) '(t)
1930             '(:help "Post followup to this article"))]
1931        ["Followup and yank" gnus-summary-followup-with-original
1932         ,@(if (featurep 'xemacs) '(t)
1933             '(:help "Post followup to this article, quoting its contents"))]
1934        ["Supersede article" gnus-summary-supersede-article t]
1935        ["Cancel article" gnus-summary-cancel-article
1936         ,@(if (featurep 'xemacs) '(t)
1937             '(:help "Cancel an article you posted"))]
1938        ["Reply" gnus-summary-reply t]
1939        ["Reply and yank" gnus-summary-reply-with-original t]
1940        ["Wide reply" gnus-summary-wide-reply t]
1941        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1942         ,@(if (featurep 'xemacs) '(t)
1943             '(:help "Mail a reply, quoting this article"))]
1944        ["Mail forward" gnus-summary-mail-forward t]
1945        ["Post forward" gnus-summary-post-forward t]
1946        ["Digest and mail" gnus-uu-digest-mail-forward t]
1947        ["Digest and post" gnus-uu-digest-post-forward t]
1948        ["Resend message" gnus-summary-resend-message t]
1949        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1950        ["Send a mail" gnus-summary-mail-other-window t]
1951        ["Uuencode and post" gnus-uu-post-news
1952         ,@(if (featurep 'xemacs) '(t)
1953             '(:help "Post a uuencoded article"))]
1954        ["Followup via news" gnus-summary-followup-to-mail t]
1955        ["Followup via news and yank"
1956         gnus-summary-followup-to-mail-with-original t]
1957        ;;("Draft"
1958        ;;["Send" gnus-summary-send-draft t]
1959        ;;["Send bounced" gnus-resend-bounced-mail t])
1960        ))
1961
1962     (easy-menu-define
1963      gnus-summary-misc-menu gnus-summary-mode-map ""
1964      `("Misc"
1965        ("Mark Read"
1966         ["Mark as read" gnus-summary-mark-as-read-forward t]
1967         ["Mark same subject and select"
1968          gnus-summary-kill-same-subject-and-select t]
1969         ["Mark same subject" gnus-summary-kill-same-subject t]
1970         ["Catchup" gnus-summary-catchup
1971          ,@(if (featurep 'xemacs) '(t)
1972              '(:help "Mark unread articles in this group as read"))]
1973         ["Catchup all" gnus-summary-catchup-all t]
1974         ["Catchup to here" gnus-summary-catchup-to-here t]
1975         ["Catchup region" gnus-summary-mark-region-as-read t]
1976         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1977        ("Mark Various"
1978         ["Tick" gnus-summary-tick-article-forward t]
1979         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1980         ["Remove marks" gnus-summary-clear-mark-forward t]
1981         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1982         ["Set bookmark" gnus-summary-set-bookmark t]
1983         ["Remove bookmark" gnus-summary-remove-bookmark t])
1984        ("Mark Limit"
1985         ["Marks..." gnus-summary-limit-to-marks t]
1986         ["Subject..." gnus-summary-limit-to-subject t]
1987         ["Author..." gnus-summary-limit-to-author t]
1988         ["Age..." gnus-summary-limit-to-age t]
1989         ["Extra..." gnus-summary-limit-to-extra t]
1990         ["Score" gnus-summary-limit-to-score t]
1991         ["Unread" gnus-summary-limit-to-unread t]
1992         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1993         ["Articles" gnus-summary-limit-to-articles t]
1994         ["Pop limit" gnus-summary-pop-limit t]
1995         ["Show dormant" gnus-summary-limit-include-dormant t]
1996         ["Hide childless dormant"
1997          gnus-summary-limit-exclude-childless-dormant t]
1998         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1999         ["Hide marked" gnus-summary-limit-exclude-marks t]
2000         ["Show expunged" gnus-summary-show-all-expunged t])
2001        ("Process Mark"
2002         ["Set mark" gnus-summary-mark-as-processable t]
2003         ["Remove mark" gnus-summary-unmark-as-processable t]
2004         ["Remove all marks" gnus-summary-unmark-all-processable t]
2005         ["Mark above" gnus-uu-mark-over t]
2006         ["Mark series" gnus-uu-mark-series t]
2007         ["Mark region" gnus-uu-mark-region t]
2008         ["Unmark region" gnus-uu-unmark-region t]
2009         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2010         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2011         ["Mark all" gnus-uu-mark-all t]
2012         ["Mark buffer" gnus-uu-mark-buffer t]
2013         ["Mark sparse" gnus-uu-mark-sparse t]
2014         ["Mark thread" gnus-uu-mark-thread t]
2015         ["Unmark thread" gnus-uu-unmark-thread t]
2016         ("Process Mark Sets"
2017          ["Kill" gnus-summary-kill-process-mark t]
2018          ["Yank" gnus-summary-yank-process-mark
2019           gnus-newsgroup-process-stack]
2020          ["Save" gnus-summary-save-process-mark t]))
2021        ("Scroll article"
2022         ["Page forward" gnus-summary-next-page
2023          ,@(if (featurep 'xemacs) '(t)
2024              '(:help "Show next page of article"))]
2025         ["Page backward" gnus-summary-prev-page
2026          ,@(if (featurep 'xemacs) '(t)
2027              '(:help "Show previous page of article"))]
2028         ["Line forward" gnus-summary-scroll-up t])
2029        ("Move"
2030         ["Next unread article" gnus-summary-next-unread-article t]
2031         ["Previous unread article" gnus-summary-prev-unread-article t]
2032         ["Next article" gnus-summary-next-article t]
2033         ["Previous article" gnus-summary-prev-article t]
2034         ["Next unread subject" gnus-summary-next-unread-subject t]
2035         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2036         ["Next article same subject" gnus-summary-next-same-subject t]
2037         ["Previous article same subject" gnus-summary-prev-same-subject t]
2038         ["First unread article" gnus-summary-first-unread-article t]
2039         ["Best unread article" gnus-summary-best-unread-article t]
2040         ["Go to subject number..." gnus-summary-goto-subject t]
2041         ["Go to article number..." gnus-summary-goto-article t]
2042         ["Go to the last article" gnus-summary-goto-last-article t]
2043         ["Pop article off history" gnus-summary-pop-article t])
2044        ("Sort"
2045         ["Sort by number" gnus-summary-sort-by-number t]
2046         ["Sort by author" gnus-summary-sort-by-author t]
2047         ["Sort by subject" gnus-summary-sort-by-subject t]
2048         ["Sort by date" gnus-summary-sort-by-date t]
2049         ["Sort by score" gnus-summary-sort-by-score t]
2050         ["Sort by lines" gnus-summary-sort-by-lines t]
2051         ["Sort by characters" gnus-summary-sort-by-chars t])
2052        ("Help"
2053         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2054         ["Describe group" gnus-summary-describe-group t]
2055         ["Read manual" gnus-info-find-node t])
2056        ("Modes"
2057         ["Pick and read" gnus-pick-mode t]
2058         ["Binary" gnus-binary-mode t])
2059        ("Regeneration"
2060         ["Regenerate" gnus-summary-prepare t]
2061         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2062         ["Toggle threading" gnus-summary-toggle-threads t])
2063        ["Filter articles..." gnus-summary-execute-command t]
2064        ["Run command on subjects..." gnus-summary-universal-argument t]
2065        ["Search articles forward..." gnus-summary-search-article-forward t]
2066        ["Search articles backward..." gnus-summary-search-article-backward t]
2067        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2068        ["Expand window" gnus-summary-expand-window t]
2069        ["Expire expirable articles" gnus-summary-expire-articles
2070         (gnus-check-backend-function
2071          'request-expire-articles gnus-newsgroup-name)]
2072        ["Edit local kill file" gnus-summary-edit-local-kill t]
2073        ["Edit main kill file" gnus-summary-edit-global-kill t]
2074        ["Edit group parameters" gnus-summary-edit-parameters t]
2075        ["Customize group parameters" gnus-summary-customize-parameters t]
2076        ["Send a bug report" gnus-bug t]
2077        ("Exit"
2078         ["Catchup and exit" gnus-summary-catchup-and-exit
2079          ,@(if (featurep 'xemacs) '(t)
2080              '(:help "Mark unread articles in this group as read, then exit"))]
2081         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2082         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2083         ["Exit group" gnus-summary-exit
2084          ,@(if (featurep 'xemacs) '(t)
2085              '(:help "Exit current group, return to group selection mode"))]
2086         ["Exit group without updating" gnus-summary-exit-no-update t]
2087         ["Exit and goto next group" gnus-summary-next-group t]
2088         ["Exit and goto prev group" gnus-summary-prev-group t]
2089         ["Reselect group" gnus-summary-reselect-current-group t]
2090         ["Rescan group" gnus-summary-rescan-group t]
2091         ["Update dribble" gnus-summary-save-newsrc t])))
2092
2093     (gnus-run-hooks 'gnus-summary-menu-hook)))
2094
2095 (defvar gnus-summary-tool-bar-map nil)
2096
2097 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2098 (defun gnus-summary-make-tool-bar ()
2099   (if (and (fboundp 'tool-bar-add-item-from-menu)
2100            (default-value 'tool-bar-mode)
2101            (not gnus-summary-tool-bar-map))
2102       (setq gnus-summary-tool-bar-map
2103             (let ((tool-bar-map (make-sparse-keymap))
2104                   (load-path (mm-image-load-path)))
2105               (tool-bar-add-item-from-menu
2106                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2107               (tool-bar-add-item-from-menu
2108                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2109               (tool-bar-add-item-from-menu
2110                'gnus-summary-post-news "post" gnus-summary-mode-map)
2111               (tool-bar-add-item-from-menu
2112                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2113               (tool-bar-add-item-from-menu
2114                'gnus-summary-followup "followup" gnus-summary-mode-map)
2115               (tool-bar-add-item-from-menu
2116                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2117               (tool-bar-add-item-from-menu
2118                'gnus-summary-reply "reply" gnus-summary-mode-map)
2119               (tool-bar-add-item-from-menu
2120                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2121               (tool-bar-add-item-from-menu
2122                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2123               (tool-bar-add-item-from-menu
2124                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2125               (tool-bar-add-item-from-menu
2126                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2127               (tool-bar-add-item-from-menu
2128                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2129               (tool-bar-add-item-from-menu
2130                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2131               (tool-bar-add-item-from-menu
2132                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2133               (tool-bar-add-item-from-menu
2134                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2135               tool-bar-map)))
2136   (if gnus-summary-tool-bar-map
2137       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2138
2139 (defun gnus-score-set-default (var value)
2140   "A version of set that updates the GNU Emacs menu-bar."
2141   (set var value)
2142   ;; It is the message that forces the active status to be updated.
2143   (message ""))
2144
2145 (defun gnus-make-score-map (type)
2146   "Make a summary score map of type TYPE."
2147   (if t
2148       nil
2149     (let ((headers '(("author" "from" string)
2150                      ("subject" "subject" string)
2151                      ("article body" "body" string)
2152                      ("article head" "head" string)
2153                      ("xref" "xref" string)
2154                      ("extra header" "extra" string)
2155                      ("lines" "lines" number)
2156                      ("followups to author" "followup" string)))
2157           (types '((number ("less than" <)
2158                            ("greater than" >)
2159                            ("equal" =))
2160                    (string ("substring" s)
2161                            ("exact string" e)
2162                            ("fuzzy string" f)
2163                            ("regexp" r))))
2164           (perms '(("temporary" (current-time-string))
2165                    ("permanent" nil)
2166                    ("immediate" now)))
2167           header)
2168       (list
2169        (apply
2170         'nconc
2171         (list
2172          (if (eq type 'lower)
2173              "Lower score"
2174            "Increase score"))
2175         (let (outh)
2176           (while headers
2177             (setq header (car headers))
2178             (setq outh
2179                   (cons
2180                    (apply
2181                     'nconc
2182                     (list (car header))
2183                     (let ((ts (cdr (assoc (nth 2 header) types)))
2184                           outt)
2185                       (while ts
2186                         (setq outt
2187                               (cons
2188                                (apply
2189                                 'nconc
2190                                 (list (caar ts))
2191                                 (let ((ps perms)
2192                                       outp)
2193                                   (while ps
2194                                     (setq outp
2195                                           (cons
2196                                            (vector
2197                                             (caar ps)
2198                                             (list
2199                                              'gnus-summary-score-entry
2200                                              (nth 1 header)
2201                                              (if (or (string= (nth 1 header)
2202                                                               "head")
2203                                                      (string= (nth 1 header)
2204                                                               "body"))
2205                                                  ""
2206                                                (list 'gnus-summary-header
2207                                                      (nth 1 header)))
2208                                              (list 'quote (nth 1 (car ts)))
2209                                              (list 'gnus-score-delta-default
2210                                                    nil)
2211                                              (nth 1 (car ps))
2212                                              t)
2213                                             t)
2214                                            outp))
2215                                     (setq ps (cdr ps)))
2216                                   (list (nreverse outp))))
2217                                outt))
2218                         (setq ts (cdr ts)))
2219                       (list (nreverse outt))))
2220                    outh))
2221             (setq headers (cdr headers)))
2222           (list (nreverse outh))))))))
2223
2224 \f
2225
2226 (defun gnus-summary-mode (&optional group)
2227   "Major mode for reading articles.
2228
2229 All normal editing commands are switched off.
2230 \\<gnus-summary-mode-map>
2231 Each line in this buffer represents one article.  To read an
2232 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2233 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2234 respectively.
2235
2236 You can also post articles and send mail from this buffer.  To
2237 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2238 of an article, type `\\[gnus-summary-reply]'.
2239
2240 There are approx. one gazillion commands you can execute in this
2241 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2242
2243 The following commands are available:
2244
2245 \\{gnus-summary-mode-map}"
2246   (interactive)
2247   (kill-all-local-variables)
2248   (when (gnus-visual-p 'summary-menu 'menu)
2249     (gnus-summary-make-menu-bar)
2250     (gnus-summary-make-tool-bar))
2251   (gnus-summary-make-local-variables)
2252   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2253     (gnus-summary-make-local-variables))
2254   (gnus-make-thread-indent-array)
2255   (gnus-simplify-mode-line)
2256   (setq major-mode 'gnus-summary-mode)
2257   (setq mode-name "Summary")
2258   (make-local-variable 'minor-mode-alist)
2259   (use-local-map gnus-summary-mode-map)
2260   (buffer-disable-undo)
2261   (setq buffer-read-only t)             ;Disable modification
2262   (setq truncate-lines t)
2263   (setq selective-display t)
2264   (setq selective-display-ellipses t)   ;Display `...'
2265   (gnus-summary-set-display-table)
2266   (gnus-set-default-directory)
2267   (setq gnus-newsgroup-name group)
2268   (make-local-variable 'gnus-summary-line-format)
2269   (make-local-variable 'gnus-summary-line-format-spec)
2270   (make-local-variable 'gnus-summary-dummy-line-format)
2271   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2272   (make-local-variable 'gnus-summary-mark-positions)
2273   (make-local-hook 'pre-command-hook)
2274   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2275   (gnus-run-hooks 'gnus-summary-mode-hook)
2276   (turn-on-gnus-mailing-list-mode)
2277   (mm-enable-multibyte-mule4)
2278   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2279   (gnus-update-summary-mark-positions))
2280
2281 (defun gnus-summary-make-local-variables ()
2282   "Make all the local summary buffer variables."
2283   (let (global)
2284     (dolist (local gnus-summary-local-variables)
2285       (if (consp local)
2286           (progn
2287             (if (eq (cdr local) 'global)
2288                 ;; Copy the global value of the variable.
2289                 (setq global (symbol-value (car local)))
2290               ;; Use the value from the list.
2291               (setq global (eval (cdr local))))
2292             (set (make-local-variable (car local)) global))
2293         ;; Simple nil-valued local variable.
2294         (set (make-local-variable local) nil)))))
2295
2296 (defun gnus-summary-clear-local-variables ()
2297   (let ((locals gnus-summary-local-variables))
2298     (while locals
2299       (if (consp (car locals))
2300           (and (vectorp (caar locals))
2301                (set (caar locals) nil))
2302         (and (vectorp (car locals))
2303              (set (car locals) nil)))
2304       (setq locals (cdr locals)))))
2305
2306 ;; Summary data functions.
2307
2308 (defmacro gnus-data-number (data)
2309   `(car ,data))
2310
2311 (defmacro gnus-data-set-number (data number)
2312   `(setcar ,data ,number))
2313
2314 (defmacro gnus-data-mark (data)
2315   `(nth 1 ,data))
2316
2317 (defmacro gnus-data-set-mark (data mark)
2318   `(setcar (nthcdr 1 ,data) ,mark))
2319
2320 (defmacro gnus-data-pos (data)
2321   `(nth 2 ,data))
2322
2323 (defmacro gnus-data-set-pos (data pos)
2324   `(setcar (nthcdr 2 ,data) ,pos))
2325
2326 (defmacro gnus-data-header (data)
2327   `(nth 3 ,data))
2328
2329 (defmacro gnus-data-set-header (data header)
2330   `(setf (nth 3 ,data) ,header))
2331
2332 (defmacro gnus-data-level (data)
2333   `(nth 4 ,data))
2334
2335 (defmacro gnus-data-unread-p (data)
2336   `(= (nth 1 ,data) gnus-unread-mark))
2337
2338 (defmacro gnus-data-read-p (data)
2339   `(/= (nth 1 ,data) gnus-unread-mark))
2340
2341 (defmacro gnus-data-pseudo-p (data)
2342   `(consp (nth 3 ,data)))
2343
2344 (defmacro gnus-data-find (number)
2345   `(assq ,number gnus-newsgroup-data))
2346
2347 (defmacro gnus-data-find-list (number &optional data)
2348   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2349      (memq (assq ,number bdata)
2350            bdata)))
2351
2352 (defmacro gnus-data-make (number mark pos header level)
2353   `(list ,number ,mark ,pos ,header ,level))
2354
2355 (defun gnus-data-enter (after-article number mark pos header level offset)
2356   (let ((data (gnus-data-find-list after-article)))
2357     (unless data
2358       (error "No such article: %d" after-article))
2359     (setcdr data (cons (gnus-data-make number mark pos header level)
2360                        (cdr data)))
2361     (setq gnus-newsgroup-data-reverse nil)
2362     (gnus-data-update-list (cddr data) offset)))
2363
2364 (defun gnus-data-enter-list (after-article list &optional offset)
2365   (when list
2366     (let ((data (and after-article (gnus-data-find-list after-article)))
2367           (ilist list))
2368       (if (not (or data
2369                    after-article))
2370           (let ((odata gnus-newsgroup-data))
2371             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2372             (when offset
2373               (gnus-data-update-list odata offset)))
2374         ;; Find the last element in the list to be spliced into the main
2375         ;; list.
2376         (while (cdr list)
2377           (setq list (cdr list)))
2378         (if (not data)
2379             (progn
2380               (setcdr list gnus-newsgroup-data)
2381               (setq gnus-newsgroup-data ilist)
2382               (when offset
2383                 (gnus-data-update-list (cdr list) offset)))
2384           (setcdr list (cdr data))
2385           (setcdr data ilist)
2386           (when offset
2387             (gnus-data-update-list (cdr list) offset))))
2388       (setq gnus-newsgroup-data-reverse nil))))
2389
2390 (defun gnus-data-remove (article &optional offset)
2391   (let ((data gnus-newsgroup-data))
2392     (if (= (gnus-data-number (car data)) article)
2393         (progn
2394           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2395                 gnus-newsgroup-data-reverse nil)
2396           (when offset
2397             (gnus-data-update-list gnus-newsgroup-data offset)))
2398       (while (cdr data)
2399         (when (= (gnus-data-number (cadr data)) article)
2400           (setcdr data (cddr data))
2401           (when offset
2402             (gnus-data-update-list (cdr data) offset))
2403           (setq data nil
2404                 gnus-newsgroup-data-reverse nil))
2405         (setq data (cdr data))))))
2406
2407 (defmacro gnus-data-list (backward)
2408   `(if ,backward
2409        (or gnus-newsgroup-data-reverse
2410            (setq gnus-newsgroup-data-reverse
2411                  (reverse gnus-newsgroup-data)))
2412      gnus-newsgroup-data))
2413
2414 (defun gnus-data-update-list (data offset)
2415   "Add OFFSET to the POS of all data entries in DATA."
2416   (setq gnus-newsgroup-data-reverse nil)
2417   (while data
2418     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2419     (setq data (cdr data))))
2420
2421 (defun gnus-summary-article-pseudo-p (article)
2422   "Say whether this article is a pseudo article or not."
2423   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2424
2425 (defmacro gnus-summary-article-sparse-p (article)
2426   "Say whether this article is a sparse article or not."
2427   `(memq ,article gnus-newsgroup-sparse))
2428
2429 (defmacro gnus-summary-article-ancient-p (article)
2430   "Say whether this article is a sparse article or not."
2431   `(memq ,article gnus-newsgroup-ancient))
2432
2433 (defun gnus-article-parent-p (number)
2434   "Say whether this article is a parent or not."
2435   (let ((data (gnus-data-find-list number)))
2436     (and (cdr data)                     ; There has to be an article after...
2437          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2438             (gnus-data-level (nth 1 data))))))
2439
2440 (defun gnus-article-children (number)
2441   "Return a list of all children to NUMBER."
2442   (let* ((data (gnus-data-find-list number))
2443          (level (gnus-data-level (car data)))
2444          children)
2445     (setq data (cdr data))
2446     (while (and data
2447                 (= (gnus-data-level (car data)) (1+ level)))
2448       (push (gnus-data-number (car data)) children)
2449       (setq data (cdr data)))
2450     children))
2451
2452 (defmacro gnus-summary-skip-intangible ()
2453   "If the current article is intangible, then jump to a different article."
2454   '(let ((to (get-text-property (point) 'gnus-intangible)))
2455      (and to (gnus-summary-goto-subject to))))
2456
2457 (defmacro gnus-summary-article-intangible-p ()
2458   "Say whether this article is intangible or not."
2459   '(get-text-property (point) 'gnus-intangible))
2460
2461 (defun gnus-article-read-p (article)
2462   "Say whether ARTICLE is read or not."
2463   (not (or (memq article gnus-newsgroup-marked)
2464            (memq article gnus-newsgroup-unreads)
2465            (memq article gnus-newsgroup-unselected)
2466            (memq article gnus-newsgroup-dormant))))
2467
2468 ;; Some summary mode macros.
2469
2470 (defmacro gnus-summary-article-number ()
2471   "The article number of the article on the current line.
2472 If there isn's an article number here, then we return the current
2473 article number."
2474   '(progn
2475      (gnus-summary-skip-intangible)
2476      (or (get-text-property (point) 'gnus-number)
2477          (gnus-summary-last-subject))))
2478
2479 (defmacro gnus-summary-article-header (&optional number)
2480   "Return the header of article NUMBER."
2481   `(gnus-data-header (gnus-data-find
2482                       ,(or number '(gnus-summary-article-number)))))
2483
2484 (defmacro gnus-summary-thread-level (&optional number)
2485   "Return the level of thread that starts with article NUMBER."
2486   `(if (and (eq gnus-summary-make-false-root 'dummy)
2487             (get-text-property (point) 'gnus-intangible))
2488        0
2489      (gnus-data-level (gnus-data-find
2490                        ,(or number '(gnus-summary-article-number))))))
2491
2492 (defmacro gnus-summary-article-mark (&optional number)
2493   "Return the mark of article NUMBER."
2494   `(gnus-data-mark (gnus-data-find
2495                     ,(or number '(gnus-summary-article-number)))))
2496
2497 (defmacro gnus-summary-article-pos (&optional number)
2498   "Return the position of the line of article NUMBER."
2499   `(gnus-data-pos (gnus-data-find
2500                    ,(or number '(gnus-summary-article-number)))))
2501
2502 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2503 (defmacro gnus-summary-article-subject (&optional number)
2504   "Return current subject string or nil if nothing."
2505   `(let ((headers
2506           ,(if number
2507                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2508              '(gnus-data-header (assq (gnus-summary-article-number)
2509                                       gnus-newsgroup-data)))))
2510      (and headers
2511           (vectorp headers)
2512           (mail-header-subject headers))))
2513
2514 (defmacro gnus-summary-article-score (&optional number)
2515   "Return current article score."
2516   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2517                   gnus-newsgroup-scored))
2518        gnus-summary-default-score 0))
2519
2520 (defun gnus-summary-article-children (&optional number)
2521   "Return a list of article numbers that are children of article NUMBER."
2522   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2523          (level (gnus-data-level (car data)))
2524          l children)
2525     (while (and (setq data (cdr data))
2526                 (> (setq l (gnus-data-level (car data))) level))
2527       (and (= (1+ level) l)
2528            (push (gnus-data-number (car data))
2529                  children)))
2530     (nreverse children)))
2531
2532 (defun gnus-summary-article-parent (&optional number)
2533   "Return the article number of the parent of article NUMBER."
2534   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2535                                     (gnus-data-list t)))
2536          (level (gnus-data-level (car data))))
2537     (if (zerop level)
2538         ()                              ; This is a root.
2539       ;; We search until we find an article with a level less than
2540       ;; this one.  That function has to be the parent.
2541       (while (and (setq data (cdr data))
2542                   (not (< (gnus-data-level (car data)) level))))
2543       (and data (gnus-data-number (car data))))))
2544
2545 (defun gnus-unread-mark-p (mark)
2546   "Say whether MARK is the unread mark."
2547   (= mark gnus-unread-mark))
2548
2549 (defun gnus-read-mark-p (mark)
2550   "Say whether MARK is one of the marks that mark as read.
2551 This is all marks except unread, ticked, dormant, and expirable."
2552   (not (or (= mark gnus-unread-mark)
2553            (= mark gnus-ticked-mark)
2554            (= mark gnus-dormant-mark)
2555            (= mark gnus-expirable-mark))))
2556
2557 (defmacro gnus-article-mark (number)
2558   "Return the MARK of article NUMBER.
2559 This macro should only be used when computing the mark the \"first\"
2560 time; i.e., when generating the summary lines.  After that,
2561 `gnus-summary-article-mark' should be used to examine the
2562 marks of articles."
2563   `(cond
2564     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2565     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2566     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2567     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2568     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2569     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2570     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2571     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2572            gnus-ancient-mark))))
2573
2574 ;; Saving hidden threads.
2575
2576 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2577 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2578
2579 (defmacro gnus-save-hidden-threads (&rest forms)
2580   "Save hidden threads, eval FORMS, and restore the hidden threads."
2581   (let ((config (make-symbol "config")))
2582     `(let ((,config (gnus-hidden-threads-configuration)))
2583        (unwind-protect
2584            (save-excursion
2585              ,@forms)
2586          (gnus-restore-hidden-threads-configuration ,config)))))
2587
2588 (defun gnus-data-compute-positions ()
2589   "Compute the positions of all articles."
2590   (setq gnus-newsgroup-data-reverse nil)
2591   (let ((data gnus-newsgroup-data))
2592     (save-excursion
2593       (gnus-save-hidden-threads
2594         (gnus-summary-show-all-threads)
2595         (goto-char (point-min))
2596         (while data
2597           (while (get-text-property (point) 'gnus-intangible)
2598             (forward-line 1))
2599           (gnus-data-set-pos (car data) (+ (point) 3))
2600           (setq data (cdr data))
2601           (forward-line 1))))))
2602
2603 (defun gnus-hidden-threads-configuration ()
2604   "Return the current hidden threads configuration."
2605   (save-excursion
2606     (let (config)
2607       (goto-char (point-min))
2608       (while (search-forward "\r" nil t)
2609         (push (1- (point)) config))
2610       config)))
2611
2612 (defun gnus-restore-hidden-threads-configuration (config)
2613   "Restore hidden threads configuration from CONFIG."
2614   (save-excursion
2615     (let (point buffer-read-only)
2616       (while (setq point (pop config))
2617         (when (and (< point (point-max))
2618                    (goto-char point)
2619                    (eq (char-after) ?\n))
2620           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2621
2622 ;; Various summary mode internalish functions.
2623
2624 (defun gnus-mouse-pick-article (e)
2625   (interactive "e")
2626   (mouse-set-point e)
2627   (gnus-summary-next-page nil t))
2628
2629 (defun gnus-summary-set-display-table ()
2630   "Change the display table.
2631 Odd characters have a tendency to mess
2632 up nicely formatted displays - we make all possible glyphs
2633 display only a single character."
2634
2635   ;; We start from the standard display table, if any.
2636   (let ((table (or (copy-sequence standard-display-table)
2637                    (make-display-table)))
2638         (i 32))
2639     ;; Nix out all the control chars...
2640     (while (>= (setq i (1- i)) 0)
2641       (aset table i [??]))
2642     ;; ... but not newline and cr, of course.  (cr is necessary for the
2643     ;; selective display).
2644     (aset table ?\n nil)
2645     (aset table ?\r nil)
2646     ;; We keep TAB as well.
2647     (aset table ?\t nil)
2648     ;; We nix out any glyphs over 126 that are not set already.
2649     (let ((i 256))
2650       (while (>= (setq i (1- i)) 127)
2651         ;; Only modify if the entry is nil.
2652         (unless (aref table i)
2653           (aset table i [??]))))
2654     (setq buffer-display-table table)))
2655
2656 (defun gnus-summary-buffer-name (group)
2657   "Return the summary buffer name of GROUP."
2658   (concat "*Summary " group "*"))
2659
2660 (defun gnus-summary-setup-buffer (group)
2661   "Initialize summary buffer."
2662   (let ((buffer (gnus-summary-buffer-name group)))
2663     (if (get-buffer buffer)
2664         (progn
2665           (set-buffer buffer)
2666           (setq gnus-summary-buffer (current-buffer))
2667           (not gnus-newsgroup-prepared))
2668       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2669       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2670       (gnus-summary-mode group)
2671       (when gnus-carpal
2672         (gnus-carpal-setup-buffer 'summary))
2673       (unless gnus-single-article-buffer
2674         (make-local-variable 'gnus-article-buffer)
2675         (make-local-variable 'gnus-article-current)
2676         (make-local-variable 'gnus-original-article-buffer))
2677       (setq gnus-newsgroup-name group)
2678       ;; Set any local variables in the group parameters.
2679       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2680       t)))
2681
2682 (defun gnus-set-global-variables ()
2683   "Set the global equivalents of the buffer-local variables.
2684 They are set to the latest values they had.  These reflect the summary
2685 buffer that was in action when the last article was fetched."
2686   (when (eq major-mode 'gnus-summary-mode)
2687     (setq gnus-summary-buffer (current-buffer))
2688     (let ((name gnus-newsgroup-name)
2689           (marked gnus-newsgroup-marked)
2690           (unread gnus-newsgroup-unreads)
2691           (headers gnus-current-headers)
2692           (data gnus-newsgroup-data)
2693           (summary gnus-summary-buffer)
2694           (article-buffer gnus-article-buffer)
2695           (original gnus-original-article-buffer)
2696           (gac gnus-article-current)
2697           (reffed gnus-reffed-article-number)
2698           (score-file gnus-current-score-file)
2699           (default-charset gnus-newsgroup-charset)
2700           vlist)
2701       (let ((locals gnus-newsgroup-variables))
2702         (while locals
2703           (if (consp (car locals))
2704               (push (eval (caar locals)) vlist)
2705             (push (eval (car locals)) vlist))
2706           (setq locals (cdr locals)))
2707         (setq vlist (nreverse vlist)))
2708       (save-excursion
2709         (set-buffer gnus-group-buffer)
2710         (setq gnus-newsgroup-name name
2711               gnus-newsgroup-marked marked
2712               gnus-newsgroup-unreads unread
2713               gnus-current-headers headers
2714               gnus-newsgroup-data data
2715               gnus-article-current gac
2716               gnus-summary-buffer summary
2717               gnus-article-buffer article-buffer
2718               gnus-original-article-buffer original
2719               gnus-reffed-article-number reffed
2720               gnus-current-score-file score-file
2721               gnus-newsgroup-charset default-charset)
2722         (let ((locals gnus-newsgroup-variables))
2723           (while locals
2724             (if (consp (car locals))
2725                 (set (caar locals) (pop vlist))
2726               (set (car locals) (pop vlist)))
2727             (setq locals (cdr locals))))
2728         ;; The article buffer also has local variables.
2729         (when (gnus-buffer-live-p gnus-article-buffer)
2730           (set-buffer gnus-article-buffer)
2731           (setq gnus-summary-buffer summary))))))
2732
2733 (defun gnus-summary-article-unread-p (article)
2734   "Say whether ARTICLE is unread or not."
2735   (memq article gnus-newsgroup-unreads))
2736
2737 (defun gnus-summary-first-article-p (&optional article)
2738   "Return whether ARTICLE is the first article in the buffer."
2739   (if (not (setq article (or article (gnus-summary-article-number))))
2740       nil
2741     (eq article (caar gnus-newsgroup-data))))
2742
2743 (defun gnus-summary-last-article-p (&optional article)
2744   "Return whether ARTICLE is the last article in the buffer."
2745   (if (not (setq article (or article (gnus-summary-article-number))))
2746       ;; All non-existent numbers are the last article.  :-)
2747       t
2748     (not (cdr (gnus-data-find-list article)))))
2749
2750 (defun gnus-make-thread-indent-array ()
2751   (let ((n 200))
2752     (unless (and gnus-thread-indent-array
2753                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2754       (setq gnus-thread-indent-array (make-vector 201 "")
2755             gnus-thread-indent-array-level gnus-thread-indent-level)
2756       (while (>= n 0)
2757         (aset gnus-thread-indent-array n
2758               (make-string (* n gnus-thread-indent-level) ? ))
2759         (setq n (1- n))))))
2760
2761 (defun gnus-update-summary-mark-positions ()
2762   "Compute where the summary marks are to go."
2763   (save-excursion
2764     (when (gnus-buffer-exists-p gnus-summary-buffer)
2765       (set-buffer gnus-summary-buffer))
2766     (let ((gnus-replied-mark 129)
2767           (gnus-score-below-mark 130)
2768           (gnus-score-over-mark 130)
2769           (gnus-download-mark 131)
2770           (spec gnus-summary-line-format-spec)
2771           gnus-visual pos)
2772       (save-excursion
2773         (gnus-set-work-buffer)
2774         (let ((gnus-summary-line-format-spec spec)
2775               (gnus-newsgroup-downloadable '((0 . t))))
2776           (gnus-summary-insert-line
2777            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2778           (goto-char (point-min))
2779           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2780                                              (- (point) 2)))))
2781           (goto-char (point-min))
2782           (push (cons 'replied (and (search-forward "\201" nil t)
2783                                     (- (point) 2)))
2784                 pos)
2785           (goto-char (point-min))
2786           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2787                 pos)
2788           (goto-char (point-min))
2789           (push (cons 'download
2790                       (and (search-forward "\203" nil t) (- (point) 2)))
2791                 pos)))
2792       (setq gnus-summary-mark-positions pos))))
2793
2794 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2795   "Insert a dummy root in the summary buffer."
2796   (beginning-of-line)
2797   (gnus-add-text-properties
2798    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2799    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2800
2801 (defun gnus-summary-from-or-to-or-newsgroups (header)
2802   (let ((to (cdr (assq 'To (mail-header-extra header))))
2803         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2804         (mail-parse-charset gnus-newsgroup-charset)
2805         (mail-parse-ignored-charsets
2806          (save-excursion (set-buffer gnus-summary-buffer)
2807                          gnus-newsgroup-ignored-charsets)))
2808     (cond
2809      ((and to
2810            gnus-ignored-from-addresses
2811            (string-match gnus-ignored-from-addresses
2812                          (mail-header-from header)))
2813       (concat "-> "
2814               (or (car (funcall gnus-extract-address-components
2815                                 (funcall
2816                                  gnus-decode-encoded-word-function to)))
2817                   (funcall gnus-decode-encoded-word-function to))))
2818      ((and newsgroups
2819            gnus-ignored-from-addresses
2820            (string-match gnus-ignored-from-addresses
2821                          (mail-header-from header)))
2822       (concat "=> " newsgroups))
2823      (t
2824       (or (car (funcall gnus-extract-address-components
2825                         (mail-header-from header)))
2826           (mail-header-from header))))))
2827
2828 (defun gnus-summary-insert-line (gnus-tmp-header
2829                                  gnus-tmp-level gnus-tmp-current
2830                                  gnus-tmp-unread gnus-tmp-replied
2831                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2832                                  &optional gnus-tmp-dummy gnus-tmp-score
2833                                  gnus-tmp-process)
2834   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2835          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2836          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2837          (gnus-tmp-score-char
2838           (if (or (null gnus-summary-default-score)
2839                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2840                       gnus-summary-zcore-fuzz))
2841               ?  ;Whitespace
2842             (if (< gnus-tmp-score gnus-summary-default-score)
2843                 gnus-score-below-mark gnus-score-over-mark)))
2844          (gnus-tmp-replied
2845           (cond (gnus-tmp-process gnus-process-mark)
2846                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2847                  gnus-cached-mark)
2848                 (gnus-tmp-replied gnus-replied-mark)
2849                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2850                  gnus-saved-mark)
2851                 (t gnus-no-mark)))
2852          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2853          (gnus-tmp-name
2854           (cond
2855            ((string-match "<[^>]+> *$" gnus-tmp-from)
2856             (let ((beg (match-beginning 0)))
2857               (or (and (string-match "^\".+\"" gnus-tmp-from)
2858                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2859                   (substring gnus-tmp-from 0 beg))))
2860            ((string-match "(.+)" gnus-tmp-from)
2861             (substring gnus-tmp-from
2862                        (1+ (match-beginning 0)) (1- (match-end 0))))
2863            (t gnus-tmp-from)))
2864          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2865          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2866          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2867          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2868          (buffer-read-only nil))
2869     (when (string= gnus-tmp-name "")
2870       (setq gnus-tmp-name gnus-tmp-from))
2871     (unless (numberp gnus-tmp-lines)
2872       (setq gnus-tmp-lines 0))
2873     (gnus-put-text-property
2874      (point)
2875      (progn (eval gnus-summary-line-format-spec) (point))
2876      'gnus-number gnus-tmp-number)
2877     (when (gnus-visual-p 'summary-highlight 'highlight)
2878       (forward-line -1)
2879       (gnus-run-hooks 'gnus-summary-update-hook)
2880       (forward-line 1))))
2881
2882 (defun gnus-summary-update-line (&optional dont-update)
2883   "Update summary line after change."
2884   (when (and gnus-summary-default-score
2885              (not gnus-summary-inhibit-highlight))
2886     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2887            (article (gnus-summary-article-number))
2888            (score (gnus-summary-article-score article)))
2889       (unless dont-update
2890         (if (and gnus-summary-mark-below
2891                  (< (gnus-summary-article-score)
2892                     gnus-summary-mark-below))
2893             ;; This article has a low score, so we mark it as read.
2894             (when (memq article gnus-newsgroup-unreads)
2895               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2896           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2897             ;; This article was previously marked as read on account
2898             ;; of a low score, but now it has risen, so we mark it as
2899             ;; unread.
2900             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2901         (gnus-summary-update-mark
2902          (if (or (null gnus-summary-default-score)
2903                  (<= (abs (- score gnus-summary-default-score))
2904                      gnus-summary-zcore-fuzz))
2905              ?  ;Whitespace
2906            (if (< score gnus-summary-default-score)
2907                gnus-score-below-mark gnus-score-over-mark))
2908          'score))
2909       ;; Do visual highlighting.
2910       (when (gnus-visual-p 'summary-highlight 'highlight)
2911         (gnus-run-hooks 'gnus-summary-update-hook)))))
2912
2913 (defvar gnus-tmp-new-adopts nil)
2914
2915 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2916   "Return the number of articles in THREAD.
2917 This may be 0 in some cases -- if none of the articles in
2918 the thread are to be displayed."
2919   (let* ((number
2920           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2921           (cond
2922            ((not (listp thread))
2923             1)
2924            ((and (consp thread) (cdr thread))
2925             (apply
2926              '+ 1 (mapcar
2927                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2928            ((null thread)
2929             1)
2930            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2931             1)
2932            (t 0))))
2933     (when (and level (zerop level) gnus-tmp-new-adopts)
2934       (incf number
2935             (apply '+ (mapcar
2936                        'gnus-summary-number-of-articles-in-thread
2937                        gnus-tmp-new-adopts))))
2938     (if char
2939         (if (> number 1) gnus-not-empty-thread-mark
2940           gnus-empty-thread-mark)
2941       number)))
2942
2943 (defun gnus-summary-set-local-parameters (group)
2944   "Go through the local params of GROUP and set all variable specs in that list."
2945   (let ((params (gnus-group-find-parameter group))
2946         elem)
2947     (while params
2948       (setq elem (car params)
2949             params (cdr params))
2950       (and (consp elem)                 ; Has to be a cons.
2951            (consp (cdr elem))           ; The cdr has to be a list.
2952            (symbolp (car elem))         ; Has to be a symbol in there.
2953            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2954            (ignore-errors               ; So we set it.
2955              (make-local-variable (car elem))
2956              (set (car elem) (eval (nth 1 elem))))))))
2957
2958 (defun gnus-summary-read-group (group &optional show-all no-article
2959                                       kill-buffer no-display backward
2960                                       select-articles)
2961   "Start reading news in newsgroup GROUP.
2962 If SHOW-ALL is non-nil, already read articles are also listed.
2963 If NO-ARTICLE is non-nil, no article is selected initially.
2964 If NO-DISPLAY, don't generate a summary buffer."
2965   (let (result)
2966     (while (and group
2967                 (null (setq result
2968                             (let ((gnus-auto-select-next nil))
2969                               (or (gnus-summary-read-group-1
2970                                    group show-all no-article
2971                                    kill-buffer no-display
2972                                    select-articles)
2973                                   (setq show-all nil
2974                                         select-articles nil)))))
2975                 (eq gnus-auto-select-next 'quietly))
2976       (set-buffer gnus-group-buffer)
2977       ;; The entry function called above goes to the next
2978       ;; group automatically, so we go two groups back
2979       ;; if we are searching for the previous group.
2980       (when backward
2981         (gnus-group-prev-unread-group 2))
2982       (if (not (equal group (gnus-group-group-name)))
2983           (setq group (gnus-group-group-name))
2984         (setq group nil)))
2985     result))
2986
2987 (defun gnus-summary-read-group-1 (group show-all no-article
2988                                         kill-buffer no-display
2989                                         &optional select-articles)
2990   ;; Killed foreign groups can't be entered.
2991   ;;  (when (and (not (gnus-group-native-p group))
2992   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
2993   ;;    (error "Dead non-native groups can't be entered"))
2994   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2995   (let* ((new-group (gnus-summary-setup-buffer group))
2996          (quit-config (gnus-group-quit-config group))
2997          (did-select (and new-group (gnus-select-newsgroup
2998                                      group show-all select-articles))))
2999     (cond
3000      ;; This summary buffer exists already, so we just select it.
3001      ((not new-group)
3002       (gnus-set-global-variables)
3003       (when kill-buffer
3004         (gnus-kill-or-deaden-summary kill-buffer))
3005       (gnus-configure-windows 'summary 'force)
3006       (gnus-set-mode-line 'summary)
3007       (gnus-summary-position-point)
3008       (message "")
3009       t)
3010      ;; We couldn't select this group.
3011      ((null did-select)
3012       (when (and (eq major-mode 'gnus-summary-mode)
3013                  (not (equal (current-buffer) kill-buffer)))
3014         (kill-buffer (current-buffer))
3015         (if (not quit-config)
3016             (progn
3017               ;; Update the info -- marks might need to be removed,
3018               ;; for instance.
3019               (gnus-summary-update-info)
3020               (set-buffer gnus-group-buffer)
3021               (gnus-group-jump-to-group group)
3022               (gnus-group-next-unread-group 1))
3023           (gnus-handle-ephemeral-exit quit-config)))
3024       (let ((grpinfo (gnus-get-info group)))
3025         (if (null (gnus-info-read grpinfo))
3026             (gnus-message 3 "Group %s contains no messages" group)
3027           (gnus-message 3 "Can't select group")))
3028       nil)
3029      ;; The user did a `C-g' while prompting for number of articles,
3030      ;; so we exit this group.
3031      ((eq did-select 'quit)
3032       (and (eq major-mode 'gnus-summary-mode)
3033            (not (equal (current-buffer) kill-buffer))
3034            (kill-buffer (current-buffer)))
3035       (when kill-buffer
3036         (gnus-kill-or-deaden-summary kill-buffer))
3037       (if (not quit-config)
3038           (progn
3039             (set-buffer gnus-group-buffer)
3040             (gnus-group-jump-to-group group)
3041             (gnus-group-next-unread-group 1)
3042             (gnus-configure-windows 'group 'force))
3043         (gnus-handle-ephemeral-exit quit-config))
3044       ;; Finally signal the quit.
3045       (signal 'quit nil))
3046      ;; The group was successfully selected.
3047      (t
3048       (gnus-set-global-variables)
3049       ;; Save the active value in effect when the group was entered.
3050       (setq gnus-newsgroup-active
3051             (gnus-copy-sequence
3052              (gnus-active gnus-newsgroup-name)))
3053       ;; You can change the summary buffer in some way with this hook.
3054       (gnus-run-hooks 'gnus-select-group-hook)
3055       (gnus-update-format-specifications
3056        nil 'summary 'summary-mode 'summary-dummy)
3057       (gnus-update-summary-mark-positions)
3058       ;; Do score processing.
3059       (when gnus-use-scoring
3060         (gnus-possibly-score-headers))
3061       ;; Check whether to fill in the gaps in the threads.
3062       (when gnus-build-sparse-threads
3063         (gnus-build-sparse-threads))
3064       ;; Find the initial limit.
3065       (if gnus-show-threads
3066           (if show-all
3067               (let ((gnus-newsgroup-dormant nil))
3068                 (gnus-summary-initial-limit show-all))
3069             (gnus-summary-initial-limit show-all))
3070         ;; When untreaded, all articles are always shown.
3071         (setq gnus-newsgroup-limit
3072               (mapcar
3073                (lambda (header) (mail-header-number header))
3074                gnus-newsgroup-headers)))
3075       ;; Generate the summary buffer.
3076       (unless no-display
3077         (gnus-summary-prepare))
3078       (when gnus-use-trees
3079         (gnus-tree-open group)
3080         (setq gnus-summary-highlight-line-function
3081               'gnus-tree-highlight-article))
3082       ;; If the summary buffer is empty, but there are some low-scored
3083       ;; articles or some excluded dormants, we include these in the
3084       ;; buffer.
3085       (when (and (zerop (buffer-size))
3086                  (not no-display))
3087         (cond (gnus-newsgroup-dormant
3088                (gnus-summary-limit-include-dormant))
3089               ((and gnus-newsgroup-scored show-all)
3090                (gnus-summary-limit-include-expunged t))))
3091       ;; Function `gnus-apply-kill-file' must be called in this hook.
3092       (gnus-run-hooks 'gnus-apply-kill-hook)
3093       (if (and (zerop (buffer-size))
3094                (not no-display))
3095           (progn
3096             ;; This newsgroup is empty.
3097             (gnus-summary-catchup-and-exit nil t)
3098             (gnus-message 6 "No unread news")
3099             (when kill-buffer
3100               (gnus-kill-or-deaden-summary kill-buffer))
3101             ;; Return nil from this function.
3102             nil)
3103         ;; Hide conversation thread subtrees.  We cannot do this in
3104         ;; gnus-summary-prepare-hook since kill processing may not
3105         ;; work with hidden articles.
3106         (and gnus-show-threads
3107              gnus-thread-hide-subtree
3108              (gnus-summary-hide-all-threads))
3109         (when kill-buffer
3110           (gnus-kill-or-deaden-summary kill-buffer))
3111         ;; Show first unread article if requested.
3112         (if (and (not no-article)
3113                  (not no-display)
3114                  gnus-newsgroup-unreads
3115                  gnus-auto-select-first)
3116             (progn
3117               (gnus-configure-windows 'summary)
3118               (cond
3119                ((eq gnus-auto-select-first 'best)
3120                 (gnus-summary-best-unread-article))
3121                ((eq gnus-auto-select-first t)
3122                 (gnus-summary-first-unread-article))
3123                ((gnus-functionp gnus-auto-select-first)
3124                 (funcall gnus-auto-select-first))))
3125           ;; Don't select any articles, just move point to the first
3126           ;; article in the group.
3127           (goto-char (point-min))
3128           (gnus-summary-position-point)
3129           (gnus-configure-windows 'summary 'force)
3130           (gnus-set-mode-line 'summary))
3131         (when (get-buffer-window gnus-group-buffer t)
3132           ;; Gotta use windows, because recenter does weird stuff if
3133           ;; the current buffer ain't the displayed window.
3134           (let ((owin (selected-window)))
3135             (select-window (get-buffer-window gnus-group-buffer t))
3136             (when (gnus-group-goto-group group)
3137               (recenter))
3138             (select-window owin)))
3139         ;; Mark this buffer as "prepared".
3140         (setq gnus-newsgroup-prepared t)
3141         (gnus-run-hooks 'gnus-summary-prepared-hook)
3142         t)))))
3143
3144 (defun gnus-summary-prepare ()
3145   "Generate the summary buffer."
3146   (interactive)
3147   (let ((buffer-read-only nil))
3148     (erase-buffer)
3149     (setq gnus-newsgroup-data nil
3150           gnus-newsgroup-data-reverse nil)
3151     (gnus-run-hooks 'gnus-summary-generate-hook)
3152     ;; Generate the buffer, either with threads or without.
3153     (when gnus-newsgroup-headers
3154       (gnus-summary-prepare-threads
3155        (if gnus-show-threads
3156            (gnus-sort-gathered-threads
3157             (funcall gnus-summary-thread-gathering-function
3158                      (gnus-sort-threads
3159                       (gnus-cut-threads (gnus-make-threads)))))
3160          ;; Unthreaded display.
3161          (gnus-sort-articles gnus-newsgroup-headers))))
3162     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3163     ;; Call hooks for modifying summary buffer.
3164     (goto-char (point-min))
3165     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3166
3167 (defsubst gnus-general-simplify-subject (subject)
3168   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3169   (setq subject
3170         (cond
3171          ;; Truncate the subject.
3172          (gnus-simplify-subject-functions
3173           (gnus-map-function gnus-simplify-subject-functions subject))
3174          ((numberp gnus-summary-gather-subject-limit)
3175           (setq subject (gnus-simplify-subject-re subject))
3176           (if (> (length subject) gnus-summary-gather-subject-limit)
3177               (substring subject 0 gnus-summary-gather-subject-limit)
3178             subject))
3179          ;; Fuzzily simplify it.
3180          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3181           (gnus-simplify-subject-fuzzy subject))
3182          ;; Just remove the leading "Re:".
3183          (t
3184           (gnus-simplify-subject-re subject))))
3185
3186   (if (and gnus-summary-gather-exclude-subject
3187            (string-match gnus-summary-gather-exclude-subject subject))
3188       nil                               ; This article shouldn't be gathered
3189     subject))
3190
3191 (defun gnus-summary-simplify-subject-query ()
3192   "Query where the respool algorithm would put this article."
3193   (interactive)
3194   (gnus-summary-select-article)
3195   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3196
3197 (defun gnus-gather-threads-by-subject (threads)
3198   "Gather threads by looking at Subject headers."
3199   (if (not gnus-summary-make-false-root)
3200       threads
3201     (let ((hashtb (gnus-make-hashtable 1024))
3202           (prev threads)
3203           (result threads)
3204           subject hthread whole-subject)
3205       (while threads
3206         (setq subject (gnus-general-simplify-subject
3207                        (setq whole-subject (mail-header-subject
3208                                             (caar threads)))))
3209         (when subject
3210           (if (setq hthread (gnus-gethash subject hashtb))
3211               (progn
3212                 ;; We enter a dummy root into the thread, if we
3213                 ;; haven't done that already.
3214                 (unless (stringp (caar hthread))
3215                   (setcar hthread (list whole-subject (car hthread))))
3216                 ;; We add this new gathered thread to this gathered
3217                 ;; thread.
3218                 (setcdr (car hthread)
3219                         (nconc (cdar hthread) (list (car threads))))
3220                 ;; Remove it from the list of threads.
3221                 (setcdr prev (cdr threads))
3222                 (setq threads prev))
3223             ;; Enter this thread into the hash table.
3224             (gnus-sethash subject threads hashtb)))
3225         (setq prev threads)
3226         (setq threads (cdr threads)))
3227       result)))
3228
3229 (defun gnus-gather-threads-by-references (threads)
3230   "Gather threads by looking at References headers."
3231   (let ((idhashtb (gnus-make-hashtable 1024))
3232         (thhashtb (gnus-make-hashtable 1024))
3233         (prev threads)
3234         (result threads)
3235         ids references id gthread gid entered ref)
3236     (while threads
3237       (when (setq references (mail-header-references (caar threads)))
3238         (setq id (mail-header-id (caar threads))
3239               ids (gnus-split-references references)
3240               entered nil)
3241         (while (setq ref (pop ids))
3242           (setq ids (delete ref ids))
3243           (if (not (setq gid (gnus-gethash ref idhashtb)))
3244               (progn
3245                 (gnus-sethash ref id idhashtb)
3246                 (gnus-sethash id threads thhashtb))
3247             (setq gthread (gnus-gethash gid thhashtb))
3248             (unless entered
3249               ;; We enter a dummy root into the thread, if we
3250               ;; haven't done that already.
3251               (unless (stringp (caar gthread))
3252                 (setcar gthread (list (mail-header-subject (caar gthread))
3253                                       (car gthread))))
3254               ;; We add this new gathered thread to this gathered
3255               ;; thread.
3256               (setcdr (car gthread)
3257                       (nconc (cdar gthread) (list (car threads)))))
3258             ;; Add it into the thread hash table.
3259             (gnus-sethash id gthread thhashtb)
3260             (setq entered t)
3261             ;; Remove it from the list of threads.
3262             (setcdr prev (cdr threads))
3263             (setq threads prev))))
3264       (setq prev threads)
3265       (setq threads (cdr threads)))
3266     result))
3267
3268 (defun gnus-sort-gathered-threads (threads)
3269   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3270   (let ((result threads))
3271     (while threads
3272       (when (stringp (caar threads))
3273         (setcdr (car threads)
3274                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3275       (setq threads (cdr threads)))
3276     result))
3277
3278 (defun gnus-thread-loop-p (root thread)
3279   "Say whether ROOT is in THREAD."
3280   (let ((stack (list thread))
3281         (infloop 0)
3282         th)
3283     (while (setq thread (pop stack))
3284       (setq th (cdr thread))
3285       (while (and th
3286                   (not (eq (caar th) root)))
3287         (pop th))
3288       (if th
3289           ;; We have found a loop.
3290           (let (ref-dep)
3291             (setcdr thread (delq (car th) (cdr thread)))
3292             (if (boundp (setq ref-dep (intern "none"
3293                                               gnus-newsgroup-dependencies)))
3294                 (setcdr (symbol-value ref-dep)
3295                         (nconc (cdr (symbol-value ref-dep))
3296                                (list (car th))))
3297               (set ref-dep (list nil (car th))))
3298             (setq infloop 1
3299                   stack nil))
3300         ;; Push all the subthreads onto the stack.
3301         (push (cdr thread) stack)))
3302     infloop))
3303
3304 (defun gnus-make-threads ()
3305   "Go through the dependency hashtb and find the roots.  Return all threads."
3306   (let (threads)
3307     (while (catch 'infloop
3308              (mapatoms
3309               (lambda (refs)
3310                 ;; Deal with self-referencing References loops.
3311                 (when (and (car (symbol-value refs))
3312                            (not (zerop
3313                                  (apply
3314                                   '+
3315                                   (mapcar
3316                                    (lambda (thread)
3317                                      (gnus-thread-loop-p
3318                                       (car (symbol-value refs)) thread))
3319                                    (cdr (symbol-value refs)))))))
3320                   (setq threads nil)
3321                   (throw 'infloop t))
3322                 (unless (car (symbol-value refs))
3323                   ;; These threads do not refer back to any other articles,
3324                   ;; so they're roots.
3325                   (setq threads (append (cdr (symbol-value refs)) threads))))
3326               gnus-newsgroup-dependencies)))
3327     threads))
3328
3329 ;; Build the thread tree.
3330 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3331   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3332
3333 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3334 if it was already present.
3335
3336 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3337 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3338 Message-IDs will be renamed be renamed to a unique Message-ID before
3339 being entered.
3340
3341 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3342   (let* ((id (mail-header-id header))
3343          (id-dep (and id (intern id dependencies)))
3344          ref ref-dep ref-header)
3345     ;; Enter this `header' in the `dependencies' table.
3346     (cond
3347      ((not id-dep)
3348       (setq header nil))
3349      ;; The first two cases do the normal part: enter a new `header'
3350      ;; in the `dependencies' table.
3351      ((not (boundp id-dep))
3352       (set id-dep (list header)))
3353      ((null (car (symbol-value id-dep)))
3354       (setcar (symbol-value id-dep) header))
3355
3356      ;; From here the `header' was already present in the
3357      ;; `dependencies' table.
3358      (force-new
3359       ;; Overrides an existing entry;
3360       ;; just set the header part of the entry.
3361       (setcar (symbol-value id-dep) header))
3362
3363      ;; Renames the existing `header' to a unique Message-ID.
3364      ((not gnus-summary-ignore-duplicates)
3365       ;; An article with this Message-ID has already been seen.
3366       ;; We rename the Message-ID.
3367       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3368            (list header))
3369       (mail-header-set-id header id))
3370
3371      ;; The last case ignores an existing entry, except it adds any
3372      ;; additional Xrefs (in case the two articles came from different
3373      ;; servers.
3374      ;; Also sets `header' to `nil' meaning that the `dependencies'
3375      ;; table was *not* modified.
3376      (t
3377       (mail-header-set-xref
3378        (car (symbol-value id-dep))
3379        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3380                    "")
3381                (or (mail-header-xref header) "")))
3382       (setq header nil)))
3383
3384     (when header
3385       ;; First check if that we are not creating a References loop.
3386       (setq ref (gnus-parent-id (mail-header-references header)))
3387       (while (and ref
3388                   (setq ref-dep (intern-soft ref dependencies))
3389                   (boundp ref-dep)
3390                   (setq ref-header (car (symbol-value ref-dep))))
3391         (if (string= id ref)
3392             ;; Yuk!  This is a reference loop.  Make the article be a
3393             ;; root article.
3394             (progn
3395               (mail-header-set-references (car (symbol-value id-dep)) "none")
3396               (setq ref nil))
3397           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3398       (setq ref (gnus-parent-id (mail-header-references header)))
3399       (setq ref-dep (intern (or ref "none") dependencies))
3400       (if (boundp ref-dep)
3401           (setcdr (symbol-value ref-dep)
3402                   (nconc (cdr (symbol-value ref-dep))
3403                          (list (symbol-value id-dep))))
3404         (set ref-dep (list nil (symbol-value id-dep)))))
3405     header))
3406
3407 (defun gnus-build-sparse-threads ()
3408   (let ((headers gnus-newsgroup-headers)
3409         (mail-parse-charset gnus-newsgroup-charset)
3410         (gnus-summary-ignore-duplicates t)
3411         header references generation relations
3412         subject child end new-child date)
3413     ;; First we create an alist of generations/relations, where
3414     ;; generations is how much we trust the relation, and the relation
3415     ;; is parent/child.
3416     (gnus-message 7 "Making sparse threads...")
3417     (save-excursion
3418       (nnheader-set-temp-buffer " *gnus sparse threads*")
3419       (while (setq header (pop headers))
3420         (when (and (setq references (mail-header-references header))
3421                    (not (string= references "")))
3422           (insert references)
3423           (setq child (mail-header-id header)
3424                 subject (mail-header-subject header)
3425                 date (mail-header-date header)
3426                 generation 0)
3427           (while (search-backward ">" nil t)
3428             (setq end (1+ (point)))
3429             (when (search-backward "<" nil t)
3430               (setq new-child (buffer-substring (point) end))
3431               (push (list (incf generation)
3432                           child (setq child new-child)
3433                           subject date)
3434                     relations)))
3435           (when child
3436             (push (list (1+ generation) child nil subject) relations))
3437           (erase-buffer)))
3438       (kill-buffer (current-buffer)))
3439     ;; Sort over trustworthiness.
3440     (mapcar
3441      (lambda (relation)
3442        (when (gnus-dependencies-add-header
3443               (make-full-mail-header
3444                gnus-reffed-article-number
3445                (nth 3 relation) "" (or (nth 4 relation) "")
3446                (nth 1 relation)
3447                (or (nth 2 relation) "") 0 0 "")
3448               gnus-newsgroup-dependencies nil)
3449          (push gnus-reffed-article-number gnus-newsgroup-limit)
3450          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3451          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3452                gnus-newsgroup-reads)
3453          (decf gnus-reffed-article-number)))
3454      (sort relations 'car-less-than-car))
3455     (gnus-message 7 "Making sparse threads...done")))
3456
3457 (defun gnus-build-old-threads ()
3458   ;; Look at all the articles that refer back to old articles, and
3459   ;; fetch the headers for the articles that aren't there.  This will
3460   ;; build complete threads - if the roots haven't been expired by the
3461   ;; server, that is.
3462   (let ((mail-parse-charset gnus-newsgroup-charset)
3463         id heads)
3464     (mapatoms
3465      (lambda (refs)
3466        (when (not (car (symbol-value refs)))
3467          (setq heads (cdr (symbol-value refs)))
3468          (while heads
3469            (if (memq (mail-header-number (caar heads))
3470                      gnus-newsgroup-dormant)
3471                (setq heads (cdr heads))
3472              (setq id (symbol-name refs))
3473              (while (and (setq id (gnus-build-get-header id))
3474                          (not (car (gnus-id-to-thread id)))))
3475              (setq heads nil)))))
3476      gnus-newsgroup-dependencies)))
3477
3478 ;; This function has to be called with point after the article number
3479 ;; on the beginning of the line.
3480 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3481   (let ((eol (gnus-point-at-eol))
3482         (buffer (current-buffer))
3483         header)
3484
3485     ;; overview: [num subject from date id refs chars lines misc]
3486     (unwind-protect
3487         (progn
3488           (narrow-to-region (point) eol)
3489           (unless (eobp)
3490             (forward-char))
3491
3492           (setq header
3493                 (make-full-mail-header
3494                  number                 ; number
3495                  (funcall gnus-decode-encoded-word-function
3496                           (nnheader-nov-field)) ; subject
3497                  (funcall gnus-decode-encoded-word-function
3498                           (nnheader-nov-field)) ; from
3499                  (nnheader-nov-field)   ; date
3500                  (nnheader-nov-read-message-id) ; id
3501                  (nnheader-nov-field)   ; refs
3502                  (nnheader-nov-read-integer) ; chars
3503                  (nnheader-nov-read-integer) ; lines
3504                  (unless (eobp)
3505                    (if (looking-at "Xref: ")
3506                        (goto-char (match-end 0)))
3507                    (nnheader-nov-field)) ; Xref
3508                  (nnheader-nov-parse-extra)))) ; extra
3509
3510       (widen))
3511
3512     (when gnus-alter-header-function
3513       (funcall gnus-alter-header-function header))
3514     (gnus-dependencies-add-header header dependencies force-new)))
3515
3516 (defun gnus-build-get-header (id)
3517   "Look through the buffer of NOV lines and find the header to ID.
3518 Enter this line into the dependencies hash table, and return
3519 the id of the parent article (if any)."
3520   (let ((deps gnus-newsgroup-dependencies)
3521         found header)
3522     (prog1
3523         (save-excursion
3524           (set-buffer nntp-server-buffer)
3525           (let ((case-fold-search nil))
3526             (goto-char (point-min))
3527             (while (and (not found)
3528                         (search-forward id nil t))
3529               (beginning-of-line)
3530               (setq found (looking-at
3531                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3532                                    (regexp-quote id))))
3533               (or found (beginning-of-line 2)))
3534             (when found
3535               (beginning-of-line)
3536               (and
3537                (setq header (gnus-nov-parse-line
3538                              (read (current-buffer)) deps))
3539                (gnus-parent-id (mail-header-references header))))))
3540       (when header
3541         (let ((number (mail-header-number header)))
3542           (push number gnus-newsgroup-limit)
3543           (push header gnus-newsgroup-headers)
3544           (if (memq number gnus-newsgroup-unselected)
3545               (progn
3546                 (push number gnus-newsgroup-unreads)
3547                 (setq gnus-newsgroup-unselected
3548                       (delq number gnus-newsgroup-unselected)))
3549             (push number gnus-newsgroup-ancient)))))))
3550
3551 (defun gnus-build-all-threads ()
3552   "Read all the headers."
3553   (let ((gnus-summary-ignore-duplicates t)
3554         (mail-parse-charset gnus-newsgroup-charset)
3555         (dependencies gnus-newsgroup-dependencies)
3556         header article)
3557     (save-excursion
3558       (set-buffer nntp-server-buffer)
3559       (let ((case-fold-search nil))
3560         (goto-char (point-min))
3561         (while (not (eobp))
3562           (ignore-errors
3563             (setq article (read (current-buffer))
3564                   header (gnus-nov-parse-line article dependencies)))
3565           (when header
3566             (save-excursion
3567               (set-buffer gnus-summary-buffer)
3568               (push header gnus-newsgroup-headers)
3569               (if (memq (setq article (mail-header-number header))
3570                         gnus-newsgroup-unselected)
3571                   (progn
3572                     (push article gnus-newsgroup-unreads)
3573                     (setq gnus-newsgroup-unselected
3574                           (delq article gnus-newsgroup-unselected)))
3575                 (push article gnus-newsgroup-ancient)))
3576             (forward-line 1)))))))
3577
3578 (defun gnus-summary-update-article-line (article header)
3579   "Update the line for ARTICLE using HEADERS."
3580   (let* ((id (mail-header-id header))
3581          (thread (gnus-id-to-thread id)))
3582     (unless thread
3583       (error "Article in no thread"))
3584     ;; Update the thread.
3585     (setcar thread header)
3586     (gnus-summary-goto-subject article)
3587     (let* ((datal (gnus-data-find-list article))
3588            (data (car datal))
3589            (length (when (cdr datal)
3590                      (- (gnus-data-pos data)
3591                         (gnus-data-pos (cadr datal)))))
3592            (buffer-read-only nil)
3593            (level (gnus-summary-thread-level)))
3594       (gnus-delete-line)
3595       (gnus-summary-insert-line
3596        header level nil (gnus-article-mark article)
3597        (memq article gnus-newsgroup-replied)
3598        (memq article gnus-newsgroup-expirable)
3599        ;; Only insert the Subject string when it's different
3600        ;; from the previous Subject string.
3601        (if (and
3602             gnus-show-threads
3603             (gnus-subject-equal
3604              (condition-case ()
3605                  (mail-header-subject
3606                   (gnus-data-header
3607                    (cadr
3608                     (gnus-data-find-list
3609                      article
3610                      (gnus-data-list t)))))
3611                ;; Error on the side of excessive subjects.
3612                (error ""))
3613              (mail-header-subject header)))
3614            ""
3615          (mail-header-subject header))
3616        nil (cdr (assq article gnus-newsgroup-scored))
3617        (memq article gnus-newsgroup-processable))
3618       (when length
3619         (gnus-data-update-list
3620          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3621
3622 (defun gnus-summary-update-article (article &optional iheader)
3623   "Update ARTICLE in the summary buffer."
3624   (set-buffer gnus-summary-buffer)
3625   (let* ((header (gnus-summary-article-header article))
3626          (id (mail-header-id header))
3627          (data (gnus-data-find article))
3628          (thread (gnus-id-to-thread id))
3629          (references (mail-header-references header))
3630          (parent
3631           (gnus-id-to-thread
3632            (or (gnus-parent-id
3633                 (when (and references
3634                            (not (equal "" references)))
3635                   references))
3636                "none")))
3637          (buffer-read-only nil)
3638          (old (car thread)))
3639     (when thread
3640       (unless iheader
3641         (setcar thread nil)
3642         (when parent
3643           (delq thread parent)))
3644       (if (gnus-summary-insert-subject id header)
3645           ;; Set the (possibly) new article number in the data structure.
3646           (gnus-data-set-number data (gnus-id-to-article id))
3647         (setcar thread old)
3648         nil))))
3649
3650 (defun gnus-rebuild-thread (id &optional line)
3651   "Rebuild the thread containing ID.
3652 If LINE, insert the rebuilt thread starting on line LINE."
3653   (let ((buffer-read-only nil)
3654         old-pos current thread data)
3655     (if (not gnus-show-threads)
3656         (setq thread (list (car (gnus-id-to-thread id))))
3657       ;; Get the thread this article is part of.
3658       (setq thread (gnus-remove-thread id)))
3659     (setq old-pos (gnus-point-at-bol))
3660     (setq current (save-excursion
3661                     (and (zerop (forward-line -1))
3662                          (gnus-summary-article-number))))
3663     ;; If this is a gathered thread, we have to go some re-gathering.
3664     (when (stringp (car thread))
3665       (let ((subject (car thread))
3666             roots thr)
3667         (setq thread (cdr thread))
3668         (while thread
3669           (unless (memq (setq thr (gnus-id-to-thread
3670                                    (gnus-root-id
3671                                     (mail-header-id (caar thread)))))
3672                         roots)
3673             (push thr roots))
3674           (setq thread (cdr thread)))
3675         ;; We now have all (unique) roots.
3676         (if (= (length roots) 1)
3677             ;; All the loose roots are now one solid root.
3678             (setq thread (car roots))
3679           (setq thread (cons subject (gnus-sort-threads roots))))))
3680     (let (threads)
3681       ;; We then insert this thread into the summary buffer.
3682       (when line
3683         (goto-char (point-min))
3684         (forward-line (1- line)))
3685       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3686         (if gnus-show-threads
3687             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3688           (gnus-summary-prepare-unthreaded thread))
3689         (setq data (nreverse gnus-newsgroup-data))
3690         (setq threads gnus-newsgroup-threads))
3691       ;; We splice the new data into the data structure.
3692       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3693       ;;!!! then we want to insert at the beginning of the buffer.
3694       ;;!!! That happens to be true with Gnus now, but that may
3695       ;;!!! change in the future.  Perhaps.
3696       (gnus-data-enter-list
3697        (if line nil current) data (- (point) old-pos))
3698       (setq gnus-newsgroup-threads
3699             (nconc threads gnus-newsgroup-threads))
3700       (gnus-data-compute-positions))))
3701
3702 (defun gnus-number-to-header (number)
3703   "Return the header for article NUMBER."
3704   (let ((headers gnus-newsgroup-headers))
3705     (while (and headers
3706                 (not (= number (mail-header-number (car headers)))))
3707       (pop headers))
3708     (when headers
3709       (car headers))))
3710
3711 (defun gnus-parent-headers (in-headers &optional generation)
3712   "Return the headers of the GENERATIONeth parent of HEADERS."
3713   (unless generation
3714     (setq generation 1))
3715   (let ((parent t)
3716         (headers in-headers)
3717         references)
3718     (while (and parent
3719                 (not (zerop generation))
3720                 (setq references (mail-header-references headers)))
3721       (setq headers (if (and references
3722                              (setq parent (gnus-parent-id references)))
3723                         (car (gnus-id-to-thread parent))
3724                       nil))
3725       (decf generation))
3726     (and (not (eq headers in-headers))
3727          headers)))
3728
3729 (defun gnus-id-to-thread (id)
3730   "Return the (sub-)thread where ID appears."
3731   (gnus-gethash id gnus-newsgroup-dependencies))
3732
3733 (defun gnus-id-to-article (id)
3734   "Return the article number of ID."
3735   (let ((thread (gnus-id-to-thread id)))
3736     (when (and thread
3737                (car thread))
3738       (mail-header-number (car thread)))))
3739
3740 (defun gnus-id-to-header (id)
3741   "Return the article headers of ID."
3742   (car (gnus-id-to-thread id)))
3743
3744 (defun gnus-article-displayed-root-p (article)
3745   "Say whether ARTICLE is a root(ish) article."
3746   (let ((level (gnus-summary-thread-level article))
3747         (refs (mail-header-references  (gnus-summary-article-header article)))
3748         particle)
3749     (cond
3750      ((null level) nil)
3751      ((zerop level) t)
3752      ((null refs) t)
3753      ((null (gnus-parent-id refs)) t)
3754      ((and (= 1 level)
3755            (null (setq particle (gnus-id-to-article
3756                                  (gnus-parent-id refs))))
3757            (null (gnus-summary-thread-level particle)))))))
3758
3759 (defun gnus-root-id (id)
3760   "Return the id of the root of the thread where ID appears."
3761   (let (last-id prev)
3762     (while (and id (setq prev (car (gnus-id-to-thread id))))
3763       (setq last-id id
3764             id (gnus-parent-id (mail-header-references prev))))
3765     last-id))
3766
3767 (defun gnus-articles-in-thread (thread)
3768   "Return the list of articles in THREAD."
3769   (cons (mail-header-number (car thread))
3770         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3771
3772 (defun gnus-remove-thread (id &optional dont-remove)
3773   "Remove the thread that has ID in it."
3774   (let (headers thread last-id)
3775     ;; First go up in this thread until we find the root.
3776     (setq last-id (gnus-root-id id)
3777           headers (message-flatten-list (gnus-id-to-thread last-id)))
3778     ;; We have now found the real root of this thread.  It might have
3779     ;; been gathered into some loose thread, so we have to search
3780     ;; through the threads to find the thread we wanted.
3781     (let ((threads gnus-newsgroup-threads)
3782           sub)
3783       (while threads
3784         (setq sub (car threads))
3785         (if (stringp (car sub))
3786             ;; This is a gathered thread, so we look at the roots
3787             ;; below it to find whether this article is in this
3788             ;; gathered root.
3789             (progn
3790               (setq sub (cdr sub))
3791               (while sub
3792                 (when (member (caar sub) headers)
3793                   (setq thread (car threads)
3794                         threads nil
3795                         sub nil))
3796                 (setq sub (cdr sub))))
3797           ;; It's an ordinary thread, so we check it.
3798           (when (eq (car sub) (car headers))
3799             (setq thread sub
3800                   threads nil)))
3801         (setq threads (cdr threads)))
3802       ;; If this article is in no thread, then it's a root.
3803       (if thread
3804           (unless dont-remove
3805             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3806         (setq thread (gnus-id-to-thread last-id)))
3807       (when thread
3808         (prog1
3809             thread                      ; We return this thread.
3810           (unless dont-remove
3811             (if (stringp (car thread))
3812                 (progn
3813                   ;; If we use dummy roots, then we have to remove the
3814                   ;; dummy root as well.
3815                   (when (eq gnus-summary-make-false-root 'dummy)
3816                     ;; We go to the dummy root by going to
3817                     ;; the first sub-"thread", and then one line up.
3818                     (gnus-summary-goto-article
3819                      (mail-header-number (caadr thread)))
3820                     (forward-line -1)
3821                     (gnus-delete-line)
3822                     (gnus-data-compute-positions))
3823                   (setq thread (cdr thread))
3824                   (while thread
3825                     (gnus-remove-thread-1 (car thread))
3826                     (setq thread (cdr thread))))
3827               (gnus-remove-thread-1 thread))))))))
3828
3829 (defun gnus-remove-thread-1 (thread)
3830   "Remove the thread THREAD recursively."
3831   (let ((number (mail-header-number (pop thread)))
3832         d)
3833     (setq thread (reverse thread))
3834     (while thread
3835       (gnus-remove-thread-1 (pop thread)))
3836     (when (setq d (gnus-data-find number))
3837       (goto-char (gnus-data-pos d))
3838       (gnus-summary-show-thread)
3839       (gnus-data-remove
3840        number
3841        (- (gnus-point-at-bol)
3842           (prog1
3843               (1+ (gnus-point-at-eol))
3844             (gnus-delete-line)))))))
3845
3846 (defun gnus-sort-threads-1 (threads func)
3847   (sort (mapcar (lambda (thread)
3848                   (cons (car thread)
3849                         (and (cdr thread)
3850                              (gnus-sort-threads-1 (cdr thread) func))))
3851                 threads) func))
3852
3853 (defun gnus-sort-threads (threads)
3854   "Sort THREADS."
3855   (if (not gnus-thread-sort-functions)
3856       threads
3857     (gnus-message 8 "Sorting threads...")
3858     (prog1
3859         (gnus-sort-threads-1
3860          threads
3861          (gnus-make-sort-function gnus-thread-sort-functions))
3862       (gnus-message 8 "Sorting threads...done"))))
3863
3864 (defun gnus-sort-articles (articles)
3865   "Sort ARTICLES."
3866   (when gnus-article-sort-functions
3867     (gnus-message 7 "Sorting articles...")
3868     (prog1
3869         (setq gnus-newsgroup-headers
3870               (sort articles (gnus-make-sort-function
3871                               gnus-article-sort-functions)))
3872       (gnus-message 7 "Sorting articles...done"))))
3873
3874 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3875 (defmacro gnus-thread-header (thread)
3876   "Return header of first article in THREAD.
3877 Note that THREAD must never, ever be anything else than a variable -
3878 using some other form will lead to serious barfage."
3879   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3880   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3881   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3882         (vector thread) 2))
3883
3884 (defsubst gnus-article-sort-by-number (h1 h2)
3885   "Sort articles by article number."
3886   (< (mail-header-number h1)
3887      (mail-header-number h2)))
3888
3889 (defun gnus-thread-sort-by-number (h1 h2)
3890   "Sort threads by root article number."
3891   (gnus-article-sort-by-number
3892    (gnus-thread-header h1) (gnus-thread-header h2)))
3893
3894 (defsubst gnus-article-sort-by-lines (h1 h2)
3895   "Sort articles by article Lines header."
3896   (< (mail-header-lines h1)
3897      (mail-header-lines h2)))
3898
3899 (defun gnus-thread-sort-by-lines (h1 h2)
3900   "Sort threads by root article Lines header."
3901   (gnus-article-sort-by-lines
3902    (gnus-thread-header h1) (gnus-thread-header h2)))
3903
3904 (defsubst gnus-article-sort-by-chars (h1 h2)
3905   "Sort articles by octet length."
3906   (< (mail-header-chars h1)
3907      (mail-header-chars h2)))
3908
3909 (defun gnus-thread-sort-by-chars (h1 h2)
3910   "Sort threads by root article octet length."
3911   (gnus-article-sort-by-chars
3912    (gnus-thread-header h1) (gnus-thread-header h2)))
3913
3914 (defsubst gnus-article-sort-by-author (h1 h2)
3915   "Sort articles by root author."
3916   (string-lessp
3917    (let ((extract (funcall
3918                    gnus-extract-address-components
3919                    (mail-header-from h1))))
3920      (or (car extract) (cadr extract) ""))
3921    (let ((extract (funcall
3922                    gnus-extract-address-components
3923                    (mail-header-from h2))))
3924      (or (car extract) (cadr extract) ""))))
3925
3926 (defun gnus-thread-sort-by-author (h1 h2)
3927   "Sort threads by root author."
3928   (gnus-article-sort-by-author
3929    (gnus-thread-header h1)  (gnus-thread-header h2)))
3930
3931 (defsubst gnus-article-sort-by-subject (h1 h2)
3932   "Sort articles by root subject."
3933   (string-lessp
3934    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3935    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3936
3937 (defun gnus-thread-sort-by-subject (h1 h2)
3938   "Sort threads by root subject."
3939   (gnus-article-sort-by-subject
3940    (gnus-thread-header h1) (gnus-thread-header h2)))
3941
3942 (defsubst gnus-article-sort-by-date (h1 h2)
3943   "Sort articles by root article date."
3944   (time-less-p
3945    (gnus-date-get-time (mail-header-date h1))
3946    (gnus-date-get-time (mail-header-date h2))))
3947
3948 (defun gnus-thread-sort-by-date (h1 h2)
3949   "Sort threads by root article date."
3950   (gnus-article-sort-by-date
3951    (gnus-thread-header h1) (gnus-thread-header h2)))
3952
3953 (defsubst gnus-article-sort-by-score (h1 h2)
3954   "Sort articles by root article score.
3955 Unscored articles will be counted as having a score of zero."
3956   (> (or (cdr (assq (mail-header-number h1)
3957                     gnus-newsgroup-scored))
3958          gnus-summary-default-score 0)
3959      (or (cdr (assq (mail-header-number h2)
3960                     gnus-newsgroup-scored))
3961          gnus-summary-default-score 0)))
3962
3963 (defun gnus-thread-sort-by-score (h1 h2)
3964   "Sort threads by root article score."
3965   (gnus-article-sort-by-score
3966    (gnus-thread-header h1) (gnus-thread-header h2)))
3967
3968 (defun gnus-thread-sort-by-total-score (h1 h2)
3969   "Sort threads by the sum of all scores in the thread.
3970 Unscored articles will be counted as having a score of zero."
3971   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3972
3973 (defun gnus-thread-total-score (thread)
3974   ;; This function find the total score of THREAD.
3975   (cond ((null thread)
3976          0)
3977         ((consp thread)
3978          (if (stringp (car thread))
3979              (apply gnus-thread-score-function 0
3980                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3981            (gnus-thread-total-score-1 thread)))
3982         (t
3983          (gnus-thread-total-score-1 (list thread)))))
3984
3985 (defun gnus-thread-total-score-1 (root)
3986   ;; This function find the total score of the thread below ROOT.
3987   (setq root (car root))
3988   (apply gnus-thread-score-function
3989          (or (append
3990               (mapcar 'gnus-thread-total-score
3991                       (cdr (gnus-id-to-thread (mail-header-id root))))
3992               (when (> (mail-header-number root) 0)
3993                 (list (or (cdr (assq (mail-header-number root)
3994                                      gnus-newsgroup-scored))
3995                           gnus-summary-default-score 0))))
3996              (list gnus-summary-default-score)
3997              '(0))))
3998
3999 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4000 (defvar gnus-tmp-prev-subject nil)
4001 (defvar gnus-tmp-false-parent nil)
4002 (defvar gnus-tmp-root-expunged nil)
4003 (defvar gnus-tmp-dummy-line nil)
4004
4005 (eval-when-compile (defvar gnus-tmp-header))
4006 (defun gnus-extra-header (type &optional header)
4007   "Return the extra header of TYPE."
4008   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4009       ""))
4010
4011 (defun gnus-summary-prepare-threads (threads)
4012   "Prepare summary buffer from THREADS and indentation LEVEL.
4013 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4014 or a straight list of headers."
4015   (gnus-message 7 "Generating summary...")
4016
4017   (setq gnus-newsgroup-threads threads)
4018   (beginning-of-line)
4019
4020   (let ((gnus-tmp-level 0)
4021         (default-score (or gnus-summary-default-score 0))
4022         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4023         thread number subject stack state gnus-tmp-gathered beg-match
4024         new-roots gnus-tmp-new-adopts thread-end
4025         gnus-tmp-header gnus-tmp-unread
4026         gnus-tmp-replied gnus-tmp-subject-or-nil
4027         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4028         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4029         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4030
4031     (setq gnus-tmp-prev-subject nil)
4032
4033     (if (vectorp (car threads))
4034         ;; If this is a straight (sic) list of headers, then a
4035         ;; threaded summary display isn't required, so we just create
4036         ;; an unthreaded one.
4037         (gnus-summary-prepare-unthreaded threads)
4038
4039       ;; Do the threaded display.
4040
4041       (while (or threads stack gnus-tmp-new-adopts new-roots)
4042
4043         (if (and (= gnus-tmp-level 0)
4044                  (or (not stack)
4045                      (= (caar stack) 0))
4046                  (not gnus-tmp-false-parent)
4047                  (or gnus-tmp-new-adopts new-roots))
4048             (if gnus-tmp-new-adopts
4049                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4050                       thread (list (car gnus-tmp-new-adopts))
4051                       gnus-tmp-header (caar thread)
4052                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4053               (when new-roots
4054                 (setq thread (list (car new-roots))
4055                       gnus-tmp-header (caar thread)
4056                       new-roots (cdr new-roots))))
4057
4058           (if threads
4059               ;; If there are some threads, we do them before the
4060               ;; threads on the stack.
4061               (setq thread threads
4062                     gnus-tmp-header (caar thread))
4063             ;; There were no current threads, so we pop something off
4064             ;; the stack.
4065             (setq state (car stack)
4066                   gnus-tmp-level (car state)
4067                   thread (cdr state)
4068                   stack (cdr stack)
4069                   gnus-tmp-header (caar thread))))
4070
4071         (setq gnus-tmp-false-parent nil)
4072         (setq gnus-tmp-root-expunged nil)
4073         (setq thread-end nil)
4074
4075         (if (stringp gnus-tmp-header)
4076             ;; The header is a dummy root.
4077             (cond
4078              ((eq gnus-summary-make-false-root 'adopt)
4079               ;; We let the first article adopt the rest.
4080               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4081                                                (cddar thread)))
4082               (setq gnus-tmp-gathered
4083                     (nconc (mapcar
4084                             (lambda (h) (mail-header-number (car h)))
4085                             (cddar thread))
4086                            gnus-tmp-gathered))
4087               (setq thread (cons (list (caar thread)
4088                                        (cadar thread))
4089                                  (cdr thread)))
4090               (setq gnus-tmp-level -1
4091                     gnus-tmp-false-parent t))
4092              ((eq gnus-summary-make-false-root 'empty)
4093               ;; We print adopted articles with empty subject fields.
4094               (setq gnus-tmp-gathered
4095                     (nconc (mapcar
4096                             (lambda (h) (mail-header-number (car h)))
4097                             (cddar thread))
4098                            gnus-tmp-gathered))
4099               (setq gnus-tmp-level -1))
4100              ((eq gnus-summary-make-false-root 'dummy)
4101               ;; We remember that we probably want to output a dummy
4102               ;; root.
4103               (setq gnus-tmp-dummy-line gnus-tmp-header)
4104               (setq gnus-tmp-prev-subject gnus-tmp-header))
4105              (t
4106               ;; We do not make a root for the gathered
4107               ;; sub-threads at all.
4108               (setq gnus-tmp-level -1)))
4109
4110           (setq number (mail-header-number gnus-tmp-header)
4111                 subject (mail-header-subject gnus-tmp-header))
4112
4113           (cond
4114            ;; If the thread has changed subject, we might want to make
4115            ;; this subthread into a root.
4116            ((and (null gnus-thread-ignore-subject)
4117                  (not (zerop gnus-tmp-level))
4118                  gnus-tmp-prev-subject
4119                  (not (inline
4120                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4121             (setq new-roots (nconc new-roots (list (car thread)))
4122                   thread-end t
4123                   gnus-tmp-header nil))
4124            ;; If the article lies outside the current limit,
4125            ;; then we do not display it.
4126            ((not (memq number gnus-newsgroup-limit))
4127             (setq gnus-tmp-gathered
4128                   (nconc (mapcar
4129                           (lambda (h) (mail-header-number (car h)))
4130                           (cdar thread))
4131                          gnus-tmp-gathered))
4132             (setq gnus-tmp-new-adopts (if (cdar thread)
4133                                           (append gnus-tmp-new-adopts
4134                                                   (cdar thread))
4135                                         gnus-tmp-new-adopts)
4136                   thread-end t
4137                   gnus-tmp-header nil)
4138             (when (zerop gnus-tmp-level)
4139               (setq gnus-tmp-root-expunged t)))
4140            ;; Perhaps this article is to be marked as read?
4141            ((and gnus-summary-mark-below
4142                  (< (or (cdr (assq number gnus-newsgroup-scored))
4143                         default-score)
4144                     gnus-summary-mark-below)
4145                  ;; Don't touch sparse articles.
4146                  (not (gnus-summary-article-sparse-p number))
4147                  (not (gnus-summary-article-ancient-p number)))
4148             (setq gnus-newsgroup-unreads
4149                   (delq number gnus-newsgroup-unreads))
4150             (if gnus-newsgroup-auto-expire
4151                 (push number gnus-newsgroup-expirable)
4152               (push (cons number gnus-low-score-mark)
4153                     gnus-newsgroup-reads))))
4154
4155           (when gnus-tmp-header
4156             ;; We may have an old dummy line to output before this
4157             ;; article.
4158             (when (and gnus-tmp-dummy-line
4159                        (gnus-subject-equal
4160                         gnus-tmp-dummy-line
4161                         (mail-header-subject gnus-tmp-header)))
4162               (gnus-summary-insert-dummy-line
4163                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4164               (setq gnus-tmp-dummy-line nil))
4165
4166             ;; Compute the mark.
4167             (setq gnus-tmp-unread (gnus-article-mark number))
4168
4169             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4170                                   gnus-tmp-header gnus-tmp-level)
4171                   gnus-newsgroup-data)
4172
4173             ;; Actually insert the line.
4174             (setq
4175              gnus-tmp-subject-or-nil
4176              (cond
4177               ((and gnus-thread-ignore-subject
4178                     gnus-tmp-prev-subject
4179                     (not (inline (gnus-subject-equal
4180                                   gnus-tmp-prev-subject subject))))
4181                subject)
4182               ((zerop gnus-tmp-level)
4183                (if (and (eq gnus-summary-make-false-root 'empty)
4184                         (memq number gnus-tmp-gathered)
4185                         gnus-tmp-prev-subject
4186                         (inline (gnus-subject-equal
4187                                  gnus-tmp-prev-subject subject)))
4188                    gnus-summary-same-subject
4189                  subject))
4190               (t gnus-summary-same-subject)))
4191             (if (and (eq gnus-summary-make-false-root 'adopt)
4192                      (= gnus-tmp-level 1)
4193                      (memq number gnus-tmp-gathered))
4194                 (setq gnus-tmp-opening-bracket ?\<
4195                       gnus-tmp-closing-bracket ?\>)
4196               (setq gnus-tmp-opening-bracket ?\[
4197                     gnus-tmp-closing-bracket ?\]))
4198             (setq
4199              gnus-tmp-indentation
4200              (aref gnus-thread-indent-array gnus-tmp-level)
4201              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4202              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4203                                 gnus-summary-default-score 0)
4204              gnus-tmp-score-char
4205              (if (or (null gnus-summary-default-score)
4206                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4207                          gnus-summary-zcore-fuzz))
4208                  ?  ;Whitespace
4209                (if (< gnus-tmp-score gnus-summary-default-score)
4210                    gnus-score-below-mark gnus-score-over-mark))
4211              gnus-tmp-replied
4212              (cond ((memq number gnus-newsgroup-processable)
4213                     gnus-process-mark)
4214                    ((memq number gnus-newsgroup-cached)
4215                     gnus-cached-mark)
4216                    ((memq number gnus-newsgroup-replied)
4217                     gnus-replied-mark)
4218                    ((memq number gnus-newsgroup-saved)
4219                     gnus-saved-mark)
4220                    (t gnus-no-mark))
4221              gnus-tmp-from (mail-header-from gnus-tmp-header)
4222              gnus-tmp-name
4223              (cond
4224               ((string-match "<[^>]+> *$" gnus-tmp-from)
4225                (setq beg-match (match-beginning 0))
4226                (or (and (string-match "^\".+\"" gnus-tmp-from)
4227                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4228                    (substring gnus-tmp-from 0 beg-match)))
4229               ((string-match "(.+)" gnus-tmp-from)
4230                (substring gnus-tmp-from
4231                           (1+ (match-beginning 0)) (1- (match-end 0))))
4232               (t gnus-tmp-from)))
4233             (when (string= gnus-tmp-name "")
4234               (setq gnus-tmp-name gnus-tmp-from))
4235             (unless (numberp gnus-tmp-lines)
4236               (setq gnus-tmp-lines 0))
4237             (gnus-put-text-property
4238              (point)
4239              (progn (eval gnus-summary-line-format-spec) (point))
4240              'gnus-number number)
4241             (when gnus-visual-p
4242               (forward-line -1)
4243               (gnus-run-hooks 'gnus-summary-update-hook)
4244               (forward-line 1))
4245
4246             (setq gnus-tmp-prev-subject subject)))
4247
4248         (when (nth 1 thread)
4249           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4250         (incf gnus-tmp-level)
4251         (setq threads (if thread-end nil (cdar thread)))
4252         (unless threads
4253           (setq gnus-tmp-level 0)))))
4254   (gnus-message 7 "Generating summary...done"))
4255
4256 (defun gnus-summary-prepare-unthreaded (headers)
4257   "Generate an unthreaded summary buffer based on HEADERS."
4258   (let (header number mark)
4259
4260     (beginning-of-line)
4261
4262     (while headers
4263       ;; We may have to root out some bad articles...
4264       (when (memq (setq number (mail-header-number
4265                                 (setq header (pop headers))))
4266                   gnus-newsgroup-limit)
4267         ;; Mark article as read when it has a low score.
4268         (when (and gnus-summary-mark-below
4269                    (< (or (cdr (assq number gnus-newsgroup-scored))
4270                           gnus-summary-default-score 0)
4271                       gnus-summary-mark-below)
4272                    (not (gnus-summary-article-ancient-p number)))
4273           (setq gnus-newsgroup-unreads
4274                 (delq number gnus-newsgroup-unreads))
4275           (if gnus-newsgroup-auto-expire
4276               (push number gnus-newsgroup-expirable)
4277             (push (cons number gnus-low-score-mark)
4278                   gnus-newsgroup-reads)))
4279
4280         (setq mark (gnus-article-mark number))
4281         (push (gnus-data-make number mark (1+ (point)) header 0)
4282               gnus-newsgroup-data)
4283         (gnus-summary-insert-line
4284          header 0 number
4285          mark (memq number gnus-newsgroup-replied)
4286          (memq number gnus-newsgroup-expirable)
4287          (mail-header-subject header) nil
4288          (cdr (assq number gnus-newsgroup-scored))
4289          (memq number gnus-newsgroup-processable))))))
4290
4291 (defun gnus-summary-remove-list-identifiers ()
4292   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4293   (let ((regexp (if (consp gnus-list-identifiers)
4294                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4295                   gnus-list-identifiers))
4296         changed subject)
4297     (when regexp
4298       (dolist (header gnus-newsgroup-headers)
4299         (setq subject (mail-header-subject header)
4300               changed nil)
4301         (while (string-match
4302                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4303                 subject)
4304           (setq subject
4305                 (concat (substring subject 0 (match-beginning 2))
4306                         (substring subject (match-end 0)))
4307                 changed t))
4308         (when (and changed
4309                    (string-match
4310                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4311           (setq subject
4312                 (concat (substring subject 0 (match-beginning 1))
4313                         (substring subject (match-end 1)))))
4314         (when changed
4315           (mail-header-set-subject header subject))))))
4316
4317 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4318   "Select newsgroup GROUP.
4319 If READ-ALL is non-nil, all articles in the group are selected.
4320 If SELECT-ARTICLES, only select those articles from GROUP."
4321   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4322          ;;!!! Dirty hack; should be removed.
4323          (gnus-summary-ignore-duplicates
4324           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4325               t
4326             gnus-summary-ignore-duplicates))
4327          (info (nth 2 entry))
4328          articles fetched-articles cached)
4329
4330     (unless (gnus-check-server
4331              (setq gnus-current-select-method
4332                    (gnus-find-method-for-group group)))
4333       (error "Couldn't open server"))
4334
4335     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4336         (gnus-activate-group group)     ; Or we can activate it...
4337         (progn                          ; Or we bug out.
4338           (when (equal major-mode 'gnus-summary-mode)
4339             (kill-buffer (current-buffer)))
4340           (error "Couldn't activate group %s: %s"
4341                  group (gnus-status-message group))))
4342
4343     (unless (gnus-request-group group t)
4344       (when (equal major-mode 'gnus-summary-mode)
4345         (kill-buffer (current-buffer)))
4346       (error "Couldn't request group %s: %s"
4347              group (gnus-status-message group)))
4348
4349     (setq gnus-newsgroup-name group)
4350     (setq gnus-newsgroup-unselected nil)
4351     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4352     (gnus-summary-setup-default-charset)
4353
4354     ;; Adjust and set lists of article marks.
4355     (when info
4356       (gnus-adjust-marked-articles info))
4357
4358     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4359     (when (gnus-virtual-group-p group)
4360       (setq cached gnus-newsgroup-cached))
4361
4362     (setq gnus-newsgroup-unreads
4363           (gnus-set-difference
4364            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4365            gnus-newsgroup-dormant))
4366
4367     (setq gnus-newsgroup-processable nil)
4368
4369     (gnus-update-read-articles group gnus-newsgroup-unreads)
4370
4371     (if (setq articles select-articles)
4372         (setq gnus-newsgroup-unselected
4373               (gnus-sorted-intersection
4374                gnus-newsgroup-unreads
4375                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4376       (setq articles (gnus-articles-to-read group read-all)))
4377
4378     (cond
4379      ((null articles)
4380       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4381       'quit)
4382      ((eq articles 0) nil)
4383      (t
4384       ;; Init the dependencies hash table.
4385       (setq gnus-newsgroup-dependencies
4386             (gnus-make-hashtable (length articles)))
4387       (gnus-set-global-variables)
4388       ;; Retrieve the headers and read them in.
4389       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4390       (setq gnus-newsgroup-headers
4391             (if (eq 'nov
4392                     (setq gnus-headers-retrieved-by
4393                           (gnus-retrieve-headers
4394                            articles gnus-newsgroup-name
4395                            ;; We might want to fetch old headers, but
4396                            ;; not if there is only 1 article.
4397                            (and (or (and
4398                                      (not (eq gnus-fetch-old-headers 'some))
4399                                      (not (numberp gnus-fetch-old-headers)))
4400                                     (> (length articles) 1))
4401                                 gnus-fetch-old-headers))))
4402                 (gnus-get-newsgroup-headers-xover
4403                  articles nil nil gnus-newsgroup-name t)
4404               (gnus-get-newsgroup-headers)))
4405       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4406
4407       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4408       (when cached
4409         (setq gnus-newsgroup-cached cached))
4410
4411       ;; Suppress duplicates?
4412       (when gnus-suppress-duplicates
4413         (gnus-dup-suppress-articles))
4414
4415       ;; Set the initial limit.
4416       (setq gnus-newsgroup-limit (copy-sequence articles))
4417       ;; Remove canceled articles from the list of unread articles.
4418       (setq gnus-newsgroup-unreads
4419             (gnus-set-sorted-intersection
4420              gnus-newsgroup-unreads
4421              (setq fetched-articles
4422                    (mapcar (lambda (headers) (mail-header-number headers))
4423                            gnus-newsgroup-headers))))
4424       ;; Removed marked articles that do not exist.
4425       (gnus-update-missing-marks
4426        (gnus-sorted-complement fetched-articles articles))
4427       ;; We might want to build some more threads first.
4428       (when (and gnus-fetch-old-headers
4429                  (eq gnus-headers-retrieved-by 'nov))
4430         (if (eq gnus-fetch-old-headers 'invisible)
4431             (gnus-build-all-threads)
4432           (gnus-build-old-threads)))
4433       ;; Let the Gnus agent mark articles as read.
4434       (when gnus-agent
4435         (gnus-agent-get-undownloaded-list))
4436       ;; Remove list identifiers from subject
4437       (when gnus-list-identifiers
4438         (gnus-summary-remove-list-identifiers))
4439       ;; Check whether auto-expire is to be done in this group.
4440       (setq gnus-newsgroup-auto-expire
4441             (gnus-group-auto-expirable-p group))
4442       ;; Set up the article buffer now, if necessary.
4443       (unless gnus-single-article-buffer
4444         (gnus-article-setup-buffer))
4445       ;; First and last article in this newsgroup.
4446       (when gnus-newsgroup-headers
4447         (setq gnus-newsgroup-begin
4448               (mail-header-number (car gnus-newsgroup-headers))
4449               gnus-newsgroup-end
4450               (mail-header-number
4451                (gnus-last-element gnus-newsgroup-headers))))
4452       ;; GROUP is successfully selected.
4453       (or gnus-newsgroup-headers t)))))
4454
4455 (defun gnus-articles-to-read (group &optional read-all)
4456   "Find out what articles the user wants to read."
4457   (let* ((articles
4458           ;; Select all articles if `read-all' is non-nil, or if there
4459           ;; are no unread articles.
4460           (if (or read-all
4461                   (and (zerop (length gnus-newsgroup-marked))
4462                        (zerop (length gnus-newsgroup-unreads)))
4463                   (eq (gnus-group-find-parameter group 'display)
4464                       'all))
4465               (or
4466                (gnus-uncompress-range (gnus-active group))
4467                (gnus-cache-articles-in-group group))
4468             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4469                           (copy-sequence gnus-newsgroup-unreads))
4470                   '<)))
4471          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4472          (scored (length scored-list))
4473          (number (length articles))
4474          (marked (+ (length gnus-newsgroup-marked)
4475                     (length gnus-newsgroup-dormant)))
4476          (select
4477           (cond
4478            ((numberp read-all)
4479             read-all)
4480            (t
4481             (condition-case ()
4482                 (cond
4483                  ((and (or (<= scored marked) (= scored number))
4484                        (numberp gnus-large-newsgroup)
4485                        (> number gnus-large-newsgroup))
4486                   (let ((input
4487                          (read-string
4488                           (format
4489                            "How many articles from %s (default %d): "
4490                            (gnus-limit-string gnus-newsgroup-name 35)
4491                            number))))
4492                     (if (string-match "^[ \t]*$" input) number input)))
4493                  ((and (> scored marked) (< scored number)
4494                        (> (- scored number) 20))
4495                   (let ((input
4496                          (read-string
4497                           (format "%s %s (%d scored, %d total): "
4498                                   "How many articles from"
4499                                   group scored number))))
4500                     (if (string-match "^[ \t]*$" input)
4501                         number input)))
4502                  (t number))
4503               (quit
4504                (message "Quit getting the articles to read")
4505                nil))))))
4506     (setq select (if (stringp select) (string-to-number select) select))
4507     (if (or (null select) (zerop select))
4508         select
4509       (if (and (not (zerop scored)) (<= (abs select) scored))
4510           (progn
4511             (setq articles (sort scored-list '<))
4512             (setq number (length articles)))
4513         (setq articles (copy-sequence articles)))
4514
4515       (when (< (abs select) number)
4516         (if (< select 0)
4517             ;; Select the N oldest articles.
4518             (setcdr (nthcdr (1- (abs select)) articles) nil)
4519           ;; Select the N most recent articles.
4520           (setq articles (nthcdr (- number select) articles))))
4521       (setq gnus-newsgroup-unselected
4522             (gnus-sorted-intersection
4523              gnus-newsgroup-unreads
4524              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4525       (when gnus-alter-articles-to-read-function
4526         (setq gnus-newsgroup-unreads
4527               (sort
4528                (funcall gnus-alter-articles-to-read-function
4529                         gnus-newsgroup-name gnus-newsgroup-unreads)
4530                '<)))
4531       articles)))
4532
4533 (defun gnus-killed-articles (killed articles)
4534   (let (out)
4535     (while articles
4536       (when (inline (gnus-member-of-range (car articles) killed))
4537         (push (car articles) out))
4538       (setq articles (cdr articles)))
4539     out))
4540
4541 (defun gnus-uncompress-marks (marks)
4542   "Uncompress the mark ranges in MARKS."
4543   (let ((uncompressed '(score bookmark))
4544         out)
4545     (while marks
4546       (if (memq (caar marks) uncompressed)
4547           (push (car marks) out)
4548         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4549       (setq marks (cdr marks)))
4550     out))
4551
4552 (defun gnus-adjust-marked-articles (info)
4553   "Set all article lists and remove all marks that are no longer valid."
4554   (let* ((marked-lists (gnus-info-marks info))
4555          (active (gnus-active (gnus-info-group info)))
4556          (min (car active))
4557          (max (cdr active))
4558          (types gnus-article-mark-lists)
4559          (uncompressed '(score bookmark killed))
4560          marks var articles article mark)
4561
4562     (while marked-lists
4563       (setq marks (pop marked-lists))
4564       (set (setq var (intern (format "gnus-newsgroup-%s"
4565                                      (car (rassq (setq mark (car marks))
4566                                                  types)))))
4567            (if (memq (car marks) uncompressed) (cdr marks)
4568              (gnus-uncompress-range (cdr marks))))
4569
4570       (setq articles (symbol-value var))
4571
4572       ;; All articles have to be subsets of the active articles.
4573       (cond
4574        ;; Adjust "simple" lists.
4575        ((memq mark '(tick dormant expire reply save))
4576         (while articles
4577           (when (or (< (setq article (pop articles)) min) (> article max))
4578             (set var (delq article (symbol-value var))))))
4579        ;; Adjust assocs.
4580        ((memq mark uncompressed)
4581         (when (not (listp (cdr (symbol-value var))))
4582           (set var (list (symbol-value var))))
4583         (when (not (listp (cdr articles)))
4584           (setq articles (list articles)))
4585         (while articles
4586           (when (or (not (consp (setq article (pop articles))))
4587                     (< (car article) min)
4588                     (> (car article) max))
4589             (set var (delq article (symbol-value var))))))))))
4590
4591 (defun gnus-update-missing-marks (missing)
4592   "Go through the list of MISSING articles and remove them from the mark lists."
4593   (when missing
4594     (let ((types gnus-article-mark-lists)
4595           var m)
4596       ;; Go through all types.
4597       (while types
4598         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4599         (when (symbol-value var)
4600           ;; This list has articles.  So we delete all missing articles
4601           ;; from it.
4602           (setq m missing)
4603           (while m
4604             (set var (delq (pop m) (symbol-value var)))))))))
4605
4606 (defun gnus-update-marks ()
4607   "Enter the various lists of marked articles into the newsgroup info list."
4608   (let ((types gnus-article-mark-lists)
4609         (info (gnus-get-info gnus-newsgroup-name))
4610         (uncompressed '(score bookmark killed))
4611         type list newmarked symbol delta-marks)
4612     (when info
4613       ;; Add all marks lists to the list of marks lists.
4614       (while (setq type (pop types))
4615         (setq list (symbol-value
4616                     (setq symbol
4617                           (intern (format "gnus-newsgroup-%s"
4618                                           (car type))))))
4619
4620         (when list
4621           ;; Get rid of the entries of the articles that have the
4622           ;; default score.
4623           (when (and (eq (cdr type) 'score)
4624                      gnus-save-score
4625                      list)
4626             (let* ((arts list)
4627                    (prev (cons nil list))
4628                    (all prev))
4629               (while arts
4630                 (if (or (not (consp (car arts)))
4631                         (= (cdar arts) gnus-summary-default-score))
4632                     (setcdr prev (cdr arts))
4633                   (setq prev arts))
4634                 (setq arts (cdr arts)))
4635               (setq list (cdr all)))))
4636
4637         (unless (memq (cdr type) uncompressed)
4638           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4639
4640         (when (gnus-check-backend-function
4641                'request-set-mark gnus-newsgroup-name)
4642           ;; propagate flags to server, with the following exceptions:
4643           ;; uncompressed:s are not proper flags (they are cons cells)
4644           ;; cache is a internal gnus flag
4645           ;; download are local to one gnus installation (well)
4646           ;; unsend are for nndraft groups only
4647           ;; xxx: generality of this?  this suits nnimap anyway
4648           (unless (memq (cdr type) (append '(cache download unsend)
4649                                            uncompressed))
4650             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4651                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4652                    (add (gnus-remove-from-range
4653                          (gnus-copy-sequence list) old)))
4654               (when add
4655                 (push (list add 'add (list (cdr type))) delta-marks))
4656               (when del
4657                 (push (list del 'del (list (cdr type))) delta-marks)))))
4658
4659         (when list
4660           (push (cons (cdr type) list) newmarked)))
4661
4662       (when delta-marks
4663         (unless (gnus-check-group gnus-newsgroup-name)
4664           (error "Can't open server for %s" gnus-newsgroup-name))
4665         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4666
4667       ;; Enter these new marks into the info of the group.
4668       (if (nthcdr 3 info)
4669           (setcar (nthcdr 3 info) newmarked)
4670         ;; Add the marks lists to the end of the info.
4671         (when newmarked
4672           (setcdr (nthcdr 2 info) (list newmarked))))
4673
4674       ;; Cut off the end of the info if there's nothing else there.
4675       (let ((i 5))
4676         (while (and (> i 2)
4677                     (not (nth i info)))
4678           (when (nthcdr (decf i) info)
4679             (setcdr (nthcdr i info) nil)))))))
4680
4681 (defun gnus-set-mode-line (where)
4682   "Set the mode line of the article or summary buffers.
4683 If WHERE is `summary', the summary mode line format will be used."
4684   ;; Is this mode line one we keep updated?
4685   (when (and (memq where gnus-updated-mode-lines)
4686              (symbol-value
4687               (intern (format "gnus-%s-mode-line-format-spec" where))))
4688     (let (mode-string)
4689       (save-excursion
4690         ;; We evaluate this in the summary buffer since these
4691         ;; variables are buffer-local to that buffer.
4692         (set-buffer gnus-summary-buffer)
4693         ;; We bind all these variables that are used in the `eval' form
4694         ;; below.
4695         (let* ((mformat (symbol-value
4696                          (intern
4697                           (format "gnus-%s-mode-line-format-spec" where))))
4698                (gnus-tmp-group-name (gnus-group-name-decode
4699                                      gnus-newsgroup-name
4700                                      (gnus-group-name-charset
4701                                       nil
4702                                       gnus-newsgroup-name)))
4703                (gnus-tmp-article-number (or gnus-current-article 0))
4704                (gnus-tmp-unread gnus-newsgroup-unreads)
4705                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4706                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4707                (gnus-tmp-unread-and-unselected
4708                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4709                             (zerop gnus-tmp-unselected))
4710                        "")
4711                       ((zerop gnus-tmp-unselected)
4712                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4713                       (t (format "{%d(+%d) more}"
4714                                  gnus-tmp-unread-and-unticked
4715                                  gnus-tmp-unselected))))
4716                (gnus-tmp-subject
4717                 (if (and gnus-current-headers
4718                          (vectorp gnus-current-headers))
4719                     (gnus-mode-string-quote
4720                      (mail-header-subject gnus-current-headers))
4721                   ""))
4722                bufname-length max-len
4723                gnus-tmp-header);; passed as argument to any user-format-funcs
4724           (setq mode-string (eval mformat))
4725           (setq bufname-length (if (string-match "%b" mode-string)
4726                                    (- (length
4727                                        (buffer-name
4728                                         (if (eq where 'summary)
4729                                             nil
4730                                           (get-buffer gnus-article-buffer))))
4731                                       2)
4732                                  0))
4733           (setq max-len (max 4 (if gnus-mode-non-string-length
4734                                    (- (window-width)
4735                                       gnus-mode-non-string-length
4736                                       bufname-length)
4737                                  (length mode-string))))
4738           ;; We might have to chop a bit of the string off...
4739           (when (> (length mode-string) max-len)
4740             (setq mode-string
4741                   (concat (truncate-string-to-width mode-string (- max-len 3))
4742                           "...")))
4743           ;; Pad the mode string a bit.
4744           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4745       ;; Update the mode line.
4746       (setq mode-line-buffer-identification
4747             (gnus-mode-line-buffer-identification (list mode-string)))
4748       (set-buffer-modified-p t))))
4749
4750 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4751   "Go through the HEADERS list and add all Xrefs to a hash table.
4752 The resulting hash table is returned, or nil if no Xrefs were found."
4753   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4754          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4755          (xref-hashtb (gnus-make-hashtable))
4756          start group entry number xrefs header)
4757     (while headers
4758       (setq header (pop headers))
4759       (when (and (setq xrefs (mail-header-xref header))
4760                  (not (memq (setq number (mail-header-number header))
4761                             unreads)))
4762         (setq start 0)
4763         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4764           (setq start (match-end 0))
4765           (setq group (if prefix
4766                           (concat prefix (substring xrefs (match-beginning 1)
4767                                                     (match-end 1)))
4768                         (substring xrefs (match-beginning 1) (match-end 1))))
4769           (setq number
4770                 (string-to-int (substring xrefs (match-beginning 2)
4771                                           (match-end 2))))
4772           (if (setq entry (gnus-gethash group xref-hashtb))
4773               (setcdr entry (cons number (cdr entry)))
4774             (gnus-sethash group (cons number nil) xref-hashtb)))))
4775     (and start xref-hashtb)))
4776
4777 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4778   "Look through all the headers and mark the Xrefs as read."
4779   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4780         name entry info xref-hashtb idlist method nth4)
4781     (save-excursion
4782       (set-buffer gnus-group-buffer)
4783       (when (setq xref-hashtb
4784                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4785         (mapatoms
4786          (lambda (group)
4787            (unless (string= from-newsgroup (setq name (symbol-name group)))
4788              (setq idlist (symbol-value group))
4789              ;; Dead groups are not updated.
4790              (and (prog1
4791                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4792                             info (nth 2 entry))
4793                     (when (stringp (setq nth4 (gnus-info-method info)))
4794                       (setq nth4 (gnus-server-to-method nth4))))
4795                   ;; Only do the xrefs if the group has the same
4796                   ;; select method as the group we have just read.
4797                   (or (gnus-methods-equal-p
4798                        nth4 (gnus-find-method-for-group from-newsgroup))
4799                       virtual
4800                       (equal nth4 (setq method (gnus-find-method-for-group
4801                                                 from-newsgroup)))
4802                       (and (equal (car nth4) (car method))
4803                            (equal (nth 1 nth4) (nth 1 method))))
4804                   gnus-use-cross-reference
4805                   (or (not (eq gnus-use-cross-reference t))
4806                       virtual
4807                       ;; Only do cross-references on subscribed
4808                       ;; groups, if that is what is wanted.
4809                       (<= (gnus-info-level info) gnus-level-subscribed))
4810                   (gnus-group-make-articles-read name idlist))))
4811          xref-hashtb)))))
4812
4813 (defun gnus-compute-read-articles (group articles)
4814   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4815          (info (nth 2 entry))
4816          (active (gnus-active group))
4817          ninfo)
4818     (when entry
4819       ;; First peel off all invalid article numbers.
4820       (when active
4821         (let ((ids articles)
4822               id first)
4823           (while (setq id (pop ids))
4824             (when (and first (> id (cdr active)))
4825               ;; We'll end up in this situation in one particular
4826               ;; obscure situation.  If you re-scan a group and get
4827               ;; a new article that is cross-posted to a different
4828               ;; group that has not been re-scanned, you might get
4829               ;; crossposted article that has a higher number than
4830               ;; Gnus believes possible.  So we re-activate this
4831               ;; group as well.  This might mean doing the
4832               ;; crossposting thingy will *increase* the number
4833               ;; of articles in some groups.  Tsk, tsk.
4834               (setq active (or (gnus-activate-group group) active)))
4835             (when (or (> id (cdr active))
4836                       (< id (car active)))
4837               (setq articles (delq id articles))))))
4838       ;; If the read list is nil, we init it.
4839       (if (and active
4840                (null (gnus-info-read info))
4841                (> (car active) 1))
4842           (setq ninfo (cons 1 (1- (car active))))
4843         (setq ninfo (gnus-info-read info)))
4844       ;; Then we add the read articles to the range.
4845       (gnus-add-to-range
4846        ninfo (setq articles (sort articles '<))))))
4847
4848 (defun gnus-group-make-articles-read (group articles)
4849   "Update the info of GROUP to say that ARTICLES are read."
4850   (let* ((num 0)
4851          (entry (gnus-gethash group gnus-newsrc-hashtb))
4852          (info (nth 2 entry))
4853          (active (gnus-active group))
4854          range)
4855     (when entry
4856       (setq range (gnus-compute-read-articles group articles))
4857       (save-excursion
4858         (set-buffer gnus-group-buffer)
4859         (gnus-undo-register
4860           `(progn
4861              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4862              (gnus-info-set-read ',info ',(gnus-info-read info))
4863              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4864              (gnus-group-update-group ,group t))))
4865       ;; Add the read articles to the range.
4866       (gnus-info-set-read info range)
4867       ;; Then we have to re-compute how many unread
4868       ;; articles there are in this group.
4869       (when active
4870         (cond
4871          ((not range)
4872           (setq num (- (1+ (cdr active)) (car active))))
4873          ((not (listp (cdr range)))
4874           (setq num (- (cdr active) (- (1+ (cdr range))
4875                                        (car range)))))
4876          (t
4877           (while range
4878             (if (numberp (car range))
4879                 (setq num (1+ num))
4880               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4881             (setq range (cdr range)))
4882           (setq num (- (cdr active) num))))
4883         ;; Update the number of unread articles.
4884         (setcar entry num)
4885         ;; Update the group buffer.
4886         (gnus-group-update-group group t)))))
4887
4888 (defvar gnus-newsgroup-none-id 0)
4889
4890 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4891   (let ((cur nntp-server-buffer)
4892         (dependencies
4893          (or dependencies
4894              (save-excursion (set-buffer gnus-summary-buffer)
4895                              gnus-newsgroup-dependencies)))
4896         headers id end ref
4897         (mail-parse-charset gnus-newsgroup-charset)
4898         (mail-parse-ignored-charsets
4899          (save-excursion (condition-case nil
4900                              (set-buffer gnus-summary-buffer)
4901                            (error))
4902                          gnus-newsgroup-ignored-charsets)))
4903     (save-excursion
4904       (set-buffer nntp-server-buffer)
4905       ;; Translate all TAB characters into SPACE characters.
4906       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4907       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4908       (gnus-run-hooks 'gnus-parse-headers-hook)
4909       (let ((case-fold-search t)
4910             in-reply-to header p lines chars)
4911         (goto-char (point-min))
4912         ;; Search to the beginning of the next header.  Error messages
4913         ;; do not begin with 2 or 3.
4914         (while (re-search-forward "^[23][0-9]+ " nil t)
4915           (setq id nil
4916                 ref nil)
4917           ;; This implementation of this function, with nine
4918           ;; search-forwards instead of the one re-search-forward and
4919           ;; a case (which basically was the old function) is actually
4920           ;; about twice as fast, even though it looks messier.  You
4921           ;; can't have everything, I guess.  Speed and elegance
4922           ;; doesn't always go hand in hand.
4923           (setq
4924            header
4925            (vector
4926             ;; Number.
4927             (prog1
4928                 (read cur)
4929               (end-of-line)
4930               (setq p (point))
4931               (narrow-to-region (point)
4932                                 (or (and (search-forward "\n.\n" nil t)
4933                                          (- (point) 2))
4934                                     (point))))
4935             ;; Subject.
4936             (progn
4937               (goto-char p)
4938               (if (search-forward "\nsubject: " nil t)
4939                   (funcall gnus-decode-encoded-word-function
4940                            (nnheader-header-value))
4941                 "(none)"))
4942             ;; From.
4943             (progn
4944               (goto-char p)
4945               (if (or (search-forward "\nfrom: " nil t)
4946                       (search-forward "\nfrom:" nil t))
4947                   (funcall gnus-decode-encoded-word-function
4948                            (nnheader-header-value))
4949                 "(nobody)"))
4950             ;; Date.
4951             (progn
4952               (goto-char p)
4953               (if (search-forward "\ndate: " nil t)
4954                   (nnheader-header-value) ""))
4955             ;; Message-ID.
4956             (progn
4957               (goto-char p)
4958               (setq id (if (re-search-forward
4959                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4960                            ;; We do it this way to make sure the Message-ID
4961                            ;; is (somewhat) syntactically valid.
4962                            (buffer-substring (match-beginning 1)
4963                                              (match-end 1))
4964                          ;; If there was no message-id, we just fake one
4965                          ;; to make subsequent routines simpler.
4966                          (nnheader-generate-fake-message-id))))
4967             ;; References.
4968             (progn
4969               (goto-char p)
4970               (if (search-forward "\nreferences: " nil t)
4971                   (progn
4972                     (setq end (point))
4973                     (prog1
4974                         (nnheader-header-value)
4975                       (setq ref
4976                             (buffer-substring
4977                              (progn
4978                                (end-of-line)
4979                                (search-backward ">" end t)
4980                                (1+ (point)))
4981                              (progn
4982                                (search-backward "<" end t)
4983                                (point))))))
4984                 ;; Get the references from the in-reply-to header if there
4985                 ;; were no references and the in-reply-to header looks
4986                 ;; promising.
4987                 (if (and (search-forward "\nin-reply-to: " nil t)
4988                          (setq in-reply-to (nnheader-header-value))
4989                          (string-match "<[^>]+>" in-reply-to))
4990                     (let (ref2)
4991                       (setq ref (substring in-reply-to (match-beginning 0)
4992                                            (match-end 0)))
4993                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4994                         (setq ref2 (substring in-reply-to (match-beginning 0)
4995                                               (match-end 0)))
4996                         (when (> (length ref2) (length ref))
4997                           (setq ref ref2)))
4998                       ref)
4999                   (setq ref nil))))
5000             ;; Chars.
5001             (progn
5002               (goto-char p)
5003               (if (search-forward "\nchars: " nil t)
5004                   (if (numberp (setq chars (ignore-errors (read cur))))
5005                       chars 0)
5006                 0))
5007             ;; Lines.
5008             (progn
5009               (goto-char p)
5010               (if (search-forward "\nlines: " nil t)
5011                   (if (numberp (setq lines (ignore-errors (read cur))))
5012                       lines 0)
5013                 0))
5014             ;; Xref.
5015             (progn
5016               (goto-char p)
5017               (and (search-forward "\nxref: " nil t)
5018                    (nnheader-header-value)))
5019             ;; Extra.
5020             (when gnus-extra-headers
5021               (let ((extra gnus-extra-headers)
5022                     out)
5023                 (while extra
5024                   (goto-char p)
5025                   (when (search-forward
5026                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5027                     (push (cons (car extra) (nnheader-header-value))
5028                           out))
5029                   (pop extra))
5030                 out))))
5031           (when (equal id ref)
5032             (setq ref nil))
5033
5034           (when gnus-alter-header-function
5035             (funcall gnus-alter-header-function header)
5036             (setq id (mail-header-id header)
5037                   ref (gnus-parent-id (mail-header-references header))))
5038
5039           (when (setq header
5040                       (gnus-dependencies-add-header
5041                        header dependencies force-new))
5042             (push header headers))
5043           (goto-char (point-max))
5044           (widen))
5045         (nreverse headers)))))
5046
5047 ;; Goes through the xover lines and returns a list of vectors
5048 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5049                                                   force-new dependencies
5050                                                   group also-fetch-heads)
5051   "Parse the news overview data in the server buffer.
5052 Return a list of headers that match SEQUENCE (see
5053 `nntp-retrieve-headers')."
5054   ;; Get the Xref when the users reads the articles since most/some
5055   ;; NNTP servers do not include Xrefs when using XOVER.
5056   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5057   (let ((mail-parse-charset gnus-newsgroup-charset)
5058         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5059         (cur nntp-server-buffer)
5060         (dependencies (or dependencies gnus-newsgroup-dependencies))
5061         number headers header)
5062     (save-excursion
5063       (set-buffer nntp-server-buffer)
5064       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5065       ;; Allow the user to mangle the headers before parsing them.
5066       (gnus-run-hooks 'gnus-parse-headers-hook)
5067       (goto-char (point-min))
5068       (while (not (eobp))
5069         (condition-case ()
5070             (while (and sequence (not (eobp)))
5071               (setq number (read cur))
5072               (while (and sequence
5073                           (< (car sequence) number))
5074                 (setq sequence (cdr sequence)))
5075               (and sequence
5076                    (eq number (car sequence))
5077                    (progn
5078                      (setq sequence (cdr sequence))
5079                      (setq header (inline
5080                                     (gnus-nov-parse-line
5081                                      number dependencies force-new))))
5082                    (push header headers))
5083               (forward-line 1))
5084           (error
5085            (gnus-error 4 "Strange nov line (%d)"
5086                        (count-lines (point-min) (point)))))
5087         (forward-line 1))
5088       ;; A common bug in inn is that if you have posted an article and
5089       ;; then retrieves the active file, it will answer correctly --
5090       ;; the new article is included.  However, a NOV entry for the
5091       ;; article may not have been generated yet, so this may fail.
5092       ;; We work around this problem by retrieving the last few
5093       ;; headers using HEAD.
5094       (if (or (not also-fetch-heads)
5095               (not sequence))
5096           ;; We (probably) got all the headers.
5097           (nreverse headers)
5098         (let ((gnus-nov-is-evil t))
5099           (nconc
5100            (nreverse headers)
5101            (when (gnus-retrieve-headers sequence group)
5102              (gnus-get-newsgroup-headers))))))))
5103
5104 (defun gnus-article-get-xrefs ()
5105   "Fill in the Xref value in `gnus-current-headers', if necessary.
5106 This is meant to be called in `gnus-article-internal-prepare-hook'."
5107   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5108                                  gnus-current-headers)))
5109     (or (not gnus-use-cross-reference)
5110         (not headers)
5111         (and (mail-header-xref headers)
5112              (not (string= (mail-header-xref headers) "")))
5113         (let ((case-fold-search t)
5114               xref)
5115           (save-restriction
5116             (nnheader-narrow-to-headers)
5117             (goto-char (point-min))
5118             (when (or (and (not (eobp))
5119                            (eq (downcase (char-after)) ?x)
5120                            (looking-at "Xref:"))
5121                       (search-forward "\nXref:" nil t))
5122               (goto-char (1+ (match-end 0)))
5123               (setq xref (buffer-substring (point)
5124                                            (progn (end-of-line) (point))))
5125               (mail-header-set-xref headers xref)))))))
5126
5127 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5128   "Find article ID and insert the summary line for that article.
5129 OLD-HEADER can either be a header or a line number to insert
5130 the subject line on."
5131   (let* ((line (and (numberp old-header) old-header))
5132          (old-header (and (vectorp old-header) old-header))
5133          (header (cond ((and old-header use-old-header)
5134                         old-header)
5135                        ((and (numberp id)
5136                              (gnus-number-to-header id))
5137                         (gnus-number-to-header id))
5138                        (t
5139                         (gnus-read-header id))))
5140          (number (and (numberp id) id))
5141          d)
5142     (when header
5143       ;; Rebuild the thread that this article is part of and go to the
5144       ;; article we have fetched.
5145       (when (and (not gnus-show-threads)
5146                  old-header)
5147         (when (and number
5148                    (setq d (gnus-data-find (mail-header-number old-header))))
5149           (goto-char (gnus-data-pos d))
5150           (gnus-data-remove
5151            number
5152            (- (gnus-point-at-bol)
5153               (prog1
5154                   (1+ (gnus-point-at-eol))
5155                 (gnus-delete-line))))))
5156       (when old-header
5157         (mail-header-set-number header (mail-header-number old-header)))
5158       (setq gnus-newsgroup-sparse
5159             (delq (setq number (mail-header-number header))
5160                   gnus-newsgroup-sparse))
5161       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5162       (push number gnus-newsgroup-limit)
5163       (gnus-rebuild-thread (mail-header-id header) line)
5164       (gnus-summary-goto-subject number nil t))
5165     (when (and (numberp number)
5166                (> number 0))
5167       ;; We have to update the boundaries even if we can't fetch the
5168       ;; article if ID is a number -- so that the next `P' or `N'
5169       ;; command will fetch the previous (or next) article even
5170       ;; if the one we tried to fetch this time has been canceled.
5171       (when (> number gnus-newsgroup-end)
5172         (setq gnus-newsgroup-end number))
5173       (when (< number gnus-newsgroup-begin)
5174         (setq gnus-newsgroup-begin number))
5175       (setq gnus-newsgroup-unselected
5176             (delq number gnus-newsgroup-unselected)))
5177     ;; Report back a success?
5178     (and header (mail-header-number header))))
5179
5180 ;;; Process/prefix in the summary buffer
5181
5182 (defun gnus-summary-work-articles (n)
5183   "Return a list of articles to be worked upon.
5184 The prefix argument, the list of process marked articles, and the
5185 current article will be taken into consideration."
5186   (save-excursion
5187     (set-buffer gnus-summary-buffer)
5188     (cond
5189      (n
5190       ;; A numerical prefix has been given.
5191       (setq n (prefix-numeric-value n))
5192       (let ((backward (< n 0))
5193             (n (abs (prefix-numeric-value n)))
5194             articles article)
5195         (save-excursion
5196           (while
5197               (and (> n 0)
5198                    (push (setq article (gnus-summary-article-number))
5199                          articles)
5200                    (if backward
5201                        (gnus-summary-find-prev nil article)
5202                      (gnus-summary-find-next nil article)))
5203             (decf n)))
5204         (nreverse articles)))
5205      ((and (gnus-region-active-p) (mark))
5206       (message "region active")
5207       ;; Work on the region between point and mark.
5208       (let ((max (max (point) (mark)))
5209             articles article)
5210         (save-excursion
5211           (goto-char (min (min (point) (mark))))
5212           (while
5213               (and
5214                (push (setq article (gnus-summary-article-number)) articles)
5215                (gnus-summary-find-next nil article)
5216                (< (point) max)))
5217           (nreverse articles))))
5218      (gnus-newsgroup-processable
5219       ;; There are process-marked articles present.
5220       ;; Save current state.
5221       (gnus-summary-save-process-mark)
5222       ;; Return the list.
5223       (reverse gnus-newsgroup-processable))
5224      (t
5225       ;; Just return the current article.
5226       (list (gnus-summary-article-number))))))
5227
5228 (defmacro gnus-summary-iterate (arg &rest forms)
5229   "Iterate over the process/prefixed articles and do FORMS.
5230 ARG is the interactive prefix given to the command.  FORMS will be
5231 executed with point over the summary line of the articles."
5232   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5233     `(let ((,articles (gnus-summary-work-articles ,arg)))
5234        (while ,articles
5235          (gnus-summary-goto-subject (car ,articles))
5236          ,@forms
5237          (pop ,articles)))))
5238
5239 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5240 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5241
5242 (defun gnus-summary-save-process-mark ()
5243   "Push the current set of process marked articles on the stack."
5244   (interactive)
5245   (push (copy-sequence gnus-newsgroup-processable)
5246         gnus-newsgroup-process-stack))
5247
5248 (defun gnus-summary-kill-process-mark ()
5249   "Push the current set of process marked articles on the stack and unmark."
5250   (interactive)
5251   (gnus-summary-save-process-mark)
5252   (gnus-summary-unmark-all-processable))
5253
5254 (defun gnus-summary-yank-process-mark ()
5255   "Pop the last process mark state off the stack and restore it."
5256   (interactive)
5257   (unless gnus-newsgroup-process-stack
5258     (error "Empty mark stack"))
5259   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5260
5261 (defun gnus-summary-process-mark-set (set)
5262   "Make SET into the current process marked articles."
5263   (gnus-summary-unmark-all-processable)
5264   (while set
5265     (gnus-summary-set-process-mark (pop set))))
5266
5267 ;;; Searching and stuff
5268
5269 (defun gnus-summary-search-group (&optional backward use-level)
5270   "Search for next unread newsgroup.
5271 If optional argument BACKWARD is non-nil, search backward instead."
5272   (save-excursion
5273     (set-buffer gnus-group-buffer)
5274     (when (gnus-group-search-forward
5275            backward nil (if use-level (gnus-group-group-level) nil))
5276       (gnus-group-group-name))))
5277
5278 (defun gnus-summary-best-group (&optional exclude-group)
5279   "Find the name of the best unread group.
5280 If EXCLUDE-GROUP, do not go to this group."
5281   (save-excursion
5282     (set-buffer gnus-group-buffer)
5283     (save-excursion
5284       (gnus-group-best-unread-group exclude-group))))
5285
5286 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5287   (if backward (gnus-summary-find-prev)
5288     (let* ((dummy (gnus-summary-article-intangible-p))
5289            (article (or article (gnus-summary-article-number)))
5290            (arts (gnus-data-find-list article))
5291            result)
5292       (when (and (not dummy)
5293                  (or (not gnus-summary-check-current)
5294                      (not unread)
5295                      (not (gnus-data-unread-p (car arts)))))
5296         (setq arts (cdr arts)))
5297       (when (setq result
5298                   (if unread
5299                       (progn
5300                         (while arts
5301                           (when (or (and undownloaded
5302                                          (eq gnus-undownloaded-mark
5303                                              (gnus-data-mark (car arts))))
5304                                     (gnus-data-unread-p (car arts)))
5305                             (setq result (car arts)
5306                                   arts nil))
5307                           (setq arts (cdr arts)))
5308                         result)
5309                     (car arts)))
5310         (goto-char (gnus-data-pos result))
5311         (gnus-data-number result)))))
5312
5313 (defun gnus-summary-find-prev (&optional unread article)
5314   (let* ((eobp (eobp))
5315          (article (or article (gnus-summary-article-number)))
5316          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5317          result)
5318     (when (and (not eobp)
5319                (or (not gnus-summary-check-current)
5320                    (not unread)
5321                    (not (gnus-data-unread-p (car arts)))))
5322       (setq arts (cdr arts)))
5323     (when (setq result
5324                 (if unread
5325                     (progn
5326                       (while arts
5327                         (when (gnus-data-unread-p (car arts))
5328                           (setq result (car arts)
5329                                 arts nil))
5330                         (setq arts (cdr arts)))
5331                       result)
5332                   (car arts)))
5333       (goto-char (gnus-data-pos result))
5334       (gnus-data-number result))))
5335
5336 (defun gnus-summary-find-subject (subject &optional unread backward article)
5337   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5338          (article (or article (gnus-summary-article-number)))
5339          (articles (gnus-data-list backward))
5340          (arts (gnus-data-find-list article articles))
5341          result)
5342     (when (or (not gnus-summary-check-current)
5343               (not unread)
5344               (not (gnus-data-unread-p (car arts))))
5345       (setq arts (cdr arts)))
5346     (while arts
5347       (and (or (not unread)
5348                (gnus-data-unread-p (car arts)))
5349            (vectorp (gnus-data-header (car arts)))
5350            (gnus-subject-equal
5351             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5352            (setq result (car arts)
5353                  arts nil))
5354       (setq arts (cdr arts)))
5355     (and result
5356          (goto-char (gnus-data-pos result))
5357          (gnus-data-number result))))
5358
5359 (defun gnus-summary-search-forward (&optional unread subject backward)
5360   "Search forward for an article.
5361 If UNREAD, look for unread articles.  If SUBJECT, look for
5362 articles with that subject.  If BACKWARD, search backward instead."
5363   (cond (subject (gnus-summary-find-subject subject unread backward))
5364         (backward (gnus-summary-find-prev unread))
5365         (t (gnus-summary-find-next unread))))
5366
5367 (defun gnus-recenter (&optional n)
5368   "Center point in window and redisplay frame.
5369 Also do horizontal recentering."
5370   (interactive "P")
5371   (when (and gnus-auto-center-summary
5372              (not (eq gnus-auto-center-summary 'vertical)))
5373     (gnus-horizontal-recenter))
5374   (recenter n))
5375
5376 (defun gnus-summary-recenter ()
5377   "Center point in the summary window.
5378 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5379 displayed, no centering will be performed."
5380   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5381   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5382   (interactive)
5383   (let* ((top (cond ((< (window-height) 4) 0)
5384                     ((< (window-height) 7) 1)
5385                     (t (if (numberp gnus-auto-center-summary)
5386                            gnus-auto-center-summary
5387                          2))))
5388          (height (1- (window-height)))
5389          (bottom (save-excursion (goto-char (point-max))
5390                                  (forward-line (- height))
5391                                  (point)))
5392          (window (get-buffer-window (current-buffer))))
5393     ;; The user has to want it.
5394     (when gnus-auto-center-summary
5395       (when (get-buffer-window gnus-article-buffer)
5396         ;; Only do recentering when the article buffer is displayed,
5397         ;; Set the window start to either `bottom', which is the biggest
5398         ;; possible valid number, or the second line from the top,
5399         ;; whichever is the least.
5400         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5401           (if (> bottom top-pos)
5402               ;; Keep the second line from the top visible
5403               (set-window-start window top-pos t)
5404             ;; Try to keep the bottom line visible; if it's partially
5405             ;; obscured, either scroll one more line to make it fully
5406             ;; visible, or revert to using TOP-POS.
5407             (save-excursion
5408               (goto-char (point-max))
5409               (forward-line -1)
5410               (let ((last-line-start (point)))
5411                 (goto-char bottom)
5412                 (set-window-start window (point) t)
5413                 (when (not (pos-visible-in-window-p last-line-start window))
5414                   (forward-line 1)
5415                   (set-window-start window (min (point) top-pos) t)))))))
5416       ;; Do horizontal recentering while we're at it.
5417       (when (and (get-buffer-window (current-buffer) t)
5418                  (not (eq gnus-auto-center-summary 'vertical)))
5419         (let ((selected (selected-window)))
5420           (select-window (get-buffer-window (current-buffer) t))
5421           (gnus-summary-position-point)
5422           (gnus-horizontal-recenter)
5423           (select-window selected))))))
5424
5425 (defun gnus-summary-jump-to-group (newsgroup)
5426   "Move point to NEWSGROUP in group mode buffer."
5427   ;; Keep update point of group mode buffer if visible.
5428   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5429       (save-window-excursion
5430         ;; Take care of tree window mode.
5431         (when (get-buffer-window gnus-group-buffer)
5432           (pop-to-buffer gnus-group-buffer))
5433         (gnus-group-jump-to-group newsgroup))
5434     (save-excursion
5435       ;; Take care of tree window mode.
5436       (if (get-buffer-window gnus-group-buffer)
5437           (pop-to-buffer gnus-group-buffer)
5438         (set-buffer gnus-group-buffer))
5439       (gnus-group-jump-to-group newsgroup))))
5440
5441 ;; This function returns a list of article numbers based on the
5442 ;; difference between the ranges of read articles in this group and
5443 ;; the range of active articles.
5444 (defun gnus-list-of-unread-articles (group)
5445   (let* ((read (gnus-info-read (gnus-get-info group)))
5446          (active (or (gnus-active group) (gnus-activate-group group)))
5447          (last (cdr active))
5448          first nlast unread)
5449     ;; If none are read, then all are unread.
5450     (if (not read)
5451         (setq first (car active))
5452       ;; If the range of read articles is a single range, then the
5453       ;; first unread article is the article after the last read
5454       ;; article.  Sounds logical, doesn't it?
5455       (if (and (not (listp (cdr read)))
5456                (or (< (car read) (car active))
5457                    (progn (setq read (list read))
5458                           nil)))
5459           (setq first (max (car active) (1+ (cdr read))))
5460         ;; `read' is a list of ranges.
5461         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5462                                   (caar read)))
5463                   1)
5464           (setq first (car active)))
5465         (while read
5466           (when first
5467             (while (< first nlast)
5468               (push first unread)
5469               (setq first (1+ first))))
5470           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5471           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5472           (setq read (cdr read)))))
5473     ;; And add the last unread articles.
5474     (while (<= first last)
5475       (push first unread)
5476       (setq first (1+ first)))
5477     ;; Return the list of unread articles.
5478     (delq 0 (nreverse unread))))
5479
5480 (defun gnus-list-of-read-articles (group)
5481   "Return a list of unread, unticked and non-dormant articles."
5482   (let* ((info (gnus-get-info group))
5483          (marked (gnus-info-marks info))
5484          (active (gnus-active group)))
5485     (and info active
5486          (gnus-set-difference
5487           (gnus-sorted-complement
5488            (gnus-uncompress-range active)
5489            (gnus-list-of-unread-articles group))
5490           (append
5491            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5492            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5493
5494 ;; Various summary commands
5495
5496 (defun gnus-summary-select-article-buffer ()
5497   "Reconfigure windows to show article buffer."
5498   (interactive)
5499   (if (not (gnus-buffer-live-p gnus-article-buffer))
5500       (error "There is no article buffer for this summary buffer")
5501     (gnus-configure-windows 'article)
5502     (select-window (get-buffer-window gnus-article-buffer))))
5503
5504 (defun gnus-summary-universal-argument (arg)
5505   "Perform any operation on all articles that are process/prefixed."
5506   (interactive "P")
5507   (let ((articles (gnus-summary-work-articles arg))
5508         func article)
5509     (if (eq
5510          (setq
5511           func
5512           (key-binding
5513            (read-key-sequence
5514             (substitute-command-keys
5515              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5516          'undefined)
5517         (gnus-error 1 "Undefined key")
5518       (save-excursion
5519         (while articles
5520           (gnus-summary-goto-subject (setq article (pop articles)))
5521           (let (gnus-newsgroup-processable)
5522             (command-execute func))
5523           (gnus-summary-remove-process-mark article)))))
5524   (gnus-summary-position-point))
5525
5526 (defun gnus-summary-toggle-truncation (&optional arg)
5527   "Toggle truncation of summary lines.
5528 With arg, turn line truncation on iff arg is positive."
5529   (interactive "P")
5530   (setq truncate-lines
5531         (if (null arg) (not truncate-lines)
5532           (> (prefix-numeric-value arg) 0)))
5533   (redraw-display))
5534
5535 (defun gnus-summary-reselect-current-group (&optional all rescan)
5536   "Exit and then reselect the current newsgroup.
5537 The prefix argument ALL means to select all articles."
5538   (interactive "P")
5539   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5540     (error "Ephemeral groups can't be reselected"))
5541   (let ((current-subject (gnus-summary-article-number))
5542         (group gnus-newsgroup-name))
5543     (setq gnus-newsgroup-begin nil)
5544     (gnus-summary-exit)
5545     ;; We have to adjust the point of group mode buffer because
5546     ;; point was moved to the next unread newsgroup by exiting.
5547     (gnus-summary-jump-to-group group)
5548     (when rescan
5549       (save-excursion
5550         (gnus-group-get-new-news-this-group 1)))
5551     (gnus-group-read-group all t)
5552     (gnus-summary-goto-subject current-subject nil t)))
5553
5554 (defun gnus-summary-rescan-group (&optional all)
5555   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5556   (interactive "P")
5557   (gnus-summary-reselect-current-group all t))
5558
5559 (defun gnus-summary-update-info (&optional non-destructive)
5560   (save-excursion
5561     (let ((group gnus-newsgroup-name))
5562       (when group
5563         (when gnus-newsgroup-kill-headers
5564           (setq gnus-newsgroup-killed
5565                 (gnus-compress-sequence
5566                  (nconc
5567                   (gnus-set-sorted-intersection
5568                    (gnus-uncompress-range gnus-newsgroup-killed)
5569                    (setq gnus-newsgroup-unselected
5570                          (sort gnus-newsgroup-unselected '<)))
5571                   (setq gnus-newsgroup-unreads
5572                         (sort gnus-newsgroup-unreads '<)))
5573                  t)))
5574         (unless (listp (cdr gnus-newsgroup-killed))
5575           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5576         (let ((headers gnus-newsgroup-headers))
5577           ;; Set the new ranges of read articles.
5578           (save-excursion
5579             (set-buffer gnus-group-buffer)
5580             (gnus-undo-force-boundary))
5581           (gnus-update-read-articles
5582            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5583           ;; Set the current article marks.
5584           (let ((gnus-newsgroup-scored
5585                  (if (and (not gnus-save-score)
5586                           (not non-destructive))
5587                      nil
5588                    gnus-newsgroup-scored)))
5589             (save-excursion
5590               (gnus-update-marks)))
5591           ;; Do the cross-ref thing.
5592           (when gnus-use-cross-reference
5593             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5594           ;; Do not switch windows but change the buffer to work.
5595           (set-buffer gnus-group-buffer)
5596           (unless (gnus-ephemeral-group-p group)
5597             (gnus-group-update-group group)))))))
5598
5599 (defun gnus-summary-save-newsrc (&optional force)
5600   "Save the current number of read/marked articles in the dribble buffer.
5601 The dribble buffer will then be saved.
5602 If FORCE (the prefix), also save the .newsrc file(s)."
5603   (interactive "P")
5604   (gnus-summary-update-info t)
5605   (if force
5606       (gnus-save-newsrc-file)
5607     (gnus-dribble-save)))
5608
5609 (defun gnus-summary-exit (&optional temporary)
5610   "Exit reading current newsgroup, and then return to group selection mode.
5611 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5612   (interactive)
5613   (gnus-set-global-variables)
5614   (when (gnus-buffer-live-p gnus-article-buffer)
5615     (save-excursion
5616       (set-buffer gnus-article-buffer)
5617       (mm-destroy-parts gnus-article-mime-handles)
5618       ;; Set it to nil for safety reason.
5619       (setq gnus-article-mime-handle-alist nil)
5620       (setq gnus-article-mime-handles nil)))
5621   (gnus-kill-save-kill-buffer)
5622   (gnus-async-halt-prefetch)
5623   (let* ((group gnus-newsgroup-name)
5624          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5625          (mode major-mode)
5626          (group-point nil)
5627          (buf (current-buffer)))
5628     (unless quit-config
5629       ;; Do adaptive scoring, and possibly save score files.
5630       (when gnus-newsgroup-adaptive
5631         (gnus-score-adaptive))
5632       (when gnus-use-scoring
5633         (gnus-score-save)))
5634     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5635     ;; If we have several article buffers, we kill them at exit.
5636     (unless gnus-single-article-buffer
5637       (gnus-kill-buffer gnus-original-article-buffer)
5638       (setq gnus-article-current nil))
5639     (when gnus-use-cache
5640       (gnus-cache-possibly-remove-articles)
5641       (gnus-cache-save-buffers))
5642     (gnus-async-prefetch-remove-group group)
5643     (when gnus-suppress-duplicates
5644       (gnus-dup-enter-articles))
5645     (when gnus-use-trees
5646       (gnus-tree-close group))
5647     (when gnus-use-cache
5648       (gnus-cache-write-active))
5649     ;; Remove entries for this group.
5650     (nnmail-purge-split-history (gnus-group-real-name group))
5651     ;; Make all changes in this group permanent.
5652     (unless quit-config
5653       (gnus-run-hooks 'gnus-exit-group-hook)
5654       (gnus-summary-update-info))
5655     (gnus-close-group group)
5656     ;; Make sure where we were, and go to next newsgroup.
5657     (set-buffer gnus-group-buffer)
5658     (unless quit-config
5659       (gnus-group-jump-to-group group))
5660     (gnus-run-hooks 'gnus-summary-exit-hook)
5661     (unless (or quit-config
5662                 ;; If this group has disappeared from the summary
5663                 ;; buffer, don't skip forwards.
5664                 (not (string= group (gnus-group-group-name))))
5665       (gnus-group-next-unread-group 1))
5666     (setq group-point (point))
5667     (if temporary
5668         nil                             ;Nothing to do.
5669       ;; If we have several article buffers, we kill them at exit.
5670       (unless gnus-single-article-buffer
5671         (gnus-kill-buffer gnus-article-buffer)
5672         (gnus-kill-buffer gnus-original-article-buffer)
5673         (setq gnus-article-current nil))
5674       (set-buffer buf)
5675       (if (not gnus-kill-summary-on-exit)
5676           (gnus-deaden-summary)
5677         ;; We set all buffer-local variables to nil.  It is unclear why
5678         ;; this is needed, but if we don't, buffer-local variables are
5679         ;; not garbage-collected, it seems.  This would the lead to en
5680         ;; ever-growing Emacs.
5681         (gnus-summary-clear-local-variables)
5682         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5683           (gnus-summary-clear-local-variables))
5684         (when (get-buffer gnus-article-buffer)
5685           (bury-buffer gnus-article-buffer))
5686         ;; We clear the global counterparts of the buffer-local
5687         ;; variables as well, just to be on the safe side.
5688         (set-buffer gnus-group-buffer)
5689         (gnus-summary-clear-local-variables)
5690         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5691           (gnus-summary-clear-local-variables))
5692         ;; Return to group mode buffer.
5693         (when (eq mode 'gnus-summary-mode)
5694           (gnus-kill-buffer buf)))
5695       (setq gnus-current-select-method gnus-select-method)
5696       (pop-to-buffer gnus-group-buffer)
5697       (if (not quit-config)
5698           (progn
5699             (goto-char group-point)
5700             (gnus-configure-windows 'group 'force))
5701         (gnus-handle-ephemeral-exit quit-config))
5702       ;; Clear the current group name.
5703       (unless quit-config
5704         (setq gnus-newsgroup-name nil)))))
5705
5706 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5707 (defun gnus-summary-exit-no-update (&optional no-questions)
5708   "Quit reading current newsgroup without updating read article info."
5709   (interactive)
5710   (let* ((group gnus-newsgroup-name)
5711          (quit-config (gnus-group-quit-config group)))
5712     (when (or no-questions
5713               gnus-expert-user
5714               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5715       (gnus-async-halt-prefetch)
5716       (mapcar 'funcall
5717               (delq 'gnus-summary-expire-articles
5718                     (copy-sequence gnus-summary-prepare-exit-hook)))
5719       (when (gnus-buffer-live-p gnus-article-buffer)
5720         (save-excursion
5721           (set-buffer gnus-article-buffer)
5722           (mm-destroy-parts gnus-article-mime-handles)
5723           ;; Set it to nil for safety reason.
5724           (setq gnus-article-mime-handle-alist nil)
5725           (setq gnus-article-mime-handles nil)))
5726       ;; If we have several article buffers, we kill them at exit.
5727       (unless gnus-single-article-buffer
5728         (gnus-kill-buffer gnus-article-buffer)
5729         (gnus-kill-buffer gnus-original-article-buffer)
5730         (setq gnus-article-current nil))
5731       (if (not gnus-kill-summary-on-exit)
5732           (gnus-deaden-summary)
5733         (gnus-close-group group)
5734         (gnus-summary-clear-local-variables)
5735         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5736           (gnus-summary-clear-local-variables))
5737         (set-buffer gnus-group-buffer)
5738         (gnus-summary-clear-local-variables)
5739         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5740           (gnus-summary-clear-local-variables))
5741         (when (get-buffer gnus-summary-buffer)
5742           (kill-buffer gnus-summary-buffer)))
5743       (unless gnus-single-article-buffer
5744         (setq gnus-article-current nil))
5745       (when gnus-use-trees
5746         (gnus-tree-close group))
5747       (gnus-async-prefetch-remove-group group)
5748       (when (get-buffer gnus-article-buffer)
5749         (bury-buffer gnus-article-buffer))
5750       ;; Return to the group buffer.
5751       (gnus-configure-windows 'group 'force)
5752       ;; Clear the current group name.
5753       (setq gnus-newsgroup-name nil)
5754       (when (equal (gnus-group-group-name) group)
5755         (gnus-group-next-unread-group 1))
5756       (when quit-config
5757         (gnus-handle-ephemeral-exit quit-config)))))
5758
5759 (defun gnus-handle-ephemeral-exit (quit-config)
5760   "Handle movement when leaving an ephemeral group.
5761 The state which existed when entering the ephemeral is reset."
5762   (if (not (buffer-name (car quit-config)))
5763       (gnus-configure-windows 'group 'force)
5764     (set-buffer (car quit-config))
5765     (cond ((eq major-mode 'gnus-summary-mode)
5766            (gnus-set-global-variables))
5767           ((eq major-mode 'gnus-article-mode)
5768            (save-excursion
5769              ;; The `gnus-summary-buffer' variable may point
5770              ;; to the old summary buffer when using a single
5771              ;; article buffer.
5772              (unless (gnus-buffer-live-p gnus-summary-buffer)
5773                (set-buffer gnus-group-buffer))
5774              (set-buffer gnus-summary-buffer)
5775              (gnus-set-global-variables))))
5776     (if (or (eq (cdr quit-config) 'article)
5777             (eq (cdr quit-config) 'pick))
5778         (progn
5779           ;; The current article may be from the ephemeral group
5780           ;; thus it is best that we reload this article
5781           (gnus-summary-show-article)
5782           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5783               (gnus-configure-windows 'pick 'force)
5784             (gnus-configure-windows (cdr quit-config) 'force)))
5785       (gnus-configure-windows (cdr quit-config) 'force))
5786     (when (eq major-mode 'gnus-summary-mode)
5787       (gnus-summary-next-subject 1 nil t)
5788       (gnus-summary-recenter)
5789       (gnus-summary-position-point))))
5790
5791 ;;; Dead summaries.
5792
5793 (defvar gnus-dead-summary-mode-map nil)
5794
5795 (unless gnus-dead-summary-mode-map
5796   (setq gnus-dead-summary-mode-map (make-keymap))
5797   (suppress-keymap gnus-dead-summary-mode-map)
5798   (substitute-key-definition
5799    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5800   (let ((keys '("\C-d" "\r" "\177" [delete])))
5801     (while keys
5802       (define-key gnus-dead-summary-mode-map
5803         (pop keys) 'gnus-summary-wake-up-the-dead))))
5804
5805 (defvar gnus-dead-summary-mode nil
5806   "Minor mode for Gnus summary buffers.")
5807
5808 (defun gnus-dead-summary-mode (&optional arg)
5809   "Minor mode for Gnus summary buffers."
5810   (interactive "P")
5811   (when (eq major-mode 'gnus-summary-mode)
5812     (make-local-variable 'gnus-dead-summary-mode)
5813     (setq gnus-dead-summary-mode
5814           (if (null arg) (not gnus-dead-summary-mode)
5815             (> (prefix-numeric-value arg) 0)))
5816     (when gnus-dead-summary-mode
5817       (gnus-add-minor-mode
5818        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5819
5820 (defun gnus-deaden-summary ()
5821   "Make the current summary buffer into a dead summary buffer."
5822   ;; Kill any previous dead summary buffer.
5823   (when (and gnus-dead-summary
5824              (buffer-name gnus-dead-summary))
5825     (save-excursion
5826       (set-buffer gnus-dead-summary)
5827       (when gnus-dead-summary-mode
5828         (kill-buffer (current-buffer)))))
5829   ;; Make this the current dead summary.
5830   (setq gnus-dead-summary (current-buffer))
5831   (gnus-dead-summary-mode 1)
5832   (let ((name (buffer-name)))
5833     (when (string-match "Summary" name)
5834       (rename-buffer
5835        (concat (substring name 0 (match-beginning 0)) "Dead "
5836                (substring name (match-beginning 0)))
5837        t)
5838       (bury-buffer))))
5839
5840 (defun gnus-kill-or-deaden-summary (buffer)
5841   "Kill or deaden the summary BUFFER."
5842   (save-excursion
5843     (when (and (buffer-name buffer)
5844                (not gnus-single-article-buffer))
5845       (save-excursion
5846         (set-buffer buffer)
5847         (gnus-kill-buffer gnus-article-buffer)
5848         (gnus-kill-buffer gnus-original-article-buffer)))
5849     (cond (gnus-kill-summary-on-exit
5850            (when (and gnus-use-trees
5851                       (gnus-buffer-exists-p buffer))
5852              (save-excursion
5853                (set-buffer buffer)
5854                (gnus-tree-close gnus-newsgroup-name)))
5855            (gnus-kill-buffer buffer))
5856           ((gnus-buffer-exists-p buffer)
5857            (save-excursion
5858              (set-buffer buffer)
5859              (gnus-deaden-summary))))))
5860
5861 (defun gnus-summary-wake-up-the-dead (&rest args)
5862   "Wake up the dead summary buffer."
5863   (interactive)
5864   (gnus-dead-summary-mode -1)
5865   (let ((name (buffer-name)))
5866     (when (string-match "Dead " name)
5867       (rename-buffer
5868        (concat (substring name 0 (match-beginning 0))
5869                (substring name (match-end 0)))
5870        t)))
5871   (gnus-message 3 "This dead summary is now alive again"))
5872
5873 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5874 (defun gnus-summary-fetch-faq (&optional faq-dir)
5875   "Fetch the FAQ for the current group.
5876 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5877 in."
5878   (interactive
5879    (list
5880     (when current-prefix-arg
5881       (completing-read
5882        "Faq dir: " (and (listp gnus-group-faq-directory)
5883                         (mapcar (lambda (file) (list file))
5884                                 gnus-group-faq-directory))))))
5885   (let (gnus-faq-buffer)
5886     (when (setq gnus-faq-buffer
5887                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5888       (gnus-configure-windows 'summary-faq))))
5889
5890 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5891 (defun gnus-summary-describe-group (&optional force)
5892   "Describe the current newsgroup."
5893   (interactive "P")
5894   (gnus-group-describe-group force gnus-newsgroup-name))
5895
5896 (defun gnus-summary-describe-briefly ()
5897   "Describe summary mode commands briefly."
5898   (interactive)
5899   (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")))
5900
5901 ;; Walking around group mode buffer from summary mode.
5902
5903 (defun gnus-summary-next-group (&optional no-article target-group backward)
5904   "Exit current newsgroup and then select next unread newsgroup.
5905 If prefix argument NO-ARTICLE is non-nil, no article is selected
5906 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5907 previous group instead."
5908   (interactive "P")
5909   ;; Stop pre-fetching.
5910   (gnus-async-halt-prefetch)
5911   (let ((current-group gnus-newsgroup-name)
5912         (current-buffer (current-buffer))
5913         entered)
5914     ;; First we semi-exit this group to update Xrefs and all variables.
5915     ;; We can't do a real exit, because the window conf must remain
5916     ;; the same in case the user is prompted for info, and we don't
5917     ;; want the window conf to change before that...
5918     (gnus-summary-exit t)
5919     (while (not entered)
5920       ;; Then we find what group we are supposed to enter.
5921       (set-buffer gnus-group-buffer)
5922       (gnus-group-jump-to-group current-group)
5923       (setq target-group
5924             (or target-group
5925                 (if (eq gnus-keep-same-level 'best)
5926                     (gnus-summary-best-group gnus-newsgroup-name)
5927                   (gnus-summary-search-group backward gnus-keep-same-level))))
5928       (if (not target-group)
5929           ;; There are no further groups, so we return to the group
5930           ;; buffer.
5931           (progn
5932             (gnus-message 5 "Returning to the group buffer")
5933             (setq entered t)
5934             (when (gnus-buffer-live-p current-buffer)
5935               (set-buffer current-buffer)
5936               (gnus-summary-exit))
5937             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5938         ;; We try to enter the target group.
5939         (gnus-group-jump-to-group target-group)
5940         (let ((unreads (gnus-group-group-unread)))
5941           (if (and (or (eq t unreads)
5942                        (and unreads (not (zerop unreads))))
5943                    (gnus-summary-read-group
5944                     target-group nil no-article
5945                     (and (buffer-name current-buffer) current-buffer)
5946                     nil backward))
5947               (setq entered t)
5948             (setq current-group target-group
5949                   target-group nil)))))))
5950
5951 (defun gnus-summary-prev-group (&optional no-article)
5952   "Exit current newsgroup and then select previous unread newsgroup.
5953 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5954   (interactive "P")
5955   (gnus-summary-next-group no-article nil t))
5956
5957 ;; Walking around summary lines.
5958
5959 (defun gnus-summary-first-subject (&optional unread undownloaded)
5960   "Go to the first unread subject.
5961 If UNREAD is non-nil, go to the first unread article.
5962 Returns the article selected or nil if there are no unread articles."
5963   (interactive "P")
5964   (prog1
5965       (cond
5966        ;; Empty summary.
5967        ((null gnus-newsgroup-data)
5968         (gnus-message 3 "No articles in the group")
5969         nil)
5970        ;; Pick the first article.
5971        ((not unread)
5972         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5973         (gnus-data-number (car gnus-newsgroup-data)))
5974        ;; No unread articles.
5975        ((null gnus-newsgroup-unreads)
5976         (gnus-message 3 "No more unread articles")
5977         nil)
5978        ;; Find the first unread article.
5979        (t
5980         (let ((data gnus-newsgroup-data))
5981           (while (and data
5982                       (and (not (and undownloaded
5983                                      (eq gnus-undownloaded-mark
5984                                          (gnus-data-mark (car data)))))
5985                            (not (gnus-data-unread-p (car data)))))
5986             (setq data (cdr data)))
5987           (when data
5988             (goto-char (gnus-data-pos (car data)))
5989             (gnus-data-number (car data))))))
5990     (gnus-summary-position-point)))
5991
5992 (defun gnus-summary-next-subject (n &optional unread dont-display)
5993   "Go to next N'th summary line.
5994 If N is negative, go to the previous N'th subject line.
5995 If UNREAD is non-nil, only unread articles are selected.
5996 The difference between N and the actual number of steps taken is
5997 returned."
5998   (interactive "p")
5999   (let ((backward (< n 0))
6000         (n (abs n)))
6001     (while (and (> n 0)
6002                 (if backward
6003                     (gnus-summary-find-prev unread)
6004                   (gnus-summary-find-next unread)))
6005       (unless (zerop (setq n (1- n)))
6006         (gnus-summary-show-thread)))
6007     (when (/= 0 n)
6008       (gnus-message 7 "No more%s articles"
6009                     (if unread " unread" "")))
6010     (unless dont-display
6011       (gnus-summary-recenter)
6012       (gnus-summary-position-point))
6013     n))
6014
6015 (defun gnus-summary-next-unread-subject (n)
6016   "Go to next N'th unread summary line."
6017   (interactive "p")
6018   (gnus-summary-next-subject n t))
6019
6020 (defun gnus-summary-prev-subject (n &optional unread)
6021   "Go to previous N'th summary line.
6022 If optional argument UNREAD is non-nil, only unread article is selected."
6023   (interactive "p")
6024   (gnus-summary-next-subject (- n) unread))
6025
6026 (defun gnus-summary-prev-unread-subject (n)
6027   "Go to previous N'th unread summary line."
6028   (interactive "p")
6029   (gnus-summary-next-subject (- n) t))
6030
6031 (defun gnus-summary-goto-subject (article &optional force silent)
6032   "Go the subject line of ARTICLE.
6033 If FORCE, also allow jumping to articles not currently shown."
6034   (interactive "nArticle number: ")
6035   (let ((b (point))
6036         (data (gnus-data-find article)))
6037     ;; We read in the article if we have to.
6038     (and (not data)
6039          force
6040          (gnus-summary-insert-subject
6041           article
6042           (if (or (numberp force) (vectorp force)) force)
6043           t)
6044          (setq data (gnus-data-find article)))
6045     (goto-char b)
6046     (if (not data)
6047         (progn
6048           (unless silent
6049             (gnus-message 3 "Can't find article %d" article))
6050           nil)
6051       (goto-char (gnus-data-pos data))
6052       (gnus-summary-position-point)
6053       article)))
6054
6055 ;; Walking around summary lines with displaying articles.
6056
6057 (defun gnus-summary-expand-window (&optional arg)
6058   "Make the summary buffer take up the entire Emacs frame.
6059 Given a prefix, will force an `article' buffer configuration."
6060   (interactive "P")
6061   (if arg
6062       (gnus-configure-windows 'article 'force)
6063     (gnus-configure-windows 'summary 'force)))
6064
6065 (defun gnus-summary-display-article (article &optional all-header)
6066   "Display ARTICLE in article buffer."
6067   (when (gnus-buffer-live-p gnus-article-buffer)
6068     (with-current-buffer gnus-article-buffer
6069       (mm-enable-multibyte-mule4)))
6070   (gnus-set-global-variables)
6071   (when (gnus-buffer-live-p gnus-article-buffer)
6072     (with-current-buffer gnus-article-buffer
6073       (setq gnus-article-charset gnus-newsgroup-charset)
6074       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6075       (mm-enable-multibyte-mule4)))
6076   (if (null article)
6077       nil
6078     (prog1
6079         (if gnus-summary-display-article-function
6080             (funcall gnus-summary-display-article-function article all-header)
6081           (gnus-article-prepare article all-header))
6082       (gnus-run-hooks 'gnus-select-article-hook)
6083       (when (and gnus-current-article
6084                  (not (zerop gnus-current-article)))
6085         (gnus-summary-goto-subject gnus-current-article))
6086       (gnus-summary-recenter)
6087       (when (and gnus-use-trees gnus-show-threads)
6088         (gnus-possibly-generate-tree article)
6089         (gnus-highlight-selected-tree article))
6090       ;; Successfully display article.
6091       (gnus-article-set-window-start
6092        (cdr (assq article gnus-newsgroup-bookmarks))))))
6093
6094 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6095   "Select the current article.
6096 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6097 non-nil, the article will be re-fetched even if it already present in
6098 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6099 be displayed."
6100   ;; Make sure we are in the summary buffer to work around bbdb bug.
6101   (unless (eq major-mode 'gnus-summary-mode)
6102     (set-buffer gnus-summary-buffer))
6103   (let ((article (or article (gnus-summary-article-number)))
6104         (all-headers (not (not all-headers))) ;Must be T or NIL.
6105         gnus-summary-display-article-function)
6106     (and (not pseudo)
6107          (gnus-summary-article-pseudo-p article)
6108          (error "This is a pseudo-article"))
6109     (save-excursion
6110       (set-buffer gnus-summary-buffer)
6111       (if (or (and gnus-single-article-buffer
6112                    (or (null gnus-current-article)
6113                        (null gnus-article-current)
6114                        (null (get-buffer gnus-article-buffer))
6115                        (not (eq article (cdr gnus-article-current)))
6116                        (not (equal (car gnus-article-current)
6117                                    gnus-newsgroup-name))))
6118               (and (not gnus-single-article-buffer)
6119                    (or (null gnus-current-article)
6120                        (not (eq gnus-current-article article))))
6121               force)
6122           ;; The requested article is different from the current article.
6123           (progn
6124             (gnus-summary-display-article article all-headers)
6125             (when (gnus-buffer-live-p gnus-article-buffer)
6126                (with-current-buffer gnus-article-buffer
6127                 (if (not gnus-article-decoded-p) ;; a local variable
6128                     (mm-disable-multibyte-mule4))))
6129             (when (or all-headers gnus-show-all-headers)
6130               (gnus-article-show-all-headers))
6131             (gnus-article-set-window-start
6132              (cdr (assq article gnus-newsgroup-bookmarks)))
6133             article)
6134         (when (or all-headers gnus-show-all-headers)
6135           (gnus-article-show-all-headers))
6136         'old))))
6137
6138 (defun gnus-summary-force-verify-and-decrypt ()
6139   (interactive)
6140   (let ((mm-verify-option 'known)
6141         (mm-decrypt-option 'known))
6142     (gnus-summary-select-article nil 'force)))
6143
6144 (defun gnus-summary-set-current-mark (&optional current-mark)
6145   "Obsolete function."
6146   nil)
6147
6148 (defun gnus-summary-next-article (&optional unread subject backward push)
6149   "Select the next article.
6150 If UNREAD, only unread articles are selected.
6151 If SUBJECT, only articles with SUBJECT are selected.
6152 If BACKWARD, the previous article is selected instead of the next."
6153   (interactive "P")
6154   (cond
6155    ;; Is there such an article?
6156    ((and (gnus-summary-search-forward unread subject backward)
6157          (or (gnus-summary-display-article (gnus-summary-article-number))
6158              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6159     (gnus-summary-position-point))
6160    ;; If not, we try the first unread, if that is wanted.
6161    ((and subject
6162          gnus-auto-select-same
6163          (gnus-summary-first-unread-article))
6164     (gnus-summary-position-point)
6165     (gnus-message 6 "Wrapped"))
6166    ;; Try to get next/previous article not displayed in this group.
6167    ((and gnus-auto-extend-newsgroup
6168          (not unread) (not subject))
6169     (gnus-summary-goto-article
6170      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6171      nil (count-lines (point-min) (point))))
6172    ;; Go to next/previous group.
6173    (t
6174     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6175       (gnus-summary-jump-to-group gnus-newsgroup-name))
6176     (let ((cmd last-command-char)
6177           (point
6178            (save-excursion
6179              (set-buffer gnus-group-buffer)
6180              (point)))
6181           (group
6182            (if (eq gnus-keep-same-level 'best)
6183                (gnus-summary-best-group gnus-newsgroup-name)
6184              (gnus-summary-search-group backward gnus-keep-same-level))))
6185       ;; For some reason, the group window gets selected.  We change
6186       ;; it back.
6187       (select-window (get-buffer-window (current-buffer)))
6188       ;; Select next unread newsgroup automagically.
6189       (cond
6190        ((or (not gnus-auto-select-next)
6191             (not cmd))
6192         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6193        ((or (eq gnus-auto-select-next 'quietly)
6194             (and (eq gnus-auto-select-next 'slightly-quietly)
6195                  push)
6196             (and (eq gnus-auto-select-next 'almost-quietly)
6197                  (gnus-summary-last-article-p)))
6198         ;; Select quietly.
6199         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6200             (gnus-summary-exit)
6201           (gnus-message 7 "No more%s articles (%s)..."
6202                         (if unread " unread" "")
6203                         (if group (concat "selecting " group)
6204                           "exiting"))
6205           (gnus-summary-next-group nil group backward)))
6206        (t
6207         (when (gnus-key-press-event-p last-input-event)
6208           (gnus-summary-walk-group-buffer
6209            gnus-newsgroup-name cmd unread backward point))))))))
6210
6211 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6212   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6213                       (?\C-p (gnus-group-prev-unread-group 1))))
6214         (cursor-in-echo-area t)
6215         keve key group ended)
6216     (save-excursion
6217       (set-buffer gnus-group-buffer)
6218       (goto-char start)
6219       (setq group
6220             (if (eq gnus-keep-same-level 'best)
6221                 (gnus-summary-best-group gnus-newsgroup-name)
6222               (gnus-summary-search-group backward gnus-keep-same-level))))
6223     (while (not ended)
6224       (gnus-message
6225        5 "No more%s articles%s" (if unread " unread" "")
6226        (if (and group
6227                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6228            (format " (Type %s for %s [%s])"
6229                    (single-key-description cmd) group
6230                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6231          (format " (Type %s to exit %s)"
6232                  (single-key-description cmd)
6233                  gnus-newsgroup-name)))
6234       ;; Confirm auto selection.
6235       (setq key (car (setq keve (gnus-read-event-char))))
6236       (setq ended t)
6237       (cond
6238        ((assq key keystrokes)
6239         (let ((obuf (current-buffer)))
6240           (switch-to-buffer gnus-group-buffer)
6241           (when group
6242             (gnus-group-jump-to-group group))
6243           (eval (cadr (assq key keystrokes)))
6244           (setq group (gnus-group-group-name))
6245           (switch-to-buffer obuf))
6246         (setq ended nil))
6247        ((equal key cmd)
6248         (if (or (not group)
6249                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6250             (gnus-summary-exit)
6251           (gnus-summary-next-group nil group backward)))
6252        (t
6253         (push (cdr keve) unread-command-events))))))
6254
6255 (defun gnus-summary-next-unread-article ()
6256   "Select unread article after current one."
6257   (interactive)
6258   (gnus-summary-next-article
6259    (or (not (eq gnus-summary-goto-unread 'never))
6260        (gnus-summary-last-article-p (gnus-summary-article-number)))
6261    (and gnus-auto-select-same
6262         (gnus-summary-article-subject))))
6263
6264 (defun gnus-summary-prev-article (&optional unread subject)
6265   "Select the article after the current one.
6266 If UNREAD is non-nil, only unread articles are selected."
6267   (interactive "P")
6268   (gnus-summary-next-article unread subject t))
6269
6270 (defun gnus-summary-prev-unread-article ()
6271   "Select unread article before current one."
6272   (interactive)
6273   (gnus-summary-prev-article
6274    (or (not (eq gnus-summary-goto-unread 'never))
6275        (gnus-summary-first-article-p (gnus-summary-article-number)))
6276    (and gnus-auto-select-same
6277         (gnus-summary-article-subject))))
6278
6279 (defun gnus-summary-next-page (&optional lines circular)
6280   "Show next page of the selected article.
6281 If at the end of the current article, select the next article.
6282 LINES says how many lines should be scrolled up.
6283
6284 If CIRCULAR is non-nil, go to the start of the article instead of
6285 selecting the next article when reaching the end of the current
6286 article."
6287   (interactive "P")
6288   (setq gnus-summary-buffer (current-buffer))
6289   (gnus-set-global-variables)
6290   (let ((article (gnus-summary-article-number))
6291         (article-window (get-buffer-window gnus-article-buffer t))
6292         endp)
6293     ;; If the buffer is empty, we have no article.
6294     (unless article
6295       (error "No article to select"))
6296     (gnus-configure-windows 'article)
6297     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6298         (if (and (eq gnus-summary-goto-unread 'never)
6299                  (not (gnus-summary-last-article-p article)))
6300             (gnus-summary-next-article)
6301           (gnus-summary-next-unread-article))
6302       (if (or (null gnus-current-article)
6303               (null gnus-article-current)
6304               (/= article (cdr gnus-article-current))
6305               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6306           ;; Selected subject is different from current article's.
6307           (gnus-summary-display-article article)
6308         (when article-window
6309           (gnus-eval-in-buffer-window gnus-article-buffer
6310             (setq endp (gnus-article-next-page lines)))
6311           (when endp
6312             (cond (circular
6313                    (gnus-summary-beginning-of-article))
6314                   (lines
6315                    (gnus-message 3 "End of message"))
6316                   ((null lines)
6317                    (if (and (eq gnus-summary-goto-unread 'never)
6318                             (not (gnus-summary-last-article-p article)))
6319                        (gnus-summary-next-article)
6320                      (gnus-summary-next-unread-article))))))))
6321     (gnus-summary-recenter)
6322     (gnus-summary-position-point)))
6323
6324 (defun gnus-summary-prev-page (&optional lines move)
6325   "Show previous page of selected article.
6326 Argument LINES specifies lines to be scrolled down.
6327 If MOVE, move to the previous unread article if point is at
6328 the beginning of the buffer."
6329   (interactive "P")
6330   (let ((article (gnus-summary-article-number))
6331         (article-window (get-buffer-window gnus-article-buffer t))
6332         endp)
6333     (gnus-configure-windows 'article)
6334     (if (or (null gnus-current-article)
6335             (null gnus-article-current)
6336             (/= article (cdr gnus-article-current))
6337             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6338         ;; Selected subject is different from current article's.
6339         (gnus-summary-display-article article)
6340       (gnus-summary-recenter)
6341       (when article-window
6342         (gnus-eval-in-buffer-window gnus-article-buffer
6343           (setq endp (gnus-article-prev-page lines)))
6344         (when (and move endp)
6345           (cond (lines
6346                  (gnus-message 3 "Beginning of message"))
6347                 ((null lines)
6348                  (if (and (eq gnus-summary-goto-unread 'never)
6349                           (not (gnus-summary-first-article-p article)))
6350                      (gnus-summary-prev-article)
6351                    (gnus-summary-prev-unread-article))))))))
6352   (gnus-summary-position-point))
6353
6354 (defun gnus-summary-prev-page-or-article (&optional lines)
6355   "Show previous page of selected article.
6356 Argument LINES specifies lines to be scrolled down.
6357 If at the beginning of the article, go to the next article."
6358   (interactive "P")
6359   (gnus-summary-prev-page lines t))
6360
6361 (defun gnus-summary-scroll-up (lines)
6362   "Scroll up (or down) one line current article.
6363 Argument LINES specifies lines to be scrolled up (or down if negative)."
6364   (interactive "p")
6365   (gnus-configure-windows 'article)
6366   (gnus-summary-show-thread)
6367   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6368     (gnus-eval-in-buffer-window gnus-article-buffer
6369       (cond ((> lines 0)
6370              (when (gnus-article-next-page lines)
6371                (gnus-message 3 "End of message")))
6372             ((< lines 0)
6373              (gnus-article-prev-page (- lines))))))
6374   (gnus-summary-recenter)
6375   (gnus-summary-position-point))
6376
6377 (defun gnus-summary-scroll-down (lines)
6378   "Scroll down (or up) one line current article.
6379 Argument LINES specifies lines to be scrolled down (or up if negative)."
6380   (interactive "p")
6381   (gnus-summary-scroll-up (- lines)))
6382
6383 (defun gnus-summary-next-same-subject ()
6384   "Select next article which has the same subject as current one."
6385   (interactive)
6386   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6387
6388 (defun gnus-summary-prev-same-subject ()
6389   "Select previous article which has the same subject as current one."
6390   (interactive)
6391   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6392
6393 (defun gnus-summary-next-unread-same-subject ()
6394   "Select next unread article which has the same subject as current one."
6395   (interactive)
6396   (gnus-summary-next-article t (gnus-summary-article-subject)))
6397
6398 (defun gnus-summary-prev-unread-same-subject ()
6399   "Select previous unread article which has the same subject as current one."
6400   (interactive)
6401   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6402
6403 (defun gnus-summary-first-unread-article ()
6404   "Select the first unread article.
6405 Return nil if there are no unread articles."
6406   (interactive)
6407   (prog1
6408       (when (gnus-summary-first-subject t)
6409         (gnus-summary-show-thread)
6410         (gnus-summary-first-subject t)
6411         (gnus-summary-display-article (gnus-summary-article-number)))
6412     (gnus-summary-position-point)))
6413
6414 (defun gnus-summary-first-unread-subject ()
6415   "Place the point on the subject line of the first unread article.
6416 Return nil if there are no unread articles."
6417   (interactive)
6418   (prog1
6419       (when (gnus-summary-first-subject t)
6420         (gnus-summary-show-thread)
6421         (gnus-summary-first-subject t))
6422     (gnus-summary-position-point)))
6423
6424 (defun gnus-summary-first-article ()
6425   "Select the first article.
6426 Return nil if there are no articles."
6427   (interactive)
6428   (prog1
6429       (when (gnus-summary-first-subject)
6430         (gnus-summary-show-thread)
6431         (gnus-summary-first-subject)
6432         (gnus-summary-display-article (gnus-summary-article-number)))
6433     (gnus-summary-position-point)))
6434
6435 (defun gnus-summary-best-unread-article ()
6436   "Select the unread article with the highest score."
6437   (interactive)
6438   (let ((best -1000000)
6439         (data gnus-newsgroup-data)
6440         article score)
6441     (while data
6442       (and (gnus-data-unread-p (car data))
6443            (> (setq score
6444                     (gnus-summary-article-score (gnus-data-number (car data))))
6445               best)
6446            (setq best score
6447                  article (gnus-data-number (car data))))
6448       (setq data (cdr data)))
6449     (prog1
6450         (if article
6451             (gnus-summary-goto-article article)
6452           (error "No unread articles"))
6453       (gnus-summary-position-point))))
6454
6455 (defun gnus-summary-last-subject ()
6456   "Go to the last displayed subject line in the group."
6457   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6458     (when article
6459       (gnus-summary-goto-subject article))))
6460
6461 (defun gnus-summary-goto-article (article &optional all-headers force)
6462   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6463 If ALL-HEADERS is non-nil, no header lines are hidden.
6464 If FORCE, go to the article even if it isn't displayed.  If FORCE
6465 is a number, it is the line the article is to be displayed on."
6466   (interactive
6467    (list
6468     (completing-read
6469      "Article number or Message-ID: "
6470      (mapcar (lambda (number) (list (int-to-string number)))
6471              gnus-newsgroup-limit))
6472     current-prefix-arg
6473     t))
6474   (prog1
6475       (if (and (stringp article)
6476                (string-match "@" article))
6477           (gnus-summary-refer-article article)
6478         (when (stringp article)
6479           (setq article (string-to-number article)))
6480         (if (gnus-summary-goto-subject article force)
6481             (gnus-summary-display-article article all-headers)
6482           (gnus-message 4 "Couldn't go to article %s" article) nil))
6483     (gnus-summary-position-point)))
6484
6485 (defun gnus-summary-goto-last-article ()
6486   "Go to the previously read article."
6487   (interactive)
6488   (prog1
6489       (when gnus-last-article
6490         (gnus-summary-goto-article gnus-last-article nil t))
6491     (gnus-summary-position-point)))
6492
6493 (defun gnus-summary-pop-article (number)
6494   "Pop one article off the history and go to the previous.
6495 NUMBER articles will be popped off."
6496   (interactive "p")
6497   (let (to)
6498     (setq gnus-newsgroup-history
6499           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6500     (if to
6501         (gnus-summary-goto-article (car to) nil t)
6502       (error "Article history empty")))
6503   (gnus-summary-position-point))
6504
6505 ;; Summary commands and functions for limiting the summary buffer.
6506
6507 (defun gnus-summary-limit-to-articles (n)
6508   "Limit the summary buffer to the next N articles.
6509 If not given a prefix, use the process marked articles instead."
6510   (interactive "P")
6511   (prog1
6512       (let ((articles (gnus-summary-work-articles n)))
6513         (setq gnus-newsgroup-processable nil)
6514         (gnus-summary-limit articles))
6515     (gnus-summary-position-point)))
6516
6517 (defun gnus-summary-pop-limit (&optional total)
6518   "Restore the previous limit.
6519 If given a prefix, remove all limits."
6520   (interactive "P")
6521   (when total
6522     (setq gnus-newsgroup-limits
6523           (list (mapcar (lambda (h) (mail-header-number h))
6524                         gnus-newsgroup-headers))))
6525   (unless gnus-newsgroup-limits
6526     (error "No limit to pop"))
6527   (prog1
6528       (gnus-summary-limit nil 'pop)
6529     (gnus-summary-position-point)))
6530
6531 (defun gnus-summary-limit-to-subject (subject &optional header)
6532   "Limit the summary buffer to articles that have subjects that match a regexp."
6533   (interactive "sLimit to subject (regexp): ")
6534   (unless header
6535     (setq header "subject"))
6536   (when (not (equal "" subject))
6537     (prog1
6538         (let ((articles (gnus-summary-find-matching
6539                          (or header "subject") subject 'all)))
6540           (unless articles
6541             (error "Found no matches for \"%s\"" subject))
6542           (gnus-summary-limit articles))
6543       (gnus-summary-position-point))))
6544
6545 (defun gnus-summary-limit-to-author (from)
6546   "Limit the summary buffer to articles that have authors that match a regexp."
6547   (interactive "sLimit to author (regexp): ")
6548   (gnus-summary-limit-to-subject from "from"))
6549
6550 (defun gnus-summary-limit-to-age (age &optional younger-p)
6551   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6552 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6553 articles that are younger than AGE days."
6554   (interactive
6555    (let ((younger current-prefix-arg)
6556          (days-got nil)
6557          days)
6558      (while (not days-got)
6559        (setq days (if younger
6560                       (read-string "Limit to articles within (in days): ")
6561                     (read-string "Limit to articles older than (in days): ")))
6562        (when (> (length days) 0)
6563          (setq days (read days)))
6564        (if (numberp days)
6565            (setq days-got t)
6566          (message "Please enter a number.")
6567          (sleep-for 1)))
6568      (list days younger)))
6569   (prog1
6570       (let ((data gnus-newsgroup-data)
6571             (cutoff (days-to-time age))
6572             articles d date is-younger)
6573         (while (setq d (pop data))
6574           (when (and (vectorp (gnus-data-header d))
6575                      (setq date (mail-header-date (gnus-data-header d))))
6576             (setq is-younger (time-less-p
6577                               (time-since (condition-case ()
6578                                               (date-to-time date)
6579                                             (error '(0 0))))
6580                               cutoff))
6581             (when (if younger-p
6582                       is-younger
6583                     (not is-younger))
6584               (push (gnus-data-number d) articles))))
6585         (gnus-summary-limit (nreverse articles)))
6586     (gnus-summary-position-point)))
6587
6588 (defun gnus-summary-limit-to-extra (header regexp)
6589   "Limit the summary buffer to articles that match an 'extra' header."
6590   (interactive
6591    (let ((header
6592           (intern
6593            (gnus-completing-read
6594             (symbol-name (car gnus-extra-headers))
6595             "Limit extra header:"
6596             (mapcar (lambda (x)
6597                       (cons (symbol-name x) x))
6598                     gnus-extra-headers)
6599             nil
6600             t))))
6601      (list header
6602            (read-string (format "Limit to header %s (regexp): " header)))))
6603   (when (not (equal "" regexp))
6604     (prog1
6605         (let ((articles (gnus-summary-find-matching
6606                          (cons 'extra header) regexp 'all)))
6607           (unless articles
6608             (error "Found no matches for \"%s\"" regexp))
6609           (gnus-summary-limit articles))
6610       (gnus-summary-position-point))))
6611
6612 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6613 (make-obsolete
6614  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6615
6616 (defun gnus-summary-limit-to-unread (&optional all)
6617   "Limit the summary buffer to articles that are not marked as read.
6618 If ALL is non-nil, limit strictly to unread articles."
6619   (interactive "P")
6620   (if all
6621       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6622     (gnus-summary-limit-to-marks
6623      ;; Concat all the marks that say that an article is read and have
6624      ;; those removed.
6625      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6626            gnus-killed-mark gnus-kill-file-mark
6627            gnus-low-score-mark gnus-expirable-mark
6628            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6629            gnus-duplicate-mark gnus-souped-mark)
6630      'reverse)))
6631
6632 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6633 (make-obsolete 'gnus-summary-delete-marked-with
6634                'gnus-summary-limit-exlude-marks)
6635
6636 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6637   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6638 If REVERSE, limit the summary buffer to articles that are marked
6639 with MARKS.  MARKS can either be a string of marks or a list of marks.
6640 Returns how many articles were removed."
6641   (interactive "sMarks: ")
6642   (gnus-summary-limit-to-marks marks t))
6643
6644 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6645   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6646 If REVERSE (the prefix), limit the summary buffer to articles that are
6647 not marked with MARKS.  MARKS can either be a string of marks or a
6648 list of marks.
6649 Returns how many articles were removed."
6650   (interactive "sMarks: \nP")
6651   (prog1
6652       (let ((data gnus-newsgroup-data)
6653             (marks (if (listp marks) marks
6654                      (append marks nil))) ; Transform to list.
6655             articles)
6656         (while data
6657           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6658                   (memq (gnus-data-mark (car data)) marks))
6659             (push (gnus-data-number (car data)) articles))
6660           (setq data (cdr data)))
6661         (gnus-summary-limit articles))
6662     (gnus-summary-position-point)))
6663
6664 (defun gnus-summary-limit-to-score (&optional score)
6665   "Limit to articles with score at or above SCORE."
6666   (interactive "P")
6667   (setq score (if score
6668                   (prefix-numeric-value score)
6669                 (or gnus-summary-default-score 0)))
6670   (let ((data gnus-newsgroup-data)
6671         articles)
6672     (while data
6673       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6674                 score)
6675         (push (gnus-data-number (car data)) articles))
6676       (setq data (cdr data)))
6677     (prog1
6678         (gnus-summary-limit articles)
6679       (gnus-summary-position-point))))
6680
6681 (defun gnus-summary-limit-include-thread (id)
6682   "Display all the hidden articles that in the current thread."
6683   (interactive (list (mail-header-id (gnus-summary-article-header))))
6684   (let ((articles (gnus-articles-in-thread
6685                    (gnus-id-to-thread (gnus-root-id id)))))
6686     (prog1
6687         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6688       (gnus-summary-position-point))))
6689
6690 (defun gnus-summary-limit-include-dormant ()
6691   "Display all the hidden articles that are marked as dormant.
6692 Note that this command only works on a subset of the articles currently
6693 fetched for this group."
6694   (interactive)
6695   (unless gnus-newsgroup-dormant
6696     (error "There are no dormant articles in this group"))
6697   (prog1
6698       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6699     (gnus-summary-position-point)))
6700
6701 (defun gnus-summary-limit-exclude-dormant ()
6702   "Hide all dormant articles."
6703   (interactive)
6704   (prog1
6705       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6706     (gnus-summary-position-point)))
6707
6708 (defun gnus-summary-limit-exclude-childless-dormant ()
6709   "Hide all dormant articles that have no children."
6710   (interactive)
6711   (let ((data (gnus-data-list t))
6712         articles d children)
6713     ;; Find all articles that are either not dormant or have
6714     ;; children.
6715     (while (setq d (pop data))
6716       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6717                 (and (setq children
6718                            (gnus-article-children (gnus-data-number d)))
6719                      (let (found)
6720                        (while children
6721                          (when (memq (car children) articles)
6722                            (setq children nil
6723                                  found t))
6724                          (pop children))
6725                        found)))
6726         (push (gnus-data-number d) articles)))
6727     ;; Do the limiting.
6728     (prog1
6729         (gnus-summary-limit articles)
6730       (gnus-summary-position-point))))
6731
6732 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6733   "Mark all unread excluded articles as read.
6734 If ALL, mark even excluded ticked and dormants as read."
6735   (interactive "P")
6736   (let ((articles (gnus-sorted-complement
6737                    (sort
6738                     (mapcar (lambda (h) (mail-header-number h))
6739                             gnus-newsgroup-headers)
6740                     '<)
6741                    (sort gnus-newsgroup-limit '<)))
6742         article)
6743     (setq gnus-newsgroup-unreads
6744           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6745     (if all
6746         (setq gnus-newsgroup-dormant nil
6747               gnus-newsgroup-marked nil
6748               gnus-newsgroup-reads
6749               (nconc
6750                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6751                gnus-newsgroup-reads))
6752       (while (setq article (pop articles))
6753         (unless (or (memq article gnus-newsgroup-dormant)
6754                     (memq article gnus-newsgroup-marked))
6755           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6756
6757 (defun gnus-summary-limit (articles &optional pop)
6758   (if pop
6759       ;; We pop the previous limit off the stack and use that.
6760       (setq articles (car gnus-newsgroup-limits)
6761             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6762     ;; We use the new limit, so we push the old limit on the stack.
6763     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6764   ;; Set the limit.
6765   (setq gnus-newsgroup-limit articles)
6766   (let ((total (length gnus-newsgroup-data))
6767         (data (gnus-data-find-list (gnus-summary-article-number)))
6768         (gnus-summary-mark-below nil)   ; Inhibit this.
6769         found)
6770     ;; This will do all the work of generating the new summary buffer
6771     ;; according to the new limit.
6772     (gnus-summary-prepare)
6773     ;; Hide any threads, possibly.
6774     (and gnus-show-threads
6775          gnus-thread-hide-subtree
6776          (gnus-summary-hide-all-threads))
6777     ;; Try to return to the article you were at, or one in the
6778     ;; neighborhood.
6779     (when data
6780       ;; We try to find some article after the current one.
6781       (while data
6782         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6783           (setq data nil
6784                 found t))
6785         (setq data (cdr data))))
6786     (unless found
6787       ;; If there is no data, that means that we were after the last
6788       ;; article.  The same goes when we can't find any articles
6789       ;; after the current one.
6790       (goto-char (point-max))
6791       (gnus-summary-find-prev))
6792     (gnus-set-mode-line 'summary)
6793     ;; We return how many articles were removed from the summary
6794     ;; buffer as a result of the new limit.
6795     (- total (length gnus-newsgroup-data))))
6796
6797 (defsubst gnus-invisible-cut-children (threads)
6798   (let ((num 0))
6799     (while threads
6800       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6801         (incf num))
6802       (pop threads))
6803     (< num 2)))
6804
6805 (defsubst gnus-cut-thread (thread)
6806   "Go forwards in the thread until we find an article that we want to display."
6807   (when (or (eq gnus-fetch-old-headers 'some)
6808             (eq gnus-fetch-old-headers 'invisible)
6809             (numberp gnus-fetch-old-headers)
6810             (eq gnus-build-sparse-threads 'some)
6811             (eq gnus-build-sparse-threads 'more))
6812     ;; Deal with old-fetched headers and sparse threads.
6813     (while (and
6814             thread
6815             (or
6816              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6817              (gnus-summary-article-ancient-p
6818               (mail-header-number (car thread))))
6819             (if (or (<= (length (cdr thread)) 1)
6820                     (eq gnus-fetch-old-headers 'invisible))
6821                 (setq gnus-newsgroup-limit
6822                       (delq (mail-header-number (car thread))
6823                             gnus-newsgroup-limit)
6824                       thread (cadr thread))
6825               (when (gnus-invisible-cut-children (cdr thread))
6826                 (let ((th (cdr thread)))
6827                   (while th
6828                     (if (memq (mail-header-number (caar th))
6829                               gnus-newsgroup-limit)
6830                         (setq thread (car th)
6831                               th nil)
6832                       (setq th (cdr th))))))))))
6833   thread)
6834
6835 (defun gnus-cut-threads (threads)
6836   "Cut off all uninteresting articles from the beginning of threads."
6837   (when (or (eq gnus-fetch-old-headers 'some)
6838             (eq gnus-fetch-old-headers 'invisible)
6839             (numberp gnus-fetch-old-headers)
6840             (eq gnus-build-sparse-threads 'some)
6841             (eq gnus-build-sparse-threads 'more))
6842     (let ((th threads))
6843       (while th
6844         (setcar th (gnus-cut-thread (car th)))
6845         (setq th (cdr th)))))
6846   ;; Remove nixed out threads.
6847   (delq nil threads))
6848
6849 (defun gnus-summary-initial-limit (&optional show-if-empty)
6850   "Figure out what the initial limit is supposed to be on group entry.
6851 This entails weeding out unwanted dormants, low-scored articles,
6852 fetch-old-headers verbiage, and so on."
6853   ;; Most groups have nothing to remove.
6854   (if (or gnus-inhibit-limiting
6855           (and (null gnus-newsgroup-dormant)
6856                (not (eq gnus-fetch-old-headers 'some))
6857                (not (numberp gnus-fetch-old-headers))
6858                (not (eq gnus-fetch-old-headers 'invisible))
6859                (null gnus-summary-expunge-below)
6860                (not (eq gnus-build-sparse-threads 'some))
6861                (not (eq gnus-build-sparse-threads 'more))
6862                (null gnus-thread-expunge-below)
6863                (not gnus-use-nocem)))
6864       ()                                ; Do nothing.
6865     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6866     (setq gnus-newsgroup-limit nil)
6867     (mapatoms
6868      (lambda (node)
6869        (unless (car (symbol-value node))
6870          ;; These threads have no parents -- they are roots.
6871          (let ((nodes (cdr (symbol-value node)))
6872                thread)
6873            (while nodes
6874              (if (and gnus-thread-expunge-below
6875                       (< (gnus-thread-total-score (car nodes))
6876                          gnus-thread-expunge-below))
6877                  (gnus-expunge-thread (pop nodes))
6878                (setq thread (pop nodes))
6879                (gnus-summary-limit-children thread))))))
6880      gnus-newsgroup-dependencies)
6881     ;; If this limitation resulted in an empty group, we might
6882     ;; pop the previous limit and use it instead.
6883     (when (and (not gnus-newsgroup-limit)
6884                show-if-empty)
6885       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6886     gnus-newsgroup-limit))
6887
6888 (defun gnus-summary-limit-children (thread)
6889   "Return 1 if this subthread is visible and 0 if it is not."
6890   ;; First we get the number of visible children to this thread.  This
6891   ;; is done by recursing down the thread using this function, so this
6892   ;; will really go down to a leaf article first, before slowly
6893   ;; working its way up towards the root.
6894   (when thread
6895     (let ((children
6896            (if (cdr thread)
6897                (apply '+ (mapcar 'gnus-summary-limit-children
6898                                  (cdr thread)))
6899              0))
6900           (number (mail-header-number (car thread)))
6901           score)
6902       (if (and
6903            (not (memq number gnus-newsgroup-marked))
6904            (or
6905             ;; If this article is dormant and has absolutely no visible
6906             ;; children, then this article isn't visible.
6907             (and (memq number gnus-newsgroup-dormant)
6908                  (zerop children))
6909             ;; If this is "fetch-old-headered" and there is no
6910             ;; visible children, then we don't want this article.
6911             (and (or (eq gnus-fetch-old-headers 'some)
6912                      (numberp gnus-fetch-old-headers))
6913                  (gnus-summary-article-ancient-p number)
6914                  (zerop children))
6915             ;; If this is "fetch-old-headered" and `invisible', then
6916             ;; we don't want this article.
6917             (and (eq gnus-fetch-old-headers 'invisible)
6918                  (gnus-summary-article-ancient-p number))
6919             ;; If this is a sparsely inserted article with no children,
6920             ;; we don't want it.
6921             (and (eq gnus-build-sparse-threads 'some)
6922                  (gnus-summary-article-sparse-p number)
6923                  (zerop children))
6924             ;; If we use expunging, and this article is really
6925             ;; low-scored, then we don't want this article.
6926             (when (and gnus-summary-expunge-below
6927                        (< (setq score
6928                                 (or (cdr (assq number gnus-newsgroup-scored))
6929                                     gnus-summary-default-score))
6930                           gnus-summary-expunge-below))
6931               ;; We increase the expunge-tally here, but that has
6932               ;; nothing to do with the limits, really.
6933               (incf gnus-newsgroup-expunged-tally)
6934               ;; We also mark as read here, if that's wanted.
6935               (when (and gnus-summary-mark-below
6936                          (< score gnus-summary-mark-below))
6937                 (setq gnus-newsgroup-unreads
6938                       (delq number gnus-newsgroup-unreads))
6939                 (if gnus-newsgroup-auto-expire
6940                     (push number gnus-newsgroup-expirable)
6941                   (push (cons number gnus-low-score-mark)
6942                         gnus-newsgroup-reads)))
6943               t)
6944             ;; Check NoCeM things.
6945             (if (and gnus-use-nocem
6946                      (gnus-nocem-unwanted-article-p
6947                       (mail-header-id (car thread))))
6948                 (progn
6949                   (setq gnus-newsgroup-unreads
6950                         (delq number gnus-newsgroup-unreads))
6951                   t))))
6952           ;; Nope, invisible article.
6953           0
6954         ;; Ok, this article is to be visible, so we add it to the limit
6955         ;; and return 1.
6956         (push number gnus-newsgroup-limit)
6957         1))))
6958
6959 (defun gnus-expunge-thread (thread)
6960   "Mark all articles in THREAD as read."
6961   (let* ((number (mail-header-number (car thread))))
6962     (incf gnus-newsgroup-expunged-tally)
6963     ;; We also mark as read here, if that's wanted.
6964     (setq gnus-newsgroup-unreads
6965           (delq number gnus-newsgroup-unreads))
6966     (if gnus-newsgroup-auto-expire
6967         (push number gnus-newsgroup-expirable)
6968       (push (cons number gnus-low-score-mark)
6969             gnus-newsgroup-reads)))
6970   ;; Go recursively through all subthreads.
6971   (mapcar 'gnus-expunge-thread (cdr thread)))
6972
6973 ;; Summary article oriented commands
6974
6975 (defun gnus-summary-refer-parent-article (n)
6976   "Refer parent article N times.
6977 If N is negative, go to ancestor -N instead.
6978 The difference between N and the number of articles fetched is returned."
6979   (interactive "p")
6980   (let ((skip 1)
6981         error header ref)
6982     (when (not (natnump n))
6983       (setq skip (abs n)
6984             n 1))
6985     (while (and (> n 0)
6986                 (not error))
6987       (setq header (gnus-summary-article-header))
6988       (if (and (eq (mail-header-number header)
6989                    (cdr gnus-article-current))
6990                (equal gnus-newsgroup-name
6991                       (car gnus-article-current)))
6992           ;; If we try to find the parent of the currently
6993           ;; displayed article, then we take a look at the actual
6994           ;; References header, since this is slightly more
6995           ;; reliable than the References field we got from the
6996           ;; server.
6997           (save-excursion
6998             (set-buffer gnus-original-article-buffer)
6999             (nnheader-narrow-to-headers)
7000             (unless (setq ref (message-fetch-field "references"))
7001               (setq ref (message-fetch-field "in-reply-to")))
7002             (widen))
7003         (setq ref
7004               ;; It's not the current article, so we take a bet on
7005               ;; the value we got from the server.
7006               (mail-header-references header)))
7007       (if (and ref
7008                (not (equal ref "")))
7009           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7010             (gnus-message 1 "Couldn't find parent"))
7011         (gnus-message 1 "No references in article %d"
7012                       (gnus-summary-article-number))
7013         (setq error t))
7014       (decf n))
7015     (gnus-summary-position-point)
7016     n))
7017
7018 (defun gnus-summary-refer-references ()
7019   "Fetch all articles mentioned in the References header.
7020 Return the number of articles fetched."
7021   (interactive)
7022   (let ((ref (mail-header-references (gnus-summary-article-header)))
7023         (current (gnus-summary-article-number))
7024         (n 0))
7025     (if (or (not ref)
7026             (equal ref ""))
7027         (error "No References in the current article")
7028       ;; For each Message-ID in the References header...
7029       (while (string-match "<[^>]*>" ref)
7030         (incf n)
7031         ;; ... fetch that article.
7032         (gnus-summary-refer-article
7033          (prog1 (match-string 0 ref)
7034            (setq ref (substring ref (match-end 0))))))
7035       (gnus-summary-goto-subject current)
7036       (gnus-summary-position-point)
7037       n)))
7038
7039 (defun gnus-summary-refer-thread (&optional limit)
7040   "Fetch all articles in the current thread.
7041 If LIMIT (the numerical prefix), fetch that many old headers instead
7042 of what's specified by the `gnus-refer-thread-limit' variable."
7043   (interactive "P")
7044   (let ((id (mail-header-id (gnus-summary-article-header)))
7045         (limit (if limit (prefix-numeric-value limit)
7046                  gnus-refer-thread-limit)))
7047     ;; We want to fetch LIMIT *old* headers, but we also have to
7048     ;; re-fetch all the headers in the current buffer, because many of
7049     ;; them may be undisplayed.  So we adjust LIMIT.
7050     (when (numberp limit)
7051       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7052     (unless (eq gnus-fetch-old-headers 'invisible)
7053       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7054       ;; Retrieve the headers and read them in.
7055       (if (eq (gnus-retrieve-headers
7056                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7057               'nov)
7058           (gnus-build-all-threads)
7059         (error "Can't fetch thread from backends that don't support NOV"))
7060       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7061     (gnus-summary-limit-include-thread id)))
7062
7063 (defun gnus-summary-refer-article (message-id)
7064   "Fetch an article specified by MESSAGE-ID."
7065   (interactive "sMessage-ID: ")
7066   (when (and (stringp message-id)
7067              (not (zerop (length message-id))))
7068     ;; Construct the correct Message-ID if necessary.
7069     ;; Suggested by tale@pawl.rpi.edu.
7070     (unless (string-match "^<" message-id)
7071       (setq message-id (concat "<" message-id)))
7072     (unless (string-match ">$" message-id)
7073       (setq message-id (concat message-id ">")))
7074     (let* ((header (gnus-id-to-header message-id))
7075            (sparse (and header
7076                         (gnus-summary-article-sparse-p
7077                          (mail-header-number header))
7078                         (memq (mail-header-number header)
7079                               gnus-newsgroup-limit)))
7080            number)
7081       (cond
7082        ;; If the article is present in the buffer we just go to it.
7083        ((and header
7084              (or (not (gnus-summary-article-sparse-p
7085                        (mail-header-number header)))
7086                  sparse))
7087         (prog1
7088             (gnus-summary-goto-article
7089              (mail-header-number header) nil t)
7090           (when sparse
7091             (gnus-summary-update-article (mail-header-number header)))))
7092        (t
7093         ;; We fetch the article.
7094         (catch 'found
7095           (dolist (gnus-override-method (gnus-refer-article-methods))
7096             (gnus-check-server gnus-override-method)
7097             ;; Fetch the header, and display the article.
7098             (when (setq number (gnus-summary-insert-subject message-id))
7099               (gnus-summary-select-article nil nil nil number)
7100               (throw 'found t)))
7101           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7102
7103 (defun gnus-refer-article-methods ()
7104   "Return a list of referrable methods."
7105   (cond
7106    ;; No method, so we default to current and native.
7107    ((null gnus-refer-article-method)
7108     (list gnus-current-select-method gnus-select-method))
7109    ;; Current.
7110    ((eq 'current gnus-refer-article-method)
7111     (list gnus-current-select-method))
7112    ;; List of select methods.
7113    ((not (and (symbolp (car gnus-refer-article-method))
7114               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7115     (let (out)
7116       (dolist (method gnus-refer-article-method)
7117         (push (if (eq 'current method)
7118                   gnus-current-select-method
7119                 method)
7120               out))
7121       (nreverse out)))
7122    ;; One single select method.
7123    (t
7124     (list gnus-refer-article-method))))
7125
7126 (defun gnus-summary-edit-parameters ()
7127   "Edit the group parameters of the current group."
7128   (interactive)
7129   (gnus-group-edit-group gnus-newsgroup-name 'params))
7130
7131 (defun gnus-summary-customize-parameters ()
7132   "Customize the group parameters of the current group."
7133   (interactive)
7134   (gnus-group-customize gnus-newsgroup-name))
7135
7136 (defun gnus-summary-enter-digest-group (&optional force)
7137   "Enter an nndoc group based on the current article.
7138 If FORCE, force a digest interpretation.  If not, try
7139 to guess what the document format is."
7140   (interactive "P")
7141   (let ((conf gnus-current-window-configuration))
7142     (save-excursion
7143       (gnus-summary-select-article))
7144     (setq gnus-current-window-configuration conf)
7145     (let* ((name (format "%s-%d"
7146                          (gnus-group-prefixed-name
7147                           gnus-newsgroup-name (list 'nndoc ""))
7148                          (save-excursion
7149                            (set-buffer gnus-summary-buffer)
7150                            gnus-current-article)))
7151            (ogroup gnus-newsgroup-name)
7152            (params (append (gnus-info-params (gnus-get-info ogroup))
7153                            (list (cons 'to-group ogroup))
7154                            (list (cons 'save-article-group ogroup))))
7155            (case-fold-search t)
7156            (buf (current-buffer))
7157            dig to-address)
7158       (save-excursion
7159         (set-buffer gnus-original-article-buffer)
7160         ;; Have the digest group inherit the main mail address of
7161         ;; the parent article.
7162         (when (setq to-address (or (message-fetch-field "reply-to")
7163                                    (message-fetch-field "from")))
7164           (setq params (append
7165                         (list (cons 'to-address
7166                                     (funcall gnus-decode-encoded-word-function
7167                                              to-address))))))
7168         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7169         (insert-buffer-substring gnus-original-article-buffer)
7170         ;; Remove lines that may lead nndoc to misinterpret the
7171         ;; document type.
7172         (narrow-to-region
7173          (goto-char (point-min))
7174          (or (search-forward "\n\n" nil t) (point)))
7175         (goto-char (point-min))
7176         (delete-matching-lines "^Path:\\|^From ")
7177         (widen))
7178       (unwind-protect
7179           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7180                     (gnus-newsgroup-ephemeral-ignored-charsets
7181                      gnus-newsgroup-ignored-charsets))
7182                 (gnus-group-read-ephemeral-group
7183                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7184                               (nndoc-article-type
7185                                ,(if force 'mbox 'guess))) t))
7186               ;; Make all postings to this group go to the parent group.
7187               (nconc (gnus-info-params (gnus-get-info name))
7188                      params)
7189             ;; Couldn't select this doc group.
7190             (switch-to-buffer buf)
7191             (gnus-set-global-variables)
7192             (gnus-configure-windows 'summary)
7193             (gnus-message 3 "Article couldn't be entered?"))
7194         (kill-buffer dig)))))
7195
7196 (defun gnus-summary-read-document (n)
7197   "Open a new group based on the current article(s).
7198 This will allow you to read digests and other similar
7199 documents as newsgroups.
7200 Obeys the standard process/prefix convention."
7201   (interactive "P")
7202   (let* ((articles (gnus-summary-work-articles n))
7203          (ogroup gnus-newsgroup-name)
7204          (params (append (gnus-info-params (gnus-get-info ogroup))
7205                          (list (cons 'to-group ogroup))))
7206          article group egroup groups vgroup)
7207     (while (setq article (pop articles))
7208       (setq group (format "%s-%d" gnus-newsgroup-name article))
7209       (gnus-summary-remove-process-mark article)
7210       (when (gnus-summary-display-article article)
7211         (save-excursion
7212           (with-temp-buffer
7213             (insert-buffer-substring gnus-original-article-buffer)
7214             ;; Remove some headers that may lead nndoc to make
7215             ;; the wrong guess.
7216             (message-narrow-to-head)
7217             (goto-char (point-min))
7218             (delete-matching-lines "^\\(Path\\):\\|^From ")
7219             (widen)
7220             (if (setq egroup
7221                       (gnus-group-read-ephemeral-group
7222                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7223                                      (nndoc-article-type guess))
7224                        t nil t))
7225                 (progn
7226                   ;; Make all postings to this group go to the parent group.
7227                   (nconc (gnus-info-params (gnus-get-info egroup))
7228                          params)
7229                   (push egroup groups))
7230               ;; Couldn't select this doc group.
7231               (gnus-error 3 "Article couldn't be entered"))))))
7232     ;; Now we have selected all the documents.
7233     (cond
7234      ((not groups)
7235       (error "None of the articles could be interpreted as documents"))
7236      ((gnus-group-read-ephemeral-group
7237        (setq vgroup (format
7238                      "nnvirtual:%s-%s" gnus-newsgroup-name
7239                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7240        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7241        t
7242        (cons (current-buffer) 'summary)))
7243      (t
7244       (error "Couldn't select virtual nndoc group")))))
7245
7246 (defun gnus-summary-isearch-article (&optional regexp-p)
7247   "Do incremental search forward on the current article.
7248 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7249   (interactive "P")
7250   (gnus-summary-select-article)
7251   (gnus-configure-windows 'article)
7252   (gnus-eval-in-buffer-window gnus-article-buffer
7253     (save-restriction
7254       (widen)
7255       (isearch-forward regexp-p))))
7256
7257 (defun gnus-summary-search-article-forward (regexp &optional backward)
7258   "Search for an article containing REGEXP forward.
7259 If BACKWARD, search backward instead."
7260   (interactive
7261    (list (read-string
7262           (format "Search article %s (regexp%s): "
7263                   (if current-prefix-arg "backward" "forward")
7264                   (if gnus-last-search-regexp
7265                       (concat ", default " gnus-last-search-regexp)
7266                     "")))
7267          current-prefix-arg))
7268   (if (string-equal regexp "")
7269       (setq regexp (or gnus-last-search-regexp ""))
7270     (setq gnus-last-search-regexp regexp)
7271     (setq gnus-article-before-search gnus-current-article))
7272   ;; Intentionally set gnus-last-article.
7273   (setq gnus-last-article gnus-article-before-search)
7274   (let ((gnus-last-article gnus-last-article))
7275     (if (gnus-summary-search-article regexp backward)
7276         (gnus-summary-show-thread)
7277       (error "Search failed: \"%s\"" regexp))))
7278
7279 (defun gnus-summary-search-article-backward (regexp)
7280   "Search for an article containing REGEXP backward."
7281   (interactive
7282    (list (read-string
7283           (format "Search article backward (regexp%s): "
7284                   (if gnus-last-search-regexp
7285                       (concat ", default " gnus-last-search-regexp)
7286                     "")))))
7287   (gnus-summary-search-article-forward regexp 'backward))
7288
7289 (defun gnus-summary-search-article (regexp &optional backward)
7290   "Search for an article containing REGEXP.
7291 Optional argument BACKWARD means do search for backward.
7292 `gnus-select-article-hook' is not called during the search."
7293   ;; We have to require this here to make sure that the following
7294   ;; dynamic binding isn't shadowed by autoloading.
7295   (require 'gnus-async)
7296   (require 'gnus-art)
7297   (let ((gnus-select-article-hook nil)  ;Disable hook.
7298         (gnus-article-prepare-hook nil)
7299         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7300         (gnus-use-article-prefetch nil)
7301         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7302         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7303         (sum (current-buffer))
7304         (gnus-display-mime-function nil)
7305         (found nil)
7306         point)
7307     (gnus-save-hidden-threads
7308       (gnus-summary-select-article)
7309       (set-buffer gnus-article-buffer)
7310       (goto-char (window-point (get-buffer-window (current-buffer))))
7311       (when backward
7312         (forward-line -1))
7313       (while (not found)
7314         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7315         (if (if backward
7316                 (re-search-backward regexp nil t)
7317               (re-search-forward regexp nil t))
7318             ;; We found the regexp.
7319             (progn
7320               (setq found 'found)
7321               (beginning-of-line)
7322               (set-window-start
7323                (get-buffer-window (current-buffer))
7324                (point))
7325               (forward-line 1)
7326               (set-window-point
7327                (get-buffer-window (current-buffer))
7328                (point))
7329               (set-buffer sum)
7330               (setq point (point)))
7331           ;; We didn't find it, so we go to the next article.
7332           (set-buffer sum)
7333           (setq found 'not)
7334           (while (eq found 'not)
7335             (if (not (if backward (gnus-summary-find-prev)
7336                        (gnus-summary-find-next)))
7337                 ;; No more articles.
7338                 (setq found t)
7339               ;; Select the next article and adjust point.
7340               (unless (gnus-summary-article-sparse-p
7341                        (gnus-summary-article-number))
7342                 (setq found nil)
7343                 (gnus-summary-select-article)
7344                 (set-buffer gnus-article-buffer)
7345                 (widen)
7346                 (goto-char (if backward (point-max) (point-min))))))))
7347       (gnus-message 7 ""))
7348     ;; Return whether we found the regexp.
7349     (when (eq found 'found)
7350       (goto-char point)
7351       (gnus-summary-show-thread)
7352       (gnus-summary-goto-subject gnus-current-article)
7353       (gnus-summary-position-point)
7354       t)))
7355
7356 (defun gnus-summary-find-matching (header regexp &optional backward unread
7357                                           not-case-fold)
7358   "Return a list of all articles that match REGEXP on HEADER.
7359 The search stars on the current article and goes forwards unless
7360 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7361 If UNREAD is non-nil, only unread articles will
7362 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7363 in the comparisons."
7364   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7365                 (gnus-data-find-list
7366                  (gnus-summary-article-number) (gnus-data-list backward))))
7367         (case-fold-search (not not-case-fold))
7368         articles d func)
7369     (if (consp header)
7370         (if (eq (car header) 'extra)
7371             (setq func
7372                   `(lambda (h)
7373                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7374                          "")))
7375           (error "%s is an invalid header" header))
7376       (unless (fboundp (intern (concat "mail-header-" header)))
7377         (error "%s is not a valid header" header))
7378       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7379     (while data
7380       (setq d (car data))
7381       (and (or (not unread)             ; We want all articles...
7382                (gnus-data-unread-p d))  ; Or just unreads.
7383            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7384            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7385            (push (gnus-data-number d) articles)) ; Success!
7386       (setq data (cdr data)))
7387     (nreverse articles)))
7388
7389 (defun gnus-summary-execute-command (header regexp command &optional backward)
7390   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7391 If HEADER is an empty string (or nil), the match is done on the entire
7392 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7393   (interactive
7394    (list (let ((completion-ignore-case t))
7395            (completing-read
7396             "Header name: "
7397             (mapcar (lambda (string) (list string))
7398                     '("Number" "Subject" "From" "Lines" "Date"
7399                       "Message-ID" "Xref" "References" "Body"))
7400             nil 'require-match))
7401          (read-string "Regexp: ")
7402          (read-key-sequence "Command: ")
7403          current-prefix-arg))
7404   (when (equal header "Body")
7405     (setq header ""))
7406   ;; Hidden thread subtrees must be searched as well.
7407   (gnus-summary-show-all-threads)
7408   ;; We don't want to change current point nor window configuration.
7409   (save-excursion
7410     (save-window-excursion
7411       (gnus-message 6 "Executing %s..." (key-description command))
7412       ;; We'd like to execute COMMAND interactively so as to give arguments.
7413       (gnus-execute header regexp
7414                     `(call-interactively ',(key-binding command))
7415                     backward)
7416       (gnus-message 6 "Executing %s...done" (key-description command)))))
7417
7418 (defun gnus-summary-beginning-of-article ()
7419   "Scroll the article back to the beginning."
7420   (interactive)
7421   (gnus-summary-select-article)
7422   (gnus-configure-windows 'article)
7423   (gnus-eval-in-buffer-window gnus-article-buffer
7424     (widen)
7425     (goto-char (point-min))
7426     (when gnus-page-broken
7427       (gnus-narrow-to-page))))
7428
7429 (defun gnus-summary-end-of-article ()
7430   "Scroll to the end of the article."
7431   (interactive)
7432   (gnus-summary-select-article)
7433   (gnus-configure-windows 'article)
7434   (gnus-eval-in-buffer-window gnus-article-buffer
7435     (widen)
7436     (goto-char (point-max))
7437     (recenter -3)
7438     (when gnus-page-broken
7439       (gnus-narrow-to-page))))
7440
7441 (defun gnus-summary-print-article (&optional filename n)
7442   "Generate and print a PostScript image of the N next (mail) articles.
7443
7444 If N is negative, print the N previous articles.  If N is nil and articles
7445 have been marked with the process mark, print these instead.
7446
7447 If the optional first argument FILENAME is nil, send the image to the
7448 printer.  If FILENAME is a string, save the PostScript image in a file with
7449 that name.  If FILENAME is a number, prompt the user for the name of the file
7450 to save in."
7451   (interactive (list (ps-print-preprint current-prefix-arg)))
7452   (dolist (article (gnus-summary-work-articles n))
7453     (gnus-summary-select-article nil nil 'pseudo article)
7454     (gnus-eval-in-buffer-window gnus-article-buffer
7455       (let ((buffer (generate-new-buffer " *print*")))
7456         (unwind-protect
7457             (progn
7458               (copy-to-buffer buffer (point-min) (point-max))
7459               (set-buffer buffer)
7460               (gnus-article-delete-invisible-text)
7461               (when (gnus-visual-p 'article-highlight 'highlight)
7462                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7463                 ;; highlight.
7464                 (let ((gnus-article-buffer buffer))
7465                   (gnus-article-highlight-citation t)
7466                   (gnus-article-highlight-signature)))
7467               (let ((ps-left-header
7468                      (list
7469                       (concat "("
7470                               (mail-header-subject gnus-current-headers) ")")
7471                       (concat "("
7472                               (mail-header-from gnus-current-headers) ")")))
7473                     (ps-right-header
7474                      (list
7475                       "/pagenumberstring load"
7476                       (concat "("
7477                               (mail-header-date gnus-current-headers) ")"))))
7478                 (gnus-run-hooks 'gnus-ps-print-hook)
7479                 (save-excursion
7480                   (ps-spool-buffer-with-faces))))
7481           (kill-buffer buffer))))
7482     (gnus-summary-remove-process-mark article))
7483   (ps-despool filename))
7484
7485 (defun gnus-summary-show-article (&optional arg)
7486   "Force re-fetching of the current article.
7487 If ARG (the prefix) is a number, show the article with the charset
7488 defined in `gnus-summary-show-article-charset-alist', or the charset
7489 inputed.
7490 If ARG (the prefix) is non-nil and not a number, show the raw article
7491 without any article massaging functions being run."
7492   (interactive "P")
7493   (cond
7494    ((numberp arg)
7495     (let ((gnus-newsgroup-charset
7496            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7497                (read-coding-system "Charset: ")))
7498           (gnus-newsgroup-ignored-charsets 'gnus-all))
7499       (gnus-summary-select-article nil 'force)
7500       (let ((deps gnus-newsgroup-dependencies)
7501             head header)
7502         (save-excursion
7503           (set-buffer gnus-original-article-buffer)
7504           (save-restriction
7505             (message-narrow-to-head)
7506             (setq head (buffer-string)))
7507           (with-temp-buffer
7508             (insert (format "211 %d Article retrieved.\n"
7509                             (cdr gnus-article-current)))
7510             (insert head)
7511             (insert ".\n")
7512             (let ((nntp-server-buffer (current-buffer)))
7513               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7514         (gnus-data-set-header
7515          (gnus-data-find (cdr gnus-article-current))
7516          header)
7517         (gnus-summary-update-article-line
7518          (cdr gnus-article-current) header))))
7519    ((not arg)
7520     ;; Select the article the normal way.
7521     (gnus-summary-select-article nil 'force))
7522    (t
7523     ;; We have to require this here to make sure that the following
7524     ;; dynamic binding isn't shadowed by autoloading.
7525     (require 'gnus-async)
7526     (require 'gnus-art)
7527     ;; Bind the article treatment functions to nil.
7528     (let ((gnus-have-all-headers t)
7529           gnus-article-prepare-hook
7530           gnus-article-decode-hook
7531           gnus-display-mime-function
7532           gnus-break-pages)
7533       ;; Destroy any MIME parts.
7534       (when (gnus-buffer-live-p gnus-article-buffer)
7535         (save-excursion
7536           (set-buffer gnus-article-buffer)
7537           (mm-destroy-parts gnus-article-mime-handles)
7538           ;; Set it to nil for safety reason.
7539           (setq gnus-article-mime-handle-alist nil)
7540           (setq gnus-article-mime-handles nil)))
7541       (gnus-summary-select-article nil 'force))))
7542   (gnus-summary-goto-subject gnus-current-article)
7543   (gnus-summary-position-point))
7544
7545 (defun gnus-summary-verbose-headers (&optional arg)
7546   "Toggle permanent full header display.
7547 If ARG is a positive number, turn header display on.
7548 If ARG is a negative number, turn header display off."
7549   (interactive "P")
7550   (setq gnus-show-all-headers
7551         (cond ((or (not (numberp arg))
7552                    (zerop arg))
7553                (not gnus-show-all-headers))
7554               ((natnump arg)
7555                t)))
7556   (gnus-summary-show-article))
7557
7558 (defun gnus-summary-toggle-header (&optional arg)
7559   "Show the headers if they are hidden, or hide them if they are shown.
7560 If ARG is a positive number, show the entire header.
7561 If ARG is a negative number, hide the unwanted header lines."
7562   (interactive "P")
7563   (save-excursion
7564     (set-buffer gnus-article-buffer)
7565     (save-restriction
7566       (let* ((buffer-read-only nil)
7567              (inhibit-point-motion-hooks t)
7568              hidden e)
7569         (setq hidden
7570               (if (numberp arg)
7571                   (>= arg 0)
7572                 (save-restriction
7573                   (article-narrow-to-head)
7574                   (gnus-article-hidden-text-p 'headers))))
7575         (goto-char (point-min))
7576         (when (search-forward "\n\n" nil t)
7577           (delete-region (point-min) (1- (point))))
7578         (goto-char (point-min))
7579         (save-excursion
7580           (set-buffer gnus-original-article-buffer)
7581           (goto-char (point-min))
7582           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7583         (insert-buffer-substring gnus-original-article-buffer 1 e)
7584         (save-restriction
7585           (narrow-to-region (point-min) (point))
7586           (article-decode-encoded-words)
7587           (if  hidden
7588               (let ((gnus-treat-hide-headers nil)
7589                     (gnus-treat-hide-boring-headers nil))
7590                 (setq gnus-article-wash-types
7591                       (delq 'headers gnus-article-wash-types))
7592                 (gnus-treat-article 'head))
7593             (gnus-treat-article 'head)))
7594         (gnus-set-mode-line 'article)))))
7595
7596 (defun gnus-summary-show-all-headers ()
7597   "Make all header lines visible."
7598   (interactive)
7599   (gnus-article-show-all-headers))
7600
7601 (defun gnus-summary-caesar-message (&optional arg)
7602   "Caesar rotate the current article by 13.
7603 The numerical prefix specifies how many places to rotate each letter
7604 forward."
7605   (interactive "P")
7606   (gnus-summary-select-article)
7607   (let ((mail-header-separator ""))
7608     (gnus-eval-in-buffer-window gnus-article-buffer
7609       (save-restriction
7610         (widen)
7611         (let ((start (window-start))
7612               buffer-read-only)
7613           (message-caesar-buffer-body arg)
7614           (set-window-start (get-buffer-window (current-buffer)) start))))))
7615
7616 (defun gnus-summary-stop-page-breaking ()
7617   "Stop page breaking in the current article."
7618   (interactive)
7619   (gnus-summary-select-article)
7620   (gnus-eval-in-buffer-window gnus-article-buffer
7621     (widen)
7622     (when (gnus-visual-p 'page-marker)
7623       (let ((buffer-read-only nil))
7624         (gnus-remove-text-with-property 'gnus-prev)
7625         (gnus-remove-text-with-property 'gnus-next))
7626       (setq gnus-page-broken nil))))
7627
7628 (defun gnus-summary-move-article (&optional n to-newsgroup
7629                                             select-method action)
7630   "Move the current article to a different newsgroup.
7631 If N is a positive number, move the N next articles.
7632 If N is a negative number, move the N previous articles.
7633 If N is nil and any articles have been marked with the process mark,
7634 move those articles instead.
7635 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7636 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7637 re-spool using this method.
7638
7639 For this function to work, both the current newsgroup and the
7640 newsgroup that you want to move to have to support the `request-move'
7641 and `request-accept' functions.
7642
7643 ACTION can be either `move' (the default), `crosspost' or `copy'."
7644   (interactive "P")
7645   (unless action
7646     (setq action 'move))
7647   ;; Disable marking as read.
7648   (let (gnus-mark-article-hook)
7649     (save-window-excursion
7650       (gnus-summary-select-article)))
7651   ;; Check whether the source group supports the required functions.
7652   (cond ((and (eq action 'move)
7653               (not (gnus-check-backend-function
7654                     'request-move-article gnus-newsgroup-name)))
7655          (error "The current group does not support article moving"))
7656         ((and (eq action 'crosspost)
7657               (not (gnus-check-backend-function
7658                     'request-replace-article gnus-newsgroup-name)))
7659          (error "The current group does not support article editing")))
7660   (let ((articles (gnus-summary-work-articles n))
7661         (prefix (if (gnus-check-backend-function
7662                     'request-move-article gnus-newsgroup-name)
7663                     (gnus-group-real-prefix gnus-newsgroup-name)
7664                   ""))
7665         (names '((move "Move" "Moving")
7666                  (copy "Copy" "Copying")
7667                  (crosspost "Crosspost" "Crossposting")))
7668         (copy-buf (save-excursion
7669                     (nnheader-set-temp-buffer " *copy article*")))
7670         art-group to-method new-xref article to-groups)
7671     (unless (assq action names)
7672       (error "Unknown action %s" action))
7673     ;; Read the newsgroup name.
7674     (when (and (not to-newsgroup)
7675                (not select-method))
7676       (setq to-newsgroup
7677             (gnus-read-move-group-name
7678              (cadr (assq action names))
7679              (symbol-value (intern (format "gnus-current-%s-group" action)))
7680              articles prefix))
7681       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7682     (setq to-method (or select-method
7683                         (gnus-server-to-method
7684                          (gnus-group-method to-newsgroup))))
7685     ;; Check the method we are to move this article to...
7686     (unless (gnus-check-backend-function
7687              'request-accept-article (car to-method))
7688       (error "%s does not support article copying" (car to-method)))
7689     (unless (gnus-check-server to-method)
7690       (error "Can't open server %s" (car to-method)))
7691     (gnus-message 6 "%s to %s: %s..."
7692                   (caddr (assq action names))
7693                   (or (car select-method) to-newsgroup) articles)
7694     (while articles
7695       (setq article (pop articles))
7696       (setq
7697        art-group
7698        (cond
7699         ;; Move the article.
7700         ((eq action 'move)
7701          ;; Remove this article from future suppression.
7702          (gnus-dup-unsuppress-article article)
7703          (gnus-request-move-article
7704           article                       ; Article to move
7705           gnus-newsgroup-name           ; From newsgroup
7706           (nth 1 (gnus-find-method-for-group
7707                   gnus-newsgroup-name)) ; Server
7708           (list 'gnus-request-accept-article
7709                 to-newsgroup (list 'quote select-method)
7710                 (not articles) t)       ; Accept form
7711           (not articles)))              ; Only save nov last time
7712         ;; Copy the article.
7713         ((eq action 'copy)
7714          (save-excursion
7715            (set-buffer copy-buf)
7716            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7717              (gnus-request-accept-article
7718               to-newsgroup select-method (not articles) t))))
7719         ;; Crosspost the article.
7720         ((eq action 'crosspost)
7721          (let ((xref (message-tokenize-header
7722                       (mail-header-xref (gnus-summary-article-header article))
7723                       " ")))
7724            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7725                                   ":" article))
7726            (unless xref
7727              (setq xref (list (system-name))))
7728            (setq new-xref
7729                  (concat
7730                   (mapconcat 'identity
7731                              (delete "Xref:" (delete new-xref xref))
7732                              " ")
7733                   " " new-xref))
7734            (save-excursion
7735              (set-buffer copy-buf)
7736              ;; First put the article in the destination group.
7737              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7738              (when (consp (setq art-group
7739                                 (gnus-request-accept-article
7740                                  to-newsgroup select-method (not articles))))
7741                (setq new-xref (concat new-xref " " (car art-group)
7742                                       ":" (cdr art-group)))
7743                ;; Now we have the new Xrefs header, so we insert
7744                ;; it and replace the new article.
7745                (nnheader-replace-header "Xref" new-xref)
7746                (gnus-request-replace-article
7747                 (cdr art-group) to-newsgroup (current-buffer))
7748                art-group))))))
7749       (cond
7750        ((not art-group)
7751         (gnus-message 1 "Couldn't %s article %s: %s"
7752                       (cadr (assq action names)) article
7753                       (nnheader-get-report (car to-method))))
7754        ((eq art-group 'junk)
7755         (when (eq action 'move)
7756           (gnus-summary-mark-article article gnus-canceled-mark)
7757           (gnus-message 4 "Deleted article %s" article)))
7758        (t
7759         (let* ((pto-group (gnus-group-prefixed-name
7760                            (car art-group) to-method))
7761                (entry
7762                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7763                (info (nth 2 entry))
7764                (to-group (gnus-info-group info))
7765                to-marks)
7766           ;; Update the group that has been moved to.
7767           (when (and info
7768                      (memq action '(move copy)))
7769             (unless (member to-group to-groups)
7770               (push to-group to-groups))
7771
7772             (unless (memq article gnus-newsgroup-unreads)
7773               (push 'read to-marks)
7774               (gnus-info-set-read
7775                info (gnus-add-to-range (gnus-info-read info)
7776                                        (list (cdr art-group)))))
7777
7778             ;; See whether the article is to be put in the cache.
7779             (let ((marks gnus-article-mark-lists)
7780                   (to-article (cdr art-group)))
7781
7782               ;; Enter the article into the cache in the new group,
7783               ;; if that is required.
7784               (when gnus-use-cache
7785                 (gnus-cache-possibly-enter-article
7786                  to-group to-article
7787                  (memq article gnus-newsgroup-marked)
7788                  (memq article gnus-newsgroup-dormant)
7789                  (memq article gnus-newsgroup-unreads)))
7790
7791               (when gnus-preserve-marks
7792                 ;; Copy any marks over to the new group.
7793                 (when (and (equal to-group gnus-newsgroup-name)
7794                            (not (memq article gnus-newsgroup-unreads)))
7795                   ;; Mark this article as read in this group.
7796                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7797                   (setcdr (gnus-active to-group) to-article)
7798                   (setcdr gnus-newsgroup-active to-article))
7799
7800                 (while marks
7801                   (when (memq article (symbol-value
7802                                        (intern (format "gnus-newsgroup-%s"
7803                                                        (caar marks)))))
7804                     (push (cdar marks) to-marks)
7805                     ;; If the other group is the same as this group,
7806                     ;; then we have to add the mark to the list.
7807                     (when (equal to-group gnus-newsgroup-name)
7808                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7809                            (cons to-article
7810                                  (symbol-value
7811                                   (intern (format "gnus-newsgroup-%s"
7812                                                   (caar marks)))))))
7813                     ;; Copy the marks to other group.
7814                     (gnus-add-marked-articles
7815                      to-group (cdar marks) (list to-article) info))
7816                   (setq marks (cdr marks)))
7817
7818                 (gnus-request-set-mark to-group (list (list (list to-article)
7819                                                             'set
7820                                                             to-marks))))
7821
7822               (gnus-dribble-enter
7823                (concat "(gnus-group-set-info '"
7824                        (gnus-prin1-to-string (gnus-get-info to-group))
7825                        ")"))))
7826
7827           ;; Update the Xref header in this article to point to
7828           ;; the new crossposted article we have just created.
7829           (when (eq action 'crosspost)
7830             (save-excursion
7831               (set-buffer copy-buf)
7832               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7833               (nnheader-replace-header "Xref" new-xref)
7834               (gnus-request-replace-article
7835                article gnus-newsgroup-name (current-buffer)))))
7836
7837         ;;;!!!Why is this necessary?
7838         (set-buffer gnus-summary-buffer)
7839
7840         (gnus-summary-goto-subject article)
7841         (when (eq action 'move)
7842           (gnus-summary-mark-article article gnus-canceled-mark))))
7843       (gnus-summary-remove-process-mark article))
7844     ;; Re-activate all groups that have been moved to.
7845     (while to-groups
7846       (save-excursion
7847         (set-buffer gnus-group-buffer)
7848         (when (gnus-group-goto-group (car to-groups) t)
7849           (gnus-group-get-new-news-this-group 1 t))
7850         (pop to-groups)))
7851
7852     (gnus-kill-buffer copy-buf)
7853     (gnus-summary-position-point)
7854     (gnus-set-mode-line 'summary)))
7855
7856 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7857   "Move the current article to a different newsgroup.
7858 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7859 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7860 re-spool using this method."
7861   (interactive "P")
7862   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7863
7864 (defun gnus-summary-crosspost-article (&optional n)
7865   "Crosspost the current article to some other group."
7866   (interactive "P")
7867   (gnus-summary-move-article n nil nil 'crosspost))
7868
7869 (defcustom gnus-summary-respool-default-method nil
7870   "Default method for respooling an article.
7871 If nil, use to the current newsgroup method."
7872   :type '(choice (gnus-select-method :value (nnml ""))
7873                  (const nil))
7874   :group 'gnus-summary-mail)
7875
7876 (defun gnus-summary-respool-article (&optional n method)
7877   "Respool the current article.
7878 The article will be squeezed through the mail spooling process again,
7879 which means that it will be put in some mail newsgroup or other
7880 depending on `nnmail-split-methods'.
7881 If N is a positive number, respool the N next articles.
7882 If N is a negative number, respool the N previous articles.
7883 If N is nil and any articles have been marked with the process mark,
7884 respool those articles instead.
7885
7886 Respooling can be done both from mail groups and \"real\" newsgroups.
7887 In the former case, the articles in question will be moved from the
7888 current group into whatever groups they are destined to.  In the
7889 latter case, they will be copied into the relevant groups."
7890   (interactive
7891    (list current-prefix-arg
7892          (let* ((methods (gnus-methods-using 'respool))
7893                 (methname
7894                  (symbol-name (or gnus-summary-respool-default-method
7895                                   (car (gnus-find-method-for-group
7896                                         gnus-newsgroup-name)))))
7897                 (method
7898                  (gnus-completing-read
7899                   methname "What backend do you want to use when respooling?"
7900                   methods nil t nil 'gnus-mail-method-history))
7901                 ms)
7902            (cond
7903             ((zerop (length (setq ms (gnus-servers-using-backend
7904                                       (intern method)))))
7905              (list (intern method) ""))
7906             ((= 1 (length ms))
7907              (car ms))
7908             (t
7909              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7910                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7911                            ms-alist))))))))
7912   (unless method
7913     (error "No method given for respooling"))
7914   (if (assoc (symbol-name
7915               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7916              (gnus-methods-using 'respool))
7917       (gnus-summary-move-article n nil method)
7918     (gnus-summary-copy-article n nil method)))
7919
7920 (defun gnus-summary-import-article (file)
7921   "Import an arbitrary file into a mail newsgroup."
7922   (interactive "fImport file: ")
7923   (let ((group gnus-newsgroup-name)
7924         (now (current-time))
7925         atts lines)
7926     (unless (gnus-check-backend-function 'request-accept-article group)
7927       (error "%s does not support article importing" group))
7928     (or (file-readable-p file)
7929         (not (file-regular-p file))
7930         (error "Can't read %s" file))
7931     (save-excursion
7932       (set-buffer (gnus-get-buffer-create " *import file*"))
7933       (erase-buffer)
7934       (nnheader-insert-file-contents file)
7935       (goto-char (point-min))
7936       (unless (nnheader-article-p)
7937         ;; This doesn't look like an article, so we fudge some headers.
7938         (setq atts (file-attributes file)
7939               lines (count-lines (point-min) (point-max)))
7940         (insert "From: " (read-string "From: ") "\n"
7941                 "Subject: " (read-string "Subject: ") "\n"
7942                 "Date: " (message-make-date (nth 5 atts))
7943                 "\n"
7944                 "Message-ID: " (message-make-message-id) "\n"
7945                 "Lines: " (int-to-string lines) "\n"
7946                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7947       (gnus-request-accept-article group nil t)
7948       (kill-buffer (current-buffer)))))
7949
7950 (defun gnus-summary-article-posted-p ()
7951   "Say whether the current (mail) article is available from news as well.
7952 This will be the case if the article has both been mailed and posted."
7953   (interactive)
7954   (let ((id (mail-header-references (gnus-summary-article-header)))
7955         (gnus-override-method (car (gnus-refer-article-methods))))
7956     (if (gnus-request-head id "")
7957         (gnus-message 2 "The current message was found on %s"
7958                       gnus-override-method)
7959       (gnus-message 2 "The current message couldn't be found on %s"
7960                     gnus-override-method)
7961       nil)))
7962
7963 (defun gnus-summary-expire-articles (&optional now)
7964   "Expire all articles that are marked as expirable in the current group."
7965   (interactive)
7966   (when (gnus-check-backend-function
7967          'request-expire-articles gnus-newsgroup-name)
7968     ;; This backend supports expiry.
7969     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7970            (expirable (if total
7971                           (progn
7972                             ;; We need to update the info for
7973                             ;; this group for `gnus-list-of-read-articles'
7974                             ;; to give us the right answer.
7975                             (gnus-run-hooks 'gnus-exit-group-hook)
7976                             (gnus-summary-update-info)
7977                             (gnus-list-of-read-articles gnus-newsgroup-name))
7978                         (setq gnus-newsgroup-expirable
7979                               (sort gnus-newsgroup-expirable '<))))
7980            (expiry-wait (if now 'immediate
7981                           (gnus-group-find-parameter
7982                            gnus-newsgroup-name 'expiry-wait)))
7983            (nnmail-expiry-target
7984             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7985                 nnmail-expiry-target))
7986            es)
7987       (when expirable
7988         ;; There are expirable articles in this group, so we run them
7989         ;; through the expiry process.
7990         (gnus-message 6 "Expiring articles...")
7991         (unless (gnus-check-group gnus-newsgroup-name)
7992           (error "Can't open server for %s" gnus-newsgroup-name))
7993         ;; The list of articles that weren't expired is returned.
7994         (save-excursion
7995           (if expiry-wait
7996               (let ((nnmail-expiry-wait-function nil)
7997                     (nnmail-expiry-wait expiry-wait))
7998                 (setq es (gnus-request-expire-articles
7999                           expirable gnus-newsgroup-name)))
8000             (setq es (gnus-request-expire-articles
8001                       expirable gnus-newsgroup-name)))
8002           (unless total
8003             (setq gnus-newsgroup-expirable es))
8004           ;; We go through the old list of expirable, and mark all
8005           ;; really expired articles as nonexistent.
8006           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8007             (let ((gnus-use-cache nil))
8008               (while expirable
8009                 (unless (memq (car expirable) es)
8010                   (when (gnus-data-find (car expirable))
8011                     (gnus-summary-mark-article
8012                      (car expirable) gnus-canceled-mark)))
8013                 (setq expirable (cdr expirable))))))
8014         (gnus-message 6 "Expiring articles...done")))))
8015
8016 (defun gnus-summary-expire-articles-now ()
8017   "Expunge all expirable articles in the current group.
8018 This means that *all* articles that are marked as expirable will be
8019 deleted forever, right now."
8020   (interactive)
8021   (or gnus-expert-user
8022       (gnus-yes-or-no-p
8023        "Are you really, really, really sure you want to delete all these messages? ")
8024       (error "Phew!"))
8025   (gnus-summary-expire-articles t))
8026
8027 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8028 (defun gnus-summary-delete-article (&optional n)
8029   "Delete the N next (mail) articles.
8030 This command actually deletes articles.  This is not a marking
8031 command.  The article will disappear forever from your life, never to
8032 return.
8033 If N is negative, delete backwards.
8034 If N is nil and articles have been marked with the process mark,
8035 delete these instead."
8036   (interactive "P")
8037   (unless (gnus-check-backend-function 'request-expire-articles
8038                                        gnus-newsgroup-name)
8039     (error "The current newsgroup does not support article deletion"))
8040   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8041     (error "Couldn't open server"))
8042   ;; Compute the list of articles to delete.
8043   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8044         not-deleted)
8045     (if (and gnus-novice-user
8046              (not (gnus-yes-or-no-p
8047                    (format "Do you really want to delete %s forever? "
8048                            (if (> (length articles) 1)
8049                                (format "these %s articles" (length articles))
8050                              "this article")))))
8051         ()
8052       ;; Delete the articles.
8053       (setq not-deleted (gnus-request-expire-articles
8054                          articles gnus-newsgroup-name 'force))
8055       (while articles
8056         (gnus-summary-remove-process-mark (car articles))
8057         ;; The backend might not have been able to delete the article
8058         ;; after all.
8059         (unless (memq (car articles) not-deleted)
8060           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8061         (setq articles (cdr articles)))
8062       (when not-deleted
8063         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8064     (gnus-summary-position-point)
8065     (gnus-set-mode-line 'summary)
8066     not-deleted))
8067
8068 (defun gnus-summary-edit-article (&optional arg)
8069   "Edit the current article.
8070 This will have permanent effect only in mail groups.
8071 If ARG is nil, edit the decoded articles.
8072 If ARG is 1, edit the raw articles.
8073 If ARG is 2, edit the raw articles even in read-only groups.
8074 If ARG is 3, edit the articles with the current handles.
8075 Otherwise, allow editing of articles even in read-only
8076 groups."
8077   (interactive "P")
8078   (let (force raw current-handles)
8079     (cond
8080      ((null arg))
8081      ((eq arg 1) (setq raw t))
8082      ((eq arg 2) (setq raw t
8083                        force t))
8084      ((eq arg 3) (setq current-handles
8085                        (and (gnus-buffer-live-p gnus-article-buffer)
8086                             (with-current-buffer gnus-article-buffer
8087                               (prog1
8088                                   gnus-article-mime-handles
8089                                   (setq gnus-article-mime-handles nil))))))
8090      (t (setq force t)))
8091     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8092         (error "Can't edit the raw article in group nndraft:drafts."))
8093     (save-excursion
8094       (set-buffer gnus-summary-buffer)
8095       (let ((mail-parse-charset gnus-newsgroup-charset)
8096             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8097         (gnus-set-global-variables)
8098         (when (and (not force)
8099                    (gnus-group-read-only-p))
8100           (error "The current newsgroup does not support article editing"))
8101         (gnus-summary-show-article t)
8102         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8103           (with-current-buffer gnus-article-buffer
8104             (mm-enable-multibyte-mule4)))
8105         (if (equal gnus-newsgroup-name "nndraft:drafts")
8106             (setq raw t))
8107         (gnus-article-edit-article
8108          (if raw 'ignore
8109            `(lambda ()
8110               (let ((mbl mml-buffer-list))
8111                 (setq mml-buffer-list nil)
8112                 (mime-to-mml ,'current-handles)
8113                 (make-local-hook 'kill-buffer-hook)
8114                 (let ((mbl1 mml-buffer-list))
8115                   (setq mml-buffer-list mbl)
8116                   (set (make-local-variable 'mml-buffer-list) mbl1))
8117                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8118          `(lambda (no-highlight)
8119             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8120                   (message-options message-options)
8121                   (message-options-set-recipient)
8122                   (mail-parse-ignored-charsets
8123                    ',gnus-newsgroup-ignored-charsets))
8124               ,(if (not raw) '(progn
8125                                 (mml-to-mime)
8126                                 (mml-destroy-buffers)
8127                                 (remove-hook 'kill-buffer-hook
8128                                              'mml-destroy-buffers t)
8129                                 (kill-local-variable 'mml-buffer-list)))
8130               (gnus-summary-edit-article-done
8131                ,(or (mail-header-references gnus-current-headers) "")
8132                ,(gnus-group-read-only-p)
8133                ,gnus-summary-buffer no-highlight))))))))
8134
8135 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8136
8137 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8138                                                  no-highlight)
8139   "Make edits to the current article permanent."
8140   (interactive)
8141   (save-excursion
8142     ;; The buffer restriction contains the entire article if it exists.
8143     (when (article-goto-body)
8144       (let ((lines (count-lines (point) (point-max)))
8145             (length (- (point-max) (point)))
8146             (case-fold-search t)
8147             (body (copy-marker (point))))
8148         (goto-char (point-min))
8149         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8150           (delete-region (match-beginning 1) (match-end 1))
8151           (insert (number-to-string length)))
8152         (goto-char (point-min))
8153         (when (re-search-forward
8154                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8155           (delete-region (match-beginning 1) (match-end 1))
8156           (insert (number-to-string length)))
8157         (goto-char (point-min))
8158         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8159           (delete-region (match-beginning 1) (match-end 1))
8160           (insert (number-to-string lines))))))
8161   ;; Replace the article.
8162   (let ((buf (current-buffer)))
8163     (with-temp-buffer
8164       (insert-buffer-substring buf)
8165
8166       (if (and (not read-only)
8167                (not (gnus-request-replace-article
8168                      (cdr gnus-article-current) (car gnus-article-current)
8169                      (current-buffer) t)))
8170           (error "Couldn't replace article")
8171         ;; Update the summary buffer.
8172         (if (and references
8173                  (equal (message-tokenize-header references " ")
8174                         (message-tokenize-header
8175                          (or (message-fetch-field "references") "") " ")))
8176             ;; We only have to update this line.
8177             (save-excursion
8178               (save-restriction
8179                 (message-narrow-to-head)
8180                 (let ((head (buffer-string))
8181                       header)
8182                   (with-temp-buffer
8183                     (insert (format "211 %d Article retrieved.\n"
8184                                     (cdr gnus-article-current)))
8185                     (insert head)
8186                     (insert ".\n")
8187                     (let ((nntp-server-buffer (current-buffer)))
8188                       (setq header (car (gnus-get-newsgroup-headers
8189                                          (save-excursion
8190                                            (set-buffer gnus-summary-buffer)
8191                                            gnus-newsgroup-dependencies)
8192                                          t))))
8193                     (save-excursion
8194                       (set-buffer gnus-summary-buffer)
8195                       (gnus-data-set-header
8196                        (gnus-data-find (cdr gnus-article-current))
8197                        header)
8198                       (gnus-summary-update-article-line
8199                        (cdr gnus-article-current) header))))))
8200           ;; Update threads.
8201           (set-buffer (or buffer gnus-summary-buffer))
8202           (gnus-summary-update-article (cdr gnus-article-current)))
8203         ;; Prettify the article buffer again.
8204         (unless no-highlight
8205           (save-excursion
8206             (set-buffer gnus-article-buffer)
8207             ;;;!!! Fix this -- article should be rehighlighted.
8208             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8209             (set-buffer gnus-original-article-buffer)
8210             (gnus-request-article
8211              (cdr gnus-article-current)
8212              (car gnus-article-current) (current-buffer))))
8213         ;; Prettify the summary buffer line.
8214         (when (gnus-visual-p 'summary-highlight 'highlight)
8215           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8216
8217 (defun gnus-summary-edit-wash (key)
8218   "Perform editing command KEY in the article buffer."
8219   (interactive
8220    (list
8221     (progn
8222       (message "%s" (concat (this-command-keys) "- "))
8223       (read-char))))
8224   (message "")
8225   (gnus-summary-edit-article)
8226   (execute-kbd-macro (concat (this-command-keys) key))
8227   (gnus-article-edit-done))
8228
8229 ;;; Respooling
8230
8231 (defun gnus-summary-respool-query (&optional silent trace)
8232   "Query where the respool algorithm would put this article."
8233   (interactive)
8234   (let (gnus-mark-article-hook)
8235     (gnus-summary-select-article)
8236     (save-excursion
8237       (set-buffer gnus-original-article-buffer)
8238       (save-restriction
8239         (message-narrow-to-head)
8240         (let ((groups (nnmail-article-group 'identity trace)))
8241           (unless silent
8242             (if groups
8243                 (message "This message would go to %s"
8244                          (mapconcat 'car groups ", "))
8245               (message "This message would go to no groups"))
8246             groups))))))
8247
8248 (defun gnus-summary-respool-trace ()
8249   "Trace where the respool algorithm would put this article.
8250 Display a buffer showing all fancy splitting patterns which matched."
8251   (interactive)
8252   (gnus-summary-respool-query nil t))
8253
8254 ;; Summary marking commands.
8255
8256 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8257   "Mark articles which has the same subject as read, and then select the next.
8258 If UNMARK is positive, remove any kind of mark.
8259 If UNMARK is negative, tick articles."
8260   (interactive "P")
8261   (when unmark
8262     (setq unmark (prefix-numeric-value unmark)))
8263   (let ((count
8264          (gnus-summary-mark-same-subject
8265           (gnus-summary-article-subject) unmark)))
8266     ;; Select next unread article.  If auto-select-same mode, should
8267     ;; select the first unread article.
8268     (gnus-summary-next-article t (and gnus-auto-select-same
8269                                       (gnus-summary-article-subject)))
8270     (gnus-message 7 "%d article%s marked as %s"
8271                   count (if (= count 1) " is" "s are")
8272                   (if unmark "unread" "read"))))
8273
8274 (defun gnus-summary-kill-same-subject (&optional unmark)
8275   "Mark articles which has the same subject as read.
8276 If UNMARK is positive, remove any kind of mark.
8277 If UNMARK is negative, tick articles."
8278   (interactive "P")
8279   (when unmark
8280     (setq unmark (prefix-numeric-value unmark)))
8281   (let ((count
8282          (gnus-summary-mark-same-subject
8283           (gnus-summary-article-subject) unmark)))
8284     ;; If marked as read, go to next unread subject.
8285     (when (null unmark)
8286       ;; Go to next unread subject.
8287       (gnus-summary-next-subject 1 t))
8288     (gnus-message 7 "%d articles are marked as %s"
8289                   count (if unmark "unread" "read"))))
8290
8291 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8292   "Mark articles with same SUBJECT as read, and return marked number.
8293 If optional argument UNMARK is positive, remove any kinds of marks.
8294 If optional argument UNMARK is negative, mark articles as unread instead."
8295   (let ((count 1))
8296     (save-excursion
8297       (cond
8298        ((null unmark)                   ; Mark as read.
8299         (while (and
8300                 (progn
8301                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8302                   (gnus-summary-show-thread) t)
8303                 (gnus-summary-find-subject subject))
8304           (setq count (1+ count))))
8305        ((> unmark 0)                    ; Tick.
8306         (while (and
8307                 (progn
8308                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8309                   (gnus-summary-show-thread) t)
8310                 (gnus-summary-find-subject subject))
8311           (setq count (1+ count))))
8312        (t                               ; Mark as unread.
8313         (while (and
8314                 (progn
8315                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8316                   (gnus-summary-show-thread) t)
8317                 (gnus-summary-find-subject subject))
8318           (setq count (1+ count)))))
8319       (gnus-set-mode-line 'summary)
8320       ;; Return the number of marked articles.
8321       count)))
8322
8323 (defun gnus-summary-mark-as-processable (n &optional unmark)
8324   "Set the process mark on the next N articles.
8325 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8326 the process mark instead.  The difference between N and the actual
8327 number of articles marked is returned."
8328   (interactive "P")
8329   (if (and (null n) (gnus-region-active-p))
8330       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8331     (setq n (prefix-numeric-value n))
8332     (let ((backward (< n 0))
8333           (n (abs n)))
8334       (while (and
8335               (> n 0)
8336               (if unmark
8337                 (gnus-summary-remove-process-mark
8338                  (gnus-summary-article-number))
8339                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8340               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8341         (setq n (1- n)))
8342       (when (/= 0 n)
8343         (gnus-message 7 "No more articles"))
8344       (gnus-summary-recenter)
8345       (gnus-summary-position-point)
8346       n)))
8347
8348 (defun gnus-summary-unmark-as-processable (n)
8349   "Remove the process mark from the next N articles.
8350 If N is negative, unmark backward instead.  The difference between N and
8351 the actual number of articles unmarked is returned."
8352   (interactive "P")
8353   (gnus-summary-mark-as-processable n t))
8354
8355 (defun gnus-summary-unmark-all-processable ()
8356   "Remove the process mark from all articles."
8357   (interactive)
8358   (save-excursion
8359     (while gnus-newsgroup-processable
8360       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8361   (gnus-summary-position-point))
8362
8363 (defun gnus-summary-add-mark (article type)
8364   "Mark ARTICLE with a mark of TYPE."
8365   (let ((vtype (car (assq type gnus-article-mark-lists)))
8366         var)
8367     (if (not vtype)
8368         (error "No such mark type: %s" type)
8369       (setq var (intern (format "gnus-newsgroup-%s" type)))
8370       (set var (cons article (symbol-value var)))
8371       (if (memq type '(processable cached replied saved))
8372           (gnus-summary-update-secondary-mark article)
8373         ;;; !!! This is bobus.  We should find out what primary
8374         ;;; !!! mark we want to set.
8375         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8376
8377 (defun gnus-summary-mark-as-expirable (n)
8378   "Mark N articles forward as expirable.
8379 If N is negative, mark backward instead.  The difference between N and
8380 the actual number of articles marked is returned."
8381   (interactive "p")
8382   (gnus-summary-mark-forward n gnus-expirable-mark))
8383
8384 (defun gnus-summary-mark-article-as-replied (article)
8385   "Mark ARTICLE replied and update the summary line."
8386   (push article gnus-newsgroup-replied)
8387   (let ((buffer-read-only nil))
8388     (when (gnus-summary-goto-subject article nil t)
8389       (gnus-summary-update-secondary-mark article))))
8390
8391 (defun gnus-summary-set-bookmark (article)
8392   "Set a bookmark in current article."
8393   (interactive (list (gnus-summary-article-number)))
8394   (when (or (not (get-buffer gnus-article-buffer))
8395             (not gnus-current-article)
8396             (not gnus-article-current)
8397             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8398     (error "No current article selected"))
8399   ;; Remove old bookmark, if one exists.
8400   (let ((old (assq article gnus-newsgroup-bookmarks)))
8401     (when old
8402       (setq gnus-newsgroup-bookmarks
8403             (delq old gnus-newsgroup-bookmarks))))
8404   ;; Set the new bookmark, which is on the form
8405   ;; (article-number . line-number-in-body).
8406   (push
8407    (cons article
8408          (save-excursion
8409            (set-buffer gnus-article-buffer)
8410            (count-lines
8411             (min (point)
8412                  (save-excursion
8413                    (goto-char (point-min))
8414                    (search-forward "\n\n" nil t)
8415                    (point)))
8416             (point))))
8417    gnus-newsgroup-bookmarks)
8418   (gnus-message 6 "A bookmark has been added to the current article."))
8419
8420 (defun gnus-summary-remove-bookmark (article)
8421   "Remove the bookmark from the current article."
8422   (interactive (list (gnus-summary-article-number)))
8423   ;; Remove old bookmark, if one exists.
8424   (let ((old (assq article gnus-newsgroup-bookmarks)))
8425     (if old
8426         (progn
8427           (setq gnus-newsgroup-bookmarks
8428                 (delq old gnus-newsgroup-bookmarks))
8429           (gnus-message 6 "Removed bookmark."))
8430       (gnus-message 6 "No bookmark in current article."))))
8431
8432 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8433 (defun gnus-summary-mark-as-dormant (n)
8434   "Mark N articles forward as dormant.
8435 If N is negative, mark backward instead.  The difference between N and
8436 the actual number of articles marked is returned."
8437   (interactive "p")
8438   (gnus-summary-mark-forward n gnus-dormant-mark))
8439
8440 (defun gnus-summary-set-process-mark (article)
8441   "Set the process mark on ARTICLE and update the summary line."
8442   (setq gnus-newsgroup-processable
8443         (cons article
8444               (delq article gnus-newsgroup-processable)))
8445   (when (gnus-summary-goto-subject article)
8446     (gnus-summary-show-thread)
8447     (gnus-summary-goto-subject article)
8448     (gnus-summary-update-secondary-mark article)))
8449
8450 (defun gnus-summary-remove-process-mark (article)
8451   "Remove the process mark from ARTICLE and update the summary line."
8452   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8453   (when (gnus-summary-goto-subject article)
8454     (gnus-summary-show-thread)
8455     (gnus-summary-goto-subject article)
8456     (gnus-summary-update-secondary-mark article)))
8457
8458 (defun gnus-summary-set-saved-mark (article)
8459   "Set the process mark on ARTICLE and update the summary line."
8460   (push article gnus-newsgroup-saved)
8461   (when (gnus-summary-goto-subject article)
8462     (gnus-summary-update-secondary-mark article)))
8463
8464 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8465   "Mark N articles as read forwards.
8466 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8467 The difference between N and the actual number of articles marked is
8468 returned.
8469 Iff NO-EXPIRE, auto-expiry will be inhibited."
8470   (interactive "p")
8471   (gnus-summary-show-thread)
8472   (let ((backward (< n 0))
8473         (gnus-summary-goto-unread
8474          (and gnus-summary-goto-unread
8475               (not (eq gnus-summary-goto-unread 'never))
8476               (not (memq mark (list gnus-unread-mark
8477                                     gnus-ticked-mark gnus-dormant-mark)))))
8478         (n (abs n))
8479         (mark (or mark gnus-del-mark)))
8480     (while (and (> n 0)
8481                 (gnus-summary-mark-article nil mark no-expire)
8482                 (zerop (gnus-summary-next-subject
8483                         (if backward -1 1)
8484                         (and gnus-summary-goto-unread
8485                              (not (eq gnus-summary-goto-unread 'never)))
8486                         t)))
8487       (setq n (1- n)))
8488     (when (/= 0 n)
8489       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8490     (gnus-summary-recenter)
8491     (gnus-summary-position-point)
8492     (gnus-set-mode-line 'summary)
8493     n))
8494
8495 (defun gnus-summary-mark-article-as-read (mark)
8496   "Mark the current article quickly as read with MARK."
8497   (let ((article (gnus-summary-article-number)))
8498     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8499     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8500     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8501     (push (cons article mark) gnus-newsgroup-reads)
8502     ;; Possibly remove from cache, if that is used.
8503     (when gnus-use-cache
8504       (gnus-cache-enter-remove-article article))
8505     ;; Allow the backend to change the mark.
8506     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8507     ;; Check for auto-expiry.
8508     (when (and gnus-newsgroup-auto-expire
8509                (memq mark gnus-auto-expirable-marks))
8510       (setq mark gnus-expirable-mark)
8511       ;; Let the backend know about the mark change.
8512       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8513       (push article gnus-newsgroup-expirable))
8514     ;; Set the mark in the buffer.
8515     (gnus-summary-update-mark mark 'unread)
8516     t))
8517
8518 (defun gnus-summary-mark-article-as-unread (mark)
8519   "Mark the current article quickly as unread with MARK."
8520   (let* ((article (gnus-summary-article-number))
8521          (old-mark (gnus-summary-article-mark article)))
8522     ;; Allow the backend to change the mark.
8523     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8524     (if (eq mark old-mark)
8525         t
8526       (if (<= article 0)
8527           (progn
8528             (gnus-error 1 "Can't mark negative article numbers")
8529             nil)
8530         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8531         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8532         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8533         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8534         (cond ((= mark gnus-ticked-mark)
8535                (push article gnus-newsgroup-marked))
8536               ((= mark gnus-dormant-mark)
8537                (push article gnus-newsgroup-dormant))
8538               (t
8539                (push article gnus-newsgroup-unreads)))
8540         (gnus-pull article gnus-newsgroup-reads)
8541
8542         ;; See whether the article is to be put in the cache.
8543         (and gnus-use-cache
8544              (vectorp (gnus-summary-article-header article))
8545              (save-excursion
8546                (gnus-cache-possibly-enter-article
8547                 gnus-newsgroup-name article
8548                 (= mark gnus-ticked-mark)
8549                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8550
8551         ;; Fix the mark.
8552         (gnus-summary-update-mark mark 'unread)
8553         t))))
8554
8555 (defun gnus-summary-mark-article (&optional article mark no-expire)
8556   "Mark ARTICLE with MARK.  MARK can be any character.
8557 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8558 `??' (dormant) and `?E' (expirable).
8559 If MARK is nil, then the default character `?r' is used.
8560 If ARTICLE is nil, then the article on the current line will be
8561 marked.
8562 Iff NO-EXPIRE, auto-expiry will be inhibited."
8563   ;; The mark might be a string.
8564   (when (stringp mark)
8565     (setq mark (aref mark 0)))
8566   ;; If no mark is given, then we check auto-expiring.
8567   (when (null mark)
8568     (setq mark gnus-del-mark))
8569   (when (and (not no-expire)
8570              gnus-newsgroup-auto-expire
8571              (memq mark gnus-auto-expirable-marks))
8572     (setq mark gnus-expirable-mark))
8573   (let ((article (or article (gnus-summary-article-number)))
8574         (old-mark (gnus-summary-article-mark article)))
8575     ;; Allow the backend to change the mark.
8576     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8577     (if (eq mark old-mark)
8578         t
8579       (unless article
8580         (error "No article on current line"))
8581       (if (not (if (or (= mark gnus-unread-mark)
8582                        (= mark gnus-ticked-mark)
8583                        (= mark gnus-dormant-mark))
8584                    (gnus-mark-article-as-unread article mark)
8585                  (gnus-mark-article-as-read article mark)))
8586           t
8587         ;; See whether the article is to be put in the cache.
8588         (and gnus-use-cache
8589              (not (= mark gnus-canceled-mark))
8590              (vectorp (gnus-summary-article-header article))
8591              (save-excursion
8592                (gnus-cache-possibly-enter-article
8593                 gnus-newsgroup-name article
8594                 (= mark gnus-ticked-mark)
8595                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8596
8597         (when (gnus-summary-goto-subject article nil t)
8598           (let ((buffer-read-only nil))
8599             (gnus-summary-show-thread)
8600             ;; Fix the mark.
8601             (gnus-summary-update-mark mark 'unread)
8602             t))))))
8603
8604 (defun gnus-summary-update-secondary-mark (article)
8605   "Update the secondary (read, process, cache) mark."
8606   (gnus-summary-update-mark
8607    (cond ((memq article gnus-newsgroup-processable)
8608           gnus-process-mark)
8609          ((memq article gnus-newsgroup-cached)
8610           gnus-cached-mark)
8611          ((memq article gnus-newsgroup-replied)
8612           gnus-replied-mark)
8613          ((memq article gnus-newsgroup-saved)
8614           gnus-saved-mark)
8615          (t gnus-no-mark))
8616    'replied)
8617   (when (gnus-visual-p 'summary-highlight 'highlight)
8618     (gnus-run-hooks 'gnus-summary-update-hook))
8619   t)
8620
8621 (defun gnus-summary-update-mark (mark type)
8622   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8623         (buffer-read-only nil))
8624     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8625     (when forward
8626       (when (looking-at "\r")
8627         (incf forward))
8628       (when (<= (+ forward (point)) (point-max))
8629         ;; Go to the right position on the line.
8630         (goto-char (+ forward (point)))
8631         ;; Replace the old mark with the new mark.
8632         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8633         ;; Optionally update the marks by some user rule.
8634         (when (eq type 'unread)
8635           (gnus-data-set-mark
8636            (gnus-data-find (gnus-summary-article-number)) mark)
8637           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8638
8639 (defun gnus-mark-article-as-read (article &optional mark)
8640   "Enter ARTICLE in the pertinent lists and remove it from others."
8641   ;; Make the article expirable.
8642   (let ((mark (or mark gnus-del-mark)))
8643     (if (= mark gnus-expirable-mark)
8644         (push article gnus-newsgroup-expirable)
8645       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8646     ;; Remove from unread and marked lists.
8647     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8648     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8649     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8650     (push (cons article mark) gnus-newsgroup-reads)
8651     ;; Possibly remove from cache, if that is used.
8652     (when gnus-use-cache
8653       (gnus-cache-enter-remove-article article))
8654     t))
8655
8656 (defun gnus-mark-article-as-unread (article &optional mark)
8657   "Enter ARTICLE in the pertinent lists and remove it from others."
8658   (let ((mark (or mark gnus-ticked-mark)))
8659     (if (<= article 0)
8660         (progn
8661           (gnus-error 1 "Can't mark negative article numbers")
8662           nil)
8663       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8664             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8665             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8666             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8667
8668       ;; Unsuppress duplicates?
8669       (when gnus-suppress-duplicates
8670         (gnus-dup-unsuppress-article article))
8671
8672       (cond ((= mark gnus-ticked-mark)
8673              (push article gnus-newsgroup-marked))
8674             ((= mark gnus-dormant-mark)
8675              (push article gnus-newsgroup-dormant))
8676             (t
8677              (push article gnus-newsgroup-unreads)))
8678       (gnus-pull article gnus-newsgroup-reads)
8679       t)))
8680
8681 (defalias 'gnus-summary-mark-as-unread-forward
8682   'gnus-summary-tick-article-forward)
8683 (make-obsolete 'gnus-summary-mark-as-unread-forward
8684                'gnus-summary-tick-article-forward)
8685 (defun gnus-summary-tick-article-forward (n)
8686   "Tick N articles forwards.
8687 If N is negative, tick backwards instead.
8688 The difference between N and the number of articles ticked is returned."
8689   (interactive "p")
8690   (gnus-summary-mark-forward n gnus-ticked-mark))
8691
8692 (defalias 'gnus-summary-mark-as-unread-backward
8693   'gnus-summary-tick-article-backward)
8694 (make-obsolete 'gnus-summary-mark-as-unread-backward
8695                'gnus-summary-tick-article-backward)
8696 (defun gnus-summary-tick-article-backward (n)
8697   "Tick N articles backwards.
8698 The difference between N and the number of articles ticked is returned."
8699   (interactive "p")
8700   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8701
8702 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8703 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8704 (defun gnus-summary-tick-article (&optional article clear-mark)
8705   "Mark current article as unread.
8706 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8707 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8708   (interactive)
8709   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8710                                        gnus-ticked-mark)))
8711
8712 (defun gnus-summary-mark-as-read-forward (n)
8713   "Mark N articles as read forwards.
8714 If N is negative, mark backwards instead.
8715 The difference between N and the actual number of articles marked is
8716 returned."
8717   (interactive "p")
8718   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8719
8720 (defun gnus-summary-mark-as-read-backward (n)
8721   "Mark the N articles as read backwards.
8722 The difference between N and the actual number of articles marked is
8723 returned."
8724   (interactive "p")
8725   (gnus-summary-mark-forward
8726    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8727
8728 (defun gnus-summary-mark-as-read (&optional article mark)
8729   "Mark current article as read.
8730 ARTICLE specifies the article to be marked as read.
8731 MARK specifies a string to be inserted at the beginning of the line."
8732   (gnus-summary-mark-article article mark))
8733
8734 (defun gnus-summary-clear-mark-forward (n)
8735   "Clear marks from N articles forward.
8736 If N is negative, clear backward instead.
8737 The difference between N and the number of marks cleared is returned."
8738   (interactive "p")
8739   (gnus-summary-mark-forward n gnus-unread-mark))
8740
8741 (defun gnus-summary-clear-mark-backward (n)
8742   "Clear marks from N articles backward.
8743 The difference between N and the number of marks cleared is returned."
8744   (interactive "p")
8745   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8746
8747 (defun gnus-summary-mark-unread-as-read ()
8748   "Intended to be used by `gnus-summary-mark-article-hook'."
8749   (when (memq gnus-current-article gnus-newsgroup-unreads)
8750     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8751
8752 (defun gnus-summary-mark-read-and-unread-as-read ()
8753   "Intended to be used by `gnus-summary-mark-article-hook'."
8754   (let ((mark (gnus-summary-article-mark)))
8755     (when (or (gnus-unread-mark-p mark)
8756               (gnus-read-mark-p mark))
8757       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8758
8759 (defun gnus-summary-mark-unread-as-ticked ()
8760    "Intended to be used by `gnus-summary-mark-article-hook'."
8761   (when (memq gnus-current-article gnus-newsgroup-unreads)
8762     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8763
8764 (defun gnus-summary-mark-region-as-read (point mark all)
8765   "Mark all unread articles between point and mark as read.
8766 If given a prefix, mark all articles between point and mark as read,
8767 even ticked and dormant ones."
8768   (interactive "r\nP")
8769   (save-excursion
8770     (let (article)
8771       (goto-char point)
8772       (beginning-of-line)
8773       (while (and
8774               (< (point) mark)
8775               (progn
8776                 (when (or all
8777                           (memq (setq article (gnus-summary-article-number))
8778                                 gnus-newsgroup-unreads))
8779                   (gnus-summary-mark-article article gnus-del-mark))
8780                 t)
8781               (gnus-summary-find-next))))))
8782
8783 (defun gnus-summary-mark-below (score mark)
8784   "Mark articles with score less than SCORE with MARK."
8785   (interactive "P\ncMark: ")
8786   (setq score (if score
8787                   (prefix-numeric-value score)
8788                 (or gnus-summary-default-score 0)))
8789   (save-excursion
8790     (set-buffer gnus-summary-buffer)
8791     (goto-char (point-min))
8792     (while
8793         (progn
8794           (and (< (gnus-summary-article-score) score)
8795                (gnus-summary-mark-article nil mark))
8796           (gnus-summary-find-next)))))
8797
8798 (defun gnus-summary-kill-below (&optional score)
8799   "Mark articles with score below SCORE as read."
8800   (interactive "P")
8801   (gnus-summary-mark-below score gnus-killed-mark))
8802
8803 (defun gnus-summary-clear-above (&optional score)
8804   "Clear all marks from articles with score above SCORE."
8805   (interactive "P")
8806   (gnus-summary-mark-above score gnus-unread-mark))
8807
8808 (defun gnus-summary-tick-above (&optional score)
8809   "Tick all articles with score above SCORE."
8810   (interactive "P")
8811   (gnus-summary-mark-above score gnus-ticked-mark))
8812
8813 (defun gnus-summary-mark-above (score mark)
8814   "Mark articles with score over SCORE with MARK."
8815   (interactive "P\ncMark: ")
8816   (setq score (if score
8817                   (prefix-numeric-value score)
8818                 (or gnus-summary-default-score 0)))
8819   (save-excursion
8820     (set-buffer gnus-summary-buffer)
8821     (goto-char (point-min))
8822     (while (and (progn
8823                   (when (> (gnus-summary-article-score) score)
8824                     (gnus-summary-mark-article nil mark))
8825                   t)
8826                 (gnus-summary-find-next)))))
8827
8828 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8829 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8830 (defun gnus-summary-limit-include-expunged (&optional no-error)
8831   "Display all the hidden articles that were expunged for low scores."
8832   (interactive)
8833   (let ((buffer-read-only nil))
8834     (let ((scored gnus-newsgroup-scored)
8835           headers h)
8836       (while scored
8837         (unless (gnus-number-to-header (caar scored))
8838           (and (setq h (gnus-summary-article-header (caar scored)))
8839                (< (cdar scored) gnus-summary-expunge-below)
8840                (push h headers)))
8841         (setq scored (cdr scored)))
8842       (if (not headers)
8843           (when (not no-error)
8844             (error "No expunged articles hidden"))
8845         (goto-char (point-min))
8846         (gnus-summary-prepare-unthreaded (nreverse headers))
8847         (goto-char (point-min))
8848         (gnus-summary-position-point)
8849         t))))
8850
8851 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8852   "Mark all unread articles in this newsgroup as read.
8853 If prefix argument ALL is non-nil, ticked and dormant articles will
8854 also be marked as read.
8855 If QUIETLY is non-nil, no questions will be asked.
8856 If TO-HERE is non-nil, it should be a point in the buffer.  All
8857 articles before this point will be marked as read.
8858 Note that this function will only catch up the unread article
8859 in the current summary buffer limitation.
8860 The number of articles marked as read is returned."
8861   (interactive "P")
8862   (prog1
8863       (save-excursion
8864         (when (or quietly
8865                   (not gnus-interactive-catchup) ;Without confirmation?
8866                   gnus-expert-user
8867                   (gnus-y-or-n-p
8868                    (if all
8869                        "Mark absolutely all articles as read? "
8870                      "Mark all unread articles as read? ")))
8871           (if (and not-mark
8872                    (not gnus-newsgroup-adaptive)
8873                    (not gnus-newsgroup-auto-expire)
8874                    (not gnus-suppress-duplicates)
8875                    (or (not gnus-use-cache)
8876                        (eq gnus-use-cache 'passive)))
8877               (progn
8878                 (when all
8879                   (setq gnus-newsgroup-marked nil
8880                         gnus-newsgroup-dormant nil))
8881                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8882             ;; We actually mark all articles as canceled, which we
8883             ;; have to do when using auto-expiry or adaptive scoring.
8884             (gnus-summary-show-all-threads)
8885             (when (gnus-summary-first-subject (not all) t)
8886               (while (and
8887                       (if to-here (< (point) to-here) t)
8888                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8889                       (gnus-summary-find-next (not all) nil nil t))))
8890             (gnus-set-mode-line 'summary))
8891           t))
8892     (gnus-summary-position-point)))
8893
8894 (defun gnus-summary-catchup-to-here (&optional all)
8895   "Mark all unticked articles before the current one as read.
8896 If ALL is non-nil, also mark ticked and dormant articles as read."
8897   (interactive "P")
8898   (save-excursion
8899     (gnus-save-hidden-threads
8900       (let ((beg (point)))
8901         ;; We check that there are unread articles.
8902         (when (or all (gnus-summary-find-prev))
8903           (gnus-summary-catchup all t beg)))))
8904   (gnus-summary-position-point))
8905
8906 (defun gnus-summary-catchup-all (&optional quietly)
8907   "Mark all articles in this newsgroup as read."
8908   (interactive "P")
8909   (gnus-summary-catchup t quietly))
8910
8911 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8912   "Mark all unread articles in this group as read, then exit.
8913 If prefix argument ALL is non-nil, all articles are marked as read."
8914   (interactive "P")
8915   (when (gnus-summary-catchup all quietly nil 'fast)
8916     ;; Select next newsgroup or exit.
8917     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8918              (eq gnus-auto-select-next 'quietly))
8919         (gnus-summary-next-group nil)
8920       (gnus-summary-exit))))
8921
8922 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8923   "Mark all articles in this newsgroup as read, and then exit."
8924   (interactive "P")
8925   (gnus-summary-catchup-and-exit t quietly))
8926
8927 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8928   "Mark all articles in this group as read and select the next group.
8929 If given a prefix, mark all articles, unread as well as ticked, as
8930 read."
8931   (interactive "P")
8932   (save-excursion
8933     (gnus-summary-catchup all))
8934   (gnus-summary-next-group))
8935
8936 ;;;
8937 ;;; with article
8938 ;;;
8939
8940 (defmacro gnus-with-article (article &rest forms)
8941   "Select ARTICLE and perform FORMS in the original article buffer.
8942 Then replace the article with the result."
8943   `(progn
8944      ;; We don't want the article to be marked as read.
8945      (let (gnus-mark-article-hook)
8946        (gnus-summary-select-article t t nil ,article))
8947      (set-buffer gnus-original-article-buffer)
8948      ,@forms
8949      (if (not (gnus-check-backend-function
8950                'request-replace-article (car gnus-article-current)))
8951          (gnus-message 5 "Read-only group; not replacing")
8952        (unless (gnus-request-replace-article
8953                 ,article (car gnus-article-current)
8954                 (current-buffer) t)
8955          (error "Couldn't replace article")))
8956      ;; The cache and backlog have to be flushed somewhat.
8957      (when gnus-keep-backlog
8958        (gnus-backlog-remove-article
8959         (car gnus-article-current) (cdr gnus-article-current)))
8960      (when gnus-use-cache
8961        (gnus-cache-update-article
8962         (car gnus-article-current) (cdr gnus-article-current)))))
8963
8964 (put 'gnus-with-article 'lisp-indent-function 1)
8965 (put 'gnus-with-article 'edebug-form-spec '(form body))
8966
8967 ;; Thread-based commands.
8968
8969 (defun gnus-summary-articles-in-thread (&optional article)
8970   "Return a list of all articles in the current thread.
8971 If ARTICLE is non-nil, return all articles in the thread that starts
8972 with that article."
8973   (let* ((article (or article (gnus-summary-article-number)))
8974          (data (gnus-data-find-list article))
8975          (top-level (gnus-data-level (car data)))
8976          (top-subject
8977           (cond ((null gnus-thread-operation-ignore-subject)
8978                  (gnus-simplify-subject-re
8979                   (mail-header-subject (gnus-data-header (car data)))))
8980                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8981                  (gnus-simplify-subject-fuzzy
8982                   (mail-header-subject (gnus-data-header (car data)))))
8983                 (t nil)))
8984          (end-point (save-excursion
8985                       (if (gnus-summary-go-to-next-thread)
8986                           (point) (point-max))))
8987          articles)
8988     (while (and data
8989                 (< (gnus-data-pos (car data)) end-point))
8990       (when (or (not top-subject)
8991                 (string= top-subject
8992                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8993                              (gnus-simplify-subject-fuzzy
8994                               (mail-header-subject
8995                                (gnus-data-header (car data))))
8996                            (gnus-simplify-subject-re
8997                             (mail-header-subject
8998                              (gnus-data-header (car data)))))))
8999         (push (gnus-data-number (car data)) articles))
9000       (unless (and (setq data (cdr data))
9001                    (> (gnus-data-level (car data)) top-level))
9002         (setq data nil)))
9003     ;; Return the list of articles.
9004     (nreverse articles)))
9005
9006 (defun gnus-summary-rethread-current ()
9007   "Rethread the thread the current article is part of."
9008   (interactive)
9009   (let* ((gnus-show-threads t)
9010          (article (gnus-summary-article-number))
9011          (id (mail-header-id (gnus-summary-article-header)))
9012          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9013     (unless id
9014       (error "No article on the current line"))
9015     (gnus-rebuild-thread id)
9016     (gnus-summary-goto-subject article)))
9017
9018 (defun gnus-summary-reparent-thread ()
9019   "Make the current article child of the marked (or previous) article.
9020
9021 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9022 is non-nil or the Subject: of both articles are the same."
9023   (interactive)
9024   (unless (not (gnus-group-read-only-p))
9025     (error "The current newsgroup does not support article editing"))
9026   (unless (<= (length gnus-newsgroup-processable) 1)
9027     (error "No more than one article may be marked"))
9028   (save-window-excursion
9029     (let ((gnus-article-buffer " *reparent*")
9030           (current-article (gnus-summary-article-number))
9031           ;; First grab the marked article, otherwise one line up.
9032           (parent-article (if (not (null gnus-newsgroup-processable))
9033                               (car gnus-newsgroup-processable)
9034                             (save-excursion
9035                               (if (eq (forward-line -1) 0)
9036                                   (gnus-summary-article-number)
9037                                 (error "Beginning of summary buffer"))))))
9038       (unless (not (eq current-article parent-article))
9039         (error "An article may not be self-referential"))
9040       (let ((message-id (mail-header-id
9041                          (gnus-summary-article-header parent-article))))
9042         (unless (and message-id (not (equal message-id "")))
9043           (error "No message-id in desired parent"))
9044         (gnus-with-article current-article
9045           (save-restriction
9046             (goto-char (point-min))
9047             (message-narrow-to-head)
9048             (if (re-search-forward "^References: " nil t)
9049                 (progn
9050                   (re-search-forward "^[^ \t]" nil t)
9051                   (forward-line -1)
9052                   (end-of-line)
9053                   (insert " " message-id))
9054               (insert "References: " message-id "\n"))))
9055         (set-buffer gnus-summary-buffer)
9056         (gnus-summary-unmark-all-processable)
9057         (gnus-summary-update-article current-article)
9058         (gnus-summary-rethread-current)
9059         (gnus-message 3 "Article %d is now the child of article %d"
9060                       current-article parent-article)))))
9061
9062 (defun gnus-summary-toggle-threads (&optional arg)
9063   "Toggle showing conversation threads.
9064 If ARG is positive number, turn showing conversation threads on."
9065   (interactive "P")
9066   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9067     (setq gnus-show-threads
9068           (if (null arg) (not gnus-show-threads)
9069             (> (prefix-numeric-value arg) 0)))
9070     (gnus-summary-prepare)
9071     (gnus-summary-goto-subject current)
9072     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9073     (gnus-summary-position-point)))
9074
9075 (defun gnus-summary-show-all-threads ()
9076   "Show all threads."
9077   (interactive)
9078   (save-excursion
9079     (let ((buffer-read-only nil))
9080       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9081   (gnus-summary-position-point))
9082
9083 (defun gnus-summary-show-thread ()
9084   "Show thread subtrees.
9085 Returns nil if no thread was there to be shown."
9086   (interactive)
9087   (let ((buffer-read-only nil)
9088         (orig (point))
9089         ;; first goto end then to beg, to have point at beg after let
9090         (end (progn (end-of-line) (point)))
9091         (beg (progn (beginning-of-line) (point))))
9092     (prog1
9093         ;; Any hidden lines here?
9094         (search-forward "\r" end t)
9095       (subst-char-in-region beg end ?\^M ?\n t)
9096       (goto-char orig)
9097       (gnus-summary-position-point))))
9098
9099 (defun gnus-summary-hide-all-threads ()
9100   "Hide all thread subtrees."
9101   (interactive)
9102   (save-excursion
9103     (goto-char (point-min))
9104     (gnus-summary-hide-thread)
9105     (while (zerop (gnus-summary-next-thread 1 t))
9106       (gnus-summary-hide-thread)))
9107   (gnus-summary-position-point))
9108
9109 (defun gnus-summary-hide-thread ()
9110   "Hide thread subtrees.
9111 Returns nil if no threads were there to be hidden."
9112   (interactive)
9113   (let ((buffer-read-only nil)
9114         (start (point))
9115         (article (gnus-summary-article-number)))
9116     (goto-char start)
9117     ;; Go forward until either the buffer ends or the subthread
9118     ;; ends.
9119     (when (and (not (eobp))
9120                (or (zerop (gnus-summary-next-thread 1 t))
9121                    (goto-char (point-max))))
9122       (prog1
9123           (if (and (> (point) start)
9124                    (search-backward "\n" start t))
9125               (progn
9126                 (subst-char-in-region start (point) ?\n ?\^M)
9127                 (gnus-summary-goto-subject article))
9128             (goto-char start)
9129             nil)))))
9130
9131 (defun gnus-summary-go-to-next-thread (&optional previous)
9132   "Go to the same level (or less) next thread.
9133 If PREVIOUS is non-nil, go to previous thread instead.
9134 Return the article number moved to, or nil if moving was impossible."
9135   (let ((level (gnus-summary-thread-level))
9136         (way (if previous -1 1))
9137         (beg (point)))
9138     (forward-line way)
9139     (while (and (not (eobp))
9140                 (< level (gnus-summary-thread-level)))
9141       (forward-line way))
9142     (if (eobp)
9143         (progn
9144           (goto-char beg)
9145           nil)
9146       (setq beg (point))
9147       (prog1
9148           (gnus-summary-article-number)
9149         (goto-char beg)))))
9150
9151 (defun gnus-summary-next-thread (n &optional silent)
9152   "Go to the same level next N'th thread.
9153 If N is negative, search backward instead.
9154 Returns the difference between N and the number of skips actually
9155 done.
9156
9157 If SILENT, don't output messages."
9158   (interactive "p")
9159   (let ((backward (< n 0))
9160         (n (abs n)))
9161     (while (and (> n 0)
9162                 (gnus-summary-go-to-next-thread backward))
9163       (decf n))
9164     (unless silent
9165       (gnus-summary-position-point))
9166     (when (and (not silent) (/= 0 n))
9167       (gnus-message 7 "No more threads"))
9168     n))
9169
9170 (defun gnus-summary-prev-thread (n)
9171   "Go to the same level previous N'th thread.
9172 Returns the difference between N and the number of skips actually
9173 done."
9174   (interactive "p")
9175   (gnus-summary-next-thread (- n)))
9176
9177 (defun gnus-summary-go-down-thread ()
9178   "Go down one level in the current thread."
9179   (let ((children (gnus-summary-article-children)))
9180     (when children
9181       (gnus-summary-goto-subject (car children)))))
9182
9183 (defun gnus-summary-go-up-thread ()
9184   "Go up one level in the current thread."
9185   (let ((parent (gnus-summary-article-parent)))
9186     (when parent
9187       (gnus-summary-goto-subject parent))))
9188
9189 (defun gnus-summary-down-thread (n)
9190   "Go down thread N steps.
9191 If N is negative, go up instead.
9192 Returns the difference between N and how many steps down that were
9193 taken."
9194   (interactive "p")
9195   (let ((up (< n 0))
9196         (n (abs n)))
9197     (while (and (> n 0)
9198                 (if up (gnus-summary-go-up-thread)
9199                   (gnus-summary-go-down-thread)))
9200       (setq n (1- n)))
9201     (gnus-summary-position-point)
9202     (when (/= 0 n)
9203       (gnus-message 7 "Can't go further"))
9204     n))
9205
9206 (defun gnus-summary-up-thread (n)
9207   "Go up thread N steps.
9208 If N is negative, go up instead.
9209 Returns the difference between N and how many steps down that were
9210 taken."
9211   (interactive "p")
9212   (gnus-summary-down-thread (- n)))
9213
9214 (defun gnus-summary-top-thread ()
9215   "Go to the top of the thread."
9216   (interactive)
9217   (while (gnus-summary-go-up-thread))
9218   (gnus-summary-article-number))
9219
9220 (defun gnus-summary-kill-thread (&optional unmark)
9221   "Mark articles under current thread as read.
9222 If the prefix argument is positive, remove any kinds of marks.
9223 If the prefix argument is negative, tick articles instead."
9224   (interactive "P")
9225   (when unmark
9226     (setq unmark (prefix-numeric-value unmark)))
9227   (let ((articles (gnus-summary-articles-in-thread)))
9228     (save-excursion
9229       ;; Expand the thread.
9230       (gnus-summary-show-thread)
9231       ;; Mark all the articles.
9232       (while articles
9233         (gnus-summary-goto-subject (car articles))
9234         (cond ((null unmark)
9235                (gnus-summary-mark-article-as-read gnus-killed-mark))
9236               ((> unmark 0)
9237                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9238               (t
9239                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9240         (setq articles (cdr articles))))
9241     ;; Hide killed subtrees.
9242     (and (null unmark)
9243          gnus-thread-hide-killed
9244          (gnus-summary-hide-thread))
9245     ;; If marked as read, go to next unread subject.
9246     (when (null unmark)
9247       ;; Go to next unread subject.
9248       (gnus-summary-next-subject 1 t)))
9249   (gnus-set-mode-line 'summary))
9250
9251 ;; Summary sorting commands
9252
9253 (defun gnus-summary-sort-by-number (&optional reverse)
9254   "Sort the summary buffer by article number.
9255 Argument REVERSE means reverse order."
9256   (interactive "P")
9257   (gnus-summary-sort 'number reverse))
9258
9259 (defun gnus-summary-sort-by-author (&optional reverse)
9260   "Sort the summary buffer by author name alphabetically.
9261 If `case-fold-search' is non-nil, case of letters is ignored.
9262 Argument REVERSE means reverse order."
9263   (interactive "P")
9264   (gnus-summary-sort 'author reverse))
9265
9266 (defun gnus-summary-sort-by-subject (&optional reverse)
9267   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9268 If `case-fold-search' is non-nil, case of letters is ignored.
9269 Argument REVERSE means reverse order."
9270   (interactive "P")
9271   (gnus-summary-sort 'subject reverse))
9272
9273 (defun gnus-summary-sort-by-date (&optional reverse)
9274   "Sort the summary buffer by date.
9275 Argument REVERSE means reverse order."
9276   (interactive "P")
9277   (gnus-summary-sort 'date reverse))
9278
9279 (defun gnus-summary-sort-by-score (&optional reverse)
9280   "Sort the summary buffer by score.
9281 Argument REVERSE means reverse order."
9282   (interactive "P")
9283   (gnus-summary-sort 'score reverse))
9284
9285 (defun gnus-summary-sort-by-lines (&optional reverse)
9286   "Sort the summary buffer by the number of lines.
9287 Argument REVERSE means reverse order."
9288   (interactive "P")
9289   (gnus-summary-sort 'lines reverse))
9290
9291 (defun gnus-summary-sort-by-chars (&optional reverse)
9292   "Sort the summary buffer by article length.
9293 Argument REVERSE means reverse order."
9294   (interactive "P")
9295   (gnus-summary-sort 'chars reverse))
9296
9297 (defun gnus-summary-sort (predicate reverse)
9298   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9299   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9300          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9301          (gnus-thread-sort-functions
9302           (if (not reverse)
9303               thread
9304             `(lambda (t1 t2)
9305                (,thread t2 t1))))
9306          (gnus-sort-gathered-threads-function
9307           gnus-thread-sort-functions)
9308          (gnus-article-sort-functions
9309           (if (not reverse)
9310               article
9311             `(lambda (t1 t2)
9312                (,article t2 t1))))
9313          (buffer-read-only)
9314          (gnus-summary-prepare-hook nil))
9315     ;; We do the sorting by regenerating the threads.
9316     (gnus-summary-prepare)
9317     ;; Hide subthreads if needed.
9318     (when (and gnus-show-threads gnus-thread-hide-subtree)
9319       (gnus-summary-hide-all-threads))))
9320
9321 ;; Summary saving commands.
9322
9323 (defun gnus-summary-save-article (&optional n not-saved)
9324   "Save the current article using the default saver function.
9325 If N is a positive number, save the N next articles.
9326 If N is a negative number, save the N previous articles.
9327 If N is nil and any articles have been marked with the process mark,
9328 save those articles instead.
9329 The variable `gnus-default-article-saver' specifies the saver function."
9330   (interactive "P")
9331   (let* ((articles (gnus-summary-work-articles n))
9332          (save-buffer (save-excursion
9333                         (nnheader-set-temp-buffer " *Gnus Save*")))
9334          (num (length articles))
9335          header file)
9336     (dolist (article articles)
9337       (setq header (gnus-summary-article-header article))
9338       (if (not (vectorp header))
9339           ;; This is a pseudo-article.
9340           (if (assq 'name header)
9341               (gnus-copy-file (cdr (assq 'name header)))
9342             (gnus-message 1 "Article %d is unsaveable" article))
9343         ;; This is a real article.
9344         (save-window-excursion
9345           (gnus-summary-select-article t nil nil article))
9346         (save-excursion
9347           (set-buffer save-buffer)
9348           (erase-buffer)
9349           (insert-buffer-substring gnus-original-article-buffer))
9350         (setq file (gnus-article-save save-buffer file num))
9351         (gnus-summary-remove-process-mark article)
9352         (unless not-saved
9353           (gnus-summary-set-saved-mark article))))
9354     (gnus-kill-buffer save-buffer)
9355     (gnus-summary-position-point)
9356     (gnus-set-mode-line 'summary)
9357     n))
9358
9359 (defun gnus-summary-pipe-output (&optional arg)
9360   "Pipe the current article to a subprocess.
9361 If N is a positive number, pipe the N next articles.
9362 If N is a negative number, pipe the N previous articles.
9363 If N is nil and any articles have been marked with the process mark,
9364 pipe those articles instead."
9365   (interactive "P")
9366   (require 'gnus-art)
9367   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9368     (gnus-summary-save-article arg t))
9369   (gnus-configure-windows 'pipe))
9370
9371 (defun gnus-summary-save-article-mail (&optional arg)
9372   "Append the current article to an mail file.
9373 If N is a positive number, save the N next articles.
9374 If N is a negative number, save the N previous articles.
9375 If N is nil and any articles have been marked with the process mark,
9376 save those articles instead."
9377   (interactive "P")
9378   (require 'gnus-art)
9379   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9380     (gnus-summary-save-article arg)))
9381
9382 (defun gnus-summary-save-article-rmail (&optional arg)
9383   "Append the current article to an rmail file.
9384 If N is a positive number, save the N next articles.
9385 If N is a negative number, save the N previous articles.
9386 If N is nil and any articles have been marked with the process mark,
9387 save those articles instead."
9388   (interactive "P")
9389   (require 'gnus-art)
9390   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9391     (gnus-summary-save-article arg)))
9392
9393 (defun gnus-summary-save-article-file (&optional arg)
9394   "Append the current article to a file.
9395 If N is a positive number, save the N next articles.
9396 If N is a negative number, save the N previous articles.
9397 If N is nil and any articles have been marked with the process mark,
9398 save those articles instead."
9399   (interactive "P")
9400   (require 'gnus-art)
9401   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9402     (gnus-summary-save-article arg)))
9403
9404 (defun gnus-summary-write-article-file (&optional arg)
9405   "Write the current article to a file, deleting the previous file.
9406 If N is a positive number, save the N next articles.
9407 If N is a negative number, save the N previous articles.
9408 If N is nil and any articles have been marked with the process mark,
9409 save those articles instead."
9410   (interactive "P")
9411   (require 'gnus-art)
9412   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9413     (gnus-summary-save-article arg)))
9414
9415 (defun gnus-summary-save-article-body-file (&optional arg)
9416   "Append the current article body to a file.
9417 If N is a positive number, save the N next articles.
9418 If N is a negative number, save the N previous articles.
9419 If N is nil and any articles have been marked with the process mark,
9420 save those articles instead."
9421   (interactive "P")
9422   (require 'gnus-art)
9423   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9424     (gnus-summary-save-article arg)))
9425
9426 (defun gnus-summary-pipe-message (program)
9427   "Pipe the current article through PROGRAM."
9428   (interactive "sProgram: ")
9429   (gnus-summary-select-article)
9430   (let ((mail-header-separator ""))
9431     (gnus-eval-in-buffer-window gnus-article-buffer
9432       (save-restriction
9433         (widen)
9434         (let ((start (window-start))
9435               buffer-read-only)
9436           (message-pipe-buffer-body program)
9437           (set-window-start (get-buffer-window (current-buffer)) start))))))
9438
9439 (defun gnus-get-split-value (methods)
9440   "Return a value based on the split METHODS."
9441   (let (split-name method result match)
9442     (when methods
9443       (save-excursion
9444         (set-buffer gnus-original-article-buffer)
9445         (save-restriction
9446           (nnheader-narrow-to-headers)
9447           (while (and methods (not split-name))
9448             (goto-char (point-min))
9449             (setq method (pop methods))
9450             (setq match (car method))
9451             (when (cond
9452                    ((stringp match)
9453                     ;; Regular expression.
9454                     (ignore-errors
9455                       (re-search-forward match nil t)))
9456                    ((gnus-functionp match)
9457                     ;; Function.
9458                     (save-restriction
9459                       (widen)
9460                       (setq result (funcall match gnus-newsgroup-name))))
9461                    ((consp match)
9462                     ;; Form.
9463                     (save-restriction
9464                       (widen)
9465                       (setq result (eval match)))))
9466               (setq split-name (cdr method))
9467               (cond ((stringp result)
9468                      (push (expand-file-name
9469                             result gnus-article-save-directory)
9470                            split-name))
9471                     ((consp result)
9472                      (setq split-name (append result split-name)))))))))
9473     (nreverse split-name)))
9474
9475 (defun gnus-valid-move-group-p (group)
9476   (and (boundp group)
9477        (symbol-name group)
9478        (symbol-value group)
9479        (gnus-get-function (gnus-find-method-for-group
9480                            (symbol-name group)) 'request-accept-article t)))
9481
9482 (defun gnus-read-move-group-name (prompt default articles prefix)
9483   "Read a group name."
9484   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9485          (minibuffer-confirm-incomplete nil) ; XEmacs
9486          (prom
9487           (format "%s %s to:"
9488                   prompt
9489                   (if (> (length articles) 1)
9490                       (format "these %d articles" (length articles))
9491                     "this article")))
9492          (to-newsgroup
9493           (cond
9494            ((null split-name)
9495             (gnus-completing-read default prom
9496                                   gnus-active-hashtb
9497                                   'gnus-valid-move-group-p
9498                                   nil prefix
9499                                   'gnus-group-history))
9500            ((= 1 (length split-name))
9501             (gnus-completing-read (car split-name) prom
9502                                   gnus-active-hashtb
9503                                   'gnus-valid-move-group-p
9504                                   nil nil
9505                                   'gnus-group-history))
9506            (t
9507             (gnus-completing-read nil prom
9508                                   (mapcar (lambda (el) (list el))
9509                                           (nreverse split-name))
9510                                   nil nil nil
9511                                   'gnus-group-history))))
9512          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9513     (when to-newsgroup
9514       (if (or (string= to-newsgroup "")
9515               (string= to-newsgroup prefix))
9516           (setq to-newsgroup default))
9517       (unless to-newsgroup
9518         (error "No group name entered"))
9519       (or (gnus-active to-newsgroup)
9520           (gnus-activate-group to-newsgroup nil nil to-method)
9521           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9522                                      to-newsgroup))
9523               (or (and (gnus-request-create-group to-newsgroup to-method)
9524                        (gnus-activate-group
9525                         to-newsgroup nil nil to-method)
9526                        (gnus-subscribe-group to-newsgroup))
9527                   (error "Couldn't create group %s" to-newsgroup)))
9528           (error "No such group: %s" to-newsgroup)))
9529     to-newsgroup))
9530
9531 (defun gnus-summary-save-parts (type dir n &optional reverse)
9532   "Save parts matching TYPE to DIR.
9533 If REVERSE, save parts that do not match TYPE."
9534   (interactive
9535    (list (read-string "Save parts of type: "
9536                       (or (car gnus-summary-save-parts-type-history)
9537                           gnus-summary-save-parts-default-mime)
9538                       'gnus-summary-save-parts-type-history)
9539          (setq gnus-summary-save-parts-last-directory
9540                (read-file-name "Save to directory: "
9541                                gnus-summary-save-parts-last-directory
9542                                nil t))
9543          current-prefix-arg))
9544   (gnus-summary-iterate n
9545     (let ((gnus-display-mime-function nil)
9546           (gnus-inhibit-treatment t))
9547       (gnus-summary-select-article))
9548     (save-excursion
9549       (set-buffer gnus-article-buffer)
9550       (let ((handles (or gnus-article-mime-handles
9551                          (mm-dissect-buffer) (mm-uu-dissect))))
9552         (when handles
9553           (gnus-summary-save-parts-1 type dir handles reverse)
9554           (unless gnus-article-mime-handles ;; Don't destroy this case.
9555             (mm-destroy-parts handles)))))))
9556
9557 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9558   (if (stringp (car handle))
9559       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9560               (cdr handle))
9561     (when (if reverse
9562               (not (string-match type (mm-handle-media-type handle)))
9563             (string-match type (mm-handle-media-type handle)))
9564       (let ((file (expand-file-name
9565                    (file-name-nondirectory
9566                     (or
9567                      (mail-content-type-get
9568                       (mm-handle-disposition handle) 'filename)
9569                      (concat gnus-newsgroup-name
9570                              "." (number-to-string
9571                                   (cdr gnus-article-current)))))
9572                    dir)))
9573         (unless (file-exists-p file)
9574           (mm-save-part-to-file handle file))))))
9575
9576 ;; Summary extract commands
9577
9578 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9579   (let ((buffer-read-only nil)
9580         (article (gnus-summary-article-number))
9581         after-article b e)
9582     (unless (gnus-summary-goto-subject article)
9583       (error "No such article: %d" article))
9584     (gnus-summary-position-point)
9585     ;; If all commands are to be bunched up on one line, we collect
9586     ;; them here.
9587     (unless gnus-view-pseudos-separately
9588       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9589             files action)
9590         (while ps
9591           (setq action (cdr (assq 'action (car ps))))
9592           (setq files (list (cdr (assq 'name (car ps)))))
9593           (while (and ps (cdr ps)
9594                       (string= (or action "1")
9595                                (or (cdr (assq 'action (cadr ps))) "2")))
9596             (push (cdr (assq 'name (cadr ps))) files)
9597             (setcdr ps (cddr ps)))
9598           (when files
9599             (when (not (string-match "%s" action))
9600               (push " " files))
9601             (push " " files)
9602             (when (assq 'execute (car ps))
9603               (setcdr (assq 'execute (car ps))
9604                       (funcall (if (string-match "%s" action)
9605                                    'format 'concat)
9606                                action
9607                                (mapconcat
9608                                 (lambda (f)
9609                                   (if (equal f " ")
9610                                       f
9611                                     (mm-quote-arg f)))
9612                                 files " ")))))
9613           (setq ps (cdr ps)))))
9614     (if (and gnus-view-pseudos (not not-view))
9615         (while pslist
9616           (when (assq 'execute (car pslist))
9617             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9618                                   (eq gnus-view-pseudos 'not-confirm)))
9619           (setq pslist (cdr pslist)))
9620       (save-excursion
9621         (while pslist
9622           (setq after-article (or (cdr (assq 'article (car pslist)))
9623                                   (gnus-summary-article-number)))
9624           (gnus-summary-goto-subject after-article)
9625           (forward-line 1)
9626           (setq b (point))
9627           (insert "    " (file-name-nondirectory
9628                           (cdr (assq 'name (car pslist))))
9629                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9630           (setq e (point))
9631           (forward-line -1)             ; back to `b'
9632           (gnus-add-text-properties
9633            b (1- e) (list 'gnus-number gnus-reffed-article-number
9634                           gnus-mouse-face-prop gnus-mouse-face))
9635           (gnus-data-enter
9636            after-article gnus-reffed-article-number
9637            gnus-unread-mark b (car pslist) 0 (- e b))
9638           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9639           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9640           (setq pslist (cdr pslist)))))))
9641
9642 (defun gnus-pseudos< (p1 p2)
9643   (let ((c1 (cdr (assq 'action p1)))
9644         (c2 (cdr (assq 'action p2))))
9645     (and c1 c2 (string< c1 c2))))
9646
9647 (defun gnus-request-pseudo-article (props)
9648   (cond ((assq 'execute props)
9649          (gnus-execute-command (cdr (assq 'execute props)))))
9650   (let ((gnus-current-article (gnus-summary-article-number)))
9651     (gnus-run-hooks 'gnus-mark-article-hook)))
9652
9653 (defun gnus-execute-command (command &optional automatic)
9654   (save-excursion
9655     (gnus-article-setup-buffer)
9656     (set-buffer gnus-article-buffer)
9657     (setq buffer-read-only nil)
9658     (let ((command (if automatic command
9659                      (read-string "Command: " (cons command 0)))))
9660       (erase-buffer)
9661       (insert "$ " command "\n\n")
9662       (if gnus-view-pseudo-asynchronously
9663           (start-process "gnus-execute" (current-buffer) shell-file-name
9664                          shell-command-switch command)
9665         (call-process shell-file-name nil t nil
9666                       shell-command-switch command)))))
9667
9668 ;; Summary kill commands.
9669
9670 (defun gnus-summary-edit-global-kill (article)
9671   "Edit the \"global\" kill file."
9672   (interactive (list (gnus-summary-article-number)))
9673   (gnus-group-edit-global-kill article))
9674
9675 (defun gnus-summary-edit-local-kill ()
9676   "Edit a local kill file applied to the current newsgroup."
9677   (interactive)
9678   (setq gnus-current-headers (gnus-summary-article-header))
9679   (gnus-group-edit-local-kill
9680    (gnus-summary-article-number) gnus-newsgroup-name))
9681
9682 ;;; Header reading.
9683
9684 (defun gnus-read-header (id &optional header)
9685   "Read the headers of article ID and enter them into the Gnus system."
9686   (let ((group gnus-newsgroup-name)
9687         (gnus-override-method
9688          (or
9689           gnus-override-method
9690           (and (gnus-news-group-p gnus-newsgroup-name)
9691                (car (gnus-refer-article-methods)))))
9692         where)
9693     ;; First we check to see whether the header in question is already
9694     ;; fetched.
9695     (if (stringp id)
9696         ;; This is a Message-ID.
9697         (setq header (or header (gnus-id-to-header id)))
9698       ;; This is an article number.
9699       (setq header (or header (gnus-summary-article-header id))))
9700     (if (and header
9701              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9702         ;; We have found the header.
9703         header
9704       ;; If this is a sparse article, we have to nix out its
9705       ;; previous entry in the thread hashtb.
9706       (when (and header
9707                  (gnus-summary-article-sparse-p (mail-header-number header)))
9708         (let* ((parent (gnus-parent-id (mail-header-references header)))
9709                (thread (and parent (gnus-id-to-thread parent))))
9710           (when thread
9711             (delq (assq header thread) thread))))
9712       ;; We have to really fetch the header to this article.
9713       (save-excursion
9714         (set-buffer nntp-server-buffer)
9715         (when (setq where (gnus-request-head id group))
9716           (nnheader-fold-continuation-lines)
9717           (goto-char (point-max))
9718           (insert ".\n")
9719           (goto-char (point-min))
9720           (insert "211 ")
9721           (princ (cond
9722                   ((numberp id) id)
9723                   ((cdr where) (cdr where))
9724                   (header (mail-header-number header))
9725                   (t gnus-reffed-article-number))
9726                  (current-buffer))
9727           (insert " Article retrieved.\n"))
9728         (if (or (not where)
9729                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9730             ()                          ; Malformed head.
9731           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9732             (when (and (stringp id)
9733                        (not (string= (gnus-group-real-name group)
9734                                      (car where))))
9735               ;; If we fetched by Message-ID and the article came
9736               ;; from a different group, we fudge some bogus article
9737               ;; numbers for this article.
9738               (mail-header-set-number header gnus-reffed-article-number))
9739             (save-excursion
9740               (set-buffer gnus-summary-buffer)
9741               (decf gnus-reffed-article-number)
9742               (gnus-remove-header (mail-header-number header))
9743               (push header gnus-newsgroup-headers)
9744               (setq gnus-current-headers header)
9745               (push (mail-header-number header) gnus-newsgroup-limit)))
9746           header)))))
9747
9748 (defun gnus-remove-header (number)
9749   "Remove header NUMBER from `gnus-newsgroup-headers'."
9750   (if (and gnus-newsgroup-headers
9751            (= number (mail-header-number (car gnus-newsgroup-headers))))
9752       (pop gnus-newsgroup-headers)
9753     (let ((headers gnus-newsgroup-headers))
9754       (while (and (cdr headers)
9755                   (not (= number (mail-header-number (cadr headers)))))
9756         (pop headers))
9757       (when (cdr headers)
9758         (setcdr headers (cddr headers))))))
9759
9760 ;;;
9761 ;;; summary highlights
9762 ;;;
9763
9764 (defun gnus-highlight-selected-summary ()
9765   "Highlight selected article in summary buffer."
9766   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9767   (when gnus-summary-selected-face
9768     (save-excursion
9769       (let* ((beg (progn (beginning-of-line) (point)))
9770              (end (progn (end-of-line) (point)))
9771              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9772              (from (if (get-text-property beg gnus-mouse-face-prop)
9773                        beg
9774                      (or (next-single-property-change
9775                           beg gnus-mouse-face-prop nil end)
9776                          beg)))
9777              (to
9778               (if (= from end)
9779                   (- from 2)
9780                 (or (next-single-property-change
9781                      from gnus-mouse-face-prop nil end)
9782                     end))))
9783         ;; If no mouse-face prop on line we will have to = from = end,
9784         ;; so we highlight the entire line instead.
9785         (when (= (+ to 2) from)
9786           (setq from beg)
9787           (setq to end))
9788         (if gnus-newsgroup-selected-overlay
9789             ;; Move old overlay.
9790             (gnus-move-overlay
9791              gnus-newsgroup-selected-overlay from to (current-buffer))
9792           ;; Create new overlay.
9793           (gnus-overlay-put
9794            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9795            'face gnus-summary-selected-face))))))
9796
9797 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9798 (defun gnus-summary-highlight-line ()
9799   "Highlight current line according to `gnus-summary-highlight'."
9800   (let* ((list gnus-summary-highlight)
9801          (p (point))
9802          (end (progn (end-of-line) (point)))
9803          ;; now find out where the line starts and leave point there.
9804          (beg (progn (beginning-of-line) (point)))
9805          (article (gnus-summary-article-number))
9806          (score (or (cdr (assq (or article gnus-current-article)
9807                                gnus-newsgroup-scored))
9808                     gnus-summary-default-score 0))
9809          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9810          (inhibit-read-only t))
9811     ;; Eval the cars of the lists until we find a match.
9812     (let ((default gnus-summary-default-score))
9813       (while (and list
9814                   (not (eval (caar list))))
9815         (setq list (cdr list))))
9816     (let ((face (cdar list)))
9817       (unless (eq face (get-text-property beg 'face))
9818         (gnus-put-text-property-excluding-characters-with-faces
9819          beg end 'face
9820          (setq face (if (boundp face) (symbol-value face) face)))
9821         (when gnus-summary-highlight-line-function
9822           (funcall gnus-summary-highlight-line-function article face))))
9823     (goto-char p)))
9824
9825 (defun gnus-update-read-articles (group unread &optional compute)
9826   "Update the list of read articles in GROUP."
9827   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9828          (entry (gnus-gethash group gnus-newsrc-hashtb))
9829          (info (nth 2 entry))
9830          (prev 1)
9831          (unread (sort (copy-sequence unread) '<))
9832          read)
9833     (if (or (not info) (not active))
9834         ;; There is no info on this group if it was, in fact,
9835         ;; killed.  Gnus stores no information on killed groups, so
9836         ;; there's nothing to be done.
9837         ;; One could store the information somewhere temporarily,
9838         ;; perhaps...  Hmmm...
9839         ()
9840       ;; Remove any negative articles numbers.
9841       (while (and unread (< (car unread) 0))
9842         (setq unread (cdr unread)))
9843       ;; Remove any expired article numbers
9844       (while (and unread (< (car unread) (car active)))
9845         (setq unread (cdr unread)))
9846       ;; Compute the ranges of read articles by looking at the list of
9847       ;; unread articles.
9848       (while unread
9849         (when (/= (car unread) prev)
9850           (push (if (= prev (1- (car unread))) prev
9851                   (cons prev (1- (car unread))))
9852                 read))
9853         (setq prev (1+ (car unread)))
9854         (setq unread (cdr unread)))
9855       (when (<= prev (cdr active))
9856         (push (cons prev (cdr active)) read))
9857       (setq read (if (> (length read) 1) (nreverse read) read))
9858       (if compute
9859           read
9860         (save-excursion
9861           (let (setmarkundo)
9862             ;; Propagate the read marks to the backend.
9863             (when (gnus-check-backend-function 'request-set-mark group)
9864               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9865                     (add (gnus-remove-from-range read (gnus-info-read info))))
9866                 (when (or add del)
9867                   (unless (gnus-check-group group)
9868                     (error "Can't open server for %s" group))
9869                   (gnus-request-set-mark
9870                    group (delq nil (list (if add (list add 'add '(read)))
9871                                          (if del (list del 'del '(read))))))
9872                   (setq setmarkundo
9873                         `(gnus-request-set-mark
9874                           ,group
9875                           ',(delq nil (list
9876                                        (if del (list del 'add '(read)))
9877                                        (if add (list add 'del '(read))))))))))
9878             (set-buffer gnus-group-buffer)
9879             (gnus-undo-register
9880               `(progn
9881                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9882                  (gnus-info-set-read ',info ',(gnus-info-read info))
9883                  (gnus-get-unread-articles-in-group ',info
9884                                                     (gnus-active ,group))
9885                  (gnus-group-update-group ,group t)
9886                  ,setmarkundo))))
9887         ;; Enter this list into the group info.
9888         (gnus-info-set-read info read)
9889         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9890         (gnus-get-unread-articles-in-group info (gnus-active group))
9891         t))))
9892
9893 (defun gnus-offer-save-summaries ()
9894   "Offer to save all active summary buffers."
9895   (save-excursion
9896     (let ((buflist (buffer-list))
9897           buffers bufname)
9898       ;; Go through all buffers and find all summaries.
9899       (while buflist
9900         (and (setq bufname (buffer-name (car buflist)))
9901              (string-match "Summary" bufname)
9902              (save-excursion
9903                (set-buffer bufname)
9904                ;; We check that this is, indeed, a summary buffer.
9905                (and (eq major-mode 'gnus-summary-mode)
9906                     ;; Also make sure this isn't bogus.
9907                     gnus-newsgroup-prepared
9908                     ;; Also make sure that this isn't a dead summary buffer.
9909                     (not gnus-dead-summary-mode)))
9910              (push bufname buffers))
9911         (setq buflist (cdr buflist)))
9912       ;; Go through all these summary buffers and offer to save them.
9913       (when buffers
9914         (map-y-or-n-p
9915          "Update summary buffer %s? "
9916          (lambda (buf)
9917            (switch-to-buffer buf)
9918            (gnus-summary-exit))
9919          buffers)))))
9920
9921 (defun gnus-summary-setup-default-charset ()
9922   "Setup newsgroup default charset."
9923   (if (equal gnus-newsgroup-name "nndraft:drafts")
9924       (setq gnus-newsgroup-charset nil)
9925     (let* ((ignored-charsets
9926             (or gnus-newsgroup-ephemeral-ignored-charsets
9927                 (append
9928                  (and gnus-newsgroup-name
9929                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
9930                  gnus-newsgroup-ignored-charsets))))
9931       (setq gnus-newsgroup-charset
9932             (or gnus-newsgroup-ephemeral-charset
9933                 (and gnus-newsgroup-name
9934                      (gnus-parameter-charset gnus-newsgroup-name))
9935                 gnus-default-charset))
9936       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9937            ignored-charsets))))
9938
9939 ;;;
9940 ;;; Mime Commands
9941 ;;;
9942
9943 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9944   "Display the current article buffer fully MIME-buttonized.
9945 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9946 treated as multipart/mixed."
9947   (interactive "P")
9948   (require 'gnus-art)
9949   (let ((gnus-unbuttonized-mime-types nil)
9950         (gnus-mime-display-multipart-as-mixed show-all-parts))
9951     (gnus-summary-show-article)))
9952
9953 (defun gnus-summary-repair-multipart (article)
9954   "Add a Content-Type header to a multipart article without one."
9955   (interactive (list (gnus-summary-article-number)))
9956   (gnus-with-article article
9957     (message-narrow-to-head)
9958     (message-remove-header "Mime-Version")
9959     (goto-char (point-max))
9960     (insert "Mime-Version: 1.0\n")
9961     (widen)
9962     (when (search-forward "\n--" nil t)
9963       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9964         (message-narrow-to-head)
9965         (message-remove-header "Content-Type")
9966         (goto-char (point-max))
9967         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9968                         separator))
9969         (widen))))
9970   (let (gnus-mark-article-hook)
9971     (gnus-summary-select-article t t nil article)))
9972
9973 (defun gnus-summary-toggle-display-buttonized ()
9974   "Toggle the buttonizing of the article buffer."
9975   (interactive)
9976   (require 'gnus-art)
9977   (if (setq gnus-inhibit-mime-unbuttonizing
9978             (not gnus-inhibit-mime-unbuttonizing))
9979       (let ((gnus-unbuttonized-mime-types nil))
9980         (gnus-summary-show-article))
9981     (gnus-summary-show-article)))
9982
9983 ;;;
9984 ;;; Generic summary marking commands
9985 ;;;
9986
9987 (defvar gnus-summary-marking-alist
9988   '((read gnus-del-mark "d")
9989     (unread gnus-unread-mark "u")
9990     (ticked gnus-ticked-mark "!")
9991     (dormant gnus-dormant-mark "?")
9992     (expirable gnus-expirable-mark "e"))
9993   "An alist of names/marks/keystrokes.")
9994
9995 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9996 (defvar gnus-summary-mark-map)
9997
9998 (defun gnus-summary-make-all-marking-commands ()
9999   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10000   (dolist (elem gnus-summary-marking-alist)
10001     (apply 'gnus-summary-make-marking-command elem)))
10002
10003 (defun gnus-summary-make-marking-command (name mark keystroke)
10004   (let ((map (make-sparse-keymap)))
10005     (define-key gnus-summary-generic-mark-map keystroke map)
10006     (dolist (lway `((next "next" next nil "n")
10007                     (next-unread "next unread" next t "N")
10008                     (prev "previous" prev nil "p")
10009                     (prev-unread "previous unread" prev t "P")
10010                     (nomove "" nil nil ,keystroke)))
10011       (let ((func (gnus-summary-make-marking-command-1
10012                    mark (car lway) lway name)))
10013         (setq func (eval func))
10014         (define-key map (nth 4 lway) func)))))
10015
10016 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10017   `(defun ,(intern
10018             (format "gnus-summary-put-mark-as-%s%s"
10019                     name (if (eq way 'nomove)
10020                              ""
10021                            (concat "-" (symbol-name way)))))
10022      (n)
10023      ,(format
10024        "Mark the current article as %s%s.
10025 If N, the prefix, then repeat N times.
10026 If N is negative, move in reverse order.
10027 The difference between N and the actual number of articles marked is
10028 returned."
10029        name (cadr lway))
10030      (interactive "p")
10031      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10032
10033 (defun gnus-summary-generic-mark (n mark move unread)
10034   "Mark N articles with MARK."
10035   (unless (eq major-mode 'gnus-summary-mode)
10036     (error "This command can only be used in the summary buffer"))
10037   (gnus-summary-show-thread)
10038   (let ((nummove
10039          (cond
10040           ((eq move 'next) 1)
10041           ((eq move 'prev) -1)
10042           (t 0))))
10043     (if (zerop nummove)
10044         (setq n 1)
10045       (when (< n 0)
10046         (setq n (abs n)
10047               nummove (* -1 nummove))))
10048     (while (and (> n 0)
10049                 (gnus-summary-mark-article nil mark)
10050                 (zerop (gnus-summary-next-subject nummove unread t)))
10051       (setq n (1- n)))
10052     (when (/= 0 n)
10053       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10054     (gnus-summary-recenter)
10055     (gnus-summary-position-point)
10056     (gnus-set-mode-line 'summary)
10057     n))
10058
10059 (gnus-summary-make-all-marking-commands)
10060
10061 (gnus-ems-redefine)
10062
10063 (provide 'gnus-sum)
10064
10065 (run-hooks 'gnus-sum-load-hook)
10066
10067 ;;; gnus-sum.el ends here