2000-12-21 09: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
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-ancient-mark ?O
434   "*Mark used for ancient articles."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-sparse-mark ?Q
439   "*Mark used for sparsely reffed articles."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-canceled-mark ?G
444   "*Mark used for canceled articles."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-duplicate-mark ?M
449   "*Mark used for duplicate articles."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-undownloaded-mark ?@
454   "*Mark used for articles that weren't downloaded."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-downloadable-mark ?%
459   "*Mark used for articles that are to be downloaded."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-unsendable-mark ?=
464   "*Mark used for articles that won't be sent."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-score-over-mark ?+
469   "*Score mark used for articles with high scores."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-score-below-mark ?-
474   "*Score mark used for articles with low scores."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-empty-thread-mark ?  ;Whitespace
479   "*There is no thread under the article."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-not-empty-thread-mark ?=
484   "*There is a thread under the article."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-view-pseudo-asynchronously nil
489   "*If non-nil, Gnus will view pseudo-articles asynchronously."
490   :group 'gnus-extract-view
491   :type 'boolean)
492
493 (defcustom gnus-auto-expirable-marks
494   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
495         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
496         gnus-souped-mark gnus-duplicate-mark)
497   "*The list of marks converted into expiration if a group is auto-expirable."
498   :version "21.1"
499   :group 'gnus-summary
500   :type '(repeat character))
501
502 (defcustom gnus-inhibit-user-auto-expire t
503   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
504   :version "21.1"
505   :group 'gnus-summary
506   :type 'boolean)
507
508 (defcustom gnus-view-pseudos nil
509   "*If `automatic', pseudo-articles will be viewed automatically.
510 If `not-confirm', pseudos will be viewed automatically, and the user
511 will not be asked to confirm the command."
512   :group 'gnus-extract-view
513   :type '(choice (const :tag "off" nil)
514                  (const automatic)
515                  (const not-confirm)))
516
517 (defcustom gnus-view-pseudos-separately t
518   "*If non-nil, one pseudo-article will be created for each file to be viewed.
519 If nil, all files that use the same viewing command will be given as a
520 list of parameters to that command."
521   :group 'gnus-extract-view
522   :type 'boolean)
523
524 (defcustom gnus-insert-pseudo-articles t
525   "*If non-nil, insert pseudo-articles when decoding articles."
526   :group 'gnus-extract-view
527   :type 'boolean)
528
529 (defcustom gnus-summary-dummy-line-format
530   "  %(:                          :%) %S\n"
531   "*The format specification for the dummy roots in the summary buffer.
532 It works along the same lines as a normal formatting string,
533 with some simple extensions.
534
535 %S  The subject"
536   :group 'gnus-threading
537   :type 'string)
538
539 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
540   "*The format specification for the summary mode line.
541 It works along the same lines as a normal formatting string,
542 with some simple extensions:
543
544 %G  Group name
545 %p  Unprefixed group name
546 %A  Current article number
547 %z  Current article score
548 %V  Gnus version
549 %U  Number of unread articles in the group
550 %e  Number of unselected articles in the group
551 %Z  A string with unread/unselected article counts
552 %g  Shortish group name
553 %S  Subject of the current article
554 %u  User-defined spec
555 %s  Current score file name
556 %d  Number of dormant articles
557 %r  Number of articles that have been marked as read in this session
558 %E  Number of articles expunged by the score files"
559   :group 'gnus-summary-format
560   :type 'string)
561
562 (defcustom gnus-list-identifiers nil
563   "Regexp that matches list identifiers to be removed from subject.
564 This can also be a list of regexps."
565   :version "21.1"
566   :group 'gnus-summary-format
567   :group 'gnus-article-hiding
568   :type '(choice (const :tag "none" nil)
569                  (regexp :value ".*")
570                  (repeat :value (".*") regexp)))
571
572 (defcustom gnus-summary-mark-below 0
573   "*Mark all articles with a score below this variable as read.
574 This variable is local to each summary buffer and usually set by the
575 score file."
576   :group 'gnus-score-default
577   :type 'integer)
578
579 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
580   "*List of functions used for sorting articles in the summary buffer.
581 This variable is only used when not using a threaded display."
582   :group 'gnus-summary-sort
583   :type '(repeat (choice (function-item gnus-article-sort-by-number)
584                          (function-item gnus-article-sort-by-author)
585                          (function-item gnus-article-sort-by-subject)
586                          (function-item gnus-article-sort-by-date)
587                          (function-item gnus-article-sort-by-score)
588                          (function :tag "other"))))
589
590 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
591   "*List of functions used for sorting threads in the summary buffer.
592 By default, threads are sorted by article number.
593
594 Each function takes two threads and return non-nil if the first thread
595 should be sorted before the other.  If you use more than one function,
596 the primary sort function should be the last.  You should probably
597 always include `gnus-thread-sort-by-number' in the list of sorting
598 functions -- preferably first.
599
600 Ready-made functions include `gnus-thread-sort-by-number',
601 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
602 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
603 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
604   :group 'gnus-summary-sort
605   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
606                          (function-item gnus-thread-sort-by-author)
607                          (function-item gnus-thread-sort-by-subject)
608                          (function-item gnus-thread-sort-by-date)
609                          (function-item gnus-thread-sort-by-score)
610                          (function-item gnus-thread-sort-by-total-score)
611                          (function :tag "other"))))
612
613 (defcustom gnus-thread-score-function '+
614   "*Function used for calculating the total score of a thread.
615
616 The function is called with the scores of the article and each
617 subthread and should then return the score of the thread.
618
619 Some functions you can use are `+', `max', or `min'."
620   :group 'gnus-summary-sort
621   :type 'function)
622
623 (defcustom gnus-summary-expunge-below nil
624   "All articles that have a score less than this variable will be expunged.
625 This variable is local to the summary buffers."
626   :group 'gnus-score-default
627   :type '(choice (const :tag "off" nil)
628                  integer))
629
630 (defcustom gnus-thread-expunge-below nil
631   "All threads that have a total score less than this variable will be expunged.
632 See `gnus-thread-score-function' for en explanation of what a
633 \"thread score\" is.
634
635 This variable is local to the summary buffers."
636   :group 'gnus-threading
637   :group 'gnus-score-default
638   :type '(choice (const :tag "off" nil)
639                  integer))
640
641 (defcustom gnus-summary-mode-hook nil
642   "*A hook for Gnus summary mode.
643 This hook is run before any variables are set in the summary buffer."
644   :options '(turn-on-gnus-mailing-list-mode)
645   :group 'gnus-summary-various
646   :type 'hook)
647
648 (defcustom gnus-summary-menu-hook nil
649   "*Hook run after the creation of the summary mode menu."
650   :group 'gnus-summary-visual
651   :type 'hook)
652
653 (defcustom gnus-summary-exit-hook nil
654   "*A hook called on exit from the summary buffer.
655 It will be called with point in the group buffer."
656   :group 'gnus-summary-exit
657   :type 'hook)
658
659 (defcustom gnus-summary-prepare-hook nil
660   "*A hook called after the summary buffer has been generated.
661 If you want to modify the summary buffer, you can use this hook."
662   :group 'gnus-summary-various
663   :type 'hook)
664
665 (defcustom gnus-summary-prepared-hook nil
666   "*A hook called as the last thing after the summary buffer has been generated."
667   :group 'gnus-summary-various
668   :type 'hook)
669
670 (defcustom gnus-summary-generate-hook nil
671   "*A hook run just before generating the summary buffer.
672 This hook is commonly used to customize threading variables and the
673 like."
674   :group 'gnus-summary-various
675   :type 'hook)
676
677 (defcustom gnus-select-group-hook nil
678   "*A hook called when a newsgroup is selected.
679
680 If you'd like to simplify subjects like the
681 `gnus-summary-next-same-subject' command does, you can use the
682 following hook:
683
684  (setq gnus-select-group-hook
685       (list
686         (lambda ()
687           (mapcar (lambda (header)
688                      (mail-header-set-subject
689                       header
690                       (gnus-simplify-subject
691                        (mail-header-subject header) 're-only)))
692                   gnus-newsgroup-headers))))"
693   :group 'gnus-group-select
694   :type 'hook)
695
696 (defcustom gnus-select-article-hook nil
697   "*A hook called when an article is selected."
698   :group 'gnus-summary-choose
699   :type 'hook)
700
701 (defcustom gnus-visual-mark-article-hook
702   (list 'gnus-highlight-selected-summary)
703   "*Hook run after selecting an article in the summary buffer.
704 It is meant to be used for highlighting the article in some way.  It
705 is not run if `gnus-visual' is nil."
706   :group 'gnus-summary-visual
707   :type 'hook)
708
709 (defcustom gnus-parse-headers-hook nil
710   "*A hook called before parsing the headers."
711   :group 'gnus-various
712   :type 'hook)
713
714 (defcustom gnus-exit-group-hook nil
715   "*A hook called when exiting summary mode.
716 This hook is not called from the non-updating exit commands like `Q'."
717   :group 'gnus-various
718   :type 'hook)
719
720 (defcustom gnus-summary-update-hook
721   (list 'gnus-summary-highlight-line)
722   "*A hook called when a summary line is changed.
723 The hook will not be called if `gnus-visual' is nil.
724
725 The default function `gnus-summary-highlight-line' will
726 highlight the line according to the `gnus-summary-highlight'
727 variable."
728   :group 'gnus-summary-visual
729   :type 'hook)
730
731 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
732   "*A hook called when an article is selected for the first time.
733 The hook is intended to mark an article as read (or unread)
734 automatically when it is selected."
735   :group 'gnus-summary-choose
736   :type 'hook)
737
738 (defcustom gnus-group-no-more-groups-hook nil
739   "*A hook run when returning to group mode having no more (unread) groups."
740   :group 'gnus-group-select
741   :type 'hook)
742
743 (defcustom gnus-ps-print-hook nil
744   "*A hook run before ps-printing something from Gnus."
745   :group 'gnus-summary
746   :type 'hook)
747
748 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
749   "Face used for highlighting the current article in the summary buffer."
750   :group 'gnus-summary-visual
751   :type 'face)
752
753 (defcustom gnus-summary-highlight
754   '(((= mark gnus-canceled-mark)
755      . gnus-summary-cancelled-face)
756     ((and (> score default)
757           (or (= mark gnus-dormant-mark)
758               (= mark gnus-ticked-mark)))
759      . gnus-summary-high-ticked-face)
760     ((and (< score default)
761           (or (= mark gnus-dormant-mark)
762               (= mark gnus-ticked-mark)))
763      . gnus-summary-low-ticked-face)
764     ((or (= mark gnus-dormant-mark)
765          (= mark gnus-ticked-mark))
766      . gnus-summary-normal-ticked-face)
767     ((and (> score default) (= mark gnus-ancient-mark))
768      . gnus-summary-high-ancient-face)
769     ((and (< score default) (= mark gnus-ancient-mark))
770      . gnus-summary-low-ancient-face)
771     ((= mark gnus-ancient-mark)
772      . gnus-summary-normal-ancient-face)
773     ((and (> score default) (= mark gnus-unread-mark))
774      . gnus-summary-high-unread-face)
775     ((and (< score default) (= mark gnus-unread-mark))
776      . gnus-summary-low-unread-face)
777     ((= mark gnus-unread-mark)
778      . gnus-summary-normal-unread-face)
779     ((and (> score default) (memq mark (list gnus-downloadable-mark
780                                              gnus-undownloaded-mark)))
781      . gnus-summary-high-unread-face)
782     ((and (< score default) (memq mark (list gnus-downloadable-mark
783                                              gnus-undownloaded-mark)))
784      . gnus-summary-low-unread-face)
785     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
786      . gnus-summary-normal-unread-face)
787     ((> score default)
788      . gnus-summary-high-read-face)
789     ((< score default)
790      . gnus-summary-low-read-face)
791     (t
792      . gnus-summary-normal-read-face))
793   "*Controls the highlighting of summary buffer lines.
794
795 A list of (FORM . FACE) pairs.  When deciding how a a particular
796 summary line should be displayed, each form is evaluated.  The content
797 of the face field after the first true form is used.  You can change
798 how those summary lines are displayed, by editing the face field.
799
800 You can use the following variables in the FORM field.
801
802 score:   The articles score
803 default: The default article score.
804 below:   The score below which articles are automatically marked as read.
805 mark:    The articles mark."
806   :group 'gnus-summary-visual
807   :type '(repeat (cons (sexp :tag "Form" nil)
808                        face)))
809
810 (defcustom gnus-alter-header-function nil
811   "Function called to allow alteration of article header structures.
812 The function is called with one parameter, the article header vector,
813 which it may alter in any way.")
814
815 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
816   "Variable that says which function should be used to decode a string with encoded words.")
817
818 (defcustom gnus-extra-headers nil
819   "*Extra headers to parse."
820   :version "21.1"
821   :group 'gnus-summary
822   :type '(repeat symbol))
823
824 (defcustom gnus-ignored-from-addresses
825   (and user-mail-address (regexp-quote user-mail-address))
826   "*Regexp of From headers that may be suppressed in favor of To headers."
827   :version "21.1"
828   :group 'gnus-summary
829   :type 'regexp)
830
831 (defcustom gnus-group-charset-alist
832   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
833     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
834     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
835     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
836     ("^relcom\\>" koi8-r)
837     ("^fido7\\>" koi8-r)
838     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
839     ("^israel\\>" iso-8859-1)
840     ("^han\\>" euc-kr)
841     ("^alt.chinese.text.big5\\>" chinese-big5)
842     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
843     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
844     (".*" iso-8859-1))
845   "Alist of regexps (to match group names) and default charsets to be used when reading."
846   :type '(repeat (list (regexp :tag "Group")
847                        (symbol :tag "Charset")))
848   :group 'gnus-charset)
849
850 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
851   "List of charsets that should be ignored.
852 When these charsets are used in the \"charset\" parameter, the
853 default charset will be used instead."
854   :version "21.1"
855   :type '(repeat symbol)
856   :group 'gnus-charset)
857
858 (defcustom gnus-group-ignored-charsets-alist
859   '(("alt\\.chinese\\.text" iso-8859-1))
860   "Alist of regexps (to match group names) and charsets that should be ignored.
861 When these charsets are used in the \"charset\" parameter, the
862 default charset will be used instead."
863   :type '(repeat (cons (regexp :tag "Group")
864                        (repeat symbol)))
865   :group 'gnus-charset)
866
867 (defcustom gnus-group-highlight-words-alist nil
868   "Alist of group regexps and highlight regexps.
869 This variable uses the same syntax as `gnus-emphasis-alist'."
870   :version "21.1"
871   :type '(repeat (cons (regexp :tag "Group")
872                        (repeat (list (regexp :tag "Highlight regexp")
873                                      (number :tag "Group for entire word" 0)
874                                      (number :tag "Group for displayed part" 0)
875                                      (symbol :tag "Face"
876                                              gnus-emphasis-highlight-words)))))
877   :group 'gnus-summary-visual)
878
879 (defcustom gnus-summary-show-article-charset-alist
880   nil
881   "Alist of number and charset.
882 The article will be shown with the charset corresponding to the
883 numbered argument.
884 For example: ((1 . cn-gb-2312) (2 . big5))."
885   :version "21.1"
886   :type '(repeat (cons (number :tag "Argument" 1)
887                        (symbol :tag "Charset")))
888   :group 'gnus-charset)
889
890 (defcustom gnus-preserve-marks t
891   "Whether marks are preserved when moving, copying and respooling messages."
892   :version "21.1"
893   :type 'boolean
894   :group 'gnus-summary-marks)
895
896 (defcustom gnus-alter-articles-to-read-function nil
897   "Function to be called to alter the list of articles to be selected."
898   :type 'function
899   :group 'gnus-summary)
900
901 (defcustom gnus-orphan-score nil
902   "*All orphans get this score added.  Set in the score file."
903   :group 'gnus-score-default
904   :type '(choice (const nil)
905                  integer))
906
907 (defcustom gnus-summary-save-parts-default-mime "image/.*"
908   "*A regexp to match MIME parts when saving multiple parts of a message
909 with gnus-summary-save-parts (X m). This regexp will be used by default
910 when prompting the user for which type of files to save."
911   :group 'gnus-summary
912   :type 'regexp)
913
914
915 (defcustom gnus-summary-save-parts-default-mime "image/.*"
916   "*A regexp to match MIME parts when saving multiple parts of a message
917 with gnus-summary-save-parts (X m). This regexp will be used by default
918 when prompting the user for which type of files to save."
919   :group 'gnus-summary
920   :type 'regexp)
921
922
923 ;;; Internal variables
924
925 (defvar gnus-article-mime-handles nil)
926 (defvar gnus-article-decoded-p nil)
927 (defvar gnus-article-charset nil)
928 (defvar gnus-article-ignored-charsets nil)
929 (defvar gnus-scores-exclude-files nil)
930 (defvar gnus-page-broken nil)
931 (defvar gnus-inhibit-mime-unbuttonizing nil)
932
933 (defvar gnus-original-article nil)
934 (defvar gnus-article-internal-prepare-hook nil)
935 (defvar gnus-newsgroup-process-stack nil)
936
937 (defvar gnus-thread-indent-array nil)
938 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
939 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
940   "Function called to sort the articles within a thread after it has been gathered together.")
941
942 (defvar gnus-summary-save-parts-type-history nil)
943 (defvar gnus-summary-save-parts-last-directory nil)
944
945 (defvar gnus-summary-save-parts-type-history nil)
946 (defvar gnus-summary-save-parts-last-directory nil)
947
948 ;; Avoid highlighting in kill files.
949 (defvar gnus-summary-inhibit-highlight nil)
950 (defvar gnus-newsgroup-selected-overlay nil)
951 (defvar gnus-inhibit-limiting nil)
952 (defvar gnus-newsgroup-adaptive-score-file nil)
953 (defvar gnus-current-score-file nil)
954 (defvar gnus-current-move-group nil)
955 (defvar gnus-current-copy-group nil)
956 (defvar gnus-current-crosspost-group nil)
957
958 (defvar gnus-newsgroup-dependencies nil)
959 (defvar gnus-newsgroup-adaptive nil)
960 (defvar gnus-summary-display-article-function nil)
961 (defvar gnus-summary-highlight-line-function nil
962   "Function called after highlighting a summary line.")
963
964 (defvar gnus-summary-line-format-alist
965   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
966     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
967     (?s gnus-tmp-subject-or-nil ?s)
968     (?n gnus-tmp-name ?s)
969     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
970         ?s)
971     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
972             gnus-tmp-from) ?s)
973     (?F gnus-tmp-from ?s)
974     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
975     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
976     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
977     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
978     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
979     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
980     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
981     (?L gnus-tmp-lines ?d)
982     (?I gnus-tmp-indentation ?s)
983     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
984     (?R gnus-tmp-replied ?c)
985     (?\[ gnus-tmp-opening-bracket ?c)
986     (?\] gnus-tmp-closing-bracket ?c)
987     (?\> (make-string gnus-tmp-level ? ) ?s)
988     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
989     (?i gnus-tmp-score ?d)
990     (?z gnus-tmp-score-char ?c)
991     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
992     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
993     (?U gnus-tmp-unread ?c)
994     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
995     (?t (gnus-summary-number-of-articles-in-thread
996          (and (boundp 'thread) (car thread)) gnus-tmp-level)
997         ?d)
998     (?e (gnus-summary-number-of-articles-in-thread
999          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1000         ?c)
1001     (?u gnus-tmp-user-defined ?s)
1002     (?P (gnus-pick-line-number) ?d))
1003   "An alist of format specifications that can appear in summary lines.
1004 These are paired with what variables they correspond with, along with
1005 the type of the variable (string, integer, character, etc).")
1006
1007 (defvar gnus-summary-dummy-line-format-alist
1008   `((?S gnus-tmp-subject ?s)
1009     (?N gnus-tmp-number ?d)
1010     (?u gnus-tmp-user-defined ?s)))
1011
1012 (defvar gnus-summary-mode-line-format-alist
1013   `((?G gnus-tmp-group-name ?s)
1014     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1015     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1016     (?A gnus-tmp-article-number ?d)
1017     (?Z gnus-tmp-unread-and-unselected ?s)
1018     (?V gnus-version ?s)
1019     (?U gnus-tmp-unread-and-unticked ?d)
1020     (?S gnus-tmp-subject ?s)
1021     (?e gnus-tmp-unselected ?d)
1022     (?u gnus-tmp-user-defined ?s)
1023     (?d (length gnus-newsgroup-dormant) ?d)
1024     (?t (length gnus-newsgroup-marked) ?d)
1025     (?r (length gnus-newsgroup-reads) ?d)
1026     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1027     (?E gnus-newsgroup-expunged-tally ?d)
1028     (?s (gnus-current-score-file-nondirectory) ?s)))
1029
1030 (defvar gnus-last-search-regexp nil
1031   "Default regexp for article search command.")
1032
1033 (defvar gnus-last-shell-command nil
1034   "Default shell command on article.")
1035
1036 (defvar gnus-newsgroup-begin nil)
1037 (defvar gnus-newsgroup-end nil)
1038 (defvar gnus-newsgroup-last-rmail nil)
1039 (defvar gnus-newsgroup-last-mail nil)
1040 (defvar gnus-newsgroup-last-folder nil)
1041 (defvar gnus-newsgroup-last-file nil)
1042 (defvar gnus-newsgroup-auto-expire nil)
1043 (defvar gnus-newsgroup-active nil)
1044
1045 (defvar gnus-newsgroup-data nil)
1046 (defvar gnus-newsgroup-data-reverse nil)
1047 (defvar gnus-newsgroup-limit nil)
1048 (defvar gnus-newsgroup-limits nil)
1049
1050 (defvar gnus-newsgroup-unreads nil
1051   "List of unread articles in the current newsgroup.")
1052
1053 (defvar gnus-newsgroup-unselected nil
1054   "List of unselected unread articles in the current newsgroup.")
1055
1056 (defvar gnus-newsgroup-reads nil
1057   "Alist of read articles and article marks in the current newsgroup.")
1058
1059 (defvar gnus-newsgroup-expunged-tally nil)
1060
1061 (defvar gnus-newsgroup-marked nil
1062   "List of ticked articles in the current newsgroup (a subset of unread art).")
1063
1064 (defvar gnus-newsgroup-killed nil
1065   "List of ranges of articles that have been through the scoring process.")
1066
1067 (defvar gnus-newsgroup-cached nil
1068   "List of articles that come from the article cache.")
1069
1070 (defvar gnus-newsgroup-saved nil
1071   "List of articles that have been saved.")
1072
1073 (defvar gnus-newsgroup-kill-headers nil)
1074
1075 (defvar gnus-newsgroup-replied nil
1076   "List of articles that have been replied to in the current newsgroup.")
1077
1078 (defvar gnus-newsgroup-expirable nil
1079   "List of articles in the current newsgroup that can be expired.")
1080
1081 (defvar gnus-newsgroup-processable nil
1082   "List of articles in the current newsgroup that can be processed.")
1083
1084 (defvar gnus-newsgroup-downloadable nil
1085   "List of articles in the current newsgroup that can be processed.")
1086
1087 (defvar gnus-newsgroup-undownloaded nil
1088   "List of articles in the current newsgroup that haven't been downloaded..")
1089
1090 (defvar gnus-newsgroup-unsendable nil
1091   "List of articles in the current newsgroup that won't be sent.")
1092
1093 (defvar gnus-newsgroup-bookmarks nil
1094   "List of articles in the current newsgroup that have bookmarks.")
1095
1096 (defvar gnus-newsgroup-dormant nil
1097   "List of dormant articles in the current newsgroup.")
1098
1099 (defvar gnus-newsgroup-scored nil
1100   "List of scored articles in the current newsgroup.")
1101
1102 (defvar gnus-newsgroup-headers nil
1103   "List of article headers in the current newsgroup.")
1104
1105 (defvar gnus-newsgroup-threads nil)
1106
1107 (defvar gnus-newsgroup-prepared nil
1108   "Whether the current group has been prepared properly.")
1109
1110 (defvar gnus-newsgroup-ancient nil
1111   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1112
1113 (defvar gnus-newsgroup-sparse nil)
1114
1115 (defvar gnus-current-article nil)
1116 (defvar gnus-article-current nil)
1117 (defvar gnus-current-headers nil)
1118 (defvar gnus-have-all-headers nil)
1119 (defvar gnus-last-article nil)
1120 (defvar gnus-newsgroup-history nil)
1121 (defvar gnus-newsgroup-charset nil)
1122 (defvar gnus-newsgroup-ephemeral-charset nil)
1123 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1124
1125 (defvar gnus-article-before-search nil)
1126
1127 (defconst gnus-summary-local-variables
1128   '(gnus-newsgroup-name
1129     gnus-newsgroup-begin gnus-newsgroup-end
1130     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1131     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1132     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1133     gnus-newsgroup-unselected gnus-newsgroup-marked
1134     gnus-newsgroup-reads gnus-newsgroup-saved
1135     gnus-newsgroup-replied gnus-newsgroup-expirable
1136     gnus-newsgroup-processable gnus-newsgroup-killed
1137     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1138     gnus-newsgroup-unsendable
1139     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1140     gnus-newsgroup-headers gnus-newsgroup-threads
1141     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1142     gnus-current-article gnus-current-headers gnus-have-all-headers
1143     gnus-last-article gnus-article-internal-prepare-hook
1144     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1145     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1146     gnus-thread-expunge-below
1147     gnus-score-alist gnus-current-score-file
1148     (gnus-summary-expunge-below . global)
1149     (gnus-summary-mark-below . global)
1150     (gnus-orphan-score . global)
1151     gnus-newsgroup-active gnus-scores-exclude-files
1152     gnus-newsgroup-history gnus-newsgroup-ancient
1153     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1154     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1155     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1156     (gnus-newsgroup-expunged-tally . 0)
1157     gnus-cache-removable-articles gnus-newsgroup-cached
1158     gnus-newsgroup-data gnus-newsgroup-data-reverse
1159     gnus-newsgroup-limit gnus-newsgroup-limits
1160     gnus-newsgroup-charset)
1161   "Variables that are buffer-local to the summary buffers.")
1162
1163 (defvar gnus-newsgroup-variables nil
1164   "Variables that have separate values in the newsgroups.")
1165
1166 ;; Byte-compiler warning.
1167 (eval-when-compile (defvar gnus-article-mode-map))
1168
1169 ;; MIME stuff.
1170
1171 (defvar gnus-decode-encoded-word-methods
1172   '(mail-decode-encoded-word-string)
1173   "List of methods used to decode encoded words.
1174
1175 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1176 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1177 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1178 whose names match REGEXP.
1179
1180 For example:
1181 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1182  mail-decode-encoded-word-string
1183  (\"chinese\" . rfc1843-decode-string))")
1184
1185 (defvar gnus-decode-encoded-word-methods-cache nil)
1186
1187 (defun gnus-multi-decode-encoded-word-string (string)
1188   "Apply the functions from `gnus-encoded-word-methods' that match."
1189   (unless (and gnus-decode-encoded-word-methods-cache
1190                (eq gnus-newsgroup-name
1191                    (car gnus-decode-encoded-word-methods-cache)))
1192     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1193     (mapcar (lambda (x)
1194               (if (symbolp x)
1195                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1196                 (if (and gnus-newsgroup-name
1197                          (string-match (car x) gnus-newsgroup-name))
1198                     (nconc gnus-decode-encoded-word-methods-cache
1199                            (list (cdr x))))))
1200           gnus-decode-encoded-word-methods))
1201   (let ((xlist gnus-decode-encoded-word-methods-cache))
1202     (pop xlist)
1203     (while xlist
1204       (setq string (funcall (pop xlist) string))))
1205   string)
1206
1207 ;; Subject simplification.
1208
1209 (defun gnus-simplify-whitespace (str)
1210   "Remove excessive whitespace from STR."
1211   (let ((mystr str))
1212     ;; Multiple spaces.
1213     (while (string-match "[ \t][ \t]+" mystr)
1214       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1215                           " "
1216                           (substring mystr (match-end 0)))))
1217     ;; Leading spaces.
1218     (when (string-match "^[ \t]+" mystr)
1219       (setq mystr (substring mystr (match-end 0))))
1220     ;; Trailing spaces.
1221     (when (string-match "[ \t]+$" mystr)
1222       (setq mystr (substring mystr 0 (match-beginning 0))))
1223     mystr))
1224
1225 (defsubst gnus-simplify-subject-re (subject)
1226   "Remove \"Re:\" from subject lines."
1227   (if (string-match "^[Rr][Ee]: *" subject)
1228       (substring subject (match-end 0))
1229     subject))
1230
1231 (defun gnus-simplify-subject (subject &optional re-only)
1232   "Remove `Re:' and words in parentheses.
1233 If RE-ONLY is non-nil, strip leading `Re:'s only."
1234   (let ((case-fold-search t))           ;Ignore case.
1235     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1236     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1237       (setq subject (substring subject (match-end 0))))
1238     ;; Remove uninteresting prefixes.
1239     (when (and (not re-only)
1240                gnus-simplify-ignored-prefixes
1241                (string-match gnus-simplify-ignored-prefixes subject))
1242       (setq subject (substring subject (match-end 0))))
1243     ;; Remove words in parentheses from end.
1244     (unless re-only
1245       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1246         (setq subject (substring subject 0 (match-beginning 0)))))
1247     ;; Return subject string.
1248     subject))
1249
1250 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1251 ;; all whitespace.
1252 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1253   (goto-char (point-min))
1254   (while (re-search-forward regexp nil t)
1255     (replace-match (or newtext ""))))
1256
1257 (defun gnus-simplify-buffer-fuzzy ()
1258   "Simplify string in the buffer fuzzily.
1259 The string in the accessible portion of the current buffer is simplified.
1260 It is assumed to be a single-line subject.
1261 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1262 matter is removed.  Additional things can be deleted by setting
1263 `gnus-simplify-subject-fuzzy-regexp'."
1264   (let ((case-fold-search t)
1265         (modified-tick))
1266     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1267
1268     (while (not (eq modified-tick (buffer-modified-tick)))
1269       (setq modified-tick (buffer-modified-tick))
1270       (cond
1271        ((listp gnus-simplify-subject-fuzzy-regexp)
1272         (mapcar 'gnus-simplify-buffer-fuzzy-step
1273                 gnus-simplify-subject-fuzzy-regexp))
1274        (gnus-simplify-subject-fuzzy-regexp
1275         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1276       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1277       (gnus-simplify-buffer-fuzzy-step
1278        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1279       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1280
1281     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1282     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1283     (gnus-simplify-buffer-fuzzy-step " $")
1284     (gnus-simplify-buffer-fuzzy-step "^ +")))
1285
1286 (defun gnus-simplify-subject-fuzzy (subject)
1287   "Simplify a subject string fuzzily.
1288 See `gnus-simplify-buffer-fuzzy' for details."
1289   (save-excursion
1290     (gnus-set-work-buffer)
1291     (let ((case-fold-search t))
1292       ;; Remove uninteresting prefixes.
1293       (when (and gnus-simplify-ignored-prefixes
1294                  (string-match gnus-simplify-ignored-prefixes subject))
1295         (setq subject (substring subject (match-end 0))))
1296       (insert subject)
1297       (inline (gnus-simplify-buffer-fuzzy))
1298       (buffer-string))))
1299
1300 (defsubst gnus-simplify-subject-fully (subject)
1301   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1302   (cond
1303    (gnus-simplify-subject-functions
1304     (gnus-map-function gnus-simplify-subject-functions subject))
1305    ((null gnus-summary-gather-subject-limit)
1306     (gnus-simplify-subject-re subject))
1307    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1308     (gnus-simplify-subject-fuzzy subject))
1309    ((numberp gnus-summary-gather-subject-limit)
1310     (gnus-limit-string (gnus-simplify-subject-re subject)
1311                        gnus-summary-gather-subject-limit))
1312    (t
1313     subject)))
1314
1315 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1316   "Check whether two subjects are equal.
1317 If optional argument simple-first is t, first argument is already
1318 simplified."
1319   (cond
1320    ((null simple-first)
1321     (equal (gnus-simplify-subject-fully s1)
1322            (gnus-simplify-subject-fully s2)))
1323    (t
1324     (equal s1
1325            (gnus-simplify-subject-fully s2)))))
1326
1327 (defun gnus-summary-bubble-group ()
1328   "Increase the score of the current group.
1329 This is a handy function to add to `gnus-summary-exit-hook' to
1330 increase the score of each group you read."
1331   (gnus-group-add-score gnus-newsgroup-name))
1332
1333 \f
1334 ;;;
1335 ;;; Gnus summary mode
1336 ;;;
1337
1338 (put 'gnus-summary-mode 'mode-class 'special)
1339
1340 (when t
1341   ;; Non-orthogonal keys
1342
1343   (gnus-define-keys gnus-summary-mode-map
1344     " " gnus-summary-next-page
1345     "\177" gnus-summary-prev-page
1346     [delete] gnus-summary-prev-page
1347     [backspace] gnus-summary-prev-page
1348     "\r" gnus-summary-scroll-up
1349     "\M-\r" gnus-summary-scroll-down
1350     "n" gnus-summary-next-unread-article
1351     "p" gnus-summary-prev-unread-article
1352     "N" gnus-summary-next-article
1353     "P" gnus-summary-prev-article
1354     "\M-\C-n" gnus-summary-next-same-subject
1355     "\M-\C-p" gnus-summary-prev-same-subject
1356     "\M-n" gnus-summary-next-unread-subject
1357     "\M-p" gnus-summary-prev-unread-subject
1358     "." gnus-summary-first-unread-article
1359     "," gnus-summary-best-unread-article
1360     "\M-s" gnus-summary-search-article-forward
1361     "\M-r" gnus-summary-search-article-backward
1362     "<" gnus-summary-beginning-of-article
1363     ">" gnus-summary-end-of-article
1364     "j" gnus-summary-goto-article
1365     "^" gnus-summary-refer-parent-article
1366     "\M-^" gnus-summary-refer-article
1367     "u" gnus-summary-tick-article-forward
1368     "!" gnus-summary-tick-article-forward
1369     "U" gnus-summary-tick-article-backward
1370     "d" gnus-summary-mark-as-read-forward
1371     "D" gnus-summary-mark-as-read-backward
1372     "E" gnus-summary-mark-as-expirable
1373     "\M-u" gnus-summary-clear-mark-forward
1374     "\M-U" gnus-summary-clear-mark-backward
1375     "k" gnus-summary-kill-same-subject-and-select
1376     "\C-k" gnus-summary-kill-same-subject
1377     "\M-\C-k" gnus-summary-kill-thread
1378     "\M-\C-l" gnus-summary-lower-thread
1379     "e" gnus-summary-edit-article
1380     "#" gnus-summary-mark-as-processable
1381     "\M-#" gnus-summary-unmark-as-processable
1382     "\M-\C-t" gnus-summary-toggle-threads
1383     "\M-\C-s" gnus-summary-show-thread
1384     "\M-\C-h" gnus-summary-hide-thread
1385     "\M-\C-f" gnus-summary-next-thread
1386     "\M-\C-b" gnus-summary-prev-thread
1387     [(meta down)] gnus-summary-next-thread
1388     [(meta up)] gnus-summary-prev-thread
1389     "\M-\C-u" gnus-summary-up-thread
1390     "\M-\C-d" gnus-summary-down-thread
1391     "&" gnus-summary-execute-command
1392     "c" gnus-summary-catchup-and-exit
1393     "\C-w" gnus-summary-mark-region-as-read
1394     "\C-t" gnus-summary-toggle-truncation
1395     "?" gnus-summary-mark-as-dormant
1396     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1397     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1398     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1399     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1400     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1401     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1402     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1403     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1404     "=" gnus-summary-expand-window
1405     "\C-x\C-s" gnus-summary-reselect-current-group
1406     "\M-g" gnus-summary-rescan-group
1407     "w" gnus-summary-stop-page-breaking
1408     "\C-c\C-r" gnus-summary-caesar-message
1409     "f" gnus-summary-followup
1410     "F" gnus-summary-followup-with-original
1411     "C" gnus-summary-cancel-article
1412     "r" gnus-summary-reply
1413     "R" gnus-summary-reply-with-original
1414     "\C-c\C-f" gnus-summary-mail-forward
1415     "o" gnus-summary-save-article
1416     "\C-o" gnus-summary-save-article-mail
1417     "|" gnus-summary-pipe-output
1418     "\M-k" gnus-summary-edit-local-kill
1419     "\M-K" gnus-summary-edit-global-kill
1420     ;; "V" gnus-version
1421     "\C-c\C-d" gnus-summary-describe-group
1422     "q" gnus-summary-exit
1423     "Q" gnus-summary-exit-no-update
1424     "\C-c\C-i" gnus-info-find-node
1425     gnus-mouse-2 gnus-mouse-pick-article
1426     "m" gnus-summary-mail-other-window
1427     "a" gnus-summary-post-news
1428     "x" gnus-summary-limit-to-unread
1429     "s" gnus-summary-isearch-article
1430     "t" gnus-summary-toggle-header
1431     "g" gnus-summary-show-article
1432     "l" gnus-summary-goto-last-article
1433     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1434     "\C-d" gnus-summary-enter-digest-group
1435     "\M-\C-d" gnus-summary-read-document
1436     "\M-\C-e" gnus-summary-edit-parameters
1437     "\M-\C-a" gnus-summary-customize-parameters
1438     "\C-c\C-b" gnus-bug
1439     "*" gnus-cache-enter-article
1440     "\M-*" gnus-cache-remove-article
1441     "\M-&" gnus-summary-universal-argument
1442     "\C-l" gnus-recenter
1443     "I" gnus-summary-increase-score
1444     "L" gnus-summary-lower-score
1445     "\M-i" gnus-symbolic-argument
1446     "h" gnus-summary-select-article-buffer
1447
1448     "b" gnus-article-view-part
1449     "\M-t" gnus-summary-toggle-display-buttonized
1450
1451     "V" gnus-summary-score-map
1452     "X" gnus-uu-extract-map
1453     "S" gnus-summary-send-map)
1454
1455   ;; Sort of orthogonal keymap
1456   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1457     "t" gnus-summary-tick-article-forward
1458     "!" gnus-summary-tick-article-forward
1459     "d" gnus-summary-mark-as-read-forward
1460     "r" gnus-summary-mark-as-read-forward
1461     "c" gnus-summary-clear-mark-forward
1462     " " gnus-summary-clear-mark-forward
1463     "e" gnus-summary-mark-as-expirable
1464     "x" gnus-summary-mark-as-expirable
1465     "?" gnus-summary-mark-as-dormant
1466     "b" gnus-summary-set-bookmark
1467     "B" gnus-summary-remove-bookmark
1468     "#" gnus-summary-mark-as-processable
1469     "\M-#" gnus-summary-unmark-as-processable
1470     "S" gnus-summary-limit-include-expunged
1471     "C" gnus-summary-catchup
1472     "H" gnus-summary-catchup-to-here
1473     "\C-c" gnus-summary-catchup-all
1474     "k" gnus-summary-kill-same-subject-and-select
1475     "K" gnus-summary-kill-same-subject
1476     "P" gnus-uu-mark-map)
1477
1478   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1479     "c" gnus-summary-clear-above
1480     "u" gnus-summary-tick-above
1481     "m" gnus-summary-mark-above
1482     "k" gnus-summary-kill-below)
1483
1484   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1485     "/" gnus-summary-limit-to-subject
1486     "n" gnus-summary-limit-to-articles
1487     "w" gnus-summary-pop-limit
1488     "s" gnus-summary-limit-to-subject
1489     "a" gnus-summary-limit-to-author
1490     "u" gnus-summary-limit-to-unread
1491     "m" gnus-summary-limit-to-marks
1492     "M" gnus-summary-limit-exclude-marks
1493     "v" gnus-summary-limit-to-score
1494     "*" gnus-summary-limit-include-cached
1495     "D" gnus-summary-limit-include-dormant
1496     "T" gnus-summary-limit-include-thread
1497     "d" gnus-summary-limit-exclude-dormant
1498     "t" gnus-summary-limit-to-age
1499     "x" gnus-summary-limit-to-extra
1500     "E" gnus-summary-limit-include-expunged
1501     "c" gnus-summary-limit-exclude-childless-dormant
1502     "C" gnus-summary-limit-mark-excluded-as-read)
1503
1504   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1505     "n" gnus-summary-next-unread-article
1506     "p" gnus-summary-prev-unread-article
1507     "N" gnus-summary-next-article
1508     "P" gnus-summary-prev-article
1509     "\C-n" gnus-summary-next-same-subject
1510     "\C-p" gnus-summary-prev-same-subject
1511     "\M-n" gnus-summary-next-unread-subject
1512     "\M-p" gnus-summary-prev-unread-subject
1513     "f" gnus-summary-first-unread-article
1514     "b" gnus-summary-best-unread-article
1515     "j" gnus-summary-goto-article
1516     "g" gnus-summary-goto-subject
1517     "l" gnus-summary-goto-last-article
1518     "o" gnus-summary-pop-article)
1519
1520   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1521     "k" gnus-summary-kill-thread
1522     "l" gnus-summary-lower-thread
1523     "i" gnus-summary-raise-thread
1524     "T" gnus-summary-toggle-threads
1525     "t" gnus-summary-rethread-current
1526     "^" gnus-summary-reparent-thread
1527     "s" gnus-summary-show-thread
1528     "S" gnus-summary-show-all-threads
1529     "h" gnus-summary-hide-thread
1530     "H" gnus-summary-hide-all-threads
1531     "n" gnus-summary-next-thread
1532     "p" gnus-summary-prev-thread
1533     "u" gnus-summary-up-thread
1534     "o" gnus-summary-top-thread
1535     "d" gnus-summary-down-thread
1536     "#" gnus-uu-mark-thread
1537     "\M-#" gnus-uu-unmark-thread)
1538
1539   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1540     "g" gnus-summary-prepare
1541     "c" gnus-summary-insert-cached-articles)
1542
1543   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1544     "c" gnus-summary-catchup-and-exit
1545     "C" gnus-summary-catchup-all-and-exit
1546     "E" gnus-summary-exit-no-update
1547     "Q" gnus-summary-exit
1548     "Z" gnus-summary-exit
1549     "n" gnus-summary-catchup-and-goto-next-group
1550     "R" gnus-summary-reselect-current-group
1551     "G" gnus-summary-rescan-group
1552     "N" gnus-summary-next-group
1553     "s" gnus-summary-save-newsrc
1554     "P" gnus-summary-prev-group)
1555
1556   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1557     " " gnus-summary-next-page
1558     "n" gnus-summary-next-page
1559     "\177" gnus-summary-prev-page
1560     [delete] gnus-summary-prev-page
1561     "p" gnus-summary-prev-page
1562     "\r" gnus-summary-scroll-up
1563     "\M-\r" gnus-summary-scroll-down
1564     "<" gnus-summary-beginning-of-article
1565     ">" gnus-summary-end-of-article
1566     "b" gnus-summary-beginning-of-article
1567     "e" gnus-summary-end-of-article
1568     "^" gnus-summary-refer-parent-article
1569     "r" gnus-summary-refer-parent-article
1570     "D" gnus-summary-enter-digest-group
1571     "R" gnus-summary-refer-references
1572     "T" gnus-summary-refer-thread
1573     "g" gnus-summary-show-article
1574     "s" gnus-summary-isearch-article
1575     "P" gnus-summary-print-article
1576     "M" gnus-mailing-list-insinuate
1577     "t" gnus-article-babel)
1578
1579   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1580     "b" gnus-article-add-buttons
1581     "B" gnus-article-add-buttons-to-head
1582     "o" gnus-article-treat-overstrike
1583     "e" gnus-article-emphasize
1584     "w" gnus-article-fill-cited-article
1585     "Q" gnus-article-fill-long-lines
1586     "C" gnus-article-capitalize-sentences
1587     "c" gnus-article-remove-cr
1588     "q" gnus-article-de-quoted-unreadable
1589     "6" gnus-article-de-base64-unreadable
1590     "Z" gnus-article-decode-HZ
1591     "h" gnus-article-wash-html
1592     "s" gnus-summary-force-verify-and-decrypt
1593     "f" gnus-article-display-x-face
1594     "l" gnus-summary-stop-page-breaking
1595     "r" gnus-summary-caesar-message
1596     "t" gnus-summary-toggle-header
1597     "v" gnus-summary-verbose-headers
1598     "H" gnus-article-strip-headers-in-body
1599     "p" gnus-article-verify-x-pgp-sig
1600     "d" gnus-article-treat-dumbquotes)
1601
1602   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1603     "a" gnus-article-hide
1604     "h" gnus-article-hide-headers
1605     "b" gnus-article-hide-boring-headers
1606     "s" gnus-article-hide-signature
1607     "c" gnus-article-hide-citation
1608     "C" gnus-article-hide-citation-in-followups
1609     "l" gnus-article-hide-list-identifiers
1610     "p" gnus-article-hide-pgp
1611     "B" gnus-article-strip-banner
1612     "P" gnus-article-hide-pem
1613     "\C-c" gnus-article-hide-citation-maybe)
1614
1615   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1616     "a" gnus-article-highlight
1617     "h" gnus-article-highlight-headers
1618     "c" gnus-article-highlight-citation
1619     "s" gnus-article-highlight-signature)
1620
1621   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1622     "w" gnus-article-decode-mime-words
1623     "c" gnus-article-decode-charset
1624     "v" gnus-mime-view-all-parts
1625     "b" gnus-article-view-part)
1626
1627   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1628     "z" gnus-article-date-ut
1629     "u" gnus-article-date-ut
1630     "l" gnus-article-date-local
1631     "e" gnus-article-date-lapsed
1632     "o" gnus-article-date-original
1633     "i" gnus-article-date-iso8601
1634     "s" gnus-article-date-user)
1635
1636   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1637     "t" gnus-article-remove-trailing-blank-lines
1638     "l" gnus-article-strip-leading-blank-lines
1639     "m" gnus-article-strip-multiple-blank-lines
1640     "a" gnus-article-strip-blank-lines
1641     "A" gnus-article-strip-all-blank-lines
1642     "s" gnus-article-strip-leading-space
1643     "e" gnus-article-strip-trailing-space)
1644
1645   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1646     "v" gnus-version
1647     "f" gnus-summary-fetch-faq
1648     "d" gnus-summary-describe-group
1649     "h" gnus-summary-describe-briefly
1650     "i" gnus-info-find-node)
1651
1652   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1653     "e" gnus-summary-expire-articles
1654     "\M-\C-e" gnus-summary-expire-articles-now
1655     "\177" gnus-summary-delete-article
1656     [delete] gnus-summary-delete-article
1657     [backspace] gnus-summary-delete-article
1658     "m" gnus-summary-move-article
1659     "r" gnus-summary-respool-article
1660     "w" gnus-summary-edit-article
1661     "c" gnus-summary-copy-article
1662     "B" gnus-summary-crosspost-article
1663     "q" gnus-summary-respool-query
1664     "t" gnus-summary-respool-trace
1665     "i" gnus-summary-import-article
1666     "p" gnus-summary-article-posted-p)
1667
1668   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1669     "o" gnus-summary-save-article
1670     "m" gnus-summary-save-article-mail
1671     "F" gnus-summary-write-article-file
1672     "r" gnus-summary-save-article-rmail
1673     "f" gnus-summary-save-article-file
1674     "b" gnus-summary-save-article-body-file
1675     "h" gnus-summary-save-article-folder
1676     "v" gnus-summary-save-article-vm
1677     "p" gnus-summary-pipe-output
1678     "s" gnus-soup-add-article)
1679
1680   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1681     "b" gnus-summary-display-buttonized
1682     "m" gnus-summary-repair-multipart
1683     "v" gnus-article-view-part
1684     "o" gnus-article-save-part
1685     "c" gnus-article-copy-part
1686     "C" gnus-article-view-part-as-charset
1687     "e" gnus-article-externalize-part
1688     "E" gnus-article-encrypt-body
1689     "i" gnus-article-inline-part
1690     "|" gnus-article-pipe-part))
1691
1692 (defun gnus-summary-make-menu-bar ()
1693   (gnus-turn-off-edit-menu 'summary)
1694
1695   (unless (boundp 'gnus-summary-misc-menu)
1696
1697     (easy-menu-define
1698      gnus-summary-kill-menu gnus-summary-mode-map ""
1699      (cons
1700       "Score"
1701       (nconc
1702        (list
1703         ["Enter score..." gnus-summary-score-entry t]
1704         ["Customize" gnus-score-customize t])
1705        (gnus-make-score-map 'increase)
1706        (gnus-make-score-map 'lower)
1707        '(("Mark"
1708           ["Kill below" gnus-summary-kill-below t]
1709           ["Mark above" gnus-summary-mark-above t]
1710           ["Tick above" gnus-summary-tick-above t]
1711           ["Clear above" gnus-summary-clear-above t])
1712          ["Current score" gnus-summary-current-score t]
1713          ["Set score" gnus-summary-set-score t]
1714          ["Switch current score file..." gnus-score-change-score-file t]
1715          ["Set mark below..." gnus-score-set-mark-below t]
1716          ["Set expunge below..." gnus-score-set-expunge-below t]
1717          ["Edit current score file" gnus-score-edit-current-scores t]
1718          ["Edit score file" gnus-score-edit-file t]
1719          ["Trace score" gnus-score-find-trace t]
1720          ["Find words" gnus-score-find-favourite-words t]
1721          ["Rescore buffer" gnus-summary-rescore t]
1722          ["Increase score..." gnus-summary-increase-score t]
1723          ["Lower score..." gnus-summary-lower-score t]))))
1724
1725     ;; Define both the Article menu in the summary buffer and the equivalent
1726     ;; Commands menu in the article buffer here for consistency.
1727     (let ((innards
1728            `(("Hide"
1729               ["All" gnus-article-hide t]
1730               ["Headers" gnus-article-hide-headers t]
1731               ["Signature" gnus-article-hide-signature t]
1732               ["Citation" gnus-article-hide-citation t]
1733               ["List identifiers" gnus-article-hide-list-identifiers t]
1734               ["PGP" gnus-article-hide-pgp t]
1735               ["Banner" gnus-article-strip-banner t]
1736               ["Boring headers" gnus-article-hide-boring-headers t])
1737              ("Highlight"
1738               ["All" gnus-article-highlight t]
1739               ["Headers" gnus-article-highlight-headers t]
1740               ["Signature" gnus-article-highlight-signature t]
1741               ["Citation" gnus-article-highlight-citation t])
1742              ("MIME"
1743               ["Words" gnus-article-decode-mime-words t]
1744               ["Charset" gnus-article-decode-charset t]
1745               ["QP" gnus-article-de-quoted-unreadable t]
1746               ["Base64" gnus-article-de-base64-unreadable t]
1747               ["View all" gnus-mime-view-all-parts t]
1748               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1749               ["Encrypt body" gnus-article-encrypt-body t])
1750              ("Date"
1751               ["Local" gnus-article-date-local t]
1752               ["ISO8601" gnus-article-date-iso8601 t]
1753               ["UT" gnus-article-date-ut t]
1754               ["Original" gnus-article-date-original t]
1755               ["Lapsed" gnus-article-date-lapsed t]
1756               ["User-defined" gnus-article-date-user t])
1757              ("Washing"
1758               ("Remove Blanks"
1759                ["Leading" gnus-article-strip-leading-blank-lines t]
1760                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1761                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1762                ["All of the above" gnus-article-strip-blank-lines t]
1763                ["All" gnus-article-strip-all-blank-lines t]
1764                ["Leading space" gnus-article-strip-leading-space t]
1765                ["Trailing space" gnus-article-strip-trailing-space t])
1766               ["Overstrike" gnus-article-treat-overstrike t]
1767               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1768               ["Emphasis" gnus-article-emphasize t]
1769               ["Word wrap" gnus-article-fill-cited-article t]
1770               ["Fill long lines" gnus-article-fill-long-lines t]
1771               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1772               ["CR" gnus-article-remove-cr t]
1773               ["Show X-Face" gnus-article-display-x-face t]
1774               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1775               ["Base64" gnus-article-de-base64-unreadable t]
1776               ["Rot 13" gnus-summary-caesar-message
1777                ,@(if (featurep 'xemacs) nil
1778                    '(:help "\"Caesar rotate\" article by 13"))]
1779               ["Unix pipe" gnus-summary-pipe-message t]
1780               ["Add buttons" gnus-article-add-buttons t]
1781               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1782               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1783               ["Verbose header" gnus-summary-verbose-headers t]
1784               ["Toggle header" gnus-summary-toggle-header t]
1785               ["Html" gnus-article-wash-html t]
1786               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1787               ["HZ" gnus-article-decode-HZ t])
1788              ("Output"
1789               ["Save in default format" gnus-summary-save-article
1790                ,@(if (featurep 'xemacs) nil
1791                    '(:help "Save article using default method"))]
1792               ["Save in file" gnus-summary-save-article-file
1793                ,@(if (featurep 'xemacs) nil
1794                    '(:help "Save article in file"))]
1795               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1796               ["Save in MH folder" gnus-summary-save-article-folder t]
1797               ["Save in VM folder" gnus-summary-save-article-vm t]
1798               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1799               ["Save body in file" gnus-summary-save-article-body-file t]
1800               ["Pipe through a filter" gnus-summary-pipe-output t]
1801               ["Add to SOUP packet" gnus-soup-add-article t]
1802               ["Print" gnus-summary-print-article t])
1803              ("Backend"
1804               ["Respool article..." gnus-summary-respool-article t]
1805               ["Move article..." gnus-summary-move-article
1806                (gnus-check-backend-function
1807                 'request-move-article gnus-newsgroup-name)]
1808               ["Copy article..." gnus-summary-copy-article t]
1809               ["Crosspost article..." gnus-summary-crosspost-article
1810                (gnus-check-backend-function
1811                 'request-replace-article gnus-newsgroup-name)]
1812               ["Import file..." gnus-summary-import-article t]
1813               ["Check if posted" gnus-summary-article-posted-p t]
1814               ["Edit article" gnus-summary-edit-article
1815                (not (gnus-group-read-only-p))]
1816               ["Delete article" gnus-summary-delete-article
1817                (gnus-check-backend-function
1818                 'request-expire-articles gnus-newsgroup-name)]
1819               ["Query respool" gnus-summary-respool-query t]
1820               ["Trace respool" gnus-summary-respool-trace t]
1821               ["Delete expirable articles" gnus-summary-expire-articles-now
1822                (gnus-check-backend-function
1823                 'request-expire-articles gnus-newsgroup-name)])
1824              ("Extract"
1825               ["Uudecode" gnus-uu-decode-uu
1826                ,@(if (featurep 'xemacs) nil
1827                    '(:help "Decode uuencoded article(s)"))]
1828               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1829               ["Unshar" gnus-uu-decode-unshar t]
1830               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1831               ["Save" gnus-uu-decode-save t]
1832               ["Binhex" gnus-uu-decode-binhex t]
1833               ["Postscript" gnus-uu-decode-postscript t])
1834              ("Cache"
1835               ["Enter article" gnus-cache-enter-article t]
1836               ["Remove article" gnus-cache-remove-article t])
1837              ["Translate" gnus-article-babel t]
1838              ["Select article buffer" gnus-summary-select-article-buffer t]
1839              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1840              ["Isearch article..." gnus-summary-isearch-article t]
1841              ["Beginning of the article" gnus-summary-beginning-of-article t]
1842              ["End of the article" gnus-summary-end-of-article t]
1843              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1844              ["Fetch referenced articles" gnus-summary-refer-references t]
1845              ["Fetch current thread" gnus-summary-refer-thread t]
1846              ["Fetch article with id..." gnus-summary-refer-article t]
1847              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1848              ["Redisplay" gnus-summary-show-article t])))
1849       (easy-menu-define
1850        gnus-summary-article-menu gnus-summary-mode-map ""
1851        (cons "Article" innards))
1852
1853       (easy-menu-define
1854        gnus-article-commands-menu gnus-article-mode-map ""
1855        (cons "Commands" innards)))
1856
1857     (easy-menu-define
1858      gnus-summary-thread-menu gnus-summary-mode-map ""
1859      '("Threads"
1860        ["Toggle threading" gnus-summary-toggle-threads t]
1861        ["Hide threads" gnus-summary-hide-all-threads t]
1862        ["Show threads" gnus-summary-show-all-threads t]
1863        ["Hide thread" gnus-summary-hide-thread t]
1864        ["Show thread" gnus-summary-show-thread t]
1865        ["Go to next thread" gnus-summary-next-thread t]
1866        ["Go to previous thread" gnus-summary-prev-thread t]
1867        ["Go down thread" gnus-summary-down-thread t]
1868        ["Go up thread" gnus-summary-up-thread t]
1869        ["Top of thread" gnus-summary-top-thread t]
1870        ["Mark thread as read" gnus-summary-kill-thread t]
1871        ["Lower thread score" gnus-summary-lower-thread t]
1872        ["Raise thread score" gnus-summary-raise-thread t]
1873        ["Rethread current" gnus-summary-rethread-current t]))
1874
1875     (easy-menu-define
1876      gnus-summary-post-menu gnus-summary-mode-map ""
1877      `("Post"
1878        ["Post an article" gnus-summary-post-news
1879         ,@(if (featurep 'xemacs) nil
1880             '(:help "Post an article"))]
1881        ["Followup" gnus-summary-followup
1882         ,@(if (featurep 'xemacs) nil
1883             '(:help "Post followup to this article"))]
1884        ["Followup and yank" gnus-summary-followup-with-original
1885         ,@(if (featurep 'xemacs) nil
1886             '(:help "Post followup to this article, quoting its contents"))]
1887        ["Supersede article" gnus-summary-supersede-article t]
1888        ["Cancel article" gnus-summary-cancel-article
1889         ,@(if (featurep 'xemacs) nil
1890             '(:help "Cancel an article you posted"))]
1891        ["Reply" gnus-summary-reply t]
1892        ["Reply and yank" gnus-summary-reply-with-original t]
1893        ["Wide reply" gnus-summary-wide-reply t]
1894        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1895         ,@(if (featurep 'xemacs) nil
1896             '(:help "Mail a reply, quoting this article"))]
1897        ["Mail forward" gnus-summary-mail-forward t]
1898        ["Post forward" gnus-summary-post-forward t]
1899        ["Digest and mail" gnus-uu-digest-mail-forward t]
1900        ["Digest and post" gnus-uu-digest-post-forward t]
1901        ["Resend message" gnus-summary-resend-message t]
1902        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1903        ["Send a mail" gnus-summary-mail-other-window t]
1904        ["Uuencode and post" gnus-uu-post-news
1905         ,@(if (featurep 'xemacs) nil
1906             '(:help "Post a uuencoded article"))]
1907        ["Followup via news" gnus-summary-followup-to-mail t]
1908        ["Followup via news and yank"
1909         gnus-summary-followup-to-mail-with-original t]
1910        ;;("Draft"
1911        ;;["Send" gnus-summary-send-draft t]
1912        ;;["Send bounced" gnus-resend-bounced-mail t])
1913        ))
1914
1915     (easy-menu-define
1916      gnus-summary-misc-menu gnus-summary-mode-map ""
1917      `("Misc"
1918        ("Mark Read"
1919         ["Mark as read" gnus-summary-mark-as-read-forward t]
1920         ["Mark same subject and select"
1921          gnus-summary-kill-same-subject-and-select t]
1922         ["Mark same subject" gnus-summary-kill-same-subject t]
1923         ["Catchup" gnus-summary-catchup
1924          ,@(if (featurep 'xemacs) nil
1925              '(:help "Mark unread articles in this group as read"))]
1926         ["Catchup all" gnus-summary-catchup-all t]
1927         ["Catchup to here" gnus-summary-catchup-to-here t]
1928         ["Catchup region" gnus-summary-mark-region-as-read t]
1929         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1930        ("Mark Various"
1931         ["Tick" gnus-summary-tick-article-forward t]
1932         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1933         ["Remove marks" gnus-summary-clear-mark-forward t]
1934         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1935         ["Set bookmark" gnus-summary-set-bookmark t]
1936         ["Remove bookmark" gnus-summary-remove-bookmark t])
1937        ("Mark Limit"
1938         ["Marks..." gnus-summary-limit-to-marks t]
1939         ["Subject..." gnus-summary-limit-to-subject t]
1940         ["Author..." gnus-summary-limit-to-author t]
1941         ["Age..." gnus-summary-limit-to-age t]
1942         ["Extra..." gnus-summary-limit-to-extra t]
1943         ["Score" gnus-summary-limit-to-score t]
1944         ["Unread" gnus-summary-limit-to-unread t]
1945         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1946         ["Articles" gnus-summary-limit-to-articles t]
1947         ["Pop limit" gnus-summary-pop-limit t]
1948         ["Show dormant" gnus-summary-limit-include-dormant t]
1949         ["Hide childless dormant"
1950          gnus-summary-limit-exclude-childless-dormant t]
1951         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1952         ["Hide marked" gnus-summary-limit-exclude-marks t]
1953         ["Show expunged" gnus-summary-show-all-expunged t])
1954        ("Process Mark"
1955         ["Set mark" gnus-summary-mark-as-processable t]
1956         ["Remove mark" gnus-summary-unmark-as-processable t]
1957         ["Remove all marks" gnus-summary-unmark-all-processable t]
1958         ["Mark above" gnus-uu-mark-over t]
1959         ["Mark series" gnus-uu-mark-series t]
1960         ["Mark region" gnus-uu-mark-region t]
1961         ["Unmark region" gnus-uu-unmark-region t]
1962         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1963         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1964         ["Mark all" gnus-uu-mark-all t]
1965         ["Mark buffer" gnus-uu-mark-buffer t]
1966         ["Mark sparse" gnus-uu-mark-sparse t]
1967         ["Mark thread" gnus-uu-mark-thread t]
1968         ["Unmark thread" gnus-uu-unmark-thread t]
1969         ("Process Mark Sets"
1970          ["Kill" gnus-summary-kill-process-mark t]
1971          ["Yank" gnus-summary-yank-process-mark
1972           gnus-newsgroup-process-stack]
1973          ["Save" gnus-summary-save-process-mark t]))
1974        ("Scroll article"
1975         ["Page forward" gnus-summary-next-page
1976          ,@(if (featurep 'xemacs) nil
1977              '(:help "Show next page of article"))]
1978         ["Page backward" gnus-summary-prev-page
1979          ,@(if (featurep 'xemacs) nil
1980              '(:help "Show previous page of article"))]
1981         ["Line forward" gnus-summary-scroll-up t])
1982        ("Move"
1983         ["Next unread article" gnus-summary-next-unread-article t]
1984         ["Previous unread article" gnus-summary-prev-unread-article t]
1985         ["Next article" gnus-summary-next-article t]
1986         ["Previous article" gnus-summary-prev-article t]
1987         ["Next unread subject" gnus-summary-next-unread-subject t]
1988         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1989         ["Next article same subject" gnus-summary-next-same-subject t]
1990         ["Previous article same subject" gnus-summary-prev-same-subject t]
1991         ["First unread article" gnus-summary-first-unread-article t]
1992         ["Best unread article" gnus-summary-best-unread-article t]
1993         ["Go to subject number..." gnus-summary-goto-subject t]
1994         ["Go to article number..." gnus-summary-goto-article t]
1995         ["Go to the last article" gnus-summary-goto-last-article t]
1996         ["Pop article off history" gnus-summary-pop-article t])
1997        ("Sort"
1998         ["Sort by number" gnus-summary-sort-by-number t]
1999         ["Sort by author" gnus-summary-sort-by-author t]
2000         ["Sort by subject" gnus-summary-sort-by-subject t]
2001         ["Sort by date" gnus-summary-sort-by-date t]
2002         ["Sort by score" gnus-summary-sort-by-score t]
2003         ["Sort by lines" gnus-summary-sort-by-lines t]
2004         ["Sort by characters" gnus-summary-sort-by-chars t])
2005        ("Help"
2006         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2007         ["Describe group" gnus-summary-describe-group t]
2008         ["Read manual" gnus-info-find-node t])
2009        ("Modes"
2010         ["Pick and read" gnus-pick-mode t]
2011         ["Binary" gnus-binary-mode t])
2012        ("Regeneration"
2013         ["Regenerate" gnus-summary-prepare t]
2014         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2015         ["Toggle threading" gnus-summary-toggle-threads t])
2016        ["Filter articles..." gnus-summary-execute-command t]
2017        ["Run command on subjects..." gnus-summary-universal-argument t]
2018        ["Search articles forward..." gnus-summary-search-article-forward t]
2019        ["Search articles backward..." gnus-summary-search-article-backward t]
2020        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2021        ["Expand window" gnus-summary-expand-window t]
2022        ["Expire expirable articles" gnus-summary-expire-articles
2023         (gnus-check-backend-function
2024          'request-expire-articles gnus-newsgroup-name)]
2025        ["Edit local kill file" gnus-summary-edit-local-kill t]
2026        ["Edit main kill file" gnus-summary-edit-global-kill t]
2027        ["Edit group parameters" gnus-summary-edit-parameters t]
2028        ["Customize group parameters" gnus-summary-customize-parameters t]
2029        ["Send a bug report" gnus-bug t]
2030        ("Exit"
2031         ["Catchup and exit" gnus-summary-catchup-and-exit
2032          ,@(if (featurep 'xemacs) nil
2033              '(:help "Mark unread articles in this group as read, then exit"))]
2034         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2035         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2036         ["Exit group" gnus-summary-exit
2037          ,@(if (featurep 'xemacs) nil
2038              '(:help "Exit current group, return to group selection mode"))]
2039         ["Exit group without updating" gnus-summary-exit-no-update t]
2040         ["Exit and goto next group" gnus-summary-next-group t]
2041         ["Exit and goto prev group" gnus-summary-prev-group t]
2042         ["Reselect group" gnus-summary-reselect-current-group t]
2043         ["Rescan group" gnus-summary-rescan-group t]
2044         ["Update dribble" gnus-summary-save-newsrc t])))
2045
2046     (gnus-run-hooks 'gnus-summary-menu-hook)))
2047
2048 (defvar gnus-summary-tool-bar-map nil)
2049
2050 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2051 (defun gnus-summary-make-tool-bar ()
2052   (if (and (fboundp 'tool-bar-add-item-from-menu)
2053            (default-value 'tool-bar-mode)
2054            (not gnus-summary-tool-bar-map))
2055       (setq gnus-summary-tool-bar-map
2056             (let ((tool-bar-map (make-sparse-keymap)))
2057               (tool-bar-add-item-from-menu
2058                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2059               (tool-bar-add-item-from-menu
2060                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2061               (tool-bar-add-item-from-menu
2062                'gnus-summary-post-news "post" gnus-summary-mode-map)
2063               (tool-bar-add-item-from-menu
2064                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2065               (tool-bar-add-item-from-menu
2066                'gnus-summary-followup "followup" gnus-summary-mode-map)
2067               (tool-bar-add-item-from-menu
2068                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2069               (tool-bar-add-item-from-menu
2070                'gnus-summary-reply "reply" gnus-summary-mode-map)
2071               (tool-bar-add-item-from-menu
2072                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2073               (tool-bar-add-item-from-menu
2074                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2075               (tool-bar-add-item-from-menu
2076                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2077               (tool-bar-add-item-from-menu
2078                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2079               (tool-bar-add-item-from-menu
2080                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2081               (tool-bar-add-item-from-menu
2082                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2083               (tool-bar-add-item-from-menu
2084                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2085               (tool-bar-add-item-from-menu
2086                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2087               tool-bar-map)))
2088   (if gnus-summary-tool-bar-map
2089       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2090
2091 (defun gnus-score-set-default (var value)
2092   "A version of set that updates the GNU Emacs menu-bar."
2093   (set var value)
2094   ;; It is the message that forces the active status to be updated.
2095   (message ""))
2096
2097 (defun gnus-make-score-map (type)
2098   "Make a summary score map of type TYPE."
2099   (if t
2100       nil
2101     (let ((headers '(("author" "from" string)
2102                      ("subject" "subject" string)
2103                      ("article body" "body" string)
2104                      ("article head" "head" string)
2105                      ("xref" "xref" string)
2106                      ("extra header" "extra" string)
2107                      ("lines" "lines" number)
2108                      ("followups to author" "followup" string)))
2109           (types '((number ("less than" <)
2110                            ("greater than" >)
2111                            ("equal" =))
2112                    (string ("substring" s)
2113                            ("exact string" e)
2114                            ("fuzzy string" f)
2115                            ("regexp" r))))
2116           (perms '(("temporary" (current-time-string))
2117                    ("permanent" nil)
2118                    ("immediate" now)))
2119           header)
2120       (list
2121        (apply
2122         'nconc
2123         (list
2124          (if (eq type 'lower)
2125              "Lower score"
2126            "Increase score"))
2127         (let (outh)
2128           (while headers
2129             (setq header (car headers))
2130             (setq outh
2131                   (cons
2132                    (apply
2133                     'nconc
2134                     (list (car header))
2135                     (let ((ts (cdr (assoc (nth 2 header) types)))
2136                           outt)
2137                       (while ts
2138                         (setq outt
2139                               (cons
2140                                (apply
2141                                 'nconc
2142                                 (list (caar ts))
2143                                 (let ((ps perms)
2144                                       outp)
2145                                   (while ps
2146                                     (setq outp
2147                                           (cons
2148                                            (vector
2149                                             (caar ps)
2150                                             (list
2151                                              'gnus-summary-score-entry
2152                                              (nth 1 header)
2153                                              (if (or (string= (nth 1 header)
2154                                                               "head")
2155                                                      (string= (nth 1 header)
2156                                                               "body"))
2157                                                  ""
2158                                                (list 'gnus-summary-header
2159                                                      (nth 1 header)))
2160                                              (list 'quote (nth 1 (car ts)))
2161                                              (list 'gnus-score-delta-default
2162                                                    nil)
2163                                              (nth 1 (car ps))
2164                                              t)
2165                                             t)
2166                                            outp))
2167                                     (setq ps (cdr ps)))
2168                                   (list (nreverse outp))))
2169                                outt))
2170                         (setq ts (cdr ts)))
2171                       (list (nreverse outt))))
2172                    outh))
2173             (setq headers (cdr headers)))
2174           (list (nreverse outh))))))))
2175
2176 \f
2177
2178 (defun gnus-summary-mode (&optional group)
2179   "Major mode for reading articles.
2180
2181 All normal editing commands are switched off.
2182 \\<gnus-summary-mode-map>
2183 Each line in this buffer represents one article.  To read an
2184 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2185 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2186 respectively.
2187
2188 You can also post articles and send mail from this buffer.  To
2189 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2190 of an article, type `\\[gnus-summary-reply]'.
2191
2192 There are approx. one gazillion commands you can execute in this
2193 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2194
2195 The following commands are available:
2196
2197 \\{gnus-summary-mode-map}"
2198   (interactive)
2199   (kill-all-local-variables)
2200   (when (gnus-visual-p 'summary-menu 'menu)
2201     (gnus-summary-make-menu-bar)
2202     (gnus-summary-make-tool-bar))
2203   (gnus-summary-make-local-variables)
2204   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2205     (gnus-summary-make-local-variables))
2206   (gnus-make-thread-indent-array)
2207   (gnus-simplify-mode-line)
2208   (setq major-mode 'gnus-summary-mode)
2209   (setq mode-name "Summary")
2210   (make-local-variable 'minor-mode-alist)
2211   (use-local-map gnus-summary-mode-map)
2212   (buffer-disable-undo)
2213   (setq buffer-read-only t)             ;Disable modification
2214   (setq truncate-lines t)
2215   (setq selective-display t)
2216   (setq selective-display-ellipses t)   ;Display `...'
2217   (gnus-summary-set-display-table)
2218   (gnus-set-default-directory)
2219   (setq gnus-newsgroup-name group)
2220   (make-local-variable 'gnus-summary-line-format)
2221   (make-local-variable 'gnus-summary-line-format-spec)
2222   (make-local-variable 'gnus-summary-dummy-line-format)
2223   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2224   (make-local-variable 'gnus-summary-mark-positions)
2225   (make-local-hook 'pre-command-hook)
2226   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2227   (gnus-run-hooks 'gnus-summary-mode-hook)
2228   (turn-on-gnus-mailing-list-mode)
2229   (mm-enable-multibyte-mule4)
2230   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2231   (gnus-update-summary-mark-positions))
2232
2233 (defun gnus-summary-make-local-variables ()
2234   "Make all the local summary buffer variables."
2235   (let (global)
2236     (dolist (local gnus-summary-local-variables)
2237       (if (consp local)
2238           (progn
2239             (if (eq (cdr local) 'global)
2240                 ;; Copy the global value of the variable.
2241                 (setq global (symbol-value (car local)))
2242               ;; Use the value from the list.
2243               (setq global (eval (cdr local))))
2244             (set (make-local-variable (car local)) global))
2245         ;; Simple nil-valued local variable.
2246         (set (make-local-variable local) nil)))))
2247
2248 (defun gnus-summary-clear-local-variables ()
2249   (let ((locals gnus-summary-local-variables))
2250     (while locals
2251       (if (consp (car locals))
2252           (and (vectorp (caar locals))
2253                (set (caar locals) nil))
2254         (and (vectorp (car locals))
2255              (set (car locals) nil)))
2256       (setq locals (cdr locals)))))
2257
2258 ;; Summary data functions.
2259
2260 (defmacro gnus-data-number (data)
2261   `(car ,data))
2262
2263 (defmacro gnus-data-set-number (data number)
2264   `(setcar ,data ,number))
2265
2266 (defmacro gnus-data-mark (data)
2267   `(nth 1 ,data))
2268
2269 (defmacro gnus-data-set-mark (data mark)
2270   `(setcar (nthcdr 1 ,data) ,mark))
2271
2272 (defmacro gnus-data-pos (data)
2273   `(nth 2 ,data))
2274
2275 (defmacro gnus-data-set-pos (data pos)
2276   `(setcar (nthcdr 2 ,data) ,pos))
2277
2278 (defmacro gnus-data-header (data)
2279   `(nth 3 ,data))
2280
2281 (defmacro gnus-data-set-header (data header)
2282   `(setf (nth 3 ,data) ,header))
2283
2284 (defmacro gnus-data-level (data)
2285   `(nth 4 ,data))
2286
2287 (defmacro gnus-data-unread-p (data)
2288   `(= (nth 1 ,data) gnus-unread-mark))
2289
2290 (defmacro gnus-data-read-p (data)
2291   `(/= (nth 1 ,data) gnus-unread-mark))
2292
2293 (defmacro gnus-data-pseudo-p (data)
2294   `(consp (nth 3 ,data)))
2295
2296 (defmacro gnus-data-find (number)
2297   `(assq ,number gnus-newsgroup-data))
2298
2299 (defmacro gnus-data-find-list (number &optional data)
2300   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2301      (memq (assq ,number bdata)
2302            bdata)))
2303
2304 (defmacro gnus-data-make (number mark pos header level)
2305   `(list ,number ,mark ,pos ,header ,level))
2306
2307 (defun gnus-data-enter (after-article number mark pos header level offset)
2308   (let ((data (gnus-data-find-list after-article)))
2309     (unless data
2310       (error "No such article: %d" after-article))
2311     (setcdr data (cons (gnus-data-make number mark pos header level)
2312                        (cdr data)))
2313     (setq gnus-newsgroup-data-reverse nil)
2314     (gnus-data-update-list (cddr data) offset)))
2315
2316 (defun gnus-data-enter-list (after-article list &optional offset)
2317   (when list
2318     (let ((data (and after-article (gnus-data-find-list after-article)))
2319           (ilist list))
2320       (if (not (or data
2321                    after-article))
2322           (let ((odata gnus-newsgroup-data))
2323             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2324             (when offset
2325               (gnus-data-update-list odata offset)))
2326         ;; Find the last element in the list to be spliced into the main
2327         ;; list.
2328         (while (cdr list)
2329           (setq list (cdr list)))
2330         (if (not data)
2331             (progn
2332               (setcdr list gnus-newsgroup-data)
2333               (setq gnus-newsgroup-data ilist)
2334               (when offset
2335                 (gnus-data-update-list (cdr list) offset)))
2336           (setcdr list (cdr data))
2337           (setcdr data ilist)
2338           (when offset
2339             (gnus-data-update-list (cdr list) offset))))
2340       (setq gnus-newsgroup-data-reverse nil))))
2341
2342 (defun gnus-data-remove (article &optional offset)
2343   (let ((data gnus-newsgroup-data))
2344     (if (= (gnus-data-number (car data)) article)
2345         (progn
2346           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2347                 gnus-newsgroup-data-reverse nil)
2348           (when offset
2349             (gnus-data-update-list gnus-newsgroup-data offset)))
2350       (while (cdr data)
2351         (when (= (gnus-data-number (cadr data)) article)
2352           (setcdr data (cddr data))
2353           (when offset
2354             (gnus-data-update-list (cdr data) offset))
2355           (setq data nil
2356                 gnus-newsgroup-data-reverse nil))
2357         (setq data (cdr data))))))
2358
2359 (defmacro gnus-data-list (backward)
2360   `(if ,backward
2361        (or gnus-newsgroup-data-reverse
2362            (setq gnus-newsgroup-data-reverse
2363                  (reverse gnus-newsgroup-data)))
2364      gnus-newsgroup-data))
2365
2366 (defun gnus-data-update-list (data offset)
2367   "Add OFFSET to the POS of all data entries in DATA."
2368   (setq gnus-newsgroup-data-reverse nil)
2369   (while data
2370     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2371     (setq data (cdr data))))
2372
2373 (defun gnus-summary-article-pseudo-p (article)
2374   "Say whether this article is a pseudo article or not."
2375   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2376
2377 (defmacro gnus-summary-article-sparse-p (article)
2378   "Say whether this article is a sparse article or not."
2379   `(memq ,article gnus-newsgroup-sparse))
2380
2381 (defmacro gnus-summary-article-ancient-p (article)
2382   "Say whether this article is a sparse article or not."
2383   `(memq ,article gnus-newsgroup-ancient))
2384
2385 (defun gnus-article-parent-p (number)
2386   "Say whether this article is a parent or not."
2387   (let ((data (gnus-data-find-list number)))
2388     (and (cdr data)                     ; There has to be an article after...
2389          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2390             (gnus-data-level (nth 1 data))))))
2391
2392 (defun gnus-article-children (number)
2393   "Return a list of all children to NUMBER."
2394   (let* ((data (gnus-data-find-list number))
2395          (level (gnus-data-level (car data)))
2396          children)
2397     (setq data (cdr data))
2398     (while (and data
2399                 (= (gnus-data-level (car data)) (1+ level)))
2400       (push (gnus-data-number (car data)) children)
2401       (setq data (cdr data)))
2402     children))
2403
2404 (defmacro gnus-summary-skip-intangible ()
2405   "If the current article is intangible, then jump to a different article."
2406   '(let ((to (get-text-property (point) 'gnus-intangible)))
2407      (and to (gnus-summary-goto-subject to))))
2408
2409 (defmacro gnus-summary-article-intangible-p ()
2410   "Say whether this article is intangible or not."
2411   '(get-text-property (point) 'gnus-intangible))
2412
2413 (defun gnus-article-read-p (article)
2414   "Say whether ARTICLE is read or not."
2415   (not (or (memq article gnus-newsgroup-marked)
2416            (memq article gnus-newsgroup-unreads)
2417            (memq article gnus-newsgroup-unselected)
2418            (memq article gnus-newsgroup-dormant))))
2419
2420 ;; Some summary mode macros.
2421
2422 (defmacro gnus-summary-article-number ()
2423   "The article number of the article on the current line.
2424 If there isn's an article number here, then we return the current
2425 article number."
2426   '(progn
2427      (gnus-summary-skip-intangible)
2428      (or (get-text-property (point) 'gnus-number)
2429          (gnus-summary-last-subject))))
2430
2431 (defmacro gnus-summary-article-header (&optional number)
2432   "Return the header of article NUMBER."
2433   `(gnus-data-header (gnus-data-find
2434                       ,(or number '(gnus-summary-article-number)))))
2435
2436 (defmacro gnus-summary-thread-level (&optional number)
2437   "Return the level of thread that starts with article NUMBER."
2438   `(if (and (eq gnus-summary-make-false-root 'dummy)
2439             (get-text-property (point) 'gnus-intangible))
2440        0
2441      (gnus-data-level (gnus-data-find
2442                        ,(or number '(gnus-summary-article-number))))))
2443
2444 (defmacro gnus-summary-article-mark (&optional number)
2445   "Return the mark of article NUMBER."
2446   `(gnus-data-mark (gnus-data-find
2447                     ,(or number '(gnus-summary-article-number)))))
2448
2449 (defmacro gnus-summary-article-pos (&optional number)
2450   "Return the position of the line of article NUMBER."
2451   `(gnus-data-pos (gnus-data-find
2452                    ,(or number '(gnus-summary-article-number)))))
2453
2454 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2455 (defmacro gnus-summary-article-subject (&optional number)
2456   "Return current subject string or nil if nothing."
2457   `(let ((headers
2458           ,(if number
2459                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2460              '(gnus-data-header (assq (gnus-summary-article-number)
2461                                       gnus-newsgroup-data)))))
2462      (and headers
2463           (vectorp headers)
2464           (mail-header-subject headers))))
2465
2466 (defmacro gnus-summary-article-score (&optional number)
2467   "Return current article score."
2468   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2469                   gnus-newsgroup-scored))
2470        gnus-summary-default-score 0))
2471
2472 (defun gnus-summary-article-children (&optional number)
2473   "Return a list of article numbers that are children of article NUMBER."
2474   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2475          (level (gnus-data-level (car data)))
2476          l children)
2477     (while (and (setq data (cdr data))
2478                 (> (setq l (gnus-data-level (car data))) level))
2479       (and (= (1+ level) l)
2480            (push (gnus-data-number (car data))
2481                  children)))
2482     (nreverse children)))
2483
2484 (defun gnus-summary-article-parent (&optional number)
2485   "Return the article number of the parent of article NUMBER."
2486   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2487                                     (gnus-data-list t)))
2488          (level (gnus-data-level (car data))))
2489     (if (zerop level)
2490         ()                              ; This is a root.
2491       ;; We search until we find an article with a level less than
2492       ;; this one.  That function has to be the parent.
2493       (while (and (setq data (cdr data))
2494                   (not (< (gnus-data-level (car data)) level))))
2495       (and data (gnus-data-number (car data))))))
2496
2497 (defun gnus-unread-mark-p (mark)
2498   "Say whether MARK is the unread mark."
2499   (= mark gnus-unread-mark))
2500
2501 (defun gnus-read-mark-p (mark)
2502   "Say whether MARK is one of the marks that mark as read.
2503 This is all marks except unread, ticked, dormant, and expirable."
2504   (not (or (= mark gnus-unread-mark)
2505            (= mark gnus-ticked-mark)
2506            (= mark gnus-dormant-mark)
2507            (= mark gnus-expirable-mark))))
2508
2509 (defmacro gnus-article-mark (number)
2510   "Return the MARK of article NUMBER.
2511 This macro should only be used when computing the mark the \"first\"
2512 time; i.e., when generating the summary lines.  After that,
2513 `gnus-summary-article-mark' should be used to examine the
2514 marks of articles."
2515   `(cond
2516     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2517     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2518     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2519     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2520     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2521     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2522     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2523     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2524            gnus-ancient-mark))))
2525
2526 ;; Saving hidden threads.
2527
2528 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2529 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2530
2531 (defmacro gnus-save-hidden-threads (&rest forms)
2532   "Save hidden threads, eval FORMS, and restore the hidden threads."
2533   (let ((config (make-symbol "config")))
2534     `(let ((,config (gnus-hidden-threads-configuration)))
2535        (unwind-protect
2536            (save-excursion
2537              ,@forms)
2538          (gnus-restore-hidden-threads-configuration ,config)))))
2539
2540 (defun gnus-data-compute-positions ()
2541   "Compute the positions of all articles."
2542   (setq gnus-newsgroup-data-reverse nil)
2543   (let ((data gnus-newsgroup-data))
2544     (save-excursion
2545       (gnus-save-hidden-threads
2546         (gnus-summary-show-all-threads)
2547         (goto-char (point-min))
2548         (while data
2549           (while (get-text-property (point) 'gnus-intangible)
2550             (forward-line 1))
2551           (gnus-data-set-pos (car data) (+ (point) 3))
2552           (setq data (cdr data))
2553           (forward-line 1))))))
2554
2555 (defun gnus-hidden-threads-configuration ()
2556   "Return the current hidden threads configuration."
2557   (save-excursion
2558     (let (config)
2559       (goto-char (point-min))
2560       (while (search-forward "\r" nil t)
2561         (push (1- (point)) config))
2562       config)))
2563
2564 (defun gnus-restore-hidden-threads-configuration (config)
2565   "Restore hidden threads configuration from CONFIG."
2566   (save-excursion
2567     (let (point buffer-read-only)
2568       (while (setq point (pop config))
2569         (when (and (< point (point-max))
2570                    (goto-char point)
2571                    (eq (char-after) ?\n))
2572           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2573
2574 ;; Various summary mode internalish functions.
2575
2576 (defun gnus-mouse-pick-article (e)
2577   (interactive "e")
2578   (mouse-set-point e)
2579   (gnus-summary-next-page nil t))
2580
2581 (defun gnus-summary-set-display-table ()
2582   "Change the display table.
2583 Odd characters have a tendency to mess
2584 up nicely formatted displays - we make all possible glyphs
2585 display only a single character."
2586
2587   ;; We start from the standard display table, if any.
2588   (let ((table (or (copy-sequence standard-display-table)
2589                    (make-display-table)))
2590         (i 32))
2591     ;; Nix out all the control chars...
2592     (while (>= (setq i (1- i)) 0)
2593       (aset table i [??]))
2594     ;; ... but not newline and cr, of course.  (cr is necessary for the
2595     ;; selective display).
2596     (aset table ?\n nil)
2597     (aset table ?\r nil)
2598     ;; We keep TAB as well.
2599     (aset table ?\t nil)
2600     ;; We nix out any glyphs over 126 that are not set already.
2601     (let ((i 256))
2602       (while (>= (setq i (1- i)) 127)
2603         ;; Only modify if the entry is nil.
2604         (unless (aref table i)
2605           (aset table i [??]))))
2606     (setq buffer-display-table table)))
2607
2608 (defun gnus-summary-setup-buffer (group)
2609   "Initialize summary buffer."
2610   (let ((buffer (concat "*Summary " group "*")))
2611     (if (get-buffer buffer)
2612         (progn
2613           (set-buffer buffer)
2614           (setq gnus-summary-buffer (current-buffer))
2615           (not gnus-newsgroup-prepared))
2616       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2617       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2618       (gnus-summary-mode group)
2619       (when gnus-carpal
2620         (gnus-carpal-setup-buffer 'summary))
2621       (unless gnus-single-article-buffer
2622         (make-local-variable 'gnus-article-buffer)
2623         (make-local-variable 'gnus-article-current)
2624         (make-local-variable 'gnus-original-article-buffer))
2625       (setq gnus-newsgroup-name group)
2626       t)))
2627
2628 (defun gnus-set-global-variables ()
2629   "Set the global equivalents of the buffer-local variables.
2630 They are set to the latest values they had.  These reflect the summary
2631 buffer that was in action when the last article was fetched."
2632   (when (eq major-mode 'gnus-summary-mode)
2633     (setq gnus-summary-buffer (current-buffer))
2634     (let ((name gnus-newsgroup-name)
2635           (marked gnus-newsgroup-marked)
2636           (unread gnus-newsgroup-unreads)
2637           (headers gnus-current-headers)
2638           (data gnus-newsgroup-data)
2639           (summary gnus-summary-buffer)
2640           (article-buffer gnus-article-buffer)
2641           (original gnus-original-article-buffer)
2642           (gac gnus-article-current)
2643           (reffed gnus-reffed-article-number)
2644           (score-file gnus-current-score-file)
2645           (default-charset gnus-newsgroup-charset)
2646           vlist)
2647       (let ((locals gnus-newsgroup-variables))
2648         (while locals
2649           (if (consp (car locals))
2650               (push (eval (caar locals)) vlist)
2651             (push (eval (car locals)) vlist))
2652           (setq locals (cdr locals)))
2653         (setq vlist (nreverse vlist)))
2654       (save-excursion
2655         (set-buffer gnus-group-buffer)
2656         (setq gnus-newsgroup-name name
2657               gnus-newsgroup-marked marked
2658               gnus-newsgroup-unreads unread
2659               gnus-current-headers headers
2660               gnus-newsgroup-data data
2661               gnus-article-current gac
2662               gnus-summary-buffer summary
2663               gnus-article-buffer article-buffer
2664               gnus-original-article-buffer original
2665               gnus-reffed-article-number reffed
2666               gnus-current-score-file score-file
2667               gnus-newsgroup-charset default-charset)
2668         (let ((locals gnus-newsgroup-variables))
2669           (while locals
2670             (if (consp (car locals))
2671                 (set (caar locals) (pop vlist))
2672               (set (car locals) (pop vlist)))
2673             (setq locals (cdr locals))))
2674         ;; The article buffer also has local variables.
2675         (when (gnus-buffer-live-p gnus-article-buffer)
2676           (set-buffer gnus-article-buffer)
2677           (setq gnus-summary-buffer summary))))))
2678
2679 (defun gnus-summary-article-unread-p (article)
2680   "Say whether ARTICLE is unread or not."
2681   (memq article gnus-newsgroup-unreads))
2682
2683 (defun gnus-summary-first-article-p (&optional article)
2684   "Return whether ARTICLE is the first article in the buffer."
2685   (if (not (setq article (or article (gnus-summary-article-number))))
2686       nil
2687     (eq article (caar gnus-newsgroup-data))))
2688
2689 (defun gnus-summary-last-article-p (&optional article)
2690   "Return whether ARTICLE is the last article in the buffer."
2691   (if (not (setq article (or article (gnus-summary-article-number))))
2692       ;; All non-existent numbers are the last article.  :-)
2693       t
2694     (not (cdr (gnus-data-find-list article)))))
2695
2696 (defun gnus-make-thread-indent-array ()
2697   (let ((n 200))
2698     (unless (and gnus-thread-indent-array
2699                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2700       (setq gnus-thread-indent-array (make-vector 201 "")
2701             gnus-thread-indent-array-level gnus-thread-indent-level)
2702       (while (>= n 0)
2703         (aset gnus-thread-indent-array n
2704               (make-string (* n gnus-thread-indent-level) ? ))
2705         (setq n (1- n))))))
2706
2707 (defun gnus-update-summary-mark-positions ()
2708   "Compute where the summary marks are to go."
2709   (save-excursion
2710     (when (gnus-buffer-exists-p gnus-summary-buffer)
2711       (set-buffer gnus-summary-buffer))
2712     (let ((gnus-replied-mark 129)
2713           (gnus-score-below-mark 130)
2714           (gnus-score-over-mark 130)
2715           (gnus-download-mark 131)
2716           (spec gnus-summary-line-format-spec)
2717           gnus-visual pos)
2718       (save-excursion
2719         (gnus-set-work-buffer)
2720         (let ((gnus-summary-line-format-spec spec)
2721               (gnus-newsgroup-downloadable '((0 . t))))
2722           (gnus-summary-insert-line
2723            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2724           (goto-char (point-min))
2725           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2726                                              (- (point) 2)))))
2727           (goto-char (point-min))
2728           (push (cons 'replied (and (search-forward "\201" nil t)
2729                                     (- (point) 2)))
2730                 pos)
2731           (goto-char (point-min))
2732           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2733                 pos)
2734           (goto-char (point-min))
2735           (push (cons 'download
2736                       (and (search-forward "\203" nil t) (- (point) 2)))
2737                 pos)))
2738       (setq gnus-summary-mark-positions pos))))
2739
2740 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2741   "Insert a dummy root in the summary buffer."
2742   (beginning-of-line)
2743   (gnus-add-text-properties
2744    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2745    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2746
2747 (defun gnus-summary-from-or-to-or-newsgroups (header)
2748   (let ((to (cdr (assq 'To (mail-header-extra header))))
2749         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2750         (mail-parse-charset gnus-newsgroup-charset)
2751         (mail-parse-ignored-charsets
2752          (save-excursion (set-buffer gnus-summary-buffer)
2753                          gnus-newsgroup-ignored-charsets)))
2754     (cond
2755      ((and to
2756            gnus-ignored-from-addresses
2757            (string-match gnus-ignored-from-addresses
2758                          (mail-header-from header)))
2759       (concat "-> "
2760               (or (car (funcall gnus-extract-address-components
2761                                 (funcall
2762                                  gnus-decode-encoded-word-function to)))
2763                   (funcall gnus-decode-encoded-word-function to))))
2764      ((and newsgroups
2765            gnus-ignored-from-addresses
2766            (string-match gnus-ignored-from-addresses
2767                          (mail-header-from header)))
2768       (concat "=> " newsgroups))
2769      (t
2770       (or (car (funcall gnus-extract-address-components
2771                         (mail-header-from header)))
2772           (mail-header-from header))))))
2773
2774 (defun gnus-summary-insert-line (gnus-tmp-header
2775                                  gnus-tmp-level gnus-tmp-current
2776                                  gnus-tmp-unread gnus-tmp-replied
2777                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2778                                  &optional gnus-tmp-dummy gnus-tmp-score
2779                                  gnus-tmp-process)
2780   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2781          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2782          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2783          (gnus-tmp-score-char
2784           (if (or (null gnus-summary-default-score)
2785                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2786                       gnus-summary-zcore-fuzz))
2787               ?  ;Whitespace
2788             (if (< gnus-tmp-score gnus-summary-default-score)
2789                 gnus-score-below-mark gnus-score-over-mark)))
2790          (gnus-tmp-replied
2791           (cond (gnus-tmp-process gnus-process-mark)
2792                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2793                  gnus-cached-mark)
2794                 (gnus-tmp-replied gnus-replied-mark)
2795                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2796                  gnus-saved-mark)
2797                 (t gnus-unread-mark)))
2798          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2799          (gnus-tmp-name
2800           (cond
2801            ((string-match "<[^>]+> *$" gnus-tmp-from)
2802             (let ((beg (match-beginning 0)))
2803               (or (and (string-match "^\".+\"" gnus-tmp-from)
2804                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2805                   (substring gnus-tmp-from 0 beg))))
2806            ((string-match "(.+)" gnus-tmp-from)
2807             (substring gnus-tmp-from
2808                        (1+ (match-beginning 0)) (1- (match-end 0))))
2809            (t gnus-tmp-from)))
2810          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2811          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2812          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2813          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2814          (buffer-read-only nil))
2815     (when (string= gnus-tmp-name "")
2816       (setq gnus-tmp-name gnus-tmp-from))
2817     (unless (numberp gnus-tmp-lines)
2818       (setq gnus-tmp-lines 0))
2819     (gnus-put-text-property
2820      (point)
2821      (progn (eval gnus-summary-line-format-spec) (point))
2822      'gnus-number gnus-tmp-number)
2823     (when (gnus-visual-p 'summary-highlight 'highlight)
2824       (forward-line -1)
2825       (gnus-run-hooks 'gnus-summary-update-hook)
2826       (forward-line 1))))
2827
2828 (defun gnus-summary-update-line (&optional dont-update)
2829   "Update summary line after change."
2830   (when (and gnus-summary-default-score
2831              (not gnus-summary-inhibit-highlight))
2832     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2833            (article (gnus-summary-article-number))
2834            (score (gnus-summary-article-score article)))
2835       (unless dont-update
2836         (if (and gnus-summary-mark-below
2837                  (< (gnus-summary-article-score)
2838                     gnus-summary-mark-below))
2839             ;; This article has a low score, so we mark it as read.
2840             (when (memq article gnus-newsgroup-unreads)
2841               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2842           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2843             ;; This article was previously marked as read on account
2844             ;; of a low score, but now it has risen, so we mark it as
2845             ;; unread.
2846             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2847         (gnus-summary-update-mark
2848          (if (or (null gnus-summary-default-score)
2849                  (<= (abs (- score gnus-summary-default-score))
2850                      gnus-summary-zcore-fuzz))
2851              ?  ;Whitespace
2852            (if (< score gnus-summary-default-score)
2853                gnus-score-below-mark gnus-score-over-mark))
2854          'score))
2855       ;; Do visual highlighting.
2856       (when (gnus-visual-p 'summary-highlight 'highlight)
2857         (gnus-run-hooks 'gnus-summary-update-hook)))))
2858
2859 (defvar gnus-tmp-new-adopts nil)
2860
2861 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2862   "Return the number of articles in THREAD.
2863 This may be 0 in some cases -- if none of the articles in
2864 the thread are to be displayed."
2865   (let* ((number
2866           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2867           (cond
2868            ((not (listp thread))
2869             1)
2870            ((and (consp thread) (cdr thread))
2871             (apply
2872              '+ 1 (mapcar
2873                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2874            ((null thread)
2875             1)
2876            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2877             1)
2878            (t 0))))
2879     (when (and level (zerop level) gnus-tmp-new-adopts)
2880       (incf number
2881             (apply '+ (mapcar
2882                        'gnus-summary-number-of-articles-in-thread
2883                        gnus-tmp-new-adopts))))
2884     (if char
2885         (if (> number 1) gnus-not-empty-thread-mark
2886           gnus-empty-thread-mark)
2887       number)))
2888
2889 (defun gnus-summary-set-local-parameters (group)
2890   "Go through the local params of GROUP and set all variable specs in that list."
2891   (let ((params (gnus-group-find-parameter group))
2892         elem)
2893     (while params
2894       (setq elem (car params)
2895             params (cdr params))
2896       (and (consp elem)                 ; Has to be a cons.
2897            (consp (cdr elem))           ; The cdr has to be a list.
2898            (symbolp (car elem))         ; Has to be a symbol in there.
2899            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2900            (ignore-errors               ; So we set it.
2901              (make-local-variable (car elem))
2902              (set (car elem) (eval (nth 1 elem))))))))
2903
2904 (defun gnus-summary-read-group (group &optional show-all no-article
2905                                       kill-buffer no-display backward
2906                                       select-articles)
2907   "Start reading news in newsgroup GROUP.
2908 If SHOW-ALL is non-nil, already read articles are also listed.
2909 If NO-ARTICLE is non-nil, no article is selected initially.
2910 If NO-DISPLAY, don't generate a summary buffer."
2911   (let (result)
2912     (while (and group
2913                 (null (setq result
2914                             (let ((gnus-auto-select-next nil))
2915                               (or (gnus-summary-read-group-1
2916                                    group show-all no-article
2917                                    kill-buffer no-display
2918                                    select-articles)
2919                                   (setq show-all nil
2920                                         select-articles nil)))))
2921                 (eq gnus-auto-select-next 'quietly))
2922       (set-buffer gnus-group-buffer)
2923       ;; The entry function called above goes to the next
2924       ;; group automatically, so we go two groups back
2925       ;; if we are searching for the previous group.
2926       (when backward
2927         (gnus-group-prev-unread-group 2))
2928       (if (not (equal group (gnus-group-group-name)))
2929           (setq group (gnus-group-group-name))
2930         (setq group nil)))
2931     result))
2932
2933 (defun gnus-summary-read-group-1 (group show-all no-article
2934                                         kill-buffer no-display
2935                                         &optional select-articles)
2936   ;; Killed foreign groups can't be entered.
2937   (when (and (not (gnus-group-native-p group))
2938              (not (gnus-gethash group gnus-newsrc-hashtb)))
2939     (error "Dead non-native groups can't be entered"))
2940   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2941   (let* ((new-group (gnus-summary-setup-buffer group))
2942          (quit-config (gnus-group-quit-config group))
2943          (did-select (and new-group (gnus-select-newsgroup
2944                                      group show-all select-articles))))
2945     (cond
2946      ;; This summary buffer exists already, so we just select it.
2947      ((not new-group)
2948       (gnus-set-global-variables)
2949       (when kill-buffer
2950         (gnus-kill-or-deaden-summary kill-buffer))
2951       (gnus-configure-windows 'summary 'force)
2952       (gnus-set-mode-line 'summary)
2953       (gnus-summary-position-point)
2954       (message "")
2955       t)
2956      ;; We couldn't select this group.
2957      ((null did-select)
2958       (when (and (eq major-mode 'gnus-summary-mode)
2959                  (not (equal (current-buffer) kill-buffer)))
2960         (kill-buffer (current-buffer))
2961         (if (not quit-config)
2962             (progn
2963               ;; Update the info -- marks might need to be removed,
2964               ;; for instance.
2965               (gnus-summary-update-info)
2966               (set-buffer gnus-group-buffer)
2967               (gnus-group-jump-to-group group)
2968               (gnus-group-next-unread-group 1))
2969           (gnus-handle-ephemeral-exit quit-config)))
2970       (gnus-message 3 "Can't select group")
2971       nil)
2972      ;; The user did a `C-g' while prompting for number of articles,
2973      ;; so we exit this group.
2974      ((eq did-select 'quit)
2975       (and (eq major-mode 'gnus-summary-mode)
2976            (not (equal (current-buffer) kill-buffer))
2977            (kill-buffer (current-buffer)))
2978       (when kill-buffer
2979         (gnus-kill-or-deaden-summary kill-buffer))
2980       (if (not quit-config)
2981           (progn
2982             (set-buffer gnus-group-buffer)
2983             (gnus-group-jump-to-group group)
2984             (gnus-group-next-unread-group 1)
2985             (gnus-configure-windows 'group 'force))
2986         (gnus-handle-ephemeral-exit quit-config))
2987       ;; Finally signal the quit.
2988       (signal 'quit nil))
2989      ;; The group was successfully selected.
2990      (t
2991       (gnus-set-global-variables)
2992       ;; Save the active value in effect when the group was entered.
2993       (setq gnus-newsgroup-active
2994             (gnus-copy-sequence
2995              (gnus-active gnus-newsgroup-name)))
2996       ;; You can change the summary buffer in some way with this hook.
2997       (gnus-run-hooks 'gnus-select-group-hook)
2998       ;; Set any local variables in the group parameters.
2999       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3000       (gnus-update-format-specifications
3001        nil 'summary 'summary-mode 'summary-dummy)
3002       (gnus-update-summary-mark-positions)
3003       ;; Do score processing.
3004       (when gnus-use-scoring
3005         (gnus-possibly-score-headers))
3006       ;; Check whether to fill in the gaps in the threads.
3007       (when gnus-build-sparse-threads
3008         (gnus-build-sparse-threads))
3009       ;; Find the initial limit.
3010       (if gnus-show-threads
3011           (if show-all
3012               (let ((gnus-newsgroup-dormant nil))
3013                 (gnus-summary-initial-limit show-all))
3014             (gnus-summary-initial-limit show-all))
3015         ;; When untreaded, all articles are always shown.
3016         (setq gnus-newsgroup-limit
3017               (mapcar
3018                (lambda (header) (mail-header-number header))
3019                gnus-newsgroup-headers)))
3020       ;; Generate the summary buffer.
3021       (unless no-display
3022         (gnus-summary-prepare))
3023       (when gnus-use-trees
3024         (gnus-tree-open group)
3025         (setq gnus-summary-highlight-line-function
3026               'gnus-tree-highlight-article))
3027       ;; If the summary buffer is empty, but there are some low-scored
3028       ;; articles or some excluded dormants, we include these in the
3029       ;; buffer.
3030       (when (and (zerop (buffer-size))
3031                  (not no-display))
3032         (cond (gnus-newsgroup-dormant
3033                (gnus-summary-limit-include-dormant))
3034               ((and gnus-newsgroup-scored show-all)
3035                (gnus-summary-limit-include-expunged t))))
3036       ;; Function `gnus-apply-kill-file' must be called in this hook.
3037       (gnus-run-hooks 'gnus-apply-kill-hook)
3038       (if (and (zerop (buffer-size))
3039                (not no-display))
3040           (progn
3041             ;; This newsgroup is empty.
3042             (gnus-summary-catchup-and-exit nil t)
3043             (gnus-message 6 "No unread news")
3044             (when kill-buffer
3045               (gnus-kill-or-deaden-summary kill-buffer))
3046             ;; Return nil from this function.
3047             nil)
3048         ;; Hide conversation thread subtrees.  We cannot do this in
3049         ;; gnus-summary-prepare-hook since kill processing may not
3050         ;; work with hidden articles.
3051         (and gnus-show-threads
3052              gnus-thread-hide-subtree
3053              (gnus-summary-hide-all-threads))
3054         (when kill-buffer
3055           (gnus-kill-or-deaden-summary kill-buffer))
3056         ;; Show first unread article if requested.
3057         (if (and (not no-article)
3058                  (not no-display)
3059                  gnus-newsgroup-unreads
3060                  gnus-auto-select-first)
3061             (progn
3062               (gnus-configure-windows 'summary)
3063               (cond
3064                ((eq gnus-auto-select-first 'best)
3065                 (gnus-summary-best-unread-article))
3066                ((eq gnus-auto-select-first t)
3067                 (gnus-summary-first-unread-article))
3068                ((gnus-functionp gnus-auto-select-first)
3069                 (funcall gnus-auto-select-first))))
3070           ;; Don't select any articles, just move point to the first
3071           ;; article in the group.
3072           (goto-char (point-min))
3073           (gnus-summary-position-point)
3074           (gnus-configure-windows 'summary 'force)
3075           (gnus-set-mode-line 'summary))
3076         (when (get-buffer-window gnus-group-buffer t)
3077           ;; Gotta use windows, because recenter does weird stuff if
3078           ;; the current buffer ain't the displayed window.
3079           (let ((owin (selected-window)))
3080             (select-window (get-buffer-window gnus-group-buffer t))
3081             (when (gnus-group-goto-group group)
3082               (recenter))
3083             (select-window owin)))
3084         ;; Mark this buffer as "prepared".
3085         (setq gnus-newsgroup-prepared t)
3086         (gnus-run-hooks 'gnus-summary-prepared-hook)
3087         t)))))
3088
3089 (defun gnus-summary-prepare ()
3090   "Generate the summary buffer."
3091   (interactive)
3092   (let ((buffer-read-only nil))
3093     (erase-buffer)
3094     (setq gnus-newsgroup-data nil
3095           gnus-newsgroup-data-reverse nil)
3096     (gnus-run-hooks 'gnus-summary-generate-hook)
3097     ;; Generate the buffer, either with threads or without.
3098     (when gnus-newsgroup-headers
3099       (gnus-summary-prepare-threads
3100        (if gnus-show-threads
3101            (gnus-sort-gathered-threads
3102             (funcall gnus-summary-thread-gathering-function
3103                      (gnus-sort-threads
3104                       (gnus-cut-threads (gnus-make-threads)))))
3105          ;; Unthreaded display.
3106          (gnus-sort-articles gnus-newsgroup-headers))))
3107     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3108     ;; Call hooks for modifying summary buffer.
3109     (goto-char (point-min))
3110     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3111
3112 (defsubst gnus-general-simplify-subject (subject)
3113   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3114   (setq subject
3115         (cond
3116          ;; Truncate the subject.
3117          (gnus-simplify-subject-functions
3118           (gnus-map-function gnus-simplify-subject-functions subject))
3119          ((numberp gnus-summary-gather-subject-limit)
3120           (setq subject (gnus-simplify-subject-re subject))
3121           (if (> (length subject) gnus-summary-gather-subject-limit)
3122               (substring subject 0 gnus-summary-gather-subject-limit)
3123             subject))
3124          ;; Fuzzily simplify it.
3125          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3126           (gnus-simplify-subject-fuzzy subject))
3127          ;; Just remove the leading "Re:".
3128          (t
3129           (gnus-simplify-subject-re subject))))
3130
3131   (if (and gnus-summary-gather-exclude-subject
3132            (string-match gnus-summary-gather-exclude-subject subject))
3133       nil                               ; This article shouldn't be gathered
3134     subject))
3135
3136 (defun gnus-summary-simplify-subject-query ()
3137   "Query where the respool algorithm would put this article."
3138   (interactive)
3139   (gnus-summary-select-article)
3140   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3141
3142 (defun gnus-gather-threads-by-subject (threads)
3143   "Gather threads by looking at Subject headers."
3144   (if (not gnus-summary-make-false-root)
3145       threads
3146     (let ((hashtb (gnus-make-hashtable 1024))
3147           (prev threads)
3148           (result threads)
3149           subject hthread whole-subject)
3150       (while threads
3151         (setq subject (gnus-general-simplify-subject
3152                        (setq whole-subject (mail-header-subject
3153                                             (caar threads)))))
3154         (when subject
3155           (if (setq hthread (gnus-gethash subject hashtb))
3156               (progn
3157                 ;; We enter a dummy root into the thread, if we
3158                 ;; haven't done that already.
3159                 (unless (stringp (caar hthread))
3160                   (setcar hthread (list whole-subject (car hthread))))
3161                 ;; We add this new gathered thread to this gathered
3162                 ;; thread.
3163                 (setcdr (car hthread)
3164                         (nconc (cdar hthread) (list (car threads))))
3165                 ;; Remove it from the list of threads.
3166                 (setcdr prev (cdr threads))
3167                 (setq threads prev))
3168             ;; Enter this thread into the hash table.
3169             (gnus-sethash subject threads hashtb)))
3170         (setq prev threads)
3171         (setq threads (cdr threads)))
3172       result)))
3173
3174 (defun gnus-gather-threads-by-references (threads)
3175   "Gather threads by looking at References headers."
3176   (let ((idhashtb (gnus-make-hashtable 1024))
3177         (thhashtb (gnus-make-hashtable 1024))
3178         (prev threads)
3179         (result threads)
3180         ids references id gthread gid entered ref)
3181     (while threads
3182       (when (setq references (mail-header-references (caar threads)))
3183         (setq id (mail-header-id (caar threads))
3184               ids (gnus-split-references references)
3185               entered nil)
3186         (while (setq ref (pop ids))
3187           (setq ids (delete ref ids))
3188           (if (not (setq gid (gnus-gethash ref idhashtb)))
3189               (progn
3190                 (gnus-sethash ref id idhashtb)
3191                 (gnus-sethash id threads thhashtb))
3192             (setq gthread (gnus-gethash gid thhashtb))
3193             (unless entered
3194               ;; We enter a dummy root into the thread, if we
3195               ;; haven't done that already.
3196               (unless (stringp (caar gthread))
3197                 (setcar gthread (list (mail-header-subject (caar gthread))
3198                                       (car gthread))))
3199               ;; We add this new gathered thread to this gathered
3200               ;; thread.
3201               (setcdr (car gthread)
3202                       (nconc (cdar gthread) (list (car threads)))))
3203             ;; Add it into the thread hash table.
3204             (gnus-sethash id gthread thhashtb)
3205             (setq entered t)
3206             ;; Remove it from the list of threads.
3207             (setcdr prev (cdr threads))
3208             (setq threads prev))))
3209       (setq prev threads)
3210       (setq threads (cdr threads)))
3211     result))
3212
3213 (defun gnus-sort-gathered-threads (threads)
3214   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3215   (let ((result threads))
3216     (while threads
3217       (when (stringp (caar threads))
3218         (setcdr (car threads)
3219                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3220       (setq threads (cdr threads)))
3221     result))
3222
3223 (defun gnus-thread-loop-p (root thread)
3224   "Say whether ROOT is in THREAD."
3225   (let ((stack (list thread))
3226         (infloop 0)
3227         th)
3228     (while (setq thread (pop stack))
3229       (setq th (cdr thread))
3230       (while (and th
3231                   (not (eq (caar th) root)))
3232         (pop th))
3233       (if th
3234           ;; We have found a loop.
3235           (let (ref-dep)
3236             (setcdr thread (delq (car th) (cdr thread)))
3237             (if (boundp (setq ref-dep (intern "none"
3238                                               gnus-newsgroup-dependencies)))
3239                 (setcdr (symbol-value ref-dep)
3240                         (nconc (cdr (symbol-value ref-dep))
3241                                (list (car th))))
3242               (set ref-dep (list nil (car th))))
3243             (setq infloop 1
3244                   stack nil))
3245         ;; Push all the subthreads onto the stack.
3246         (push (cdr thread) stack)))
3247     infloop))
3248
3249 (defun gnus-make-threads ()
3250   "Go through the dependency hashtb and find the roots.  Return all threads."
3251   (let (threads)
3252     (while (catch 'infloop
3253              (mapatoms
3254               (lambda (refs)
3255                 ;; Deal with self-referencing References loops.
3256                 (when (and (car (symbol-value refs))
3257                            (not (zerop
3258                                  (apply
3259                                   '+
3260                                   (mapcar
3261                                    (lambda (thread)
3262                                      (gnus-thread-loop-p
3263                                       (car (symbol-value refs)) thread))
3264                                    (cdr (symbol-value refs)))))))
3265                   (setq threads nil)
3266                   (throw 'infloop t))
3267                 (unless (car (symbol-value refs))
3268                   ;; These threads do not refer back to any other articles,
3269                   ;; so they're roots.
3270                   (setq threads (append (cdr (symbol-value refs)) threads))))
3271               gnus-newsgroup-dependencies)))
3272     threads))
3273
3274 ;; Build the thread tree.
3275 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3276   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3277
3278 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3279 if it was already present.
3280
3281 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3282 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3283 Message-IDs will be renamed be renamed to a unique Message-ID before
3284 being entered.
3285
3286 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3287   (let* ((id (mail-header-id header))
3288          (id-dep (and id (intern id dependencies)))
3289          ref ref-dep ref-header)
3290     ;; Enter this `header' in the `dependencies' table.
3291     (cond
3292      ((not id-dep)
3293       (setq header nil))
3294      ;; The first two cases do the normal part: enter a new `header'
3295      ;; in the `dependencies' table.
3296      ((not (boundp id-dep))
3297       (set id-dep (list header)))
3298      ((null (car (symbol-value id-dep)))
3299       (setcar (symbol-value id-dep) header))
3300
3301      ;; From here the `header' was already present in the
3302      ;; `dependencies' table.
3303      (force-new
3304       ;; Overrides an existing entry;
3305       ;; just set the header part of the entry.
3306       (setcar (symbol-value id-dep) header))
3307
3308      ;; Renames the existing `header' to a unique Message-ID.
3309      ((not gnus-summary-ignore-duplicates)
3310       ;; An article with this Message-ID has already been seen.
3311       ;; We rename the Message-ID.
3312       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3313            (list header))
3314       (mail-header-set-id header id))
3315
3316      ;; The last case ignores an existing entry, except it adds any
3317      ;; additional Xrefs (in case the two articles came from different
3318      ;; servers.
3319      ;; Also sets `header' to `nil' meaning that the `dependencies'
3320      ;; table was *not* modified.
3321      (t
3322       (mail-header-set-xref
3323        (car (symbol-value id-dep))
3324        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3325                    "")
3326                (or (mail-header-xref header) "")))
3327       (setq header nil)))
3328
3329     (when header
3330       ;; First check if that we are not creating a References loop.
3331       (setq ref (gnus-parent-id (mail-header-references header)))
3332       (while (and ref
3333                   (setq ref-dep (intern-soft ref dependencies))
3334                   (boundp ref-dep)
3335                   (setq ref-header (car (symbol-value ref-dep))))
3336         (if (string= id ref)
3337             ;; Yuk!  This is a reference loop.  Make the article be a
3338             ;; root article.
3339             (progn
3340               (mail-header-set-references (car (symbol-value id-dep)) "none")
3341               (setq ref nil))
3342           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3343       (setq ref (gnus-parent-id (mail-header-references header)))
3344       (setq ref-dep (intern (or ref "none") dependencies))
3345       (if (boundp ref-dep)
3346           (setcdr (symbol-value ref-dep)
3347                   (nconc (cdr (symbol-value ref-dep))
3348                          (list (symbol-value id-dep))))
3349         (set ref-dep (list nil (symbol-value id-dep)))))
3350     header))
3351
3352 (defun gnus-build-sparse-threads ()
3353   (let ((headers gnus-newsgroup-headers)
3354         (mail-parse-charset gnus-newsgroup-charset)
3355         (gnus-summary-ignore-duplicates t)
3356         header references generation relations
3357         subject child end new-child date)
3358     ;; First we create an alist of generations/relations, where
3359     ;; generations is how much we trust the relation, and the relation
3360     ;; is parent/child.
3361     (gnus-message 7 "Making sparse threads...")
3362     (save-excursion
3363       (nnheader-set-temp-buffer " *gnus sparse threads*")
3364       (while (setq header (pop headers))
3365         (when (and (setq references (mail-header-references header))
3366                    (not (string= references "")))
3367           (insert references)
3368           (setq child (mail-header-id header)
3369                 subject (mail-header-subject header)
3370                 date (mail-header-date header)
3371                 generation 0)
3372           (while (search-backward ">" nil t)
3373             (setq end (1+ (point)))
3374             (when (search-backward "<" nil t)
3375               (setq new-child (buffer-substring (point) end))
3376               (push (list (incf generation)
3377                           child (setq child new-child)
3378                           subject date)
3379                     relations)))
3380           (when child
3381             (push (list (1+ generation) child nil subject) relations))
3382           (erase-buffer)))
3383       (kill-buffer (current-buffer)))
3384     ;; Sort over trustworthiness.
3385     (mapcar
3386      (lambda (relation)
3387        (when (gnus-dependencies-add-header
3388               (make-full-mail-header
3389                gnus-reffed-article-number
3390                (nth 3 relation) "" (or (nth 4 relation) "")
3391                (nth 1 relation)
3392                (or (nth 2 relation) "") 0 0 "")
3393               gnus-newsgroup-dependencies nil)
3394          (push gnus-reffed-article-number gnus-newsgroup-limit)
3395          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3396          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3397                gnus-newsgroup-reads)
3398          (decf gnus-reffed-article-number)))
3399      (sort relations 'car-less-than-car))
3400     (gnus-message 7 "Making sparse threads...done")))
3401
3402 (defun gnus-build-old-threads ()
3403   ;; Look at all the articles that refer back to old articles, and
3404   ;; fetch the headers for the articles that aren't there.  This will
3405   ;; build complete threads - if the roots haven't been expired by the
3406   ;; server, that is.
3407   (let ((mail-parse-charset gnus-newsgroup-charset)
3408         id heads)
3409     (mapatoms
3410      (lambda (refs)
3411        (when (not (car (symbol-value refs)))
3412          (setq heads (cdr (symbol-value refs)))
3413          (while heads
3414            (if (memq (mail-header-number (caar heads))
3415                      gnus-newsgroup-dormant)
3416                (setq heads (cdr heads))
3417              (setq id (symbol-name refs))
3418              (while (and (setq id (gnus-build-get-header id))
3419                          (not (car (gnus-id-to-thread id)))))
3420              (setq heads nil)))))
3421      gnus-newsgroup-dependencies)))
3422
3423 ;; This function has to be called with point after the article number
3424 ;; on the beginning of the line.
3425 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3426   (let ((eol (gnus-point-at-eol))
3427         (buffer (current-buffer))
3428         header)
3429
3430     ;; overview: [num subject from date id refs chars lines misc]
3431     (unwind-protect
3432         (progn
3433           (narrow-to-region (point) eol)
3434           (unless (eobp)
3435             (forward-char))
3436
3437           (setq header
3438                 (make-full-mail-header
3439                  number                 ; number
3440                  (funcall gnus-decode-encoded-word-function
3441                           (nnheader-nov-field)) ; subject
3442                  (funcall gnus-decode-encoded-word-function
3443                           (nnheader-nov-field)) ; from
3444                  (nnheader-nov-field)   ; date
3445                  (nnheader-nov-read-message-id) ; id
3446                  (nnheader-nov-field)   ; refs
3447                  (nnheader-nov-read-integer) ; chars
3448                  (nnheader-nov-read-integer) ; lines
3449                  (unless (eobp)
3450                    (if (looking-at "Xref: ")
3451                        (goto-char (match-end 0)))
3452                    (nnheader-nov-field)) ; Xref
3453                  (nnheader-nov-parse-extra)))) ; extra
3454
3455       (widen))
3456
3457     (when gnus-alter-header-function
3458       (funcall gnus-alter-header-function header))
3459     (gnus-dependencies-add-header header dependencies force-new)))
3460
3461 (defun gnus-build-get-header (id)
3462   "Look through the buffer of NOV lines and find the header to ID.
3463 Enter this line into the dependencies hash table, and return
3464 the id of the parent article (if any)."
3465   (let ((deps gnus-newsgroup-dependencies)
3466         found header)
3467     (prog1
3468         (save-excursion
3469           (set-buffer nntp-server-buffer)
3470           (let ((case-fold-search nil))
3471             (goto-char (point-min))
3472             (while (and (not found)
3473                         (search-forward id nil t))
3474               (beginning-of-line)
3475               (setq found (looking-at
3476                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3477                                    (regexp-quote id))))
3478               (or found (beginning-of-line 2)))
3479             (when found
3480               (beginning-of-line)
3481               (and
3482                (setq header (gnus-nov-parse-line
3483                              (read (current-buffer)) deps))
3484                (gnus-parent-id (mail-header-references header))))))
3485       (when header
3486         (let ((number (mail-header-number header)))
3487           (push number gnus-newsgroup-limit)
3488           (push header gnus-newsgroup-headers)
3489           (if (memq number gnus-newsgroup-unselected)
3490               (progn
3491                 (push number gnus-newsgroup-unreads)
3492                 (setq gnus-newsgroup-unselected
3493                       (delq number gnus-newsgroup-unselected)))
3494             (push number gnus-newsgroup-ancient)))))))
3495
3496 (defun gnus-build-all-threads ()
3497   "Read all the headers."
3498   (let ((gnus-summary-ignore-duplicates t)
3499         (mail-parse-charset gnus-newsgroup-charset)
3500         (dependencies gnus-newsgroup-dependencies)
3501         header article)
3502     (save-excursion
3503       (set-buffer nntp-server-buffer)
3504       (let ((case-fold-search nil))
3505         (goto-char (point-min))
3506         (while (not (eobp))
3507           (ignore-errors
3508             (setq article (read (current-buffer))
3509                   header (gnus-nov-parse-line article dependencies)))
3510           (when header
3511             (save-excursion
3512               (set-buffer gnus-summary-buffer)
3513               (push header gnus-newsgroup-headers)
3514               (if (memq (setq article (mail-header-number header))
3515                         gnus-newsgroup-unselected)
3516                   (progn
3517                     (push article gnus-newsgroup-unreads)
3518                     (setq gnus-newsgroup-unselected
3519                           (delq article gnus-newsgroup-unselected)))
3520                 (push article gnus-newsgroup-ancient)))
3521             (forward-line 1)))))))
3522
3523 (defun gnus-summary-update-article-line (article header)
3524   "Update the line for ARTICLE using HEADERS."
3525   (let* ((id (mail-header-id header))
3526          (thread (gnus-id-to-thread id)))
3527     (unless thread
3528       (error "Article in no thread"))
3529     ;; Update the thread.
3530     (setcar thread header)
3531     (gnus-summary-goto-subject article)
3532     (let* ((datal (gnus-data-find-list article))
3533            (data (car datal))
3534            (length (when (cdr datal)
3535                      (- (gnus-data-pos data)
3536                         (gnus-data-pos (cadr datal)))))
3537            (buffer-read-only nil)
3538            (level (gnus-summary-thread-level)))
3539       (gnus-delete-line)
3540       (gnus-summary-insert-line
3541        header level nil (gnus-article-mark article)
3542        (memq article gnus-newsgroup-replied)
3543        (memq article gnus-newsgroup-expirable)
3544        ;; Only insert the Subject string when it's different
3545        ;; from the previous Subject string.
3546        (if (and
3547             gnus-show-threads
3548             (gnus-subject-equal
3549              (condition-case ()
3550                  (mail-header-subject
3551                   (gnus-data-header
3552                    (cadr
3553                     (gnus-data-find-list
3554                      article
3555                      (gnus-data-list t)))))
3556                ;; Error on the side of excessive subjects.
3557                (error ""))
3558              (mail-header-subject header)))
3559            ""
3560          (mail-header-subject header))
3561        nil (cdr (assq article gnus-newsgroup-scored))
3562        (memq article gnus-newsgroup-processable))
3563       (when length
3564         (gnus-data-update-list
3565          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3566
3567 (defun gnus-summary-update-article (article &optional iheader)
3568   "Update ARTICLE in the summary buffer."
3569   (set-buffer gnus-summary-buffer)
3570   (let* ((header (gnus-summary-article-header article))
3571          (id (mail-header-id header))
3572          (data (gnus-data-find article))
3573          (thread (gnus-id-to-thread id))
3574          (references (mail-header-references header))
3575          (parent
3576           (gnus-id-to-thread
3577            (or (gnus-parent-id
3578                 (when (and references
3579                            (not (equal "" references)))
3580                   references))
3581                "none")))
3582          (buffer-read-only nil)
3583          (old (car thread)))
3584     (when thread
3585       (unless iheader
3586         (setcar thread nil)
3587         (when parent
3588           (delq thread parent)))
3589       (if (gnus-summary-insert-subject id header)
3590           ;; Set the (possibly) new article number in the data structure.
3591           (gnus-data-set-number data (gnus-id-to-article id))
3592         (setcar thread old)
3593         nil))))
3594
3595 (defun gnus-rebuild-thread (id &optional line)
3596   "Rebuild the thread containing ID.
3597 If LINE, insert the rebuilt thread starting on line LINE."
3598   (let ((buffer-read-only nil)
3599         old-pos current thread data)
3600     (if (not gnus-show-threads)
3601         (setq thread (list (car (gnus-id-to-thread id))))
3602       ;; Get the thread this article is part of.
3603       (setq thread (gnus-remove-thread id)))
3604     (setq old-pos (gnus-point-at-bol))
3605     (setq current (save-excursion
3606                     (and (zerop (forward-line -1))
3607                          (gnus-summary-article-number))))
3608     ;; If this is a gathered thread, we have to go some re-gathering.
3609     (when (stringp (car thread))
3610       (let ((subject (car thread))
3611             roots thr)
3612         (setq thread (cdr thread))
3613         (while thread
3614           (unless (memq (setq thr (gnus-id-to-thread
3615                                    (gnus-root-id
3616                                     (mail-header-id (caar thread)))))
3617                         roots)
3618             (push thr roots))
3619           (setq thread (cdr thread)))
3620         ;; We now have all (unique) roots.
3621         (if (= (length roots) 1)
3622             ;; All the loose roots are now one solid root.
3623             (setq thread (car roots))
3624           (setq thread (cons subject (gnus-sort-threads roots))))))
3625     (let (threads)
3626       ;; We then insert this thread into the summary buffer.
3627       (when line
3628         (goto-char (point-min))
3629         (forward-line (1- line)))
3630       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3631         (if gnus-show-threads
3632             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3633           (gnus-summary-prepare-unthreaded thread))
3634         (setq data (nreverse gnus-newsgroup-data))
3635         (setq threads gnus-newsgroup-threads))
3636       ;; We splice the new data into the data structure.
3637       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3638       ;;!!! then we want to insert at the beginning of the buffer.
3639       ;;!!! That happens to be true with Gnus now, but that may
3640       ;;!!! change in the future.  Perhaps.
3641       (gnus-data-enter-list
3642        (if line nil current) data (- (point) old-pos))
3643       (setq gnus-newsgroup-threads
3644             (nconc threads gnus-newsgroup-threads))
3645       (gnus-data-compute-positions))))
3646
3647 (defun gnus-number-to-header (number)
3648   "Return the header for article NUMBER."
3649   (let ((headers gnus-newsgroup-headers))
3650     (while (and headers
3651                 (not (= number (mail-header-number (car headers)))))
3652       (pop headers))
3653     (when headers
3654       (car headers))))
3655
3656 (defun gnus-parent-headers (in-headers &optional generation)
3657   "Return the headers of the GENERATIONeth parent of HEADERS."
3658   (unless generation
3659     (setq generation 1))
3660   (let ((parent t)
3661         (headers in-headers)
3662         references)
3663     (while (and parent
3664                 (not (zerop generation))
3665                 (setq references (mail-header-references headers)))
3666       (setq headers (if (and references
3667                              (setq parent (gnus-parent-id references)))
3668                         (car (gnus-id-to-thread parent))
3669                       nil))
3670       (decf generation))
3671     (and (not (eq headers in-headers))
3672          headers)))
3673
3674 (defun gnus-id-to-thread (id)
3675   "Return the (sub-)thread where ID appears."
3676   (gnus-gethash id gnus-newsgroup-dependencies))
3677
3678 (defun gnus-id-to-article (id)
3679   "Return the article number of ID."
3680   (let ((thread (gnus-id-to-thread id)))
3681     (when (and thread
3682                (car thread))
3683       (mail-header-number (car thread)))))
3684
3685 (defun gnus-id-to-header (id)
3686   "Return the article headers of ID."
3687   (car (gnus-id-to-thread id)))
3688
3689 (defun gnus-article-displayed-root-p (article)
3690   "Say whether ARTICLE is a root(ish) article."
3691   (let ((level (gnus-summary-thread-level article))
3692         (refs (mail-header-references  (gnus-summary-article-header article)))
3693         particle)
3694     (cond
3695      ((null level) nil)
3696      ((zerop level) t)
3697      ((null refs) t)
3698      ((null (gnus-parent-id refs)) t)
3699      ((and (= 1 level)
3700            (null (setq particle (gnus-id-to-article
3701                                  (gnus-parent-id refs))))
3702            (null (gnus-summary-thread-level particle)))))))
3703
3704 (defun gnus-root-id (id)
3705   "Return the id of the root of the thread where ID appears."
3706   (let (last-id prev)
3707     (while (and id (setq prev (car (gnus-id-to-thread id))))
3708       (setq last-id id
3709             id (gnus-parent-id (mail-header-references prev))))
3710     last-id))
3711
3712 (defun gnus-articles-in-thread (thread)
3713   "Return the list of articles in THREAD."
3714   (cons (mail-header-number (car thread))
3715         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3716
3717 (defun gnus-remove-thread (id &optional dont-remove)
3718   "Remove the thread that has ID in it."
3719   (let (headers thread last-id)
3720     ;; First go up in this thread until we find the root.
3721     (setq last-id (gnus-root-id id)
3722           headers (message-flatten-list (gnus-id-to-thread last-id)))
3723     ;; We have now found the real root of this thread.  It might have
3724     ;; been gathered into some loose thread, so we have to search
3725     ;; through the threads to find the thread we wanted.
3726     (let ((threads gnus-newsgroup-threads)
3727           sub)
3728       (while threads
3729         (setq sub (car threads))
3730         (if (stringp (car sub))
3731             ;; This is a gathered thread, so we look at the roots
3732             ;; below it to find whether this article is in this
3733             ;; gathered root.
3734             (progn
3735               (setq sub (cdr sub))
3736               (while sub
3737                 (when (member (caar sub) headers)
3738                   (setq thread (car threads)
3739                         threads nil
3740                         sub nil))
3741                 (setq sub (cdr sub))))
3742           ;; It's an ordinary thread, so we check it.
3743           (when (eq (car sub) (car headers))
3744             (setq thread sub
3745                   threads nil)))
3746         (setq threads (cdr threads)))
3747       ;; If this article is in no thread, then it's a root.
3748       (if thread
3749           (unless dont-remove
3750             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3751         (setq thread (gnus-id-to-thread last-id)))
3752       (when thread
3753         (prog1
3754             thread                      ; We return this thread.
3755           (unless dont-remove
3756             (if (stringp (car thread))
3757                 (progn
3758                   ;; If we use dummy roots, then we have to remove the
3759                   ;; dummy root as well.
3760                   (when (eq gnus-summary-make-false-root 'dummy)
3761                     ;; We go to the dummy root by going to
3762                     ;; the first sub-"thread", and then one line up.
3763                     (gnus-summary-goto-article
3764                      (mail-header-number (caadr thread)))
3765                     (forward-line -1)
3766                     (gnus-delete-line)
3767                     (gnus-data-compute-positions))
3768                   (setq thread (cdr thread))
3769                   (while thread
3770                     (gnus-remove-thread-1 (car thread))
3771                     (setq thread (cdr thread))))
3772               (gnus-remove-thread-1 thread))))))))
3773
3774 (defun gnus-remove-thread-1 (thread)
3775   "Remove the thread THREAD recursively."
3776   (let ((number (mail-header-number (pop thread)))
3777         d)
3778     (setq thread (reverse thread))
3779     (while thread
3780       (gnus-remove-thread-1 (pop thread)))
3781     (when (setq d (gnus-data-find number))
3782       (goto-char (gnus-data-pos d))
3783       (gnus-summary-show-thread)
3784       (gnus-data-remove
3785        number
3786        (- (gnus-point-at-bol)
3787           (prog1
3788               (1+ (gnus-point-at-eol))
3789             (gnus-delete-line)))))))
3790
3791 (defun gnus-sort-threads-1 (threads func)
3792   (sort (mapcar (lambda (thread)
3793                   (cons (car thread)
3794                         (and (cdr thread)
3795                              (gnus-sort-threads-1 (cdr thread) func))))
3796                 threads) func))
3797
3798 (defun gnus-sort-threads (threads)
3799   "Sort THREADS."
3800   (if (not gnus-thread-sort-functions)
3801       threads
3802     (gnus-message 8 "Sorting threads...")
3803     (prog1
3804         (gnus-sort-threads-1 
3805          threads 
3806          (gnus-make-sort-function gnus-thread-sort-functions))
3807       (gnus-message 8 "Sorting threads...done"))))
3808
3809 (defun gnus-sort-articles (articles)
3810   "Sort ARTICLES."
3811   (when gnus-article-sort-functions
3812     (gnus-message 7 "Sorting articles...")
3813     (prog1
3814         (setq gnus-newsgroup-headers
3815               (sort articles (gnus-make-sort-function
3816                               gnus-article-sort-functions)))
3817       (gnus-message 7 "Sorting articles...done"))))
3818
3819 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3820 (defmacro gnus-thread-header (thread)
3821   "Return header of first article in THREAD.
3822 Note that THREAD must never, ever be anything else than a variable -
3823 using some other form will lead to serious barfage."
3824   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3825   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3826   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3827         (vector thread) 2))
3828
3829 (defsubst gnus-article-sort-by-number (h1 h2)
3830   "Sort articles by article number."
3831   (< (mail-header-number h1)
3832      (mail-header-number h2)))
3833
3834 (defun gnus-thread-sort-by-number (h1 h2)
3835   "Sort threads by root article number."
3836   (gnus-article-sort-by-number
3837    (gnus-thread-header h1) (gnus-thread-header h2)))
3838
3839 (defsubst gnus-article-sort-by-lines (h1 h2)
3840   "Sort articles by article Lines header."
3841   (< (mail-header-lines h1)
3842      (mail-header-lines h2)))
3843
3844 (defun gnus-thread-sort-by-lines (h1 h2)
3845   "Sort threads by root article Lines header."
3846   (gnus-article-sort-by-lines
3847    (gnus-thread-header h1) (gnus-thread-header h2)))
3848
3849 (defsubst gnus-article-sort-by-chars (h1 h2)
3850   "Sort articles by octet length."
3851   (< (mail-header-chars h1)
3852      (mail-header-chars h2)))
3853
3854 (defun gnus-thread-sort-by-chars (h1 h2)
3855   "Sort threads by root article octet length."
3856   (gnus-article-sort-by-chars
3857    (gnus-thread-header h1) (gnus-thread-header h2)))
3858
3859 (defsubst gnus-article-sort-by-author (h1 h2)
3860   "Sort articles by root author."
3861   (string-lessp
3862    (let ((extract (funcall
3863                    gnus-extract-address-components
3864                    (mail-header-from h1))))
3865      (or (car extract) (cadr extract) ""))
3866    (let ((extract (funcall
3867                    gnus-extract-address-components
3868                    (mail-header-from h2))))
3869      (or (car extract) (cadr extract) ""))))
3870
3871 (defun gnus-thread-sort-by-author (h1 h2)
3872   "Sort threads by root author."
3873   (gnus-article-sort-by-author
3874    (gnus-thread-header h1)  (gnus-thread-header h2)))
3875
3876 (defsubst gnus-article-sort-by-subject (h1 h2)
3877   "Sort articles by root subject."
3878   (string-lessp
3879    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3880    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3881
3882 (defun gnus-thread-sort-by-subject (h1 h2)
3883   "Sort threads by root subject."
3884   (gnus-article-sort-by-subject
3885    (gnus-thread-header h1) (gnus-thread-header h2)))
3886
3887 (defsubst gnus-article-sort-by-date (h1 h2)
3888   "Sort articles by root article date."
3889   (time-less-p
3890    (gnus-date-get-time (mail-header-date h1))
3891    (gnus-date-get-time (mail-header-date h2))))
3892
3893 (defun gnus-thread-sort-by-date (h1 h2)
3894   "Sort threads by root article date."
3895   (gnus-article-sort-by-date
3896    (gnus-thread-header h1) (gnus-thread-header h2)))
3897
3898 (defsubst gnus-article-sort-by-score (h1 h2)
3899   "Sort articles by root article score.
3900 Unscored articles will be counted as having a score of zero."
3901   (> (or (cdr (assq (mail-header-number h1)
3902                     gnus-newsgroup-scored))
3903          gnus-summary-default-score 0)
3904      (or (cdr (assq (mail-header-number h2)
3905                     gnus-newsgroup-scored))
3906          gnus-summary-default-score 0)))
3907
3908 (defun gnus-thread-sort-by-score (h1 h2)
3909   "Sort threads by root article score."
3910   (gnus-article-sort-by-score
3911    (gnus-thread-header h1) (gnus-thread-header h2)))
3912
3913 (defun gnus-thread-sort-by-total-score (h1 h2)
3914   "Sort threads by the sum of all scores in the thread.
3915 Unscored articles will be counted as having a score of zero."
3916   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3917
3918 (defun gnus-thread-total-score (thread)
3919   ;; This function find the total score of THREAD.
3920   (cond ((null thread)
3921          0)
3922         ((consp thread)
3923          (if (stringp (car thread))
3924              (apply gnus-thread-score-function 0
3925                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3926            (gnus-thread-total-score-1 thread)))
3927         (t
3928          (gnus-thread-total-score-1 (list thread)))))
3929
3930 (defun gnus-thread-total-score-1 (root)
3931   ;; This function find the total score of the thread below ROOT.
3932   (setq root (car root))
3933   (apply gnus-thread-score-function
3934          (or (append
3935               (mapcar 'gnus-thread-total-score
3936                       (cdr (gnus-id-to-thread (mail-header-id root))))
3937               (when (> (mail-header-number root) 0)
3938                 (list (or (cdr (assq (mail-header-number root)
3939                                      gnus-newsgroup-scored))
3940                           gnus-summary-default-score 0))))
3941              (list gnus-summary-default-score)
3942              '(0))))
3943
3944 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3945 (defvar gnus-tmp-prev-subject nil)
3946 (defvar gnus-tmp-false-parent nil)
3947 (defvar gnus-tmp-root-expunged nil)
3948 (defvar gnus-tmp-dummy-line nil)
3949
3950 (eval-when-compile (defvar gnus-tmp-header))
3951 (defun gnus-extra-header (type &optional header)
3952   "Return the extra header of TYPE."
3953   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3954       ""))
3955
3956 (defun gnus-summary-prepare-threads (threads)
3957   "Prepare summary buffer from THREADS and indentation LEVEL.
3958 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3959 or a straight list of headers."
3960   (gnus-message 7 "Generating summary...")
3961
3962   (setq gnus-newsgroup-threads threads)
3963   (beginning-of-line)
3964
3965   (let ((gnus-tmp-level 0)
3966         (default-score (or gnus-summary-default-score 0))
3967         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3968         thread number subject stack state gnus-tmp-gathered beg-match
3969         new-roots gnus-tmp-new-adopts thread-end
3970         gnus-tmp-header gnus-tmp-unread
3971         gnus-tmp-replied gnus-tmp-subject-or-nil
3972         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3973         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3974         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3975
3976     (setq gnus-tmp-prev-subject nil)
3977
3978     (if (vectorp (car threads))
3979         ;; If this is a straight (sic) list of headers, then a
3980         ;; threaded summary display isn't required, so we just create
3981         ;; an unthreaded one.
3982         (gnus-summary-prepare-unthreaded threads)
3983
3984       ;; Do the threaded display.
3985
3986       (while (or threads stack gnus-tmp-new-adopts new-roots)
3987
3988         (if (and (= gnus-tmp-level 0)
3989                  (or (not stack)
3990                      (= (caar stack) 0))
3991                  (not gnus-tmp-false-parent)
3992                  (or gnus-tmp-new-adopts new-roots))
3993             (if gnus-tmp-new-adopts
3994                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3995                       thread (list (car gnus-tmp-new-adopts))
3996                       gnus-tmp-header (caar thread)
3997                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3998               (when new-roots
3999                 (setq thread (list (car new-roots))
4000                       gnus-tmp-header (caar thread)
4001                       new-roots (cdr new-roots))))
4002
4003           (if threads
4004               ;; If there are some threads, we do them before the
4005               ;; threads on the stack.
4006               (setq thread threads
4007                     gnus-tmp-header (caar thread))
4008             ;; There were no current threads, so we pop something off
4009             ;; the stack.
4010             (setq state (car stack)
4011                   gnus-tmp-level (car state)
4012                   thread (cdr state)
4013                   stack (cdr stack)
4014                   gnus-tmp-header (caar thread))))
4015
4016         (setq gnus-tmp-false-parent nil)
4017         (setq gnus-tmp-root-expunged nil)
4018         (setq thread-end nil)
4019
4020         (if (stringp gnus-tmp-header)
4021             ;; The header is a dummy root.
4022             (cond
4023              ((eq gnus-summary-make-false-root 'adopt)
4024               ;; We let the first article adopt the rest.
4025               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4026                                                (cddar thread)))
4027               (setq gnus-tmp-gathered
4028                     (nconc (mapcar
4029                             (lambda (h) (mail-header-number (car h)))
4030                             (cddar thread))
4031                            gnus-tmp-gathered))
4032               (setq thread (cons (list (caar thread)
4033                                        (cadar thread))
4034                                  (cdr thread)))
4035               (setq gnus-tmp-level -1
4036                     gnus-tmp-false-parent t))
4037              ((eq gnus-summary-make-false-root 'empty)
4038               ;; We print adopted articles with empty subject fields.
4039               (setq gnus-tmp-gathered
4040                     (nconc (mapcar
4041                             (lambda (h) (mail-header-number (car h)))
4042                             (cddar thread))
4043                            gnus-tmp-gathered))
4044               (setq gnus-tmp-level -1))
4045              ((eq gnus-summary-make-false-root 'dummy)
4046               ;; We remember that we probably want to output a dummy
4047               ;; root.
4048               (setq gnus-tmp-dummy-line gnus-tmp-header)
4049               (setq gnus-tmp-prev-subject gnus-tmp-header))
4050              (t
4051               ;; We do not make a root for the gathered
4052               ;; sub-threads at all.
4053               (setq gnus-tmp-level -1)))
4054
4055           (setq number (mail-header-number gnus-tmp-header)
4056                 subject (mail-header-subject gnus-tmp-header))
4057
4058           (cond
4059            ;; If the thread has changed subject, we might want to make
4060            ;; this subthread into a root.
4061            ((and (null gnus-thread-ignore-subject)
4062                  (not (zerop gnus-tmp-level))
4063                  gnus-tmp-prev-subject
4064                  (not (inline
4065                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4066             (setq new-roots (nconc new-roots (list (car thread)))
4067                   thread-end t
4068                   gnus-tmp-header nil))
4069            ;; If the article lies outside the current limit,
4070            ;; then we do not display it.
4071            ((not (memq number gnus-newsgroup-limit))
4072             (setq gnus-tmp-gathered
4073                   (nconc (mapcar
4074                           (lambda (h) (mail-header-number (car h)))
4075                           (cdar thread))
4076                          gnus-tmp-gathered))
4077             (setq gnus-tmp-new-adopts (if (cdar thread)
4078                                           (append gnus-tmp-new-adopts
4079                                                   (cdar thread))
4080                                         gnus-tmp-new-adopts)
4081                   thread-end t
4082                   gnus-tmp-header nil)
4083             (when (zerop gnus-tmp-level)
4084               (setq gnus-tmp-root-expunged t)))
4085            ;; Perhaps this article is to be marked as read?
4086            ((and gnus-summary-mark-below
4087                  (< (or (cdr (assq number gnus-newsgroup-scored))
4088                         default-score)
4089                     gnus-summary-mark-below)
4090                  ;; Don't touch sparse articles.
4091                  (not (gnus-summary-article-sparse-p number))
4092                  (not (gnus-summary-article-ancient-p number)))
4093             (setq gnus-newsgroup-unreads
4094                   (delq number gnus-newsgroup-unreads))
4095             (if gnus-newsgroup-auto-expire
4096                 (push number gnus-newsgroup-expirable)
4097               (push (cons number gnus-low-score-mark)
4098                     gnus-newsgroup-reads))))
4099
4100           (when gnus-tmp-header
4101             ;; We may have an old dummy line to output before this
4102             ;; article.
4103             (when (and gnus-tmp-dummy-line
4104                        (gnus-subject-equal
4105                         gnus-tmp-dummy-line
4106                         (mail-header-subject gnus-tmp-header)))
4107               (gnus-summary-insert-dummy-line
4108                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4109               (setq gnus-tmp-dummy-line nil))
4110
4111             ;; Compute the mark.
4112             (setq gnus-tmp-unread (gnus-article-mark number))
4113
4114             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4115                                   gnus-tmp-header gnus-tmp-level)
4116                   gnus-newsgroup-data)
4117
4118             ;; Actually insert the line.
4119             (setq
4120              gnus-tmp-subject-or-nil
4121              (cond
4122               ((and gnus-thread-ignore-subject
4123                     gnus-tmp-prev-subject
4124                     (not (inline (gnus-subject-equal
4125                                   gnus-tmp-prev-subject subject))))
4126                subject)
4127               ((zerop gnus-tmp-level)
4128                (if (and (eq gnus-summary-make-false-root 'empty)
4129                         (memq number gnus-tmp-gathered)
4130                         gnus-tmp-prev-subject
4131                         (inline (gnus-subject-equal
4132                                  gnus-tmp-prev-subject subject)))
4133                    gnus-summary-same-subject
4134                  subject))
4135               (t gnus-summary-same-subject)))
4136             (if (and (eq gnus-summary-make-false-root 'adopt)
4137                      (= gnus-tmp-level 1)
4138                      (memq number gnus-tmp-gathered))
4139                 (setq gnus-tmp-opening-bracket ?\<
4140                       gnus-tmp-closing-bracket ?\>)
4141               (setq gnus-tmp-opening-bracket ?\[
4142                     gnus-tmp-closing-bracket ?\]))
4143             (setq
4144              gnus-tmp-indentation
4145              (aref gnus-thread-indent-array gnus-tmp-level)
4146              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4147              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4148                                 gnus-summary-default-score 0)
4149              gnus-tmp-score-char
4150              (if (or (null gnus-summary-default-score)
4151                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4152                          gnus-summary-zcore-fuzz))
4153                  ?  ;Whitespace
4154                (if (< gnus-tmp-score gnus-summary-default-score)
4155                    gnus-score-below-mark gnus-score-over-mark))
4156              gnus-tmp-replied
4157              (cond ((memq number gnus-newsgroup-processable)
4158                     gnus-process-mark)
4159                    ((memq number gnus-newsgroup-cached)
4160                     gnus-cached-mark)
4161                    ((memq number gnus-newsgroup-replied)
4162                     gnus-replied-mark)
4163                    ((memq number gnus-newsgroup-saved)
4164                     gnus-saved-mark)
4165                    (t gnus-unread-mark))
4166              gnus-tmp-from (mail-header-from gnus-tmp-header)
4167              gnus-tmp-name
4168              (cond
4169               ((string-match "<[^>]+> *$" gnus-tmp-from)
4170                (setq beg-match (match-beginning 0))
4171                (or (and (string-match "^\".+\"" gnus-tmp-from)
4172                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4173                    (substring gnus-tmp-from 0 beg-match)))
4174               ((string-match "(.+)" gnus-tmp-from)
4175                (substring gnus-tmp-from
4176                           (1+ (match-beginning 0)) (1- (match-end 0))))
4177               (t gnus-tmp-from)))
4178             (when (string= gnus-tmp-name "")
4179               (setq gnus-tmp-name gnus-tmp-from))
4180             (unless (numberp gnus-tmp-lines)
4181               (setq gnus-tmp-lines 0))
4182             (gnus-put-text-property
4183              (point)
4184              (progn (eval gnus-summary-line-format-spec) (point))
4185              'gnus-number number)
4186             (when gnus-visual-p
4187               (forward-line -1)
4188               (gnus-run-hooks 'gnus-summary-update-hook)
4189               (forward-line 1))
4190
4191             (setq gnus-tmp-prev-subject subject)))
4192
4193         (when (nth 1 thread)
4194           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4195         (incf gnus-tmp-level)
4196         (setq threads (if thread-end nil (cdar thread)))
4197         (unless threads
4198           (setq gnus-tmp-level 0)))))
4199   (gnus-message 7 "Generating summary...done"))
4200
4201 (defun gnus-summary-prepare-unthreaded (headers)
4202   "Generate an unthreaded summary buffer based on HEADERS."
4203   (let (header number mark)
4204
4205     (beginning-of-line)
4206
4207     (while headers
4208       ;; We may have to root out some bad articles...
4209       (when (memq (setq number (mail-header-number
4210                                 (setq header (pop headers))))
4211                   gnus-newsgroup-limit)
4212         ;; Mark article as read when it has a low score.
4213         (when (and gnus-summary-mark-below
4214                    (< (or (cdr (assq number gnus-newsgroup-scored))
4215                           gnus-summary-default-score 0)
4216                       gnus-summary-mark-below)
4217                    (not (gnus-summary-article-ancient-p number)))
4218           (setq gnus-newsgroup-unreads
4219                 (delq number gnus-newsgroup-unreads))
4220           (if gnus-newsgroup-auto-expire
4221               (push number gnus-newsgroup-expirable)
4222             (push (cons number gnus-low-score-mark)
4223                   gnus-newsgroup-reads)))
4224
4225         (setq mark (gnus-article-mark number))
4226         (push (gnus-data-make number mark (1+ (point)) header 0)
4227               gnus-newsgroup-data)
4228         (gnus-summary-insert-line
4229          header 0 number
4230          mark (memq number gnus-newsgroup-replied)
4231          (memq number gnus-newsgroup-expirable)
4232          (mail-header-subject header) nil
4233          (cdr (assq number gnus-newsgroup-scored))
4234          (memq number gnus-newsgroup-processable))))))
4235
4236 (defun gnus-summary-remove-list-identifiers ()
4237   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4238   (let ((regexp (if (stringp gnus-list-identifiers)
4239                     gnus-list-identifiers
4240                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4241     (dolist (header gnus-newsgroup-headers)
4242       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4243                                   " *\\)\\)+\\(Re: +\\)?\\)")
4244                           (mail-header-subject header))
4245         (mail-header-set-subject
4246          header (concat (substring (mail-header-subject header)
4247                                    0 (match-beginning 1))
4248                         (or
4249                          (match-string 3 (mail-header-subject header))
4250                          (match-string 5 (mail-header-subject header)))
4251                         (substring (mail-header-subject header)
4252                                    (match-end 1))))))))
4253
4254 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4255   "Select newsgroup GROUP.
4256 If READ-ALL is non-nil, all articles in the group are selected.
4257 If SELECT-ARTICLES, only select those articles from GROUP."
4258   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4259          ;;!!! Dirty hack; should be removed.
4260          (gnus-summary-ignore-duplicates
4261           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4262               t
4263             gnus-summary-ignore-duplicates))
4264          (info (nth 2 entry))
4265          articles fetched-articles cached)
4266
4267     (unless (gnus-check-server
4268              (setq gnus-current-select-method
4269                    (gnus-find-method-for-group group)))
4270       (error "Couldn't open server"))
4271
4272     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4273         (gnus-activate-group group)     ; Or we can activate it...
4274         (progn                          ; Or we bug out.
4275           (when (equal major-mode 'gnus-summary-mode)
4276             (kill-buffer (current-buffer)))
4277           (error "Couldn't activate group %s: %s"
4278                  group (gnus-status-message group))))
4279
4280     (unless (gnus-request-group group t)
4281       (when (equal major-mode 'gnus-summary-mode)
4282         (kill-buffer (current-buffer)))
4283       (error "Couldn't request group %s: %s"
4284              group (gnus-status-message group)))
4285
4286     (setq gnus-newsgroup-name group)
4287     (setq gnus-newsgroup-unselected nil)
4288     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4289     (gnus-summary-setup-default-charset)
4290
4291     ;; Adjust and set lists of article marks.
4292     (when info
4293       (gnus-adjust-marked-articles info))
4294
4295     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4296     (when (gnus-virtual-group-p group)
4297       (setq cached gnus-newsgroup-cached))
4298
4299     (setq gnus-newsgroup-unreads
4300           (gnus-set-difference
4301            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4302            gnus-newsgroup-dormant))
4303
4304     (setq gnus-newsgroup-processable nil)
4305
4306     (gnus-update-read-articles group gnus-newsgroup-unreads)
4307
4308     (if (setq articles select-articles)
4309         (setq gnus-newsgroup-unselected
4310               (gnus-sorted-intersection
4311                gnus-newsgroup-unreads
4312                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4313       (setq articles (gnus-articles-to-read group read-all)))
4314
4315     (cond
4316      ((null articles)
4317       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4318       'quit)
4319      ((eq articles 0) nil)
4320      (t
4321       ;; Init the dependencies hash table.
4322       (setq gnus-newsgroup-dependencies
4323             (gnus-make-hashtable (length articles)))
4324       (gnus-set-global-variables)
4325       ;; Retrieve the headers and read them in.
4326       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4327       (setq gnus-newsgroup-headers
4328             (if (eq 'nov
4329                     (setq gnus-headers-retrieved-by
4330                           (gnus-retrieve-headers
4331                            articles gnus-newsgroup-name
4332                            ;; We might want to fetch old headers, but
4333                            ;; not if there is only 1 article.
4334                            (and (or (and
4335                                      (not (eq gnus-fetch-old-headers 'some))
4336                                      (not (numberp gnus-fetch-old-headers)))
4337                                     (> (length articles) 1))
4338                                 gnus-fetch-old-headers))))
4339                 (gnus-get-newsgroup-headers-xover
4340                  articles nil nil gnus-newsgroup-name t)
4341               (gnus-get-newsgroup-headers)))
4342       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4343
4344       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4345       (when cached
4346         (setq gnus-newsgroup-cached cached))
4347
4348       ;; Suppress duplicates?
4349       (when gnus-suppress-duplicates
4350         (gnus-dup-suppress-articles))
4351
4352       ;; Set the initial limit.
4353       (setq gnus-newsgroup-limit (copy-sequence articles))
4354       ;; Remove canceled articles from the list of unread articles.
4355       (setq gnus-newsgroup-unreads
4356             (gnus-set-sorted-intersection
4357              gnus-newsgroup-unreads
4358              (setq fetched-articles
4359                    (mapcar (lambda (headers) (mail-header-number headers))
4360                            gnus-newsgroup-headers))))
4361       ;; Removed marked articles that do not exist.
4362       (gnus-update-missing-marks
4363        (gnus-sorted-complement fetched-articles articles))
4364       ;; We might want to build some more threads first.
4365       (when (and gnus-fetch-old-headers
4366                  (eq gnus-headers-retrieved-by 'nov))
4367         (if (eq gnus-fetch-old-headers 'invisible)
4368             (gnus-build-all-threads)
4369           (gnus-build-old-threads)))
4370       ;; Let the Gnus agent mark articles as read.
4371       (when gnus-agent
4372         (gnus-agent-get-undownloaded-list))
4373       ;; Remove list identifiers from subject
4374       (when gnus-list-identifiers
4375         (gnus-summary-remove-list-identifiers))
4376       ;; Check whether auto-expire is to be done in this group.
4377       (setq gnus-newsgroup-auto-expire
4378             (gnus-group-auto-expirable-p group))
4379       ;; Set up the article buffer now, if necessary.
4380       (unless gnus-single-article-buffer
4381         (gnus-article-setup-buffer))
4382       ;; First and last article in this newsgroup.
4383       (when gnus-newsgroup-headers
4384         (setq gnus-newsgroup-begin
4385               (mail-header-number (car gnus-newsgroup-headers))
4386               gnus-newsgroup-end
4387               (mail-header-number
4388                (gnus-last-element gnus-newsgroup-headers))))
4389       ;; GROUP is successfully selected.
4390       (or gnus-newsgroup-headers t)))))
4391
4392 (defun gnus-articles-to-read (group &optional read-all)
4393   "Find out what articles the user wants to read."
4394   (let* ((articles
4395           ;; Select all articles if `read-all' is non-nil, or if there
4396           ;; are no unread articles.
4397           (if (or read-all
4398                   (and (zerop (length gnus-newsgroup-marked))
4399                        (zerop (length gnus-newsgroup-unreads)))
4400                   (eq (gnus-group-find-parameter group 'display)
4401                       'all))
4402               (or
4403                (gnus-uncompress-range (gnus-active group))
4404                (gnus-cache-articles-in-group group))
4405             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4406                           (copy-sequence gnus-newsgroup-unreads))
4407                   '<)))
4408          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4409          (scored (length scored-list))
4410          (number (length articles))
4411          (marked (+ (length gnus-newsgroup-marked)
4412                     (length gnus-newsgroup-dormant)))
4413          (select
4414           (cond
4415            ((numberp read-all)
4416             read-all)
4417            (t
4418             (condition-case ()
4419                 (cond
4420                  ((and (or (<= scored marked) (= scored number))
4421                        (numberp gnus-large-newsgroup)
4422                        (> number gnus-large-newsgroup))
4423                   (let ((input
4424                          (read-string
4425                           (format
4426                            "How many articles from %s (default %d): "
4427                            (gnus-limit-string gnus-newsgroup-name 35)
4428                            number))))
4429                     (if (string-match "^[ \t]*$" input) number input)))
4430                  ((and (> scored marked) (< scored number)
4431                        (> (- scored number) 20))
4432                   (let ((input
4433                          (read-string
4434                           (format "%s %s (%d scored, %d total): "
4435                                   "How many articles from"
4436                                   group scored number))))
4437                     (if (string-match "^[ \t]*$" input)
4438                         number input)))
4439                  (t number))
4440               (quit
4441                (message "Quit getting the articles to read")
4442                nil))))))
4443     (setq select (if (stringp select) (string-to-number select) select))
4444     (if (or (null select) (zerop select))
4445         select
4446       (if (and (not (zerop scored)) (<= (abs select) scored))
4447           (progn
4448             (setq articles (sort scored-list '<))
4449             (setq number (length articles)))
4450         (setq articles (copy-sequence articles)))
4451
4452       (when (< (abs select) number)
4453         (if (< select 0)
4454             ;; Select the N oldest articles.
4455             (setcdr (nthcdr (1- (abs select)) articles) nil)
4456           ;; Select the N most recent articles.
4457           (setq articles (nthcdr (- number select) articles))))
4458       (setq gnus-newsgroup-unselected
4459             (gnus-sorted-intersection
4460              gnus-newsgroup-unreads
4461              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4462       (when gnus-alter-articles-to-read-function
4463         (setq gnus-newsgroup-unreads
4464               (sort 
4465                (funcall gnus-alter-articles-to-read-function
4466                         gnus-newsgroup-name gnus-newsgroup-unreads)
4467                '<)))
4468       articles)))
4469
4470 (defun gnus-killed-articles (killed articles)
4471   (let (out)
4472     (while articles
4473       (when (inline (gnus-member-of-range (car articles) killed))
4474         (push (car articles) out))
4475       (setq articles (cdr articles)))
4476     out))
4477
4478 (defun gnus-uncompress-marks (marks)
4479   "Uncompress the mark ranges in MARKS."
4480   (let ((uncompressed '(score bookmark))
4481         out)
4482     (while marks
4483       (if (memq (caar marks) uncompressed)
4484           (push (car marks) out)
4485         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4486       (setq marks (cdr marks)))
4487     out))
4488
4489 (defun gnus-adjust-marked-articles (info)
4490   "Set all article lists and remove all marks that are no longer valid."
4491   (let* ((marked-lists (gnus-info-marks info))
4492          (active (gnus-active (gnus-info-group info)))
4493          (min (car active))
4494          (max (cdr active))
4495          (types gnus-article-mark-lists)
4496          (uncompressed '(score bookmark killed))
4497          marks var articles article mark)
4498
4499     (while marked-lists
4500       (setq marks (pop marked-lists))
4501       (set (setq var (intern (format "gnus-newsgroup-%s"
4502                                      (car (rassq (setq mark (car marks))
4503                                                  types)))))
4504            (if (memq (car marks) uncompressed) (cdr marks)
4505              (gnus-uncompress-range (cdr marks))))
4506
4507       (setq articles (symbol-value var))
4508
4509       ;; All articles have to be subsets of the active articles.
4510       (cond
4511        ;; Adjust "simple" lists.
4512        ((memq mark '(tick dormant expire reply save))
4513         (while articles
4514           (when (or (< (setq article (pop articles)) min) (> article max))
4515             (set var (delq article (symbol-value var))))))
4516        ;; Adjust assocs.
4517        ((memq mark uncompressed)
4518         (when (not (listp (cdr (symbol-value var))))
4519           (set var (list (symbol-value var))))
4520         (when (not (listp (cdr articles)))
4521           (setq articles (list articles)))
4522         (while articles
4523           (when (or (not (consp (setq article (pop articles))))
4524                     (< (car article) min)
4525                     (> (car article) max))
4526             (set var (delq article (symbol-value var))))))))))
4527
4528 (defun gnus-update-missing-marks (missing)
4529   "Go through the list of MISSING articles and remove them from the mark lists."
4530   (when missing
4531     (let ((types gnus-article-mark-lists)
4532           var m)
4533       ;; Go through all types.
4534       (while types
4535         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4536         (when (symbol-value var)
4537           ;; This list has articles.  So we delete all missing articles
4538           ;; from it.
4539           (setq m missing)
4540           (while m
4541             (set var (delq (pop m) (symbol-value var)))))))))
4542
4543 (defun gnus-update-marks ()
4544   "Enter the various lists of marked articles into the newsgroup info list."
4545   (let ((types gnus-article-mark-lists)
4546         (info (gnus-get-info gnus-newsgroup-name))
4547         (uncompressed '(score bookmark killed))
4548         type list newmarked symbol delta-marks)
4549     (when info
4550       ;; Add all marks lists to the list of marks lists.
4551       (while (setq type (pop types))
4552         (setq list (symbol-value
4553                     (setq symbol
4554                           (intern (format "gnus-newsgroup-%s"
4555                                           (car type))))))
4556
4557         (when list
4558           ;; Get rid of the entries of the articles that have the
4559           ;; default score.
4560           (when (and (eq (cdr type) 'score)
4561                      gnus-save-score
4562                      list)
4563             (let* ((arts list)
4564                    (prev (cons nil list))
4565                    (all prev))
4566               (while arts
4567                 (if (or (not (consp (car arts)))
4568                         (= (cdar arts) gnus-summary-default-score))
4569                     (setcdr prev (cdr arts))
4570                   (setq prev arts))
4571                 (setq arts (cdr arts)))
4572               (setq list (cdr all)))))
4573
4574         (unless (memq (cdr type) uncompressed)
4575           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4576        
4577         (when (gnus-check-backend-function
4578                'request-set-mark gnus-newsgroup-name)
4579           ;; propagate flags to server, with the following exceptions:
4580           ;; uncompressed:s are not proper flags (they are cons cells)
4581           ;; cache is a internal gnus flag
4582           ;; download are local to one gnus installation (well)
4583           ;; unsend are for nndraft groups only
4584           ;; xxx: generality of this?  this suits nnimap anyway
4585           (unless (memq (cdr type) (append '(cache download unsend)
4586                                            uncompressed))
4587             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4588                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4589                    (add (gnus-remove-from-range
4590                          (gnus-copy-sequence list) old)))
4591               (when add
4592                 (push (list add 'add (list (cdr type))) delta-marks))
4593               (when del
4594                 (push (list del 'del (list (cdr type))) delta-marks)))))
4595           
4596         (when list
4597           (push (cons (cdr type) list) newmarked)))
4598
4599       (when delta-marks
4600         (unless (gnus-check-group gnus-newsgroup-name)
4601           (error "Can't open server for %s" gnus-newsgroup-name))
4602         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4603           
4604       ;; Enter these new marks into the info of the group.
4605       (if (nthcdr 3 info)
4606           (setcar (nthcdr 3 info) newmarked)
4607         ;; Add the marks lists to the end of the info.
4608         (when newmarked
4609           (setcdr (nthcdr 2 info) (list newmarked))))
4610
4611       ;; Cut off the end of the info if there's nothing else there.
4612       (let ((i 5))
4613         (while (and (> i 2)
4614                     (not (nth i info)))
4615           (when (nthcdr (decf i) info)
4616             (setcdr (nthcdr i info) nil)))))))
4617
4618 (defun gnus-set-mode-line (where)
4619   "Set the mode line of the article or summary buffers.
4620 If WHERE is `summary', the summary mode line format will be used."
4621   ;; Is this mode line one we keep updated?
4622   (when (and (memq where gnus-updated-mode-lines)
4623              (symbol-value
4624               (intern (format "gnus-%s-mode-line-format-spec" where))))
4625     (let (mode-string)
4626       (save-excursion
4627         ;; We evaluate this in the summary buffer since these
4628         ;; variables are buffer-local to that buffer.
4629         (set-buffer gnus-summary-buffer)
4630         ;; We bind all these variables that are used in the `eval' form
4631         ;; below.
4632         (let* ((mformat (symbol-value
4633                          (intern
4634                           (format "gnus-%s-mode-line-format-spec" where))))
4635                (gnus-tmp-group-name (gnus-group-name-decode 
4636                                      gnus-newsgroup-name
4637                                      (gnus-group-name-charset 
4638                                       nil
4639                                       gnus-newsgroup-name)))
4640                (gnus-tmp-article-number (or gnus-current-article 0))
4641                (gnus-tmp-unread gnus-newsgroup-unreads)
4642                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4643                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4644                (gnus-tmp-unread-and-unselected
4645                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4646                             (zerop gnus-tmp-unselected))
4647                        "")
4648                       ((zerop gnus-tmp-unselected)
4649                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4650                       (t (format "{%d(+%d) more}"
4651                                  gnus-tmp-unread-and-unticked
4652                                  gnus-tmp-unselected))))
4653                (gnus-tmp-subject
4654                 (if (and gnus-current-headers
4655                          (vectorp gnus-current-headers))
4656                     (gnus-mode-string-quote
4657                      (mail-header-subject gnus-current-headers))
4658                   ""))
4659                bufname-length max-len
4660                gnus-tmp-header);; passed as argument to any user-format-funcs
4661           (setq mode-string (eval mformat))
4662           (setq bufname-length (if (string-match "%b" mode-string)
4663                                    (- (length
4664                                        (buffer-name
4665                                         (if (eq where 'summary)
4666                                             nil
4667                                           (get-buffer gnus-article-buffer))))
4668                                       2)
4669                                  0))
4670           (setq max-len (max 4 (if gnus-mode-non-string-length
4671                                    (- (window-width)
4672                                       gnus-mode-non-string-length
4673                                       bufname-length)
4674                                  (length mode-string))))
4675           ;; We might have to chop a bit of the string off...
4676           (when (> (length mode-string) max-len)
4677             (setq mode-string
4678                   (concat (truncate-string-to-width mode-string (- max-len 3))
4679                           "...")))
4680           ;; Pad the mode string a bit.
4681           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4682       ;; Update the mode line.
4683       (setq mode-line-buffer-identification
4684             (gnus-mode-line-buffer-identification (list mode-string)))
4685       (set-buffer-modified-p t))))
4686
4687 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4688   "Go through the HEADERS list and add all Xrefs to a hash table.
4689 The resulting hash table is returned, or nil if no Xrefs were found."
4690   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4691          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4692          (xref-hashtb (gnus-make-hashtable))
4693          start group entry number xrefs header)
4694     (while headers
4695       (setq header (pop headers))
4696       (when (and (setq xrefs (mail-header-xref header))
4697                  (not (memq (setq number (mail-header-number header))
4698                             unreads)))
4699         (setq start 0)
4700         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4701           (setq start (match-end 0))
4702           (setq group (if prefix
4703                           (concat prefix (substring xrefs (match-beginning 1)
4704                                                     (match-end 1)))
4705                         (substring xrefs (match-beginning 1) (match-end 1))))
4706           (setq number
4707                 (string-to-int (substring xrefs (match-beginning 2)
4708                                           (match-end 2))))
4709           (if (setq entry (gnus-gethash group xref-hashtb))
4710               (setcdr entry (cons number (cdr entry)))
4711             (gnus-sethash group (cons number nil) xref-hashtb)))))
4712     (and start xref-hashtb)))
4713
4714 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4715   "Look through all the headers and mark the Xrefs as read."
4716   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4717         name entry info xref-hashtb idlist method nth4)
4718     (save-excursion
4719       (set-buffer gnus-group-buffer)
4720       (when (setq xref-hashtb
4721                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4722         (mapatoms
4723          (lambda (group)
4724            (unless (string= from-newsgroup (setq name (symbol-name group)))
4725              (setq idlist (symbol-value group))
4726              ;; Dead groups are not updated.
4727              (and (prog1
4728                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4729                             info (nth 2 entry))
4730                     (when (stringp (setq nth4 (gnus-info-method info)))
4731                       (setq nth4 (gnus-server-to-method nth4))))
4732                   ;; Only do the xrefs if the group has the same
4733                   ;; select method as the group we have just read.
4734                   (or (gnus-methods-equal-p
4735                        nth4 (gnus-find-method-for-group from-newsgroup))
4736                       virtual
4737                       (equal nth4 (setq method (gnus-find-method-for-group
4738                                                 from-newsgroup)))
4739                       (and (equal (car nth4) (car method))
4740                            (equal (nth 1 nth4) (nth 1 method))))
4741                   gnus-use-cross-reference
4742                   (or (not (eq gnus-use-cross-reference t))
4743                       virtual
4744                       ;; Only do cross-references on subscribed
4745                       ;; groups, if that is what is wanted.
4746                       (<= (gnus-info-level info) gnus-level-subscribed))
4747                   (gnus-group-make-articles-read name idlist))))
4748          xref-hashtb)))))
4749
4750 (defun gnus-compute-read-articles (group articles)
4751   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4752          (info (nth 2 entry))
4753          (active (gnus-active group))
4754          ninfo)
4755     (when entry
4756       ;; First peel off all invalid article numbers.
4757       (when active
4758         (let ((ids articles)
4759               id first)
4760           (while (setq id (pop ids))
4761             (when (and first (> id (cdr active)))
4762               ;; We'll end up in this situation in one particular
4763               ;; obscure situation.  If you re-scan a group and get
4764               ;; a new article that is cross-posted to a different
4765               ;; group that has not been re-scanned, you might get
4766               ;; crossposted article that has a higher number than
4767               ;; Gnus believes possible.  So we re-activate this
4768               ;; group as well.  This might mean doing the
4769               ;; crossposting thingy will *increase* the number
4770               ;; of articles in some groups.  Tsk, tsk.
4771               (setq active (or (gnus-activate-group group) active)))
4772             (when (or (> id (cdr active))
4773                       (< id (car active)))
4774               (setq articles (delq id articles))))))
4775       ;; If the read list is nil, we init it.
4776       (if (and active
4777                (null (gnus-info-read info))
4778                (> (car active) 1))
4779           (setq ninfo (cons 1 (1- (car active))))
4780         (setq ninfo (gnus-info-read info)))
4781       ;; Then we add the read articles to the range.
4782       (gnus-add-to-range
4783        ninfo (setq articles (sort articles '<))))))
4784
4785 (defun gnus-group-make-articles-read (group articles)
4786   "Update the info of GROUP to say that ARTICLES are read."
4787   (let* ((num 0)
4788          (entry (gnus-gethash group gnus-newsrc-hashtb))
4789          (info (nth 2 entry))
4790          (active (gnus-active group))
4791          range)
4792     (when entry
4793       (setq range (gnus-compute-read-articles group articles))
4794       (save-excursion
4795         (set-buffer gnus-group-buffer)
4796         (gnus-undo-register
4797           `(progn
4798              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4799              (gnus-info-set-read ',info ',(gnus-info-read info))
4800              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4801              (gnus-group-update-group ,group t))))
4802       ;; Add the read articles to the range.
4803       (gnus-info-set-read info range)
4804       ;; Then we have to re-compute how many unread
4805       ;; articles there are in this group.
4806       (when active
4807         (cond
4808          ((not range)
4809           (setq num (- (1+ (cdr active)) (car active))))
4810          ((not (listp (cdr range)))
4811           (setq num (- (cdr active) (- (1+ (cdr range))
4812                                        (car range)))))
4813          (t
4814           (while range
4815             (if (numberp (car range))
4816                 (setq num (1+ num))
4817               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4818             (setq range (cdr range)))
4819           (setq num (- (cdr active) num))))
4820         ;; Update the number of unread articles.
4821         (setcar entry num)
4822         ;; Update the group buffer.
4823         (gnus-group-update-group group t)))))
4824
4825 (defvar gnus-newsgroup-none-id 0)
4826
4827 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4828   (let ((cur nntp-server-buffer)
4829         (dependencies
4830          (or dependencies
4831              (save-excursion (set-buffer gnus-summary-buffer)
4832                              gnus-newsgroup-dependencies)))
4833         headers id end ref
4834         (mail-parse-charset gnus-newsgroup-charset)
4835         (mail-parse-ignored-charsets
4836          (save-excursion (condition-case nil
4837                              (set-buffer gnus-summary-buffer)
4838                            (error))
4839                          gnus-newsgroup-ignored-charsets)))
4840     (save-excursion
4841       (set-buffer nntp-server-buffer)
4842       ;; Translate all TAB characters into SPACE characters.
4843       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4844       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4845       (gnus-run-hooks 'gnus-parse-headers-hook)
4846       (let ((case-fold-search t)
4847             in-reply-to header p lines chars)
4848         (goto-char (point-min))
4849         ;; Search to the beginning of the next header.  Error messages
4850         ;; do not begin with 2 or 3.
4851         (while (re-search-forward "^[23][0-9]+ " nil t)
4852           (setq id nil
4853                 ref nil)
4854           ;; This implementation of this function, with nine
4855           ;; search-forwards instead of the one re-search-forward and
4856           ;; a case (which basically was the old function) is actually
4857           ;; about twice as fast, even though it looks messier.  You
4858           ;; can't have everything, I guess.  Speed and elegance
4859           ;; doesn't always go hand in hand.
4860           (setq
4861            header
4862            (vector
4863             ;; Number.
4864             (prog1
4865                 (read cur)
4866               (end-of-line)
4867               (setq p (point))
4868               (narrow-to-region (point)
4869                                 (or (and (search-forward "\n.\n" nil t)
4870                                          (- (point) 2))
4871                                     (point))))
4872             ;; Subject.
4873             (progn
4874               (goto-char p)
4875               (if (search-forward "\nsubject: " nil t)
4876                   (funcall gnus-decode-encoded-word-function
4877                            (nnheader-header-value))
4878                 "(none)"))
4879             ;; From.
4880             (progn
4881               (goto-char p)
4882               (if (or (search-forward "\nfrom: " nil t)
4883                       (search-forward "\nfrom:" nil t))
4884                   (funcall gnus-decode-encoded-word-function
4885                            (nnheader-header-value))
4886                 "(nobody)"))
4887             ;; Date.
4888             (progn
4889               (goto-char p)
4890               (if (search-forward "\ndate: " nil t)
4891                   (nnheader-header-value) ""))
4892             ;; Message-ID.
4893             (progn
4894               (goto-char p)
4895               (setq id (if (re-search-forward
4896                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4897                            ;; We do it this way to make sure the Message-ID
4898                            ;; is (somewhat) syntactically valid.
4899                            (buffer-substring (match-beginning 1)
4900                                              (match-end 1))
4901                          ;; If there was no message-id, we just fake one
4902                          ;; to make subsequent routines simpler.
4903                          (nnheader-generate-fake-message-id))))
4904             ;; References.
4905             (progn
4906               (goto-char p)
4907               (if (search-forward "\nreferences: " nil t)
4908                   (progn
4909                     (setq end (point))
4910                     (prog1
4911                         (nnheader-header-value)
4912                       (setq ref
4913                             (buffer-substring
4914                              (progn
4915                                (end-of-line)
4916                                (search-backward ">" end t)
4917                                (1+ (point)))
4918                              (progn
4919                                (search-backward "<" end t)
4920                                (point))))))
4921                 ;; Get the references from the in-reply-to header if there
4922                 ;; were no references and the in-reply-to header looks
4923                 ;; promising.
4924                 (if (and (search-forward "\nin-reply-to: " nil t)
4925                          (setq in-reply-to (nnheader-header-value))
4926                          (string-match "<[^>]+>" in-reply-to))
4927                     (let (ref2)
4928                       (setq ref (substring in-reply-to (match-beginning 0)
4929                                            (match-end 0)))
4930                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4931                         (setq ref2 (substring in-reply-to (match-beginning 0)
4932                                               (match-end 0)))
4933                         (when (> (length ref2) (length ref))
4934                           (setq ref ref2)))
4935                       ref)
4936                   (setq ref nil))))
4937             ;; Chars.
4938             (progn
4939               (goto-char p)
4940               (if (search-forward "\nchars: " nil t)
4941                   (if (numberp (setq chars (ignore-errors (read cur))))
4942                       chars 0)
4943                 0))
4944             ;; Lines.
4945             (progn
4946               (goto-char p)
4947               (if (search-forward "\nlines: " nil t)
4948                   (if (numberp (setq lines (ignore-errors (read cur))))
4949                       lines 0)
4950                 0))
4951             ;; Xref.
4952             (progn
4953               (goto-char p)
4954               (and (search-forward "\nxref: " nil t)
4955                    (nnheader-header-value)))
4956             ;; Extra.
4957             (when gnus-extra-headers
4958               (let ((extra gnus-extra-headers)
4959                     out)
4960                 (while extra
4961                   (goto-char p)
4962                   (when (search-forward
4963                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4964                     (push (cons (car extra) (nnheader-header-value))
4965                           out))
4966                   (pop extra))
4967                 out))))
4968           (when (equal id ref)
4969             (setq ref nil))
4970
4971           (when gnus-alter-header-function
4972             (funcall gnus-alter-header-function header)
4973             (setq id (mail-header-id header)
4974                   ref (gnus-parent-id (mail-header-references header))))
4975
4976           (when (setq header
4977                       (gnus-dependencies-add-header
4978                        header dependencies force-new))
4979             (push header headers))
4980           (goto-char (point-max))
4981           (widen))
4982         (nreverse headers)))))
4983
4984 ;; Goes through the xover lines and returns a list of vectors
4985 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4986                                                   force-new dependencies
4987                                                   group also-fetch-heads)
4988   "Parse the news overview data in the server buffer.
4989 Return a list of headers that match SEQUENCE (see
4990 `nntp-retrieve-headers')."
4991   ;; Get the Xref when the users reads the articles since most/some
4992   ;; NNTP servers do not include Xrefs when using XOVER.
4993   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4994   (let ((mail-parse-charset gnus-newsgroup-charset)
4995         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4996         (cur nntp-server-buffer)
4997         (dependencies (or dependencies gnus-newsgroup-dependencies))
4998         number headers header)
4999     (save-excursion
5000       (set-buffer nntp-server-buffer)
5001       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5002       ;; Allow the user to mangle the headers before parsing them.
5003       (gnus-run-hooks 'gnus-parse-headers-hook)
5004       (goto-char (point-min))
5005       (while (not (eobp))
5006         (condition-case ()
5007             (while (and sequence (not (eobp)))
5008               (setq number (read cur))
5009               (while (and sequence
5010                           (< (car sequence) number))
5011                 (setq sequence (cdr sequence)))
5012               (and sequence
5013                    (eq number (car sequence))
5014                    (progn
5015                      (setq sequence (cdr sequence))
5016                      (setq header (inline
5017                                     (gnus-nov-parse-line
5018                                      number dependencies force-new))))
5019                    (push header headers))
5020               (forward-line 1))
5021           (error
5022            (gnus-error 4 "Strange nov line (%d)"
5023                        (count-lines (point-min) (point)))))
5024         (forward-line 1))
5025       ;; A common bug in inn is that if you have posted an article and
5026       ;; then retrieves the active file, it will answer correctly --
5027       ;; the new article is included.  However, a NOV entry for the
5028       ;; article may not have been generated yet, so this may fail.
5029       ;; We work around this problem by retrieving the last few
5030       ;; headers using HEAD.
5031       (if (or (not also-fetch-heads)
5032               (not sequence))
5033           ;; We (probably) got all the headers.
5034           (nreverse headers)
5035         (let ((gnus-nov-is-evil t))
5036           (nconc
5037            (nreverse headers)
5038            (when (gnus-retrieve-headers sequence group)
5039              (gnus-get-newsgroup-headers))))))))
5040
5041 (defun gnus-article-get-xrefs ()
5042   "Fill in the Xref value in `gnus-current-headers', if necessary.
5043 This is meant to be called in `gnus-article-internal-prepare-hook'."
5044   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5045                                  gnus-current-headers)))
5046     (or (not gnus-use-cross-reference)
5047         (not headers)
5048         (and (mail-header-xref headers)
5049              (not (string= (mail-header-xref headers) "")))
5050         (let ((case-fold-search t)
5051               xref)
5052           (save-restriction
5053             (nnheader-narrow-to-headers)
5054             (goto-char (point-min))
5055             (when (or (and (not (eobp))
5056                            (eq (downcase (char-after)) ?x)
5057                            (looking-at "Xref:"))
5058                       (search-forward "\nXref:" nil t))
5059               (goto-char (1+ (match-end 0)))
5060               (setq xref (buffer-substring (point)
5061                                            (progn (end-of-line) (point))))
5062               (mail-header-set-xref headers xref)))))))
5063
5064 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5065   "Find article ID and insert the summary line for that article.
5066 OLD-HEADER can either be a header or a line number to insert
5067 the subject line on."
5068   (let* ((line (and (numberp old-header) old-header))
5069          (old-header (and (vectorp old-header) old-header))
5070          (header (cond ((and old-header use-old-header)
5071                         old-header)
5072                        ((and (numberp id)
5073                              (gnus-number-to-header id))
5074                         (gnus-number-to-header id))
5075                        (t
5076                         (gnus-read-header id))))
5077          (number (and (numberp id) id))
5078          d)
5079     (when header
5080       ;; Rebuild the thread that this article is part of and go to the
5081       ;; article we have fetched.
5082       (when (and (not gnus-show-threads)
5083                  old-header)
5084         (when (and number
5085                    (setq d (gnus-data-find (mail-header-number old-header))))
5086           (goto-char (gnus-data-pos d))
5087           (gnus-data-remove
5088            number
5089            (- (gnus-point-at-bol)
5090               (prog1
5091                   (1+ (gnus-point-at-eol))
5092                 (gnus-delete-line))))))
5093       (when old-header
5094         (mail-header-set-number header (mail-header-number old-header)))
5095       (setq gnus-newsgroup-sparse
5096             (delq (setq number (mail-header-number header))
5097                   gnus-newsgroup-sparse))
5098       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5099       (push number gnus-newsgroup-limit)
5100       (gnus-rebuild-thread (mail-header-id header) line)
5101       (gnus-summary-goto-subject number nil t))
5102     (when (and (numberp number)
5103                (> number 0))
5104       ;; We have to update the boundaries even if we can't fetch the
5105       ;; article if ID is a number -- so that the next `P' or `N'
5106       ;; command will fetch the previous (or next) article even
5107       ;; if the one we tried to fetch this time has been canceled.
5108       (when (> number gnus-newsgroup-end)
5109         (setq gnus-newsgroup-end number))
5110       (when (< number gnus-newsgroup-begin)
5111         (setq gnus-newsgroup-begin number))
5112       (setq gnus-newsgroup-unselected
5113             (delq number gnus-newsgroup-unselected)))
5114     ;; Report back a success?
5115     (and header (mail-header-number header))))
5116
5117 ;;; Process/prefix in the summary buffer
5118
5119 (defun gnus-summary-work-articles (n)
5120   "Return a list of articles to be worked upon.
5121 The prefix argument, the list of process marked articles, and the
5122 current article will be taken into consideration."
5123   (save-excursion
5124     (set-buffer gnus-summary-buffer)
5125     (cond
5126      (n
5127       ;; A numerical prefix has been given.
5128       (setq n (prefix-numeric-value n))
5129       (let ((backward (< n 0))
5130             (n (abs (prefix-numeric-value n)))
5131             articles article)
5132         (save-excursion
5133           (while
5134               (and (> n 0)
5135                    (push (setq article (gnus-summary-article-number))
5136                          articles)
5137                    (if backward
5138                        (gnus-summary-find-prev nil article)
5139                      (gnus-summary-find-next nil article)))
5140             (decf n)))
5141         (nreverse articles)))
5142      ((and (gnus-region-active-p) (mark))
5143       (message "region active")
5144       ;; Work on the region between point and mark.
5145       (let ((max (max (point) (mark)))
5146             articles article)
5147         (save-excursion
5148           (goto-char (min (min (point) (mark))))
5149           (while
5150               (and
5151                (push (setq article (gnus-summary-article-number)) articles)
5152                (gnus-summary-find-next nil article)
5153                (< (point) max)))
5154           (nreverse articles))))
5155      (gnus-newsgroup-processable
5156       ;; There are process-marked articles present.
5157       ;; Save current state.
5158       (gnus-summary-save-process-mark)
5159       ;; Return the list.
5160       (reverse gnus-newsgroup-processable))
5161      (t
5162       ;; Just return the current article.
5163       (list (gnus-summary-article-number))))))
5164
5165 (defmacro gnus-summary-iterate (arg &rest forms)
5166   "Iterate over the process/prefixed articles and do FORMS.
5167 ARG is the interactive prefix given to the command.  FORMS will be
5168 executed with point over the summary line of the articles."
5169   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5170     `(let ((,articles (gnus-summary-work-articles ,arg)))
5171        (while ,articles
5172          (gnus-summary-goto-subject (car ,articles))
5173          ,@forms
5174          (pop ,articles)))))
5175
5176 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5177 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5178
5179 (defun gnus-summary-save-process-mark ()
5180   "Push the current set of process marked articles on the stack."
5181   (interactive)
5182   (push (copy-sequence gnus-newsgroup-processable)
5183         gnus-newsgroup-process-stack))
5184
5185 (defun gnus-summary-kill-process-mark ()
5186   "Push the current set of process marked articles on the stack and unmark."
5187   (interactive)
5188   (gnus-summary-save-process-mark)
5189   (gnus-summary-unmark-all-processable))
5190
5191 (defun gnus-summary-yank-process-mark ()
5192   "Pop the last process mark state off the stack and restore it."
5193   (interactive)
5194   (unless gnus-newsgroup-process-stack
5195     (error "Empty mark stack"))
5196   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5197
5198 (defun gnus-summary-process-mark-set (set)
5199   "Make SET into the current process marked articles."
5200   (gnus-summary-unmark-all-processable)
5201   (while set
5202     (gnus-summary-set-process-mark (pop set))))
5203
5204 ;;; Searching and stuff
5205
5206 (defun gnus-summary-search-group (&optional backward use-level)
5207   "Search for next unread newsgroup.
5208 If optional argument BACKWARD is non-nil, search backward instead."
5209   (save-excursion
5210     (set-buffer gnus-group-buffer)
5211     (when (gnus-group-search-forward
5212            backward nil (if use-level (gnus-group-group-level) nil))
5213       (gnus-group-group-name))))
5214
5215 (defun gnus-summary-best-group (&optional exclude-group)
5216   "Find the name of the best unread group.
5217 If EXCLUDE-GROUP, do not go to this group."
5218   (save-excursion
5219     (set-buffer gnus-group-buffer)
5220     (save-excursion
5221       (gnus-group-best-unread-group exclude-group))))
5222
5223 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5224   (if backward (gnus-summary-find-prev)
5225     (let* ((dummy (gnus-summary-article-intangible-p))
5226            (article (or article (gnus-summary-article-number)))
5227            (arts (gnus-data-find-list article))
5228            result)
5229       (when (and (not dummy)
5230                  (or (not gnus-summary-check-current)
5231                      (not unread)
5232                      (not (gnus-data-unread-p (car arts)))))
5233         (setq arts (cdr arts)))
5234       (when (setq result
5235                   (if unread
5236                       (progn
5237                         (while arts
5238                           (when (or (and undownloaded
5239                                          (eq gnus-undownloaded-mark
5240                                              (gnus-data-mark (car arts))))
5241                                     (gnus-data-unread-p (car arts)))
5242                             (setq result (car arts)
5243                                   arts nil))
5244                           (setq arts (cdr arts)))
5245                         result)
5246                     (car arts)))
5247         (goto-char (gnus-data-pos result))
5248         (gnus-data-number result)))))
5249
5250 (defun gnus-summary-find-prev (&optional unread article)
5251   (let* ((eobp (eobp))
5252          (article (or article (gnus-summary-article-number)))
5253          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5254          result)
5255     (when (and (not eobp)
5256                (or (not gnus-summary-check-current)
5257                    (not unread)
5258                    (not (gnus-data-unread-p (car arts)))))
5259       (setq arts (cdr arts)))
5260     (when (setq result
5261                 (if unread
5262                     (progn
5263                       (while arts
5264                         (when (gnus-data-unread-p (car arts))
5265                           (setq result (car arts)
5266                                 arts nil))
5267                         (setq arts (cdr arts)))
5268                       result)
5269                   (car arts)))
5270       (goto-char (gnus-data-pos result))
5271       (gnus-data-number result))))
5272
5273 (defun gnus-summary-find-subject (subject &optional unread backward article)
5274   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5275          (article (or article (gnus-summary-article-number)))
5276          (articles (gnus-data-list backward))
5277          (arts (gnus-data-find-list article articles))
5278          result)
5279     (when (or (not gnus-summary-check-current)
5280               (not unread)
5281               (not (gnus-data-unread-p (car arts))))
5282       (setq arts (cdr arts)))
5283     (while arts
5284       (and (or (not unread)
5285                (gnus-data-unread-p (car arts)))
5286            (vectorp (gnus-data-header (car arts)))
5287            (gnus-subject-equal
5288             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5289            (setq result (car arts)
5290                  arts nil))
5291       (setq arts (cdr arts)))
5292     (and result
5293          (goto-char (gnus-data-pos result))
5294          (gnus-data-number result))))
5295
5296 (defun gnus-summary-search-forward (&optional unread subject backward)
5297   "Search forward for an article.
5298 If UNREAD, look for unread articles.  If SUBJECT, look for
5299 articles with that subject.  If BACKWARD, search backward instead."
5300   (cond (subject (gnus-summary-find-subject subject unread backward))
5301         (backward (gnus-summary-find-prev unread))
5302         (t (gnus-summary-find-next unread))))
5303
5304 (defun gnus-recenter (&optional n)
5305   "Center point in window and redisplay frame.
5306 Also do horizontal recentering."
5307   (interactive "P")
5308   (when (and gnus-auto-center-summary
5309              (not (eq gnus-auto-center-summary 'vertical)))
5310     (gnus-horizontal-recenter))
5311   (recenter n))
5312
5313 (defun gnus-summary-recenter ()
5314   "Center point in the summary window.
5315 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5316 displayed, no centering will be performed."
5317   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5318   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5319   (interactive)
5320   (let* ((top (cond ((< (window-height) 4) 0)
5321                     ((< (window-height) 7) 1)
5322                     (t (if (numberp gnus-auto-center-summary)
5323                            gnus-auto-center-summary
5324                          2))))
5325          (height (1- (window-height)))
5326          (bottom (save-excursion (goto-char (point-max))
5327                                  (forward-line (- height))
5328                                  (point)))
5329          (window (get-buffer-window (current-buffer))))
5330     ;; The user has to want it.
5331     (when gnus-auto-center-summary
5332       (when (get-buffer-window gnus-article-buffer)
5333         ;; Only do recentering when the article buffer is displayed,
5334         ;; Set the window start to either `bottom', which is the biggest
5335         ;; possible valid number, or the second line from the top,
5336         ;; whichever is the least.
5337         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5338           (if (> bottom top-pos)
5339               ;; Keep the second line from the top visible
5340               (set-window-start window top-pos t)
5341             ;; Try to keep the bottom line visible; if it's partially
5342             ;; obscured, either scroll one more line to make it fully
5343             ;; visible, or revert to using TOP-POS.
5344             (save-excursion
5345               (goto-char (point-max))
5346               (forward-line -1)
5347               (let ((last-line-start (point)))
5348                 (goto-char bottom)
5349                 (set-window-start window (point) t)
5350                 (when (not (pos-visible-in-window-p last-line-start window))
5351                   (forward-line 1)
5352                   (set-window-start window (min (point) top-pos) t)))))))
5353       ;; Do horizontal recentering while we're at it.
5354       (when (and (get-buffer-window (current-buffer) t)
5355                  (not (eq gnus-auto-center-summary 'vertical)))
5356         (let ((selected (selected-window)))
5357           (select-window (get-buffer-window (current-buffer) t))
5358           (gnus-summary-position-point)
5359           (gnus-horizontal-recenter)
5360           (select-window selected))))))
5361
5362 (defun gnus-summary-jump-to-group (newsgroup)
5363   "Move point to NEWSGROUP in group mode buffer."
5364   ;; Keep update point of group mode buffer if visible.
5365   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5366       (save-window-excursion
5367         ;; Take care of tree window mode.
5368         (when (get-buffer-window gnus-group-buffer)
5369           (pop-to-buffer gnus-group-buffer))
5370         (gnus-group-jump-to-group newsgroup))
5371     (save-excursion
5372       ;; Take care of tree window mode.
5373       (if (get-buffer-window gnus-group-buffer)
5374           (pop-to-buffer gnus-group-buffer)
5375         (set-buffer gnus-group-buffer))
5376       (gnus-group-jump-to-group newsgroup))))
5377
5378 ;; This function returns a list of article numbers based on the
5379 ;; difference between the ranges of read articles in this group and
5380 ;; the range of active articles.
5381 (defun gnus-list-of-unread-articles (group)
5382   (let* ((read (gnus-info-read (gnus-get-info group)))
5383          (active (or (gnus-active group) (gnus-activate-group group)))
5384          (last (cdr active))
5385          first nlast unread)
5386     ;; If none are read, then all are unread.
5387     (if (not read)
5388         (setq first (car active))
5389       ;; If the range of read articles is a single range, then the
5390       ;; first unread article is the article after the last read
5391       ;; article.  Sounds logical, doesn't it?
5392       (if (and (not (listp (cdr read)))
5393                (or (< (car read) (car active))
5394                    (progn (setq read (list read))
5395                           nil)))
5396           (setq first (max (car active) (1+ (cdr read))))
5397         ;; `read' is a list of ranges.
5398         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5399                                   (caar read)))
5400                   1)
5401           (setq first (car active)))
5402         (while read
5403           (when first
5404             (while (< first nlast)
5405               (push first unread)
5406               (setq first (1+ first))))
5407           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5408           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5409           (setq read (cdr read)))))
5410     ;; And add the last unread articles.
5411     (while (<= first last)
5412       (push first unread)
5413       (setq first (1+ first)))
5414     ;; Return the list of unread articles.
5415     (delq 0 (nreverse unread))))
5416
5417 (defun gnus-list-of-read-articles (group)
5418   "Return a list of unread, unticked and non-dormant articles."
5419   (let* ((info (gnus-get-info group))
5420          (marked (gnus-info-marks info))
5421          (active (gnus-active group)))
5422     (and info active
5423          (gnus-set-difference
5424           (gnus-sorted-complement
5425            (gnus-uncompress-range active)
5426            (gnus-list-of-unread-articles group))
5427           (append
5428            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5429            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5430
5431 ;; Various summary commands
5432
5433 (defun gnus-summary-select-article-buffer ()
5434   "Reconfigure windows to show article buffer."
5435   (interactive)
5436   (if (not (gnus-buffer-live-p gnus-article-buffer))
5437       (error "There is no article buffer for this summary buffer")
5438     (gnus-configure-windows 'article)
5439     (select-window (get-buffer-window gnus-article-buffer))))
5440
5441 (defun gnus-summary-universal-argument (arg)
5442   "Perform any operation on all articles that are process/prefixed."
5443   (interactive "P")
5444   (let ((articles (gnus-summary-work-articles arg))
5445         func article)
5446     (if (eq
5447          (setq
5448           func
5449           (key-binding
5450            (read-key-sequence
5451             (substitute-command-keys
5452              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5453          'undefined)
5454         (gnus-error 1 "Undefined key")
5455       (save-excursion
5456         (while articles
5457           (gnus-summary-goto-subject (setq article (pop articles)))
5458           (let (gnus-newsgroup-processable)
5459             (command-execute func))
5460           (gnus-summary-remove-process-mark article)))))
5461   (gnus-summary-position-point))
5462
5463 (defun gnus-summary-toggle-truncation (&optional arg)
5464   "Toggle truncation of summary lines.
5465 With arg, turn line truncation on iff arg is positive."
5466   (interactive "P")
5467   (setq truncate-lines
5468         (if (null arg) (not truncate-lines)
5469           (> (prefix-numeric-value arg) 0)))
5470   (redraw-display))
5471
5472 (defun gnus-summary-reselect-current-group (&optional all rescan)
5473   "Exit and then reselect the current newsgroup.
5474 The prefix argument ALL means to select all articles."
5475   (interactive "P")
5476   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5477     (error "Ephemeral groups can't be reselected"))
5478   (let ((current-subject (gnus-summary-article-number))
5479         (group gnus-newsgroup-name))
5480     (setq gnus-newsgroup-begin nil)
5481     (gnus-summary-exit)
5482     ;; We have to adjust the point of group mode buffer because
5483     ;; point was moved to the next unread newsgroup by exiting.
5484     (gnus-summary-jump-to-group group)
5485     (when rescan
5486       (save-excursion
5487         (gnus-group-get-new-news-this-group 1)))
5488     (gnus-group-read-group all t)
5489     (gnus-summary-goto-subject current-subject nil t)))
5490
5491 (defun gnus-summary-rescan-group (&optional all)
5492   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5493   (interactive "P")
5494   (gnus-summary-reselect-current-group all t))
5495
5496 (defun gnus-summary-update-info (&optional non-destructive)
5497   (save-excursion
5498     (let ((group gnus-newsgroup-name))
5499       (when group
5500         (when gnus-newsgroup-kill-headers
5501           (setq gnus-newsgroup-killed
5502                 (gnus-compress-sequence
5503                  (nconc
5504                   (gnus-set-sorted-intersection
5505                    (gnus-uncompress-range gnus-newsgroup-killed)
5506                    (setq gnus-newsgroup-unselected
5507                          (sort gnus-newsgroup-unselected '<)))
5508                   (setq gnus-newsgroup-unreads
5509                         (sort gnus-newsgroup-unreads '<)))
5510                  t)))
5511         (unless (listp (cdr gnus-newsgroup-killed))
5512           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5513         (let ((headers gnus-newsgroup-headers))
5514           ;; Set the new ranges of read articles.
5515           (save-excursion
5516             (set-buffer gnus-group-buffer)
5517             (gnus-undo-force-boundary))
5518           (gnus-update-read-articles
5519            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5520           ;; Set the current article marks.
5521           (let ((gnus-newsgroup-scored
5522                  (if (and (not gnus-save-score)
5523                           (not non-destructive))
5524                      nil
5525                    gnus-newsgroup-scored)))
5526             (save-excursion
5527               (gnus-update-marks)))
5528           ;; Do the cross-ref thing.
5529           (when gnus-use-cross-reference
5530             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5531           ;; Do not switch windows but change the buffer to work.
5532           (set-buffer gnus-group-buffer)
5533           (unless (gnus-ephemeral-group-p group)
5534             (gnus-group-update-group group)))))))
5535
5536 (defun gnus-summary-save-newsrc (&optional force)
5537   "Save the current number of read/marked articles in the dribble buffer.
5538 The dribble buffer will then be saved.
5539 If FORCE (the prefix), also save the .newsrc file(s)."
5540   (interactive "P")
5541   (gnus-summary-update-info t)
5542   (if force
5543       (gnus-save-newsrc-file)
5544     (gnus-dribble-save)))
5545
5546 (defun gnus-summary-exit (&optional temporary)
5547   "Exit reading current newsgroup, and then return to group selection mode.
5548 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5549   (interactive)
5550   (gnus-set-global-variables)
5551   (when (gnus-buffer-live-p gnus-article-buffer)
5552     (save-excursion
5553       (set-buffer gnus-article-buffer)
5554       (mm-destroy-parts gnus-article-mime-handles)
5555       ;; Set it to nil for safety reason.
5556       (setq gnus-article-mime-handle-alist nil)
5557       (setq gnus-article-mime-handles nil)))
5558   (gnus-kill-save-kill-buffer)
5559   (gnus-async-halt-prefetch)
5560   (let* ((group gnus-newsgroup-name)
5561          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5562          (mode major-mode)
5563          (group-point nil)
5564          (buf (current-buffer)))
5565     (unless quit-config
5566       ;; Do adaptive scoring, and possibly save score files.
5567       (when gnus-newsgroup-adaptive
5568         (gnus-score-adaptive))
5569       (when gnus-use-scoring
5570         (gnus-score-save)))
5571     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5572     ;; If we have several article buffers, we kill them at exit.
5573     (unless gnus-single-article-buffer
5574       (gnus-kill-buffer gnus-original-article-buffer)
5575       (setq gnus-article-current nil))
5576     (when gnus-use-cache
5577       (gnus-cache-possibly-remove-articles)
5578       (gnus-cache-save-buffers))
5579     (gnus-async-prefetch-remove-group group)
5580     (when gnus-suppress-duplicates
5581       (gnus-dup-enter-articles))
5582     (when gnus-use-trees
5583       (gnus-tree-close group))
5584     (when gnus-use-cache
5585       (gnus-cache-write-active))
5586     ;; Remove entries for this group.
5587     (nnmail-purge-split-history (gnus-group-real-name group))
5588     ;; Make all changes in this group permanent.
5589     (unless quit-config
5590       (gnus-run-hooks 'gnus-exit-group-hook)
5591       (gnus-summary-update-info))
5592     (gnus-close-group group)
5593     ;; Make sure where we were, and go to next newsgroup.
5594     (set-buffer gnus-group-buffer)
5595     (unless quit-config
5596       (gnus-group-jump-to-group group))
5597     (gnus-run-hooks 'gnus-summary-exit-hook)
5598     (unless (or quit-config
5599                 ;; If this group has disappeared from the summary
5600                 ;; buffer, don't skip forwards.
5601                 (not (string= group (gnus-group-group-name))))
5602       (gnus-group-next-unread-group 1))
5603     (setq group-point (point))
5604     (if temporary
5605         nil                             ;Nothing to do.
5606       ;; If we have several article buffers, we kill them at exit.
5607       (unless gnus-single-article-buffer
5608         (gnus-kill-buffer gnus-article-buffer)
5609         (gnus-kill-buffer gnus-original-article-buffer)
5610         (setq gnus-article-current nil))
5611       (set-buffer buf)
5612       (if (not gnus-kill-summary-on-exit)
5613           (gnus-deaden-summary)
5614         ;; We set all buffer-local variables to nil.  It is unclear why
5615         ;; this is needed, but if we don't, buffer-local variables are
5616         ;; not garbage-collected, it seems.  This would the lead to en
5617         ;; ever-growing Emacs.
5618         (gnus-summary-clear-local-variables)
5619         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5620           (gnus-summary-clear-local-variables))
5621         (when (get-buffer gnus-article-buffer)
5622           (bury-buffer gnus-article-buffer))
5623         ;; We clear the global counterparts of the buffer-local
5624         ;; variables as well, just to be on the safe side.
5625         (set-buffer gnus-group-buffer)
5626         (gnus-summary-clear-local-variables)
5627         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5628           (gnus-summary-clear-local-variables))
5629         ;; Return to group mode buffer.
5630         (when (eq mode 'gnus-summary-mode)
5631           (gnus-kill-buffer buf)))
5632       (setq gnus-current-select-method gnus-select-method)
5633       (pop-to-buffer gnus-group-buffer)
5634       (if (not quit-config)
5635           (progn
5636             (goto-char group-point)
5637             (gnus-configure-windows 'group 'force))
5638         (gnus-handle-ephemeral-exit quit-config))
5639       ;; Clear the current group name.
5640       (unless quit-config
5641         (setq gnus-newsgroup-name nil)))))
5642
5643 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5644 (defun gnus-summary-exit-no-update (&optional no-questions)
5645   "Quit reading current newsgroup without updating read article info."
5646   (interactive)
5647   (let* ((group gnus-newsgroup-name)
5648          (quit-config (gnus-group-quit-config group)))
5649     (when (or no-questions
5650               gnus-expert-user
5651               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5652       (gnus-async-halt-prefetch)
5653       (mapcar 'funcall
5654               (delq 'gnus-summary-expire-articles
5655                     (copy-sequence gnus-summary-prepare-exit-hook)))
5656       (when (gnus-buffer-live-p gnus-article-buffer)
5657         (save-excursion
5658           (set-buffer gnus-article-buffer)
5659           (mm-destroy-parts gnus-article-mime-handles)
5660           ;; Set it to nil for safety reason.
5661           (setq gnus-article-mime-handle-alist nil)
5662           (setq gnus-article-mime-handles nil)))
5663       ;; If we have several article buffers, we kill them at exit.
5664       (unless gnus-single-article-buffer
5665         (gnus-kill-buffer gnus-article-buffer)
5666         (gnus-kill-buffer gnus-original-article-buffer)
5667         (setq gnus-article-current nil))
5668       (if (not gnus-kill-summary-on-exit)
5669           (gnus-deaden-summary)
5670         (gnus-close-group group)
5671         (gnus-summary-clear-local-variables)
5672         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5673           (gnus-summary-clear-local-variables))
5674         (set-buffer gnus-group-buffer)
5675         (gnus-summary-clear-local-variables)
5676         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5677           (gnus-summary-clear-local-variables))
5678         (when (get-buffer gnus-summary-buffer)
5679           (kill-buffer gnus-summary-buffer)))
5680       (unless gnus-single-article-buffer
5681         (setq gnus-article-current nil))
5682       (when gnus-use-trees
5683         (gnus-tree-close group))
5684       (gnus-async-prefetch-remove-group group)
5685       (when (get-buffer gnus-article-buffer)
5686         (bury-buffer gnus-article-buffer))
5687       ;; Return to the group buffer.
5688       (gnus-configure-windows 'group 'force)
5689       ;; Clear the current group name.
5690       (setq gnus-newsgroup-name nil)
5691       (when (equal (gnus-group-group-name) group)
5692         (gnus-group-next-unread-group 1))
5693       (when quit-config
5694         (gnus-handle-ephemeral-exit quit-config)))))
5695
5696 (defun gnus-handle-ephemeral-exit (quit-config)
5697   "Handle movement when leaving an ephemeral group.
5698 The state which existed when entering the ephemeral is reset."
5699   (if (not (buffer-name (car quit-config)))
5700       (gnus-configure-windows 'group 'force)
5701     (set-buffer (car quit-config))
5702     (cond ((eq major-mode 'gnus-summary-mode)
5703            (gnus-set-global-variables))
5704           ((eq major-mode 'gnus-article-mode)
5705            (save-excursion
5706              ;; The `gnus-summary-buffer' variable may point
5707              ;; to the old summary buffer when using a single
5708              ;; article buffer.
5709              (unless (gnus-buffer-live-p gnus-summary-buffer)
5710                (set-buffer gnus-group-buffer))
5711              (set-buffer gnus-summary-buffer)
5712              (gnus-set-global-variables))))
5713     (if (or (eq (cdr quit-config) 'article)
5714             (eq (cdr quit-config) 'pick))
5715         (progn
5716           ;; The current article may be from the ephemeral group
5717           ;; thus it is best that we reload this article
5718           (gnus-summary-show-article)
5719           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5720               (gnus-configure-windows 'pick 'force)
5721             (gnus-configure-windows (cdr quit-config) 'force)))
5722       (gnus-configure-windows (cdr quit-config) 'force))
5723     (when (eq major-mode 'gnus-summary-mode)
5724       (gnus-summary-next-subject 1 nil t)
5725       (gnus-summary-recenter)
5726       (gnus-summary-position-point))))
5727
5728 ;;; Dead summaries.
5729
5730 (defvar gnus-dead-summary-mode-map nil)
5731
5732 (unless gnus-dead-summary-mode-map
5733   (setq gnus-dead-summary-mode-map (make-keymap))
5734   (suppress-keymap gnus-dead-summary-mode-map)
5735   (substitute-key-definition
5736    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5737   (let ((keys '("\C-d" "\r" "\177" [delete])))
5738     (while keys
5739       (define-key gnus-dead-summary-mode-map
5740         (pop keys) 'gnus-summary-wake-up-the-dead))))
5741
5742 (defvar gnus-dead-summary-mode nil
5743   "Minor mode for Gnus summary buffers.")
5744
5745 (defun gnus-dead-summary-mode (&optional arg)
5746   "Minor mode for Gnus summary buffers."
5747   (interactive "P")
5748   (when (eq major-mode 'gnus-summary-mode)
5749     (make-local-variable 'gnus-dead-summary-mode)
5750     (setq gnus-dead-summary-mode
5751           (if (null arg) (not gnus-dead-summary-mode)
5752             (> (prefix-numeric-value arg) 0)))
5753     (when gnus-dead-summary-mode
5754       (gnus-add-minor-mode
5755        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5756
5757 (defun gnus-deaden-summary ()
5758   "Make the current summary buffer into a dead summary buffer."
5759   ;; Kill any previous dead summary buffer.
5760   (when (and gnus-dead-summary
5761              (buffer-name gnus-dead-summary))
5762     (save-excursion
5763       (set-buffer gnus-dead-summary)
5764       (when gnus-dead-summary-mode
5765         (kill-buffer (current-buffer)))))
5766   ;; Make this the current dead summary.
5767   (setq gnus-dead-summary (current-buffer))
5768   (gnus-dead-summary-mode 1)
5769   (let ((name (buffer-name)))
5770     (when (string-match "Summary" name)
5771       (rename-buffer
5772        (concat (substring name 0 (match-beginning 0)) "Dead "
5773                (substring name (match-beginning 0)))
5774        t)
5775       (bury-buffer))))
5776
5777 (defun gnus-kill-or-deaden-summary (buffer)
5778   "Kill or deaden the summary BUFFER."
5779   (save-excursion
5780     (when (and (buffer-name buffer)
5781                (not gnus-single-article-buffer))
5782       (save-excursion
5783         (set-buffer buffer)
5784         (gnus-kill-buffer gnus-article-buffer)
5785         (gnus-kill-buffer gnus-original-article-buffer)))
5786     (cond (gnus-kill-summary-on-exit
5787            (when (and gnus-use-trees
5788                       (gnus-buffer-exists-p buffer))
5789              (save-excursion
5790                (set-buffer buffer)
5791                (gnus-tree-close gnus-newsgroup-name)))
5792            (gnus-kill-buffer buffer))
5793           ((gnus-buffer-exists-p buffer)
5794            (save-excursion
5795              (set-buffer buffer)
5796              (gnus-deaden-summary))))))
5797
5798 (defun gnus-summary-wake-up-the-dead (&rest args)
5799   "Wake up the dead summary buffer."
5800   (interactive)
5801   (gnus-dead-summary-mode -1)
5802   (let ((name (buffer-name)))
5803     (when (string-match "Dead " name)
5804       (rename-buffer
5805        (concat (substring name 0 (match-beginning 0))
5806                (substring name (match-end 0)))
5807        t)))
5808   (gnus-message 3 "This dead summary is now alive again"))
5809
5810 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5811 (defun gnus-summary-fetch-faq (&optional faq-dir)
5812   "Fetch the FAQ for the current group.
5813 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5814 in."
5815   (interactive
5816    (list
5817     (when current-prefix-arg
5818       (completing-read
5819        "Faq dir: " (and (listp gnus-group-faq-directory)
5820                         (mapcar (lambda (file) (list file))
5821                                 gnus-group-faq-directory))))))
5822   (let (gnus-faq-buffer)
5823     (when (setq gnus-faq-buffer
5824                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5825       (gnus-configure-windows 'summary-faq))))
5826
5827 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5828 (defun gnus-summary-describe-group (&optional force)
5829   "Describe the current newsgroup."
5830   (interactive "P")
5831   (gnus-group-describe-group force gnus-newsgroup-name))
5832
5833 (defun gnus-summary-describe-briefly ()
5834   "Describe summary mode commands briefly."
5835   (interactive)
5836   (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")))
5837
5838 ;; Walking around group mode buffer from summary mode.
5839
5840 (defun gnus-summary-next-group (&optional no-article target-group backward)
5841   "Exit current newsgroup and then select next unread newsgroup.
5842 If prefix argument NO-ARTICLE is non-nil, no article is selected
5843 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5844 previous group instead."
5845   (interactive "P")
5846   ;; Stop pre-fetching.
5847   (gnus-async-halt-prefetch)
5848   (let ((current-group gnus-newsgroup-name)
5849         (current-buffer (current-buffer))
5850         entered)
5851     ;; First we semi-exit this group to update Xrefs and all variables.
5852     ;; We can't do a real exit, because the window conf must remain
5853     ;; the same in case the user is prompted for info, and we don't
5854     ;; want the window conf to change before that...
5855     (gnus-summary-exit t)
5856     (while (not entered)
5857       ;; Then we find what group we are supposed to enter.
5858       (set-buffer gnus-group-buffer)
5859       (gnus-group-jump-to-group current-group)
5860       (setq target-group
5861             (or target-group
5862                 (if (eq gnus-keep-same-level 'best)
5863                     (gnus-summary-best-group gnus-newsgroup-name)
5864                   (gnus-summary-search-group backward gnus-keep-same-level))))
5865       (if (not target-group)
5866           ;; There are no further groups, so we return to the group
5867           ;; buffer.
5868           (progn
5869             (gnus-message 5 "Returning to the group buffer")
5870             (setq entered t)
5871             (when (gnus-buffer-live-p current-buffer)
5872               (set-buffer current-buffer)
5873               (gnus-summary-exit))
5874             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5875         ;; We try to enter the target group.
5876         (gnus-group-jump-to-group target-group)
5877         (let ((unreads (gnus-group-group-unread)))
5878           (if (and (or (eq t unreads)
5879                        (and unreads (not (zerop unreads))))
5880                    (gnus-summary-read-group
5881                     target-group nil no-article
5882                     (and (buffer-name current-buffer) current-buffer)
5883                     nil backward))
5884               (setq entered t)
5885             (setq current-group target-group
5886                   target-group nil)))))))
5887
5888 (defun gnus-summary-prev-group (&optional no-article)
5889   "Exit current newsgroup and then select previous unread newsgroup.
5890 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5891   (interactive "P")
5892   (gnus-summary-next-group no-article nil t))
5893
5894 ;; Walking around summary lines.
5895
5896 (defun gnus-summary-first-subject (&optional unread undownloaded)
5897   "Go to the first unread subject.
5898 If UNREAD is non-nil, go to the first unread article.
5899 Returns the article selected or nil if there are no unread articles."
5900   (interactive "P")
5901   (prog1
5902       (cond
5903        ;; Empty summary.
5904        ((null gnus-newsgroup-data)
5905         (gnus-message 3 "No articles in the group")
5906         nil)
5907        ;; Pick the first article.
5908        ((not unread)
5909         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5910         (gnus-data-number (car gnus-newsgroup-data)))
5911        ;; No unread articles.
5912        ((null gnus-newsgroup-unreads)
5913         (gnus-message 3 "No more unread articles")
5914         nil)
5915        ;; Find the first unread article.
5916        (t
5917         (let ((data gnus-newsgroup-data))
5918           (while (and data
5919                       (and (not (and undownloaded
5920                                      (eq gnus-undownloaded-mark
5921                                          (gnus-data-mark (car data)))))
5922                            (not (gnus-data-unread-p (car data)))))
5923             (setq data (cdr data)))
5924           (when data
5925             (goto-char (gnus-data-pos (car data)))
5926             (gnus-data-number (car data))))))
5927     (gnus-summary-position-point)))
5928
5929 (defun gnus-summary-next-subject (n &optional unread dont-display)
5930   "Go to next N'th summary line.
5931 If N is negative, go to the previous N'th subject line.
5932 If UNREAD is non-nil, only unread articles are selected.
5933 The difference between N and the actual number of steps taken is
5934 returned."
5935   (interactive "p")
5936   (let ((backward (< n 0))
5937         (n (abs n)))
5938     (while (and (> n 0)
5939                 (if backward
5940                     (gnus-summary-find-prev unread)
5941                   (gnus-summary-find-next unread)))
5942       (unless (zerop (setq n (1- n)))
5943         (gnus-summary-show-thread)))
5944     (when (/= 0 n)
5945       (gnus-message 7 "No more%s articles"
5946                     (if unread " unread" "")))
5947     (unless dont-display
5948       (gnus-summary-recenter)
5949       (gnus-summary-position-point))
5950     n))
5951
5952 (defun gnus-summary-next-unread-subject (n)
5953   "Go to next N'th unread summary line."
5954   (interactive "p")
5955   (gnus-summary-next-subject n t))
5956
5957 (defun gnus-summary-prev-subject (n &optional unread)
5958   "Go to previous N'th summary line.
5959 If optional argument UNREAD is non-nil, only unread article is selected."
5960   (interactive "p")
5961   (gnus-summary-next-subject (- n) unread))
5962
5963 (defun gnus-summary-prev-unread-subject (n)
5964   "Go to previous N'th unread summary line."
5965   (interactive "p")
5966   (gnus-summary-next-subject (- n) t))
5967
5968 (defun gnus-summary-goto-subject (article &optional force silent)
5969   "Go the subject line of ARTICLE.
5970 If FORCE, also allow jumping to articles not currently shown."
5971   (interactive "nArticle number: ")
5972   (let ((b (point))
5973         (data (gnus-data-find article)))
5974     ;; We read in the article if we have to.
5975     (and (not data)
5976          force
5977          (gnus-summary-insert-subject
5978           article
5979           (if (or (numberp force) (vectorp force)) force)
5980           t)
5981          (setq data (gnus-data-find article)))
5982     (goto-char b)
5983     (if (not data)
5984         (progn
5985           (unless silent
5986             (gnus-message 3 "Can't find article %d" article))
5987           nil)
5988       (goto-char (gnus-data-pos data))
5989       (gnus-summary-position-point)
5990       article)))
5991
5992 ;; Walking around summary lines with displaying articles.
5993
5994 (defun gnus-summary-expand-window (&optional arg)
5995   "Make the summary buffer take up the entire Emacs frame.
5996 Given a prefix, will force an `article' buffer configuration."
5997   (interactive "P")
5998   (if arg
5999       (gnus-configure-windows 'article 'force)
6000     (gnus-configure-windows 'summary 'force)))
6001
6002 (defun gnus-summary-display-article (article &optional all-header)
6003   "Display ARTICLE in article buffer."
6004   (when (gnus-buffer-live-p gnus-article-buffer)
6005     (with-current-buffer gnus-article-buffer
6006       (mm-enable-multibyte-mule4)))
6007   (gnus-set-global-variables)
6008   (when (gnus-buffer-live-p gnus-article-buffer)
6009     (with-current-buffer gnus-article-buffer
6010       (setq gnus-article-charset gnus-newsgroup-charset)
6011       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6012       (mm-enable-multibyte-mule4)))
6013   (if (null article)
6014       nil
6015     (prog1
6016         (if gnus-summary-display-article-function
6017             (funcall gnus-summary-display-article-function article all-header)
6018           (gnus-article-prepare article all-header))
6019       (gnus-run-hooks 'gnus-select-article-hook)
6020       (when (and gnus-current-article
6021                  (not (zerop gnus-current-article)))
6022         (gnus-summary-goto-subject gnus-current-article))
6023       (gnus-summary-recenter)
6024       (when (and gnus-use-trees gnus-show-threads)
6025         (gnus-possibly-generate-tree article)
6026         (gnus-highlight-selected-tree article))
6027       ;; Successfully display article.
6028       (gnus-article-set-window-start
6029        (cdr (assq article gnus-newsgroup-bookmarks))))))
6030
6031 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6032   "Select the current article.
6033 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6034 non-nil, the article will be re-fetched even if it already present in
6035 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6036 be displayed."
6037   ;; Make sure we are in the summary buffer to work around bbdb bug.
6038   (unless (eq major-mode 'gnus-summary-mode)
6039     (set-buffer gnus-summary-buffer))
6040   (let ((article (or article (gnus-summary-article-number)))
6041         (all-headers (not (not all-headers))) ;Must be T or NIL.
6042         gnus-summary-display-article-function)
6043     (and (not pseudo)
6044          (gnus-summary-article-pseudo-p article)
6045          (error "This is a pseudo-article"))
6046     (save-excursion
6047       (set-buffer gnus-summary-buffer)
6048       (if (or (and gnus-single-article-buffer
6049                    (or (null gnus-current-article)
6050                        (null gnus-article-current)
6051                        (null (get-buffer gnus-article-buffer))
6052                        (not (eq article (cdr gnus-article-current)))
6053                        (not (equal (car gnus-article-current)
6054                                    gnus-newsgroup-name))))
6055               (and (not gnus-single-article-buffer)
6056                    (or (null gnus-current-article)
6057                        (not (eq gnus-current-article article))))
6058               force)
6059           ;; The requested article is different from the current article.
6060           (progn
6061             (gnus-summary-display-article article all-headers)
6062             (when (gnus-buffer-live-p gnus-article-buffer)
6063                (with-current-buffer gnus-article-buffer
6064                 (if (not gnus-article-decoded-p) ;; a local variable
6065                     (mm-disable-multibyte-mule4))))
6066             (when (or all-headers gnus-show-all-headers)
6067               (gnus-article-show-all-headers))
6068             (gnus-article-set-window-start
6069              (cdr (assq article gnus-newsgroup-bookmarks)))
6070             article)
6071         (when (or all-headers gnus-show-all-headers)
6072           (gnus-article-show-all-headers))
6073         'old))))
6074
6075 (defun gnus-summary-force-verify-and-decrypt ()
6076   (interactive)
6077   (let ((mm-verify-option 'known)
6078         (mm-decrypt-option 'known))
6079     (gnus-summary-select-article nil 'force)))
6080
6081 (defun gnus-summary-set-current-mark (&optional current-mark)
6082   "Obsolete function."
6083   nil)
6084
6085 (defun gnus-summary-next-article (&optional unread subject backward push)
6086   "Select the next article.
6087 If UNREAD, only unread articles are selected.
6088 If SUBJECT, only articles with SUBJECT are selected.
6089 If BACKWARD, the previous article is selected instead of the next."
6090   (interactive "P")
6091   (cond
6092    ;; Is there such an article?
6093    ((and (gnus-summary-search-forward unread subject backward)
6094          (or (gnus-summary-display-article (gnus-summary-article-number))
6095              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6096     (gnus-summary-position-point))
6097    ;; If not, we try the first unread, if that is wanted.
6098    ((and subject
6099          gnus-auto-select-same
6100          (gnus-summary-first-unread-article))
6101     (gnus-summary-position-point)
6102     (gnus-message 6 "Wrapped"))
6103    ;; Try to get next/previous article not displayed in this group.
6104    ((and gnus-auto-extend-newsgroup
6105          (not unread) (not subject))
6106     (gnus-summary-goto-article
6107      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6108      nil (count-lines (point-min) (point))))
6109    ;; Go to next/previous group.
6110    (t
6111     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6112       (gnus-summary-jump-to-group gnus-newsgroup-name))
6113     (let ((cmd last-command-char)
6114           (point
6115            (save-excursion
6116              (set-buffer gnus-group-buffer)
6117              (point)))
6118           (group
6119            (if (eq gnus-keep-same-level 'best)
6120                (gnus-summary-best-group gnus-newsgroup-name)
6121              (gnus-summary-search-group backward gnus-keep-same-level))))
6122       ;; For some reason, the group window gets selected.  We change
6123       ;; it back.
6124       (select-window (get-buffer-window (current-buffer)))
6125       ;; Select next unread newsgroup automagically.
6126       (cond
6127        ((or (not gnus-auto-select-next)
6128             (not cmd))
6129         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6130        ((or (eq gnus-auto-select-next 'quietly)
6131             (and (eq gnus-auto-select-next 'slightly-quietly)
6132                  push)
6133             (and (eq gnus-auto-select-next 'almost-quietly)
6134                  (gnus-summary-last-article-p)))
6135         ;; Select quietly.
6136         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6137             (gnus-summary-exit)
6138           (gnus-message 7 "No more%s articles (%s)..."
6139                         (if unread " unread" "")
6140                         (if group (concat "selecting " group)
6141                           "exiting"))
6142           (gnus-summary-next-group nil group backward)))
6143        (t
6144         (when (gnus-key-press-event-p last-input-event)
6145           (gnus-summary-walk-group-buffer
6146            gnus-newsgroup-name cmd unread backward point))))))))
6147
6148 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6149   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6150                       (?\C-p (gnus-group-prev-unread-group 1))))
6151         (cursor-in-echo-area t)
6152         keve key group ended)
6153     (save-excursion
6154       (set-buffer gnus-group-buffer)
6155       (goto-char start)
6156       (setq group
6157             (if (eq gnus-keep-same-level 'best)
6158                 (gnus-summary-best-group gnus-newsgroup-name)
6159               (gnus-summary-search-group backward gnus-keep-same-level))))
6160     (while (not ended)
6161       (gnus-message
6162        5 "No more%s articles%s" (if unread " unread" "")
6163        (if (and group
6164                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6165            (format " (Type %s for %s [%s])"
6166                    (single-key-description cmd) group
6167                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6168          (format " (Type %s to exit %s)"
6169                  (single-key-description cmd)
6170                  gnus-newsgroup-name)))
6171       ;; Confirm auto selection.
6172       (setq key (car (setq keve (gnus-read-event-char))))
6173       (setq ended t)
6174       (cond
6175        ((assq key keystrokes)
6176         (let ((obuf (current-buffer)))
6177           (switch-to-buffer gnus-group-buffer)
6178           (when group
6179             (gnus-group-jump-to-group group))
6180           (eval (cadr (assq key keystrokes)))
6181           (setq group (gnus-group-group-name))
6182           (switch-to-buffer obuf))
6183         (setq ended nil))
6184        ((equal key cmd)
6185         (if (or (not group)
6186                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6187             (gnus-summary-exit)
6188           (gnus-summary-next-group nil group backward)))
6189        (t
6190         (push (cdr keve) unread-command-events))))))
6191
6192 (defun gnus-summary-next-unread-article ()
6193   "Select unread article after current one."
6194   (interactive)
6195   (gnus-summary-next-article
6196    (or (not (eq gnus-summary-goto-unread 'never))
6197        (gnus-summary-last-article-p (gnus-summary-article-number)))
6198    (and gnus-auto-select-same
6199         (gnus-summary-article-subject))))
6200
6201 (defun gnus-summary-prev-article (&optional unread subject)
6202   "Select the article after the current one.
6203 If UNREAD is non-nil, only unread articles are selected."
6204   (interactive "P")
6205   (gnus-summary-next-article unread subject t))
6206
6207 (defun gnus-summary-prev-unread-article ()
6208   "Select unread article before current one."
6209   (interactive)
6210   (gnus-summary-prev-article
6211    (or (not (eq gnus-summary-goto-unread 'never))
6212        (gnus-summary-first-article-p (gnus-summary-article-number)))
6213    (and gnus-auto-select-same
6214         (gnus-summary-article-subject))))
6215
6216 (defun gnus-summary-next-page (&optional lines circular)
6217   "Show next page of the selected article.
6218 If at the end of the current article, select the next article.
6219 LINES says how many lines should be scrolled up.
6220
6221 If CIRCULAR is non-nil, go to the start of the article instead of
6222 selecting the next article when reaching the end of the current
6223 article."
6224   (interactive "P")
6225   (setq gnus-summary-buffer (current-buffer))
6226   (gnus-set-global-variables)
6227   (let ((article (gnus-summary-article-number))
6228         (article-window (get-buffer-window gnus-article-buffer t))
6229         endp)
6230     ;; If the buffer is empty, we have no article.
6231     (unless article
6232       (error "No article to select"))
6233     (gnus-configure-windows 'article)
6234     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6235         (if (and (eq gnus-summary-goto-unread 'never)
6236                  (not (gnus-summary-last-article-p article)))
6237             (gnus-summary-next-article)
6238           (gnus-summary-next-unread-article))
6239       (if (or (null gnus-current-article)
6240               (null gnus-article-current)
6241               (/= article (cdr gnus-article-current))
6242               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6243           ;; Selected subject is different from current article's.
6244           (gnus-summary-display-article article)
6245         (when article-window
6246           (gnus-eval-in-buffer-window gnus-article-buffer
6247             (setq endp (gnus-article-next-page lines)))
6248           (when endp
6249             (cond (circular
6250                    (gnus-summary-beginning-of-article))
6251                   (lines
6252                    (gnus-message 3 "End of message"))
6253                   ((null lines)
6254                    (if (and (eq gnus-summary-goto-unread 'never)
6255                             (not (gnus-summary-last-article-p article)))
6256                        (gnus-summary-next-article)
6257                      (gnus-summary-next-unread-article))))))))
6258     (gnus-summary-recenter)
6259     (gnus-summary-position-point)))
6260
6261 (defun gnus-summary-prev-page (&optional lines move)
6262   "Show previous page of selected article.
6263 Argument LINES specifies lines to be scrolled down.
6264 If MOVE, move to the previous unread article if point is at
6265 the beginning of the buffer."
6266   (interactive "P")
6267   (let ((article (gnus-summary-article-number))
6268         (article-window (get-buffer-window gnus-article-buffer t))
6269         endp)
6270     (gnus-configure-windows 'article)
6271     (if (or (null gnus-current-article)
6272             (null gnus-article-current)
6273             (/= article (cdr gnus-article-current))
6274             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6275         ;; Selected subject is different from current article's.
6276         (gnus-summary-display-article article)
6277       (gnus-summary-recenter)
6278       (when article-window
6279         (gnus-eval-in-buffer-window gnus-article-buffer
6280           (setq endp (gnus-article-prev-page lines)))
6281         (when (and move endp)
6282           (cond (lines
6283                  (gnus-message 3 "Beginning of message"))
6284                 ((null lines)
6285                  (if (and (eq gnus-summary-goto-unread 'never)
6286                           (not (gnus-summary-first-article-p article)))
6287                      (gnus-summary-prev-article)
6288                    (gnus-summary-prev-unread-article))))))))
6289   (gnus-summary-position-point))
6290
6291 (defun gnus-summary-prev-page-or-article (&optional lines)
6292   "Show previous page of selected article.
6293 Argument LINES specifies lines to be scrolled down.
6294 If at the beginning of the article, go to the next article."
6295   (interactive "P")
6296   (gnus-summary-prev-page lines t))
6297
6298 (defun gnus-summary-scroll-up (lines)
6299   "Scroll up (or down) one line current article.
6300 Argument LINES specifies lines to be scrolled up (or down if negative)."
6301   (interactive "p")
6302   (gnus-configure-windows 'article)
6303   (gnus-summary-show-thread)
6304   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6305     (gnus-eval-in-buffer-window gnus-article-buffer
6306       (cond ((> lines 0)
6307              (when (gnus-article-next-page lines)
6308                (gnus-message 3 "End of message")))
6309             ((< lines 0)
6310              (gnus-article-prev-page (- lines))))))
6311   (gnus-summary-recenter)
6312   (gnus-summary-position-point))
6313
6314 (defun gnus-summary-scroll-down (lines)
6315   "Scroll down (or up) one line current article.
6316 Argument LINES specifies lines to be scrolled down (or up if negative)."
6317   (interactive "p")
6318   (gnus-summary-scroll-up (- lines)))
6319
6320 (defun gnus-summary-next-same-subject ()
6321   "Select next article which has the same subject as current one."
6322   (interactive)
6323   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6324
6325 (defun gnus-summary-prev-same-subject ()
6326   "Select previous article which has the same subject as current one."
6327   (interactive)
6328   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6329
6330 (defun gnus-summary-next-unread-same-subject ()
6331   "Select next unread article which has the same subject as current one."
6332   (interactive)
6333   (gnus-summary-next-article t (gnus-summary-article-subject)))
6334
6335 (defun gnus-summary-prev-unread-same-subject ()
6336   "Select previous unread article which has the same subject as current one."
6337   (interactive)
6338   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6339
6340 (defun gnus-summary-first-unread-article ()
6341   "Select the first unread article.
6342 Return nil if there are no unread articles."
6343   (interactive)
6344   (prog1
6345       (when (gnus-summary-first-subject t)
6346         (gnus-summary-show-thread)
6347         (gnus-summary-first-subject t)
6348         (gnus-summary-display-article (gnus-summary-article-number)))
6349     (gnus-summary-position-point)))
6350
6351 (defun gnus-summary-first-unread-subject ()
6352   "Place the point on the subject line of the first unread article.
6353 Return nil if there are no unread articles."
6354   (interactive)
6355   (prog1
6356       (when (gnus-summary-first-subject t)
6357         (gnus-summary-show-thread)
6358         (gnus-summary-first-subject t))
6359     (gnus-summary-position-point)))
6360
6361 (defun gnus-summary-first-article ()
6362   "Select the first article.
6363 Return nil if there are no articles."
6364   (interactive)
6365   (prog1
6366       (when (gnus-summary-first-subject)
6367         (gnus-summary-show-thread)
6368         (gnus-summary-first-subject)
6369         (gnus-summary-display-article (gnus-summary-article-number)))
6370     (gnus-summary-position-point)))
6371
6372 (defun gnus-summary-best-unread-article ()
6373   "Select the unread article with the highest score."
6374   (interactive)
6375   (let ((best -1000000)
6376         (data gnus-newsgroup-data)
6377         article score)
6378     (while data
6379       (and (gnus-data-unread-p (car data))
6380            (> (setq score
6381                     (gnus-summary-article-score (gnus-data-number (car data))))
6382               best)
6383            (setq best score
6384                  article (gnus-data-number (car data))))
6385       (setq data (cdr data)))
6386     (prog1
6387         (if article
6388             (gnus-summary-goto-article article)
6389           (error "No unread articles"))
6390       (gnus-summary-position-point))))
6391
6392 (defun gnus-summary-last-subject ()
6393   "Go to the last displayed subject line in the group."
6394   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6395     (when article
6396       (gnus-summary-goto-subject article))))
6397
6398 (defun gnus-summary-goto-article (article &optional all-headers force)
6399   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6400 If ALL-HEADERS is non-nil, no header lines are hidden.
6401 If FORCE, go to the article even if it isn't displayed.  If FORCE
6402 is a number, it is the line the article is to be displayed on."
6403   (interactive
6404    (list
6405     (completing-read
6406      "Article number or Message-ID: "
6407      (mapcar (lambda (number) (list (int-to-string number)))
6408              gnus-newsgroup-limit))
6409     current-prefix-arg
6410     t))
6411   (prog1
6412       (if (and (stringp article)
6413                (string-match "@" article))
6414           (gnus-summary-refer-article article)
6415         (when (stringp article)
6416           (setq article (string-to-number article)))
6417         (if (gnus-summary-goto-subject article force)
6418             (gnus-summary-display-article article all-headers)
6419           (gnus-message 4 "Couldn't go to article %s" article) nil))
6420     (gnus-summary-position-point)))
6421
6422 (defun gnus-summary-goto-last-article ()
6423   "Go to the previously read article."
6424   (interactive)
6425   (prog1
6426       (when gnus-last-article
6427         (gnus-summary-goto-article gnus-last-article nil t))
6428     (gnus-summary-position-point)))
6429
6430 (defun gnus-summary-pop-article (number)
6431   "Pop one article off the history and go to the previous.
6432 NUMBER articles will be popped off."
6433   (interactive "p")
6434   (let (to)
6435     (setq gnus-newsgroup-history
6436           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6437     (if to
6438         (gnus-summary-goto-article (car to) nil t)
6439       (error "Article history empty")))
6440   (gnus-summary-position-point))
6441
6442 ;; Summary commands and functions for limiting the summary buffer.
6443
6444 (defun gnus-summary-limit-to-articles (n)
6445   "Limit the summary buffer to the next N articles.
6446 If not given a prefix, use the process marked articles instead."
6447   (interactive "P")
6448   (prog1
6449       (let ((articles (gnus-summary-work-articles n)))
6450         (setq gnus-newsgroup-processable nil)
6451         (gnus-summary-limit articles))
6452     (gnus-summary-position-point)))
6453
6454 (defun gnus-summary-pop-limit (&optional total)
6455   "Restore the previous limit.
6456 If given a prefix, remove all limits."
6457   (interactive "P")
6458   (when total
6459     (setq gnus-newsgroup-limits
6460           (list (mapcar (lambda (h) (mail-header-number h))
6461                         gnus-newsgroup-headers))))
6462   (unless gnus-newsgroup-limits
6463     (error "No limit to pop"))
6464   (prog1
6465       (gnus-summary-limit nil 'pop)
6466     (gnus-summary-position-point)))
6467
6468 (defun gnus-summary-limit-to-subject (subject &optional header)
6469   "Limit the summary buffer to articles that have subjects that match a regexp."
6470   (interactive "sLimit to subject (regexp): ")
6471   (unless header
6472     (setq header "subject"))
6473   (when (not (equal "" subject))
6474     (prog1
6475         (let ((articles (gnus-summary-find-matching
6476                          (or header "subject") subject 'all)))
6477           (unless articles
6478             (error "Found no matches for \"%s\"" subject))
6479           (gnus-summary-limit articles))
6480       (gnus-summary-position-point))))
6481
6482 (defun gnus-summary-limit-to-author (from)
6483   "Limit the summary buffer to articles that have authors that match a regexp."
6484   (interactive "sLimit to author (regexp): ")
6485   (gnus-summary-limit-to-subject from "from"))
6486
6487 (defun gnus-summary-limit-to-age (age &optional younger-p)
6488   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6489 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6490 articles that are younger than AGE days."
6491   (interactive
6492    (let ((younger current-prefix-arg)
6493          (days-got nil)
6494          days)
6495      (while (not days-got)
6496        (setq days (if younger
6497                       (read-string "Limit to articles within (in days): ")
6498                     (read-string "Limit to articles older than (in days): ")))
6499        (when (> (length days) 0)
6500          (setq days (read days)))
6501        (if (numberp days)
6502            (setq days-got t)
6503          (message "Please enter a number.")
6504          (sleep-for 1)))
6505      (list days younger)))
6506   (prog1
6507       (let ((data gnus-newsgroup-data)
6508             (cutoff (days-to-time age))
6509             articles d date is-younger)
6510         (while (setq d (pop data))
6511           (when (and (vectorp (gnus-data-header d))
6512                      (setq date (mail-header-date (gnus-data-header d))))
6513             (setq is-younger (time-less-p
6514                               (time-since (condition-case ()
6515                                               (date-to-time date)
6516                                             (error '(0 0))))
6517                               cutoff))
6518             (when (if younger-p
6519                       is-younger
6520                     (not is-younger))
6521               (push (gnus-data-number d) articles))))
6522         (gnus-summary-limit (nreverse articles)))
6523     (gnus-summary-position-point)))
6524
6525 (defun gnus-summary-limit-to-extra (header regexp)
6526   "Limit the summary buffer to articles that match an 'extra' header."
6527   (interactive
6528    (let ((header
6529           (intern
6530            (gnus-completing-read
6531             (symbol-name (car gnus-extra-headers))
6532             "Limit extra header:"
6533             (mapcar (lambda (x)
6534                       (cons (symbol-name x) x))
6535                     gnus-extra-headers)
6536             nil
6537             t))))
6538      (list header
6539            (read-string (format "Limit to header %s (regexp): " header)))))
6540   (when (not (equal "" regexp))
6541     (prog1
6542         (let ((articles (gnus-summary-find-matching
6543                          (cons 'extra header) regexp 'all)))
6544           (unless articles
6545             (error "Found no matches for \"%s\"" regexp))
6546           (gnus-summary-limit articles))
6547       (gnus-summary-position-point))))
6548
6549 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6550 (make-obsolete
6551  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6552
6553 (defun gnus-summary-limit-to-unread (&optional all)
6554   "Limit the summary buffer to articles that are not marked as read.
6555 If ALL is non-nil, limit strictly to unread articles."
6556   (interactive "P")
6557   (if all
6558       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6559     (gnus-summary-limit-to-marks
6560      ;; Concat all the marks that say that an article is read and have
6561      ;; those removed.
6562      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6563            gnus-killed-mark gnus-kill-file-mark
6564            gnus-low-score-mark gnus-expirable-mark
6565            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6566            gnus-duplicate-mark gnus-souped-mark)
6567      'reverse)))
6568
6569 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6570 (make-obsolete 'gnus-summary-delete-marked-with
6571                'gnus-summary-limit-exlude-marks)
6572
6573 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6574   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6575 If REVERSE, limit the summary buffer to articles that are marked
6576 with MARKS.  MARKS can either be a string of marks or a list of marks.
6577 Returns how many articles were removed."
6578   (interactive "sMarks: ")
6579   (gnus-summary-limit-to-marks marks t))
6580
6581 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6582   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6583 If REVERSE (the prefix), limit the summary buffer to articles that are
6584 not marked with MARKS.  MARKS can either be a string of marks or a
6585 list of marks.
6586 Returns how many articles were removed."
6587   (interactive "sMarks: \nP")
6588   (prog1
6589       (let ((data gnus-newsgroup-data)
6590             (marks (if (listp marks) marks
6591                      (append marks nil))) ; Transform to list.
6592             articles)
6593         (while data
6594           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6595                   (memq (gnus-data-mark (car data)) marks))
6596             (push (gnus-data-number (car data)) articles))
6597           (setq data (cdr data)))
6598         (gnus-summary-limit articles))
6599     (gnus-summary-position-point)))
6600
6601 (defun gnus-summary-limit-to-score (&optional score)
6602   "Limit to articles with score at or above SCORE."
6603   (interactive "P")
6604   (setq score (if score
6605                   (prefix-numeric-value score)
6606                 (or gnus-summary-default-score 0)))
6607   (let ((data gnus-newsgroup-data)
6608         articles)
6609     (while data
6610       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6611                 score)
6612         (push (gnus-data-number (car data)) articles))
6613       (setq data (cdr data)))
6614     (prog1
6615         (gnus-summary-limit articles)
6616       (gnus-summary-position-point))))
6617
6618 (defun gnus-summary-limit-include-thread (id)
6619   "Display all the hidden articles that in the current thread."
6620   (interactive (list (mail-header-id (gnus-summary-article-header))))
6621   (let ((articles (gnus-articles-in-thread
6622                    (gnus-id-to-thread (gnus-root-id id)))))
6623     (prog1
6624         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6625       (gnus-summary-position-point))))
6626
6627 (defun gnus-summary-limit-include-dormant ()
6628   "Display all the hidden articles that are marked as dormant.
6629 Note that this command only works on a subset of the articles currently
6630 fetched for this group."
6631   (interactive)
6632   (unless gnus-newsgroup-dormant
6633     (error "There are no dormant articles in this group"))
6634   (prog1
6635       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6636     (gnus-summary-position-point)))
6637
6638 (defun gnus-summary-limit-exclude-dormant ()
6639   "Hide all dormant articles."
6640   (interactive)
6641   (prog1
6642       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6643     (gnus-summary-position-point)))
6644
6645 (defun gnus-summary-limit-exclude-childless-dormant ()
6646   "Hide all dormant articles that have no children."
6647   (interactive)
6648   (let ((data (gnus-data-list t))
6649         articles d children)
6650     ;; Find all articles that are either not dormant or have
6651     ;; children.
6652     (while (setq d (pop data))
6653       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6654                 (and (setq children
6655                            (gnus-article-children (gnus-data-number d)))
6656                      (let (found)
6657                        (while children
6658                          (when (memq (car children) articles)
6659                            (setq children nil
6660                                  found t))
6661                          (pop children))
6662                        found)))
6663         (push (gnus-data-number d) articles)))
6664     ;; Do the limiting.
6665     (prog1
6666         (gnus-summary-limit articles)
6667       (gnus-summary-position-point))))
6668
6669 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6670   "Mark all unread excluded articles as read.
6671 If ALL, mark even excluded ticked and dormants as read."
6672   (interactive "P")
6673   (let ((articles (gnus-sorted-complement
6674                    (sort
6675                     (mapcar (lambda (h) (mail-header-number h))
6676                             gnus-newsgroup-headers)
6677                     '<)
6678                    (sort gnus-newsgroup-limit '<)))
6679         article)
6680     (setq gnus-newsgroup-unreads
6681           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6682     (if all
6683         (setq gnus-newsgroup-dormant nil
6684               gnus-newsgroup-marked nil
6685               gnus-newsgroup-reads
6686               (nconc
6687                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6688                gnus-newsgroup-reads))
6689       (while (setq article (pop articles))
6690         (unless (or (memq article gnus-newsgroup-dormant)
6691                     (memq article gnus-newsgroup-marked))
6692           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6693
6694 (defun gnus-summary-limit (articles &optional pop)
6695   (if pop
6696       ;; We pop the previous limit off the stack and use that.
6697       (setq articles (car gnus-newsgroup-limits)
6698             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6699     ;; We use the new limit, so we push the old limit on the stack.
6700     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6701   ;; Set the limit.
6702   (setq gnus-newsgroup-limit articles)
6703   (let ((total (length gnus-newsgroup-data))
6704         (data (gnus-data-find-list (gnus-summary-article-number)))
6705         (gnus-summary-mark-below nil)   ; Inhibit this.
6706         found)
6707     ;; This will do all the work of generating the new summary buffer
6708     ;; according to the new limit.
6709     (gnus-summary-prepare)
6710     ;; Hide any threads, possibly.
6711     (and gnus-show-threads
6712          gnus-thread-hide-subtree
6713          (gnus-summary-hide-all-threads))
6714     ;; Try to return to the article you were at, or one in the
6715     ;; neighborhood.
6716     (when data
6717       ;; We try to find some article after the current one.
6718       (while data
6719         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6720           (setq data nil
6721                 found t))
6722         (setq data (cdr data))))
6723     (unless found
6724       ;; If there is no data, that means that we were after the last
6725       ;; article.  The same goes when we can't find any articles
6726       ;; after the current one.
6727       (goto-char (point-max))
6728       (gnus-summary-find-prev))
6729     (gnus-set-mode-line 'summary)
6730     ;; We return how many articles were removed from the summary
6731     ;; buffer as a result of the new limit.
6732     (- total (length gnus-newsgroup-data))))
6733
6734 (defsubst gnus-invisible-cut-children (threads)
6735   (let ((num 0))
6736     (while threads
6737       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6738         (incf num))
6739       (pop threads))
6740     (< num 2)))
6741
6742 (defsubst gnus-cut-thread (thread)
6743   "Go forwards in the thread until we find an article that we want to display."
6744   (when (or (eq gnus-fetch-old-headers 'some)
6745             (eq gnus-fetch-old-headers 'invisible)
6746             (numberp gnus-fetch-old-headers)
6747             (eq gnus-build-sparse-threads 'some)
6748             (eq gnus-build-sparse-threads 'more))
6749     ;; Deal with old-fetched headers and sparse threads.
6750     (while (and
6751             thread
6752             (or
6753              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6754              (gnus-summary-article-ancient-p
6755               (mail-header-number (car thread))))
6756             (if (or (<= (length (cdr thread)) 1)
6757                     (eq gnus-fetch-old-headers 'invisible))
6758                 (setq gnus-newsgroup-limit
6759                       (delq (mail-header-number (car thread))
6760                             gnus-newsgroup-limit)
6761                       thread (cadr thread))
6762               (when (gnus-invisible-cut-children (cdr thread))
6763                 (let ((th (cdr thread)))
6764                   (while th
6765                     (if (memq (mail-header-number (caar th))
6766                               gnus-newsgroup-limit)
6767                         (setq thread (car th)
6768                               th nil)
6769                       (setq th (cdr th))))))))))
6770   thread)
6771
6772 (defun gnus-cut-threads (threads)
6773   "Cut off all uninteresting articles from the beginning of threads."
6774   (when (or (eq gnus-fetch-old-headers 'some)
6775             (eq gnus-fetch-old-headers 'invisible)
6776             (numberp gnus-fetch-old-headers)
6777             (eq gnus-build-sparse-threads 'some)
6778             (eq gnus-build-sparse-threads 'more))
6779     (let ((th threads))
6780       (while th
6781         (setcar th (gnus-cut-thread (car th)))
6782         (setq th (cdr th)))))
6783   ;; Remove nixed out threads.
6784   (delq nil threads))
6785
6786 (defun gnus-summary-initial-limit (&optional show-if-empty)
6787   "Figure out what the initial limit is supposed to be on group entry.
6788 This entails weeding out unwanted dormants, low-scored articles,
6789 fetch-old-headers verbiage, and so on."
6790   ;; Most groups have nothing to remove.
6791   (if (or gnus-inhibit-limiting
6792           (and (null gnus-newsgroup-dormant)
6793                (not (eq gnus-fetch-old-headers 'some))
6794                (not (numberp gnus-fetch-old-headers))
6795                (not (eq gnus-fetch-old-headers 'invisible))
6796                (null gnus-summary-expunge-below)
6797                (not (eq gnus-build-sparse-threads 'some))
6798                (not (eq gnus-build-sparse-threads 'more))
6799                (null gnus-thread-expunge-below)
6800                (not gnus-use-nocem)))
6801       ()                                ; Do nothing.
6802     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6803     (setq gnus-newsgroup-limit nil)
6804     (mapatoms
6805      (lambda (node)
6806        (unless (car (symbol-value node))
6807          ;; These threads have no parents -- they are roots.
6808          (let ((nodes (cdr (symbol-value node)))
6809                thread)
6810            (while nodes
6811              (if (and gnus-thread-expunge-below
6812                       (< (gnus-thread-total-score (car nodes))
6813                          gnus-thread-expunge-below))
6814                  (gnus-expunge-thread (pop nodes))
6815                (setq thread (pop nodes))
6816                (gnus-summary-limit-children thread))))))
6817      gnus-newsgroup-dependencies)
6818     ;; If this limitation resulted in an empty group, we might
6819     ;; pop the previous limit and use it instead.
6820     (when (and (not gnus-newsgroup-limit)
6821                show-if-empty)
6822       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6823     gnus-newsgroup-limit))
6824
6825 (defun gnus-summary-limit-children (thread)
6826   "Return 1 if this subthread is visible and 0 if it is not."
6827   ;; First we get the number of visible children to this thread.  This
6828   ;; is done by recursing down the thread using this function, so this
6829   ;; will really go down to a leaf article first, before slowly
6830   ;; working its way up towards the root.
6831   (when thread
6832     (let ((children
6833            (if (cdr thread)
6834                (apply '+ (mapcar 'gnus-summary-limit-children
6835                                  (cdr thread)))
6836              0))
6837           (number (mail-header-number (car thread)))
6838           score)
6839       (if (and
6840            (not (memq number gnus-newsgroup-marked))
6841            (or
6842             ;; If this article is dormant and has absolutely no visible
6843             ;; children, then this article isn't visible.
6844             (and (memq number gnus-newsgroup-dormant)
6845                  (zerop children))
6846             ;; If this is "fetch-old-headered" and there is no
6847             ;; visible children, then we don't want this article.
6848             (and (or (eq gnus-fetch-old-headers 'some)
6849                      (numberp gnus-fetch-old-headers))
6850                  (gnus-summary-article-ancient-p number)
6851                  (zerop children))
6852             ;; If this is "fetch-old-headered" and `invisible', then
6853             ;; we don't want this article.
6854             (and (eq gnus-fetch-old-headers 'invisible)
6855                  (gnus-summary-article-ancient-p number))
6856             ;; If this is a sparsely inserted article with no children,
6857             ;; we don't want it.
6858             (and (eq gnus-build-sparse-threads 'some)
6859                  (gnus-summary-article-sparse-p number)
6860                  (zerop children))
6861             ;; If we use expunging, and this article is really
6862             ;; low-scored, then we don't want this article.
6863             (when (and gnus-summary-expunge-below
6864                        (< (setq score
6865                                 (or (cdr (assq number gnus-newsgroup-scored))
6866                                     gnus-summary-default-score))
6867                           gnus-summary-expunge-below))
6868               ;; We increase the expunge-tally here, but that has
6869               ;; nothing to do with the limits, really.
6870               (incf gnus-newsgroup-expunged-tally)
6871               ;; We also mark as read here, if that's wanted.
6872               (when (and gnus-summary-mark-below
6873                          (< score gnus-summary-mark-below))
6874                 (setq gnus-newsgroup-unreads
6875                       (delq number gnus-newsgroup-unreads))
6876                 (if gnus-newsgroup-auto-expire
6877                     (push number gnus-newsgroup-expirable)
6878                   (push (cons number gnus-low-score-mark)
6879                         gnus-newsgroup-reads)))
6880               t)
6881             ;; Check NoCeM things.
6882             (if (and gnus-use-nocem
6883                      (gnus-nocem-unwanted-article-p
6884                       (mail-header-id (car thread))))
6885                 (progn
6886                   (setq gnus-newsgroup-unreads
6887                         (delq number gnus-newsgroup-unreads))
6888                   t))))
6889           ;; Nope, invisible article.
6890           0
6891         ;; Ok, this article is to be visible, so we add it to the limit
6892         ;; and return 1.
6893         (push number gnus-newsgroup-limit)
6894         1))))
6895
6896 (defun gnus-expunge-thread (thread)
6897   "Mark all articles in THREAD as read."
6898   (let* ((number (mail-header-number (car thread))))
6899     (incf gnus-newsgroup-expunged-tally)
6900     ;; We also mark as read here, if that's wanted.
6901     (setq gnus-newsgroup-unreads
6902           (delq number gnus-newsgroup-unreads))
6903     (if gnus-newsgroup-auto-expire
6904         (push number gnus-newsgroup-expirable)
6905       (push (cons number gnus-low-score-mark)
6906             gnus-newsgroup-reads)))
6907   ;; Go recursively through all subthreads.
6908   (mapcar 'gnus-expunge-thread (cdr thread)))
6909
6910 ;; Summary article oriented commands
6911
6912 (defun gnus-summary-refer-parent-article (n)
6913   "Refer parent article N times.
6914 If N is negative, go to ancestor -N instead.
6915 The difference between N and the number of articles fetched is returned."
6916   (interactive "p")
6917   (let ((skip 1)
6918         error header ref)
6919     (when (not (natnump n))
6920       (setq skip (abs n)
6921             n 1))
6922     (while (and (> n 0)
6923                 (not error))
6924       (setq header (gnus-summary-article-header))
6925       (if (and (eq (mail-header-number header)
6926                    (cdr gnus-article-current))
6927                (equal gnus-newsgroup-name
6928                       (car gnus-article-current)))
6929           ;; If we try to find the parent of the currently
6930           ;; displayed article, then we take a look at the actual
6931           ;; References header, since this is slightly more
6932           ;; reliable than the References field we got from the
6933           ;; server.
6934           (save-excursion
6935             (set-buffer gnus-original-article-buffer)
6936             (nnheader-narrow-to-headers)
6937             (unless (setq ref (message-fetch-field "references"))
6938               (setq ref (message-fetch-field "in-reply-to")))
6939             (widen))
6940         (setq ref
6941               ;; It's not the current article, so we take a bet on
6942               ;; the value we got from the server.
6943               (mail-header-references header)))
6944       (if (and ref
6945                (not (equal ref "")))
6946           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6947             (gnus-message 1 "Couldn't find parent"))
6948         (gnus-message 1 "No references in article %d"
6949                       (gnus-summary-article-number))
6950         (setq error t))
6951       (decf n))
6952     (gnus-summary-position-point)
6953     n))
6954
6955 (defun gnus-summary-refer-references ()
6956   "Fetch all articles mentioned in the References header.
6957 Return the number of articles fetched."
6958   (interactive)
6959   (let ((ref (mail-header-references (gnus-summary-article-header)))
6960         (current (gnus-summary-article-number))
6961         (n 0))
6962     (if (or (not ref)
6963             (equal ref ""))
6964         (error "No References in the current article")
6965       ;; For each Message-ID in the References header...
6966       (while (string-match "<[^>]*>" ref)
6967         (incf n)
6968         ;; ... fetch that article.
6969         (gnus-summary-refer-article
6970          (prog1 (match-string 0 ref)
6971            (setq ref (substring ref (match-end 0))))))
6972       (gnus-summary-goto-subject current)
6973       (gnus-summary-position-point)
6974       n)))
6975
6976 (defun gnus-summary-refer-thread (&optional limit)
6977   "Fetch all articles in the current thread.
6978 If LIMIT (the numerical prefix), fetch that many old headers instead
6979 of what's specified by the `gnus-refer-thread-limit' variable."
6980   (interactive "P")
6981   (let ((id (mail-header-id (gnus-summary-article-header)))
6982         (limit (if limit (prefix-numeric-value limit)
6983                  gnus-refer-thread-limit)))
6984     ;; We want to fetch LIMIT *old* headers, but we also have to
6985     ;; re-fetch all the headers in the current buffer, because many of
6986     ;; them may be undisplayed.  So we adjust LIMIT.
6987     (when (numberp limit)
6988       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6989     (unless (eq gnus-fetch-old-headers 'invisible)
6990       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6991       ;; Retrieve the headers and read them in.
6992       (if (eq (gnus-retrieve-headers
6993                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6994               'nov)
6995           (gnus-build-all-threads)
6996         (error "Can't fetch thread from backends that don't support NOV"))
6997       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6998     (gnus-summary-limit-include-thread id)))
6999
7000 (defun gnus-summary-refer-article (message-id)
7001   "Fetch an article specified by MESSAGE-ID."
7002   (interactive "sMessage-ID: ")
7003   (when (and (stringp message-id)
7004              (not (zerop (length message-id))))
7005     ;; Construct the correct Message-ID if necessary.
7006     ;; Suggested by tale@pawl.rpi.edu.
7007     (unless (string-match "^<" message-id)
7008       (setq message-id (concat "<" message-id)))
7009     (unless (string-match ">$" message-id)
7010       (setq message-id (concat message-id ">")))
7011     (let* ((header (gnus-id-to-header message-id))
7012            (sparse (and header
7013                         (gnus-summary-article-sparse-p
7014                          (mail-header-number header))
7015                         (memq (mail-header-number header)
7016                               gnus-newsgroup-limit)))
7017            number)
7018       (cond
7019        ;; If the article is present in the buffer we just go to it.
7020        ((and header
7021              (or (not (gnus-summary-article-sparse-p
7022                        (mail-header-number header)))
7023                  sparse))
7024         (prog1
7025             (gnus-summary-goto-article
7026              (mail-header-number header) nil t)
7027           (when sparse
7028             (gnus-summary-update-article (mail-header-number header)))))
7029        (t
7030         ;; We fetch the article.
7031         (catch 'found
7032           (dolist (gnus-override-method (gnus-refer-article-methods))
7033             (gnus-check-server gnus-override-method)
7034             ;; Fetch the header, and display the article.
7035             (when (setq number (gnus-summary-insert-subject message-id))
7036               (gnus-summary-select-article nil nil nil number)
7037               (throw 'found t)))
7038           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7039
7040 (defun gnus-refer-article-methods ()
7041   "Return a list of referrable methods."
7042   (cond
7043    ;; No method, so we default to current and native.
7044    ((null gnus-refer-article-method)
7045     (list gnus-current-select-method gnus-select-method))
7046    ;; Current.
7047    ((eq 'current gnus-refer-article-method)
7048     (list gnus-current-select-method))
7049    ;; List of select methods.
7050    ((not (and (symbolp (car gnus-refer-article-method))
7051               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7052     (let (out)
7053       (dolist (method gnus-refer-article-method)
7054         (push (if (eq 'current method)
7055                   gnus-current-select-method
7056                 method)
7057               out))
7058       (nreverse out)))
7059    ;; One single select method.
7060    (t
7061     (list gnus-refer-article-method))))
7062
7063 (defun gnus-summary-edit-parameters ()
7064   "Edit the group parameters of the current group."
7065   (interactive)
7066   (gnus-group-edit-group gnus-newsgroup-name 'params))
7067
7068 (defun gnus-summary-customize-parameters ()
7069   "Customize the group parameters of the current group."
7070   (interactive)
7071   (gnus-group-customize gnus-newsgroup-name))
7072
7073 (defun gnus-summary-enter-digest-group (&optional force)
7074   "Enter an nndoc group based on the current article.
7075 If FORCE, force a digest interpretation.  If not, try
7076 to guess what the document format is."
7077   (interactive "P")
7078   (let ((conf gnus-current-window-configuration))
7079     (save-excursion
7080       (gnus-summary-select-article))
7081     (setq gnus-current-window-configuration conf)
7082     (let* ((name (format "%s-%d"
7083                          (gnus-group-prefixed-name
7084                           gnus-newsgroup-name (list 'nndoc ""))
7085                          (save-excursion
7086                            (set-buffer gnus-summary-buffer)
7087                            gnus-current-article)))
7088            (ogroup gnus-newsgroup-name)
7089            (params (append (gnus-info-params (gnus-get-info ogroup))
7090                            (list (cons 'to-group ogroup))
7091                            (list (cons 'save-article-group ogroup))))
7092            (case-fold-search t)
7093            (buf (current-buffer))
7094            dig to-address)
7095       (save-excursion
7096         (set-buffer gnus-original-article-buffer)
7097         ;; Have the digest group inherit the main mail address of
7098         ;; the parent article.
7099         (when (setq to-address (or (message-fetch-field "reply-to")
7100                                    (message-fetch-field "from")))
7101           (setq params (append 
7102                         (list (cons 'to-address 
7103                                     (funcall gnus-decode-encoded-word-function
7104                                              to-address))))))
7105         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7106         (insert-buffer-substring gnus-original-article-buffer)
7107         ;; Remove lines that may lead nndoc to misinterpret the
7108         ;; document type.
7109         (narrow-to-region
7110          (goto-char (point-min))
7111          (or (search-forward "\n\n" nil t) (point)))
7112         (goto-char (point-min))
7113         (delete-matching-lines "^Path:\\|^From ")
7114         (widen))
7115       (unwind-protect
7116           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7117                     (gnus-newsgroup-ephemeral-ignored-charsets
7118                      gnus-newsgroup-ignored-charsets))
7119                 (gnus-group-read-ephemeral-group
7120                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7121                               (nndoc-article-type
7122                                ,(if force 'mbox 'guess))) t))
7123               ;; Make all postings to this group go to the parent group.
7124               (nconc (gnus-info-params (gnus-get-info name))
7125                      params)
7126             ;; Couldn't select this doc group.
7127             (switch-to-buffer buf)
7128             (gnus-set-global-variables)
7129             (gnus-configure-windows 'summary)
7130             (gnus-message 3 "Article couldn't be entered?"))
7131         (kill-buffer dig)))))
7132
7133 (defun gnus-summary-read-document (n)
7134   "Open a new group based on the current article(s).
7135 This will allow you to read digests and other similar
7136 documents as newsgroups.
7137 Obeys the standard process/prefix convention."
7138   (interactive "P")
7139   (let* ((articles (gnus-summary-work-articles n))
7140          (ogroup gnus-newsgroup-name)
7141          (params (append (gnus-info-params (gnus-get-info ogroup))
7142                          (list (cons 'to-group ogroup))))
7143          article group egroup groups vgroup)
7144     (while (setq article (pop articles))
7145       (setq group (format "%s-%d" gnus-newsgroup-name article))
7146       (gnus-summary-remove-process-mark article)
7147       (when (gnus-summary-display-article article)
7148         (save-excursion
7149           (with-temp-buffer
7150             (insert-buffer-substring gnus-original-article-buffer)
7151             ;; Remove some headers that may lead nndoc to make
7152             ;; the wrong guess.
7153             (message-narrow-to-head)
7154             (goto-char (point-min))
7155             (delete-matching-lines "^\\(Path\\):\\|^From ")
7156             (widen)
7157             (if (setq egroup
7158                       (gnus-group-read-ephemeral-group
7159                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7160                                      (nndoc-article-type guess))
7161                        t nil t))
7162                 (progn
7163                   ;; Make all postings to this group go to the parent group.
7164                   (nconc (gnus-info-params (gnus-get-info egroup))
7165                          params)
7166                   (push egroup groups))
7167               ;; Couldn't select this doc group.
7168               (gnus-error 3 "Article couldn't be entered"))))))
7169     ;; Now we have selected all the documents.
7170     (cond
7171      ((not groups)
7172       (error "None of the articles could be interpreted as documents"))
7173      ((gnus-group-read-ephemeral-group
7174        (setq vgroup (format
7175                      "nnvirtual:%s-%s" gnus-newsgroup-name
7176                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7177        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7178        t
7179        (cons (current-buffer) 'summary)))
7180      (t
7181       (error "Couldn't select virtual nndoc group")))))
7182
7183 (defun gnus-summary-isearch-article (&optional regexp-p)
7184   "Do incremental search forward on the current article.
7185 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7186   (interactive "P")
7187   (gnus-summary-select-article)
7188   (gnus-configure-windows 'article)
7189   (gnus-eval-in-buffer-window gnus-article-buffer
7190     (save-restriction
7191       (widen)
7192       (isearch-forward regexp-p))))
7193
7194 (defun gnus-summary-search-article-forward (regexp &optional backward)
7195   "Search for an article containing REGEXP forward.
7196 If BACKWARD, search backward instead."
7197   (interactive
7198    (list (read-string
7199           (format "Search article %s (regexp%s): "
7200                   (if current-prefix-arg "backward" "forward")
7201                   (if gnus-last-search-regexp
7202                       (concat ", default " gnus-last-search-regexp)
7203                     "")))
7204          current-prefix-arg))
7205   (if (string-equal regexp "")
7206       (setq regexp (or gnus-last-search-regexp ""))
7207     (setq gnus-last-search-regexp regexp)
7208     (setq gnus-article-before-search gnus-current-article))
7209   ;; Intentionally set gnus-last-article.
7210   (setq gnus-last-article gnus-article-before-search)
7211   (let ((gnus-last-article gnus-last-article))
7212     (if (gnus-summary-search-article regexp backward)
7213         (gnus-summary-show-thread)
7214       (error "Search failed: \"%s\"" regexp))))
7215
7216 (defun gnus-summary-search-article-backward (regexp)
7217   "Search for an article containing REGEXP backward."
7218   (interactive
7219    (list (read-string
7220           (format "Search article backward (regexp%s): "
7221                   (if gnus-last-search-regexp
7222                       (concat ", default " gnus-last-search-regexp)
7223                     "")))))
7224   (gnus-summary-search-article-forward regexp 'backward))
7225
7226 (defun gnus-summary-search-article (regexp &optional backward)
7227   "Search for an article containing REGEXP.
7228 Optional argument BACKWARD means do search for backward.
7229 `gnus-select-article-hook' is not called during the search."
7230   ;; We have to require this here to make sure that the following
7231   ;; dynamic binding isn't shadowed by autoloading.
7232   (require 'gnus-async)
7233   (require 'gnus-art)
7234   (let ((gnus-select-article-hook nil)  ;Disable hook.
7235         (gnus-article-prepare-hook nil)
7236         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7237         (gnus-use-article-prefetch nil)
7238         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7239         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7240         (sum (current-buffer))
7241         (gnus-display-mime-function nil)
7242         (found nil)
7243         point)
7244     (gnus-save-hidden-threads
7245       (gnus-summary-select-article)
7246       (set-buffer gnus-article-buffer)
7247       (goto-char (window-point (get-buffer-window (current-buffer))))
7248       (when backward
7249         (forward-line -1))
7250       (while (not found)
7251         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7252         (if (if backward
7253                 (re-search-backward regexp nil t)
7254               (re-search-forward regexp nil t))
7255             ;; We found the regexp.
7256             (progn
7257               (setq found 'found)
7258               (beginning-of-line)
7259               (set-window-start
7260                (get-buffer-window (current-buffer))
7261                (point))
7262               (forward-line 1)
7263               (set-window-point
7264                (get-buffer-window (current-buffer))
7265                (point))
7266               (set-buffer sum)
7267               (setq point (point)))
7268           ;; We didn't find it, so we go to the next article.
7269           (set-buffer sum)
7270           (setq found 'not)
7271           (while (eq found 'not)
7272             (if (not (if backward (gnus-summary-find-prev)
7273                        (gnus-summary-find-next)))
7274                 ;; No more articles.
7275                 (setq found t)
7276               ;; Select the next article and adjust point.
7277               (unless (gnus-summary-article-sparse-p
7278                        (gnus-summary-article-number))
7279                 (setq found nil)
7280                 (gnus-summary-select-article)
7281                 (set-buffer gnus-article-buffer)
7282                 (widen)
7283                 (goto-char (if backward (point-max) (point-min))))))))
7284       (gnus-message 7 ""))
7285     ;; Return whether we found the regexp.
7286     (when (eq found 'found)
7287       (goto-char point)
7288       (gnus-summary-show-thread)
7289       (gnus-summary-goto-subject gnus-current-article)
7290       (gnus-summary-position-point)
7291       t)))
7292
7293 (defun gnus-summary-find-matching (header regexp &optional backward unread
7294                                           not-case-fold)
7295   "Return a list of all articles that match REGEXP on HEADER.
7296 The search stars on the current article and goes forwards unless
7297 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7298 If UNREAD is non-nil, only unread articles will
7299 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7300 in the comparisons."
7301   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7302                 (gnus-data-find-list
7303                  (gnus-summary-article-number) (gnus-data-list backward))))
7304         (case-fold-search (not not-case-fold))
7305         articles d func)
7306     (if (consp header)
7307         (if (eq (car header) 'extra)
7308             (setq func
7309                   `(lambda (h)
7310                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7311                          "")))
7312           (error "%s is an invalid header" header))
7313       (unless (fboundp (intern (concat "mail-header-" header)))
7314         (error "%s is not a valid header" header))
7315       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7316     (while data
7317       (setq d (car data))
7318       (and (or (not unread)             ; We want all articles...
7319                (gnus-data-unread-p d))  ; Or just unreads.
7320            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7321            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7322            (push (gnus-data-number d) articles)) ; Success!
7323       (setq data (cdr data)))
7324     (nreverse articles)))
7325
7326 (defun gnus-summary-execute-command (header regexp command &optional backward)
7327   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7328 If HEADER is an empty string (or nil), the match is done on the entire
7329 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7330   (interactive
7331    (list (let ((completion-ignore-case t))
7332            (completing-read
7333             "Header name: "
7334             (mapcar (lambda (string) (list string))
7335                     '("Number" "Subject" "From" "Lines" "Date"
7336                       "Message-ID" "Xref" "References" "Body"))
7337             nil 'require-match))
7338          (read-string "Regexp: ")
7339          (read-key-sequence "Command: ")
7340          current-prefix-arg))
7341   (when (equal header "Body")
7342     (setq header ""))
7343   ;; Hidden thread subtrees must be searched as well.
7344   (gnus-summary-show-all-threads)
7345   ;; We don't want to change current point nor window configuration.
7346   (save-excursion
7347     (save-window-excursion
7348       (gnus-message 6 "Executing %s..." (key-description command))
7349       ;; We'd like to execute COMMAND interactively so as to give arguments.
7350       (gnus-execute header regexp
7351                     `(call-interactively ',(key-binding command))
7352                     backward)
7353       (gnus-message 6 "Executing %s...done" (key-description command)))))
7354
7355 (defun gnus-summary-beginning-of-article ()
7356   "Scroll the article back to the beginning."
7357   (interactive)
7358   (gnus-summary-select-article)
7359   (gnus-configure-windows 'article)
7360   (gnus-eval-in-buffer-window gnus-article-buffer
7361     (widen)
7362     (goto-char (point-min))
7363     (when gnus-page-broken
7364       (gnus-narrow-to-page))))
7365
7366 (defun gnus-summary-end-of-article ()
7367   "Scroll to the end of the article."
7368   (interactive)
7369   (gnus-summary-select-article)
7370   (gnus-configure-windows 'article)
7371   (gnus-eval-in-buffer-window gnus-article-buffer
7372     (widen)
7373     (goto-char (point-max))
7374     (recenter -3)
7375     (when gnus-page-broken
7376       (gnus-narrow-to-page))))
7377
7378 (defun gnus-summary-print-article (&optional filename n)
7379   "Generate and print a PostScript image of the N next (mail) articles.
7380
7381 If N is negative, print the N previous articles.  If N is nil and articles
7382 have been marked with the process mark, print these instead.
7383
7384 If the optional first argument FILENAME is nil, send the image to the
7385 printer.  If FILENAME is a string, save the PostScript image in a file with
7386 that name.  If FILENAME is a number, prompt the user for the name of the file
7387 to save in."
7388   (interactive (list (ps-print-preprint current-prefix-arg)
7389                      current-prefix-arg))
7390   (dolist (article (gnus-summary-work-articles n))
7391     (gnus-summary-select-article nil nil 'pseudo article)
7392     (gnus-eval-in-buffer-window gnus-article-buffer
7393       (let ((buffer (generate-new-buffer " *print*")))
7394         (unwind-protect
7395             (progn
7396               (copy-to-buffer buffer (point-min) (point-max))
7397               (set-buffer buffer)
7398               (gnus-article-delete-invisible-text)
7399               (let ((ps-left-header
7400                      (list
7401                       (concat "("
7402                               (mail-header-subject gnus-current-headers) ")")
7403                       (concat "("
7404                               (mail-header-from gnus-current-headers) ")")))
7405                     (ps-right-header
7406                      (list
7407                       "/pagenumberstring load"
7408                       (concat "("
7409                               (mail-header-date gnus-current-headers) ")"))))
7410                 (gnus-run-hooks 'gnus-ps-print-hook)
7411                 (save-excursion
7412                   (ps-print-buffer-with-faces filename))))
7413           (kill-buffer buffer))))))
7414
7415 (defun gnus-summary-show-article (&optional arg)
7416   "Force re-fetching of the current article.
7417 If ARG (the prefix) is a number, show the article with the charset
7418 defined in `gnus-summary-show-article-charset-alist', or the charset
7419 inputed.
7420 If ARG (the prefix) is non-nil and not a number, show the raw article
7421 without any article massaging functions being run."
7422   (interactive "P")
7423   (cond
7424    ((numberp arg)
7425     (let ((gnus-newsgroup-charset
7426            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7427                (read-coding-system "Charset: ")))
7428           (gnus-newsgroup-ignored-charsets 'gnus-all))
7429       (gnus-summary-select-article nil 'force)
7430       (let ((deps gnus-newsgroup-dependencies)
7431             head header)
7432         (save-excursion
7433           (set-buffer gnus-original-article-buffer)
7434           (save-restriction
7435             (message-narrow-to-head)
7436             (setq head (buffer-string)))
7437           (with-temp-buffer
7438             (insert (format "211 %d Article retrieved.\n"
7439                             (cdr gnus-article-current)))
7440             (insert head)
7441             (insert ".\n")
7442             (let ((nntp-server-buffer (current-buffer)))
7443               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7444         (gnus-data-set-header
7445          (gnus-data-find (cdr gnus-article-current))
7446          header)
7447         (gnus-summary-update-article-line
7448          (cdr gnus-article-current) header))))
7449    ((not arg)
7450     ;; Select the article the normal way.
7451     (gnus-summary-select-article nil 'force))
7452    (t
7453     ;; We have to require this here to make sure that the following
7454     ;; dynamic binding isn't shadowed by autoloading.
7455     (require 'gnus-async)
7456     (require 'gnus-art)
7457     ;; Bind the article treatment functions to nil.
7458     (let ((gnus-have-all-headers t)
7459           gnus-article-prepare-hook
7460           gnus-article-decode-hook
7461           gnus-display-mime-function
7462           gnus-break-pages)
7463       ;; Destroy any MIME parts.
7464       (when (gnus-buffer-live-p gnus-article-buffer)
7465         (save-excursion
7466           (set-buffer gnus-article-buffer)
7467           (mm-destroy-parts gnus-article-mime-handles)
7468           ;; Set it to nil for safety reason.
7469           (setq gnus-article-mime-handle-alist nil)
7470           (setq gnus-article-mime-handles nil)))
7471       (gnus-summary-select-article nil 'force))))
7472   (gnus-summary-goto-subject gnus-current-article)
7473   (gnus-summary-position-point))
7474
7475 (defun gnus-summary-verbose-headers (&optional arg)
7476   "Toggle permanent full header display.
7477 If ARG is a positive number, turn header display on.
7478 If ARG is a negative number, turn header display off."
7479   (interactive "P")
7480   (setq gnus-show-all-headers
7481         (cond ((or (not (numberp arg))
7482                    (zerop arg))
7483                (not gnus-show-all-headers))
7484               ((natnump arg)
7485                t)))
7486   (gnus-summary-show-article))
7487
7488 (defun gnus-summary-toggle-header (&optional arg)
7489   "Show the headers if they are hidden, or hide them if they are shown.
7490 If ARG is a positive number, show the entire header.
7491 If ARG is a negative number, hide the unwanted header lines."
7492   (interactive "P")
7493   (save-excursion
7494     (set-buffer gnus-article-buffer)
7495     (save-restriction
7496       (let* ((buffer-read-only nil)
7497              (inhibit-point-motion-hooks t)
7498              hidden e)
7499         (setq hidden
7500               (if (numberp arg)
7501                   (>= arg 0)
7502                 (save-restriction
7503                   (article-narrow-to-head)
7504                   (gnus-article-hidden-text-p 'headers))))
7505         (goto-char (point-min))
7506         (when (search-forward "\n\n" nil t)
7507           (delete-region (point-min) (1- (point))))
7508         (goto-char (point-min))
7509         (save-excursion
7510           (set-buffer gnus-original-article-buffer)
7511           (goto-char (point-min))
7512           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7513         (insert-buffer-substring gnus-original-article-buffer 1 e)
7514         (save-restriction
7515           (narrow-to-region (point-min) (point))
7516           (article-decode-encoded-words)
7517           (if  hidden
7518               (let ((gnus-treat-hide-headers nil)
7519                     (gnus-treat-hide-boring-headers nil))
7520                 (setq gnus-article-wash-types
7521                       (delq 'headers gnus-article-wash-types))
7522                 (gnus-treat-article 'head))
7523             (gnus-treat-article 'head)))
7524         (gnus-set-mode-line 'article)))))
7525
7526 (defun gnus-summary-show-all-headers ()
7527   "Make all header lines visible."
7528   (interactive)
7529   (gnus-article-show-all-headers))
7530
7531 (defun gnus-summary-caesar-message (&optional arg)
7532   "Caesar rotate the current article by 13.
7533 The numerical prefix specifies how many places to rotate each letter
7534 forward."
7535   (interactive "P")
7536   (gnus-summary-select-article)
7537   (let ((mail-header-separator ""))
7538     (gnus-eval-in-buffer-window gnus-article-buffer
7539       (save-restriction
7540         (widen)
7541         (let ((start (window-start))
7542               buffer-read-only)
7543           (message-caesar-buffer-body arg)
7544           (set-window-start (get-buffer-window (current-buffer)) start))))))
7545
7546 (defun gnus-summary-stop-page-breaking ()
7547   "Stop page breaking in the current article."
7548   (interactive)
7549   (gnus-summary-select-article)
7550   (gnus-eval-in-buffer-window gnus-article-buffer
7551     (widen)
7552     (when (gnus-visual-p 'page-marker)
7553       (let ((buffer-read-only nil))
7554         (gnus-remove-text-with-property 'gnus-prev)
7555         (gnus-remove-text-with-property 'gnus-next))
7556       (setq gnus-page-broken nil))))
7557
7558 (defun gnus-summary-move-article (&optional n to-newsgroup
7559                                             select-method action)
7560   "Move the current article to a different newsgroup.
7561 If N is a positive number, move the N next articles.
7562 If N is a negative number, move the N previous articles.
7563 If N is nil and any articles have been marked with the process mark,
7564 move those articles instead.
7565 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7566 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7567 re-spool using this method.
7568
7569 For this function to work, both the current newsgroup and the
7570 newsgroup that you want to move to have to support the `request-move'
7571 and `request-accept' functions.
7572
7573 ACTION can be either `move' (the default), `crosspost' or `copy'."
7574   (interactive "P")
7575   (unless action
7576     (setq action 'move))
7577   ;; Disable marking as read.
7578   (let (gnus-mark-article-hook)
7579     (save-window-excursion
7580       (gnus-summary-select-article)))
7581   ;; Check whether the source group supports the required functions.
7582   (cond ((and (eq action 'move)
7583               (not (gnus-check-backend-function
7584                     'request-move-article gnus-newsgroup-name)))
7585          (error "The current group does not support article moving"))
7586         ((and (eq action 'crosspost)
7587               (not (gnus-check-backend-function
7588                     'request-replace-article gnus-newsgroup-name)))
7589          (error "The current group does not support article editing")))
7590   (let ((articles (gnus-summary-work-articles n))
7591         (prefix (if (gnus-check-backend-function
7592                     'request-move-article gnus-newsgroup-name)
7593                     (gnus-group-real-prefix gnus-newsgroup-name)
7594                   ""))
7595         (names '((move "Move" "Moving")
7596                  (copy "Copy" "Copying")
7597                  (crosspost "Crosspost" "Crossposting")))
7598         (copy-buf (save-excursion
7599                     (nnheader-set-temp-buffer " *copy article*")))
7600         art-group to-method new-xref article to-groups)
7601     (unless (assq action names)
7602       (error "Unknown action %s" action))
7603     ;; Read the newsgroup name.
7604     (when (and (not to-newsgroup)
7605                (not select-method))
7606       (setq to-newsgroup
7607             (gnus-read-move-group-name
7608              (cadr (assq action names))
7609              (symbol-value (intern (format "gnus-current-%s-group" action)))
7610              articles prefix))
7611       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7612     (setq to-method (or select-method
7613                         (gnus-server-to-method
7614                          (gnus-group-method to-newsgroup))))
7615     ;; Check the method we are to move this article to...
7616     (unless (gnus-check-backend-function
7617              'request-accept-article (car to-method))
7618       (error "%s does not support article copying" (car to-method)))
7619     (unless (gnus-check-server to-method)
7620       (error "Can't open server %s" (car to-method)))
7621     (gnus-message 6 "%s to %s: %s..."
7622                   (caddr (assq action names))
7623                   (or (car select-method) to-newsgroup) articles)
7624     (while articles
7625       (setq article (pop articles))
7626       (setq
7627        art-group
7628        (cond
7629         ;; Move the article.
7630         ((eq action 'move)
7631          ;; Remove this article from future suppression.
7632          (gnus-dup-unsuppress-article article)
7633          (gnus-request-move-article
7634           article                       ; Article to move
7635           gnus-newsgroup-name           ; From newsgroup
7636           (nth 1 (gnus-find-method-for-group
7637                   gnus-newsgroup-name)) ; Server
7638           (list 'gnus-request-accept-article
7639                 to-newsgroup (list 'quote select-method)
7640                 (not articles) t)       ; Accept form
7641           (not articles)))              ; Only save nov last time
7642         ;; Copy the article.
7643         ((eq action 'copy)
7644          (save-excursion
7645            (set-buffer copy-buf)
7646            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7647              (gnus-request-accept-article
7648               to-newsgroup select-method (not articles) t))))
7649         ;; Crosspost the article.
7650         ((eq action 'crosspost)
7651          (let ((xref (message-tokenize-header
7652                       (mail-header-xref (gnus-summary-article-header article))
7653                       " ")))
7654            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7655                                   ":" article))
7656            (unless xref
7657              (setq xref (list (system-name))))
7658            (setq new-xref
7659                  (concat
7660                   (mapconcat 'identity
7661                              (delete "Xref:" (delete new-xref xref))
7662                              " ")
7663                   " " new-xref))
7664            (save-excursion
7665              (set-buffer copy-buf)
7666              ;; First put the article in the destination group.
7667              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7668              (when (consp (setq art-group
7669                                 (gnus-request-accept-article
7670                                  to-newsgroup select-method (not articles))))
7671                (setq new-xref (concat new-xref " " (car art-group)
7672                                       ":" (cdr art-group)))
7673                ;; Now we have the new Xrefs header, so we insert
7674                ;; it and replace the new article.
7675                (nnheader-replace-header "Xref" new-xref)
7676                (gnus-request-replace-article
7677                 (cdr art-group) to-newsgroup (current-buffer))
7678                art-group))))))
7679       (cond
7680        ((not art-group)
7681         (gnus-message 1 "Couldn't %s article %s: %s"
7682                       (cadr (assq action names)) article
7683                       (nnheader-get-report (car to-method))))
7684        ((eq art-group 'junk)
7685         (when (eq action 'move)
7686           (gnus-summary-mark-article article gnus-canceled-mark)
7687           (gnus-message 4 "Deleted article %s" article)))
7688        (t
7689         (let* ((pto-group (gnus-group-prefixed-name
7690                            (car art-group) to-method))
7691                (entry
7692                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7693                (info (nth 2 entry))
7694                (to-group (gnus-info-group info))
7695                to-marks)
7696           ;; Update the group that has been moved to.
7697           (when (and info
7698                      (memq action '(move copy)))
7699             (unless (member to-group to-groups)
7700               (push to-group to-groups))
7701
7702             (unless (memq article gnus-newsgroup-unreads)
7703               (push 'read to-marks)
7704               (gnus-info-set-read
7705                info (gnus-add-to-range (gnus-info-read info)
7706                                        (list (cdr art-group)))))
7707
7708             ;; See whether the article is to be put in the cache.
7709             (let ((marks gnus-article-mark-lists)
7710                   (to-article (cdr art-group)))
7711
7712               ;; Enter the article into the cache in the new group,
7713               ;; if that is required.
7714               (when gnus-use-cache
7715                 (gnus-cache-possibly-enter-article
7716                  to-group to-article
7717                  (memq article gnus-newsgroup-marked)
7718                  (memq article gnus-newsgroup-dormant)
7719                  (memq article gnus-newsgroup-unreads)))
7720
7721               (when gnus-preserve-marks
7722                 ;; Copy any marks over to the new group.
7723                 (when (and (equal to-group gnus-newsgroup-name)
7724                            (not (memq article gnus-newsgroup-unreads)))
7725                   ;; Mark this article as read in this group.
7726                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7727                   (setcdr (gnus-active to-group) to-article)
7728                   (setcdr gnus-newsgroup-active to-article))
7729
7730                 (while marks
7731                   (when (memq article (symbol-value
7732                                        (intern (format "gnus-newsgroup-%s"
7733                                                        (caar marks)))))
7734                     (push (cdar marks) to-marks)
7735                     ;; If the other group is the same as this group,
7736                     ;; then we have to add the mark to the list.
7737                     (when (equal to-group gnus-newsgroup-name)
7738                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7739                            (cons to-article
7740                                  (symbol-value
7741                                   (intern (format "gnus-newsgroup-%s"
7742                                                   (caar marks)))))))
7743                     ;; Copy the marks to other group.
7744                     (gnus-add-marked-articles
7745                      to-group (cdar marks) (list to-article) info))
7746                   (setq marks (cdr marks)))
7747
7748                 (gnus-request-set-mark to-group (list (list (list to-article)
7749                                                             'set
7750                                                             to-marks))))
7751
7752               (gnus-dribble-enter
7753                (concat "(gnus-group-set-info '"
7754                        (gnus-prin1-to-string (gnus-get-info to-group))
7755                        ")"))))
7756
7757           ;; Update the Xref header in this article to point to
7758           ;; the new crossposted article we have just created.
7759           (when (eq action 'crosspost)
7760             (save-excursion
7761               (set-buffer copy-buf)
7762               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7763               (nnheader-replace-header "Xref" new-xref)
7764               (gnus-request-replace-article
7765                article gnus-newsgroup-name (current-buffer)))))
7766
7767         ;;;!!!Why is this necessary?
7768         (set-buffer gnus-summary-buffer)
7769
7770         (gnus-summary-goto-subject article)
7771         (when (eq action 'move)
7772           (gnus-summary-mark-article article gnus-canceled-mark))))
7773       (gnus-summary-remove-process-mark article))
7774     ;; Re-activate all groups that have been moved to.
7775     (while to-groups
7776       (save-excursion
7777         (set-buffer gnus-group-buffer)
7778         (when (gnus-group-goto-group (car to-groups) t)
7779           (gnus-group-get-new-news-this-group 1 t))
7780         (pop to-groups)))
7781
7782     (gnus-kill-buffer copy-buf)
7783     (gnus-summary-position-point)
7784     (gnus-set-mode-line 'summary)))
7785
7786 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7787   "Move the current article to a different newsgroup.
7788 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7789 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7790 re-spool using this method."
7791   (interactive "P")
7792   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7793
7794 (defun gnus-summary-crosspost-article (&optional n)
7795   "Crosspost the current article to some other group."
7796   (interactive "P")
7797   (gnus-summary-move-article n nil nil 'crosspost))
7798
7799 (defcustom gnus-summary-respool-default-method nil
7800   "Default method for respooling an article.
7801 If nil, use to the current newsgroup method."
7802   :type '(choice (gnus-select-method :value (nnml ""))
7803                  (const nil))
7804   :group 'gnus-summary-mail)
7805
7806 (defun gnus-summary-respool-article (&optional n method)
7807   "Respool the current article.
7808 The article will be squeezed through the mail spooling process again,
7809 which means that it will be put in some mail newsgroup or other
7810 depending on `nnmail-split-methods'.
7811 If N is a positive number, respool the N next articles.
7812 If N is a negative number, respool the N previous articles.
7813 If N is nil and any articles have been marked with the process mark,
7814 respool those articles instead.
7815
7816 Respooling can be done both from mail groups and \"real\" newsgroups.
7817 In the former case, the articles in question will be moved from the
7818 current group into whatever groups they are destined to.  In the
7819 latter case, they will be copied into the relevant groups."
7820   (interactive
7821    (list current-prefix-arg
7822          (let* ((methods (gnus-methods-using 'respool))
7823                 (methname
7824                  (symbol-name (or gnus-summary-respool-default-method
7825                                   (car (gnus-find-method-for-group
7826                                         gnus-newsgroup-name)))))
7827                 (method
7828                  (gnus-completing-read
7829                   methname "What backend do you want to use when respooling?"
7830                   methods nil t nil 'gnus-mail-method-history))
7831                 ms)
7832            (cond
7833             ((zerop (length (setq ms (gnus-servers-using-backend
7834                                       (intern method)))))
7835              (list (intern method) ""))
7836             ((= 1 (length ms))
7837              (car ms))
7838             (t
7839              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7840                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7841                            ms-alist))))))))
7842   (unless method
7843     (error "No method given for respooling"))
7844   (if (assoc (symbol-name
7845               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7846              (gnus-methods-using 'respool))
7847       (gnus-summary-move-article n nil method)
7848     (gnus-summary-copy-article n nil method)))
7849
7850 (defun gnus-summary-import-article (file)
7851   "Import an arbitrary file into a mail newsgroup."
7852   (interactive "fImport file: ")
7853   (let ((group gnus-newsgroup-name)
7854         (now (current-time))
7855         atts lines)
7856     (unless (gnus-check-backend-function 'request-accept-article group)
7857       (error "%s does not support article importing" group))
7858     (or (file-readable-p file)
7859         (not (file-regular-p file))
7860         (error "Can't read %s" file))
7861     (save-excursion
7862       (set-buffer (gnus-get-buffer-create " *import file*"))
7863       (erase-buffer)
7864       (nnheader-insert-file-contents file)
7865       (goto-char (point-min))
7866       (unless (nnheader-article-p)
7867         ;; This doesn't look like an article, so we fudge some headers.
7868         (setq atts (file-attributes file)
7869               lines (count-lines (point-min) (point-max)))
7870         (insert "From: " (read-string "From: ") "\n"
7871                 "Subject: " (read-string "Subject: ") "\n"
7872                 "Date: " (message-make-date (nth 5 atts))
7873                 "\n"
7874                 "Message-ID: " (message-make-message-id) "\n"
7875                 "Lines: " (int-to-string lines) "\n"
7876                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7877       (gnus-request-accept-article group nil t)
7878       (kill-buffer (current-buffer)))))
7879
7880 (defun gnus-summary-article-posted-p ()
7881   "Say whether the current (mail) article is available from news as well.
7882 This will be the case if the article has both been mailed and posted."
7883   (interactive)
7884   (let ((id (mail-header-references (gnus-summary-article-header)))
7885         (gnus-override-method (car (gnus-refer-article-methods))))
7886     (if (gnus-request-head id "")
7887         (gnus-message 2 "The current message was found on %s"
7888                       gnus-override-method)
7889       (gnus-message 2 "The current message couldn't be found on %s"
7890                     gnus-override-method)
7891       nil)))
7892
7893 (defun gnus-summary-expire-articles (&optional now)
7894   "Expire all articles that are marked as expirable in the current group."
7895   (interactive)
7896   (when (gnus-check-backend-function
7897          'request-expire-articles gnus-newsgroup-name)
7898     ;; This backend supports expiry.
7899     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7900            (expirable (if total
7901                           (progn
7902                             ;; We need to update the info for
7903                             ;; this group for `gnus-list-of-read-articles'
7904                             ;; to give us the right answer.
7905                             (gnus-run-hooks 'gnus-exit-group-hook)
7906                             (gnus-summary-update-info)
7907                             (gnus-list-of-read-articles gnus-newsgroup-name))
7908                         (setq gnus-newsgroup-expirable
7909                               (sort gnus-newsgroup-expirable '<))))
7910            (expiry-wait (if now 'immediate
7911                           (gnus-group-find-parameter
7912                            gnus-newsgroup-name 'expiry-wait)))
7913            (nnmail-expiry-target
7914             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7915                 nnmail-expiry-target))
7916            es)
7917       (when expirable
7918         ;; There are expirable articles in this group, so we run them
7919         ;; through the expiry process.
7920         (gnus-message 6 "Expiring articles...")
7921         (unless (gnus-check-group gnus-newsgroup-name)
7922           (error "Can't open server for %s" gnus-newsgroup-name))
7923         ;; The list of articles that weren't expired is returned.
7924         (save-excursion
7925           (if expiry-wait
7926               (let ((nnmail-expiry-wait-function nil)
7927                     (nnmail-expiry-wait expiry-wait))
7928                 (setq es (gnus-request-expire-articles
7929                           expirable gnus-newsgroup-name)))
7930             (setq es (gnus-request-expire-articles
7931                       expirable gnus-newsgroup-name)))
7932           (unless total
7933             (setq gnus-newsgroup-expirable es))
7934           ;; We go through the old list of expirable, and mark all
7935           ;; really expired articles as nonexistent.
7936           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
7937             (let ((gnus-use-cache nil))
7938               (while expirable
7939                 (unless (memq (car expirable) es)
7940                   (when (gnus-data-find (car expirable))
7941                     (gnus-summary-mark-article
7942                      (car expirable) gnus-canceled-mark)))
7943                 (setq expirable (cdr expirable))))))
7944         (gnus-message 6 "Expiring articles...done")))))
7945
7946 (defun gnus-summary-expire-articles-now ()
7947   "Expunge all expirable articles in the current group.
7948 This means that *all* articles that are marked as expirable will be
7949 deleted forever, right now."
7950   (interactive)
7951   (or gnus-expert-user
7952       (gnus-yes-or-no-p
7953        "Are you really, really, really sure you want to delete all these messages? ")
7954       (error "Phew!"))
7955   (gnus-summary-expire-articles t))
7956
7957 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7958 (defun gnus-summary-delete-article (&optional n)
7959   "Delete the N next (mail) articles.
7960 This command actually deletes articles.  This is not a marking
7961 command.  The article will disappear forever from your life, never to
7962 return.
7963 If N is negative, delete backwards.
7964 If N is nil and articles have been marked with the process mark,
7965 delete these instead."
7966   (interactive "P")
7967   (unless (gnus-check-backend-function 'request-expire-articles
7968                                        gnus-newsgroup-name)
7969     (error "The current newsgroup does not support article deletion"))
7970   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7971     (error "Couldn't open server"))
7972   ;; Compute the list of articles to delete.
7973   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7974         not-deleted)
7975     (if (and gnus-novice-user
7976              (not (gnus-yes-or-no-p
7977                    (format "Do you really want to delete %s forever? "
7978                            (if (> (length articles) 1)
7979                                (format "these %s articles" (length articles))
7980                              "this article")))))
7981         ()
7982       ;; Delete the articles.
7983       (setq not-deleted (gnus-request-expire-articles
7984                          articles gnus-newsgroup-name 'force))
7985       (while articles
7986         (gnus-summary-remove-process-mark (car articles))
7987         ;; The backend might not have been able to delete the article
7988         ;; after all.
7989         (unless (memq (car articles) not-deleted)
7990           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7991         (setq articles (cdr articles)))
7992       (when not-deleted
7993         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7994     (gnus-summary-position-point)
7995     (gnus-set-mode-line 'summary)
7996     not-deleted))
7997
7998 (defun gnus-summary-edit-article (&optional arg)
7999   "Edit the current article.
8000 This will have permanent effect only in mail groups.
8001 If ARG is nil, edit the decoded articles.
8002 If ARG is 1, edit the raw articles. 
8003 If ARG is 2, edit the raw articles even in read-only groups.
8004 If ARG is 3, edit the articles with the current handles.
8005 Otherwise, allow editing of articles even in read-only
8006 groups."
8007   (interactive "P")
8008   (let (force raw current-handles)
8009     (cond 
8010      ((null arg))
8011      ((eq arg 1) (setq raw t))
8012      ((eq arg 2) (setq raw t
8013                        force t))
8014      ((eq arg 3) (setq current-handles 
8015                        (and (gnus-buffer-live-p gnus-article-buffer)
8016                             (with-current-buffer gnus-article-buffer
8017                               (prog1
8018                                   gnus-article-mime-handles
8019                                   (setq gnus-article-mime-handles nil))))))
8020      (t (setq force t)))
8021     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8022         (error "Can't edit the raw article in group nndraft:drafts."))
8023     (save-excursion
8024       (set-buffer gnus-summary-buffer)
8025       (let ((mail-parse-charset gnus-newsgroup-charset)
8026             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8027         (gnus-set-global-variables)
8028         (when (and (not force)
8029                    (gnus-group-read-only-p))
8030           (error "The current newsgroup does not support article editing"))
8031         (gnus-summary-show-article t)
8032         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8033           (with-current-buffer gnus-article-buffer
8034             (mm-enable-multibyte-mule4)))
8035         (if (equal gnus-newsgroup-name "nndraft:drafts")
8036             (setq raw t))
8037         (gnus-article-edit-article
8038          (if raw 'ignore 
8039            `(lambda () 
8040               (let ((mbl mml-buffer-list))
8041                 (setq mml-buffer-list nil)
8042                 (mime-to-mml ,'current-handles)
8043                 (make-local-hook 'kill-buffer-hook)
8044                 (let ((mbl1 mml-buffer-list))
8045                   (setq mml-buffer-list mbl)
8046                   (set (make-local-variable 'mml-buffer-list) mbl1))
8047                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8048          `(lambda (no-highlight)
8049             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8050                   (message-options message-options)
8051                   (message-options-set-recipient)
8052                   (mail-parse-ignored-charsets 
8053                    ',gnus-newsgroup-ignored-charsets))
8054               ,(if (not raw) '(progn 
8055                                 (mml-to-mime)
8056                                 (mml-destroy-buffers)
8057                                 (remove-hook 'kill-buffer-hook 
8058                                              'mml-destroy-buffers t)
8059                                 (kill-local-variable 'mml-buffer-list)))
8060               (gnus-summary-edit-article-done
8061                ,(or (mail-header-references gnus-current-headers) "")
8062                ,(gnus-group-read-only-p) 
8063                ,gnus-summary-buffer no-highlight))))))))
8064
8065 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8066
8067 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8068                                                  no-highlight)
8069   "Make edits to the current article permanent."
8070   (interactive)
8071   (save-excursion
8072     ;; The buffer restriction contains the entire article if it exists.
8073     (when (article-goto-body)
8074       (let ((lines (count-lines (point) (point-max)))
8075             (length (- (point-max) (point)))
8076             (case-fold-search t)
8077             (body (copy-marker (point))))
8078         (goto-char (point-min))
8079         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8080           (delete-region (match-beginning 1) (match-end 1))
8081           (insert (number-to-string length)))
8082         (goto-char (point-min))
8083         (when (re-search-forward
8084                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8085           (delete-region (match-beginning 1) (match-end 1))
8086           (insert (number-to-string length)))
8087         (goto-char (point-min))
8088         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8089           (delete-region (match-beginning 1) (match-end 1))
8090           (insert (number-to-string lines))))))
8091   ;; Replace the article.
8092   (let ((buf (current-buffer)))
8093     (with-temp-buffer
8094       (insert-buffer-substring buf)
8095       
8096       (if (and (not read-only)
8097                (not (gnus-request-replace-article
8098                      (cdr gnus-article-current) (car gnus-article-current)
8099                      (current-buffer) t)))
8100           (error "Couldn't replace article")
8101         ;; Update the summary buffer.
8102         (if (and references
8103                  (equal (message-tokenize-header references " ")
8104                         (message-tokenize-header
8105                          (or (message-fetch-field "references") "") " ")))
8106             ;; We only have to update this line.
8107             (save-excursion
8108               (save-restriction
8109                 (message-narrow-to-head)
8110                 (let ((head (buffer-string))
8111                       header)
8112                   (with-temp-buffer
8113                     (insert (format "211 %d Article retrieved.\n"
8114                                     (cdr gnus-article-current)))
8115                     (insert head)
8116                     (insert ".\n")
8117                     (let ((nntp-server-buffer (current-buffer)))
8118                       (setq header (car (gnus-get-newsgroup-headers
8119                                          (save-excursion
8120                                            (set-buffer gnus-summary-buffer)
8121                                            gnus-newsgroup-dependencies)
8122                                          t))))
8123                     (save-excursion
8124                       (set-buffer gnus-summary-buffer)
8125                       (gnus-data-set-header
8126                        (gnus-data-find (cdr gnus-article-current))
8127                        header)
8128                       (gnus-summary-update-article-line
8129                        (cdr gnus-article-current) header))))))
8130           ;; Update threads.
8131           (set-buffer (or buffer gnus-summary-buffer))
8132           (gnus-summary-update-article (cdr gnus-article-current)))
8133         ;; Prettify the article buffer again.
8134         (unless no-highlight
8135           (save-excursion
8136             (set-buffer gnus-article-buffer)
8137             ;;;!!! Fix this -- article should be rehighlighted.
8138             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8139             (set-buffer gnus-original-article-buffer)
8140             (gnus-request-article
8141              (cdr gnus-article-current)
8142              (car gnus-article-current) (current-buffer))))
8143         ;; Prettify the summary buffer line.
8144         (when (gnus-visual-p 'summary-highlight 'highlight)
8145           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8146
8147 (defun gnus-summary-edit-wash (key)
8148   "Perform editing command KEY in the article buffer."
8149   (interactive
8150    (list
8151     (progn
8152       (message "%s" (concat (this-command-keys) "- "))
8153       (read-char))))
8154   (message "")
8155   (gnus-summary-edit-article)
8156   (execute-kbd-macro (concat (this-command-keys) key))
8157   (gnus-article-edit-done))
8158
8159 ;;; Respooling
8160
8161 (defun gnus-summary-respool-query (&optional silent trace)
8162   "Query where the respool algorithm would put this article."
8163   (interactive)
8164   (let (gnus-mark-article-hook)
8165     (gnus-summary-select-article)
8166     (save-excursion
8167       (set-buffer gnus-original-article-buffer)
8168       (save-restriction
8169         (message-narrow-to-head)
8170         (let ((groups (nnmail-article-group 'identity trace)))
8171           (unless silent
8172             (if groups
8173                 (message "This message would go to %s"
8174                          (mapconcat 'car groups ", "))
8175               (message "This message would go to no groups"))
8176             groups))))))
8177
8178 (defun gnus-summary-respool-trace ()
8179   "Trace where the respool algorithm would put this article.
8180 Display a buffer showing all fancy splitting patterns which matched."
8181   (interactive)
8182   (gnus-summary-respool-query nil t))
8183
8184 ;; Summary marking commands.
8185
8186 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8187   "Mark articles which has the same subject as read, and then select the next.
8188 If UNMARK is positive, remove any kind of mark.
8189 If UNMARK is negative, tick articles."
8190   (interactive "P")
8191   (when unmark
8192     (setq unmark (prefix-numeric-value unmark)))
8193   (let ((count
8194          (gnus-summary-mark-same-subject
8195           (gnus-summary-article-subject) unmark)))
8196     ;; Select next unread article.  If auto-select-same mode, should
8197     ;; select the first unread article.
8198     (gnus-summary-next-article t (and gnus-auto-select-same
8199                                       (gnus-summary-article-subject)))
8200     (gnus-message 7 "%d article%s marked as %s"
8201                   count (if (= count 1) " is" "s are")
8202                   (if unmark "unread" "read"))))
8203
8204 (defun gnus-summary-kill-same-subject (&optional unmark)
8205   "Mark articles which has the same subject as read.
8206 If UNMARK is positive, remove any kind of mark.
8207 If UNMARK is negative, tick articles."
8208   (interactive "P")
8209   (when unmark
8210     (setq unmark (prefix-numeric-value unmark)))
8211   (let ((count
8212          (gnus-summary-mark-same-subject
8213           (gnus-summary-article-subject) unmark)))
8214     ;; If marked as read, go to next unread subject.
8215     (when (null unmark)
8216       ;; Go to next unread subject.
8217       (gnus-summary-next-subject 1 t))
8218     (gnus-message 7 "%d articles are marked as %s"
8219                   count (if unmark "unread" "read"))))
8220
8221 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8222   "Mark articles with same SUBJECT as read, and return marked number.
8223 If optional argument UNMARK is positive, remove any kinds of marks.
8224 If optional argument UNMARK is negative, mark articles as unread instead."
8225   (let ((count 1))
8226     (save-excursion
8227       (cond
8228        ((null unmark)                   ; Mark as read.
8229         (while (and
8230                 (progn
8231                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8232                   (gnus-summary-show-thread) t)
8233                 (gnus-summary-find-subject subject))
8234           (setq count (1+ count))))
8235        ((> unmark 0)                    ; Tick.
8236         (while (and
8237                 (progn
8238                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8239                   (gnus-summary-show-thread) t)
8240                 (gnus-summary-find-subject subject))
8241           (setq count (1+ count))))
8242        (t                               ; Mark as unread.
8243         (while (and
8244                 (progn
8245                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8246                   (gnus-summary-show-thread) t)
8247                 (gnus-summary-find-subject subject))
8248           (setq count (1+ count)))))
8249       (gnus-set-mode-line 'summary)
8250       ;; Return the number of marked articles.
8251       count)))
8252
8253 (defun gnus-summary-mark-as-processable (n &optional unmark)
8254   "Set the process mark on the next N articles.
8255 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8256 the process mark instead.  The difference between N and the actual
8257 number of articles marked is returned."
8258   (interactive "p")
8259   (let ((backward (< n 0))
8260         (n (abs n)))
8261     (while (and
8262             (> n 0)
8263             (if unmark
8264                 (gnus-summary-remove-process-mark
8265                  (gnus-summary-article-number))
8266               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8267             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8268       (setq n (1- n)))
8269     (when (/= 0 n)
8270       (gnus-message 7 "No more articles"))
8271     (gnus-summary-recenter)
8272     (gnus-summary-position-point)
8273     n))
8274
8275 (defun gnus-summary-unmark-as-processable (n)
8276   "Remove the process mark from the next N articles.
8277 If N is negative, unmark backward instead.  The difference between N and
8278 the actual number of articles unmarked is returned."
8279   (interactive "p")
8280   (gnus-summary-mark-as-processable n t))
8281
8282 (defun gnus-summary-unmark-all-processable ()
8283   "Remove the process mark from all articles."
8284   (interactive)
8285   (save-excursion
8286     (while gnus-newsgroup-processable
8287       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8288   (gnus-summary-position-point))
8289
8290 (defun gnus-summary-mark-as-expirable (n)
8291   "Mark N articles forward as expirable.
8292 If N is negative, mark backward instead.  The difference between N and
8293 the actual number of articles marked is returned."
8294   (interactive "p")
8295   (gnus-summary-mark-forward n gnus-expirable-mark))
8296
8297 (defun gnus-summary-mark-article-as-replied (article)
8298   "Mark ARTICLE replied and update the summary line."
8299   (push article gnus-newsgroup-replied)
8300   (let ((buffer-read-only nil))
8301     (when (gnus-summary-goto-subject article nil t)
8302       (gnus-summary-update-secondary-mark article))))
8303
8304 (defun gnus-summary-set-bookmark (article)
8305   "Set a bookmark in current article."
8306   (interactive (list (gnus-summary-article-number)))
8307   (when (or (not (get-buffer gnus-article-buffer))
8308             (not gnus-current-article)
8309             (not gnus-article-current)
8310             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8311     (error "No current article selected"))
8312   ;; Remove old bookmark, if one exists.
8313   (let ((old (assq article gnus-newsgroup-bookmarks)))
8314     (when old
8315       (setq gnus-newsgroup-bookmarks
8316             (delq old gnus-newsgroup-bookmarks))))
8317   ;; Set the new bookmark, which is on the form
8318   ;; (article-number . line-number-in-body).
8319   (push
8320    (cons article
8321          (save-excursion
8322            (set-buffer gnus-article-buffer)
8323            (count-lines
8324             (min (point)
8325                  (save-excursion
8326                    (goto-char (point-min))
8327                    (search-forward "\n\n" nil t)
8328                    (point)))
8329             (point))))
8330    gnus-newsgroup-bookmarks)
8331   (gnus-message 6 "A bookmark has been added to the current article."))
8332
8333 (defun gnus-summary-remove-bookmark (article)
8334   "Remove the bookmark from the current article."
8335   (interactive (list (gnus-summary-article-number)))
8336   ;; Remove old bookmark, if one exists.
8337   (let ((old (assq article gnus-newsgroup-bookmarks)))
8338     (if old
8339         (progn
8340           (setq gnus-newsgroup-bookmarks
8341                 (delq old gnus-newsgroup-bookmarks))
8342           (gnus-message 6 "Removed bookmark."))
8343       (gnus-message 6 "No bookmark in current article."))))
8344
8345 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8346 (defun gnus-summary-mark-as-dormant (n)
8347   "Mark N articles forward as dormant.
8348 If N is negative, mark backward instead.  The difference between N and
8349 the actual number of articles marked is returned."
8350   (interactive "p")
8351   (gnus-summary-mark-forward n gnus-dormant-mark))
8352
8353 (defun gnus-summary-set-process-mark (article)
8354   "Set the process mark on ARTICLE and update the summary line."
8355   (setq gnus-newsgroup-processable
8356         (cons article
8357               (delq article gnus-newsgroup-processable)))
8358   (when (gnus-summary-goto-subject article)
8359     (gnus-summary-show-thread)
8360     (gnus-summary-goto-subject article)
8361     (gnus-summary-update-secondary-mark article)))
8362
8363 (defun gnus-summary-remove-process-mark (article)
8364   "Remove the process mark from ARTICLE and update the summary line."
8365   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8366   (when (gnus-summary-goto-subject article)
8367     (gnus-summary-show-thread)
8368     (gnus-summary-goto-subject article)
8369     (gnus-summary-update-secondary-mark article)))
8370
8371 (defun gnus-summary-set-saved-mark (article)
8372   "Set the process mark on ARTICLE and update the summary line."
8373   (push article gnus-newsgroup-saved)
8374   (when (gnus-summary-goto-subject article)
8375     (gnus-summary-update-secondary-mark article)))
8376
8377 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8378   "Mark N articles as read forwards.
8379 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8380 The difference between N and the actual number of articles marked is
8381 returned.
8382 Iff NO-EXPIRE, auto-expiry will be inhibited."
8383   (interactive "p")
8384   (gnus-summary-show-thread)
8385   (let ((backward (< n 0))
8386         (gnus-summary-goto-unread
8387          (and gnus-summary-goto-unread
8388               (not (eq gnus-summary-goto-unread 'never))
8389               (not (memq mark (list gnus-unread-mark
8390                                     gnus-ticked-mark gnus-dormant-mark)))))
8391         (n (abs n))
8392         (mark (or mark gnus-del-mark)))
8393     (while (and (> n 0)
8394                 (gnus-summary-mark-article nil mark no-expire)
8395                 (zerop (gnus-summary-next-subject
8396                         (if backward -1 1)
8397                         (and gnus-summary-goto-unread
8398                              (not (eq gnus-summary-goto-unread 'never)))
8399                         t)))
8400       (setq n (1- n)))
8401     (when (/= 0 n)
8402       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8403     (gnus-summary-recenter)
8404     (gnus-summary-position-point)
8405     (gnus-set-mode-line 'summary)
8406     n))
8407
8408 (defun gnus-summary-mark-article-as-read (mark)
8409   "Mark the current article quickly as read with MARK."
8410   (let ((article (gnus-summary-article-number)))
8411     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8412     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8413     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8414     (push (cons article mark) gnus-newsgroup-reads)
8415     ;; Possibly remove from cache, if that is used.
8416     (when gnus-use-cache
8417       (gnus-cache-enter-remove-article article))
8418     ;; Allow the backend to change the mark.
8419     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8420     ;; Check for auto-expiry.
8421     (when (and gnus-newsgroup-auto-expire
8422                (memq mark gnus-auto-expirable-marks))
8423       (setq mark gnus-expirable-mark)
8424       ;; Let the backend know about the mark change.
8425       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8426       (push article gnus-newsgroup-expirable))
8427     ;; Set the mark in the buffer.
8428     (gnus-summary-update-mark mark 'unread)
8429     t))
8430
8431 (defun gnus-summary-mark-article-as-unread (mark)
8432   "Mark the current article quickly as unread with MARK."
8433   (let* ((article (gnus-summary-article-number))
8434          (old-mark (gnus-summary-article-mark article)))
8435     ;; Allow the backend to change the mark.
8436     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8437     (if (eq mark old-mark)
8438         t
8439       (if (<= article 0)
8440           (progn
8441             (gnus-error 1 "Can't mark negative article numbers")
8442             nil)
8443         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8444         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8445         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8446         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8447         (cond ((= mark gnus-ticked-mark)
8448                (push article gnus-newsgroup-marked))
8449               ((= mark gnus-dormant-mark)
8450                (push article gnus-newsgroup-dormant))
8451               (t
8452                (push article gnus-newsgroup-unreads)))
8453         (gnus-pull article gnus-newsgroup-reads)
8454
8455         ;; See whether the article is to be put in the cache.
8456         (and gnus-use-cache
8457              (vectorp (gnus-summary-article-header article))
8458              (save-excursion
8459                (gnus-cache-possibly-enter-article
8460                 gnus-newsgroup-name article
8461                 (= mark gnus-ticked-mark)
8462                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8463
8464         ;; Fix the mark.
8465         (gnus-summary-update-mark mark 'unread)
8466         t))))
8467
8468 (defun gnus-summary-mark-article (&optional article mark no-expire)
8469   "Mark ARTICLE with MARK.  MARK can be any character.
8470 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8471 `??' (dormant) and `?E' (expirable).
8472 If MARK is nil, then the default character `?r' is used.
8473 If ARTICLE is nil, then the article on the current line will be
8474 marked.
8475 Iff NO-EXPIRE, auto-expiry will be inhibited."
8476   ;; The mark might be a string.
8477   (when (stringp mark)
8478     (setq mark (aref mark 0)))
8479   ;; If no mark is given, then we check auto-expiring.
8480   (when (null mark)
8481     (setq mark gnus-del-mark))
8482   (when (and (not no-expire)
8483              gnus-newsgroup-auto-expire
8484              (memq mark gnus-auto-expirable-marks))
8485     (setq mark gnus-expirable-mark))
8486   (let ((article (or article (gnus-summary-article-number)))
8487         (old-mark (gnus-summary-article-mark article)))
8488     ;; Allow the backend to change the mark.
8489     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8490     (if (eq mark old-mark)
8491         t
8492       (unless article
8493         (error "No article on current line"))
8494       (if (not (if (or (= mark gnus-unread-mark)
8495                        (= mark gnus-ticked-mark)
8496                        (= mark gnus-dormant-mark))
8497                    (gnus-mark-article-as-unread article mark)
8498                  (gnus-mark-article-as-read article mark)))
8499           t
8500         ;; See whether the article is to be put in the cache.
8501         (and gnus-use-cache
8502              (not (= mark gnus-canceled-mark))
8503              (vectorp (gnus-summary-article-header article))
8504              (save-excursion
8505                (gnus-cache-possibly-enter-article
8506                 gnus-newsgroup-name article
8507                 (= mark gnus-ticked-mark)
8508                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8509
8510         (when (gnus-summary-goto-subject article nil t)
8511           (let ((buffer-read-only nil))
8512             (gnus-summary-show-thread)
8513             ;; Fix the mark.
8514             (gnus-summary-update-mark mark 'unread)
8515             t))))))
8516
8517 (defun gnus-summary-update-secondary-mark (article)
8518   "Update the secondary (read, process, cache) mark."
8519   (gnus-summary-update-mark
8520    (cond ((memq article gnus-newsgroup-processable)
8521           gnus-process-mark)
8522          ((memq article gnus-newsgroup-cached)
8523           gnus-cached-mark)
8524          ((memq article gnus-newsgroup-replied)
8525           gnus-replied-mark)
8526          ((memq article gnus-newsgroup-saved)
8527           gnus-saved-mark)
8528          (t gnus-unread-mark))
8529    'replied)
8530   (when (gnus-visual-p 'summary-highlight 'highlight)
8531     (gnus-run-hooks 'gnus-summary-update-hook))
8532   t)
8533
8534 (defun gnus-summary-update-mark (mark type)
8535   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8536         (buffer-read-only nil))
8537     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8538     (when forward
8539       (when (looking-at "\r")
8540         (incf forward))
8541       (when (<= (+ forward (point)) (point-max))
8542         ;; Go to the right position on the line.
8543         (goto-char (+ forward (point)))
8544         ;; Replace the old mark with the new mark.
8545         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8546         ;; Optionally update the marks by some user rule.
8547         (when (eq type 'unread)
8548           (gnus-data-set-mark
8549            (gnus-data-find (gnus-summary-article-number)) mark)
8550           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8551
8552 (defun gnus-mark-article-as-read (article &optional mark)
8553   "Enter ARTICLE in the pertinent lists and remove it from others."
8554   ;; Make the article expirable.
8555   (let ((mark (or mark gnus-del-mark)))
8556     (if (= mark gnus-expirable-mark)
8557         (push article gnus-newsgroup-expirable)
8558       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8559     ;; Remove from unread and marked lists.
8560     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8561     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8562     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8563     (push (cons article mark) gnus-newsgroup-reads)
8564     ;; Possibly remove from cache, if that is used.
8565     (when gnus-use-cache
8566       (gnus-cache-enter-remove-article article))
8567     t))
8568
8569 (defun gnus-mark-article-as-unread (article &optional mark)
8570   "Enter ARTICLE in the pertinent lists and remove it from others."
8571   (let ((mark (or mark gnus-ticked-mark)))
8572     (if (<= article 0)
8573         (progn
8574           (gnus-error 1 "Can't mark negative article numbers")
8575           nil)
8576       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8577             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8578             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8579             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8580
8581       ;; Unsuppress duplicates?
8582       (when gnus-suppress-duplicates
8583         (gnus-dup-unsuppress-article article))
8584
8585       (cond ((= mark gnus-ticked-mark)
8586              (push article gnus-newsgroup-marked))
8587             ((= mark gnus-dormant-mark)
8588              (push article gnus-newsgroup-dormant))
8589             (t
8590              (push article gnus-newsgroup-unreads)))
8591       (gnus-pull article gnus-newsgroup-reads)
8592       t)))
8593
8594 (defalias 'gnus-summary-mark-as-unread-forward
8595   'gnus-summary-tick-article-forward)
8596 (make-obsolete 'gnus-summary-mark-as-unread-forward
8597                'gnus-summary-tick-article-forward)
8598 (defun gnus-summary-tick-article-forward (n)
8599   "Tick N articles forwards.
8600 If N is negative, tick backwards instead.
8601 The difference between N and the number of articles ticked is returned."
8602   (interactive "p")
8603   (gnus-summary-mark-forward n gnus-ticked-mark))
8604
8605 (defalias 'gnus-summary-mark-as-unread-backward
8606   'gnus-summary-tick-article-backward)
8607 (make-obsolete 'gnus-summary-mark-as-unread-backward
8608                'gnus-summary-tick-article-backward)
8609 (defun gnus-summary-tick-article-backward (n)
8610   "Tick N articles backwards.
8611 The difference between N and the number of articles ticked is returned."
8612   (interactive "p")
8613   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8614
8615 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8616 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8617 (defun gnus-summary-tick-article (&optional article clear-mark)
8618   "Mark current article as unread.
8619 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8620 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8621   (interactive)
8622   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8623                                        gnus-ticked-mark)))
8624
8625 (defun gnus-summary-mark-as-read-forward (n)
8626   "Mark N articles as read forwards.
8627 If N is negative, mark backwards instead.
8628 The difference between N and the actual number of articles marked is
8629 returned."
8630   (interactive "p")
8631   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8632
8633 (defun gnus-summary-mark-as-read-backward (n)
8634   "Mark the N articles as read backwards.
8635 The difference between N and the actual number of articles marked is
8636 returned."
8637   (interactive "p")
8638   (gnus-summary-mark-forward
8639    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8640
8641 (defun gnus-summary-mark-as-read (&optional article mark)
8642   "Mark current article as read.
8643 ARTICLE specifies the article to be marked as read.
8644 MARK specifies a string to be inserted at the beginning of the line."
8645   (gnus-summary-mark-article article mark))
8646
8647 (defun gnus-summary-clear-mark-forward (n)
8648   "Clear marks from N articles forward.
8649 If N is negative, clear backward instead.
8650 The difference between N and the number of marks cleared is returned."
8651   (interactive "p")
8652   (gnus-summary-mark-forward n gnus-unread-mark))
8653
8654 (defun gnus-summary-clear-mark-backward (n)
8655   "Clear marks from N articles backward.
8656 The difference between N and the number of marks cleared is returned."
8657   (interactive "p")
8658   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8659
8660 (defun gnus-summary-mark-unread-as-read ()
8661   "Intended to be used by `gnus-summary-mark-article-hook'."
8662   (when (memq gnus-current-article gnus-newsgroup-unreads)
8663     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8664
8665 (defun gnus-summary-mark-read-and-unread-as-read ()
8666   "Intended to be used by `gnus-summary-mark-article-hook'."
8667   (let ((mark (gnus-summary-article-mark)))
8668     (when (or (gnus-unread-mark-p mark)
8669               (gnus-read-mark-p mark))
8670       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8671
8672 (defun gnus-summary-mark-region-as-read (point mark all)
8673   "Mark all unread articles between point and mark as read.
8674 If given a prefix, mark all articles between point and mark as read,
8675 even ticked and dormant ones."
8676   (interactive "r\nP")
8677   (save-excursion
8678     (let (article)
8679       (goto-char point)
8680       (beginning-of-line)
8681       (while (and
8682               (< (point) mark)
8683               (progn
8684                 (when (or all
8685                           (memq (setq article (gnus-summary-article-number))
8686                                 gnus-newsgroup-unreads))
8687                   (gnus-summary-mark-article article gnus-del-mark))
8688                 t)
8689               (gnus-summary-find-next))))))
8690
8691 (defun gnus-summary-mark-below (score mark)
8692   "Mark articles with score less than SCORE with MARK."
8693   (interactive "P\ncMark: ")
8694   (setq score (if score
8695                   (prefix-numeric-value score)
8696                 (or gnus-summary-default-score 0)))
8697   (save-excursion
8698     (set-buffer gnus-summary-buffer)
8699     (goto-char (point-min))
8700     (while
8701         (progn
8702           (and (< (gnus-summary-article-score) score)
8703                (gnus-summary-mark-article nil mark))
8704           (gnus-summary-find-next)))))
8705
8706 (defun gnus-summary-kill-below (&optional score)
8707   "Mark articles with score below SCORE as read."
8708   (interactive "P")
8709   (gnus-summary-mark-below score gnus-killed-mark))
8710
8711 (defun gnus-summary-clear-above (&optional score)
8712   "Clear all marks from articles with score above SCORE."
8713   (interactive "P")
8714   (gnus-summary-mark-above score gnus-unread-mark))
8715
8716 (defun gnus-summary-tick-above (&optional score)
8717   "Tick all articles with score above SCORE."
8718   (interactive "P")
8719   (gnus-summary-mark-above score gnus-ticked-mark))
8720
8721 (defun gnus-summary-mark-above (score mark)
8722   "Mark articles with score over SCORE with MARK."
8723   (interactive "P\ncMark: ")
8724   (setq score (if score
8725                   (prefix-numeric-value score)
8726                 (or gnus-summary-default-score 0)))
8727   (save-excursion
8728     (set-buffer gnus-summary-buffer)
8729     (goto-char (point-min))
8730     (while (and (progn
8731                   (when (> (gnus-summary-article-score) score)
8732                     (gnus-summary-mark-article nil mark))
8733                   t)
8734                 (gnus-summary-find-next)))))
8735
8736 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8737 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8738 (defun gnus-summary-limit-include-expunged (&optional no-error)
8739   "Display all the hidden articles that were expunged for low scores."
8740   (interactive)
8741   (let ((buffer-read-only nil))
8742     (let ((scored gnus-newsgroup-scored)
8743           headers h)
8744       (while scored
8745         (unless (gnus-summary-goto-subject (caar scored))
8746           (and (setq h (gnus-summary-article-header (caar scored)))
8747                (< (cdar scored) gnus-summary-expunge-below)
8748                (push h headers)))
8749         (setq scored (cdr scored)))
8750       (if (not headers)
8751           (when (not no-error)
8752             (error "No expunged articles hidden"))
8753         (goto-char (point-min))
8754         (gnus-summary-prepare-unthreaded (nreverse headers))
8755         (goto-char (point-min))
8756         (gnus-summary-position-point)
8757         t))))
8758
8759 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8760   "Mark all unread articles in this newsgroup as read.
8761 If prefix argument ALL is non-nil, ticked and dormant articles will
8762 also be marked as read.
8763 If QUIETLY is non-nil, no questions will be asked.
8764 If TO-HERE is non-nil, it should be a point in the buffer.  All
8765 articles before this point will be marked as read.
8766 Note that this function will only catch up the unread article
8767 in the current summary buffer limitation.
8768 The number of articles marked as read is returned."
8769   (interactive "P")
8770   (prog1
8771       (save-excursion
8772         (when (or quietly
8773                   (not gnus-interactive-catchup) ;Without confirmation?
8774                   gnus-expert-user
8775                   (gnus-y-or-n-p
8776                    (if all
8777                        "Mark absolutely all articles as read? "
8778                      "Mark all unread articles as read? ")))
8779           (if (and not-mark
8780                    (not gnus-newsgroup-adaptive)
8781                    (not gnus-newsgroup-auto-expire)
8782                    (not gnus-suppress-duplicates)
8783                    (or (not gnus-use-cache)
8784                        (eq gnus-use-cache 'passive)))
8785               (progn
8786                 (when all
8787                   (setq gnus-newsgroup-marked nil
8788                         gnus-newsgroup-dormant nil))
8789                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8790             ;; We actually mark all articles as canceled, which we
8791             ;; have to do when using auto-expiry or adaptive scoring.
8792             (gnus-summary-show-all-threads)
8793             (when (gnus-summary-first-subject (not all) t)
8794               (while (and
8795                       (if to-here (< (point) to-here) t)
8796                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8797                       (gnus-summary-find-next (not all) nil nil t))))
8798             (gnus-set-mode-line 'summary))
8799           t))
8800     (gnus-summary-position-point)))
8801
8802 (defun gnus-summary-catchup-to-here (&optional all)
8803   "Mark all unticked articles before the current one as read.
8804 If ALL is non-nil, also mark ticked and dormant articles as read."
8805   (interactive "P")
8806   (save-excursion
8807     (gnus-save-hidden-threads
8808       (let ((beg (point)))
8809         ;; We check that there are unread articles.
8810         (when (or all (gnus-summary-find-prev))
8811           (gnus-summary-catchup all t beg)))))
8812   (gnus-summary-position-point))
8813
8814 (defun gnus-summary-catchup-all (&optional quietly)
8815   "Mark all articles in this newsgroup as read."
8816   (interactive "P")
8817   (gnus-summary-catchup t quietly))
8818
8819 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8820   "Mark all unread articles in this group as read, then exit.
8821 If prefix argument ALL is non-nil, all articles are marked as read."
8822   (interactive "P")
8823   (when (gnus-summary-catchup all quietly nil 'fast)
8824     ;; Select next newsgroup or exit.
8825     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8826              (eq gnus-auto-select-next 'quietly))
8827         (gnus-summary-next-group nil)
8828       (gnus-summary-exit))))
8829
8830 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8831   "Mark all articles in this newsgroup as read, and then exit."
8832   (interactive "P")
8833   (gnus-summary-catchup-and-exit t quietly))
8834
8835 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8836   "Mark all articles in this group as read and select the next group.
8837 If given a prefix, mark all articles, unread as well as ticked, as
8838 read."
8839   (interactive "P")
8840   (save-excursion
8841     (gnus-summary-catchup all))
8842   (gnus-summary-next-group))
8843
8844 ;;;
8845 ;;; with article
8846 ;;;
8847
8848 (defmacro gnus-with-article (article &rest forms)
8849   "Select ARTICLE and perform FORMS in the original article buffer.
8850 Then replace the article with the result."
8851   `(progn
8852      ;; We don't want the article to be marked as read.
8853      (let (gnus-mark-article-hook)
8854        (gnus-summary-select-article t t nil ,article))
8855      (set-buffer gnus-original-article-buffer)
8856      ,@forms
8857      (if (not (gnus-check-backend-function
8858                'request-replace-article (car gnus-article-current)))
8859          (gnus-message 5 "Read-only group; not replacing")
8860        (unless (gnus-request-replace-article
8861                 ,article (car gnus-article-current)
8862                 (current-buffer) t)
8863          (error "Couldn't replace article")))
8864      ;; The cache and backlog have to be flushed somewhat.
8865      (when gnus-keep-backlog
8866        (gnus-backlog-remove-article
8867         (car gnus-article-current) (cdr gnus-article-current)))
8868      (when gnus-use-cache
8869        (gnus-cache-update-article
8870         (car gnus-article-current) (cdr gnus-article-current)))))
8871
8872 (put 'gnus-with-article 'lisp-indent-function 1)
8873 (put 'gnus-with-article 'edebug-form-spec '(form body))
8874
8875 ;; Thread-based commands.
8876
8877 (defun gnus-summary-articles-in-thread (&optional article)
8878   "Return a list of all articles in the current thread.
8879 If ARTICLE is non-nil, return all articles in the thread that starts
8880 with that article."
8881   (let* ((article (or article (gnus-summary-article-number)))
8882          (data (gnus-data-find-list article))
8883          (top-level (gnus-data-level (car data)))
8884          (top-subject
8885           (cond ((null gnus-thread-operation-ignore-subject)
8886                  (gnus-simplify-subject-re
8887                   (mail-header-subject (gnus-data-header (car data)))))
8888                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8889                  (gnus-simplify-subject-fuzzy
8890                   (mail-header-subject (gnus-data-header (car data)))))
8891                 (t nil)))
8892          (end-point (save-excursion
8893                       (if (gnus-summary-go-to-next-thread)
8894                           (point) (point-max))))
8895          articles)
8896     (while (and data
8897                 (< (gnus-data-pos (car data)) end-point))
8898       (when (or (not top-subject)
8899                 (string= top-subject
8900                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8901                              (gnus-simplify-subject-fuzzy
8902                               (mail-header-subject
8903                                (gnus-data-header (car data))))
8904                            (gnus-simplify-subject-re
8905                             (mail-header-subject
8906                              (gnus-data-header (car data)))))))
8907         (push (gnus-data-number (car data)) articles))
8908       (unless (and (setq data (cdr data))
8909                    (> (gnus-data-level (car data)) top-level))
8910         (setq data nil)))
8911     ;; Return the list of articles.
8912     (nreverse articles)))
8913
8914 (defun gnus-summary-rethread-current ()
8915   "Rethread the thread the current article is part of."
8916   (interactive)
8917   (let* ((gnus-show-threads t)
8918          (article (gnus-summary-article-number))
8919          (id (mail-header-id (gnus-summary-article-header)))
8920          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8921     (unless id
8922       (error "No article on the current line"))
8923     (gnus-rebuild-thread id)
8924     (gnus-summary-goto-subject article)))
8925
8926 (defun gnus-summary-reparent-thread ()
8927   "Make the current article child of the marked (or previous) article.
8928
8929 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8930 is non-nil or the Subject: of both articles are the same."
8931   (interactive)
8932   (unless (not (gnus-group-read-only-p))
8933     (error "The current newsgroup does not support article editing"))
8934   (unless (<= (length gnus-newsgroup-processable) 1)
8935     (error "No more than one article may be marked"))
8936   (save-window-excursion
8937     (let ((gnus-article-buffer " *reparent*")
8938           (current-article (gnus-summary-article-number))
8939           ;; First grab the marked article, otherwise one line up.
8940           (parent-article (if (not (null gnus-newsgroup-processable))
8941                               (car gnus-newsgroup-processable)
8942                             (save-excursion
8943                               (if (eq (forward-line -1) 0)
8944                                   (gnus-summary-article-number)
8945                                 (error "Beginning of summary buffer"))))))
8946       (unless (not (eq current-article parent-article))
8947         (error "An article may not be self-referential"))
8948       (let ((message-id (mail-header-id
8949                          (gnus-summary-article-header parent-article))))
8950         (unless (and message-id (not (equal message-id "")))
8951           (error "No message-id in desired parent"))
8952         (gnus-with-article current-article
8953           (save-restriction
8954             (goto-char (point-min))
8955             (message-narrow-to-head)
8956             (if (re-search-forward "^References: " nil t)
8957                 (progn
8958                   (re-search-forward "^[^ \t]" nil t)
8959                   (forward-line -1)
8960                   (end-of-line)
8961                   (insert " " message-id))
8962               (insert "References: " message-id "\n"))))
8963         (set-buffer gnus-summary-buffer)
8964         (gnus-summary-unmark-all-processable)
8965         (gnus-summary-update-article current-article)
8966         (gnus-summary-rethread-current)
8967         (gnus-message 3 "Article %d is now the child of article %d"
8968                       current-article parent-article)))))
8969
8970 (defun gnus-summary-toggle-threads (&optional arg)
8971   "Toggle showing conversation threads.
8972 If ARG is positive number, turn showing conversation threads on."
8973   (interactive "P")
8974   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8975     (setq gnus-show-threads
8976           (if (null arg) (not gnus-show-threads)
8977             (> (prefix-numeric-value arg) 0)))
8978     (gnus-summary-prepare)
8979     (gnus-summary-goto-subject current)
8980     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8981     (gnus-summary-position-point)))
8982
8983 (defun gnus-summary-show-all-threads ()
8984   "Show all threads."
8985   (interactive)
8986   (save-excursion
8987     (let ((buffer-read-only nil))
8988       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8989   (gnus-summary-position-point))
8990
8991 (defun gnus-summary-show-thread ()
8992   "Show thread subtrees.
8993 Returns nil if no thread was there to be shown."
8994   (interactive)
8995   (let ((buffer-read-only nil)
8996         (orig (point))
8997         ;; first goto end then to beg, to have point at beg after let
8998         (end (progn (end-of-line) (point)))
8999         (beg (progn (beginning-of-line) (point))))
9000     (prog1
9001         ;; Any hidden lines here?
9002         (search-forward "\r" end t)
9003       (subst-char-in-region beg end ?\^M ?\n t)
9004       (goto-char orig)
9005       (gnus-summary-position-point))))
9006
9007 (defun gnus-summary-hide-all-threads ()
9008   "Hide all thread subtrees."
9009   (interactive)
9010   (save-excursion
9011     (goto-char (point-min))
9012     (gnus-summary-hide-thread)
9013     (while (zerop (gnus-summary-next-thread 1 t))
9014       (gnus-summary-hide-thread)))
9015   (gnus-summary-position-point))
9016
9017 (defun gnus-summary-hide-thread ()
9018   "Hide thread subtrees.
9019 Returns nil if no threads were there to be hidden."
9020   (interactive)
9021   (let ((buffer-read-only nil)
9022         (start (point))
9023         (article (gnus-summary-article-number)))
9024     (goto-char start)
9025     ;; Go forward until either the buffer ends or the subthread
9026     ;; ends.
9027     (when (and (not (eobp))
9028                (or (zerop (gnus-summary-next-thread 1 t))
9029                    (goto-char (point-max))))
9030       (prog1
9031           (if (and (> (point) start)
9032                    (search-backward "\n" start t))
9033               (progn
9034                 (subst-char-in-region start (point) ?\n ?\^M)
9035                 (gnus-summary-goto-subject article))
9036             (goto-char start)
9037             nil)))))
9038
9039 (defun gnus-summary-go-to-next-thread (&optional previous)
9040   "Go to the same level (or less) next thread.
9041 If PREVIOUS is non-nil, go to previous thread instead.
9042 Return the article number moved to, or nil if moving was impossible."
9043   (let ((level (gnus-summary-thread-level))
9044         (way (if previous -1 1))
9045         (beg (point)))
9046     (forward-line way)
9047     (while (and (not (eobp))
9048                 (< level (gnus-summary-thread-level)))
9049       (forward-line way))
9050     (if (eobp)
9051         (progn
9052           (goto-char beg)
9053           nil)
9054       (setq beg (point))
9055       (prog1
9056           (gnus-summary-article-number)
9057         (goto-char beg)))))
9058
9059 (defun gnus-summary-next-thread (n &optional silent)
9060   "Go to the same level next N'th thread.
9061 If N is negative, search backward instead.
9062 Returns the difference between N and the number of skips actually
9063 done.
9064
9065 If SILENT, don't output messages."
9066   (interactive "p")
9067   (let ((backward (< n 0))
9068         (n (abs n)))
9069     (while (and (> n 0)
9070                 (gnus-summary-go-to-next-thread backward))
9071       (decf n))
9072     (unless silent
9073       (gnus-summary-position-point))
9074     (when (and (not silent) (/= 0 n))
9075       (gnus-message 7 "No more threads"))
9076     n))
9077
9078 (defun gnus-summary-prev-thread (n)
9079   "Go to the same level previous N'th thread.
9080 Returns the difference between N and the number of skips actually
9081 done."
9082   (interactive "p")
9083   (gnus-summary-next-thread (- n)))
9084
9085 (defun gnus-summary-go-down-thread ()
9086   "Go down one level in the current thread."
9087   (let ((children (gnus-summary-article-children)))
9088     (when children
9089       (gnus-summary-goto-subject (car children)))))
9090
9091 (defun gnus-summary-go-up-thread ()
9092   "Go up one level in the current thread."
9093   (let ((parent (gnus-summary-article-parent)))
9094     (when parent
9095       (gnus-summary-goto-subject parent))))
9096
9097 (defun gnus-summary-down-thread (n)
9098   "Go down thread N steps.
9099 If N is negative, go up instead.
9100 Returns the difference between N and how many steps down that were
9101 taken."
9102   (interactive "p")
9103   (let ((up (< n 0))
9104         (n (abs n)))
9105     (while (and (> n 0)
9106                 (if up (gnus-summary-go-up-thread)
9107                   (gnus-summary-go-down-thread)))
9108       (setq n (1- n)))
9109     (gnus-summary-position-point)
9110     (when (/= 0 n)
9111       (gnus-message 7 "Can't go further"))
9112     n))
9113
9114 (defun gnus-summary-up-thread (n)
9115   "Go up thread N steps.
9116 If N is negative, go up instead.
9117 Returns the difference between N and how many steps down that were
9118 taken."
9119   (interactive "p")
9120   (gnus-summary-down-thread (- n)))
9121
9122 (defun gnus-summary-top-thread ()
9123   "Go to the top of the thread."
9124   (interactive)
9125   (while (gnus-summary-go-up-thread))
9126   (gnus-summary-article-number))
9127
9128 (defun gnus-summary-kill-thread (&optional unmark)
9129   "Mark articles under current thread as read.
9130 If the prefix argument is positive, remove any kinds of marks.
9131 If the prefix argument is negative, tick articles instead."
9132   (interactive "P")
9133   (when unmark
9134     (setq unmark (prefix-numeric-value unmark)))
9135   (let ((articles (gnus-summary-articles-in-thread)))
9136     (save-excursion
9137       ;; Expand the thread.
9138       (gnus-summary-show-thread)
9139       ;; Mark all the articles.
9140       (while articles
9141         (gnus-summary-goto-subject (car articles))
9142         (cond ((null unmark)
9143                (gnus-summary-mark-article-as-read gnus-killed-mark))
9144               ((> unmark 0)
9145                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9146               (t
9147                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9148         (setq articles (cdr articles))))
9149     ;; Hide killed subtrees.
9150     (and (null unmark)
9151          gnus-thread-hide-killed
9152          (gnus-summary-hide-thread))
9153     ;; If marked as read, go to next unread subject.
9154     (when (null unmark)
9155       ;; Go to next unread subject.
9156       (gnus-summary-next-subject 1 t)))
9157   (gnus-set-mode-line 'summary))
9158
9159 ;; Summary sorting commands
9160
9161 (defun gnus-summary-sort-by-number (&optional reverse)
9162   "Sort the summary buffer by article number.
9163 Argument REVERSE means reverse order."
9164   (interactive "P")
9165   (gnus-summary-sort 'number reverse))
9166
9167 (defun gnus-summary-sort-by-author (&optional reverse)
9168   "Sort the summary buffer by author name alphabetically.
9169 If `case-fold-search' is non-nil, case of letters is ignored.
9170 Argument REVERSE means reverse order."
9171   (interactive "P")
9172   (gnus-summary-sort 'author reverse))
9173
9174 (defun gnus-summary-sort-by-subject (&optional reverse)
9175   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9176 If `case-fold-search' is non-nil, case of letters is ignored.
9177 Argument REVERSE means reverse order."
9178   (interactive "P")
9179   (gnus-summary-sort 'subject reverse))
9180
9181 (defun gnus-summary-sort-by-date (&optional reverse)
9182   "Sort the summary buffer by date.
9183 Argument REVERSE means reverse order."
9184   (interactive "P")
9185   (gnus-summary-sort 'date reverse))
9186
9187 (defun gnus-summary-sort-by-score (&optional reverse)
9188   "Sort the summary buffer by score.
9189 Argument REVERSE means reverse order."
9190   (interactive "P")
9191   (gnus-summary-sort 'score reverse))
9192
9193 (defun gnus-summary-sort-by-lines (&optional reverse)
9194   "Sort the summary buffer by the number of lines.
9195 Argument REVERSE means reverse order."
9196   (interactive "P")
9197   (gnus-summary-sort 'lines reverse))
9198
9199 (defun gnus-summary-sort-by-chars (&optional reverse)
9200   "Sort the summary buffer by article length.
9201 Argument REVERSE means reverse order."
9202   (interactive "P")
9203   (gnus-summary-sort 'chars reverse))
9204
9205 (defun gnus-summary-sort (predicate reverse)
9206   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9207   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9208          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9209          (gnus-thread-sort-functions
9210           (if (not reverse)
9211               thread
9212             `(lambda (t1 t2)
9213                (,thread t2 t1))))
9214          (gnus-sort-gathered-threads-function
9215           gnus-thread-sort-functions)
9216          (gnus-article-sort-functions
9217           (if (not reverse)
9218               article
9219             `(lambda (t1 t2)
9220                (,article t2 t1))))
9221          (buffer-read-only)
9222          (gnus-summary-prepare-hook nil))
9223     ;; We do the sorting by regenerating the threads.
9224     (gnus-summary-prepare)
9225     ;; Hide subthreads if needed.
9226     (when (and gnus-show-threads gnus-thread-hide-subtree)
9227       (gnus-summary-hide-all-threads))))
9228
9229 ;; Summary saving commands.
9230
9231 (defun gnus-summary-save-article (&optional n not-saved)
9232   "Save the current article using the default saver function.
9233 If N is a positive number, save the N next articles.
9234 If N is a negative number, save the N previous articles.
9235 If N is nil and any articles have been marked with the process mark,
9236 save those articles instead.
9237 The variable `gnus-default-article-saver' specifies the saver function."
9238   (interactive "P")
9239   (let* ((articles (gnus-summary-work-articles n))
9240          (save-buffer (save-excursion
9241                         (nnheader-set-temp-buffer " *Gnus Save*")))
9242          (num (length articles))
9243          header file)
9244     (dolist (article articles)
9245       (setq header (gnus-summary-article-header article))
9246       (if (not (vectorp header))
9247           ;; This is a pseudo-article.
9248           (if (assq 'name header)
9249               (gnus-copy-file (cdr (assq 'name header)))
9250             (gnus-message 1 "Article %d is unsaveable" article))
9251         ;; This is a real article.
9252         (save-window-excursion
9253           (gnus-summary-select-article t nil nil article))
9254         (save-excursion
9255           (set-buffer save-buffer)
9256           (erase-buffer)
9257           (insert-buffer-substring gnus-original-article-buffer))
9258         (setq file (gnus-article-save save-buffer file num))
9259         (gnus-summary-remove-process-mark article)
9260         (unless not-saved
9261           (gnus-summary-set-saved-mark article))))
9262     (gnus-kill-buffer save-buffer)
9263     (gnus-summary-position-point)
9264     (gnus-set-mode-line 'summary)
9265     n))
9266
9267 (defun gnus-summary-pipe-output (&optional arg)
9268   "Pipe the current article to a subprocess.
9269 If N is a positive number, pipe the N next articles.
9270 If N is a negative number, pipe the N previous articles.
9271 If N is nil and any articles have been marked with the process mark,
9272 pipe those articles instead."
9273   (interactive "P")
9274   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9275     (gnus-summary-save-article arg t))
9276   (gnus-configure-windows 'pipe))
9277
9278 (defun gnus-summary-save-article-mail (&optional arg)
9279   "Append the current article to an mail file.
9280 If N is a positive number, save the N next articles.
9281 If N is a negative number, save the N previous articles.
9282 If N is nil and any articles have been marked with the process mark,
9283 save those articles instead."
9284   (interactive "P")
9285   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9286     (gnus-summary-save-article arg)))
9287
9288 (defun gnus-summary-save-article-rmail (&optional arg)
9289   "Append the current article to an rmail file.
9290 If N is a positive number, save the N next articles.
9291 If N is a negative number, save the N previous articles.
9292 If N is nil and any articles have been marked with the process mark,
9293 save those articles instead."
9294   (interactive "P")
9295   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9296     (gnus-summary-save-article arg)))
9297
9298 (defun gnus-summary-save-article-file (&optional arg)
9299   "Append the current article to a file.
9300 If N is a positive number, save the N next articles.
9301 If N is a negative number, save the N previous articles.
9302 If N is nil and any articles have been marked with the process mark,
9303 save those articles instead."
9304   (interactive "P")
9305   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9306     (gnus-summary-save-article arg)))
9307
9308 (defun gnus-summary-write-article-file (&optional arg)
9309   "Write the current article to a file, deleting the previous file.
9310 If N is a positive number, save the N next articles.
9311 If N is a negative number, save the N previous articles.
9312 If N is nil and any articles have been marked with the process mark,
9313 save those articles instead."
9314   (interactive "P")
9315   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9316     (gnus-summary-save-article arg)))
9317
9318 (defun gnus-summary-save-article-body-file (&optional arg)
9319   "Append the current article body to a file.
9320 If N is a positive number, save the N next articles.
9321 If N is a negative number, save the N previous articles.
9322 If N is nil and any articles have been marked with the process mark,
9323 save those articles instead."
9324   (interactive "P")
9325   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9326     (gnus-summary-save-article arg)))
9327
9328 (defun gnus-summary-pipe-message (program)
9329   "Pipe the current article through PROGRAM."
9330   (interactive "sProgram: ")
9331   (gnus-summary-select-article)
9332   (let ((mail-header-separator ""))
9333     (gnus-eval-in-buffer-window gnus-article-buffer
9334       (save-restriction
9335         (widen)
9336         (let ((start (window-start))
9337               buffer-read-only)
9338           (message-pipe-buffer-body program)
9339           (set-window-start (get-buffer-window (current-buffer)) start))))))
9340
9341 (defun gnus-get-split-value (methods)
9342   "Return a value based on the split METHODS."
9343   (let (split-name method result match)
9344     (when methods
9345       (save-excursion
9346         (set-buffer gnus-original-article-buffer)
9347         (save-restriction
9348           (nnheader-narrow-to-headers)
9349           (while (and methods (not split-name))
9350             (goto-char (point-min))
9351             (setq method (pop methods))
9352             (setq match (car method))
9353             (when (cond
9354                    ((stringp match)
9355                     ;; Regular expression.
9356                     (ignore-errors
9357                       (re-search-forward match nil t)))
9358                    ((gnus-functionp match)
9359                     ;; Function.
9360                     (save-restriction
9361                       (widen)
9362                       (setq result (funcall match gnus-newsgroup-name))))
9363                    ((consp match)
9364                     ;; Form.
9365                     (save-restriction
9366                       (widen)
9367                       (setq result (eval match)))))
9368               (setq split-name (cdr method))
9369               (cond ((stringp result)
9370                      (push (expand-file-name
9371                             result gnus-article-save-directory)
9372                            split-name))
9373                     ((consp result)
9374                      (setq split-name (append result split-name)))))))))
9375     (nreverse split-name)))
9376
9377 (defun gnus-valid-move-group-p (group)
9378   (and (boundp group)
9379        (symbol-name group)
9380        (symbol-value group)
9381        (gnus-get-function (gnus-find-method-for-group
9382                            (symbol-name group)) 'request-accept-article t)))
9383
9384 (defun gnus-read-move-group-name (prompt default articles prefix)
9385   "Read a group name."
9386   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9387          (minibuffer-confirm-incomplete nil) ; XEmacs
9388          (prom
9389           (format "%s %s to:"
9390                   prompt
9391                   (if (> (length articles) 1)
9392                       (format "these %d articles" (length articles))
9393                     "this article")))
9394          (to-newsgroup
9395           (cond
9396            ((null split-name)
9397             (gnus-completing-read default prom
9398                                   gnus-active-hashtb
9399                                   'gnus-valid-move-group-p
9400                                   nil prefix
9401                                   'gnus-group-history))
9402            ((= 1 (length split-name))
9403             (gnus-completing-read (car split-name) prom
9404                                   gnus-active-hashtb
9405                                   'gnus-valid-move-group-p
9406                                   nil nil
9407                                   'gnus-group-history))
9408            (t
9409             (gnus-completing-read nil prom
9410                                   (mapcar (lambda (el) (list el))
9411                                           (nreverse split-name))
9412                                   nil nil nil
9413                                   'gnus-group-history))))
9414          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9415     (when to-newsgroup
9416       (if (or (string= to-newsgroup "")
9417               (string= to-newsgroup prefix))
9418           (setq to-newsgroup default))
9419       (unless to-newsgroup
9420         (error "No group name entered"))
9421       (or (gnus-active to-newsgroup)
9422           (gnus-activate-group to-newsgroup nil nil to-method)
9423           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9424                                      to-newsgroup))
9425               (or (and (gnus-request-create-group to-newsgroup to-method)
9426                        (gnus-activate-group
9427                         to-newsgroup nil nil to-method)
9428                        (gnus-subscribe-group to-newsgroup))
9429                   (error "Couldn't create group %s" to-newsgroup)))
9430           (error "No such group: %s" to-newsgroup)))
9431     to-newsgroup))
9432
9433 (defun gnus-summary-save-parts (type dir n &optional reverse)
9434   "Save parts matching TYPE to DIR.
9435 If REVERSE, save parts that do not match TYPE."
9436   (interactive
9437    (list (read-string "Save parts of type: " 
9438                       (or (car gnus-summary-save-parts-type-history)
9439                           gnus-summary-save-parts-default-mime)
9440                       'gnus-summary-save-parts-type-history)
9441          (setq gnus-summary-save-parts-last-directory
9442                (read-file-name "Save to directory: " 
9443                                gnus-summary-save-parts-last-directory
9444                                nil t))
9445          current-prefix-arg))
9446   (gnus-summary-iterate n
9447     (let ((gnus-display-mime-function nil)
9448           (gnus-inhibit-treatment t))
9449       (gnus-summary-select-article))
9450     (save-excursion
9451       (set-buffer gnus-article-buffer)
9452       (let ((handles (or gnus-article-mime-handles
9453                          (mm-dissect-buffer) (mm-uu-dissect))))
9454         (when handles
9455           (gnus-summary-save-parts-1 type dir handles reverse)
9456           (unless gnus-article-mime-handles ;; Don't destroy this case.
9457             (mm-destroy-parts handles)))))))
9458
9459 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9460   (if (stringp (car handle))
9461       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9462               (cdr handle))
9463     (when (if reverse
9464               (not (string-match type (mm-handle-media-type handle)))
9465             (string-match type (mm-handle-media-type handle)))
9466       (let ((file (expand-file-name
9467                    (file-name-nondirectory
9468                     (or
9469                      (mail-content-type-get
9470                       (mm-handle-disposition handle) 'filename)
9471                      (concat gnus-newsgroup-name
9472                              "." (number-to-string
9473                                   (cdr gnus-article-current)))))
9474                    dir)))
9475         (unless (file-exists-p file)
9476           (mm-save-part-to-file handle file))))))
9477
9478 ;; Summary extract commands
9479
9480 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9481   (let ((buffer-read-only nil)
9482         (article (gnus-summary-article-number))
9483         after-article b e)
9484     (unless (gnus-summary-goto-subject article)
9485       (error "No such article: %d" article))
9486     (gnus-summary-position-point)
9487     ;; If all commands are to be bunched up on one line, we collect
9488     ;; them here.
9489     (unless gnus-view-pseudos-separately
9490       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9491             files action)
9492         (while ps
9493           (setq action (cdr (assq 'action (car ps))))
9494           (setq files (list (cdr (assq 'name (car ps)))))
9495           (while (and ps (cdr ps)
9496                       (string= (or action "1")
9497                                (or (cdr (assq 'action (cadr ps))) "2")))
9498             (push (cdr (assq 'name (cadr ps))) files)
9499             (setcdr ps (cddr ps)))
9500           (when files
9501             (when (not (string-match "%s" action))
9502               (push " " files))
9503             (push " " files)
9504             (when (assq 'execute (car ps))
9505               (setcdr (assq 'execute (car ps))
9506                       (funcall (if (string-match "%s" action)
9507                                    'format 'concat)
9508                                action
9509                                (mapconcat
9510                                 (lambda (f)
9511                                   (if (equal f " ")
9512                                       f
9513                                     (mm-quote-arg f)))
9514                                 files " ")))))
9515           (setq ps (cdr ps)))))
9516     (if (and gnus-view-pseudos (not not-view))
9517         (while pslist
9518           (when (assq 'execute (car pslist))
9519             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9520                                   (eq gnus-view-pseudos 'not-confirm)))
9521           (setq pslist (cdr pslist)))
9522       (save-excursion
9523         (while pslist
9524           (setq after-article (or (cdr (assq 'article (car pslist)))
9525                                   (gnus-summary-article-number)))
9526           (gnus-summary-goto-subject after-article)
9527           (forward-line 1)
9528           (setq b (point))
9529           (insert "    " (file-name-nondirectory
9530                           (cdr (assq 'name (car pslist))))
9531                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9532           (setq e (point))
9533           (forward-line -1)             ; back to `b'
9534           (gnus-add-text-properties
9535            b (1- e) (list 'gnus-number gnus-reffed-article-number
9536                           gnus-mouse-face-prop gnus-mouse-face))
9537           (gnus-data-enter
9538            after-article gnus-reffed-article-number
9539            gnus-unread-mark b (car pslist) 0 (- e b))
9540           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9541           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9542           (setq pslist (cdr pslist)))))))
9543
9544 (defun gnus-pseudos< (p1 p2)
9545   (let ((c1 (cdr (assq 'action p1)))
9546         (c2 (cdr (assq 'action p2))))
9547     (and c1 c2 (string< c1 c2))))
9548
9549 (defun gnus-request-pseudo-article (props)
9550   (cond ((assq 'execute props)
9551          (gnus-execute-command (cdr (assq 'execute props)))))
9552   (let ((gnus-current-article (gnus-summary-article-number)))
9553     (gnus-run-hooks 'gnus-mark-article-hook)))
9554
9555 (defun gnus-execute-command (command &optional automatic)
9556   (save-excursion
9557     (gnus-article-setup-buffer)
9558     (set-buffer gnus-article-buffer)
9559     (setq buffer-read-only nil)
9560     (let ((command (if automatic command
9561                      (read-string "Command: " (cons command 0)))))
9562       (erase-buffer)
9563       (insert "$ " command "\n\n")
9564       (if gnus-view-pseudo-asynchronously
9565           (start-process "gnus-execute" (current-buffer) shell-file-name
9566                          shell-command-switch command)
9567         (call-process shell-file-name nil t nil
9568                       shell-command-switch command)))))
9569
9570 ;; Summary kill commands.
9571
9572 (defun gnus-summary-edit-global-kill (article)
9573   "Edit the \"global\" kill file."
9574   (interactive (list (gnus-summary-article-number)))
9575   (gnus-group-edit-global-kill article))
9576
9577 (defun gnus-summary-edit-local-kill ()
9578   "Edit a local kill file applied to the current newsgroup."
9579   (interactive)
9580   (setq gnus-current-headers (gnus-summary-article-header))
9581   (gnus-group-edit-local-kill
9582    (gnus-summary-article-number) gnus-newsgroup-name))
9583
9584 ;;; Header reading.
9585
9586 (defun gnus-read-header (id &optional header)
9587   "Read the headers of article ID and enter them into the Gnus system."
9588   (let ((group gnus-newsgroup-name)
9589         (gnus-override-method
9590          (or
9591           gnus-override-method
9592           (and (gnus-news-group-p gnus-newsgroup-name)
9593                (car (gnus-refer-article-methods)))))
9594         where)
9595     ;; First we check to see whether the header in question is already
9596     ;; fetched.
9597     (if (stringp id)
9598         ;; This is a Message-ID.
9599         (setq header (or header (gnus-id-to-header id)))
9600       ;; This is an article number.
9601       (setq header (or header (gnus-summary-article-header id))))
9602     (if (and header
9603              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9604         ;; We have found the header.
9605         header
9606       ;; If this is a sparse article, we have to nix out its
9607       ;; previous entry in the thread hashtb.
9608       (when (and header
9609                  (gnus-summary-article-sparse-p (mail-header-number header)))
9610         (let* ((parent (gnus-parent-id (mail-header-references header)))
9611                (thread (and parent (gnus-id-to-thread parent))))
9612           (when thread
9613             (delq (assq header thread) thread))))
9614       ;; We have to really fetch the header to this article.
9615       (save-excursion
9616         (set-buffer nntp-server-buffer)
9617         (when (setq where (gnus-request-head id group))
9618           (nnheader-fold-continuation-lines)
9619           (goto-char (point-max))
9620           (insert ".\n")
9621           (goto-char (point-min))
9622           (insert "211 ")
9623           (princ (cond
9624                   ((numberp id) id)
9625                   ((cdr where) (cdr where))
9626                   (header (mail-header-number header))
9627                   (t gnus-reffed-article-number))
9628                  (current-buffer))
9629           (insert " Article retrieved.\n"))
9630         (if (or (not where)
9631                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9632             ()                          ; Malformed head.
9633           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9634             (when (and (stringp id)
9635                        (not (string= (gnus-group-real-name group)
9636                                      (car where))))
9637               ;; If we fetched by Message-ID and the article came
9638               ;; from a different group, we fudge some bogus article
9639               ;; numbers for this article.
9640               (mail-header-set-number header gnus-reffed-article-number))
9641             (save-excursion
9642               (set-buffer gnus-summary-buffer)
9643               (decf gnus-reffed-article-number)
9644               (gnus-remove-header (mail-header-number header))
9645               (push header gnus-newsgroup-headers)
9646               (setq gnus-current-headers header)
9647               (push (mail-header-number header) gnus-newsgroup-limit)))
9648           header)))))
9649
9650 (defun gnus-remove-header (number)
9651   "Remove header NUMBER from `gnus-newsgroup-headers'."
9652   (if (and gnus-newsgroup-headers
9653            (= number (mail-header-number (car gnus-newsgroup-headers))))
9654       (pop gnus-newsgroup-headers)
9655     (let ((headers gnus-newsgroup-headers))
9656       (while (and (cdr headers)
9657                   (not (= number (mail-header-number (cadr headers)))))
9658         (pop headers))
9659       (when (cdr headers)
9660         (setcdr headers (cddr headers))))))
9661
9662 ;;;
9663 ;;; summary highlights
9664 ;;;
9665
9666 (defun gnus-highlight-selected-summary ()
9667   "Highlight selected article in summary buffer."
9668   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9669   (when gnus-summary-selected-face
9670     (save-excursion
9671       (let* ((beg (progn (beginning-of-line) (point)))
9672              (end (progn (end-of-line) (point)))
9673              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9674              (from (if (get-text-property beg gnus-mouse-face-prop)
9675                        beg
9676                      (or (next-single-property-change
9677                           beg gnus-mouse-face-prop nil end)
9678                          beg)))
9679              (to
9680               (if (= from end)
9681                   (- from 2)
9682                 (or (next-single-property-change
9683                      from gnus-mouse-face-prop nil end)
9684                     end))))
9685         ;; If no mouse-face prop on line we will have to = from = end,
9686         ;; so we highlight the entire line instead.
9687         (when (= (+ to 2) from)
9688           (setq from beg)
9689           (setq to end))
9690         (if gnus-newsgroup-selected-overlay
9691             ;; Move old overlay.
9692             (gnus-move-overlay
9693              gnus-newsgroup-selected-overlay from to (current-buffer))
9694           ;; Create new overlay.
9695           (gnus-overlay-put
9696            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9697            'face gnus-summary-selected-face))))))
9698
9699 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9700 (defun gnus-summary-highlight-line ()
9701   "Highlight current line according to `gnus-summary-highlight'."
9702   (let* ((list gnus-summary-highlight)
9703          (p (point))
9704          (end (progn (end-of-line) (point)))
9705          ;; now find out where the line starts and leave point there.
9706          (beg (progn (beginning-of-line) (point)))
9707          (article (gnus-summary-article-number))
9708          (score (or (cdr (assq (or article gnus-current-article)
9709                                gnus-newsgroup-scored))
9710                     gnus-summary-default-score 0))
9711          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9712          (inhibit-read-only t))
9713     ;; Eval the cars of the lists until we find a match.
9714     (let ((default gnus-summary-default-score))
9715       (while (and list
9716                   (not (eval (caar list))))
9717         (setq list (cdr list))))
9718     (let ((face (cdar list)))
9719       (unless (eq face (get-text-property beg 'face))
9720         (gnus-put-text-property-excluding-characters-with-faces
9721          beg end 'face
9722          (setq face (if (boundp face) (symbol-value face) face)))
9723         (when gnus-summary-highlight-line-function
9724           (funcall gnus-summary-highlight-line-function article face))))
9725     (goto-char p)))
9726
9727 (defun gnus-update-read-articles (group unread &optional compute)
9728   "Update the list of read articles in GROUP."
9729   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9730          (entry (gnus-gethash group gnus-newsrc-hashtb))
9731          (info (nth 2 entry))
9732          (prev 1)
9733          (unread (sort (copy-sequence unread) '<))
9734          read)
9735     (if (or (not info) (not active))
9736         ;; There is no info on this group if it was, in fact,
9737         ;; killed.  Gnus stores no information on killed groups, so
9738         ;; there's nothing to be done.
9739         ;; One could store the information somewhere temporarily,
9740         ;; perhaps...  Hmmm...
9741         ()
9742       ;; Remove any negative articles numbers.
9743       (while (and unread (< (car unread) 0))
9744         (setq unread (cdr unread)))
9745       ;; Remove any expired article numbers
9746       (while (and unread (< (car unread) (car active)))
9747         (setq unread (cdr unread)))
9748       ;; Compute the ranges of read articles by looking at the list of
9749       ;; unread articles.
9750       (while unread
9751         (when (/= (car unread) prev)
9752           (push (if (= prev (1- (car unread))) prev
9753                   (cons prev (1- (car unread))))
9754                 read))
9755         (setq prev (1+ (car unread)))
9756         (setq unread (cdr unread)))
9757       (when (<= prev (cdr active))
9758         (push (cons prev (cdr active)) read))
9759       (setq read (if (> (length read) 1) (nreverse read) read))
9760       (if compute
9761           read
9762         (save-excursion
9763           (let (setmarkundo)
9764             ;; Propagate the read marks to the backend.
9765             (when (gnus-check-backend-function 'request-set-mark group)
9766               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9767                     (add (gnus-remove-from-range read (gnus-info-read info))))
9768                 (when (or add del)
9769                   (unless (gnus-check-group group)
9770                     (error "Can't open server for %s" group))
9771                   (gnus-request-set-mark
9772                    group (delq nil (list (if add (list add 'add '(read)))
9773                                          (if del (list del 'del '(read))))))
9774                   (setq setmarkundo
9775                         `(gnus-request-set-mark
9776                           ,group
9777                           ',(delq nil (list
9778                                        (if del (list del 'add '(read)))
9779                                        (if add (list add 'del '(read))))))))))
9780             (set-buffer gnus-group-buffer)
9781             (gnus-undo-register
9782               `(progn
9783                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9784                  (gnus-info-set-read ',info ',(gnus-info-read info))
9785                  (gnus-get-unread-articles-in-group ',info 
9786                                                     (gnus-active ,group))
9787                  (gnus-group-update-group ,group t)
9788                  ,setmarkundo))))
9789         ;; Enter this list into the group info.
9790         (gnus-info-set-read info read)
9791         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9792         (gnus-get-unread-articles-in-group info (gnus-active group))
9793         t))))
9794
9795 (defun gnus-offer-save-summaries ()
9796   "Offer to save all active summary buffers."
9797   (save-excursion
9798     (let ((buflist (buffer-list))
9799           buffers bufname)
9800       ;; Go through all buffers and find all summaries.
9801       (while buflist
9802         (and (setq bufname (buffer-name (car buflist)))
9803              (string-match "Summary" bufname)
9804              (save-excursion
9805                (set-buffer bufname)
9806                ;; We check that this is, indeed, a summary buffer.
9807                (and (eq major-mode 'gnus-summary-mode)
9808                     ;; Also make sure this isn't bogus.
9809                     gnus-newsgroup-prepared
9810                     ;; Also make sure that this isn't a dead summary buffer.
9811                     (not gnus-dead-summary-mode)))
9812              (push bufname buffers))
9813         (setq buflist (cdr buflist)))
9814       ;; Go through all these summary buffers and offer to save them.
9815       (when buffers
9816         (map-y-or-n-p
9817          "Update summary buffer %s? "
9818          (lambda (buf)
9819            (switch-to-buffer buf)
9820            (gnus-summary-exit))
9821          buffers)))))
9822
9823 (defun gnus-summary-setup-default-charset ()
9824   "Setup newsgroup default charset."
9825   (if (equal gnus-newsgroup-name "nndraft:drafts")
9826       (setq gnus-newsgroup-charset nil)
9827     (let* ((name (and gnus-newsgroup-name
9828                       (gnus-group-real-name gnus-newsgroup-name)))
9829            (ignored-charsets
9830             (or gnus-newsgroup-ephemeral-ignored-charsets
9831                 (append
9832                  (and gnus-newsgroup-name
9833                       (or (gnus-group-find-parameter gnus-newsgroup-name
9834                                                      'ignored-charsets t)
9835                           (let ((alist gnus-group-ignored-charsets-alist)
9836                                 elem (charsets nil))
9837                             (while (setq elem (pop alist))
9838                               (when (and name
9839                                          (string-match (car elem) name))
9840                                 (setq alist nil
9841                                       charsets (cdr elem))))
9842                             charsets)))
9843                  gnus-newsgroup-ignored-charsets))))
9844       (setq gnus-newsgroup-charset
9845             (or gnus-newsgroup-ephemeral-charset
9846                 (and gnus-newsgroup-name
9847                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9848                          (let ((alist gnus-group-charset-alist)
9849                                elem charset)
9850                            (while (setq elem (pop alist))
9851                              (when (and name
9852                                         (string-match (car elem) name))
9853                                (setq alist nil
9854                                      charset (cadr elem))))
9855                            charset)))
9856                 gnus-default-charset))
9857       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9858            ignored-charsets))))
9859
9860 ;;;
9861 ;;; Mime Commands
9862 ;;;
9863
9864 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9865   "Display the current article buffer fully MIME-buttonized.
9866 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9867 treated as multipart/mixed."
9868   (interactive "P")
9869   (require 'gnus-art)
9870   (let ((gnus-unbuttonized-mime-types nil)
9871         (gnus-mime-display-multipart-as-mixed show-all-parts))
9872     (gnus-summary-show-article)))
9873
9874 (defun gnus-summary-repair-multipart (article)
9875   "Add a Content-Type header to a multipart article without one."
9876   (interactive (list (gnus-summary-article-number)))
9877   (gnus-with-article article
9878     (message-narrow-to-head)
9879     (message-remove-header "Mime-Version")
9880     (goto-char (point-max))
9881     (insert "Mime-Version: 1.0\n")
9882     (widen)
9883     (when (search-forward "\n--" nil t)
9884       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9885         (message-narrow-to-head)
9886         (message-remove-header "Content-Type")
9887         (goto-char (point-max))
9888         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9889                         separator))
9890         (widen))))
9891   (let (gnus-mark-article-hook)
9892     (gnus-summary-select-article t t nil article)))
9893
9894 (defun gnus-summary-toggle-display-buttonized ()
9895   "Toggle the buttonizing of the article buffer."
9896   (interactive)
9897   (require 'gnus-art)
9898   (if (setq gnus-inhibit-mime-unbuttonizing
9899             (not gnus-inhibit-mime-unbuttonizing))
9900       (let ((gnus-unbuttonized-mime-types nil))
9901         (gnus-summary-show-article))
9902     (gnus-summary-show-article)))
9903
9904 ;;;
9905 ;;; Generic summary marking commands
9906 ;;;
9907
9908 (defvar gnus-summary-marking-alist
9909   '((read gnus-del-mark "d")
9910     (unread gnus-unread-mark "u")
9911     (ticked gnus-ticked-mark "!")
9912     (dormant gnus-dormant-mark "?")
9913     (expirable gnus-expirable-mark "e"))
9914   "An alist of names/marks/keystrokes.")
9915
9916 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9917 (defvar gnus-summary-mark-map)
9918
9919 (defun gnus-summary-make-all-marking-commands ()
9920   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9921   (dolist (elem gnus-summary-marking-alist)
9922     (apply 'gnus-summary-make-marking-command elem)))
9923
9924 (defun gnus-summary-make-marking-command (name mark keystroke)
9925   (let ((map (make-sparse-keymap)))
9926     (define-key gnus-summary-generic-mark-map keystroke map)
9927     (dolist (lway `((next "next" next nil "n")
9928                     (next-unread "next unread" next t "N")
9929                     (prev "previous" prev nil "p")
9930                     (prev-unread "previous unread" prev t "P")
9931                     (nomove "" nil nil ,keystroke)))
9932       (let ((func (gnus-summary-make-marking-command-1
9933                    mark (car lway) lway name)))
9934         (setq func (eval func))
9935         (define-key map (nth 4 lway) func)))))
9936       
9937 (defun gnus-summary-make-marking-command-1 (mark way lway name)
9938   `(defun ,(intern
9939             (format "gnus-summary-put-mark-as-%s%s"
9940                     name (if (eq way 'nomove)
9941                              ""
9942                            (concat "-" (symbol-name way)))))
9943      (n)
9944      ,(format
9945        "Mark the current article as %s%s.
9946 If N, the prefix, then repeat N times.
9947 If N is negative, move in reverse order.
9948 The difference between N and the actual number of articles marked is
9949 returned."
9950        name (cadr lway))
9951      (interactive "p")
9952      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9953     
9954 (defun gnus-summary-generic-mark (n mark move unread)
9955   "Mark N articles with MARK."
9956   (unless (eq major-mode 'gnus-summary-mode)
9957     (error "This command can only be used in the summary buffer"))
9958   (gnus-summary-show-thread)
9959   (let ((nummove
9960          (cond
9961           ((eq move 'next) 1)
9962           ((eq move 'prev) -1)
9963           (t 0))))
9964     (if (zerop nummove)
9965         (setq n 1)
9966       (when (< n 0)
9967         (setq n (abs n)
9968               nummove (* -1 nummove))))
9969     (while (and (> n 0)
9970                 (gnus-summary-mark-article nil mark)
9971                 (zerop (gnus-summary-next-subject nummove unread t)))
9972       (setq n (1- n)))
9973     (when (/= 0 n)
9974       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9975     (gnus-summary-recenter)
9976     (gnus-summary-position-point)
9977     (gnus-set-mode-line 'summary)
9978     n))
9979
9980 (gnus-summary-make-all-marking-commands)
9981
9982 (gnus-ems-redefine)
9983
9984 (provide 'gnus-sum)
9985
9986 (run-hooks 'gnus-sum-load-hook)
9987
9988 ;;; gnus-sum.el ends here