* gnus-sum.el (gnus-summary-limit-include-expunged): Really
[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-no-mark ?  ;Whitespace
434   "*Mark used for articles that have no other secondary mark."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-ancient-mark ?O
439   "*Mark used for ancient articles."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-sparse-mark ?Q
444   "*Mark used for sparsely reffed articles."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-canceled-mark ?G
449   "*Mark used for canceled articles."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-duplicate-mark ?M
454   "*Mark used for duplicate articles."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-undownloaded-mark ?@
459   "*Mark used for articles that weren't downloaded."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-downloadable-mark ?%
464   "*Mark used for articles that are to be downloaded."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-unsendable-mark ?=
469   "*Mark used for articles that won't be sent."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-score-over-mark ?+
474   "*Score mark used for articles with high scores."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-score-below-mark ?-
479   "*Score mark used for articles with low scores."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-empty-thread-mark ?  ;Whitespace
484   "*There is no thread under the article."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-not-empty-thread-mark ?=
489   "*There is a thread under the article."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-view-pseudo-asynchronously nil
494   "*If non-nil, Gnus will view pseudo-articles asynchronously."
495   :group 'gnus-extract-view
496   :type 'boolean)
497
498 (defcustom gnus-auto-expirable-marks
499   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
500         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
501         gnus-souped-mark gnus-duplicate-mark)
502   "*The list of marks converted into expiration if a group is auto-expirable."
503   :version "21.1"
504   :group 'gnus-summary
505   :type '(repeat character))
506
507 (defcustom gnus-inhibit-user-auto-expire t
508   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
509   :version "21.1"
510   :group 'gnus-summary
511   :type 'boolean)
512
513 (defcustom gnus-view-pseudos nil
514   "*If `automatic', pseudo-articles will be viewed automatically.
515 If `not-confirm', pseudos will be viewed automatically, and the user
516 will not be asked to confirm the command."
517   :group 'gnus-extract-view
518   :type '(choice (const :tag "off" nil)
519                  (const automatic)
520                  (const not-confirm)))
521
522 (defcustom gnus-view-pseudos-separately t
523   "*If non-nil, one pseudo-article will be created for each file to be viewed.
524 If nil, all files that use the same viewing command will be given as a
525 list of parameters to that command."
526   :group 'gnus-extract-view
527   :type 'boolean)
528
529 (defcustom gnus-insert-pseudo-articles t
530   "*If non-nil, insert pseudo-articles when decoding articles."
531   :group 'gnus-extract-view
532   :type 'boolean)
533
534 (defcustom gnus-summary-dummy-line-format
535   "  %(:                          :%) %S\n"
536   "*The format specification for the dummy roots in the summary buffer.
537 It works along the same lines as a normal formatting string,
538 with some simple extensions.
539
540 %S  The subject"
541   :group 'gnus-threading
542   :type 'string)
543
544 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
545   "*The format specification for the summary mode line.
546 It works along the same lines as a normal formatting string,
547 with some simple extensions:
548
549 %G  Group name
550 %p  Unprefixed group name
551 %A  Current article number
552 %z  Current article score
553 %V  Gnus version
554 %U  Number of unread articles in the group
555 %e  Number of unselected articles in the group
556 %Z  A string with unread/unselected article counts
557 %g  Shortish group name
558 %S  Subject of the current article
559 %u  User-defined spec
560 %s  Current score file name
561 %d  Number of dormant articles
562 %r  Number of articles that have been marked as read in this session
563 %E  Number of articles expunged by the score files"
564   :group 'gnus-summary-format
565   :type 'string)
566
567 (defcustom gnus-list-identifiers nil
568   "Regexp that matches list identifiers to be removed from subject.
569 This can also be a list of regexps."
570   :version "21.1"
571   :group 'gnus-summary-format
572   :group 'gnus-article-hiding
573   :type '(choice (const :tag "none" nil)
574                  (regexp :value ".*")
575                  (repeat :value (".*") regexp)))
576
577 (defcustom gnus-summary-mark-below 0
578   "*Mark all articles with a score below this variable as read.
579 This variable is local to each summary buffer and usually set by the
580 score file."
581   :group 'gnus-score-default
582   :type 'integer)
583
584 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
585   "*List of functions used for sorting articles in the summary buffer.
586 This variable is only used when not using a threaded display."
587   :group 'gnus-summary-sort
588   :type '(repeat (choice (function-item gnus-article-sort-by-number)
589                          (function-item gnus-article-sort-by-author)
590                          (function-item gnus-article-sort-by-subject)
591                          (function-item gnus-article-sort-by-date)
592                          (function-item gnus-article-sort-by-score)
593                          (function :tag "other"))))
594
595 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
596   "*List of functions used for sorting threads in the summary buffer.
597 By default, threads are sorted by article number.
598
599 Each function takes two threads and return non-nil if the first thread
600 should be sorted before the other.  If you use more than one function,
601 the primary sort function should be the last.  You should probably
602 always include `gnus-thread-sort-by-number' in the list of sorting
603 functions -- preferably first.
604
605 Ready-made functions include `gnus-thread-sort-by-number',
606 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
607 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
608 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
609   :group 'gnus-summary-sort
610   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
611                          (function-item gnus-thread-sort-by-author)
612                          (function-item gnus-thread-sort-by-subject)
613                          (function-item gnus-thread-sort-by-date)
614                          (function-item gnus-thread-sort-by-score)
615                          (function-item gnus-thread-sort-by-total-score)
616                          (function :tag "other"))))
617
618 (defcustom gnus-thread-score-function '+
619   "*Function used for calculating the total score of a thread.
620
621 The function is called with the scores of the article and each
622 subthread and should then return the score of the thread.
623
624 Some functions you can use are `+', `max', or `min'."
625   :group 'gnus-summary-sort
626   :type 'function)
627
628 (defcustom gnus-summary-expunge-below nil
629   "All articles that have a score less than this variable will be expunged.
630 This variable is local to the summary buffers."
631   :group 'gnus-score-default
632   :type '(choice (const :tag "off" nil)
633                  integer))
634
635 (defcustom gnus-thread-expunge-below nil
636   "All threads that have a total score less than this variable will be expunged.
637 See `gnus-thread-score-function' for en explanation of what a
638 \"thread score\" is.
639
640 This variable is local to the summary buffers."
641   :group 'gnus-threading
642   :group 'gnus-score-default
643   :type '(choice (const :tag "off" nil)
644                  integer))
645
646 (defcustom gnus-summary-mode-hook nil
647   "*A hook for Gnus summary mode.
648 This hook is run before any variables are set in the summary buffer."
649   :options '(turn-on-gnus-mailing-list-mode)
650   :group 'gnus-summary-various
651   :type 'hook)
652
653 (defcustom gnus-summary-menu-hook nil
654   "*Hook run after the creation of the summary mode menu."
655   :group 'gnus-summary-visual
656   :type 'hook)
657
658 (defcustom gnus-summary-exit-hook nil
659   "*A hook called on exit from the summary buffer.
660 It will be called with point in the group buffer."
661   :group 'gnus-summary-exit
662   :type 'hook)
663
664 (defcustom gnus-summary-prepare-hook nil
665   "*A hook called after the summary buffer has been generated.
666 If you want to modify the summary buffer, you can use this hook."
667   :group 'gnus-summary-various
668   :type 'hook)
669
670 (defcustom gnus-summary-prepared-hook nil
671   "*A hook called as the last thing after the summary buffer has been generated."
672   :group 'gnus-summary-various
673   :type 'hook)
674
675 (defcustom gnus-summary-generate-hook nil
676   "*A hook run just before generating the summary buffer.
677 This hook is commonly used to customize threading variables and the
678 like."
679   :group 'gnus-summary-various
680   :type 'hook)
681
682 (defcustom gnus-select-group-hook nil
683   "*A hook called when a newsgroup is selected.
684
685 If you'd like to simplify subjects like the
686 `gnus-summary-next-same-subject' command does, you can use the
687 following hook:
688
689  (setq gnus-select-group-hook
690       (list
691         (lambda ()
692           (mapcar (lambda (header)
693                      (mail-header-set-subject
694                       header
695                       (gnus-simplify-subject
696                        (mail-header-subject header) 're-only)))
697                   gnus-newsgroup-headers))))"
698   :group 'gnus-group-select
699   :type 'hook)
700
701 (defcustom gnus-select-article-hook nil
702   "*A hook called when an article is selected."
703   :group 'gnus-summary-choose
704   :type 'hook)
705
706 (defcustom gnus-visual-mark-article-hook
707   (list 'gnus-highlight-selected-summary)
708   "*Hook run after selecting an article in the summary buffer.
709 It is meant to be used for highlighting the article in some way.  It
710 is not run if `gnus-visual' is nil."
711   :group 'gnus-summary-visual
712   :type 'hook)
713
714 (defcustom gnus-parse-headers-hook nil
715   "*A hook called before parsing the headers."
716   :group 'gnus-various
717   :type 'hook)
718
719 (defcustom gnus-exit-group-hook nil
720   "*A hook called when exiting summary mode.
721 This hook is not called from the non-updating exit commands like `Q'."
722   :group 'gnus-various
723   :type 'hook)
724
725 (defcustom gnus-summary-update-hook
726   (list 'gnus-summary-highlight-line)
727   "*A hook called when a summary line is changed.
728 The hook will not be called if `gnus-visual' is nil.
729
730 The default function `gnus-summary-highlight-line' will
731 highlight the line according to the `gnus-summary-highlight'
732 variable."
733   :group 'gnus-summary-visual
734   :type 'hook)
735
736 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
737   "*A hook called when an article is selected for the first time.
738 The hook is intended to mark an article as read (or unread)
739 automatically when it is selected."
740   :group 'gnus-summary-choose
741   :type 'hook)
742
743 (defcustom gnus-group-no-more-groups-hook nil
744   "*A hook run when returning to group mode having no more (unread) groups."
745   :group 'gnus-group-select
746   :type 'hook)
747
748 (defcustom gnus-ps-print-hook nil
749   "*A hook run before ps-printing something from Gnus."
750   :group 'gnus-summary
751   :type 'hook)
752
753 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
754   "Face used for highlighting the current article in the summary buffer."
755   :group 'gnus-summary-visual
756   :type 'face)
757
758 (defcustom gnus-summary-highlight
759   '(((= mark gnus-canceled-mark)
760      . gnus-summary-cancelled-face)
761     ((and (> score default)
762           (or (= mark gnus-dormant-mark)
763               (= mark gnus-ticked-mark)))
764      . gnus-summary-high-ticked-face)
765     ((and (< score default)
766           (or (= mark gnus-dormant-mark)
767               (= mark gnus-ticked-mark)))
768      . gnus-summary-low-ticked-face)
769     ((or (= mark gnus-dormant-mark)
770          (= mark gnus-ticked-mark))
771      . gnus-summary-normal-ticked-face)
772     ((and (> score default) (= mark gnus-ancient-mark))
773      . gnus-summary-high-ancient-face)
774     ((and (< score default) (= mark gnus-ancient-mark))
775      . gnus-summary-low-ancient-face)
776     ((= mark gnus-ancient-mark)
777      . gnus-summary-normal-ancient-face)
778     ((and (> score default) (= mark gnus-unread-mark))
779      . gnus-summary-high-unread-face)
780     ((and (< score default) (= mark gnus-unread-mark))
781      . gnus-summary-low-unread-face)
782     ((= mark gnus-unread-mark)
783      . gnus-summary-normal-unread-face)
784     ((and (> score default) (memq mark (list gnus-downloadable-mark
785                                              gnus-undownloaded-mark)))
786      . gnus-summary-high-unread-face)
787     ((and (< score default) (memq mark (list gnus-downloadable-mark
788                                              gnus-undownloaded-mark)))
789      . gnus-summary-low-unread-face)
790     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
791      . gnus-summary-normal-unread-face)
792     ((> score default)
793      . gnus-summary-high-read-face)
794     ((< score default)
795      . gnus-summary-low-read-face)
796     (t
797      . gnus-summary-normal-read-face))
798   "*Controls the highlighting of summary buffer lines.
799
800 A list of (FORM . FACE) pairs.  When deciding how a a particular
801 summary line should be displayed, each form is evaluated.  The content
802 of the face field after the first true form is used.  You can change
803 how those summary lines are displayed, by editing the face field.
804
805 You can use the following variables in the FORM field.
806
807 score:   The articles score
808 default: The default article score.
809 below:   The score below which articles are automatically marked as read.
810 mark:    The articles mark."
811   :group 'gnus-summary-visual
812   :type '(repeat (cons (sexp :tag "Form" nil)
813                        face)))
814
815 (defcustom gnus-alter-header-function nil
816   "Function called to allow alteration of article header structures.
817 The function is called with one parameter, the article header vector,
818 which it may alter in any way.")
819
820 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
821   "Variable that says which function should be used to decode a string with encoded words.")
822
823 (defcustom gnus-extra-headers nil
824   "*Extra headers to parse."
825   :version "21.1"
826   :group 'gnus-summary
827   :type '(repeat symbol))
828
829 (defcustom gnus-ignored-from-addresses
830   (and user-mail-address (regexp-quote user-mail-address))
831   "*Regexp of From headers that may be suppressed in favor of To headers."
832   :version "21.1"
833   :group 'gnus-summary
834   :type 'regexp)
835
836 (defcustom gnus-group-charset-alist
837   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
838     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
839     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
840     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
841     ("^relcom\\>" koi8-r)
842     ("^fido7\\>" koi8-r)
843     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
844     ("^israel\\>" iso-8859-1)
845     ("^han\\>" euc-kr)
846     ("^alt.chinese.text.big5\\>" chinese-big5)
847     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
848     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
849     (".*" iso-8859-1))
850   "Alist of regexps (to match group names) and default charsets to be used when reading."
851   :type '(repeat (list (regexp :tag "Group")
852                        (symbol :tag "Charset")))
853   :group 'gnus-charset)
854
855 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
856   "List of charsets that should be ignored.
857 When these charsets are used in the \"charset\" parameter, the
858 default charset will be used instead."
859   :version "21.1"
860   :type '(repeat symbol)
861   :group 'gnus-charset)
862
863 (defcustom gnus-group-ignored-charsets-alist
864   '(("alt\\.chinese\\.text" iso-8859-1))
865   "Alist of regexps (to match group names) and charsets that should be ignored.
866 When these charsets are used in the \"charset\" parameter, the
867 default charset will be used instead."
868   :type '(repeat (cons (regexp :tag "Group")
869                        (repeat symbol)))
870   :group 'gnus-charset)
871
872 (defcustom gnus-group-highlight-words-alist nil
873   "Alist of group regexps and highlight regexps.
874 This variable uses the same syntax as `gnus-emphasis-alist'."
875   :version "21.1"
876   :type '(repeat (cons (regexp :tag "Group")
877                        (repeat (list (regexp :tag "Highlight regexp")
878                                      (number :tag "Group for entire word" 0)
879                                      (number :tag "Group for displayed part" 0)
880                                      (symbol :tag "Face"
881                                              gnus-emphasis-highlight-words)))))
882   :group 'gnus-summary-visual)
883
884 (defcustom gnus-summary-show-article-charset-alist
885   nil
886   "Alist of number and charset.
887 The article will be shown with the charset corresponding to the
888 numbered argument.
889 For example: ((1 . cn-gb-2312) (2 . big5))."
890   :version "21.1"
891   :type '(repeat (cons (number :tag "Argument" 1)
892                        (symbol :tag "Charset")))
893   :group 'gnus-charset)
894
895 (defcustom gnus-preserve-marks t
896   "Whether marks are preserved when moving, copying and respooling messages."
897   :version "21.1"
898   :type 'boolean
899   :group 'gnus-summary-marks)
900
901 (defcustom gnus-alter-articles-to-read-function nil
902   "Function to be called to alter the list of articles to be selected."
903   :type 'function
904   :group 'gnus-summary)
905
906 (defcustom gnus-orphan-score nil
907   "*All orphans get this score added.  Set in the score file."
908   :group 'gnus-score-default
909   :type '(choice (const nil)
910                  integer))
911
912 (defcustom gnus-summary-save-parts-default-mime "image/.*"
913   "*A regexp to match MIME parts when saving multiple parts of a message
914 with gnus-summary-save-parts (X m). This regexp will be used by default
915 when prompting the user for which type of files to save."
916   :group 'gnus-summary
917   :type 'regexp)
918
919
920 (defcustom gnus-summary-save-parts-default-mime "image/.*"
921   "*A regexp to match MIME parts when saving multiple parts of a message
922 with gnus-summary-save-parts (X m). This regexp will be used by default
923 when prompting the user for which type of files to save."
924   :group 'gnus-summary
925   :type 'regexp)
926
927
928 ;;; Internal variables
929
930 (defvar gnus-article-mime-handles nil)
931 (defvar gnus-article-decoded-p nil)
932 (defvar gnus-article-charset nil)
933 (defvar gnus-article-ignored-charsets nil)
934 (defvar gnus-scores-exclude-files nil)
935 (defvar gnus-page-broken nil)
936 (defvar gnus-inhibit-mime-unbuttonizing nil)
937
938 (defvar gnus-original-article nil)
939 (defvar gnus-article-internal-prepare-hook nil)
940 (defvar gnus-newsgroup-process-stack nil)
941
942 (defvar gnus-thread-indent-array nil)
943 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
944 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
945   "Function called to sort the articles within a thread after it has been gathered together.")
946
947 (defvar gnus-summary-save-parts-type-history nil)
948 (defvar gnus-summary-save-parts-last-directory nil)
949
950 (defvar gnus-summary-save-parts-type-history nil)
951 (defvar gnus-summary-save-parts-last-directory nil)
952
953 ;; Avoid highlighting in kill files.
954 (defvar gnus-summary-inhibit-highlight nil)
955 (defvar gnus-newsgroup-selected-overlay nil)
956 (defvar gnus-inhibit-limiting nil)
957 (defvar gnus-newsgroup-adaptive-score-file nil)
958 (defvar gnus-current-score-file nil)
959 (defvar gnus-current-move-group nil)
960 (defvar gnus-current-copy-group nil)
961 (defvar gnus-current-crosspost-group nil)
962
963 (defvar gnus-newsgroup-dependencies nil)
964 (defvar gnus-newsgroup-adaptive nil)
965 (defvar gnus-summary-display-article-function nil)
966 (defvar gnus-summary-highlight-line-function nil
967   "Function called after highlighting a summary line.")
968
969 (defvar gnus-summary-line-format-alist
970   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
971     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
972     (?s gnus-tmp-subject-or-nil ?s)
973     (?n gnus-tmp-name ?s)
974     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
975         ?s)
976     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
977             gnus-tmp-from) ?s)
978     (?F gnus-tmp-from ?s)
979     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
980     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
981     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
982     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
983     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
984     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
985     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
986     (?L gnus-tmp-lines ?d)
987     (?I gnus-tmp-indentation ?s)
988     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
989     (?R gnus-tmp-replied ?c)
990     (?\[ gnus-tmp-opening-bracket ?c)
991     (?\] gnus-tmp-closing-bracket ?c)
992     (?\> (make-string gnus-tmp-level ? ) ?s)
993     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
994     (?i gnus-tmp-score ?d)
995     (?z gnus-tmp-score-char ?c)
996     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
997     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
998     (?U gnus-tmp-unread ?c)
999     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1000     (?t (gnus-summary-number-of-articles-in-thread
1001          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1002         ?d)
1003     (?e (gnus-summary-number-of-articles-in-thread
1004          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1005         ?c)
1006     (?u gnus-tmp-user-defined ?s)
1007     (?P (gnus-pick-line-number) ?d))
1008   "An alist of format specifications that can appear in summary lines.
1009 These are paired with what variables they correspond with, along with
1010 the type of the variable (string, integer, character, etc).")
1011
1012 (defvar gnus-summary-dummy-line-format-alist
1013   `((?S gnus-tmp-subject ?s)
1014     (?N gnus-tmp-number ?d)
1015     (?u gnus-tmp-user-defined ?s)))
1016
1017 (defvar gnus-summary-mode-line-format-alist
1018   `((?G gnus-tmp-group-name ?s)
1019     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1020     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1021     (?A gnus-tmp-article-number ?d)
1022     (?Z gnus-tmp-unread-and-unselected ?s)
1023     (?V gnus-version ?s)
1024     (?U gnus-tmp-unread-and-unticked ?d)
1025     (?S gnus-tmp-subject ?s)
1026     (?e gnus-tmp-unselected ?d)
1027     (?u gnus-tmp-user-defined ?s)
1028     (?d (length gnus-newsgroup-dormant) ?d)
1029     (?t (length gnus-newsgroup-marked) ?d)
1030     (?r (length gnus-newsgroup-reads) ?d)
1031     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1032     (?E gnus-newsgroup-expunged-tally ?d)
1033     (?s (gnus-current-score-file-nondirectory) ?s)))
1034
1035 (defvar gnus-last-search-regexp nil
1036   "Default regexp for article search command.")
1037
1038 (defvar gnus-last-shell-command nil
1039   "Default shell command on article.")
1040
1041 (defvar gnus-newsgroup-begin nil)
1042 (defvar gnus-newsgroup-end nil)
1043 (defvar gnus-newsgroup-last-rmail nil)
1044 (defvar gnus-newsgroup-last-mail nil)
1045 (defvar gnus-newsgroup-last-folder nil)
1046 (defvar gnus-newsgroup-last-file nil)
1047 (defvar gnus-newsgroup-auto-expire nil)
1048 (defvar gnus-newsgroup-active nil)
1049
1050 (defvar gnus-newsgroup-data nil)
1051 (defvar gnus-newsgroup-data-reverse nil)
1052 (defvar gnus-newsgroup-limit nil)
1053 (defvar gnus-newsgroup-limits nil)
1054
1055 (defvar gnus-newsgroup-unreads nil
1056   "List of unread articles in the current newsgroup.")
1057
1058 (defvar gnus-newsgroup-unselected nil
1059   "List of unselected unread articles in the current newsgroup.")
1060
1061 (defvar gnus-newsgroup-reads nil
1062   "Alist of read articles and article marks in the current newsgroup.")
1063
1064 (defvar gnus-newsgroup-expunged-tally nil)
1065
1066 (defvar gnus-newsgroup-marked nil
1067   "List of ticked articles in the current newsgroup (a subset of unread art).")
1068
1069 (defvar gnus-newsgroup-killed nil
1070   "List of ranges of articles that have been through the scoring process.")
1071
1072 (defvar gnus-newsgroup-cached nil
1073   "List of articles that come from the article cache.")
1074
1075 (defvar gnus-newsgroup-saved nil
1076   "List of articles that have been saved.")
1077
1078 (defvar gnus-newsgroup-kill-headers nil)
1079
1080 (defvar gnus-newsgroup-replied nil
1081   "List of articles that have been replied to in the current newsgroup.")
1082
1083 (defvar gnus-newsgroup-expirable nil
1084   "List of articles in the current newsgroup that can be expired.")
1085
1086 (defvar gnus-newsgroup-processable nil
1087   "List of articles in the current newsgroup that can be processed.")
1088
1089 (defvar gnus-newsgroup-downloadable nil
1090   "List of articles in the current newsgroup that can be processed.")
1091
1092 (defvar gnus-newsgroup-undownloaded nil
1093   "List of articles in the current newsgroup that haven't been downloaded..")
1094
1095 (defvar gnus-newsgroup-unsendable nil
1096   "List of articles in the current newsgroup that won't be sent.")
1097
1098 (defvar gnus-newsgroup-bookmarks nil
1099   "List of articles in the current newsgroup that have bookmarks.")
1100
1101 (defvar gnus-newsgroup-dormant nil
1102   "List of dormant articles in the current newsgroup.")
1103
1104 (defvar gnus-newsgroup-scored nil
1105   "List of scored articles in the current newsgroup.")
1106
1107 (defvar gnus-newsgroup-headers nil
1108   "List of article headers in the current newsgroup.")
1109
1110 (defvar gnus-newsgroup-threads nil)
1111
1112 (defvar gnus-newsgroup-prepared nil
1113   "Whether the current group has been prepared properly.")
1114
1115 (defvar gnus-newsgroup-ancient nil
1116   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1117
1118 (defvar gnus-newsgroup-sparse nil)
1119
1120 (defvar gnus-current-article nil)
1121 (defvar gnus-article-current nil)
1122 (defvar gnus-current-headers nil)
1123 (defvar gnus-have-all-headers nil)
1124 (defvar gnus-last-article nil)
1125 (defvar gnus-newsgroup-history nil)
1126 (defvar gnus-newsgroup-charset nil)
1127 (defvar gnus-newsgroup-ephemeral-charset nil)
1128 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1129
1130 (defvar gnus-article-before-search nil)
1131
1132 (defconst gnus-summary-local-variables
1133   '(gnus-newsgroup-name
1134     gnus-newsgroup-begin gnus-newsgroup-end
1135     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1136     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1137     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1138     gnus-newsgroup-unselected gnus-newsgroup-marked
1139     gnus-newsgroup-reads gnus-newsgroup-saved
1140     gnus-newsgroup-replied gnus-newsgroup-expirable
1141     gnus-newsgroup-processable gnus-newsgroup-killed
1142     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1143     gnus-newsgroup-unsendable
1144     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1145     gnus-newsgroup-headers gnus-newsgroup-threads
1146     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1147     gnus-current-article gnus-current-headers gnus-have-all-headers
1148     gnus-last-article gnus-article-internal-prepare-hook
1149     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1150     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1151     gnus-thread-expunge-below
1152     gnus-score-alist gnus-current-score-file
1153     (gnus-summary-expunge-below . global)
1154     (gnus-summary-mark-below . global)
1155     (gnus-orphan-score . global)
1156     gnus-newsgroup-active gnus-scores-exclude-files
1157     gnus-newsgroup-history gnus-newsgroup-ancient
1158     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1159     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1160     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1161     (gnus-newsgroup-expunged-tally . 0)
1162     gnus-cache-removable-articles gnus-newsgroup-cached
1163     gnus-newsgroup-data gnus-newsgroup-data-reverse
1164     gnus-newsgroup-limit gnus-newsgroup-limits
1165     gnus-newsgroup-charset)
1166   "Variables that are buffer-local to the summary buffers.")
1167
1168 (defvar gnus-newsgroup-variables nil
1169   "Variables that have separate values in the newsgroups.")
1170
1171 ;; Byte-compiler warning.
1172 (eval-when-compile (defvar gnus-article-mode-map))
1173
1174 ;; MIME stuff.
1175
1176 (defvar gnus-decode-encoded-word-methods
1177   '(mail-decode-encoded-word-string)
1178   "List of methods used to decode encoded words.
1179
1180 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1181 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1182 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1183 whose names match REGEXP.
1184
1185 For example:
1186 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1187  mail-decode-encoded-word-string
1188  (\"chinese\" . rfc1843-decode-string))")
1189
1190 (defvar gnus-decode-encoded-word-methods-cache nil)
1191
1192 (defun gnus-multi-decode-encoded-word-string (string)
1193   "Apply the functions from `gnus-encoded-word-methods' that match."
1194   (unless (and gnus-decode-encoded-word-methods-cache
1195                (eq gnus-newsgroup-name
1196                    (car gnus-decode-encoded-word-methods-cache)))
1197     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1198     (mapcar (lambda (x)
1199               (if (symbolp x)
1200                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1201                 (if (and gnus-newsgroup-name
1202                          (string-match (car x) gnus-newsgroup-name))
1203                     (nconc gnus-decode-encoded-word-methods-cache
1204                            (list (cdr x))))))
1205           gnus-decode-encoded-word-methods))
1206   (let ((xlist gnus-decode-encoded-word-methods-cache))
1207     (pop xlist)
1208     (while xlist
1209       (setq string (funcall (pop xlist) string))))
1210   string)
1211
1212 ;; Subject simplification.
1213
1214 (defun gnus-simplify-whitespace (str)
1215   "Remove excessive whitespace from STR."
1216   (let ((mystr str))
1217     ;; Multiple spaces.
1218     (while (string-match "[ \t][ \t]+" mystr)
1219       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1220                           " "
1221                           (substring mystr (match-end 0)))))
1222     ;; Leading spaces.
1223     (when (string-match "^[ \t]+" mystr)
1224       (setq mystr (substring mystr (match-end 0))))
1225     ;; Trailing spaces.
1226     (when (string-match "[ \t]+$" mystr)
1227       (setq mystr (substring mystr 0 (match-beginning 0))))
1228     mystr))
1229
1230 (defsubst gnus-simplify-subject-re (subject)
1231   "Remove \"Re:\" from subject lines."
1232   (if (string-match "^[Rr][Ee]: *" subject)
1233       (substring subject (match-end 0))
1234     subject))
1235
1236 (defun gnus-simplify-subject (subject &optional re-only)
1237   "Remove `Re:' and words in parentheses.
1238 If RE-ONLY is non-nil, strip leading `Re:'s only."
1239   (let ((case-fold-search t))           ;Ignore case.
1240     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1241     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1242       (setq subject (substring subject (match-end 0))))
1243     ;; Remove uninteresting prefixes.
1244     (when (and (not re-only)
1245                gnus-simplify-ignored-prefixes
1246                (string-match gnus-simplify-ignored-prefixes subject))
1247       (setq subject (substring subject (match-end 0))))
1248     ;; Remove words in parentheses from end.
1249     (unless re-only
1250       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1251         (setq subject (substring subject 0 (match-beginning 0)))))
1252     ;; Return subject string.
1253     subject))
1254
1255 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1256 ;; all whitespace.
1257 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1258   (goto-char (point-min))
1259   (while (re-search-forward regexp nil t)
1260     (replace-match (or newtext ""))))
1261
1262 (defun gnus-simplify-buffer-fuzzy ()
1263   "Simplify string in the buffer fuzzily.
1264 The string in the accessible portion of the current buffer is simplified.
1265 It is assumed to be a single-line subject.
1266 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1267 matter is removed.  Additional things can be deleted by setting
1268 `gnus-simplify-subject-fuzzy-regexp'."
1269   (let ((case-fold-search t)
1270         (modified-tick))
1271     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1272
1273     (while (not (eq modified-tick (buffer-modified-tick)))
1274       (setq modified-tick (buffer-modified-tick))
1275       (cond
1276        ((listp gnus-simplify-subject-fuzzy-regexp)
1277         (mapcar 'gnus-simplify-buffer-fuzzy-step
1278                 gnus-simplify-subject-fuzzy-regexp))
1279        (gnus-simplify-subject-fuzzy-regexp
1280         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1281       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1282       (gnus-simplify-buffer-fuzzy-step
1283        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1284       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1285
1286     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1287     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1288     (gnus-simplify-buffer-fuzzy-step " $")
1289     (gnus-simplify-buffer-fuzzy-step "^ +")))
1290
1291 (defun gnus-simplify-subject-fuzzy (subject)
1292   "Simplify a subject string fuzzily.
1293 See `gnus-simplify-buffer-fuzzy' for details."
1294   (save-excursion
1295     (gnus-set-work-buffer)
1296     (let ((case-fold-search t))
1297       ;; Remove uninteresting prefixes.
1298       (when (and gnus-simplify-ignored-prefixes
1299                  (string-match gnus-simplify-ignored-prefixes subject))
1300         (setq subject (substring subject (match-end 0))))
1301       (insert subject)
1302       (inline (gnus-simplify-buffer-fuzzy))
1303       (buffer-string))))
1304
1305 (defsubst gnus-simplify-subject-fully (subject)
1306   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1307   (cond
1308    (gnus-simplify-subject-functions
1309     (gnus-map-function gnus-simplify-subject-functions subject))
1310    ((null gnus-summary-gather-subject-limit)
1311     (gnus-simplify-subject-re subject))
1312    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1313     (gnus-simplify-subject-fuzzy subject))
1314    ((numberp gnus-summary-gather-subject-limit)
1315     (gnus-limit-string (gnus-simplify-subject-re subject)
1316                        gnus-summary-gather-subject-limit))
1317    (t
1318     subject)))
1319
1320 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1321   "Check whether two subjects are equal.
1322 If optional argument simple-first is t, first argument is already
1323 simplified."
1324   (cond
1325    ((null simple-first)
1326     (equal (gnus-simplify-subject-fully s1)
1327            (gnus-simplify-subject-fully s2)))
1328    (t
1329     (equal s1
1330            (gnus-simplify-subject-fully s2)))))
1331
1332 (defun gnus-summary-bubble-group ()
1333   "Increase the score of the current group.
1334 This is a handy function to add to `gnus-summary-exit-hook' to
1335 increase the score of each group you read."
1336   (gnus-group-add-score gnus-newsgroup-name))
1337
1338 \f
1339 ;;;
1340 ;;; Gnus summary mode
1341 ;;;
1342
1343 (put 'gnus-summary-mode 'mode-class 'special)
1344
1345 (when t
1346   ;; Non-orthogonal keys
1347
1348   (gnus-define-keys gnus-summary-mode-map
1349     " " gnus-summary-next-page
1350     "\177" gnus-summary-prev-page
1351     [delete] gnus-summary-prev-page
1352     [backspace] gnus-summary-prev-page
1353     "\r" gnus-summary-scroll-up
1354     "\M-\r" gnus-summary-scroll-down
1355     "n" gnus-summary-next-unread-article
1356     "p" gnus-summary-prev-unread-article
1357     "N" gnus-summary-next-article
1358     "P" gnus-summary-prev-article
1359     "\M-\C-n" gnus-summary-next-same-subject
1360     "\M-\C-p" gnus-summary-prev-same-subject
1361     "\M-n" gnus-summary-next-unread-subject
1362     "\M-p" gnus-summary-prev-unread-subject
1363     "." gnus-summary-first-unread-article
1364     "," gnus-summary-best-unread-article
1365     "\M-s" gnus-summary-search-article-forward
1366     "\M-r" gnus-summary-search-article-backward
1367     "<" gnus-summary-beginning-of-article
1368     ">" gnus-summary-end-of-article
1369     "j" gnus-summary-goto-article
1370     "^" gnus-summary-refer-parent-article
1371     "\M-^" gnus-summary-refer-article
1372     "u" gnus-summary-tick-article-forward
1373     "!" gnus-summary-tick-article-forward
1374     "U" gnus-summary-tick-article-backward
1375     "d" gnus-summary-mark-as-read-forward
1376     "D" gnus-summary-mark-as-read-backward
1377     "E" gnus-summary-mark-as-expirable
1378     "\M-u" gnus-summary-clear-mark-forward
1379     "\M-U" gnus-summary-clear-mark-backward
1380     "k" gnus-summary-kill-same-subject-and-select
1381     "\C-k" gnus-summary-kill-same-subject
1382     "\M-\C-k" gnus-summary-kill-thread
1383     "\M-\C-l" gnus-summary-lower-thread
1384     "e" gnus-summary-edit-article
1385     "#" gnus-summary-mark-as-processable
1386     "\M-#" gnus-summary-unmark-as-processable
1387     "\M-\C-t" gnus-summary-toggle-threads
1388     "\M-\C-s" gnus-summary-show-thread
1389     "\M-\C-h" gnus-summary-hide-thread
1390     "\M-\C-f" gnus-summary-next-thread
1391     "\M-\C-b" gnus-summary-prev-thread
1392     [(meta down)] gnus-summary-next-thread
1393     [(meta up)] gnus-summary-prev-thread
1394     "\M-\C-u" gnus-summary-up-thread
1395     "\M-\C-d" gnus-summary-down-thread
1396     "&" gnus-summary-execute-command
1397     "c" gnus-summary-catchup-and-exit
1398     "\C-w" gnus-summary-mark-region-as-read
1399     "\C-t" gnus-summary-toggle-truncation
1400     "?" gnus-summary-mark-as-dormant
1401     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1402     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1403     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1404     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1405     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1406     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1407     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1408     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1409     "=" gnus-summary-expand-window
1410     "\C-x\C-s" gnus-summary-reselect-current-group
1411     "\M-g" gnus-summary-rescan-group
1412     "w" gnus-summary-stop-page-breaking
1413     "\C-c\C-r" gnus-summary-caesar-message
1414     "f" gnus-summary-followup
1415     "F" gnus-summary-followup-with-original
1416     "C" gnus-summary-cancel-article
1417     "r" gnus-summary-reply
1418     "R" gnus-summary-reply-with-original
1419     "\C-c\C-f" gnus-summary-mail-forward
1420     "o" gnus-summary-save-article
1421     "\C-o" gnus-summary-save-article-mail
1422     "|" gnus-summary-pipe-output
1423     "\M-k" gnus-summary-edit-local-kill
1424     "\M-K" gnus-summary-edit-global-kill
1425     ;; "V" gnus-version
1426     "\C-c\C-d" gnus-summary-describe-group
1427     "q" gnus-summary-exit
1428     "Q" gnus-summary-exit-no-update
1429     "\C-c\C-i" gnus-info-find-node
1430     gnus-mouse-2 gnus-mouse-pick-article
1431     "m" gnus-summary-mail-other-window
1432     "a" gnus-summary-post-news
1433     "x" gnus-summary-limit-to-unread
1434     "s" gnus-summary-isearch-article
1435     "t" gnus-summary-toggle-header
1436     "g" gnus-summary-show-article
1437     "l" gnus-summary-goto-last-article
1438     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1439     "\C-d" gnus-summary-enter-digest-group
1440     "\M-\C-d" gnus-summary-read-document
1441     "\M-\C-e" gnus-summary-edit-parameters
1442     "\M-\C-a" gnus-summary-customize-parameters
1443     "\C-c\C-b" gnus-bug
1444     "*" gnus-cache-enter-article
1445     "\M-*" gnus-cache-remove-article
1446     "\M-&" gnus-summary-universal-argument
1447     "\C-l" gnus-recenter
1448     "I" gnus-summary-increase-score
1449     "L" gnus-summary-lower-score
1450     "\M-i" gnus-symbolic-argument
1451     "h" gnus-summary-select-article-buffer
1452
1453     "b" gnus-article-view-part
1454     "\M-t" gnus-summary-toggle-display-buttonized
1455
1456     "V" gnus-summary-score-map
1457     "X" gnus-uu-extract-map
1458     "S" gnus-summary-send-map)
1459
1460   ;; Sort of orthogonal keymap
1461   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1462     "t" gnus-summary-tick-article-forward
1463     "!" gnus-summary-tick-article-forward
1464     "d" gnus-summary-mark-as-read-forward
1465     "r" gnus-summary-mark-as-read-forward
1466     "c" gnus-summary-clear-mark-forward
1467     " " gnus-summary-clear-mark-forward
1468     "e" gnus-summary-mark-as-expirable
1469     "x" gnus-summary-mark-as-expirable
1470     "?" gnus-summary-mark-as-dormant
1471     "b" gnus-summary-set-bookmark
1472     "B" gnus-summary-remove-bookmark
1473     "#" gnus-summary-mark-as-processable
1474     "\M-#" gnus-summary-unmark-as-processable
1475     "S" gnus-summary-limit-include-expunged
1476     "C" gnus-summary-catchup
1477     "H" gnus-summary-catchup-to-here
1478     "\C-c" gnus-summary-catchup-all
1479     "k" gnus-summary-kill-same-subject-and-select
1480     "K" gnus-summary-kill-same-subject
1481     "P" gnus-uu-mark-map)
1482
1483   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1484     "c" gnus-summary-clear-above
1485     "u" gnus-summary-tick-above
1486     "m" gnus-summary-mark-above
1487     "k" gnus-summary-kill-below)
1488
1489   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1490     "/" gnus-summary-limit-to-subject
1491     "n" gnus-summary-limit-to-articles
1492     "w" gnus-summary-pop-limit
1493     "s" gnus-summary-limit-to-subject
1494     "a" gnus-summary-limit-to-author
1495     "u" gnus-summary-limit-to-unread
1496     "m" gnus-summary-limit-to-marks
1497     "M" gnus-summary-limit-exclude-marks
1498     "v" gnus-summary-limit-to-score
1499     "*" gnus-summary-limit-include-cached
1500     "D" gnus-summary-limit-include-dormant
1501     "T" gnus-summary-limit-include-thread
1502     "d" gnus-summary-limit-exclude-dormant
1503     "t" gnus-summary-limit-to-age
1504     "x" gnus-summary-limit-to-extra
1505     "E" gnus-summary-limit-include-expunged
1506     "c" gnus-summary-limit-exclude-childless-dormant
1507     "C" gnus-summary-limit-mark-excluded-as-read)
1508
1509   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1510     "n" gnus-summary-next-unread-article
1511     "p" gnus-summary-prev-unread-article
1512     "N" gnus-summary-next-article
1513     "P" gnus-summary-prev-article
1514     "\C-n" gnus-summary-next-same-subject
1515     "\C-p" gnus-summary-prev-same-subject
1516     "\M-n" gnus-summary-next-unread-subject
1517     "\M-p" gnus-summary-prev-unread-subject
1518     "f" gnus-summary-first-unread-article
1519     "b" gnus-summary-best-unread-article
1520     "j" gnus-summary-goto-article
1521     "g" gnus-summary-goto-subject
1522     "l" gnus-summary-goto-last-article
1523     "o" gnus-summary-pop-article)
1524
1525   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1526     "k" gnus-summary-kill-thread
1527     "l" gnus-summary-lower-thread
1528     "i" gnus-summary-raise-thread
1529     "T" gnus-summary-toggle-threads
1530     "t" gnus-summary-rethread-current
1531     "^" gnus-summary-reparent-thread
1532     "s" gnus-summary-show-thread
1533     "S" gnus-summary-show-all-threads
1534     "h" gnus-summary-hide-thread
1535     "H" gnus-summary-hide-all-threads
1536     "n" gnus-summary-next-thread
1537     "p" gnus-summary-prev-thread
1538     "u" gnus-summary-up-thread
1539     "o" gnus-summary-top-thread
1540     "d" gnus-summary-down-thread
1541     "#" gnus-uu-mark-thread
1542     "\M-#" gnus-uu-unmark-thread)
1543
1544   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1545     "g" gnus-summary-prepare
1546     "c" gnus-summary-insert-cached-articles)
1547
1548   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1549     "c" gnus-summary-catchup-and-exit
1550     "C" gnus-summary-catchup-all-and-exit
1551     "E" gnus-summary-exit-no-update
1552     "Q" gnus-summary-exit
1553     "Z" gnus-summary-exit
1554     "n" gnus-summary-catchup-and-goto-next-group
1555     "R" gnus-summary-reselect-current-group
1556     "G" gnus-summary-rescan-group
1557     "N" gnus-summary-next-group
1558     "s" gnus-summary-save-newsrc
1559     "P" gnus-summary-prev-group)
1560
1561   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1562     " " gnus-summary-next-page
1563     "n" gnus-summary-next-page
1564     "\177" gnus-summary-prev-page
1565     [delete] gnus-summary-prev-page
1566     "p" gnus-summary-prev-page
1567     "\r" gnus-summary-scroll-up
1568     "\M-\r" gnus-summary-scroll-down
1569     "<" gnus-summary-beginning-of-article
1570     ">" gnus-summary-end-of-article
1571     "b" gnus-summary-beginning-of-article
1572     "e" gnus-summary-end-of-article
1573     "^" gnus-summary-refer-parent-article
1574     "r" gnus-summary-refer-parent-article
1575     "D" gnus-summary-enter-digest-group
1576     "R" gnus-summary-refer-references
1577     "T" gnus-summary-refer-thread
1578     "g" gnus-summary-show-article
1579     "s" gnus-summary-isearch-article
1580     "P" gnus-summary-print-article
1581     "M" gnus-mailing-list-insinuate
1582     "t" gnus-article-babel)
1583
1584   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1585     "b" gnus-article-add-buttons
1586     "B" gnus-article-add-buttons-to-head
1587     "o" gnus-article-treat-overstrike
1588     "e" gnus-article-emphasize
1589     "w" gnus-article-fill-cited-article
1590     "Q" gnus-article-fill-long-lines
1591     "C" gnus-article-capitalize-sentences
1592     "c" gnus-article-remove-cr
1593     "q" gnus-article-de-quoted-unreadable
1594     "6" gnus-article-de-base64-unreadable
1595     "Z" gnus-article-decode-HZ
1596     "h" gnus-article-wash-html
1597     "s" gnus-summary-force-verify-and-decrypt
1598     "f" gnus-article-display-x-face
1599     "l" gnus-summary-stop-page-breaking
1600     "r" gnus-summary-caesar-message
1601     "t" gnus-summary-toggle-header
1602     "v" gnus-summary-verbose-headers
1603     "H" gnus-article-strip-headers-in-body
1604     "p" gnus-article-verify-x-pgp-sig
1605     "d" gnus-article-treat-dumbquotes)
1606
1607   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1608     "a" gnus-article-hide
1609     "h" gnus-article-hide-headers
1610     "b" gnus-article-hide-boring-headers
1611     "s" gnus-article-hide-signature
1612     "c" gnus-article-hide-citation
1613     "C" gnus-article-hide-citation-in-followups
1614     "l" gnus-article-hide-list-identifiers
1615     "p" gnus-article-hide-pgp
1616     "B" gnus-article-strip-banner
1617     "P" gnus-article-hide-pem
1618     "\C-c" gnus-article-hide-citation-maybe)
1619
1620   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1621     "a" gnus-article-highlight
1622     "h" gnus-article-highlight-headers
1623     "c" gnus-article-highlight-citation
1624     "s" gnus-article-highlight-signature)
1625
1626   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1627     "w" gnus-article-decode-mime-words
1628     "c" gnus-article-decode-charset
1629     "v" gnus-mime-view-all-parts
1630     "b" gnus-article-view-part)
1631
1632   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1633     "z" gnus-article-date-ut
1634     "u" gnus-article-date-ut
1635     "l" gnus-article-date-local
1636     "p" gnus-article-date-english
1637     "e" gnus-article-date-lapsed
1638     "o" gnus-article-date-original
1639     "i" gnus-article-date-iso8601
1640     "s" gnus-article-date-user)
1641
1642   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1643     "t" gnus-article-remove-trailing-blank-lines
1644     "l" gnus-article-strip-leading-blank-lines
1645     "m" gnus-article-strip-multiple-blank-lines
1646     "a" gnus-article-strip-blank-lines
1647     "A" gnus-article-strip-all-blank-lines
1648     "s" gnus-article-strip-leading-space
1649     "e" gnus-article-strip-trailing-space)
1650
1651   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1652     "v" gnus-version
1653     "f" gnus-summary-fetch-faq
1654     "d" gnus-summary-describe-group
1655     "h" gnus-summary-describe-briefly
1656     "i" gnus-info-find-node)
1657
1658   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1659     "e" gnus-summary-expire-articles
1660     "\M-\C-e" gnus-summary-expire-articles-now
1661     "\177" gnus-summary-delete-article
1662     [delete] gnus-summary-delete-article
1663     [backspace] gnus-summary-delete-article
1664     "m" gnus-summary-move-article
1665     "r" gnus-summary-respool-article
1666     "w" gnus-summary-edit-article
1667     "c" gnus-summary-copy-article
1668     "B" gnus-summary-crosspost-article
1669     "q" gnus-summary-respool-query
1670     "t" gnus-summary-respool-trace
1671     "i" gnus-summary-import-article
1672     "p" gnus-summary-article-posted-p)
1673
1674   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1675     "o" gnus-summary-save-article
1676     "m" gnus-summary-save-article-mail
1677     "F" gnus-summary-write-article-file
1678     "r" gnus-summary-save-article-rmail
1679     "f" gnus-summary-save-article-file
1680     "b" gnus-summary-save-article-body-file
1681     "h" gnus-summary-save-article-folder
1682     "v" gnus-summary-save-article-vm
1683     "p" gnus-summary-pipe-output
1684     "s" gnus-soup-add-article)
1685
1686   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1687     "b" gnus-summary-display-buttonized
1688     "m" gnus-summary-repair-multipart
1689     "v" gnus-article-view-part
1690     "o" gnus-article-save-part
1691     "c" gnus-article-copy-part
1692     "C" gnus-article-view-part-as-charset
1693     "e" gnus-article-externalize-part
1694     "E" gnus-article-encrypt-body
1695     "i" gnus-article-inline-part
1696     "|" gnus-article-pipe-part))
1697
1698 (defun gnus-summary-make-menu-bar ()
1699   (gnus-turn-off-edit-menu 'summary)
1700
1701   (unless (boundp 'gnus-summary-misc-menu)
1702
1703     (easy-menu-define
1704      gnus-summary-kill-menu gnus-summary-mode-map ""
1705      (cons
1706       "Score"
1707       (nconc
1708        (list
1709         ["Customize" gnus-score-customize t])
1710        (gnus-make-score-map 'increase)
1711        (gnus-make-score-map 'lower)
1712        '(("Mark"
1713           ["Kill below" gnus-summary-kill-below t]
1714           ["Mark above" gnus-summary-mark-above t]
1715           ["Tick above" gnus-summary-tick-above t]
1716           ["Clear above" gnus-summary-clear-above t])
1717          ["Current score" gnus-summary-current-score t]
1718          ["Set score" gnus-summary-set-score t]
1719          ["Switch current score file..." gnus-score-change-score-file t]
1720          ["Set mark below..." gnus-score-set-mark-below t]
1721          ["Set expunge below..." gnus-score-set-expunge-below t]
1722          ["Edit current score file" gnus-score-edit-current-scores t]
1723          ["Edit score file" gnus-score-edit-file t]
1724          ["Trace score" gnus-score-find-trace t]
1725          ["Find words" gnus-score-find-favourite-words t]
1726          ["Rescore buffer" gnus-summary-rescore t]
1727          ["Increase score..." gnus-summary-increase-score t]
1728          ["Lower score..." gnus-summary-lower-score t]))))
1729
1730     ;; Define both the Article menu in the summary buffer and the equivalent
1731     ;; Commands menu in the article buffer here for consistency.
1732     (let ((innards
1733            `(("Hide"
1734               ["All" gnus-article-hide t]
1735               ["Headers" gnus-article-hide-headers t]
1736               ["Signature" gnus-article-hide-signature t]
1737               ["Citation" gnus-article-hide-citation t]
1738               ["List identifiers" gnus-article-hide-list-identifiers t]
1739               ["PGP" gnus-article-hide-pgp t]
1740               ["Banner" gnus-article-strip-banner t]
1741               ["Boring headers" gnus-article-hide-boring-headers t])
1742              ("Highlight"
1743               ["All" gnus-article-highlight t]
1744               ["Headers" gnus-article-highlight-headers t]
1745               ["Signature" gnus-article-highlight-signature t]
1746               ["Citation" gnus-article-highlight-citation t])
1747              ("MIME"
1748               ["Words" gnus-article-decode-mime-words t]
1749               ["Charset" gnus-article-decode-charset t]
1750               ["QP" gnus-article-de-quoted-unreadable t]
1751               ["Base64" gnus-article-de-base64-unreadable t]
1752               ["View all" gnus-mime-view-all-parts t]
1753               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1754               ["Encrypt body" gnus-article-encrypt-body t])
1755              ("Date"
1756               ["Local" gnus-article-date-local t]
1757               ["ISO8601" gnus-article-date-iso8601 t]
1758               ["UT" gnus-article-date-ut t]
1759               ["Original" gnus-article-date-original t]
1760               ["Lapsed" gnus-article-date-lapsed t]
1761               ["User-defined" gnus-article-date-user t])
1762              ("Washing"
1763               ("Remove Blanks"
1764                ["Leading" gnus-article-strip-leading-blank-lines t]
1765                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1766                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1767                ["All of the above" gnus-article-strip-blank-lines t]
1768                ["All" gnus-article-strip-all-blank-lines t]
1769                ["Leading space" gnus-article-strip-leading-space t]
1770                ["Trailing space" gnus-article-strip-trailing-space t])
1771               ["Overstrike" gnus-article-treat-overstrike t]
1772               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1773               ["Emphasis" gnus-article-emphasize t]
1774               ["Word wrap" gnus-article-fill-cited-article t]
1775               ["Fill long lines" gnus-article-fill-long-lines t]
1776               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1777               ["CR" gnus-article-remove-cr t]
1778               ["Show X-Face" gnus-article-display-x-face t]
1779               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1780               ["Base64" gnus-article-de-base64-unreadable t]
1781               ["Rot 13" gnus-summary-caesar-message
1782                ,@(if (featurep 'xemacs) nil
1783                    '(:help "\"Caesar rotate\" article by 13"))]
1784               ["Unix pipe" gnus-summary-pipe-message t]
1785               ["Add buttons" gnus-article-add-buttons t]
1786               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1787               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1788               ["Verbose header" gnus-summary-verbose-headers t]
1789               ["Toggle header" gnus-summary-toggle-header t]
1790               ["Html" gnus-article-wash-html t]
1791               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1792               ["HZ" gnus-article-decode-HZ t])
1793              ("Output"
1794               ["Save in default format" gnus-summary-save-article
1795                ,@(if (featurep 'xemacs) nil
1796                    '(:help "Save article using default method"))]
1797               ["Save in file" gnus-summary-save-article-file
1798                ,@(if (featurep 'xemacs) nil
1799                    '(:help "Save article in file"))]
1800               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1801               ["Save in MH folder" gnus-summary-save-article-folder t]
1802               ["Save in VM folder" gnus-summary-save-article-vm t]
1803               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1804               ["Save body in file" gnus-summary-save-article-body-file t]
1805               ["Pipe through a filter" gnus-summary-pipe-output t]
1806               ["Add to SOUP packet" gnus-soup-add-article t]
1807               ["Print" gnus-summary-print-article t])
1808              ("Backend"
1809               ["Respool article..." gnus-summary-respool-article t]
1810               ["Move article..." gnus-summary-move-article
1811                (gnus-check-backend-function
1812                 'request-move-article gnus-newsgroup-name)]
1813               ["Copy article..." gnus-summary-copy-article t]
1814               ["Crosspost article..." gnus-summary-crosspost-article
1815                (gnus-check-backend-function
1816                 'request-replace-article gnus-newsgroup-name)]
1817               ["Import file..." gnus-summary-import-article t]
1818               ["Check if posted" gnus-summary-article-posted-p t]
1819               ["Edit article" gnus-summary-edit-article
1820                (not (gnus-group-read-only-p))]
1821               ["Delete article" gnus-summary-delete-article
1822                (gnus-check-backend-function
1823                 'request-expire-articles gnus-newsgroup-name)]
1824               ["Query respool" gnus-summary-respool-query t]
1825               ["Trace respool" gnus-summary-respool-trace t]
1826               ["Delete expirable articles" gnus-summary-expire-articles-now
1827                (gnus-check-backend-function
1828                 'request-expire-articles gnus-newsgroup-name)])
1829              ("Extract"
1830               ["Uudecode" gnus-uu-decode-uu
1831                ,@(if (featurep 'xemacs) nil
1832                    '(:help "Decode uuencoded article(s)"))]
1833               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1834               ["Unshar" gnus-uu-decode-unshar t]
1835               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1836               ["Save" gnus-uu-decode-save t]
1837               ["Binhex" gnus-uu-decode-binhex t]
1838               ["Postscript" gnus-uu-decode-postscript t])
1839              ("Cache"
1840               ["Enter article" gnus-cache-enter-article t]
1841               ["Remove article" gnus-cache-remove-article t])
1842              ["Translate" gnus-article-babel t]
1843              ["Select article buffer" gnus-summary-select-article-buffer t]
1844              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1845              ["Isearch article..." gnus-summary-isearch-article t]
1846              ["Beginning of the article" gnus-summary-beginning-of-article t]
1847              ["End of the article" gnus-summary-end-of-article t]
1848              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1849              ["Fetch referenced articles" gnus-summary-refer-references t]
1850              ["Fetch current thread" gnus-summary-refer-thread t]
1851              ["Fetch article with id..." gnus-summary-refer-article t]
1852              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1853              ["Redisplay" gnus-summary-show-article t])))
1854       (easy-menu-define
1855        gnus-summary-article-menu gnus-summary-mode-map ""
1856        (cons "Article" innards))
1857
1858       (easy-menu-define
1859        gnus-article-commands-menu gnus-article-mode-map ""
1860        (cons "Commands" innards)))
1861
1862     (easy-menu-define
1863      gnus-summary-thread-menu gnus-summary-mode-map ""
1864      '("Threads"
1865        ["Toggle threading" gnus-summary-toggle-threads t]
1866        ["Hide threads" gnus-summary-hide-all-threads t]
1867        ["Show threads" gnus-summary-show-all-threads t]
1868        ["Hide thread" gnus-summary-hide-thread t]
1869        ["Show thread" gnus-summary-show-thread t]
1870        ["Go to next thread" gnus-summary-next-thread t]
1871        ["Go to previous thread" gnus-summary-prev-thread t]
1872        ["Go down thread" gnus-summary-down-thread t]
1873        ["Go up thread" gnus-summary-up-thread t]
1874        ["Top of thread" gnus-summary-top-thread t]
1875        ["Mark thread as read" gnus-summary-kill-thread t]
1876        ["Lower thread score" gnus-summary-lower-thread t]
1877        ["Raise thread score" gnus-summary-raise-thread t]
1878        ["Rethread current" gnus-summary-rethread-current t]))
1879
1880     (easy-menu-define
1881      gnus-summary-post-menu gnus-summary-mode-map ""
1882      `("Post"
1883        ["Post an article" gnus-summary-post-news
1884         ,@(if (featurep 'xemacs) nil
1885             '(:help "Post an article"))]
1886        ["Followup" gnus-summary-followup
1887         ,@(if (featurep 'xemacs) nil
1888             '(:help "Post followup to this article"))]
1889        ["Followup and yank" gnus-summary-followup-with-original
1890         ,@(if (featurep 'xemacs) nil
1891             '(:help "Post followup to this article, quoting its contents"))]
1892        ["Supersede article" gnus-summary-supersede-article t]
1893        ["Cancel article" gnus-summary-cancel-article
1894         ,@(if (featurep 'xemacs) nil
1895             '(:help "Cancel an article you posted"))]
1896        ["Reply" gnus-summary-reply t]
1897        ["Reply and yank" gnus-summary-reply-with-original t]
1898        ["Wide reply" gnus-summary-wide-reply t]
1899        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1900         ,@(if (featurep 'xemacs) nil
1901             '(:help "Mail a reply, quoting this article"))]
1902        ["Mail forward" gnus-summary-mail-forward t]
1903        ["Post forward" gnus-summary-post-forward t]
1904        ["Digest and mail" gnus-uu-digest-mail-forward t]
1905        ["Digest and post" gnus-uu-digest-post-forward t]
1906        ["Resend message" gnus-summary-resend-message t]
1907        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1908        ["Send a mail" gnus-summary-mail-other-window t]
1909        ["Uuencode and post" gnus-uu-post-news
1910         ,@(if (featurep 'xemacs) nil
1911             '(:help "Post a uuencoded article"))]
1912        ["Followup via news" gnus-summary-followup-to-mail t]
1913        ["Followup via news and yank"
1914         gnus-summary-followup-to-mail-with-original t]
1915        ;;("Draft"
1916        ;;["Send" gnus-summary-send-draft t]
1917        ;;["Send bounced" gnus-resend-bounced-mail t])
1918        ))
1919
1920     (easy-menu-define
1921      gnus-summary-misc-menu gnus-summary-mode-map ""
1922      `("Misc"
1923        ("Mark Read"
1924         ["Mark as read" gnus-summary-mark-as-read-forward t]
1925         ["Mark same subject and select"
1926          gnus-summary-kill-same-subject-and-select t]
1927         ["Mark same subject" gnus-summary-kill-same-subject t]
1928         ["Catchup" gnus-summary-catchup
1929          ,@(if (featurep 'xemacs) nil
1930              '(:help "Mark unread articles in this group as read"))]
1931         ["Catchup all" gnus-summary-catchup-all t]
1932         ["Catchup to here" gnus-summary-catchup-to-here t]
1933         ["Catchup region" gnus-summary-mark-region-as-read t]
1934         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1935        ("Mark Various"
1936         ["Tick" gnus-summary-tick-article-forward t]
1937         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1938         ["Remove marks" gnus-summary-clear-mark-forward t]
1939         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1940         ["Set bookmark" gnus-summary-set-bookmark t]
1941         ["Remove bookmark" gnus-summary-remove-bookmark t])
1942        ("Mark Limit"
1943         ["Marks..." gnus-summary-limit-to-marks t]
1944         ["Subject..." gnus-summary-limit-to-subject t]
1945         ["Author..." gnus-summary-limit-to-author t]
1946         ["Age..." gnus-summary-limit-to-age t]
1947         ["Extra..." gnus-summary-limit-to-extra t]
1948         ["Score" gnus-summary-limit-to-score t]
1949         ["Unread" gnus-summary-limit-to-unread t]
1950         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1951         ["Articles" gnus-summary-limit-to-articles t]
1952         ["Pop limit" gnus-summary-pop-limit t]
1953         ["Show dormant" gnus-summary-limit-include-dormant t]
1954         ["Hide childless dormant"
1955          gnus-summary-limit-exclude-childless-dormant t]
1956         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1957         ["Hide marked" gnus-summary-limit-exclude-marks t]
1958         ["Show expunged" gnus-summary-show-all-expunged t])
1959        ("Process Mark"
1960         ["Set mark" gnus-summary-mark-as-processable t]
1961         ["Remove mark" gnus-summary-unmark-as-processable t]
1962         ["Remove all marks" gnus-summary-unmark-all-processable t]
1963         ["Mark above" gnus-uu-mark-over t]
1964         ["Mark series" gnus-uu-mark-series t]
1965         ["Mark region" gnus-uu-mark-region t]
1966         ["Unmark region" gnus-uu-unmark-region t]
1967         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1968         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1969         ["Mark all" gnus-uu-mark-all t]
1970         ["Mark buffer" gnus-uu-mark-buffer t]
1971         ["Mark sparse" gnus-uu-mark-sparse t]
1972         ["Mark thread" gnus-uu-mark-thread t]
1973         ["Unmark thread" gnus-uu-unmark-thread t]
1974         ("Process Mark Sets"
1975          ["Kill" gnus-summary-kill-process-mark t]
1976          ["Yank" gnus-summary-yank-process-mark
1977           gnus-newsgroup-process-stack]
1978          ["Save" gnus-summary-save-process-mark t]))
1979        ("Scroll article"
1980         ["Page forward" gnus-summary-next-page
1981          ,@(if (featurep 'xemacs) nil
1982              '(:help "Show next page of article"))]
1983         ["Page backward" gnus-summary-prev-page
1984          ,@(if (featurep 'xemacs) nil
1985              '(:help "Show previous page of article"))]
1986         ["Line forward" gnus-summary-scroll-up t])
1987        ("Move"
1988         ["Next unread article" gnus-summary-next-unread-article t]
1989         ["Previous unread article" gnus-summary-prev-unread-article t]
1990         ["Next article" gnus-summary-next-article t]
1991         ["Previous article" gnus-summary-prev-article t]
1992         ["Next unread subject" gnus-summary-next-unread-subject t]
1993         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1994         ["Next article same subject" gnus-summary-next-same-subject t]
1995         ["Previous article same subject" gnus-summary-prev-same-subject t]
1996         ["First unread article" gnus-summary-first-unread-article t]
1997         ["Best unread article" gnus-summary-best-unread-article t]
1998         ["Go to subject number..." gnus-summary-goto-subject t]
1999         ["Go to article number..." gnus-summary-goto-article t]
2000         ["Go to the last article" gnus-summary-goto-last-article t]
2001         ["Pop article off history" gnus-summary-pop-article t])
2002        ("Sort"
2003         ["Sort by number" gnus-summary-sort-by-number t]
2004         ["Sort by author" gnus-summary-sort-by-author t]
2005         ["Sort by subject" gnus-summary-sort-by-subject t]
2006         ["Sort by date" gnus-summary-sort-by-date t]
2007         ["Sort by score" gnus-summary-sort-by-score t]
2008         ["Sort by lines" gnus-summary-sort-by-lines t]
2009         ["Sort by characters" gnus-summary-sort-by-chars t])
2010        ("Help"
2011         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2012         ["Describe group" gnus-summary-describe-group t]
2013         ["Read manual" gnus-info-find-node t])
2014        ("Modes"
2015         ["Pick and read" gnus-pick-mode t]
2016         ["Binary" gnus-binary-mode t])
2017        ("Regeneration"
2018         ["Regenerate" gnus-summary-prepare t]
2019         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2020         ["Toggle threading" gnus-summary-toggle-threads t])
2021        ["Filter articles..." gnus-summary-execute-command t]
2022        ["Run command on subjects..." gnus-summary-universal-argument t]
2023        ["Search articles forward..." gnus-summary-search-article-forward t]
2024        ["Search articles backward..." gnus-summary-search-article-backward t]
2025        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2026        ["Expand window" gnus-summary-expand-window t]
2027        ["Expire expirable articles" gnus-summary-expire-articles
2028         (gnus-check-backend-function
2029          'request-expire-articles gnus-newsgroup-name)]
2030        ["Edit local kill file" gnus-summary-edit-local-kill t]
2031        ["Edit main kill file" gnus-summary-edit-global-kill t]
2032        ["Edit group parameters" gnus-summary-edit-parameters t]
2033        ["Customize group parameters" gnus-summary-customize-parameters t]
2034        ["Send a bug report" gnus-bug t]
2035        ("Exit"
2036         ["Catchup and exit" gnus-summary-catchup-and-exit
2037          ,@(if (featurep 'xemacs) nil
2038              '(:help "Mark unread articles in this group as read, then exit"))]
2039         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2040         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2041         ["Exit group" gnus-summary-exit
2042          ,@(if (featurep 'xemacs) nil
2043              '(:help "Exit current group, return to group selection mode"))]
2044         ["Exit group without updating" gnus-summary-exit-no-update t]
2045         ["Exit and goto next group" gnus-summary-next-group t]
2046         ["Exit and goto prev group" gnus-summary-prev-group t]
2047         ["Reselect group" gnus-summary-reselect-current-group t]
2048         ["Rescan group" gnus-summary-rescan-group t]
2049         ["Update dribble" gnus-summary-save-newsrc t])))
2050
2051     (gnus-run-hooks 'gnus-summary-menu-hook)))
2052
2053 (defvar gnus-summary-tool-bar-map nil)
2054
2055 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2056 (defun gnus-summary-make-tool-bar ()
2057   (if (and (fboundp 'tool-bar-add-item-from-menu)
2058            (default-value 'tool-bar-mode)
2059            (not gnus-summary-tool-bar-map))
2060       (setq gnus-summary-tool-bar-map
2061             (let ((tool-bar-map (make-sparse-keymap))
2062                   (load-path (mm-image-load-path)))
2063               (tool-bar-add-item-from-menu
2064                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2065               (tool-bar-add-item-from-menu
2066                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2067               (tool-bar-add-item-from-menu
2068                'gnus-summary-post-news "post" gnus-summary-mode-map)
2069               (tool-bar-add-item-from-menu
2070                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2071               (tool-bar-add-item-from-menu
2072                'gnus-summary-followup "followup" gnus-summary-mode-map)
2073               (tool-bar-add-item-from-menu
2074                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2075               (tool-bar-add-item-from-menu
2076                'gnus-summary-reply "reply" gnus-summary-mode-map)
2077               (tool-bar-add-item-from-menu
2078                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2079               (tool-bar-add-item-from-menu
2080                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2081               (tool-bar-add-item-from-menu
2082                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2083               (tool-bar-add-item-from-menu
2084                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2085               (tool-bar-add-item-from-menu
2086                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2087               (tool-bar-add-item-from-menu
2088                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2089               (tool-bar-add-item-from-menu
2090                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2091               (tool-bar-add-item-from-menu
2092                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2093               tool-bar-map)))
2094   (if gnus-summary-tool-bar-map
2095       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2096
2097 (defun gnus-score-set-default (var value)
2098   "A version of set that updates the GNU Emacs menu-bar."
2099   (set var value)
2100   ;; It is the message that forces the active status to be updated.
2101   (message ""))
2102
2103 (defun gnus-make-score-map (type)
2104   "Make a summary score map of type TYPE."
2105   (if t
2106       nil
2107     (let ((headers '(("author" "from" string)
2108                      ("subject" "subject" string)
2109                      ("article body" "body" string)
2110                      ("article head" "head" string)
2111                      ("xref" "xref" string)
2112                      ("extra header" "extra" string)
2113                      ("lines" "lines" number)
2114                      ("followups to author" "followup" string)))
2115           (types '((number ("less than" <)
2116                            ("greater than" >)
2117                            ("equal" =))
2118                    (string ("substring" s)
2119                            ("exact string" e)
2120                            ("fuzzy string" f)
2121                            ("regexp" r))))
2122           (perms '(("temporary" (current-time-string))
2123                    ("permanent" nil)
2124                    ("immediate" now)))
2125           header)
2126       (list
2127        (apply
2128         'nconc
2129         (list
2130          (if (eq type 'lower)
2131              "Lower score"
2132            "Increase score"))
2133         (let (outh)
2134           (while headers
2135             (setq header (car headers))
2136             (setq outh
2137                   (cons
2138                    (apply
2139                     'nconc
2140                     (list (car header))
2141                     (let ((ts (cdr (assoc (nth 2 header) types)))
2142                           outt)
2143                       (while ts
2144                         (setq outt
2145                               (cons
2146                                (apply
2147                                 'nconc
2148                                 (list (caar ts))
2149                                 (let ((ps perms)
2150                                       outp)
2151                                   (while ps
2152                                     (setq outp
2153                                           (cons
2154                                            (vector
2155                                             (caar ps)
2156                                             (list
2157                                              'gnus-summary-score-entry
2158                                              (nth 1 header)
2159                                              (if (or (string= (nth 1 header)
2160                                                               "head")
2161                                                      (string= (nth 1 header)
2162                                                               "body"))
2163                                                  ""
2164                                                (list 'gnus-summary-header
2165                                                      (nth 1 header)))
2166                                              (list 'quote (nth 1 (car ts)))
2167                                              (list 'gnus-score-delta-default
2168                                                    nil)
2169                                              (nth 1 (car ps))
2170                                              t)
2171                                             t)
2172                                            outp))
2173                                     (setq ps (cdr ps)))
2174                                   (list (nreverse outp))))
2175                                outt))
2176                         (setq ts (cdr ts)))
2177                       (list (nreverse outt))))
2178                    outh))
2179             (setq headers (cdr headers)))
2180           (list (nreverse outh))))))))
2181
2182 \f
2183
2184 (defun gnus-summary-mode (&optional group)
2185   "Major mode for reading articles.
2186
2187 All normal editing commands are switched off.
2188 \\<gnus-summary-mode-map>
2189 Each line in this buffer represents one article.  To read an
2190 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2191 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2192 respectively.
2193
2194 You can also post articles and send mail from this buffer.  To
2195 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2196 of an article, type `\\[gnus-summary-reply]'.
2197
2198 There are approx. one gazillion commands you can execute in this
2199 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2200
2201 The following commands are available:
2202
2203 \\{gnus-summary-mode-map}"
2204   (interactive)
2205   (kill-all-local-variables)
2206   (when (gnus-visual-p 'summary-menu 'menu)
2207     (gnus-summary-make-menu-bar)
2208     (gnus-summary-make-tool-bar))
2209   (gnus-summary-make-local-variables)
2210   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2211     (gnus-summary-make-local-variables))
2212   (gnus-make-thread-indent-array)
2213   (gnus-simplify-mode-line)
2214   (setq major-mode 'gnus-summary-mode)
2215   (setq mode-name "Summary")
2216   (make-local-variable 'minor-mode-alist)
2217   (use-local-map gnus-summary-mode-map)
2218   (buffer-disable-undo)
2219   (setq buffer-read-only t)             ;Disable modification
2220   (setq truncate-lines t)
2221   (setq selective-display t)
2222   (setq selective-display-ellipses t)   ;Display `...'
2223   (gnus-summary-set-display-table)
2224   (gnus-set-default-directory)
2225   (setq gnus-newsgroup-name group)
2226   (make-local-variable 'gnus-summary-line-format)
2227   (make-local-variable 'gnus-summary-line-format-spec)
2228   (make-local-variable 'gnus-summary-dummy-line-format)
2229   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2230   (make-local-variable 'gnus-summary-mark-positions)
2231   (make-local-hook 'pre-command-hook)
2232   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2233   (gnus-run-hooks 'gnus-summary-mode-hook)
2234   (turn-on-gnus-mailing-list-mode)
2235   (mm-enable-multibyte-mule4)
2236   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2237   (gnus-update-summary-mark-positions))
2238
2239 (defun gnus-summary-make-local-variables ()
2240   "Make all the local summary buffer variables."
2241   (let (global)
2242     (dolist (local gnus-summary-local-variables)
2243       (if (consp local)
2244           (progn
2245             (if (eq (cdr local) 'global)
2246                 ;; Copy the global value of the variable.
2247                 (setq global (symbol-value (car local)))
2248               ;; Use the value from the list.
2249               (setq global (eval (cdr local))))
2250             (set (make-local-variable (car local)) global))
2251         ;; Simple nil-valued local variable.
2252         (set (make-local-variable local) nil)))))
2253
2254 (defun gnus-summary-clear-local-variables ()
2255   (let ((locals gnus-summary-local-variables))
2256     (while locals
2257       (if (consp (car locals))
2258           (and (vectorp (caar locals))
2259                (set (caar locals) nil))
2260         (and (vectorp (car locals))
2261              (set (car locals) nil)))
2262       (setq locals (cdr locals)))))
2263
2264 ;; Summary data functions.
2265
2266 (defmacro gnus-data-number (data)
2267   `(car ,data))
2268
2269 (defmacro gnus-data-set-number (data number)
2270   `(setcar ,data ,number))
2271
2272 (defmacro gnus-data-mark (data)
2273   `(nth 1 ,data))
2274
2275 (defmacro gnus-data-set-mark (data mark)
2276   `(setcar (nthcdr 1 ,data) ,mark))
2277
2278 (defmacro gnus-data-pos (data)
2279   `(nth 2 ,data))
2280
2281 (defmacro gnus-data-set-pos (data pos)
2282   `(setcar (nthcdr 2 ,data) ,pos))
2283
2284 (defmacro gnus-data-header (data)
2285   `(nth 3 ,data))
2286
2287 (defmacro gnus-data-set-header (data header)
2288   `(setf (nth 3 ,data) ,header))
2289
2290 (defmacro gnus-data-level (data)
2291   `(nth 4 ,data))
2292
2293 (defmacro gnus-data-unread-p (data)
2294   `(= (nth 1 ,data) gnus-unread-mark))
2295
2296 (defmacro gnus-data-read-p (data)
2297   `(/= (nth 1 ,data) gnus-unread-mark))
2298
2299 (defmacro gnus-data-pseudo-p (data)
2300   `(consp (nth 3 ,data)))
2301
2302 (defmacro gnus-data-find (number)
2303   `(assq ,number gnus-newsgroup-data))
2304
2305 (defmacro gnus-data-find-list (number &optional data)
2306   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2307      (memq (assq ,number bdata)
2308            bdata)))
2309
2310 (defmacro gnus-data-make (number mark pos header level)
2311   `(list ,number ,mark ,pos ,header ,level))
2312
2313 (defun gnus-data-enter (after-article number mark pos header level offset)
2314   (let ((data (gnus-data-find-list after-article)))
2315     (unless data
2316       (error "No such article: %d" after-article))
2317     (setcdr data (cons (gnus-data-make number mark pos header level)
2318                        (cdr data)))
2319     (setq gnus-newsgroup-data-reverse nil)
2320     (gnus-data-update-list (cddr data) offset)))
2321
2322 (defun gnus-data-enter-list (after-article list &optional offset)
2323   (when list
2324     (let ((data (and after-article (gnus-data-find-list after-article)))
2325           (ilist list))
2326       (if (not (or data
2327                    after-article))
2328           (let ((odata gnus-newsgroup-data))
2329             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2330             (when offset
2331               (gnus-data-update-list odata offset)))
2332         ;; Find the last element in the list to be spliced into the main
2333         ;; list.
2334         (while (cdr list)
2335           (setq list (cdr list)))
2336         (if (not data)
2337             (progn
2338               (setcdr list gnus-newsgroup-data)
2339               (setq gnus-newsgroup-data ilist)
2340               (when offset
2341                 (gnus-data-update-list (cdr list) offset)))
2342           (setcdr list (cdr data))
2343           (setcdr data ilist)
2344           (when offset
2345             (gnus-data-update-list (cdr list) offset))))
2346       (setq gnus-newsgroup-data-reverse nil))))
2347
2348 (defun gnus-data-remove (article &optional offset)
2349   (let ((data gnus-newsgroup-data))
2350     (if (= (gnus-data-number (car data)) article)
2351         (progn
2352           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2353                 gnus-newsgroup-data-reverse nil)
2354           (when offset
2355             (gnus-data-update-list gnus-newsgroup-data offset)))
2356       (while (cdr data)
2357         (when (= (gnus-data-number (cadr data)) article)
2358           (setcdr data (cddr data))
2359           (when offset
2360             (gnus-data-update-list (cdr data) offset))
2361           (setq data nil
2362                 gnus-newsgroup-data-reverse nil))
2363         (setq data (cdr data))))))
2364
2365 (defmacro gnus-data-list (backward)
2366   `(if ,backward
2367        (or gnus-newsgroup-data-reverse
2368            (setq gnus-newsgroup-data-reverse
2369                  (reverse gnus-newsgroup-data)))
2370      gnus-newsgroup-data))
2371
2372 (defun gnus-data-update-list (data offset)
2373   "Add OFFSET to the POS of all data entries in DATA."
2374   (setq gnus-newsgroup-data-reverse nil)
2375   (while data
2376     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2377     (setq data (cdr data))))
2378
2379 (defun gnus-summary-article-pseudo-p (article)
2380   "Say whether this article is a pseudo article or not."
2381   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2382
2383 (defmacro gnus-summary-article-sparse-p (article)
2384   "Say whether this article is a sparse article or not."
2385   `(memq ,article gnus-newsgroup-sparse))
2386
2387 (defmacro gnus-summary-article-ancient-p (article)
2388   "Say whether this article is a sparse article or not."
2389   `(memq ,article gnus-newsgroup-ancient))
2390
2391 (defun gnus-article-parent-p (number)
2392   "Say whether this article is a parent or not."
2393   (let ((data (gnus-data-find-list number)))
2394     (and (cdr data)                     ; There has to be an article after...
2395          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2396             (gnus-data-level (nth 1 data))))))
2397
2398 (defun gnus-article-children (number)
2399   "Return a list of all children to NUMBER."
2400   (let* ((data (gnus-data-find-list number))
2401          (level (gnus-data-level (car data)))
2402          children)
2403     (setq data (cdr data))
2404     (while (and data
2405                 (= (gnus-data-level (car data)) (1+ level)))
2406       (push (gnus-data-number (car data)) children)
2407       (setq data (cdr data)))
2408     children))
2409
2410 (defmacro gnus-summary-skip-intangible ()
2411   "If the current article is intangible, then jump to a different article."
2412   '(let ((to (get-text-property (point) 'gnus-intangible)))
2413      (and to (gnus-summary-goto-subject to))))
2414
2415 (defmacro gnus-summary-article-intangible-p ()
2416   "Say whether this article is intangible or not."
2417   '(get-text-property (point) 'gnus-intangible))
2418
2419 (defun gnus-article-read-p (article)
2420   "Say whether ARTICLE is read or not."
2421   (not (or (memq article gnus-newsgroup-marked)
2422            (memq article gnus-newsgroup-unreads)
2423            (memq article gnus-newsgroup-unselected)
2424            (memq article gnus-newsgroup-dormant))))
2425
2426 ;; Some summary mode macros.
2427
2428 (defmacro gnus-summary-article-number ()
2429   "The article number of the article on the current line.
2430 If there isn's an article number here, then we return the current
2431 article number."
2432   '(progn
2433      (gnus-summary-skip-intangible)
2434      (or (get-text-property (point) 'gnus-number)
2435          (gnus-summary-last-subject))))
2436
2437 (defmacro gnus-summary-article-header (&optional number)
2438   "Return the header of article NUMBER."
2439   `(gnus-data-header (gnus-data-find
2440                       ,(or number '(gnus-summary-article-number)))))
2441
2442 (defmacro gnus-summary-thread-level (&optional number)
2443   "Return the level of thread that starts with article NUMBER."
2444   `(if (and (eq gnus-summary-make-false-root 'dummy)
2445             (get-text-property (point) 'gnus-intangible))
2446        0
2447      (gnus-data-level (gnus-data-find
2448                        ,(or number '(gnus-summary-article-number))))))
2449
2450 (defmacro gnus-summary-article-mark (&optional number)
2451   "Return the mark of article NUMBER."
2452   `(gnus-data-mark (gnus-data-find
2453                     ,(or number '(gnus-summary-article-number)))))
2454
2455 (defmacro gnus-summary-article-pos (&optional number)
2456   "Return the position of the line of article NUMBER."
2457   `(gnus-data-pos (gnus-data-find
2458                    ,(or number '(gnus-summary-article-number)))))
2459
2460 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2461 (defmacro gnus-summary-article-subject (&optional number)
2462   "Return current subject string or nil if nothing."
2463   `(let ((headers
2464           ,(if number
2465                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2466              '(gnus-data-header (assq (gnus-summary-article-number)
2467                                       gnus-newsgroup-data)))))
2468      (and headers
2469           (vectorp headers)
2470           (mail-header-subject headers))))
2471
2472 (defmacro gnus-summary-article-score (&optional number)
2473   "Return current article score."
2474   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2475                   gnus-newsgroup-scored))
2476        gnus-summary-default-score 0))
2477
2478 (defun gnus-summary-article-children (&optional number)
2479   "Return a list of article numbers that are children of article NUMBER."
2480   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2481          (level (gnus-data-level (car data)))
2482          l children)
2483     (while (and (setq data (cdr data))
2484                 (> (setq l (gnus-data-level (car data))) level))
2485       (and (= (1+ level) l)
2486            (push (gnus-data-number (car data))
2487                  children)))
2488     (nreverse children)))
2489
2490 (defun gnus-summary-article-parent (&optional number)
2491   "Return the article number of the parent of article NUMBER."
2492   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2493                                     (gnus-data-list t)))
2494          (level (gnus-data-level (car data))))
2495     (if (zerop level)
2496         ()                              ; This is a root.
2497       ;; We search until we find an article with a level less than
2498       ;; this one.  That function has to be the parent.
2499       (while (and (setq data (cdr data))
2500                   (not (< (gnus-data-level (car data)) level))))
2501       (and data (gnus-data-number (car data))))))
2502
2503 (defun gnus-unread-mark-p (mark)
2504   "Say whether MARK is the unread mark."
2505   (= mark gnus-unread-mark))
2506
2507 (defun gnus-read-mark-p (mark)
2508   "Say whether MARK is one of the marks that mark as read.
2509 This is all marks except unread, ticked, dormant, and expirable."
2510   (not (or (= mark gnus-unread-mark)
2511            (= mark gnus-ticked-mark)
2512            (= mark gnus-dormant-mark)
2513            (= mark gnus-expirable-mark))))
2514
2515 (defmacro gnus-article-mark (number)
2516   "Return the MARK of article NUMBER.
2517 This macro should only be used when computing the mark the \"first\"
2518 time; i.e., when generating the summary lines.  After that,
2519 `gnus-summary-article-mark' should be used to examine the
2520 marks of articles."
2521   `(cond
2522     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2523     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2524     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2525     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2526     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2527     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2528     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2529     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2530            gnus-ancient-mark))))
2531
2532 ;; Saving hidden threads.
2533
2534 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2535 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2536
2537 (defmacro gnus-save-hidden-threads (&rest forms)
2538   "Save hidden threads, eval FORMS, and restore the hidden threads."
2539   (let ((config (make-symbol "config")))
2540     `(let ((,config (gnus-hidden-threads-configuration)))
2541        (unwind-protect
2542            (save-excursion
2543              ,@forms)
2544          (gnus-restore-hidden-threads-configuration ,config)))))
2545
2546 (defun gnus-data-compute-positions ()
2547   "Compute the positions of all articles."
2548   (setq gnus-newsgroup-data-reverse nil)
2549   (let ((data gnus-newsgroup-data))
2550     (save-excursion
2551       (gnus-save-hidden-threads
2552         (gnus-summary-show-all-threads)
2553         (goto-char (point-min))
2554         (while data
2555           (while (get-text-property (point) 'gnus-intangible)
2556             (forward-line 1))
2557           (gnus-data-set-pos (car data) (+ (point) 3))
2558           (setq data (cdr data))
2559           (forward-line 1))))))
2560
2561 (defun gnus-hidden-threads-configuration ()
2562   "Return the current hidden threads configuration."
2563   (save-excursion
2564     (let (config)
2565       (goto-char (point-min))
2566       (while (search-forward "\r" nil t)
2567         (push (1- (point)) config))
2568       config)))
2569
2570 (defun gnus-restore-hidden-threads-configuration (config)
2571   "Restore hidden threads configuration from CONFIG."
2572   (save-excursion
2573     (let (point buffer-read-only)
2574       (while (setq point (pop config))
2575         (when (and (< point (point-max))
2576                    (goto-char point)
2577                    (eq (char-after) ?\n))
2578           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2579
2580 ;; Various summary mode internalish functions.
2581
2582 (defun gnus-mouse-pick-article (e)
2583   (interactive "e")
2584   (mouse-set-point e)
2585   (gnus-summary-next-page nil t))
2586
2587 (defun gnus-summary-set-display-table ()
2588   "Change the display table.
2589 Odd characters have a tendency to mess
2590 up nicely formatted displays - we make all possible glyphs
2591 display only a single character."
2592
2593   ;; We start from the standard display table, if any.
2594   (let ((table (or (copy-sequence standard-display-table)
2595                    (make-display-table)))
2596         (i 32))
2597     ;; Nix out all the control chars...
2598     (while (>= (setq i (1- i)) 0)
2599       (aset table i [??]))
2600     ;; ... but not newline and cr, of course.  (cr is necessary for the
2601     ;; selective display).
2602     (aset table ?\n nil)
2603     (aset table ?\r nil)
2604     ;; We keep TAB as well.
2605     (aset table ?\t nil)
2606     ;; We nix out any glyphs over 126 that are not set already.
2607     (let ((i 256))
2608       (while (>= (setq i (1- i)) 127)
2609         ;; Only modify if the entry is nil.
2610         (unless (aref table i)
2611           (aset table i [??]))))
2612     (setq buffer-display-table table)))
2613
2614 (defun gnus-summary-setup-buffer (group)
2615   "Initialize summary buffer."
2616   (let ((buffer (concat "*Summary " group "*")))
2617     (if (get-buffer buffer)
2618         (progn
2619           (set-buffer buffer)
2620           (setq gnus-summary-buffer (current-buffer))
2621           (not gnus-newsgroup-prepared))
2622       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2623       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2624       (gnus-summary-mode group)
2625       (when gnus-carpal
2626         (gnus-carpal-setup-buffer 'summary))
2627       (unless gnus-single-article-buffer
2628         (make-local-variable 'gnus-article-buffer)
2629         (make-local-variable 'gnus-article-current)
2630         (make-local-variable 'gnus-original-article-buffer))
2631       (setq gnus-newsgroup-name group)
2632       t)))
2633
2634 (defun gnus-set-global-variables ()
2635   "Set the global equivalents of the buffer-local variables.
2636 They are set to the latest values they had.  These reflect the summary
2637 buffer that was in action when the last article was fetched."
2638   (when (eq major-mode 'gnus-summary-mode)
2639     (setq gnus-summary-buffer (current-buffer))
2640     (let ((name gnus-newsgroup-name)
2641           (marked gnus-newsgroup-marked)
2642           (unread gnus-newsgroup-unreads)
2643           (headers gnus-current-headers)
2644           (data gnus-newsgroup-data)
2645           (summary gnus-summary-buffer)
2646           (article-buffer gnus-article-buffer)
2647           (original gnus-original-article-buffer)
2648           (gac gnus-article-current)
2649           (reffed gnus-reffed-article-number)
2650           (score-file gnus-current-score-file)
2651           (default-charset gnus-newsgroup-charset)
2652           vlist)
2653       (let ((locals gnus-newsgroup-variables))
2654         (while locals
2655           (if (consp (car locals))
2656               (push (eval (caar locals)) vlist)
2657             (push (eval (car locals)) vlist))
2658           (setq locals (cdr locals)))
2659         (setq vlist (nreverse vlist)))
2660       (save-excursion
2661         (set-buffer gnus-group-buffer)
2662         (setq gnus-newsgroup-name name
2663               gnus-newsgroup-marked marked
2664               gnus-newsgroup-unreads unread
2665               gnus-current-headers headers
2666               gnus-newsgroup-data data
2667               gnus-article-current gac
2668               gnus-summary-buffer summary
2669               gnus-article-buffer article-buffer
2670               gnus-original-article-buffer original
2671               gnus-reffed-article-number reffed
2672               gnus-current-score-file score-file
2673               gnus-newsgroup-charset default-charset)
2674         (let ((locals gnus-newsgroup-variables))
2675           (while locals
2676             (if (consp (car locals))
2677                 (set (caar locals) (pop vlist))
2678               (set (car locals) (pop vlist)))
2679             (setq locals (cdr locals))))
2680         ;; The article buffer also has local variables.
2681         (when (gnus-buffer-live-p gnus-article-buffer)
2682           (set-buffer gnus-article-buffer)
2683           (setq gnus-summary-buffer summary))))))
2684
2685 (defun gnus-summary-article-unread-p (article)
2686   "Say whether ARTICLE is unread or not."
2687   (memq article gnus-newsgroup-unreads))
2688
2689 (defun gnus-summary-first-article-p (&optional article)
2690   "Return whether ARTICLE is the first article in the buffer."
2691   (if (not (setq article (or article (gnus-summary-article-number))))
2692       nil
2693     (eq article (caar gnus-newsgroup-data))))
2694
2695 (defun gnus-summary-last-article-p (&optional article)
2696   "Return whether ARTICLE is the last article in the buffer."
2697   (if (not (setq article (or article (gnus-summary-article-number))))
2698       ;; All non-existent numbers are the last article.  :-)
2699       t
2700     (not (cdr (gnus-data-find-list article)))))
2701
2702 (defun gnus-make-thread-indent-array ()
2703   (let ((n 200))
2704     (unless (and gnus-thread-indent-array
2705                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2706       (setq gnus-thread-indent-array (make-vector 201 "")
2707             gnus-thread-indent-array-level gnus-thread-indent-level)
2708       (while (>= n 0)
2709         (aset gnus-thread-indent-array n
2710               (make-string (* n gnus-thread-indent-level) ? ))
2711         (setq n (1- n))))))
2712
2713 (defun gnus-update-summary-mark-positions ()
2714   "Compute where the summary marks are to go."
2715   (save-excursion
2716     (when (gnus-buffer-exists-p gnus-summary-buffer)
2717       (set-buffer gnus-summary-buffer))
2718     (let ((gnus-replied-mark 129)
2719           (gnus-score-below-mark 130)
2720           (gnus-score-over-mark 130)
2721           (gnus-download-mark 131)
2722           (spec gnus-summary-line-format-spec)
2723           gnus-visual pos)
2724       (save-excursion
2725         (gnus-set-work-buffer)
2726         (let ((gnus-summary-line-format-spec spec)
2727               (gnus-newsgroup-downloadable '((0 . t))))
2728           (gnus-summary-insert-line
2729            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2730           (goto-char (point-min))
2731           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2732                                              (- (point) 2)))))
2733           (goto-char (point-min))
2734           (push (cons 'replied (and (search-forward "\201" nil t)
2735                                     (- (point) 2)))
2736                 pos)
2737           (goto-char (point-min))
2738           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2739                 pos)
2740           (goto-char (point-min))
2741           (push (cons 'download
2742                       (and (search-forward "\203" nil t) (- (point) 2)))
2743                 pos)))
2744       (setq gnus-summary-mark-positions pos))))
2745
2746 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2747   "Insert a dummy root in the summary buffer."
2748   (beginning-of-line)
2749   (gnus-add-text-properties
2750    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2751    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2752
2753 (defun gnus-summary-from-or-to-or-newsgroups (header)
2754   (let ((to (cdr (assq 'To (mail-header-extra header))))
2755         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2756         (mail-parse-charset gnus-newsgroup-charset)
2757         (mail-parse-ignored-charsets
2758          (save-excursion (set-buffer gnus-summary-buffer)
2759                          gnus-newsgroup-ignored-charsets)))
2760     (cond
2761      ((and to
2762            gnus-ignored-from-addresses
2763            (string-match gnus-ignored-from-addresses
2764                          (mail-header-from header)))
2765       (concat "-> "
2766               (or (car (funcall gnus-extract-address-components
2767                                 (funcall
2768                                  gnus-decode-encoded-word-function to)))
2769                   (funcall gnus-decode-encoded-word-function to))))
2770      ((and newsgroups
2771            gnus-ignored-from-addresses
2772            (string-match gnus-ignored-from-addresses
2773                          (mail-header-from header)))
2774       (concat "=> " newsgroups))
2775      (t
2776       (or (car (funcall gnus-extract-address-components
2777                         (mail-header-from header)))
2778           (mail-header-from header))))))
2779
2780 (defun gnus-summary-insert-line (gnus-tmp-header
2781                                  gnus-tmp-level gnus-tmp-current
2782                                  gnus-tmp-unread gnus-tmp-replied
2783                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2784                                  &optional gnus-tmp-dummy gnus-tmp-score
2785                                  gnus-tmp-process)
2786   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2787          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2788          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2789          (gnus-tmp-score-char
2790           (if (or (null gnus-summary-default-score)
2791                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2792                       gnus-summary-zcore-fuzz))
2793               ?  ;Whitespace
2794             (if (< gnus-tmp-score gnus-summary-default-score)
2795                 gnus-score-below-mark gnus-score-over-mark)))
2796          (gnus-tmp-replied
2797           (cond (gnus-tmp-process gnus-process-mark)
2798                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2799                  gnus-cached-mark)
2800                 (gnus-tmp-replied gnus-replied-mark)
2801                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2802                  gnus-saved-mark)
2803                 (t gnus-no-mark)))
2804          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2805          (gnus-tmp-name
2806           (cond
2807            ((string-match "<[^>]+> *$" gnus-tmp-from)
2808             (let ((beg (match-beginning 0)))
2809               (or (and (string-match "^\".+\"" gnus-tmp-from)
2810                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2811                   (substring gnus-tmp-from 0 beg))))
2812            ((string-match "(.+)" gnus-tmp-from)
2813             (substring gnus-tmp-from
2814                        (1+ (match-beginning 0)) (1- (match-end 0))))
2815            (t gnus-tmp-from)))
2816          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2817          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2818          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2819          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2820          (buffer-read-only nil))
2821     (when (string= gnus-tmp-name "")
2822       (setq gnus-tmp-name gnus-tmp-from))
2823     (unless (numberp gnus-tmp-lines)
2824       (setq gnus-tmp-lines 0))
2825     (gnus-put-text-property
2826      (point)
2827      (progn (eval gnus-summary-line-format-spec) (point))
2828      'gnus-number gnus-tmp-number)
2829     (when (gnus-visual-p 'summary-highlight 'highlight)
2830       (forward-line -1)
2831       (gnus-run-hooks 'gnus-summary-update-hook)
2832       (forward-line 1))))
2833
2834 (defun gnus-summary-update-line (&optional dont-update)
2835   "Update summary line after change."
2836   (when (and gnus-summary-default-score
2837              (not gnus-summary-inhibit-highlight))
2838     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2839            (article (gnus-summary-article-number))
2840            (score (gnus-summary-article-score article)))
2841       (unless dont-update
2842         (if (and gnus-summary-mark-below
2843                  (< (gnus-summary-article-score)
2844                     gnus-summary-mark-below))
2845             ;; This article has a low score, so we mark it as read.
2846             (when (memq article gnus-newsgroup-unreads)
2847               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2848           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2849             ;; This article was previously marked as read on account
2850             ;; of a low score, but now it has risen, so we mark it as
2851             ;; unread.
2852             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2853         (gnus-summary-update-mark
2854          (if (or (null gnus-summary-default-score)
2855                  (<= (abs (- score gnus-summary-default-score))
2856                      gnus-summary-zcore-fuzz))
2857              ?  ;Whitespace
2858            (if (< score gnus-summary-default-score)
2859                gnus-score-below-mark gnus-score-over-mark))
2860          'score))
2861       ;; Do visual highlighting.
2862       (when (gnus-visual-p 'summary-highlight 'highlight)
2863         (gnus-run-hooks 'gnus-summary-update-hook)))))
2864
2865 (defvar gnus-tmp-new-adopts nil)
2866
2867 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2868   "Return the number of articles in THREAD.
2869 This may be 0 in some cases -- if none of the articles in
2870 the thread are to be displayed."
2871   (let* ((number
2872           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2873           (cond
2874            ((not (listp thread))
2875             1)
2876            ((and (consp thread) (cdr thread))
2877             (apply
2878              '+ 1 (mapcar
2879                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2880            ((null thread)
2881             1)
2882            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2883             1)
2884            (t 0))))
2885     (when (and level (zerop level) gnus-tmp-new-adopts)
2886       (incf number
2887             (apply '+ (mapcar
2888                        'gnus-summary-number-of-articles-in-thread
2889                        gnus-tmp-new-adopts))))
2890     (if char
2891         (if (> number 1) gnus-not-empty-thread-mark
2892           gnus-empty-thread-mark)
2893       number)))
2894
2895 (defun gnus-summary-set-local-parameters (group)
2896   "Go through the local params of GROUP and set all variable specs in that list."
2897   (let ((params (gnus-group-find-parameter group))
2898         elem)
2899     (while params
2900       (setq elem (car params)
2901             params (cdr params))
2902       (and (consp elem)                 ; Has to be a cons.
2903            (consp (cdr elem))           ; The cdr has to be a list.
2904            (symbolp (car elem))         ; Has to be a symbol in there.
2905            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2906            (ignore-errors               ; So we set it.
2907              (make-local-variable (car elem))
2908              (set (car elem) (eval (nth 1 elem))))))))
2909
2910 (defun gnus-summary-read-group (group &optional show-all no-article
2911                                       kill-buffer no-display backward
2912                                       select-articles)
2913   "Start reading news in newsgroup GROUP.
2914 If SHOW-ALL is non-nil, already read articles are also listed.
2915 If NO-ARTICLE is non-nil, no article is selected initially.
2916 If NO-DISPLAY, don't generate a summary buffer."
2917   (let (result)
2918     (while (and group
2919                 (null (setq result
2920                             (let ((gnus-auto-select-next nil))
2921                               (or (gnus-summary-read-group-1
2922                                    group show-all no-article
2923                                    kill-buffer no-display
2924                                    select-articles)
2925                                   (setq show-all nil
2926                                         select-articles nil)))))
2927                 (eq gnus-auto-select-next 'quietly))
2928       (set-buffer gnus-group-buffer)
2929       ;; The entry function called above goes to the next
2930       ;; group automatically, so we go two groups back
2931       ;; if we are searching for the previous group.
2932       (when backward
2933         (gnus-group-prev-unread-group 2))
2934       (if (not (equal group (gnus-group-group-name)))
2935           (setq group (gnus-group-group-name))
2936         (setq group nil)))
2937     result))
2938
2939 (defun gnus-summary-read-group-1 (group show-all no-article
2940                                         kill-buffer no-display
2941                                         &optional select-articles)
2942   ;; Killed foreign groups can't be entered.
2943   (when (and (not (gnus-group-native-p group))
2944              (not (gnus-gethash group gnus-newsrc-hashtb)))
2945     (error "Dead non-native groups can't be entered"))
2946   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2947   (let* ((new-group (gnus-summary-setup-buffer group))
2948          (quit-config (gnus-group-quit-config group))
2949          (did-select (and new-group (gnus-select-newsgroup
2950                                      group show-all select-articles))))
2951     (cond
2952      ;; This summary buffer exists already, so we just select it.
2953      ((not new-group)
2954       (gnus-set-global-variables)
2955       (when kill-buffer
2956         (gnus-kill-or-deaden-summary kill-buffer))
2957       (gnus-configure-windows 'summary 'force)
2958       (gnus-set-mode-line 'summary)
2959       (gnus-summary-position-point)
2960       (message "")
2961       t)
2962      ;; We couldn't select this group.
2963      ((null did-select)
2964       (when (and (eq major-mode 'gnus-summary-mode)
2965                  (not (equal (current-buffer) kill-buffer)))
2966         (kill-buffer (current-buffer))
2967         (if (not quit-config)
2968             (progn
2969               ;; Update the info -- marks might need to be removed,
2970               ;; for instance.
2971               (gnus-summary-update-info)
2972               (set-buffer gnus-group-buffer)
2973               (gnus-group-jump-to-group group)
2974               (gnus-group-next-unread-group 1))
2975           (gnus-handle-ephemeral-exit quit-config)))
2976       (gnus-message 3 "Can't select group")
2977       nil)
2978      ;; The user did a `C-g' while prompting for number of articles,
2979      ;; so we exit this group.
2980      ((eq did-select 'quit)
2981       (and (eq major-mode 'gnus-summary-mode)
2982            (not (equal (current-buffer) kill-buffer))
2983            (kill-buffer (current-buffer)))
2984       (when kill-buffer
2985         (gnus-kill-or-deaden-summary kill-buffer))
2986       (if (not quit-config)
2987           (progn
2988             (set-buffer gnus-group-buffer)
2989             (gnus-group-jump-to-group group)
2990             (gnus-group-next-unread-group 1)
2991             (gnus-configure-windows 'group 'force))
2992         (gnus-handle-ephemeral-exit quit-config))
2993       ;; Finally signal the quit.
2994       (signal 'quit nil))
2995      ;; The group was successfully selected.
2996      (t
2997       (gnus-set-global-variables)
2998       ;; Save the active value in effect when the group was entered.
2999       (setq gnus-newsgroup-active
3000             (gnus-copy-sequence
3001              (gnus-active gnus-newsgroup-name)))
3002       ;; You can change the summary buffer in some way with this hook.
3003       (gnus-run-hooks 'gnus-select-group-hook)
3004       ;; Set any local variables in the group parameters.
3005       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3006       (gnus-update-format-specifications
3007        nil 'summary 'summary-mode 'summary-dummy)
3008       (gnus-update-summary-mark-positions)
3009       ;; Do score processing.
3010       (when gnus-use-scoring
3011         (gnus-possibly-score-headers))
3012       ;; Check whether to fill in the gaps in the threads.
3013       (when gnus-build-sparse-threads
3014         (gnus-build-sparse-threads))
3015       ;; Find the initial limit.
3016       (if gnus-show-threads
3017           (if show-all
3018               (let ((gnus-newsgroup-dormant nil))
3019                 (gnus-summary-initial-limit show-all))
3020             (gnus-summary-initial-limit show-all))
3021         ;; When untreaded, all articles are always shown.
3022         (setq gnus-newsgroup-limit
3023               (mapcar
3024                (lambda (header) (mail-header-number header))
3025                gnus-newsgroup-headers)))
3026       ;; Generate the summary buffer.
3027       (unless no-display
3028         (gnus-summary-prepare))
3029       (when gnus-use-trees
3030         (gnus-tree-open group)
3031         (setq gnus-summary-highlight-line-function
3032               'gnus-tree-highlight-article))
3033       ;; If the summary buffer is empty, but there are some low-scored
3034       ;; articles or some excluded dormants, we include these in the
3035       ;; buffer.
3036       (when (and (zerop (buffer-size))
3037                  (not no-display))
3038         (cond (gnus-newsgroup-dormant
3039                (gnus-summary-limit-include-dormant))
3040               ((and gnus-newsgroup-scored show-all)
3041                (gnus-summary-limit-include-expunged t))))
3042       ;; Function `gnus-apply-kill-file' must be called in this hook.
3043       (gnus-run-hooks 'gnus-apply-kill-hook)
3044       (if (and (zerop (buffer-size))
3045                (not no-display))
3046           (progn
3047             ;; This newsgroup is empty.
3048             (gnus-summary-catchup-and-exit nil t)
3049             (gnus-message 6 "No unread news")
3050             (when kill-buffer
3051               (gnus-kill-or-deaden-summary kill-buffer))
3052             ;; Return nil from this function.
3053             nil)
3054         ;; Hide conversation thread subtrees.  We cannot do this in
3055         ;; gnus-summary-prepare-hook since kill processing may not
3056         ;; work with hidden articles.
3057         (and gnus-show-threads
3058              gnus-thread-hide-subtree
3059              (gnus-summary-hide-all-threads))
3060         (when kill-buffer
3061           (gnus-kill-or-deaden-summary kill-buffer))
3062         ;; Show first unread article if requested.
3063         (if (and (not no-article)
3064                  (not no-display)
3065                  gnus-newsgroup-unreads
3066                  gnus-auto-select-first)
3067             (progn
3068               (gnus-configure-windows 'summary)
3069               (cond
3070                ((eq gnus-auto-select-first 'best)
3071                 (gnus-summary-best-unread-article))
3072                ((eq gnus-auto-select-first t)
3073                 (gnus-summary-first-unread-article))
3074                ((gnus-functionp gnus-auto-select-first)
3075                 (funcall gnus-auto-select-first))))
3076           ;; Don't select any articles, just move point to the first
3077           ;; article in the group.
3078           (goto-char (point-min))
3079           (gnus-summary-position-point)
3080           (gnus-configure-windows 'summary 'force)
3081           (gnus-set-mode-line 'summary))
3082         (when (get-buffer-window gnus-group-buffer t)
3083           ;; Gotta use windows, because recenter does weird stuff if
3084           ;; the current buffer ain't the displayed window.
3085           (let ((owin (selected-window)))
3086             (select-window (get-buffer-window gnus-group-buffer t))
3087             (when (gnus-group-goto-group group)
3088               (recenter))
3089             (select-window owin)))
3090         ;; Mark this buffer as "prepared".
3091         (setq gnus-newsgroup-prepared t)
3092         (gnus-run-hooks 'gnus-summary-prepared-hook)
3093         t)))))
3094
3095 (defun gnus-summary-prepare ()
3096   "Generate the summary buffer."
3097   (interactive)
3098   (let ((buffer-read-only nil))
3099     (erase-buffer)
3100     (setq gnus-newsgroup-data nil
3101           gnus-newsgroup-data-reverse nil)
3102     (gnus-run-hooks 'gnus-summary-generate-hook)
3103     ;; Generate the buffer, either with threads or without.
3104     (when gnus-newsgroup-headers
3105       (gnus-summary-prepare-threads
3106        (if gnus-show-threads
3107            (gnus-sort-gathered-threads
3108             (funcall gnus-summary-thread-gathering-function
3109                      (gnus-sort-threads
3110                       (gnus-cut-threads (gnus-make-threads)))))
3111          ;; Unthreaded display.
3112          (gnus-sort-articles gnus-newsgroup-headers))))
3113     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3114     ;; Call hooks for modifying summary buffer.
3115     (goto-char (point-min))
3116     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3117
3118 (defsubst gnus-general-simplify-subject (subject)
3119   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3120   (setq subject
3121         (cond
3122          ;; Truncate the subject.
3123          (gnus-simplify-subject-functions
3124           (gnus-map-function gnus-simplify-subject-functions subject))
3125          ((numberp gnus-summary-gather-subject-limit)
3126           (setq subject (gnus-simplify-subject-re subject))
3127           (if (> (length subject) gnus-summary-gather-subject-limit)
3128               (substring subject 0 gnus-summary-gather-subject-limit)
3129             subject))
3130          ;; Fuzzily simplify it.
3131          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3132           (gnus-simplify-subject-fuzzy subject))
3133          ;; Just remove the leading "Re:".
3134          (t
3135           (gnus-simplify-subject-re subject))))
3136
3137   (if (and gnus-summary-gather-exclude-subject
3138            (string-match gnus-summary-gather-exclude-subject subject))
3139       nil                               ; This article shouldn't be gathered
3140     subject))
3141
3142 (defun gnus-summary-simplify-subject-query ()
3143   "Query where the respool algorithm would put this article."
3144   (interactive)
3145   (gnus-summary-select-article)
3146   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3147
3148 (defun gnus-gather-threads-by-subject (threads)
3149   "Gather threads by looking at Subject headers."
3150   (if (not gnus-summary-make-false-root)
3151       threads
3152     (let ((hashtb (gnus-make-hashtable 1024))
3153           (prev threads)
3154           (result threads)
3155           subject hthread whole-subject)
3156       (while threads
3157         (setq subject (gnus-general-simplify-subject
3158                        (setq whole-subject (mail-header-subject
3159                                             (caar threads)))))
3160         (when subject
3161           (if (setq hthread (gnus-gethash subject hashtb))
3162               (progn
3163                 ;; We enter a dummy root into the thread, if we
3164                 ;; haven't done that already.
3165                 (unless (stringp (caar hthread))
3166                   (setcar hthread (list whole-subject (car hthread))))
3167                 ;; We add this new gathered thread to this gathered
3168                 ;; thread.
3169                 (setcdr (car hthread)
3170                         (nconc (cdar hthread) (list (car threads))))
3171                 ;; Remove it from the list of threads.
3172                 (setcdr prev (cdr threads))
3173                 (setq threads prev))
3174             ;; Enter this thread into the hash table.
3175             (gnus-sethash subject threads hashtb)))
3176         (setq prev threads)
3177         (setq threads (cdr threads)))
3178       result)))
3179
3180 (defun gnus-gather-threads-by-references (threads)
3181   "Gather threads by looking at References headers."
3182   (let ((idhashtb (gnus-make-hashtable 1024))
3183         (thhashtb (gnus-make-hashtable 1024))
3184         (prev threads)
3185         (result threads)
3186         ids references id gthread gid entered ref)
3187     (while threads
3188       (when (setq references (mail-header-references (caar threads)))
3189         (setq id (mail-header-id (caar threads))
3190               ids (gnus-split-references references)
3191               entered nil)
3192         (while (setq ref (pop ids))
3193           (setq ids (delete ref ids))
3194           (if (not (setq gid (gnus-gethash ref idhashtb)))
3195               (progn
3196                 (gnus-sethash ref id idhashtb)
3197                 (gnus-sethash id threads thhashtb))
3198             (setq gthread (gnus-gethash gid thhashtb))
3199             (unless entered
3200               ;; We enter a dummy root into the thread, if we
3201               ;; haven't done that already.
3202               (unless (stringp (caar gthread))
3203                 (setcar gthread (list (mail-header-subject (caar gthread))
3204                                       (car gthread))))
3205               ;; We add this new gathered thread to this gathered
3206               ;; thread.
3207               (setcdr (car gthread)
3208                       (nconc (cdar gthread) (list (car threads)))))
3209             ;; Add it into the thread hash table.
3210             (gnus-sethash id gthread thhashtb)
3211             (setq entered t)
3212             ;; Remove it from the list of threads.
3213             (setcdr prev (cdr threads))
3214             (setq threads prev))))
3215       (setq prev threads)
3216       (setq threads (cdr threads)))
3217     result))
3218
3219 (defun gnus-sort-gathered-threads (threads)
3220   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3221   (let ((result threads))
3222     (while threads
3223       (when (stringp (caar threads))
3224         (setcdr (car threads)
3225                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3226       (setq threads (cdr threads)))
3227     result))
3228
3229 (defun gnus-thread-loop-p (root thread)
3230   "Say whether ROOT is in THREAD."
3231   (let ((stack (list thread))
3232         (infloop 0)
3233         th)
3234     (while (setq thread (pop stack))
3235       (setq th (cdr thread))
3236       (while (and th
3237                   (not (eq (caar th) root)))
3238         (pop th))
3239       (if th
3240           ;; We have found a loop.
3241           (let (ref-dep)
3242             (setcdr thread (delq (car th) (cdr thread)))
3243             (if (boundp (setq ref-dep (intern "none"
3244                                               gnus-newsgroup-dependencies)))
3245                 (setcdr (symbol-value ref-dep)
3246                         (nconc (cdr (symbol-value ref-dep))
3247                                (list (car th))))
3248               (set ref-dep (list nil (car th))))
3249             (setq infloop 1
3250                   stack nil))
3251         ;; Push all the subthreads onto the stack.
3252         (push (cdr thread) stack)))
3253     infloop))
3254
3255 (defun gnus-make-threads ()
3256   "Go through the dependency hashtb and find the roots.  Return all threads."
3257   (let (threads)
3258     (while (catch 'infloop
3259              (mapatoms
3260               (lambda (refs)
3261                 ;; Deal with self-referencing References loops.
3262                 (when (and (car (symbol-value refs))
3263                            (not (zerop
3264                                  (apply
3265                                   '+
3266                                   (mapcar
3267                                    (lambda (thread)
3268                                      (gnus-thread-loop-p
3269                                       (car (symbol-value refs)) thread))
3270                                    (cdr (symbol-value refs)))))))
3271                   (setq threads nil)
3272                   (throw 'infloop t))
3273                 (unless (car (symbol-value refs))
3274                   ;; These threads do not refer back to any other articles,
3275                   ;; so they're roots.
3276                   (setq threads (append (cdr (symbol-value refs)) threads))))
3277               gnus-newsgroup-dependencies)))
3278     threads))
3279
3280 ;; Build the thread tree.
3281 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3282   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3283
3284 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3285 if it was already present.
3286
3287 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3288 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3289 Message-IDs will be renamed be renamed to a unique Message-ID before
3290 being entered.
3291
3292 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3293   (let* ((id (mail-header-id header))
3294          (id-dep (and id (intern id dependencies)))
3295          ref ref-dep ref-header)
3296     ;; Enter this `header' in the `dependencies' table.
3297     (cond
3298      ((not id-dep)
3299       (setq header nil))
3300      ;; The first two cases do the normal part: enter a new `header'
3301      ;; in the `dependencies' table.
3302      ((not (boundp id-dep))
3303       (set id-dep (list header)))
3304      ((null (car (symbol-value id-dep)))
3305       (setcar (symbol-value id-dep) header))
3306
3307      ;; From here the `header' was already present in the
3308      ;; `dependencies' table.
3309      (force-new
3310       ;; Overrides an existing entry;
3311       ;; just set the header part of the entry.
3312       (setcar (symbol-value id-dep) header))
3313
3314      ;; Renames the existing `header' to a unique Message-ID.
3315      ((not gnus-summary-ignore-duplicates)
3316       ;; An article with this Message-ID has already been seen.
3317       ;; We rename the Message-ID.
3318       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3319            (list header))
3320       (mail-header-set-id header id))
3321
3322      ;; The last case ignores an existing entry, except it adds any
3323      ;; additional Xrefs (in case the two articles came from different
3324      ;; servers.
3325      ;; Also sets `header' to `nil' meaning that the `dependencies'
3326      ;; table was *not* modified.
3327      (t
3328       (mail-header-set-xref
3329        (car (symbol-value id-dep))
3330        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3331                    "")
3332                (or (mail-header-xref header) "")))
3333       (setq header nil)))
3334
3335     (when header
3336       ;; First check if that we are not creating a References loop.
3337       (setq ref (gnus-parent-id (mail-header-references header)))
3338       (while (and ref
3339                   (setq ref-dep (intern-soft ref dependencies))
3340                   (boundp ref-dep)
3341                   (setq ref-header (car (symbol-value ref-dep))))
3342         (if (string= id ref)
3343             ;; Yuk!  This is a reference loop.  Make the article be a
3344             ;; root article.
3345             (progn
3346               (mail-header-set-references (car (symbol-value id-dep)) "none")
3347               (setq ref nil))
3348           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3349       (setq ref (gnus-parent-id (mail-header-references header)))
3350       (setq ref-dep (intern (or ref "none") dependencies))
3351       (if (boundp ref-dep)
3352           (setcdr (symbol-value ref-dep)
3353                   (nconc (cdr (symbol-value ref-dep))
3354                          (list (symbol-value id-dep))))
3355         (set ref-dep (list nil (symbol-value id-dep)))))
3356     header))
3357
3358 (defun gnus-build-sparse-threads ()
3359   (let ((headers gnus-newsgroup-headers)
3360         (mail-parse-charset gnus-newsgroup-charset)
3361         (gnus-summary-ignore-duplicates t)
3362         header references generation relations
3363         subject child end new-child date)
3364     ;; First we create an alist of generations/relations, where
3365     ;; generations is how much we trust the relation, and the relation
3366     ;; is parent/child.
3367     (gnus-message 7 "Making sparse threads...")
3368     (save-excursion
3369       (nnheader-set-temp-buffer " *gnus sparse threads*")
3370       (while (setq header (pop headers))
3371         (when (and (setq references (mail-header-references header))
3372                    (not (string= references "")))
3373           (insert references)
3374           (setq child (mail-header-id header)
3375                 subject (mail-header-subject header)
3376                 date (mail-header-date header)
3377                 generation 0)
3378           (while (search-backward ">" nil t)
3379             (setq end (1+ (point)))
3380             (when (search-backward "<" nil t)
3381               (setq new-child (buffer-substring (point) end))
3382               (push (list (incf generation)
3383                           child (setq child new-child)
3384                           subject date)
3385                     relations)))
3386           (when child
3387             (push (list (1+ generation) child nil subject) relations))
3388           (erase-buffer)))
3389       (kill-buffer (current-buffer)))
3390     ;; Sort over trustworthiness.
3391     (mapcar
3392      (lambda (relation)
3393        (when (gnus-dependencies-add-header
3394               (make-full-mail-header
3395                gnus-reffed-article-number
3396                (nth 3 relation) "" (or (nth 4 relation) "")
3397                (nth 1 relation)
3398                (or (nth 2 relation) "") 0 0 "")
3399               gnus-newsgroup-dependencies nil)
3400          (push gnus-reffed-article-number gnus-newsgroup-limit)
3401          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3402          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3403                gnus-newsgroup-reads)
3404          (decf gnus-reffed-article-number)))
3405      (sort relations 'car-less-than-car))
3406     (gnus-message 7 "Making sparse threads...done")))
3407
3408 (defun gnus-build-old-threads ()
3409   ;; Look at all the articles that refer back to old articles, and
3410   ;; fetch the headers for the articles that aren't there.  This will
3411   ;; build complete threads - if the roots haven't been expired by the
3412   ;; server, that is.
3413   (let ((mail-parse-charset gnus-newsgroup-charset)
3414         id heads)
3415     (mapatoms
3416      (lambda (refs)
3417        (when (not (car (symbol-value refs)))
3418          (setq heads (cdr (symbol-value refs)))
3419          (while heads
3420            (if (memq (mail-header-number (caar heads))
3421                      gnus-newsgroup-dormant)
3422                (setq heads (cdr heads))
3423              (setq id (symbol-name refs))
3424              (while (and (setq id (gnus-build-get-header id))
3425                          (not (car (gnus-id-to-thread id)))))
3426              (setq heads nil)))))
3427      gnus-newsgroup-dependencies)))
3428
3429 ;; This function has to be called with point after the article number
3430 ;; on the beginning of the line.
3431 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3432   (let ((eol (gnus-point-at-eol))
3433         (buffer (current-buffer))
3434         header)
3435
3436     ;; overview: [num subject from date id refs chars lines misc]
3437     (unwind-protect
3438         (progn
3439           (narrow-to-region (point) eol)
3440           (unless (eobp)
3441             (forward-char))
3442
3443           (setq header
3444                 (make-full-mail-header
3445                  number                 ; number
3446                  (funcall gnus-decode-encoded-word-function
3447                           (nnheader-nov-field)) ; subject
3448                  (funcall gnus-decode-encoded-word-function
3449                           (nnheader-nov-field)) ; from
3450                  (nnheader-nov-field)   ; date
3451                  (nnheader-nov-read-message-id) ; id
3452                  (nnheader-nov-field)   ; refs
3453                  (nnheader-nov-read-integer) ; chars
3454                  (nnheader-nov-read-integer) ; lines
3455                  (unless (eobp)
3456                    (if (looking-at "Xref: ")
3457                        (goto-char (match-end 0)))
3458                    (nnheader-nov-field)) ; Xref
3459                  (nnheader-nov-parse-extra)))) ; extra
3460
3461       (widen))
3462
3463     (when gnus-alter-header-function
3464       (funcall gnus-alter-header-function header))
3465     (gnus-dependencies-add-header header dependencies force-new)))
3466
3467 (defun gnus-build-get-header (id)
3468   "Look through the buffer of NOV lines and find the header to ID.
3469 Enter this line into the dependencies hash table, and return
3470 the id of the parent article (if any)."
3471   (let ((deps gnus-newsgroup-dependencies)
3472         found header)
3473     (prog1
3474         (save-excursion
3475           (set-buffer nntp-server-buffer)
3476           (let ((case-fold-search nil))
3477             (goto-char (point-min))
3478             (while (and (not found)
3479                         (search-forward id nil t))
3480               (beginning-of-line)
3481               (setq found (looking-at
3482                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3483                                    (regexp-quote id))))
3484               (or found (beginning-of-line 2)))
3485             (when found
3486               (beginning-of-line)
3487               (and
3488                (setq header (gnus-nov-parse-line
3489                              (read (current-buffer)) deps))
3490                (gnus-parent-id (mail-header-references header))))))
3491       (when header
3492         (let ((number (mail-header-number header)))
3493           (push number gnus-newsgroup-limit)
3494           (push header gnus-newsgroup-headers)
3495           (if (memq number gnus-newsgroup-unselected)
3496               (progn
3497                 (push number gnus-newsgroup-unreads)
3498                 (setq gnus-newsgroup-unselected
3499                       (delq number gnus-newsgroup-unselected)))
3500             (push number gnus-newsgroup-ancient)))))))
3501
3502 (defun gnus-build-all-threads ()
3503   "Read all the headers."
3504   (let ((gnus-summary-ignore-duplicates t)
3505         (mail-parse-charset gnus-newsgroup-charset)
3506         (dependencies gnus-newsgroup-dependencies)
3507         header article)
3508     (save-excursion
3509       (set-buffer nntp-server-buffer)
3510       (let ((case-fold-search nil))
3511         (goto-char (point-min))
3512         (while (not (eobp))
3513           (ignore-errors
3514             (setq article (read (current-buffer))
3515                   header (gnus-nov-parse-line article dependencies)))
3516           (when header
3517             (save-excursion
3518               (set-buffer gnus-summary-buffer)
3519               (push header gnus-newsgroup-headers)
3520               (if (memq (setq article (mail-header-number header))
3521                         gnus-newsgroup-unselected)
3522                   (progn
3523                     (push article gnus-newsgroup-unreads)
3524                     (setq gnus-newsgroup-unselected
3525                           (delq article gnus-newsgroup-unselected)))
3526                 (push article gnus-newsgroup-ancient)))
3527             (forward-line 1)))))))
3528
3529 (defun gnus-summary-update-article-line (article header)
3530   "Update the line for ARTICLE using HEADERS."
3531   (let* ((id (mail-header-id header))
3532          (thread (gnus-id-to-thread id)))
3533     (unless thread
3534       (error "Article in no thread"))
3535     ;; Update the thread.
3536     (setcar thread header)
3537     (gnus-summary-goto-subject article)
3538     (let* ((datal (gnus-data-find-list article))
3539            (data (car datal))
3540            (length (when (cdr datal)
3541                      (- (gnus-data-pos data)
3542                         (gnus-data-pos (cadr datal)))))
3543            (buffer-read-only nil)
3544            (level (gnus-summary-thread-level)))
3545       (gnus-delete-line)
3546       (gnus-summary-insert-line
3547        header level nil (gnus-article-mark article)
3548        (memq article gnus-newsgroup-replied)
3549        (memq article gnus-newsgroup-expirable)
3550        ;; Only insert the Subject string when it's different
3551        ;; from the previous Subject string.
3552        (if (and
3553             gnus-show-threads
3554             (gnus-subject-equal
3555              (condition-case ()
3556                  (mail-header-subject
3557                   (gnus-data-header
3558                    (cadr
3559                     (gnus-data-find-list
3560                      article
3561                      (gnus-data-list t)))))
3562                ;; Error on the side of excessive subjects.
3563                (error ""))
3564              (mail-header-subject header)))
3565            ""
3566          (mail-header-subject header))
3567        nil (cdr (assq article gnus-newsgroup-scored))
3568        (memq article gnus-newsgroup-processable))
3569       (when length
3570         (gnus-data-update-list
3571          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3572
3573 (defun gnus-summary-update-article (article &optional iheader)
3574   "Update ARTICLE in the summary buffer."
3575   (set-buffer gnus-summary-buffer)
3576   (let* ((header (gnus-summary-article-header article))
3577          (id (mail-header-id header))
3578          (data (gnus-data-find article))
3579          (thread (gnus-id-to-thread id))
3580          (references (mail-header-references header))
3581          (parent
3582           (gnus-id-to-thread
3583            (or (gnus-parent-id
3584                 (when (and references
3585                            (not (equal "" references)))
3586                   references))
3587                "none")))
3588          (buffer-read-only nil)
3589          (old (car thread)))
3590     (when thread
3591       (unless iheader
3592         (setcar thread nil)
3593         (when parent
3594           (delq thread parent)))
3595       (if (gnus-summary-insert-subject id header)
3596           ;; Set the (possibly) new article number in the data structure.
3597           (gnus-data-set-number data (gnus-id-to-article id))
3598         (setcar thread old)
3599         nil))))
3600
3601 (defun gnus-rebuild-thread (id &optional line)
3602   "Rebuild the thread containing ID.
3603 If LINE, insert the rebuilt thread starting on line LINE."
3604   (let ((buffer-read-only nil)
3605         old-pos current thread data)
3606     (if (not gnus-show-threads)
3607         (setq thread (list (car (gnus-id-to-thread id))))
3608       ;; Get the thread this article is part of.
3609       (setq thread (gnus-remove-thread id)))
3610     (setq old-pos (gnus-point-at-bol))
3611     (setq current (save-excursion
3612                     (and (zerop (forward-line -1))
3613                          (gnus-summary-article-number))))
3614     ;; If this is a gathered thread, we have to go some re-gathering.
3615     (when (stringp (car thread))
3616       (let ((subject (car thread))
3617             roots thr)
3618         (setq thread (cdr thread))
3619         (while thread
3620           (unless (memq (setq thr (gnus-id-to-thread
3621                                    (gnus-root-id
3622                                     (mail-header-id (caar thread)))))
3623                         roots)
3624             (push thr roots))
3625           (setq thread (cdr thread)))
3626         ;; We now have all (unique) roots.
3627         (if (= (length roots) 1)
3628             ;; All the loose roots are now one solid root.
3629             (setq thread (car roots))
3630           (setq thread (cons subject (gnus-sort-threads roots))))))
3631     (let (threads)
3632       ;; We then insert this thread into the summary buffer.
3633       (when line
3634         (goto-char (point-min))
3635         (forward-line (1- line)))
3636       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3637         (if gnus-show-threads
3638             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3639           (gnus-summary-prepare-unthreaded thread))
3640         (setq data (nreverse gnus-newsgroup-data))
3641         (setq threads gnus-newsgroup-threads))
3642       ;; We splice the new data into the data structure.
3643       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3644       ;;!!! then we want to insert at the beginning of the buffer.
3645       ;;!!! That happens to be true with Gnus now, but that may
3646       ;;!!! change in the future.  Perhaps.
3647       (gnus-data-enter-list
3648        (if line nil current) data (- (point) old-pos))
3649       (setq gnus-newsgroup-threads
3650             (nconc threads gnus-newsgroup-threads))
3651       (gnus-data-compute-positions))))
3652
3653 (defun gnus-number-to-header (number)
3654   "Return the header for article NUMBER."
3655   (let ((headers gnus-newsgroup-headers))
3656     (while (and headers
3657                 (not (= number (mail-header-number (car headers)))))
3658       (pop headers))
3659     (when headers
3660       (car headers))))
3661
3662 (defun gnus-parent-headers (in-headers &optional generation)
3663   "Return the headers of the GENERATIONeth parent of HEADERS."
3664   (unless generation
3665     (setq generation 1))
3666   (let ((parent t)
3667         (headers in-headers)
3668         references)
3669     (while (and parent
3670                 (not (zerop generation))
3671                 (setq references (mail-header-references headers)))
3672       (setq headers (if (and references
3673                              (setq parent (gnus-parent-id references)))
3674                         (car (gnus-id-to-thread parent))
3675                       nil))
3676       (decf generation))
3677     (and (not (eq headers in-headers))
3678          headers)))
3679
3680 (defun gnus-id-to-thread (id)
3681   "Return the (sub-)thread where ID appears."
3682   (gnus-gethash id gnus-newsgroup-dependencies))
3683
3684 (defun gnus-id-to-article (id)
3685   "Return the article number of ID."
3686   (let ((thread (gnus-id-to-thread id)))
3687     (when (and thread
3688                (car thread))
3689       (mail-header-number (car thread)))))
3690
3691 (defun gnus-id-to-header (id)
3692   "Return the article headers of ID."
3693   (car (gnus-id-to-thread id)))
3694
3695 (defun gnus-article-displayed-root-p (article)
3696   "Say whether ARTICLE is a root(ish) article."
3697   (let ((level (gnus-summary-thread-level article))
3698         (refs (mail-header-references  (gnus-summary-article-header article)))
3699         particle)
3700     (cond
3701      ((null level) nil)
3702      ((zerop level) t)
3703      ((null refs) t)
3704      ((null (gnus-parent-id refs)) t)
3705      ((and (= 1 level)
3706            (null (setq particle (gnus-id-to-article
3707                                  (gnus-parent-id refs))))
3708            (null (gnus-summary-thread-level particle)))))))
3709
3710 (defun gnus-root-id (id)
3711   "Return the id of the root of the thread where ID appears."
3712   (let (last-id prev)
3713     (while (and id (setq prev (car (gnus-id-to-thread id))))
3714       (setq last-id id
3715             id (gnus-parent-id (mail-header-references prev))))
3716     last-id))
3717
3718 (defun gnus-articles-in-thread (thread)
3719   "Return the list of articles in THREAD."
3720   (cons (mail-header-number (car thread))
3721         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3722
3723 (defun gnus-remove-thread (id &optional dont-remove)
3724   "Remove the thread that has ID in it."
3725   (let (headers thread last-id)
3726     ;; First go up in this thread until we find the root.
3727     (setq last-id (gnus-root-id id)
3728           headers (message-flatten-list (gnus-id-to-thread last-id)))
3729     ;; We have now found the real root of this thread.  It might have
3730     ;; been gathered into some loose thread, so we have to search
3731     ;; through the threads to find the thread we wanted.
3732     (let ((threads gnus-newsgroup-threads)
3733           sub)
3734       (while threads
3735         (setq sub (car threads))
3736         (if (stringp (car sub))
3737             ;; This is a gathered thread, so we look at the roots
3738             ;; below it to find whether this article is in this
3739             ;; gathered root.
3740             (progn
3741               (setq sub (cdr sub))
3742               (while sub
3743                 (when (member (caar sub) headers)
3744                   (setq thread (car threads)
3745                         threads nil
3746                         sub nil))
3747                 (setq sub (cdr sub))))
3748           ;; It's an ordinary thread, so we check it.
3749           (when (eq (car sub) (car headers))
3750             (setq thread sub
3751                   threads nil)))
3752         (setq threads (cdr threads)))
3753       ;; If this article is in no thread, then it's a root.
3754       (if thread
3755           (unless dont-remove
3756             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3757         (setq thread (gnus-id-to-thread last-id)))
3758       (when thread
3759         (prog1
3760             thread                      ; We return this thread.
3761           (unless dont-remove
3762             (if (stringp (car thread))
3763                 (progn
3764                   ;; If we use dummy roots, then we have to remove the
3765                   ;; dummy root as well.
3766                   (when (eq gnus-summary-make-false-root 'dummy)
3767                     ;; We go to the dummy root by going to
3768                     ;; the first sub-"thread", and then one line up.
3769                     (gnus-summary-goto-article
3770                      (mail-header-number (caadr thread)))
3771                     (forward-line -1)
3772                     (gnus-delete-line)
3773                     (gnus-data-compute-positions))
3774                   (setq thread (cdr thread))
3775                   (while thread
3776                     (gnus-remove-thread-1 (car thread))
3777                     (setq thread (cdr thread))))
3778               (gnus-remove-thread-1 thread))))))))
3779
3780 (defun gnus-remove-thread-1 (thread)
3781   "Remove the thread THREAD recursively."
3782   (let ((number (mail-header-number (pop thread)))
3783         d)
3784     (setq thread (reverse thread))
3785     (while thread
3786       (gnus-remove-thread-1 (pop thread)))
3787     (when (setq d (gnus-data-find number))
3788       (goto-char (gnus-data-pos d))
3789       (gnus-summary-show-thread)
3790       (gnus-data-remove
3791        number
3792        (- (gnus-point-at-bol)
3793           (prog1
3794               (1+ (gnus-point-at-eol))
3795             (gnus-delete-line)))))))
3796
3797 (defun gnus-sort-threads-1 (threads func)
3798   (sort (mapcar (lambda (thread)
3799                   (cons (car thread)
3800                         (and (cdr thread)
3801                              (gnus-sort-threads-1 (cdr thread) func))))
3802                 threads) func))
3803
3804 (defun gnus-sort-threads (threads)
3805   "Sort THREADS."
3806   (if (not gnus-thread-sort-functions)
3807       threads
3808     (gnus-message 8 "Sorting threads...")
3809     (prog1
3810         (gnus-sort-threads-1 
3811          threads 
3812          (gnus-make-sort-function gnus-thread-sort-functions))
3813       (gnus-message 8 "Sorting threads...done"))))
3814
3815 (defun gnus-sort-articles (articles)
3816   "Sort ARTICLES."
3817   (when gnus-article-sort-functions
3818     (gnus-message 7 "Sorting articles...")
3819     (prog1
3820         (setq gnus-newsgroup-headers
3821               (sort articles (gnus-make-sort-function
3822                               gnus-article-sort-functions)))
3823       (gnus-message 7 "Sorting articles...done"))))
3824
3825 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3826 (defmacro gnus-thread-header (thread)
3827   "Return header of first article in THREAD.
3828 Note that THREAD must never, ever be anything else than a variable -
3829 using some other form will lead to serious barfage."
3830   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3831   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3832   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3833         (vector thread) 2))
3834
3835 (defsubst gnus-article-sort-by-number (h1 h2)
3836   "Sort articles by article number."
3837   (< (mail-header-number h1)
3838      (mail-header-number h2)))
3839
3840 (defun gnus-thread-sort-by-number (h1 h2)
3841   "Sort threads by root article number."
3842   (gnus-article-sort-by-number
3843    (gnus-thread-header h1) (gnus-thread-header h2)))
3844
3845 (defsubst gnus-article-sort-by-lines (h1 h2)
3846   "Sort articles by article Lines header."
3847   (< (mail-header-lines h1)
3848      (mail-header-lines h2)))
3849
3850 (defun gnus-thread-sort-by-lines (h1 h2)
3851   "Sort threads by root article Lines header."
3852   (gnus-article-sort-by-lines
3853    (gnus-thread-header h1) (gnus-thread-header h2)))
3854
3855 (defsubst gnus-article-sort-by-chars (h1 h2)
3856   "Sort articles by octet length."
3857   (< (mail-header-chars h1)
3858      (mail-header-chars h2)))
3859
3860 (defun gnus-thread-sort-by-chars (h1 h2)
3861   "Sort threads by root article octet length."
3862   (gnus-article-sort-by-chars
3863    (gnus-thread-header h1) (gnus-thread-header h2)))
3864
3865 (defsubst gnus-article-sort-by-author (h1 h2)
3866   "Sort articles by root author."
3867   (string-lessp
3868    (let ((extract (funcall
3869                    gnus-extract-address-components
3870                    (mail-header-from h1))))
3871      (or (car extract) (cadr extract) ""))
3872    (let ((extract (funcall
3873                    gnus-extract-address-components
3874                    (mail-header-from h2))))
3875      (or (car extract) (cadr extract) ""))))
3876
3877 (defun gnus-thread-sort-by-author (h1 h2)
3878   "Sort threads by root author."
3879   (gnus-article-sort-by-author
3880    (gnus-thread-header h1)  (gnus-thread-header h2)))
3881
3882 (defsubst gnus-article-sort-by-subject (h1 h2)
3883   "Sort articles by root subject."
3884   (string-lessp
3885    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3886    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3887
3888 (defun gnus-thread-sort-by-subject (h1 h2)
3889   "Sort threads by root subject."
3890   (gnus-article-sort-by-subject
3891    (gnus-thread-header h1) (gnus-thread-header h2)))
3892
3893 (defsubst gnus-article-sort-by-date (h1 h2)
3894   "Sort articles by root article date."
3895   (time-less-p
3896    (gnus-date-get-time (mail-header-date h1))
3897    (gnus-date-get-time (mail-header-date h2))))
3898
3899 (defun gnus-thread-sort-by-date (h1 h2)
3900   "Sort threads by root article date."
3901   (gnus-article-sort-by-date
3902    (gnus-thread-header h1) (gnus-thread-header h2)))
3903
3904 (defsubst gnus-article-sort-by-score (h1 h2)
3905   "Sort articles by root article score.
3906 Unscored articles will be counted as having a score of zero."
3907   (> (or (cdr (assq (mail-header-number h1)
3908                     gnus-newsgroup-scored))
3909          gnus-summary-default-score 0)
3910      (or (cdr (assq (mail-header-number h2)
3911                     gnus-newsgroup-scored))
3912          gnus-summary-default-score 0)))
3913
3914 (defun gnus-thread-sort-by-score (h1 h2)
3915   "Sort threads by root article score."
3916   (gnus-article-sort-by-score
3917    (gnus-thread-header h1) (gnus-thread-header h2)))
3918
3919 (defun gnus-thread-sort-by-total-score (h1 h2)
3920   "Sort threads by the sum of all scores in the thread.
3921 Unscored articles will be counted as having a score of zero."
3922   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3923
3924 (defun gnus-thread-total-score (thread)
3925   ;; This function find the total score of THREAD.
3926   (cond ((null thread)
3927          0)
3928         ((consp thread)
3929          (if (stringp (car thread))
3930              (apply gnus-thread-score-function 0
3931                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3932            (gnus-thread-total-score-1 thread)))
3933         (t
3934          (gnus-thread-total-score-1 (list thread)))))
3935
3936 (defun gnus-thread-total-score-1 (root)
3937   ;; This function find the total score of the thread below ROOT.
3938   (setq root (car root))
3939   (apply gnus-thread-score-function
3940          (or (append
3941               (mapcar 'gnus-thread-total-score
3942                       (cdr (gnus-id-to-thread (mail-header-id root))))
3943               (when (> (mail-header-number root) 0)
3944                 (list (or (cdr (assq (mail-header-number root)
3945                                      gnus-newsgroup-scored))
3946                           gnus-summary-default-score 0))))
3947              (list gnus-summary-default-score)
3948              '(0))))
3949
3950 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3951 (defvar gnus-tmp-prev-subject nil)
3952 (defvar gnus-tmp-false-parent nil)
3953 (defvar gnus-tmp-root-expunged nil)
3954 (defvar gnus-tmp-dummy-line nil)
3955
3956 (eval-when-compile (defvar gnus-tmp-header))
3957 (defun gnus-extra-header (type &optional header)
3958   "Return the extra header of TYPE."
3959   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3960       ""))
3961
3962 (defun gnus-summary-prepare-threads (threads)
3963   "Prepare summary buffer from THREADS and indentation LEVEL.
3964 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3965 or a straight list of headers."
3966   (gnus-message 7 "Generating summary...")
3967
3968   (setq gnus-newsgroup-threads threads)
3969   (beginning-of-line)
3970
3971   (let ((gnus-tmp-level 0)
3972         (default-score (or gnus-summary-default-score 0))
3973         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3974         thread number subject stack state gnus-tmp-gathered beg-match
3975         new-roots gnus-tmp-new-adopts thread-end
3976         gnus-tmp-header gnus-tmp-unread
3977         gnus-tmp-replied gnus-tmp-subject-or-nil
3978         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3979         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3980         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3981
3982     (setq gnus-tmp-prev-subject nil)
3983
3984     (if (vectorp (car threads))
3985         ;; If this is a straight (sic) list of headers, then a
3986         ;; threaded summary display isn't required, so we just create
3987         ;; an unthreaded one.
3988         (gnus-summary-prepare-unthreaded threads)
3989
3990       ;; Do the threaded display.
3991
3992       (while (or threads stack gnus-tmp-new-adopts new-roots)
3993
3994         (if (and (= gnus-tmp-level 0)
3995                  (or (not stack)
3996                      (= (caar stack) 0))
3997                  (not gnus-tmp-false-parent)
3998                  (or gnus-tmp-new-adopts new-roots))
3999             (if gnus-tmp-new-adopts
4000                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4001                       thread (list (car gnus-tmp-new-adopts))
4002                       gnus-tmp-header (caar thread)
4003                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4004               (when new-roots
4005                 (setq thread (list (car new-roots))
4006                       gnus-tmp-header (caar thread)
4007                       new-roots (cdr new-roots))))
4008
4009           (if threads
4010               ;; If there are some threads, we do them before the
4011               ;; threads on the stack.
4012               (setq thread threads
4013                     gnus-tmp-header (caar thread))
4014             ;; There were no current threads, so we pop something off
4015             ;; the stack.
4016             (setq state (car stack)
4017                   gnus-tmp-level (car state)
4018                   thread (cdr state)
4019                   stack (cdr stack)
4020                   gnus-tmp-header (caar thread))))
4021
4022         (setq gnus-tmp-false-parent nil)
4023         (setq gnus-tmp-root-expunged nil)
4024         (setq thread-end nil)
4025
4026         (if (stringp gnus-tmp-header)
4027             ;; The header is a dummy root.
4028             (cond
4029              ((eq gnus-summary-make-false-root 'adopt)
4030               ;; We let the first article adopt the rest.
4031               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4032                                                (cddar thread)))
4033               (setq gnus-tmp-gathered
4034                     (nconc (mapcar
4035                             (lambda (h) (mail-header-number (car h)))
4036                             (cddar thread))
4037                            gnus-tmp-gathered))
4038               (setq thread (cons (list (caar thread)
4039                                        (cadar thread))
4040                                  (cdr thread)))
4041               (setq gnus-tmp-level -1
4042                     gnus-tmp-false-parent t))
4043              ((eq gnus-summary-make-false-root 'empty)
4044               ;; We print adopted articles with empty subject fields.
4045               (setq gnus-tmp-gathered
4046                     (nconc (mapcar
4047                             (lambda (h) (mail-header-number (car h)))
4048                             (cddar thread))
4049                            gnus-tmp-gathered))
4050               (setq gnus-tmp-level -1))
4051              ((eq gnus-summary-make-false-root 'dummy)
4052               ;; We remember that we probably want to output a dummy
4053               ;; root.
4054               (setq gnus-tmp-dummy-line gnus-tmp-header)
4055               (setq gnus-tmp-prev-subject gnus-tmp-header))
4056              (t
4057               ;; We do not make a root for the gathered
4058               ;; sub-threads at all.
4059               (setq gnus-tmp-level -1)))
4060
4061           (setq number (mail-header-number gnus-tmp-header)
4062                 subject (mail-header-subject gnus-tmp-header))
4063
4064           (cond
4065            ;; If the thread has changed subject, we might want to make
4066            ;; this subthread into a root.
4067            ((and (null gnus-thread-ignore-subject)
4068                  (not (zerop gnus-tmp-level))
4069                  gnus-tmp-prev-subject
4070                  (not (inline
4071                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4072             (setq new-roots (nconc new-roots (list (car thread)))
4073                   thread-end t
4074                   gnus-tmp-header nil))
4075            ;; If the article lies outside the current limit,
4076            ;; then we do not display it.
4077            ((not (memq number gnus-newsgroup-limit))
4078             (setq gnus-tmp-gathered
4079                   (nconc (mapcar
4080                           (lambda (h) (mail-header-number (car h)))
4081                           (cdar thread))
4082                          gnus-tmp-gathered))
4083             (setq gnus-tmp-new-adopts (if (cdar thread)
4084                                           (append gnus-tmp-new-adopts
4085                                                   (cdar thread))
4086                                         gnus-tmp-new-adopts)
4087                   thread-end t
4088                   gnus-tmp-header nil)
4089             (when (zerop gnus-tmp-level)
4090               (setq gnus-tmp-root-expunged t)))
4091            ;; Perhaps this article is to be marked as read?
4092            ((and gnus-summary-mark-below
4093                  (< (or (cdr (assq number gnus-newsgroup-scored))
4094                         default-score)
4095                     gnus-summary-mark-below)
4096                  ;; Don't touch sparse articles.
4097                  (not (gnus-summary-article-sparse-p number))
4098                  (not (gnus-summary-article-ancient-p number)))
4099             (setq gnus-newsgroup-unreads
4100                   (delq number gnus-newsgroup-unreads))
4101             (if gnus-newsgroup-auto-expire
4102                 (push number gnus-newsgroup-expirable)
4103               (push (cons number gnus-low-score-mark)
4104                     gnus-newsgroup-reads))))
4105
4106           (when gnus-tmp-header
4107             ;; We may have an old dummy line to output before this
4108             ;; article.
4109             (when (and gnus-tmp-dummy-line
4110                        (gnus-subject-equal
4111                         gnus-tmp-dummy-line
4112                         (mail-header-subject gnus-tmp-header)))
4113               (gnus-summary-insert-dummy-line
4114                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4115               (setq gnus-tmp-dummy-line nil))
4116
4117             ;; Compute the mark.
4118             (setq gnus-tmp-unread (gnus-article-mark number))
4119
4120             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4121                                   gnus-tmp-header gnus-tmp-level)
4122                   gnus-newsgroup-data)
4123
4124             ;; Actually insert the line.
4125             (setq
4126              gnus-tmp-subject-or-nil
4127              (cond
4128               ((and gnus-thread-ignore-subject
4129                     gnus-tmp-prev-subject
4130                     (not (inline (gnus-subject-equal
4131                                   gnus-tmp-prev-subject subject))))
4132                subject)
4133               ((zerop gnus-tmp-level)
4134                (if (and (eq gnus-summary-make-false-root 'empty)
4135                         (memq number gnus-tmp-gathered)
4136                         gnus-tmp-prev-subject
4137                         (inline (gnus-subject-equal
4138                                  gnus-tmp-prev-subject subject)))
4139                    gnus-summary-same-subject
4140                  subject))
4141               (t gnus-summary-same-subject)))
4142             (if (and (eq gnus-summary-make-false-root 'adopt)
4143                      (= gnus-tmp-level 1)
4144                      (memq number gnus-tmp-gathered))
4145                 (setq gnus-tmp-opening-bracket ?\<
4146                       gnus-tmp-closing-bracket ?\>)
4147               (setq gnus-tmp-opening-bracket ?\[
4148                     gnus-tmp-closing-bracket ?\]))
4149             (setq
4150              gnus-tmp-indentation
4151              (aref gnus-thread-indent-array gnus-tmp-level)
4152              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4153              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4154                                 gnus-summary-default-score 0)
4155              gnus-tmp-score-char
4156              (if (or (null gnus-summary-default-score)
4157                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4158                          gnus-summary-zcore-fuzz))
4159                  ?  ;Whitespace
4160                (if (< gnus-tmp-score gnus-summary-default-score)
4161                    gnus-score-below-mark gnus-score-over-mark))
4162              gnus-tmp-replied
4163              (cond ((memq number gnus-newsgroup-processable)
4164                     gnus-process-mark)
4165                    ((memq number gnus-newsgroup-cached)
4166                     gnus-cached-mark)
4167                    ((memq number gnus-newsgroup-replied)
4168                     gnus-replied-mark)
4169                    ((memq number gnus-newsgroup-saved)
4170                     gnus-saved-mark)
4171                    (t gnus-no-mark))
4172              gnus-tmp-from (mail-header-from gnus-tmp-header)
4173              gnus-tmp-name
4174              (cond
4175               ((string-match "<[^>]+> *$" gnus-tmp-from)
4176                (setq beg-match (match-beginning 0))
4177                (or (and (string-match "^\".+\"" gnus-tmp-from)
4178                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4179                    (substring gnus-tmp-from 0 beg-match)))
4180               ((string-match "(.+)" gnus-tmp-from)
4181                (substring gnus-tmp-from
4182                           (1+ (match-beginning 0)) (1- (match-end 0))))
4183               (t gnus-tmp-from)))
4184             (when (string= gnus-tmp-name "")
4185               (setq gnus-tmp-name gnus-tmp-from))
4186             (unless (numberp gnus-tmp-lines)
4187               (setq gnus-tmp-lines 0))
4188             (gnus-put-text-property
4189              (point)
4190              (progn (eval gnus-summary-line-format-spec) (point))
4191              'gnus-number number)
4192             (when gnus-visual-p
4193               (forward-line -1)
4194               (gnus-run-hooks 'gnus-summary-update-hook)
4195               (forward-line 1))
4196
4197             (setq gnus-tmp-prev-subject subject)))
4198
4199         (when (nth 1 thread)
4200           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4201         (incf gnus-tmp-level)
4202         (setq threads (if thread-end nil (cdar thread)))
4203         (unless threads
4204           (setq gnus-tmp-level 0)))))
4205   (gnus-message 7 "Generating summary...done"))
4206
4207 (defun gnus-summary-prepare-unthreaded (headers)
4208   "Generate an unthreaded summary buffer based on HEADERS."
4209   (let (header number mark)
4210
4211     (beginning-of-line)
4212
4213     (while headers
4214       ;; We may have to root out some bad articles...
4215       (when (memq (setq number (mail-header-number
4216                                 (setq header (pop headers))))
4217                   gnus-newsgroup-limit)
4218         ;; Mark article as read when it has a low score.
4219         (when (and gnus-summary-mark-below
4220                    (< (or (cdr (assq number gnus-newsgroup-scored))
4221                           gnus-summary-default-score 0)
4222                       gnus-summary-mark-below)
4223                    (not (gnus-summary-article-ancient-p number)))
4224           (setq gnus-newsgroup-unreads
4225                 (delq number gnus-newsgroup-unreads))
4226           (if gnus-newsgroup-auto-expire
4227               (push number gnus-newsgroup-expirable)
4228             (push (cons number gnus-low-score-mark)
4229                   gnus-newsgroup-reads)))
4230
4231         (setq mark (gnus-article-mark number))
4232         (push (gnus-data-make number mark (1+ (point)) header 0)
4233               gnus-newsgroup-data)
4234         (gnus-summary-insert-line
4235          header 0 number
4236          mark (memq number gnus-newsgroup-replied)
4237          (memq number gnus-newsgroup-expirable)
4238          (mail-header-subject header) nil
4239          (cdr (assq number gnus-newsgroup-scored))
4240          (memq number gnus-newsgroup-processable))))))
4241
4242 (defun gnus-summary-remove-list-identifiers ()
4243   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4244   (let ((regexp (if (stringp gnus-list-identifiers)
4245                     gnus-list-identifiers
4246                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4247     (dolist (header gnus-newsgroup-headers)
4248       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4249                                   " *\\)\\)+\\(Re: +\\)?\\)")
4250                           (mail-header-subject header))
4251         (mail-header-set-subject
4252          header (concat (substring (mail-header-subject header)
4253                                    0 (match-beginning 1))
4254                         (or
4255                          (match-string 3 (mail-header-subject header))
4256                          (match-string 5 (mail-header-subject header)))
4257                         (substring (mail-header-subject header)
4258                                    (match-end 1))))))))
4259
4260 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4261   "Select newsgroup GROUP.
4262 If READ-ALL is non-nil, all articles in the group are selected.
4263 If SELECT-ARTICLES, only select those articles from GROUP."
4264   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4265          ;;!!! Dirty hack; should be removed.
4266          (gnus-summary-ignore-duplicates
4267           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4268               t
4269             gnus-summary-ignore-duplicates))
4270          (info (nth 2 entry))
4271          articles fetched-articles cached)
4272
4273     (unless (gnus-check-server
4274              (setq gnus-current-select-method
4275                    (gnus-find-method-for-group group)))
4276       (error "Couldn't open server"))
4277
4278     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4279         (gnus-activate-group group)     ; Or we can activate it...
4280         (progn                          ; Or we bug out.
4281           (when (equal major-mode 'gnus-summary-mode)
4282             (kill-buffer (current-buffer)))
4283           (error "Couldn't activate group %s: %s"
4284                  group (gnus-status-message group))))
4285
4286     (unless (gnus-request-group group t)
4287       (when (equal major-mode 'gnus-summary-mode)
4288         (kill-buffer (current-buffer)))
4289       (error "Couldn't request group %s: %s"
4290              group (gnus-status-message group)))
4291
4292     (setq gnus-newsgroup-name group)
4293     (setq gnus-newsgroup-unselected nil)
4294     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4295     (gnus-summary-setup-default-charset)
4296
4297     ;; Adjust and set lists of article marks.
4298     (when info
4299       (gnus-adjust-marked-articles info))
4300
4301     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4302     (when (gnus-virtual-group-p group)
4303       (setq cached gnus-newsgroup-cached))
4304
4305     (setq gnus-newsgroup-unreads
4306           (gnus-set-difference
4307            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4308            gnus-newsgroup-dormant))
4309
4310     (setq gnus-newsgroup-processable nil)
4311
4312     (gnus-update-read-articles group gnus-newsgroup-unreads)
4313
4314     (if (setq articles select-articles)
4315         (setq gnus-newsgroup-unselected
4316               (gnus-sorted-intersection
4317                gnus-newsgroup-unreads
4318                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4319       (setq articles (gnus-articles-to-read group read-all)))
4320
4321     (cond
4322      ((null articles)
4323       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4324       'quit)
4325      ((eq articles 0) nil)
4326      (t
4327       ;; Init the dependencies hash table.
4328       (setq gnus-newsgroup-dependencies
4329             (gnus-make-hashtable (length articles)))
4330       (gnus-set-global-variables)
4331       ;; Retrieve the headers and read them in.
4332       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4333       (setq gnus-newsgroup-headers
4334             (if (eq 'nov
4335                     (setq gnus-headers-retrieved-by
4336                           (gnus-retrieve-headers
4337                            articles gnus-newsgroup-name
4338                            ;; We might want to fetch old headers, but
4339                            ;; not if there is only 1 article.
4340                            (and (or (and
4341                                      (not (eq gnus-fetch-old-headers 'some))
4342                                      (not (numberp gnus-fetch-old-headers)))
4343                                     (> (length articles) 1))
4344                                 gnus-fetch-old-headers))))
4345                 (gnus-get-newsgroup-headers-xover
4346                  articles nil nil gnus-newsgroup-name t)
4347               (gnus-get-newsgroup-headers)))
4348       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4349
4350       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4351       (when cached
4352         (setq gnus-newsgroup-cached cached))
4353
4354       ;; Suppress duplicates?
4355       (when gnus-suppress-duplicates
4356         (gnus-dup-suppress-articles))
4357
4358       ;; Set the initial limit.
4359       (setq gnus-newsgroup-limit (copy-sequence articles))
4360       ;; Remove canceled articles from the list of unread articles.
4361       (setq gnus-newsgroup-unreads
4362             (gnus-set-sorted-intersection
4363              gnus-newsgroup-unreads
4364              (setq fetched-articles
4365                    (mapcar (lambda (headers) (mail-header-number headers))
4366                            gnus-newsgroup-headers))))
4367       ;; Removed marked articles that do not exist.
4368       (gnus-update-missing-marks
4369        (gnus-sorted-complement fetched-articles articles))
4370       ;; We might want to build some more threads first.
4371       (when (and gnus-fetch-old-headers
4372                  (eq gnus-headers-retrieved-by 'nov))
4373         (if (eq gnus-fetch-old-headers 'invisible)
4374             (gnus-build-all-threads)
4375           (gnus-build-old-threads)))
4376       ;; Let the Gnus agent mark articles as read.
4377       (when gnus-agent
4378         (gnus-agent-get-undownloaded-list))
4379       ;; Remove list identifiers from subject
4380       (when gnus-list-identifiers
4381         (gnus-summary-remove-list-identifiers))
4382       ;; Check whether auto-expire is to be done in this group.
4383       (setq gnus-newsgroup-auto-expire
4384             (gnus-group-auto-expirable-p group))
4385       ;; Set up the article buffer now, if necessary.
4386       (unless gnus-single-article-buffer
4387         (gnus-article-setup-buffer))
4388       ;; First and last article in this newsgroup.
4389       (when gnus-newsgroup-headers
4390         (setq gnus-newsgroup-begin
4391               (mail-header-number (car gnus-newsgroup-headers))
4392               gnus-newsgroup-end
4393               (mail-header-number
4394                (gnus-last-element gnus-newsgroup-headers))))
4395       ;; GROUP is successfully selected.
4396       (or gnus-newsgroup-headers t)))))
4397
4398 (defun gnus-articles-to-read (group &optional read-all)
4399   "Find out what articles the user wants to read."
4400   (let* ((articles
4401           ;; Select all articles if `read-all' is non-nil, or if there
4402           ;; are no unread articles.
4403           (if (or read-all
4404                   (and (zerop (length gnus-newsgroup-marked))
4405                        (zerop (length gnus-newsgroup-unreads)))
4406                   (eq (gnus-group-find-parameter group 'display)
4407                       'all))
4408               (or
4409                (gnus-uncompress-range (gnus-active group))
4410                (gnus-cache-articles-in-group group))
4411             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4412                           (copy-sequence gnus-newsgroup-unreads))
4413                   '<)))
4414          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4415          (scored (length scored-list))
4416          (number (length articles))
4417          (marked (+ (length gnus-newsgroup-marked)
4418                     (length gnus-newsgroup-dormant)))
4419          (select
4420           (cond
4421            ((numberp read-all)
4422             read-all)
4423            (t
4424             (condition-case ()
4425                 (cond
4426                  ((and (or (<= scored marked) (= scored number))
4427                        (numberp gnus-large-newsgroup)
4428                        (> number gnus-large-newsgroup))
4429                   (let ((input
4430                          (read-string
4431                           (format
4432                            "How many articles from %s (default %d): "
4433                            (gnus-limit-string gnus-newsgroup-name 35)
4434                            number))))
4435                     (if (string-match "^[ \t]*$" input) number input)))
4436                  ((and (> scored marked) (< scored number)
4437                        (> (- scored number) 20))
4438                   (let ((input
4439                          (read-string
4440                           (format "%s %s (%d scored, %d total): "
4441                                   "How many articles from"
4442                                   group scored number))))
4443                     (if (string-match "^[ \t]*$" input)
4444                         number input)))
4445                  (t number))
4446               (quit
4447                (message "Quit getting the articles to read")
4448                nil))))))
4449     (setq select (if (stringp select) (string-to-number select) select))
4450     (if (or (null select) (zerop select))
4451         select
4452       (if (and (not (zerop scored)) (<= (abs select) scored))
4453           (progn
4454             (setq articles (sort scored-list '<))
4455             (setq number (length articles)))
4456         (setq articles (copy-sequence articles)))
4457
4458       (when (< (abs select) number)
4459         (if (< select 0)
4460             ;; Select the N oldest articles.
4461             (setcdr (nthcdr (1- (abs select)) articles) nil)
4462           ;; Select the N most recent articles.
4463           (setq articles (nthcdr (- number select) articles))))
4464       (setq gnus-newsgroup-unselected
4465             (gnus-sorted-intersection
4466              gnus-newsgroup-unreads
4467              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4468       (when gnus-alter-articles-to-read-function
4469         (setq gnus-newsgroup-unreads
4470               (sort 
4471                (funcall gnus-alter-articles-to-read-function
4472                         gnus-newsgroup-name gnus-newsgroup-unreads)
4473                '<)))
4474       articles)))
4475
4476 (defun gnus-killed-articles (killed articles)
4477   (let (out)
4478     (while articles
4479       (when (inline (gnus-member-of-range (car articles) killed))
4480         (push (car articles) out))
4481       (setq articles (cdr articles)))
4482     out))
4483
4484 (defun gnus-uncompress-marks (marks)
4485   "Uncompress the mark ranges in MARKS."
4486   (let ((uncompressed '(score bookmark))
4487         out)
4488     (while marks
4489       (if (memq (caar marks) uncompressed)
4490           (push (car marks) out)
4491         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4492       (setq marks (cdr marks)))
4493     out))
4494
4495 (defun gnus-adjust-marked-articles (info)
4496   "Set all article lists and remove all marks that are no longer valid."
4497   (let* ((marked-lists (gnus-info-marks info))
4498          (active (gnus-active (gnus-info-group info)))
4499          (min (car active))
4500          (max (cdr active))
4501          (types gnus-article-mark-lists)
4502          (uncompressed '(score bookmark killed))
4503          marks var articles article mark)
4504
4505     (while marked-lists
4506       (setq marks (pop marked-lists))
4507       (set (setq var (intern (format "gnus-newsgroup-%s"
4508                                      (car (rassq (setq mark (car marks))
4509                                                  types)))))
4510            (if (memq (car marks) uncompressed) (cdr marks)
4511              (gnus-uncompress-range (cdr marks))))
4512
4513       (setq articles (symbol-value var))
4514
4515       ;; All articles have to be subsets of the active articles.
4516       (cond
4517        ;; Adjust "simple" lists.
4518        ((memq mark '(tick dormant expire reply save))
4519         (while articles
4520           (when (or (< (setq article (pop articles)) min) (> article max))
4521             (set var (delq article (symbol-value var))))))
4522        ;; Adjust assocs.
4523        ((memq mark uncompressed)
4524         (when (not (listp (cdr (symbol-value var))))
4525           (set var (list (symbol-value var))))
4526         (when (not (listp (cdr articles)))
4527           (setq articles (list articles)))
4528         (while articles
4529           (when (or (not (consp (setq article (pop articles))))
4530                     (< (car article) min)
4531                     (> (car article) max))
4532             (set var (delq article (symbol-value var))))))))))
4533
4534 (defun gnus-update-missing-marks (missing)
4535   "Go through the list of MISSING articles and remove them from the mark lists."
4536   (when missing
4537     (let ((types gnus-article-mark-lists)
4538           var m)
4539       ;; Go through all types.
4540       (while types
4541         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4542         (when (symbol-value var)
4543           ;; This list has articles.  So we delete all missing articles
4544           ;; from it.
4545           (setq m missing)
4546           (while m
4547             (set var (delq (pop m) (symbol-value var)))))))))
4548
4549 (defun gnus-update-marks ()
4550   "Enter the various lists of marked articles into the newsgroup info list."
4551   (let ((types gnus-article-mark-lists)
4552         (info (gnus-get-info gnus-newsgroup-name))
4553         (uncompressed '(score bookmark killed))
4554         type list newmarked symbol delta-marks)
4555     (when info
4556       ;; Add all marks lists to the list of marks lists.
4557       (while (setq type (pop types))
4558         (setq list (symbol-value
4559                     (setq symbol
4560                           (intern (format "gnus-newsgroup-%s"
4561                                           (car type))))))
4562
4563         (when list
4564           ;; Get rid of the entries of the articles that have the
4565           ;; default score.
4566           (when (and (eq (cdr type) 'score)
4567                      gnus-save-score
4568                      list)
4569             (let* ((arts list)
4570                    (prev (cons nil list))
4571                    (all prev))
4572               (while arts
4573                 (if (or (not (consp (car arts)))
4574                         (= (cdar arts) gnus-summary-default-score))
4575                     (setcdr prev (cdr arts))
4576                   (setq prev arts))
4577                 (setq arts (cdr arts)))
4578               (setq list (cdr all)))))
4579
4580         (unless (memq (cdr type) uncompressed)
4581           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4582        
4583         (when (gnus-check-backend-function
4584                'request-set-mark gnus-newsgroup-name)
4585           ;; propagate flags to server, with the following exceptions:
4586           ;; uncompressed:s are not proper flags (they are cons cells)
4587           ;; cache is a internal gnus flag
4588           ;; download are local to one gnus installation (well)
4589           ;; unsend are for nndraft groups only
4590           ;; xxx: generality of this?  this suits nnimap anyway
4591           (unless (memq (cdr type) (append '(cache download unsend)
4592                                            uncompressed))
4593             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4594                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4595                    (add (gnus-remove-from-range
4596                          (gnus-copy-sequence list) old)))
4597               (when add
4598                 (push (list add 'add (list (cdr type))) delta-marks))
4599               (when del
4600                 (push (list del 'del (list (cdr type))) delta-marks)))))
4601           
4602         (when list
4603           (push (cons (cdr type) list) newmarked)))
4604
4605       (when delta-marks
4606         (unless (gnus-check-group gnus-newsgroup-name)
4607           (error "Can't open server for %s" gnus-newsgroup-name))
4608         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4609           
4610       ;; Enter these new marks into the info of the group.
4611       (if (nthcdr 3 info)
4612           (setcar (nthcdr 3 info) newmarked)
4613         ;; Add the marks lists to the end of the info.
4614         (when newmarked
4615           (setcdr (nthcdr 2 info) (list newmarked))))
4616
4617       ;; Cut off the end of the info if there's nothing else there.
4618       (let ((i 5))
4619         (while (and (> i 2)
4620                     (not (nth i info)))
4621           (when (nthcdr (decf i) info)
4622             (setcdr (nthcdr i info) nil)))))))
4623
4624 (defun gnus-set-mode-line (where)
4625   "Set the mode line of the article or summary buffers.
4626 If WHERE is `summary', the summary mode line format will be used."
4627   ;; Is this mode line one we keep updated?
4628   (when (and (memq where gnus-updated-mode-lines)
4629              (symbol-value
4630               (intern (format "gnus-%s-mode-line-format-spec" where))))
4631     (let (mode-string)
4632       (save-excursion
4633         ;; We evaluate this in the summary buffer since these
4634         ;; variables are buffer-local to that buffer.
4635         (set-buffer gnus-summary-buffer)
4636         ;; We bind all these variables that are used in the `eval' form
4637         ;; below.
4638         (let* ((mformat (symbol-value
4639                          (intern
4640                           (format "gnus-%s-mode-line-format-spec" where))))
4641                (gnus-tmp-group-name (gnus-group-name-decode 
4642                                      gnus-newsgroup-name
4643                                      (gnus-group-name-charset 
4644                                       nil
4645                                       gnus-newsgroup-name)))
4646                (gnus-tmp-article-number (or gnus-current-article 0))
4647                (gnus-tmp-unread gnus-newsgroup-unreads)
4648                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4649                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4650                (gnus-tmp-unread-and-unselected
4651                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4652                             (zerop gnus-tmp-unselected))
4653                        "")
4654                       ((zerop gnus-tmp-unselected)
4655                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4656                       (t (format "{%d(+%d) more}"
4657                                  gnus-tmp-unread-and-unticked
4658                                  gnus-tmp-unselected))))
4659                (gnus-tmp-subject
4660                 (if (and gnus-current-headers
4661                          (vectorp gnus-current-headers))
4662                     (gnus-mode-string-quote
4663                      (mail-header-subject gnus-current-headers))
4664                   ""))
4665                bufname-length max-len
4666                gnus-tmp-header);; passed as argument to any user-format-funcs
4667           (setq mode-string (eval mformat))
4668           (setq bufname-length (if (string-match "%b" mode-string)
4669                                    (- (length
4670                                        (buffer-name
4671                                         (if (eq where 'summary)
4672                                             nil
4673                                           (get-buffer gnus-article-buffer))))
4674                                       2)
4675                                  0))
4676           (setq max-len (max 4 (if gnus-mode-non-string-length
4677                                    (- (window-width)
4678                                       gnus-mode-non-string-length
4679                                       bufname-length)
4680                                  (length mode-string))))
4681           ;; We might have to chop a bit of the string off...
4682           (when (> (length mode-string) max-len)
4683             (setq mode-string
4684                   (concat (truncate-string-to-width mode-string (- max-len 3))
4685                           "...")))
4686           ;; Pad the mode string a bit.
4687           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4688       ;; Update the mode line.
4689       (setq mode-line-buffer-identification
4690             (gnus-mode-line-buffer-identification (list mode-string)))
4691       (set-buffer-modified-p t))))
4692
4693 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4694   "Go through the HEADERS list and add all Xrefs to a hash table.
4695 The resulting hash table is returned, or nil if no Xrefs were found."
4696   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4697          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4698          (xref-hashtb (gnus-make-hashtable))
4699          start group entry number xrefs header)
4700     (while headers
4701       (setq header (pop headers))
4702       (when (and (setq xrefs (mail-header-xref header))
4703                  (not (memq (setq number (mail-header-number header))
4704                             unreads)))
4705         (setq start 0)
4706         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4707           (setq start (match-end 0))
4708           (setq group (if prefix
4709                           (concat prefix (substring xrefs (match-beginning 1)
4710                                                     (match-end 1)))
4711                         (substring xrefs (match-beginning 1) (match-end 1))))
4712           (setq number
4713                 (string-to-int (substring xrefs (match-beginning 2)
4714                                           (match-end 2))))
4715           (if (setq entry (gnus-gethash group xref-hashtb))
4716               (setcdr entry (cons number (cdr entry)))
4717             (gnus-sethash group (cons number nil) xref-hashtb)))))
4718     (and start xref-hashtb)))
4719
4720 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4721   "Look through all the headers and mark the Xrefs as read."
4722   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4723         name entry info xref-hashtb idlist method nth4)
4724     (save-excursion
4725       (set-buffer gnus-group-buffer)
4726       (when (setq xref-hashtb
4727                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4728         (mapatoms
4729          (lambda (group)
4730            (unless (string= from-newsgroup (setq name (symbol-name group)))
4731              (setq idlist (symbol-value group))
4732              ;; Dead groups are not updated.
4733              (and (prog1
4734                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4735                             info (nth 2 entry))
4736                     (when (stringp (setq nth4 (gnus-info-method info)))
4737                       (setq nth4 (gnus-server-to-method nth4))))
4738                   ;; Only do the xrefs if the group has the same
4739                   ;; select method as the group we have just read.
4740                   (or (gnus-methods-equal-p
4741                        nth4 (gnus-find-method-for-group from-newsgroup))
4742                       virtual
4743                       (equal nth4 (setq method (gnus-find-method-for-group
4744                                                 from-newsgroup)))
4745                       (and (equal (car nth4) (car method))
4746                            (equal (nth 1 nth4) (nth 1 method))))
4747                   gnus-use-cross-reference
4748                   (or (not (eq gnus-use-cross-reference t))
4749                       virtual
4750                       ;; Only do cross-references on subscribed
4751                       ;; groups, if that is what is wanted.
4752                       (<= (gnus-info-level info) gnus-level-subscribed))
4753                   (gnus-group-make-articles-read name idlist))))
4754          xref-hashtb)))))
4755
4756 (defun gnus-compute-read-articles (group articles)
4757   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4758          (info (nth 2 entry))
4759          (active (gnus-active group))
4760          ninfo)
4761     (when entry
4762       ;; First peel off all invalid article numbers.
4763       (when active
4764         (let ((ids articles)
4765               id first)
4766           (while (setq id (pop ids))
4767             (when (and first (> id (cdr active)))
4768               ;; We'll end up in this situation in one particular
4769               ;; obscure situation.  If you re-scan a group and get
4770               ;; a new article that is cross-posted to a different
4771               ;; group that has not been re-scanned, you might get
4772               ;; crossposted article that has a higher number than
4773               ;; Gnus believes possible.  So we re-activate this
4774               ;; group as well.  This might mean doing the
4775               ;; crossposting thingy will *increase* the number
4776               ;; of articles in some groups.  Tsk, tsk.
4777               (setq active (or (gnus-activate-group group) active)))
4778             (when (or (> id (cdr active))
4779                       (< id (car active)))
4780               (setq articles (delq id articles))))))
4781       ;; If the read list is nil, we init it.
4782       (if (and active
4783                (null (gnus-info-read info))
4784                (> (car active) 1))
4785           (setq ninfo (cons 1 (1- (car active))))
4786         (setq ninfo (gnus-info-read info)))
4787       ;; Then we add the read articles to the range.
4788       (gnus-add-to-range
4789        ninfo (setq articles (sort articles '<))))))
4790
4791 (defun gnus-group-make-articles-read (group articles)
4792   "Update the info of GROUP to say that ARTICLES are read."
4793   (let* ((num 0)
4794          (entry (gnus-gethash group gnus-newsrc-hashtb))
4795          (info (nth 2 entry))
4796          (active (gnus-active group))
4797          range)
4798     (when entry
4799       (setq range (gnus-compute-read-articles group articles))
4800       (save-excursion
4801         (set-buffer gnus-group-buffer)
4802         (gnus-undo-register
4803           `(progn
4804              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4805              (gnus-info-set-read ',info ',(gnus-info-read info))
4806              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4807              (gnus-group-update-group ,group t))))
4808       ;; Add the read articles to the range.
4809       (gnus-info-set-read info range)
4810       ;; Then we have to re-compute how many unread
4811       ;; articles there are in this group.
4812       (when active
4813         (cond
4814          ((not range)
4815           (setq num (- (1+ (cdr active)) (car active))))
4816          ((not (listp (cdr range)))
4817           (setq num (- (cdr active) (- (1+ (cdr range))
4818                                        (car range)))))
4819          (t
4820           (while range
4821             (if (numberp (car range))
4822                 (setq num (1+ num))
4823               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4824             (setq range (cdr range)))
4825           (setq num (- (cdr active) num))))
4826         ;; Update the number of unread articles.
4827         (setcar entry num)
4828         ;; Update the group buffer.
4829         (gnus-group-update-group group t)))))
4830
4831 (defvar gnus-newsgroup-none-id 0)
4832
4833 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4834   (let ((cur nntp-server-buffer)
4835         (dependencies
4836          (or dependencies
4837              (save-excursion (set-buffer gnus-summary-buffer)
4838                              gnus-newsgroup-dependencies)))
4839         headers id end ref
4840         (mail-parse-charset gnus-newsgroup-charset)
4841         (mail-parse-ignored-charsets
4842          (save-excursion (condition-case nil
4843                              (set-buffer gnus-summary-buffer)
4844                            (error))
4845                          gnus-newsgroup-ignored-charsets)))
4846     (save-excursion
4847       (set-buffer nntp-server-buffer)
4848       ;; Translate all TAB characters into SPACE characters.
4849       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4850       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4851       (gnus-run-hooks 'gnus-parse-headers-hook)
4852       (let ((case-fold-search t)
4853             in-reply-to header p lines chars)
4854         (goto-char (point-min))
4855         ;; Search to the beginning of the next header.  Error messages
4856         ;; do not begin with 2 or 3.
4857         (while (re-search-forward "^[23][0-9]+ " nil t)
4858           (setq id nil
4859                 ref nil)
4860           ;; This implementation of this function, with nine
4861           ;; search-forwards instead of the one re-search-forward and
4862           ;; a case (which basically was the old function) is actually
4863           ;; about twice as fast, even though it looks messier.  You
4864           ;; can't have everything, I guess.  Speed and elegance
4865           ;; doesn't always go hand in hand.
4866           (setq
4867            header
4868            (vector
4869             ;; Number.
4870             (prog1
4871                 (read cur)
4872               (end-of-line)
4873               (setq p (point))
4874               (narrow-to-region (point)
4875                                 (or (and (search-forward "\n.\n" nil t)
4876                                          (- (point) 2))
4877                                     (point))))
4878             ;; Subject.
4879             (progn
4880               (goto-char p)
4881               (if (search-forward "\nsubject: " nil t)
4882                   (funcall gnus-decode-encoded-word-function
4883                            (nnheader-header-value))
4884                 "(none)"))
4885             ;; From.
4886             (progn
4887               (goto-char p)
4888               (if (or (search-forward "\nfrom: " nil t)
4889                       (search-forward "\nfrom:" nil t))
4890                   (funcall gnus-decode-encoded-word-function
4891                            (nnheader-header-value))
4892                 "(nobody)"))
4893             ;; Date.
4894             (progn
4895               (goto-char p)
4896               (if (search-forward "\ndate: " nil t)
4897                   (nnheader-header-value) ""))
4898             ;; Message-ID.
4899             (progn
4900               (goto-char p)
4901               (setq id (if (re-search-forward
4902                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4903                            ;; We do it this way to make sure the Message-ID
4904                            ;; is (somewhat) syntactically valid.
4905                            (buffer-substring (match-beginning 1)
4906                                              (match-end 1))
4907                          ;; If there was no message-id, we just fake one
4908                          ;; to make subsequent routines simpler.
4909                          (nnheader-generate-fake-message-id))))
4910             ;; References.
4911             (progn
4912               (goto-char p)
4913               (if (search-forward "\nreferences: " nil t)
4914                   (progn
4915                     (setq end (point))
4916                     (prog1
4917                         (nnheader-header-value)
4918                       (setq ref
4919                             (buffer-substring
4920                              (progn
4921                                (end-of-line)
4922                                (search-backward ">" end t)
4923                                (1+ (point)))
4924                              (progn
4925                                (search-backward "<" end t)
4926                                (point))))))
4927                 ;; Get the references from the in-reply-to header if there
4928                 ;; were no references and the in-reply-to header looks
4929                 ;; promising.
4930                 (if (and (search-forward "\nin-reply-to: " nil t)
4931                          (setq in-reply-to (nnheader-header-value))
4932                          (string-match "<[^>]+>" in-reply-to))
4933                     (let (ref2)
4934                       (setq ref (substring in-reply-to (match-beginning 0)
4935                                            (match-end 0)))
4936                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4937                         (setq ref2 (substring in-reply-to (match-beginning 0)
4938                                               (match-end 0)))
4939                         (when (> (length ref2) (length ref))
4940                           (setq ref ref2)))
4941                       ref)
4942                   (setq ref nil))))
4943             ;; Chars.
4944             (progn
4945               (goto-char p)
4946               (if (search-forward "\nchars: " nil t)
4947                   (if (numberp (setq chars (ignore-errors (read cur))))
4948                       chars 0)
4949                 0))
4950             ;; Lines.
4951             (progn
4952               (goto-char p)
4953               (if (search-forward "\nlines: " nil t)
4954                   (if (numberp (setq lines (ignore-errors (read cur))))
4955                       lines 0)
4956                 0))
4957             ;; Xref.
4958             (progn
4959               (goto-char p)
4960               (and (search-forward "\nxref: " nil t)
4961                    (nnheader-header-value)))
4962             ;; Extra.
4963             (when gnus-extra-headers
4964               (let ((extra gnus-extra-headers)
4965                     out)
4966                 (while extra
4967                   (goto-char p)
4968                   (when (search-forward
4969                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4970                     (push (cons (car extra) (nnheader-header-value))
4971                           out))
4972                   (pop extra))
4973                 out))))
4974           (when (equal id ref)
4975             (setq ref nil))
4976
4977           (when gnus-alter-header-function
4978             (funcall gnus-alter-header-function header)
4979             (setq id (mail-header-id header)
4980                   ref (gnus-parent-id (mail-header-references header))))
4981
4982           (when (setq header
4983                       (gnus-dependencies-add-header
4984                        header dependencies force-new))
4985             (push header headers))
4986           (goto-char (point-max))
4987           (widen))
4988         (nreverse headers)))))
4989
4990 ;; Goes through the xover lines and returns a list of vectors
4991 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4992                                                   force-new dependencies
4993                                                   group also-fetch-heads)
4994   "Parse the news overview data in the server buffer.
4995 Return a list of headers that match SEQUENCE (see
4996 `nntp-retrieve-headers')."
4997   ;; Get the Xref when the users reads the articles since most/some
4998   ;; NNTP servers do not include Xrefs when using XOVER.
4999   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5000   (let ((mail-parse-charset gnus-newsgroup-charset)
5001         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5002         (cur nntp-server-buffer)
5003         (dependencies (or dependencies gnus-newsgroup-dependencies))
5004         number headers header)
5005     (save-excursion
5006       (set-buffer nntp-server-buffer)
5007       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5008       ;; Allow the user to mangle the headers before parsing them.
5009       (gnus-run-hooks 'gnus-parse-headers-hook)
5010       (goto-char (point-min))
5011       (while (not (eobp))
5012         (condition-case ()
5013             (while (and sequence (not (eobp)))
5014               (setq number (read cur))
5015               (while (and sequence
5016                           (< (car sequence) number))
5017                 (setq sequence (cdr sequence)))
5018               (and sequence
5019                    (eq number (car sequence))
5020                    (progn
5021                      (setq sequence (cdr sequence))
5022                      (setq header (inline
5023                                     (gnus-nov-parse-line
5024                                      number dependencies force-new))))
5025                    (push header headers))
5026               (forward-line 1))
5027           (error
5028            (gnus-error 4 "Strange nov line (%d)"
5029                        (count-lines (point-min) (point)))))
5030         (forward-line 1))
5031       ;; A common bug in inn is that if you have posted an article and
5032       ;; then retrieves the active file, it will answer correctly --
5033       ;; the new article is included.  However, a NOV entry for the
5034       ;; article may not have been generated yet, so this may fail.
5035       ;; We work around this problem by retrieving the last few
5036       ;; headers using HEAD.
5037       (if (or (not also-fetch-heads)
5038               (not sequence))
5039           ;; We (probably) got all the headers.
5040           (nreverse headers)
5041         (let ((gnus-nov-is-evil t))
5042           (nconc
5043            (nreverse headers)
5044            (when (gnus-retrieve-headers sequence group)
5045              (gnus-get-newsgroup-headers))))))))
5046
5047 (defun gnus-article-get-xrefs ()
5048   "Fill in the Xref value in `gnus-current-headers', if necessary.
5049 This is meant to be called in `gnus-article-internal-prepare-hook'."
5050   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5051                                  gnus-current-headers)))
5052     (or (not gnus-use-cross-reference)
5053         (not headers)
5054         (and (mail-header-xref headers)
5055              (not (string= (mail-header-xref headers) "")))
5056         (let ((case-fold-search t)
5057               xref)
5058           (save-restriction
5059             (nnheader-narrow-to-headers)
5060             (goto-char (point-min))
5061             (when (or (and (not (eobp))
5062                            (eq (downcase (char-after)) ?x)
5063                            (looking-at "Xref:"))
5064                       (search-forward "\nXref:" nil t))
5065               (goto-char (1+ (match-end 0)))
5066               (setq xref (buffer-substring (point)
5067                                            (progn (end-of-line) (point))))
5068               (mail-header-set-xref headers xref)))))))
5069
5070 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5071   "Find article ID and insert the summary line for that article.
5072 OLD-HEADER can either be a header or a line number to insert
5073 the subject line on."
5074   (let* ((line (and (numberp old-header) old-header))
5075          (old-header (and (vectorp old-header) old-header))
5076          (header (cond ((and old-header use-old-header)
5077                         old-header)
5078                        ((and (numberp id)
5079                              (gnus-number-to-header id))
5080                         (gnus-number-to-header id))
5081                        (t
5082                         (gnus-read-header id))))
5083          (number (and (numberp id) id))
5084          d)
5085     (when header
5086       ;; Rebuild the thread that this article is part of and go to the
5087       ;; article we have fetched.
5088       (when (and (not gnus-show-threads)
5089                  old-header)
5090         (when (and number
5091                    (setq d (gnus-data-find (mail-header-number old-header))))
5092           (goto-char (gnus-data-pos d))
5093           (gnus-data-remove
5094            number
5095            (- (gnus-point-at-bol)
5096               (prog1
5097                   (1+ (gnus-point-at-eol))
5098                 (gnus-delete-line))))))
5099       (when old-header
5100         (mail-header-set-number header (mail-header-number old-header)))
5101       (setq gnus-newsgroup-sparse
5102             (delq (setq number (mail-header-number header))
5103                   gnus-newsgroup-sparse))
5104       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5105       (push number gnus-newsgroup-limit)
5106       (gnus-rebuild-thread (mail-header-id header) line)
5107       (gnus-summary-goto-subject number nil t))
5108     (when (and (numberp number)
5109                (> number 0))
5110       ;; We have to update the boundaries even if we can't fetch the
5111       ;; article if ID is a number -- so that the next `P' or `N'
5112       ;; command will fetch the previous (or next) article even
5113       ;; if the one we tried to fetch this time has been canceled.
5114       (when (> number gnus-newsgroup-end)
5115         (setq gnus-newsgroup-end number))
5116       (when (< number gnus-newsgroup-begin)
5117         (setq gnus-newsgroup-begin number))
5118       (setq gnus-newsgroup-unselected
5119             (delq number gnus-newsgroup-unselected)))
5120     ;; Report back a success?
5121     (and header (mail-header-number header))))
5122
5123 ;;; Process/prefix in the summary buffer
5124
5125 (defun gnus-summary-work-articles (n)
5126   "Return a list of articles to be worked upon.
5127 The prefix argument, the list of process marked articles, and the
5128 current article will be taken into consideration."
5129   (save-excursion
5130     (set-buffer gnus-summary-buffer)
5131     (cond
5132      (n
5133       ;; A numerical prefix has been given.
5134       (setq n (prefix-numeric-value n))
5135       (let ((backward (< n 0))
5136             (n (abs (prefix-numeric-value n)))
5137             articles article)
5138         (save-excursion
5139           (while
5140               (and (> n 0)
5141                    (push (setq article (gnus-summary-article-number))
5142                          articles)
5143                    (if backward
5144                        (gnus-summary-find-prev nil article)
5145                      (gnus-summary-find-next nil article)))
5146             (decf n)))
5147         (nreverse articles)))
5148      ((and (gnus-region-active-p) (mark))
5149       (message "region active")
5150       ;; Work on the region between point and mark.
5151       (let ((max (max (point) (mark)))
5152             articles article)
5153         (save-excursion
5154           (goto-char (min (min (point) (mark))))
5155           (while
5156               (and
5157                (push (setq article (gnus-summary-article-number)) articles)
5158                (gnus-summary-find-next nil article)
5159                (< (point) max)))
5160           (nreverse articles))))
5161      (gnus-newsgroup-processable
5162       ;; There are process-marked articles present.
5163       ;; Save current state.
5164       (gnus-summary-save-process-mark)
5165       ;; Return the list.
5166       (reverse gnus-newsgroup-processable))
5167      (t
5168       ;; Just return the current article.
5169       (list (gnus-summary-article-number))))))
5170
5171 (defmacro gnus-summary-iterate (arg &rest forms)
5172   "Iterate over the process/prefixed articles and do FORMS.
5173 ARG is the interactive prefix given to the command.  FORMS will be
5174 executed with point over the summary line of the articles."
5175   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5176     `(let ((,articles (gnus-summary-work-articles ,arg)))
5177        (while ,articles
5178          (gnus-summary-goto-subject (car ,articles))
5179          ,@forms
5180          (pop ,articles)))))
5181
5182 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5183 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5184
5185 (defun gnus-summary-save-process-mark ()
5186   "Push the current set of process marked articles on the stack."
5187   (interactive)
5188   (push (copy-sequence gnus-newsgroup-processable)
5189         gnus-newsgroup-process-stack))
5190
5191 (defun gnus-summary-kill-process-mark ()
5192   "Push the current set of process marked articles on the stack and unmark."
5193   (interactive)
5194   (gnus-summary-save-process-mark)
5195   (gnus-summary-unmark-all-processable))
5196
5197 (defun gnus-summary-yank-process-mark ()
5198   "Pop the last process mark state off the stack and restore it."
5199   (interactive)
5200   (unless gnus-newsgroup-process-stack
5201     (error "Empty mark stack"))
5202   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5203
5204 (defun gnus-summary-process-mark-set (set)
5205   "Make SET into the current process marked articles."
5206   (gnus-summary-unmark-all-processable)
5207   (while set
5208     (gnus-summary-set-process-mark (pop set))))
5209
5210 ;;; Searching and stuff
5211
5212 (defun gnus-summary-search-group (&optional backward use-level)
5213   "Search for next unread newsgroup.
5214 If optional argument BACKWARD is non-nil, search backward instead."
5215   (save-excursion
5216     (set-buffer gnus-group-buffer)
5217     (when (gnus-group-search-forward
5218            backward nil (if use-level (gnus-group-group-level) nil))
5219       (gnus-group-group-name))))
5220
5221 (defun gnus-summary-best-group (&optional exclude-group)
5222   "Find the name of the best unread group.
5223 If EXCLUDE-GROUP, do not go to this group."
5224   (save-excursion
5225     (set-buffer gnus-group-buffer)
5226     (save-excursion
5227       (gnus-group-best-unread-group exclude-group))))
5228
5229 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5230   (if backward (gnus-summary-find-prev)
5231     (let* ((dummy (gnus-summary-article-intangible-p))
5232            (article (or article (gnus-summary-article-number)))
5233            (arts (gnus-data-find-list article))
5234            result)
5235       (when (and (not dummy)
5236                  (or (not gnus-summary-check-current)
5237                      (not unread)
5238                      (not (gnus-data-unread-p (car arts)))))
5239         (setq arts (cdr arts)))
5240       (when (setq result
5241                   (if unread
5242                       (progn
5243                         (while arts
5244                           (when (or (and undownloaded
5245                                          (eq gnus-undownloaded-mark
5246                                              (gnus-data-mark (car arts))))
5247                                     (gnus-data-unread-p (car arts)))
5248                             (setq result (car arts)
5249                                   arts nil))
5250                           (setq arts (cdr arts)))
5251                         result)
5252                     (car arts)))
5253         (goto-char (gnus-data-pos result))
5254         (gnus-data-number result)))))
5255
5256 (defun gnus-summary-find-prev (&optional unread article)
5257   (let* ((eobp (eobp))
5258          (article (or article (gnus-summary-article-number)))
5259          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5260          result)
5261     (when (and (not eobp)
5262                (or (not gnus-summary-check-current)
5263                    (not unread)
5264                    (not (gnus-data-unread-p (car arts)))))
5265       (setq arts (cdr arts)))
5266     (when (setq result
5267                 (if unread
5268                     (progn
5269                       (while arts
5270                         (when (gnus-data-unread-p (car arts))
5271                           (setq result (car arts)
5272                                 arts nil))
5273                         (setq arts (cdr arts)))
5274                       result)
5275                   (car arts)))
5276       (goto-char (gnus-data-pos result))
5277       (gnus-data-number result))))
5278
5279 (defun gnus-summary-find-subject (subject &optional unread backward article)
5280   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5281          (article (or article (gnus-summary-article-number)))
5282          (articles (gnus-data-list backward))
5283          (arts (gnus-data-find-list article articles))
5284          result)
5285     (when (or (not gnus-summary-check-current)
5286               (not unread)
5287               (not (gnus-data-unread-p (car arts))))
5288       (setq arts (cdr arts)))
5289     (while arts
5290       (and (or (not unread)
5291                (gnus-data-unread-p (car arts)))
5292            (vectorp (gnus-data-header (car arts)))
5293            (gnus-subject-equal
5294             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5295            (setq result (car arts)
5296                  arts nil))
5297       (setq arts (cdr arts)))
5298     (and result
5299          (goto-char (gnus-data-pos result))
5300          (gnus-data-number result))))
5301
5302 (defun gnus-summary-search-forward (&optional unread subject backward)
5303   "Search forward for an article.
5304 If UNREAD, look for unread articles.  If SUBJECT, look for
5305 articles with that subject.  If BACKWARD, search backward instead."
5306   (cond (subject (gnus-summary-find-subject subject unread backward))
5307         (backward (gnus-summary-find-prev unread))
5308         (t (gnus-summary-find-next unread))))
5309
5310 (defun gnus-recenter (&optional n)
5311   "Center point in window and redisplay frame.
5312 Also do horizontal recentering."
5313   (interactive "P")
5314   (when (and gnus-auto-center-summary
5315              (not (eq gnus-auto-center-summary 'vertical)))
5316     (gnus-horizontal-recenter))
5317   (recenter n))
5318
5319 (defun gnus-summary-recenter ()
5320   "Center point in the summary window.
5321 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5322 displayed, no centering will be performed."
5323   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5324   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5325   (interactive)
5326   (let* ((top (cond ((< (window-height) 4) 0)
5327                     ((< (window-height) 7) 1)
5328                     (t (if (numberp gnus-auto-center-summary)
5329                            gnus-auto-center-summary
5330                          2))))
5331          (height (1- (window-height)))
5332          (bottom (save-excursion (goto-char (point-max))
5333                                  (forward-line (- height))
5334                                  (point)))
5335          (window (get-buffer-window (current-buffer))))
5336     ;; The user has to want it.
5337     (when gnus-auto-center-summary
5338       (when (get-buffer-window gnus-article-buffer)
5339         ;; Only do recentering when the article buffer is displayed,
5340         ;; Set the window start to either `bottom', which is the biggest
5341         ;; possible valid number, or the second line from the top,
5342         ;; whichever is the least.
5343         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5344           (if (> bottom top-pos)
5345               ;; Keep the second line from the top visible
5346               (set-window-start window top-pos t)
5347             ;; Try to keep the bottom line visible; if it's partially
5348             ;; obscured, either scroll one more line to make it fully
5349             ;; visible, or revert to using TOP-POS.
5350             (save-excursion
5351               (goto-char (point-max))
5352               (forward-line -1)
5353               (let ((last-line-start (point)))
5354                 (goto-char bottom)
5355                 (set-window-start window (point) t)
5356                 (when (not (pos-visible-in-window-p last-line-start window))
5357                   (forward-line 1)
5358                   (set-window-start window (min (point) top-pos) t)))))))
5359       ;; Do horizontal recentering while we're at it.
5360       (when (and (get-buffer-window (current-buffer) t)
5361                  (not (eq gnus-auto-center-summary 'vertical)))
5362         (let ((selected (selected-window)))
5363           (select-window (get-buffer-window (current-buffer) t))
5364           (gnus-summary-position-point)
5365           (gnus-horizontal-recenter)
5366           (select-window selected))))))
5367
5368 (defun gnus-summary-jump-to-group (newsgroup)
5369   "Move point to NEWSGROUP in group mode buffer."
5370   ;; Keep update point of group mode buffer if visible.
5371   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5372       (save-window-excursion
5373         ;; Take care of tree window mode.
5374         (when (get-buffer-window gnus-group-buffer)
5375           (pop-to-buffer gnus-group-buffer))
5376         (gnus-group-jump-to-group newsgroup))
5377     (save-excursion
5378       ;; Take care of tree window mode.
5379       (if (get-buffer-window gnus-group-buffer)
5380           (pop-to-buffer gnus-group-buffer)
5381         (set-buffer gnus-group-buffer))
5382       (gnus-group-jump-to-group newsgroup))))
5383
5384 ;; This function returns a list of article numbers based on the
5385 ;; difference between the ranges of read articles in this group and
5386 ;; the range of active articles.
5387 (defun gnus-list-of-unread-articles (group)
5388   (let* ((read (gnus-info-read (gnus-get-info group)))
5389          (active (or (gnus-active group) (gnus-activate-group group)))
5390          (last (cdr active))
5391          first nlast unread)
5392     ;; If none are read, then all are unread.
5393     (if (not read)
5394         (setq first (car active))
5395       ;; If the range of read articles is a single range, then the
5396       ;; first unread article is the article after the last read
5397       ;; article.  Sounds logical, doesn't it?
5398       (if (and (not (listp (cdr read)))
5399                (or (< (car read) (car active))
5400                    (progn (setq read (list read))
5401                           nil)))
5402           (setq first (max (car active) (1+ (cdr read))))
5403         ;; `read' is a list of ranges.
5404         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5405                                   (caar read)))
5406                   1)
5407           (setq first (car active)))
5408         (while read
5409           (when first
5410             (while (< first nlast)
5411               (push first unread)
5412               (setq first (1+ first))))
5413           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5414           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5415           (setq read (cdr read)))))
5416     ;; And add the last unread articles.
5417     (while (<= first last)
5418       (push first unread)
5419       (setq first (1+ first)))
5420     ;; Return the list of unread articles.
5421     (delq 0 (nreverse unread))))
5422
5423 (defun gnus-list-of-read-articles (group)
5424   "Return a list of unread, unticked and non-dormant articles."
5425   (let* ((info (gnus-get-info group))
5426          (marked (gnus-info-marks info))
5427          (active (gnus-active group)))
5428     (and info active
5429          (gnus-set-difference
5430           (gnus-sorted-complement
5431            (gnus-uncompress-range active)
5432            (gnus-list-of-unread-articles group))
5433           (append
5434            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5435            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5436
5437 ;; Various summary commands
5438
5439 (defun gnus-summary-select-article-buffer ()
5440   "Reconfigure windows to show article buffer."
5441   (interactive)
5442   (if (not (gnus-buffer-live-p gnus-article-buffer))
5443       (error "There is no article buffer for this summary buffer")
5444     (gnus-configure-windows 'article)
5445     (select-window (get-buffer-window gnus-article-buffer))))
5446
5447 (defun gnus-summary-universal-argument (arg)
5448   "Perform any operation on all articles that are process/prefixed."
5449   (interactive "P")
5450   (let ((articles (gnus-summary-work-articles arg))
5451         func article)
5452     (if (eq
5453          (setq
5454           func
5455           (key-binding
5456            (read-key-sequence
5457             (substitute-command-keys
5458              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5459          'undefined)
5460         (gnus-error 1 "Undefined key")
5461       (save-excursion
5462         (while articles
5463           (gnus-summary-goto-subject (setq article (pop articles)))
5464           (let (gnus-newsgroup-processable)
5465             (command-execute func))
5466           (gnus-summary-remove-process-mark article)))))
5467   (gnus-summary-position-point))
5468
5469 (defun gnus-summary-toggle-truncation (&optional arg)
5470   "Toggle truncation of summary lines.
5471 With arg, turn line truncation on iff arg is positive."
5472   (interactive "P")
5473   (setq truncate-lines
5474         (if (null arg) (not truncate-lines)
5475           (> (prefix-numeric-value arg) 0)))
5476   (redraw-display))
5477
5478 (defun gnus-summary-reselect-current-group (&optional all rescan)
5479   "Exit and then reselect the current newsgroup.
5480 The prefix argument ALL means to select all articles."
5481   (interactive "P")
5482   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5483     (error "Ephemeral groups can't be reselected"))
5484   (let ((current-subject (gnus-summary-article-number))
5485         (group gnus-newsgroup-name))
5486     (setq gnus-newsgroup-begin nil)
5487     (gnus-summary-exit)
5488     ;; We have to adjust the point of group mode buffer because
5489     ;; point was moved to the next unread newsgroup by exiting.
5490     (gnus-summary-jump-to-group group)
5491     (when rescan
5492       (save-excursion
5493         (gnus-group-get-new-news-this-group 1)))
5494     (gnus-group-read-group all t)
5495     (gnus-summary-goto-subject current-subject nil t)))
5496
5497 (defun gnus-summary-rescan-group (&optional all)
5498   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5499   (interactive "P")
5500   (gnus-summary-reselect-current-group all t))
5501
5502 (defun gnus-summary-update-info (&optional non-destructive)
5503   (save-excursion
5504     (let ((group gnus-newsgroup-name))
5505       (when group
5506         (when gnus-newsgroup-kill-headers
5507           (setq gnus-newsgroup-killed
5508                 (gnus-compress-sequence
5509                  (nconc
5510                   (gnus-set-sorted-intersection
5511                    (gnus-uncompress-range gnus-newsgroup-killed)
5512                    (setq gnus-newsgroup-unselected
5513                          (sort gnus-newsgroup-unselected '<)))
5514                   (setq gnus-newsgroup-unreads
5515                         (sort gnus-newsgroup-unreads '<)))
5516                  t)))
5517         (unless (listp (cdr gnus-newsgroup-killed))
5518           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5519         (let ((headers gnus-newsgroup-headers))
5520           ;; Set the new ranges of read articles.
5521           (save-excursion
5522             (set-buffer gnus-group-buffer)
5523             (gnus-undo-force-boundary))
5524           (gnus-update-read-articles
5525            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5526           ;; Set the current article marks.
5527           (let ((gnus-newsgroup-scored
5528                  (if (and (not gnus-save-score)
5529                           (not non-destructive))
5530                      nil
5531                    gnus-newsgroup-scored)))
5532             (save-excursion
5533               (gnus-update-marks)))
5534           ;; Do the cross-ref thing.
5535           (when gnus-use-cross-reference
5536             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5537           ;; Do not switch windows but change the buffer to work.
5538           (set-buffer gnus-group-buffer)
5539           (unless (gnus-ephemeral-group-p group)
5540             (gnus-group-update-group group)))))))
5541
5542 (defun gnus-summary-save-newsrc (&optional force)
5543   "Save the current number of read/marked articles in the dribble buffer.
5544 The dribble buffer will then be saved.
5545 If FORCE (the prefix), also save the .newsrc file(s)."
5546   (interactive "P")
5547   (gnus-summary-update-info t)
5548   (if force
5549       (gnus-save-newsrc-file)
5550     (gnus-dribble-save)))
5551
5552 (defun gnus-summary-exit (&optional temporary)
5553   "Exit reading current newsgroup, and then return to group selection mode.
5554 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5555   (interactive)
5556   (gnus-set-global-variables)
5557   (when (gnus-buffer-live-p gnus-article-buffer)
5558     (save-excursion
5559       (set-buffer gnus-article-buffer)
5560       (mm-destroy-parts gnus-article-mime-handles)
5561       ;; Set it to nil for safety reason.
5562       (setq gnus-article-mime-handle-alist nil)
5563       (setq gnus-article-mime-handles nil)))
5564   (gnus-kill-save-kill-buffer)
5565   (gnus-async-halt-prefetch)
5566   (let* ((group gnus-newsgroup-name)
5567          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5568          (mode major-mode)
5569          (group-point nil)
5570          (buf (current-buffer)))
5571     (unless quit-config
5572       ;; Do adaptive scoring, and possibly save score files.
5573       (when gnus-newsgroup-adaptive
5574         (gnus-score-adaptive))
5575       (when gnus-use-scoring
5576         (gnus-score-save)))
5577     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5578     ;; If we have several article buffers, we kill them at exit.
5579     (unless gnus-single-article-buffer
5580       (gnus-kill-buffer gnus-original-article-buffer)
5581       (setq gnus-article-current nil))
5582     (when gnus-use-cache
5583       (gnus-cache-possibly-remove-articles)
5584       (gnus-cache-save-buffers))
5585     (gnus-async-prefetch-remove-group group)
5586     (when gnus-suppress-duplicates
5587       (gnus-dup-enter-articles))
5588     (when gnus-use-trees
5589       (gnus-tree-close group))
5590     (when gnus-use-cache
5591       (gnus-cache-write-active))
5592     ;; Remove entries for this group.
5593     (nnmail-purge-split-history (gnus-group-real-name group))
5594     ;; Make all changes in this group permanent.
5595     (unless quit-config
5596       (gnus-run-hooks 'gnus-exit-group-hook)
5597       (gnus-summary-update-info))
5598     (gnus-close-group group)
5599     ;; Make sure where we were, and go to next newsgroup.
5600     (set-buffer gnus-group-buffer)
5601     (unless quit-config
5602       (gnus-group-jump-to-group group))
5603     (gnus-run-hooks 'gnus-summary-exit-hook)
5604     (unless (or quit-config
5605                 ;; If this group has disappeared from the summary
5606                 ;; buffer, don't skip forwards.
5607                 (not (string= group (gnus-group-group-name))))
5608       (gnus-group-next-unread-group 1))
5609     (setq group-point (point))
5610     (if temporary
5611         nil                             ;Nothing to do.
5612       ;; If we have several article buffers, we kill them at exit.
5613       (unless gnus-single-article-buffer
5614         (gnus-kill-buffer gnus-article-buffer)
5615         (gnus-kill-buffer gnus-original-article-buffer)
5616         (setq gnus-article-current nil))
5617       (set-buffer buf)
5618       (if (not gnus-kill-summary-on-exit)
5619           (gnus-deaden-summary)
5620         ;; We set all buffer-local variables to nil.  It is unclear why
5621         ;; this is needed, but if we don't, buffer-local variables are
5622         ;; not garbage-collected, it seems.  This would the lead to en
5623         ;; ever-growing Emacs.
5624         (gnus-summary-clear-local-variables)
5625         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5626           (gnus-summary-clear-local-variables))
5627         (when (get-buffer gnus-article-buffer)
5628           (bury-buffer gnus-article-buffer))
5629         ;; We clear the global counterparts of the buffer-local
5630         ;; variables as well, just to be on the safe side.
5631         (set-buffer gnus-group-buffer)
5632         (gnus-summary-clear-local-variables)
5633         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5634           (gnus-summary-clear-local-variables))
5635         ;; Return to group mode buffer.
5636         (when (eq mode 'gnus-summary-mode)
5637           (gnus-kill-buffer buf)))
5638       (setq gnus-current-select-method gnus-select-method)
5639       (pop-to-buffer gnus-group-buffer)
5640       (if (not quit-config)
5641           (progn
5642             (goto-char group-point)
5643             (gnus-configure-windows 'group 'force))
5644         (gnus-handle-ephemeral-exit quit-config))
5645       ;; Clear the current group name.
5646       (unless quit-config
5647         (setq gnus-newsgroup-name nil)))))
5648
5649 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5650 (defun gnus-summary-exit-no-update (&optional no-questions)
5651   "Quit reading current newsgroup without updating read article info."
5652   (interactive)
5653   (let* ((group gnus-newsgroup-name)
5654          (quit-config (gnus-group-quit-config group)))
5655     (when (or no-questions
5656               gnus-expert-user
5657               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5658       (gnus-async-halt-prefetch)
5659       (mapcar 'funcall
5660               (delq 'gnus-summary-expire-articles
5661                     (copy-sequence gnus-summary-prepare-exit-hook)))
5662       (when (gnus-buffer-live-p gnus-article-buffer)
5663         (save-excursion
5664           (set-buffer gnus-article-buffer)
5665           (mm-destroy-parts gnus-article-mime-handles)
5666           ;; Set it to nil for safety reason.
5667           (setq gnus-article-mime-handle-alist nil)
5668           (setq gnus-article-mime-handles nil)))
5669       ;; If we have several article buffers, we kill them at exit.
5670       (unless gnus-single-article-buffer
5671         (gnus-kill-buffer gnus-article-buffer)
5672         (gnus-kill-buffer gnus-original-article-buffer)
5673         (setq gnus-article-current nil))
5674       (if (not gnus-kill-summary-on-exit)
5675           (gnus-deaden-summary)
5676         (gnus-close-group group)
5677         (gnus-summary-clear-local-variables)
5678         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5679           (gnus-summary-clear-local-variables))
5680         (set-buffer gnus-group-buffer)
5681         (gnus-summary-clear-local-variables)
5682         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5683           (gnus-summary-clear-local-variables))
5684         (when (get-buffer gnus-summary-buffer)
5685           (kill-buffer gnus-summary-buffer)))
5686       (unless gnus-single-article-buffer
5687         (setq gnus-article-current nil))
5688       (when gnus-use-trees
5689         (gnus-tree-close group))
5690       (gnus-async-prefetch-remove-group group)
5691       (when (get-buffer gnus-article-buffer)
5692         (bury-buffer gnus-article-buffer))
5693       ;; Return to the group buffer.
5694       (gnus-configure-windows 'group 'force)
5695       ;; Clear the current group name.
5696       (setq gnus-newsgroup-name nil)
5697       (when (equal (gnus-group-group-name) group)
5698         (gnus-group-next-unread-group 1))
5699       (when quit-config
5700         (gnus-handle-ephemeral-exit quit-config)))))
5701
5702 (defun gnus-handle-ephemeral-exit (quit-config)
5703   "Handle movement when leaving an ephemeral group.
5704 The state which existed when entering the ephemeral is reset."
5705   (if (not (buffer-name (car quit-config)))
5706       (gnus-configure-windows 'group 'force)
5707     (set-buffer (car quit-config))
5708     (cond ((eq major-mode 'gnus-summary-mode)
5709            (gnus-set-global-variables))
5710           ((eq major-mode 'gnus-article-mode)
5711            (save-excursion
5712              ;; The `gnus-summary-buffer' variable may point
5713              ;; to the old summary buffer when using a single
5714              ;; article buffer.
5715              (unless (gnus-buffer-live-p gnus-summary-buffer)
5716                (set-buffer gnus-group-buffer))
5717              (set-buffer gnus-summary-buffer)
5718              (gnus-set-global-variables))))
5719     (if (or (eq (cdr quit-config) 'article)
5720             (eq (cdr quit-config) 'pick))
5721         (progn
5722           ;; The current article may be from the ephemeral group
5723           ;; thus it is best that we reload this article
5724           (gnus-summary-show-article)
5725           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5726               (gnus-configure-windows 'pick 'force)
5727             (gnus-configure-windows (cdr quit-config) 'force)))
5728       (gnus-configure-windows (cdr quit-config) 'force))
5729     (when (eq major-mode 'gnus-summary-mode)
5730       (gnus-summary-next-subject 1 nil t)
5731       (gnus-summary-recenter)
5732       (gnus-summary-position-point))))
5733
5734 ;;; Dead summaries.
5735
5736 (defvar gnus-dead-summary-mode-map nil)
5737
5738 (unless gnus-dead-summary-mode-map
5739   (setq gnus-dead-summary-mode-map (make-keymap))
5740   (suppress-keymap gnus-dead-summary-mode-map)
5741   (substitute-key-definition
5742    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5743   (let ((keys '("\C-d" "\r" "\177" [delete])))
5744     (while keys
5745       (define-key gnus-dead-summary-mode-map
5746         (pop keys) 'gnus-summary-wake-up-the-dead))))
5747
5748 (defvar gnus-dead-summary-mode nil
5749   "Minor mode for Gnus summary buffers.")
5750
5751 (defun gnus-dead-summary-mode (&optional arg)
5752   "Minor mode for Gnus summary buffers."
5753   (interactive "P")
5754   (when (eq major-mode 'gnus-summary-mode)
5755     (make-local-variable 'gnus-dead-summary-mode)
5756     (setq gnus-dead-summary-mode
5757           (if (null arg) (not gnus-dead-summary-mode)
5758             (> (prefix-numeric-value arg) 0)))
5759     (when gnus-dead-summary-mode
5760       (gnus-add-minor-mode
5761        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5762
5763 (defun gnus-deaden-summary ()
5764   "Make the current summary buffer into a dead summary buffer."
5765   ;; Kill any previous dead summary buffer.
5766   (when (and gnus-dead-summary
5767              (buffer-name gnus-dead-summary))
5768     (save-excursion
5769       (set-buffer gnus-dead-summary)
5770       (when gnus-dead-summary-mode
5771         (kill-buffer (current-buffer)))))
5772   ;; Make this the current dead summary.
5773   (setq gnus-dead-summary (current-buffer))
5774   (gnus-dead-summary-mode 1)
5775   (let ((name (buffer-name)))
5776     (when (string-match "Summary" name)
5777       (rename-buffer
5778        (concat (substring name 0 (match-beginning 0)) "Dead "
5779                (substring name (match-beginning 0)))
5780        t)
5781       (bury-buffer))))
5782
5783 (defun gnus-kill-or-deaden-summary (buffer)
5784   "Kill or deaden the summary BUFFER."
5785   (save-excursion
5786     (when (and (buffer-name buffer)
5787                (not gnus-single-article-buffer))
5788       (save-excursion
5789         (set-buffer buffer)
5790         (gnus-kill-buffer gnus-article-buffer)
5791         (gnus-kill-buffer gnus-original-article-buffer)))
5792     (cond (gnus-kill-summary-on-exit
5793            (when (and gnus-use-trees
5794                       (gnus-buffer-exists-p buffer))
5795              (save-excursion
5796                (set-buffer buffer)
5797                (gnus-tree-close gnus-newsgroup-name)))
5798            (gnus-kill-buffer buffer))
5799           ((gnus-buffer-exists-p buffer)
5800            (save-excursion
5801              (set-buffer buffer)
5802              (gnus-deaden-summary))))))
5803
5804 (defun gnus-summary-wake-up-the-dead (&rest args)
5805   "Wake up the dead summary buffer."
5806   (interactive)
5807   (gnus-dead-summary-mode -1)
5808   (let ((name (buffer-name)))
5809     (when (string-match "Dead " name)
5810       (rename-buffer
5811        (concat (substring name 0 (match-beginning 0))
5812                (substring name (match-end 0)))
5813        t)))
5814   (gnus-message 3 "This dead summary is now alive again"))
5815
5816 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5817 (defun gnus-summary-fetch-faq (&optional faq-dir)
5818   "Fetch the FAQ for the current group.
5819 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5820 in."
5821   (interactive
5822    (list
5823     (when current-prefix-arg
5824       (completing-read
5825        "Faq dir: " (and (listp gnus-group-faq-directory)
5826                         (mapcar (lambda (file) (list file))
5827                                 gnus-group-faq-directory))))))
5828   (let (gnus-faq-buffer)
5829     (when (setq gnus-faq-buffer
5830                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5831       (gnus-configure-windows 'summary-faq))))
5832
5833 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5834 (defun gnus-summary-describe-group (&optional force)
5835   "Describe the current newsgroup."
5836   (interactive "P")
5837   (gnus-group-describe-group force gnus-newsgroup-name))
5838
5839 (defun gnus-summary-describe-briefly ()
5840   "Describe summary mode commands briefly."
5841   (interactive)
5842   (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")))
5843
5844 ;; Walking around group mode buffer from summary mode.
5845
5846 (defun gnus-summary-next-group (&optional no-article target-group backward)
5847   "Exit current newsgroup and then select next unread newsgroup.
5848 If prefix argument NO-ARTICLE is non-nil, no article is selected
5849 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5850 previous group instead."
5851   (interactive "P")
5852   ;; Stop pre-fetching.
5853   (gnus-async-halt-prefetch)
5854   (let ((current-group gnus-newsgroup-name)
5855         (current-buffer (current-buffer))
5856         entered)
5857     ;; First we semi-exit this group to update Xrefs and all variables.
5858     ;; We can't do a real exit, because the window conf must remain
5859     ;; the same in case the user is prompted for info, and we don't
5860     ;; want the window conf to change before that...
5861     (gnus-summary-exit t)
5862     (while (not entered)
5863       ;; Then we find what group we are supposed to enter.
5864       (set-buffer gnus-group-buffer)
5865       (gnus-group-jump-to-group current-group)
5866       (setq target-group
5867             (or target-group
5868                 (if (eq gnus-keep-same-level 'best)
5869                     (gnus-summary-best-group gnus-newsgroup-name)
5870                   (gnus-summary-search-group backward gnus-keep-same-level))))
5871       (if (not target-group)
5872           ;; There are no further groups, so we return to the group
5873           ;; buffer.
5874           (progn
5875             (gnus-message 5 "Returning to the group buffer")
5876             (setq entered t)
5877             (when (gnus-buffer-live-p current-buffer)
5878               (set-buffer current-buffer)
5879               (gnus-summary-exit))
5880             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5881         ;; We try to enter the target group.
5882         (gnus-group-jump-to-group target-group)
5883         (let ((unreads (gnus-group-group-unread)))
5884           (if (and (or (eq t unreads)
5885                        (and unreads (not (zerop unreads))))
5886                    (gnus-summary-read-group
5887                     target-group nil no-article
5888                     (and (buffer-name current-buffer) current-buffer)
5889                     nil backward))
5890               (setq entered t)
5891             (setq current-group target-group
5892                   target-group nil)))))))
5893
5894 (defun gnus-summary-prev-group (&optional no-article)
5895   "Exit current newsgroup and then select previous unread newsgroup.
5896 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5897   (interactive "P")
5898   (gnus-summary-next-group no-article nil t))
5899
5900 ;; Walking around summary lines.
5901
5902 (defun gnus-summary-first-subject (&optional unread undownloaded)
5903   "Go to the first unread subject.
5904 If UNREAD is non-nil, go to the first unread article.
5905 Returns the article selected or nil if there are no unread articles."
5906   (interactive "P")
5907   (prog1
5908       (cond
5909        ;; Empty summary.
5910        ((null gnus-newsgroup-data)
5911         (gnus-message 3 "No articles in the group")
5912         nil)
5913        ;; Pick the first article.
5914        ((not unread)
5915         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5916         (gnus-data-number (car gnus-newsgroup-data)))
5917        ;; No unread articles.
5918        ((null gnus-newsgroup-unreads)
5919         (gnus-message 3 "No more unread articles")
5920         nil)
5921        ;; Find the first unread article.
5922        (t
5923         (let ((data gnus-newsgroup-data))
5924           (while (and data
5925                       (and (not (and undownloaded
5926                                      (eq gnus-undownloaded-mark
5927                                          (gnus-data-mark (car data)))))
5928                            (not (gnus-data-unread-p (car data)))))
5929             (setq data (cdr data)))
5930           (when data
5931             (goto-char (gnus-data-pos (car data)))
5932             (gnus-data-number (car data))))))
5933     (gnus-summary-position-point)))
5934
5935 (defun gnus-summary-next-subject (n &optional unread dont-display)
5936   "Go to next N'th summary line.
5937 If N is negative, go to the previous N'th subject line.
5938 If UNREAD is non-nil, only unread articles are selected.
5939 The difference between N and the actual number of steps taken is
5940 returned."
5941   (interactive "p")
5942   (let ((backward (< n 0))
5943         (n (abs n)))
5944     (while (and (> n 0)
5945                 (if backward
5946                     (gnus-summary-find-prev unread)
5947                   (gnus-summary-find-next unread)))
5948       (unless (zerop (setq n (1- n)))
5949         (gnus-summary-show-thread)))
5950     (when (/= 0 n)
5951       (gnus-message 7 "No more%s articles"
5952                     (if unread " unread" "")))
5953     (unless dont-display
5954       (gnus-summary-recenter)
5955       (gnus-summary-position-point))
5956     n))
5957
5958 (defun gnus-summary-next-unread-subject (n)
5959   "Go to next N'th unread summary line."
5960   (interactive "p")
5961   (gnus-summary-next-subject n t))
5962
5963 (defun gnus-summary-prev-subject (n &optional unread)
5964   "Go to previous N'th summary line.
5965 If optional argument UNREAD is non-nil, only unread article is selected."
5966   (interactive "p")
5967   (gnus-summary-next-subject (- n) unread))
5968
5969 (defun gnus-summary-prev-unread-subject (n)
5970   "Go to previous N'th unread summary line."
5971   (interactive "p")
5972   (gnus-summary-next-subject (- n) t))
5973
5974 (defun gnus-summary-goto-subject (article &optional force silent)
5975   "Go the subject line of ARTICLE.
5976 If FORCE, also allow jumping to articles not currently shown."
5977   (interactive "nArticle number: ")
5978   (let ((b (point))
5979         (data (gnus-data-find article)))
5980     ;; We read in the article if we have to.
5981     (and (not data)
5982          force
5983          (gnus-summary-insert-subject
5984           article
5985           (if (or (numberp force) (vectorp force)) force)
5986           t)
5987          (setq data (gnus-data-find article)))
5988     (goto-char b)
5989     (if (not data)
5990         (progn
5991           (unless silent
5992             (gnus-message 3 "Can't find article %d" article))
5993           nil)
5994       (goto-char (gnus-data-pos data))
5995       (gnus-summary-position-point)
5996       article)))
5997
5998 ;; Walking around summary lines with displaying articles.
5999
6000 (defun gnus-summary-expand-window (&optional arg)
6001   "Make the summary buffer take up the entire Emacs frame.
6002 Given a prefix, will force an `article' buffer configuration."
6003   (interactive "P")
6004   (if arg
6005       (gnus-configure-windows 'article 'force)
6006     (gnus-configure-windows 'summary 'force)))
6007
6008 (defun gnus-summary-display-article (article &optional all-header)
6009   "Display ARTICLE in article buffer."
6010   (when (gnus-buffer-live-p gnus-article-buffer)
6011     (with-current-buffer gnus-article-buffer
6012       (mm-enable-multibyte-mule4)))
6013   (gnus-set-global-variables)
6014   (when (gnus-buffer-live-p gnus-article-buffer)
6015     (with-current-buffer gnus-article-buffer
6016       (setq gnus-article-charset gnus-newsgroup-charset)
6017       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6018       (mm-enable-multibyte-mule4)))
6019   (if (null article)
6020       nil
6021     (prog1
6022         (if gnus-summary-display-article-function
6023             (funcall gnus-summary-display-article-function article all-header)
6024           (gnus-article-prepare article all-header))
6025       (gnus-run-hooks 'gnus-select-article-hook)
6026       (when (and gnus-current-article
6027                  (not (zerop gnus-current-article)))
6028         (gnus-summary-goto-subject gnus-current-article))
6029       (gnus-summary-recenter)
6030       (when (and gnus-use-trees gnus-show-threads)
6031         (gnus-possibly-generate-tree article)
6032         (gnus-highlight-selected-tree article))
6033       ;; Successfully display article.
6034       (gnus-article-set-window-start
6035        (cdr (assq article gnus-newsgroup-bookmarks))))))
6036
6037 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6038   "Select the current article.
6039 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6040 non-nil, the article will be re-fetched even if it already present in
6041 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6042 be displayed."
6043   ;; Make sure we are in the summary buffer to work around bbdb bug.
6044   (unless (eq major-mode 'gnus-summary-mode)
6045     (set-buffer gnus-summary-buffer))
6046   (let ((article (or article (gnus-summary-article-number)))
6047         (all-headers (not (not all-headers))) ;Must be T or NIL.
6048         gnus-summary-display-article-function)
6049     (and (not pseudo)
6050          (gnus-summary-article-pseudo-p article)
6051          (error "This is a pseudo-article"))
6052     (save-excursion
6053       (set-buffer gnus-summary-buffer)
6054       (if (or (and gnus-single-article-buffer
6055                    (or (null gnus-current-article)
6056                        (null gnus-article-current)
6057                        (null (get-buffer gnus-article-buffer))
6058                        (not (eq article (cdr gnus-article-current)))
6059                        (not (equal (car gnus-article-current)
6060                                    gnus-newsgroup-name))))
6061               (and (not gnus-single-article-buffer)
6062                    (or (null gnus-current-article)
6063                        (not (eq gnus-current-article article))))
6064               force)
6065           ;; The requested article is different from the current article.
6066           (progn
6067             (gnus-summary-display-article article all-headers)
6068             (when (gnus-buffer-live-p gnus-article-buffer)
6069                (with-current-buffer gnus-article-buffer
6070                 (if (not gnus-article-decoded-p) ;; a local variable
6071                     (mm-disable-multibyte-mule4))))
6072             (when (or all-headers gnus-show-all-headers)
6073               (gnus-article-show-all-headers))
6074             (gnus-article-set-window-start
6075              (cdr (assq article gnus-newsgroup-bookmarks)))
6076             article)
6077         (when (or all-headers gnus-show-all-headers)
6078           (gnus-article-show-all-headers))
6079         'old))))
6080
6081 (defun gnus-summary-force-verify-and-decrypt ()
6082   (interactive)
6083   (let ((mm-verify-option 'known)
6084         (mm-decrypt-option 'known))
6085     (gnus-summary-select-article nil 'force)))
6086
6087 (defun gnus-summary-set-current-mark (&optional current-mark)
6088   "Obsolete function."
6089   nil)
6090
6091 (defun gnus-summary-next-article (&optional unread subject backward push)
6092   "Select the next article.
6093 If UNREAD, only unread articles are selected.
6094 If SUBJECT, only articles with SUBJECT are selected.
6095 If BACKWARD, the previous article is selected instead of the next."
6096   (interactive "P")
6097   (cond
6098    ;; Is there such an article?
6099    ((and (gnus-summary-search-forward unread subject backward)
6100          (or (gnus-summary-display-article (gnus-summary-article-number))
6101              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6102     (gnus-summary-position-point))
6103    ;; If not, we try the first unread, if that is wanted.
6104    ((and subject
6105          gnus-auto-select-same
6106          (gnus-summary-first-unread-article))
6107     (gnus-summary-position-point)
6108     (gnus-message 6 "Wrapped"))
6109    ;; Try to get next/previous article not displayed in this group.
6110    ((and gnus-auto-extend-newsgroup
6111          (not unread) (not subject))
6112     (gnus-summary-goto-article
6113      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6114      nil (count-lines (point-min) (point))))
6115    ;; Go to next/previous group.
6116    (t
6117     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6118       (gnus-summary-jump-to-group gnus-newsgroup-name))
6119     (let ((cmd last-command-char)
6120           (point
6121            (save-excursion
6122              (set-buffer gnus-group-buffer)
6123              (point)))
6124           (group
6125            (if (eq gnus-keep-same-level 'best)
6126                (gnus-summary-best-group gnus-newsgroup-name)
6127              (gnus-summary-search-group backward gnus-keep-same-level))))
6128       ;; For some reason, the group window gets selected.  We change
6129       ;; it back.
6130       (select-window (get-buffer-window (current-buffer)))
6131       ;; Select next unread newsgroup automagically.
6132       (cond
6133        ((or (not gnus-auto-select-next)
6134             (not cmd))
6135         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6136        ((or (eq gnus-auto-select-next 'quietly)
6137             (and (eq gnus-auto-select-next 'slightly-quietly)
6138                  push)
6139             (and (eq gnus-auto-select-next 'almost-quietly)
6140                  (gnus-summary-last-article-p)))
6141         ;; Select quietly.
6142         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6143             (gnus-summary-exit)
6144           (gnus-message 7 "No more%s articles (%s)..."
6145                         (if unread " unread" "")
6146                         (if group (concat "selecting " group)
6147                           "exiting"))
6148           (gnus-summary-next-group nil group backward)))
6149        (t
6150         (when (gnus-key-press-event-p last-input-event)
6151           (gnus-summary-walk-group-buffer
6152            gnus-newsgroup-name cmd unread backward point))))))))
6153
6154 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6155   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6156                       (?\C-p (gnus-group-prev-unread-group 1))))
6157         (cursor-in-echo-area t)
6158         keve key group ended)
6159     (save-excursion
6160       (set-buffer gnus-group-buffer)
6161       (goto-char start)
6162       (setq group
6163             (if (eq gnus-keep-same-level 'best)
6164                 (gnus-summary-best-group gnus-newsgroup-name)
6165               (gnus-summary-search-group backward gnus-keep-same-level))))
6166     (while (not ended)
6167       (gnus-message
6168        5 "No more%s articles%s" (if unread " unread" "")
6169        (if (and group
6170                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6171            (format " (Type %s for %s [%s])"
6172                    (single-key-description cmd) group
6173                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6174          (format " (Type %s to exit %s)"
6175                  (single-key-description cmd)
6176                  gnus-newsgroup-name)))
6177       ;; Confirm auto selection.
6178       (setq key (car (setq keve (gnus-read-event-char))))
6179       (setq ended t)
6180       (cond
6181        ((assq key keystrokes)
6182         (let ((obuf (current-buffer)))
6183           (switch-to-buffer gnus-group-buffer)
6184           (when group
6185             (gnus-group-jump-to-group group))
6186           (eval (cadr (assq key keystrokes)))
6187           (setq group (gnus-group-group-name))
6188           (switch-to-buffer obuf))
6189         (setq ended nil))
6190        ((equal key cmd)
6191         (if (or (not group)
6192                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6193             (gnus-summary-exit)
6194           (gnus-summary-next-group nil group backward)))
6195        (t
6196         (push (cdr keve) unread-command-events))))))
6197
6198 (defun gnus-summary-next-unread-article ()
6199   "Select unread article after current one."
6200   (interactive)
6201   (gnus-summary-next-article
6202    (or (not (eq gnus-summary-goto-unread 'never))
6203        (gnus-summary-last-article-p (gnus-summary-article-number)))
6204    (and gnus-auto-select-same
6205         (gnus-summary-article-subject))))
6206
6207 (defun gnus-summary-prev-article (&optional unread subject)
6208   "Select the article after the current one.
6209 If UNREAD is non-nil, only unread articles are selected."
6210   (interactive "P")
6211   (gnus-summary-next-article unread subject t))
6212
6213 (defun gnus-summary-prev-unread-article ()
6214   "Select unread article before current one."
6215   (interactive)
6216   (gnus-summary-prev-article
6217    (or (not (eq gnus-summary-goto-unread 'never))
6218        (gnus-summary-first-article-p (gnus-summary-article-number)))
6219    (and gnus-auto-select-same
6220         (gnus-summary-article-subject))))
6221
6222 (defun gnus-summary-next-page (&optional lines circular)
6223   "Show next page of the selected article.
6224 If at the end of the current article, select the next article.
6225 LINES says how many lines should be scrolled up.
6226
6227 If CIRCULAR is non-nil, go to the start of the article instead of
6228 selecting the next article when reaching the end of the current
6229 article."
6230   (interactive "P")
6231   (setq gnus-summary-buffer (current-buffer))
6232   (gnus-set-global-variables)
6233   (let ((article (gnus-summary-article-number))
6234         (article-window (get-buffer-window gnus-article-buffer t))
6235         endp)
6236     ;; If the buffer is empty, we have no article.
6237     (unless article
6238       (error "No article to select"))
6239     (gnus-configure-windows 'article)
6240     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6241         (if (and (eq gnus-summary-goto-unread 'never)
6242                  (not (gnus-summary-last-article-p article)))
6243             (gnus-summary-next-article)
6244           (gnus-summary-next-unread-article))
6245       (if (or (null gnus-current-article)
6246               (null gnus-article-current)
6247               (/= article (cdr gnus-article-current))
6248               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6249           ;; Selected subject is different from current article's.
6250           (gnus-summary-display-article article)
6251         (when article-window
6252           (gnus-eval-in-buffer-window gnus-article-buffer
6253             (setq endp (gnus-article-next-page lines)))
6254           (when endp
6255             (cond (circular
6256                    (gnus-summary-beginning-of-article))
6257                   (lines
6258                    (gnus-message 3 "End of message"))
6259                   ((null lines)
6260                    (if (and (eq gnus-summary-goto-unread 'never)
6261                             (not (gnus-summary-last-article-p article)))
6262                        (gnus-summary-next-article)
6263                      (gnus-summary-next-unread-article))))))))
6264     (gnus-summary-recenter)
6265     (gnus-summary-position-point)))
6266
6267 (defun gnus-summary-prev-page (&optional lines move)
6268   "Show previous page of selected article.
6269 Argument LINES specifies lines to be scrolled down.
6270 If MOVE, move to the previous unread article if point is at
6271 the beginning of the buffer."
6272   (interactive "P")
6273   (let ((article (gnus-summary-article-number))
6274         (article-window (get-buffer-window gnus-article-buffer t))
6275         endp)
6276     (gnus-configure-windows 'article)
6277     (if (or (null gnus-current-article)
6278             (null gnus-article-current)
6279             (/= article (cdr gnus-article-current))
6280             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6281         ;; Selected subject is different from current article's.
6282         (gnus-summary-display-article article)
6283       (gnus-summary-recenter)
6284       (when article-window
6285         (gnus-eval-in-buffer-window gnus-article-buffer
6286           (setq endp (gnus-article-prev-page lines)))
6287         (when (and move endp)
6288           (cond (lines
6289                  (gnus-message 3 "Beginning of message"))
6290                 ((null lines)
6291                  (if (and (eq gnus-summary-goto-unread 'never)
6292                           (not (gnus-summary-first-article-p article)))
6293                      (gnus-summary-prev-article)
6294                    (gnus-summary-prev-unread-article))))))))
6295   (gnus-summary-position-point))
6296
6297 (defun gnus-summary-prev-page-or-article (&optional lines)
6298   "Show previous page of selected article.
6299 Argument LINES specifies lines to be scrolled down.
6300 If at the beginning of the article, go to the next article."
6301   (interactive "P")
6302   (gnus-summary-prev-page lines t))
6303
6304 (defun gnus-summary-scroll-up (lines)
6305   "Scroll up (or down) one line current article.
6306 Argument LINES specifies lines to be scrolled up (or down if negative)."
6307   (interactive "p")
6308   (gnus-configure-windows 'article)
6309   (gnus-summary-show-thread)
6310   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6311     (gnus-eval-in-buffer-window gnus-article-buffer
6312       (cond ((> lines 0)
6313              (when (gnus-article-next-page lines)
6314                (gnus-message 3 "End of message")))
6315             ((< lines 0)
6316              (gnus-article-prev-page (- lines))))))
6317   (gnus-summary-recenter)
6318   (gnus-summary-position-point))
6319
6320 (defun gnus-summary-scroll-down (lines)
6321   "Scroll down (or up) one line current article.
6322 Argument LINES specifies lines to be scrolled down (or up if negative)."
6323   (interactive "p")
6324   (gnus-summary-scroll-up (- lines)))
6325
6326 (defun gnus-summary-next-same-subject ()
6327   "Select next article which has the same subject as current one."
6328   (interactive)
6329   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6330
6331 (defun gnus-summary-prev-same-subject ()
6332   "Select previous article which has the same subject as current one."
6333   (interactive)
6334   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6335
6336 (defun gnus-summary-next-unread-same-subject ()
6337   "Select next unread article which has the same subject as current one."
6338   (interactive)
6339   (gnus-summary-next-article t (gnus-summary-article-subject)))
6340
6341 (defun gnus-summary-prev-unread-same-subject ()
6342   "Select previous unread article which has the same subject as current one."
6343   (interactive)
6344   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6345
6346 (defun gnus-summary-first-unread-article ()
6347   "Select the first unread article.
6348 Return nil if there are no unread articles."
6349   (interactive)
6350   (prog1
6351       (when (gnus-summary-first-subject t)
6352         (gnus-summary-show-thread)
6353         (gnus-summary-first-subject t)
6354         (gnus-summary-display-article (gnus-summary-article-number)))
6355     (gnus-summary-position-point)))
6356
6357 (defun gnus-summary-first-unread-subject ()
6358   "Place the point on the subject line of the first unread article.
6359 Return nil if there are no unread articles."
6360   (interactive)
6361   (prog1
6362       (when (gnus-summary-first-subject t)
6363         (gnus-summary-show-thread)
6364         (gnus-summary-first-subject t))
6365     (gnus-summary-position-point)))
6366
6367 (defun gnus-summary-first-article ()
6368   "Select the first article.
6369 Return nil if there are no articles."
6370   (interactive)
6371   (prog1
6372       (when (gnus-summary-first-subject)
6373         (gnus-summary-show-thread)
6374         (gnus-summary-first-subject)
6375         (gnus-summary-display-article (gnus-summary-article-number)))
6376     (gnus-summary-position-point)))
6377
6378 (defun gnus-summary-best-unread-article ()
6379   "Select the unread article with the highest score."
6380   (interactive)
6381   (let ((best -1000000)
6382         (data gnus-newsgroup-data)
6383         article score)
6384     (while data
6385       (and (gnus-data-unread-p (car data))
6386            (> (setq score
6387                     (gnus-summary-article-score (gnus-data-number (car data))))
6388               best)
6389            (setq best score
6390                  article (gnus-data-number (car data))))
6391       (setq data (cdr data)))
6392     (prog1
6393         (if article
6394             (gnus-summary-goto-article article)
6395           (error "No unread articles"))
6396       (gnus-summary-position-point))))
6397
6398 (defun gnus-summary-last-subject ()
6399   "Go to the last displayed subject line in the group."
6400   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6401     (when article
6402       (gnus-summary-goto-subject article))))
6403
6404 (defun gnus-summary-goto-article (article &optional all-headers force)
6405   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6406 If ALL-HEADERS is non-nil, no header lines are hidden.
6407 If FORCE, go to the article even if it isn't displayed.  If FORCE
6408 is a number, it is the line the article is to be displayed on."
6409   (interactive
6410    (list
6411     (completing-read
6412      "Article number or Message-ID: "
6413      (mapcar (lambda (number) (list (int-to-string number)))
6414              gnus-newsgroup-limit))
6415     current-prefix-arg
6416     t))
6417   (prog1
6418       (if (and (stringp article)
6419                (string-match "@" article))
6420           (gnus-summary-refer-article article)
6421         (when (stringp article)
6422           (setq article (string-to-number article)))
6423         (if (gnus-summary-goto-subject article force)
6424             (gnus-summary-display-article article all-headers)
6425           (gnus-message 4 "Couldn't go to article %s" article) nil))
6426     (gnus-summary-position-point)))
6427
6428 (defun gnus-summary-goto-last-article ()
6429   "Go to the previously read article."
6430   (interactive)
6431   (prog1
6432       (when gnus-last-article
6433         (gnus-summary-goto-article gnus-last-article nil t))
6434     (gnus-summary-position-point)))
6435
6436 (defun gnus-summary-pop-article (number)
6437   "Pop one article off the history and go to the previous.
6438 NUMBER articles will be popped off."
6439   (interactive "p")
6440   (let (to)
6441     (setq gnus-newsgroup-history
6442           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6443     (if to
6444         (gnus-summary-goto-article (car to) nil t)
6445       (error "Article history empty")))
6446   (gnus-summary-position-point))
6447
6448 ;; Summary commands and functions for limiting the summary buffer.
6449
6450 (defun gnus-summary-limit-to-articles (n)
6451   "Limit the summary buffer to the next N articles.
6452 If not given a prefix, use the process marked articles instead."
6453   (interactive "P")
6454   (prog1
6455       (let ((articles (gnus-summary-work-articles n)))
6456         (setq gnus-newsgroup-processable nil)
6457         (gnus-summary-limit articles))
6458     (gnus-summary-position-point)))
6459
6460 (defun gnus-summary-pop-limit (&optional total)
6461   "Restore the previous limit.
6462 If given a prefix, remove all limits."
6463   (interactive "P")
6464   (when total
6465     (setq gnus-newsgroup-limits
6466           (list (mapcar (lambda (h) (mail-header-number h))
6467                         gnus-newsgroup-headers))))
6468   (unless gnus-newsgroup-limits
6469     (error "No limit to pop"))
6470   (prog1
6471       (gnus-summary-limit nil 'pop)
6472     (gnus-summary-position-point)))
6473
6474 (defun gnus-summary-limit-to-subject (subject &optional header)
6475   "Limit the summary buffer to articles that have subjects that match a regexp."
6476   (interactive "sLimit to subject (regexp): ")
6477   (unless header
6478     (setq header "subject"))
6479   (when (not (equal "" subject))
6480     (prog1
6481         (let ((articles (gnus-summary-find-matching
6482                          (or header "subject") subject 'all)))
6483           (unless articles
6484             (error "Found no matches for \"%s\"" subject))
6485           (gnus-summary-limit articles))
6486       (gnus-summary-position-point))))
6487
6488 (defun gnus-summary-limit-to-author (from)
6489   "Limit the summary buffer to articles that have authors that match a regexp."
6490   (interactive "sLimit to author (regexp): ")
6491   (gnus-summary-limit-to-subject from "from"))
6492
6493 (defun gnus-summary-limit-to-age (age &optional younger-p)
6494   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6495 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6496 articles that are younger than AGE days."
6497   (interactive
6498    (let ((younger current-prefix-arg)
6499          (days-got nil)
6500          days)
6501      (while (not days-got)
6502        (setq days (if younger
6503                       (read-string "Limit to articles within (in days): ")
6504                     (read-string "Limit to articles older than (in days): ")))
6505        (when (> (length days) 0)
6506          (setq days (read days)))
6507        (if (numberp days)
6508            (setq days-got t)
6509          (message "Please enter a number.")
6510          (sleep-for 1)))
6511      (list days younger)))
6512   (prog1
6513       (let ((data gnus-newsgroup-data)
6514             (cutoff (days-to-time age))
6515             articles d date is-younger)
6516         (while (setq d (pop data))
6517           (when (and (vectorp (gnus-data-header d))
6518                      (setq date (mail-header-date (gnus-data-header d))))
6519             (setq is-younger (time-less-p
6520                               (time-since (condition-case ()
6521                                               (date-to-time date)
6522                                             (error '(0 0))))
6523                               cutoff))
6524             (when (if younger-p
6525                       is-younger
6526                     (not is-younger))
6527               (push (gnus-data-number d) articles))))
6528         (gnus-summary-limit (nreverse articles)))
6529     (gnus-summary-position-point)))
6530
6531 (defun gnus-summary-limit-to-extra (header regexp)
6532   "Limit the summary buffer to articles that match an 'extra' header."
6533   (interactive
6534    (let ((header
6535           (intern
6536            (gnus-completing-read
6537             (symbol-name (car gnus-extra-headers))
6538             "Limit extra header:"
6539             (mapcar (lambda (x)
6540                       (cons (symbol-name x) x))
6541                     gnus-extra-headers)
6542             nil
6543             t))))
6544      (list header
6545            (read-string (format "Limit to header %s (regexp): " header)))))
6546   (when (not (equal "" regexp))
6547     (prog1
6548         (let ((articles (gnus-summary-find-matching
6549                          (cons 'extra header) regexp 'all)))
6550           (unless articles
6551             (error "Found no matches for \"%s\"" regexp))
6552           (gnus-summary-limit articles))
6553       (gnus-summary-position-point))))
6554
6555 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6556 (make-obsolete
6557  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6558
6559 (defun gnus-summary-limit-to-unread (&optional all)
6560   "Limit the summary buffer to articles that are not marked as read.
6561 If ALL is non-nil, limit strictly to unread articles."
6562   (interactive "P")
6563   (if all
6564       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6565     (gnus-summary-limit-to-marks
6566      ;; Concat all the marks that say that an article is read and have
6567      ;; those removed.
6568      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6569            gnus-killed-mark gnus-kill-file-mark
6570            gnus-low-score-mark gnus-expirable-mark
6571            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6572            gnus-duplicate-mark gnus-souped-mark)
6573      'reverse)))
6574
6575 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6576 (make-obsolete 'gnus-summary-delete-marked-with
6577                'gnus-summary-limit-exlude-marks)
6578
6579 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6580   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6581 If REVERSE, limit the summary buffer to articles that are marked
6582 with MARKS.  MARKS can either be a string of marks or a list of marks.
6583 Returns how many articles were removed."
6584   (interactive "sMarks: ")
6585   (gnus-summary-limit-to-marks marks t))
6586
6587 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6588   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6589 If REVERSE (the prefix), limit the summary buffer to articles that are
6590 not marked with MARKS.  MARKS can either be a string of marks or a
6591 list of marks.
6592 Returns how many articles were removed."
6593   (interactive "sMarks: \nP")
6594   (prog1
6595       (let ((data gnus-newsgroup-data)
6596             (marks (if (listp marks) marks
6597                      (append marks nil))) ; Transform to list.
6598             articles)
6599         (while data
6600           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6601                   (memq (gnus-data-mark (car data)) marks))
6602             (push (gnus-data-number (car data)) articles))
6603           (setq data (cdr data)))
6604         (gnus-summary-limit articles))
6605     (gnus-summary-position-point)))
6606
6607 (defun gnus-summary-limit-to-score (&optional score)
6608   "Limit to articles with score at or above SCORE."
6609   (interactive "P")
6610   (setq score (if score
6611                   (prefix-numeric-value score)
6612                 (or gnus-summary-default-score 0)))
6613   (let ((data gnus-newsgroup-data)
6614         articles)
6615     (while data
6616       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6617                 score)
6618         (push (gnus-data-number (car data)) articles))
6619       (setq data (cdr data)))
6620     (prog1
6621         (gnus-summary-limit articles)
6622       (gnus-summary-position-point))))
6623
6624 (defun gnus-summary-limit-include-thread (id)
6625   "Display all the hidden articles that in the current thread."
6626   (interactive (list (mail-header-id (gnus-summary-article-header))))
6627   (let ((articles (gnus-articles-in-thread
6628                    (gnus-id-to-thread (gnus-root-id id)))))
6629     (prog1
6630         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6631       (gnus-summary-position-point))))
6632
6633 (defun gnus-summary-limit-include-dormant ()
6634   "Display all the hidden articles that are marked as dormant.
6635 Note that this command only works on a subset of the articles currently
6636 fetched for this group."
6637   (interactive)
6638   (unless gnus-newsgroup-dormant
6639     (error "There are no dormant articles in this group"))
6640   (prog1
6641       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6642     (gnus-summary-position-point)))
6643
6644 (defun gnus-summary-limit-exclude-dormant ()
6645   "Hide all dormant articles."
6646   (interactive)
6647   (prog1
6648       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6649     (gnus-summary-position-point)))
6650
6651 (defun gnus-summary-limit-exclude-childless-dormant ()
6652   "Hide all dormant articles that have no children."
6653   (interactive)
6654   (let ((data (gnus-data-list t))
6655         articles d children)
6656     ;; Find all articles that are either not dormant or have
6657     ;; children.
6658     (while (setq d (pop data))
6659       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6660                 (and (setq children
6661                            (gnus-article-children (gnus-data-number d)))
6662                      (let (found)
6663                        (while children
6664                          (when (memq (car children) articles)
6665                            (setq children nil
6666                                  found t))
6667                          (pop children))
6668                        found)))
6669         (push (gnus-data-number d) articles)))
6670     ;; Do the limiting.
6671     (prog1
6672         (gnus-summary-limit articles)
6673       (gnus-summary-position-point))))
6674
6675 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6676   "Mark all unread excluded articles as read.
6677 If ALL, mark even excluded ticked and dormants as read."
6678   (interactive "P")
6679   (let ((articles (gnus-sorted-complement
6680                    (sort
6681                     (mapcar (lambda (h) (mail-header-number h))
6682                             gnus-newsgroup-headers)
6683                     '<)
6684                    (sort gnus-newsgroup-limit '<)))
6685         article)
6686     (setq gnus-newsgroup-unreads
6687           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6688     (if all
6689         (setq gnus-newsgroup-dormant nil
6690               gnus-newsgroup-marked nil
6691               gnus-newsgroup-reads
6692               (nconc
6693                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6694                gnus-newsgroup-reads))
6695       (while (setq article (pop articles))
6696         (unless (or (memq article gnus-newsgroup-dormant)
6697                     (memq article gnus-newsgroup-marked))
6698           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6699
6700 (defun gnus-summary-limit (articles &optional pop)
6701   (if pop
6702       ;; We pop the previous limit off the stack and use that.
6703       (setq articles (car gnus-newsgroup-limits)
6704             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6705     ;; We use the new limit, so we push the old limit on the stack.
6706     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6707   ;; Set the limit.
6708   (setq gnus-newsgroup-limit articles)
6709   (let ((total (length gnus-newsgroup-data))
6710         (data (gnus-data-find-list (gnus-summary-article-number)))
6711         (gnus-summary-mark-below nil)   ; Inhibit this.
6712         found)
6713     ;; This will do all the work of generating the new summary buffer
6714     ;; according to the new limit.
6715     (gnus-summary-prepare)
6716     ;; Hide any threads, possibly.
6717     (and gnus-show-threads
6718          gnus-thread-hide-subtree
6719          (gnus-summary-hide-all-threads))
6720     ;; Try to return to the article you were at, or one in the
6721     ;; neighborhood.
6722     (when data
6723       ;; We try to find some article after the current one.
6724       (while data
6725         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6726           (setq data nil
6727                 found t))
6728         (setq data (cdr data))))
6729     (unless found
6730       ;; If there is no data, that means that we were after the last
6731       ;; article.  The same goes when we can't find any articles
6732       ;; after the current one.
6733       (goto-char (point-max))
6734       (gnus-summary-find-prev))
6735     (gnus-set-mode-line 'summary)
6736     ;; We return how many articles were removed from the summary
6737     ;; buffer as a result of the new limit.
6738     (- total (length gnus-newsgroup-data))))
6739
6740 (defsubst gnus-invisible-cut-children (threads)
6741   (let ((num 0))
6742     (while threads
6743       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6744         (incf num))
6745       (pop threads))
6746     (< num 2)))
6747
6748 (defsubst gnus-cut-thread (thread)
6749   "Go forwards in the thread until we find an article that we want to display."
6750   (when (or (eq gnus-fetch-old-headers 'some)
6751             (eq gnus-fetch-old-headers 'invisible)
6752             (numberp gnus-fetch-old-headers)
6753             (eq gnus-build-sparse-threads 'some)
6754             (eq gnus-build-sparse-threads 'more))
6755     ;; Deal with old-fetched headers and sparse threads.
6756     (while (and
6757             thread
6758             (or
6759              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6760              (gnus-summary-article-ancient-p
6761               (mail-header-number (car thread))))
6762             (if (or (<= (length (cdr thread)) 1)
6763                     (eq gnus-fetch-old-headers 'invisible))
6764                 (setq gnus-newsgroup-limit
6765                       (delq (mail-header-number (car thread))
6766                             gnus-newsgroup-limit)
6767                       thread (cadr thread))
6768               (when (gnus-invisible-cut-children (cdr thread))
6769                 (let ((th (cdr thread)))
6770                   (while th
6771                     (if (memq (mail-header-number (caar th))
6772                               gnus-newsgroup-limit)
6773                         (setq thread (car th)
6774                               th nil)
6775                       (setq th (cdr th))))))))))
6776   thread)
6777
6778 (defun gnus-cut-threads (threads)
6779   "Cut off all uninteresting articles from the beginning of threads."
6780   (when (or (eq gnus-fetch-old-headers 'some)
6781             (eq gnus-fetch-old-headers 'invisible)
6782             (numberp gnus-fetch-old-headers)
6783             (eq gnus-build-sparse-threads 'some)
6784             (eq gnus-build-sparse-threads 'more))
6785     (let ((th threads))
6786       (while th
6787         (setcar th (gnus-cut-thread (car th)))
6788         (setq th (cdr th)))))
6789   ;; Remove nixed out threads.
6790   (delq nil threads))
6791
6792 (defun gnus-summary-initial-limit (&optional show-if-empty)
6793   "Figure out what the initial limit is supposed to be on group entry.
6794 This entails weeding out unwanted dormants, low-scored articles,
6795 fetch-old-headers verbiage, and so on."
6796   ;; Most groups have nothing to remove.
6797   (if (or gnus-inhibit-limiting
6798           (and (null gnus-newsgroup-dormant)
6799                (not (eq gnus-fetch-old-headers 'some))
6800                (not (numberp gnus-fetch-old-headers))
6801                (not (eq gnus-fetch-old-headers 'invisible))
6802                (null gnus-summary-expunge-below)
6803                (not (eq gnus-build-sparse-threads 'some))
6804                (not (eq gnus-build-sparse-threads 'more))
6805                (null gnus-thread-expunge-below)
6806                (not gnus-use-nocem)))
6807       ()                                ; Do nothing.
6808     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6809     (setq gnus-newsgroup-limit nil)
6810     (mapatoms
6811      (lambda (node)
6812        (unless (car (symbol-value node))
6813          ;; These threads have no parents -- they are roots.
6814          (let ((nodes (cdr (symbol-value node)))
6815                thread)
6816            (while nodes
6817              (if (and gnus-thread-expunge-below
6818                       (< (gnus-thread-total-score (car nodes))
6819                          gnus-thread-expunge-below))
6820                  (gnus-expunge-thread (pop nodes))
6821                (setq thread (pop nodes))
6822                (gnus-summary-limit-children thread))))))
6823      gnus-newsgroup-dependencies)
6824     ;; If this limitation resulted in an empty group, we might
6825     ;; pop the previous limit and use it instead.
6826     (when (and (not gnus-newsgroup-limit)
6827                show-if-empty)
6828       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6829     gnus-newsgroup-limit))
6830
6831 (defun gnus-summary-limit-children (thread)
6832   "Return 1 if this subthread is visible and 0 if it is not."
6833   ;; First we get the number of visible children to this thread.  This
6834   ;; is done by recursing down the thread using this function, so this
6835   ;; will really go down to a leaf article first, before slowly
6836   ;; working its way up towards the root.
6837   (when thread
6838     (let ((children
6839            (if (cdr thread)
6840                (apply '+ (mapcar 'gnus-summary-limit-children
6841                                  (cdr thread)))
6842              0))
6843           (number (mail-header-number (car thread)))
6844           score)
6845       (if (and
6846            (not (memq number gnus-newsgroup-marked))
6847            (or
6848             ;; If this article is dormant and has absolutely no visible
6849             ;; children, then this article isn't visible.
6850             (and (memq number gnus-newsgroup-dormant)
6851                  (zerop children))
6852             ;; If this is "fetch-old-headered" and there is no
6853             ;; visible children, then we don't want this article.
6854             (and (or (eq gnus-fetch-old-headers 'some)
6855                      (numberp gnus-fetch-old-headers))
6856                  (gnus-summary-article-ancient-p number)
6857                  (zerop children))
6858             ;; If this is "fetch-old-headered" and `invisible', then
6859             ;; we don't want this article.
6860             (and (eq gnus-fetch-old-headers 'invisible)
6861                  (gnus-summary-article-ancient-p number))
6862             ;; If this is a sparsely inserted article with no children,
6863             ;; we don't want it.
6864             (and (eq gnus-build-sparse-threads 'some)
6865                  (gnus-summary-article-sparse-p number)
6866                  (zerop children))
6867             ;; If we use expunging, and this article is really
6868             ;; low-scored, then we don't want this article.
6869             (when (and gnus-summary-expunge-below
6870                        (< (setq score
6871                                 (or (cdr (assq number gnus-newsgroup-scored))
6872                                     gnus-summary-default-score))
6873                           gnus-summary-expunge-below))
6874               ;; We increase the expunge-tally here, but that has
6875               ;; nothing to do with the limits, really.
6876               (incf gnus-newsgroup-expunged-tally)
6877               ;; We also mark as read here, if that's wanted.
6878               (when (and gnus-summary-mark-below
6879                          (< score gnus-summary-mark-below))
6880                 (setq gnus-newsgroup-unreads
6881                       (delq number gnus-newsgroup-unreads))
6882                 (if gnus-newsgroup-auto-expire
6883                     (push number gnus-newsgroup-expirable)
6884                   (push (cons number gnus-low-score-mark)
6885                         gnus-newsgroup-reads)))
6886               t)
6887             ;; Check NoCeM things.
6888             (if (and gnus-use-nocem
6889                      (gnus-nocem-unwanted-article-p
6890                       (mail-header-id (car thread))))
6891                 (progn
6892                   (setq gnus-newsgroup-unreads
6893                         (delq number gnus-newsgroup-unreads))
6894                   t))))
6895           ;; Nope, invisible article.
6896           0
6897         ;; Ok, this article is to be visible, so we add it to the limit
6898         ;; and return 1.
6899         (push number gnus-newsgroup-limit)
6900         1))))
6901
6902 (defun gnus-expunge-thread (thread)
6903   "Mark all articles in THREAD as read."
6904   (let* ((number (mail-header-number (car thread))))
6905     (incf gnus-newsgroup-expunged-tally)
6906     ;; We also mark as read here, if that's wanted.
6907     (setq gnus-newsgroup-unreads
6908           (delq number gnus-newsgroup-unreads))
6909     (if gnus-newsgroup-auto-expire
6910         (push number gnus-newsgroup-expirable)
6911       (push (cons number gnus-low-score-mark)
6912             gnus-newsgroup-reads)))
6913   ;; Go recursively through all subthreads.
6914   (mapcar 'gnus-expunge-thread (cdr thread)))
6915
6916 ;; Summary article oriented commands
6917
6918 (defun gnus-summary-refer-parent-article (n)
6919   "Refer parent article N times.
6920 If N is negative, go to ancestor -N instead.
6921 The difference between N and the number of articles fetched is returned."
6922   (interactive "p")
6923   (let ((skip 1)
6924         error header ref)
6925     (when (not (natnump n))
6926       (setq skip (abs n)
6927             n 1))
6928     (while (and (> n 0)
6929                 (not error))
6930       (setq header (gnus-summary-article-header))
6931       (if (and (eq (mail-header-number header)
6932                    (cdr gnus-article-current))
6933                (equal gnus-newsgroup-name
6934                       (car gnus-article-current)))
6935           ;; If we try to find the parent of the currently
6936           ;; displayed article, then we take a look at the actual
6937           ;; References header, since this is slightly more
6938           ;; reliable than the References field we got from the
6939           ;; server.
6940           (save-excursion
6941             (set-buffer gnus-original-article-buffer)
6942             (nnheader-narrow-to-headers)
6943             (unless (setq ref (message-fetch-field "references"))
6944               (setq ref (message-fetch-field "in-reply-to")))
6945             (widen))
6946         (setq ref
6947               ;; It's not the current article, so we take a bet on
6948               ;; the value we got from the server.
6949               (mail-header-references header)))
6950       (if (and ref
6951                (not (equal ref "")))
6952           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6953             (gnus-message 1 "Couldn't find parent"))
6954         (gnus-message 1 "No references in article %d"
6955                       (gnus-summary-article-number))
6956         (setq error t))
6957       (decf n))
6958     (gnus-summary-position-point)
6959     n))
6960
6961 (defun gnus-summary-refer-references ()
6962   "Fetch all articles mentioned in the References header.
6963 Return the number of articles fetched."
6964   (interactive)
6965   (let ((ref (mail-header-references (gnus-summary-article-header)))
6966         (current (gnus-summary-article-number))
6967         (n 0))
6968     (if (or (not ref)
6969             (equal ref ""))
6970         (error "No References in the current article")
6971       ;; For each Message-ID in the References header...
6972       (while (string-match "<[^>]*>" ref)
6973         (incf n)
6974         ;; ... fetch that article.
6975         (gnus-summary-refer-article
6976          (prog1 (match-string 0 ref)
6977            (setq ref (substring ref (match-end 0))))))
6978       (gnus-summary-goto-subject current)
6979       (gnus-summary-position-point)
6980       n)))
6981
6982 (defun gnus-summary-refer-thread (&optional limit)
6983   "Fetch all articles in the current thread.
6984 If LIMIT (the numerical prefix), fetch that many old headers instead
6985 of what's specified by the `gnus-refer-thread-limit' variable."
6986   (interactive "P")
6987   (let ((id (mail-header-id (gnus-summary-article-header)))
6988         (limit (if limit (prefix-numeric-value limit)
6989                  gnus-refer-thread-limit)))
6990     ;; We want to fetch LIMIT *old* headers, but we also have to
6991     ;; re-fetch all the headers in the current buffer, because many of
6992     ;; them may be undisplayed.  So we adjust LIMIT.
6993     (when (numberp limit)
6994       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6995     (unless (eq gnus-fetch-old-headers 'invisible)
6996       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6997       ;; Retrieve the headers and read them in.
6998       (if (eq (gnus-retrieve-headers
6999                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7000               'nov)
7001           (gnus-build-all-threads)
7002         (error "Can't fetch thread from backends that don't support NOV"))
7003       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7004     (gnus-summary-limit-include-thread id)))
7005
7006 (defun gnus-summary-refer-article (message-id)
7007   "Fetch an article specified by MESSAGE-ID."
7008   (interactive "sMessage-ID: ")
7009   (when (and (stringp message-id)
7010              (not (zerop (length message-id))))
7011     ;; Construct the correct Message-ID if necessary.
7012     ;; Suggested by tale@pawl.rpi.edu.
7013     (unless (string-match "^<" message-id)
7014       (setq message-id (concat "<" message-id)))
7015     (unless (string-match ">$" message-id)
7016       (setq message-id (concat message-id ">")))
7017     (let* ((header (gnus-id-to-header message-id))
7018            (sparse (and header
7019                         (gnus-summary-article-sparse-p
7020                          (mail-header-number header))
7021                         (memq (mail-header-number header)
7022                               gnus-newsgroup-limit)))
7023            number)
7024       (cond
7025        ;; If the article is present in the buffer we just go to it.
7026        ((and header
7027              (or (not (gnus-summary-article-sparse-p
7028                        (mail-header-number header)))
7029                  sparse))
7030         (prog1
7031             (gnus-summary-goto-article
7032              (mail-header-number header) nil t)
7033           (when sparse
7034             (gnus-summary-update-article (mail-header-number header)))))
7035        (t
7036         ;; We fetch the article.
7037         (catch 'found
7038           (dolist (gnus-override-method (gnus-refer-article-methods))
7039             (gnus-check-server gnus-override-method)
7040             ;; Fetch the header, and display the article.
7041             (when (setq number (gnus-summary-insert-subject message-id))
7042               (gnus-summary-select-article nil nil nil number)
7043               (throw 'found t)))
7044           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7045
7046 (defun gnus-refer-article-methods ()
7047   "Return a list of referrable methods."
7048   (cond
7049    ;; No method, so we default to current and native.
7050    ((null gnus-refer-article-method)
7051     (list gnus-current-select-method gnus-select-method))
7052    ;; Current.
7053    ((eq 'current gnus-refer-article-method)
7054     (list gnus-current-select-method))
7055    ;; List of select methods.
7056    ((not (and (symbolp (car gnus-refer-article-method))
7057               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7058     (let (out)
7059       (dolist (method gnus-refer-article-method)
7060         (push (if (eq 'current method)
7061                   gnus-current-select-method
7062                 method)
7063               out))
7064       (nreverse out)))
7065    ;; One single select method.
7066    (t
7067     (list gnus-refer-article-method))))
7068
7069 (defun gnus-summary-edit-parameters ()
7070   "Edit the group parameters of the current group."
7071   (interactive)
7072   (gnus-group-edit-group gnus-newsgroup-name 'params))
7073
7074 (defun gnus-summary-customize-parameters ()
7075   "Customize the group parameters of the current group."
7076   (interactive)
7077   (gnus-group-customize gnus-newsgroup-name))
7078
7079 (defun gnus-summary-enter-digest-group (&optional force)
7080   "Enter an nndoc group based on the current article.
7081 If FORCE, force a digest interpretation.  If not, try
7082 to guess what the document format is."
7083   (interactive "P")
7084   (let ((conf gnus-current-window-configuration))
7085     (save-excursion
7086       (gnus-summary-select-article))
7087     (setq gnus-current-window-configuration conf)
7088     (let* ((name (format "%s-%d"
7089                          (gnus-group-prefixed-name
7090                           gnus-newsgroup-name (list 'nndoc ""))
7091                          (save-excursion
7092                            (set-buffer gnus-summary-buffer)
7093                            gnus-current-article)))
7094            (ogroup gnus-newsgroup-name)
7095            (params (append (gnus-info-params (gnus-get-info ogroup))
7096                            (list (cons 'to-group ogroup))
7097                            (list (cons 'save-article-group ogroup))))
7098            (case-fold-search t)
7099            (buf (current-buffer))
7100            dig to-address)
7101       (save-excursion
7102         (set-buffer gnus-original-article-buffer)
7103         ;; Have the digest group inherit the main mail address of
7104         ;; the parent article.
7105         (when (setq to-address (or (message-fetch-field "reply-to")
7106                                    (message-fetch-field "from")))
7107           (setq params (append 
7108                         (list (cons 'to-address 
7109                                     (funcall gnus-decode-encoded-word-function
7110                                              to-address))))))
7111         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7112         (insert-buffer-substring gnus-original-article-buffer)
7113         ;; Remove lines that may lead nndoc to misinterpret the
7114         ;; document type.
7115         (narrow-to-region
7116          (goto-char (point-min))
7117          (or (search-forward "\n\n" nil t) (point)))
7118         (goto-char (point-min))
7119         (delete-matching-lines "^Path:\\|^From ")
7120         (widen))
7121       (unwind-protect
7122           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7123                     (gnus-newsgroup-ephemeral-ignored-charsets
7124                      gnus-newsgroup-ignored-charsets))
7125                 (gnus-group-read-ephemeral-group
7126                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7127                               (nndoc-article-type
7128                                ,(if force 'mbox 'guess))) t))
7129               ;; Make all postings to this group go to the parent group.
7130               (nconc (gnus-info-params (gnus-get-info name))
7131                      params)
7132             ;; Couldn't select this doc group.
7133             (switch-to-buffer buf)
7134             (gnus-set-global-variables)
7135             (gnus-configure-windows 'summary)
7136             (gnus-message 3 "Article couldn't be entered?"))
7137         (kill-buffer dig)))))
7138
7139 (defun gnus-summary-read-document (n)
7140   "Open a new group based on the current article(s).
7141 This will allow you to read digests and other similar
7142 documents as newsgroups.
7143 Obeys the standard process/prefix convention."
7144   (interactive "P")
7145   (let* ((articles (gnus-summary-work-articles n))
7146          (ogroup gnus-newsgroup-name)
7147          (params (append (gnus-info-params (gnus-get-info ogroup))
7148                          (list (cons 'to-group ogroup))))
7149          article group egroup groups vgroup)
7150     (while (setq article (pop articles))
7151       (setq group (format "%s-%d" gnus-newsgroup-name article))
7152       (gnus-summary-remove-process-mark article)
7153       (when (gnus-summary-display-article article)
7154         (save-excursion
7155           (with-temp-buffer
7156             (insert-buffer-substring gnus-original-article-buffer)
7157             ;; Remove some headers that may lead nndoc to make
7158             ;; the wrong guess.
7159             (message-narrow-to-head)
7160             (goto-char (point-min))
7161             (delete-matching-lines "^\\(Path\\):\\|^From ")
7162             (widen)
7163             (if (setq egroup
7164                       (gnus-group-read-ephemeral-group
7165                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7166                                      (nndoc-article-type guess))
7167                        t nil t))
7168                 (progn
7169                   ;; Make all postings to this group go to the parent group.
7170                   (nconc (gnus-info-params (gnus-get-info egroup))
7171                          params)
7172                   (push egroup groups))
7173               ;; Couldn't select this doc group.
7174               (gnus-error 3 "Article couldn't be entered"))))))
7175     ;; Now we have selected all the documents.
7176     (cond
7177      ((not groups)
7178       (error "None of the articles could be interpreted as documents"))
7179      ((gnus-group-read-ephemeral-group
7180        (setq vgroup (format
7181                      "nnvirtual:%s-%s" gnus-newsgroup-name
7182                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7183        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7184        t
7185        (cons (current-buffer) 'summary)))
7186      (t
7187       (error "Couldn't select virtual nndoc group")))))
7188
7189 (defun gnus-summary-isearch-article (&optional regexp-p)
7190   "Do incremental search forward on the current article.
7191 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7192   (interactive "P")
7193   (gnus-summary-select-article)
7194   (gnus-configure-windows 'article)
7195   (gnus-eval-in-buffer-window gnus-article-buffer
7196     (save-restriction
7197       (widen)
7198       (isearch-forward regexp-p))))
7199
7200 (defun gnus-summary-search-article-forward (regexp &optional backward)
7201   "Search for an article containing REGEXP forward.
7202 If BACKWARD, search backward instead."
7203   (interactive
7204    (list (read-string
7205           (format "Search article %s (regexp%s): "
7206                   (if current-prefix-arg "backward" "forward")
7207                   (if gnus-last-search-regexp
7208                       (concat ", default " gnus-last-search-regexp)
7209                     "")))
7210          current-prefix-arg))
7211   (if (string-equal regexp "")
7212       (setq regexp (or gnus-last-search-regexp ""))
7213     (setq gnus-last-search-regexp regexp)
7214     (setq gnus-article-before-search gnus-current-article))
7215   ;; Intentionally set gnus-last-article.
7216   (setq gnus-last-article gnus-article-before-search)
7217   (let ((gnus-last-article gnus-last-article))
7218     (if (gnus-summary-search-article regexp backward)
7219         (gnus-summary-show-thread)
7220       (error "Search failed: \"%s\"" regexp))))
7221
7222 (defun gnus-summary-search-article-backward (regexp)
7223   "Search for an article containing REGEXP backward."
7224   (interactive
7225    (list (read-string
7226           (format "Search article backward (regexp%s): "
7227                   (if gnus-last-search-regexp
7228                       (concat ", default " gnus-last-search-regexp)
7229                     "")))))
7230   (gnus-summary-search-article-forward regexp 'backward))
7231
7232 (defun gnus-summary-search-article (regexp &optional backward)
7233   "Search for an article containing REGEXP.
7234 Optional argument BACKWARD means do search for backward.
7235 `gnus-select-article-hook' is not called during the search."
7236   ;; We have to require this here to make sure that the following
7237   ;; dynamic binding isn't shadowed by autoloading.
7238   (require 'gnus-async)
7239   (require 'gnus-art)
7240   (let ((gnus-select-article-hook nil)  ;Disable hook.
7241         (gnus-article-prepare-hook nil)
7242         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7243         (gnus-use-article-prefetch nil)
7244         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7245         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7246         (sum (current-buffer))
7247         (gnus-display-mime-function nil)
7248         (found nil)
7249         point)
7250     (gnus-save-hidden-threads
7251       (gnus-summary-select-article)
7252       (set-buffer gnus-article-buffer)
7253       (goto-char (window-point (get-buffer-window (current-buffer))))
7254       (when backward
7255         (forward-line -1))
7256       (while (not found)
7257         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7258         (if (if backward
7259                 (re-search-backward regexp nil t)
7260               (re-search-forward regexp nil t))
7261             ;; We found the regexp.
7262             (progn
7263               (setq found 'found)
7264               (beginning-of-line)
7265               (set-window-start
7266                (get-buffer-window (current-buffer))
7267                (point))
7268               (forward-line 1)
7269               (set-window-point
7270                (get-buffer-window (current-buffer))
7271                (point))
7272               (set-buffer sum)
7273               (setq point (point)))
7274           ;; We didn't find it, so we go to the next article.
7275           (set-buffer sum)
7276           (setq found 'not)
7277           (while (eq found 'not)
7278             (if (not (if backward (gnus-summary-find-prev)
7279                        (gnus-summary-find-next)))
7280                 ;; No more articles.
7281                 (setq found t)
7282               ;; Select the next article and adjust point.
7283               (unless (gnus-summary-article-sparse-p
7284                        (gnus-summary-article-number))
7285                 (setq found nil)
7286                 (gnus-summary-select-article)
7287                 (set-buffer gnus-article-buffer)
7288                 (widen)
7289                 (goto-char (if backward (point-max) (point-min))))))))
7290       (gnus-message 7 ""))
7291     ;; Return whether we found the regexp.
7292     (when (eq found 'found)
7293       (goto-char point)
7294       (gnus-summary-show-thread)
7295       (gnus-summary-goto-subject gnus-current-article)
7296       (gnus-summary-position-point)
7297       t)))
7298
7299 (defun gnus-summary-find-matching (header regexp &optional backward unread
7300                                           not-case-fold)
7301   "Return a list of all articles that match REGEXP on HEADER.
7302 The search stars on the current article and goes forwards unless
7303 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7304 If UNREAD is non-nil, only unread articles will
7305 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7306 in the comparisons."
7307   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7308                 (gnus-data-find-list
7309                  (gnus-summary-article-number) (gnus-data-list backward))))
7310         (case-fold-search (not not-case-fold))
7311         articles d func)
7312     (if (consp header)
7313         (if (eq (car header) 'extra)
7314             (setq func
7315                   `(lambda (h)
7316                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7317                          "")))
7318           (error "%s is an invalid header" header))
7319       (unless (fboundp (intern (concat "mail-header-" header)))
7320         (error "%s is not a valid header" header))
7321       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7322     (while data
7323       (setq d (car data))
7324       (and (or (not unread)             ; We want all articles...
7325                (gnus-data-unread-p d))  ; Or just unreads.
7326            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7327            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7328            (push (gnus-data-number d) articles)) ; Success!
7329       (setq data (cdr data)))
7330     (nreverse articles)))
7331
7332 (defun gnus-summary-execute-command (header regexp command &optional backward)
7333   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7334 If HEADER is an empty string (or nil), the match is done on the entire
7335 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7336   (interactive
7337    (list (let ((completion-ignore-case t))
7338            (completing-read
7339             "Header name: "
7340             (mapcar (lambda (string) (list string))
7341                     '("Number" "Subject" "From" "Lines" "Date"
7342                       "Message-ID" "Xref" "References" "Body"))
7343             nil 'require-match))
7344          (read-string "Regexp: ")
7345          (read-key-sequence "Command: ")
7346          current-prefix-arg))
7347   (when (equal header "Body")
7348     (setq header ""))
7349   ;; Hidden thread subtrees must be searched as well.
7350   (gnus-summary-show-all-threads)
7351   ;; We don't want to change current point nor window configuration.
7352   (save-excursion
7353     (save-window-excursion
7354       (gnus-message 6 "Executing %s..." (key-description command))
7355       ;; We'd like to execute COMMAND interactively so as to give arguments.
7356       (gnus-execute header regexp
7357                     `(call-interactively ',(key-binding command))
7358                     backward)
7359       (gnus-message 6 "Executing %s...done" (key-description command)))))
7360
7361 (defun gnus-summary-beginning-of-article ()
7362   "Scroll the article back to the beginning."
7363   (interactive)
7364   (gnus-summary-select-article)
7365   (gnus-configure-windows 'article)
7366   (gnus-eval-in-buffer-window gnus-article-buffer
7367     (widen)
7368     (goto-char (point-min))
7369     (when gnus-page-broken
7370       (gnus-narrow-to-page))))
7371
7372 (defun gnus-summary-end-of-article ()
7373   "Scroll to the end of the article."
7374   (interactive)
7375   (gnus-summary-select-article)
7376   (gnus-configure-windows 'article)
7377   (gnus-eval-in-buffer-window gnus-article-buffer
7378     (widen)
7379     (goto-char (point-max))
7380     (recenter -3)
7381     (when gnus-page-broken
7382       (gnus-narrow-to-page))))
7383
7384 (defun gnus-summary-print-article (&optional filename n)
7385   "Generate and print a PostScript image of the N next (mail) articles.
7386
7387 If N is negative, print the N previous articles.  If N is nil and articles
7388 have been marked with the process mark, print these instead.
7389
7390 If the optional first argument FILENAME is nil, send the image to the
7391 printer.  If FILENAME is a string, save the PostScript image in a file with
7392 that name.  If FILENAME is a number, prompt the user for the name of the file
7393 to save in."
7394   (interactive (list (ps-print-preprint current-prefix-arg)
7395                      current-prefix-arg))
7396   (dolist (article (gnus-summary-work-articles n))
7397     (gnus-summary-select-article nil nil 'pseudo article)
7398     (gnus-eval-in-buffer-window gnus-article-buffer
7399       (let ((buffer (generate-new-buffer " *print*")))
7400         (unwind-protect
7401             (progn
7402               (copy-to-buffer buffer (point-min) (point-max))
7403               (set-buffer buffer)
7404               (gnus-article-delete-invisible-text)
7405               (let ((ps-left-header
7406                      (list
7407                       (concat "("
7408                               (mail-header-subject gnus-current-headers) ")")
7409                       (concat "("
7410                               (mail-header-from gnus-current-headers) ")")))
7411                     (ps-right-header
7412                      (list
7413                       "/pagenumberstring load"
7414                       (concat "("
7415                               (mail-header-date gnus-current-headers) ")"))))
7416                 (gnus-run-hooks 'gnus-ps-print-hook)
7417                 (save-excursion
7418                   (ps-print-buffer-with-faces filename))))
7419           (kill-buffer buffer))))))
7420
7421 (defun gnus-summary-show-article (&optional arg)
7422   "Force re-fetching of the current article.
7423 If ARG (the prefix) is a number, show the article with the charset
7424 defined in `gnus-summary-show-article-charset-alist', or the charset
7425 inputed.
7426 If ARG (the prefix) is non-nil and not a number, show the raw article
7427 without any article massaging functions being run."
7428   (interactive "P")
7429   (cond
7430    ((numberp arg)
7431     (let ((gnus-newsgroup-charset
7432            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7433                (read-coding-system "Charset: ")))
7434           (gnus-newsgroup-ignored-charsets 'gnus-all))
7435       (gnus-summary-select-article nil 'force)
7436       (let ((deps gnus-newsgroup-dependencies)
7437             head header)
7438         (save-excursion
7439           (set-buffer gnus-original-article-buffer)
7440           (save-restriction
7441             (message-narrow-to-head)
7442             (setq head (buffer-string)))
7443           (with-temp-buffer
7444             (insert (format "211 %d Article retrieved.\n"
7445                             (cdr gnus-article-current)))
7446             (insert head)
7447             (insert ".\n")
7448             (let ((nntp-server-buffer (current-buffer)))
7449               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7450         (gnus-data-set-header
7451          (gnus-data-find (cdr gnus-article-current))
7452          header)
7453         (gnus-summary-update-article-line
7454          (cdr gnus-article-current) header))))
7455    ((not arg)
7456     ;; Select the article the normal way.
7457     (gnus-summary-select-article nil 'force))
7458    (t
7459     ;; We have to require this here to make sure that the following
7460     ;; dynamic binding isn't shadowed by autoloading.
7461     (require 'gnus-async)
7462     (require 'gnus-art)
7463     ;; Bind the article treatment functions to nil.
7464     (let ((gnus-have-all-headers t)
7465           gnus-article-prepare-hook
7466           gnus-article-decode-hook
7467           gnus-display-mime-function
7468           gnus-break-pages)
7469       ;; Destroy any MIME parts.
7470       (when (gnus-buffer-live-p gnus-article-buffer)
7471         (save-excursion
7472           (set-buffer gnus-article-buffer)
7473           (mm-destroy-parts gnus-article-mime-handles)
7474           ;; Set it to nil for safety reason.
7475           (setq gnus-article-mime-handle-alist nil)
7476           (setq gnus-article-mime-handles nil)))
7477       (gnus-summary-select-article nil 'force))))
7478   (gnus-summary-goto-subject gnus-current-article)
7479   (gnus-summary-position-point))
7480
7481 (defun gnus-summary-verbose-headers (&optional arg)
7482   "Toggle permanent full header display.
7483 If ARG is a positive number, turn header display on.
7484 If ARG is a negative number, turn header display off."
7485   (interactive "P")
7486   (setq gnus-show-all-headers
7487         (cond ((or (not (numberp arg))
7488                    (zerop arg))
7489                (not gnus-show-all-headers))
7490               ((natnump arg)
7491                t)))
7492   (gnus-summary-show-article))
7493
7494 (defun gnus-summary-toggle-header (&optional arg)
7495   "Show the headers if they are hidden, or hide them if they are shown.
7496 If ARG is a positive number, show the entire header.
7497 If ARG is a negative number, hide the unwanted header lines."
7498   (interactive "P")
7499   (save-excursion
7500     (set-buffer gnus-article-buffer)
7501     (save-restriction
7502       (let* ((buffer-read-only nil)
7503              (inhibit-point-motion-hooks t)
7504              hidden e)
7505         (setq hidden
7506               (if (numberp arg)
7507                   (>= arg 0)
7508                 (save-restriction
7509                   (article-narrow-to-head)
7510                   (gnus-article-hidden-text-p 'headers))))
7511         (goto-char (point-min))
7512         (when (search-forward "\n\n" nil t)
7513           (delete-region (point-min) (1- (point))))
7514         (goto-char (point-min))
7515         (save-excursion
7516           (set-buffer gnus-original-article-buffer)
7517           (goto-char (point-min))
7518           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7519         (insert-buffer-substring gnus-original-article-buffer 1 e)
7520         (save-restriction
7521           (narrow-to-region (point-min) (point))
7522           (article-decode-encoded-words)
7523           (if  hidden
7524               (let ((gnus-treat-hide-headers nil)
7525                     (gnus-treat-hide-boring-headers nil))
7526                 (setq gnus-article-wash-types
7527                       (delq 'headers gnus-article-wash-types))
7528                 (gnus-treat-article 'head))
7529             (gnus-treat-article 'head)))
7530         (gnus-set-mode-line 'article)))))
7531
7532 (defun gnus-summary-show-all-headers ()
7533   "Make all header lines visible."
7534   (interactive)
7535   (gnus-article-show-all-headers))
7536
7537 (defun gnus-summary-caesar-message (&optional arg)
7538   "Caesar rotate the current article by 13.
7539 The numerical prefix specifies how many places to rotate each letter
7540 forward."
7541   (interactive "P")
7542   (gnus-summary-select-article)
7543   (let ((mail-header-separator ""))
7544     (gnus-eval-in-buffer-window gnus-article-buffer
7545       (save-restriction
7546         (widen)
7547         (let ((start (window-start))
7548               buffer-read-only)
7549           (message-caesar-buffer-body arg)
7550           (set-window-start (get-buffer-window (current-buffer)) start))))))
7551
7552 (defun gnus-summary-stop-page-breaking ()
7553   "Stop page breaking in the current article."
7554   (interactive)
7555   (gnus-summary-select-article)
7556   (gnus-eval-in-buffer-window gnus-article-buffer
7557     (widen)
7558     (when (gnus-visual-p 'page-marker)
7559       (let ((buffer-read-only nil))
7560         (gnus-remove-text-with-property 'gnus-prev)
7561         (gnus-remove-text-with-property 'gnus-next))
7562       (setq gnus-page-broken nil))))
7563
7564 (defun gnus-summary-move-article (&optional n to-newsgroup
7565                                             select-method action)
7566   "Move the current article to a different newsgroup.
7567 If N is a positive number, move the N next articles.
7568 If N is a negative number, move the N previous articles.
7569 If N is nil and any articles have been marked with the process mark,
7570 move those articles instead.
7571 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7572 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7573 re-spool using this method.
7574
7575 For this function to work, both the current newsgroup and the
7576 newsgroup that you want to move to have to support the `request-move'
7577 and `request-accept' functions.
7578
7579 ACTION can be either `move' (the default), `crosspost' or `copy'."
7580   (interactive "P")
7581   (unless action
7582     (setq action 'move))
7583   ;; Disable marking as read.
7584   (let (gnus-mark-article-hook)
7585     (save-window-excursion
7586       (gnus-summary-select-article)))
7587   ;; Check whether the source group supports the required functions.
7588   (cond ((and (eq action 'move)
7589               (not (gnus-check-backend-function
7590                     'request-move-article gnus-newsgroup-name)))
7591          (error "The current group does not support article moving"))
7592         ((and (eq action 'crosspost)
7593               (not (gnus-check-backend-function
7594                     'request-replace-article gnus-newsgroup-name)))
7595          (error "The current group does not support article editing")))
7596   (let ((articles (gnus-summary-work-articles n))
7597         (prefix (if (gnus-check-backend-function
7598                     'request-move-article gnus-newsgroup-name)
7599                     (gnus-group-real-prefix gnus-newsgroup-name)
7600                   ""))
7601         (names '((move "Move" "Moving")
7602                  (copy "Copy" "Copying")
7603                  (crosspost "Crosspost" "Crossposting")))
7604         (copy-buf (save-excursion
7605                     (nnheader-set-temp-buffer " *copy article*")))
7606         art-group to-method new-xref article to-groups)
7607     (unless (assq action names)
7608       (error "Unknown action %s" action))
7609     ;; Read the newsgroup name.
7610     (when (and (not to-newsgroup)
7611                (not select-method))
7612       (setq to-newsgroup
7613             (gnus-read-move-group-name
7614              (cadr (assq action names))
7615              (symbol-value (intern (format "gnus-current-%s-group" action)))
7616              articles prefix))
7617       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7618     (setq to-method (or select-method
7619                         (gnus-server-to-method
7620                          (gnus-group-method to-newsgroup))))
7621     ;; Check the method we are to move this article to...
7622     (unless (gnus-check-backend-function
7623              'request-accept-article (car to-method))
7624       (error "%s does not support article copying" (car to-method)))
7625     (unless (gnus-check-server to-method)
7626       (error "Can't open server %s" (car to-method)))
7627     (gnus-message 6 "%s to %s: %s..."
7628                   (caddr (assq action names))
7629                   (or (car select-method) to-newsgroup) articles)
7630     (while articles
7631       (setq article (pop articles))
7632       (setq
7633        art-group
7634        (cond
7635         ;; Move the article.
7636         ((eq action 'move)
7637          ;; Remove this article from future suppression.
7638          (gnus-dup-unsuppress-article article)
7639          (gnus-request-move-article
7640           article                       ; Article to move
7641           gnus-newsgroup-name           ; From newsgroup
7642           (nth 1 (gnus-find-method-for-group
7643                   gnus-newsgroup-name)) ; Server
7644           (list 'gnus-request-accept-article
7645                 to-newsgroup (list 'quote select-method)
7646                 (not articles) t)       ; Accept form
7647           (not articles)))              ; Only save nov last time
7648         ;; Copy the article.
7649         ((eq action 'copy)
7650          (save-excursion
7651            (set-buffer copy-buf)
7652            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7653              (gnus-request-accept-article
7654               to-newsgroup select-method (not articles) t))))
7655         ;; Crosspost the article.
7656         ((eq action 'crosspost)
7657          (let ((xref (message-tokenize-header
7658                       (mail-header-xref (gnus-summary-article-header article))
7659                       " ")))
7660            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7661                                   ":" article))
7662            (unless xref
7663              (setq xref (list (system-name))))
7664            (setq new-xref
7665                  (concat
7666                   (mapconcat 'identity
7667                              (delete "Xref:" (delete new-xref xref))
7668                              " ")
7669                   " " new-xref))
7670            (save-excursion
7671              (set-buffer copy-buf)
7672              ;; First put the article in the destination group.
7673              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7674              (when (consp (setq art-group
7675                                 (gnus-request-accept-article
7676                                  to-newsgroup select-method (not articles))))
7677                (setq new-xref (concat new-xref " " (car art-group)
7678                                       ":" (cdr art-group)))
7679                ;; Now we have the new Xrefs header, so we insert
7680                ;; it and replace the new article.
7681                (nnheader-replace-header "Xref" new-xref)
7682                (gnus-request-replace-article
7683                 (cdr art-group) to-newsgroup (current-buffer))
7684                art-group))))))
7685       (cond
7686        ((not art-group)
7687         (gnus-message 1 "Couldn't %s article %s: %s"
7688                       (cadr (assq action names)) article
7689                       (nnheader-get-report (car to-method))))
7690        ((eq art-group 'junk)
7691         (when (eq action 'move)
7692           (gnus-summary-mark-article article gnus-canceled-mark)
7693           (gnus-message 4 "Deleted article %s" article)))
7694        (t
7695         (let* ((pto-group (gnus-group-prefixed-name
7696                            (car art-group) to-method))
7697                (entry
7698                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7699                (info (nth 2 entry))
7700                (to-group (gnus-info-group info))
7701                to-marks)
7702           ;; Update the group that has been moved to.
7703           (when (and info
7704                      (memq action '(move copy)))
7705             (unless (member to-group to-groups)
7706               (push to-group to-groups))
7707
7708             (unless (memq article gnus-newsgroup-unreads)
7709               (push 'read to-marks)
7710               (gnus-info-set-read
7711                info (gnus-add-to-range (gnus-info-read info)
7712                                        (list (cdr art-group)))))
7713
7714             ;; See whether the article is to be put in the cache.
7715             (let ((marks gnus-article-mark-lists)
7716                   (to-article (cdr art-group)))
7717
7718               ;; Enter the article into the cache in the new group,
7719               ;; if that is required.
7720               (when gnus-use-cache
7721                 (gnus-cache-possibly-enter-article
7722                  to-group to-article
7723                  (memq article gnus-newsgroup-marked)
7724                  (memq article gnus-newsgroup-dormant)
7725                  (memq article gnus-newsgroup-unreads)))
7726
7727               (when gnus-preserve-marks
7728                 ;; Copy any marks over to the new group.
7729                 (when (and (equal to-group gnus-newsgroup-name)
7730                            (not (memq article gnus-newsgroup-unreads)))
7731                   ;; Mark this article as read in this group.
7732                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7733                   (setcdr (gnus-active to-group) to-article)
7734                   (setcdr gnus-newsgroup-active to-article))
7735
7736                 (while marks
7737                   (when (memq article (symbol-value
7738                                        (intern (format "gnus-newsgroup-%s"
7739                                                        (caar marks)))))
7740                     (push (cdar marks) to-marks)
7741                     ;; If the other group is the same as this group,
7742                     ;; then we have to add the mark to the list.
7743                     (when (equal to-group gnus-newsgroup-name)
7744                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7745                            (cons to-article
7746                                  (symbol-value
7747                                   (intern (format "gnus-newsgroup-%s"
7748                                                   (caar marks)))))))
7749                     ;; Copy the marks to other group.
7750                     (gnus-add-marked-articles
7751                      to-group (cdar marks) (list to-article) info))
7752                   (setq marks (cdr marks)))
7753
7754                 (gnus-request-set-mark to-group (list (list (list to-article)
7755                                                             'set
7756                                                             to-marks))))
7757
7758               (gnus-dribble-enter
7759                (concat "(gnus-group-set-info '"
7760                        (gnus-prin1-to-string (gnus-get-info to-group))
7761                        ")"))))
7762
7763           ;; Update the Xref header in this article to point to
7764           ;; the new crossposted article we have just created.
7765           (when (eq action 'crosspost)
7766             (save-excursion
7767               (set-buffer copy-buf)
7768               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7769               (nnheader-replace-header "Xref" new-xref)
7770               (gnus-request-replace-article
7771                article gnus-newsgroup-name (current-buffer)))))
7772
7773         ;;;!!!Why is this necessary?
7774         (set-buffer gnus-summary-buffer)
7775
7776         (gnus-summary-goto-subject article)
7777         (when (eq action 'move)
7778           (gnus-summary-mark-article article gnus-canceled-mark))))
7779       (gnus-summary-remove-process-mark article))
7780     ;; Re-activate all groups that have been moved to.
7781     (while to-groups
7782       (save-excursion
7783         (set-buffer gnus-group-buffer)
7784         (when (gnus-group-goto-group (car to-groups) t)
7785           (gnus-group-get-new-news-this-group 1 t))
7786         (pop to-groups)))
7787
7788     (gnus-kill-buffer copy-buf)
7789     (gnus-summary-position-point)
7790     (gnus-set-mode-line 'summary)))
7791
7792 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7793   "Move the current article to a different newsgroup.
7794 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7795 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7796 re-spool using this method."
7797   (interactive "P")
7798   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7799
7800 (defun gnus-summary-crosspost-article (&optional n)
7801   "Crosspost the current article to some other group."
7802   (interactive "P")
7803   (gnus-summary-move-article n nil nil 'crosspost))
7804
7805 (defcustom gnus-summary-respool-default-method nil
7806   "Default method for respooling an article.
7807 If nil, use to the current newsgroup method."
7808   :type '(choice (gnus-select-method :value (nnml ""))
7809                  (const nil))
7810   :group 'gnus-summary-mail)
7811
7812 (defun gnus-summary-respool-article (&optional n method)
7813   "Respool the current article.
7814 The article will be squeezed through the mail spooling process again,
7815 which means that it will be put in some mail newsgroup or other
7816 depending on `nnmail-split-methods'.
7817 If N is a positive number, respool the N next articles.
7818 If N is a negative number, respool the N previous articles.
7819 If N is nil and any articles have been marked with the process mark,
7820 respool those articles instead.
7821
7822 Respooling can be done both from mail groups and \"real\" newsgroups.
7823 In the former case, the articles in question will be moved from the
7824 current group into whatever groups they are destined to.  In the
7825 latter case, they will be copied into the relevant groups."
7826   (interactive
7827    (list current-prefix-arg
7828          (let* ((methods (gnus-methods-using 'respool))
7829                 (methname
7830                  (symbol-name (or gnus-summary-respool-default-method
7831                                   (car (gnus-find-method-for-group
7832                                         gnus-newsgroup-name)))))
7833                 (method
7834                  (gnus-completing-read
7835                   methname "What backend do you want to use when respooling?"
7836                   methods nil t nil 'gnus-mail-method-history))
7837                 ms)
7838            (cond
7839             ((zerop (length (setq ms (gnus-servers-using-backend
7840                                       (intern method)))))
7841              (list (intern method) ""))
7842             ((= 1 (length ms))
7843              (car ms))
7844             (t
7845              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7846                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7847                            ms-alist))))))))
7848   (unless method
7849     (error "No method given for respooling"))
7850   (if (assoc (symbol-name
7851               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7852              (gnus-methods-using 'respool))
7853       (gnus-summary-move-article n nil method)
7854     (gnus-summary-copy-article n nil method)))
7855
7856 (defun gnus-summary-import-article (file)
7857   "Import an arbitrary file into a mail newsgroup."
7858   (interactive "fImport file: ")
7859   (let ((group gnus-newsgroup-name)
7860         (now (current-time))
7861         atts lines)
7862     (unless (gnus-check-backend-function 'request-accept-article group)
7863       (error "%s does not support article importing" group))
7864     (or (file-readable-p file)
7865         (not (file-regular-p file))
7866         (error "Can't read %s" file))
7867     (save-excursion
7868       (set-buffer (gnus-get-buffer-create " *import file*"))
7869       (erase-buffer)
7870       (nnheader-insert-file-contents file)
7871       (goto-char (point-min))
7872       (unless (nnheader-article-p)
7873         ;; This doesn't look like an article, so we fudge some headers.
7874         (setq atts (file-attributes file)
7875               lines (count-lines (point-min) (point-max)))
7876         (insert "From: " (read-string "From: ") "\n"
7877                 "Subject: " (read-string "Subject: ") "\n"
7878                 "Date: " (message-make-date (nth 5 atts))
7879                 "\n"
7880                 "Message-ID: " (message-make-message-id) "\n"
7881                 "Lines: " (int-to-string lines) "\n"
7882                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7883       (gnus-request-accept-article group nil t)
7884       (kill-buffer (current-buffer)))))
7885
7886 (defun gnus-summary-article-posted-p ()
7887   "Say whether the current (mail) article is available from news as well.
7888 This will be the case if the article has both been mailed and posted."
7889   (interactive)
7890   (let ((id (mail-header-references (gnus-summary-article-header)))
7891         (gnus-override-method (car (gnus-refer-article-methods))))
7892     (if (gnus-request-head id "")
7893         (gnus-message 2 "The current message was found on %s"
7894                       gnus-override-method)
7895       (gnus-message 2 "The current message couldn't be found on %s"
7896                     gnus-override-method)
7897       nil)))
7898
7899 (defun gnus-summary-expire-articles (&optional now)
7900   "Expire all articles that are marked as expirable in the current group."
7901   (interactive)
7902   (when (gnus-check-backend-function
7903          'request-expire-articles gnus-newsgroup-name)
7904     ;; This backend supports expiry.
7905     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7906            (expirable (if total
7907                           (progn
7908                             ;; We need to update the info for
7909                             ;; this group for `gnus-list-of-read-articles'
7910                             ;; to give us the right answer.
7911                             (gnus-run-hooks 'gnus-exit-group-hook)
7912                             (gnus-summary-update-info)
7913                             (gnus-list-of-read-articles gnus-newsgroup-name))
7914                         (setq gnus-newsgroup-expirable
7915                               (sort gnus-newsgroup-expirable '<))))
7916            (expiry-wait (if now 'immediate
7917                           (gnus-group-find-parameter
7918                            gnus-newsgroup-name 'expiry-wait)))
7919            (nnmail-expiry-target
7920             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7921                 nnmail-expiry-target))
7922            es)
7923       (when expirable
7924         ;; There are expirable articles in this group, so we run them
7925         ;; through the expiry process.
7926         (gnus-message 6 "Expiring articles...")
7927         (unless (gnus-check-group gnus-newsgroup-name)
7928           (error "Can't open server for %s" gnus-newsgroup-name))
7929         ;; The list of articles that weren't expired is returned.
7930         (save-excursion
7931           (if expiry-wait
7932               (let ((nnmail-expiry-wait-function nil)
7933                     (nnmail-expiry-wait expiry-wait))
7934                 (setq es (gnus-request-expire-articles
7935                           expirable gnus-newsgroup-name)))
7936             (setq es (gnus-request-expire-articles
7937                       expirable gnus-newsgroup-name)))
7938           (unless total
7939             (setq gnus-newsgroup-expirable es))
7940           ;; We go through the old list of expirable, and mark all
7941           ;; really expired articles as nonexistent.
7942           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
7943             (let ((gnus-use-cache nil))
7944               (while expirable
7945                 (unless (memq (car expirable) es)
7946                   (when (gnus-data-find (car expirable))
7947                     (gnus-summary-mark-article
7948                      (car expirable) gnus-canceled-mark)))
7949                 (setq expirable (cdr expirable))))))
7950         (gnus-message 6 "Expiring articles...done")))))
7951
7952 (defun gnus-summary-expire-articles-now ()
7953   "Expunge all expirable articles in the current group.
7954 This means that *all* articles that are marked as expirable will be
7955 deleted forever, right now."
7956   (interactive)
7957   (or gnus-expert-user
7958       (gnus-yes-or-no-p
7959        "Are you really, really, really sure you want to delete all these messages? ")
7960       (error "Phew!"))
7961   (gnus-summary-expire-articles t))
7962
7963 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7964 (defun gnus-summary-delete-article (&optional n)
7965   "Delete the N next (mail) articles.
7966 This command actually deletes articles.  This is not a marking
7967 command.  The article will disappear forever from your life, never to
7968 return.
7969 If N is negative, delete backwards.
7970 If N is nil and articles have been marked with the process mark,
7971 delete these instead."
7972   (interactive "P")
7973   (unless (gnus-check-backend-function 'request-expire-articles
7974                                        gnus-newsgroup-name)
7975     (error "The current newsgroup does not support article deletion"))
7976   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7977     (error "Couldn't open server"))
7978   ;; Compute the list of articles to delete.
7979   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7980         not-deleted)
7981     (if (and gnus-novice-user
7982              (not (gnus-yes-or-no-p
7983                    (format "Do you really want to delete %s forever? "
7984                            (if (> (length articles) 1)
7985                                (format "these %s articles" (length articles))
7986                              "this article")))))
7987         ()
7988       ;; Delete the articles.
7989       (setq not-deleted (gnus-request-expire-articles
7990                          articles gnus-newsgroup-name 'force))
7991       (while articles
7992         (gnus-summary-remove-process-mark (car articles))
7993         ;; The backend might not have been able to delete the article
7994         ;; after all.
7995         (unless (memq (car articles) not-deleted)
7996           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7997         (setq articles (cdr articles)))
7998       (when not-deleted
7999         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8000     (gnus-summary-position-point)
8001     (gnus-set-mode-line 'summary)
8002     not-deleted))
8003
8004 (defun gnus-summary-edit-article (&optional arg)
8005   "Edit the current article.
8006 This will have permanent effect only in mail groups.
8007 If ARG is nil, edit the decoded articles.
8008 If ARG is 1, edit the raw articles. 
8009 If ARG is 2, edit the raw articles even in read-only groups.
8010 If ARG is 3, edit the articles with the current handles.
8011 Otherwise, allow editing of articles even in read-only
8012 groups."
8013   (interactive "P")
8014   (let (force raw current-handles)
8015     (cond 
8016      ((null arg))
8017      ((eq arg 1) (setq raw t))
8018      ((eq arg 2) (setq raw t
8019                        force t))
8020      ((eq arg 3) (setq current-handles 
8021                        (and (gnus-buffer-live-p gnus-article-buffer)
8022                             (with-current-buffer gnus-article-buffer
8023                               (prog1
8024                                   gnus-article-mime-handles
8025                                   (setq gnus-article-mime-handles nil))))))
8026      (t (setq force t)))
8027     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8028         (error "Can't edit the raw article in group nndraft:drafts."))
8029     (save-excursion
8030       (set-buffer gnus-summary-buffer)
8031       (let ((mail-parse-charset gnus-newsgroup-charset)
8032             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8033         (gnus-set-global-variables)
8034         (when (and (not force)
8035                    (gnus-group-read-only-p))
8036           (error "The current newsgroup does not support article editing"))
8037         (gnus-summary-show-article t)
8038         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8039           (with-current-buffer gnus-article-buffer
8040             (mm-enable-multibyte-mule4)))
8041         (if (equal gnus-newsgroup-name "nndraft:drafts")
8042             (setq raw t))
8043         (gnus-article-edit-article
8044          (if raw 'ignore 
8045            `(lambda () 
8046               (let ((mbl mml-buffer-list))
8047                 (setq mml-buffer-list nil)
8048                 (mime-to-mml ,'current-handles)
8049                 (make-local-hook 'kill-buffer-hook)
8050                 (let ((mbl1 mml-buffer-list))
8051                   (setq mml-buffer-list mbl)
8052                   (set (make-local-variable 'mml-buffer-list) mbl1))
8053                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8054          `(lambda (no-highlight)
8055             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8056                   (message-options message-options)
8057                   (message-options-set-recipient)
8058                   (mail-parse-ignored-charsets 
8059                    ',gnus-newsgroup-ignored-charsets))
8060               ,(if (not raw) '(progn 
8061                                 (mml-to-mime)
8062                                 (mml-destroy-buffers)
8063                                 (remove-hook 'kill-buffer-hook 
8064                                              'mml-destroy-buffers t)
8065                                 (kill-local-variable 'mml-buffer-list)))
8066               (gnus-summary-edit-article-done
8067                ,(or (mail-header-references gnus-current-headers) "")
8068                ,(gnus-group-read-only-p) 
8069                ,gnus-summary-buffer no-highlight))))))))
8070
8071 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8072
8073 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8074                                                  no-highlight)
8075   "Make edits to the current article permanent."
8076   (interactive)
8077   (save-excursion
8078     ;; The buffer restriction contains the entire article if it exists.
8079     (when (article-goto-body)
8080       (let ((lines (count-lines (point) (point-max)))
8081             (length (- (point-max) (point)))
8082             (case-fold-search t)
8083             (body (copy-marker (point))))
8084         (goto-char (point-min))
8085         (when (re-search-forward "^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
8090                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8091           (delete-region (match-beginning 1) (match-end 1))
8092           (insert (number-to-string length)))
8093         (goto-char (point-min))
8094         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8095           (delete-region (match-beginning 1) (match-end 1))
8096           (insert (number-to-string lines))))))
8097   ;; Replace the article.
8098   (let ((buf (current-buffer)))
8099     (with-temp-buffer
8100       (insert-buffer-substring buf)
8101       
8102       (if (and (not read-only)
8103                (not (gnus-request-replace-article
8104                      (cdr gnus-article-current) (car gnus-article-current)
8105                      (current-buffer) t)))
8106           (error "Couldn't replace article")
8107         ;; Update the summary buffer.
8108         (if (and references
8109                  (equal (message-tokenize-header references " ")
8110                         (message-tokenize-header
8111                          (or (message-fetch-field "references") "") " ")))
8112             ;; We only have to update this line.
8113             (save-excursion
8114               (save-restriction
8115                 (message-narrow-to-head)
8116                 (let ((head (buffer-string))
8117                       header)
8118                   (with-temp-buffer
8119                     (insert (format "211 %d Article retrieved.\n"
8120                                     (cdr gnus-article-current)))
8121                     (insert head)
8122                     (insert ".\n")
8123                     (let ((nntp-server-buffer (current-buffer)))
8124                       (setq header (car (gnus-get-newsgroup-headers
8125                                          (save-excursion
8126                                            (set-buffer gnus-summary-buffer)
8127                                            gnus-newsgroup-dependencies)
8128                                          t))))
8129                     (save-excursion
8130                       (set-buffer gnus-summary-buffer)
8131                       (gnus-data-set-header
8132                        (gnus-data-find (cdr gnus-article-current))
8133                        header)
8134                       (gnus-summary-update-article-line
8135                        (cdr gnus-article-current) header))))))
8136           ;; Update threads.
8137           (set-buffer (or buffer gnus-summary-buffer))
8138           (gnus-summary-update-article (cdr gnus-article-current)))
8139         ;; Prettify the article buffer again.
8140         (unless no-highlight
8141           (save-excursion
8142             (set-buffer gnus-article-buffer)
8143             ;;;!!! Fix this -- article should be rehighlighted.
8144             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8145             (set-buffer gnus-original-article-buffer)
8146             (gnus-request-article
8147              (cdr gnus-article-current)
8148              (car gnus-article-current) (current-buffer))))
8149         ;; Prettify the summary buffer line.
8150         (when (gnus-visual-p 'summary-highlight 'highlight)
8151           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8152
8153 (defun gnus-summary-edit-wash (key)
8154   "Perform editing command KEY in the article buffer."
8155   (interactive
8156    (list
8157     (progn
8158       (message "%s" (concat (this-command-keys) "- "))
8159       (read-char))))
8160   (message "")
8161   (gnus-summary-edit-article)
8162   (execute-kbd-macro (concat (this-command-keys) key))
8163   (gnus-article-edit-done))
8164
8165 ;;; Respooling
8166
8167 (defun gnus-summary-respool-query (&optional silent trace)
8168   "Query where the respool algorithm would put this article."
8169   (interactive)
8170   (let (gnus-mark-article-hook)
8171     (gnus-summary-select-article)
8172     (save-excursion
8173       (set-buffer gnus-original-article-buffer)
8174       (save-restriction
8175         (message-narrow-to-head)
8176         (let ((groups (nnmail-article-group 'identity trace)))
8177           (unless silent
8178             (if groups
8179                 (message "This message would go to %s"
8180                          (mapconcat 'car groups ", "))
8181               (message "This message would go to no groups"))
8182             groups))))))
8183
8184 (defun gnus-summary-respool-trace ()
8185   "Trace where the respool algorithm would put this article.
8186 Display a buffer showing all fancy splitting patterns which matched."
8187   (interactive)
8188   (gnus-summary-respool-query nil t))
8189
8190 ;; Summary marking commands.
8191
8192 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8193   "Mark articles which has the same subject as read, and then select the next.
8194 If UNMARK is positive, remove any kind of mark.
8195 If UNMARK is negative, tick articles."
8196   (interactive "P")
8197   (when unmark
8198     (setq unmark (prefix-numeric-value unmark)))
8199   (let ((count
8200          (gnus-summary-mark-same-subject
8201           (gnus-summary-article-subject) unmark)))
8202     ;; Select next unread article.  If auto-select-same mode, should
8203     ;; select the first unread article.
8204     (gnus-summary-next-article t (and gnus-auto-select-same
8205                                       (gnus-summary-article-subject)))
8206     (gnus-message 7 "%d article%s marked as %s"
8207                   count (if (= count 1) " is" "s are")
8208                   (if unmark "unread" "read"))))
8209
8210 (defun gnus-summary-kill-same-subject (&optional unmark)
8211   "Mark articles which has the same subject as read.
8212 If UNMARK is positive, remove any kind of mark.
8213 If UNMARK is negative, tick articles."
8214   (interactive "P")
8215   (when unmark
8216     (setq unmark (prefix-numeric-value unmark)))
8217   (let ((count
8218          (gnus-summary-mark-same-subject
8219           (gnus-summary-article-subject) unmark)))
8220     ;; If marked as read, go to next unread subject.
8221     (when (null unmark)
8222       ;; Go to next unread subject.
8223       (gnus-summary-next-subject 1 t))
8224     (gnus-message 7 "%d articles are marked as %s"
8225                   count (if unmark "unread" "read"))))
8226
8227 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8228   "Mark articles with same SUBJECT as read, and return marked number.
8229 If optional argument UNMARK is positive, remove any kinds of marks.
8230 If optional argument UNMARK is negative, mark articles as unread instead."
8231   (let ((count 1))
8232     (save-excursion
8233       (cond
8234        ((null unmark)                   ; Mark as read.
8235         (while (and
8236                 (progn
8237                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8238                   (gnus-summary-show-thread) t)
8239                 (gnus-summary-find-subject subject))
8240           (setq count (1+ count))))
8241        ((> unmark 0)                    ; Tick.
8242         (while (and
8243                 (progn
8244                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8245                   (gnus-summary-show-thread) t)
8246                 (gnus-summary-find-subject subject))
8247           (setq count (1+ count))))
8248        (t                               ; Mark as unread.
8249         (while (and
8250                 (progn
8251                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8252                   (gnus-summary-show-thread) t)
8253                 (gnus-summary-find-subject subject))
8254           (setq count (1+ count)))))
8255       (gnus-set-mode-line 'summary)
8256       ;; Return the number of marked articles.
8257       count)))
8258
8259 (defun gnus-summary-mark-as-processable (n &optional unmark)
8260   "Set the process mark on the next N articles.
8261 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8262 the process mark instead.  The difference between N and the actual
8263 number of articles marked is returned."
8264   (interactive "p")
8265   (let ((backward (< n 0))
8266         (n (abs n)))
8267     (while (and
8268             (> n 0)
8269             (if unmark
8270                 (gnus-summary-remove-process-mark
8271                  (gnus-summary-article-number))
8272               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8273             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8274       (setq n (1- n)))
8275     (when (/= 0 n)
8276       (gnus-message 7 "No more articles"))
8277     (gnus-summary-recenter)
8278     (gnus-summary-position-point)
8279     n))
8280
8281 (defun gnus-summary-unmark-as-processable (n)
8282   "Remove the process mark from the next N articles.
8283 If N is negative, unmark backward instead.  The difference between N and
8284 the actual number of articles unmarked is returned."
8285   (interactive "p")
8286   (gnus-summary-mark-as-processable n t))
8287
8288 (defun gnus-summary-unmark-all-processable ()
8289   "Remove the process mark from all articles."
8290   (interactive)
8291   (save-excursion
8292     (while gnus-newsgroup-processable
8293       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8294   (gnus-summary-position-point))
8295
8296 (defun gnus-summary-mark-as-expirable (n)
8297   "Mark N articles forward as expirable.
8298 If N is negative, mark backward instead.  The difference between N and
8299 the actual number of articles marked is returned."
8300   (interactive "p")
8301   (gnus-summary-mark-forward n gnus-expirable-mark))
8302
8303 (defun gnus-summary-mark-article-as-replied (article)
8304   "Mark ARTICLE replied and update the summary line."
8305   (push article gnus-newsgroup-replied)
8306   (let ((buffer-read-only nil))
8307     (when (gnus-summary-goto-subject article nil t)
8308       (gnus-summary-update-secondary-mark article))))
8309
8310 (defun gnus-summary-set-bookmark (article)
8311   "Set a bookmark in current article."
8312   (interactive (list (gnus-summary-article-number)))
8313   (when (or (not (get-buffer gnus-article-buffer))
8314             (not gnus-current-article)
8315             (not gnus-article-current)
8316             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8317     (error "No current article selected"))
8318   ;; Remove old bookmark, if one exists.
8319   (let ((old (assq article gnus-newsgroup-bookmarks)))
8320     (when old
8321       (setq gnus-newsgroup-bookmarks
8322             (delq old gnus-newsgroup-bookmarks))))
8323   ;; Set the new bookmark, which is on the form
8324   ;; (article-number . line-number-in-body).
8325   (push
8326    (cons article
8327          (save-excursion
8328            (set-buffer gnus-article-buffer)
8329            (count-lines
8330             (min (point)
8331                  (save-excursion
8332                    (goto-char (point-min))
8333                    (search-forward "\n\n" nil t)
8334                    (point)))
8335             (point))))
8336    gnus-newsgroup-bookmarks)
8337   (gnus-message 6 "A bookmark has been added to the current article."))
8338
8339 (defun gnus-summary-remove-bookmark (article)
8340   "Remove the bookmark from the current article."
8341   (interactive (list (gnus-summary-article-number)))
8342   ;; Remove old bookmark, if one exists.
8343   (let ((old (assq article gnus-newsgroup-bookmarks)))
8344     (if old
8345         (progn
8346           (setq gnus-newsgroup-bookmarks
8347                 (delq old gnus-newsgroup-bookmarks))
8348           (gnus-message 6 "Removed bookmark."))
8349       (gnus-message 6 "No bookmark in current article."))))
8350
8351 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8352 (defun gnus-summary-mark-as-dormant (n)
8353   "Mark N articles forward as dormant.
8354 If N is negative, mark backward instead.  The difference between N and
8355 the actual number of articles marked is returned."
8356   (interactive "p")
8357   (gnus-summary-mark-forward n gnus-dormant-mark))
8358
8359 (defun gnus-summary-set-process-mark (article)
8360   "Set the process mark on ARTICLE and update the summary line."
8361   (setq gnus-newsgroup-processable
8362         (cons article
8363               (delq article gnus-newsgroup-processable)))
8364   (when (gnus-summary-goto-subject article)
8365     (gnus-summary-show-thread)
8366     (gnus-summary-goto-subject article)
8367     (gnus-summary-update-secondary-mark article)))
8368
8369 (defun gnus-summary-remove-process-mark (article)
8370   "Remove the process mark from ARTICLE and update the summary line."
8371   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8372   (when (gnus-summary-goto-subject article)
8373     (gnus-summary-show-thread)
8374     (gnus-summary-goto-subject article)
8375     (gnus-summary-update-secondary-mark article)))
8376
8377 (defun gnus-summary-set-saved-mark (article)
8378   "Set the process mark on ARTICLE and update the summary line."
8379   (push article gnus-newsgroup-saved)
8380   (when (gnus-summary-goto-subject article)
8381     (gnus-summary-update-secondary-mark article)))
8382
8383 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8384   "Mark N articles as read forwards.
8385 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8386 The difference between N and the actual number of articles marked is
8387 returned.
8388 Iff NO-EXPIRE, auto-expiry will be inhibited."
8389   (interactive "p")
8390   (gnus-summary-show-thread)
8391   (let ((backward (< n 0))
8392         (gnus-summary-goto-unread
8393          (and gnus-summary-goto-unread
8394               (not (eq gnus-summary-goto-unread 'never))
8395               (not (memq mark (list gnus-unread-mark
8396                                     gnus-ticked-mark gnus-dormant-mark)))))
8397         (n (abs n))
8398         (mark (or mark gnus-del-mark)))
8399     (while (and (> n 0)
8400                 (gnus-summary-mark-article nil mark no-expire)
8401                 (zerop (gnus-summary-next-subject
8402                         (if backward -1 1)
8403                         (and gnus-summary-goto-unread
8404                              (not (eq gnus-summary-goto-unread 'never)))
8405                         t)))
8406       (setq n (1- n)))
8407     (when (/= 0 n)
8408       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8409     (gnus-summary-recenter)
8410     (gnus-summary-position-point)
8411     (gnus-set-mode-line 'summary)
8412     n))
8413
8414 (defun gnus-summary-mark-article-as-read (mark)
8415   "Mark the current article quickly as read with MARK."
8416   (let ((article (gnus-summary-article-number)))
8417     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8418     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8419     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8420     (push (cons article mark) gnus-newsgroup-reads)
8421     ;; Possibly remove from cache, if that is used.
8422     (when gnus-use-cache
8423       (gnus-cache-enter-remove-article article))
8424     ;; Allow the backend to change the mark.
8425     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8426     ;; Check for auto-expiry.
8427     (when (and gnus-newsgroup-auto-expire
8428                (memq mark gnus-auto-expirable-marks))
8429       (setq mark gnus-expirable-mark)
8430       ;; Let the backend know about the mark change.
8431       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8432       (push article gnus-newsgroup-expirable))
8433     ;; Set the mark in the buffer.
8434     (gnus-summary-update-mark mark 'unread)
8435     t))
8436
8437 (defun gnus-summary-mark-article-as-unread (mark)
8438   "Mark the current article quickly as unread with MARK."
8439   (let* ((article (gnus-summary-article-number))
8440          (old-mark (gnus-summary-article-mark article)))
8441     ;; Allow the backend to change the mark.
8442     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8443     (if (eq mark old-mark)
8444         t
8445       (if (<= article 0)
8446           (progn
8447             (gnus-error 1 "Can't mark negative article numbers")
8448             nil)
8449         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8450         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8451         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8452         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8453         (cond ((= mark gnus-ticked-mark)
8454                (push article gnus-newsgroup-marked))
8455               ((= mark gnus-dormant-mark)
8456                (push article gnus-newsgroup-dormant))
8457               (t
8458                (push article gnus-newsgroup-unreads)))
8459         (gnus-pull article gnus-newsgroup-reads)
8460
8461         ;; See whether the article is to be put in the cache.
8462         (and gnus-use-cache
8463              (vectorp (gnus-summary-article-header article))
8464              (save-excursion
8465                (gnus-cache-possibly-enter-article
8466                 gnus-newsgroup-name article
8467                 (= mark gnus-ticked-mark)
8468                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8469
8470         ;; Fix the mark.
8471         (gnus-summary-update-mark mark 'unread)
8472         t))))
8473
8474 (defun gnus-summary-mark-article (&optional article mark no-expire)
8475   "Mark ARTICLE with MARK.  MARK can be any character.
8476 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8477 `??' (dormant) and `?E' (expirable).
8478 If MARK is nil, then the default character `?r' is used.
8479 If ARTICLE is nil, then the article on the current line will be
8480 marked.
8481 Iff NO-EXPIRE, auto-expiry will be inhibited."
8482   ;; The mark might be a string.
8483   (when (stringp mark)
8484     (setq mark (aref mark 0)))
8485   ;; If no mark is given, then we check auto-expiring.
8486   (when (null mark)
8487     (setq mark gnus-del-mark))
8488   (when (and (not no-expire)
8489              gnus-newsgroup-auto-expire
8490              (memq mark gnus-auto-expirable-marks))
8491     (setq mark gnus-expirable-mark))
8492   (let ((article (or article (gnus-summary-article-number)))
8493         (old-mark (gnus-summary-article-mark article)))
8494     ;; Allow the backend to change the mark.
8495     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8496     (if (eq mark old-mark)
8497         t
8498       (unless article
8499         (error "No article on current line"))
8500       (if (not (if (or (= mark gnus-unread-mark)
8501                        (= mark gnus-ticked-mark)
8502                        (= mark gnus-dormant-mark))
8503                    (gnus-mark-article-as-unread article mark)
8504                  (gnus-mark-article-as-read article mark)))
8505           t
8506         ;; See whether the article is to be put in the cache.
8507         (and gnus-use-cache
8508              (not (= mark gnus-canceled-mark))
8509              (vectorp (gnus-summary-article-header article))
8510              (save-excursion
8511                (gnus-cache-possibly-enter-article
8512                 gnus-newsgroup-name article
8513                 (= mark gnus-ticked-mark)
8514                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8515
8516         (when (gnus-summary-goto-subject article nil t)
8517           (let ((buffer-read-only nil))
8518             (gnus-summary-show-thread)
8519             ;; Fix the mark.
8520             (gnus-summary-update-mark mark 'unread)
8521             t))))))
8522
8523 (defun gnus-summary-update-secondary-mark (article)
8524   "Update the secondary (read, process, cache) mark."
8525   (gnus-summary-update-mark
8526    (cond ((memq article gnus-newsgroup-processable)
8527           gnus-process-mark)
8528          ((memq article gnus-newsgroup-cached)
8529           gnus-cached-mark)
8530          ((memq article gnus-newsgroup-replied)
8531           gnus-replied-mark)
8532          ((memq article gnus-newsgroup-saved)
8533           gnus-saved-mark)
8534          (t gnus-no-mark))
8535    'replied)
8536   (when (gnus-visual-p 'summary-highlight 'highlight)
8537     (gnus-run-hooks 'gnus-summary-update-hook))
8538   t)
8539
8540 (defun gnus-summary-update-mark (mark type)
8541   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8542         (buffer-read-only nil))
8543     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8544     (when forward
8545       (when (looking-at "\r")
8546         (incf forward))
8547       (when (<= (+ forward (point)) (point-max))
8548         ;; Go to the right position on the line.
8549         (goto-char (+ forward (point)))
8550         ;; Replace the old mark with the new mark.
8551         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8552         ;; Optionally update the marks by some user rule.
8553         (when (eq type 'unread)
8554           (gnus-data-set-mark
8555            (gnus-data-find (gnus-summary-article-number)) mark)
8556           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8557
8558 (defun gnus-mark-article-as-read (article &optional mark)
8559   "Enter ARTICLE in the pertinent lists and remove it from others."
8560   ;; Make the article expirable.
8561   (let ((mark (or mark gnus-del-mark)))
8562     (if (= mark gnus-expirable-mark)
8563         (push article gnus-newsgroup-expirable)
8564       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8565     ;; Remove from unread and marked lists.
8566     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8567     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8568     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8569     (push (cons article mark) gnus-newsgroup-reads)
8570     ;; Possibly remove from cache, if that is used.
8571     (when gnus-use-cache
8572       (gnus-cache-enter-remove-article article))
8573     t))
8574
8575 (defun gnus-mark-article-as-unread (article &optional mark)
8576   "Enter ARTICLE in the pertinent lists and remove it from others."
8577   (let ((mark (or mark gnus-ticked-mark)))
8578     (if (<= article 0)
8579         (progn
8580           (gnus-error 1 "Can't mark negative article numbers")
8581           nil)
8582       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8583             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8584             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8585             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8586
8587       ;; Unsuppress duplicates?
8588       (when gnus-suppress-duplicates
8589         (gnus-dup-unsuppress-article article))
8590
8591       (cond ((= mark gnus-ticked-mark)
8592              (push article gnus-newsgroup-marked))
8593             ((= mark gnus-dormant-mark)
8594              (push article gnus-newsgroup-dormant))
8595             (t
8596              (push article gnus-newsgroup-unreads)))
8597       (gnus-pull article gnus-newsgroup-reads)
8598       t)))
8599
8600 (defalias 'gnus-summary-mark-as-unread-forward
8601   'gnus-summary-tick-article-forward)
8602 (make-obsolete 'gnus-summary-mark-as-unread-forward
8603                'gnus-summary-tick-article-forward)
8604 (defun gnus-summary-tick-article-forward (n)
8605   "Tick N articles forwards.
8606 If N is negative, tick backwards instead.
8607 The difference between N and the number of articles ticked is returned."
8608   (interactive "p")
8609   (gnus-summary-mark-forward n gnus-ticked-mark))
8610
8611 (defalias 'gnus-summary-mark-as-unread-backward
8612   'gnus-summary-tick-article-backward)
8613 (make-obsolete 'gnus-summary-mark-as-unread-backward
8614                'gnus-summary-tick-article-backward)
8615 (defun gnus-summary-tick-article-backward (n)
8616   "Tick N articles backwards.
8617 The difference between N and the number of articles ticked is returned."
8618   (interactive "p")
8619   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8620
8621 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8622 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8623 (defun gnus-summary-tick-article (&optional article clear-mark)
8624   "Mark current article as unread.
8625 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8626 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8627   (interactive)
8628   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8629                                        gnus-ticked-mark)))
8630
8631 (defun gnus-summary-mark-as-read-forward (n)
8632   "Mark N articles as read forwards.
8633 If N is negative, mark backwards instead.
8634 The difference between N and the actual number of articles marked is
8635 returned."
8636   (interactive "p")
8637   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8638
8639 (defun gnus-summary-mark-as-read-backward (n)
8640   "Mark the N articles as read backwards.
8641 The difference between N and the actual number of articles marked is
8642 returned."
8643   (interactive "p")
8644   (gnus-summary-mark-forward
8645    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8646
8647 (defun gnus-summary-mark-as-read (&optional article mark)
8648   "Mark current article as read.
8649 ARTICLE specifies the article to be marked as read.
8650 MARK specifies a string to be inserted at the beginning of the line."
8651   (gnus-summary-mark-article article mark))
8652
8653 (defun gnus-summary-clear-mark-forward (n)
8654   "Clear marks from N articles forward.
8655 If N is negative, clear backward instead.
8656 The difference between N and the number of marks cleared is returned."
8657   (interactive "p")
8658   (gnus-summary-mark-forward n gnus-unread-mark))
8659
8660 (defun gnus-summary-clear-mark-backward (n)
8661   "Clear marks from N articles backward.
8662 The difference between N and the number of marks cleared is returned."
8663   (interactive "p")
8664   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8665
8666 (defun gnus-summary-mark-unread-as-read ()
8667   "Intended to be used by `gnus-summary-mark-article-hook'."
8668   (when (memq gnus-current-article gnus-newsgroup-unreads)
8669     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8670
8671 (defun gnus-summary-mark-read-and-unread-as-read ()
8672   "Intended to be used by `gnus-summary-mark-article-hook'."
8673   (let ((mark (gnus-summary-article-mark)))
8674     (when (or (gnus-unread-mark-p mark)
8675               (gnus-read-mark-p mark))
8676       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8677
8678 (defun gnus-summary-mark-unread-as-ticked ()
8679    "Intended to be used by `gnus-summary-mark-article-hook'."
8680   (when (memq gnus-current-article gnus-newsgroup-unreads)
8681     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8682
8683 (defun gnus-summary-mark-region-as-read (point mark all)
8684   "Mark all unread articles between point and mark as read.
8685 If given a prefix, mark all articles between point and mark as read,
8686 even ticked and dormant ones."
8687   (interactive "r\nP")
8688   (save-excursion
8689     (let (article)
8690       (goto-char point)
8691       (beginning-of-line)
8692       (while (and
8693               (< (point) mark)
8694               (progn
8695                 (when (or all
8696                           (memq (setq article (gnus-summary-article-number))
8697                                 gnus-newsgroup-unreads))
8698                   (gnus-summary-mark-article article gnus-del-mark))
8699                 t)
8700               (gnus-summary-find-next))))))
8701
8702 (defun gnus-summary-mark-below (score mark)
8703   "Mark articles with score less than SCORE with MARK."
8704   (interactive "P\ncMark: ")
8705   (setq score (if score
8706                   (prefix-numeric-value score)
8707                 (or gnus-summary-default-score 0)))
8708   (save-excursion
8709     (set-buffer gnus-summary-buffer)
8710     (goto-char (point-min))
8711     (while
8712         (progn
8713           (and (< (gnus-summary-article-score) score)
8714                (gnus-summary-mark-article nil mark))
8715           (gnus-summary-find-next)))))
8716
8717 (defun gnus-summary-kill-below (&optional score)
8718   "Mark articles with score below SCORE as read."
8719   (interactive "P")
8720   (gnus-summary-mark-below score gnus-killed-mark))
8721
8722 (defun gnus-summary-clear-above (&optional score)
8723   "Clear all marks from articles with score above SCORE."
8724   (interactive "P")
8725   (gnus-summary-mark-above score gnus-unread-mark))
8726
8727 (defun gnus-summary-tick-above (&optional score)
8728   "Tick all articles with score above SCORE."
8729   (interactive "P")
8730   (gnus-summary-mark-above score gnus-ticked-mark))
8731
8732 (defun gnus-summary-mark-above (score mark)
8733   "Mark articles with score over SCORE with MARK."
8734   (interactive "P\ncMark: ")
8735   (setq score (if score
8736                   (prefix-numeric-value score)
8737                 (or gnus-summary-default-score 0)))
8738   (save-excursion
8739     (set-buffer gnus-summary-buffer)
8740     (goto-char (point-min))
8741     (while (and (progn
8742                   (when (> (gnus-summary-article-score) score)
8743                     (gnus-summary-mark-article nil mark))
8744                   t)
8745                 (gnus-summary-find-next)))))
8746
8747 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8748 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8749 (defun gnus-summary-limit-include-expunged (&optional no-error)
8750   "Display all the hidden articles that were expunged for low scores."
8751   (interactive)
8752   (let ((buffer-read-only nil))
8753     (let ((scored gnus-newsgroup-scored)
8754           headers h)
8755       (while scored
8756         (unless (gnus-number-to-header (caar scored))
8757           (and (setq h (gnus-summary-article-header (caar scored)))
8758                (< (cdar scored) gnus-summary-expunge-below)
8759                (push h headers)))
8760         (setq scored (cdr scored)))
8761       (if (not headers)
8762           (when (not no-error)
8763             (error "No expunged articles hidden"))
8764         (goto-char (point-min))
8765         (gnus-summary-prepare-unthreaded (nreverse headers))
8766         (goto-char (point-min))
8767         (gnus-summary-position-point)
8768         t))))
8769
8770 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8771   "Mark all unread articles in this newsgroup as read.
8772 If prefix argument ALL is non-nil, ticked and dormant articles will
8773 also be marked as read.
8774 If QUIETLY is non-nil, no questions will be asked.
8775 If TO-HERE is non-nil, it should be a point in the buffer.  All
8776 articles before this point will be marked as read.
8777 Note that this function will only catch up the unread article
8778 in the current summary buffer limitation.
8779 The number of articles marked as read is returned."
8780   (interactive "P")
8781   (prog1
8782       (save-excursion
8783         (when (or quietly
8784                   (not gnus-interactive-catchup) ;Without confirmation?
8785                   gnus-expert-user
8786                   (gnus-y-or-n-p
8787                    (if all
8788                        "Mark absolutely all articles as read? "
8789                      "Mark all unread articles as read? ")))
8790           (if (and not-mark
8791                    (not gnus-newsgroup-adaptive)
8792                    (not gnus-newsgroup-auto-expire)
8793                    (not gnus-suppress-duplicates)
8794                    (or (not gnus-use-cache)
8795                        (eq gnus-use-cache 'passive)))
8796               (progn
8797                 (when all
8798                   (setq gnus-newsgroup-marked nil
8799                         gnus-newsgroup-dormant nil))
8800                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8801             ;; We actually mark all articles as canceled, which we
8802             ;; have to do when using auto-expiry or adaptive scoring.
8803             (gnus-summary-show-all-threads)
8804             (when (gnus-summary-first-subject (not all) t)
8805               (while (and
8806                       (if to-here (< (point) to-here) t)
8807                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8808                       (gnus-summary-find-next (not all) nil nil t))))
8809             (gnus-set-mode-line 'summary))
8810           t))
8811     (gnus-summary-position-point)))
8812
8813 (defun gnus-summary-catchup-to-here (&optional all)
8814   "Mark all unticked articles before the current one as read.
8815 If ALL is non-nil, also mark ticked and dormant articles as read."
8816   (interactive "P")
8817   (save-excursion
8818     (gnus-save-hidden-threads
8819       (let ((beg (point)))
8820         ;; We check that there are unread articles.
8821         (when (or all (gnus-summary-find-prev))
8822           (gnus-summary-catchup all t beg)))))
8823   (gnus-summary-position-point))
8824
8825 (defun gnus-summary-catchup-all (&optional quietly)
8826   "Mark all articles in this newsgroup as read."
8827   (interactive "P")
8828   (gnus-summary-catchup t quietly))
8829
8830 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8831   "Mark all unread articles in this group as read, then exit.
8832 If prefix argument ALL is non-nil, all articles are marked as read."
8833   (interactive "P")
8834   (when (gnus-summary-catchup all quietly nil 'fast)
8835     ;; Select next newsgroup or exit.
8836     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8837              (eq gnus-auto-select-next 'quietly))
8838         (gnus-summary-next-group nil)
8839       (gnus-summary-exit))))
8840
8841 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8842   "Mark all articles in this newsgroup as read, and then exit."
8843   (interactive "P")
8844   (gnus-summary-catchup-and-exit t quietly))
8845
8846 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8847   "Mark all articles in this group as read and select the next group.
8848 If given a prefix, mark all articles, unread as well as ticked, as
8849 read."
8850   (interactive "P")
8851   (save-excursion
8852     (gnus-summary-catchup all))
8853   (gnus-summary-next-group))
8854
8855 ;;;
8856 ;;; with article
8857 ;;;
8858
8859 (defmacro gnus-with-article (article &rest forms)
8860   "Select ARTICLE and perform FORMS in the original article buffer.
8861 Then replace the article with the result."
8862   `(progn
8863      ;; We don't want the article to be marked as read.
8864      (let (gnus-mark-article-hook)
8865        (gnus-summary-select-article t t nil ,article))
8866      (set-buffer gnus-original-article-buffer)
8867      ,@forms
8868      (if (not (gnus-check-backend-function
8869                'request-replace-article (car gnus-article-current)))
8870          (gnus-message 5 "Read-only group; not replacing")
8871        (unless (gnus-request-replace-article
8872                 ,article (car gnus-article-current)
8873                 (current-buffer) t)
8874          (error "Couldn't replace article")))
8875      ;; The cache and backlog have to be flushed somewhat.
8876      (when gnus-keep-backlog
8877        (gnus-backlog-remove-article
8878         (car gnus-article-current) (cdr gnus-article-current)))
8879      (when gnus-use-cache
8880        (gnus-cache-update-article
8881         (car gnus-article-current) (cdr gnus-article-current)))))
8882
8883 (put 'gnus-with-article 'lisp-indent-function 1)
8884 (put 'gnus-with-article 'edebug-form-spec '(form body))
8885
8886 ;; Thread-based commands.
8887
8888 (defun gnus-summary-articles-in-thread (&optional article)
8889   "Return a list of all articles in the current thread.
8890 If ARTICLE is non-nil, return all articles in the thread that starts
8891 with that article."
8892   (let* ((article (or article (gnus-summary-article-number)))
8893          (data (gnus-data-find-list article))
8894          (top-level (gnus-data-level (car data)))
8895          (top-subject
8896           (cond ((null gnus-thread-operation-ignore-subject)
8897                  (gnus-simplify-subject-re
8898                   (mail-header-subject (gnus-data-header (car data)))))
8899                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8900                  (gnus-simplify-subject-fuzzy
8901                   (mail-header-subject (gnus-data-header (car data)))))
8902                 (t nil)))
8903          (end-point (save-excursion
8904                       (if (gnus-summary-go-to-next-thread)
8905                           (point) (point-max))))
8906          articles)
8907     (while (and data
8908                 (< (gnus-data-pos (car data)) end-point))
8909       (when (or (not top-subject)
8910                 (string= top-subject
8911                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8912                              (gnus-simplify-subject-fuzzy
8913                               (mail-header-subject
8914                                (gnus-data-header (car data))))
8915                            (gnus-simplify-subject-re
8916                             (mail-header-subject
8917                              (gnus-data-header (car data)))))))
8918         (push (gnus-data-number (car data)) articles))
8919       (unless (and (setq data (cdr data))
8920                    (> (gnus-data-level (car data)) top-level))
8921         (setq data nil)))
8922     ;; Return the list of articles.
8923     (nreverse articles)))
8924
8925 (defun gnus-summary-rethread-current ()
8926   "Rethread the thread the current article is part of."
8927   (interactive)
8928   (let* ((gnus-show-threads t)
8929          (article (gnus-summary-article-number))
8930          (id (mail-header-id (gnus-summary-article-header)))
8931          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8932     (unless id
8933       (error "No article on the current line"))
8934     (gnus-rebuild-thread id)
8935     (gnus-summary-goto-subject article)))
8936
8937 (defun gnus-summary-reparent-thread ()
8938   "Make the current article child of the marked (or previous) article.
8939
8940 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8941 is non-nil or the Subject: of both articles are the same."
8942   (interactive)
8943   (unless (not (gnus-group-read-only-p))
8944     (error "The current newsgroup does not support article editing"))
8945   (unless (<= (length gnus-newsgroup-processable) 1)
8946     (error "No more than one article may be marked"))
8947   (save-window-excursion
8948     (let ((gnus-article-buffer " *reparent*")
8949           (current-article (gnus-summary-article-number))
8950           ;; First grab the marked article, otherwise one line up.
8951           (parent-article (if (not (null gnus-newsgroup-processable))
8952                               (car gnus-newsgroup-processable)
8953                             (save-excursion
8954                               (if (eq (forward-line -1) 0)
8955                                   (gnus-summary-article-number)
8956                                 (error "Beginning of summary buffer"))))))
8957       (unless (not (eq current-article parent-article))
8958         (error "An article may not be self-referential"))
8959       (let ((message-id (mail-header-id
8960                          (gnus-summary-article-header parent-article))))
8961         (unless (and message-id (not (equal message-id "")))
8962           (error "No message-id in desired parent"))
8963         (gnus-with-article current-article
8964           (save-restriction
8965             (goto-char (point-min))
8966             (message-narrow-to-head)
8967             (if (re-search-forward "^References: " nil t)
8968                 (progn
8969                   (re-search-forward "^[^ \t]" nil t)
8970                   (forward-line -1)
8971                   (end-of-line)
8972                   (insert " " message-id))
8973               (insert "References: " message-id "\n"))))
8974         (set-buffer gnus-summary-buffer)
8975         (gnus-summary-unmark-all-processable)
8976         (gnus-summary-update-article current-article)
8977         (gnus-summary-rethread-current)
8978         (gnus-message 3 "Article %d is now the child of article %d"
8979                       current-article parent-article)))))
8980
8981 (defun gnus-summary-toggle-threads (&optional arg)
8982   "Toggle showing conversation threads.
8983 If ARG is positive number, turn showing conversation threads on."
8984   (interactive "P")
8985   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8986     (setq gnus-show-threads
8987           (if (null arg) (not gnus-show-threads)
8988             (> (prefix-numeric-value arg) 0)))
8989     (gnus-summary-prepare)
8990     (gnus-summary-goto-subject current)
8991     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8992     (gnus-summary-position-point)))
8993
8994 (defun gnus-summary-show-all-threads ()
8995   "Show all threads."
8996   (interactive)
8997   (save-excursion
8998     (let ((buffer-read-only nil))
8999       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9000   (gnus-summary-position-point))
9001
9002 (defun gnus-summary-show-thread ()
9003   "Show thread subtrees.
9004 Returns nil if no thread was there to be shown."
9005   (interactive)
9006   (let ((buffer-read-only nil)
9007         (orig (point))
9008         ;; first goto end then to beg, to have point at beg after let
9009         (end (progn (end-of-line) (point)))
9010         (beg (progn (beginning-of-line) (point))))
9011     (prog1
9012         ;; Any hidden lines here?
9013         (search-forward "\r" end t)
9014       (subst-char-in-region beg end ?\^M ?\n t)
9015       (goto-char orig)
9016       (gnus-summary-position-point))))
9017
9018 (defun gnus-summary-hide-all-threads ()
9019   "Hide all thread subtrees."
9020   (interactive)
9021   (save-excursion
9022     (goto-char (point-min))
9023     (gnus-summary-hide-thread)
9024     (while (zerop (gnus-summary-next-thread 1 t))
9025       (gnus-summary-hide-thread)))
9026   (gnus-summary-position-point))
9027
9028 (defun gnus-summary-hide-thread ()
9029   "Hide thread subtrees.
9030 Returns nil if no threads were there to be hidden."
9031   (interactive)
9032   (let ((buffer-read-only nil)
9033         (start (point))
9034         (article (gnus-summary-article-number)))
9035     (goto-char start)
9036     ;; Go forward until either the buffer ends or the subthread
9037     ;; ends.
9038     (when (and (not (eobp))
9039                (or (zerop (gnus-summary-next-thread 1 t))
9040                    (goto-char (point-max))))
9041       (prog1
9042           (if (and (> (point) start)
9043                    (search-backward "\n" start t))
9044               (progn
9045                 (subst-char-in-region start (point) ?\n ?\^M)
9046                 (gnus-summary-goto-subject article))
9047             (goto-char start)
9048             nil)))))
9049
9050 (defun gnus-summary-go-to-next-thread (&optional previous)
9051   "Go to the same level (or less) next thread.
9052 If PREVIOUS is non-nil, go to previous thread instead.
9053 Return the article number moved to, or nil if moving was impossible."
9054   (let ((level (gnus-summary-thread-level))
9055         (way (if previous -1 1))
9056         (beg (point)))
9057     (forward-line way)
9058     (while (and (not (eobp))
9059                 (< level (gnus-summary-thread-level)))
9060       (forward-line way))
9061     (if (eobp)
9062         (progn
9063           (goto-char beg)
9064           nil)
9065       (setq beg (point))
9066       (prog1
9067           (gnus-summary-article-number)
9068         (goto-char beg)))))
9069
9070 (defun gnus-summary-next-thread (n &optional silent)
9071   "Go to the same level next N'th thread.
9072 If N is negative, search backward instead.
9073 Returns the difference between N and the number of skips actually
9074 done.
9075
9076 If SILENT, don't output messages."
9077   (interactive "p")
9078   (let ((backward (< n 0))
9079         (n (abs n)))
9080     (while (and (> n 0)
9081                 (gnus-summary-go-to-next-thread backward))
9082       (decf n))
9083     (unless silent
9084       (gnus-summary-position-point))
9085     (when (and (not silent) (/= 0 n))
9086       (gnus-message 7 "No more threads"))
9087     n))
9088
9089 (defun gnus-summary-prev-thread (n)
9090   "Go to the same level previous N'th thread.
9091 Returns the difference between N and the number of skips actually
9092 done."
9093   (interactive "p")
9094   (gnus-summary-next-thread (- n)))
9095
9096 (defun gnus-summary-go-down-thread ()
9097   "Go down one level in the current thread."
9098   (let ((children (gnus-summary-article-children)))
9099     (when children
9100       (gnus-summary-goto-subject (car children)))))
9101
9102 (defun gnus-summary-go-up-thread ()
9103   "Go up one level in the current thread."
9104   (let ((parent (gnus-summary-article-parent)))
9105     (when parent
9106       (gnus-summary-goto-subject parent))))
9107
9108 (defun gnus-summary-down-thread (n)
9109   "Go down thread N steps.
9110 If N is negative, go up instead.
9111 Returns the difference between N and how many steps down that were
9112 taken."
9113   (interactive "p")
9114   (let ((up (< n 0))
9115         (n (abs n)))
9116     (while (and (> n 0)
9117                 (if up (gnus-summary-go-up-thread)
9118                   (gnus-summary-go-down-thread)))
9119       (setq n (1- n)))
9120     (gnus-summary-position-point)
9121     (when (/= 0 n)
9122       (gnus-message 7 "Can't go further"))
9123     n))
9124
9125 (defun gnus-summary-up-thread (n)
9126   "Go up thread N steps.
9127 If N is negative, go up instead.
9128 Returns the difference between N and how many steps down that were
9129 taken."
9130   (interactive "p")
9131   (gnus-summary-down-thread (- n)))
9132
9133 (defun gnus-summary-top-thread ()
9134   "Go to the top of the thread."
9135   (interactive)
9136   (while (gnus-summary-go-up-thread))
9137   (gnus-summary-article-number))
9138
9139 (defun gnus-summary-kill-thread (&optional unmark)
9140   "Mark articles under current thread as read.
9141 If the prefix argument is positive, remove any kinds of marks.
9142 If the prefix argument is negative, tick articles instead."
9143   (interactive "P")
9144   (when unmark
9145     (setq unmark (prefix-numeric-value unmark)))
9146   (let ((articles (gnus-summary-articles-in-thread)))
9147     (save-excursion
9148       ;; Expand the thread.
9149       (gnus-summary-show-thread)
9150       ;; Mark all the articles.
9151       (while articles
9152         (gnus-summary-goto-subject (car articles))
9153         (cond ((null unmark)
9154                (gnus-summary-mark-article-as-read gnus-killed-mark))
9155               ((> unmark 0)
9156                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9157               (t
9158                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9159         (setq articles (cdr articles))))
9160     ;; Hide killed subtrees.
9161     (and (null unmark)
9162          gnus-thread-hide-killed
9163          (gnus-summary-hide-thread))
9164     ;; If marked as read, go to next unread subject.
9165     (when (null unmark)
9166       ;; Go to next unread subject.
9167       (gnus-summary-next-subject 1 t)))
9168   (gnus-set-mode-line 'summary))
9169
9170 ;; Summary sorting commands
9171
9172 (defun gnus-summary-sort-by-number (&optional reverse)
9173   "Sort the summary buffer by article number.
9174 Argument REVERSE means reverse order."
9175   (interactive "P")
9176   (gnus-summary-sort 'number reverse))
9177
9178 (defun gnus-summary-sort-by-author (&optional reverse)
9179   "Sort the summary buffer by author name alphabetically.
9180 If `case-fold-search' is non-nil, case of letters is ignored.
9181 Argument REVERSE means reverse order."
9182   (interactive "P")
9183   (gnus-summary-sort 'author reverse))
9184
9185 (defun gnus-summary-sort-by-subject (&optional reverse)
9186   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9187 If `case-fold-search' is non-nil, case of letters is ignored.
9188 Argument REVERSE means reverse order."
9189   (interactive "P")
9190   (gnus-summary-sort 'subject reverse))
9191
9192 (defun gnus-summary-sort-by-date (&optional reverse)
9193   "Sort the summary buffer by date.
9194 Argument REVERSE means reverse order."
9195   (interactive "P")
9196   (gnus-summary-sort 'date reverse))
9197
9198 (defun gnus-summary-sort-by-score (&optional reverse)
9199   "Sort the summary buffer by score.
9200 Argument REVERSE means reverse order."
9201   (interactive "P")
9202   (gnus-summary-sort 'score reverse))
9203
9204 (defun gnus-summary-sort-by-lines (&optional reverse)
9205   "Sort the summary buffer by the number of lines.
9206 Argument REVERSE means reverse order."
9207   (interactive "P")
9208   (gnus-summary-sort 'lines reverse))
9209
9210 (defun gnus-summary-sort-by-chars (&optional reverse)
9211   "Sort the summary buffer by article length.
9212 Argument REVERSE means reverse order."
9213   (interactive "P")
9214   (gnus-summary-sort 'chars reverse))
9215
9216 (defun gnus-summary-sort (predicate reverse)
9217   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9218   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9219          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9220          (gnus-thread-sort-functions
9221           (if (not reverse)
9222               thread
9223             `(lambda (t1 t2)
9224                (,thread t2 t1))))
9225          (gnus-sort-gathered-threads-function
9226           gnus-thread-sort-functions)
9227          (gnus-article-sort-functions
9228           (if (not reverse)
9229               article
9230             `(lambda (t1 t2)
9231                (,article t2 t1))))
9232          (buffer-read-only)
9233          (gnus-summary-prepare-hook nil))
9234     ;; We do the sorting by regenerating the threads.
9235     (gnus-summary-prepare)
9236     ;; Hide subthreads if needed.
9237     (when (and gnus-show-threads gnus-thread-hide-subtree)
9238       (gnus-summary-hide-all-threads))))
9239
9240 ;; Summary saving commands.
9241
9242 (defun gnus-summary-save-article (&optional n not-saved)
9243   "Save the current article using the default saver function.
9244 If N is a positive number, save the N next articles.
9245 If N is a negative number, save the N previous articles.
9246 If N is nil and any articles have been marked with the process mark,
9247 save those articles instead.
9248 The variable `gnus-default-article-saver' specifies the saver function."
9249   (interactive "P")
9250   (let* ((articles (gnus-summary-work-articles n))
9251          (save-buffer (save-excursion
9252                         (nnheader-set-temp-buffer " *Gnus Save*")))
9253          (num (length articles))
9254          header file)
9255     (dolist (article articles)
9256       (setq header (gnus-summary-article-header article))
9257       (if (not (vectorp header))
9258           ;; This is a pseudo-article.
9259           (if (assq 'name header)
9260               (gnus-copy-file (cdr (assq 'name header)))
9261             (gnus-message 1 "Article %d is unsaveable" article))
9262         ;; This is a real article.
9263         (save-window-excursion
9264           (gnus-summary-select-article t nil nil article))
9265         (save-excursion
9266           (set-buffer save-buffer)
9267           (erase-buffer)
9268           (insert-buffer-substring gnus-original-article-buffer))
9269         (setq file (gnus-article-save save-buffer file num))
9270         (gnus-summary-remove-process-mark article)
9271         (unless not-saved
9272           (gnus-summary-set-saved-mark article))))
9273     (gnus-kill-buffer save-buffer)
9274     (gnus-summary-position-point)
9275     (gnus-set-mode-line 'summary)
9276     n))
9277
9278 (defun gnus-summary-pipe-output (&optional arg)
9279   "Pipe the current article to a subprocess.
9280 If N is a positive number, pipe the N next articles.
9281 If N is a negative number, pipe the N previous articles.
9282 If N is nil and any articles have been marked with the process mark,
9283 pipe those articles instead."
9284   (interactive "P")
9285   (require 'gnus-art)
9286   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9287     (gnus-summary-save-article arg t))
9288   (gnus-configure-windows 'pipe))
9289
9290 (defun gnus-summary-save-article-mail (&optional arg)
9291   "Append the current article to an mail file.
9292 If N is a positive number, save the N next articles.
9293 If N is a negative number, save the N previous articles.
9294 If N is nil and any articles have been marked with the process mark,
9295 save those articles instead."
9296   (interactive "P")
9297   (require 'gnus-art)
9298   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9299     (gnus-summary-save-article arg)))
9300
9301 (defun gnus-summary-save-article-rmail (&optional arg)
9302   "Append the current article to an rmail file.
9303 If N is a positive number, save the N next articles.
9304 If N is a negative number, save the N previous articles.
9305 If N is nil and any articles have been marked with the process mark,
9306 save those articles instead."
9307   (interactive "P")
9308   (require 'gnus-art)
9309   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9310     (gnus-summary-save-article arg)))
9311
9312 (defun gnus-summary-save-article-file (&optional arg)
9313   "Append the current article to a file.
9314 If N is a positive number, save the N next articles.
9315 If N is a negative number, save the N previous articles.
9316 If N is nil and any articles have been marked with the process mark,
9317 save those articles instead."
9318   (interactive "P")
9319   (require 'gnus-art)
9320   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9321     (gnus-summary-save-article arg)))
9322
9323 (defun gnus-summary-write-article-file (&optional arg)
9324   "Write the current article to a file, deleting the previous file.
9325 If N is a positive number, save the N next articles.
9326 If N is a negative number, save the N previous articles.
9327 If N is nil and any articles have been marked with the process mark,
9328 save those articles instead."
9329   (interactive "P")
9330   (require 'gnus-art)
9331   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9332     (gnus-summary-save-article arg)))
9333
9334 (defun gnus-summary-save-article-body-file (&optional arg)
9335   "Append the current article body to a file.
9336 If N is a positive number, save the N next articles.
9337 If N is a negative number, save the N previous articles.
9338 If N is nil and any articles have been marked with the process mark,
9339 save those articles instead."
9340   (interactive "P")
9341   (require 'gnus-art)
9342   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9343     (gnus-summary-save-article arg)))
9344
9345 (defun gnus-summary-pipe-message (program)
9346   "Pipe the current article through PROGRAM."
9347   (interactive "sProgram: ")
9348   (gnus-summary-select-article)
9349   (let ((mail-header-separator ""))
9350     (gnus-eval-in-buffer-window gnus-article-buffer
9351       (save-restriction
9352         (widen)
9353         (let ((start (window-start))
9354               buffer-read-only)
9355           (message-pipe-buffer-body program)
9356           (set-window-start (get-buffer-window (current-buffer)) start))))))
9357
9358 (defun gnus-get-split-value (methods)
9359   "Return a value based on the split METHODS."
9360   (let (split-name method result match)
9361     (when methods
9362       (save-excursion
9363         (set-buffer gnus-original-article-buffer)
9364         (save-restriction
9365           (nnheader-narrow-to-headers)
9366           (while (and methods (not split-name))
9367             (goto-char (point-min))
9368             (setq method (pop methods))
9369             (setq match (car method))
9370             (when (cond
9371                    ((stringp match)
9372                     ;; Regular expression.
9373                     (ignore-errors
9374                       (re-search-forward match nil t)))
9375                    ((gnus-functionp match)
9376                     ;; Function.
9377                     (save-restriction
9378                       (widen)
9379                       (setq result (funcall match gnus-newsgroup-name))))
9380                    ((consp match)
9381                     ;; Form.
9382                     (save-restriction
9383                       (widen)
9384                       (setq result (eval match)))))
9385               (setq split-name (cdr method))
9386               (cond ((stringp result)
9387                      (push (expand-file-name
9388                             result gnus-article-save-directory)
9389                            split-name))
9390                     ((consp result)
9391                      (setq split-name (append result split-name)))))))))
9392     (nreverse split-name)))
9393
9394 (defun gnus-valid-move-group-p (group)
9395   (and (boundp group)
9396        (symbol-name group)
9397        (symbol-value group)
9398        (gnus-get-function (gnus-find-method-for-group
9399                            (symbol-name group)) 'request-accept-article t)))
9400
9401 (defun gnus-read-move-group-name (prompt default articles prefix)
9402   "Read a group name."
9403   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9404          (minibuffer-confirm-incomplete nil) ; XEmacs
9405          (prom
9406           (format "%s %s to:"
9407                   prompt
9408                   (if (> (length articles) 1)
9409                       (format "these %d articles" (length articles))
9410                     "this article")))
9411          (to-newsgroup
9412           (cond
9413            ((null split-name)
9414             (gnus-completing-read default prom
9415                                   gnus-active-hashtb
9416                                   'gnus-valid-move-group-p
9417                                   nil prefix
9418                                   'gnus-group-history))
9419            ((= 1 (length split-name))
9420             (gnus-completing-read (car split-name) prom
9421                                   gnus-active-hashtb
9422                                   'gnus-valid-move-group-p
9423                                   nil nil
9424                                   'gnus-group-history))
9425            (t
9426             (gnus-completing-read nil prom
9427                                   (mapcar (lambda (el) (list el))
9428                                           (nreverse split-name))
9429                                   nil nil nil
9430                                   'gnus-group-history))))
9431          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9432     (when to-newsgroup
9433       (if (or (string= to-newsgroup "")
9434               (string= to-newsgroup prefix))
9435           (setq to-newsgroup default))
9436       (unless to-newsgroup
9437         (error "No group name entered"))
9438       (or (gnus-active to-newsgroup)
9439           (gnus-activate-group to-newsgroup nil nil to-method)
9440           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9441                                      to-newsgroup))
9442               (or (and (gnus-request-create-group to-newsgroup to-method)
9443                        (gnus-activate-group
9444                         to-newsgroup nil nil to-method)
9445                        (gnus-subscribe-group to-newsgroup))
9446                   (error "Couldn't create group %s" to-newsgroup)))
9447           (error "No such group: %s" to-newsgroup)))
9448     to-newsgroup))
9449
9450 (defun gnus-summary-save-parts (type dir n &optional reverse)
9451   "Save parts matching TYPE to DIR.
9452 If REVERSE, save parts that do not match TYPE."
9453   (interactive
9454    (list (read-string "Save parts of type: " 
9455                       (or (car gnus-summary-save-parts-type-history)
9456                           gnus-summary-save-parts-default-mime)
9457                       'gnus-summary-save-parts-type-history)
9458          (setq gnus-summary-save-parts-last-directory
9459                (read-file-name "Save to directory: " 
9460                                gnus-summary-save-parts-last-directory
9461                                nil t))
9462          current-prefix-arg))
9463   (gnus-summary-iterate n
9464     (let ((gnus-display-mime-function nil)
9465           (gnus-inhibit-treatment t))
9466       (gnus-summary-select-article))
9467     (save-excursion
9468       (set-buffer gnus-article-buffer)
9469       (let ((handles (or gnus-article-mime-handles
9470                          (mm-dissect-buffer) (mm-uu-dissect))))
9471         (when handles
9472           (gnus-summary-save-parts-1 type dir handles reverse)
9473           (unless gnus-article-mime-handles ;; Don't destroy this case.
9474             (mm-destroy-parts handles)))))))
9475
9476 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9477   (if (stringp (car handle))
9478       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9479               (cdr handle))
9480     (when (if reverse
9481               (not (string-match type (mm-handle-media-type handle)))
9482             (string-match type (mm-handle-media-type handle)))
9483       (let ((file (expand-file-name
9484                    (file-name-nondirectory
9485                     (or
9486                      (mail-content-type-get
9487                       (mm-handle-disposition handle) 'filename)
9488                      (concat gnus-newsgroup-name
9489                              "." (number-to-string
9490                                   (cdr gnus-article-current)))))
9491                    dir)))
9492         (unless (file-exists-p file)
9493           (mm-save-part-to-file handle file))))))
9494
9495 ;; Summary extract commands
9496
9497 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9498   (let ((buffer-read-only nil)
9499         (article (gnus-summary-article-number))
9500         after-article b e)
9501     (unless (gnus-summary-goto-subject article)
9502       (error "No such article: %d" article))
9503     (gnus-summary-position-point)
9504     ;; If all commands are to be bunched up on one line, we collect
9505     ;; them here.
9506     (unless gnus-view-pseudos-separately
9507       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9508             files action)
9509         (while ps
9510           (setq action (cdr (assq 'action (car ps))))
9511           (setq files (list (cdr (assq 'name (car ps)))))
9512           (while (and ps (cdr ps)
9513                       (string= (or action "1")
9514                                (or (cdr (assq 'action (cadr ps))) "2")))
9515             (push (cdr (assq 'name (cadr ps))) files)
9516             (setcdr ps (cddr ps)))
9517           (when files
9518             (when (not (string-match "%s" action))
9519               (push " " files))
9520             (push " " files)
9521             (when (assq 'execute (car ps))
9522               (setcdr (assq 'execute (car ps))
9523                       (funcall (if (string-match "%s" action)
9524                                    'format 'concat)
9525                                action
9526                                (mapconcat
9527                                 (lambda (f)
9528                                   (if (equal f " ")
9529                                       f
9530                                     (mm-quote-arg f)))
9531                                 files " ")))))
9532           (setq ps (cdr ps)))))
9533     (if (and gnus-view-pseudos (not not-view))
9534         (while pslist
9535           (when (assq 'execute (car pslist))
9536             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9537                                   (eq gnus-view-pseudos 'not-confirm)))
9538           (setq pslist (cdr pslist)))
9539       (save-excursion
9540         (while pslist
9541           (setq after-article (or (cdr (assq 'article (car pslist)))
9542                                   (gnus-summary-article-number)))
9543           (gnus-summary-goto-subject after-article)
9544           (forward-line 1)
9545           (setq b (point))
9546           (insert "    " (file-name-nondirectory
9547                           (cdr (assq 'name (car pslist))))
9548                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9549           (setq e (point))
9550           (forward-line -1)             ; back to `b'
9551           (gnus-add-text-properties
9552            b (1- e) (list 'gnus-number gnus-reffed-article-number
9553                           gnus-mouse-face-prop gnus-mouse-face))
9554           (gnus-data-enter
9555            after-article gnus-reffed-article-number
9556            gnus-unread-mark b (car pslist) 0 (- e b))
9557           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9558           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9559           (setq pslist (cdr pslist)))))))
9560
9561 (defun gnus-pseudos< (p1 p2)
9562   (let ((c1 (cdr (assq 'action p1)))
9563         (c2 (cdr (assq 'action p2))))
9564     (and c1 c2 (string< c1 c2))))
9565
9566 (defun gnus-request-pseudo-article (props)
9567   (cond ((assq 'execute props)
9568          (gnus-execute-command (cdr (assq 'execute props)))))
9569   (let ((gnus-current-article (gnus-summary-article-number)))
9570     (gnus-run-hooks 'gnus-mark-article-hook)))
9571
9572 (defun gnus-execute-command (command &optional automatic)
9573   (save-excursion
9574     (gnus-article-setup-buffer)
9575     (set-buffer gnus-article-buffer)
9576     (setq buffer-read-only nil)
9577     (let ((command (if automatic command
9578                      (read-string "Command: " (cons command 0)))))
9579       (erase-buffer)
9580       (insert "$ " command "\n\n")
9581       (if gnus-view-pseudo-asynchronously
9582           (start-process "gnus-execute" (current-buffer) shell-file-name
9583                          shell-command-switch command)
9584         (call-process shell-file-name nil t nil
9585                       shell-command-switch command)))))
9586
9587 ;; Summary kill commands.
9588
9589 (defun gnus-summary-edit-global-kill (article)
9590   "Edit the \"global\" kill file."
9591   (interactive (list (gnus-summary-article-number)))
9592   (gnus-group-edit-global-kill article))
9593
9594 (defun gnus-summary-edit-local-kill ()
9595   "Edit a local kill file applied to the current newsgroup."
9596   (interactive)
9597   (setq gnus-current-headers (gnus-summary-article-header))
9598   (gnus-group-edit-local-kill
9599    (gnus-summary-article-number) gnus-newsgroup-name))
9600
9601 ;;; Header reading.
9602
9603 (defun gnus-read-header (id &optional header)
9604   "Read the headers of article ID and enter them into the Gnus system."
9605   (let ((group gnus-newsgroup-name)
9606         (gnus-override-method
9607          (or
9608           gnus-override-method
9609           (and (gnus-news-group-p gnus-newsgroup-name)
9610                (car (gnus-refer-article-methods)))))
9611         where)
9612     ;; First we check to see whether the header in question is already
9613     ;; fetched.
9614     (if (stringp id)
9615         ;; This is a Message-ID.
9616         (setq header (or header (gnus-id-to-header id)))
9617       ;; This is an article number.
9618       (setq header (or header (gnus-summary-article-header id))))
9619     (if (and header
9620              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9621         ;; We have found the header.
9622         header
9623       ;; If this is a sparse article, we have to nix out its
9624       ;; previous entry in the thread hashtb.
9625       (when (and header
9626                  (gnus-summary-article-sparse-p (mail-header-number header)))
9627         (let* ((parent (gnus-parent-id (mail-header-references header)))
9628                (thread (and parent (gnus-id-to-thread parent))))
9629           (when thread
9630             (delq (assq header thread) thread))))
9631       ;; We have to really fetch the header to this article.
9632       (save-excursion
9633         (set-buffer nntp-server-buffer)
9634         (when (setq where (gnus-request-head id group))
9635           (nnheader-fold-continuation-lines)
9636           (goto-char (point-max))
9637           (insert ".\n")
9638           (goto-char (point-min))
9639           (insert "211 ")
9640           (princ (cond
9641                   ((numberp id) id)
9642                   ((cdr where) (cdr where))
9643                   (header (mail-header-number header))
9644                   (t gnus-reffed-article-number))
9645                  (current-buffer))
9646           (insert " Article retrieved.\n"))
9647         (if (or (not where)
9648                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9649             ()                          ; Malformed head.
9650           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9651             (when (and (stringp id)
9652                        (not (string= (gnus-group-real-name group)
9653                                      (car where))))
9654               ;; If we fetched by Message-ID and the article came
9655               ;; from a different group, we fudge some bogus article
9656               ;; numbers for this article.
9657               (mail-header-set-number header gnus-reffed-article-number))
9658             (save-excursion
9659               (set-buffer gnus-summary-buffer)
9660               (decf gnus-reffed-article-number)
9661               (gnus-remove-header (mail-header-number header))
9662               (push header gnus-newsgroup-headers)
9663               (setq gnus-current-headers header)
9664               (push (mail-header-number header) gnus-newsgroup-limit)))
9665           header)))))
9666
9667 (defun gnus-remove-header (number)
9668   "Remove header NUMBER from `gnus-newsgroup-headers'."
9669   (if (and gnus-newsgroup-headers
9670            (= number (mail-header-number (car gnus-newsgroup-headers))))
9671       (pop gnus-newsgroup-headers)
9672     (let ((headers gnus-newsgroup-headers))
9673       (while (and (cdr headers)
9674                   (not (= number (mail-header-number (cadr headers)))))
9675         (pop headers))
9676       (when (cdr headers)
9677         (setcdr headers (cddr headers))))))
9678
9679 ;;;
9680 ;;; summary highlights
9681 ;;;
9682
9683 (defun gnus-highlight-selected-summary ()
9684   "Highlight selected article in summary buffer."
9685   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9686   (when gnus-summary-selected-face
9687     (save-excursion
9688       (let* ((beg (progn (beginning-of-line) (point)))
9689              (end (progn (end-of-line) (point)))
9690              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9691              (from (if (get-text-property beg gnus-mouse-face-prop)
9692                        beg
9693                      (or (next-single-property-change
9694                           beg gnus-mouse-face-prop nil end)
9695                          beg)))
9696              (to
9697               (if (= from end)
9698                   (- from 2)
9699                 (or (next-single-property-change
9700                      from gnus-mouse-face-prop nil end)
9701                     end))))
9702         ;; If no mouse-face prop on line we will have to = from = end,
9703         ;; so we highlight the entire line instead.
9704         (when (= (+ to 2) from)
9705           (setq from beg)
9706           (setq to end))
9707         (if gnus-newsgroup-selected-overlay
9708             ;; Move old overlay.
9709             (gnus-move-overlay
9710              gnus-newsgroup-selected-overlay from to (current-buffer))
9711           ;; Create new overlay.
9712           (gnus-overlay-put
9713            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9714            'face gnus-summary-selected-face))))))
9715
9716 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9717 (defun gnus-summary-highlight-line ()
9718   "Highlight current line according to `gnus-summary-highlight'."
9719   (let* ((list gnus-summary-highlight)
9720          (p (point))
9721          (end (progn (end-of-line) (point)))
9722          ;; now find out where the line starts and leave point there.
9723          (beg (progn (beginning-of-line) (point)))
9724          (article (gnus-summary-article-number))
9725          (score (or (cdr (assq (or article gnus-current-article)
9726                                gnus-newsgroup-scored))
9727                     gnus-summary-default-score 0))
9728          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9729          (inhibit-read-only t))
9730     ;; Eval the cars of the lists until we find a match.
9731     (let ((default gnus-summary-default-score))
9732       (while (and list
9733                   (not (eval (caar list))))
9734         (setq list (cdr list))))
9735     (let ((face (cdar list)))
9736       (unless (eq face (get-text-property beg 'face))
9737         (gnus-put-text-property-excluding-characters-with-faces
9738          beg end 'face
9739          (setq face (if (boundp face) (symbol-value face) face)))
9740         (when gnus-summary-highlight-line-function
9741           (funcall gnus-summary-highlight-line-function article face))))
9742     (goto-char p)))
9743
9744 (defun gnus-update-read-articles (group unread &optional compute)
9745   "Update the list of read articles in GROUP."
9746   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9747          (entry (gnus-gethash group gnus-newsrc-hashtb))
9748          (info (nth 2 entry))
9749          (prev 1)
9750          (unread (sort (copy-sequence unread) '<))
9751          read)
9752     (if (or (not info) (not active))
9753         ;; There is no info on this group if it was, in fact,
9754         ;; killed.  Gnus stores no information on killed groups, so
9755         ;; there's nothing to be done.
9756         ;; One could store the information somewhere temporarily,
9757         ;; perhaps...  Hmmm...
9758         ()
9759       ;; Remove any negative articles numbers.
9760       (while (and unread (< (car unread) 0))
9761         (setq unread (cdr unread)))
9762       ;; Remove any expired article numbers
9763       (while (and unread (< (car unread) (car active)))
9764         (setq unread (cdr unread)))
9765       ;; Compute the ranges of read articles by looking at the list of
9766       ;; unread articles.
9767       (while unread
9768         (when (/= (car unread) prev)
9769           (push (if (= prev (1- (car unread))) prev
9770                   (cons prev (1- (car unread))))
9771                 read))
9772         (setq prev (1+ (car unread)))
9773         (setq unread (cdr unread)))
9774       (when (<= prev (cdr active))
9775         (push (cons prev (cdr active)) read))
9776       (setq read (if (> (length read) 1) (nreverse read) read))
9777       (if compute
9778           read
9779         (save-excursion
9780           (let (setmarkundo)
9781             ;; Propagate the read marks to the backend.
9782             (when (gnus-check-backend-function 'request-set-mark group)
9783               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9784                     (add (gnus-remove-from-range read (gnus-info-read info))))
9785                 (when (or add del)
9786                   (unless (gnus-check-group group)
9787                     (error "Can't open server for %s" group))
9788                   (gnus-request-set-mark
9789                    group (delq nil (list (if add (list add 'add '(read)))
9790                                          (if del (list del 'del '(read))))))
9791                   (setq setmarkundo
9792                         `(gnus-request-set-mark
9793                           ,group
9794                           ',(delq nil (list
9795                                        (if del (list del 'add '(read)))
9796                                        (if add (list add 'del '(read))))))))))
9797             (set-buffer gnus-group-buffer)
9798             (gnus-undo-register
9799               `(progn
9800                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9801                  (gnus-info-set-read ',info ',(gnus-info-read info))
9802                  (gnus-get-unread-articles-in-group ',info 
9803                                                     (gnus-active ,group))
9804                  (gnus-group-update-group ,group t)
9805                  ,setmarkundo))))
9806         ;; Enter this list into the group info.
9807         (gnus-info-set-read info read)
9808         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9809         (gnus-get-unread-articles-in-group info (gnus-active group))
9810         t))))
9811
9812 (defun gnus-offer-save-summaries ()
9813   "Offer to save all active summary buffers."
9814   (save-excursion
9815     (let ((buflist (buffer-list))
9816           buffers bufname)
9817       ;; Go through all buffers and find all summaries.
9818       (while buflist
9819         (and (setq bufname (buffer-name (car buflist)))
9820              (string-match "Summary" bufname)
9821              (save-excursion
9822                (set-buffer bufname)
9823                ;; We check that this is, indeed, a summary buffer.
9824                (and (eq major-mode 'gnus-summary-mode)
9825                     ;; Also make sure this isn't bogus.
9826                     gnus-newsgroup-prepared
9827                     ;; Also make sure that this isn't a dead summary buffer.
9828                     (not gnus-dead-summary-mode)))
9829              (push bufname buffers))
9830         (setq buflist (cdr buflist)))
9831       ;; Go through all these summary buffers and offer to save them.
9832       (when buffers
9833         (map-y-or-n-p
9834          "Update summary buffer %s? "
9835          (lambda (buf)
9836            (switch-to-buffer buf)
9837            (gnus-summary-exit))
9838          buffers)))))
9839
9840 (defun gnus-summary-setup-default-charset ()
9841   "Setup newsgroup default charset."
9842   (if (equal gnus-newsgroup-name "nndraft:drafts")
9843       (setq gnus-newsgroup-charset nil)
9844     (let* ((name (and gnus-newsgroup-name
9845                       (gnus-group-real-name gnus-newsgroup-name)))
9846            (ignored-charsets
9847             (or gnus-newsgroup-ephemeral-ignored-charsets
9848                 (append
9849                  (and gnus-newsgroup-name
9850                       (or (gnus-group-find-parameter gnus-newsgroup-name
9851                                                      'ignored-charsets t)
9852                           (let ((alist gnus-group-ignored-charsets-alist)
9853                                 elem (charsets nil))
9854                             (while (setq elem (pop alist))
9855                               (when (and name
9856                                          (string-match (car elem) name))
9857                                 (setq alist nil
9858                                       charsets (cdr elem))))
9859                             charsets)))
9860                  gnus-newsgroup-ignored-charsets))))
9861       (setq gnus-newsgroup-charset
9862             (or gnus-newsgroup-ephemeral-charset
9863                 (and gnus-newsgroup-name
9864                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9865                          (let ((alist gnus-group-charset-alist)
9866                                elem charset)
9867                            (while (setq elem (pop alist))
9868                              (when (and name
9869                                         (string-match (car elem) name))
9870                                (setq alist nil
9871                                      charset (cadr elem))))
9872                            charset)))
9873                 gnus-default-charset))
9874       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9875            ignored-charsets))))
9876
9877 ;;;
9878 ;;; Mime Commands
9879 ;;;
9880
9881 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9882   "Display the current article buffer fully MIME-buttonized.
9883 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9884 treated as multipart/mixed."
9885   (interactive "P")
9886   (require 'gnus-art)
9887   (let ((gnus-unbuttonized-mime-types nil)
9888         (gnus-mime-display-multipart-as-mixed show-all-parts))
9889     (gnus-summary-show-article)))
9890
9891 (defun gnus-summary-repair-multipart (article)
9892   "Add a Content-Type header to a multipart article without one."
9893   (interactive (list (gnus-summary-article-number)))
9894   (gnus-with-article article
9895     (message-narrow-to-head)
9896     (message-remove-header "Mime-Version")
9897     (goto-char (point-max))
9898     (insert "Mime-Version: 1.0\n")
9899     (widen)
9900     (when (search-forward "\n--" nil t)
9901       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9902         (message-narrow-to-head)
9903         (message-remove-header "Content-Type")
9904         (goto-char (point-max))
9905         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9906                         separator))
9907         (widen))))
9908   (let (gnus-mark-article-hook)
9909     (gnus-summary-select-article t t nil article)))
9910
9911 (defun gnus-summary-toggle-display-buttonized ()
9912   "Toggle the buttonizing of the article buffer."
9913   (interactive)
9914   (require 'gnus-art)
9915   (if (setq gnus-inhibit-mime-unbuttonizing
9916             (not gnus-inhibit-mime-unbuttonizing))
9917       (let ((gnus-unbuttonized-mime-types nil))
9918         (gnus-summary-show-article))
9919     (gnus-summary-show-article)))
9920
9921 ;;;
9922 ;;; Generic summary marking commands
9923 ;;;
9924
9925 (defvar gnus-summary-marking-alist
9926   '((read gnus-del-mark "d")
9927     (unread gnus-unread-mark "u")
9928     (ticked gnus-ticked-mark "!")
9929     (dormant gnus-dormant-mark "?")
9930     (expirable gnus-expirable-mark "e"))
9931   "An alist of names/marks/keystrokes.")
9932
9933 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9934 (defvar gnus-summary-mark-map)
9935
9936 (defun gnus-summary-make-all-marking-commands ()
9937   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9938   (dolist (elem gnus-summary-marking-alist)
9939     (apply 'gnus-summary-make-marking-command elem)))
9940
9941 (defun gnus-summary-make-marking-command (name mark keystroke)
9942   (let ((map (make-sparse-keymap)))
9943     (define-key gnus-summary-generic-mark-map keystroke map)
9944     (dolist (lway `((next "next" next nil "n")
9945                     (next-unread "next unread" next t "N")
9946                     (prev "previous" prev nil "p")
9947                     (prev-unread "previous unread" prev t "P")
9948                     (nomove "" nil nil ,keystroke)))
9949       (let ((func (gnus-summary-make-marking-command-1
9950                    mark (car lway) lway name)))
9951         (setq func (eval func))
9952         (define-key map (nth 4 lway) func)))))
9953       
9954 (defun gnus-summary-make-marking-command-1 (mark way lway name)
9955   `(defun ,(intern
9956             (format "gnus-summary-put-mark-as-%s%s"
9957                     name (if (eq way 'nomove)
9958                              ""
9959                            (concat "-" (symbol-name way)))))
9960      (n)
9961      ,(format
9962        "Mark the current article as %s%s.
9963 If N, the prefix, then repeat N times.
9964 If N is negative, move in reverse order.
9965 The difference between N and the actual number of articles marked is
9966 returned."
9967        name (cadr lway))
9968      (interactive "p")
9969      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9970     
9971 (defun gnus-summary-generic-mark (n mark move unread)
9972   "Mark N articles with MARK."
9973   (unless (eq major-mode 'gnus-summary-mode)
9974     (error "This command can only be used in the summary buffer"))
9975   (gnus-summary-show-thread)
9976   (let ((nummove
9977          (cond
9978           ((eq move 'next) 1)
9979           ((eq move 'prev) -1)
9980           (t 0))))
9981     (if (zerop nummove)
9982         (setq n 1)
9983       (when (< n 0)
9984         (setq n (abs n)
9985               nummove (* -1 nummove))))
9986     (while (and (> n 0)
9987                 (gnus-summary-mark-article nil mark)
9988                 (zerop (gnus-summary-next-subject nummove unread t)))
9989       (setq n (1- n)))
9990     (when (/= 0 n)
9991       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9992     (gnus-summary-recenter)
9993     (gnus-summary-position-point)
9994     (gnus-set-mode-line 'summary)
9995     n))
9996
9997 (gnus-summary-make-all-marking-commands)
9998
9999 (gnus-ems-redefine)
10000
10001 (provide 'gnus-sum)
10002
10003 (run-hooks 'gnus-sum-load-hook)
10004
10005 ;;; gnus-sum.el ends here