Move image files to etc/gnus.
[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                   (load-path (mm-image-load-path)))
2058               (tool-bar-add-item-from-menu
2059                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2060               (tool-bar-add-item-from-menu
2061                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2062               (tool-bar-add-item-from-menu
2063                'gnus-summary-post-news "post" gnus-summary-mode-map)
2064               (tool-bar-add-item-from-menu
2065                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2066               (tool-bar-add-item-from-menu
2067                'gnus-summary-followup "followup" gnus-summary-mode-map)
2068               (tool-bar-add-item-from-menu
2069                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2070               (tool-bar-add-item-from-menu
2071                'gnus-summary-reply "reply" gnus-summary-mode-map)
2072               (tool-bar-add-item-from-menu
2073                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2074               (tool-bar-add-item-from-menu
2075                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2076               (tool-bar-add-item-from-menu
2077                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2078               (tool-bar-add-item-from-menu
2079                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2080               (tool-bar-add-item-from-menu
2081                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2082               (tool-bar-add-item-from-menu
2083                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2084               (tool-bar-add-item-from-menu
2085                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2086               (tool-bar-add-item-from-menu
2087                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2088               tool-bar-map)))
2089   (if gnus-summary-tool-bar-map
2090       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2091
2092 (defun gnus-score-set-default (var value)
2093   "A version of set that updates the GNU Emacs menu-bar."
2094   (set var value)
2095   ;; It is the message that forces the active status to be updated.
2096   (message ""))
2097
2098 (defun gnus-make-score-map (type)
2099   "Make a summary score map of type TYPE."
2100   (if t
2101       nil
2102     (let ((headers '(("author" "from" string)
2103                      ("subject" "subject" string)
2104                      ("article body" "body" string)
2105                      ("article head" "head" string)
2106                      ("xref" "xref" string)
2107                      ("extra header" "extra" string)
2108                      ("lines" "lines" number)
2109                      ("followups to author" "followup" string)))
2110           (types '((number ("less than" <)
2111                            ("greater than" >)
2112                            ("equal" =))
2113                    (string ("substring" s)
2114                            ("exact string" e)
2115                            ("fuzzy string" f)
2116                            ("regexp" r))))
2117           (perms '(("temporary" (current-time-string))
2118                    ("permanent" nil)
2119                    ("immediate" now)))
2120           header)
2121       (list
2122        (apply
2123         'nconc
2124         (list
2125          (if (eq type 'lower)
2126              "Lower score"
2127            "Increase score"))
2128         (let (outh)
2129           (while headers
2130             (setq header (car headers))
2131             (setq outh
2132                   (cons
2133                    (apply
2134                     'nconc
2135                     (list (car header))
2136                     (let ((ts (cdr (assoc (nth 2 header) types)))
2137                           outt)
2138                       (while ts
2139                         (setq outt
2140                               (cons
2141                                (apply
2142                                 'nconc
2143                                 (list (caar ts))
2144                                 (let ((ps perms)
2145                                       outp)
2146                                   (while ps
2147                                     (setq outp
2148                                           (cons
2149                                            (vector
2150                                             (caar ps)
2151                                             (list
2152                                              'gnus-summary-score-entry
2153                                              (nth 1 header)
2154                                              (if (or (string= (nth 1 header)
2155                                                               "head")
2156                                                      (string= (nth 1 header)
2157                                                               "body"))
2158                                                  ""
2159                                                (list 'gnus-summary-header
2160                                                      (nth 1 header)))
2161                                              (list 'quote (nth 1 (car ts)))
2162                                              (list 'gnus-score-delta-default
2163                                                    nil)
2164                                              (nth 1 (car ps))
2165                                              t)
2166                                             t)
2167                                            outp))
2168                                     (setq ps (cdr ps)))
2169                                   (list (nreverse outp))))
2170                                outt))
2171                         (setq ts (cdr ts)))
2172                       (list (nreverse outt))))
2173                    outh))
2174             (setq headers (cdr headers)))
2175           (list (nreverse outh))))))))
2176
2177 \f
2178
2179 (defun gnus-summary-mode (&optional group)
2180   "Major mode for reading articles.
2181
2182 All normal editing commands are switched off.
2183 \\<gnus-summary-mode-map>
2184 Each line in this buffer represents one article.  To read an
2185 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2186 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2187 respectively.
2188
2189 You can also post articles and send mail from this buffer.  To
2190 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2191 of an article, type `\\[gnus-summary-reply]'.
2192
2193 There are approx. one gazillion commands you can execute in this
2194 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2195
2196 The following commands are available:
2197
2198 \\{gnus-summary-mode-map}"
2199   (interactive)
2200   (kill-all-local-variables)
2201   (when (gnus-visual-p 'summary-menu 'menu)
2202     (gnus-summary-make-menu-bar)
2203     (gnus-summary-make-tool-bar))
2204   (gnus-summary-make-local-variables)
2205   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2206     (gnus-summary-make-local-variables))
2207   (gnus-make-thread-indent-array)
2208   (gnus-simplify-mode-line)
2209   (setq major-mode 'gnus-summary-mode)
2210   (setq mode-name "Summary")
2211   (make-local-variable 'minor-mode-alist)
2212   (use-local-map gnus-summary-mode-map)
2213   (buffer-disable-undo)
2214   (setq buffer-read-only t)             ;Disable modification
2215   (setq truncate-lines t)
2216   (setq selective-display t)
2217   (setq selective-display-ellipses t)   ;Display `...'
2218   (gnus-summary-set-display-table)
2219   (gnus-set-default-directory)
2220   (setq gnus-newsgroup-name group)
2221   (make-local-variable 'gnus-summary-line-format)
2222   (make-local-variable 'gnus-summary-line-format-spec)
2223   (make-local-variable 'gnus-summary-dummy-line-format)
2224   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2225   (make-local-variable 'gnus-summary-mark-positions)
2226   (make-local-hook 'pre-command-hook)
2227   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2228   (gnus-run-hooks 'gnus-summary-mode-hook)
2229   (turn-on-gnus-mailing-list-mode)
2230   (mm-enable-multibyte-mule4)
2231   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2232   (gnus-update-summary-mark-positions))
2233
2234 (defun gnus-summary-make-local-variables ()
2235   "Make all the local summary buffer variables."
2236   (let (global)
2237     (dolist (local gnus-summary-local-variables)
2238       (if (consp local)
2239           (progn
2240             (if (eq (cdr local) 'global)
2241                 ;; Copy the global value of the variable.
2242                 (setq global (symbol-value (car local)))
2243               ;; Use the value from the list.
2244               (setq global (eval (cdr local))))
2245             (set (make-local-variable (car local)) global))
2246         ;; Simple nil-valued local variable.
2247         (set (make-local-variable local) nil)))))
2248
2249 (defun gnus-summary-clear-local-variables ()
2250   (let ((locals gnus-summary-local-variables))
2251     (while locals
2252       (if (consp (car locals))
2253           (and (vectorp (caar locals))
2254                (set (caar locals) nil))
2255         (and (vectorp (car locals))
2256              (set (car locals) nil)))
2257       (setq locals (cdr locals)))))
2258
2259 ;; Summary data functions.
2260
2261 (defmacro gnus-data-number (data)
2262   `(car ,data))
2263
2264 (defmacro gnus-data-set-number (data number)
2265   `(setcar ,data ,number))
2266
2267 (defmacro gnus-data-mark (data)
2268   `(nth 1 ,data))
2269
2270 (defmacro gnus-data-set-mark (data mark)
2271   `(setcar (nthcdr 1 ,data) ,mark))
2272
2273 (defmacro gnus-data-pos (data)
2274   `(nth 2 ,data))
2275
2276 (defmacro gnus-data-set-pos (data pos)
2277   `(setcar (nthcdr 2 ,data) ,pos))
2278
2279 (defmacro gnus-data-header (data)
2280   `(nth 3 ,data))
2281
2282 (defmacro gnus-data-set-header (data header)
2283   `(setf (nth 3 ,data) ,header))
2284
2285 (defmacro gnus-data-level (data)
2286   `(nth 4 ,data))
2287
2288 (defmacro gnus-data-unread-p (data)
2289   `(= (nth 1 ,data) gnus-unread-mark))
2290
2291 (defmacro gnus-data-read-p (data)
2292   `(/= (nth 1 ,data) gnus-unread-mark))
2293
2294 (defmacro gnus-data-pseudo-p (data)
2295   `(consp (nth 3 ,data)))
2296
2297 (defmacro gnus-data-find (number)
2298   `(assq ,number gnus-newsgroup-data))
2299
2300 (defmacro gnus-data-find-list (number &optional data)
2301   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2302      (memq (assq ,number bdata)
2303            bdata)))
2304
2305 (defmacro gnus-data-make (number mark pos header level)
2306   `(list ,number ,mark ,pos ,header ,level))
2307
2308 (defun gnus-data-enter (after-article number mark pos header level offset)
2309   (let ((data (gnus-data-find-list after-article)))
2310     (unless data
2311       (error "No such article: %d" after-article))
2312     (setcdr data (cons (gnus-data-make number mark pos header level)
2313                        (cdr data)))
2314     (setq gnus-newsgroup-data-reverse nil)
2315     (gnus-data-update-list (cddr data) offset)))
2316
2317 (defun gnus-data-enter-list (after-article list &optional offset)
2318   (when list
2319     (let ((data (and after-article (gnus-data-find-list after-article)))
2320           (ilist list))
2321       (if (not (or data
2322                    after-article))
2323           (let ((odata gnus-newsgroup-data))
2324             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2325             (when offset
2326               (gnus-data-update-list odata offset)))
2327         ;; Find the last element in the list to be spliced into the main
2328         ;; list.
2329         (while (cdr list)
2330           (setq list (cdr list)))
2331         (if (not data)
2332             (progn
2333               (setcdr list gnus-newsgroup-data)
2334               (setq gnus-newsgroup-data ilist)
2335               (when offset
2336                 (gnus-data-update-list (cdr list) offset)))
2337           (setcdr list (cdr data))
2338           (setcdr data ilist)
2339           (when offset
2340             (gnus-data-update-list (cdr list) offset))))
2341       (setq gnus-newsgroup-data-reverse nil))))
2342
2343 (defun gnus-data-remove (article &optional offset)
2344   (let ((data gnus-newsgroup-data))
2345     (if (= (gnus-data-number (car data)) article)
2346         (progn
2347           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2348                 gnus-newsgroup-data-reverse nil)
2349           (when offset
2350             (gnus-data-update-list gnus-newsgroup-data offset)))
2351       (while (cdr data)
2352         (when (= (gnus-data-number (cadr data)) article)
2353           (setcdr data (cddr data))
2354           (when offset
2355             (gnus-data-update-list (cdr data) offset))
2356           (setq data nil
2357                 gnus-newsgroup-data-reverse nil))
2358         (setq data (cdr data))))))
2359
2360 (defmacro gnus-data-list (backward)
2361   `(if ,backward
2362        (or gnus-newsgroup-data-reverse
2363            (setq gnus-newsgroup-data-reverse
2364                  (reverse gnus-newsgroup-data)))
2365      gnus-newsgroup-data))
2366
2367 (defun gnus-data-update-list (data offset)
2368   "Add OFFSET to the POS of all data entries in DATA."
2369   (setq gnus-newsgroup-data-reverse nil)
2370   (while data
2371     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2372     (setq data (cdr data))))
2373
2374 (defun gnus-summary-article-pseudo-p (article)
2375   "Say whether this article is a pseudo article or not."
2376   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2377
2378 (defmacro gnus-summary-article-sparse-p (article)
2379   "Say whether this article is a sparse article or not."
2380   `(memq ,article gnus-newsgroup-sparse))
2381
2382 (defmacro gnus-summary-article-ancient-p (article)
2383   "Say whether this article is a sparse article or not."
2384   `(memq ,article gnus-newsgroup-ancient))
2385
2386 (defun gnus-article-parent-p (number)
2387   "Say whether this article is a parent or not."
2388   (let ((data (gnus-data-find-list number)))
2389     (and (cdr data)                     ; There has to be an article after...
2390          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2391             (gnus-data-level (nth 1 data))))))
2392
2393 (defun gnus-article-children (number)
2394   "Return a list of all children to NUMBER."
2395   (let* ((data (gnus-data-find-list number))
2396          (level (gnus-data-level (car data)))
2397          children)
2398     (setq data (cdr data))
2399     (while (and data
2400                 (= (gnus-data-level (car data)) (1+ level)))
2401       (push (gnus-data-number (car data)) children)
2402       (setq data (cdr data)))
2403     children))
2404
2405 (defmacro gnus-summary-skip-intangible ()
2406   "If the current article is intangible, then jump to a different article."
2407   '(let ((to (get-text-property (point) 'gnus-intangible)))
2408      (and to (gnus-summary-goto-subject to))))
2409
2410 (defmacro gnus-summary-article-intangible-p ()
2411   "Say whether this article is intangible or not."
2412   '(get-text-property (point) 'gnus-intangible))
2413
2414 (defun gnus-article-read-p (article)
2415   "Say whether ARTICLE is read or not."
2416   (not (or (memq article gnus-newsgroup-marked)
2417            (memq article gnus-newsgroup-unreads)
2418            (memq article gnus-newsgroup-unselected)
2419            (memq article gnus-newsgroup-dormant))))
2420
2421 ;; Some summary mode macros.
2422
2423 (defmacro gnus-summary-article-number ()
2424   "The article number of the article on the current line.
2425 If there isn's an article number here, then we return the current
2426 article number."
2427   '(progn
2428      (gnus-summary-skip-intangible)
2429      (or (get-text-property (point) 'gnus-number)
2430          (gnus-summary-last-subject))))
2431
2432 (defmacro gnus-summary-article-header (&optional number)
2433   "Return the header of article NUMBER."
2434   `(gnus-data-header (gnus-data-find
2435                       ,(or number '(gnus-summary-article-number)))))
2436
2437 (defmacro gnus-summary-thread-level (&optional number)
2438   "Return the level of thread that starts with article NUMBER."
2439   `(if (and (eq gnus-summary-make-false-root 'dummy)
2440             (get-text-property (point) 'gnus-intangible))
2441        0
2442      (gnus-data-level (gnus-data-find
2443                        ,(or number '(gnus-summary-article-number))))))
2444
2445 (defmacro gnus-summary-article-mark (&optional number)
2446   "Return the mark of article NUMBER."
2447   `(gnus-data-mark (gnus-data-find
2448                     ,(or number '(gnus-summary-article-number)))))
2449
2450 (defmacro gnus-summary-article-pos (&optional number)
2451   "Return the position of the line of article NUMBER."
2452   `(gnus-data-pos (gnus-data-find
2453                    ,(or number '(gnus-summary-article-number)))))
2454
2455 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2456 (defmacro gnus-summary-article-subject (&optional number)
2457   "Return current subject string or nil if nothing."
2458   `(let ((headers
2459           ,(if number
2460                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2461              '(gnus-data-header (assq (gnus-summary-article-number)
2462                                       gnus-newsgroup-data)))))
2463      (and headers
2464           (vectorp headers)
2465           (mail-header-subject headers))))
2466
2467 (defmacro gnus-summary-article-score (&optional number)
2468   "Return current article score."
2469   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2470                   gnus-newsgroup-scored))
2471        gnus-summary-default-score 0))
2472
2473 (defun gnus-summary-article-children (&optional number)
2474   "Return a list of article numbers that are children of article NUMBER."
2475   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2476          (level (gnus-data-level (car data)))
2477          l children)
2478     (while (and (setq data (cdr data))
2479                 (> (setq l (gnus-data-level (car data))) level))
2480       (and (= (1+ level) l)
2481            (push (gnus-data-number (car data))
2482                  children)))
2483     (nreverse children)))
2484
2485 (defun gnus-summary-article-parent (&optional number)
2486   "Return the article number of the parent of article NUMBER."
2487   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2488                                     (gnus-data-list t)))
2489          (level (gnus-data-level (car data))))
2490     (if (zerop level)
2491         ()                              ; This is a root.
2492       ;; We search until we find an article with a level less than
2493       ;; this one.  That function has to be the parent.
2494       (while (and (setq data (cdr data))
2495                   (not (< (gnus-data-level (car data)) level))))
2496       (and data (gnus-data-number (car data))))))
2497
2498 (defun gnus-unread-mark-p (mark)
2499   "Say whether MARK is the unread mark."
2500   (= mark gnus-unread-mark))
2501
2502 (defun gnus-read-mark-p (mark)
2503   "Say whether MARK is one of the marks that mark as read.
2504 This is all marks except unread, ticked, dormant, and expirable."
2505   (not (or (= mark gnus-unread-mark)
2506            (= mark gnus-ticked-mark)
2507            (= mark gnus-dormant-mark)
2508            (= mark gnus-expirable-mark))))
2509
2510 (defmacro gnus-article-mark (number)
2511   "Return the MARK of article NUMBER.
2512 This macro should only be used when computing the mark the \"first\"
2513 time; i.e., when generating the summary lines.  After that,
2514 `gnus-summary-article-mark' should be used to examine the
2515 marks of articles."
2516   `(cond
2517     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2518     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2519     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2520     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2521     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2522     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2523     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2524     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2525            gnus-ancient-mark))))
2526
2527 ;; Saving hidden threads.
2528
2529 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2530 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2531
2532 (defmacro gnus-save-hidden-threads (&rest forms)
2533   "Save hidden threads, eval FORMS, and restore the hidden threads."
2534   (let ((config (make-symbol "config")))
2535     `(let ((,config (gnus-hidden-threads-configuration)))
2536        (unwind-protect
2537            (save-excursion
2538              ,@forms)
2539          (gnus-restore-hidden-threads-configuration ,config)))))
2540
2541 (defun gnus-data-compute-positions ()
2542   "Compute the positions of all articles."
2543   (setq gnus-newsgroup-data-reverse nil)
2544   (let ((data gnus-newsgroup-data))
2545     (save-excursion
2546       (gnus-save-hidden-threads
2547         (gnus-summary-show-all-threads)
2548         (goto-char (point-min))
2549         (while data
2550           (while (get-text-property (point) 'gnus-intangible)
2551             (forward-line 1))
2552           (gnus-data-set-pos (car data) (+ (point) 3))
2553           (setq data (cdr data))
2554           (forward-line 1))))))
2555
2556 (defun gnus-hidden-threads-configuration ()
2557   "Return the current hidden threads configuration."
2558   (save-excursion
2559     (let (config)
2560       (goto-char (point-min))
2561       (while (search-forward "\r" nil t)
2562         (push (1- (point)) config))
2563       config)))
2564
2565 (defun gnus-restore-hidden-threads-configuration (config)
2566   "Restore hidden threads configuration from CONFIG."
2567   (save-excursion
2568     (let (point buffer-read-only)
2569       (while (setq point (pop config))
2570         (when (and (< point (point-max))
2571                    (goto-char point)
2572                    (eq (char-after) ?\n))
2573           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2574
2575 ;; Various summary mode internalish functions.
2576
2577 (defun gnus-mouse-pick-article (e)
2578   (interactive "e")
2579   (mouse-set-point e)
2580   (gnus-summary-next-page nil t))
2581
2582 (defun gnus-summary-set-display-table ()
2583   "Change the display table.
2584 Odd characters have a tendency to mess
2585 up nicely formatted displays - we make all possible glyphs
2586 display only a single character."
2587
2588   ;; We start from the standard display table, if any.
2589   (let ((table (or (copy-sequence standard-display-table)
2590                    (make-display-table)))
2591         (i 32))
2592     ;; Nix out all the control chars...
2593     (while (>= (setq i (1- i)) 0)
2594       (aset table i [??]))
2595     ;; ... but not newline and cr, of course.  (cr is necessary for the
2596     ;; selective display).
2597     (aset table ?\n nil)
2598     (aset table ?\r nil)
2599     ;; We keep TAB as well.
2600     (aset table ?\t nil)
2601     ;; We nix out any glyphs over 126 that are not set already.
2602     (let ((i 256))
2603       (while (>= (setq i (1- i)) 127)
2604         ;; Only modify if the entry is nil.
2605         (unless (aref table i)
2606           (aset table i [??]))))
2607     (setq buffer-display-table table)))
2608
2609 (defun gnus-summary-setup-buffer (group)
2610   "Initialize summary buffer."
2611   (let ((buffer (concat "*Summary " group "*")))
2612     (if (get-buffer buffer)
2613         (progn
2614           (set-buffer buffer)
2615           (setq gnus-summary-buffer (current-buffer))
2616           (not gnus-newsgroup-prepared))
2617       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2618       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2619       (gnus-summary-mode group)
2620       (when gnus-carpal
2621         (gnus-carpal-setup-buffer 'summary))
2622       (unless gnus-single-article-buffer
2623         (make-local-variable 'gnus-article-buffer)
2624         (make-local-variable 'gnus-article-current)
2625         (make-local-variable 'gnus-original-article-buffer))
2626       (setq gnus-newsgroup-name group)
2627       t)))
2628
2629 (defun gnus-set-global-variables ()
2630   "Set the global equivalents of the buffer-local variables.
2631 They are set to the latest values they had.  These reflect the summary
2632 buffer that was in action when the last article was fetched."
2633   (when (eq major-mode 'gnus-summary-mode)
2634     (setq gnus-summary-buffer (current-buffer))
2635     (let ((name gnus-newsgroup-name)
2636           (marked gnus-newsgroup-marked)
2637           (unread gnus-newsgroup-unreads)
2638           (headers gnus-current-headers)
2639           (data gnus-newsgroup-data)
2640           (summary gnus-summary-buffer)
2641           (article-buffer gnus-article-buffer)
2642           (original gnus-original-article-buffer)
2643           (gac gnus-article-current)
2644           (reffed gnus-reffed-article-number)
2645           (score-file gnus-current-score-file)
2646           (default-charset gnus-newsgroup-charset)
2647           vlist)
2648       (let ((locals gnus-newsgroup-variables))
2649         (while locals
2650           (if (consp (car locals))
2651               (push (eval (caar locals)) vlist)
2652             (push (eval (car locals)) vlist))
2653           (setq locals (cdr locals)))
2654         (setq vlist (nreverse vlist)))
2655       (save-excursion
2656         (set-buffer gnus-group-buffer)
2657         (setq gnus-newsgroup-name name
2658               gnus-newsgroup-marked marked
2659               gnus-newsgroup-unreads unread
2660               gnus-current-headers headers
2661               gnus-newsgroup-data data
2662               gnus-article-current gac
2663               gnus-summary-buffer summary
2664               gnus-article-buffer article-buffer
2665               gnus-original-article-buffer original
2666               gnus-reffed-article-number reffed
2667               gnus-current-score-file score-file
2668               gnus-newsgroup-charset default-charset)
2669         (let ((locals gnus-newsgroup-variables))
2670           (while locals
2671             (if (consp (car locals))
2672                 (set (caar locals) (pop vlist))
2673               (set (car locals) (pop vlist)))
2674             (setq locals (cdr locals))))
2675         ;; The article buffer also has local variables.
2676         (when (gnus-buffer-live-p gnus-article-buffer)
2677           (set-buffer gnus-article-buffer)
2678           (setq gnus-summary-buffer summary))))))
2679
2680 (defun gnus-summary-article-unread-p (article)
2681   "Say whether ARTICLE is unread or not."
2682   (memq article gnus-newsgroup-unreads))
2683
2684 (defun gnus-summary-first-article-p (&optional article)
2685   "Return whether ARTICLE is the first article in the buffer."
2686   (if (not (setq article (or article (gnus-summary-article-number))))
2687       nil
2688     (eq article (caar gnus-newsgroup-data))))
2689
2690 (defun gnus-summary-last-article-p (&optional article)
2691   "Return whether ARTICLE is the last article in the buffer."
2692   (if (not (setq article (or article (gnus-summary-article-number))))
2693       ;; All non-existent numbers are the last article.  :-)
2694       t
2695     (not (cdr (gnus-data-find-list article)))))
2696
2697 (defun gnus-make-thread-indent-array ()
2698   (let ((n 200))
2699     (unless (and gnus-thread-indent-array
2700                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2701       (setq gnus-thread-indent-array (make-vector 201 "")
2702             gnus-thread-indent-array-level gnus-thread-indent-level)
2703       (while (>= n 0)
2704         (aset gnus-thread-indent-array n
2705               (make-string (* n gnus-thread-indent-level) ? ))
2706         (setq n (1- n))))))
2707
2708 (defun gnus-update-summary-mark-positions ()
2709   "Compute where the summary marks are to go."
2710   (save-excursion
2711     (when (gnus-buffer-exists-p gnus-summary-buffer)
2712       (set-buffer gnus-summary-buffer))
2713     (let ((gnus-replied-mark 129)
2714           (gnus-score-below-mark 130)
2715           (gnus-score-over-mark 130)
2716           (gnus-download-mark 131)
2717           (spec gnus-summary-line-format-spec)
2718           gnus-visual pos)
2719       (save-excursion
2720         (gnus-set-work-buffer)
2721         (let ((gnus-summary-line-format-spec spec)
2722               (gnus-newsgroup-downloadable '((0 . t))))
2723           (gnus-summary-insert-line
2724            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2725           (goto-char (point-min))
2726           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2727                                              (- (point) 2)))))
2728           (goto-char (point-min))
2729           (push (cons 'replied (and (search-forward "\201" nil t)
2730                                     (- (point) 2)))
2731                 pos)
2732           (goto-char (point-min))
2733           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2734                 pos)
2735           (goto-char (point-min))
2736           (push (cons 'download
2737                       (and (search-forward "\203" nil t) (- (point) 2)))
2738                 pos)))
2739       (setq gnus-summary-mark-positions pos))))
2740
2741 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2742   "Insert a dummy root in the summary buffer."
2743   (beginning-of-line)
2744   (gnus-add-text-properties
2745    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2746    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2747
2748 (defun gnus-summary-from-or-to-or-newsgroups (header)
2749   (let ((to (cdr (assq 'To (mail-header-extra header))))
2750         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2751         (mail-parse-charset gnus-newsgroup-charset)
2752         (mail-parse-ignored-charsets
2753          (save-excursion (set-buffer gnus-summary-buffer)
2754                          gnus-newsgroup-ignored-charsets)))
2755     (cond
2756      ((and to
2757            gnus-ignored-from-addresses
2758            (string-match gnus-ignored-from-addresses
2759                          (mail-header-from header)))
2760       (concat "-> "
2761               (or (car (funcall gnus-extract-address-components
2762                                 (funcall
2763                                  gnus-decode-encoded-word-function to)))
2764                   (funcall gnus-decode-encoded-word-function to))))
2765      ((and newsgroups
2766            gnus-ignored-from-addresses
2767            (string-match gnus-ignored-from-addresses
2768                          (mail-header-from header)))
2769       (concat "=> " newsgroups))
2770      (t
2771       (or (car (funcall gnus-extract-address-components
2772                         (mail-header-from header)))
2773           (mail-header-from header))))))
2774
2775 (defun gnus-summary-insert-line (gnus-tmp-header
2776                                  gnus-tmp-level gnus-tmp-current
2777                                  gnus-tmp-unread gnus-tmp-replied
2778                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2779                                  &optional gnus-tmp-dummy gnus-tmp-score
2780                                  gnus-tmp-process)
2781   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2782          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2783          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2784          (gnus-tmp-score-char
2785           (if (or (null gnus-summary-default-score)
2786                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2787                       gnus-summary-zcore-fuzz))
2788               ?  ;Whitespace
2789             (if (< gnus-tmp-score gnus-summary-default-score)
2790                 gnus-score-below-mark gnus-score-over-mark)))
2791          (gnus-tmp-replied
2792           (cond (gnus-tmp-process gnus-process-mark)
2793                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2794                  gnus-cached-mark)
2795                 (gnus-tmp-replied gnus-replied-mark)
2796                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2797                  gnus-saved-mark)
2798                 (t gnus-unread-mark)))
2799          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2800          (gnus-tmp-name
2801           (cond
2802            ((string-match "<[^>]+> *$" gnus-tmp-from)
2803             (let ((beg (match-beginning 0)))
2804               (or (and (string-match "^\".+\"" gnus-tmp-from)
2805                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2806                   (substring gnus-tmp-from 0 beg))))
2807            ((string-match "(.+)" gnus-tmp-from)
2808             (substring gnus-tmp-from
2809                        (1+ (match-beginning 0)) (1- (match-end 0))))
2810            (t gnus-tmp-from)))
2811          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2812          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2813          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2814          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2815          (buffer-read-only nil))
2816     (when (string= gnus-tmp-name "")
2817       (setq gnus-tmp-name gnus-tmp-from))
2818     (unless (numberp gnus-tmp-lines)
2819       (setq gnus-tmp-lines 0))
2820     (gnus-put-text-property
2821      (point)
2822      (progn (eval gnus-summary-line-format-spec) (point))
2823      'gnus-number gnus-tmp-number)
2824     (when (gnus-visual-p 'summary-highlight 'highlight)
2825       (forward-line -1)
2826       (gnus-run-hooks 'gnus-summary-update-hook)
2827       (forward-line 1))))
2828
2829 (defun gnus-summary-update-line (&optional dont-update)
2830   "Update summary line after change."
2831   (when (and gnus-summary-default-score
2832              (not gnus-summary-inhibit-highlight))
2833     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2834            (article (gnus-summary-article-number))
2835            (score (gnus-summary-article-score article)))
2836       (unless dont-update
2837         (if (and gnus-summary-mark-below
2838                  (< (gnus-summary-article-score)
2839                     gnus-summary-mark-below))
2840             ;; This article has a low score, so we mark it as read.
2841             (when (memq article gnus-newsgroup-unreads)
2842               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2843           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2844             ;; This article was previously marked as read on account
2845             ;; of a low score, but now it has risen, so we mark it as
2846             ;; unread.
2847             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2848         (gnus-summary-update-mark
2849          (if (or (null gnus-summary-default-score)
2850                  (<= (abs (- score gnus-summary-default-score))
2851                      gnus-summary-zcore-fuzz))
2852              ?  ;Whitespace
2853            (if (< score gnus-summary-default-score)
2854                gnus-score-below-mark gnus-score-over-mark))
2855          'score))
2856       ;; Do visual highlighting.
2857       (when (gnus-visual-p 'summary-highlight 'highlight)
2858         (gnus-run-hooks 'gnus-summary-update-hook)))))
2859
2860 (defvar gnus-tmp-new-adopts nil)
2861
2862 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2863   "Return the number of articles in THREAD.
2864 This may be 0 in some cases -- if none of the articles in
2865 the thread are to be displayed."
2866   (let* ((number
2867           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2868           (cond
2869            ((not (listp thread))
2870             1)
2871            ((and (consp thread) (cdr thread))
2872             (apply
2873              '+ 1 (mapcar
2874                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2875            ((null thread)
2876             1)
2877            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2878             1)
2879            (t 0))))
2880     (when (and level (zerop level) gnus-tmp-new-adopts)
2881       (incf number
2882             (apply '+ (mapcar
2883                        'gnus-summary-number-of-articles-in-thread
2884                        gnus-tmp-new-adopts))))
2885     (if char
2886         (if (> number 1) gnus-not-empty-thread-mark
2887           gnus-empty-thread-mark)
2888       number)))
2889
2890 (defun gnus-summary-set-local-parameters (group)
2891   "Go through the local params of GROUP and set all variable specs in that list."
2892   (let ((params (gnus-group-find-parameter group))
2893         elem)
2894     (while params
2895       (setq elem (car params)
2896             params (cdr params))
2897       (and (consp elem)                 ; Has to be a cons.
2898            (consp (cdr elem))           ; The cdr has to be a list.
2899            (symbolp (car elem))         ; Has to be a symbol in there.
2900            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2901            (ignore-errors               ; So we set it.
2902              (make-local-variable (car elem))
2903              (set (car elem) (eval (nth 1 elem))))))))
2904
2905 (defun gnus-summary-read-group (group &optional show-all no-article
2906                                       kill-buffer no-display backward
2907                                       select-articles)
2908   "Start reading news in newsgroup GROUP.
2909 If SHOW-ALL is non-nil, already read articles are also listed.
2910 If NO-ARTICLE is non-nil, no article is selected initially.
2911 If NO-DISPLAY, don't generate a summary buffer."
2912   (let (result)
2913     (while (and group
2914                 (null (setq result
2915                             (let ((gnus-auto-select-next nil))
2916                               (or (gnus-summary-read-group-1
2917                                    group show-all no-article
2918                                    kill-buffer no-display
2919                                    select-articles)
2920                                   (setq show-all nil
2921                                         select-articles nil)))))
2922                 (eq gnus-auto-select-next 'quietly))
2923       (set-buffer gnus-group-buffer)
2924       ;; The entry function called above goes to the next
2925       ;; group automatically, so we go two groups back
2926       ;; if we are searching for the previous group.
2927       (when backward
2928         (gnus-group-prev-unread-group 2))
2929       (if (not (equal group (gnus-group-group-name)))
2930           (setq group (gnus-group-group-name))
2931         (setq group nil)))
2932     result))
2933
2934 (defun gnus-summary-read-group-1 (group show-all no-article
2935                                         kill-buffer no-display
2936                                         &optional select-articles)
2937   ;; Killed foreign groups can't be entered.
2938   (when (and (not (gnus-group-native-p group))
2939              (not (gnus-gethash group gnus-newsrc-hashtb)))
2940     (error "Dead non-native groups can't be entered"))
2941   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2942   (let* ((new-group (gnus-summary-setup-buffer group))
2943          (quit-config (gnus-group-quit-config group))
2944          (did-select (and new-group (gnus-select-newsgroup
2945                                      group show-all select-articles))))
2946     (cond
2947      ;; This summary buffer exists already, so we just select it.
2948      ((not new-group)
2949       (gnus-set-global-variables)
2950       (when kill-buffer
2951         (gnus-kill-or-deaden-summary kill-buffer))
2952       (gnus-configure-windows 'summary 'force)
2953       (gnus-set-mode-line 'summary)
2954       (gnus-summary-position-point)
2955       (message "")
2956       t)
2957      ;; We couldn't select this group.
2958      ((null did-select)
2959       (when (and (eq major-mode 'gnus-summary-mode)
2960                  (not (equal (current-buffer) kill-buffer)))
2961         (kill-buffer (current-buffer))
2962         (if (not quit-config)
2963             (progn
2964               ;; Update the info -- marks might need to be removed,
2965               ;; for instance.
2966               (gnus-summary-update-info)
2967               (set-buffer gnus-group-buffer)
2968               (gnus-group-jump-to-group group)
2969               (gnus-group-next-unread-group 1))
2970           (gnus-handle-ephemeral-exit quit-config)))
2971       (gnus-message 3 "Can't select group")
2972       nil)
2973      ;; The user did a `C-g' while prompting for number of articles,
2974      ;; so we exit this group.
2975      ((eq did-select 'quit)
2976       (and (eq major-mode 'gnus-summary-mode)
2977            (not (equal (current-buffer) kill-buffer))
2978            (kill-buffer (current-buffer)))
2979       (when kill-buffer
2980         (gnus-kill-or-deaden-summary kill-buffer))
2981       (if (not quit-config)
2982           (progn
2983             (set-buffer gnus-group-buffer)
2984             (gnus-group-jump-to-group group)
2985             (gnus-group-next-unread-group 1)
2986             (gnus-configure-windows 'group 'force))
2987         (gnus-handle-ephemeral-exit quit-config))
2988       ;; Finally signal the quit.
2989       (signal 'quit nil))
2990      ;; The group was successfully selected.
2991      (t
2992       (gnus-set-global-variables)
2993       ;; Save the active value in effect when the group was entered.
2994       (setq gnus-newsgroup-active
2995             (gnus-copy-sequence
2996              (gnus-active gnus-newsgroup-name)))
2997       ;; You can change the summary buffer in some way with this hook.
2998       (gnus-run-hooks 'gnus-select-group-hook)
2999       ;; Set any local variables in the group parameters.
3000       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3001       (gnus-update-format-specifications
3002        nil 'summary 'summary-mode 'summary-dummy)
3003       (gnus-update-summary-mark-positions)
3004       ;; Do score processing.
3005       (when gnus-use-scoring
3006         (gnus-possibly-score-headers))
3007       ;; Check whether to fill in the gaps in the threads.
3008       (when gnus-build-sparse-threads
3009         (gnus-build-sparse-threads))
3010       ;; Find the initial limit.
3011       (if gnus-show-threads
3012           (if show-all
3013               (let ((gnus-newsgroup-dormant nil))
3014                 (gnus-summary-initial-limit show-all))
3015             (gnus-summary-initial-limit show-all))
3016         ;; When untreaded, all articles are always shown.
3017         (setq gnus-newsgroup-limit
3018               (mapcar
3019                (lambda (header) (mail-header-number header))
3020                gnus-newsgroup-headers)))
3021       ;; Generate the summary buffer.
3022       (unless no-display
3023         (gnus-summary-prepare))
3024       (when gnus-use-trees
3025         (gnus-tree-open group)
3026         (setq gnus-summary-highlight-line-function
3027               'gnus-tree-highlight-article))
3028       ;; If the summary buffer is empty, but there are some low-scored
3029       ;; articles or some excluded dormants, we include these in the
3030       ;; buffer.
3031       (when (and (zerop (buffer-size))
3032                  (not no-display))
3033         (cond (gnus-newsgroup-dormant
3034                (gnus-summary-limit-include-dormant))
3035               ((and gnus-newsgroup-scored show-all)
3036                (gnus-summary-limit-include-expunged t))))
3037       ;; Function `gnus-apply-kill-file' must be called in this hook.
3038       (gnus-run-hooks 'gnus-apply-kill-hook)
3039       (if (and (zerop (buffer-size))
3040                (not no-display))
3041           (progn
3042             ;; This newsgroup is empty.
3043             (gnus-summary-catchup-and-exit nil t)
3044             (gnus-message 6 "No unread news")
3045             (when kill-buffer
3046               (gnus-kill-or-deaden-summary kill-buffer))
3047             ;; Return nil from this function.
3048             nil)
3049         ;; Hide conversation thread subtrees.  We cannot do this in
3050         ;; gnus-summary-prepare-hook since kill processing may not
3051         ;; work with hidden articles.
3052         (and gnus-show-threads
3053              gnus-thread-hide-subtree
3054              (gnus-summary-hide-all-threads))
3055         (when kill-buffer
3056           (gnus-kill-or-deaden-summary kill-buffer))
3057         ;; Show first unread article if requested.
3058         (if (and (not no-article)
3059                  (not no-display)
3060                  gnus-newsgroup-unreads
3061                  gnus-auto-select-first)
3062             (progn
3063               (gnus-configure-windows 'summary)
3064               (cond
3065                ((eq gnus-auto-select-first 'best)
3066                 (gnus-summary-best-unread-article))
3067                ((eq gnus-auto-select-first t)
3068                 (gnus-summary-first-unread-article))
3069                ((gnus-functionp gnus-auto-select-first)
3070                 (funcall gnus-auto-select-first))))
3071           ;; Don't select any articles, just move point to the first
3072           ;; article in the group.
3073           (goto-char (point-min))
3074           (gnus-summary-position-point)
3075           (gnus-configure-windows 'summary 'force)
3076           (gnus-set-mode-line 'summary))
3077         (when (get-buffer-window gnus-group-buffer t)
3078           ;; Gotta use windows, because recenter does weird stuff if
3079           ;; the current buffer ain't the displayed window.
3080           (let ((owin (selected-window)))
3081             (select-window (get-buffer-window gnus-group-buffer t))
3082             (when (gnus-group-goto-group group)
3083               (recenter))
3084             (select-window owin)))
3085         ;; Mark this buffer as "prepared".
3086         (setq gnus-newsgroup-prepared t)
3087         (gnus-run-hooks 'gnus-summary-prepared-hook)
3088         t)))))
3089
3090 (defun gnus-summary-prepare ()
3091   "Generate the summary buffer."
3092   (interactive)
3093   (let ((buffer-read-only nil))
3094     (erase-buffer)
3095     (setq gnus-newsgroup-data nil
3096           gnus-newsgroup-data-reverse nil)
3097     (gnus-run-hooks 'gnus-summary-generate-hook)
3098     ;; Generate the buffer, either with threads or without.
3099     (when gnus-newsgroup-headers
3100       (gnus-summary-prepare-threads
3101        (if gnus-show-threads
3102            (gnus-sort-gathered-threads
3103             (funcall gnus-summary-thread-gathering-function
3104                      (gnus-sort-threads
3105                       (gnus-cut-threads (gnus-make-threads)))))
3106          ;; Unthreaded display.
3107          (gnus-sort-articles gnus-newsgroup-headers))))
3108     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3109     ;; Call hooks for modifying summary buffer.
3110     (goto-char (point-min))
3111     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3112
3113 (defsubst gnus-general-simplify-subject (subject)
3114   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3115   (setq subject
3116         (cond
3117          ;; Truncate the subject.
3118          (gnus-simplify-subject-functions
3119           (gnus-map-function gnus-simplify-subject-functions subject))
3120          ((numberp gnus-summary-gather-subject-limit)
3121           (setq subject (gnus-simplify-subject-re subject))
3122           (if (> (length subject) gnus-summary-gather-subject-limit)
3123               (substring subject 0 gnus-summary-gather-subject-limit)
3124             subject))
3125          ;; Fuzzily simplify it.
3126          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3127           (gnus-simplify-subject-fuzzy subject))
3128          ;; Just remove the leading "Re:".
3129          (t
3130           (gnus-simplify-subject-re subject))))
3131
3132   (if (and gnus-summary-gather-exclude-subject
3133            (string-match gnus-summary-gather-exclude-subject subject))
3134       nil                               ; This article shouldn't be gathered
3135     subject))
3136
3137 (defun gnus-summary-simplify-subject-query ()
3138   "Query where the respool algorithm would put this article."
3139   (interactive)
3140   (gnus-summary-select-article)
3141   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3142
3143 (defun gnus-gather-threads-by-subject (threads)
3144   "Gather threads by looking at Subject headers."
3145   (if (not gnus-summary-make-false-root)
3146       threads
3147     (let ((hashtb (gnus-make-hashtable 1024))
3148           (prev threads)
3149           (result threads)
3150           subject hthread whole-subject)
3151       (while threads
3152         (setq subject (gnus-general-simplify-subject
3153                        (setq whole-subject (mail-header-subject
3154                                             (caar threads)))))
3155         (when subject
3156           (if (setq hthread (gnus-gethash subject hashtb))
3157               (progn
3158                 ;; We enter a dummy root into the thread, if we
3159                 ;; haven't done that already.
3160                 (unless (stringp (caar hthread))
3161                   (setcar hthread (list whole-subject (car hthread))))
3162                 ;; We add this new gathered thread to this gathered
3163                 ;; thread.
3164                 (setcdr (car hthread)
3165                         (nconc (cdar hthread) (list (car threads))))
3166                 ;; Remove it from the list of threads.
3167                 (setcdr prev (cdr threads))
3168                 (setq threads prev))
3169             ;; Enter this thread into the hash table.
3170             (gnus-sethash subject threads hashtb)))
3171         (setq prev threads)
3172         (setq threads (cdr threads)))
3173       result)))
3174
3175 (defun gnus-gather-threads-by-references (threads)
3176   "Gather threads by looking at References headers."
3177   (let ((idhashtb (gnus-make-hashtable 1024))
3178         (thhashtb (gnus-make-hashtable 1024))
3179         (prev threads)
3180         (result threads)
3181         ids references id gthread gid entered ref)
3182     (while threads
3183       (when (setq references (mail-header-references (caar threads)))
3184         (setq id (mail-header-id (caar threads))
3185               ids (gnus-split-references references)
3186               entered nil)
3187         (while (setq ref (pop ids))
3188           (setq ids (delete ref ids))
3189           (if (not (setq gid (gnus-gethash ref idhashtb)))
3190               (progn
3191                 (gnus-sethash ref id idhashtb)
3192                 (gnus-sethash id threads thhashtb))
3193             (setq gthread (gnus-gethash gid thhashtb))
3194             (unless entered
3195               ;; We enter a dummy root into the thread, if we
3196               ;; haven't done that already.
3197               (unless (stringp (caar gthread))
3198                 (setcar gthread (list (mail-header-subject (caar gthread))
3199                                       (car gthread))))
3200               ;; We add this new gathered thread to this gathered
3201               ;; thread.
3202               (setcdr (car gthread)
3203                       (nconc (cdar gthread) (list (car threads)))))
3204             ;; Add it into the thread hash table.
3205             (gnus-sethash id gthread thhashtb)
3206             (setq entered t)
3207             ;; Remove it from the list of threads.
3208             (setcdr prev (cdr threads))
3209             (setq threads prev))))
3210       (setq prev threads)
3211       (setq threads (cdr threads)))
3212     result))
3213
3214 (defun gnus-sort-gathered-threads (threads)
3215   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3216   (let ((result threads))
3217     (while threads
3218       (when (stringp (caar threads))
3219         (setcdr (car threads)
3220                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3221       (setq threads (cdr threads)))
3222     result))
3223
3224 (defun gnus-thread-loop-p (root thread)
3225   "Say whether ROOT is in THREAD."
3226   (let ((stack (list thread))
3227         (infloop 0)
3228         th)
3229     (while (setq thread (pop stack))
3230       (setq th (cdr thread))
3231       (while (and th
3232                   (not (eq (caar th) root)))
3233         (pop th))
3234       (if th
3235           ;; We have found a loop.
3236           (let (ref-dep)
3237             (setcdr thread (delq (car th) (cdr thread)))
3238             (if (boundp (setq ref-dep (intern "none"
3239                                               gnus-newsgroup-dependencies)))
3240                 (setcdr (symbol-value ref-dep)
3241                         (nconc (cdr (symbol-value ref-dep))
3242                                (list (car th))))
3243               (set ref-dep (list nil (car th))))
3244             (setq infloop 1
3245                   stack nil))
3246         ;; Push all the subthreads onto the stack.
3247         (push (cdr thread) stack)))
3248     infloop))
3249
3250 (defun gnus-make-threads ()
3251   "Go through the dependency hashtb and find the roots.  Return all threads."
3252   (let (threads)
3253     (while (catch 'infloop
3254              (mapatoms
3255               (lambda (refs)
3256                 ;; Deal with self-referencing References loops.
3257                 (when (and (car (symbol-value refs))
3258                            (not (zerop
3259                                  (apply
3260                                   '+
3261                                   (mapcar
3262                                    (lambda (thread)
3263                                      (gnus-thread-loop-p
3264                                       (car (symbol-value refs)) thread))
3265                                    (cdr (symbol-value refs)))))))
3266                   (setq threads nil)
3267                   (throw 'infloop t))
3268                 (unless (car (symbol-value refs))
3269                   ;; These threads do not refer back to any other articles,
3270                   ;; so they're roots.
3271                   (setq threads (append (cdr (symbol-value refs)) threads))))
3272               gnus-newsgroup-dependencies)))
3273     threads))
3274
3275 ;; Build the thread tree.
3276 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3277   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3278
3279 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3280 if it was already present.
3281
3282 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3283 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3284 Message-IDs will be renamed be renamed to a unique Message-ID before
3285 being entered.
3286
3287 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3288   (let* ((id (mail-header-id header))
3289          (id-dep (and id (intern id dependencies)))
3290          ref ref-dep ref-header)
3291     ;; Enter this `header' in the `dependencies' table.
3292     (cond
3293      ((not id-dep)
3294       (setq header nil))
3295      ;; The first two cases do the normal part: enter a new `header'
3296      ;; in the `dependencies' table.
3297      ((not (boundp id-dep))
3298       (set id-dep (list header)))
3299      ((null (car (symbol-value id-dep)))
3300       (setcar (symbol-value id-dep) header))
3301
3302      ;; From here the `header' was already present in the
3303      ;; `dependencies' table.
3304      (force-new
3305       ;; Overrides an existing entry;
3306       ;; just set the header part of the entry.
3307       (setcar (symbol-value id-dep) header))
3308
3309      ;; Renames the existing `header' to a unique Message-ID.
3310      ((not gnus-summary-ignore-duplicates)
3311       ;; An article with this Message-ID has already been seen.
3312       ;; We rename the Message-ID.
3313       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3314            (list header))
3315       (mail-header-set-id header id))
3316
3317      ;; The last case ignores an existing entry, except it adds any
3318      ;; additional Xrefs (in case the two articles came from different
3319      ;; servers.
3320      ;; Also sets `header' to `nil' meaning that the `dependencies'
3321      ;; table was *not* modified.
3322      (t
3323       (mail-header-set-xref
3324        (car (symbol-value id-dep))
3325        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3326                    "")
3327                (or (mail-header-xref header) "")))
3328       (setq header nil)))
3329
3330     (when header
3331       ;; First check if that we are not creating a References loop.
3332       (setq ref (gnus-parent-id (mail-header-references header)))
3333       (while (and ref
3334                   (setq ref-dep (intern-soft ref dependencies))
3335                   (boundp ref-dep)
3336                   (setq ref-header (car (symbol-value ref-dep))))
3337         (if (string= id ref)
3338             ;; Yuk!  This is a reference loop.  Make the article be a
3339             ;; root article.
3340             (progn
3341               (mail-header-set-references (car (symbol-value id-dep)) "none")
3342               (setq ref nil))
3343           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3344       (setq ref (gnus-parent-id (mail-header-references header)))
3345       (setq ref-dep (intern (or ref "none") dependencies))
3346       (if (boundp ref-dep)
3347           (setcdr (symbol-value ref-dep)
3348                   (nconc (cdr (symbol-value ref-dep))
3349                          (list (symbol-value id-dep))))
3350         (set ref-dep (list nil (symbol-value id-dep)))))
3351     header))
3352
3353 (defun gnus-build-sparse-threads ()
3354   (let ((headers gnus-newsgroup-headers)
3355         (mail-parse-charset gnus-newsgroup-charset)
3356         (gnus-summary-ignore-duplicates t)
3357         header references generation relations
3358         subject child end new-child date)
3359     ;; First we create an alist of generations/relations, where
3360     ;; generations is how much we trust the relation, and the relation
3361     ;; is parent/child.
3362     (gnus-message 7 "Making sparse threads...")
3363     (save-excursion
3364       (nnheader-set-temp-buffer " *gnus sparse threads*")
3365       (while (setq header (pop headers))
3366         (when (and (setq references (mail-header-references header))
3367                    (not (string= references "")))
3368           (insert references)
3369           (setq child (mail-header-id header)
3370                 subject (mail-header-subject header)
3371                 date (mail-header-date header)
3372                 generation 0)
3373           (while (search-backward ">" nil t)
3374             (setq end (1+ (point)))
3375             (when (search-backward "<" nil t)
3376               (setq new-child (buffer-substring (point) end))
3377               (push (list (incf generation)
3378                           child (setq child new-child)
3379                           subject date)
3380                     relations)))
3381           (when child
3382             (push (list (1+ generation) child nil subject) relations))
3383           (erase-buffer)))
3384       (kill-buffer (current-buffer)))
3385     ;; Sort over trustworthiness.
3386     (mapcar
3387      (lambda (relation)
3388        (when (gnus-dependencies-add-header
3389               (make-full-mail-header
3390                gnus-reffed-article-number
3391                (nth 3 relation) "" (or (nth 4 relation) "")
3392                (nth 1 relation)
3393                (or (nth 2 relation) "") 0 0 "")
3394               gnus-newsgroup-dependencies nil)
3395          (push gnus-reffed-article-number gnus-newsgroup-limit)
3396          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3397          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3398                gnus-newsgroup-reads)
3399          (decf gnus-reffed-article-number)))
3400      (sort relations 'car-less-than-car))
3401     (gnus-message 7 "Making sparse threads...done")))
3402
3403 (defun gnus-build-old-threads ()
3404   ;; Look at all the articles that refer back to old articles, and
3405   ;; fetch the headers for the articles that aren't there.  This will
3406   ;; build complete threads - if the roots haven't been expired by the
3407   ;; server, that is.
3408   (let ((mail-parse-charset gnus-newsgroup-charset)
3409         id heads)
3410     (mapatoms
3411      (lambda (refs)
3412        (when (not (car (symbol-value refs)))
3413          (setq heads (cdr (symbol-value refs)))
3414          (while heads
3415            (if (memq (mail-header-number (caar heads))
3416                      gnus-newsgroup-dormant)
3417                (setq heads (cdr heads))
3418              (setq id (symbol-name refs))
3419              (while (and (setq id (gnus-build-get-header id))
3420                          (not (car (gnus-id-to-thread id)))))
3421              (setq heads nil)))))
3422      gnus-newsgroup-dependencies)))
3423
3424 ;; This function has to be called with point after the article number
3425 ;; on the beginning of the line.
3426 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3427   (let ((eol (gnus-point-at-eol))
3428         (buffer (current-buffer))
3429         header)
3430
3431     ;; overview: [num subject from date id refs chars lines misc]
3432     (unwind-protect
3433         (progn
3434           (narrow-to-region (point) eol)
3435           (unless (eobp)
3436             (forward-char))
3437
3438           (setq header
3439                 (make-full-mail-header
3440                  number                 ; number
3441                  (funcall gnus-decode-encoded-word-function
3442                           (nnheader-nov-field)) ; subject
3443                  (funcall gnus-decode-encoded-word-function
3444                           (nnheader-nov-field)) ; from
3445                  (nnheader-nov-field)   ; date
3446                  (nnheader-nov-read-message-id) ; id
3447                  (nnheader-nov-field)   ; refs
3448                  (nnheader-nov-read-integer) ; chars
3449                  (nnheader-nov-read-integer) ; lines
3450                  (unless (eobp)
3451                    (if (looking-at "Xref: ")
3452                        (goto-char (match-end 0)))
3453                    (nnheader-nov-field)) ; Xref
3454                  (nnheader-nov-parse-extra)))) ; extra
3455
3456       (widen))
3457
3458     (when gnus-alter-header-function
3459       (funcall gnus-alter-header-function header))
3460     (gnus-dependencies-add-header header dependencies force-new)))
3461
3462 (defun gnus-build-get-header (id)
3463   "Look through the buffer of NOV lines and find the header to ID.
3464 Enter this line into the dependencies hash table, and return
3465 the id of the parent article (if any)."
3466   (let ((deps gnus-newsgroup-dependencies)
3467         found header)
3468     (prog1
3469         (save-excursion
3470           (set-buffer nntp-server-buffer)
3471           (let ((case-fold-search nil))
3472             (goto-char (point-min))
3473             (while (and (not found)
3474                         (search-forward id nil t))
3475               (beginning-of-line)
3476               (setq found (looking-at
3477                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3478                                    (regexp-quote id))))
3479               (or found (beginning-of-line 2)))
3480             (when found
3481               (beginning-of-line)
3482               (and
3483                (setq header (gnus-nov-parse-line
3484                              (read (current-buffer)) deps))
3485                (gnus-parent-id (mail-header-references header))))))
3486       (when header
3487         (let ((number (mail-header-number header)))
3488           (push number gnus-newsgroup-limit)
3489           (push header gnus-newsgroup-headers)
3490           (if (memq number gnus-newsgroup-unselected)
3491               (progn
3492                 (push number gnus-newsgroup-unreads)
3493                 (setq gnus-newsgroup-unselected
3494                       (delq number gnus-newsgroup-unselected)))
3495             (push number gnus-newsgroup-ancient)))))))
3496
3497 (defun gnus-build-all-threads ()
3498   "Read all the headers."
3499   (let ((gnus-summary-ignore-duplicates t)
3500         (mail-parse-charset gnus-newsgroup-charset)
3501         (dependencies gnus-newsgroup-dependencies)
3502         header article)
3503     (save-excursion
3504       (set-buffer nntp-server-buffer)
3505       (let ((case-fold-search nil))
3506         (goto-char (point-min))
3507         (while (not (eobp))
3508           (ignore-errors
3509             (setq article (read (current-buffer))
3510                   header (gnus-nov-parse-line article dependencies)))
3511           (when header
3512             (save-excursion
3513               (set-buffer gnus-summary-buffer)
3514               (push header gnus-newsgroup-headers)
3515               (if (memq (setq article (mail-header-number header))
3516                         gnus-newsgroup-unselected)
3517                   (progn
3518                     (push article gnus-newsgroup-unreads)
3519                     (setq gnus-newsgroup-unselected
3520                           (delq article gnus-newsgroup-unselected)))
3521                 (push article gnus-newsgroup-ancient)))
3522             (forward-line 1)))))))
3523
3524 (defun gnus-summary-update-article-line (article header)
3525   "Update the line for ARTICLE using HEADERS."
3526   (let* ((id (mail-header-id header))
3527          (thread (gnus-id-to-thread id)))
3528     (unless thread
3529       (error "Article in no thread"))
3530     ;; Update the thread.
3531     (setcar thread header)
3532     (gnus-summary-goto-subject article)
3533     (let* ((datal (gnus-data-find-list article))
3534            (data (car datal))
3535            (length (when (cdr datal)
3536                      (- (gnus-data-pos data)
3537                         (gnus-data-pos (cadr datal)))))
3538            (buffer-read-only nil)
3539            (level (gnus-summary-thread-level)))
3540       (gnus-delete-line)
3541       (gnus-summary-insert-line
3542        header level nil (gnus-article-mark article)
3543        (memq article gnus-newsgroup-replied)
3544        (memq article gnus-newsgroup-expirable)
3545        ;; Only insert the Subject string when it's different
3546        ;; from the previous Subject string.
3547        (if (and
3548             gnus-show-threads
3549             (gnus-subject-equal
3550              (condition-case ()
3551                  (mail-header-subject
3552                   (gnus-data-header
3553                    (cadr
3554                     (gnus-data-find-list
3555                      article
3556                      (gnus-data-list t)))))
3557                ;; Error on the side of excessive subjects.
3558                (error ""))
3559              (mail-header-subject header)))
3560            ""
3561          (mail-header-subject header))
3562        nil (cdr (assq article gnus-newsgroup-scored))
3563        (memq article gnus-newsgroup-processable))
3564       (when length
3565         (gnus-data-update-list
3566          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3567
3568 (defun gnus-summary-update-article (article &optional iheader)
3569   "Update ARTICLE in the summary buffer."
3570   (set-buffer gnus-summary-buffer)
3571   (let* ((header (gnus-summary-article-header article))
3572          (id (mail-header-id header))
3573          (data (gnus-data-find article))
3574          (thread (gnus-id-to-thread id))
3575          (references (mail-header-references header))
3576          (parent
3577           (gnus-id-to-thread
3578            (or (gnus-parent-id
3579                 (when (and references
3580                            (not (equal "" references)))
3581                   references))
3582                "none")))
3583          (buffer-read-only nil)
3584          (old (car thread)))
3585     (when thread
3586       (unless iheader
3587         (setcar thread nil)
3588         (when parent
3589           (delq thread parent)))
3590       (if (gnus-summary-insert-subject id header)
3591           ;; Set the (possibly) new article number in the data structure.
3592           (gnus-data-set-number data (gnus-id-to-article id))
3593         (setcar thread old)
3594         nil))))
3595
3596 (defun gnus-rebuild-thread (id &optional line)
3597   "Rebuild the thread containing ID.
3598 If LINE, insert the rebuilt thread starting on line LINE."
3599   (let ((buffer-read-only nil)
3600         old-pos current thread data)
3601     (if (not gnus-show-threads)
3602         (setq thread (list (car (gnus-id-to-thread id))))
3603       ;; Get the thread this article is part of.
3604       (setq thread (gnus-remove-thread id)))
3605     (setq old-pos (gnus-point-at-bol))
3606     (setq current (save-excursion
3607                     (and (zerop (forward-line -1))
3608                          (gnus-summary-article-number))))
3609     ;; If this is a gathered thread, we have to go some re-gathering.
3610     (when (stringp (car thread))
3611       (let ((subject (car thread))
3612             roots thr)
3613         (setq thread (cdr thread))
3614         (while thread
3615           (unless (memq (setq thr (gnus-id-to-thread
3616                                    (gnus-root-id
3617                                     (mail-header-id (caar thread)))))
3618                         roots)
3619             (push thr roots))
3620           (setq thread (cdr thread)))
3621         ;; We now have all (unique) roots.
3622         (if (= (length roots) 1)
3623             ;; All the loose roots are now one solid root.
3624             (setq thread (car roots))
3625           (setq thread (cons subject (gnus-sort-threads roots))))))
3626     (let (threads)
3627       ;; We then insert this thread into the summary buffer.
3628       (when line
3629         (goto-char (point-min))
3630         (forward-line (1- line)))
3631       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3632         (if gnus-show-threads
3633             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3634           (gnus-summary-prepare-unthreaded thread))
3635         (setq data (nreverse gnus-newsgroup-data))
3636         (setq threads gnus-newsgroup-threads))
3637       ;; We splice the new data into the data structure.
3638       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3639       ;;!!! then we want to insert at the beginning of the buffer.
3640       ;;!!! That happens to be true with Gnus now, but that may
3641       ;;!!! change in the future.  Perhaps.
3642       (gnus-data-enter-list
3643        (if line nil current) data (- (point) old-pos))
3644       (setq gnus-newsgroup-threads
3645             (nconc threads gnus-newsgroup-threads))
3646       (gnus-data-compute-positions))))
3647
3648 (defun gnus-number-to-header (number)
3649   "Return the header for article NUMBER."
3650   (let ((headers gnus-newsgroup-headers))
3651     (while (and headers
3652                 (not (= number (mail-header-number (car headers)))))
3653       (pop headers))
3654     (when headers
3655       (car headers))))
3656
3657 (defun gnus-parent-headers (in-headers &optional generation)
3658   "Return the headers of the GENERATIONeth parent of HEADERS."
3659   (unless generation
3660     (setq generation 1))
3661   (let ((parent t)
3662         (headers in-headers)
3663         references)
3664     (while (and parent
3665                 (not (zerop generation))
3666                 (setq references (mail-header-references headers)))
3667       (setq headers (if (and references
3668                              (setq parent (gnus-parent-id references)))
3669                         (car (gnus-id-to-thread parent))
3670                       nil))
3671       (decf generation))
3672     (and (not (eq headers in-headers))
3673          headers)))
3674
3675 (defun gnus-id-to-thread (id)
3676   "Return the (sub-)thread where ID appears."
3677   (gnus-gethash id gnus-newsgroup-dependencies))
3678
3679 (defun gnus-id-to-article (id)
3680   "Return the article number of ID."
3681   (let ((thread (gnus-id-to-thread id)))
3682     (when (and thread
3683                (car thread))
3684       (mail-header-number (car thread)))))
3685
3686 (defun gnus-id-to-header (id)
3687   "Return the article headers of ID."
3688   (car (gnus-id-to-thread id)))
3689
3690 (defun gnus-article-displayed-root-p (article)
3691   "Say whether ARTICLE is a root(ish) article."
3692   (let ((level (gnus-summary-thread-level article))
3693         (refs (mail-header-references  (gnus-summary-article-header article)))
3694         particle)
3695     (cond
3696      ((null level) nil)
3697      ((zerop level) t)
3698      ((null refs) t)
3699      ((null (gnus-parent-id refs)) t)
3700      ((and (= 1 level)
3701            (null (setq particle (gnus-id-to-article
3702                                  (gnus-parent-id refs))))
3703            (null (gnus-summary-thread-level particle)))))))
3704
3705 (defun gnus-root-id (id)
3706   "Return the id of the root of the thread where ID appears."
3707   (let (last-id prev)
3708     (while (and id (setq prev (car (gnus-id-to-thread id))))
3709       (setq last-id id
3710             id (gnus-parent-id (mail-header-references prev))))
3711     last-id))
3712
3713 (defun gnus-articles-in-thread (thread)
3714   "Return the list of articles in THREAD."
3715   (cons (mail-header-number (car thread))
3716         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3717
3718 (defun gnus-remove-thread (id &optional dont-remove)
3719   "Remove the thread that has ID in it."
3720   (let (headers thread last-id)
3721     ;; First go up in this thread until we find the root.
3722     (setq last-id (gnus-root-id id)
3723           headers (message-flatten-list (gnus-id-to-thread last-id)))
3724     ;; We have now found the real root of this thread.  It might have
3725     ;; been gathered into some loose thread, so we have to search
3726     ;; through the threads to find the thread we wanted.
3727     (let ((threads gnus-newsgroup-threads)
3728           sub)
3729       (while threads
3730         (setq sub (car threads))
3731         (if (stringp (car sub))
3732             ;; This is a gathered thread, so we look at the roots
3733             ;; below it to find whether this article is in this
3734             ;; gathered root.
3735             (progn
3736               (setq sub (cdr sub))
3737               (while sub
3738                 (when (member (caar sub) headers)
3739                   (setq thread (car threads)
3740                         threads nil
3741                         sub nil))
3742                 (setq sub (cdr sub))))
3743           ;; It's an ordinary thread, so we check it.
3744           (when (eq (car sub) (car headers))
3745             (setq thread sub
3746                   threads nil)))
3747         (setq threads (cdr threads)))
3748       ;; If this article is in no thread, then it's a root.
3749       (if thread
3750           (unless dont-remove
3751             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3752         (setq thread (gnus-id-to-thread last-id)))
3753       (when thread
3754         (prog1
3755             thread                      ; We return this thread.
3756           (unless dont-remove
3757             (if (stringp (car thread))
3758                 (progn
3759                   ;; If we use dummy roots, then we have to remove the
3760                   ;; dummy root as well.
3761                   (when (eq gnus-summary-make-false-root 'dummy)
3762                     ;; We go to the dummy root by going to
3763                     ;; the first sub-"thread", and then one line up.
3764                     (gnus-summary-goto-article
3765                      (mail-header-number (caadr thread)))
3766                     (forward-line -1)
3767                     (gnus-delete-line)
3768                     (gnus-data-compute-positions))
3769                   (setq thread (cdr thread))
3770                   (while thread
3771                     (gnus-remove-thread-1 (car thread))
3772                     (setq thread (cdr thread))))
3773               (gnus-remove-thread-1 thread))))))))
3774
3775 (defun gnus-remove-thread-1 (thread)
3776   "Remove the thread THREAD recursively."
3777   (let ((number (mail-header-number (pop thread)))
3778         d)
3779     (setq thread (reverse thread))
3780     (while thread
3781       (gnus-remove-thread-1 (pop thread)))
3782     (when (setq d (gnus-data-find number))
3783       (goto-char (gnus-data-pos d))
3784       (gnus-summary-show-thread)
3785       (gnus-data-remove
3786        number
3787        (- (gnus-point-at-bol)
3788           (prog1
3789               (1+ (gnus-point-at-eol))
3790             (gnus-delete-line)))))))
3791
3792 (defun gnus-sort-threads-1 (threads func)
3793   (sort (mapcar (lambda (thread)
3794                   (cons (car thread)
3795                         (and (cdr thread)
3796                              (gnus-sort-threads-1 (cdr thread) func))))
3797                 threads) func))
3798
3799 (defun gnus-sort-threads (threads)
3800   "Sort THREADS."
3801   (if (not gnus-thread-sort-functions)
3802       threads
3803     (gnus-message 8 "Sorting threads...")
3804     (prog1
3805         (gnus-sort-threads-1 
3806          threads 
3807          (gnus-make-sort-function gnus-thread-sort-functions))
3808       (gnus-message 8 "Sorting threads...done"))))
3809
3810 (defun gnus-sort-articles (articles)
3811   "Sort ARTICLES."
3812   (when gnus-article-sort-functions
3813     (gnus-message 7 "Sorting articles...")
3814     (prog1
3815         (setq gnus-newsgroup-headers
3816               (sort articles (gnus-make-sort-function
3817                               gnus-article-sort-functions)))
3818       (gnus-message 7 "Sorting articles...done"))))
3819
3820 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3821 (defmacro gnus-thread-header (thread)
3822   "Return header of first article in THREAD.
3823 Note that THREAD must never, ever be anything else than a variable -
3824 using some other form will lead to serious barfage."
3825   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3826   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3827   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3828         (vector thread) 2))
3829
3830 (defsubst gnus-article-sort-by-number (h1 h2)
3831   "Sort articles by article number."
3832   (< (mail-header-number h1)
3833      (mail-header-number h2)))
3834
3835 (defun gnus-thread-sort-by-number (h1 h2)
3836   "Sort threads by root article number."
3837   (gnus-article-sort-by-number
3838    (gnus-thread-header h1) (gnus-thread-header h2)))
3839
3840 (defsubst gnus-article-sort-by-lines (h1 h2)
3841   "Sort articles by article Lines header."
3842   (< (mail-header-lines h1)
3843      (mail-header-lines h2)))
3844
3845 (defun gnus-thread-sort-by-lines (h1 h2)
3846   "Sort threads by root article Lines header."
3847   (gnus-article-sort-by-lines
3848    (gnus-thread-header h1) (gnus-thread-header h2)))
3849
3850 (defsubst gnus-article-sort-by-chars (h1 h2)
3851   "Sort articles by octet length."
3852   (< (mail-header-chars h1)
3853      (mail-header-chars h2)))
3854
3855 (defun gnus-thread-sort-by-chars (h1 h2)
3856   "Sort threads by root article octet length."
3857   (gnus-article-sort-by-chars
3858    (gnus-thread-header h1) (gnus-thread-header h2)))
3859
3860 (defsubst gnus-article-sort-by-author (h1 h2)
3861   "Sort articles by root author."
3862   (string-lessp
3863    (let ((extract (funcall
3864                    gnus-extract-address-components
3865                    (mail-header-from h1))))
3866      (or (car extract) (cadr extract) ""))
3867    (let ((extract (funcall
3868                    gnus-extract-address-components
3869                    (mail-header-from h2))))
3870      (or (car extract) (cadr extract) ""))))
3871
3872 (defun gnus-thread-sort-by-author (h1 h2)
3873   "Sort threads by root author."
3874   (gnus-article-sort-by-author
3875    (gnus-thread-header h1)  (gnus-thread-header h2)))
3876
3877 (defsubst gnus-article-sort-by-subject (h1 h2)
3878   "Sort articles by root subject."
3879   (string-lessp
3880    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3881    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3882
3883 (defun gnus-thread-sort-by-subject (h1 h2)
3884   "Sort threads by root subject."
3885   (gnus-article-sort-by-subject
3886    (gnus-thread-header h1) (gnus-thread-header h2)))
3887
3888 (defsubst gnus-article-sort-by-date (h1 h2)
3889   "Sort articles by root article date."
3890   (time-less-p
3891    (gnus-date-get-time (mail-header-date h1))
3892    (gnus-date-get-time (mail-header-date h2))))
3893
3894 (defun gnus-thread-sort-by-date (h1 h2)
3895   "Sort threads by root article date."
3896   (gnus-article-sort-by-date
3897    (gnus-thread-header h1) (gnus-thread-header h2)))
3898
3899 (defsubst gnus-article-sort-by-score (h1 h2)
3900   "Sort articles by root article score.
3901 Unscored articles will be counted as having a score of zero."
3902   (> (or (cdr (assq (mail-header-number h1)
3903                     gnus-newsgroup-scored))
3904          gnus-summary-default-score 0)
3905      (or (cdr (assq (mail-header-number h2)
3906                     gnus-newsgroup-scored))
3907          gnus-summary-default-score 0)))
3908
3909 (defun gnus-thread-sort-by-score (h1 h2)
3910   "Sort threads by root article score."
3911   (gnus-article-sort-by-score
3912    (gnus-thread-header h1) (gnus-thread-header h2)))
3913
3914 (defun gnus-thread-sort-by-total-score (h1 h2)
3915   "Sort threads by the sum of all scores in the thread.
3916 Unscored articles will be counted as having a score of zero."
3917   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3918
3919 (defun gnus-thread-total-score (thread)
3920   ;; This function find the total score of THREAD.
3921   (cond ((null thread)
3922          0)
3923         ((consp thread)
3924          (if (stringp (car thread))
3925              (apply gnus-thread-score-function 0
3926                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3927            (gnus-thread-total-score-1 thread)))
3928         (t
3929          (gnus-thread-total-score-1 (list thread)))))
3930
3931 (defun gnus-thread-total-score-1 (root)
3932   ;; This function find the total score of the thread below ROOT.
3933   (setq root (car root))
3934   (apply gnus-thread-score-function
3935          (or (append
3936               (mapcar 'gnus-thread-total-score
3937                       (cdr (gnus-id-to-thread (mail-header-id root))))
3938               (when (> (mail-header-number root) 0)
3939                 (list (or (cdr (assq (mail-header-number root)
3940                                      gnus-newsgroup-scored))
3941                           gnus-summary-default-score 0))))
3942              (list gnus-summary-default-score)
3943              '(0))))
3944
3945 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3946 (defvar gnus-tmp-prev-subject nil)
3947 (defvar gnus-tmp-false-parent nil)
3948 (defvar gnus-tmp-root-expunged nil)
3949 (defvar gnus-tmp-dummy-line nil)
3950
3951 (eval-when-compile (defvar gnus-tmp-header))
3952 (defun gnus-extra-header (type &optional header)
3953   "Return the extra header of TYPE."
3954   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3955       ""))
3956
3957 (defun gnus-summary-prepare-threads (threads)
3958   "Prepare summary buffer from THREADS and indentation LEVEL.
3959 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3960 or a straight list of headers."
3961   (gnus-message 7 "Generating summary...")
3962
3963   (setq gnus-newsgroup-threads threads)
3964   (beginning-of-line)
3965
3966   (let ((gnus-tmp-level 0)
3967         (default-score (or gnus-summary-default-score 0))
3968         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3969         thread number subject stack state gnus-tmp-gathered beg-match
3970         new-roots gnus-tmp-new-adopts thread-end
3971         gnus-tmp-header gnus-tmp-unread
3972         gnus-tmp-replied gnus-tmp-subject-or-nil
3973         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3974         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3975         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3976
3977     (setq gnus-tmp-prev-subject nil)
3978
3979     (if (vectorp (car threads))
3980         ;; If this is a straight (sic) list of headers, then a
3981         ;; threaded summary display isn't required, so we just create
3982         ;; an unthreaded one.
3983         (gnus-summary-prepare-unthreaded threads)
3984
3985       ;; Do the threaded display.
3986
3987       (while (or threads stack gnus-tmp-new-adopts new-roots)
3988
3989         (if (and (= gnus-tmp-level 0)
3990                  (or (not stack)
3991                      (= (caar stack) 0))
3992                  (not gnus-tmp-false-parent)
3993                  (or gnus-tmp-new-adopts new-roots))
3994             (if gnus-tmp-new-adopts
3995                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3996                       thread (list (car gnus-tmp-new-adopts))
3997                       gnus-tmp-header (caar thread)
3998                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3999               (when new-roots
4000                 (setq thread (list (car new-roots))
4001                       gnus-tmp-header (caar thread)
4002                       new-roots (cdr new-roots))))
4003
4004           (if threads
4005               ;; If there are some threads, we do them before the
4006               ;; threads on the stack.
4007               (setq thread threads
4008                     gnus-tmp-header (caar thread))
4009             ;; There were no current threads, so we pop something off
4010             ;; the stack.
4011             (setq state (car stack)
4012                   gnus-tmp-level (car state)
4013                   thread (cdr state)
4014                   stack (cdr stack)
4015                   gnus-tmp-header (caar thread))))
4016
4017         (setq gnus-tmp-false-parent nil)
4018         (setq gnus-tmp-root-expunged nil)
4019         (setq thread-end nil)
4020
4021         (if (stringp gnus-tmp-header)
4022             ;; The header is a dummy root.
4023             (cond
4024              ((eq gnus-summary-make-false-root 'adopt)
4025               ;; We let the first article adopt the rest.
4026               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4027                                                (cddar thread)))
4028               (setq gnus-tmp-gathered
4029                     (nconc (mapcar
4030                             (lambda (h) (mail-header-number (car h)))
4031                             (cddar thread))
4032                            gnus-tmp-gathered))
4033               (setq thread (cons (list (caar thread)
4034                                        (cadar thread))
4035                                  (cdr thread)))
4036               (setq gnus-tmp-level -1
4037                     gnus-tmp-false-parent t))
4038              ((eq gnus-summary-make-false-root 'empty)
4039               ;; We print adopted articles with empty subject fields.
4040               (setq gnus-tmp-gathered
4041                     (nconc (mapcar
4042                             (lambda (h) (mail-header-number (car h)))
4043                             (cddar thread))
4044                            gnus-tmp-gathered))
4045               (setq gnus-tmp-level -1))
4046              ((eq gnus-summary-make-false-root 'dummy)
4047               ;; We remember that we probably want to output a dummy
4048               ;; root.
4049               (setq gnus-tmp-dummy-line gnus-tmp-header)
4050               (setq gnus-tmp-prev-subject gnus-tmp-header))
4051              (t
4052               ;; We do not make a root for the gathered
4053               ;; sub-threads at all.
4054               (setq gnus-tmp-level -1)))
4055
4056           (setq number (mail-header-number gnus-tmp-header)
4057                 subject (mail-header-subject gnus-tmp-header))
4058
4059           (cond
4060            ;; If the thread has changed subject, we might want to make
4061            ;; this subthread into a root.
4062            ((and (null gnus-thread-ignore-subject)
4063                  (not (zerop gnus-tmp-level))
4064                  gnus-tmp-prev-subject
4065                  (not (inline
4066                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4067             (setq new-roots (nconc new-roots (list (car thread)))
4068                   thread-end t
4069                   gnus-tmp-header nil))
4070            ;; If the article lies outside the current limit,
4071            ;; then we do not display it.
4072            ((not (memq number gnus-newsgroup-limit))
4073             (setq gnus-tmp-gathered
4074                   (nconc (mapcar
4075                           (lambda (h) (mail-header-number (car h)))
4076                           (cdar thread))
4077                          gnus-tmp-gathered))
4078             (setq gnus-tmp-new-adopts (if (cdar thread)
4079                                           (append gnus-tmp-new-adopts
4080                                                   (cdar thread))
4081                                         gnus-tmp-new-adopts)
4082                   thread-end t
4083                   gnus-tmp-header nil)
4084             (when (zerop gnus-tmp-level)
4085               (setq gnus-tmp-root-expunged t)))
4086            ;; Perhaps this article is to be marked as read?
4087            ((and gnus-summary-mark-below
4088                  (< (or (cdr (assq number gnus-newsgroup-scored))
4089                         default-score)
4090                     gnus-summary-mark-below)
4091                  ;; Don't touch sparse articles.
4092                  (not (gnus-summary-article-sparse-p number))
4093                  (not (gnus-summary-article-ancient-p number)))
4094             (setq gnus-newsgroup-unreads
4095                   (delq number gnus-newsgroup-unreads))
4096             (if gnus-newsgroup-auto-expire
4097                 (push number gnus-newsgroup-expirable)
4098               (push (cons number gnus-low-score-mark)
4099                     gnus-newsgroup-reads))))
4100
4101           (when gnus-tmp-header
4102             ;; We may have an old dummy line to output before this
4103             ;; article.
4104             (when (and gnus-tmp-dummy-line
4105                        (gnus-subject-equal
4106                         gnus-tmp-dummy-line
4107                         (mail-header-subject gnus-tmp-header)))
4108               (gnus-summary-insert-dummy-line
4109                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4110               (setq gnus-tmp-dummy-line nil))
4111
4112             ;; Compute the mark.
4113             (setq gnus-tmp-unread (gnus-article-mark number))
4114
4115             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4116                                   gnus-tmp-header gnus-tmp-level)
4117                   gnus-newsgroup-data)
4118
4119             ;; Actually insert the line.
4120             (setq
4121              gnus-tmp-subject-or-nil
4122              (cond
4123               ((and gnus-thread-ignore-subject
4124                     gnus-tmp-prev-subject
4125                     (not (inline (gnus-subject-equal
4126                                   gnus-tmp-prev-subject subject))))
4127                subject)
4128               ((zerop gnus-tmp-level)
4129                (if (and (eq gnus-summary-make-false-root 'empty)
4130                         (memq number gnus-tmp-gathered)
4131                         gnus-tmp-prev-subject
4132                         (inline (gnus-subject-equal
4133                                  gnus-tmp-prev-subject subject)))
4134                    gnus-summary-same-subject
4135                  subject))
4136               (t gnus-summary-same-subject)))
4137             (if (and (eq gnus-summary-make-false-root 'adopt)
4138                      (= gnus-tmp-level 1)
4139                      (memq number gnus-tmp-gathered))
4140                 (setq gnus-tmp-opening-bracket ?\<
4141                       gnus-tmp-closing-bracket ?\>)
4142               (setq gnus-tmp-opening-bracket ?\[
4143                     gnus-tmp-closing-bracket ?\]))
4144             (setq
4145              gnus-tmp-indentation
4146              (aref gnus-thread-indent-array gnus-tmp-level)
4147              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4148              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4149                                 gnus-summary-default-score 0)
4150              gnus-tmp-score-char
4151              (if (or (null gnus-summary-default-score)
4152                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4153                          gnus-summary-zcore-fuzz))
4154                  ?  ;Whitespace
4155                (if (< gnus-tmp-score gnus-summary-default-score)
4156                    gnus-score-below-mark gnus-score-over-mark))
4157              gnus-tmp-replied
4158              (cond ((memq number gnus-newsgroup-processable)
4159                     gnus-process-mark)
4160                    ((memq number gnus-newsgroup-cached)
4161                     gnus-cached-mark)
4162                    ((memq number gnus-newsgroup-replied)
4163                     gnus-replied-mark)
4164                    ((memq number gnus-newsgroup-saved)
4165                     gnus-saved-mark)
4166                    (t gnus-unread-mark))
4167              gnus-tmp-from (mail-header-from gnus-tmp-header)
4168              gnus-tmp-name
4169              (cond
4170               ((string-match "<[^>]+> *$" gnus-tmp-from)
4171                (setq beg-match (match-beginning 0))
4172                (or (and (string-match "^\".+\"" gnus-tmp-from)
4173                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4174                    (substring gnus-tmp-from 0 beg-match)))
4175               ((string-match "(.+)" gnus-tmp-from)
4176                (substring gnus-tmp-from
4177                           (1+ (match-beginning 0)) (1- (match-end 0))))
4178               (t gnus-tmp-from)))
4179             (when (string= gnus-tmp-name "")
4180               (setq gnus-tmp-name gnus-tmp-from))
4181             (unless (numberp gnus-tmp-lines)
4182               (setq gnus-tmp-lines 0))
4183             (gnus-put-text-property
4184              (point)
4185              (progn (eval gnus-summary-line-format-spec) (point))
4186              'gnus-number number)
4187             (when gnus-visual-p
4188               (forward-line -1)
4189               (gnus-run-hooks 'gnus-summary-update-hook)
4190               (forward-line 1))
4191
4192             (setq gnus-tmp-prev-subject subject)))
4193
4194         (when (nth 1 thread)
4195           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4196         (incf gnus-tmp-level)
4197         (setq threads (if thread-end nil (cdar thread)))
4198         (unless threads
4199           (setq gnus-tmp-level 0)))))
4200   (gnus-message 7 "Generating summary...done"))
4201
4202 (defun gnus-summary-prepare-unthreaded (headers)
4203   "Generate an unthreaded summary buffer based on HEADERS."
4204   (let (header number mark)
4205
4206     (beginning-of-line)
4207
4208     (while headers
4209       ;; We may have to root out some bad articles...
4210       (when (memq (setq number (mail-header-number
4211                                 (setq header (pop headers))))
4212                   gnus-newsgroup-limit)
4213         ;; Mark article as read when it has a low score.
4214         (when (and gnus-summary-mark-below
4215                    (< (or (cdr (assq number gnus-newsgroup-scored))
4216                           gnus-summary-default-score 0)
4217                       gnus-summary-mark-below)
4218                    (not (gnus-summary-article-ancient-p number)))
4219           (setq gnus-newsgroup-unreads
4220                 (delq number gnus-newsgroup-unreads))
4221           (if gnus-newsgroup-auto-expire
4222               (push number gnus-newsgroup-expirable)
4223             (push (cons number gnus-low-score-mark)
4224                   gnus-newsgroup-reads)))
4225
4226         (setq mark (gnus-article-mark number))
4227         (push (gnus-data-make number mark (1+ (point)) header 0)
4228               gnus-newsgroup-data)
4229         (gnus-summary-insert-line
4230          header 0 number
4231          mark (memq number gnus-newsgroup-replied)
4232          (memq number gnus-newsgroup-expirable)
4233          (mail-header-subject header) nil
4234          (cdr (assq number gnus-newsgroup-scored))
4235          (memq number gnus-newsgroup-processable))))))
4236
4237 (defun gnus-summary-remove-list-identifiers ()
4238   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4239   (let ((regexp (if (stringp gnus-list-identifiers)
4240                     gnus-list-identifiers
4241                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4242     (dolist (header gnus-newsgroup-headers)
4243       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4244                                   " *\\)\\)+\\(Re: +\\)?\\)")
4245                           (mail-header-subject header))
4246         (mail-header-set-subject
4247          header (concat (substring (mail-header-subject header)
4248                                    0 (match-beginning 1))
4249                         (or
4250                          (match-string 3 (mail-header-subject header))
4251                          (match-string 5 (mail-header-subject header)))
4252                         (substring (mail-header-subject header)
4253                                    (match-end 1))))))))
4254
4255 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4256   "Select newsgroup GROUP.
4257 If READ-ALL is non-nil, all articles in the group are selected.
4258 If SELECT-ARTICLES, only select those articles from GROUP."
4259   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4260          ;;!!! Dirty hack; should be removed.
4261          (gnus-summary-ignore-duplicates
4262           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4263               t
4264             gnus-summary-ignore-duplicates))
4265          (info (nth 2 entry))
4266          articles fetched-articles cached)
4267
4268     (unless (gnus-check-server
4269              (setq gnus-current-select-method
4270                    (gnus-find-method-for-group group)))
4271       (error "Couldn't open server"))
4272
4273     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4274         (gnus-activate-group group)     ; Or we can activate it...
4275         (progn                          ; Or we bug out.
4276           (when (equal major-mode 'gnus-summary-mode)
4277             (kill-buffer (current-buffer)))
4278           (error "Couldn't activate group %s: %s"
4279                  group (gnus-status-message group))))
4280
4281     (unless (gnus-request-group group t)
4282       (when (equal major-mode 'gnus-summary-mode)
4283         (kill-buffer (current-buffer)))
4284       (error "Couldn't request group %s: %s"
4285              group (gnus-status-message group)))
4286
4287     (setq gnus-newsgroup-name group)
4288     (setq gnus-newsgroup-unselected nil)
4289     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4290     (gnus-summary-setup-default-charset)
4291
4292     ;; Adjust and set lists of article marks.
4293     (when info
4294       (gnus-adjust-marked-articles info))
4295
4296     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4297     (when (gnus-virtual-group-p group)
4298       (setq cached gnus-newsgroup-cached))
4299
4300     (setq gnus-newsgroup-unreads
4301           (gnus-set-difference
4302            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4303            gnus-newsgroup-dormant))
4304
4305     (setq gnus-newsgroup-processable nil)
4306
4307     (gnus-update-read-articles group gnus-newsgroup-unreads)
4308
4309     (if (setq articles select-articles)
4310         (setq gnus-newsgroup-unselected
4311               (gnus-sorted-intersection
4312                gnus-newsgroup-unreads
4313                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4314       (setq articles (gnus-articles-to-read group read-all)))
4315
4316     (cond
4317      ((null articles)
4318       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4319       'quit)
4320      ((eq articles 0) nil)
4321      (t
4322       ;; Init the dependencies hash table.
4323       (setq gnus-newsgroup-dependencies
4324             (gnus-make-hashtable (length articles)))
4325       (gnus-set-global-variables)
4326       ;; Retrieve the headers and read them in.
4327       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4328       (setq gnus-newsgroup-headers
4329             (if (eq 'nov
4330                     (setq gnus-headers-retrieved-by
4331                           (gnus-retrieve-headers
4332                            articles gnus-newsgroup-name
4333                            ;; We might want to fetch old headers, but
4334                            ;; not if there is only 1 article.
4335                            (and (or (and
4336                                      (not (eq gnus-fetch-old-headers 'some))
4337                                      (not (numberp gnus-fetch-old-headers)))
4338                                     (> (length articles) 1))
4339                                 gnus-fetch-old-headers))))
4340                 (gnus-get-newsgroup-headers-xover
4341                  articles nil nil gnus-newsgroup-name t)
4342               (gnus-get-newsgroup-headers)))
4343       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4344
4345       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4346       (when cached
4347         (setq gnus-newsgroup-cached cached))
4348
4349       ;; Suppress duplicates?
4350       (when gnus-suppress-duplicates
4351         (gnus-dup-suppress-articles))
4352
4353       ;; Set the initial limit.
4354       (setq gnus-newsgroup-limit (copy-sequence articles))
4355       ;; Remove canceled articles from the list of unread articles.
4356       (setq gnus-newsgroup-unreads
4357             (gnus-set-sorted-intersection
4358              gnus-newsgroup-unreads
4359              (setq fetched-articles
4360                    (mapcar (lambda (headers) (mail-header-number headers))
4361                            gnus-newsgroup-headers))))
4362       ;; Removed marked articles that do not exist.
4363       (gnus-update-missing-marks
4364        (gnus-sorted-complement fetched-articles articles))
4365       ;; We might want to build some more threads first.
4366       (when (and gnus-fetch-old-headers
4367                  (eq gnus-headers-retrieved-by 'nov))
4368         (if (eq gnus-fetch-old-headers 'invisible)
4369             (gnus-build-all-threads)
4370           (gnus-build-old-threads)))
4371       ;; Let the Gnus agent mark articles as read.
4372       (when gnus-agent
4373         (gnus-agent-get-undownloaded-list))
4374       ;; Remove list identifiers from subject
4375       (when gnus-list-identifiers
4376         (gnus-summary-remove-list-identifiers))
4377       ;; Check whether auto-expire is to be done in this group.
4378       (setq gnus-newsgroup-auto-expire
4379             (gnus-group-auto-expirable-p group))
4380       ;; Set up the article buffer now, if necessary.
4381       (unless gnus-single-article-buffer
4382         (gnus-article-setup-buffer))
4383       ;; First and last article in this newsgroup.
4384       (when gnus-newsgroup-headers
4385         (setq gnus-newsgroup-begin
4386               (mail-header-number (car gnus-newsgroup-headers))
4387               gnus-newsgroup-end
4388               (mail-header-number
4389                (gnus-last-element gnus-newsgroup-headers))))
4390       ;; GROUP is successfully selected.
4391       (or gnus-newsgroup-headers t)))))
4392
4393 (defun gnus-articles-to-read (group &optional read-all)
4394   "Find out what articles the user wants to read."
4395   (let* ((articles
4396           ;; Select all articles if `read-all' is non-nil, or if there
4397           ;; are no unread articles.
4398           (if (or read-all
4399                   (and (zerop (length gnus-newsgroup-marked))
4400                        (zerop (length gnus-newsgroup-unreads)))
4401                   (eq (gnus-group-find-parameter group 'display)
4402                       'all))
4403               (or
4404                (gnus-uncompress-range (gnus-active group))
4405                (gnus-cache-articles-in-group group))
4406             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4407                           (copy-sequence gnus-newsgroup-unreads))
4408                   '<)))
4409          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4410          (scored (length scored-list))
4411          (number (length articles))
4412          (marked (+ (length gnus-newsgroup-marked)
4413                     (length gnus-newsgroup-dormant)))
4414          (select
4415           (cond
4416            ((numberp read-all)
4417             read-all)
4418            (t
4419             (condition-case ()
4420                 (cond
4421                  ((and (or (<= scored marked) (= scored number))
4422                        (numberp gnus-large-newsgroup)
4423                        (> number gnus-large-newsgroup))
4424                   (let ((input
4425                          (read-string
4426                           (format
4427                            "How many articles from %s (default %d): "
4428                            (gnus-limit-string gnus-newsgroup-name 35)
4429                            number))))
4430                     (if (string-match "^[ \t]*$" input) number input)))
4431                  ((and (> scored marked) (< scored number)
4432                        (> (- scored number) 20))
4433                   (let ((input
4434                          (read-string
4435                           (format "%s %s (%d scored, %d total): "
4436                                   "How many articles from"
4437                                   group scored number))))
4438                     (if (string-match "^[ \t]*$" input)
4439                         number input)))
4440                  (t number))
4441               (quit
4442                (message "Quit getting the articles to read")
4443                nil))))))
4444     (setq select (if (stringp select) (string-to-number select) select))
4445     (if (or (null select) (zerop select))
4446         select
4447       (if (and (not (zerop scored)) (<= (abs select) scored))
4448           (progn
4449             (setq articles (sort scored-list '<))
4450             (setq number (length articles)))
4451         (setq articles (copy-sequence articles)))
4452
4453       (when (< (abs select) number)
4454         (if (< select 0)
4455             ;; Select the N oldest articles.
4456             (setcdr (nthcdr (1- (abs select)) articles) nil)
4457           ;; Select the N most recent articles.
4458           (setq articles (nthcdr (- number select) articles))))
4459       (setq gnus-newsgroup-unselected
4460             (gnus-sorted-intersection
4461              gnus-newsgroup-unreads
4462              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4463       (when gnus-alter-articles-to-read-function
4464         (setq gnus-newsgroup-unreads
4465               (sort 
4466                (funcall gnus-alter-articles-to-read-function
4467                         gnus-newsgroup-name gnus-newsgroup-unreads)
4468                '<)))
4469       articles)))
4470
4471 (defun gnus-killed-articles (killed articles)
4472   (let (out)
4473     (while articles
4474       (when (inline (gnus-member-of-range (car articles) killed))
4475         (push (car articles) out))
4476       (setq articles (cdr articles)))
4477     out))
4478
4479 (defun gnus-uncompress-marks (marks)
4480   "Uncompress the mark ranges in MARKS."
4481   (let ((uncompressed '(score bookmark))
4482         out)
4483     (while marks
4484       (if (memq (caar marks) uncompressed)
4485           (push (car marks) out)
4486         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4487       (setq marks (cdr marks)))
4488     out))
4489
4490 (defun gnus-adjust-marked-articles (info)
4491   "Set all article lists and remove all marks that are no longer valid."
4492   (let* ((marked-lists (gnus-info-marks info))
4493          (active (gnus-active (gnus-info-group info)))
4494          (min (car active))
4495          (max (cdr active))
4496          (types gnus-article-mark-lists)
4497          (uncompressed '(score bookmark killed))
4498          marks var articles article mark)
4499
4500     (while marked-lists
4501       (setq marks (pop marked-lists))
4502       (set (setq var (intern (format "gnus-newsgroup-%s"
4503                                      (car (rassq (setq mark (car marks))
4504                                                  types)))))
4505            (if (memq (car marks) uncompressed) (cdr marks)
4506              (gnus-uncompress-range (cdr marks))))
4507
4508       (setq articles (symbol-value var))
4509
4510       ;; All articles have to be subsets of the active articles.
4511       (cond
4512        ;; Adjust "simple" lists.
4513        ((memq mark '(tick dormant expire reply save))
4514         (while articles
4515           (when (or (< (setq article (pop articles)) min) (> article max))
4516             (set var (delq article (symbol-value var))))))
4517        ;; Adjust assocs.
4518        ((memq mark uncompressed)
4519         (when (not (listp (cdr (symbol-value var))))
4520           (set var (list (symbol-value var))))
4521         (when (not (listp (cdr articles)))
4522           (setq articles (list articles)))
4523         (while articles
4524           (when (or (not (consp (setq article (pop articles))))
4525                     (< (car article) min)
4526                     (> (car article) max))
4527             (set var (delq article (symbol-value var))))))))))
4528
4529 (defun gnus-update-missing-marks (missing)
4530   "Go through the list of MISSING articles and remove them from the mark lists."
4531   (when missing
4532     (let ((types gnus-article-mark-lists)
4533           var m)
4534       ;; Go through all types.
4535       (while types
4536         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4537         (when (symbol-value var)
4538           ;; This list has articles.  So we delete all missing articles
4539           ;; from it.
4540           (setq m missing)
4541           (while m
4542             (set var (delq (pop m) (symbol-value var)))))))))
4543
4544 (defun gnus-update-marks ()
4545   "Enter the various lists of marked articles into the newsgroup info list."
4546   (let ((types gnus-article-mark-lists)
4547         (info (gnus-get-info gnus-newsgroup-name))
4548         (uncompressed '(score bookmark killed))
4549         type list newmarked symbol delta-marks)
4550     (when info
4551       ;; Add all marks lists to the list of marks lists.
4552       (while (setq type (pop types))
4553         (setq list (symbol-value
4554                     (setq symbol
4555                           (intern (format "gnus-newsgroup-%s"
4556                                           (car type))))))
4557
4558         (when list
4559           ;; Get rid of the entries of the articles that have the
4560           ;; default score.
4561           (when (and (eq (cdr type) 'score)
4562                      gnus-save-score
4563                      list)
4564             (let* ((arts list)
4565                    (prev (cons nil list))
4566                    (all prev))
4567               (while arts
4568                 (if (or (not (consp (car arts)))
4569                         (= (cdar arts) gnus-summary-default-score))
4570                     (setcdr prev (cdr arts))
4571                   (setq prev arts))
4572                 (setq arts (cdr arts)))
4573               (setq list (cdr all)))))
4574
4575         (unless (memq (cdr type) uncompressed)
4576           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4577        
4578         (when (gnus-check-backend-function
4579                'request-set-mark gnus-newsgroup-name)
4580           ;; propagate flags to server, with the following exceptions:
4581           ;; uncompressed:s are not proper flags (they are cons cells)
4582           ;; cache is a internal gnus flag
4583           ;; download are local to one gnus installation (well)
4584           ;; unsend are for nndraft groups only
4585           ;; xxx: generality of this?  this suits nnimap anyway
4586           (unless (memq (cdr type) (append '(cache download unsend)
4587                                            uncompressed))
4588             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4589                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4590                    (add (gnus-remove-from-range
4591                          (gnus-copy-sequence list) old)))
4592               (when add
4593                 (push (list add 'add (list (cdr type))) delta-marks))
4594               (when del
4595                 (push (list del 'del (list (cdr type))) delta-marks)))))
4596           
4597         (when list
4598           (push (cons (cdr type) list) newmarked)))
4599
4600       (when delta-marks
4601         (unless (gnus-check-group gnus-newsgroup-name)
4602           (error "Can't open server for %s" gnus-newsgroup-name))
4603         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4604           
4605       ;; Enter these new marks into the info of the group.
4606       (if (nthcdr 3 info)
4607           (setcar (nthcdr 3 info) newmarked)
4608         ;; Add the marks lists to the end of the info.
4609         (when newmarked
4610           (setcdr (nthcdr 2 info) (list newmarked))))
4611
4612       ;; Cut off the end of the info if there's nothing else there.
4613       (let ((i 5))
4614         (while (and (> i 2)
4615                     (not (nth i info)))
4616           (when (nthcdr (decf i) info)
4617             (setcdr (nthcdr i info) nil)))))))
4618
4619 (defun gnus-set-mode-line (where)
4620   "Set the mode line of the article or summary buffers.
4621 If WHERE is `summary', the summary mode line format will be used."
4622   ;; Is this mode line one we keep updated?
4623   (when (and (memq where gnus-updated-mode-lines)
4624              (symbol-value
4625               (intern (format "gnus-%s-mode-line-format-spec" where))))
4626     (let (mode-string)
4627       (save-excursion
4628         ;; We evaluate this in the summary buffer since these
4629         ;; variables are buffer-local to that buffer.
4630         (set-buffer gnus-summary-buffer)
4631         ;; We bind all these variables that are used in the `eval' form
4632         ;; below.
4633         (let* ((mformat (symbol-value
4634                          (intern
4635                           (format "gnus-%s-mode-line-format-spec" where))))
4636                (gnus-tmp-group-name (gnus-group-name-decode 
4637                                      gnus-newsgroup-name
4638                                      (gnus-group-name-charset 
4639                                       nil
4640                                       gnus-newsgroup-name)))
4641                (gnus-tmp-article-number (or gnus-current-article 0))
4642                (gnus-tmp-unread gnus-newsgroup-unreads)
4643                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4644                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4645                (gnus-tmp-unread-and-unselected
4646                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4647                             (zerop gnus-tmp-unselected))
4648                        "")
4649                       ((zerop gnus-tmp-unselected)
4650                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4651                       (t (format "{%d(+%d) more}"
4652                                  gnus-tmp-unread-and-unticked
4653                                  gnus-tmp-unselected))))
4654                (gnus-tmp-subject
4655                 (if (and gnus-current-headers
4656                          (vectorp gnus-current-headers))
4657                     (gnus-mode-string-quote
4658                      (mail-header-subject gnus-current-headers))
4659                   ""))
4660                bufname-length max-len
4661                gnus-tmp-header);; passed as argument to any user-format-funcs
4662           (setq mode-string (eval mformat))
4663           (setq bufname-length (if (string-match "%b" mode-string)
4664                                    (- (length
4665                                        (buffer-name
4666                                         (if (eq where 'summary)
4667                                             nil
4668                                           (get-buffer gnus-article-buffer))))
4669                                       2)
4670                                  0))
4671           (setq max-len (max 4 (if gnus-mode-non-string-length
4672                                    (- (window-width)
4673                                       gnus-mode-non-string-length
4674                                       bufname-length)
4675                                  (length mode-string))))
4676           ;; We might have to chop a bit of the string off...
4677           (when (> (length mode-string) max-len)
4678             (setq mode-string
4679                   (concat (truncate-string-to-width mode-string (- max-len 3))
4680                           "...")))
4681           ;; Pad the mode string a bit.
4682           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4683       ;; Update the mode line.
4684       (setq mode-line-buffer-identification
4685             (gnus-mode-line-buffer-identification (list mode-string)))
4686       (set-buffer-modified-p t))))
4687
4688 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4689   "Go through the HEADERS list and add all Xrefs to a hash table.
4690 The resulting hash table is returned, or nil if no Xrefs were found."
4691   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4692          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4693          (xref-hashtb (gnus-make-hashtable))
4694          start group entry number xrefs header)
4695     (while headers
4696       (setq header (pop headers))
4697       (when (and (setq xrefs (mail-header-xref header))
4698                  (not (memq (setq number (mail-header-number header))
4699                             unreads)))
4700         (setq start 0)
4701         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4702           (setq start (match-end 0))
4703           (setq group (if prefix
4704                           (concat prefix (substring xrefs (match-beginning 1)
4705                                                     (match-end 1)))
4706                         (substring xrefs (match-beginning 1) (match-end 1))))
4707           (setq number
4708                 (string-to-int (substring xrefs (match-beginning 2)
4709                                           (match-end 2))))
4710           (if (setq entry (gnus-gethash group xref-hashtb))
4711               (setcdr entry (cons number (cdr entry)))
4712             (gnus-sethash group (cons number nil) xref-hashtb)))))
4713     (and start xref-hashtb)))
4714
4715 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4716   "Look through all the headers and mark the Xrefs as read."
4717   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4718         name entry info xref-hashtb idlist method nth4)
4719     (save-excursion
4720       (set-buffer gnus-group-buffer)
4721       (when (setq xref-hashtb
4722                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4723         (mapatoms
4724          (lambda (group)
4725            (unless (string= from-newsgroup (setq name (symbol-name group)))
4726              (setq idlist (symbol-value group))
4727              ;; Dead groups are not updated.
4728              (and (prog1
4729                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4730                             info (nth 2 entry))
4731                     (when (stringp (setq nth4 (gnus-info-method info)))
4732                       (setq nth4 (gnus-server-to-method nth4))))
4733                   ;; Only do the xrefs if the group has the same
4734                   ;; select method as the group we have just read.
4735                   (or (gnus-methods-equal-p
4736                        nth4 (gnus-find-method-for-group from-newsgroup))
4737                       virtual
4738                       (equal nth4 (setq method (gnus-find-method-for-group
4739                                                 from-newsgroup)))
4740                       (and (equal (car nth4) (car method))
4741                            (equal (nth 1 nth4) (nth 1 method))))
4742                   gnus-use-cross-reference
4743                   (or (not (eq gnus-use-cross-reference t))
4744                       virtual
4745                       ;; Only do cross-references on subscribed
4746                       ;; groups, if that is what is wanted.
4747                       (<= (gnus-info-level info) gnus-level-subscribed))
4748                   (gnus-group-make-articles-read name idlist))))
4749          xref-hashtb)))))
4750
4751 (defun gnus-compute-read-articles (group articles)
4752   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4753          (info (nth 2 entry))
4754          (active (gnus-active group))
4755          ninfo)
4756     (when entry
4757       ;; First peel off all invalid article numbers.
4758       (when active
4759         (let ((ids articles)
4760               id first)
4761           (while (setq id (pop ids))
4762             (when (and first (> id (cdr active)))
4763               ;; We'll end up in this situation in one particular
4764               ;; obscure situation.  If you re-scan a group and get
4765               ;; a new article that is cross-posted to a different
4766               ;; group that has not been re-scanned, you might get
4767               ;; crossposted article that has a higher number than
4768               ;; Gnus believes possible.  So we re-activate this
4769               ;; group as well.  This might mean doing the
4770               ;; crossposting thingy will *increase* the number
4771               ;; of articles in some groups.  Tsk, tsk.
4772               (setq active (or (gnus-activate-group group) active)))
4773             (when (or (> id (cdr active))
4774                       (< id (car active)))
4775               (setq articles (delq id articles))))))
4776       ;; If the read list is nil, we init it.
4777       (if (and active
4778                (null (gnus-info-read info))
4779                (> (car active) 1))
4780           (setq ninfo (cons 1 (1- (car active))))
4781         (setq ninfo (gnus-info-read info)))
4782       ;; Then we add the read articles to the range.
4783       (gnus-add-to-range
4784        ninfo (setq articles (sort articles '<))))))
4785
4786 (defun gnus-group-make-articles-read (group articles)
4787   "Update the info of GROUP to say that ARTICLES are read."
4788   (let* ((num 0)
4789          (entry (gnus-gethash group gnus-newsrc-hashtb))
4790          (info (nth 2 entry))
4791          (active (gnus-active group))
4792          range)
4793     (when entry
4794       (setq range (gnus-compute-read-articles group articles))
4795       (save-excursion
4796         (set-buffer gnus-group-buffer)
4797         (gnus-undo-register
4798           `(progn
4799              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4800              (gnus-info-set-read ',info ',(gnus-info-read info))
4801              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4802              (gnus-group-update-group ,group t))))
4803       ;; Add the read articles to the range.
4804       (gnus-info-set-read info range)
4805       ;; Then we have to re-compute how many unread
4806       ;; articles there are in this group.
4807       (when active
4808         (cond
4809          ((not range)
4810           (setq num (- (1+ (cdr active)) (car active))))
4811          ((not (listp (cdr range)))
4812           (setq num (- (cdr active) (- (1+ (cdr range))
4813                                        (car range)))))
4814          (t
4815           (while range
4816             (if (numberp (car range))
4817                 (setq num (1+ num))
4818               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4819             (setq range (cdr range)))
4820           (setq num (- (cdr active) num))))
4821         ;; Update the number of unread articles.
4822         (setcar entry num)
4823         ;; Update the group buffer.
4824         (gnus-group-update-group group t)))))
4825
4826 (defvar gnus-newsgroup-none-id 0)
4827
4828 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4829   (let ((cur nntp-server-buffer)
4830         (dependencies
4831          (or dependencies
4832              (save-excursion (set-buffer gnus-summary-buffer)
4833                              gnus-newsgroup-dependencies)))
4834         headers id end ref
4835         (mail-parse-charset gnus-newsgroup-charset)
4836         (mail-parse-ignored-charsets
4837          (save-excursion (condition-case nil
4838                              (set-buffer gnus-summary-buffer)
4839                            (error))
4840                          gnus-newsgroup-ignored-charsets)))
4841     (save-excursion
4842       (set-buffer nntp-server-buffer)
4843       ;; Translate all TAB characters into SPACE characters.
4844       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4845       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4846       (gnus-run-hooks 'gnus-parse-headers-hook)
4847       (let ((case-fold-search t)
4848             in-reply-to header p lines chars)
4849         (goto-char (point-min))
4850         ;; Search to the beginning of the next header.  Error messages
4851         ;; do not begin with 2 or 3.
4852         (while (re-search-forward "^[23][0-9]+ " nil t)
4853           (setq id nil
4854                 ref nil)
4855           ;; This implementation of this function, with nine
4856           ;; search-forwards instead of the one re-search-forward and
4857           ;; a case (which basically was the old function) is actually
4858           ;; about twice as fast, even though it looks messier.  You
4859           ;; can't have everything, I guess.  Speed and elegance
4860           ;; doesn't always go hand in hand.
4861           (setq
4862            header
4863            (vector
4864             ;; Number.
4865             (prog1
4866                 (read cur)
4867               (end-of-line)
4868               (setq p (point))
4869               (narrow-to-region (point)
4870                                 (or (and (search-forward "\n.\n" nil t)
4871                                          (- (point) 2))
4872                                     (point))))
4873             ;; Subject.
4874             (progn
4875               (goto-char p)
4876               (if (search-forward "\nsubject: " nil t)
4877                   (funcall gnus-decode-encoded-word-function
4878                            (nnheader-header-value))
4879                 "(none)"))
4880             ;; From.
4881             (progn
4882               (goto-char p)
4883               (if (or (search-forward "\nfrom: " nil t)
4884                       (search-forward "\nfrom:" nil t))
4885                   (funcall gnus-decode-encoded-word-function
4886                            (nnheader-header-value))
4887                 "(nobody)"))
4888             ;; Date.
4889             (progn
4890               (goto-char p)
4891               (if (search-forward "\ndate: " nil t)
4892                   (nnheader-header-value) ""))
4893             ;; Message-ID.
4894             (progn
4895               (goto-char p)
4896               (setq id (if (re-search-forward
4897                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4898                            ;; We do it this way to make sure the Message-ID
4899                            ;; is (somewhat) syntactically valid.
4900                            (buffer-substring (match-beginning 1)
4901                                              (match-end 1))
4902                          ;; If there was no message-id, we just fake one
4903                          ;; to make subsequent routines simpler.
4904                          (nnheader-generate-fake-message-id))))
4905             ;; References.
4906             (progn
4907               (goto-char p)
4908               (if (search-forward "\nreferences: " nil t)
4909                   (progn
4910                     (setq end (point))
4911                     (prog1
4912                         (nnheader-header-value)
4913                       (setq ref
4914                             (buffer-substring
4915                              (progn
4916                                (end-of-line)
4917                                (search-backward ">" end t)
4918                                (1+ (point)))
4919                              (progn
4920                                (search-backward "<" end t)
4921                                (point))))))
4922                 ;; Get the references from the in-reply-to header if there
4923                 ;; were no references and the in-reply-to header looks
4924                 ;; promising.
4925                 (if (and (search-forward "\nin-reply-to: " nil t)
4926                          (setq in-reply-to (nnheader-header-value))
4927                          (string-match "<[^>]+>" in-reply-to))
4928                     (let (ref2)
4929                       (setq ref (substring in-reply-to (match-beginning 0)
4930                                            (match-end 0)))
4931                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4932                         (setq ref2 (substring in-reply-to (match-beginning 0)
4933                                               (match-end 0)))
4934                         (when (> (length ref2) (length ref))
4935                           (setq ref ref2)))
4936                       ref)
4937                   (setq ref nil))))
4938             ;; Chars.
4939             (progn
4940               (goto-char p)
4941               (if (search-forward "\nchars: " nil t)
4942                   (if (numberp (setq chars (ignore-errors (read cur))))
4943                       chars 0)
4944                 0))
4945             ;; Lines.
4946             (progn
4947               (goto-char p)
4948               (if (search-forward "\nlines: " nil t)
4949                   (if (numberp (setq lines (ignore-errors (read cur))))
4950                       lines 0)
4951                 0))
4952             ;; Xref.
4953             (progn
4954               (goto-char p)
4955               (and (search-forward "\nxref: " nil t)
4956                    (nnheader-header-value)))
4957             ;; Extra.
4958             (when gnus-extra-headers
4959               (let ((extra gnus-extra-headers)
4960                     out)
4961                 (while extra
4962                   (goto-char p)
4963                   (when (search-forward
4964                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4965                     (push (cons (car extra) (nnheader-header-value))
4966                           out))
4967                   (pop extra))
4968                 out))))
4969           (when (equal id ref)
4970             (setq ref nil))
4971
4972           (when gnus-alter-header-function
4973             (funcall gnus-alter-header-function header)
4974             (setq id (mail-header-id header)
4975                   ref (gnus-parent-id (mail-header-references header))))
4976
4977           (when (setq header
4978                       (gnus-dependencies-add-header
4979                        header dependencies force-new))
4980             (push header headers))
4981           (goto-char (point-max))
4982           (widen))
4983         (nreverse headers)))))
4984
4985 ;; Goes through the xover lines and returns a list of vectors
4986 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4987                                                   force-new dependencies
4988                                                   group also-fetch-heads)
4989   "Parse the news overview data in the server buffer.
4990 Return a list of headers that match SEQUENCE (see
4991 `nntp-retrieve-headers')."
4992   ;; Get the Xref when the users reads the articles since most/some
4993   ;; NNTP servers do not include Xrefs when using XOVER.
4994   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4995   (let ((mail-parse-charset gnus-newsgroup-charset)
4996         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4997         (cur nntp-server-buffer)
4998         (dependencies (or dependencies gnus-newsgroup-dependencies))
4999         number headers header)
5000     (save-excursion
5001       (set-buffer nntp-server-buffer)
5002       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5003       ;; Allow the user to mangle the headers before parsing them.
5004       (gnus-run-hooks 'gnus-parse-headers-hook)
5005       (goto-char (point-min))
5006       (while (not (eobp))
5007         (condition-case ()
5008             (while (and sequence (not (eobp)))
5009               (setq number (read cur))
5010               (while (and sequence
5011                           (< (car sequence) number))
5012                 (setq sequence (cdr sequence)))
5013               (and sequence
5014                    (eq number (car sequence))
5015                    (progn
5016                      (setq sequence (cdr sequence))
5017                      (setq header (inline
5018                                     (gnus-nov-parse-line
5019                                      number dependencies force-new))))
5020                    (push header headers))
5021               (forward-line 1))
5022           (error
5023            (gnus-error 4 "Strange nov line (%d)"
5024                        (count-lines (point-min) (point)))))
5025         (forward-line 1))
5026       ;; A common bug in inn is that if you have posted an article and
5027       ;; then retrieves the active file, it will answer correctly --
5028       ;; the new article is included.  However, a NOV entry for the
5029       ;; article may not have been generated yet, so this may fail.
5030       ;; We work around this problem by retrieving the last few
5031       ;; headers using HEAD.
5032       (if (or (not also-fetch-heads)
5033               (not sequence))
5034           ;; We (probably) got all the headers.
5035           (nreverse headers)
5036         (let ((gnus-nov-is-evil t))
5037           (nconc
5038            (nreverse headers)
5039            (when (gnus-retrieve-headers sequence group)
5040              (gnus-get-newsgroup-headers))))))))
5041
5042 (defun gnus-article-get-xrefs ()
5043   "Fill in the Xref value in `gnus-current-headers', if necessary.
5044 This is meant to be called in `gnus-article-internal-prepare-hook'."
5045   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5046                                  gnus-current-headers)))
5047     (or (not gnus-use-cross-reference)
5048         (not headers)
5049         (and (mail-header-xref headers)
5050              (not (string= (mail-header-xref headers) "")))
5051         (let ((case-fold-search t)
5052               xref)
5053           (save-restriction
5054             (nnheader-narrow-to-headers)
5055             (goto-char (point-min))
5056             (when (or (and (not (eobp))
5057                            (eq (downcase (char-after)) ?x)
5058                            (looking-at "Xref:"))
5059                       (search-forward "\nXref:" nil t))
5060               (goto-char (1+ (match-end 0)))
5061               (setq xref (buffer-substring (point)
5062                                            (progn (end-of-line) (point))))
5063               (mail-header-set-xref headers xref)))))))
5064
5065 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5066   "Find article ID and insert the summary line for that article.
5067 OLD-HEADER can either be a header or a line number to insert
5068 the subject line on."
5069   (let* ((line (and (numberp old-header) old-header))
5070          (old-header (and (vectorp old-header) old-header))
5071          (header (cond ((and old-header use-old-header)
5072                         old-header)
5073                        ((and (numberp id)
5074                              (gnus-number-to-header id))
5075                         (gnus-number-to-header id))
5076                        (t
5077                         (gnus-read-header id))))
5078          (number (and (numberp id) id))
5079          d)
5080     (when header
5081       ;; Rebuild the thread that this article is part of and go to the
5082       ;; article we have fetched.
5083       (when (and (not gnus-show-threads)
5084                  old-header)
5085         (when (and number
5086                    (setq d (gnus-data-find (mail-header-number old-header))))
5087           (goto-char (gnus-data-pos d))
5088           (gnus-data-remove
5089            number
5090            (- (gnus-point-at-bol)
5091               (prog1
5092                   (1+ (gnus-point-at-eol))
5093                 (gnus-delete-line))))))
5094       (when old-header
5095         (mail-header-set-number header (mail-header-number old-header)))
5096       (setq gnus-newsgroup-sparse
5097             (delq (setq number (mail-header-number header))
5098                   gnus-newsgroup-sparse))
5099       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5100       (push number gnus-newsgroup-limit)
5101       (gnus-rebuild-thread (mail-header-id header) line)
5102       (gnus-summary-goto-subject number nil t))
5103     (when (and (numberp number)
5104                (> number 0))
5105       ;; We have to update the boundaries even if we can't fetch the
5106       ;; article if ID is a number -- so that the next `P' or `N'
5107       ;; command will fetch the previous (or next) article even
5108       ;; if the one we tried to fetch this time has been canceled.
5109       (when (> number gnus-newsgroup-end)
5110         (setq gnus-newsgroup-end number))
5111       (when (< number gnus-newsgroup-begin)
5112         (setq gnus-newsgroup-begin number))
5113       (setq gnus-newsgroup-unselected
5114             (delq number gnus-newsgroup-unselected)))
5115     ;; Report back a success?
5116     (and header (mail-header-number header))))
5117
5118 ;;; Process/prefix in the summary buffer
5119
5120 (defun gnus-summary-work-articles (n)
5121   "Return a list of articles to be worked upon.
5122 The prefix argument, the list of process marked articles, and the
5123 current article will be taken into consideration."
5124   (save-excursion
5125     (set-buffer gnus-summary-buffer)
5126     (cond
5127      (n
5128       ;; A numerical prefix has been given.
5129       (setq n (prefix-numeric-value n))
5130       (let ((backward (< n 0))
5131             (n (abs (prefix-numeric-value n)))
5132             articles article)
5133         (save-excursion
5134           (while
5135               (and (> n 0)
5136                    (push (setq article (gnus-summary-article-number))
5137                          articles)
5138                    (if backward
5139                        (gnus-summary-find-prev nil article)
5140                      (gnus-summary-find-next nil article)))
5141             (decf n)))
5142         (nreverse articles)))
5143      ((and (gnus-region-active-p) (mark))
5144       (message "region active")
5145       ;; Work on the region between point and mark.
5146       (let ((max (max (point) (mark)))
5147             articles article)
5148         (save-excursion
5149           (goto-char (min (min (point) (mark))))
5150           (while
5151               (and
5152                (push (setq article (gnus-summary-article-number)) articles)
5153                (gnus-summary-find-next nil article)
5154                (< (point) max)))
5155           (nreverse articles))))
5156      (gnus-newsgroup-processable
5157       ;; There are process-marked articles present.
5158       ;; Save current state.
5159       (gnus-summary-save-process-mark)
5160       ;; Return the list.
5161       (reverse gnus-newsgroup-processable))
5162      (t
5163       ;; Just return the current article.
5164       (list (gnus-summary-article-number))))))
5165
5166 (defmacro gnus-summary-iterate (arg &rest forms)
5167   "Iterate over the process/prefixed articles and do FORMS.
5168 ARG is the interactive prefix given to the command.  FORMS will be
5169 executed with point over the summary line of the articles."
5170   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5171     `(let ((,articles (gnus-summary-work-articles ,arg)))
5172        (while ,articles
5173          (gnus-summary-goto-subject (car ,articles))
5174          ,@forms
5175          (pop ,articles)))))
5176
5177 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5178 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5179
5180 (defun gnus-summary-save-process-mark ()
5181   "Push the current set of process marked articles on the stack."
5182   (interactive)
5183   (push (copy-sequence gnus-newsgroup-processable)
5184         gnus-newsgroup-process-stack))
5185
5186 (defun gnus-summary-kill-process-mark ()
5187   "Push the current set of process marked articles on the stack and unmark."
5188   (interactive)
5189   (gnus-summary-save-process-mark)
5190   (gnus-summary-unmark-all-processable))
5191
5192 (defun gnus-summary-yank-process-mark ()
5193   "Pop the last process mark state off the stack and restore it."
5194   (interactive)
5195   (unless gnus-newsgroup-process-stack
5196     (error "Empty mark stack"))
5197   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5198
5199 (defun gnus-summary-process-mark-set (set)
5200   "Make SET into the current process marked articles."
5201   (gnus-summary-unmark-all-processable)
5202   (while set
5203     (gnus-summary-set-process-mark (pop set))))
5204
5205 ;;; Searching and stuff
5206
5207 (defun gnus-summary-search-group (&optional backward use-level)
5208   "Search for next unread newsgroup.
5209 If optional argument BACKWARD is non-nil, search backward instead."
5210   (save-excursion
5211     (set-buffer gnus-group-buffer)
5212     (when (gnus-group-search-forward
5213            backward nil (if use-level (gnus-group-group-level) nil))
5214       (gnus-group-group-name))))
5215
5216 (defun gnus-summary-best-group (&optional exclude-group)
5217   "Find the name of the best unread group.
5218 If EXCLUDE-GROUP, do not go to this group."
5219   (save-excursion
5220     (set-buffer gnus-group-buffer)
5221     (save-excursion
5222       (gnus-group-best-unread-group exclude-group))))
5223
5224 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5225   (if backward (gnus-summary-find-prev)
5226     (let* ((dummy (gnus-summary-article-intangible-p))
5227            (article (or article (gnus-summary-article-number)))
5228            (arts (gnus-data-find-list article))
5229            result)
5230       (when (and (not dummy)
5231                  (or (not gnus-summary-check-current)
5232                      (not unread)
5233                      (not (gnus-data-unread-p (car arts)))))
5234         (setq arts (cdr arts)))
5235       (when (setq result
5236                   (if unread
5237                       (progn
5238                         (while arts
5239                           (when (or (and undownloaded
5240                                          (eq gnus-undownloaded-mark
5241                                              (gnus-data-mark (car arts))))
5242                                     (gnus-data-unread-p (car arts)))
5243                             (setq result (car arts)
5244                                   arts nil))
5245                           (setq arts (cdr arts)))
5246                         result)
5247                     (car arts)))
5248         (goto-char (gnus-data-pos result))
5249         (gnus-data-number result)))))
5250
5251 (defun gnus-summary-find-prev (&optional unread article)
5252   (let* ((eobp (eobp))
5253          (article (or article (gnus-summary-article-number)))
5254          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5255          result)
5256     (when (and (not eobp)
5257                (or (not gnus-summary-check-current)
5258                    (not unread)
5259                    (not (gnus-data-unread-p (car arts)))))
5260       (setq arts (cdr arts)))
5261     (when (setq result
5262                 (if unread
5263                     (progn
5264                       (while arts
5265                         (when (gnus-data-unread-p (car arts))
5266                           (setq result (car arts)
5267                                 arts nil))
5268                         (setq arts (cdr arts)))
5269                       result)
5270                   (car arts)))
5271       (goto-char (gnus-data-pos result))
5272       (gnus-data-number result))))
5273
5274 (defun gnus-summary-find-subject (subject &optional unread backward article)
5275   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5276          (article (or article (gnus-summary-article-number)))
5277          (articles (gnus-data-list backward))
5278          (arts (gnus-data-find-list article articles))
5279          result)
5280     (when (or (not gnus-summary-check-current)
5281               (not unread)
5282               (not (gnus-data-unread-p (car arts))))
5283       (setq arts (cdr arts)))
5284     (while arts
5285       (and (or (not unread)
5286                (gnus-data-unread-p (car arts)))
5287            (vectorp (gnus-data-header (car arts)))
5288            (gnus-subject-equal
5289             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5290            (setq result (car arts)
5291                  arts nil))
5292       (setq arts (cdr arts)))
5293     (and result
5294          (goto-char (gnus-data-pos result))
5295          (gnus-data-number result))))
5296
5297 (defun gnus-summary-search-forward (&optional unread subject backward)
5298   "Search forward for an article.
5299 If UNREAD, look for unread articles.  If SUBJECT, look for
5300 articles with that subject.  If BACKWARD, search backward instead."
5301   (cond (subject (gnus-summary-find-subject subject unread backward))
5302         (backward (gnus-summary-find-prev unread))
5303         (t (gnus-summary-find-next unread))))
5304
5305 (defun gnus-recenter (&optional n)
5306   "Center point in window and redisplay frame.
5307 Also do horizontal recentering."
5308   (interactive "P")
5309   (when (and gnus-auto-center-summary
5310              (not (eq gnus-auto-center-summary 'vertical)))
5311     (gnus-horizontal-recenter))
5312   (recenter n))
5313
5314 (defun gnus-summary-recenter ()
5315   "Center point in the summary window.
5316 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5317 displayed, no centering will be performed."
5318   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5319   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5320   (interactive)
5321   (let* ((top (cond ((< (window-height) 4) 0)
5322                     ((< (window-height) 7) 1)
5323                     (t (if (numberp gnus-auto-center-summary)
5324                            gnus-auto-center-summary
5325                          2))))
5326          (height (1- (window-height)))
5327          (bottom (save-excursion (goto-char (point-max))
5328                                  (forward-line (- height))
5329                                  (point)))
5330          (window (get-buffer-window (current-buffer))))
5331     ;; The user has to want it.
5332     (when gnus-auto-center-summary
5333       (when (get-buffer-window gnus-article-buffer)
5334         ;; Only do recentering when the article buffer is displayed,
5335         ;; Set the window start to either `bottom', which is the biggest
5336         ;; possible valid number, or the second line from the top,
5337         ;; whichever is the least.
5338         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5339           (if (> bottom top-pos)
5340               ;; Keep the second line from the top visible
5341               (set-window-start window top-pos t)
5342             ;; Try to keep the bottom line visible; if it's partially
5343             ;; obscured, either scroll one more line to make it fully
5344             ;; visible, or revert to using TOP-POS.
5345             (save-excursion
5346               (goto-char (point-max))
5347               (forward-line -1)
5348               (let ((last-line-start (point)))
5349                 (goto-char bottom)
5350                 (set-window-start window (point) t)
5351                 (when (not (pos-visible-in-window-p last-line-start window))
5352                   (forward-line 1)
5353                   (set-window-start window (min (point) top-pos) t)))))))
5354       ;; Do horizontal recentering while we're at it.
5355       (when (and (get-buffer-window (current-buffer) t)
5356                  (not (eq gnus-auto-center-summary 'vertical)))
5357         (let ((selected (selected-window)))
5358           (select-window (get-buffer-window (current-buffer) t))
5359           (gnus-summary-position-point)
5360           (gnus-horizontal-recenter)
5361           (select-window selected))))))
5362
5363 (defun gnus-summary-jump-to-group (newsgroup)
5364   "Move point to NEWSGROUP in group mode buffer."
5365   ;; Keep update point of group mode buffer if visible.
5366   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5367       (save-window-excursion
5368         ;; Take care of tree window mode.
5369         (when (get-buffer-window gnus-group-buffer)
5370           (pop-to-buffer gnus-group-buffer))
5371         (gnus-group-jump-to-group newsgroup))
5372     (save-excursion
5373       ;; Take care of tree window mode.
5374       (if (get-buffer-window gnus-group-buffer)
5375           (pop-to-buffer gnus-group-buffer)
5376         (set-buffer gnus-group-buffer))
5377       (gnus-group-jump-to-group newsgroup))))
5378
5379 ;; This function returns a list of article numbers based on the
5380 ;; difference between the ranges of read articles in this group and
5381 ;; the range of active articles.
5382 (defun gnus-list-of-unread-articles (group)
5383   (let* ((read (gnus-info-read (gnus-get-info group)))
5384          (active (or (gnus-active group) (gnus-activate-group group)))
5385          (last (cdr active))
5386          first nlast unread)
5387     ;; If none are read, then all are unread.
5388     (if (not read)
5389         (setq first (car active))
5390       ;; If the range of read articles is a single range, then the
5391       ;; first unread article is the article after the last read
5392       ;; article.  Sounds logical, doesn't it?
5393       (if (and (not (listp (cdr read)))
5394                (or (< (car read) (car active))
5395                    (progn (setq read (list read))
5396                           nil)))
5397           (setq first (max (car active) (1+ (cdr read))))
5398         ;; `read' is a list of ranges.
5399         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5400                                   (caar read)))
5401                   1)
5402           (setq first (car active)))
5403         (while read
5404           (when first
5405             (while (< first nlast)
5406               (push first unread)
5407               (setq first (1+ first))))
5408           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5409           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5410           (setq read (cdr read)))))
5411     ;; And add the last unread articles.
5412     (while (<= first last)
5413       (push first unread)
5414       (setq first (1+ first)))
5415     ;; Return the list of unread articles.
5416     (delq 0 (nreverse unread))))
5417
5418 (defun gnus-list-of-read-articles (group)
5419   "Return a list of unread, unticked and non-dormant articles."
5420   (let* ((info (gnus-get-info group))
5421          (marked (gnus-info-marks info))
5422          (active (gnus-active group)))
5423     (and info active
5424          (gnus-set-difference
5425           (gnus-sorted-complement
5426            (gnus-uncompress-range active)
5427            (gnus-list-of-unread-articles group))
5428           (append
5429            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5430            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5431
5432 ;; Various summary commands
5433
5434 (defun gnus-summary-select-article-buffer ()
5435   "Reconfigure windows to show article buffer."
5436   (interactive)
5437   (if (not (gnus-buffer-live-p gnus-article-buffer))
5438       (error "There is no article buffer for this summary buffer")
5439     (gnus-configure-windows 'article)
5440     (select-window (get-buffer-window gnus-article-buffer))))
5441
5442 (defun gnus-summary-universal-argument (arg)
5443   "Perform any operation on all articles that are process/prefixed."
5444   (interactive "P")
5445   (let ((articles (gnus-summary-work-articles arg))
5446         func article)
5447     (if (eq
5448          (setq
5449           func
5450           (key-binding
5451            (read-key-sequence
5452             (substitute-command-keys
5453              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5454          'undefined)
5455         (gnus-error 1 "Undefined key")
5456       (save-excursion
5457         (while articles
5458           (gnus-summary-goto-subject (setq article (pop articles)))
5459           (let (gnus-newsgroup-processable)
5460             (command-execute func))
5461           (gnus-summary-remove-process-mark article)))))
5462   (gnus-summary-position-point))
5463
5464 (defun gnus-summary-toggle-truncation (&optional arg)
5465   "Toggle truncation of summary lines.
5466 With arg, turn line truncation on iff arg is positive."
5467   (interactive "P")
5468   (setq truncate-lines
5469         (if (null arg) (not truncate-lines)
5470           (> (prefix-numeric-value arg) 0)))
5471   (redraw-display))
5472
5473 (defun gnus-summary-reselect-current-group (&optional all rescan)
5474   "Exit and then reselect the current newsgroup.
5475 The prefix argument ALL means to select all articles."
5476   (interactive "P")
5477   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5478     (error "Ephemeral groups can't be reselected"))
5479   (let ((current-subject (gnus-summary-article-number))
5480         (group gnus-newsgroup-name))
5481     (setq gnus-newsgroup-begin nil)
5482     (gnus-summary-exit)
5483     ;; We have to adjust the point of group mode buffer because
5484     ;; point was moved to the next unread newsgroup by exiting.
5485     (gnus-summary-jump-to-group group)
5486     (when rescan
5487       (save-excursion
5488         (gnus-group-get-new-news-this-group 1)))
5489     (gnus-group-read-group all t)
5490     (gnus-summary-goto-subject current-subject nil t)))
5491
5492 (defun gnus-summary-rescan-group (&optional all)
5493   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5494   (interactive "P")
5495   (gnus-summary-reselect-current-group all t))
5496
5497 (defun gnus-summary-update-info (&optional non-destructive)
5498   (save-excursion
5499     (let ((group gnus-newsgroup-name))
5500       (when group
5501         (when gnus-newsgroup-kill-headers
5502           (setq gnus-newsgroup-killed
5503                 (gnus-compress-sequence
5504                  (nconc
5505                   (gnus-set-sorted-intersection
5506                    (gnus-uncompress-range gnus-newsgroup-killed)
5507                    (setq gnus-newsgroup-unselected
5508                          (sort gnus-newsgroup-unselected '<)))
5509                   (setq gnus-newsgroup-unreads
5510                         (sort gnus-newsgroup-unreads '<)))
5511                  t)))
5512         (unless (listp (cdr gnus-newsgroup-killed))
5513           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5514         (let ((headers gnus-newsgroup-headers))
5515           ;; Set the new ranges of read articles.
5516           (save-excursion
5517             (set-buffer gnus-group-buffer)
5518             (gnus-undo-force-boundary))
5519           (gnus-update-read-articles
5520            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5521           ;; Set the current article marks.
5522           (let ((gnus-newsgroup-scored
5523                  (if (and (not gnus-save-score)
5524                           (not non-destructive))
5525                      nil
5526                    gnus-newsgroup-scored)))
5527             (save-excursion
5528               (gnus-update-marks)))
5529           ;; Do the cross-ref thing.
5530           (when gnus-use-cross-reference
5531             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5532           ;; Do not switch windows but change the buffer to work.
5533           (set-buffer gnus-group-buffer)
5534           (unless (gnus-ephemeral-group-p group)
5535             (gnus-group-update-group group)))))))
5536
5537 (defun gnus-summary-save-newsrc (&optional force)
5538   "Save the current number of read/marked articles in the dribble buffer.
5539 The dribble buffer will then be saved.
5540 If FORCE (the prefix), also save the .newsrc file(s)."
5541   (interactive "P")
5542   (gnus-summary-update-info t)
5543   (if force
5544       (gnus-save-newsrc-file)
5545     (gnus-dribble-save)))
5546
5547 (defun gnus-summary-exit (&optional temporary)
5548   "Exit reading current newsgroup, and then return to group selection mode.
5549 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5550   (interactive)
5551   (gnus-set-global-variables)
5552   (when (gnus-buffer-live-p gnus-article-buffer)
5553     (save-excursion
5554       (set-buffer gnus-article-buffer)
5555       (mm-destroy-parts gnus-article-mime-handles)
5556       ;; Set it to nil for safety reason.
5557       (setq gnus-article-mime-handle-alist nil)
5558       (setq gnus-article-mime-handles nil)))
5559   (gnus-kill-save-kill-buffer)
5560   (gnus-async-halt-prefetch)
5561   (let* ((group gnus-newsgroup-name)
5562          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5563          (mode major-mode)
5564          (group-point nil)
5565          (buf (current-buffer)))
5566     (unless quit-config
5567       ;; Do adaptive scoring, and possibly save score files.
5568       (when gnus-newsgroup-adaptive
5569         (gnus-score-adaptive))
5570       (when gnus-use-scoring
5571         (gnus-score-save)))
5572     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5573     ;; If we have several article buffers, we kill them at exit.
5574     (unless gnus-single-article-buffer
5575       (gnus-kill-buffer gnus-original-article-buffer)
5576       (setq gnus-article-current nil))
5577     (when gnus-use-cache
5578       (gnus-cache-possibly-remove-articles)
5579       (gnus-cache-save-buffers))
5580     (gnus-async-prefetch-remove-group group)
5581     (when gnus-suppress-duplicates
5582       (gnus-dup-enter-articles))
5583     (when gnus-use-trees
5584       (gnus-tree-close group))
5585     (when gnus-use-cache
5586       (gnus-cache-write-active))
5587     ;; Remove entries for this group.
5588     (nnmail-purge-split-history (gnus-group-real-name group))
5589     ;; Make all changes in this group permanent.
5590     (unless quit-config
5591       (gnus-run-hooks 'gnus-exit-group-hook)
5592       (gnus-summary-update-info))
5593     (gnus-close-group group)
5594     ;; Make sure where we were, and go to next newsgroup.
5595     (set-buffer gnus-group-buffer)
5596     (unless quit-config
5597       (gnus-group-jump-to-group group))
5598     (gnus-run-hooks 'gnus-summary-exit-hook)
5599     (unless (or quit-config
5600                 ;; If this group has disappeared from the summary
5601                 ;; buffer, don't skip forwards.
5602                 (not (string= group (gnus-group-group-name))))
5603       (gnus-group-next-unread-group 1))
5604     (setq group-point (point))
5605     (if temporary
5606         nil                             ;Nothing to do.
5607       ;; If we have several article buffers, we kill them at exit.
5608       (unless gnus-single-article-buffer
5609         (gnus-kill-buffer gnus-article-buffer)
5610         (gnus-kill-buffer gnus-original-article-buffer)
5611         (setq gnus-article-current nil))
5612       (set-buffer buf)
5613       (if (not gnus-kill-summary-on-exit)
5614           (gnus-deaden-summary)
5615         ;; We set all buffer-local variables to nil.  It is unclear why
5616         ;; this is needed, but if we don't, buffer-local variables are
5617         ;; not garbage-collected, it seems.  This would the lead to en
5618         ;; ever-growing Emacs.
5619         (gnus-summary-clear-local-variables)
5620         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5621           (gnus-summary-clear-local-variables))
5622         (when (get-buffer gnus-article-buffer)
5623           (bury-buffer gnus-article-buffer))
5624         ;; We clear the global counterparts of the buffer-local
5625         ;; variables as well, just to be on the safe side.
5626         (set-buffer gnus-group-buffer)
5627         (gnus-summary-clear-local-variables)
5628         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5629           (gnus-summary-clear-local-variables))
5630         ;; Return to group mode buffer.
5631         (when (eq mode 'gnus-summary-mode)
5632           (gnus-kill-buffer buf)))
5633       (setq gnus-current-select-method gnus-select-method)
5634       (pop-to-buffer gnus-group-buffer)
5635       (if (not quit-config)
5636           (progn
5637             (goto-char group-point)
5638             (gnus-configure-windows 'group 'force))
5639         (gnus-handle-ephemeral-exit quit-config))
5640       ;; Clear the current group name.
5641       (unless quit-config
5642         (setq gnus-newsgroup-name nil)))))
5643
5644 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5645 (defun gnus-summary-exit-no-update (&optional no-questions)
5646   "Quit reading current newsgroup without updating read article info."
5647   (interactive)
5648   (let* ((group gnus-newsgroup-name)
5649          (quit-config (gnus-group-quit-config group)))
5650     (when (or no-questions
5651               gnus-expert-user
5652               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5653       (gnus-async-halt-prefetch)
5654       (mapcar 'funcall
5655               (delq 'gnus-summary-expire-articles
5656                     (copy-sequence gnus-summary-prepare-exit-hook)))
5657       (when (gnus-buffer-live-p gnus-article-buffer)
5658         (save-excursion
5659           (set-buffer gnus-article-buffer)
5660           (mm-destroy-parts gnus-article-mime-handles)
5661           ;; Set it to nil for safety reason.
5662           (setq gnus-article-mime-handle-alist nil)
5663           (setq gnus-article-mime-handles nil)))
5664       ;; If we have several article buffers, we kill them at exit.
5665       (unless gnus-single-article-buffer
5666         (gnus-kill-buffer gnus-article-buffer)
5667         (gnus-kill-buffer gnus-original-article-buffer)
5668         (setq gnus-article-current nil))
5669       (if (not gnus-kill-summary-on-exit)
5670           (gnus-deaden-summary)
5671         (gnus-close-group group)
5672         (gnus-summary-clear-local-variables)
5673         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5674           (gnus-summary-clear-local-variables))
5675         (set-buffer gnus-group-buffer)
5676         (gnus-summary-clear-local-variables)
5677         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5678           (gnus-summary-clear-local-variables))
5679         (when (get-buffer gnus-summary-buffer)
5680           (kill-buffer gnus-summary-buffer)))
5681       (unless gnus-single-article-buffer
5682         (setq gnus-article-current nil))
5683       (when gnus-use-trees
5684         (gnus-tree-close group))
5685       (gnus-async-prefetch-remove-group group)
5686       (when (get-buffer gnus-article-buffer)
5687         (bury-buffer gnus-article-buffer))
5688       ;; Return to the group buffer.
5689       (gnus-configure-windows 'group 'force)
5690       ;; Clear the current group name.
5691       (setq gnus-newsgroup-name nil)
5692       (when (equal (gnus-group-group-name) group)
5693         (gnus-group-next-unread-group 1))
5694       (when quit-config
5695         (gnus-handle-ephemeral-exit quit-config)))))
5696
5697 (defun gnus-handle-ephemeral-exit (quit-config)
5698   "Handle movement when leaving an ephemeral group.
5699 The state which existed when entering the ephemeral is reset."
5700   (if (not (buffer-name (car quit-config)))
5701       (gnus-configure-windows 'group 'force)
5702     (set-buffer (car quit-config))
5703     (cond ((eq major-mode 'gnus-summary-mode)
5704            (gnus-set-global-variables))
5705           ((eq major-mode 'gnus-article-mode)
5706            (save-excursion
5707              ;; The `gnus-summary-buffer' variable may point
5708              ;; to the old summary buffer when using a single
5709              ;; article buffer.
5710              (unless (gnus-buffer-live-p gnus-summary-buffer)
5711                (set-buffer gnus-group-buffer))
5712              (set-buffer gnus-summary-buffer)
5713              (gnus-set-global-variables))))
5714     (if (or (eq (cdr quit-config) 'article)
5715             (eq (cdr quit-config) 'pick))
5716         (progn
5717           ;; The current article may be from the ephemeral group
5718           ;; thus it is best that we reload this article
5719           (gnus-summary-show-article)
5720           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5721               (gnus-configure-windows 'pick 'force)
5722             (gnus-configure-windows (cdr quit-config) 'force)))
5723       (gnus-configure-windows (cdr quit-config) 'force))
5724     (when (eq major-mode 'gnus-summary-mode)
5725       (gnus-summary-next-subject 1 nil t)
5726       (gnus-summary-recenter)
5727       (gnus-summary-position-point))))
5728
5729 ;;; Dead summaries.
5730
5731 (defvar gnus-dead-summary-mode-map nil)
5732
5733 (unless gnus-dead-summary-mode-map
5734   (setq gnus-dead-summary-mode-map (make-keymap))
5735   (suppress-keymap gnus-dead-summary-mode-map)
5736   (substitute-key-definition
5737    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5738   (let ((keys '("\C-d" "\r" "\177" [delete])))
5739     (while keys
5740       (define-key gnus-dead-summary-mode-map
5741         (pop keys) 'gnus-summary-wake-up-the-dead))))
5742
5743 (defvar gnus-dead-summary-mode nil
5744   "Minor mode for Gnus summary buffers.")
5745
5746 (defun gnus-dead-summary-mode (&optional arg)
5747   "Minor mode for Gnus summary buffers."
5748   (interactive "P")
5749   (when (eq major-mode 'gnus-summary-mode)
5750     (make-local-variable 'gnus-dead-summary-mode)
5751     (setq gnus-dead-summary-mode
5752           (if (null arg) (not gnus-dead-summary-mode)
5753             (> (prefix-numeric-value arg) 0)))
5754     (when gnus-dead-summary-mode
5755       (gnus-add-minor-mode
5756        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5757
5758 (defun gnus-deaden-summary ()
5759   "Make the current summary buffer into a dead summary buffer."
5760   ;; Kill any previous dead summary buffer.
5761   (when (and gnus-dead-summary
5762              (buffer-name gnus-dead-summary))
5763     (save-excursion
5764       (set-buffer gnus-dead-summary)
5765       (when gnus-dead-summary-mode
5766         (kill-buffer (current-buffer)))))
5767   ;; Make this the current dead summary.
5768   (setq gnus-dead-summary (current-buffer))
5769   (gnus-dead-summary-mode 1)
5770   (let ((name (buffer-name)))
5771     (when (string-match "Summary" name)
5772       (rename-buffer
5773        (concat (substring name 0 (match-beginning 0)) "Dead "
5774                (substring name (match-beginning 0)))
5775        t)
5776       (bury-buffer))))
5777
5778 (defun gnus-kill-or-deaden-summary (buffer)
5779   "Kill or deaden the summary BUFFER."
5780   (save-excursion
5781     (when (and (buffer-name buffer)
5782                (not gnus-single-article-buffer))
5783       (save-excursion
5784         (set-buffer buffer)
5785         (gnus-kill-buffer gnus-article-buffer)
5786         (gnus-kill-buffer gnus-original-article-buffer)))
5787     (cond (gnus-kill-summary-on-exit
5788            (when (and gnus-use-trees
5789                       (gnus-buffer-exists-p buffer))
5790              (save-excursion
5791                (set-buffer buffer)
5792                (gnus-tree-close gnus-newsgroup-name)))
5793            (gnus-kill-buffer buffer))
5794           ((gnus-buffer-exists-p buffer)
5795            (save-excursion
5796              (set-buffer buffer)
5797              (gnus-deaden-summary))))))
5798
5799 (defun gnus-summary-wake-up-the-dead (&rest args)
5800   "Wake up the dead summary buffer."
5801   (interactive)
5802   (gnus-dead-summary-mode -1)
5803   (let ((name (buffer-name)))
5804     (when (string-match "Dead " name)
5805       (rename-buffer
5806        (concat (substring name 0 (match-beginning 0))
5807                (substring name (match-end 0)))
5808        t)))
5809   (gnus-message 3 "This dead summary is now alive again"))
5810
5811 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5812 (defun gnus-summary-fetch-faq (&optional faq-dir)
5813   "Fetch the FAQ for the current group.
5814 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5815 in."
5816   (interactive
5817    (list
5818     (when current-prefix-arg
5819       (completing-read
5820        "Faq dir: " (and (listp gnus-group-faq-directory)
5821                         (mapcar (lambda (file) (list file))
5822                                 gnus-group-faq-directory))))))
5823   (let (gnus-faq-buffer)
5824     (when (setq gnus-faq-buffer
5825                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5826       (gnus-configure-windows 'summary-faq))))
5827
5828 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5829 (defun gnus-summary-describe-group (&optional force)
5830   "Describe the current newsgroup."
5831   (interactive "P")
5832   (gnus-group-describe-group force gnus-newsgroup-name))
5833
5834 (defun gnus-summary-describe-briefly ()
5835   "Describe summary mode commands briefly."
5836   (interactive)
5837   (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")))
5838
5839 ;; Walking around group mode buffer from summary mode.
5840
5841 (defun gnus-summary-next-group (&optional no-article target-group backward)
5842   "Exit current newsgroup and then select next unread newsgroup.
5843 If prefix argument NO-ARTICLE is non-nil, no article is selected
5844 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5845 previous group instead."
5846   (interactive "P")
5847   ;; Stop pre-fetching.
5848   (gnus-async-halt-prefetch)
5849   (let ((current-group gnus-newsgroup-name)
5850         (current-buffer (current-buffer))
5851         entered)
5852     ;; First we semi-exit this group to update Xrefs and all variables.
5853     ;; We can't do a real exit, because the window conf must remain
5854     ;; the same in case the user is prompted for info, and we don't
5855     ;; want the window conf to change before that...
5856     (gnus-summary-exit t)
5857     (while (not entered)
5858       ;; Then we find what group we are supposed to enter.
5859       (set-buffer gnus-group-buffer)
5860       (gnus-group-jump-to-group current-group)
5861       (setq target-group
5862             (or target-group
5863                 (if (eq gnus-keep-same-level 'best)
5864                     (gnus-summary-best-group gnus-newsgroup-name)
5865                   (gnus-summary-search-group backward gnus-keep-same-level))))
5866       (if (not target-group)
5867           ;; There are no further groups, so we return to the group
5868           ;; buffer.
5869           (progn
5870             (gnus-message 5 "Returning to the group buffer")
5871             (setq entered t)
5872             (when (gnus-buffer-live-p current-buffer)
5873               (set-buffer current-buffer)
5874               (gnus-summary-exit))
5875             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5876         ;; We try to enter the target group.
5877         (gnus-group-jump-to-group target-group)
5878         (let ((unreads (gnus-group-group-unread)))
5879           (if (and (or (eq t unreads)
5880                        (and unreads (not (zerop unreads))))
5881                    (gnus-summary-read-group
5882                     target-group nil no-article
5883                     (and (buffer-name current-buffer) current-buffer)
5884                     nil backward))
5885               (setq entered t)
5886             (setq current-group target-group
5887                   target-group nil)))))))
5888
5889 (defun gnus-summary-prev-group (&optional no-article)
5890   "Exit current newsgroup and then select previous unread newsgroup.
5891 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5892   (interactive "P")
5893   (gnus-summary-next-group no-article nil t))
5894
5895 ;; Walking around summary lines.
5896
5897 (defun gnus-summary-first-subject (&optional unread undownloaded)
5898   "Go to the first unread subject.
5899 If UNREAD is non-nil, go to the first unread article.
5900 Returns the article selected or nil if there are no unread articles."
5901   (interactive "P")
5902   (prog1
5903       (cond
5904        ;; Empty summary.
5905        ((null gnus-newsgroup-data)
5906         (gnus-message 3 "No articles in the group")
5907         nil)
5908        ;; Pick the first article.
5909        ((not unread)
5910         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5911         (gnus-data-number (car gnus-newsgroup-data)))
5912        ;; No unread articles.
5913        ((null gnus-newsgroup-unreads)
5914         (gnus-message 3 "No more unread articles")
5915         nil)
5916        ;; Find the first unread article.
5917        (t
5918         (let ((data gnus-newsgroup-data))
5919           (while (and data
5920                       (and (not (and undownloaded
5921                                      (eq gnus-undownloaded-mark
5922                                          (gnus-data-mark (car data)))))
5923                            (not (gnus-data-unread-p (car data)))))
5924             (setq data (cdr data)))
5925           (when data
5926             (goto-char (gnus-data-pos (car data)))
5927             (gnus-data-number (car data))))))
5928     (gnus-summary-position-point)))
5929
5930 (defun gnus-summary-next-subject (n &optional unread dont-display)
5931   "Go to next N'th summary line.
5932 If N is negative, go to the previous N'th subject line.
5933 If UNREAD is non-nil, only unread articles are selected.
5934 The difference between N and the actual number of steps taken is
5935 returned."
5936   (interactive "p")
5937   (let ((backward (< n 0))
5938         (n (abs n)))
5939     (while (and (> n 0)
5940                 (if backward
5941                     (gnus-summary-find-prev unread)
5942                   (gnus-summary-find-next unread)))
5943       (unless (zerop (setq n (1- n)))
5944         (gnus-summary-show-thread)))
5945     (when (/= 0 n)
5946       (gnus-message 7 "No more%s articles"
5947                     (if unread " unread" "")))
5948     (unless dont-display
5949       (gnus-summary-recenter)
5950       (gnus-summary-position-point))
5951     n))
5952
5953 (defun gnus-summary-next-unread-subject (n)
5954   "Go to next N'th unread summary line."
5955   (interactive "p")
5956   (gnus-summary-next-subject n t))
5957
5958 (defun gnus-summary-prev-subject (n &optional unread)
5959   "Go to previous N'th summary line.
5960 If optional argument UNREAD is non-nil, only unread article is selected."
5961   (interactive "p")
5962   (gnus-summary-next-subject (- n) unread))
5963
5964 (defun gnus-summary-prev-unread-subject (n)
5965   "Go to previous N'th unread summary line."
5966   (interactive "p")
5967   (gnus-summary-next-subject (- n) t))
5968
5969 (defun gnus-summary-goto-subject (article &optional force silent)
5970   "Go the subject line of ARTICLE.
5971 If FORCE, also allow jumping to articles not currently shown."
5972   (interactive "nArticle number: ")
5973   (let ((b (point))
5974         (data (gnus-data-find article)))
5975     ;; We read in the article if we have to.
5976     (and (not data)
5977          force
5978          (gnus-summary-insert-subject
5979           article
5980           (if (or (numberp force) (vectorp force)) force)
5981           t)
5982          (setq data (gnus-data-find article)))
5983     (goto-char b)
5984     (if (not data)
5985         (progn
5986           (unless silent
5987             (gnus-message 3 "Can't find article %d" article))
5988           nil)
5989       (goto-char (gnus-data-pos data))
5990       (gnus-summary-position-point)
5991       article)))
5992
5993 ;; Walking around summary lines with displaying articles.
5994
5995 (defun gnus-summary-expand-window (&optional arg)
5996   "Make the summary buffer take up the entire Emacs frame.
5997 Given a prefix, will force an `article' buffer configuration."
5998   (interactive "P")
5999   (if arg
6000       (gnus-configure-windows 'article 'force)
6001     (gnus-configure-windows 'summary 'force)))
6002
6003 (defun gnus-summary-display-article (article &optional all-header)
6004   "Display ARTICLE in article buffer."
6005   (when (gnus-buffer-live-p gnus-article-buffer)
6006     (with-current-buffer gnus-article-buffer
6007       (mm-enable-multibyte-mule4)))
6008   (gnus-set-global-variables)
6009   (when (gnus-buffer-live-p gnus-article-buffer)
6010     (with-current-buffer gnus-article-buffer
6011       (setq gnus-article-charset gnus-newsgroup-charset)
6012       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6013       (mm-enable-multibyte-mule4)))
6014   (if (null article)
6015       nil
6016     (prog1
6017         (if gnus-summary-display-article-function
6018             (funcall gnus-summary-display-article-function article all-header)
6019           (gnus-article-prepare article all-header))
6020       (gnus-run-hooks 'gnus-select-article-hook)
6021       (when (and gnus-current-article
6022                  (not (zerop gnus-current-article)))
6023         (gnus-summary-goto-subject gnus-current-article))
6024       (gnus-summary-recenter)
6025       (when (and gnus-use-trees gnus-show-threads)
6026         (gnus-possibly-generate-tree article)
6027         (gnus-highlight-selected-tree article))
6028       ;; Successfully display article.
6029       (gnus-article-set-window-start
6030        (cdr (assq article gnus-newsgroup-bookmarks))))))
6031
6032 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6033   "Select the current article.
6034 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6035 non-nil, the article will be re-fetched even if it already present in
6036 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6037 be displayed."
6038   ;; Make sure we are in the summary buffer to work around bbdb bug.
6039   (unless (eq major-mode 'gnus-summary-mode)
6040     (set-buffer gnus-summary-buffer))
6041   (let ((article (or article (gnus-summary-article-number)))
6042         (all-headers (not (not all-headers))) ;Must be T or NIL.
6043         gnus-summary-display-article-function)
6044     (and (not pseudo)
6045          (gnus-summary-article-pseudo-p article)
6046          (error "This is a pseudo-article"))
6047     (save-excursion
6048       (set-buffer gnus-summary-buffer)
6049       (if (or (and gnus-single-article-buffer
6050                    (or (null gnus-current-article)
6051                        (null gnus-article-current)
6052                        (null (get-buffer gnus-article-buffer))
6053                        (not (eq article (cdr gnus-article-current)))
6054                        (not (equal (car gnus-article-current)
6055                                    gnus-newsgroup-name))))
6056               (and (not gnus-single-article-buffer)
6057                    (or (null gnus-current-article)
6058                        (not (eq gnus-current-article article))))
6059               force)
6060           ;; The requested article is different from the current article.
6061           (progn
6062             (gnus-summary-display-article article all-headers)
6063             (when (gnus-buffer-live-p gnus-article-buffer)
6064                (with-current-buffer gnus-article-buffer
6065                 (if (not gnus-article-decoded-p) ;; a local variable
6066                     (mm-disable-multibyte-mule4))))
6067             (when (or all-headers gnus-show-all-headers)
6068               (gnus-article-show-all-headers))
6069             (gnus-article-set-window-start
6070              (cdr (assq article gnus-newsgroup-bookmarks)))
6071             article)
6072         (when (or all-headers gnus-show-all-headers)
6073           (gnus-article-show-all-headers))
6074         'old))))
6075
6076 (defun gnus-summary-force-verify-and-decrypt ()
6077   (interactive)
6078   (let ((mm-verify-option 'known)
6079         (mm-decrypt-option 'known))
6080     (gnus-summary-select-article nil 'force)))
6081
6082 (defun gnus-summary-set-current-mark (&optional current-mark)
6083   "Obsolete function."
6084   nil)
6085
6086 (defun gnus-summary-next-article (&optional unread subject backward push)
6087   "Select the next article.
6088 If UNREAD, only unread articles are selected.
6089 If SUBJECT, only articles with SUBJECT are selected.
6090 If BACKWARD, the previous article is selected instead of the next."
6091   (interactive "P")
6092   (cond
6093    ;; Is there such an article?
6094    ((and (gnus-summary-search-forward unread subject backward)
6095          (or (gnus-summary-display-article (gnus-summary-article-number))
6096              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6097     (gnus-summary-position-point))
6098    ;; If not, we try the first unread, if that is wanted.
6099    ((and subject
6100          gnus-auto-select-same
6101          (gnus-summary-first-unread-article))
6102     (gnus-summary-position-point)
6103     (gnus-message 6 "Wrapped"))
6104    ;; Try to get next/previous article not displayed in this group.
6105    ((and gnus-auto-extend-newsgroup
6106          (not unread) (not subject))
6107     (gnus-summary-goto-article
6108      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6109      nil (count-lines (point-min) (point))))
6110    ;; Go to next/previous group.
6111    (t
6112     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6113       (gnus-summary-jump-to-group gnus-newsgroup-name))
6114     (let ((cmd last-command-char)
6115           (point
6116            (save-excursion
6117              (set-buffer gnus-group-buffer)
6118              (point)))
6119           (group
6120            (if (eq gnus-keep-same-level 'best)
6121                (gnus-summary-best-group gnus-newsgroup-name)
6122              (gnus-summary-search-group backward gnus-keep-same-level))))
6123       ;; For some reason, the group window gets selected.  We change
6124       ;; it back.
6125       (select-window (get-buffer-window (current-buffer)))
6126       ;; Select next unread newsgroup automagically.
6127       (cond
6128        ((or (not gnus-auto-select-next)
6129             (not cmd))
6130         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6131        ((or (eq gnus-auto-select-next 'quietly)
6132             (and (eq gnus-auto-select-next 'slightly-quietly)
6133                  push)
6134             (and (eq gnus-auto-select-next 'almost-quietly)
6135                  (gnus-summary-last-article-p)))
6136         ;; Select quietly.
6137         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6138             (gnus-summary-exit)
6139           (gnus-message 7 "No more%s articles (%s)..."
6140                         (if unread " unread" "")
6141                         (if group (concat "selecting " group)
6142                           "exiting"))
6143           (gnus-summary-next-group nil group backward)))
6144        (t
6145         (when (gnus-key-press-event-p last-input-event)
6146           (gnus-summary-walk-group-buffer
6147            gnus-newsgroup-name cmd unread backward point))))))))
6148
6149 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6150   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6151                       (?\C-p (gnus-group-prev-unread-group 1))))
6152         (cursor-in-echo-area t)
6153         keve key group ended)
6154     (save-excursion
6155       (set-buffer gnus-group-buffer)
6156       (goto-char start)
6157       (setq group
6158             (if (eq gnus-keep-same-level 'best)
6159                 (gnus-summary-best-group gnus-newsgroup-name)
6160               (gnus-summary-search-group backward gnus-keep-same-level))))
6161     (while (not ended)
6162       (gnus-message
6163        5 "No more%s articles%s" (if unread " unread" "")
6164        (if (and group
6165                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6166            (format " (Type %s for %s [%s])"
6167                    (single-key-description cmd) group
6168                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6169          (format " (Type %s to exit %s)"
6170                  (single-key-description cmd)
6171                  gnus-newsgroup-name)))
6172       ;; Confirm auto selection.
6173       (setq key (car (setq keve (gnus-read-event-char))))
6174       (setq ended t)
6175       (cond
6176        ((assq key keystrokes)
6177         (let ((obuf (current-buffer)))
6178           (switch-to-buffer gnus-group-buffer)
6179           (when group
6180             (gnus-group-jump-to-group group))
6181           (eval (cadr (assq key keystrokes)))
6182           (setq group (gnus-group-group-name))
6183           (switch-to-buffer obuf))
6184         (setq ended nil))
6185        ((equal key cmd)
6186         (if (or (not group)
6187                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6188             (gnus-summary-exit)
6189           (gnus-summary-next-group nil group backward)))
6190        (t
6191         (push (cdr keve) unread-command-events))))))
6192
6193 (defun gnus-summary-next-unread-article ()
6194   "Select unread article after current one."
6195   (interactive)
6196   (gnus-summary-next-article
6197    (or (not (eq gnus-summary-goto-unread 'never))
6198        (gnus-summary-last-article-p (gnus-summary-article-number)))
6199    (and gnus-auto-select-same
6200         (gnus-summary-article-subject))))
6201
6202 (defun gnus-summary-prev-article (&optional unread subject)
6203   "Select the article after the current one.
6204 If UNREAD is non-nil, only unread articles are selected."
6205   (interactive "P")
6206   (gnus-summary-next-article unread subject t))
6207
6208 (defun gnus-summary-prev-unread-article ()
6209   "Select unread article before current one."
6210   (interactive)
6211   (gnus-summary-prev-article
6212    (or (not (eq gnus-summary-goto-unread 'never))
6213        (gnus-summary-first-article-p (gnus-summary-article-number)))
6214    (and gnus-auto-select-same
6215         (gnus-summary-article-subject))))
6216
6217 (defun gnus-summary-next-page (&optional lines circular)
6218   "Show next page of the selected article.
6219 If at the end of the current article, select the next article.
6220 LINES says how many lines should be scrolled up.
6221
6222 If CIRCULAR is non-nil, go to the start of the article instead of
6223 selecting the next article when reaching the end of the current
6224 article."
6225   (interactive "P")
6226   (setq gnus-summary-buffer (current-buffer))
6227   (gnus-set-global-variables)
6228   (let ((article (gnus-summary-article-number))
6229         (article-window (get-buffer-window gnus-article-buffer t))
6230         endp)
6231     ;; If the buffer is empty, we have no article.
6232     (unless article
6233       (error "No article to select"))
6234     (gnus-configure-windows 'article)
6235     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6236         (if (and (eq gnus-summary-goto-unread 'never)
6237                  (not (gnus-summary-last-article-p article)))
6238             (gnus-summary-next-article)
6239           (gnus-summary-next-unread-article))
6240       (if (or (null gnus-current-article)
6241               (null gnus-article-current)
6242               (/= article (cdr gnus-article-current))
6243               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6244           ;; Selected subject is different from current article's.
6245           (gnus-summary-display-article article)
6246         (when article-window
6247           (gnus-eval-in-buffer-window gnus-article-buffer
6248             (setq endp (gnus-article-next-page lines)))
6249           (when endp
6250             (cond (circular
6251                    (gnus-summary-beginning-of-article))
6252                   (lines
6253                    (gnus-message 3 "End of message"))
6254                   ((null lines)
6255                    (if (and (eq gnus-summary-goto-unread 'never)
6256                             (not (gnus-summary-last-article-p article)))
6257                        (gnus-summary-next-article)
6258                      (gnus-summary-next-unread-article))))))))
6259     (gnus-summary-recenter)
6260     (gnus-summary-position-point)))
6261
6262 (defun gnus-summary-prev-page (&optional lines move)
6263   "Show previous page of selected article.
6264 Argument LINES specifies lines to be scrolled down.
6265 If MOVE, move to the previous unread article if point is at
6266 the beginning of the buffer."
6267   (interactive "P")
6268   (let ((article (gnus-summary-article-number))
6269         (article-window (get-buffer-window gnus-article-buffer t))
6270         endp)
6271     (gnus-configure-windows 'article)
6272     (if (or (null gnus-current-article)
6273             (null gnus-article-current)
6274             (/= article (cdr gnus-article-current))
6275             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6276         ;; Selected subject is different from current article's.
6277         (gnus-summary-display-article article)
6278       (gnus-summary-recenter)
6279       (when article-window
6280         (gnus-eval-in-buffer-window gnus-article-buffer
6281           (setq endp (gnus-article-prev-page lines)))
6282         (when (and move endp)
6283           (cond (lines
6284                  (gnus-message 3 "Beginning of message"))
6285                 ((null lines)
6286                  (if (and (eq gnus-summary-goto-unread 'never)
6287                           (not (gnus-summary-first-article-p article)))
6288                      (gnus-summary-prev-article)
6289                    (gnus-summary-prev-unread-article))))))))
6290   (gnus-summary-position-point))
6291
6292 (defun gnus-summary-prev-page-or-article (&optional lines)
6293   "Show previous page of selected article.
6294 Argument LINES specifies lines to be scrolled down.
6295 If at the beginning of the article, go to the next article."
6296   (interactive "P")
6297   (gnus-summary-prev-page lines t))
6298
6299 (defun gnus-summary-scroll-up (lines)
6300   "Scroll up (or down) one line current article.
6301 Argument LINES specifies lines to be scrolled up (or down if negative)."
6302   (interactive "p")
6303   (gnus-configure-windows 'article)
6304   (gnus-summary-show-thread)
6305   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6306     (gnus-eval-in-buffer-window gnus-article-buffer
6307       (cond ((> lines 0)
6308              (when (gnus-article-next-page lines)
6309                (gnus-message 3 "End of message")))
6310             ((< lines 0)
6311              (gnus-article-prev-page (- lines))))))
6312   (gnus-summary-recenter)
6313   (gnus-summary-position-point))
6314
6315 (defun gnus-summary-scroll-down (lines)
6316   "Scroll down (or up) one line current article.
6317 Argument LINES specifies lines to be scrolled down (or up if negative)."
6318   (interactive "p")
6319   (gnus-summary-scroll-up (- lines)))
6320
6321 (defun gnus-summary-next-same-subject ()
6322   "Select next article which has the same subject as current one."
6323   (interactive)
6324   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6325
6326 (defun gnus-summary-prev-same-subject ()
6327   "Select previous article which has the same subject as current one."
6328   (interactive)
6329   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6330
6331 (defun gnus-summary-next-unread-same-subject ()
6332   "Select next unread article which has the same subject as current one."
6333   (interactive)
6334   (gnus-summary-next-article t (gnus-summary-article-subject)))
6335
6336 (defun gnus-summary-prev-unread-same-subject ()
6337   "Select previous unread article which has the same subject as current one."
6338   (interactive)
6339   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6340
6341 (defun gnus-summary-first-unread-article ()
6342   "Select the first unread article.
6343 Return nil if there are no unread articles."
6344   (interactive)
6345   (prog1
6346       (when (gnus-summary-first-subject t)
6347         (gnus-summary-show-thread)
6348         (gnus-summary-first-subject t)
6349         (gnus-summary-display-article (gnus-summary-article-number)))
6350     (gnus-summary-position-point)))
6351
6352 (defun gnus-summary-first-unread-subject ()
6353   "Place the point on the subject line of the first unread article.
6354 Return nil if there are no unread articles."
6355   (interactive)
6356   (prog1
6357       (when (gnus-summary-first-subject t)
6358         (gnus-summary-show-thread)
6359         (gnus-summary-first-subject t))
6360     (gnus-summary-position-point)))
6361
6362 (defun gnus-summary-first-article ()
6363   "Select the first article.
6364 Return nil if there are no articles."
6365   (interactive)
6366   (prog1
6367       (when (gnus-summary-first-subject)
6368         (gnus-summary-show-thread)
6369         (gnus-summary-first-subject)
6370         (gnus-summary-display-article (gnus-summary-article-number)))
6371     (gnus-summary-position-point)))
6372
6373 (defun gnus-summary-best-unread-article ()
6374   "Select the unread article with the highest score."
6375   (interactive)
6376   (let ((best -1000000)
6377         (data gnus-newsgroup-data)
6378         article score)
6379     (while data
6380       (and (gnus-data-unread-p (car data))
6381            (> (setq score
6382                     (gnus-summary-article-score (gnus-data-number (car data))))
6383               best)
6384            (setq best score
6385                  article (gnus-data-number (car data))))
6386       (setq data (cdr data)))
6387     (prog1
6388         (if article
6389             (gnus-summary-goto-article article)
6390           (error "No unread articles"))
6391       (gnus-summary-position-point))))
6392
6393 (defun gnus-summary-last-subject ()
6394   "Go to the last displayed subject line in the group."
6395   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6396     (when article
6397       (gnus-summary-goto-subject article))))
6398
6399 (defun gnus-summary-goto-article (article &optional all-headers force)
6400   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6401 If ALL-HEADERS is non-nil, no header lines are hidden.
6402 If FORCE, go to the article even if it isn't displayed.  If FORCE
6403 is a number, it is the line the article is to be displayed on."
6404   (interactive
6405    (list
6406     (completing-read
6407      "Article number or Message-ID: "
6408      (mapcar (lambda (number) (list (int-to-string number)))
6409              gnus-newsgroup-limit))
6410     current-prefix-arg
6411     t))
6412   (prog1
6413       (if (and (stringp article)
6414                (string-match "@" article))
6415           (gnus-summary-refer-article article)
6416         (when (stringp article)
6417           (setq article (string-to-number article)))
6418         (if (gnus-summary-goto-subject article force)
6419             (gnus-summary-display-article article all-headers)
6420           (gnus-message 4 "Couldn't go to article %s" article) nil))
6421     (gnus-summary-position-point)))
6422
6423 (defun gnus-summary-goto-last-article ()
6424   "Go to the previously read article."
6425   (interactive)
6426   (prog1
6427       (when gnus-last-article
6428         (gnus-summary-goto-article gnus-last-article nil t))
6429     (gnus-summary-position-point)))
6430
6431 (defun gnus-summary-pop-article (number)
6432   "Pop one article off the history and go to the previous.
6433 NUMBER articles will be popped off."
6434   (interactive "p")
6435   (let (to)
6436     (setq gnus-newsgroup-history
6437           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6438     (if to
6439         (gnus-summary-goto-article (car to) nil t)
6440       (error "Article history empty")))
6441   (gnus-summary-position-point))
6442
6443 ;; Summary commands and functions for limiting the summary buffer.
6444
6445 (defun gnus-summary-limit-to-articles (n)
6446   "Limit the summary buffer to the next N articles.
6447 If not given a prefix, use the process marked articles instead."
6448   (interactive "P")
6449   (prog1
6450       (let ((articles (gnus-summary-work-articles n)))
6451         (setq gnus-newsgroup-processable nil)
6452         (gnus-summary-limit articles))
6453     (gnus-summary-position-point)))
6454
6455 (defun gnus-summary-pop-limit (&optional total)
6456   "Restore the previous limit.
6457 If given a prefix, remove all limits."
6458   (interactive "P")
6459   (when total
6460     (setq gnus-newsgroup-limits
6461           (list (mapcar (lambda (h) (mail-header-number h))
6462                         gnus-newsgroup-headers))))
6463   (unless gnus-newsgroup-limits
6464     (error "No limit to pop"))
6465   (prog1
6466       (gnus-summary-limit nil 'pop)
6467     (gnus-summary-position-point)))
6468
6469 (defun gnus-summary-limit-to-subject (subject &optional header)
6470   "Limit the summary buffer to articles that have subjects that match a regexp."
6471   (interactive "sLimit to subject (regexp): ")
6472   (unless header
6473     (setq header "subject"))
6474   (when (not (equal "" subject))
6475     (prog1
6476         (let ((articles (gnus-summary-find-matching
6477                          (or header "subject") subject 'all)))
6478           (unless articles
6479             (error "Found no matches for \"%s\"" subject))
6480           (gnus-summary-limit articles))
6481       (gnus-summary-position-point))))
6482
6483 (defun gnus-summary-limit-to-author (from)
6484   "Limit the summary buffer to articles that have authors that match a regexp."
6485   (interactive "sLimit to author (regexp): ")
6486   (gnus-summary-limit-to-subject from "from"))
6487
6488 (defun gnus-summary-limit-to-age (age &optional younger-p)
6489   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6490 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6491 articles that are younger than AGE days."
6492   (interactive
6493    (let ((younger current-prefix-arg)
6494          (days-got nil)
6495          days)
6496      (while (not days-got)
6497        (setq days (if younger
6498                       (read-string "Limit to articles within (in days): ")
6499                     (read-string "Limit to articles older than (in days): ")))
6500        (when (> (length days) 0)
6501          (setq days (read days)))
6502        (if (numberp days)
6503            (setq days-got t)
6504          (message "Please enter a number.")
6505          (sleep-for 1)))
6506      (list days younger)))
6507   (prog1
6508       (let ((data gnus-newsgroup-data)
6509             (cutoff (days-to-time age))
6510             articles d date is-younger)
6511         (while (setq d (pop data))
6512           (when (and (vectorp (gnus-data-header d))
6513                      (setq date (mail-header-date (gnus-data-header d))))
6514             (setq is-younger (time-less-p
6515                               (time-since (condition-case ()
6516                                               (date-to-time date)
6517                                             (error '(0 0))))
6518                               cutoff))
6519             (when (if younger-p
6520                       is-younger
6521                     (not is-younger))
6522               (push (gnus-data-number d) articles))))
6523         (gnus-summary-limit (nreverse articles)))
6524     (gnus-summary-position-point)))
6525
6526 (defun gnus-summary-limit-to-extra (header regexp)
6527   "Limit the summary buffer to articles that match an 'extra' header."
6528   (interactive
6529    (let ((header
6530           (intern
6531            (gnus-completing-read
6532             (symbol-name (car gnus-extra-headers))
6533             "Limit extra header:"
6534             (mapcar (lambda (x)
6535                       (cons (symbol-name x) x))
6536                     gnus-extra-headers)
6537             nil
6538             t))))
6539      (list header
6540            (read-string (format "Limit to header %s (regexp): " header)))))
6541   (when (not (equal "" regexp))
6542     (prog1
6543         (let ((articles (gnus-summary-find-matching
6544                          (cons 'extra header) regexp 'all)))
6545           (unless articles
6546             (error "Found no matches for \"%s\"" regexp))
6547           (gnus-summary-limit articles))
6548       (gnus-summary-position-point))))
6549
6550 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6551 (make-obsolete
6552  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6553
6554 (defun gnus-summary-limit-to-unread (&optional all)
6555   "Limit the summary buffer to articles that are not marked as read.
6556 If ALL is non-nil, limit strictly to unread articles."
6557   (interactive "P")
6558   (if all
6559       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6560     (gnus-summary-limit-to-marks
6561      ;; Concat all the marks that say that an article is read and have
6562      ;; those removed.
6563      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6564            gnus-killed-mark gnus-kill-file-mark
6565            gnus-low-score-mark gnus-expirable-mark
6566            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6567            gnus-duplicate-mark gnus-souped-mark)
6568      'reverse)))
6569
6570 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6571 (make-obsolete 'gnus-summary-delete-marked-with
6572                'gnus-summary-limit-exlude-marks)
6573
6574 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6575   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6576 If REVERSE, limit the summary buffer to articles that are marked
6577 with MARKS.  MARKS can either be a string of marks or a list of marks.
6578 Returns how many articles were removed."
6579   (interactive "sMarks: ")
6580   (gnus-summary-limit-to-marks marks t))
6581
6582 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6583   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6584 If REVERSE (the prefix), limit the summary buffer to articles that are
6585 not marked with MARKS.  MARKS can either be a string of marks or a
6586 list of marks.
6587 Returns how many articles were removed."
6588   (interactive "sMarks: \nP")
6589   (prog1
6590       (let ((data gnus-newsgroup-data)
6591             (marks (if (listp marks) marks
6592                      (append marks nil))) ; Transform to list.
6593             articles)
6594         (while data
6595           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6596                   (memq (gnus-data-mark (car data)) marks))
6597             (push (gnus-data-number (car data)) articles))
6598           (setq data (cdr data)))
6599         (gnus-summary-limit articles))
6600     (gnus-summary-position-point)))
6601
6602 (defun gnus-summary-limit-to-score (&optional score)
6603   "Limit to articles with score at or above SCORE."
6604   (interactive "P")
6605   (setq score (if score
6606                   (prefix-numeric-value score)
6607                 (or gnus-summary-default-score 0)))
6608   (let ((data gnus-newsgroup-data)
6609         articles)
6610     (while data
6611       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6612                 score)
6613         (push (gnus-data-number (car data)) articles))
6614       (setq data (cdr data)))
6615     (prog1
6616         (gnus-summary-limit articles)
6617       (gnus-summary-position-point))))
6618
6619 (defun gnus-summary-limit-include-thread (id)
6620   "Display all the hidden articles that in the current thread."
6621   (interactive (list (mail-header-id (gnus-summary-article-header))))
6622   (let ((articles (gnus-articles-in-thread
6623                    (gnus-id-to-thread (gnus-root-id id)))))
6624     (prog1
6625         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6626       (gnus-summary-position-point))))
6627
6628 (defun gnus-summary-limit-include-dormant ()
6629   "Display all the hidden articles that are marked as dormant.
6630 Note that this command only works on a subset of the articles currently
6631 fetched for this group."
6632   (interactive)
6633   (unless gnus-newsgroup-dormant
6634     (error "There are no dormant articles in this group"))
6635   (prog1
6636       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6637     (gnus-summary-position-point)))
6638
6639 (defun gnus-summary-limit-exclude-dormant ()
6640   "Hide all dormant articles."
6641   (interactive)
6642   (prog1
6643       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6644     (gnus-summary-position-point)))
6645
6646 (defun gnus-summary-limit-exclude-childless-dormant ()
6647   "Hide all dormant articles that have no children."
6648   (interactive)
6649   (let ((data (gnus-data-list t))
6650         articles d children)
6651     ;; Find all articles that are either not dormant or have
6652     ;; children.
6653     (while (setq d (pop data))
6654       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6655                 (and (setq children
6656                            (gnus-article-children (gnus-data-number d)))
6657                      (let (found)
6658                        (while children
6659                          (when (memq (car children) articles)
6660                            (setq children nil
6661                                  found t))
6662                          (pop children))
6663                        found)))
6664         (push (gnus-data-number d) articles)))
6665     ;; Do the limiting.
6666     (prog1
6667         (gnus-summary-limit articles)
6668       (gnus-summary-position-point))))
6669
6670 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6671   "Mark all unread excluded articles as read.
6672 If ALL, mark even excluded ticked and dormants as read."
6673   (interactive "P")
6674   (let ((articles (gnus-sorted-complement
6675                    (sort
6676                     (mapcar (lambda (h) (mail-header-number h))
6677                             gnus-newsgroup-headers)
6678                     '<)
6679                    (sort gnus-newsgroup-limit '<)))
6680         article)
6681     (setq gnus-newsgroup-unreads
6682           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6683     (if all
6684         (setq gnus-newsgroup-dormant nil
6685               gnus-newsgroup-marked nil
6686               gnus-newsgroup-reads
6687               (nconc
6688                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6689                gnus-newsgroup-reads))
6690       (while (setq article (pop articles))
6691         (unless (or (memq article gnus-newsgroup-dormant)
6692                     (memq article gnus-newsgroup-marked))
6693           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6694
6695 (defun gnus-summary-limit (articles &optional pop)
6696   (if pop
6697       ;; We pop the previous limit off the stack and use that.
6698       (setq articles (car gnus-newsgroup-limits)
6699             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6700     ;; We use the new limit, so we push the old limit on the stack.
6701     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6702   ;; Set the limit.
6703   (setq gnus-newsgroup-limit articles)
6704   (let ((total (length gnus-newsgroup-data))
6705         (data (gnus-data-find-list (gnus-summary-article-number)))
6706         (gnus-summary-mark-below nil)   ; Inhibit this.
6707         found)
6708     ;; This will do all the work of generating the new summary buffer
6709     ;; according to the new limit.
6710     (gnus-summary-prepare)
6711     ;; Hide any threads, possibly.
6712     (and gnus-show-threads
6713          gnus-thread-hide-subtree
6714          (gnus-summary-hide-all-threads))
6715     ;; Try to return to the article you were at, or one in the
6716     ;; neighborhood.
6717     (when data
6718       ;; We try to find some article after the current one.
6719       (while data
6720         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6721           (setq data nil
6722                 found t))
6723         (setq data (cdr data))))
6724     (unless found
6725       ;; If there is no data, that means that we were after the last
6726       ;; article.  The same goes when we can't find any articles
6727       ;; after the current one.
6728       (goto-char (point-max))
6729       (gnus-summary-find-prev))
6730     (gnus-set-mode-line 'summary)
6731     ;; We return how many articles were removed from the summary
6732     ;; buffer as a result of the new limit.
6733     (- total (length gnus-newsgroup-data))))
6734
6735 (defsubst gnus-invisible-cut-children (threads)
6736   (let ((num 0))
6737     (while threads
6738       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6739         (incf num))
6740       (pop threads))
6741     (< num 2)))
6742
6743 (defsubst gnus-cut-thread (thread)
6744   "Go forwards in the thread until we find an article that we want to display."
6745   (when (or (eq gnus-fetch-old-headers 'some)
6746             (eq gnus-fetch-old-headers 'invisible)
6747             (numberp gnus-fetch-old-headers)
6748             (eq gnus-build-sparse-threads 'some)
6749             (eq gnus-build-sparse-threads 'more))
6750     ;; Deal with old-fetched headers and sparse threads.
6751     (while (and
6752             thread
6753             (or
6754              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6755              (gnus-summary-article-ancient-p
6756               (mail-header-number (car thread))))
6757             (if (or (<= (length (cdr thread)) 1)
6758                     (eq gnus-fetch-old-headers 'invisible))
6759                 (setq gnus-newsgroup-limit
6760                       (delq (mail-header-number (car thread))
6761                             gnus-newsgroup-limit)
6762                       thread (cadr thread))
6763               (when (gnus-invisible-cut-children (cdr thread))
6764                 (let ((th (cdr thread)))
6765                   (while th
6766                     (if (memq (mail-header-number (caar th))
6767                               gnus-newsgroup-limit)
6768                         (setq thread (car th)
6769                               th nil)
6770                       (setq th (cdr th))))))))))
6771   thread)
6772
6773 (defun gnus-cut-threads (threads)
6774   "Cut off all uninteresting articles from the beginning of threads."
6775   (when (or (eq gnus-fetch-old-headers 'some)
6776             (eq gnus-fetch-old-headers 'invisible)
6777             (numberp gnus-fetch-old-headers)
6778             (eq gnus-build-sparse-threads 'some)
6779             (eq gnus-build-sparse-threads 'more))
6780     (let ((th threads))
6781       (while th
6782         (setcar th (gnus-cut-thread (car th)))
6783         (setq th (cdr th)))))
6784   ;; Remove nixed out threads.
6785   (delq nil threads))
6786
6787 (defun gnus-summary-initial-limit (&optional show-if-empty)
6788   "Figure out what the initial limit is supposed to be on group entry.
6789 This entails weeding out unwanted dormants, low-scored articles,
6790 fetch-old-headers verbiage, and so on."
6791   ;; Most groups have nothing to remove.
6792   (if (or gnus-inhibit-limiting
6793           (and (null gnus-newsgroup-dormant)
6794                (not (eq gnus-fetch-old-headers 'some))
6795                (not (numberp gnus-fetch-old-headers))
6796                (not (eq gnus-fetch-old-headers 'invisible))
6797                (null gnus-summary-expunge-below)
6798                (not (eq gnus-build-sparse-threads 'some))
6799                (not (eq gnus-build-sparse-threads 'more))
6800                (null gnus-thread-expunge-below)
6801                (not gnus-use-nocem)))
6802       ()                                ; Do nothing.
6803     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6804     (setq gnus-newsgroup-limit nil)
6805     (mapatoms
6806      (lambda (node)
6807        (unless (car (symbol-value node))
6808          ;; These threads have no parents -- they are roots.
6809          (let ((nodes (cdr (symbol-value node)))
6810                thread)
6811            (while nodes
6812              (if (and gnus-thread-expunge-below
6813                       (< (gnus-thread-total-score (car nodes))
6814                          gnus-thread-expunge-below))
6815                  (gnus-expunge-thread (pop nodes))
6816                (setq thread (pop nodes))
6817                (gnus-summary-limit-children thread))))))
6818      gnus-newsgroup-dependencies)
6819     ;; If this limitation resulted in an empty group, we might
6820     ;; pop the previous limit and use it instead.
6821     (when (and (not gnus-newsgroup-limit)
6822                show-if-empty)
6823       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6824     gnus-newsgroup-limit))
6825
6826 (defun gnus-summary-limit-children (thread)
6827   "Return 1 if this subthread is visible and 0 if it is not."
6828   ;; First we get the number of visible children to this thread.  This
6829   ;; is done by recursing down the thread using this function, so this
6830   ;; will really go down to a leaf article first, before slowly
6831   ;; working its way up towards the root.
6832   (when thread
6833     (let ((children
6834            (if (cdr thread)
6835                (apply '+ (mapcar 'gnus-summary-limit-children
6836                                  (cdr thread)))
6837              0))
6838           (number (mail-header-number (car thread)))
6839           score)
6840       (if (and
6841            (not (memq number gnus-newsgroup-marked))
6842            (or
6843             ;; If this article is dormant and has absolutely no visible
6844             ;; children, then this article isn't visible.
6845             (and (memq number gnus-newsgroup-dormant)
6846                  (zerop children))
6847             ;; If this is "fetch-old-headered" and there is no
6848             ;; visible children, then we don't want this article.
6849             (and (or (eq gnus-fetch-old-headers 'some)
6850                      (numberp gnus-fetch-old-headers))
6851                  (gnus-summary-article-ancient-p number)
6852                  (zerop children))
6853             ;; If this is "fetch-old-headered" and `invisible', then
6854             ;; we don't want this article.
6855             (and (eq gnus-fetch-old-headers 'invisible)
6856                  (gnus-summary-article-ancient-p number))
6857             ;; If this is a sparsely inserted article with no children,
6858             ;; we don't want it.
6859             (and (eq gnus-build-sparse-threads 'some)
6860                  (gnus-summary-article-sparse-p number)
6861                  (zerop children))
6862             ;; If we use expunging, and this article is really
6863             ;; low-scored, then we don't want this article.
6864             (when (and gnus-summary-expunge-below
6865                        (< (setq score
6866                                 (or (cdr (assq number gnus-newsgroup-scored))
6867                                     gnus-summary-default-score))
6868                           gnus-summary-expunge-below))
6869               ;; We increase the expunge-tally here, but that has
6870               ;; nothing to do with the limits, really.
6871               (incf gnus-newsgroup-expunged-tally)
6872               ;; We also mark as read here, if that's wanted.
6873               (when (and gnus-summary-mark-below
6874                          (< score gnus-summary-mark-below))
6875                 (setq gnus-newsgroup-unreads
6876                       (delq number gnus-newsgroup-unreads))
6877                 (if gnus-newsgroup-auto-expire
6878                     (push number gnus-newsgroup-expirable)
6879                   (push (cons number gnus-low-score-mark)
6880                         gnus-newsgroup-reads)))
6881               t)
6882             ;; Check NoCeM things.
6883             (if (and gnus-use-nocem
6884                      (gnus-nocem-unwanted-article-p
6885                       (mail-header-id (car thread))))
6886                 (progn
6887                   (setq gnus-newsgroup-unreads
6888                         (delq number gnus-newsgroup-unreads))
6889                   t))))
6890           ;; Nope, invisible article.
6891           0
6892         ;; Ok, this article is to be visible, so we add it to the limit
6893         ;; and return 1.
6894         (push number gnus-newsgroup-limit)
6895         1))))
6896
6897 (defun gnus-expunge-thread (thread)
6898   "Mark all articles in THREAD as read."
6899   (let* ((number (mail-header-number (car thread))))
6900     (incf gnus-newsgroup-expunged-tally)
6901     ;; We also mark as read here, if that's wanted.
6902     (setq gnus-newsgroup-unreads
6903           (delq number gnus-newsgroup-unreads))
6904     (if gnus-newsgroup-auto-expire
6905         (push number gnus-newsgroup-expirable)
6906       (push (cons number gnus-low-score-mark)
6907             gnus-newsgroup-reads)))
6908   ;; Go recursively through all subthreads.
6909   (mapcar 'gnus-expunge-thread (cdr thread)))
6910
6911 ;; Summary article oriented commands
6912
6913 (defun gnus-summary-refer-parent-article (n)
6914   "Refer parent article N times.
6915 If N is negative, go to ancestor -N instead.
6916 The difference between N and the number of articles fetched is returned."
6917   (interactive "p")
6918   (let ((skip 1)
6919         error header ref)
6920     (when (not (natnump n))
6921       (setq skip (abs n)
6922             n 1))
6923     (while (and (> n 0)
6924                 (not error))
6925       (setq header (gnus-summary-article-header))
6926       (if (and (eq (mail-header-number header)
6927                    (cdr gnus-article-current))
6928                (equal gnus-newsgroup-name
6929                       (car gnus-article-current)))
6930           ;; If we try to find the parent of the currently
6931           ;; displayed article, then we take a look at the actual
6932           ;; References header, since this is slightly more
6933           ;; reliable than the References field we got from the
6934           ;; server.
6935           (save-excursion
6936             (set-buffer gnus-original-article-buffer)
6937             (nnheader-narrow-to-headers)
6938             (unless (setq ref (message-fetch-field "references"))
6939               (setq ref (message-fetch-field "in-reply-to")))
6940             (widen))
6941         (setq ref
6942               ;; It's not the current article, so we take a bet on
6943               ;; the value we got from the server.
6944               (mail-header-references header)))
6945       (if (and ref
6946                (not (equal ref "")))
6947           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6948             (gnus-message 1 "Couldn't find parent"))
6949         (gnus-message 1 "No references in article %d"
6950                       (gnus-summary-article-number))
6951         (setq error t))
6952       (decf n))
6953     (gnus-summary-position-point)
6954     n))
6955
6956 (defun gnus-summary-refer-references ()
6957   "Fetch all articles mentioned in the References header.
6958 Return the number of articles fetched."
6959   (interactive)
6960   (let ((ref (mail-header-references (gnus-summary-article-header)))
6961         (current (gnus-summary-article-number))
6962         (n 0))
6963     (if (or (not ref)
6964             (equal ref ""))
6965         (error "No References in the current article")
6966       ;; For each Message-ID in the References header...
6967       (while (string-match "<[^>]*>" ref)
6968         (incf n)
6969         ;; ... fetch that article.
6970         (gnus-summary-refer-article
6971          (prog1 (match-string 0 ref)
6972            (setq ref (substring ref (match-end 0))))))
6973       (gnus-summary-goto-subject current)
6974       (gnus-summary-position-point)
6975       n)))
6976
6977 (defun gnus-summary-refer-thread (&optional limit)
6978   "Fetch all articles in the current thread.
6979 If LIMIT (the numerical prefix), fetch that many old headers instead
6980 of what's specified by the `gnus-refer-thread-limit' variable."
6981   (interactive "P")
6982   (let ((id (mail-header-id (gnus-summary-article-header)))
6983         (limit (if limit (prefix-numeric-value limit)
6984                  gnus-refer-thread-limit)))
6985     ;; We want to fetch LIMIT *old* headers, but we also have to
6986     ;; re-fetch all the headers in the current buffer, because many of
6987     ;; them may be undisplayed.  So we adjust LIMIT.
6988     (when (numberp limit)
6989       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6990     (unless (eq gnus-fetch-old-headers 'invisible)
6991       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6992       ;; Retrieve the headers and read them in.
6993       (if (eq (gnus-retrieve-headers
6994                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6995               'nov)
6996           (gnus-build-all-threads)
6997         (error "Can't fetch thread from backends that don't support NOV"))
6998       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6999     (gnus-summary-limit-include-thread id)))
7000
7001 (defun gnus-summary-refer-article (message-id)
7002   "Fetch an article specified by MESSAGE-ID."
7003   (interactive "sMessage-ID: ")
7004   (when (and (stringp message-id)
7005              (not (zerop (length message-id))))
7006     ;; Construct the correct Message-ID if necessary.
7007     ;; Suggested by tale@pawl.rpi.edu.
7008     (unless (string-match "^<" message-id)
7009       (setq message-id (concat "<" message-id)))
7010     (unless (string-match ">$" message-id)
7011       (setq message-id (concat message-id ">")))
7012     (let* ((header (gnus-id-to-header message-id))
7013            (sparse (and header
7014                         (gnus-summary-article-sparse-p
7015                          (mail-header-number header))
7016                         (memq (mail-header-number header)
7017                               gnus-newsgroup-limit)))
7018            number)
7019       (cond
7020        ;; If the article is present in the buffer we just go to it.
7021        ((and header
7022              (or (not (gnus-summary-article-sparse-p
7023                        (mail-header-number header)))
7024                  sparse))
7025         (prog1
7026             (gnus-summary-goto-article
7027              (mail-header-number header) nil t)
7028           (when sparse
7029             (gnus-summary-update-article (mail-header-number header)))))
7030        (t
7031         ;; We fetch the article.
7032         (catch 'found
7033           (dolist (gnus-override-method (gnus-refer-article-methods))
7034             (gnus-check-server gnus-override-method)
7035             ;; Fetch the header, and display the article.
7036             (when (setq number (gnus-summary-insert-subject message-id))
7037               (gnus-summary-select-article nil nil nil number)
7038               (throw 'found t)))
7039           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7040
7041 (defun gnus-refer-article-methods ()
7042   "Return a list of referrable methods."
7043   (cond
7044    ;; No method, so we default to current and native.
7045    ((null gnus-refer-article-method)
7046     (list gnus-current-select-method gnus-select-method))
7047    ;; Current.
7048    ((eq 'current gnus-refer-article-method)
7049     (list gnus-current-select-method))
7050    ;; List of select methods.
7051    ((not (and (symbolp (car gnus-refer-article-method))
7052               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7053     (let (out)
7054       (dolist (method gnus-refer-article-method)
7055         (push (if (eq 'current method)
7056                   gnus-current-select-method
7057                 method)
7058               out))
7059       (nreverse out)))
7060    ;; One single select method.
7061    (t
7062     (list gnus-refer-article-method))))
7063
7064 (defun gnus-summary-edit-parameters ()
7065   "Edit the group parameters of the current group."
7066   (interactive)
7067   (gnus-group-edit-group gnus-newsgroup-name 'params))
7068
7069 (defun gnus-summary-customize-parameters ()
7070   "Customize the group parameters of the current group."
7071   (interactive)
7072   (gnus-group-customize gnus-newsgroup-name))
7073
7074 (defun gnus-summary-enter-digest-group (&optional force)
7075   "Enter an nndoc group based on the current article.
7076 If FORCE, force a digest interpretation.  If not, try
7077 to guess what the document format is."
7078   (interactive "P")
7079   (let ((conf gnus-current-window-configuration))
7080     (save-excursion
7081       (gnus-summary-select-article))
7082     (setq gnus-current-window-configuration conf)
7083     (let* ((name (format "%s-%d"
7084                          (gnus-group-prefixed-name
7085                           gnus-newsgroup-name (list 'nndoc ""))
7086                          (save-excursion
7087                            (set-buffer gnus-summary-buffer)
7088                            gnus-current-article)))
7089            (ogroup gnus-newsgroup-name)
7090            (params (append (gnus-info-params (gnus-get-info ogroup))
7091                            (list (cons 'to-group ogroup))
7092                            (list (cons 'save-article-group ogroup))))
7093            (case-fold-search t)
7094            (buf (current-buffer))
7095            dig to-address)
7096       (save-excursion
7097         (set-buffer gnus-original-article-buffer)
7098         ;; Have the digest group inherit the main mail address of
7099         ;; the parent article.
7100         (when (setq to-address (or (message-fetch-field "reply-to")
7101                                    (message-fetch-field "from")))
7102           (setq params (append 
7103                         (list (cons 'to-address 
7104                                     (funcall gnus-decode-encoded-word-function
7105                                              to-address))))))
7106         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7107         (insert-buffer-substring gnus-original-article-buffer)
7108         ;; Remove lines that may lead nndoc to misinterpret the
7109         ;; document type.
7110         (narrow-to-region
7111          (goto-char (point-min))
7112          (or (search-forward "\n\n" nil t) (point)))
7113         (goto-char (point-min))
7114         (delete-matching-lines "^Path:\\|^From ")
7115         (widen))
7116       (unwind-protect
7117           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7118                     (gnus-newsgroup-ephemeral-ignored-charsets
7119                      gnus-newsgroup-ignored-charsets))
7120                 (gnus-group-read-ephemeral-group
7121                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7122                               (nndoc-article-type
7123                                ,(if force 'mbox 'guess))) t))
7124               ;; Make all postings to this group go to the parent group.
7125               (nconc (gnus-info-params (gnus-get-info name))
7126                      params)
7127             ;; Couldn't select this doc group.
7128             (switch-to-buffer buf)
7129             (gnus-set-global-variables)
7130             (gnus-configure-windows 'summary)
7131             (gnus-message 3 "Article couldn't be entered?"))
7132         (kill-buffer dig)))))
7133
7134 (defun gnus-summary-read-document (n)
7135   "Open a new group based on the current article(s).
7136 This will allow you to read digests and other similar
7137 documents as newsgroups.
7138 Obeys the standard process/prefix convention."
7139   (interactive "P")
7140   (let* ((articles (gnus-summary-work-articles n))
7141          (ogroup gnus-newsgroup-name)
7142          (params (append (gnus-info-params (gnus-get-info ogroup))
7143                          (list (cons 'to-group ogroup))))
7144          article group egroup groups vgroup)
7145     (while (setq article (pop articles))
7146       (setq group (format "%s-%d" gnus-newsgroup-name article))
7147       (gnus-summary-remove-process-mark article)
7148       (when (gnus-summary-display-article article)
7149         (save-excursion
7150           (with-temp-buffer
7151             (insert-buffer-substring gnus-original-article-buffer)
7152             ;; Remove some headers that may lead nndoc to make
7153             ;; the wrong guess.
7154             (message-narrow-to-head)
7155             (goto-char (point-min))
7156             (delete-matching-lines "^\\(Path\\):\\|^From ")
7157             (widen)
7158             (if (setq egroup
7159                       (gnus-group-read-ephemeral-group
7160                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7161                                      (nndoc-article-type guess))
7162                        t nil t))
7163                 (progn
7164                   ;; Make all postings to this group go to the parent group.
7165                   (nconc (gnus-info-params (gnus-get-info egroup))
7166                          params)
7167                   (push egroup groups))
7168               ;; Couldn't select this doc group.
7169               (gnus-error 3 "Article couldn't be entered"))))))
7170     ;; Now we have selected all the documents.
7171     (cond
7172      ((not groups)
7173       (error "None of the articles could be interpreted as documents"))
7174      ((gnus-group-read-ephemeral-group
7175        (setq vgroup (format
7176                      "nnvirtual:%s-%s" gnus-newsgroup-name
7177                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7178        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7179        t
7180        (cons (current-buffer) 'summary)))
7181      (t
7182       (error "Couldn't select virtual nndoc group")))))
7183
7184 (defun gnus-summary-isearch-article (&optional regexp-p)
7185   "Do incremental search forward on the current article.
7186 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7187   (interactive "P")
7188   (gnus-summary-select-article)
7189   (gnus-configure-windows 'article)
7190   (gnus-eval-in-buffer-window gnus-article-buffer
7191     (save-restriction
7192       (widen)
7193       (isearch-forward regexp-p))))
7194
7195 (defun gnus-summary-search-article-forward (regexp &optional backward)
7196   "Search for an article containing REGEXP forward.
7197 If BACKWARD, search backward instead."
7198   (interactive
7199    (list (read-string
7200           (format "Search article %s (regexp%s): "
7201                   (if current-prefix-arg "backward" "forward")
7202                   (if gnus-last-search-regexp
7203                       (concat ", default " gnus-last-search-regexp)
7204                     "")))
7205          current-prefix-arg))
7206   (if (string-equal regexp "")
7207       (setq regexp (or gnus-last-search-regexp ""))
7208     (setq gnus-last-search-regexp regexp)
7209     (setq gnus-article-before-search gnus-current-article))
7210   ;; Intentionally set gnus-last-article.
7211   (setq gnus-last-article gnus-article-before-search)
7212   (let ((gnus-last-article gnus-last-article))
7213     (if (gnus-summary-search-article regexp backward)
7214         (gnus-summary-show-thread)
7215       (error "Search failed: \"%s\"" regexp))))
7216
7217 (defun gnus-summary-search-article-backward (regexp)
7218   "Search for an article containing REGEXP backward."
7219   (interactive
7220    (list (read-string
7221           (format "Search article backward (regexp%s): "
7222                   (if gnus-last-search-regexp
7223                       (concat ", default " gnus-last-search-regexp)
7224                     "")))))
7225   (gnus-summary-search-article-forward regexp 'backward))
7226
7227 (defun gnus-summary-search-article (regexp &optional backward)
7228   "Search for an article containing REGEXP.
7229 Optional argument BACKWARD means do search for backward.
7230 `gnus-select-article-hook' is not called during the search."
7231   ;; We have to require this here to make sure that the following
7232   ;; dynamic binding isn't shadowed by autoloading.
7233   (require 'gnus-async)
7234   (require 'gnus-art)
7235   (let ((gnus-select-article-hook nil)  ;Disable hook.
7236         (gnus-article-prepare-hook nil)
7237         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7238         (gnus-use-article-prefetch nil)
7239         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7240         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7241         (sum (current-buffer))
7242         (gnus-display-mime-function nil)
7243         (found nil)
7244         point)
7245     (gnus-save-hidden-threads
7246       (gnus-summary-select-article)
7247       (set-buffer gnus-article-buffer)
7248       (goto-char (window-point (get-buffer-window (current-buffer))))
7249       (when backward
7250         (forward-line -1))
7251       (while (not found)
7252         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7253         (if (if backward
7254                 (re-search-backward regexp nil t)
7255               (re-search-forward regexp nil t))
7256             ;; We found the regexp.
7257             (progn
7258               (setq found 'found)
7259               (beginning-of-line)
7260               (set-window-start
7261                (get-buffer-window (current-buffer))
7262                (point))
7263               (forward-line 1)
7264               (set-window-point
7265                (get-buffer-window (current-buffer))
7266                (point))
7267               (set-buffer sum)
7268               (setq point (point)))
7269           ;; We didn't find it, so we go to the next article.
7270           (set-buffer sum)
7271           (setq found 'not)
7272           (while (eq found 'not)
7273             (if (not (if backward (gnus-summary-find-prev)
7274                        (gnus-summary-find-next)))
7275                 ;; No more articles.
7276                 (setq found t)
7277               ;; Select the next article and adjust point.
7278               (unless (gnus-summary-article-sparse-p
7279                        (gnus-summary-article-number))
7280                 (setq found nil)
7281                 (gnus-summary-select-article)
7282                 (set-buffer gnus-article-buffer)
7283                 (widen)
7284                 (goto-char (if backward (point-max) (point-min))))))))
7285       (gnus-message 7 ""))
7286     ;; Return whether we found the regexp.
7287     (when (eq found 'found)
7288       (goto-char point)
7289       (gnus-summary-show-thread)
7290       (gnus-summary-goto-subject gnus-current-article)
7291       (gnus-summary-position-point)
7292       t)))
7293
7294 (defun gnus-summary-find-matching (header regexp &optional backward unread
7295                                           not-case-fold)
7296   "Return a list of all articles that match REGEXP on HEADER.
7297 The search stars on the current article and goes forwards unless
7298 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7299 If UNREAD is non-nil, only unread articles will
7300 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7301 in the comparisons."
7302   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7303                 (gnus-data-find-list
7304                  (gnus-summary-article-number) (gnus-data-list backward))))
7305         (case-fold-search (not not-case-fold))
7306         articles d func)
7307     (if (consp header)
7308         (if (eq (car header) 'extra)
7309             (setq func
7310                   `(lambda (h)
7311                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7312                          "")))
7313           (error "%s is an invalid header" header))
7314       (unless (fboundp (intern (concat "mail-header-" header)))
7315         (error "%s is not a valid header" header))
7316       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7317     (while data
7318       (setq d (car data))
7319       (and (or (not unread)             ; We want all articles...
7320                (gnus-data-unread-p d))  ; Or just unreads.
7321            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7322            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7323            (push (gnus-data-number d) articles)) ; Success!
7324       (setq data (cdr data)))
7325     (nreverse articles)))
7326
7327 (defun gnus-summary-execute-command (header regexp command &optional backward)
7328   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7329 If HEADER is an empty string (or nil), the match is done on the entire
7330 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7331   (interactive
7332    (list (let ((completion-ignore-case t))
7333            (completing-read
7334             "Header name: "
7335             (mapcar (lambda (string) (list string))
7336                     '("Number" "Subject" "From" "Lines" "Date"
7337                       "Message-ID" "Xref" "References" "Body"))
7338             nil 'require-match))
7339          (read-string "Regexp: ")
7340          (read-key-sequence "Command: ")
7341          current-prefix-arg))
7342   (when (equal header "Body")
7343     (setq header ""))
7344   ;; Hidden thread subtrees must be searched as well.
7345   (gnus-summary-show-all-threads)
7346   ;; We don't want to change current point nor window configuration.
7347   (save-excursion
7348     (save-window-excursion
7349       (gnus-message 6 "Executing %s..." (key-description command))
7350       ;; We'd like to execute COMMAND interactively so as to give arguments.
7351       (gnus-execute header regexp
7352                     `(call-interactively ',(key-binding command))
7353                     backward)
7354       (gnus-message 6 "Executing %s...done" (key-description command)))))
7355
7356 (defun gnus-summary-beginning-of-article ()
7357   "Scroll the article back to the beginning."
7358   (interactive)
7359   (gnus-summary-select-article)
7360   (gnus-configure-windows 'article)
7361   (gnus-eval-in-buffer-window gnus-article-buffer
7362     (widen)
7363     (goto-char (point-min))
7364     (when gnus-page-broken
7365       (gnus-narrow-to-page))))
7366
7367 (defun gnus-summary-end-of-article ()
7368   "Scroll to the end of the article."
7369   (interactive)
7370   (gnus-summary-select-article)
7371   (gnus-configure-windows 'article)
7372   (gnus-eval-in-buffer-window gnus-article-buffer
7373     (widen)
7374     (goto-char (point-max))
7375     (recenter -3)
7376     (when gnus-page-broken
7377       (gnus-narrow-to-page))))
7378
7379 (defun gnus-summary-print-article (&optional filename n)
7380   "Generate and print a PostScript image of the N next (mail) articles.
7381
7382 If N is negative, print the N previous articles.  If N is nil and articles
7383 have been marked with the process mark, print these instead.
7384
7385 If the optional first argument FILENAME is nil, send the image to the
7386 printer.  If FILENAME is a string, save the PostScript image in a file with
7387 that name.  If FILENAME is a number, prompt the user for the name of the file
7388 to save in."
7389   (interactive (list (ps-print-preprint current-prefix-arg)
7390                      current-prefix-arg))
7391   (dolist (article (gnus-summary-work-articles n))
7392     (gnus-summary-select-article nil nil 'pseudo article)
7393     (gnus-eval-in-buffer-window gnus-article-buffer
7394       (let ((buffer (generate-new-buffer " *print*")))
7395         (unwind-protect
7396             (progn
7397               (copy-to-buffer buffer (point-min) (point-max))
7398               (set-buffer buffer)
7399               (gnus-article-delete-invisible-text)
7400               (let ((ps-left-header
7401                      (list
7402                       (concat "("
7403                               (mail-header-subject gnus-current-headers) ")")
7404                       (concat "("
7405                               (mail-header-from gnus-current-headers) ")")))
7406                     (ps-right-header
7407                      (list
7408                       "/pagenumberstring load"
7409                       (concat "("
7410                               (mail-header-date gnus-current-headers) ")"))))
7411                 (gnus-run-hooks 'gnus-ps-print-hook)
7412                 (save-excursion
7413                   (ps-print-buffer-with-faces filename))))
7414           (kill-buffer buffer))))))
7415
7416 (defun gnus-summary-show-article (&optional arg)
7417   "Force re-fetching of the current article.
7418 If ARG (the prefix) is a number, show the article with the charset
7419 defined in `gnus-summary-show-article-charset-alist', or the charset
7420 inputed.
7421 If ARG (the prefix) is non-nil and not a number, show the raw article
7422 without any article massaging functions being run."
7423   (interactive "P")
7424   (cond
7425    ((numberp arg)
7426     (let ((gnus-newsgroup-charset
7427            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7428                (read-coding-system "Charset: ")))
7429           (gnus-newsgroup-ignored-charsets 'gnus-all))
7430       (gnus-summary-select-article nil 'force)
7431       (let ((deps gnus-newsgroup-dependencies)
7432             head header)
7433         (save-excursion
7434           (set-buffer gnus-original-article-buffer)
7435           (save-restriction
7436             (message-narrow-to-head)
7437             (setq head (buffer-string)))
7438           (with-temp-buffer
7439             (insert (format "211 %d Article retrieved.\n"
7440                             (cdr gnus-article-current)))
7441             (insert head)
7442             (insert ".\n")
7443             (let ((nntp-server-buffer (current-buffer)))
7444               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7445         (gnus-data-set-header
7446          (gnus-data-find (cdr gnus-article-current))
7447          header)
7448         (gnus-summary-update-article-line
7449          (cdr gnus-article-current) header))))
7450    ((not arg)
7451     ;; Select the article the normal way.
7452     (gnus-summary-select-article nil 'force))
7453    (t
7454     ;; We have to require this here to make sure that the following
7455     ;; dynamic binding isn't shadowed by autoloading.
7456     (require 'gnus-async)
7457     (require 'gnus-art)
7458     ;; Bind the article treatment functions to nil.
7459     (let ((gnus-have-all-headers t)
7460           gnus-article-prepare-hook
7461           gnus-article-decode-hook
7462           gnus-display-mime-function
7463           gnus-break-pages)
7464       ;; Destroy any MIME parts.
7465       (when (gnus-buffer-live-p gnus-article-buffer)
7466         (save-excursion
7467           (set-buffer gnus-article-buffer)
7468           (mm-destroy-parts gnus-article-mime-handles)
7469           ;; Set it to nil for safety reason.
7470           (setq gnus-article-mime-handle-alist nil)
7471           (setq gnus-article-mime-handles nil)))
7472       (gnus-summary-select-article nil 'force))))
7473   (gnus-summary-goto-subject gnus-current-article)
7474   (gnus-summary-position-point))
7475
7476 (defun gnus-summary-verbose-headers (&optional arg)
7477   "Toggle permanent full header display.
7478 If ARG is a positive number, turn header display on.
7479 If ARG is a negative number, turn header display off."
7480   (interactive "P")
7481   (setq gnus-show-all-headers
7482         (cond ((or (not (numberp arg))
7483                    (zerop arg))
7484                (not gnus-show-all-headers))
7485               ((natnump arg)
7486                t)))
7487   (gnus-summary-show-article))
7488
7489 (defun gnus-summary-toggle-header (&optional arg)
7490   "Show the headers if they are hidden, or hide them if they are shown.
7491 If ARG is a positive number, show the entire header.
7492 If ARG is a negative number, hide the unwanted header lines."
7493   (interactive "P")
7494   (save-excursion
7495     (set-buffer gnus-article-buffer)
7496     (save-restriction
7497       (let* ((buffer-read-only nil)
7498              (inhibit-point-motion-hooks t)
7499              hidden e)
7500         (setq hidden
7501               (if (numberp arg)
7502                   (>= arg 0)
7503                 (save-restriction
7504                   (article-narrow-to-head)
7505                   (gnus-article-hidden-text-p 'headers))))
7506         (goto-char (point-min))
7507         (when (search-forward "\n\n" nil t)
7508           (delete-region (point-min) (1- (point))))
7509         (goto-char (point-min))
7510         (save-excursion
7511           (set-buffer gnus-original-article-buffer)
7512           (goto-char (point-min))
7513           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7514         (insert-buffer-substring gnus-original-article-buffer 1 e)
7515         (save-restriction
7516           (narrow-to-region (point-min) (point))
7517           (article-decode-encoded-words)
7518           (if  hidden
7519               (let ((gnus-treat-hide-headers nil)
7520                     (gnus-treat-hide-boring-headers nil))
7521                 (setq gnus-article-wash-types
7522                       (delq 'headers gnus-article-wash-types))
7523                 (gnus-treat-article 'head))
7524             (gnus-treat-article 'head)))
7525         (gnus-set-mode-line 'article)))))
7526
7527 (defun gnus-summary-show-all-headers ()
7528   "Make all header lines visible."
7529   (interactive)
7530   (gnus-article-show-all-headers))
7531
7532 (defun gnus-summary-caesar-message (&optional arg)
7533   "Caesar rotate the current article by 13.
7534 The numerical prefix specifies how many places to rotate each letter
7535 forward."
7536   (interactive "P")
7537   (gnus-summary-select-article)
7538   (let ((mail-header-separator ""))
7539     (gnus-eval-in-buffer-window gnus-article-buffer
7540       (save-restriction
7541         (widen)
7542         (let ((start (window-start))
7543               buffer-read-only)
7544           (message-caesar-buffer-body arg)
7545           (set-window-start (get-buffer-window (current-buffer)) start))))))
7546
7547 (defun gnus-summary-stop-page-breaking ()
7548   "Stop page breaking in the current article."
7549   (interactive)
7550   (gnus-summary-select-article)
7551   (gnus-eval-in-buffer-window gnus-article-buffer
7552     (widen)
7553     (when (gnus-visual-p 'page-marker)
7554       (let ((buffer-read-only nil))
7555         (gnus-remove-text-with-property 'gnus-prev)
7556         (gnus-remove-text-with-property 'gnus-next))
7557       (setq gnus-page-broken nil))))
7558
7559 (defun gnus-summary-move-article (&optional n to-newsgroup
7560                                             select-method action)
7561   "Move the current article to a different newsgroup.
7562 If N is a positive number, move the N next articles.
7563 If N is a negative number, move the N previous articles.
7564 If N is nil and any articles have been marked with the process mark,
7565 move those articles instead.
7566 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7567 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7568 re-spool using this method.
7569
7570 For this function to work, both the current newsgroup and the
7571 newsgroup that you want to move to have to support the `request-move'
7572 and `request-accept' functions.
7573
7574 ACTION can be either `move' (the default), `crosspost' or `copy'."
7575   (interactive "P")
7576   (unless action
7577     (setq action 'move))
7578   ;; Disable marking as read.
7579   (let (gnus-mark-article-hook)
7580     (save-window-excursion
7581       (gnus-summary-select-article)))
7582   ;; Check whether the source group supports the required functions.
7583   (cond ((and (eq action 'move)
7584               (not (gnus-check-backend-function
7585                     'request-move-article gnus-newsgroup-name)))
7586          (error "The current group does not support article moving"))
7587         ((and (eq action 'crosspost)
7588               (not (gnus-check-backend-function
7589                     'request-replace-article gnus-newsgroup-name)))
7590          (error "The current group does not support article editing")))
7591   (let ((articles (gnus-summary-work-articles n))
7592         (prefix (if (gnus-check-backend-function
7593                     'request-move-article gnus-newsgroup-name)
7594                     (gnus-group-real-prefix gnus-newsgroup-name)
7595                   ""))
7596         (names '((move "Move" "Moving")
7597                  (copy "Copy" "Copying")
7598                  (crosspost "Crosspost" "Crossposting")))
7599         (copy-buf (save-excursion
7600                     (nnheader-set-temp-buffer " *copy article*")))
7601         art-group to-method new-xref article to-groups)
7602     (unless (assq action names)
7603       (error "Unknown action %s" action))
7604     ;; Read the newsgroup name.
7605     (when (and (not to-newsgroup)
7606                (not select-method))
7607       (setq to-newsgroup
7608             (gnus-read-move-group-name
7609              (cadr (assq action names))
7610              (symbol-value (intern (format "gnus-current-%s-group" action)))
7611              articles prefix))
7612       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7613     (setq to-method (or select-method
7614                         (gnus-server-to-method
7615                          (gnus-group-method to-newsgroup))))
7616     ;; Check the method we are to move this article to...
7617     (unless (gnus-check-backend-function
7618              'request-accept-article (car to-method))
7619       (error "%s does not support article copying" (car to-method)))
7620     (unless (gnus-check-server to-method)
7621       (error "Can't open server %s" (car to-method)))
7622     (gnus-message 6 "%s to %s: %s..."
7623                   (caddr (assq action names))
7624                   (or (car select-method) to-newsgroup) articles)
7625     (while articles
7626       (setq article (pop articles))
7627       (setq
7628        art-group
7629        (cond
7630         ;; Move the article.
7631         ((eq action 'move)
7632          ;; Remove this article from future suppression.
7633          (gnus-dup-unsuppress-article article)
7634          (gnus-request-move-article
7635           article                       ; Article to move
7636           gnus-newsgroup-name           ; From newsgroup
7637           (nth 1 (gnus-find-method-for-group
7638                   gnus-newsgroup-name)) ; Server
7639           (list 'gnus-request-accept-article
7640                 to-newsgroup (list 'quote select-method)
7641                 (not articles) t)       ; Accept form
7642           (not articles)))              ; Only save nov last time
7643         ;; Copy the article.
7644         ((eq action 'copy)
7645          (save-excursion
7646            (set-buffer copy-buf)
7647            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7648              (gnus-request-accept-article
7649               to-newsgroup select-method (not articles) t))))
7650         ;; Crosspost the article.
7651         ((eq action 'crosspost)
7652          (let ((xref (message-tokenize-header
7653                       (mail-header-xref (gnus-summary-article-header article))
7654                       " ")))
7655            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7656                                   ":" article))
7657            (unless xref
7658              (setq xref (list (system-name))))
7659            (setq new-xref
7660                  (concat
7661                   (mapconcat 'identity
7662                              (delete "Xref:" (delete new-xref xref))
7663                              " ")
7664                   " " new-xref))
7665            (save-excursion
7666              (set-buffer copy-buf)
7667              ;; First put the article in the destination group.
7668              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7669              (when (consp (setq art-group
7670                                 (gnus-request-accept-article
7671                                  to-newsgroup select-method (not articles))))
7672                (setq new-xref (concat new-xref " " (car art-group)
7673                                       ":" (cdr art-group)))
7674                ;; Now we have the new Xrefs header, so we insert
7675                ;; it and replace the new article.
7676                (nnheader-replace-header "Xref" new-xref)
7677                (gnus-request-replace-article
7678                 (cdr art-group) to-newsgroup (current-buffer))
7679                art-group))))))
7680       (cond
7681        ((not art-group)
7682         (gnus-message 1 "Couldn't %s article %s: %s"
7683                       (cadr (assq action names)) article
7684                       (nnheader-get-report (car to-method))))
7685        ((eq art-group 'junk)
7686         (when (eq action 'move)
7687           (gnus-summary-mark-article article gnus-canceled-mark)
7688           (gnus-message 4 "Deleted article %s" article)))
7689        (t
7690         (let* ((pto-group (gnus-group-prefixed-name
7691                            (car art-group) to-method))
7692                (entry
7693                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7694                (info (nth 2 entry))
7695                (to-group (gnus-info-group info))
7696                to-marks)
7697           ;; Update the group that has been moved to.
7698           (when (and info
7699                      (memq action '(move copy)))
7700             (unless (member to-group to-groups)
7701               (push to-group to-groups))
7702
7703             (unless (memq article gnus-newsgroup-unreads)
7704               (push 'read to-marks)
7705               (gnus-info-set-read
7706                info (gnus-add-to-range (gnus-info-read info)
7707                                        (list (cdr art-group)))))
7708
7709             ;; See whether the article is to be put in the cache.
7710             (let ((marks gnus-article-mark-lists)
7711                   (to-article (cdr art-group)))
7712
7713               ;; Enter the article into the cache in the new group,
7714               ;; if that is required.
7715               (when gnus-use-cache
7716                 (gnus-cache-possibly-enter-article
7717                  to-group to-article
7718                  (memq article gnus-newsgroup-marked)
7719                  (memq article gnus-newsgroup-dormant)
7720                  (memq article gnus-newsgroup-unreads)))
7721
7722               (when gnus-preserve-marks
7723                 ;; Copy any marks over to the new group.
7724                 (when (and (equal to-group gnus-newsgroup-name)
7725                            (not (memq article gnus-newsgroup-unreads)))
7726                   ;; Mark this article as read in this group.
7727                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7728                   (setcdr (gnus-active to-group) to-article)
7729                   (setcdr gnus-newsgroup-active to-article))
7730
7731                 (while marks
7732                   (when (memq article (symbol-value
7733                                        (intern (format "gnus-newsgroup-%s"
7734                                                        (caar marks)))))
7735                     (push (cdar marks) to-marks)
7736                     ;; If the other group is the same as this group,
7737                     ;; then we have to add the mark to the list.
7738                     (when (equal to-group gnus-newsgroup-name)
7739                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7740                            (cons to-article
7741                                  (symbol-value
7742                                   (intern (format "gnus-newsgroup-%s"
7743                                                   (caar marks)))))))
7744                     ;; Copy the marks to other group.
7745                     (gnus-add-marked-articles
7746                      to-group (cdar marks) (list to-article) info))
7747                   (setq marks (cdr marks)))
7748
7749                 (gnus-request-set-mark to-group (list (list (list to-article)
7750                                                             'set
7751                                                             to-marks))))
7752
7753               (gnus-dribble-enter
7754                (concat "(gnus-group-set-info '"
7755                        (gnus-prin1-to-string (gnus-get-info to-group))
7756                        ")"))))
7757
7758           ;; Update the Xref header in this article to point to
7759           ;; the new crossposted article we have just created.
7760           (when (eq action 'crosspost)
7761             (save-excursion
7762               (set-buffer copy-buf)
7763               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7764               (nnheader-replace-header "Xref" new-xref)
7765               (gnus-request-replace-article
7766                article gnus-newsgroup-name (current-buffer)))))
7767
7768         ;;;!!!Why is this necessary?
7769         (set-buffer gnus-summary-buffer)
7770
7771         (gnus-summary-goto-subject article)
7772         (when (eq action 'move)
7773           (gnus-summary-mark-article article gnus-canceled-mark))))
7774       (gnus-summary-remove-process-mark article))
7775     ;; Re-activate all groups that have been moved to.
7776     (while to-groups
7777       (save-excursion
7778         (set-buffer gnus-group-buffer)
7779         (when (gnus-group-goto-group (car to-groups) t)
7780           (gnus-group-get-new-news-this-group 1 t))
7781         (pop to-groups)))
7782
7783     (gnus-kill-buffer copy-buf)
7784     (gnus-summary-position-point)
7785     (gnus-set-mode-line 'summary)))
7786
7787 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7788   "Move the current article to a different newsgroup.
7789 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7790 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7791 re-spool using this method."
7792   (interactive "P")
7793   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7794
7795 (defun gnus-summary-crosspost-article (&optional n)
7796   "Crosspost the current article to some other group."
7797   (interactive "P")
7798   (gnus-summary-move-article n nil nil 'crosspost))
7799
7800 (defcustom gnus-summary-respool-default-method nil
7801   "Default method for respooling an article.
7802 If nil, use to the current newsgroup method."
7803   :type '(choice (gnus-select-method :value (nnml ""))
7804                  (const nil))
7805   :group 'gnus-summary-mail)
7806
7807 (defun gnus-summary-respool-article (&optional n method)
7808   "Respool the current article.
7809 The article will be squeezed through the mail spooling process again,
7810 which means that it will be put in some mail newsgroup or other
7811 depending on `nnmail-split-methods'.
7812 If N is a positive number, respool the N next articles.
7813 If N is a negative number, respool the N previous articles.
7814 If N is nil and any articles have been marked with the process mark,
7815 respool those articles instead.
7816
7817 Respooling can be done both from mail groups and \"real\" newsgroups.
7818 In the former case, the articles in question will be moved from the
7819 current group into whatever groups they are destined to.  In the
7820 latter case, they will be copied into the relevant groups."
7821   (interactive
7822    (list current-prefix-arg
7823          (let* ((methods (gnus-methods-using 'respool))
7824                 (methname
7825                  (symbol-name (or gnus-summary-respool-default-method
7826                                   (car (gnus-find-method-for-group
7827                                         gnus-newsgroup-name)))))
7828                 (method
7829                  (gnus-completing-read
7830                   methname "What backend do you want to use when respooling?"
7831                   methods nil t nil 'gnus-mail-method-history))
7832                 ms)
7833            (cond
7834             ((zerop (length (setq ms (gnus-servers-using-backend
7835                                       (intern method)))))
7836              (list (intern method) ""))
7837             ((= 1 (length ms))
7838              (car ms))
7839             (t
7840              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7841                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7842                            ms-alist))))))))
7843   (unless method
7844     (error "No method given for respooling"))
7845   (if (assoc (symbol-name
7846               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7847              (gnus-methods-using 'respool))
7848       (gnus-summary-move-article n nil method)
7849     (gnus-summary-copy-article n nil method)))
7850
7851 (defun gnus-summary-import-article (file)
7852   "Import an arbitrary file into a mail newsgroup."
7853   (interactive "fImport file: ")
7854   (let ((group gnus-newsgroup-name)
7855         (now (current-time))
7856         atts lines)
7857     (unless (gnus-check-backend-function 'request-accept-article group)
7858       (error "%s does not support article importing" group))
7859     (or (file-readable-p file)
7860         (not (file-regular-p file))
7861         (error "Can't read %s" file))
7862     (save-excursion
7863       (set-buffer (gnus-get-buffer-create " *import file*"))
7864       (erase-buffer)
7865       (nnheader-insert-file-contents file)
7866       (goto-char (point-min))
7867       (unless (nnheader-article-p)
7868         ;; This doesn't look like an article, so we fudge some headers.
7869         (setq atts (file-attributes file)
7870               lines (count-lines (point-min) (point-max)))
7871         (insert "From: " (read-string "From: ") "\n"
7872                 "Subject: " (read-string "Subject: ") "\n"
7873                 "Date: " (message-make-date (nth 5 atts))
7874                 "\n"
7875                 "Message-ID: " (message-make-message-id) "\n"
7876                 "Lines: " (int-to-string lines) "\n"
7877                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7878       (gnus-request-accept-article group nil t)
7879       (kill-buffer (current-buffer)))))
7880
7881 (defun gnus-summary-article-posted-p ()
7882   "Say whether the current (mail) article is available from news as well.
7883 This will be the case if the article has both been mailed and posted."
7884   (interactive)
7885   (let ((id (mail-header-references (gnus-summary-article-header)))
7886         (gnus-override-method (car (gnus-refer-article-methods))))
7887     (if (gnus-request-head id "")
7888         (gnus-message 2 "The current message was found on %s"
7889                       gnus-override-method)
7890       (gnus-message 2 "The current message couldn't be found on %s"
7891                     gnus-override-method)
7892       nil)))
7893
7894 (defun gnus-summary-expire-articles (&optional now)
7895   "Expire all articles that are marked as expirable in the current group."
7896   (interactive)
7897   (when (gnus-check-backend-function
7898          'request-expire-articles gnus-newsgroup-name)
7899     ;; This backend supports expiry.
7900     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7901            (expirable (if total
7902                           (progn
7903                             ;; We need to update the info for
7904                             ;; this group for `gnus-list-of-read-articles'
7905                             ;; to give us the right answer.
7906                             (gnus-run-hooks 'gnus-exit-group-hook)
7907                             (gnus-summary-update-info)
7908                             (gnus-list-of-read-articles gnus-newsgroup-name))
7909                         (setq gnus-newsgroup-expirable
7910                               (sort gnus-newsgroup-expirable '<))))
7911            (expiry-wait (if now 'immediate
7912                           (gnus-group-find-parameter
7913                            gnus-newsgroup-name 'expiry-wait)))
7914            (nnmail-expiry-target
7915             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7916                 nnmail-expiry-target))
7917            es)
7918       (when expirable
7919         ;; There are expirable articles in this group, so we run them
7920         ;; through the expiry process.
7921         (gnus-message 6 "Expiring articles...")
7922         (unless (gnus-check-group gnus-newsgroup-name)
7923           (error "Can't open server for %s" gnus-newsgroup-name))
7924         ;; The list of articles that weren't expired is returned.
7925         (save-excursion
7926           (if expiry-wait
7927               (let ((nnmail-expiry-wait-function nil)
7928                     (nnmail-expiry-wait expiry-wait))
7929                 (setq es (gnus-request-expire-articles
7930                           expirable gnus-newsgroup-name)))
7931             (setq es (gnus-request-expire-articles
7932                       expirable gnus-newsgroup-name)))
7933           (unless total
7934             (setq gnus-newsgroup-expirable es))
7935           ;; We go through the old list of expirable, and mark all
7936           ;; really expired articles as nonexistent.
7937           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
7938             (let ((gnus-use-cache nil))
7939               (while expirable
7940                 (unless (memq (car expirable) es)
7941                   (when (gnus-data-find (car expirable))
7942                     (gnus-summary-mark-article
7943                      (car expirable) gnus-canceled-mark)))
7944                 (setq expirable (cdr expirable))))))
7945         (gnus-message 6 "Expiring articles...done")))))
7946
7947 (defun gnus-summary-expire-articles-now ()
7948   "Expunge all expirable articles in the current group.
7949 This means that *all* articles that are marked as expirable will be
7950 deleted forever, right now."
7951   (interactive)
7952   (or gnus-expert-user
7953       (gnus-yes-or-no-p
7954        "Are you really, really, really sure you want to delete all these messages? ")
7955       (error "Phew!"))
7956   (gnus-summary-expire-articles t))
7957
7958 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7959 (defun gnus-summary-delete-article (&optional n)
7960   "Delete the N next (mail) articles.
7961 This command actually deletes articles.  This is not a marking
7962 command.  The article will disappear forever from your life, never to
7963 return.
7964 If N is negative, delete backwards.
7965 If N is nil and articles have been marked with the process mark,
7966 delete these instead."
7967   (interactive "P")
7968   (unless (gnus-check-backend-function 'request-expire-articles
7969                                        gnus-newsgroup-name)
7970     (error "The current newsgroup does not support article deletion"))
7971   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7972     (error "Couldn't open server"))
7973   ;; Compute the list of articles to delete.
7974   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7975         not-deleted)
7976     (if (and gnus-novice-user
7977              (not (gnus-yes-or-no-p
7978                    (format "Do you really want to delete %s forever? "
7979                            (if (> (length articles) 1)
7980                                (format "these %s articles" (length articles))
7981                              "this article")))))
7982         ()
7983       ;; Delete the articles.
7984       (setq not-deleted (gnus-request-expire-articles
7985                          articles gnus-newsgroup-name 'force))
7986       (while articles
7987         (gnus-summary-remove-process-mark (car articles))
7988         ;; The backend might not have been able to delete the article
7989         ;; after all.
7990         (unless (memq (car articles) not-deleted)
7991           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7992         (setq articles (cdr articles)))
7993       (when not-deleted
7994         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7995     (gnus-summary-position-point)
7996     (gnus-set-mode-line 'summary)
7997     not-deleted))
7998
7999 (defun gnus-summary-edit-article (&optional arg)
8000   "Edit the current article.
8001 This will have permanent effect only in mail groups.
8002 If ARG is nil, edit the decoded articles.
8003 If ARG is 1, edit the raw articles. 
8004 If ARG is 2, edit the raw articles even in read-only groups.
8005 If ARG is 3, edit the articles with the current handles.
8006 Otherwise, allow editing of articles even in read-only
8007 groups."
8008   (interactive "P")
8009   (let (force raw current-handles)
8010     (cond 
8011      ((null arg))
8012      ((eq arg 1) (setq raw t))
8013      ((eq arg 2) (setq raw t
8014                        force t))
8015      ((eq arg 3) (setq current-handles 
8016                        (and (gnus-buffer-live-p gnus-article-buffer)
8017                             (with-current-buffer gnus-article-buffer
8018                               (prog1
8019                                   gnus-article-mime-handles
8020                                   (setq gnus-article-mime-handles nil))))))
8021      (t (setq force t)))
8022     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8023         (error "Can't edit the raw article in group nndraft:drafts."))
8024     (save-excursion
8025       (set-buffer gnus-summary-buffer)
8026       (let ((mail-parse-charset gnus-newsgroup-charset)
8027             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8028         (gnus-set-global-variables)
8029         (when (and (not force)
8030                    (gnus-group-read-only-p))
8031           (error "The current newsgroup does not support article editing"))
8032         (gnus-summary-show-article t)
8033         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8034           (with-current-buffer gnus-article-buffer
8035             (mm-enable-multibyte-mule4)))
8036         (if (equal gnus-newsgroup-name "nndraft:drafts")
8037             (setq raw t))
8038         (gnus-article-edit-article
8039          (if raw 'ignore 
8040            `(lambda () 
8041               (let ((mbl mml-buffer-list))
8042                 (setq mml-buffer-list nil)
8043                 (mime-to-mml ,'current-handles)
8044                 (make-local-hook 'kill-buffer-hook)
8045                 (let ((mbl1 mml-buffer-list))
8046                   (setq mml-buffer-list mbl)
8047                   (set (make-local-variable 'mml-buffer-list) mbl1))
8048                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8049          `(lambda (no-highlight)
8050             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8051                   (message-options message-options)
8052                   (message-options-set-recipient)
8053                   (mail-parse-ignored-charsets 
8054                    ',gnus-newsgroup-ignored-charsets))
8055               ,(if (not raw) '(progn 
8056                                 (mml-to-mime)
8057                                 (mml-destroy-buffers)
8058                                 (remove-hook 'kill-buffer-hook 
8059                                              'mml-destroy-buffers t)
8060                                 (kill-local-variable 'mml-buffer-list)))
8061               (gnus-summary-edit-article-done
8062                ,(or (mail-header-references gnus-current-headers) "")
8063                ,(gnus-group-read-only-p) 
8064                ,gnus-summary-buffer no-highlight))))))))
8065
8066 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8067
8068 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8069                                                  no-highlight)
8070   "Make edits to the current article permanent."
8071   (interactive)
8072   (save-excursion
8073     ;; The buffer restriction contains the entire article if it exists.
8074     (when (article-goto-body)
8075       (let ((lines (count-lines (point) (point-max)))
8076             (length (- (point-max) (point)))
8077             (case-fold-search t)
8078             (body (copy-marker (point))))
8079         (goto-char (point-min))
8080         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8081           (delete-region (match-beginning 1) (match-end 1))
8082           (insert (number-to-string length)))
8083         (goto-char (point-min))
8084         (when (re-search-forward
8085                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8086           (delete-region (match-beginning 1) (match-end 1))
8087           (insert (number-to-string length)))
8088         (goto-char (point-min))
8089         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8090           (delete-region (match-beginning 1) (match-end 1))
8091           (insert (number-to-string lines))))))
8092   ;; Replace the article.
8093   (let ((buf (current-buffer)))
8094     (with-temp-buffer
8095       (insert-buffer-substring buf)
8096       
8097       (if (and (not read-only)
8098                (not (gnus-request-replace-article
8099                      (cdr gnus-article-current) (car gnus-article-current)
8100                      (current-buffer) t)))
8101           (error "Couldn't replace article")
8102         ;; Update the summary buffer.
8103         (if (and references
8104                  (equal (message-tokenize-header references " ")
8105                         (message-tokenize-header
8106                          (or (message-fetch-field "references") "") " ")))
8107             ;; We only have to update this line.
8108             (save-excursion
8109               (save-restriction
8110                 (message-narrow-to-head)
8111                 (let ((head (buffer-string))
8112                       header)
8113                   (with-temp-buffer
8114                     (insert (format "211 %d Article retrieved.\n"
8115                                     (cdr gnus-article-current)))
8116                     (insert head)
8117                     (insert ".\n")
8118                     (let ((nntp-server-buffer (current-buffer)))
8119                       (setq header (car (gnus-get-newsgroup-headers
8120                                          (save-excursion
8121                                            (set-buffer gnus-summary-buffer)
8122                                            gnus-newsgroup-dependencies)
8123                                          t))))
8124                     (save-excursion
8125                       (set-buffer gnus-summary-buffer)
8126                       (gnus-data-set-header
8127                        (gnus-data-find (cdr gnus-article-current))
8128                        header)
8129                       (gnus-summary-update-article-line
8130                        (cdr gnus-article-current) header))))))
8131           ;; Update threads.
8132           (set-buffer (or buffer gnus-summary-buffer))
8133           (gnus-summary-update-article (cdr gnus-article-current)))
8134         ;; Prettify the article buffer again.
8135         (unless no-highlight
8136           (save-excursion
8137             (set-buffer gnus-article-buffer)
8138             ;;;!!! Fix this -- article should be rehighlighted.
8139             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8140             (set-buffer gnus-original-article-buffer)
8141             (gnus-request-article
8142              (cdr gnus-article-current)
8143              (car gnus-article-current) (current-buffer))))
8144         ;; Prettify the summary buffer line.
8145         (when (gnus-visual-p 'summary-highlight 'highlight)
8146           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8147
8148 (defun gnus-summary-edit-wash (key)
8149   "Perform editing command KEY in the article buffer."
8150   (interactive
8151    (list
8152     (progn
8153       (message "%s" (concat (this-command-keys) "- "))
8154       (read-char))))
8155   (message "")
8156   (gnus-summary-edit-article)
8157   (execute-kbd-macro (concat (this-command-keys) key))
8158   (gnus-article-edit-done))
8159
8160 ;;; Respooling
8161
8162 (defun gnus-summary-respool-query (&optional silent trace)
8163   "Query where the respool algorithm would put this article."
8164   (interactive)
8165   (let (gnus-mark-article-hook)
8166     (gnus-summary-select-article)
8167     (save-excursion
8168       (set-buffer gnus-original-article-buffer)
8169       (save-restriction
8170         (message-narrow-to-head)
8171         (let ((groups (nnmail-article-group 'identity trace)))
8172           (unless silent
8173             (if groups
8174                 (message "This message would go to %s"
8175                          (mapconcat 'car groups ", "))
8176               (message "This message would go to no groups"))
8177             groups))))))
8178
8179 (defun gnus-summary-respool-trace ()
8180   "Trace where the respool algorithm would put this article.
8181 Display a buffer showing all fancy splitting patterns which matched."
8182   (interactive)
8183   (gnus-summary-respool-query nil t))
8184
8185 ;; Summary marking commands.
8186
8187 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8188   "Mark articles which has the same subject as read, and then select the next.
8189 If UNMARK is positive, remove any kind of mark.
8190 If UNMARK is negative, tick articles."
8191   (interactive "P")
8192   (when unmark
8193     (setq unmark (prefix-numeric-value unmark)))
8194   (let ((count
8195          (gnus-summary-mark-same-subject
8196           (gnus-summary-article-subject) unmark)))
8197     ;; Select next unread article.  If auto-select-same mode, should
8198     ;; select the first unread article.
8199     (gnus-summary-next-article t (and gnus-auto-select-same
8200                                       (gnus-summary-article-subject)))
8201     (gnus-message 7 "%d article%s marked as %s"
8202                   count (if (= count 1) " is" "s are")
8203                   (if unmark "unread" "read"))))
8204
8205 (defun gnus-summary-kill-same-subject (&optional unmark)
8206   "Mark articles which has the same subject as read.
8207 If UNMARK is positive, remove any kind of mark.
8208 If UNMARK is negative, tick articles."
8209   (interactive "P")
8210   (when unmark
8211     (setq unmark (prefix-numeric-value unmark)))
8212   (let ((count
8213          (gnus-summary-mark-same-subject
8214           (gnus-summary-article-subject) unmark)))
8215     ;; If marked as read, go to next unread subject.
8216     (when (null unmark)
8217       ;; Go to next unread subject.
8218       (gnus-summary-next-subject 1 t))
8219     (gnus-message 7 "%d articles are marked as %s"
8220                   count (if unmark "unread" "read"))))
8221
8222 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8223   "Mark articles with same SUBJECT as read, and return marked number.
8224 If optional argument UNMARK is positive, remove any kinds of marks.
8225 If optional argument UNMARK is negative, mark articles as unread instead."
8226   (let ((count 1))
8227     (save-excursion
8228       (cond
8229        ((null unmark)                   ; Mark as read.
8230         (while (and
8231                 (progn
8232                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8233                   (gnus-summary-show-thread) t)
8234                 (gnus-summary-find-subject subject))
8235           (setq count (1+ count))))
8236        ((> unmark 0)                    ; Tick.
8237         (while (and
8238                 (progn
8239                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8240                   (gnus-summary-show-thread) t)
8241                 (gnus-summary-find-subject subject))
8242           (setq count (1+ count))))
8243        (t                               ; Mark as unread.
8244         (while (and
8245                 (progn
8246                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8247                   (gnus-summary-show-thread) t)
8248                 (gnus-summary-find-subject subject))
8249           (setq count (1+ count)))))
8250       (gnus-set-mode-line 'summary)
8251       ;; Return the number of marked articles.
8252       count)))
8253
8254 (defun gnus-summary-mark-as-processable (n &optional unmark)
8255   "Set the process mark on the next N articles.
8256 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8257 the process mark instead.  The difference between N and the actual
8258 number of articles marked is returned."
8259   (interactive "p")
8260   (let ((backward (< n 0))
8261         (n (abs n)))
8262     (while (and
8263             (> n 0)
8264             (if unmark
8265                 (gnus-summary-remove-process-mark
8266                  (gnus-summary-article-number))
8267               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8268             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8269       (setq n (1- n)))
8270     (when (/= 0 n)
8271       (gnus-message 7 "No more articles"))
8272     (gnus-summary-recenter)
8273     (gnus-summary-position-point)
8274     n))
8275
8276 (defun gnus-summary-unmark-as-processable (n)
8277   "Remove the process mark from the next N articles.
8278 If N is negative, unmark backward instead.  The difference between N and
8279 the actual number of articles unmarked is returned."
8280   (interactive "p")
8281   (gnus-summary-mark-as-processable n t))
8282
8283 (defun gnus-summary-unmark-all-processable ()
8284   "Remove the process mark from all articles."
8285   (interactive)
8286   (save-excursion
8287     (while gnus-newsgroup-processable
8288       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8289   (gnus-summary-position-point))
8290
8291 (defun gnus-summary-mark-as-expirable (n)
8292   "Mark N articles forward as expirable.
8293 If N is negative, mark backward instead.  The difference between N and
8294 the actual number of articles marked is returned."
8295   (interactive "p")
8296   (gnus-summary-mark-forward n gnus-expirable-mark))
8297
8298 (defun gnus-summary-mark-article-as-replied (article)
8299   "Mark ARTICLE replied and update the summary line."
8300   (push article gnus-newsgroup-replied)
8301   (let ((buffer-read-only nil))
8302     (when (gnus-summary-goto-subject article nil t)
8303       (gnus-summary-update-secondary-mark article))))
8304
8305 (defun gnus-summary-set-bookmark (article)
8306   "Set a bookmark in current article."
8307   (interactive (list (gnus-summary-article-number)))
8308   (when (or (not (get-buffer gnus-article-buffer))
8309             (not gnus-current-article)
8310             (not gnus-article-current)
8311             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8312     (error "No current article selected"))
8313   ;; Remove old bookmark, if one exists.
8314   (let ((old (assq article gnus-newsgroup-bookmarks)))
8315     (when old
8316       (setq gnus-newsgroup-bookmarks
8317             (delq old gnus-newsgroup-bookmarks))))
8318   ;; Set the new bookmark, which is on the form
8319   ;; (article-number . line-number-in-body).
8320   (push
8321    (cons article
8322          (save-excursion
8323            (set-buffer gnus-article-buffer)
8324            (count-lines
8325             (min (point)
8326                  (save-excursion
8327                    (goto-char (point-min))
8328                    (search-forward "\n\n" nil t)
8329                    (point)))
8330             (point))))
8331    gnus-newsgroup-bookmarks)
8332   (gnus-message 6 "A bookmark has been added to the current article."))
8333
8334 (defun gnus-summary-remove-bookmark (article)
8335   "Remove the bookmark from the current article."
8336   (interactive (list (gnus-summary-article-number)))
8337   ;; Remove old bookmark, if one exists.
8338   (let ((old (assq article gnus-newsgroup-bookmarks)))
8339     (if old
8340         (progn
8341           (setq gnus-newsgroup-bookmarks
8342                 (delq old gnus-newsgroup-bookmarks))
8343           (gnus-message 6 "Removed bookmark."))
8344       (gnus-message 6 "No bookmark in current article."))))
8345
8346 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8347 (defun gnus-summary-mark-as-dormant (n)
8348   "Mark N articles forward as dormant.
8349 If N is negative, mark backward instead.  The difference between N and
8350 the actual number of articles marked is returned."
8351   (interactive "p")
8352   (gnus-summary-mark-forward n gnus-dormant-mark))
8353
8354 (defun gnus-summary-set-process-mark (article)
8355   "Set the process mark on ARTICLE and update the summary line."
8356   (setq gnus-newsgroup-processable
8357         (cons article
8358               (delq article gnus-newsgroup-processable)))
8359   (when (gnus-summary-goto-subject article)
8360     (gnus-summary-show-thread)
8361     (gnus-summary-goto-subject article)
8362     (gnus-summary-update-secondary-mark article)))
8363
8364 (defun gnus-summary-remove-process-mark (article)
8365   "Remove the process mark from ARTICLE and update the summary line."
8366   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8367   (when (gnus-summary-goto-subject article)
8368     (gnus-summary-show-thread)
8369     (gnus-summary-goto-subject article)
8370     (gnus-summary-update-secondary-mark article)))
8371
8372 (defun gnus-summary-set-saved-mark (article)
8373   "Set the process mark on ARTICLE and update the summary line."
8374   (push article gnus-newsgroup-saved)
8375   (when (gnus-summary-goto-subject article)
8376     (gnus-summary-update-secondary-mark article)))
8377
8378 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8379   "Mark N articles as read forwards.
8380 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8381 The difference between N and the actual number of articles marked is
8382 returned.
8383 Iff NO-EXPIRE, auto-expiry will be inhibited."
8384   (interactive "p")
8385   (gnus-summary-show-thread)
8386   (let ((backward (< n 0))
8387         (gnus-summary-goto-unread
8388          (and gnus-summary-goto-unread
8389               (not (eq gnus-summary-goto-unread 'never))
8390               (not (memq mark (list gnus-unread-mark
8391                                     gnus-ticked-mark gnus-dormant-mark)))))
8392         (n (abs n))
8393         (mark (or mark gnus-del-mark)))
8394     (while (and (> n 0)
8395                 (gnus-summary-mark-article nil mark no-expire)
8396                 (zerop (gnus-summary-next-subject
8397                         (if backward -1 1)
8398                         (and gnus-summary-goto-unread
8399                              (not (eq gnus-summary-goto-unread 'never)))
8400                         t)))
8401       (setq n (1- n)))
8402     (when (/= 0 n)
8403       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8404     (gnus-summary-recenter)
8405     (gnus-summary-position-point)
8406     (gnus-set-mode-line 'summary)
8407     n))
8408
8409 (defun gnus-summary-mark-article-as-read (mark)
8410   "Mark the current article quickly as read with MARK."
8411   (let ((article (gnus-summary-article-number)))
8412     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8413     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8414     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8415     (push (cons article mark) gnus-newsgroup-reads)
8416     ;; Possibly remove from cache, if that is used.
8417     (when gnus-use-cache
8418       (gnus-cache-enter-remove-article article))
8419     ;; Allow the backend to change the mark.
8420     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8421     ;; Check for auto-expiry.
8422     (when (and gnus-newsgroup-auto-expire
8423                (memq mark gnus-auto-expirable-marks))
8424       (setq mark gnus-expirable-mark)
8425       ;; Let the backend know about the mark change.
8426       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8427       (push article gnus-newsgroup-expirable))
8428     ;; Set the mark in the buffer.
8429     (gnus-summary-update-mark mark 'unread)
8430     t))
8431
8432 (defun gnus-summary-mark-article-as-unread (mark)
8433   "Mark the current article quickly as unread with MARK."
8434   (let* ((article (gnus-summary-article-number))
8435          (old-mark (gnus-summary-article-mark article)))
8436     ;; Allow the backend to change the mark.
8437     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8438     (if (eq mark old-mark)
8439         t
8440       (if (<= article 0)
8441           (progn
8442             (gnus-error 1 "Can't mark negative article numbers")
8443             nil)
8444         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8445         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8446         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8447         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8448         (cond ((= mark gnus-ticked-mark)
8449                (push article gnus-newsgroup-marked))
8450               ((= mark gnus-dormant-mark)
8451                (push article gnus-newsgroup-dormant))
8452               (t
8453                (push article gnus-newsgroup-unreads)))
8454         (gnus-pull article gnus-newsgroup-reads)
8455
8456         ;; See whether the article is to be put in the cache.
8457         (and gnus-use-cache
8458              (vectorp (gnus-summary-article-header article))
8459              (save-excursion
8460                (gnus-cache-possibly-enter-article
8461                 gnus-newsgroup-name article
8462                 (= mark gnus-ticked-mark)
8463                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8464
8465         ;; Fix the mark.
8466         (gnus-summary-update-mark mark 'unread)
8467         t))))
8468
8469 (defun gnus-summary-mark-article (&optional article mark no-expire)
8470   "Mark ARTICLE with MARK.  MARK can be any character.
8471 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8472 `??' (dormant) and `?E' (expirable).
8473 If MARK is nil, then the default character `?r' is used.
8474 If ARTICLE is nil, then the article on the current line will be
8475 marked.
8476 Iff NO-EXPIRE, auto-expiry will be inhibited."
8477   ;; The mark might be a string.
8478   (when (stringp mark)
8479     (setq mark (aref mark 0)))
8480   ;; If no mark is given, then we check auto-expiring.
8481   (when (null mark)
8482     (setq mark gnus-del-mark))
8483   (when (and (not no-expire)
8484              gnus-newsgroup-auto-expire
8485              (memq mark gnus-auto-expirable-marks))
8486     (setq mark gnus-expirable-mark))
8487   (let ((article (or article (gnus-summary-article-number)))
8488         (old-mark (gnus-summary-article-mark article)))
8489     ;; Allow the backend to change the mark.
8490     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8491     (if (eq mark old-mark)
8492         t
8493       (unless article
8494         (error "No article on current line"))
8495       (if (not (if (or (= mark gnus-unread-mark)
8496                        (= mark gnus-ticked-mark)
8497                        (= mark gnus-dormant-mark))
8498                    (gnus-mark-article-as-unread article mark)
8499                  (gnus-mark-article-as-read article mark)))
8500           t
8501         ;; See whether the article is to be put in the cache.
8502         (and gnus-use-cache
8503              (not (= mark gnus-canceled-mark))
8504              (vectorp (gnus-summary-article-header article))
8505              (save-excursion
8506                (gnus-cache-possibly-enter-article
8507                 gnus-newsgroup-name article
8508                 (= mark gnus-ticked-mark)
8509                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8510
8511         (when (gnus-summary-goto-subject article nil t)
8512           (let ((buffer-read-only nil))
8513             (gnus-summary-show-thread)
8514             ;; Fix the mark.
8515             (gnus-summary-update-mark mark 'unread)
8516             t))))))
8517
8518 (defun gnus-summary-update-secondary-mark (article)
8519   "Update the secondary (read, process, cache) mark."
8520   (gnus-summary-update-mark
8521    (cond ((memq article gnus-newsgroup-processable)
8522           gnus-process-mark)
8523          ((memq article gnus-newsgroup-cached)
8524           gnus-cached-mark)
8525          ((memq article gnus-newsgroup-replied)
8526           gnus-replied-mark)
8527          ((memq article gnus-newsgroup-saved)
8528           gnus-saved-mark)
8529          (t gnus-unread-mark))
8530    'replied)
8531   (when (gnus-visual-p 'summary-highlight 'highlight)
8532     (gnus-run-hooks 'gnus-summary-update-hook))
8533   t)
8534
8535 (defun gnus-summary-update-mark (mark type)
8536   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8537         (buffer-read-only nil))
8538     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8539     (when forward
8540       (when (looking-at "\r")
8541         (incf forward))
8542       (when (<= (+ forward (point)) (point-max))
8543         ;; Go to the right position on the line.
8544         (goto-char (+ forward (point)))
8545         ;; Replace the old mark with the new mark.
8546         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8547         ;; Optionally update the marks by some user rule.
8548         (when (eq type 'unread)
8549           (gnus-data-set-mark
8550            (gnus-data-find (gnus-summary-article-number)) mark)
8551           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8552
8553 (defun gnus-mark-article-as-read (article &optional mark)
8554   "Enter ARTICLE in the pertinent lists and remove it from others."
8555   ;; Make the article expirable.
8556   (let ((mark (or mark gnus-del-mark)))
8557     (if (= mark gnus-expirable-mark)
8558         (push article gnus-newsgroup-expirable)
8559       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8560     ;; Remove from unread and marked lists.
8561     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8562     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8563     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8564     (push (cons article mark) gnus-newsgroup-reads)
8565     ;; Possibly remove from cache, if that is used.
8566     (when gnus-use-cache
8567       (gnus-cache-enter-remove-article article))
8568     t))
8569
8570 (defun gnus-mark-article-as-unread (article &optional mark)
8571   "Enter ARTICLE in the pertinent lists and remove it from others."
8572   (let ((mark (or mark gnus-ticked-mark)))
8573     (if (<= article 0)
8574         (progn
8575           (gnus-error 1 "Can't mark negative article numbers")
8576           nil)
8577       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8578             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8579             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8580             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8581
8582       ;; Unsuppress duplicates?
8583       (when gnus-suppress-duplicates
8584         (gnus-dup-unsuppress-article article))
8585
8586       (cond ((= mark gnus-ticked-mark)
8587              (push article gnus-newsgroup-marked))
8588             ((= mark gnus-dormant-mark)
8589              (push article gnus-newsgroup-dormant))
8590             (t
8591              (push article gnus-newsgroup-unreads)))
8592       (gnus-pull article gnus-newsgroup-reads)
8593       t)))
8594
8595 (defalias 'gnus-summary-mark-as-unread-forward
8596   'gnus-summary-tick-article-forward)
8597 (make-obsolete 'gnus-summary-mark-as-unread-forward
8598                'gnus-summary-tick-article-forward)
8599 (defun gnus-summary-tick-article-forward (n)
8600   "Tick N articles forwards.
8601 If N is negative, tick backwards instead.
8602 The difference between N and the number of articles ticked is returned."
8603   (interactive "p")
8604   (gnus-summary-mark-forward n gnus-ticked-mark))
8605
8606 (defalias 'gnus-summary-mark-as-unread-backward
8607   'gnus-summary-tick-article-backward)
8608 (make-obsolete 'gnus-summary-mark-as-unread-backward
8609                'gnus-summary-tick-article-backward)
8610 (defun gnus-summary-tick-article-backward (n)
8611   "Tick N articles backwards.
8612 The difference between N and the number of articles ticked is returned."
8613   (interactive "p")
8614   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8615
8616 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8617 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8618 (defun gnus-summary-tick-article (&optional article clear-mark)
8619   "Mark current article as unread.
8620 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8621 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8622   (interactive)
8623   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8624                                        gnus-ticked-mark)))
8625
8626 (defun gnus-summary-mark-as-read-forward (n)
8627   "Mark N articles as read forwards.
8628 If N is negative, mark backwards instead.
8629 The difference between N and the actual number of articles marked is
8630 returned."
8631   (interactive "p")
8632   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8633
8634 (defun gnus-summary-mark-as-read-backward (n)
8635   "Mark the N articles as read backwards.
8636 The difference between N and the actual number of articles marked is
8637 returned."
8638   (interactive "p")
8639   (gnus-summary-mark-forward
8640    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8641
8642 (defun gnus-summary-mark-as-read (&optional article mark)
8643   "Mark current article as read.
8644 ARTICLE specifies the article to be marked as read.
8645 MARK specifies a string to be inserted at the beginning of the line."
8646   (gnus-summary-mark-article article mark))
8647
8648 (defun gnus-summary-clear-mark-forward (n)
8649   "Clear marks from N articles forward.
8650 If N is negative, clear backward instead.
8651 The difference between N and the number of marks cleared is returned."
8652   (interactive "p")
8653   (gnus-summary-mark-forward n gnus-unread-mark))
8654
8655 (defun gnus-summary-clear-mark-backward (n)
8656   "Clear marks from N articles backward.
8657 The difference between N and the number of marks cleared is returned."
8658   (interactive "p")
8659   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8660
8661 (defun gnus-summary-mark-unread-as-read ()
8662   "Intended to be used by `gnus-summary-mark-article-hook'."
8663   (when (memq gnus-current-article gnus-newsgroup-unreads)
8664     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8665
8666 (defun gnus-summary-mark-read-and-unread-as-read ()
8667   "Intended to be used by `gnus-summary-mark-article-hook'."
8668   (let ((mark (gnus-summary-article-mark)))
8669     (when (or (gnus-unread-mark-p mark)
8670               (gnus-read-mark-p mark))
8671       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8672
8673 (defun gnus-summary-mark-region-as-read (point mark all)
8674   "Mark all unread articles between point and mark as read.
8675 If given a prefix, mark all articles between point and mark as read,
8676 even ticked and dormant ones."
8677   (interactive "r\nP")
8678   (save-excursion
8679     (let (article)
8680       (goto-char point)
8681       (beginning-of-line)
8682       (while (and
8683               (< (point) mark)
8684               (progn
8685                 (when (or all
8686                           (memq (setq article (gnus-summary-article-number))
8687                                 gnus-newsgroup-unreads))
8688                   (gnus-summary-mark-article article gnus-del-mark))
8689                 t)
8690               (gnus-summary-find-next))))))
8691
8692 (defun gnus-summary-mark-below (score mark)
8693   "Mark articles with score less than SCORE with MARK."
8694   (interactive "P\ncMark: ")
8695   (setq score (if score
8696                   (prefix-numeric-value score)
8697                 (or gnus-summary-default-score 0)))
8698   (save-excursion
8699     (set-buffer gnus-summary-buffer)
8700     (goto-char (point-min))
8701     (while
8702         (progn
8703           (and (< (gnus-summary-article-score) score)
8704                (gnus-summary-mark-article nil mark))
8705           (gnus-summary-find-next)))))
8706
8707 (defun gnus-summary-kill-below (&optional score)
8708   "Mark articles with score below SCORE as read."
8709   (interactive "P")
8710   (gnus-summary-mark-below score gnus-killed-mark))
8711
8712 (defun gnus-summary-clear-above (&optional score)
8713   "Clear all marks from articles with score above SCORE."
8714   (interactive "P")
8715   (gnus-summary-mark-above score gnus-unread-mark))
8716
8717 (defun gnus-summary-tick-above (&optional score)
8718   "Tick all articles with score above SCORE."
8719   (interactive "P")
8720   (gnus-summary-mark-above score gnus-ticked-mark))
8721
8722 (defun gnus-summary-mark-above (score mark)
8723   "Mark articles with score over SCORE with MARK."
8724   (interactive "P\ncMark: ")
8725   (setq score (if score
8726                   (prefix-numeric-value score)
8727                 (or gnus-summary-default-score 0)))
8728   (save-excursion
8729     (set-buffer gnus-summary-buffer)
8730     (goto-char (point-min))
8731     (while (and (progn
8732                   (when (> (gnus-summary-article-score) score)
8733                     (gnus-summary-mark-article nil mark))
8734                   t)
8735                 (gnus-summary-find-next)))))
8736
8737 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8738 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8739 (defun gnus-summary-limit-include-expunged (&optional no-error)
8740   "Display all the hidden articles that were expunged for low scores."
8741   (interactive)
8742   (let ((buffer-read-only nil))
8743     (let ((scored gnus-newsgroup-scored)
8744           headers h)
8745       (while scored
8746         (unless (gnus-summary-goto-subject (caar scored))
8747           (and (setq h (gnus-summary-article-header (caar scored)))
8748                (< (cdar scored) gnus-summary-expunge-below)
8749                (push h headers)))
8750         (setq scored (cdr scored)))
8751       (if (not headers)
8752           (when (not no-error)
8753             (error "No expunged articles hidden"))
8754         (goto-char (point-min))
8755         (gnus-summary-prepare-unthreaded (nreverse headers))
8756         (goto-char (point-min))
8757         (gnus-summary-position-point)
8758         t))))
8759
8760 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8761   "Mark all unread articles in this newsgroup as read.
8762 If prefix argument ALL is non-nil, ticked and dormant articles will
8763 also be marked as read.
8764 If QUIETLY is non-nil, no questions will be asked.
8765 If TO-HERE is non-nil, it should be a point in the buffer.  All
8766 articles before this point will be marked as read.
8767 Note that this function will only catch up the unread article
8768 in the current summary buffer limitation.
8769 The number of articles marked as read is returned."
8770   (interactive "P")
8771   (prog1
8772       (save-excursion
8773         (when (or quietly
8774                   (not gnus-interactive-catchup) ;Without confirmation?
8775                   gnus-expert-user
8776                   (gnus-y-or-n-p
8777                    (if all
8778                        "Mark absolutely all articles as read? "
8779                      "Mark all unread articles as read? ")))
8780           (if (and not-mark
8781                    (not gnus-newsgroup-adaptive)
8782                    (not gnus-newsgroup-auto-expire)
8783                    (not gnus-suppress-duplicates)
8784                    (or (not gnus-use-cache)
8785                        (eq gnus-use-cache 'passive)))
8786               (progn
8787                 (when all
8788                   (setq gnus-newsgroup-marked nil
8789                         gnus-newsgroup-dormant nil))
8790                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8791             ;; We actually mark all articles as canceled, which we
8792             ;; have to do when using auto-expiry or adaptive scoring.
8793             (gnus-summary-show-all-threads)
8794             (when (gnus-summary-first-subject (not all) t)
8795               (while (and
8796                       (if to-here (< (point) to-here) t)
8797                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8798                       (gnus-summary-find-next (not all) nil nil t))))
8799             (gnus-set-mode-line 'summary))
8800           t))
8801     (gnus-summary-position-point)))
8802
8803 (defun gnus-summary-catchup-to-here (&optional all)
8804   "Mark all unticked articles before the current one as read.
8805 If ALL is non-nil, also mark ticked and dormant articles as read."
8806   (interactive "P")
8807   (save-excursion
8808     (gnus-save-hidden-threads
8809       (let ((beg (point)))
8810         ;; We check that there are unread articles.
8811         (when (or all (gnus-summary-find-prev))
8812           (gnus-summary-catchup all t beg)))))
8813   (gnus-summary-position-point))
8814
8815 (defun gnus-summary-catchup-all (&optional quietly)
8816   "Mark all articles in this newsgroup as read."
8817   (interactive "P")
8818   (gnus-summary-catchup t quietly))
8819
8820 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8821   "Mark all unread articles in this group as read, then exit.
8822 If prefix argument ALL is non-nil, all articles are marked as read."
8823   (interactive "P")
8824   (when (gnus-summary-catchup all quietly nil 'fast)
8825     ;; Select next newsgroup or exit.
8826     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8827              (eq gnus-auto-select-next 'quietly))
8828         (gnus-summary-next-group nil)
8829       (gnus-summary-exit))))
8830
8831 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8832   "Mark all articles in this newsgroup as read, and then exit."
8833   (interactive "P")
8834   (gnus-summary-catchup-and-exit t quietly))
8835
8836 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8837   "Mark all articles in this group as read and select the next group.
8838 If given a prefix, mark all articles, unread as well as ticked, as
8839 read."
8840   (interactive "P")
8841   (save-excursion
8842     (gnus-summary-catchup all))
8843   (gnus-summary-next-group))
8844
8845 ;;;
8846 ;;; with article
8847 ;;;
8848
8849 (defmacro gnus-with-article (article &rest forms)
8850   "Select ARTICLE and perform FORMS in the original article buffer.
8851 Then replace the article with the result."
8852   `(progn
8853      ;; We don't want the article to be marked as read.
8854      (let (gnus-mark-article-hook)
8855        (gnus-summary-select-article t t nil ,article))
8856      (set-buffer gnus-original-article-buffer)
8857      ,@forms
8858      (if (not (gnus-check-backend-function
8859                'request-replace-article (car gnus-article-current)))
8860          (gnus-message 5 "Read-only group; not replacing")
8861        (unless (gnus-request-replace-article
8862                 ,article (car gnus-article-current)
8863                 (current-buffer) t)
8864          (error "Couldn't replace article")))
8865      ;; The cache and backlog have to be flushed somewhat.
8866      (when gnus-keep-backlog
8867        (gnus-backlog-remove-article
8868         (car gnus-article-current) (cdr gnus-article-current)))
8869      (when gnus-use-cache
8870        (gnus-cache-update-article
8871         (car gnus-article-current) (cdr gnus-article-current)))))
8872
8873 (put 'gnus-with-article 'lisp-indent-function 1)
8874 (put 'gnus-with-article 'edebug-form-spec '(form body))
8875
8876 ;; Thread-based commands.
8877
8878 (defun gnus-summary-articles-in-thread (&optional article)
8879   "Return a list of all articles in the current thread.
8880 If ARTICLE is non-nil, return all articles in the thread that starts
8881 with that article."
8882   (let* ((article (or article (gnus-summary-article-number)))
8883          (data (gnus-data-find-list article))
8884          (top-level (gnus-data-level (car data)))
8885          (top-subject
8886           (cond ((null gnus-thread-operation-ignore-subject)
8887                  (gnus-simplify-subject-re
8888                   (mail-header-subject (gnus-data-header (car data)))))
8889                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8890                  (gnus-simplify-subject-fuzzy
8891                   (mail-header-subject (gnus-data-header (car data)))))
8892                 (t nil)))
8893          (end-point (save-excursion
8894                       (if (gnus-summary-go-to-next-thread)
8895                           (point) (point-max))))
8896          articles)
8897     (while (and data
8898                 (< (gnus-data-pos (car data)) end-point))
8899       (when (or (not top-subject)
8900                 (string= top-subject
8901                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8902                              (gnus-simplify-subject-fuzzy
8903                               (mail-header-subject
8904                                (gnus-data-header (car data))))
8905                            (gnus-simplify-subject-re
8906                             (mail-header-subject
8907                              (gnus-data-header (car data)))))))
8908         (push (gnus-data-number (car data)) articles))
8909       (unless (and (setq data (cdr data))
8910                    (> (gnus-data-level (car data)) top-level))
8911         (setq data nil)))
8912     ;; Return the list of articles.
8913     (nreverse articles)))
8914
8915 (defun gnus-summary-rethread-current ()
8916   "Rethread the thread the current article is part of."
8917   (interactive)
8918   (let* ((gnus-show-threads t)
8919          (article (gnus-summary-article-number))
8920          (id (mail-header-id (gnus-summary-article-header)))
8921          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8922     (unless id
8923       (error "No article on the current line"))
8924     (gnus-rebuild-thread id)
8925     (gnus-summary-goto-subject article)))
8926
8927 (defun gnus-summary-reparent-thread ()
8928   "Make the current article child of the marked (or previous) article.
8929
8930 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8931 is non-nil or the Subject: of both articles are the same."
8932   (interactive)
8933   (unless (not (gnus-group-read-only-p))
8934     (error "The current newsgroup does not support article editing"))
8935   (unless (<= (length gnus-newsgroup-processable) 1)
8936     (error "No more than one article may be marked"))
8937   (save-window-excursion
8938     (let ((gnus-article-buffer " *reparent*")
8939           (current-article (gnus-summary-article-number))
8940           ;; First grab the marked article, otherwise one line up.
8941           (parent-article (if (not (null gnus-newsgroup-processable))
8942                               (car gnus-newsgroup-processable)
8943                             (save-excursion
8944                               (if (eq (forward-line -1) 0)
8945                                   (gnus-summary-article-number)
8946                                 (error "Beginning of summary buffer"))))))
8947       (unless (not (eq current-article parent-article))
8948         (error "An article may not be self-referential"))
8949       (let ((message-id (mail-header-id
8950                          (gnus-summary-article-header parent-article))))
8951         (unless (and message-id (not (equal message-id "")))
8952           (error "No message-id in desired parent"))
8953         (gnus-with-article current-article
8954           (save-restriction
8955             (goto-char (point-min))
8956             (message-narrow-to-head)
8957             (if (re-search-forward "^References: " nil t)
8958                 (progn
8959                   (re-search-forward "^[^ \t]" nil t)
8960                   (forward-line -1)
8961                   (end-of-line)
8962                   (insert " " message-id))
8963               (insert "References: " message-id "\n"))))
8964         (set-buffer gnus-summary-buffer)
8965         (gnus-summary-unmark-all-processable)
8966         (gnus-summary-update-article current-article)
8967         (gnus-summary-rethread-current)
8968         (gnus-message 3 "Article %d is now the child of article %d"
8969                       current-article parent-article)))))
8970
8971 (defun gnus-summary-toggle-threads (&optional arg)
8972   "Toggle showing conversation threads.
8973 If ARG is positive number, turn showing conversation threads on."
8974   (interactive "P")
8975   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8976     (setq gnus-show-threads
8977           (if (null arg) (not gnus-show-threads)
8978             (> (prefix-numeric-value arg) 0)))
8979     (gnus-summary-prepare)
8980     (gnus-summary-goto-subject current)
8981     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8982     (gnus-summary-position-point)))
8983
8984 (defun gnus-summary-show-all-threads ()
8985   "Show all threads."
8986   (interactive)
8987   (save-excursion
8988     (let ((buffer-read-only nil))
8989       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8990   (gnus-summary-position-point))
8991
8992 (defun gnus-summary-show-thread ()
8993   "Show thread subtrees.
8994 Returns nil if no thread was there to be shown."
8995   (interactive)
8996   (let ((buffer-read-only nil)
8997         (orig (point))
8998         ;; first goto end then to beg, to have point at beg after let
8999         (end (progn (end-of-line) (point)))
9000         (beg (progn (beginning-of-line) (point))))
9001     (prog1
9002         ;; Any hidden lines here?
9003         (search-forward "\r" end t)
9004       (subst-char-in-region beg end ?\^M ?\n t)
9005       (goto-char orig)
9006       (gnus-summary-position-point))))
9007
9008 (defun gnus-summary-hide-all-threads ()
9009   "Hide all thread subtrees."
9010   (interactive)
9011   (save-excursion
9012     (goto-char (point-min))
9013     (gnus-summary-hide-thread)
9014     (while (zerop (gnus-summary-next-thread 1 t))
9015       (gnus-summary-hide-thread)))
9016   (gnus-summary-position-point))
9017
9018 (defun gnus-summary-hide-thread ()
9019   "Hide thread subtrees.
9020 Returns nil if no threads were there to be hidden."
9021   (interactive)
9022   (let ((buffer-read-only nil)
9023         (start (point))
9024         (article (gnus-summary-article-number)))
9025     (goto-char start)
9026     ;; Go forward until either the buffer ends or the subthread
9027     ;; ends.
9028     (when (and (not (eobp))
9029                (or (zerop (gnus-summary-next-thread 1 t))
9030                    (goto-char (point-max))))
9031       (prog1
9032           (if (and (> (point) start)
9033                    (search-backward "\n" start t))
9034               (progn
9035                 (subst-char-in-region start (point) ?\n ?\^M)
9036                 (gnus-summary-goto-subject article))
9037             (goto-char start)
9038             nil)))))
9039
9040 (defun gnus-summary-go-to-next-thread (&optional previous)
9041   "Go to the same level (or less) next thread.
9042 If PREVIOUS is non-nil, go to previous thread instead.
9043 Return the article number moved to, or nil if moving was impossible."
9044   (let ((level (gnus-summary-thread-level))
9045         (way (if previous -1 1))
9046         (beg (point)))
9047     (forward-line way)
9048     (while (and (not (eobp))
9049                 (< level (gnus-summary-thread-level)))
9050       (forward-line way))
9051     (if (eobp)
9052         (progn
9053           (goto-char beg)
9054           nil)
9055       (setq beg (point))
9056       (prog1
9057           (gnus-summary-article-number)
9058         (goto-char beg)))))
9059
9060 (defun gnus-summary-next-thread (n &optional silent)
9061   "Go to the same level next N'th thread.
9062 If N is negative, search backward instead.
9063 Returns the difference between N and the number of skips actually
9064 done.
9065
9066 If SILENT, don't output messages."
9067   (interactive "p")
9068   (let ((backward (< n 0))
9069         (n (abs n)))
9070     (while (and (> n 0)
9071                 (gnus-summary-go-to-next-thread backward))
9072       (decf n))
9073     (unless silent
9074       (gnus-summary-position-point))
9075     (when (and (not silent) (/= 0 n))
9076       (gnus-message 7 "No more threads"))
9077     n))
9078
9079 (defun gnus-summary-prev-thread (n)
9080   "Go to the same level previous N'th thread.
9081 Returns the difference between N and the number of skips actually
9082 done."
9083   (interactive "p")
9084   (gnus-summary-next-thread (- n)))
9085
9086 (defun gnus-summary-go-down-thread ()
9087   "Go down one level in the current thread."
9088   (let ((children (gnus-summary-article-children)))
9089     (when children
9090       (gnus-summary-goto-subject (car children)))))
9091
9092 (defun gnus-summary-go-up-thread ()
9093   "Go up one level in the current thread."
9094   (let ((parent (gnus-summary-article-parent)))
9095     (when parent
9096       (gnus-summary-goto-subject parent))))
9097
9098 (defun gnus-summary-down-thread (n)
9099   "Go down thread N steps.
9100 If N is negative, go up instead.
9101 Returns the difference between N and how many steps down that were
9102 taken."
9103   (interactive "p")
9104   (let ((up (< n 0))
9105         (n (abs n)))
9106     (while (and (> n 0)
9107                 (if up (gnus-summary-go-up-thread)
9108                   (gnus-summary-go-down-thread)))
9109       (setq n (1- n)))
9110     (gnus-summary-position-point)
9111     (when (/= 0 n)
9112       (gnus-message 7 "Can't go further"))
9113     n))
9114
9115 (defun gnus-summary-up-thread (n)
9116   "Go up thread N steps.
9117 If N is negative, go up instead.
9118 Returns the difference between N and how many steps down that were
9119 taken."
9120   (interactive "p")
9121   (gnus-summary-down-thread (- n)))
9122
9123 (defun gnus-summary-top-thread ()
9124   "Go to the top of the thread."
9125   (interactive)
9126   (while (gnus-summary-go-up-thread))
9127   (gnus-summary-article-number))
9128
9129 (defun gnus-summary-kill-thread (&optional unmark)
9130   "Mark articles under current thread as read.
9131 If the prefix argument is positive, remove any kinds of marks.
9132 If the prefix argument is negative, tick articles instead."
9133   (interactive "P")
9134   (when unmark
9135     (setq unmark (prefix-numeric-value unmark)))
9136   (let ((articles (gnus-summary-articles-in-thread)))
9137     (save-excursion
9138       ;; Expand the thread.
9139       (gnus-summary-show-thread)
9140       ;; Mark all the articles.
9141       (while articles
9142         (gnus-summary-goto-subject (car articles))
9143         (cond ((null unmark)
9144                (gnus-summary-mark-article-as-read gnus-killed-mark))
9145               ((> unmark 0)
9146                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9147               (t
9148                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9149         (setq articles (cdr articles))))
9150     ;; Hide killed subtrees.
9151     (and (null unmark)
9152          gnus-thread-hide-killed
9153          (gnus-summary-hide-thread))
9154     ;; If marked as read, go to next unread subject.
9155     (when (null unmark)
9156       ;; Go to next unread subject.
9157       (gnus-summary-next-subject 1 t)))
9158   (gnus-set-mode-line 'summary))
9159
9160 ;; Summary sorting commands
9161
9162 (defun gnus-summary-sort-by-number (&optional reverse)
9163   "Sort the summary buffer by article number.
9164 Argument REVERSE means reverse order."
9165   (interactive "P")
9166   (gnus-summary-sort 'number reverse))
9167
9168 (defun gnus-summary-sort-by-author (&optional reverse)
9169   "Sort the summary buffer by author name alphabetically.
9170 If `case-fold-search' is non-nil, case of letters is ignored.
9171 Argument REVERSE means reverse order."
9172   (interactive "P")
9173   (gnus-summary-sort 'author reverse))
9174
9175 (defun gnus-summary-sort-by-subject (&optional reverse)
9176   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9177 If `case-fold-search' is non-nil, case of letters is ignored.
9178 Argument REVERSE means reverse order."
9179   (interactive "P")
9180   (gnus-summary-sort 'subject reverse))
9181
9182 (defun gnus-summary-sort-by-date (&optional reverse)
9183   "Sort the summary buffer by date.
9184 Argument REVERSE means reverse order."
9185   (interactive "P")
9186   (gnus-summary-sort 'date reverse))
9187
9188 (defun gnus-summary-sort-by-score (&optional reverse)
9189   "Sort the summary buffer by score.
9190 Argument REVERSE means reverse order."
9191   (interactive "P")
9192   (gnus-summary-sort 'score reverse))
9193
9194 (defun gnus-summary-sort-by-lines (&optional reverse)
9195   "Sort the summary buffer by the number of lines.
9196 Argument REVERSE means reverse order."
9197   (interactive "P")
9198   (gnus-summary-sort 'lines reverse))
9199
9200 (defun gnus-summary-sort-by-chars (&optional reverse)
9201   "Sort the summary buffer by article length.
9202 Argument REVERSE means reverse order."
9203   (interactive "P")
9204   (gnus-summary-sort 'chars reverse))
9205
9206 (defun gnus-summary-sort (predicate reverse)
9207   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9208   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9209          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9210          (gnus-thread-sort-functions
9211           (if (not reverse)
9212               thread
9213             `(lambda (t1 t2)
9214                (,thread t2 t1))))
9215          (gnus-sort-gathered-threads-function
9216           gnus-thread-sort-functions)
9217          (gnus-article-sort-functions
9218           (if (not reverse)
9219               article
9220             `(lambda (t1 t2)
9221                (,article t2 t1))))
9222          (buffer-read-only)
9223          (gnus-summary-prepare-hook nil))
9224     ;; We do the sorting by regenerating the threads.
9225     (gnus-summary-prepare)
9226     ;; Hide subthreads if needed.
9227     (when (and gnus-show-threads gnus-thread-hide-subtree)
9228       (gnus-summary-hide-all-threads))))
9229
9230 ;; Summary saving commands.
9231
9232 (defun gnus-summary-save-article (&optional n not-saved)
9233   "Save the current article using the default saver function.
9234 If N is a positive number, save the N next articles.
9235 If N is a negative number, save the N previous articles.
9236 If N is nil and any articles have been marked with the process mark,
9237 save those articles instead.
9238 The variable `gnus-default-article-saver' specifies the saver function."
9239   (interactive "P")
9240   (let* ((articles (gnus-summary-work-articles n))
9241          (save-buffer (save-excursion
9242                         (nnheader-set-temp-buffer " *Gnus Save*")))
9243          (num (length articles))
9244          header file)
9245     (dolist (article articles)
9246       (setq header (gnus-summary-article-header article))
9247       (if (not (vectorp header))
9248           ;; This is a pseudo-article.
9249           (if (assq 'name header)
9250               (gnus-copy-file (cdr (assq 'name header)))
9251             (gnus-message 1 "Article %d is unsaveable" article))
9252         ;; This is a real article.
9253         (save-window-excursion
9254           (gnus-summary-select-article t nil nil article))
9255         (save-excursion
9256           (set-buffer save-buffer)
9257           (erase-buffer)
9258           (insert-buffer-substring gnus-original-article-buffer))
9259         (setq file (gnus-article-save save-buffer file num))
9260         (gnus-summary-remove-process-mark article)
9261         (unless not-saved
9262           (gnus-summary-set-saved-mark article))))
9263     (gnus-kill-buffer save-buffer)
9264     (gnus-summary-position-point)
9265     (gnus-set-mode-line 'summary)
9266     n))
9267
9268 (defun gnus-summary-pipe-output (&optional arg)
9269   "Pipe the current article to a subprocess.
9270 If N is a positive number, pipe the N next articles.
9271 If N is a negative number, pipe the N previous articles.
9272 If N is nil and any articles have been marked with the process mark,
9273 pipe those articles instead."
9274   (interactive "P")
9275   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9276     (gnus-summary-save-article arg t))
9277   (gnus-configure-windows 'pipe))
9278
9279 (defun gnus-summary-save-article-mail (&optional arg)
9280   "Append the current article to an mail file.
9281 If N is a positive number, save the N next articles.
9282 If N is a negative number, save the N previous articles.
9283 If N is nil and any articles have been marked with the process mark,
9284 save those articles instead."
9285   (interactive "P")
9286   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9287     (gnus-summary-save-article arg)))
9288
9289 (defun gnus-summary-save-article-rmail (&optional arg)
9290   "Append the current article to an rmail file.
9291 If N is a positive number, save the N next articles.
9292 If N is a negative number, save the N previous articles.
9293 If N is nil and any articles have been marked with the process mark,
9294 save those articles instead."
9295   (interactive "P")
9296   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9297     (gnus-summary-save-article arg)))
9298
9299 (defun gnus-summary-save-article-file (&optional arg)
9300   "Append the current article to a file.
9301 If N is a positive number, save the N next articles.
9302 If N is a negative number, save the N previous articles.
9303 If N is nil and any articles have been marked with the process mark,
9304 save those articles instead."
9305   (interactive "P")
9306   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9307     (gnus-summary-save-article arg)))
9308
9309 (defun gnus-summary-write-article-file (&optional arg)
9310   "Write the current article to a file, deleting the previous file.
9311 If N is a positive number, save the N next articles.
9312 If N is a negative number, save the N previous articles.
9313 If N is nil and any articles have been marked with the process mark,
9314 save those articles instead."
9315   (interactive "P")
9316   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9317     (gnus-summary-save-article arg)))
9318
9319 (defun gnus-summary-save-article-body-file (&optional arg)
9320   "Append the current article body to a file.
9321 If N is a positive number, save the N next articles.
9322 If N is a negative number, save the N previous articles.
9323 If N is nil and any articles have been marked with the process mark,
9324 save those articles instead."
9325   (interactive "P")
9326   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9327     (gnus-summary-save-article arg)))
9328
9329 (defun gnus-summary-pipe-message (program)
9330   "Pipe the current article through PROGRAM."
9331   (interactive "sProgram: ")
9332   (gnus-summary-select-article)
9333   (let ((mail-header-separator ""))
9334     (gnus-eval-in-buffer-window gnus-article-buffer
9335       (save-restriction
9336         (widen)
9337         (let ((start (window-start))
9338               buffer-read-only)
9339           (message-pipe-buffer-body program)
9340           (set-window-start (get-buffer-window (current-buffer)) start))))))
9341
9342 (defun gnus-get-split-value (methods)
9343   "Return a value based on the split METHODS."
9344   (let (split-name method result match)
9345     (when methods
9346       (save-excursion
9347         (set-buffer gnus-original-article-buffer)
9348         (save-restriction
9349           (nnheader-narrow-to-headers)
9350           (while (and methods (not split-name))
9351             (goto-char (point-min))
9352             (setq method (pop methods))
9353             (setq match (car method))
9354             (when (cond
9355                    ((stringp match)
9356                     ;; Regular expression.
9357                     (ignore-errors
9358                       (re-search-forward match nil t)))
9359                    ((gnus-functionp match)
9360                     ;; Function.
9361                     (save-restriction
9362                       (widen)
9363                       (setq result (funcall match gnus-newsgroup-name))))
9364                    ((consp match)
9365                     ;; Form.
9366                     (save-restriction
9367                       (widen)
9368                       (setq result (eval match)))))
9369               (setq split-name (cdr method))
9370               (cond ((stringp result)
9371                      (push (expand-file-name
9372                             result gnus-article-save-directory)
9373                            split-name))
9374                     ((consp result)
9375                      (setq split-name (append result split-name)))))))))
9376     (nreverse split-name)))
9377
9378 (defun gnus-valid-move-group-p (group)
9379   (and (boundp group)
9380        (symbol-name group)
9381        (symbol-value group)
9382        (gnus-get-function (gnus-find-method-for-group
9383                            (symbol-name group)) 'request-accept-article t)))
9384
9385 (defun gnus-read-move-group-name (prompt default articles prefix)
9386   "Read a group name."
9387   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9388          (minibuffer-confirm-incomplete nil) ; XEmacs
9389          (prom
9390           (format "%s %s to:"
9391                   prompt
9392                   (if (> (length articles) 1)
9393                       (format "these %d articles" (length articles))
9394                     "this article")))
9395          (to-newsgroup
9396           (cond
9397            ((null split-name)
9398             (gnus-completing-read default prom
9399                                   gnus-active-hashtb
9400                                   'gnus-valid-move-group-p
9401                                   nil prefix
9402                                   'gnus-group-history))
9403            ((= 1 (length split-name))
9404             (gnus-completing-read (car split-name) prom
9405                                   gnus-active-hashtb
9406                                   'gnus-valid-move-group-p
9407                                   nil nil
9408                                   'gnus-group-history))
9409            (t
9410             (gnus-completing-read nil prom
9411                                   (mapcar (lambda (el) (list el))
9412                                           (nreverse split-name))
9413                                   nil nil nil
9414                                   'gnus-group-history))))
9415          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9416     (when to-newsgroup
9417       (if (or (string= to-newsgroup "")
9418               (string= to-newsgroup prefix))
9419           (setq to-newsgroup default))
9420       (unless to-newsgroup
9421         (error "No group name entered"))
9422       (or (gnus-active to-newsgroup)
9423           (gnus-activate-group to-newsgroup nil nil to-method)
9424           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9425                                      to-newsgroup))
9426               (or (and (gnus-request-create-group to-newsgroup to-method)
9427                        (gnus-activate-group
9428                         to-newsgroup nil nil to-method)
9429                        (gnus-subscribe-group to-newsgroup))
9430                   (error "Couldn't create group %s" to-newsgroup)))
9431           (error "No such group: %s" to-newsgroup)))
9432     to-newsgroup))
9433
9434 (defun gnus-summary-save-parts (type dir n &optional reverse)
9435   "Save parts matching TYPE to DIR.
9436 If REVERSE, save parts that do not match TYPE."
9437   (interactive
9438    (list (read-string "Save parts of type: " 
9439                       (or (car gnus-summary-save-parts-type-history)
9440                           gnus-summary-save-parts-default-mime)
9441                       'gnus-summary-save-parts-type-history)
9442          (setq gnus-summary-save-parts-last-directory
9443                (read-file-name "Save to directory: " 
9444                                gnus-summary-save-parts-last-directory
9445                                nil t))
9446          current-prefix-arg))
9447   (gnus-summary-iterate n
9448     (let ((gnus-display-mime-function nil)
9449           (gnus-inhibit-treatment t))
9450       (gnus-summary-select-article))
9451     (save-excursion
9452       (set-buffer gnus-article-buffer)
9453       (let ((handles (or gnus-article-mime-handles
9454                          (mm-dissect-buffer) (mm-uu-dissect))))
9455         (when handles
9456           (gnus-summary-save-parts-1 type dir handles reverse)
9457           (unless gnus-article-mime-handles ;; Don't destroy this case.
9458             (mm-destroy-parts handles)))))))
9459
9460 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9461   (if (stringp (car handle))
9462       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9463               (cdr handle))
9464     (when (if reverse
9465               (not (string-match type (mm-handle-media-type handle)))
9466             (string-match type (mm-handle-media-type handle)))
9467       (let ((file (expand-file-name
9468                    (file-name-nondirectory
9469                     (or
9470                      (mail-content-type-get
9471                       (mm-handle-disposition handle) 'filename)
9472                      (concat gnus-newsgroup-name
9473                              "." (number-to-string
9474                                   (cdr gnus-article-current)))))
9475                    dir)))
9476         (unless (file-exists-p file)
9477           (mm-save-part-to-file handle file))))))
9478
9479 ;; Summary extract commands
9480
9481 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9482   (let ((buffer-read-only nil)
9483         (article (gnus-summary-article-number))
9484         after-article b e)
9485     (unless (gnus-summary-goto-subject article)
9486       (error "No such article: %d" article))
9487     (gnus-summary-position-point)
9488     ;; If all commands are to be bunched up on one line, we collect
9489     ;; them here.
9490     (unless gnus-view-pseudos-separately
9491       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9492             files action)
9493         (while ps
9494           (setq action (cdr (assq 'action (car ps))))
9495           (setq files (list (cdr (assq 'name (car ps)))))
9496           (while (and ps (cdr ps)
9497                       (string= (or action "1")
9498                                (or (cdr (assq 'action (cadr ps))) "2")))
9499             (push (cdr (assq 'name (cadr ps))) files)
9500             (setcdr ps (cddr ps)))
9501           (when files
9502             (when (not (string-match "%s" action))
9503               (push " " files))
9504             (push " " files)
9505             (when (assq 'execute (car ps))
9506               (setcdr (assq 'execute (car ps))
9507                       (funcall (if (string-match "%s" action)
9508                                    'format 'concat)
9509                                action
9510                                (mapconcat
9511                                 (lambda (f)
9512                                   (if (equal f " ")
9513                                       f
9514                                     (mm-quote-arg f)))
9515                                 files " ")))))
9516           (setq ps (cdr ps)))))
9517     (if (and gnus-view-pseudos (not not-view))
9518         (while pslist
9519           (when (assq 'execute (car pslist))
9520             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9521                                   (eq gnus-view-pseudos 'not-confirm)))
9522           (setq pslist (cdr pslist)))
9523       (save-excursion
9524         (while pslist
9525           (setq after-article (or (cdr (assq 'article (car pslist)))
9526                                   (gnus-summary-article-number)))
9527           (gnus-summary-goto-subject after-article)
9528           (forward-line 1)
9529           (setq b (point))
9530           (insert "    " (file-name-nondirectory
9531                           (cdr (assq 'name (car pslist))))
9532                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9533           (setq e (point))
9534           (forward-line -1)             ; back to `b'
9535           (gnus-add-text-properties
9536            b (1- e) (list 'gnus-number gnus-reffed-article-number
9537                           gnus-mouse-face-prop gnus-mouse-face))
9538           (gnus-data-enter
9539            after-article gnus-reffed-article-number
9540            gnus-unread-mark b (car pslist) 0 (- e b))
9541           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9542           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9543           (setq pslist (cdr pslist)))))))
9544
9545 (defun gnus-pseudos< (p1 p2)
9546   (let ((c1 (cdr (assq 'action p1)))
9547         (c2 (cdr (assq 'action p2))))
9548     (and c1 c2 (string< c1 c2))))
9549
9550 (defun gnus-request-pseudo-article (props)
9551   (cond ((assq 'execute props)
9552          (gnus-execute-command (cdr (assq 'execute props)))))
9553   (let ((gnus-current-article (gnus-summary-article-number)))
9554     (gnus-run-hooks 'gnus-mark-article-hook)))
9555
9556 (defun gnus-execute-command (command &optional automatic)
9557   (save-excursion
9558     (gnus-article-setup-buffer)
9559     (set-buffer gnus-article-buffer)
9560     (setq buffer-read-only nil)
9561     (let ((command (if automatic command
9562                      (read-string "Command: " (cons command 0)))))
9563       (erase-buffer)
9564       (insert "$ " command "\n\n")
9565       (if gnus-view-pseudo-asynchronously
9566           (start-process "gnus-execute" (current-buffer) shell-file-name
9567                          shell-command-switch command)
9568         (call-process shell-file-name nil t nil
9569                       shell-command-switch command)))))
9570
9571 ;; Summary kill commands.
9572
9573 (defun gnus-summary-edit-global-kill (article)
9574   "Edit the \"global\" kill file."
9575   (interactive (list (gnus-summary-article-number)))
9576   (gnus-group-edit-global-kill article))
9577
9578 (defun gnus-summary-edit-local-kill ()
9579   "Edit a local kill file applied to the current newsgroup."
9580   (interactive)
9581   (setq gnus-current-headers (gnus-summary-article-header))
9582   (gnus-group-edit-local-kill
9583    (gnus-summary-article-number) gnus-newsgroup-name))
9584
9585 ;;; Header reading.
9586
9587 (defun gnus-read-header (id &optional header)
9588   "Read the headers of article ID and enter them into the Gnus system."
9589   (let ((group gnus-newsgroup-name)
9590         (gnus-override-method
9591          (or
9592           gnus-override-method
9593           (and (gnus-news-group-p gnus-newsgroup-name)
9594                (car (gnus-refer-article-methods)))))
9595         where)
9596     ;; First we check to see whether the header in question is already
9597     ;; fetched.
9598     (if (stringp id)
9599         ;; This is a Message-ID.
9600         (setq header (or header (gnus-id-to-header id)))
9601       ;; This is an article number.
9602       (setq header (or header (gnus-summary-article-header id))))
9603     (if (and header
9604              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9605         ;; We have found the header.
9606         header
9607       ;; If this is a sparse article, we have to nix out its
9608       ;; previous entry in the thread hashtb.
9609       (when (and header
9610                  (gnus-summary-article-sparse-p (mail-header-number header)))
9611         (let* ((parent (gnus-parent-id (mail-header-references header)))
9612                (thread (and parent (gnus-id-to-thread parent))))
9613           (when thread
9614             (delq (assq header thread) thread))))
9615       ;; We have to really fetch the header to this article.
9616       (save-excursion
9617         (set-buffer nntp-server-buffer)
9618         (when (setq where (gnus-request-head id group))
9619           (nnheader-fold-continuation-lines)
9620           (goto-char (point-max))
9621           (insert ".\n")
9622           (goto-char (point-min))
9623           (insert "211 ")
9624           (princ (cond
9625                   ((numberp id) id)
9626                   ((cdr where) (cdr where))
9627                   (header (mail-header-number header))
9628                   (t gnus-reffed-article-number))
9629                  (current-buffer))
9630           (insert " Article retrieved.\n"))
9631         (if (or (not where)
9632                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9633             ()                          ; Malformed head.
9634           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9635             (when (and (stringp id)
9636                        (not (string= (gnus-group-real-name group)
9637                                      (car where))))
9638               ;; If we fetched by Message-ID and the article came
9639               ;; from a different group, we fudge some bogus article
9640               ;; numbers for this article.
9641               (mail-header-set-number header gnus-reffed-article-number))
9642             (save-excursion
9643               (set-buffer gnus-summary-buffer)
9644               (decf gnus-reffed-article-number)
9645               (gnus-remove-header (mail-header-number header))
9646               (push header gnus-newsgroup-headers)
9647               (setq gnus-current-headers header)
9648               (push (mail-header-number header) gnus-newsgroup-limit)))
9649           header)))))
9650
9651 (defun gnus-remove-header (number)
9652   "Remove header NUMBER from `gnus-newsgroup-headers'."
9653   (if (and gnus-newsgroup-headers
9654            (= number (mail-header-number (car gnus-newsgroup-headers))))
9655       (pop gnus-newsgroup-headers)
9656     (let ((headers gnus-newsgroup-headers))
9657       (while (and (cdr headers)
9658                   (not (= number (mail-header-number (cadr headers)))))
9659         (pop headers))
9660       (when (cdr headers)
9661         (setcdr headers (cddr headers))))))
9662
9663 ;;;
9664 ;;; summary highlights
9665 ;;;
9666
9667 (defun gnus-highlight-selected-summary ()
9668   "Highlight selected article in summary buffer."
9669   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9670   (when gnus-summary-selected-face
9671     (save-excursion
9672       (let* ((beg (progn (beginning-of-line) (point)))
9673              (end (progn (end-of-line) (point)))
9674              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9675              (from (if (get-text-property beg gnus-mouse-face-prop)
9676                        beg
9677                      (or (next-single-property-change
9678                           beg gnus-mouse-face-prop nil end)
9679                          beg)))
9680              (to
9681               (if (= from end)
9682                   (- from 2)
9683                 (or (next-single-property-change
9684                      from gnus-mouse-face-prop nil end)
9685                     end))))
9686         ;; If no mouse-face prop on line we will have to = from = end,
9687         ;; so we highlight the entire line instead.
9688         (when (= (+ to 2) from)
9689           (setq from beg)
9690           (setq to end))
9691         (if gnus-newsgroup-selected-overlay
9692             ;; Move old overlay.
9693             (gnus-move-overlay
9694              gnus-newsgroup-selected-overlay from to (current-buffer))
9695           ;; Create new overlay.
9696           (gnus-overlay-put
9697            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9698            'face gnus-summary-selected-face))))))
9699
9700 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9701 (defun gnus-summary-highlight-line ()
9702   "Highlight current line according to `gnus-summary-highlight'."
9703   (let* ((list gnus-summary-highlight)
9704          (p (point))
9705          (end (progn (end-of-line) (point)))
9706          ;; now find out where the line starts and leave point there.
9707          (beg (progn (beginning-of-line) (point)))
9708          (article (gnus-summary-article-number))
9709          (score (or (cdr (assq (or article gnus-current-article)
9710                                gnus-newsgroup-scored))
9711                     gnus-summary-default-score 0))
9712          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9713          (inhibit-read-only t))
9714     ;; Eval the cars of the lists until we find a match.
9715     (let ((default gnus-summary-default-score))
9716       (while (and list
9717                   (not (eval (caar list))))
9718         (setq list (cdr list))))
9719     (let ((face (cdar list)))
9720       (unless (eq face (get-text-property beg 'face))
9721         (gnus-put-text-property-excluding-characters-with-faces
9722          beg end 'face
9723          (setq face (if (boundp face) (symbol-value face) face)))
9724         (when gnus-summary-highlight-line-function
9725           (funcall gnus-summary-highlight-line-function article face))))
9726     (goto-char p)))
9727
9728 (defun gnus-update-read-articles (group unread &optional compute)
9729   "Update the list of read articles in GROUP."
9730   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9731          (entry (gnus-gethash group gnus-newsrc-hashtb))
9732          (info (nth 2 entry))
9733          (prev 1)
9734          (unread (sort (copy-sequence unread) '<))
9735          read)
9736     (if (or (not info) (not active))
9737         ;; There is no info on this group if it was, in fact,
9738         ;; killed.  Gnus stores no information on killed groups, so
9739         ;; there's nothing to be done.
9740         ;; One could store the information somewhere temporarily,
9741         ;; perhaps...  Hmmm...
9742         ()
9743       ;; Remove any negative articles numbers.
9744       (while (and unread (< (car unread) 0))
9745         (setq unread (cdr unread)))
9746       ;; Remove any expired article numbers
9747       (while (and unread (< (car unread) (car active)))
9748         (setq unread (cdr unread)))
9749       ;; Compute the ranges of read articles by looking at the list of
9750       ;; unread articles.
9751       (while unread
9752         (when (/= (car unread) prev)
9753           (push (if (= prev (1- (car unread))) prev
9754                   (cons prev (1- (car unread))))
9755                 read))
9756         (setq prev (1+ (car unread)))
9757         (setq unread (cdr unread)))
9758       (when (<= prev (cdr active))
9759         (push (cons prev (cdr active)) read))
9760       (setq read (if (> (length read) 1) (nreverse read) read))
9761       (if compute
9762           read
9763         (save-excursion
9764           (let (setmarkundo)
9765             ;; Propagate the read marks to the backend.
9766             (when (gnus-check-backend-function 'request-set-mark group)
9767               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9768                     (add (gnus-remove-from-range read (gnus-info-read info))))
9769                 (when (or add del)
9770                   (unless (gnus-check-group group)
9771                     (error "Can't open server for %s" group))
9772                   (gnus-request-set-mark
9773                    group (delq nil (list (if add (list add 'add '(read)))
9774                                          (if del (list del 'del '(read))))))
9775                   (setq setmarkundo
9776                         `(gnus-request-set-mark
9777                           ,group
9778                           ',(delq nil (list
9779                                        (if del (list del 'add '(read)))
9780                                        (if add (list add 'del '(read))))))))))
9781             (set-buffer gnus-group-buffer)
9782             (gnus-undo-register
9783               `(progn
9784                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9785                  (gnus-info-set-read ',info ',(gnus-info-read info))
9786                  (gnus-get-unread-articles-in-group ',info 
9787                                                     (gnus-active ,group))
9788                  (gnus-group-update-group ,group t)
9789                  ,setmarkundo))))
9790         ;; Enter this list into the group info.
9791         (gnus-info-set-read info read)
9792         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9793         (gnus-get-unread-articles-in-group info (gnus-active group))
9794         t))))
9795
9796 (defun gnus-offer-save-summaries ()
9797   "Offer to save all active summary buffers."
9798   (save-excursion
9799     (let ((buflist (buffer-list))
9800           buffers bufname)
9801       ;; Go through all buffers and find all summaries.
9802       (while buflist
9803         (and (setq bufname (buffer-name (car buflist)))
9804              (string-match "Summary" bufname)
9805              (save-excursion
9806                (set-buffer bufname)
9807                ;; We check that this is, indeed, a summary buffer.
9808                (and (eq major-mode 'gnus-summary-mode)
9809                     ;; Also make sure this isn't bogus.
9810                     gnus-newsgroup-prepared
9811                     ;; Also make sure that this isn't a dead summary buffer.
9812                     (not gnus-dead-summary-mode)))
9813              (push bufname buffers))
9814         (setq buflist (cdr buflist)))
9815       ;; Go through all these summary buffers and offer to save them.
9816       (when buffers
9817         (map-y-or-n-p
9818          "Update summary buffer %s? "
9819          (lambda (buf)
9820            (switch-to-buffer buf)
9821            (gnus-summary-exit))
9822          buffers)))))
9823
9824 (defun gnus-summary-setup-default-charset ()
9825   "Setup newsgroup default charset."
9826   (if (equal gnus-newsgroup-name "nndraft:drafts")
9827       (setq gnus-newsgroup-charset nil)
9828     (let* ((name (and gnus-newsgroup-name
9829                       (gnus-group-real-name gnus-newsgroup-name)))
9830            (ignored-charsets
9831             (or gnus-newsgroup-ephemeral-ignored-charsets
9832                 (append
9833                  (and gnus-newsgroup-name
9834                       (or (gnus-group-find-parameter gnus-newsgroup-name
9835                                                      'ignored-charsets t)
9836                           (let ((alist gnus-group-ignored-charsets-alist)
9837                                 elem (charsets nil))
9838                             (while (setq elem (pop alist))
9839                               (when (and name
9840                                          (string-match (car elem) name))
9841                                 (setq alist nil
9842                                       charsets (cdr elem))))
9843                             charsets)))
9844                  gnus-newsgroup-ignored-charsets))))
9845       (setq gnus-newsgroup-charset
9846             (or gnus-newsgroup-ephemeral-charset
9847                 (and gnus-newsgroup-name
9848                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9849                          (let ((alist gnus-group-charset-alist)
9850                                elem charset)
9851                            (while (setq elem (pop alist))
9852                              (when (and name
9853                                         (string-match (car elem) name))
9854                                (setq alist nil
9855                                      charset (cadr elem))))
9856                            charset)))
9857                 gnus-default-charset))
9858       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9859            ignored-charsets))))
9860
9861 ;;;
9862 ;;; Mime Commands
9863 ;;;
9864
9865 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9866   "Display the current article buffer fully MIME-buttonized.
9867 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9868 treated as multipart/mixed."
9869   (interactive "P")
9870   (require 'gnus-art)
9871   (let ((gnus-unbuttonized-mime-types nil)
9872         (gnus-mime-display-multipart-as-mixed show-all-parts))
9873     (gnus-summary-show-article)))
9874
9875 (defun gnus-summary-repair-multipart (article)
9876   "Add a Content-Type header to a multipart article without one."
9877   (interactive (list (gnus-summary-article-number)))
9878   (gnus-with-article article
9879     (message-narrow-to-head)
9880     (message-remove-header "Mime-Version")
9881     (goto-char (point-max))
9882     (insert "Mime-Version: 1.0\n")
9883     (widen)
9884     (when (search-forward "\n--" nil t)
9885       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9886         (message-narrow-to-head)
9887         (message-remove-header "Content-Type")
9888         (goto-char (point-max))
9889         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9890                         separator))
9891         (widen))))
9892   (let (gnus-mark-article-hook)
9893     (gnus-summary-select-article t t nil article)))
9894
9895 (defun gnus-summary-toggle-display-buttonized ()
9896   "Toggle the buttonizing of the article buffer."
9897   (interactive)
9898   (require 'gnus-art)
9899   (if (setq gnus-inhibit-mime-unbuttonizing
9900             (not gnus-inhibit-mime-unbuttonizing))
9901       (let ((gnus-unbuttonized-mime-types nil))
9902         (gnus-summary-show-article))
9903     (gnus-summary-show-article)))
9904
9905 ;;;
9906 ;;; Generic summary marking commands
9907 ;;;
9908
9909 (defvar gnus-summary-marking-alist
9910   '((read gnus-del-mark "d")
9911     (unread gnus-unread-mark "u")
9912     (ticked gnus-ticked-mark "!")
9913     (dormant gnus-dormant-mark "?")
9914     (expirable gnus-expirable-mark "e"))
9915   "An alist of names/marks/keystrokes.")
9916
9917 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9918 (defvar gnus-summary-mark-map)
9919
9920 (defun gnus-summary-make-all-marking-commands ()
9921   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9922   (dolist (elem gnus-summary-marking-alist)
9923     (apply 'gnus-summary-make-marking-command elem)))
9924
9925 (defun gnus-summary-make-marking-command (name mark keystroke)
9926   (let ((map (make-sparse-keymap)))
9927     (define-key gnus-summary-generic-mark-map keystroke map)
9928     (dolist (lway `((next "next" next nil "n")
9929                     (next-unread "next unread" next t "N")
9930                     (prev "previous" prev nil "p")
9931                     (prev-unread "previous unread" prev t "P")
9932                     (nomove "" nil nil ,keystroke)))
9933       (let ((func (gnus-summary-make-marking-command-1
9934                    mark (car lway) lway name)))
9935         (setq func (eval func))
9936         (define-key map (nth 4 lway) func)))))
9937       
9938 (defun gnus-summary-make-marking-command-1 (mark way lway name)
9939   `(defun ,(intern
9940             (format "gnus-summary-put-mark-as-%s%s"
9941                     name (if (eq way 'nomove)
9942                              ""
9943                            (concat "-" (symbol-name way)))))
9944      (n)
9945      ,(format
9946        "Mark the current article as %s%s.
9947 If N, the prefix, then repeat N times.
9948 If N is negative, move in reverse order.
9949 The difference between N and the actual number of articles marked is
9950 returned."
9951        name (cadr lway))
9952      (interactive "p")
9953      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9954     
9955 (defun gnus-summary-generic-mark (n mark move unread)
9956   "Mark N articles with MARK."
9957   (unless (eq major-mode 'gnus-summary-mode)
9958     (error "This command can only be used in the summary buffer"))
9959   (gnus-summary-show-thread)
9960   (let ((nummove
9961          (cond
9962           ((eq move 'next) 1)
9963           ((eq move 'prev) -1)
9964           (t 0))))
9965     (if (zerop nummove)
9966         (setq n 1)
9967       (when (< n 0)
9968         (setq n (abs n)
9969               nummove (* -1 nummove))))
9970     (while (and (> n 0)
9971                 (gnus-summary-mark-article nil mark)
9972                 (zerop (gnus-summary-next-subject nummove unread t)))
9973       (setq n (1- n)))
9974     (when (/= 0 n)
9975       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9976     (gnus-summary-recenter)
9977     (gnus-summary-position-point)
9978     (gnus-set-mode-line 'summary)
9979     n))
9980
9981 (gnus-summary-make-all-marking-commands)
9982
9983 (gnus-ems-redefine)
9984
9985 (provide 'gnus-sum)
9986
9987 (run-hooks 'gnus-sum-load-hook)
9988
9989 ;;; gnus-sum.el ends here