abd20c2cc2b50c8fa8983a37ac5dfd92b0decf89
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996-2000 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-group)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-int)
35 (require 'gnus-undo)
36 (require 'gnus-util)
37 (require 'mm-decode)
38 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
39
40 (defcustom gnus-kill-summary-on-exit t
41   "*If non-nil, kill the summary buffer when you exit from it.
42 If nil, the summary will become a \"*Dead Summary*\" buffer, and
43 it will be killed sometime later."
44   :group 'gnus-summary-exit
45   :type 'boolean)
46
47 (defcustom gnus-fetch-old-headers nil
48   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
49 If an unread article in the group refers to an older, already read (or
50 just marked as read) article, the old article will not normally be
51 displayed in the Summary buffer.  If this variable is non-nil, Gnus
52 will attempt to grab the headers to the old articles, and thereby
53 build complete threads.  If it has the value `some', only enough
54 headers to connect otherwise loose threads will be displayed.  This
55 variable can also be a number.  In that case, no more than that number
56 of old headers will be fetched.  If it has the value `invisible', all
57 old headers will be fetched, but none will be displayed.
58
59 The server has to support NOV for any of this to work."
60   :group 'gnus-thread
61   :type '(choice (const :tag "off" nil)
62                  (const some)
63                  number
64                  (sexp :menu-tag "other" t)))
65
66 (defcustom gnus-refer-thread-limit 200
67   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
68 If t, fetch all the available old headers."
69   :group 'gnus-thread
70   :type '(choice number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-summary-make-false-root 'adopt
74   "*nil means that Gnus won't gather loose threads.
75 If the root of a thread has expired or been read in a previous
76 session, the information necessary to build a complete thread has been
77 lost.  Instead of having many small sub-threads from this original thread
78 scattered all over the summary buffer, Gnus can gather them.
79
80 If non-nil, Gnus will try to gather all loose sub-threads from an
81 original thread into one large thread.
82
83 If this variable is non-nil, it should be one of `none', `adopt',
84 `dummy' or `empty'.
85
86 If this variable is `none', Gnus will not make a false root, but just
87 present the sub-threads after another.
88 If this variable is `dummy', Gnus will create a dummy root that will
89 have all the sub-threads as children.
90 If this variable is `adopt', Gnus will make one of the \"children\"
91 the parent and mark all the step-children as such.
92 If this variable is `empty', the \"children\" are printed with empty
93 subject fields.  (Or rather, they will be printed with a string
94 given by the `gnus-summary-same-subject' variable.)"
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const none)
98                  (const dummy)
99                  (const adopt)
100                  (const empty)))
101
102 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
103   "*A regexp to match subjects to be excluded from loose thread gathering.
104 As loose thread gathering is done on subjects only, that means that
105 there can be many false gatherings performed.  By rooting out certain
106 common subjects, gathering might become saner."
107   :group 'gnus-thread
108   :type 'regexp)
109
110 (defcustom gnus-summary-gather-subject-limit nil
111   "*Maximum length of subject comparisons when gathering loose threads.
112 Use nil to compare full subjects.  Setting this variable to a low
113 number will help gather threads that have been corrupted by
114 newsreaders chopping off subject lines, but it might also mean that
115 unrelated articles that have subject that happen to begin with the
116 same few characters will be incorrectly gathered.
117
118 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
119 comparing subjects."
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const fuzzy)
123                  (sexp :menu-tag "on" t)))
124
125 (defcustom gnus-simplify-subject-functions nil
126   "List of functions taking a string argument that simplify subjects.
127 The functions are applied recursively.
128
129 Useful functions to put in this list include: `gnus-simplify-subject-re',
130 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
131   :group 'gnus-thread
132   :type '(repeat function))
133
134 (defcustom gnus-simplify-ignored-prefixes nil
135   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
136   :group 'gnus-thread
137   :type '(choice (const :tag "off" nil)
138                  regexp))
139
140 (defcustom gnus-build-sparse-threads nil
141   "*If non-nil, fill in the gaps in threads.
142 If `some', only fill in the gaps that are needed to tie loose threads
143 together.  If `more', fill in all leaf nodes that Gnus can find.  If
144 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const some)
148                  (const more)
149                  (sexp :menu-tag "all" t)))
150
151 (defcustom gnus-summary-thread-gathering-function
152   'gnus-gather-threads-by-subject
153   "*Function used for gathering loose threads.
154 There are two pre-defined functions: `gnus-gather-threads-by-subject',
155 which only takes Subjects into consideration; and
156 `gnus-gather-threads-by-references', which compared the References
157 headers of the articles to find matches."
158   :group 'gnus-thread
159   :type '(radio (function-item gnus-gather-threads-by-subject)
160                 (function-item gnus-gather-threads-by-references)
161                 (function :tag "other")))
162
163 (defcustom gnus-summary-same-subject ""
164   "*String indicating that the current article has the same subject as the previous.
165 This variable will only be used if the value of
166 `gnus-summary-make-false-root' is `empty'."
167   :group 'gnus-summary-format
168   :type 'string)
169
170 (defcustom gnus-summary-goto-unread t
171   "*If t, many commands will go to the next unread article.
172 This applies to marking commands as well as other commands that
173 \"naturally\" select the next article, like, for instance, `SPC' at
174 the end of an article.
175
176 If nil, the marking commands do NOT go to the next unread article
177 (they go to the next article instead).  If `never', commands that
178 usually go to the next unread article, will go to the next article,
179 whether it is read or not."
180   :group 'gnus-summary-marks
181   :link '(custom-manual "(gnus)Setting Marks")
182   :type '(choice (const :tag "off" nil)
183                  (const never)
184                  (sexp :menu-tag "on" t)))
185
186 (defcustom gnus-summary-default-score 0
187   "*Default article score level.
188 All scores generated by the score files will be added to this score.
189 If this variable is nil, scoring will be disabled."
190   :group 'gnus-score-default
191   :type '(choice (const :tag "disable")
192                  integer))
193
194 (defcustom gnus-summary-zcore-fuzz 0
195   "*Fuzziness factor for the zcore in the summary buffer.
196 Articles with scores closer than this to `gnus-summary-default-score'
197 will not be marked."
198   :group 'gnus-summary-format
199   :type 'integer)
200
201 (defcustom gnus-simplify-subject-fuzzy-regexp nil
202   "*Strings to be removed when doing fuzzy matches.
203 This can either be a regular expression or list of regular expressions
204 that will be removed from subject strings if fuzzy subject
205 simplification is selected."
206   :group 'gnus-thread
207   :type '(repeat regexp))
208
209 (defcustom gnus-show-threads t
210   "*If non-nil, display threads in summary mode."
211   :group 'gnus-thread
212   :type 'boolean)
213
214 (defcustom gnus-thread-hide-subtree nil
215   "*If non-nil, hide all threads initially.
216 If threads are hidden, you have to run the command
217 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
218 to expose hidden threads."
219   :group 'gnus-thread
220   :type 'boolean)
221
222 (defcustom gnus-thread-hide-killed t
223   "*If non-nil, hide killed threads automatically."
224   :group 'gnus-thread
225   :type 'boolean)
226
227 (defcustom gnus-thread-ignore-subject t
228   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
229 If nil, articles that have different subjects from their parents will
230 start separate threads."
231   :group 'gnus-thread
232   :type 'boolean)
233
234 (defcustom gnus-thread-operation-ignore-subject t
235   "*If non-nil, subjects will be ignored when doing thread commands.
236 This affects commands like `gnus-summary-kill-thread' and
237 `gnus-summary-lower-thread'.
238
239 If this variable is nil, articles in the same thread with different
240 subjects will not be included in the operation in question.  If this
241 variable is `fuzzy', only articles that have subjects that are fuzzily
242 equal will be included."
243   :group 'gnus-thread
244   :type '(choice (const :tag "off" nil)
245                  (const fuzzy)
246                  (sexp :tag "on" t)))
247
248 (defcustom gnus-thread-indent-level 4
249   "*Number that says how much each sub-thread should be indented."
250   :group 'gnus-thread
251   :type 'integer)
252
253 (defcustom gnus-auto-extend-newsgroup t
254   "*If non-nil, extend newsgroup forward and backward when requested."
255   :group 'gnus-summary-choose
256   :type 'boolean)
257
258 (defcustom gnus-auto-select-first t
259   "*If nil, don't select the first unread article when entering a group.
260 If this variable is `best', select the highest-scored unread article
261 in the group.  If t, select the first unread article.
262
263 This variable can also be a function to place point on a likely
264 subject line.  Useful values include `gnus-summary-first-unread-subject',
265 `gnus-summary-first-unread-article' and
266 `gnus-summary-best-unread-article'.
267
268 If you want to prevent automatic selection of the first unread article
269 in some newsgroups, set the variable to nil in
270 `gnus-select-group-hook'."
271   :group 'gnus-group-select
272   :type '(choice (const :tag "none" nil)
273                  (const best)
274                  (sexp :menu-tag "first" t)
275                  (function-item gnus-summary-first-unread-subject)
276                  (function-item gnus-summary-first-unread-article)
277                  (function-item gnus-summary-best-unread-article)))
278
279 (defcustom gnus-auto-select-next t
280   "*If non-nil, offer to go to the next group from the end of the previous.
281 If the value is t and the next newsgroup is empty, Gnus will exit
282 summary mode and go back to group mode.  If the value is neither nil
283 nor t, Gnus will select the following unread newsgroup.  In
284 particular, if the value is the symbol `quietly', the next unread
285 newsgroup will be selected without any confirmation, and if it is
286 `almost-quietly', the next group will be selected without any
287 confirmation if you are located on the last article in the group.
288 Finally, if this variable is `slightly-quietly', the `Z n' command
289 will go to the next group without confirmation."
290   :group 'gnus-summary-maneuvering
291   :type '(choice (const :tag "off" nil)
292                  (const quietly)
293                  (const almost-quietly)
294                  (const slightly-quietly)
295                  (sexp :menu-tag "on" t)))
296
297 (defcustom gnus-auto-select-same nil
298   "*If non-nil, select the next article with the same subject.
299 If there are no more articles with the same subject, go to
300 the first unread article."
301   :group 'gnus-summary-maneuvering
302   :type 'boolean)
303
304 (defcustom gnus-summary-check-current nil
305   "*If non-nil, consider the current article when moving.
306 The \"unread\" movement commands will stay on the same line if the
307 current article is unread."
308   :group 'gnus-summary-maneuvering
309   :type 'boolean)
310
311 (defcustom gnus-auto-center-summary t
312   "*If non-nil, always center the current summary buffer.
313 In particular, if `vertical' do only vertical recentering.  If non-nil
314 and non-`vertical', do both horizontal and vertical recentering."
315   :group 'gnus-summary-maneuvering
316   :type '(choice (const :tag "none" nil)
317                  (const vertical)
318                  (integer :tag "height")
319                  (sexp :menu-tag "both" t)))
320
321 (defcustom gnus-show-all-headers nil
322   "*If non-nil, don't hide any headers."
323   :group 'gnus-article-hiding
324   :group 'gnus-article-headers
325   :type 'boolean)
326
327 (defcustom gnus-summary-ignore-duplicates nil
328   "*If non-nil, ignore articles with identical Message-ID headers."
329   :group 'gnus-summary
330   :type 'boolean)
331
332 (defcustom gnus-single-article-buffer t
333   "*If non-nil, display all articles in the same buffer.
334 If nil, each group will get its own article buffer."
335   :group 'gnus-article-various
336   :type 'boolean)
337
338 (defcustom gnus-break-pages t
339   "*If non-nil, do page breaking on articles.
340 The page delimiter is specified by the `gnus-page-delimiter'
341 variable."
342   :group 'gnus-article-various
343   :type 'boolean)
344
345 (defcustom gnus-move-split-methods nil
346   "*Variable used to suggest where articles are to be moved to.
347 It uses the same syntax as the `gnus-split-methods' variable."
348   :group 'gnus-summary-mail
349   :type '(repeat (choice (list :value (fun) function)
350                          (cons :value ("" "") regexp (repeat string))
351                          (sexp :value nil))))
352
353 (defcustom gnus-unread-mark ?  ;Whitespace
354   "*Mark used for unread articles."
355   :group 'gnus-summary-marks
356   :type 'character)
357
358 (defcustom gnus-ticked-mark ?!
359   "*Mark used for ticked articles."
360   :group 'gnus-summary-marks
361   :type 'character)
362
363 (defcustom gnus-dormant-mark ??
364   "*Mark used for dormant articles."
365   :group 'gnus-summary-marks
366   :type 'character)
367
368 (defcustom gnus-del-mark ?r
369   "*Mark used for del'd articles."
370   :group 'gnus-summary-marks
371   :type 'character)
372
373 (defcustom gnus-read-mark ?R
374   "*Mark used for read articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-expirable-mark ?E
379   "*Mark used for expirable articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-killed-mark ?K
384   "*Mark used for killed articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-souped-mark ?F
389   "*Mark used for killed articles."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-kill-file-mark ?X
394   "*Mark used for articles killed by kill files."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-low-score-mark ?Y
399   "*Mark used for articles with a low score."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-catchup-mark ?C
404   "*Mark used for articles that are caught up."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-replied-mark ?A
409   "*Mark used for articles that have been replied to."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-cached-mark ?*
414   "*Mark used for articles that are in the cache."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-saved-mark ?S
419   "*Mark used for articles that have been saved to."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-ancient-mark ?O
424   "*Mark used for ancient articles."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-sparse-mark ?Q
429   "*Mark used for sparsely reffed articles."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-canceled-mark ?G
434   "*Mark used for canceled articles."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-duplicate-mark ?M
439   "*Mark used for duplicate articles."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-undownloaded-mark ?@
444   "*Mark used for articles that weren't downloaded."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-downloadable-mark ?%
449   "*Mark used for articles that are to be downloaded."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-unsendable-mark ?=
454   "*Mark used for articles that won't be sent."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-score-over-mark ?+
459   "*Score mark used for articles with high scores."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-score-below-mark ?-
464   "*Score mark used for articles with low scores."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-empty-thread-mark ?  ;Whitespace
469   "*There is no thread under the article."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-not-empty-thread-mark ?=
474   "*There is a thread under the article."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-view-pseudo-asynchronously nil
479   "*If non-nil, Gnus will view pseudo-articles asynchronously."
480   :group 'gnus-extract-view
481   :type 'boolean)
482
483 (defcustom gnus-auto-expirable-marks
484   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
485         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
486         gnus-souped-mark gnus-duplicate-mark)
487   "*The list of marks converted into expiration if a group is auto-expirable."
488   :group 'gnus-summary
489   :type '(repeat character))
490
491 (defcustom gnus-inhibit-user-auto-expire t
492   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
493   :group 'gnus-summary
494   :type 'boolean)
495
496 (defcustom gnus-view-pseudos nil
497   "*If `automatic', pseudo-articles will be viewed automatically.
498 If `not-confirm', pseudos will be viewed automatically, and the user
499 will not be asked to confirm the command."
500   :group 'gnus-extract-view
501   :type '(choice (const :tag "off" nil)
502                  (const automatic)
503                  (const not-confirm)))
504
505 (defcustom gnus-view-pseudos-separately t
506   "*If non-nil, one pseudo-article will be created for each file to be viewed.
507 If nil, all files that use the same viewing command will be given as a
508 list of parameters to that command."
509   :group 'gnus-extract-view
510   :type 'boolean)
511
512 (defcustom gnus-insert-pseudo-articles t
513   "*If non-nil, insert pseudo-articles when decoding articles."
514   :group 'gnus-extract-view
515   :type 'boolean)
516
517 (defcustom gnus-summary-dummy-line-format
518   "  %(:                          :%) %S\n"
519   "*The format specification for the dummy roots in the summary buffer.
520 It works along the same lines as a normal formatting string,
521 with some simple extensions.
522
523 %S  The subject"
524   :group 'gnus-threading
525   :type 'string)
526
527 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
528   "*The format specification for the summary mode line.
529 It works along the same lines as a normal formatting string,
530 with some simple extensions:
531
532 %G  Group name
533 %p  Unprefixed group name
534 %A  Current article number
535 %z  Current article score
536 %V  Gnus version
537 %U  Number of unread articles in the group
538 %e  Number of unselected articles in the group
539 %Z  A string with unread/unselected article counts
540 %g  Shortish group name
541 %S  Subject of the current article
542 %u  User-defined spec
543 %s  Current score file name
544 %d  Number of dormant articles
545 %r  Number of articles that have been marked as read in this session
546 %E  Number of articles expunged by the score files"
547   :group 'gnus-summary-format
548   :type 'string)
549
550 (defcustom gnus-list-identifiers nil
551   "Regexp that matches list identifiers to be removed from subject.
552 This can also be a list of regexps."
553   :group 'gnus-summary-format
554   :group 'gnus-article-hiding
555   :type '(choice (const :tag "none" nil)
556                  (regexp :value ".*")
557                  (repeat :value (".*") regexp)))
558
559 (defcustom gnus-summary-mark-below 0
560   "*Mark all articles with a score below this variable as read.
561 This variable is local to each summary buffer and usually set by the
562 score file."
563   :group 'gnus-score-default
564   :type 'integer)
565
566 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
567   "*List of functions used for sorting articles in the summary buffer.
568 This variable is only used when not using a threaded display."
569   :group 'gnus-summary-sort
570   :type '(repeat (choice (function-item gnus-article-sort-by-number)
571                          (function-item gnus-article-sort-by-author)
572                          (function-item gnus-article-sort-by-subject)
573                          (function-item gnus-article-sort-by-date)
574                          (function-item gnus-article-sort-by-score)
575                          (function :tag "other"))))
576
577 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
578   "*List of functions used for sorting threads in the summary buffer.
579 By default, threads are sorted by article number.
580
581 Each function takes two threads and return non-nil if the first thread
582 should be sorted before the other.  If you use more than one function,
583 the primary sort function should be the last.  You should probably
584 always include `gnus-thread-sort-by-number' in the list of sorting
585 functions -- preferably first.
586
587 Ready-made functions include `gnus-thread-sort-by-number',
588 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
589 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
590 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
591   :group 'gnus-summary-sort
592   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
593                          (function-item gnus-thread-sort-by-author)
594                          (function-item gnus-thread-sort-by-subject)
595                          (function-item gnus-thread-sort-by-date)
596                          (function-item gnus-thread-sort-by-score)
597                          (function-item gnus-thread-sort-by-total-score)
598                          (function :tag "other"))))
599
600 (defcustom gnus-thread-score-function '+
601   "*Function used for calculating the total score of a thread.
602
603 The function is called with the scores of the article and each
604 subthread and should then return the score of the thread.
605
606 Some functions you can use are `+', `max', or `min'."
607   :group 'gnus-summary-sort
608   :type 'function)
609
610 (defcustom gnus-summary-expunge-below nil
611   "All articles that have a score less than this variable will be expunged.
612 This variable is local to the summary buffers."
613   :group 'gnus-score-default
614   :type '(choice (const :tag "off" nil)
615                  integer))
616
617 (defcustom gnus-thread-expunge-below nil
618   "All threads that have a total score less than this variable will be expunged.
619 See `gnus-thread-score-function' for en explanation of what a
620 \"thread score\" is.
621
622 This variable is local to the summary buffers."
623   :group 'gnus-threading
624   :group 'gnus-score-default
625   :type '(choice (const :tag "off" nil)
626                  integer))
627
628 (defcustom gnus-summary-mode-hook nil
629   "*A hook for Gnus summary mode.
630 This hook is run before any variables are set in the summary buffer."
631   :group 'gnus-summary-various
632   :type 'hook)
633
634 (defcustom gnus-summary-menu-hook nil
635   "*Hook run after the creation of the summary mode menu."
636   :group 'gnus-summary-visual
637   :type 'hook)
638
639 (defcustom gnus-summary-exit-hook nil
640   "*A hook called on exit from the summary buffer.
641 It will be called with point in the group buffer."
642   :group 'gnus-summary-exit
643   :type 'hook)
644
645 (defcustom gnus-summary-prepare-hook nil
646   "*A hook called after the summary buffer has been generated.
647 If you want to modify the summary buffer, you can use this hook."
648   :group 'gnus-summary-various
649   :type 'hook)
650
651 (defcustom gnus-summary-prepared-hook nil
652   "*A hook called as the last thing after the summary buffer has been generated."
653   :group 'gnus-summary-various
654   :type 'hook)
655
656 (defcustom gnus-summary-generate-hook nil
657   "*A hook run just before generating the summary buffer.
658 This hook is commonly used to customize threading variables and the
659 like."
660   :group 'gnus-summary-various
661   :type 'hook)
662
663 (defcustom gnus-select-group-hook nil
664   "*A hook called when a newsgroup is selected.
665
666 If you'd like to simplify subjects like the
667 `gnus-summary-next-same-subject' command does, you can use the
668 following hook:
669
670  (setq gnus-select-group-hook
671       (list
672         (lambda ()
673           (mapcar (lambda (header)
674                      (mail-header-set-subject
675                       header
676                       (gnus-simplify-subject
677                        (mail-header-subject header) 're-only)))
678                   gnus-newsgroup-headers))))"
679   :group 'gnus-group-select
680   :type 'hook)
681
682 (defcustom gnus-select-article-hook nil
683   "*A hook called when an article is selected."
684   :group 'gnus-summary-choose
685   :type 'hook)
686
687 (defcustom gnus-visual-mark-article-hook
688   (list 'gnus-highlight-selected-summary)
689   "*Hook run after selecting an article in the summary buffer.
690 It is meant to be used for highlighting the article in some way.  It
691 is not run if `gnus-visual' is nil."
692   :group 'gnus-summary-visual
693   :type 'hook)
694
695 (defcustom gnus-parse-headers-hook nil
696   "*A hook called before parsing the headers."
697   :group 'gnus-various
698   :type 'hook)
699
700 (defcustom gnus-exit-group-hook nil
701   "*A hook called when exiting summary mode.
702 This hook is not called from the non-updating exit commands like `Q'."
703   :group 'gnus-various
704   :type 'hook)
705
706 (defcustom gnus-summary-update-hook
707   (list 'gnus-summary-highlight-line)
708   "*A hook called when a summary line is changed.
709 The hook will not be called if `gnus-visual' is nil.
710
711 The default function `gnus-summary-highlight-line' will
712 highlight the line according to the `gnus-summary-highlight'
713 variable."
714   :group 'gnus-summary-visual
715   :type 'hook)
716
717 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
718   "*A hook called when an article is selected for the first time.
719 The hook is intended to mark an article as read (or unread)
720 automatically when it is selected."
721   :group 'gnus-summary-choose
722   :type 'hook)
723
724 (defcustom gnus-group-no-more-groups-hook nil
725   "*A hook run when returning to group mode having no more (unread) groups."
726   :group 'gnus-group-select
727   :type 'hook)
728
729 (defcustom gnus-ps-print-hook nil
730   "*A hook run before ps-printing something from Gnus."
731   :group 'gnus-summary
732   :type 'hook)
733
734 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
735   "Face used for highlighting the current article in the summary buffer."
736   :group 'gnus-summary-visual
737   :type 'face)
738
739 (defcustom gnus-summary-highlight
740   '(((= mark gnus-canceled-mark)
741      . gnus-summary-cancelled-face)
742     ((and (> score default)
743           (or (= mark gnus-dormant-mark)
744               (= mark gnus-ticked-mark)))
745      . gnus-summary-high-ticked-face)
746     ((and (< score default)
747           (or (= mark gnus-dormant-mark)
748               (= mark gnus-ticked-mark)))
749      . gnus-summary-low-ticked-face)
750     ((or (= mark gnus-dormant-mark)
751          (= mark gnus-ticked-mark))
752      . gnus-summary-normal-ticked-face)
753     ((and (> score default) (= mark gnus-ancient-mark))
754      . gnus-summary-high-ancient-face)
755     ((and (< score default) (= mark gnus-ancient-mark))
756      . gnus-summary-low-ancient-face)
757     ((= mark gnus-ancient-mark)
758      . gnus-summary-normal-ancient-face)
759     ((and (> score default) (= mark gnus-unread-mark))
760      . gnus-summary-high-unread-face)
761     ((and (< score default) (= mark gnus-unread-mark))
762      . gnus-summary-low-unread-face)
763     ((= mark gnus-unread-mark)
764      . gnus-summary-normal-unread-face)
765     ((and (> score default) (memq mark (list gnus-downloadable-mark
766                                              gnus-undownloaded-mark)))
767      . gnus-summary-high-unread-face)
768     ((and (< score default) (memq mark (list gnus-downloadable-mark
769                                              gnus-undownloaded-mark)))
770      . gnus-summary-low-unread-face)
771     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
772      . gnus-summary-normal-unread-face)
773     ((> score default)
774      . gnus-summary-high-read-face)
775     ((< score default)
776      . gnus-summary-low-read-face)
777     (t
778      . gnus-summary-normal-read-face))
779   "*Controls the highlighting of summary buffer lines.
780
781 A list of (FORM . FACE) pairs.  When deciding how a a particular
782 summary line should be displayed, each form is evaluated.  The content
783 of the face field after the first true form is used.  You can change
784 how those summary lines are displayed, by editing the face field.
785
786 You can use the following variables in the FORM field.
787
788 score:   The articles score
789 default: The default article score.
790 below:   The score below which articles are automatically marked as read.
791 mark:    The articles mark."
792   :group 'gnus-summary-visual
793   :type '(repeat (cons (sexp :tag "Form" nil)
794                        face)))
795
796 (defcustom gnus-alter-header-function nil
797   "Function called to allow alteration of article header structures.
798 The function is called with one parameter, the article header vector,
799 which it may alter in any way.")
800
801 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
802   "Variable that says which function should be used to decode a string with encoded words.")
803
804 (defcustom gnus-extra-headers nil
805   "*Extra headers to parse."
806   :group 'gnus-summary
807   :type '(repeat symbol))
808
809 (defcustom gnus-ignored-from-addresses
810   (and user-mail-address (regexp-quote user-mail-address))
811   "*Regexp of From headers that may be suppressed in favor of To headers."
812   :group 'gnus-summary
813   :type 'regexp)
814
815 (defcustom gnus-group-charset-alist
816   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
817     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
818     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
819     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
820     ("^relcom\\>" koi8-r)
821     ("^fido7\\>" koi8-r)
822     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
823     ("^israel\\>" iso-8859-1)
824     ("^han\\>" euc-kr)
825     ("^alt.chinese.text.big5\\>" chinese-big5)
826     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
827     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
828     (".*" iso-8859-1))
829   "Alist of regexps (to match group names) and default charsets to be used when reading."
830   :type '(repeat (list (regexp :tag "Group")
831                        (symbol :tag "Charset")))
832   :group 'gnus-charset)
833
834 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
835   "List of charsets that should be ignored.
836 When these charsets are used in the \"charset\" parameter, the
837 default charset will be used instead."
838   :type '(repeat symbol)
839   :group 'gnus-charset)
840
841 (defcustom gnus-group-ignored-charsets-alist 
842   '(("alt\\.chinese\\.text" iso-8859-1))
843   "Alist of regexps (to match group names) and charsets that should be ignored.
844 When these charsets are used in the \"charset\" parameter, the
845 default charset will be used instead."
846   :type '(repeat (cons (regexp :tag "Group")
847                        (repeat symbol)))
848   :group 'gnus-charset)
849
850 (defcustom gnus-group-highlight-words-alist nil
851   "Alist of group regexps and highlight regexps.
852 This variable uses the same syntax as `gnus-emphasis-alist'."
853   :type '(repeat (cons (regexp :tag "Group")
854                        (repeat (list (regexp :tag "Highlight regexp")
855                                      (number :tag "Group for entire word" 0)
856                                      (number :tag "Group for displayed part" 0)
857                                      (symbol :tag "Face" 
858                                              gnus-emphasis-highlight-words)))))
859   :group 'gnus-summary-visual)
860
861 (defcustom gnus-summary-show-article-charset-alist
862   nil
863   "Alist of number and charset.
864 The article will be shown with the charset corresponding to the
865 numbered argument.
866 For example: ((1 . cn-gb-2312) (2 . big5))."
867   :type '(repeat (cons (number :tag "Argument" 1)
868                        (symbol :tag "Charset")))
869   :group 'gnus-charset)
870
871 (defcustom gnus-preserve-marks t
872   "Whether marks are preserved when moving, copying and respooling messages."
873   :type 'boolean
874   :group 'gnus-summary-marks)
875
876 (defcustom gnus-alter-articles-to-read-function nil
877   "Function to be called to alter the list of articles to be selected."
878   :type 'function
879   :group 'gnus-summary)
880
881 ;;; Internal variables
882
883 (defvar gnus-article-mime-handles nil)
884 (defvar gnus-article-decoded-p nil)
885 (defvar gnus-scores-exclude-files nil)
886 (defvar gnus-page-broken nil)
887 (defvar gnus-inhibit-mime-unbuttonizing nil)
888
889 (defvar gnus-original-article nil)
890 (defvar gnus-article-internal-prepare-hook nil)
891 (defvar gnus-newsgroup-process-stack nil)
892
893 (defvar gnus-thread-indent-array nil)
894 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
895 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
896   "Function called to sort the articles within a thread after it has been gathered together.")
897
898 ;; Avoid highlighting in kill files.
899 (defvar gnus-summary-inhibit-highlight nil)
900 (defvar gnus-newsgroup-selected-overlay nil)
901 (defvar gnus-inhibit-limiting nil)
902 (defvar gnus-newsgroup-adaptive-score-file nil)
903 (defvar gnus-current-score-file nil)
904 (defvar gnus-current-move-group nil)
905 (defvar gnus-current-copy-group nil)
906 (defvar gnus-current-crosspost-group nil)
907
908 (defvar gnus-newsgroup-dependencies nil)
909 (defvar gnus-newsgroup-adaptive nil)
910 (defvar gnus-summary-display-article-function nil)
911 (defvar gnus-summary-highlight-line-function nil
912   "Function called after highlighting a summary line.")
913
914 (defvar gnus-summary-line-format-alist
915   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
916     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
917     (?s gnus-tmp-subject-or-nil ?s)
918     (?n gnus-tmp-name ?s)
919     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
920         ?s)
921     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
922             gnus-tmp-from) ?s)
923     (?F gnus-tmp-from ?s)
924     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
925     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
926     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
927     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
928     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
929     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
930     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
931     (?L gnus-tmp-lines ?d)
932     (?I gnus-tmp-indentation ?s)
933     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
934     (?R gnus-tmp-replied ?c)
935     (?\[ gnus-tmp-opening-bracket ?c)
936     (?\] gnus-tmp-closing-bracket ?c)
937     (?\> (make-string gnus-tmp-level ? ) ?s)
938     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
939     (?i gnus-tmp-score ?d)
940     (?z gnus-tmp-score-char ?c)
941     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
942     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
943     (?U gnus-tmp-unread ?c)
944     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
945     (?t (gnus-summary-number-of-articles-in-thread
946          (and (boundp 'thread) (car thread)) gnus-tmp-level)
947         ?d)
948     (?e (gnus-summary-number-of-articles-in-thread
949          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
950         ?c)
951     (?u gnus-tmp-user-defined ?s)
952     (?P (gnus-pick-line-number) ?d))
953   "An alist of format specifications that can appear in summary lines,
954 and what variables they correspond with, along with the type of the
955 variable (string, integer, character, etc).")
956
957 (defvar gnus-summary-dummy-line-format-alist
958   `((?S gnus-tmp-subject ?s)
959     (?N gnus-tmp-number ?d)
960     (?u gnus-tmp-user-defined ?s)))
961
962 (defvar gnus-summary-mode-line-format-alist
963   `((?G gnus-tmp-group-name ?s)
964     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
965     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
966     (?A gnus-tmp-article-number ?d)
967     (?Z gnus-tmp-unread-and-unselected ?s)
968     (?V gnus-version ?s)
969     (?U gnus-tmp-unread-and-unticked ?d)
970     (?S gnus-tmp-subject ?s)
971     (?e gnus-tmp-unselected ?d)
972     (?u gnus-tmp-user-defined ?s)
973     (?d (length gnus-newsgroup-dormant) ?d)
974     (?t (length gnus-newsgroup-marked) ?d)
975     (?r (length gnus-newsgroup-reads) ?d)
976     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
977     (?E gnus-newsgroup-expunged-tally ?d)
978     (?s (gnus-current-score-file-nondirectory) ?s)))
979
980 (defvar gnus-last-search-regexp nil
981   "Default regexp for article search command.")
982
983 (defvar gnus-last-shell-command nil
984   "Default shell command on article.")
985
986 (defvar gnus-newsgroup-begin nil)
987 (defvar gnus-newsgroup-end nil)
988 (defvar gnus-newsgroup-last-rmail nil)
989 (defvar gnus-newsgroup-last-mail nil)
990 (defvar gnus-newsgroup-last-folder nil)
991 (defvar gnus-newsgroup-last-file nil)
992 (defvar gnus-newsgroup-auto-expire nil)
993 (defvar gnus-newsgroup-active nil)
994
995 (defvar gnus-newsgroup-data nil)
996 (defvar gnus-newsgroup-data-reverse nil)
997 (defvar gnus-newsgroup-limit nil)
998 (defvar gnus-newsgroup-limits nil)
999
1000 (defvar gnus-newsgroup-unreads nil
1001   "List of unread articles in the current newsgroup.")
1002
1003 (defvar gnus-newsgroup-unselected nil
1004   "List of unselected unread articles in the current newsgroup.")
1005
1006 (defvar gnus-newsgroup-reads nil
1007   "Alist of read articles and article marks in the current newsgroup.")
1008
1009 (defvar gnus-newsgroup-expunged-tally nil)
1010
1011 (defvar gnus-newsgroup-marked nil
1012   "List of ticked articles in the current newsgroup (a subset of unread art).")
1013
1014 (defvar gnus-newsgroup-killed nil
1015   "List of ranges of articles that have been through the scoring process.")
1016
1017 (defvar gnus-newsgroup-cached nil
1018   "List of articles that come from the article cache.")
1019
1020 (defvar gnus-newsgroup-saved nil
1021   "List of articles that have been saved.")
1022
1023 (defvar gnus-newsgroup-kill-headers nil)
1024
1025 (defvar gnus-newsgroup-replied nil
1026   "List of articles that have been replied to in the current newsgroup.")
1027
1028 (defvar gnus-newsgroup-expirable nil
1029   "List of articles in the current newsgroup that can be expired.")
1030
1031 (defvar gnus-newsgroup-processable nil
1032   "List of articles in the current newsgroup that can be processed.")
1033
1034 (defvar gnus-newsgroup-downloadable nil
1035   "List of articles in the current newsgroup that can be processed.")
1036
1037 (defvar gnus-newsgroup-undownloaded nil
1038   "List of articles in the current newsgroup that haven't been downloaded..")
1039
1040 (defvar gnus-newsgroup-unsendable nil
1041   "List of articles in the current newsgroup that won't be sent.")
1042
1043 (defvar gnus-newsgroup-bookmarks nil
1044   "List of articles in the current newsgroup that have bookmarks.")
1045
1046 (defvar gnus-newsgroup-dormant nil
1047   "List of dormant articles in the current newsgroup.")
1048
1049 (defvar gnus-newsgroup-scored nil
1050   "List of scored articles in the current newsgroup.")
1051
1052 (defvar gnus-newsgroup-headers nil
1053   "List of article headers in the current newsgroup.")
1054
1055 (defvar gnus-newsgroup-threads nil)
1056
1057 (defvar gnus-newsgroup-prepared nil
1058   "Whether the current group has been prepared properly.")
1059
1060 (defvar gnus-newsgroup-ancient nil
1061   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1062
1063 (defvar gnus-newsgroup-sparse nil)
1064
1065 (defvar gnus-current-article nil)
1066 (defvar gnus-article-current nil)
1067 (defvar gnus-current-headers nil)
1068 (defvar gnus-have-all-headers nil)
1069 (defvar gnus-last-article nil)
1070 (defvar gnus-newsgroup-history nil)
1071 (defvar gnus-newsgroup-charset nil)
1072 (defvar gnus-newsgroup-ephemeral-charset nil)
1073 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1074
1075 (defconst gnus-summary-local-variables
1076   '(gnus-newsgroup-name
1077     gnus-newsgroup-begin gnus-newsgroup-end
1078     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1079     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1080     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1081     gnus-newsgroup-unselected gnus-newsgroup-marked
1082     gnus-newsgroup-reads gnus-newsgroup-saved
1083     gnus-newsgroup-replied gnus-newsgroup-expirable
1084     gnus-newsgroup-processable gnus-newsgroup-killed
1085     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1086     gnus-newsgroup-unsendable
1087     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1088     gnus-newsgroup-headers gnus-newsgroup-threads
1089     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1090     gnus-current-article gnus-current-headers gnus-have-all-headers
1091     gnus-last-article gnus-article-internal-prepare-hook
1092     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1093     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1094     gnus-thread-expunge-below
1095     gnus-score-alist gnus-current-score-file
1096     (gnus-summary-expunge-below . global)
1097     (gnus-summary-mark-below . global)
1098     gnus-newsgroup-active gnus-scores-exclude-files
1099     gnus-newsgroup-history gnus-newsgroup-ancient
1100     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1101     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1102     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1103     (gnus-newsgroup-expunged-tally . 0)
1104     gnus-cache-removable-articles gnus-newsgroup-cached
1105     gnus-newsgroup-data gnus-newsgroup-data-reverse
1106     gnus-newsgroup-limit gnus-newsgroup-limits
1107     gnus-newsgroup-charset)
1108   "Variables that are buffer-local to the summary buffers.")
1109
1110 ;; Byte-compiler warning.
1111 (defvar gnus-article-mode-map)
1112
1113 ;; MIME stuff.
1114
1115 (defvar gnus-decode-encoded-word-methods
1116   '(mail-decode-encoded-word-string)
1117   "List of methods used to decode encoded words.
1118
1119 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1120 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1121 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1122 whose names match REGEXP.
1123
1124 For example:
1125 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1126  mail-decode-encoded-word-string
1127  (\"chinese\" . rfc1843-decode-string))
1128 ")
1129
1130 (defvar gnus-decode-encoded-word-methods-cache nil)
1131
1132 (defun gnus-multi-decode-encoded-word-string (string)
1133   "Apply the functions from `gnus-encoded-word-methods' that match."
1134   (unless (and gnus-decode-encoded-word-methods-cache
1135                (eq gnus-newsgroup-name
1136                    (car gnus-decode-encoded-word-methods-cache)))
1137     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1138     (mapcar (lambda (x)
1139               (if (symbolp x)
1140                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1141                 (if (and gnus-newsgroup-name
1142                          (string-match (car x) gnus-newsgroup-name))
1143                     (nconc gnus-decode-encoded-word-methods-cache
1144                            (list (cdr x))))))
1145           gnus-decode-encoded-word-methods))
1146   (let ((xlist gnus-decode-encoded-word-methods-cache))
1147     (pop xlist)
1148     (while xlist
1149       (setq string (funcall (pop xlist) string))))
1150   string)
1151
1152 ;; Subject simplification.
1153
1154 (defun gnus-simplify-whitespace (str)
1155   "Remove excessive whitespace."
1156   (let ((mystr str))
1157     ;; Multiple spaces.
1158     (while (string-match "[ \t][ \t]+" mystr)
1159       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1160                           " "
1161                           (substring mystr (match-end 0)))))
1162     ;; Leading spaces.
1163     (when (string-match "^[ \t]+" mystr)
1164       (setq mystr (substring mystr (match-end 0))))
1165     ;; Trailing spaces.
1166     (when (string-match "[ \t]+$" mystr)
1167       (setq mystr (substring mystr 0 (match-beginning 0))))
1168     mystr))
1169
1170 (defsubst gnus-simplify-subject-re (subject)
1171   "Remove \"Re:\" from subject lines."
1172   (if (string-match "^[Rr][Ee]: *" subject)
1173       (substring subject (match-end 0))
1174     subject))
1175
1176 (defun gnus-simplify-subject (subject &optional re-only)
1177   "Remove `Re:' and words in parentheses.
1178 If RE-ONLY is non-nil, strip leading `Re:'s only."
1179   (let ((case-fold-search t))           ;Ignore case.
1180     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1181     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1182       (setq subject (substring subject (match-end 0))))
1183     ;; Remove uninteresting prefixes.
1184     (when (and (not re-only)
1185                gnus-simplify-ignored-prefixes
1186                (string-match gnus-simplify-ignored-prefixes subject))
1187       (setq subject (substring subject (match-end 0))))
1188     ;; Remove words in parentheses from end.
1189     (unless re-only
1190       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1191         (setq subject (substring subject 0 (match-beginning 0)))))
1192     ;; Return subject string.
1193     subject))
1194
1195 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1196 ;; all whitespace.
1197 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1198   (goto-char (point-min))
1199   (while (re-search-forward regexp nil t)
1200     (replace-match (or newtext ""))))
1201
1202 (defun gnus-simplify-buffer-fuzzy ()
1203   "Simplify string in the buffer fuzzily.
1204 The string in the accessible portion of the current buffer is simplified.
1205 It is assumed to be a single-line subject.
1206 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1207 matter is removed.  Additional things can be deleted by setting
1208 gnus-simplify-subject-fuzzy-regexp."
1209   (let ((case-fold-search t)
1210         (modified-tick))
1211     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1212
1213     (while (not (eq modified-tick (buffer-modified-tick)))
1214       (setq modified-tick (buffer-modified-tick))
1215       (cond
1216        ((listp gnus-simplify-subject-fuzzy-regexp)
1217         (mapcar 'gnus-simplify-buffer-fuzzy-step
1218                 gnus-simplify-subject-fuzzy-regexp))
1219        (gnus-simplify-subject-fuzzy-regexp
1220         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1221       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1222       (gnus-simplify-buffer-fuzzy-step
1223        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1224       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1225
1226     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1227     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1228     (gnus-simplify-buffer-fuzzy-step " $")
1229     (gnus-simplify-buffer-fuzzy-step "^ +")))
1230
1231 (defun gnus-simplify-subject-fuzzy (subject)
1232   "Simplify a subject string fuzzily.
1233 See `gnus-simplify-buffer-fuzzy' for details."
1234   (save-excursion
1235     (gnus-set-work-buffer)
1236     (let ((case-fold-search t))
1237       ;; Remove uninteresting prefixes.
1238       (when (and gnus-simplify-ignored-prefixes
1239                  (string-match gnus-simplify-ignored-prefixes subject))
1240         (setq subject (substring subject (match-end 0))))
1241       (insert subject)
1242       (inline (gnus-simplify-buffer-fuzzy))
1243       (buffer-string))))
1244
1245 (defsubst gnus-simplify-subject-fully (subject)
1246   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1247   (cond
1248    (gnus-simplify-subject-functions
1249     (gnus-map-function gnus-simplify-subject-functions subject))
1250    ((null gnus-summary-gather-subject-limit)
1251     (gnus-simplify-subject-re subject))
1252    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1253     (gnus-simplify-subject-fuzzy subject))
1254    ((numberp gnus-summary-gather-subject-limit)
1255     (gnus-limit-string (gnus-simplify-subject-re subject)
1256                        gnus-summary-gather-subject-limit))
1257    (t
1258     subject)))
1259
1260 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1261   "Check whether two subjects are equal.
1262 If optional argument simple-first is t, first argument is already
1263 simplified."
1264   (cond
1265    ((null simple-first)
1266     (equal (gnus-simplify-subject-fully s1)
1267            (gnus-simplify-subject-fully s2)))
1268    (t
1269     (equal s1
1270            (gnus-simplify-subject-fully s2)))))
1271
1272 (defun gnus-summary-bubble-group ()
1273   "Increase the score of the current group.
1274 This is a handy function to add to `gnus-summary-exit-hook' to
1275 increase the score of each group you read."
1276   (gnus-group-add-score gnus-newsgroup-name))
1277
1278 \f
1279 ;;;
1280 ;;; Gnus summary mode
1281 ;;;
1282
1283 (put 'gnus-summary-mode 'mode-class 'special)
1284
1285 (when t
1286   ;; Non-orthogonal keys
1287
1288   (gnus-define-keys gnus-summary-mode-map
1289     " " gnus-summary-next-page
1290     "\177" gnus-summary-prev-page
1291     [delete] gnus-summary-prev-page
1292     [backspace] gnus-summary-prev-page
1293     "\r" gnus-summary-scroll-up
1294     "\M-\r" gnus-summary-scroll-down
1295     "n" gnus-summary-next-unread-article
1296     "p" gnus-summary-prev-unread-article
1297     "N" gnus-summary-next-article
1298     "P" gnus-summary-prev-article
1299     "\M-\C-n" gnus-summary-next-same-subject
1300     "\M-\C-p" gnus-summary-prev-same-subject
1301     "\M-n" gnus-summary-next-unread-subject
1302     "\M-p" gnus-summary-prev-unread-subject
1303     "." gnus-summary-first-unread-article
1304     "," gnus-summary-best-unread-article
1305     "\M-s" gnus-summary-search-article-forward
1306     "\M-r" gnus-summary-search-article-backward
1307     "<" gnus-summary-beginning-of-article
1308     ">" gnus-summary-end-of-article
1309     "j" gnus-summary-goto-article
1310     "^" gnus-summary-refer-parent-article
1311     "\M-^" gnus-summary-refer-article
1312     "u" gnus-summary-tick-article-forward
1313     "!" gnus-summary-tick-article-forward
1314     "U" gnus-summary-tick-article-backward
1315     "d" gnus-summary-mark-as-read-forward
1316     "D" gnus-summary-mark-as-read-backward
1317     "E" gnus-summary-mark-as-expirable
1318     "\M-u" gnus-summary-clear-mark-forward
1319     "\M-U" gnus-summary-clear-mark-backward
1320     "k" gnus-summary-kill-same-subject-and-select
1321     "\C-k" gnus-summary-kill-same-subject
1322     "\M-\C-k" gnus-summary-kill-thread
1323     "\M-\C-l" gnus-summary-lower-thread
1324     "e" gnus-summary-edit-article
1325     "#" gnus-summary-mark-as-processable
1326     "\M-#" gnus-summary-unmark-as-processable
1327     "\M-\C-t" gnus-summary-toggle-threads
1328     "\M-\C-s" gnus-summary-show-thread
1329     "\M-\C-h" gnus-summary-hide-thread
1330     "\M-\C-f" gnus-summary-next-thread
1331     "\M-\C-b" gnus-summary-prev-thread
1332     [(meta down)] gnus-summary-next-thread
1333     [(meta up)] gnus-summary-prev-thread
1334     "\M-\C-u" gnus-summary-up-thread
1335     "\M-\C-d" gnus-summary-down-thread
1336     "&" gnus-summary-execute-command
1337     "c" gnus-summary-catchup-and-exit
1338     "\C-w" gnus-summary-mark-region-as-read
1339     "\C-t" gnus-summary-toggle-truncation
1340     "?" gnus-summary-mark-as-dormant
1341     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1342     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1343     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1344     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1345     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1346     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1347     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1348     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1349     "=" gnus-summary-expand-window
1350     "\C-x\C-s" gnus-summary-reselect-current-group
1351     "\M-g" gnus-summary-rescan-group
1352     "w" gnus-summary-stop-page-breaking
1353     "\C-c\C-r" gnus-summary-caesar-message
1354     "f" gnus-summary-followup
1355     "F" gnus-summary-followup-with-original
1356     "C" gnus-summary-cancel-article
1357     "r" gnus-summary-reply
1358     "R" gnus-summary-reply-with-original
1359     "\C-c\C-f" gnus-summary-mail-forward
1360     "o" gnus-summary-save-article
1361     "\C-o" gnus-summary-save-article-mail
1362     "|" gnus-summary-pipe-output
1363     "\M-k" gnus-summary-edit-local-kill
1364     "\M-K" gnus-summary-edit-global-kill
1365     ;; "V" gnus-version
1366     "\C-c\C-d" gnus-summary-describe-group
1367     "q" gnus-summary-exit
1368     "Q" gnus-summary-exit-no-update
1369     "\C-c\C-i" gnus-info-find-node
1370     gnus-mouse-2 gnus-mouse-pick-article
1371     "m" gnus-summary-mail-other-window
1372     "a" gnus-summary-post-news
1373     "x" gnus-summary-limit-to-unread
1374     "s" gnus-summary-isearch-article
1375     "t" gnus-summary-toggle-header
1376     "g" gnus-summary-show-article
1377     "l" gnus-summary-goto-last-article
1378     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1379     "\C-d" gnus-summary-enter-digest-group
1380     "\M-\C-d" gnus-summary-read-document
1381     "\M-\C-e" gnus-summary-edit-parameters
1382     "\M-\C-a" gnus-summary-customize-parameters
1383     "\C-c\C-b" gnus-bug
1384     "*" gnus-cache-enter-article
1385     "\M-*" gnus-cache-remove-article
1386     "\M-&" gnus-summary-universal-argument
1387     "\C-l" gnus-recenter
1388     "I" gnus-summary-increase-score
1389     "L" gnus-summary-lower-score
1390     "\M-i" gnus-symbolic-argument
1391     "h" gnus-summary-select-article-buffer
1392
1393     "b" gnus-article-view-part
1394     "\M-t" gnus-summary-toggle-display-buttonized
1395
1396     "V" gnus-summary-score-map
1397     "X" gnus-uu-extract-map
1398     "S" gnus-summary-send-map)
1399
1400   ;; Sort of orthogonal keymap
1401   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1402     "t" gnus-summary-tick-article-forward
1403     "!" gnus-summary-tick-article-forward
1404     "d" gnus-summary-mark-as-read-forward
1405     "r" gnus-summary-mark-as-read-forward
1406     "c" gnus-summary-clear-mark-forward
1407     " " gnus-summary-clear-mark-forward
1408     "e" gnus-summary-mark-as-expirable
1409     "x" gnus-summary-mark-as-expirable
1410     "?" gnus-summary-mark-as-dormant
1411     "b" gnus-summary-set-bookmark
1412     "B" gnus-summary-remove-bookmark
1413     "#" gnus-summary-mark-as-processable
1414     "\M-#" gnus-summary-unmark-as-processable
1415     "S" gnus-summary-limit-include-expunged
1416     "C" gnus-summary-catchup
1417     "H" gnus-summary-catchup-to-here
1418     "\C-c" gnus-summary-catchup-all
1419     "k" gnus-summary-kill-same-subject-and-select
1420     "K" gnus-summary-kill-same-subject
1421     "P" gnus-uu-mark-map)
1422
1423   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1424     "c" gnus-summary-clear-above
1425     "u" gnus-summary-tick-above
1426     "m" gnus-summary-mark-above
1427     "k" gnus-summary-kill-below)
1428
1429   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1430     "/" gnus-summary-limit-to-subject
1431     "n" gnus-summary-limit-to-articles
1432     "w" gnus-summary-pop-limit
1433     "s" gnus-summary-limit-to-subject
1434     "a" gnus-summary-limit-to-author
1435     "u" gnus-summary-limit-to-unread
1436     "m" gnus-summary-limit-to-marks
1437     "M" gnus-summary-limit-exclude-marks
1438     "v" gnus-summary-limit-to-score
1439     "*" gnus-summary-limit-include-cached
1440     "D" gnus-summary-limit-include-dormant
1441     "T" gnus-summary-limit-include-thread
1442     "d" gnus-summary-limit-exclude-dormant
1443     "t" gnus-summary-limit-to-age
1444     "x" gnus-summary-limit-to-extra 
1445     "E" gnus-summary-limit-include-expunged
1446     "c" gnus-summary-limit-exclude-childless-dormant
1447     "C" gnus-summary-limit-mark-excluded-as-read)
1448
1449   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1450     "n" gnus-summary-next-unread-article
1451     "p" gnus-summary-prev-unread-article
1452     "N" gnus-summary-next-article
1453     "P" gnus-summary-prev-article
1454     "\C-n" gnus-summary-next-same-subject
1455     "\C-p" gnus-summary-prev-same-subject
1456     "\M-n" gnus-summary-next-unread-subject
1457     "\M-p" gnus-summary-prev-unread-subject
1458     "f" gnus-summary-first-unread-article
1459     "b" gnus-summary-best-unread-article
1460     "j" gnus-summary-goto-article
1461     "g" gnus-summary-goto-subject
1462     "l" gnus-summary-goto-last-article
1463     "o" gnus-summary-pop-article)
1464
1465   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1466     "k" gnus-summary-kill-thread
1467     "l" gnus-summary-lower-thread
1468     "i" gnus-summary-raise-thread
1469     "T" gnus-summary-toggle-threads
1470     "t" gnus-summary-rethread-current
1471     "^" gnus-summary-reparent-thread
1472     "s" gnus-summary-show-thread
1473     "S" gnus-summary-show-all-threads
1474     "h" gnus-summary-hide-thread
1475     "H" gnus-summary-hide-all-threads
1476     "n" gnus-summary-next-thread
1477     "p" gnus-summary-prev-thread
1478     "u" gnus-summary-up-thread
1479     "o" gnus-summary-top-thread
1480     "d" gnus-summary-down-thread
1481     "#" gnus-uu-mark-thread
1482     "\M-#" gnus-uu-unmark-thread)
1483
1484   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1485     "g" gnus-summary-prepare
1486     "c" gnus-summary-insert-cached-articles)
1487
1488   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1489     "c" gnus-summary-catchup-and-exit
1490     "C" gnus-summary-catchup-all-and-exit
1491     "E" gnus-summary-exit-no-update
1492     "Q" gnus-summary-exit
1493     "Z" gnus-summary-exit
1494     "n" gnus-summary-catchup-and-goto-next-group
1495     "R" gnus-summary-reselect-current-group
1496     "G" gnus-summary-rescan-group
1497     "N" gnus-summary-next-group
1498     "s" gnus-summary-save-newsrc
1499     "P" gnus-summary-prev-group)
1500
1501   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1502     " " gnus-summary-next-page
1503     "n" gnus-summary-next-page
1504     "\177" gnus-summary-prev-page
1505     [delete] gnus-summary-prev-page
1506     "p" gnus-summary-prev-page
1507     "\r" gnus-summary-scroll-up
1508     "\M-\r" gnus-summary-scroll-down
1509     "<" gnus-summary-beginning-of-article
1510     ">" gnus-summary-end-of-article
1511     "b" gnus-summary-beginning-of-article
1512     "e" gnus-summary-end-of-article
1513     "^" gnus-summary-refer-parent-article
1514     "r" gnus-summary-refer-parent-article
1515     "D" gnus-summary-enter-digest-group
1516     "R" gnus-summary-refer-references
1517     "T" gnus-summary-refer-thread
1518     "g" gnus-summary-show-article
1519     "s" gnus-summary-isearch-article
1520     "P" gnus-summary-print-article
1521     "t" gnus-article-babel)
1522
1523   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1524     "b" gnus-article-add-buttons
1525     "B" gnus-article-add-buttons-to-head
1526     "o" gnus-article-treat-overstrike
1527     "e" gnus-article-emphasize
1528     "w" gnus-article-fill-cited-article
1529     "Q" gnus-article-fill-long-lines
1530     "C" gnus-article-capitalize-sentences
1531     "c" gnus-article-remove-cr
1532     "q" gnus-article-de-quoted-unreadable
1533     "f" gnus-article-display-x-face
1534     "l" gnus-summary-stop-page-breaking
1535     "r" gnus-summary-caesar-message
1536     "t" gnus-article-hide-headers
1537     "v" gnus-summary-verbose-headers
1538     "H" gnus-article-strip-headers-in-body
1539     "d" gnus-article-treat-dumbquotes)
1540
1541   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1542     "a" gnus-article-hide
1543     "h" gnus-article-hide-headers
1544     "b" gnus-article-hide-boring-headers
1545     "s" gnus-article-hide-signature
1546     "c" gnus-article-hide-citation
1547     "C" gnus-article-hide-citation-in-followups
1548     "l" gnus-article-hide-list-identifiers
1549     "p" gnus-article-hide-pgp
1550     "B" gnus-article-strip-banner
1551     "P" gnus-article-hide-pem
1552     "\C-c" gnus-article-hide-citation-maybe)
1553
1554   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1555     "a" gnus-article-highlight
1556     "h" gnus-article-highlight-headers
1557     "c" gnus-article-highlight-citation
1558     "s" gnus-article-highlight-signature)
1559
1560   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1561     "w" gnus-article-decode-mime-words
1562     "c" gnus-article-decode-charset
1563     "v" gnus-mime-view-all-parts
1564     "b" gnus-article-view-part)
1565
1566   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1567     "z" gnus-article-date-ut
1568     "u" gnus-article-date-ut
1569     "l" gnus-article-date-local
1570     "e" gnus-article-date-lapsed
1571     "o" gnus-article-date-original
1572     "i" gnus-article-date-iso8601
1573     "s" gnus-article-date-user)
1574
1575   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1576     "t" gnus-article-remove-trailing-blank-lines
1577     "l" gnus-article-strip-leading-blank-lines
1578     "m" gnus-article-strip-multiple-blank-lines
1579     "a" gnus-article-strip-blank-lines
1580     "A" gnus-article-strip-all-blank-lines
1581     "s" gnus-article-strip-leading-space
1582     "e" gnus-article-strip-trailing-space)
1583
1584   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1585     "v" gnus-version
1586     "f" gnus-summary-fetch-faq
1587     "d" gnus-summary-describe-group
1588     "h" gnus-summary-describe-briefly
1589     "i" gnus-info-find-node)
1590
1591   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1592     "e" gnus-summary-expire-articles
1593     "\M-\C-e" gnus-summary-expire-articles-now
1594     "\177" gnus-summary-delete-article
1595     [delete] gnus-summary-delete-article
1596     [backspace] gnus-summary-delete-article
1597     "m" gnus-summary-move-article
1598     "r" gnus-summary-respool-article
1599     "w" gnus-summary-edit-article
1600     "c" gnus-summary-copy-article
1601     "B" gnus-summary-crosspost-article
1602     "q" gnus-summary-respool-query
1603     "t" gnus-summary-respool-trace
1604     "i" gnus-summary-import-article
1605     "p" gnus-summary-article-posted-p)
1606
1607   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1608     "o" gnus-summary-save-article
1609     "m" gnus-summary-save-article-mail
1610     "F" gnus-summary-write-article-file
1611     "r" gnus-summary-save-article-rmail
1612     "f" gnus-summary-save-article-file
1613     "b" gnus-summary-save-article-body-file
1614     "h" gnus-summary-save-article-folder
1615     "v" gnus-summary-save-article-vm
1616     "p" gnus-summary-pipe-output
1617     "s" gnus-soup-add-article)
1618
1619   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1620     "b" gnus-summary-display-buttonized
1621     "m" gnus-summary-repair-multipart
1622     "v" gnus-article-view-part
1623     "o" gnus-article-save-part
1624     "c" gnus-article-copy-part
1625     "e" gnus-article-externalize-part
1626     "i" gnus-article-inline-part
1627     "|" gnus-article-pipe-part))
1628
1629 (defun gnus-summary-make-menu-bar ()
1630   (gnus-turn-off-edit-menu 'summary)
1631
1632   (unless (boundp 'gnus-summary-misc-menu)
1633
1634     (easy-menu-define
1635      gnus-summary-kill-menu gnus-summary-mode-map ""
1636      (cons
1637       "Score"
1638       (nconc
1639        (list
1640         ["Enter score..." gnus-summary-score-entry t]
1641         ["Customize" gnus-score-customize t])
1642        (gnus-make-score-map 'increase)
1643        (gnus-make-score-map 'lower)
1644        '(("Mark"
1645           ["Kill below" gnus-summary-kill-below t]
1646           ["Mark above" gnus-summary-mark-above t]
1647           ["Tick above" gnus-summary-tick-above t]
1648           ["Clear above" gnus-summary-clear-above t])
1649          ["Current score" gnus-summary-current-score t]
1650          ["Set score" gnus-summary-set-score t]
1651          ["Switch current score file..." gnus-score-change-score-file t]
1652          ["Set mark below..." gnus-score-set-mark-below t]
1653          ["Set expunge below..." gnus-score-set-expunge-below t]
1654          ["Edit current score file" gnus-score-edit-current-scores t]
1655          ["Edit score file" gnus-score-edit-file t]
1656          ["Trace score" gnus-score-find-trace t]
1657          ["Find words" gnus-score-find-favourite-words t]
1658          ["Rescore buffer" gnus-summary-rescore t]
1659          ["Increase score..." gnus-summary-increase-score t]
1660          ["Lower score..." gnus-summary-lower-score t]))))
1661
1662     ;; Define both the Article menu in the summary buffer and the equivalent
1663     ;; Commands menu in the article buffer here for consistency.
1664     (let ((innards
1665            '(("Hide"
1666               ["All" gnus-article-hide t]
1667               ["Headers" gnus-article-hide-headers t]
1668               ["Signature" gnus-article-hide-signature t]
1669               ["Citation" gnus-article-hide-citation t]
1670               ["List identifiers" gnus-article-hide-list-identifiers t]
1671               ["PGP" gnus-article-hide-pgp t]
1672               ["Banner" gnus-article-strip-banner t]
1673               ["Boring headers" gnus-article-hide-boring-headers t])
1674              ("Highlight"
1675               ["All" gnus-article-highlight t]
1676               ["Headers" gnus-article-highlight-headers t]
1677               ["Signature" gnus-article-highlight-signature t]
1678               ["Citation" gnus-article-highlight-citation t])
1679              ("MIME"
1680               ["Words" gnus-article-decode-mime-words t]
1681               ["Charset" gnus-article-decode-charset t]
1682               ["QP" gnus-article-de-quoted-unreadable t]
1683               ["View all" gnus-mime-view-all-parts t])
1684              ("Date"
1685               ["Local" gnus-article-date-local t]
1686               ["ISO8601" gnus-article-date-iso8601 t]
1687               ["UT" gnus-article-date-ut t]
1688               ["Original" gnus-article-date-original t]
1689               ["Lapsed" gnus-article-date-lapsed t]
1690               ["User-defined" gnus-article-date-user t])
1691              ("Washing"
1692               ("Remove Blanks"
1693                ["Leading" gnus-article-strip-leading-blank-lines t]
1694                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1695                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1696                ["All of the above" gnus-article-strip-blank-lines t]
1697                ["All" gnus-article-strip-all-blank-lines t]
1698                ["Leading space" gnus-article-strip-leading-space t]
1699                ["Trailing space" gnus-article-strip-trailing-space t])
1700               ["Overstrike" gnus-article-treat-overstrike t]
1701               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1702               ["Emphasis" gnus-article-emphasize t]
1703               ["Word wrap" gnus-article-fill-cited-article t]
1704               ["Fill long lines" gnus-article-fill-long-lines t]
1705               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1706               ["CR" gnus-article-remove-cr t]
1707               ["Show X-Face" gnus-article-display-x-face t]
1708               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1709               ["Rot 13" gnus-summary-caesar-message t]
1710               ["Unix pipe" gnus-summary-pipe-message t]
1711               ["Add buttons" gnus-article-add-buttons t]
1712               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1713               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1714               ["Verbose header" gnus-summary-verbose-headers t]
1715               ["Toggle header" gnus-summary-toggle-header t]
1716               ["HZ" gnus-article-decode-HZ t])
1717              ("Output"
1718               ["Save in default format" gnus-summary-save-article t]
1719               ["Save in file" gnus-summary-save-article-file t]
1720               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1721               ["Save in MH folder" gnus-summary-save-article-folder t]
1722               ["Save in VM folder" gnus-summary-save-article-vm t]
1723               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1724               ["Save body in file" gnus-summary-save-article-body-file t]
1725               ["Pipe through a filter" gnus-summary-pipe-output t]
1726               ["Add to SOUP packet" gnus-soup-add-article t]
1727               ["Print" gnus-summary-print-article t])
1728              ("Backend"
1729               ["Respool article..." gnus-summary-respool-article t]
1730               ["Move article..." gnus-summary-move-article
1731                (gnus-check-backend-function
1732                 'request-move-article gnus-newsgroup-name)]
1733               ["Copy article..." gnus-summary-copy-article t]
1734               ["Crosspost article..." gnus-summary-crosspost-article
1735                (gnus-check-backend-function
1736                 'request-replace-article gnus-newsgroup-name)]
1737               ["Import file..." gnus-summary-import-article t]
1738               ["Check if posted" gnus-summary-article-posted-p t]
1739               ["Edit article" gnus-summary-edit-article
1740                (not (gnus-group-read-only-p))]
1741               ["Delete article" gnus-summary-delete-article
1742                (gnus-check-backend-function
1743                 'request-expire-articles gnus-newsgroup-name)]
1744               ["Query respool" gnus-summary-respool-query t]
1745               ["Trace respool" gnus-summary-respool-trace t]
1746               ["Delete expirable articles" gnus-summary-expire-articles-now
1747                (gnus-check-backend-function
1748                 'request-expire-articles gnus-newsgroup-name)])
1749              ("Extract"
1750               ["Uudecode" gnus-uu-decode-uu t]
1751               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1752               ["Unshar" gnus-uu-decode-unshar t]
1753               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1754               ["Save" gnus-uu-decode-save t]
1755               ["Binhex" gnus-uu-decode-binhex t]
1756               ["Postscript" gnus-uu-decode-postscript t])
1757              ("Cache"
1758               ["Enter article" gnus-cache-enter-article t]
1759               ["Remove article" gnus-cache-remove-article t])
1760              ["Translate" gnus-article-babel t]
1761              ["Select article buffer" gnus-summary-select-article-buffer t]
1762              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1763              ["Isearch article..." gnus-summary-isearch-article t]
1764              ["Beginning of the article" gnus-summary-beginning-of-article t]
1765              ["End of the article" gnus-summary-end-of-article t]
1766              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1767              ["Fetch referenced articles" gnus-summary-refer-references t]
1768              ["Fetch current thread" gnus-summary-refer-thread t]
1769              ["Fetch article with id..." gnus-summary-refer-article t]
1770              ["Redisplay" gnus-summary-show-article t])))
1771       (easy-menu-define
1772        gnus-summary-article-menu gnus-summary-mode-map ""
1773        (cons "Article" innards))
1774
1775       (easy-menu-define
1776        gnus-article-commands-menu gnus-article-mode-map ""
1777        (cons "Commands" innards)))
1778
1779     (easy-menu-define
1780      gnus-summary-thread-menu gnus-summary-mode-map ""
1781      '("Threads"
1782        ["Toggle threading" gnus-summary-toggle-threads t]
1783        ["Hide threads" gnus-summary-hide-all-threads t]
1784        ["Show threads" gnus-summary-show-all-threads t]
1785        ["Hide thread" gnus-summary-hide-thread t]
1786        ["Show thread" gnus-summary-show-thread t]
1787        ["Go to next thread" gnus-summary-next-thread t]
1788        ["Go to previous thread" gnus-summary-prev-thread t]
1789        ["Go down thread" gnus-summary-down-thread t]
1790        ["Go up thread" gnus-summary-up-thread t]
1791        ["Top of thread" gnus-summary-top-thread t]
1792        ["Mark thread as read" gnus-summary-kill-thread t]
1793        ["Lower thread score" gnus-summary-lower-thread t]
1794        ["Raise thread score" gnus-summary-raise-thread t]
1795        ["Rethread current" gnus-summary-rethread-current t]))
1796
1797     (easy-menu-define
1798      gnus-summary-post-menu gnus-summary-mode-map ""
1799      '("Post"
1800        ["Post an article" gnus-summary-post-news t]
1801        ["Followup" gnus-summary-followup t]
1802        ["Followup and yank" gnus-summary-followup-with-original t]
1803        ["Supersede article" gnus-summary-supersede-article t]
1804        ["Cancel article" gnus-summary-cancel-article t]
1805        ["Reply" gnus-summary-reply t]
1806        ["Reply and yank" gnus-summary-reply-with-original t]
1807        ["Wide reply" gnus-summary-wide-reply t]
1808        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1809        ["Mail forward" gnus-summary-mail-forward t]
1810        ["Post forward" gnus-summary-post-forward t]
1811        ["Digest and mail" gnus-uu-digest-mail-forward t]
1812        ["Digest and post" gnus-uu-digest-post-forward t]
1813        ["Resend message" gnus-summary-resend-message t]
1814        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1815        ["Send a mail" gnus-summary-mail-other-window t]
1816        ["Uuencode and post" gnus-uu-post-news t]
1817        ["Followup via news" gnus-summary-followup-to-mail t]
1818        ["Followup via news and yank"
1819         gnus-summary-followup-to-mail-with-original t]
1820        ;;("Draft"
1821        ;;["Send" gnus-summary-send-draft t]
1822        ;;["Send bounced" gnus-resend-bounced-mail t])
1823        ))
1824
1825     (easy-menu-define
1826      gnus-summary-misc-menu gnus-summary-mode-map ""
1827      '("Misc"
1828        ("Mark Read"
1829         ["Mark as read" gnus-summary-mark-as-read-forward t]
1830         ["Mark same subject and select"
1831          gnus-summary-kill-same-subject-and-select t]
1832         ["Mark same subject" gnus-summary-kill-same-subject t]
1833         ["Catchup" gnus-summary-catchup t]
1834         ["Catchup all" gnus-summary-catchup-all t]
1835         ["Catchup to here" gnus-summary-catchup-to-here t]
1836         ["Catchup region" gnus-summary-mark-region-as-read t]
1837         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1838        ("Mark Various"
1839         ["Tick" gnus-summary-tick-article-forward t]
1840         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1841         ["Remove marks" gnus-summary-clear-mark-forward t]
1842         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1843         ["Set bookmark" gnus-summary-set-bookmark t]
1844         ["Remove bookmark" gnus-summary-remove-bookmark t])
1845        ("Mark Limit"
1846         ["Marks..." gnus-summary-limit-to-marks t]
1847         ["Subject..." gnus-summary-limit-to-subject t]
1848         ["Author..." gnus-summary-limit-to-author t]
1849         ["Age..." gnus-summary-limit-to-age t]
1850         ["Extra..." gnus-summary-limit-to-extra t]
1851         ["Score" gnus-summary-limit-to-score t]
1852         ["Unread" gnus-summary-limit-to-unread t]
1853         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1854         ["Articles" gnus-summary-limit-to-articles t]
1855         ["Pop limit" gnus-summary-pop-limit t]
1856         ["Show dormant" gnus-summary-limit-include-dormant t]
1857         ["Hide childless dormant"
1858          gnus-summary-limit-exclude-childless-dormant t]
1859         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1860         ["Hide marked" gnus-summary-limit-exclude-marks t]
1861         ["Show expunged" gnus-summary-show-all-expunged t])
1862        ("Process Mark"
1863         ["Set mark" gnus-summary-mark-as-processable t]
1864         ["Remove mark" gnus-summary-unmark-as-processable t]
1865         ["Remove all marks" gnus-summary-unmark-all-processable t]
1866         ["Mark above" gnus-uu-mark-over t]
1867         ["Mark series" gnus-uu-mark-series t]
1868         ["Mark region" gnus-uu-mark-region t]
1869         ["Unmark region" gnus-uu-unmark-region t]
1870         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1871         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1872         ["Mark all" gnus-uu-mark-all t]
1873         ["Mark buffer" gnus-uu-mark-buffer t]
1874         ["Mark sparse" gnus-uu-mark-sparse t]
1875         ["Mark thread" gnus-uu-mark-thread t]
1876         ["Unmark thread" gnus-uu-unmark-thread t]
1877         ("Process Mark Sets"
1878          ["Kill" gnus-summary-kill-process-mark t]
1879          ["Yank" gnus-summary-yank-process-mark
1880           gnus-newsgroup-process-stack]
1881          ["Save" gnus-summary-save-process-mark t]))
1882        ("Scroll article"
1883         ["Page forward" gnus-summary-next-page t]
1884         ["Page backward" gnus-summary-prev-page t]
1885         ["Line forward" gnus-summary-scroll-up t])
1886        ("Move"
1887         ["Next unread article" gnus-summary-next-unread-article t]
1888         ["Previous unread article" gnus-summary-prev-unread-article t]
1889         ["Next article" gnus-summary-next-article t]
1890         ["Previous article" gnus-summary-prev-article t]
1891         ["Next unread subject" gnus-summary-next-unread-subject t]
1892         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1893         ["Next article same subject" gnus-summary-next-same-subject t]
1894         ["Previous article same subject" gnus-summary-prev-same-subject t]
1895         ["First unread article" gnus-summary-first-unread-article t]
1896         ["Best unread article" gnus-summary-best-unread-article t]
1897         ["Go to subject number..." gnus-summary-goto-subject t]
1898         ["Go to article number..." gnus-summary-goto-article t]
1899         ["Go to the last article" gnus-summary-goto-last-article t]
1900         ["Pop article off history" gnus-summary-pop-article t])
1901        ("Sort"
1902         ["Sort by number" gnus-summary-sort-by-number t]
1903         ["Sort by author" gnus-summary-sort-by-author t]
1904         ["Sort by subject" gnus-summary-sort-by-subject t]
1905         ["Sort by date" gnus-summary-sort-by-date t]
1906         ["Sort by score" gnus-summary-sort-by-score t]
1907         ["Sort by lines" gnus-summary-sort-by-lines t]
1908         ["Sort by characters" gnus-summary-sort-by-chars t])
1909        ("Help"
1910         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1911         ["Describe group" gnus-summary-describe-group t]
1912         ["Read manual" gnus-info-find-node t])
1913        ("Modes"
1914         ["Pick and read" gnus-pick-mode t]
1915         ["Binary" gnus-binary-mode t])
1916        ("Regeneration"
1917         ["Regenerate" gnus-summary-prepare t]
1918         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1919         ["Toggle threading" gnus-summary-toggle-threads t])
1920        ["Filter articles..." gnus-summary-execute-command t]
1921        ["Run command on subjects..." gnus-summary-universal-argument t]
1922        ["Search articles forward..." gnus-summary-search-article-forward t]
1923        ["Search articles backward..." gnus-summary-search-article-backward t]
1924        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1925        ["Expand window" gnus-summary-expand-window t]
1926        ["Expire expirable articles" gnus-summary-expire-articles
1927         (gnus-check-backend-function
1928          'request-expire-articles gnus-newsgroup-name)]
1929        ["Edit local kill file" gnus-summary-edit-local-kill t]
1930        ["Edit main kill file" gnus-summary-edit-global-kill t]
1931        ["Edit group parameters" gnus-summary-edit-parameters t]
1932        ["Customize group parameters" gnus-summary-customize-parameters t]
1933        ["Send a bug report" gnus-bug t]
1934        ("Exit"
1935         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1936         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1937         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1938         ["Exit group" gnus-summary-exit t]
1939         ["Exit group without updating" gnus-summary-exit-no-update t]
1940         ["Exit and goto next group" gnus-summary-next-group t]
1941         ["Exit and goto prev group" gnus-summary-prev-group t]
1942         ["Reselect group" gnus-summary-reselect-current-group t]
1943         ["Rescan group" gnus-summary-rescan-group t]
1944         ["Update dribble" gnus-summary-save-newsrc t])))
1945
1946     (gnus-run-hooks 'gnus-summary-menu-hook)))
1947
1948 (defun gnus-score-set-default (var value)
1949   "A version of set that updates the GNU Emacs menu-bar."
1950   (set var value)
1951   ;; It is the message that forces the active status to be updated.
1952   (message ""))
1953
1954 (defun gnus-make-score-map (type)
1955   "Make a summary score map of type TYPE."
1956   (if t
1957       nil
1958     (let ((headers '(("author" "from" string)
1959                      ("subject" "subject" string)
1960                      ("article body" "body" string)
1961                      ("article head" "head" string)
1962                      ("xref" "xref" string)
1963                      ("extra header" "extra" string)
1964                      ("lines" "lines" number)
1965                      ("followups to author" "followup" string)))
1966           (types '((number ("less than" <)
1967                            ("greater than" >)
1968                            ("equal" =))
1969                    (string ("substring" s)
1970                            ("exact string" e)
1971                            ("fuzzy string" f)
1972                            ("regexp" r))))
1973           (perms '(("temporary" (current-time-string))
1974                    ("permanent" nil)
1975                    ("immediate" now)))
1976           header)
1977       (list
1978        (apply
1979         'nconc
1980         (list
1981          (if (eq type 'lower)
1982              "Lower score"
1983            "Increase score"))
1984         (let (outh)
1985           (while headers
1986             (setq header (car headers))
1987             (setq outh
1988                   (cons
1989                    (apply
1990                     'nconc
1991                     (list (car header))
1992                     (let ((ts (cdr (assoc (nth 2 header) types)))
1993                           outt)
1994                       (while ts
1995                         (setq outt
1996                               (cons
1997                                (apply
1998                                 'nconc
1999                                 (list (caar ts))
2000                                 (let ((ps perms)
2001                                       outp)
2002                                   (while ps
2003                                     (setq outp
2004                                           (cons
2005                                            (vector
2006                                             (caar ps)
2007                                             (list
2008                                              'gnus-summary-score-entry
2009                                              (nth 1 header)
2010                                              (if (or (string= (nth 1 header)
2011                                                               "head")
2012                                                      (string= (nth 1 header)
2013                                                               "body"))
2014                                                  ""
2015                                                (list 'gnus-summary-header
2016                                                      (nth 1 header)))
2017                                              (list 'quote (nth 1 (car ts)))
2018                                              (list 'gnus-score-delta-default
2019                                                    nil)
2020                                              (nth 1 (car ps))
2021                                              t)
2022                                             t)
2023                                            outp))
2024                                     (setq ps (cdr ps)))
2025                                   (list (nreverse outp))))
2026                                outt))
2027                         (setq ts (cdr ts)))
2028                       (list (nreverse outt))))
2029                    outh))
2030             (setq headers (cdr headers)))
2031           (list (nreverse outh))))))))
2032
2033 \f
2034
2035 (defun gnus-summary-mode (&optional group)
2036   "Major mode for reading articles.
2037
2038 All normal editing commands are switched off.
2039 \\<gnus-summary-mode-map>
2040 Each line in this buffer represents one article.  To read an
2041 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2042 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2043 respectively.
2044
2045 You can also post articles and send mail from this buffer.  To
2046 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2047 of an article, type `\\[gnus-summary-reply]'.
2048
2049 There are approx. one gazillion commands you can execute in this
2050 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2051
2052 The following commands are available:
2053
2054 \\{gnus-summary-mode-map}"
2055   (interactive)
2056   (when (gnus-visual-p 'summary-menu 'menu)
2057     (gnus-summary-make-menu-bar))
2058   (kill-all-local-variables)
2059   (gnus-summary-make-local-variables)
2060   (gnus-make-thread-indent-array)
2061   (gnus-simplify-mode-line)
2062   (setq major-mode 'gnus-summary-mode)
2063   (setq mode-name "Summary")
2064   (make-local-variable 'minor-mode-alist)
2065   (use-local-map gnus-summary-mode-map)
2066   (buffer-disable-undo)
2067   (setq buffer-read-only t)             ;Disable modification
2068   (setq truncate-lines t)
2069   (setq selective-display t)
2070   (setq selective-display-ellipses t)   ;Display `...'
2071   (gnus-summary-set-display-table)
2072   (gnus-set-default-directory)
2073   (setq gnus-newsgroup-name group)
2074   (make-local-variable 'gnus-summary-line-format)
2075   (make-local-variable 'gnus-summary-line-format-spec)
2076   (make-local-variable 'gnus-summary-dummy-line-format)
2077   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2078   (make-local-variable 'gnus-summary-mark-positions)
2079   (make-local-hook 'pre-command-hook)
2080   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2081   (gnus-run-hooks 'gnus-summary-mode-hook)
2082   (mm-enable-multibyte)
2083   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2084   (gnus-update-summary-mark-positions))
2085
2086 (defun gnus-summary-make-local-variables ()
2087   "Make all the local summary buffer variables."
2088   (let (global)
2089     (dolist (local gnus-summary-local-variables)
2090       (if (consp local)
2091           (progn
2092             (if (eq (cdr local) 'global)
2093                 ;; Copy the global value of the variable.
2094                 (setq global (symbol-value (car local)))
2095               ;; Use the value from the list.
2096               (setq global (eval (cdr local))))
2097             (set (make-local-variable (car local)) global))
2098         ;; Simple nil-valued local variable.
2099         (set (make-local-variable local) nil)))))
2100
2101 (defun gnus-summary-clear-local-variables ()
2102   (let ((locals gnus-summary-local-variables))
2103     (while locals
2104       (if (consp (car locals))
2105           (and (vectorp (caar locals))
2106                (set (caar locals) nil))
2107         (and (vectorp (car locals))
2108              (set (car locals) nil)))
2109       (setq locals (cdr locals)))))
2110
2111 ;; Summary data functions.
2112
2113 (defmacro gnus-data-number (data)
2114   `(car ,data))
2115
2116 (defmacro gnus-data-set-number (data number)
2117   `(setcar ,data ,number))
2118
2119 (defmacro gnus-data-mark (data)
2120   `(nth 1 ,data))
2121
2122 (defmacro gnus-data-set-mark (data mark)
2123   `(setcar (nthcdr 1 ,data) ,mark))
2124
2125 (defmacro gnus-data-pos (data)
2126   `(nth 2 ,data))
2127
2128 (defmacro gnus-data-set-pos (data pos)
2129   `(setcar (nthcdr 2 ,data) ,pos))
2130
2131 (defmacro gnus-data-header (data)
2132   `(nth 3 ,data))
2133
2134 (defmacro gnus-data-set-header (data header)
2135   `(setf (nth 3 ,data) ,header))
2136
2137 (defmacro gnus-data-level (data)
2138   `(nth 4 ,data))
2139
2140 (defmacro gnus-data-unread-p (data)
2141   `(= (nth 1 ,data) gnus-unread-mark))
2142
2143 (defmacro gnus-data-read-p (data)
2144   `(/= (nth 1 ,data) gnus-unread-mark))
2145
2146 (defmacro gnus-data-pseudo-p (data)
2147   `(consp (nth 3 ,data)))
2148
2149 (defmacro gnus-data-find (number)
2150   `(assq ,number gnus-newsgroup-data))
2151
2152 (defmacro gnus-data-find-list (number &optional data)
2153   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2154      (memq (assq ,number bdata)
2155            bdata)))
2156
2157 (defmacro gnus-data-make (number mark pos header level)
2158   `(list ,number ,mark ,pos ,header ,level))
2159
2160 (defun gnus-data-enter (after-article number mark pos header level offset)
2161   (let ((data (gnus-data-find-list after-article)))
2162     (unless data
2163       (error "No such article: %d" after-article))
2164     (setcdr data (cons (gnus-data-make number mark pos header level)
2165                        (cdr data)))
2166     (setq gnus-newsgroup-data-reverse nil)
2167     (gnus-data-update-list (cddr data) offset)))
2168
2169 (defun gnus-data-enter-list (after-article list &optional offset)
2170   (when list
2171     (let ((data (and after-article (gnus-data-find-list after-article)))
2172           (ilist list))
2173       (if (not (or data
2174                    after-article))
2175           (let ((odata gnus-newsgroup-data))
2176             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2177             (when offset
2178               (gnus-data-update-list odata offset)))
2179         ;; Find the last element in the list to be spliced into the main
2180         ;; list.
2181         (while (cdr list)
2182           (setq list (cdr list)))
2183         (if (not data)
2184             (progn
2185               (setcdr list gnus-newsgroup-data)
2186               (setq gnus-newsgroup-data ilist)
2187               (when offset
2188                 (gnus-data-update-list (cdr list) offset)))
2189           (setcdr list (cdr data))
2190           (setcdr data ilist)
2191           (when offset
2192             (gnus-data-update-list (cdr list) offset))))
2193       (setq gnus-newsgroup-data-reverse nil))))
2194
2195 (defun gnus-data-remove (article &optional offset)
2196   (let ((data gnus-newsgroup-data))
2197     (if (= (gnus-data-number (car data)) article)
2198         (progn
2199           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2200                 gnus-newsgroup-data-reverse nil)
2201           (when offset
2202             (gnus-data-update-list gnus-newsgroup-data offset)))
2203       (while (cdr data)
2204         (when (= (gnus-data-number (cadr data)) article)
2205           (setcdr data (cddr data))
2206           (when offset
2207             (gnus-data-update-list (cdr data) offset))
2208           (setq data nil
2209                 gnus-newsgroup-data-reverse nil))
2210         (setq data (cdr data))))))
2211
2212 (defmacro gnus-data-list (backward)
2213   `(if ,backward
2214        (or gnus-newsgroup-data-reverse
2215            (setq gnus-newsgroup-data-reverse
2216                  (reverse gnus-newsgroup-data)))
2217      gnus-newsgroup-data))
2218
2219 (defun gnus-data-update-list (data offset)
2220   "Add OFFSET to the POS of all data entries in DATA."
2221   (setq gnus-newsgroup-data-reverse nil)
2222   (while data
2223     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2224     (setq data (cdr data))))
2225
2226 (defun gnus-summary-article-pseudo-p (article)
2227   "Say whether this article is a pseudo article or not."
2228   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2229
2230 (defmacro gnus-summary-article-sparse-p (article)
2231   "Say whether this article is a sparse article or not."
2232   `(memq ,article gnus-newsgroup-sparse))
2233
2234 (defmacro gnus-summary-article-ancient-p (article)
2235   "Say whether this article is a sparse article or not."
2236   `(memq ,article gnus-newsgroup-ancient))
2237
2238 (defun gnus-article-parent-p (number)
2239   "Say whether this article is a parent or not."
2240   (let ((data (gnus-data-find-list number)))
2241     (and (cdr data)                     ; There has to be an article after...
2242          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2243             (gnus-data-level (nth 1 data))))))
2244
2245 (defun gnus-article-children (number)
2246   "Return a list of all children to NUMBER."
2247   (let* ((data (gnus-data-find-list number))
2248          (level (gnus-data-level (car data)))
2249          children)
2250     (setq data (cdr data))
2251     (while (and data
2252                 (= (gnus-data-level (car data)) (1+ level)))
2253       (push (gnus-data-number (car data)) children)
2254       (setq data (cdr data)))
2255     children))
2256
2257 (defmacro gnus-summary-skip-intangible ()
2258   "If the current article is intangible, then jump to a different article."
2259   '(let ((to (get-text-property (point) 'gnus-intangible)))
2260      (and to (gnus-summary-goto-subject to))))
2261
2262 (defmacro gnus-summary-article-intangible-p ()
2263   "Say whether this article is intangible or not."
2264   '(get-text-property (point) 'gnus-intangible))
2265
2266 (defun gnus-article-read-p (article)
2267   "Say whether ARTICLE is read or not."
2268   (not (or (memq article gnus-newsgroup-marked)
2269            (memq article gnus-newsgroup-unreads)
2270            (memq article gnus-newsgroup-unselected)
2271            (memq article gnus-newsgroup-dormant))))
2272
2273 ;; Some summary mode macros.
2274
2275 (defmacro gnus-summary-article-number ()
2276   "The article number of the article on the current line.
2277 If there isn's an article number here, then we return the current
2278 article number."
2279   '(progn
2280      (gnus-summary-skip-intangible)
2281      (or (get-text-property (point) 'gnus-number)
2282          (gnus-summary-last-subject))))
2283
2284 (defmacro gnus-summary-article-header (&optional number)
2285   "Return the header of article NUMBER."
2286   `(gnus-data-header (gnus-data-find
2287                       ,(or number '(gnus-summary-article-number)))))
2288
2289 (defmacro gnus-summary-thread-level (&optional number)
2290   "Return the level of thread that starts with article NUMBER."
2291   `(if (and (eq gnus-summary-make-false-root 'dummy)
2292             (get-text-property (point) 'gnus-intangible))
2293        0
2294      (gnus-data-level (gnus-data-find
2295                        ,(or number '(gnus-summary-article-number))))))
2296
2297 (defmacro gnus-summary-article-mark (&optional number)
2298   "Return the mark of article NUMBER."
2299   `(gnus-data-mark (gnus-data-find
2300                     ,(or number '(gnus-summary-article-number)))))
2301
2302 (defmacro gnus-summary-article-pos (&optional number)
2303   "Return the position of the line of article NUMBER."
2304   `(gnus-data-pos (gnus-data-find
2305                    ,(or number '(gnus-summary-article-number)))))
2306
2307 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2308 (defmacro gnus-summary-article-subject (&optional number)
2309   "Return current subject string or nil if nothing."
2310   `(let ((headers
2311           ,(if number
2312                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2313              '(gnus-data-header (assq (gnus-summary-article-number)
2314                                       gnus-newsgroup-data)))))
2315      (and headers
2316           (vectorp headers)
2317           (mail-header-subject headers))))
2318
2319 (defmacro gnus-summary-article-score (&optional number)
2320   "Return current article score."
2321   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2322                   gnus-newsgroup-scored))
2323        gnus-summary-default-score 0))
2324
2325 (defun gnus-summary-article-children (&optional number)
2326   "Return a list of article numbers that are children of article NUMBER."
2327   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2328          (level (gnus-data-level (car data)))
2329          l children)
2330     (while (and (setq data (cdr data))
2331                 (> (setq l (gnus-data-level (car data))) level))
2332       (and (= (1+ level) l)
2333            (push (gnus-data-number (car data))
2334                  children)))
2335     (nreverse children)))
2336
2337 (defun gnus-summary-article-parent (&optional number)
2338   "Return the article number of the parent of article NUMBER."
2339   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2340                                     (gnus-data-list t)))
2341          (level (gnus-data-level (car data))))
2342     (if (zerop level)
2343         ()                              ; This is a root.
2344       ;; We search until we find an article with a level less than
2345       ;; this one.  That function has to be the parent.
2346       (while (and (setq data (cdr data))
2347                   (not (< (gnus-data-level (car data)) level))))
2348       (and data (gnus-data-number (car data))))))
2349
2350 (defun gnus-unread-mark-p (mark)
2351   "Say whether MARK is the unread mark."
2352   (= mark gnus-unread-mark))
2353
2354 (defun gnus-read-mark-p (mark)
2355   "Say whether MARK is one of the marks that mark as read.
2356 This is all marks except unread, ticked, dormant, and expirable."
2357   (not (or (= mark gnus-unread-mark)
2358            (= mark gnus-ticked-mark)
2359            (= mark gnus-dormant-mark)
2360            (= mark gnus-expirable-mark))))
2361
2362 (defmacro gnus-article-mark (number)
2363   "Return the MARK of article NUMBER.
2364 This macro should only be used when computing the mark the \"first\"
2365 time; i.e., when generating the summary lines.  After that,
2366 `gnus-summary-article-mark' should be used to examine the
2367 marks of articles."
2368   `(cond
2369     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2370     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2371     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2372     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2373     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2374     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2375     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2376     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2377            gnus-ancient-mark))))
2378
2379 ;; Saving hidden threads.
2380
2381 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2382 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2383
2384 (defmacro gnus-save-hidden-threads (&rest forms)
2385   "Save hidden threads, eval FORMS, and restore the hidden threads."
2386   (let ((config (make-symbol "config")))
2387     `(let ((,config (gnus-hidden-threads-configuration)))
2388        (unwind-protect
2389            (save-excursion
2390              ,@forms)
2391          (gnus-restore-hidden-threads-configuration ,config)))))
2392
2393 (defun gnus-data-compute-positions ()
2394   "Compute the positions of all articles."
2395   (setq gnus-newsgroup-data-reverse nil)
2396   (let ((data gnus-newsgroup-data))
2397     (save-excursion
2398       (gnus-save-hidden-threads
2399         (gnus-summary-show-all-threads)
2400         (goto-char (point-min))
2401         (while data
2402           (while (get-text-property (point) 'gnus-intangible)
2403             (forward-line 1))
2404           (gnus-data-set-pos (car data) (+ (point) 3))
2405           (setq data (cdr data))
2406           (forward-line 1))))))
2407
2408 (defun gnus-hidden-threads-configuration ()
2409   "Return the current hidden threads configuration."
2410   (save-excursion
2411     (let (config)
2412       (goto-char (point-min))
2413       (while (search-forward "\r" nil t)
2414         (push (1- (point)) config))
2415       config)))
2416
2417 (defun gnus-restore-hidden-threads-configuration (config)
2418   "Restore hidden threads configuration from CONFIG."
2419   (let (point buffer-read-only)
2420     (while (setq point (pop config))
2421       (when (and (< point (point-max))
2422                  (goto-char point)
2423                  (eq (char-after) ?\n))
2424         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2425
2426 ;; Various summary mode internalish functions.
2427
2428 (defun gnus-mouse-pick-article (e)
2429   (interactive "e")
2430   (mouse-set-point e)
2431   (gnus-summary-next-page nil t))
2432
2433 (defun gnus-summary-set-display-table ()
2434   ;; Change the display table.  Odd characters have a tendency to mess
2435   ;; up nicely formatted displays - we make all possible glyphs
2436   ;; display only a single character.
2437
2438   ;; We start from the standard display table, if any.
2439   (let ((table (or (copy-sequence standard-display-table)
2440                    (make-display-table)))
2441         (i 32))
2442     ;; Nix out all the control chars...
2443     (while (>= (setq i (1- i)) 0)
2444       (aset table i [??]))
2445     ;; ... but not newline and cr, of course.  (cr is necessary for the
2446     ;; selective display).
2447     (aset table ?\n nil)
2448     (aset table ?\r nil)
2449     ;; We keep TAB as well.
2450     (aset table ?\t nil)
2451     ;; We nix out any glyphs over 126 that are not set already.
2452     (let ((i 256))
2453       (while (>= (setq i (1- i)) 127)
2454         ;; Only modify if the entry is nil.
2455         (unless (aref table i)
2456           (aset table i [??]))))
2457     (setq buffer-display-table table)))
2458
2459 (defun gnus-summary-setup-buffer (group)
2460   "Initialize summary buffer."
2461   (let ((buffer (concat "*Summary " group "*")))
2462     (if (get-buffer buffer)
2463         (progn
2464           (set-buffer buffer)
2465           (setq gnus-summary-buffer (current-buffer))
2466           (not gnus-newsgroup-prepared))
2467       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2468       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2469       (gnus-summary-mode group)
2470       (when gnus-carpal
2471         (gnus-carpal-setup-buffer 'summary))
2472       (unless gnus-single-article-buffer
2473         (make-local-variable 'gnus-article-buffer)
2474         (make-local-variable 'gnus-article-current)
2475         (make-local-variable 'gnus-original-article-buffer))
2476       (setq gnus-newsgroup-name group)
2477       t)))
2478
2479 (defun gnus-set-global-variables ()
2480   ;; Set the global equivalents of the summary buffer-local variables
2481   ;; to the latest values they had.  These reflect the summary buffer
2482   ;; that was in action when the last article was fetched.
2483   (when (eq major-mode 'gnus-summary-mode)
2484     (setq gnus-summary-buffer (current-buffer))
2485     (let ((name gnus-newsgroup-name)
2486           (marked gnus-newsgroup-marked)
2487           (unread gnus-newsgroup-unreads)
2488           (headers gnus-current-headers)
2489           (data gnus-newsgroup-data)
2490           (summary gnus-summary-buffer)
2491           (article-buffer gnus-article-buffer)
2492           (original gnus-original-article-buffer)
2493           (gac gnus-article-current)
2494           (reffed gnus-reffed-article-number)
2495           (score-file gnus-current-score-file)
2496           (default-charset gnus-newsgroup-charset))
2497       (save-excursion
2498         (set-buffer gnus-group-buffer)
2499         (setq gnus-newsgroup-name name
2500               gnus-newsgroup-marked marked
2501               gnus-newsgroup-unreads unread
2502               gnus-current-headers headers
2503               gnus-newsgroup-data data
2504               gnus-article-current gac
2505               gnus-summary-buffer summary
2506               gnus-article-buffer article-buffer
2507               gnus-original-article-buffer original
2508               gnus-reffed-article-number reffed
2509               gnus-current-score-file score-file
2510               gnus-newsgroup-charset default-charset)
2511         ;; The article buffer also has local variables.
2512         (when (gnus-buffer-live-p gnus-article-buffer)
2513           (set-buffer gnus-article-buffer)
2514           (setq gnus-summary-buffer summary))))))
2515
2516 (defun gnus-summary-article-unread-p (article)
2517   "Say whether ARTICLE is unread or not."
2518   (memq article gnus-newsgroup-unreads))
2519
2520 (defun gnus-summary-first-article-p (&optional article)
2521   "Return whether ARTICLE is the first article in the buffer."
2522   (if (not (setq article (or article (gnus-summary-article-number))))
2523       nil
2524     (eq article (caar gnus-newsgroup-data))))
2525
2526 (defun gnus-summary-last-article-p (&optional article)
2527   "Return whether ARTICLE is the last article in the buffer."
2528   (if (not (setq article (or article (gnus-summary-article-number))))
2529       ;; All non-existent numbers are the last article.  :-)
2530       t
2531     (not (cdr (gnus-data-find-list article)))))
2532
2533 (defun gnus-make-thread-indent-array ()
2534   (let ((n 200))
2535     (unless (and gnus-thread-indent-array
2536                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2537       (setq gnus-thread-indent-array (make-vector 201 "")
2538             gnus-thread-indent-array-level gnus-thread-indent-level)
2539       (while (>= n 0)
2540         (aset gnus-thread-indent-array n
2541               (make-string (* n gnus-thread-indent-level) ? ))
2542         (setq n (1- n))))))
2543
2544 (defun gnus-update-summary-mark-positions ()
2545   "Compute where the summary marks are to go."
2546   (save-excursion
2547     (when (gnus-buffer-exists-p gnus-summary-buffer)
2548       (set-buffer gnus-summary-buffer))
2549     (let ((gnus-replied-mark 129)
2550           (gnus-score-below-mark 130)
2551           (gnus-score-over-mark 130)
2552           (gnus-download-mark 131)
2553           (spec gnus-summary-line-format-spec)
2554           gnus-visual pos)
2555       (save-excursion
2556         (gnus-set-work-buffer)
2557         (let ((gnus-summary-line-format-spec spec)
2558               (gnus-newsgroup-downloadable '((0 . t))))
2559           (gnus-summary-insert-line
2560            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2561           (goto-char (point-min))
2562           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2563                                              (- (point) 2)))))
2564           (goto-char (point-min))
2565           (push (cons 'replied (and (search-forward "\201" nil t)
2566                                     (- (point) 2)))
2567                 pos)
2568           (goto-char (point-min))
2569           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2570                 pos)
2571           (goto-char (point-min))
2572           (push (cons 'download
2573                       (and (search-forward "\203" nil t) (- (point) 2)))
2574                 pos)))
2575       (setq gnus-summary-mark-positions pos))))
2576
2577 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2578   "Insert a dummy root in the summary buffer."
2579   (beginning-of-line)
2580   (gnus-add-text-properties
2581    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2582    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2583
2584 (defun gnus-summary-from-or-to-or-newsgroups (header)
2585   (let ((to (cdr (assq 'To (mail-header-extra header))))
2586         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2587         (mail-parse-charset gnus-newsgroup-charset)
2588         (mail-parse-ignored-charsets 
2589          (save-excursion (set-buffer gnus-summary-buffer)
2590                          gnus-newsgroup-ignored-charsets)))
2591     (cond
2592      ((and to
2593            gnus-ignored-from-addresses
2594            (string-match gnus-ignored-from-addresses
2595                          (mail-header-from header)))
2596       (concat "-> "
2597               (or (car (funcall gnus-extract-address-components
2598                                 (funcall
2599                                  gnus-decode-encoded-word-function to)))
2600                   (funcall gnus-decode-encoded-word-function to))))
2601      ((and newsgroups
2602            gnus-ignored-from-addresses
2603            (string-match gnus-ignored-from-addresses
2604                          (mail-header-from header)))
2605       (concat "=> " newsgroups))
2606      (t
2607       (or (car (funcall gnus-extract-address-components
2608                         (mail-header-from header)))
2609           (mail-header-from header))))))
2610
2611 (defun gnus-summary-insert-line (gnus-tmp-header
2612                                  gnus-tmp-level gnus-tmp-current
2613                                  gnus-tmp-unread gnus-tmp-replied
2614                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2615                                  &optional gnus-tmp-dummy gnus-tmp-score
2616                                  gnus-tmp-process)
2617   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2618          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2619          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2620          (gnus-tmp-score-char
2621           (if (or (null gnus-summary-default-score)
2622                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2623                       gnus-summary-zcore-fuzz))
2624               ?  ;Whitespace
2625             (if (< gnus-tmp-score gnus-summary-default-score)
2626                 gnus-score-below-mark gnus-score-over-mark)))
2627          (gnus-tmp-replied
2628           (cond (gnus-tmp-process gnus-process-mark)
2629                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2630                  gnus-cached-mark)
2631                 (gnus-tmp-replied gnus-replied-mark)
2632                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2633                  gnus-saved-mark)
2634                 (t gnus-unread-mark)))
2635          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2636          (gnus-tmp-name
2637           (cond
2638            ((string-match "<[^>]+> *$" gnus-tmp-from)
2639             (let ((beg (match-beginning 0)))
2640               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2641                        (substring gnus-tmp-from (1+ (match-beginning 0))
2642                                   (1- (match-end 0))))
2643                   (substring gnus-tmp-from 0 beg))))
2644            ((string-match "(.+)" gnus-tmp-from)
2645             (substring gnus-tmp-from
2646                        (1+ (match-beginning 0)) (1- (match-end 0))))
2647            (t gnus-tmp-from)))
2648          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2649          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2650          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2651          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2652          (buffer-read-only nil))
2653     (when (string= gnus-tmp-name "")
2654       (setq gnus-tmp-name gnus-tmp-from))
2655     (unless (numberp gnus-tmp-lines)
2656       (setq gnus-tmp-lines 0))
2657     (gnus-put-text-property
2658      (point)
2659      (progn (eval gnus-summary-line-format-spec) (point))
2660      'gnus-number gnus-tmp-number)
2661     (when (gnus-visual-p 'summary-highlight 'highlight)
2662       (forward-line -1)
2663       (gnus-run-hooks 'gnus-summary-update-hook)
2664       (forward-line 1))))
2665
2666 (defun gnus-summary-update-line (&optional dont-update)
2667   ;; Update summary line after change.
2668   (when (and gnus-summary-default-score
2669              (not gnus-summary-inhibit-highlight))
2670     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2671            (article (gnus-summary-article-number))
2672            (score (gnus-summary-article-score article)))
2673       (unless dont-update
2674         (if (and gnus-summary-mark-below
2675                  (< (gnus-summary-article-score)
2676                     gnus-summary-mark-below))
2677             ;; This article has a low score, so we mark it as read.
2678             (when (memq article gnus-newsgroup-unreads)
2679               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2680           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2681             ;; This article was previously marked as read on account
2682             ;; of a low score, but now it has risen, so we mark it as
2683             ;; unread.
2684             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2685         (gnus-summary-update-mark
2686          (if (or (null gnus-summary-default-score)
2687                  (<= (abs (- score gnus-summary-default-score))
2688                      gnus-summary-zcore-fuzz))
2689              ?  ;Whitespace
2690            (if (< score gnus-summary-default-score)
2691                gnus-score-below-mark gnus-score-over-mark))
2692          'score))
2693       ;; Do visual highlighting.
2694       (when (gnus-visual-p 'summary-highlight 'highlight)
2695         (gnus-run-hooks 'gnus-summary-update-hook)))))
2696
2697 (defvar gnus-tmp-new-adopts nil)
2698
2699 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2700   "Return the number of articles in THREAD.
2701 This may be 0 in some cases -- if none of the articles in
2702 the thread are to be displayed."
2703   (let* ((number
2704           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2705           (cond
2706            ((not (listp thread))
2707             1)
2708            ((and (consp thread) (cdr thread))
2709             (apply
2710              '+ 1 (mapcar
2711                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2712            ((null thread)
2713             1)
2714            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2715             1)
2716            (t 0))))
2717     (when (and level (zerop level) gnus-tmp-new-adopts)
2718       (incf number
2719             (apply '+ (mapcar
2720                        'gnus-summary-number-of-articles-in-thread
2721                        gnus-tmp-new-adopts))))
2722     (if char
2723         (if (> number 1) gnus-not-empty-thread-mark
2724           gnus-empty-thread-mark)
2725       number)))
2726
2727 (defun gnus-summary-set-local-parameters (group)
2728   "Go through the local params of GROUP and set all variable specs in that list."
2729   (let ((params (gnus-group-find-parameter group))
2730         elem)
2731     (while params
2732       (setq elem (car params)
2733             params (cdr params))
2734       (and (consp elem)                 ; Has to be a cons.
2735            (consp (cdr elem))           ; The cdr has to be a list.
2736            (symbolp (car elem))         ; Has to be a symbol in there.
2737            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2738            (ignore-errors               ; So we set it.
2739              (make-local-variable (car elem))
2740              (set (car elem) (eval (nth 1 elem))))))))
2741
2742 (defun gnus-summary-read-group (group &optional show-all no-article
2743                                       kill-buffer no-display backward
2744                                       select-articles)
2745   "Start reading news in newsgroup GROUP.
2746 If SHOW-ALL is non-nil, already read articles are also listed.
2747 If NO-ARTICLE is non-nil, no article is selected initially.
2748 If NO-DISPLAY, don't generate a summary buffer."
2749   (let (result)
2750     (while (and group
2751                 (null (setq result
2752                             (let ((gnus-auto-select-next nil))
2753                               (or (gnus-summary-read-group-1
2754                                    group show-all no-article
2755                                    kill-buffer no-display
2756                                    select-articles)
2757                                   (setq show-all nil
2758                                         select-articles nil)))))
2759                 (eq gnus-auto-select-next 'quietly))
2760       (set-buffer gnus-group-buffer)
2761       ;; The entry function called above goes to the next
2762       ;; group automatically, so we go two groups back
2763       ;; if we are searching for the previous group.
2764       (when backward
2765         (gnus-group-prev-unread-group 2))
2766       (if (not (equal group (gnus-group-group-name)))
2767           (setq group (gnus-group-group-name))
2768         (setq group nil)))
2769     result))
2770
2771 (defun gnus-summary-read-group-1 (group show-all no-article
2772                                         kill-buffer no-display
2773                                         &optional select-articles)
2774   ;; Killed foreign groups can't be entered.
2775   (when (and (not (gnus-group-native-p group))
2776              (not (gnus-gethash group gnus-newsrc-hashtb)))
2777     (error "Dead non-native groups can't be entered"))
2778   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2779   (let* ((new-group (gnus-summary-setup-buffer group))
2780          (quit-config (gnus-group-quit-config group))
2781          (did-select (and new-group (gnus-select-newsgroup
2782                                      group show-all select-articles))))
2783     (cond
2784      ;; This summary buffer exists already, so we just select it.
2785      ((not new-group)
2786       (gnus-set-global-variables)
2787       (when kill-buffer
2788         (gnus-kill-or-deaden-summary kill-buffer))
2789       (gnus-configure-windows 'summary 'force)
2790       (gnus-set-mode-line 'summary)
2791       (gnus-summary-position-point)
2792       (message "")
2793       t)
2794      ;; We couldn't select this group.
2795      ((null did-select)
2796       (when (and (eq major-mode 'gnus-summary-mode)
2797                  (not (equal (current-buffer) kill-buffer)))
2798         (kill-buffer (current-buffer))
2799         (if (not quit-config)
2800             (progn
2801               ;; Update the info -- marks might need to be removed,
2802               ;; for instance.
2803               (gnus-summary-update-info)
2804               (set-buffer gnus-group-buffer)
2805               (gnus-group-jump-to-group group)
2806               (gnus-group-next-unread-group 1))
2807           (gnus-handle-ephemeral-exit quit-config)))
2808       (gnus-message 3 "Can't select group")
2809       nil)
2810      ;; The user did a `C-g' while prompting for number of articles,
2811      ;; so we exit this group.
2812      ((eq did-select 'quit)
2813       (and (eq major-mode 'gnus-summary-mode)
2814            (not (equal (current-buffer) kill-buffer))
2815            (kill-buffer (current-buffer)))
2816       (when kill-buffer
2817         (gnus-kill-or-deaden-summary kill-buffer))
2818       (if (not quit-config)
2819           (progn
2820             (set-buffer gnus-group-buffer)
2821             (gnus-group-jump-to-group group)
2822             (gnus-group-next-unread-group 1)
2823             (gnus-configure-windows 'group 'force))
2824         (gnus-handle-ephemeral-exit quit-config))
2825       ;; Finally signal the quit.
2826       (signal 'quit nil))
2827      ;; The group was successfully selected.
2828      (t
2829       (gnus-set-global-variables)
2830       ;; Save the active value in effect when the group was entered.
2831       (setq gnus-newsgroup-active
2832             (gnus-copy-sequence
2833              (gnus-active gnus-newsgroup-name)))
2834       ;; You can change the summary buffer in some way with this hook.
2835       (gnus-run-hooks 'gnus-select-group-hook)
2836       ;; Set any local variables in the group parameters.
2837       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2838       (gnus-update-format-specifications
2839        nil 'summary 'summary-mode 'summary-dummy)
2840       (gnus-update-summary-mark-positions)
2841       ;; Do score processing.
2842       (when gnus-use-scoring
2843         (gnus-possibly-score-headers))
2844       ;; Check whether to fill in the gaps in the threads.
2845       (when gnus-build-sparse-threads
2846         (gnus-build-sparse-threads))
2847       ;; Find the initial limit.
2848       (if gnus-show-threads
2849           (if show-all
2850               (let ((gnus-newsgroup-dormant nil))
2851                 (gnus-summary-initial-limit show-all))
2852             (gnus-summary-initial-limit show-all))
2853         ;; When untreaded, all articles are always shown.
2854         (setq gnus-newsgroup-limit
2855               (mapcar
2856                (lambda (header) (mail-header-number header))
2857                gnus-newsgroup-headers)))
2858       ;; Generate the summary buffer.
2859       (unless no-display
2860         (gnus-summary-prepare))
2861       (when gnus-use-trees
2862         (gnus-tree-open group)
2863         (setq gnus-summary-highlight-line-function
2864               'gnus-tree-highlight-article))
2865       ;; If the summary buffer is empty, but there are some low-scored
2866       ;; articles or some excluded dormants, we include these in the
2867       ;; buffer.
2868       (when (and (zerop (buffer-size))
2869                  (not no-display))
2870         (cond (gnus-newsgroup-dormant
2871                (gnus-summary-limit-include-dormant))
2872               ((and gnus-newsgroup-scored show-all)
2873                (gnus-summary-limit-include-expunged t))))
2874       ;; Function `gnus-apply-kill-file' must be called in this hook.
2875       (gnus-run-hooks 'gnus-apply-kill-hook)
2876       (if (and (zerop (buffer-size))
2877                (not no-display))
2878           (progn
2879             ;; This newsgroup is empty.
2880             (gnus-summary-catchup-and-exit nil t)
2881             (gnus-message 6 "No unread news")
2882             (when kill-buffer
2883               (gnus-kill-or-deaden-summary kill-buffer))
2884             ;; Return nil from this function.
2885             nil)
2886         ;; Hide conversation thread subtrees.  We cannot do this in
2887         ;; gnus-summary-prepare-hook since kill processing may not
2888         ;; work with hidden articles.
2889         (and gnus-show-threads
2890              gnus-thread-hide-subtree
2891              (gnus-summary-hide-all-threads))
2892         (when kill-buffer
2893           (gnus-kill-or-deaden-summary kill-buffer))
2894         ;; Show first unread article if requested.
2895         (if (and (not no-article)
2896                  (not no-display)
2897                  gnus-newsgroup-unreads
2898                  gnus-auto-select-first)
2899             (progn
2900               (gnus-configure-windows 'summary)
2901               (cond
2902                ((eq gnus-auto-select-first 'best)
2903                 (gnus-summary-best-unread-article))
2904                ((eq gnus-auto-select-first t)
2905                 (gnus-summary-first-unread-article))
2906                ((gnus-functionp gnus-auto-select-first)
2907                 (funcall gnus-auto-select-first))))
2908           ;; Don't select any articles, just move point to the first
2909           ;; article in the group.
2910           (goto-char (point-min))
2911           (gnus-summary-position-point)
2912           (gnus-configure-windows 'summary 'force)
2913           (gnus-set-mode-line 'summary))
2914         (when (get-buffer-window gnus-group-buffer t)
2915           ;; Gotta use windows, because recenter does weird stuff if
2916           ;; the current buffer ain't the displayed window.
2917           (let ((owin (selected-window)))
2918             (select-window (get-buffer-window gnus-group-buffer t))
2919             (when (gnus-group-goto-group group)
2920               (recenter))
2921             (select-window owin)))
2922         ;; Mark this buffer as "prepared".
2923         (setq gnus-newsgroup-prepared t)
2924         (gnus-run-hooks 'gnus-summary-prepared-hook)
2925         t)))))
2926
2927 (defun gnus-summary-prepare ()
2928   "Generate the summary buffer."
2929   (interactive)
2930   (let ((buffer-read-only nil))
2931     (erase-buffer)
2932     (setq gnus-newsgroup-data nil
2933           gnus-newsgroup-data-reverse nil)
2934     (gnus-run-hooks 'gnus-summary-generate-hook)
2935     ;; Generate the buffer, either with threads or without.
2936     (when gnus-newsgroup-headers
2937       (gnus-summary-prepare-threads
2938        (if gnus-show-threads
2939            (gnus-sort-gathered-threads
2940             (funcall gnus-summary-thread-gathering-function
2941                      (gnus-sort-threads
2942                       (gnus-cut-threads (gnus-make-threads)))))
2943          ;; Unthreaded display.
2944          (gnus-sort-articles gnus-newsgroup-headers))))
2945     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2946     ;; Call hooks for modifying summary buffer.
2947     (goto-char (point-min))
2948     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2949
2950 (defsubst gnus-general-simplify-subject (subject)
2951   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2952   (setq subject
2953         (cond
2954          ;; Truncate the subject.
2955          (gnus-simplify-subject-functions
2956           (gnus-map-function gnus-simplify-subject-functions subject))
2957          ((numberp gnus-summary-gather-subject-limit)
2958           (setq subject (gnus-simplify-subject-re subject))
2959           (if (> (length subject) gnus-summary-gather-subject-limit)
2960               (substring subject 0 gnus-summary-gather-subject-limit)
2961             subject))
2962          ;; Fuzzily simplify it.
2963          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2964           (gnus-simplify-subject-fuzzy subject))
2965          ;; Just remove the leading "Re:".
2966          (t
2967           (gnus-simplify-subject-re subject))))
2968
2969   (if (and gnus-summary-gather-exclude-subject
2970            (string-match gnus-summary-gather-exclude-subject subject))
2971       nil                               ; This article shouldn't be gathered
2972     subject))
2973
2974 (defun gnus-summary-simplify-subject-query ()
2975   "Query where the respool algorithm would put this article."
2976   (interactive)
2977   (gnus-summary-select-article)
2978   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2979
2980 (defun gnus-gather-threads-by-subject (threads)
2981   "Gather threads by looking at Subject headers."
2982   (if (not gnus-summary-make-false-root)
2983       threads
2984     (let ((hashtb (gnus-make-hashtable 1024))
2985           (prev threads)
2986           (result threads)
2987           subject hthread whole-subject)
2988       (while threads
2989         (setq subject (gnus-general-simplify-subject
2990                        (setq whole-subject (mail-header-subject
2991                                             (caar threads)))))
2992         (when subject
2993           (if (setq hthread (gnus-gethash subject hashtb))
2994               (progn
2995                 ;; We enter a dummy root into the thread, if we
2996                 ;; haven't done that already.
2997                 (unless (stringp (caar hthread))
2998                   (setcar hthread (list whole-subject (car hthread))))
2999                 ;; We add this new gathered thread to this gathered
3000                 ;; thread.
3001                 (setcdr (car hthread)
3002                         (nconc (cdar hthread) (list (car threads))))
3003                 ;; Remove it from the list of threads.
3004                 (setcdr prev (cdr threads))
3005                 (setq threads prev))
3006             ;; Enter this thread into the hash table.
3007             (gnus-sethash subject threads hashtb)))
3008         (setq prev threads)
3009         (setq threads (cdr threads)))
3010       result)))
3011
3012 (defun gnus-gather-threads-by-references (threads)
3013   "Gather threads by looking at References headers."
3014   (let ((idhashtb (gnus-make-hashtable 1024))
3015         (thhashtb (gnus-make-hashtable 1024))
3016         (prev threads)
3017         (result threads)
3018         ids references id gthread gid entered ref)
3019     (while threads
3020       (when (setq references (mail-header-references (caar threads)))
3021         (setq id (mail-header-id (caar threads))
3022               ids (gnus-split-references references)
3023               entered nil)
3024         (while (setq ref (pop ids))
3025           (setq ids (delete ref ids))
3026           (if (not (setq gid (gnus-gethash ref idhashtb)))
3027               (progn
3028                 (gnus-sethash ref id idhashtb)
3029                 (gnus-sethash id threads thhashtb))
3030             (setq gthread (gnus-gethash gid thhashtb))
3031             (unless entered
3032               ;; We enter a dummy root into the thread, if we
3033               ;; haven't done that already.
3034               (unless (stringp (caar gthread))
3035                 (setcar gthread (list (mail-header-subject (caar gthread))
3036                                       (car gthread))))
3037               ;; We add this new gathered thread to this gathered
3038               ;; thread.
3039               (setcdr (car gthread)
3040                       (nconc (cdar gthread) (list (car threads)))))
3041             ;; Add it into the thread hash table.
3042             (gnus-sethash id gthread thhashtb)
3043             (setq entered t)
3044             ;; Remove it from the list of threads.
3045             (setcdr prev (cdr threads))
3046             (setq threads prev))))
3047       (setq prev threads)
3048       (setq threads (cdr threads)))
3049     result))
3050
3051 (defun gnus-sort-gathered-threads (threads)
3052   "Sort subtreads inside each gathered thread by article number."
3053   (let ((result threads))
3054     (while threads
3055       (when (stringp (caar threads))
3056         (setcdr (car threads)
3057                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3058       (setq threads (cdr threads)))
3059     result))
3060
3061 (defun gnus-thread-loop-p (root thread)
3062   "Say whether ROOT is in THREAD."
3063   (let ((stack (list thread))
3064         (infloop 0)
3065         th)
3066     (while (setq thread (pop stack))
3067       (setq th (cdr thread))
3068       (while (and th
3069                   (not (eq (caar th) root)))
3070         (pop th))
3071       (if th
3072           ;; We have found a loop.
3073           (let (ref-dep)
3074             (setcdr thread (delq (car th) (cdr thread)))
3075             (if (boundp (setq ref-dep (intern "none"
3076                                               gnus-newsgroup-dependencies)))
3077                 (setcdr (symbol-value ref-dep)
3078                         (nconc (cdr (symbol-value ref-dep))
3079                                (list (car th))))
3080               (set ref-dep (list nil (car th))))
3081             (setq infloop 1
3082                   stack nil))
3083         ;; Push all the subthreads onto the stack.
3084         (push (cdr thread) stack)))
3085     infloop))
3086
3087 (defun gnus-make-threads ()
3088   "Go through the dependency hashtb and find the roots.  Return all threads."
3089   (let (threads)
3090     (while (catch 'infloop
3091              (mapatoms
3092               (lambda (refs)
3093                 ;; Deal with self-referencing References loops.
3094                 (when (and (car (symbol-value refs))
3095                            (not (zerop
3096                                  (apply
3097                                   '+
3098                                   (mapcar
3099                                    (lambda (thread)
3100                                      (gnus-thread-loop-p
3101                                       (car (symbol-value refs)) thread))
3102                                    (cdr (symbol-value refs)))))))
3103                   (setq threads nil)
3104                   (throw 'infloop t))
3105                 (unless (car (symbol-value refs))
3106                   ;; These threads do not refer back to any other articles,
3107                   ;; so they're roots.
3108                   (setq threads (append (cdr (symbol-value refs)) threads))))
3109               gnus-newsgroup-dependencies)))
3110     threads))
3111
3112 ;; Build the thread tree.
3113 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3114   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3115
3116 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3117 if it was already present.
3118
3119 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3120 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3121 Message-IDs will be renamed be renamed to a unique Message-ID before
3122 being entered.
3123
3124 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3125   (let* ((id (mail-header-id header))
3126          (id-dep (and id (intern id dependencies)))
3127          ref ref-dep ref-header)
3128     ;; Enter this `header' in the `dependencies' table.
3129     (cond
3130      ((not id-dep)
3131       (setq header nil))
3132      ;; The first two cases do the normal part: enter a new `header'
3133      ;; in the `dependencies' table.
3134      ((not (boundp id-dep))
3135       (set id-dep (list header)))
3136      ((null (car (symbol-value id-dep)))
3137       (setcar (symbol-value id-dep) header))
3138
3139      ;; From here the `header' was already present in the
3140      ;; `dependencies' table.
3141      (force-new
3142       ;; Overrides an existing entry;
3143       ;; just set the header part of the entry.
3144       (setcar (symbol-value id-dep) header))
3145
3146      ;; Renames the existing `header' to a unique Message-ID.
3147      ((not gnus-summary-ignore-duplicates)
3148       ;; An article with this Message-ID has already been seen.
3149       ;; We rename the Message-ID.
3150       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3151            (list header))
3152       (mail-header-set-id header id))
3153
3154      ;; The last case ignores an existing entry, except it adds any
3155      ;; additional Xrefs (in case the two articles came from different
3156      ;; servers.
3157      ;; Also sets `header' to `nil' meaning that the `dependencies'
3158      ;; table was *not* modified.
3159      (t
3160       (mail-header-set-xref
3161        (car (symbol-value id-dep))
3162        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3163                    "")
3164                (or (mail-header-xref header) "")))
3165       (setq header nil)))
3166
3167     (when header
3168       ;; First check if that we are not creating a References loop.
3169       (setq ref (gnus-parent-id (mail-header-references header)))
3170       (while (and ref
3171                   (setq ref-dep (intern-soft ref dependencies))
3172                   (boundp ref-dep)
3173                   (setq ref-header (car (symbol-value ref-dep))))
3174         (if (string= id ref)
3175             ;; Yuk!  This is a reference loop.  Make the article be a
3176             ;; root article.
3177             (progn
3178               (mail-header-set-references (car (symbol-value id-dep)) "none")
3179               (setq ref nil))
3180           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3181       (setq ref (gnus-parent-id (mail-header-references header)))
3182       (setq ref-dep (intern (or ref "none") dependencies))
3183       (if (boundp ref-dep)
3184           (setcdr (symbol-value ref-dep)
3185                   (nconc (cdr (symbol-value ref-dep))
3186                          (list (symbol-value id-dep))))
3187         (set ref-dep (list nil (symbol-value id-dep)))))
3188     header))
3189
3190 (defun gnus-build-sparse-threads ()
3191   (let ((headers gnus-newsgroup-headers)
3192         (mail-parse-charset gnus-newsgroup-charset)
3193         (gnus-summary-ignore-duplicates t)
3194         header references generation relations
3195         subject child end new-child date)
3196     ;; First we create an alist of generations/relations, where
3197     ;; generations is how much we trust the relation, and the relation
3198     ;; is parent/child.
3199     (gnus-message 7 "Making sparse threads...")
3200     (save-excursion
3201       (nnheader-set-temp-buffer " *gnus sparse threads*")
3202       (while (setq header (pop headers))
3203         (when (and (setq references (mail-header-references header))
3204                    (not (string= references "")))
3205           (insert references)
3206           (setq child (mail-header-id header)
3207                 subject (mail-header-subject header)
3208                 date (mail-header-date header)
3209                 generation 0)
3210           (while (search-backward ">" nil t)
3211             (setq end (1+ (point)))
3212             (when (search-backward "<" nil t)
3213               (setq new-child (buffer-substring (point) end))
3214               (push (list (incf generation)
3215                           child (setq child new-child)
3216                           subject date)
3217                     relations)))
3218           (when child
3219             (push (list (1+ generation) child nil subject) relations))
3220           (erase-buffer)))
3221       (kill-buffer (current-buffer)))
3222     ;; Sort over trustworthiness.
3223     (mapcar
3224      (lambda (relation)
3225        (when (gnus-dependencies-add-header
3226               (make-full-mail-header
3227                gnus-reffed-article-number
3228                (nth 3 relation) "" (or (nth 4 relation) "")
3229                (nth 1 relation)
3230                (or (nth 2 relation) "") 0 0 "")
3231               gnus-newsgroup-dependencies nil)
3232          (push gnus-reffed-article-number gnus-newsgroup-limit)
3233          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3234          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3235                gnus-newsgroup-reads)
3236          (decf gnus-reffed-article-number)))
3237      (sort relations 'car-less-than-car))
3238     (gnus-message 7 "Making sparse threads...done")))
3239
3240 (defun gnus-build-old-threads ()
3241   ;; Look at all the articles that refer back to old articles, and
3242   ;; fetch the headers for the articles that aren't there.  This will
3243   ;; build complete threads - if the roots haven't been expired by the
3244   ;; server, that is.
3245   (let ((mail-parse-charset gnus-newsgroup-charset)
3246         id heads)
3247     (mapatoms
3248      (lambda (refs)
3249        (when (not (car (symbol-value refs)))
3250          (setq heads (cdr (symbol-value refs)))
3251          (while heads
3252            (if (memq (mail-header-number (caar heads))
3253                      gnus-newsgroup-dormant)
3254                (setq heads (cdr heads))
3255              (setq id (symbol-name refs))
3256              (while (and (setq id (gnus-build-get-header id))
3257                          (not (car (gnus-id-to-thread id)))))
3258              (setq heads nil)))))
3259      gnus-newsgroup-dependencies)))
3260
3261 ;; This function has to be called with point after the article number
3262 ;; on the beginning of the line.
3263 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3264   (let ((eol (gnus-point-at-eol))
3265         (buffer (current-buffer))
3266         header)
3267
3268     ;; overview: [num subject from date id refs chars lines misc]
3269     (unwind-protect
3270         (progn
3271           (narrow-to-region (point) eol)
3272           (unless (eobp)
3273             (forward-char))
3274
3275           (setq header
3276                 (make-full-mail-header
3277                  number                 ; number
3278                  (funcall gnus-decode-encoded-word-function
3279                           (nnheader-nov-field)) ; subject
3280                  (funcall gnus-decode-encoded-word-function
3281                           (nnheader-nov-field)) ; from
3282                  (nnheader-nov-field)   ; date
3283                  (nnheader-nov-read-message-id) ; id
3284                  (nnheader-nov-field)   ; refs
3285                  (nnheader-nov-read-integer) ; chars
3286                  (nnheader-nov-read-integer) ; lines
3287                  (unless (eobp)
3288                    (nnheader-nov-field)) ; misc
3289                  (nnheader-nov-parse-extra)))) ; extra
3290
3291       (widen))
3292
3293     (when gnus-alter-header-function
3294       (funcall gnus-alter-header-function header))
3295     (gnus-dependencies-add-header header dependencies force-new)))
3296
3297 (defun gnus-build-get-header (id)
3298   ;; Look through the buffer of NOV lines and find the header to
3299   ;; ID.  Enter this line into the dependencies hash table, and return
3300   ;; the id of the parent article (if any).
3301   (let ((deps gnus-newsgroup-dependencies)
3302         found header)
3303     (prog1
3304         (save-excursion
3305           (set-buffer nntp-server-buffer)
3306           (let ((case-fold-search nil))
3307             (goto-char (point-min))
3308             (while (and (not found)
3309                         (search-forward id nil t))
3310               (beginning-of-line)
3311               (setq found (looking-at
3312                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3313                                    (regexp-quote id))))
3314               (or found (beginning-of-line 2)))
3315             (when found
3316               (beginning-of-line)
3317               (and
3318                (setq header (gnus-nov-parse-line
3319                              (read (current-buffer)) deps))
3320                (gnus-parent-id (mail-header-references header))))))
3321       (when header
3322         (let ((number (mail-header-number header)))
3323           (push number gnus-newsgroup-limit)
3324           (push header gnus-newsgroup-headers)
3325           (if (memq number gnus-newsgroup-unselected)
3326               (progn
3327                 (push number gnus-newsgroup-unreads)
3328                 (setq gnus-newsgroup-unselected
3329                       (delq number gnus-newsgroup-unselected)))
3330             (push number gnus-newsgroup-ancient)))))))
3331
3332 (defun gnus-build-all-threads ()
3333   "Read all the headers."
3334   (let ((gnus-summary-ignore-duplicates t)
3335         (mail-parse-charset gnus-newsgroup-charset)
3336         (dependencies gnus-newsgroup-dependencies)
3337         header article)
3338     (save-excursion
3339       (set-buffer nntp-server-buffer)
3340       (let ((case-fold-search nil))
3341         (goto-char (point-min))
3342         (while (not (eobp))
3343           (ignore-errors
3344             (setq article (read (current-buffer))
3345                   header (gnus-nov-parse-line article dependencies)))
3346           (when header
3347             (save-excursion
3348               (set-buffer gnus-summary-buffer)
3349               (push header gnus-newsgroup-headers)
3350               (if (memq (setq article (mail-header-number header))
3351                         gnus-newsgroup-unselected)
3352                   (progn
3353                     (push article gnus-newsgroup-unreads)
3354                     (setq gnus-newsgroup-unselected
3355                           (delq article gnus-newsgroup-unselected)))
3356                 (push article gnus-newsgroup-ancient)))
3357             (forward-line 1)))))))
3358
3359 (defun gnus-summary-update-article-line (article header)
3360   "Update the line for ARTICLE using HEADERS."
3361   (let* ((id (mail-header-id header))
3362          (thread (gnus-id-to-thread id)))
3363     (unless thread
3364       (error "Article in no thread"))
3365     ;; Update the thread.
3366     (setcar thread header)
3367     (gnus-summary-goto-subject article)
3368     (let* ((datal (gnus-data-find-list article))
3369            (data (car datal))
3370            (length (when (cdr datal)
3371                      (- (gnus-data-pos data)
3372                         (gnus-data-pos (cadr datal)))))
3373            (buffer-read-only nil)
3374            (level (gnus-summary-thread-level)))
3375       (gnus-delete-line)
3376       (gnus-summary-insert-line
3377        header level nil (gnus-article-mark article)
3378        (memq article gnus-newsgroup-replied)
3379        (memq article gnus-newsgroup-expirable)
3380        ;; Only insert the Subject string when it's different
3381        ;; from the previous Subject string.
3382        (if (and
3383             gnus-show-threads
3384             (gnus-subject-equal
3385              (condition-case ()
3386                  (mail-header-subject
3387                   (gnus-data-header
3388                    (cadr
3389                     (gnus-data-find-list
3390                      article
3391                      (gnus-data-list t)))))
3392                ;; Error on the side of excessive subjects.
3393                (error ""))
3394              (mail-header-subject header)))
3395            ""
3396          (mail-header-subject header))
3397        nil (cdr (assq article gnus-newsgroup-scored))
3398        (memq article gnus-newsgroup-processable))
3399       (when length
3400         (gnus-data-update-list
3401          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3402
3403 (defun gnus-summary-update-article (article &optional iheader)
3404   "Update ARTICLE in the summary buffer."
3405   (set-buffer gnus-summary-buffer)
3406   (let* ((header (gnus-summary-article-header article))
3407          (id (mail-header-id header))
3408          (data (gnus-data-find article))
3409          (thread (gnus-id-to-thread id))
3410          (references (mail-header-references header))
3411          (parent
3412           (gnus-id-to-thread
3413            (or (gnus-parent-id
3414                 (when (and references
3415                            (not (equal "" references)))
3416                   references))
3417                "none")))
3418          (buffer-read-only nil)
3419          (old (car thread)))
3420     (when thread
3421       (unless iheader
3422         (setcar thread nil)
3423         (when parent
3424           (delq thread parent)))
3425       (if (gnus-summary-insert-subject id header)
3426           ;; Set the (possibly) new article number in the data structure.
3427           (gnus-data-set-number data (gnus-id-to-article id))
3428         (setcar thread old)
3429         nil))))
3430
3431 (defun gnus-rebuild-thread (id &optional line)
3432   "Rebuild the thread containing ID.
3433 If LINE, insert the rebuilt thread starting on line LINE."
3434   (let ((buffer-read-only nil)
3435         old-pos current thread data)
3436     (if (not gnus-show-threads)
3437         (setq thread (list (car (gnus-id-to-thread id))))
3438       ;; Get the thread this article is part of.
3439       (setq thread (gnus-remove-thread id)))
3440     (setq old-pos (gnus-point-at-bol))
3441     (setq current (save-excursion
3442                     (and (zerop (forward-line -1))
3443                          (gnus-summary-article-number))))
3444     ;; If this is a gathered thread, we have to go some re-gathering.
3445     (when (stringp (car thread))
3446       (let ((subject (car thread))
3447             roots thr)
3448         (setq thread (cdr thread))
3449         (while thread
3450           (unless (memq (setq thr (gnus-id-to-thread
3451                                    (gnus-root-id
3452                                     (mail-header-id (caar thread)))))
3453                         roots)
3454             (push thr roots))
3455           (setq thread (cdr thread)))
3456         ;; We now have all (unique) roots.
3457         (if (= (length roots) 1)
3458             ;; All the loose roots are now one solid root.
3459             (setq thread (car roots))
3460           (setq thread (cons subject (gnus-sort-threads roots))))))
3461     (let (threads)
3462       ;; We then insert this thread into the summary buffer.
3463       (when line
3464         (goto-char (point-min))
3465         (forward-line (1- line)))
3466       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3467         (if gnus-show-threads
3468             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3469           (gnus-summary-prepare-unthreaded thread))
3470         (setq data (nreverse gnus-newsgroup-data))
3471         (setq threads gnus-newsgroup-threads))
3472       ;; We splice the new data into the data structure.
3473       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3474       ;;!!! then we want to insert at the beginning of the buffer.
3475       ;;!!! That happens to be true with Gnus now, but that may
3476       ;;!!! change in the future.  Perhaps.
3477       (gnus-data-enter-list
3478        (if line nil current) data (- (point) old-pos))
3479       (setq gnus-newsgroup-threads
3480             (nconc threads gnus-newsgroup-threads))
3481       (gnus-data-compute-positions))))
3482
3483 (defun gnus-number-to-header (number)
3484   "Return the header for article NUMBER."
3485   (let ((headers gnus-newsgroup-headers))
3486     (while (and headers
3487                 (not (= number (mail-header-number (car headers)))))
3488       (pop headers))
3489     (when headers
3490       (car headers))))
3491
3492 (defun gnus-parent-headers (in-headers &optional generation)
3493   "Return the headers of the GENERATIONeth parent of HEADERS."
3494   (unless generation
3495     (setq generation 1))
3496   (let ((parent t)
3497         (headers in-headers)
3498         references)
3499     (while (and parent
3500                 (not (zerop generation))
3501                 (setq references (mail-header-references headers)))
3502       (setq headers (if (and references
3503                              (setq parent (gnus-parent-id references)))
3504                         (car (gnus-id-to-thread parent))
3505                       nil))
3506       (decf generation))
3507     (and (not (eq headers in-headers))
3508          headers)))
3509
3510 (defun gnus-id-to-thread (id)
3511   "Return the (sub-)thread where ID appears."
3512   (gnus-gethash id gnus-newsgroup-dependencies))
3513
3514 (defun gnus-id-to-article (id)
3515   "Return the article number of ID."
3516   (let ((thread (gnus-id-to-thread id)))
3517     (when (and thread
3518                (car thread))
3519       (mail-header-number (car thread)))))
3520
3521 (defun gnus-id-to-header (id)
3522   "Return the article headers of ID."
3523   (car (gnus-id-to-thread id)))
3524
3525 (defun gnus-article-displayed-root-p (article)
3526   "Say whether ARTICLE is a root(ish) article."
3527   (let ((level (gnus-summary-thread-level article))
3528         (refs (mail-header-references  (gnus-summary-article-header article)))
3529         particle)
3530     (cond
3531      ((null level) nil)
3532      ((zerop level) t)
3533      ((null refs) t)
3534      ((null (gnus-parent-id refs)) t)
3535      ((and (= 1 level)
3536            (null (setq particle (gnus-id-to-article
3537                                  (gnus-parent-id refs))))
3538            (null (gnus-summary-thread-level particle)))))))
3539
3540 (defun gnus-root-id (id)
3541   "Return the id of the root of the thread where ID appears."
3542   (let (last-id prev)
3543     (while (and id (setq prev (car (gnus-id-to-thread id))))
3544       (setq last-id id
3545             id (gnus-parent-id (mail-header-references prev))))
3546     last-id))
3547
3548 (defun gnus-articles-in-thread (thread)
3549   "Return the list of articles in THREAD."
3550   (cons (mail-header-number (car thread))
3551         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3552
3553 (defun gnus-remove-thread (id &optional dont-remove)
3554   "Remove the thread that has ID in it."
3555   (let (headers thread last-id)
3556     ;; First go up in this thread until we find the root.
3557     (setq last-id (gnus-root-id id)
3558           headers (message-flatten-list (gnus-id-to-thread last-id)))
3559     ;; We have now found the real root of this thread.  It might have
3560     ;; been gathered into some loose thread, so we have to search
3561     ;; through the threads to find the thread we wanted.
3562     (let ((threads gnus-newsgroup-threads)
3563           sub)
3564       (while threads
3565         (setq sub (car threads))
3566         (if (stringp (car sub))
3567             ;; This is a gathered thread, so we look at the roots
3568             ;; below it to find whether this article is in this
3569             ;; gathered root.
3570             (progn
3571               (setq sub (cdr sub))
3572               (while sub
3573                 (when (member (caar sub) headers)
3574                   (setq thread (car threads)
3575                         threads nil
3576                         sub nil))
3577                 (setq sub (cdr sub))))
3578           ;; It's an ordinary thread, so we check it.
3579           (when (eq (car sub) (car headers))
3580             (setq thread sub
3581                   threads nil)))
3582         (setq threads (cdr threads)))
3583       ;; If this article is in no thread, then it's a root.
3584       (if thread
3585           (unless dont-remove
3586             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3587         (setq thread (gnus-id-to-thread last-id)))
3588       (when thread
3589         (prog1
3590             thread                      ; We return this thread.
3591           (unless dont-remove
3592             (if (stringp (car thread))
3593                 (progn
3594                   ;; If we use dummy roots, then we have to remove the
3595                   ;; dummy root as well.
3596                   (when (eq gnus-summary-make-false-root 'dummy)
3597                     ;; We go to the dummy root by going to
3598                     ;; the first sub-"thread", and then one line up.
3599                     (gnus-summary-goto-article
3600                      (mail-header-number (caadr thread)))
3601                     (forward-line -1)
3602                     (gnus-delete-line)
3603                     (gnus-data-compute-positions))
3604                   (setq thread (cdr thread))
3605                   (while thread
3606                     (gnus-remove-thread-1 (car thread))
3607                     (setq thread (cdr thread))))
3608               (gnus-remove-thread-1 thread))))))))
3609
3610 (defun gnus-remove-thread-1 (thread)
3611   "Remove the thread THREAD recursively."
3612   (let ((number (mail-header-number (pop thread)))
3613         d)
3614     (setq thread (reverse thread))
3615     (while thread
3616       (gnus-remove-thread-1 (pop thread)))
3617     (when (setq d (gnus-data-find number))
3618       (goto-char (gnus-data-pos d))
3619       (gnus-summary-show-thread)
3620       (gnus-data-remove
3621        number
3622        (- (gnus-point-at-bol)
3623           (prog1
3624               (1+ (gnus-point-at-eol))
3625             (gnus-delete-line)))))))
3626
3627 (defun gnus-sort-threads (threads)
3628   "Sort THREADS."
3629   (if (not gnus-thread-sort-functions)
3630       threads
3631     (gnus-message 8 "Sorting threads...")
3632     (prog1
3633         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3634       (gnus-message 8 "Sorting threads...done"))))
3635
3636 (defun gnus-sort-articles (articles)
3637   "Sort ARTICLES."
3638   (when gnus-article-sort-functions
3639     (gnus-message 7 "Sorting articles...")
3640     (prog1
3641         (setq gnus-newsgroup-headers
3642               (sort articles (gnus-make-sort-function
3643                               gnus-article-sort-functions)))
3644       (gnus-message 7 "Sorting articles...done"))))
3645
3646 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3647 (defmacro gnus-thread-header (thread)
3648   ;; Return header of first article in THREAD.
3649   ;; Note that THREAD must never, ever be anything else than a variable -
3650   ;; using some other form will lead to serious barfage.
3651   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3652   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3653   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" 
3654         (vector thread) 2))
3655
3656 (defsubst gnus-article-sort-by-number (h1 h2)
3657   "Sort articles by article number."
3658   (< (mail-header-number h1)
3659      (mail-header-number h2)))
3660
3661 (defun gnus-thread-sort-by-number (h1 h2)
3662   "Sort threads by root article number."
3663   (gnus-article-sort-by-number
3664    (gnus-thread-header h1) (gnus-thread-header h2)))
3665
3666 (defsubst gnus-article-sort-by-lines (h1 h2)
3667   "Sort articles by article Lines header."
3668   (< (mail-header-lines h1)
3669      (mail-header-lines h2)))
3670
3671 (defun gnus-thread-sort-by-lines (h1 h2)
3672   "Sort threads by root article Lines header."
3673   (gnus-article-sort-by-lines
3674    (gnus-thread-header h1) (gnus-thread-header h2)))
3675
3676 (defsubst gnus-article-sort-by-chars (h1 h2)
3677   "Sort articles by octet length."
3678   (< (mail-header-chars h1)
3679      (mail-header-chars h2)))
3680
3681 (defun gnus-thread-sort-by-chars (h1 h2)
3682   "Sort threads by root article octet length."
3683   (gnus-article-sort-by-chars
3684    (gnus-thread-header h1) (gnus-thread-header h2)))
3685
3686 (defsubst gnus-article-sort-by-author (h1 h2)
3687   "Sort articles by root author."
3688   (string-lessp
3689    (let ((extract (funcall
3690                    gnus-extract-address-components
3691                    (mail-header-from h1))))
3692      (or (car extract) (cadr extract) ""))
3693    (let ((extract (funcall
3694                    gnus-extract-address-components
3695                    (mail-header-from h2))))
3696      (or (car extract) (cadr extract) ""))))
3697
3698 (defun gnus-thread-sort-by-author (h1 h2)
3699   "Sort threads by root author."
3700   (gnus-article-sort-by-author
3701    (gnus-thread-header h1)  (gnus-thread-header h2)))
3702
3703 (defsubst gnus-article-sort-by-subject (h1 h2)
3704   "Sort articles by root subject."
3705   (string-lessp
3706    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3707    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3708
3709 (defun gnus-thread-sort-by-subject (h1 h2)
3710   "Sort threads by root subject."
3711   (gnus-article-sort-by-subject
3712    (gnus-thread-header h1) (gnus-thread-header h2)))
3713
3714 (defsubst gnus-article-sort-by-date (h1 h2)
3715   "Sort articles by root article date."
3716   (time-less-p
3717    (gnus-date-get-time (mail-header-date h1))
3718    (gnus-date-get-time (mail-header-date h2))))
3719
3720 (defun gnus-thread-sort-by-date (h1 h2)
3721   "Sort threads by root article date."
3722   (gnus-article-sort-by-date
3723    (gnus-thread-header h1) (gnus-thread-header h2)))
3724
3725 (defsubst gnus-article-sort-by-score (h1 h2)
3726   "Sort articles by root article score.
3727 Unscored articles will be counted as having a score of zero."
3728   (> (or (cdr (assq (mail-header-number h1)
3729                     gnus-newsgroup-scored))
3730          gnus-summary-default-score 0)
3731      (or (cdr (assq (mail-header-number h2)
3732                     gnus-newsgroup-scored))
3733          gnus-summary-default-score 0)))
3734
3735 (defun gnus-thread-sort-by-score (h1 h2)
3736   "Sort threads by root article score."
3737   (gnus-article-sort-by-score
3738    (gnus-thread-header h1) (gnus-thread-header h2)))
3739
3740 (defun gnus-thread-sort-by-total-score (h1 h2)
3741   "Sort threads by the sum of all scores in the thread.
3742 Unscored articles will be counted as having a score of zero."
3743   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3744
3745 (defun gnus-thread-total-score (thread)
3746   ;; This function find the total score of THREAD.
3747   (cond ((null thread)
3748          0)
3749         ((consp thread)
3750          (if (stringp (car thread))
3751              (apply gnus-thread-score-function 0
3752                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3753            (gnus-thread-total-score-1 thread)))
3754         (t
3755          (gnus-thread-total-score-1 (list thread)))))
3756
3757 (defun gnus-thread-total-score-1 (root)
3758   ;; This function find the total score of the thread below ROOT.
3759   (setq root (car root))
3760   (apply gnus-thread-score-function
3761          (or (append
3762               (mapcar 'gnus-thread-total-score
3763                       (cdr (gnus-id-to-thread (mail-header-id root))))
3764               (when (> (mail-header-number root) 0)
3765                 (list (or (cdr (assq (mail-header-number root)
3766                                      gnus-newsgroup-scored))
3767                           gnus-summary-default-score 0))))
3768              (list gnus-summary-default-score)
3769              '(0))))
3770
3771 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3772 (defvar gnus-tmp-prev-subject nil)
3773 (defvar gnus-tmp-false-parent nil)
3774 (defvar gnus-tmp-root-expunged nil)
3775 (defvar gnus-tmp-dummy-line nil)
3776
3777 (defvar gnus-tmp-header)
3778 (defun gnus-extra-header (type &optional header)
3779   "Return the extra header of TYPE."
3780   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3781       ""))
3782
3783 (defun gnus-summary-prepare-threads (threads)
3784   "Prepare summary buffer from THREADS and indentation LEVEL.
3785 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3786 or a straight list of headers."
3787   (gnus-message 7 "Generating summary...")
3788
3789   (setq gnus-newsgroup-threads threads)
3790   (beginning-of-line)
3791
3792   (let ((gnus-tmp-level 0)
3793         (default-score (or gnus-summary-default-score 0))
3794         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3795         thread number subject stack state gnus-tmp-gathered beg-match
3796         new-roots gnus-tmp-new-adopts thread-end
3797         gnus-tmp-header gnus-tmp-unread
3798         gnus-tmp-replied gnus-tmp-subject-or-nil
3799         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3800         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3801         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3802
3803     (setq gnus-tmp-prev-subject nil)
3804
3805     (if (vectorp (car threads))
3806         ;; If this is a straight (sic) list of headers, then a
3807         ;; threaded summary display isn't required, so we just create
3808         ;; an unthreaded one.
3809         (gnus-summary-prepare-unthreaded threads)
3810
3811       ;; Do the threaded display.
3812
3813       (while (or threads stack gnus-tmp-new-adopts new-roots)
3814
3815         (if (and (= gnus-tmp-level 0)
3816                  (or (not stack)
3817                      (= (caar stack) 0))
3818                  (not gnus-tmp-false-parent)
3819                  (or gnus-tmp-new-adopts new-roots))
3820             (if gnus-tmp-new-adopts
3821                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3822                       thread (list (car gnus-tmp-new-adopts))
3823                       gnus-tmp-header (caar thread)
3824                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3825               (when new-roots
3826                 (setq thread (list (car new-roots))
3827                       gnus-tmp-header (caar thread)
3828                       new-roots (cdr new-roots))))
3829
3830           (if threads
3831               ;; If there are some threads, we do them before the
3832               ;; threads on the stack.
3833               (setq thread threads
3834                     gnus-tmp-header (caar thread))
3835             ;; There were no current threads, so we pop something off
3836             ;; the stack.
3837             (setq state (car stack)
3838                   gnus-tmp-level (car state)
3839                   thread (cdr state)
3840                   stack (cdr stack)
3841                   gnus-tmp-header (caar thread))))
3842
3843         (setq gnus-tmp-false-parent nil)
3844         (setq gnus-tmp-root-expunged nil)
3845         (setq thread-end nil)
3846
3847         (if (stringp gnus-tmp-header)
3848             ;; The header is a dummy root.
3849             (cond
3850              ((eq gnus-summary-make-false-root 'adopt)
3851               ;; We let the first article adopt the rest.
3852               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3853                                                (cddar thread)))
3854               (setq gnus-tmp-gathered
3855                     (nconc (mapcar
3856                             (lambda (h) (mail-header-number (car h)))
3857                             (cddar thread))
3858                            gnus-tmp-gathered))
3859               (setq thread (cons (list (caar thread)
3860                                        (cadar thread))
3861                                  (cdr thread)))
3862               (setq gnus-tmp-level -1
3863                     gnus-tmp-false-parent t))
3864              ((eq gnus-summary-make-false-root 'empty)
3865               ;; We print adopted articles with empty subject fields.
3866               (setq gnus-tmp-gathered
3867                     (nconc (mapcar
3868                             (lambda (h) (mail-header-number (car h)))
3869                             (cddar thread))
3870                            gnus-tmp-gathered))
3871               (setq gnus-tmp-level -1))
3872              ((eq gnus-summary-make-false-root 'dummy)
3873               ;; We remember that we probably want to output a dummy
3874               ;; root.
3875               (setq gnus-tmp-dummy-line gnus-tmp-header)
3876               (setq gnus-tmp-prev-subject gnus-tmp-header))
3877              (t
3878               ;; We do not make a root for the gathered
3879               ;; sub-threads at all.
3880               (setq gnus-tmp-level -1)))
3881
3882           (setq number (mail-header-number gnus-tmp-header)
3883                 subject (mail-header-subject gnus-tmp-header))
3884
3885           (cond
3886            ;; If the thread has changed subject, we might want to make
3887            ;; this subthread into a root.
3888            ((and (null gnus-thread-ignore-subject)
3889                  (not (zerop gnus-tmp-level))
3890                  gnus-tmp-prev-subject
3891                  (not (inline
3892                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3893             (setq new-roots (nconc new-roots (list (car thread)))
3894                   thread-end t
3895                   gnus-tmp-header nil))
3896            ;; If the article lies outside the current limit,
3897            ;; then we do not display it.
3898            ((not (memq number gnus-newsgroup-limit))
3899             (setq gnus-tmp-gathered
3900                   (nconc (mapcar
3901                           (lambda (h) (mail-header-number (car h)))
3902                           (cdar thread))
3903                          gnus-tmp-gathered))
3904             (setq gnus-tmp-new-adopts (if (cdar thread)
3905                                           (append gnus-tmp-new-adopts
3906                                                   (cdar thread))
3907                                         gnus-tmp-new-adopts)
3908                   thread-end t
3909                   gnus-tmp-header nil)
3910             (when (zerop gnus-tmp-level)
3911               (setq gnus-tmp-root-expunged t)))
3912            ;; Perhaps this article is to be marked as read?
3913            ((and gnus-summary-mark-below
3914                  (< (or (cdr (assq number gnus-newsgroup-scored))
3915                         default-score)
3916                     gnus-summary-mark-below)
3917                  ;; Don't touch sparse articles.
3918                  (not (gnus-summary-article-sparse-p number))
3919                  (not (gnus-summary-article-ancient-p number)))
3920             (setq gnus-newsgroup-unreads
3921                   (delq number gnus-newsgroup-unreads))
3922             (if gnus-newsgroup-auto-expire
3923                 (push number gnus-newsgroup-expirable)
3924               (push (cons number gnus-low-score-mark)
3925                     gnus-newsgroup-reads))))
3926
3927           (when gnus-tmp-header
3928             ;; We may have an old dummy line to output before this
3929             ;; article.
3930             (when (and gnus-tmp-dummy-line
3931                        (gnus-subject-equal
3932                         gnus-tmp-dummy-line
3933                         (mail-header-subject gnus-tmp-header)))
3934               (gnus-summary-insert-dummy-line
3935                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3936               (setq gnus-tmp-dummy-line nil))
3937
3938             ;; Compute the mark.
3939             (setq gnus-tmp-unread (gnus-article-mark number))
3940
3941             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3942                                   gnus-tmp-header gnus-tmp-level)
3943                   gnus-newsgroup-data)
3944
3945             ;; Actually insert the line.
3946             (setq
3947              gnus-tmp-subject-or-nil
3948              (cond
3949               ((and gnus-thread-ignore-subject
3950                     gnus-tmp-prev-subject
3951                     (not (inline (gnus-subject-equal
3952                                   gnus-tmp-prev-subject subject))))
3953                subject)
3954               ((zerop gnus-tmp-level)
3955                (if (and (eq gnus-summary-make-false-root 'empty)
3956                         (memq number gnus-tmp-gathered)
3957                         gnus-tmp-prev-subject
3958                         (inline (gnus-subject-equal
3959                                  gnus-tmp-prev-subject subject)))
3960                    gnus-summary-same-subject
3961                  subject))
3962               (t gnus-summary-same-subject)))
3963             (if (and (eq gnus-summary-make-false-root 'adopt)
3964                      (= gnus-tmp-level 1)
3965                      (memq number gnus-tmp-gathered))
3966                 (setq gnus-tmp-opening-bracket ?\<
3967                       gnus-tmp-closing-bracket ?\>)
3968               (setq gnus-tmp-opening-bracket ?\[
3969                     gnus-tmp-closing-bracket ?\]))
3970             (setq
3971              gnus-tmp-indentation
3972              (aref gnus-thread-indent-array gnus-tmp-level)
3973              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3974              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3975                                 gnus-summary-default-score 0)
3976              gnus-tmp-score-char
3977              (if (or (null gnus-summary-default-score)
3978                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3979                          gnus-summary-zcore-fuzz))
3980                  ?  ;Whitespace
3981                (if (< gnus-tmp-score gnus-summary-default-score)
3982                    gnus-score-below-mark gnus-score-over-mark))
3983              gnus-tmp-replied
3984              (cond ((memq number gnus-newsgroup-processable)
3985                     gnus-process-mark)
3986                    ((memq number gnus-newsgroup-cached)
3987                     gnus-cached-mark)
3988                    ((memq number gnus-newsgroup-replied)
3989                     gnus-replied-mark)
3990                    ((memq number gnus-newsgroup-saved)
3991                     gnus-saved-mark)
3992                    (t gnus-unread-mark))
3993              gnus-tmp-from (mail-header-from gnus-tmp-header)
3994              gnus-tmp-name
3995              (cond
3996               ((string-match "<[^>]+> *$" gnus-tmp-from)
3997                (setq beg-match (match-beginning 0))
3998                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3999                         (substring gnus-tmp-from (1+ (match-beginning 0))
4000                                    (1- (match-end 0))))
4001                    (substring gnus-tmp-from 0 beg-match)))
4002               ((string-match "(.+)" gnus-tmp-from)
4003                (substring gnus-tmp-from
4004                           (1+ (match-beginning 0)) (1- (match-end 0))))
4005               (t gnus-tmp-from)))
4006             (when (string= gnus-tmp-name "")
4007               (setq gnus-tmp-name gnus-tmp-from))
4008             (unless (numberp gnus-tmp-lines)
4009               (setq gnus-tmp-lines 0))
4010             (gnus-put-text-property
4011              (point)
4012              (progn (eval gnus-summary-line-format-spec) (point))
4013              'gnus-number number)
4014             (when gnus-visual-p
4015               (forward-line -1)
4016               (gnus-run-hooks 'gnus-summary-update-hook)
4017               (forward-line 1))
4018
4019             (setq gnus-tmp-prev-subject subject)))
4020
4021         (when (nth 1 thread)
4022           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4023         (incf gnus-tmp-level)
4024         (setq threads (if thread-end nil (cdar thread)))
4025         (unless threads
4026           (setq gnus-tmp-level 0)))))
4027   (gnus-message 7 "Generating summary...done"))
4028
4029 (defun gnus-summary-prepare-unthreaded (headers)
4030   "Generate an unthreaded summary buffer based on HEADERS."
4031   (let (header number mark)
4032
4033     (beginning-of-line)
4034
4035     (while headers
4036       ;; We may have to root out some bad articles...
4037       (when (memq (setq number (mail-header-number
4038                                 (setq header (pop headers))))
4039                   gnus-newsgroup-limit)
4040         ;; Mark article as read when it has a low score.
4041         (when (and gnus-summary-mark-below
4042                    (< (or (cdr (assq number gnus-newsgroup-scored))
4043                           gnus-summary-default-score 0)
4044                       gnus-summary-mark-below)
4045                    (not (gnus-summary-article-ancient-p number)))
4046           (setq gnus-newsgroup-unreads
4047                 (delq number gnus-newsgroup-unreads))
4048           (if gnus-newsgroup-auto-expire
4049               (push number gnus-newsgroup-expirable)
4050             (push (cons number gnus-low-score-mark)
4051                   gnus-newsgroup-reads)))
4052
4053         (setq mark (gnus-article-mark number))
4054         (push (gnus-data-make number mark (1+ (point)) header 0)
4055               gnus-newsgroup-data)
4056         (gnus-summary-insert-line
4057          header 0 number
4058          mark (memq number gnus-newsgroup-replied)
4059          (memq number gnus-newsgroup-expirable)
4060          (mail-header-subject header) nil
4061          (cdr (assq number gnus-newsgroup-scored))
4062          (memq number gnus-newsgroup-processable))))))
4063
4064 (defun gnus-summary-remove-list-identifiers ()
4065   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4066   (let ((regexp (if (stringp gnus-list-identifiers)
4067                     gnus-list-identifiers
4068                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4069     (dolist (header gnus-newsgroup-headers)
4070       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4071                           (mail-header-subject header))
4072         (mail-header-set-subject
4073          header (concat (substring (mail-header-subject header)
4074                                    0 (match-beginning 2))
4075                         (substring (mail-header-subject header)
4076                                    (match-end 2))))))))
4077
4078 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4079   "Select newsgroup GROUP.
4080 If READ-ALL is non-nil, all articles in the group are selected.
4081 If SELECT-ARTICLES, only select those articles from GROUP."
4082   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4083          ;;!!! Dirty hack; should be removed.
4084          (gnus-summary-ignore-duplicates
4085           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4086               t
4087             gnus-summary-ignore-duplicates))
4088          (info (nth 2 entry))
4089          articles fetched-articles cached)
4090
4091     (unless (gnus-check-server
4092              (setq gnus-current-select-method
4093                    (gnus-find-method-for-group group)))
4094       (error "Couldn't open server"))
4095
4096     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4097         (gnus-activate-group group)     ; Or we can activate it...
4098         (progn                          ; Or we bug out.
4099           (when (equal major-mode 'gnus-summary-mode)
4100             (kill-buffer (current-buffer)))
4101           (error "Couldn't request group %s: %s"
4102                  group (gnus-status-message group))))
4103
4104     (unless (gnus-request-group group t)
4105       (when (equal major-mode 'gnus-summary-mode)
4106         (kill-buffer (current-buffer)))
4107       (error "Couldn't request group %s: %s"
4108              group (gnus-status-message group)))
4109
4110     (setq gnus-newsgroup-name group)
4111     (setq gnus-newsgroup-unselected nil)
4112     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4113     (gnus-summary-setup-default-charset)
4114
4115     ;; Adjust and set lists of article marks.
4116     (when info
4117       (gnus-adjust-marked-articles info))
4118
4119     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4120     (when (gnus-virtual-group-p group)
4121       (setq cached gnus-newsgroup-cached))
4122
4123     (setq gnus-newsgroup-unreads
4124           (gnus-set-difference
4125            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4126            gnus-newsgroup-dormant))
4127
4128     (setq gnus-newsgroup-processable nil)
4129
4130     (gnus-update-read-articles group gnus-newsgroup-unreads)
4131
4132     (if (setq articles select-articles)
4133         (setq gnus-newsgroup-unselected
4134               (gnus-sorted-intersection
4135                gnus-newsgroup-unreads
4136                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4137       (setq articles (gnus-articles-to-read group read-all)))
4138
4139     (cond
4140      ((null articles)
4141       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4142       'quit)
4143      ((eq articles 0) nil)
4144      (t
4145       ;; Init the dependencies hash table.
4146       (setq gnus-newsgroup-dependencies
4147             (gnus-make-hashtable (length articles)))
4148       (gnus-set-global-variables)
4149       ;; Retrieve the headers and read them in.
4150       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4151       (setq gnus-newsgroup-headers
4152             (if (eq 'nov
4153                     (setq gnus-headers-retrieved-by
4154                           (gnus-retrieve-headers
4155                            articles gnus-newsgroup-name
4156                            ;; We might want to fetch old headers, but
4157                            ;; not if there is only 1 article.
4158                            (and (or (and
4159                                      (not (eq gnus-fetch-old-headers 'some))
4160                                      (not (numberp gnus-fetch-old-headers)))
4161                                     (> (length articles) 1))
4162                                 gnus-fetch-old-headers))))
4163                 (gnus-get-newsgroup-headers-xover
4164                  articles nil nil gnus-newsgroup-name t)
4165               (gnus-get-newsgroup-headers)))
4166       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4167
4168       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4169       (when cached
4170         (setq gnus-newsgroup-cached cached))
4171
4172       ;; Suppress duplicates?
4173       (when gnus-suppress-duplicates
4174         (gnus-dup-suppress-articles))
4175
4176       ;; Set the initial limit.
4177       (setq gnus-newsgroup-limit (copy-sequence articles))
4178       ;; Remove canceled articles from the list of unread articles.
4179       (setq gnus-newsgroup-unreads
4180             (gnus-set-sorted-intersection
4181              gnus-newsgroup-unreads
4182              (setq fetched-articles
4183                    (mapcar (lambda (headers) (mail-header-number headers))
4184                            gnus-newsgroup-headers))))
4185       ;; Removed marked articles that do not exist.
4186       (gnus-update-missing-marks
4187        (gnus-sorted-complement fetched-articles articles))
4188       ;; We might want to build some more threads first.
4189       (when (and gnus-fetch-old-headers
4190                  (eq gnus-headers-retrieved-by 'nov))
4191         (if (eq gnus-fetch-old-headers 'invisible)
4192             (gnus-build-all-threads)
4193           (gnus-build-old-threads)))
4194       ;; Let the Gnus agent mark articles as read.
4195       (when gnus-agent
4196         (gnus-agent-get-undownloaded-list))
4197       ;; Remove list identifiers from subject
4198       (when gnus-list-identifiers
4199         (gnus-summary-remove-list-identifiers))
4200       ;; Check whether auto-expire is to be done in this group.
4201       (setq gnus-newsgroup-auto-expire
4202             (gnus-group-auto-expirable-p group))
4203       ;; Set up the article buffer now, if necessary.
4204       (unless gnus-single-article-buffer
4205         (gnus-article-setup-buffer))
4206       ;; First and last article in this newsgroup.
4207       (when gnus-newsgroup-headers
4208         (setq gnus-newsgroup-begin
4209               (mail-header-number (car gnus-newsgroup-headers))
4210               gnus-newsgroup-end
4211               (mail-header-number
4212                (gnus-last-element gnus-newsgroup-headers))))
4213       ;; GROUP is successfully selected.
4214       (or gnus-newsgroup-headers t)))))
4215
4216 (defun gnus-articles-to-read (group &optional read-all)
4217   ;; Find out what articles the user wants to read.
4218   (let* ((articles
4219           ;; Select all articles if `read-all' is non-nil, or if there
4220           ;; are no unread articles.
4221           (if (or read-all
4222                   (and (zerop (length gnus-newsgroup-marked))
4223                        (zerop (length gnus-newsgroup-unreads)))
4224                   (eq (gnus-group-find-parameter group 'display)
4225                       'all))
4226               (or
4227                (gnus-uncompress-range (gnus-active group))
4228                (gnus-cache-articles-in-group group))
4229             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4230                           (copy-sequence gnus-newsgroup-unreads))
4231                   '<)))
4232          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4233          (scored (length scored-list))
4234          (number (length articles))
4235          (marked (+ (length gnus-newsgroup-marked)
4236                     (length gnus-newsgroup-dormant)))
4237          (select
4238           (cond
4239            ((numberp read-all)
4240             read-all)
4241            (t
4242             (condition-case ()
4243                 (cond
4244                  ((and (or (<= scored marked) (= scored number))
4245                        (numberp gnus-large-newsgroup)
4246                        (> number gnus-large-newsgroup))
4247                   (let ((input
4248                          (read-string
4249                           (format
4250                            "How many articles from %s (default %d): "
4251                            (gnus-limit-string gnus-newsgroup-name 35)
4252                            number))))
4253                     (if (string-match "^[ \t]*$" input) number input)))
4254                  ((and (> scored marked) (< scored number)
4255                        (> (- scored number) 20))
4256                   (let ((input
4257                          (read-string
4258                           (format "%s %s (%d scored, %d total): "
4259                                   "How many articles from"
4260                                   group scored number))))
4261                     (if (string-match "^[ \t]*$" input)
4262                         number input)))
4263                  (t number))
4264               (quit nil))))))
4265     (setq select (if (stringp select) (string-to-number select) select))
4266     (if (or (null select) (zerop select))
4267         select
4268       (if (and (not (zerop scored)) (<= (abs select) scored))
4269           (progn
4270             (setq articles (sort scored-list '<))
4271             (setq number (length articles)))
4272         (setq articles (copy-sequence articles)))
4273
4274       (when (< (abs select) number)
4275         (if (< select 0)
4276             ;; Select the N oldest articles.
4277             (setcdr (nthcdr (1- (abs select)) articles) nil)
4278           ;; Select the N most recent articles.
4279           (setq articles (nthcdr (- number select) articles))))
4280       (setq gnus-newsgroup-unselected
4281             (gnus-sorted-intersection
4282              gnus-newsgroup-unreads
4283              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4284       (when gnus-alter-articles-to-read-function
4285         (setq gnus-newsgroup-unreads
4286               (sort 
4287                (funcall gnus-alter-articles-to-read-function
4288                         gnus-newsgroup-name gnus-newsgroup-unreads)
4289                '<)))
4290       articles)))
4291
4292 (defun gnus-killed-articles (killed articles)
4293   (let (out)
4294     (while articles
4295       (when (inline (gnus-member-of-range (car articles) killed))
4296         (push (car articles) out))
4297       (setq articles (cdr articles)))
4298     out))
4299
4300 (defun gnus-uncompress-marks (marks)
4301   "Uncompress the mark ranges in MARKS."
4302   (let ((uncompressed '(score bookmark))
4303         out)
4304     (while marks
4305       (if (memq (caar marks) uncompressed)
4306           (push (car marks) out)
4307         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4308       (setq marks (cdr marks)))
4309     out))
4310
4311 (defun gnus-adjust-marked-articles (info)
4312   "Set all article lists and remove all marks that are no longer valid."
4313   (let* ((marked-lists (gnus-info-marks info))
4314          (active (gnus-active (gnus-info-group info)))
4315          (min (car active))
4316          (max (cdr active))
4317          (types gnus-article-mark-lists)
4318          (uncompressed '(score bookmark killed))
4319          marks var articles article mark)
4320
4321     (while marked-lists
4322       (setq marks (pop marked-lists))
4323       (set (setq var (intern (format "gnus-newsgroup-%s"
4324                                      (car (rassq (setq mark (car marks))
4325                                                  types)))))
4326            (if (memq (car marks) uncompressed) (cdr marks)
4327              (gnus-uncompress-range (cdr marks))))
4328
4329       (setq articles (symbol-value var))
4330
4331       ;; All articles have to be subsets of the active articles.
4332       (cond
4333        ;; Adjust "simple" lists.
4334        ((memq mark '(tick dormant expire reply save))
4335         (while articles
4336           (when (or (< (setq article (pop articles)) min) (> article max))
4337             (set var (delq article (symbol-value var))))))
4338        ;; Adjust assocs.
4339        ((memq mark uncompressed)
4340         (when (not (listp (cdr (symbol-value var))))
4341           (set var (list (symbol-value var))))
4342         (when (not (listp (cdr articles)))
4343           (setq articles (list articles)))
4344         (while articles
4345           (when (or (not (consp (setq article (pop articles))))
4346                     (< (car article) min)
4347                     (> (car article) max))
4348             (set var (delq article (symbol-value var))))))))))
4349
4350 (defun gnus-update-missing-marks (missing)
4351   "Go through the list of MISSING articles and remove them from the mark lists."
4352   (when missing
4353     (let ((types gnus-article-mark-lists)
4354           var m)
4355       ;; Go through all types.
4356       (while types
4357         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4358         (when (symbol-value var)
4359           ;; This list has articles.  So we delete all missing articles
4360           ;; from it.
4361           (setq m missing)
4362           (while m
4363             (set var (delq (pop m) (symbol-value var)))))))))
4364
4365 (defun gnus-update-marks ()
4366   "Enter the various lists of marked articles into the newsgroup info list."
4367   (let ((types gnus-article-mark-lists)
4368         (info (gnus-get-info gnus-newsgroup-name))
4369         (uncompressed '(score bookmark killed))
4370         type list newmarked symbol delta-marks)
4371     (when info
4372       ;; Add all marks lists to the list of marks lists.
4373       (while (setq type (pop types))
4374         (setq list (symbol-value
4375                     (setq symbol
4376                           (intern (format "gnus-newsgroup-%s"
4377                                           (car type))))))
4378
4379         (when list
4380           ;; Get rid of the entries of the articles that have the
4381           ;; default score.
4382           (when (and (eq (cdr type) 'score)
4383                      gnus-save-score
4384                      list)
4385             (let* ((arts list)
4386                    (prev (cons nil list))
4387                    (all prev))
4388               (while arts
4389                 (if (or (not (consp (car arts)))
4390                         (= (cdar arts) gnus-summary-default-score))
4391                     (setcdr prev (cdr arts))
4392                   (setq prev arts))
4393                 (setq arts (cdr arts)))
4394               (setq list (cdr all)))))
4395
4396         (unless (memq (cdr type) uncompressed)
4397           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4398        
4399         (when (gnus-check-backend-function
4400                'request-set-mark gnus-newsgroup-name)
4401           ;; uncompressed:s are not proper flags (they are cons cells)
4402           ;; cache is a internal gnus flag
4403           (unless (memq (cdr type) (cons 'cache uncompressed))
4404             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4405                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4406                    (add (gnus-remove-from-range
4407                          (gnus-copy-sequence list) old)))
4408               (when add
4409                 (push (list add 'add (list (cdr type))) delta-marks))
4410               (when del
4411                 (push (list del 'del (list (cdr type))) delta-marks)))))
4412           
4413         (when list
4414           (push (cons (cdr type) list) newmarked)))
4415
4416       (when delta-marks
4417         (unless (gnus-check-group gnus-newsgroup-name)
4418           (error "Can't open server for %s" gnus-newsgroup-name))
4419         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4420           
4421       ;; Enter these new marks into the info of the group.
4422       (if (nthcdr 3 info)
4423           (setcar (nthcdr 3 info) newmarked)
4424         ;; Add the marks lists to the end of the info.
4425         (when newmarked
4426           (setcdr (nthcdr 2 info) (list newmarked))))
4427
4428       ;; Cut off the end of the info if there's nothing else there.
4429       (let ((i 5))
4430         (while (and (> i 2)
4431                     (not (nth i info)))
4432           (when (nthcdr (decf i) info)
4433             (setcdr (nthcdr i info) nil)))))))
4434
4435 (defun gnus-set-mode-line (where)
4436   "This function sets the mode line of the article or summary buffers.
4437 If WHERE is `summary', the summary mode line format will be used."
4438   ;; Is this mode line one we keep updated?
4439   (when (and (memq where gnus-updated-mode-lines)
4440              (symbol-value
4441               (intern (format "gnus-%s-mode-line-format-spec" where))))
4442     (let (mode-string)
4443       (save-excursion
4444         ;; We evaluate this in the summary buffer since these
4445         ;; variables are buffer-local to that buffer.
4446         (set-buffer gnus-summary-buffer)
4447         ;; We bind all these variables that are used in the `eval' form
4448         ;; below.
4449         (let* ((mformat (symbol-value
4450                          (intern
4451                           (format "gnus-%s-mode-line-format-spec" where))))
4452                (gnus-tmp-group-name gnus-newsgroup-name)
4453                (gnus-tmp-article-number (or gnus-current-article 0))
4454                (gnus-tmp-unread gnus-newsgroup-unreads)
4455                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4456                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4457                (gnus-tmp-unread-and-unselected
4458                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4459                             (zerop gnus-tmp-unselected))
4460                        "")
4461                       ((zerop gnus-tmp-unselected)
4462                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4463                       (t (format "{%d(+%d) more}"
4464                                  gnus-tmp-unread-and-unticked
4465                                  gnus-tmp-unselected))))
4466                (gnus-tmp-subject
4467                 (if (and gnus-current-headers
4468                          (vectorp gnus-current-headers))
4469                     (gnus-mode-string-quote
4470                      (mail-header-subject gnus-current-headers))
4471                   ""))
4472                bufname-length max-len
4473                gnus-tmp-header);; passed as argument to any user-format-funcs
4474           (setq mode-string (eval mformat))
4475           (setq bufname-length (if (string-match "%b" mode-string)
4476                                    (- (length
4477                                        (buffer-name
4478                                         (if (eq where 'summary)
4479                                             nil
4480                                           (get-buffer gnus-article-buffer))))
4481                                       2)
4482                                  0))
4483           (setq max-len (max 4 (if gnus-mode-non-string-length
4484                                    (- (window-width)
4485                                       gnus-mode-non-string-length
4486                                       bufname-length)
4487                                  (length mode-string))))
4488           ;; We might have to chop a bit of the string off...
4489           (when (> (length mode-string) max-len)
4490             (setq mode-string
4491                   (concat (truncate-string-to-width mode-string (- max-len 3))
4492                           "...")))
4493           ;; Pad the mode string a bit.
4494           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4495       ;; Update the mode line.
4496       (setq mode-line-buffer-identification
4497             (gnus-mode-line-buffer-identification (list mode-string)))
4498       (set-buffer-modified-p t))))
4499
4500 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4501   "Go through the HEADERS list and add all Xrefs to a hash table.
4502 The resulting hash table is returned, or nil if no Xrefs were found."
4503   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4504          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4505          (xref-hashtb (gnus-make-hashtable))
4506          start group entry number xrefs header)
4507     (while headers
4508       (setq header (pop headers))
4509       (when (and (setq xrefs (mail-header-xref header))
4510                  (not (memq (setq number (mail-header-number header))
4511                             unreads)))
4512         (setq start 0)
4513         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4514           (setq start (match-end 0))
4515           (setq group (if prefix
4516                           (concat prefix (substring xrefs (match-beginning 1)
4517                                                     (match-end 1)))
4518                         (substring xrefs (match-beginning 1) (match-end 1))))
4519           (setq number
4520                 (string-to-int (substring xrefs (match-beginning 2)
4521                                           (match-end 2))))
4522           (if (setq entry (gnus-gethash group xref-hashtb))
4523               (setcdr entry (cons number (cdr entry)))
4524             (gnus-sethash group (cons number nil) xref-hashtb)))))
4525     (and start xref-hashtb)))
4526
4527 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4528   "Look through all the headers and mark the Xrefs as read."
4529   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4530         name entry info xref-hashtb idlist method nth4)
4531     (save-excursion
4532       (set-buffer gnus-group-buffer)
4533       (when (setq xref-hashtb
4534                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4535         (mapatoms
4536          (lambda (group)
4537            (unless (string= from-newsgroup (setq name (symbol-name group)))
4538              (setq idlist (symbol-value group))
4539              ;; Dead groups are not updated.
4540              (and (prog1
4541                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4542                             info (nth 2 entry))
4543                     (when (stringp (setq nth4 (gnus-info-method info)))
4544                       (setq nth4 (gnus-server-to-method nth4))))
4545                   ;; Only do the xrefs if the group has the same
4546                   ;; select method as the group we have just read.
4547                   (or (gnus-methods-equal-p
4548                        nth4 (gnus-find-method-for-group from-newsgroup))
4549                       virtual
4550                       (equal nth4 (setq method (gnus-find-method-for-group
4551                                                 from-newsgroup)))
4552                       (and (equal (car nth4) (car method))
4553                            (equal (nth 1 nth4) (nth 1 method))))
4554                   gnus-use-cross-reference
4555                   (or (not (eq gnus-use-cross-reference t))
4556                       virtual
4557                       ;; Only do cross-references on subscribed
4558                       ;; groups, if that is what is wanted.
4559                       (<= (gnus-info-level info) gnus-level-subscribed))
4560                   (gnus-group-make-articles-read name idlist))))
4561          xref-hashtb)))))
4562
4563 (defun gnus-compute-read-articles (group articles)
4564   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4565          (info (nth 2 entry))
4566          (active (gnus-active group))
4567          ninfo)
4568     (when entry
4569       ;; First peel off all invalid article numbers.
4570       (when active
4571         (let ((ids articles)
4572               id first)
4573           (while (setq id (pop ids))
4574             (when (and first (> id (cdr active)))
4575               ;; We'll end up in this situation in one particular
4576               ;; obscure situation.  If you re-scan a group and get
4577               ;; a new article that is cross-posted to a different
4578               ;; group that has not been re-scanned, you might get
4579               ;; crossposted article that has a higher number than
4580               ;; Gnus believes possible.  So we re-activate this
4581               ;; group as well.  This might mean doing the
4582               ;; crossposting thingy will *increase* the number
4583               ;; of articles in some groups.  Tsk, tsk.
4584               (setq active (or (gnus-activate-group group) active)))
4585             (when (or (> id (cdr active))
4586                       (< id (car active)))
4587               (setq articles (delq id articles))))))
4588       ;; If the read list is nil, we init it.
4589       (if (and active
4590                (null (gnus-info-read info))
4591                (> (car active) 1))
4592           (setq ninfo (cons 1 (1- (car active))))
4593         (setq ninfo (gnus-info-read info)))
4594       ;; Then we add the read articles to the range.
4595       (gnus-add-to-range
4596        ninfo (setq articles (sort articles '<))))))
4597
4598 (defun gnus-group-make-articles-read (group articles)
4599   "Update the info of GROUP to say that ARTICLES are read."
4600   (let* ((num 0)
4601          (entry (gnus-gethash group gnus-newsrc-hashtb))
4602          (info (nth 2 entry))
4603          (active (gnus-active group))
4604          range)
4605     (when entry
4606       (setq range (gnus-compute-read-articles group articles))
4607       (save-excursion
4608         (set-buffer gnus-group-buffer)
4609         (gnus-undo-register
4610           `(progn
4611              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4612              (gnus-info-set-read ',info ',(gnus-info-read info))
4613              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4614              (gnus-group-update-group ,group t))))
4615       ;; Add the read articles to the range.
4616       (gnus-info-set-read info range)
4617       ;; Then we have to re-compute how many unread
4618       ;; articles there are in this group.
4619       (when active
4620         (cond
4621          ((not range)
4622           (setq num (- (1+ (cdr active)) (car active))))
4623          ((not (listp (cdr range)))
4624           (setq num (- (cdr active) (- (1+ (cdr range))
4625                                        (car range)))))
4626          (t
4627           (while range
4628             (if (numberp (car range))
4629                 (setq num (1+ num))
4630               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4631             (setq range (cdr range)))
4632           (setq num (- (cdr active) num))))
4633         ;; Update the number of unread articles.
4634         (setcar entry num)
4635         ;; Update the group buffer.
4636         (gnus-group-update-group group t)))))
4637
4638 (defvar gnus-newsgroup-none-id 0)
4639
4640 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4641   (let ((cur nntp-server-buffer)
4642         (dependencies
4643          (or dependencies
4644              (save-excursion (set-buffer gnus-summary-buffer)
4645                              gnus-newsgroup-dependencies)))
4646         headers id end ref
4647         (mail-parse-charset gnus-newsgroup-charset)
4648         (mail-parse-ignored-charsets 
4649          (save-excursion (condition-case nil
4650                              (set-buffer gnus-summary-buffer)
4651                            (error))
4652                          gnus-newsgroup-ignored-charsets)))
4653     (save-excursion
4654       (set-buffer nntp-server-buffer)
4655       ;; Translate all TAB characters into SPACE characters.
4656       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4657       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4658       (gnus-run-hooks 'gnus-parse-headers-hook)
4659       (let ((case-fold-search t)
4660             in-reply-to header p lines chars)
4661         (goto-char (point-min))
4662         ;; Search to the beginning of the next header.  Error messages
4663         ;; do not begin with 2 or 3.
4664         (while (re-search-forward "^[23][0-9]+ " nil t)
4665           (setq id nil
4666                 ref nil)
4667           ;; This implementation of this function, with nine
4668           ;; search-forwards instead of the one re-search-forward and
4669           ;; a case (which basically was the old function) is actually
4670           ;; about twice as fast, even though it looks messier.  You
4671           ;; can't have everything, I guess.  Speed and elegance
4672           ;; doesn't always go hand in hand.
4673           (setq
4674            header
4675            (vector
4676             ;; Number.
4677             (prog1
4678                 (read cur)
4679               (end-of-line)
4680               (setq p (point))
4681               (narrow-to-region (point)
4682                                 (or (and (search-forward "\n.\n" nil t)
4683                                          (- (point) 2))
4684                                     (point))))
4685             ;; Subject.
4686             (progn
4687               (goto-char p)
4688               (if (search-forward "\nsubject: " nil t)
4689                   (funcall gnus-decode-encoded-word-function
4690                            (nnheader-header-value))
4691                 "(none)"))
4692             ;; From.
4693             (progn
4694               (goto-char p)
4695               (if (search-forward "\nfrom: " nil t)
4696                   (funcall gnus-decode-encoded-word-function
4697                            (nnheader-header-value))
4698                 "(nobody)"))
4699             ;; Date.
4700             (progn
4701               (goto-char p)
4702               (if (search-forward "\ndate: " nil t)
4703                   (nnheader-header-value) ""))
4704             ;; Message-ID.
4705             (progn
4706               (goto-char p)
4707               (setq id (if (re-search-forward
4708                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4709                            ;; We do it this way to make sure the Message-ID
4710                            ;; is (somewhat) syntactically valid.
4711                            (buffer-substring (match-beginning 1)
4712                                              (match-end 1))
4713                          ;; If there was no message-id, we just fake one
4714                          ;; to make subsequent routines simpler.
4715                          (nnheader-generate-fake-message-id))))
4716             ;; References.
4717             (progn
4718               (goto-char p)
4719               (if (search-forward "\nreferences: " nil t)
4720                   (progn
4721                     (setq end (point))
4722                     (prog1
4723                         (nnheader-header-value)
4724                       (setq ref
4725                             (buffer-substring
4726                              (progn
4727                                (end-of-line)
4728                                (search-backward ">" end t)
4729                                (1+ (point)))
4730                              (progn
4731                                (search-backward "<" end t)
4732                                (point))))))
4733                 ;; Get the references from the in-reply-to header if there
4734                 ;; were no references and the in-reply-to header looks
4735                 ;; promising.
4736                 (if (and (search-forward "\nin-reply-to: " nil t)
4737                          (setq in-reply-to (nnheader-header-value))
4738                          (string-match "<[^>]+>" in-reply-to))
4739                     (let (ref2)
4740                       (setq ref (substring in-reply-to (match-beginning 0)
4741                                            (match-end 0)))
4742                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4743                         (setq ref2 (substring in-reply-to (match-beginning 0)
4744                                               (match-end 0)))
4745                         (when (> (length ref2) (length ref))
4746                           (setq ref ref2)))
4747                       ref)
4748                   (setq ref nil))))
4749             ;; Chars.
4750             (progn
4751               (goto-char p)
4752               (if (search-forward "\nchars: " nil t)
4753                   (if (numberp (setq chars (ignore-errors (read cur))))
4754                       chars 0)
4755                 0))
4756             ;; Lines.
4757             (progn
4758               (goto-char p)
4759               (if (search-forward "\nlines: " nil t)
4760                   (if (numberp (setq lines (ignore-errors (read cur))))
4761                       lines 0)
4762                 0))
4763             ;; Xref.
4764             (progn
4765               (goto-char p)
4766               (and (search-forward "\nxref: " nil t)
4767                    (nnheader-header-value)))
4768             ;; Extra.
4769             (when gnus-extra-headers
4770               (let ((extra gnus-extra-headers)
4771                     out)
4772                 (while extra
4773                   (goto-char p)
4774                   (when (search-forward
4775                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4776                     (push (cons (car extra) (nnheader-header-value))
4777                           out))
4778                   (pop extra))
4779                 out))))
4780           (when (equal id ref)
4781             (setq ref nil))
4782
4783           (when gnus-alter-header-function
4784             (funcall gnus-alter-header-function header)
4785             (setq id (mail-header-id header)
4786                   ref (gnus-parent-id (mail-header-references header))))
4787
4788           (when (setq header
4789                       (gnus-dependencies-add-header
4790                        header dependencies force-new))
4791             (push header headers))
4792           (goto-char (point-max))
4793           (widen))
4794         (nreverse headers)))))
4795
4796 ;; Goes through the xover lines and returns a list of vectors
4797 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4798                                                   force-new dependencies
4799                                                   group also-fetch-heads)
4800   "Parse the news overview data in the server buffer, and return a
4801 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4802   ;; Get the Xref when the users reads the articles since most/some
4803   ;; NNTP servers do not include Xrefs when using XOVER.
4804   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4805   (let ((mail-parse-charset gnus-newsgroup-charset)
4806         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4807         (cur nntp-server-buffer)
4808         (dependencies (or dependencies gnus-newsgroup-dependencies))
4809         number headers header)
4810     (save-excursion
4811       (set-buffer nntp-server-buffer)
4812       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4813       ;; Allow the user to mangle the headers before parsing them.
4814       (gnus-run-hooks 'gnus-parse-headers-hook)
4815       (goto-char (point-min))
4816       (while (not (eobp))
4817         (condition-case ()
4818             (while (and sequence (not (eobp)))
4819               (setq number (read cur))
4820               (while (and sequence
4821                           (< (car sequence) number))
4822                 (setq sequence (cdr sequence)))
4823               (and sequence
4824                    (eq number (car sequence))
4825                    (progn
4826                      (setq sequence (cdr sequence))
4827                      (setq header (inline
4828                                     (gnus-nov-parse-line
4829                                      number dependencies force-new))))
4830                    (push header headers))
4831               (forward-line 1))
4832           (error
4833            (gnus-error 4 "Strange nov line (%d)"
4834                        (count-lines (point-min) (point)))))
4835         (forward-line 1))
4836       ;; A common bug in inn is that if you have posted an article and
4837       ;; then retrieves the active file, it will answer correctly --
4838       ;; the new article is included.  However, a NOV entry for the
4839       ;; article may not have been generated yet, so this may fail.
4840       ;; We work around this problem by retrieving the last few
4841       ;; headers using HEAD.
4842       (if (or (not also-fetch-heads)
4843               (not sequence))
4844           ;; We (probably) got all the headers.
4845           (nreverse headers)
4846         (let ((gnus-nov-is-evil t))
4847           (nconc
4848            (nreverse headers)
4849            (when (gnus-retrieve-headers sequence group)
4850              (gnus-get-newsgroup-headers))))))))
4851
4852 (defun gnus-article-get-xrefs ()
4853   "Fill in the Xref value in `gnus-current-headers', if necessary.
4854 This is meant to be called in `gnus-article-internal-prepare-hook'."
4855   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4856                                  gnus-current-headers)))
4857     (or (not gnus-use-cross-reference)
4858         (not headers)
4859         (and (mail-header-xref headers)
4860              (not (string= (mail-header-xref headers) "")))
4861         (let ((case-fold-search t)
4862               xref)
4863           (save-restriction
4864             (nnheader-narrow-to-headers)
4865             (goto-char (point-min))
4866             (when (or (and (not (eobp))
4867                            (eq (downcase (char-after)) ?x)
4868                            (looking-at "Xref:"))
4869                       (search-forward "\nXref:" nil t))
4870               (goto-char (1+ (match-end 0)))
4871               (setq xref (buffer-substring (point)
4872                                            (progn (end-of-line) (point))))
4873               (mail-header-set-xref headers xref)))))))
4874
4875 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4876   "Find article ID and insert the summary line for that article.
4877 OLD-HEADER can either be a header or a line number to insert
4878 the subject line on."
4879   (let* ((line (and (numberp old-header) old-header))
4880          (old-header (and (vectorp old-header) old-header))
4881          (header (cond ((and old-header use-old-header)
4882                         old-header)
4883                        ((and (numberp id)
4884                              (gnus-number-to-header id))
4885                         (gnus-number-to-header id))
4886                        (t
4887                         (gnus-read-header id))))
4888          (number (and (numberp id) id))
4889          d)
4890     (when header
4891       ;; Rebuild the thread that this article is part of and go to the
4892       ;; article we have fetched.
4893       (when (and (not gnus-show-threads)
4894                  old-header)
4895         (when (and number
4896                    (setq d (gnus-data-find (mail-header-number old-header))))
4897           (goto-char (gnus-data-pos d))
4898           (gnus-data-remove
4899            number
4900            (- (gnus-point-at-bol)
4901               (prog1
4902                   (1+ (gnus-point-at-eol))
4903                 (gnus-delete-line))))))
4904       (when old-header
4905         (mail-header-set-number header (mail-header-number old-header)))
4906       (setq gnus-newsgroup-sparse
4907             (delq (setq number (mail-header-number header))
4908                   gnus-newsgroup-sparse))
4909       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4910       (push number gnus-newsgroup-limit)
4911       (gnus-rebuild-thread (mail-header-id header) line)
4912       (gnus-summary-goto-subject number nil t))
4913     (when (and (numberp number)
4914                (> number 0))
4915       ;; We have to update the boundaries even if we can't fetch the
4916       ;; article if ID is a number -- so that the next `P' or `N'
4917       ;; command will fetch the previous (or next) article even
4918       ;; if the one we tried to fetch this time has been canceled.
4919       (when (> number gnus-newsgroup-end)
4920         (setq gnus-newsgroup-end number))
4921       (when (< number gnus-newsgroup-begin)
4922         (setq gnus-newsgroup-begin number))
4923       (setq gnus-newsgroup-unselected
4924             (delq number gnus-newsgroup-unselected)))
4925     ;; Report back a success?
4926     (and header (mail-header-number header))))
4927
4928 ;;; Process/prefix in the summary buffer
4929
4930 (defun gnus-summary-work-articles (n)
4931   "Return a list of articles to be worked upon.
4932 The prefix argument, the list of process marked articles, and the
4933 current article will be taken into consideration."
4934   (save-excursion
4935     (set-buffer gnus-summary-buffer)
4936     (cond
4937      (n
4938       ;; A numerical prefix has been given.
4939       (setq n (prefix-numeric-value n))
4940       (let ((backward (< n 0))
4941             (n (abs (prefix-numeric-value n)))
4942             articles article)
4943         (save-excursion
4944           (while
4945               (and (> n 0)
4946                    (push (setq article (gnus-summary-article-number))
4947                          articles)
4948                    (if backward
4949                        (gnus-summary-find-prev nil article)
4950                      (gnus-summary-find-next nil article)))
4951             (decf n)))
4952         (nreverse articles)))
4953      ((and (gnus-region-active-p) (mark))
4954       (message "region active")
4955       ;; Work on the region between point and mark.
4956       (let ((max (max (point) (mark)))
4957             articles article)
4958         (save-excursion
4959           (goto-char (min (min (point) (mark))))
4960           (while
4961               (and
4962                (push (setq article (gnus-summary-article-number)) articles)
4963                (gnus-summary-find-next nil article)
4964                (< (point) max)))
4965           (nreverse articles))))
4966      (gnus-newsgroup-processable
4967       ;; There are process-marked articles present.
4968       ;; Save current state.
4969       (gnus-summary-save-process-mark)
4970       ;; Return the list.
4971       (reverse gnus-newsgroup-processable))
4972      (t
4973       ;; Just return the current article.
4974       (list (gnus-summary-article-number))))))
4975
4976 (defmacro gnus-summary-iterate (arg &rest forms)
4977   "Iterate over the process/prefixed articles and do FORMS.
4978 ARG is the interactive prefix given to the command.  FORMS will be
4979 executed with point over the summary line of the articles."
4980   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4981     `(let ((,articles (gnus-summary-work-articles ,arg)))
4982        (while ,articles
4983          (gnus-summary-goto-subject (car ,articles))
4984          ,@forms
4985          (pop ,articles)))))
4986
4987 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4988 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4989
4990 (defun gnus-summary-save-process-mark ()
4991   "Push the current set of process marked articles on the stack."
4992   (interactive)
4993   (push (copy-sequence gnus-newsgroup-processable)
4994         gnus-newsgroup-process-stack))
4995
4996 (defun gnus-summary-kill-process-mark ()
4997   "Push the current set of process marked articles on the stack and unmark."
4998   (interactive)
4999   (gnus-summary-save-process-mark)
5000   (gnus-summary-unmark-all-processable))
5001
5002 (defun gnus-summary-yank-process-mark ()
5003   "Pop the last process mark state off the stack and restore it."
5004   (interactive)
5005   (unless gnus-newsgroup-process-stack
5006     (error "Empty mark stack"))
5007   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5008
5009 (defun gnus-summary-process-mark-set (set)
5010   "Make SET into the current process marked articles."
5011   (gnus-summary-unmark-all-processable)
5012   (while set
5013     (gnus-summary-set-process-mark (pop set))))
5014
5015 ;;; Searching and stuff
5016
5017 (defun gnus-summary-search-group (&optional backward use-level)
5018   "Search for next unread newsgroup.
5019 If optional argument BACKWARD is non-nil, search backward instead."
5020   (save-excursion
5021     (set-buffer gnus-group-buffer)
5022     (when (gnus-group-search-forward
5023            backward nil (if use-level (gnus-group-group-level) nil))
5024       (gnus-group-group-name))))
5025
5026 (defun gnus-summary-best-group (&optional exclude-group)
5027   "Find the name of the best unread group.
5028 If EXCLUDE-GROUP, do not go to this group."
5029   (save-excursion
5030     (set-buffer gnus-group-buffer)
5031     (save-excursion
5032       (gnus-group-best-unread-group exclude-group))))
5033
5034 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5035   (if backward (gnus-summary-find-prev)
5036     (let* ((dummy (gnus-summary-article-intangible-p))
5037            (article (or article (gnus-summary-article-number)))
5038            (arts (gnus-data-find-list article))
5039            result)
5040       (when (and (not dummy)
5041                  (or (not gnus-summary-check-current)
5042                      (not unread)
5043                      (not (gnus-data-unread-p (car arts)))))
5044         (setq arts (cdr arts)))
5045       (when (setq result
5046                   (if unread
5047                       (progn
5048                         (while arts
5049                           (when (or (and undownloaded
5050                                          (eq gnus-undownloaded-mark
5051                                              (gnus-data-mark (car arts))))
5052                                     (gnus-data-unread-p (car arts)))
5053                             (setq result (car arts)
5054                                   arts nil))
5055                           (setq arts (cdr arts)))
5056                         result)
5057                     (car arts)))
5058         (goto-char (gnus-data-pos result))
5059         (gnus-data-number result)))))
5060
5061 (defun gnus-summary-find-prev (&optional unread article)
5062   (let* ((eobp (eobp))
5063          (article (or article (gnus-summary-article-number)))
5064          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5065          result)
5066     (when (and (not eobp)
5067                (or (not gnus-summary-check-current)
5068                    (not unread)
5069                    (not (gnus-data-unread-p (car arts)))))
5070       (setq arts (cdr arts)))
5071     (when (setq result
5072                 (if unread
5073                     (progn
5074                       (while arts
5075                         (when (gnus-data-unread-p (car arts))
5076                           (setq result (car arts)
5077                                 arts nil))
5078                         (setq arts (cdr arts)))
5079                       result)
5080                   (car arts)))
5081       (goto-char (gnus-data-pos result))
5082       (gnus-data-number result))))
5083
5084 (defun gnus-summary-find-subject (subject &optional unread backward article)
5085   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5086          (article (or article (gnus-summary-article-number)))
5087          (articles (gnus-data-list backward))
5088          (arts (gnus-data-find-list article articles))
5089          result)
5090     (when (or (not gnus-summary-check-current)
5091               (not unread)
5092               (not (gnus-data-unread-p (car arts))))
5093       (setq arts (cdr arts)))
5094     (while arts
5095       (and (or (not unread)
5096                (gnus-data-unread-p (car arts)))
5097            (vectorp (gnus-data-header (car arts)))
5098            (gnus-subject-equal
5099             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5100            (setq result (car arts)
5101                  arts nil))
5102       (setq arts (cdr arts)))
5103     (and result
5104          (goto-char (gnus-data-pos result))
5105          (gnus-data-number result))))
5106
5107 (defun gnus-summary-search-forward (&optional unread subject backward)
5108   "Search forward for an article.
5109 If UNREAD, look for unread articles.  If SUBJECT, look for
5110 articles with that subject.  If BACKWARD, search backward instead."
5111   (cond (subject (gnus-summary-find-subject subject unread backward))
5112         (backward (gnus-summary-find-prev unread))
5113         (t (gnus-summary-find-next unread))))
5114
5115 (defun gnus-recenter (&optional n)
5116   "Center point in window and redisplay frame.
5117 Also do horizontal recentering."
5118   (interactive "P")
5119   (when (and nil
5120              gnus-auto-center-summary
5121              (not (eq gnus-auto-center-summary 'vertical)))
5122     (gnus-horizontal-recenter))
5123   (recenter n))
5124
5125 (defun gnus-summary-recenter ()
5126   "Center point in the summary window.
5127 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5128 displayed, no centering will be performed."
5129   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5130   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5131   (interactive)
5132   (let* ((top (cond ((< (window-height) 4) 0)
5133                     ((< (window-height) 7) 1)
5134                     (t (if (numberp gnus-auto-center-summary)
5135                            gnus-auto-center-summary
5136                          2))))
5137          (height (1- (window-height)))
5138          (bottom (save-excursion (goto-char (point-max))
5139                                  (forward-line (- height))
5140                                  (point)))
5141          (window (get-buffer-window (current-buffer))))
5142     ;; The user has to want it.
5143     (when gnus-auto-center-summary
5144       (when (get-buffer-window gnus-article-buffer)
5145         ;; Only do recentering when the article buffer is displayed,
5146         ;; Set the window start to either `bottom', which is the biggest
5147         ;; possible valid number, or the second line from the top,
5148         ;; whichever is the least.
5149         (set-window-start
5150          window (min bottom (save-excursion
5151                               (forward-line (- top)) (point)))
5152          t))
5153       ;; Do horizontal recentering while we're at it.
5154       (when (and (get-buffer-window (current-buffer) t)
5155                  (not (eq gnus-auto-center-summary 'vertical)))
5156         (let ((selected (selected-window)))
5157           (select-window (get-buffer-window (current-buffer) t))
5158           (gnus-summary-position-point)
5159           (gnus-horizontal-recenter)
5160           (select-window selected))))))
5161
5162 (defun gnus-summary-jump-to-group (newsgroup)
5163   "Move point to NEWSGROUP in group mode buffer."
5164   ;; Keep update point of group mode buffer if visible.
5165   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5166       (save-window-excursion
5167         ;; Take care of tree window mode.
5168         (when (get-buffer-window gnus-group-buffer)
5169           (pop-to-buffer gnus-group-buffer))
5170         (gnus-group-jump-to-group newsgroup))
5171     (save-excursion
5172       ;; Take care of tree window mode.
5173       (if (get-buffer-window gnus-group-buffer)
5174           (pop-to-buffer gnus-group-buffer)
5175         (set-buffer gnus-group-buffer))
5176       (gnus-group-jump-to-group newsgroup))))
5177
5178 ;; This function returns a list of article numbers based on the
5179 ;; difference between the ranges of read articles in this group and
5180 ;; the range of active articles.
5181 (defun gnus-list-of-unread-articles (group)
5182   (let* ((read (gnus-info-read (gnus-get-info group)))
5183          (active (or (gnus-active group) (gnus-activate-group group)))
5184          (last (cdr active))
5185          first nlast unread)
5186     ;; If none are read, then all are unread.
5187     (if (not read)
5188         (setq first (car active))
5189       ;; If the range of read articles is a single range, then the
5190       ;; first unread article is the article after the last read
5191       ;; article.  Sounds logical, doesn't it?
5192       (if (and (not (listp (cdr read)))
5193                (or (< (car read) (car active))
5194                    (progn (setq read (list read))
5195                           nil)))
5196           (setq first (max (car active) (1+ (cdr read))))
5197         ;; `read' is a list of ranges.
5198         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5199                                   (caar read)))
5200                   1)
5201           (setq first (car active)))
5202         (while read
5203           (when first
5204             (while (< first nlast)
5205               (push first unread)
5206               (setq first (1+ first))))
5207           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5208           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5209           (setq read (cdr read)))))
5210     ;; And add the last unread articles.
5211     (while (<= first last)
5212       (push first unread)
5213       (setq first (1+ first)))
5214     ;; Return the list of unread articles.
5215     (delq 0 (nreverse unread))))
5216
5217 (defun gnus-list-of-read-articles (group)
5218   "Return a list of unread, unticked and non-dormant articles."
5219   (let* ((info (gnus-get-info group))
5220          (marked (gnus-info-marks info))
5221          (active (gnus-active group)))
5222     (and info active
5223          (gnus-set-difference
5224           (gnus-sorted-complement
5225            (gnus-uncompress-range active)
5226            (gnus-list-of-unread-articles group))
5227           (append
5228            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5229            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5230
5231 ;; Various summary commands
5232
5233 (defun gnus-summary-select-article-buffer ()
5234   "Reconfigure windows to show article buffer."
5235   (interactive)
5236   (if (not (gnus-buffer-live-p gnus-article-buffer))
5237       (error "There is no article buffer for this summary buffer")
5238     (gnus-configure-windows 'article)
5239     (select-window (get-buffer-window gnus-article-buffer))))
5240
5241 (defun gnus-summary-universal-argument (arg)
5242   "Perform any operation on all articles that are process/prefixed."
5243   (interactive "P")
5244   (let ((articles (gnus-summary-work-articles arg))
5245         func article)
5246     (if (eq
5247          (setq
5248           func
5249           (key-binding
5250            (read-key-sequence
5251             (substitute-command-keys
5252              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5253          'undefined)
5254         (gnus-error 1 "Undefined key")
5255       (save-excursion
5256         (while articles
5257           (gnus-summary-goto-subject (setq article (pop articles)))
5258           (let (gnus-newsgroup-processable)
5259             (command-execute func))
5260           (gnus-summary-remove-process-mark article)))))
5261   (gnus-summary-position-point))
5262
5263 (defun gnus-summary-toggle-truncation (&optional arg)
5264   "Toggle truncation of summary lines.
5265 With arg, turn line truncation on iff arg is positive."
5266   (interactive "P")
5267   (setq truncate-lines
5268         (if (null arg) (not truncate-lines)
5269           (> (prefix-numeric-value arg) 0)))
5270   (redraw-display))
5271
5272 (defun gnus-summary-reselect-current-group (&optional all rescan)
5273   "Exit and then reselect the current newsgroup.
5274 The prefix argument ALL means to select all articles."
5275   (interactive "P")
5276   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5277     (error "Ephemeral groups can't be reselected"))
5278   (let ((current-subject (gnus-summary-article-number))
5279         (group gnus-newsgroup-name))
5280     (setq gnus-newsgroup-begin nil)
5281     (gnus-summary-exit)
5282     ;; We have to adjust the point of group mode buffer because
5283     ;; point was moved to the next unread newsgroup by exiting.
5284     (gnus-summary-jump-to-group group)
5285     (when rescan
5286       (save-excursion
5287         (gnus-group-get-new-news-this-group 1)))
5288     (gnus-group-read-group all t)
5289     (gnus-summary-goto-subject current-subject nil t)))
5290
5291 (defun gnus-summary-rescan-group (&optional all)
5292   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5293   (interactive "P")
5294   (gnus-summary-reselect-current-group all t))
5295
5296 (defun gnus-summary-update-info (&optional non-destructive)
5297   (save-excursion
5298     (let ((group gnus-newsgroup-name))
5299       (when group
5300         (when gnus-newsgroup-kill-headers
5301           (setq gnus-newsgroup-killed
5302                 (gnus-compress-sequence
5303                  (nconc
5304                   (gnus-set-sorted-intersection
5305                    (gnus-uncompress-range gnus-newsgroup-killed)
5306                    (setq gnus-newsgroup-unselected
5307                          (sort gnus-newsgroup-unselected '<)))
5308                   (setq gnus-newsgroup-unreads
5309                         (sort gnus-newsgroup-unreads '<)))
5310                  t)))
5311         (unless (listp (cdr gnus-newsgroup-killed))
5312           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5313         (let ((headers gnus-newsgroup-headers))
5314           ;; Set the new ranges of read articles.
5315           (save-excursion
5316             (set-buffer gnus-group-buffer)
5317             (gnus-undo-force-boundary))
5318           (gnus-update-read-articles
5319            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5320           ;; Set the current article marks.
5321           (let ((gnus-newsgroup-scored
5322                  (if (and (not gnus-save-score)
5323                           (not non-destructive))
5324                      nil
5325                    gnus-newsgroup-scored)))
5326             (save-excursion
5327               (gnus-update-marks)))
5328           ;; Do the cross-ref thing.
5329           (when gnus-use-cross-reference
5330             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5331           ;; Do not switch windows but change the buffer to work.
5332           (set-buffer gnus-group-buffer)
5333           (unless (gnus-ephemeral-group-p group)
5334             (gnus-group-update-group group)))))))
5335
5336 (defun gnus-summary-save-newsrc (&optional force)
5337   "Save the current number of read/marked articles in the dribble buffer.
5338 The dribble buffer will then be saved.
5339 If FORCE (the prefix), also save the .newsrc file(s)."
5340   (interactive "P")
5341   (gnus-summary-update-info t)
5342   (if force
5343       (gnus-save-newsrc-file)
5344     (gnus-dribble-save)))
5345
5346 (defun gnus-summary-exit (&optional temporary)
5347   "Exit reading current newsgroup, and then return to group selection mode.
5348 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5349   (interactive)
5350   (gnus-set-global-variables)
5351   (when (gnus-buffer-live-p gnus-article-buffer)
5352     (save-excursion
5353       (set-buffer gnus-article-buffer)
5354       (mm-destroy-parts gnus-article-mime-handles)))
5355   (gnus-kill-save-kill-buffer)
5356   (gnus-async-halt-prefetch)
5357   (let* ((group gnus-newsgroup-name)
5358          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5359          (mode major-mode)
5360          (group-point nil)
5361          (buf (current-buffer)))
5362     (unless quit-config
5363       ;; Do adaptive scoring, and possibly save score files.
5364       (when gnus-newsgroup-adaptive
5365         (gnus-score-adaptive))
5366       (when gnus-use-scoring
5367         (gnus-score-save)))
5368     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5369     ;; If we have several article buffers, we kill them at exit.
5370     (unless gnus-single-article-buffer
5371       (gnus-kill-buffer gnus-original-article-buffer)
5372       (setq gnus-article-current nil))
5373     (when gnus-use-cache
5374       (gnus-cache-possibly-remove-articles)
5375       (gnus-cache-save-buffers))
5376     (gnus-async-prefetch-remove-group group)
5377     (when gnus-suppress-duplicates
5378       (gnus-dup-enter-articles))
5379     (when gnus-use-trees
5380       (gnus-tree-close group))
5381     (when gnus-use-cache
5382       (gnus-cache-write-active))
5383     ;; Remove entries for this group.
5384     (nnmail-purge-split-history (gnus-group-real-name group))
5385     ;; Make all changes in this group permanent.
5386     (unless quit-config
5387       (gnus-run-hooks 'gnus-exit-group-hook)
5388       (gnus-summary-update-info))
5389     (gnus-close-group group)
5390     ;; Make sure where we were, and go to next newsgroup.
5391     (set-buffer gnus-group-buffer)
5392     (unless quit-config
5393       (gnus-group-jump-to-group group))
5394     (gnus-run-hooks 'gnus-summary-exit-hook)
5395     (unless (or quit-config
5396                 ;; If this group has disappeared from the summary
5397                 ;; buffer, don't skip forwards.
5398                 (not (string= group (gnus-group-group-name))))
5399       (gnus-group-next-unread-group 1))
5400     (setq group-point (point))
5401     (if temporary
5402         nil                             ;Nothing to do.
5403       ;; If we have several article buffers, we kill them at exit.
5404       (unless gnus-single-article-buffer
5405         (gnus-kill-buffer gnus-article-buffer)
5406         (gnus-kill-buffer gnus-original-article-buffer)
5407         (setq gnus-article-current nil))
5408       (set-buffer buf)
5409       (if (not gnus-kill-summary-on-exit)
5410           (gnus-deaden-summary)
5411         ;; We set all buffer-local variables to nil.  It is unclear why
5412         ;; this is needed, but if we don't, buffer-local variables are
5413         ;; not garbage-collected, it seems.  This would the lead to en
5414         ;; ever-growing Emacs.
5415         (gnus-summary-clear-local-variables)
5416         (when (get-buffer gnus-article-buffer)
5417           (bury-buffer gnus-article-buffer))
5418         ;; We clear the global counterparts of the buffer-local
5419         ;; variables as well, just to be on the safe side.
5420         (set-buffer gnus-group-buffer)
5421         (gnus-summary-clear-local-variables)
5422         ;; Return to group mode buffer.
5423         (when (eq mode 'gnus-summary-mode)
5424           (gnus-kill-buffer buf)))
5425       (setq gnus-current-select-method gnus-select-method)
5426       (pop-to-buffer gnus-group-buffer)
5427       (if (not quit-config)
5428           (progn
5429             (goto-char group-point)
5430             (gnus-configure-windows 'group 'force))
5431         (gnus-handle-ephemeral-exit quit-config))
5432       ;; Clear the current group name.
5433       (unless quit-config
5434         (setq gnus-newsgroup-name nil)))))
5435
5436 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5437 (defun gnus-summary-exit-no-update (&optional no-questions)
5438   "Quit reading current newsgroup without updating read article info."
5439   (interactive)
5440   (let* ((group gnus-newsgroup-name)
5441          (quit-config (gnus-group-quit-config group)))
5442     (when (or no-questions
5443               gnus-expert-user
5444               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5445       (gnus-async-halt-prefetch)
5446       (mapcar 'funcall
5447               (delq 'gnus-summary-expire-articles
5448                     (copy-sequence gnus-summary-prepare-exit-hook)))
5449       (when (gnus-buffer-live-p gnus-article-buffer)
5450         (save-excursion
5451           (set-buffer gnus-article-buffer)
5452           (mm-destroy-parts gnus-article-mime-handles)))
5453       ;; If we have several article buffers, we kill them at exit.
5454       (unless gnus-single-article-buffer
5455         (gnus-kill-buffer gnus-article-buffer)
5456         (gnus-kill-buffer gnus-original-article-buffer)
5457         (setq gnus-article-current nil))
5458       (if (not gnus-kill-summary-on-exit)
5459           (gnus-deaden-summary)
5460         (gnus-close-group group)
5461         (gnus-summary-clear-local-variables)
5462         (set-buffer gnus-group-buffer)
5463         (gnus-summary-clear-local-variables)
5464         (when (get-buffer gnus-summary-buffer)
5465           (kill-buffer gnus-summary-buffer)))
5466       (unless gnus-single-article-buffer
5467         (setq gnus-article-current nil))
5468       (when gnus-use-trees
5469         (gnus-tree-close group))
5470       (gnus-async-prefetch-remove-group group)
5471       (when (get-buffer gnus-article-buffer)
5472         (bury-buffer gnus-article-buffer))
5473       ;; Return to the group buffer.
5474       (gnus-configure-windows 'group 'force)
5475       ;; Clear the current group name.
5476       (setq gnus-newsgroup-name nil)
5477       (when (equal (gnus-group-group-name) group)
5478         (gnus-group-next-unread-group 1))
5479       (when quit-config
5480         (gnus-handle-ephemeral-exit quit-config)))))
5481
5482 (defun gnus-handle-ephemeral-exit (quit-config)
5483   "Handle movement when leaving an ephemeral group.
5484 The state which existed when entering the ephemeral is reset."
5485   (if (not (buffer-name (car quit-config)))
5486       (gnus-configure-windows 'group 'force)
5487     (set-buffer (car quit-config))
5488     (cond ((eq major-mode 'gnus-summary-mode)
5489            (gnus-set-global-variables))
5490           ((eq major-mode 'gnus-article-mode)
5491            (save-excursion
5492              ;; The `gnus-summary-buffer' variable may point
5493              ;; to the old summary buffer when using a single
5494              ;; article buffer.
5495              (unless (gnus-buffer-live-p gnus-summary-buffer)
5496                (set-buffer gnus-group-buffer))
5497              (set-buffer gnus-summary-buffer)
5498              (gnus-set-global-variables))))
5499     (if (or (eq (cdr quit-config) 'article)
5500             (eq (cdr quit-config) 'pick))
5501         (progn
5502           ;; The current article may be from the ephemeral group
5503           ;; thus it is best that we reload this article
5504           (gnus-summary-show-article)
5505           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5506               (gnus-configure-windows 'pick 'force)
5507             (gnus-configure-windows (cdr quit-config) 'force)))
5508       (gnus-configure-windows (cdr quit-config) 'force))
5509     (when (eq major-mode 'gnus-summary-mode)
5510       (gnus-summary-next-subject 1 nil t)
5511       (gnus-summary-recenter)
5512       (gnus-summary-position-point))))
5513
5514 ;;; Dead summaries.
5515
5516 (defvar gnus-dead-summary-mode-map nil)
5517
5518 (unless gnus-dead-summary-mode-map
5519   (setq gnus-dead-summary-mode-map (make-keymap))
5520   (suppress-keymap gnus-dead-summary-mode-map)
5521   (substitute-key-definition
5522    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5523   (let ((keys '("\C-d" "\r" "\177" [delete])))
5524     (while keys
5525       (define-key gnus-dead-summary-mode-map
5526         (pop keys) 'gnus-summary-wake-up-the-dead))))
5527
5528 (defvar gnus-dead-summary-mode nil
5529   "Minor mode for Gnus summary buffers.")
5530
5531 (defun gnus-dead-summary-mode (&optional arg)
5532   "Minor mode for Gnus summary buffers."
5533   (interactive "P")
5534   (when (eq major-mode 'gnus-summary-mode)
5535     (make-local-variable 'gnus-dead-summary-mode)
5536     (setq gnus-dead-summary-mode
5537           (if (null arg) (not gnus-dead-summary-mode)
5538             (> (prefix-numeric-value arg) 0)))
5539     (when gnus-dead-summary-mode
5540       (gnus-add-minor-mode
5541        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5542
5543 (defun gnus-deaden-summary ()
5544   "Make the current summary buffer into a dead summary buffer."
5545   ;; Kill any previous dead summary buffer.
5546   (when (and gnus-dead-summary
5547              (buffer-name gnus-dead-summary))
5548     (save-excursion
5549       (set-buffer gnus-dead-summary)
5550       (when gnus-dead-summary-mode
5551         (kill-buffer (current-buffer)))))
5552   ;; Make this the current dead summary.
5553   (setq gnus-dead-summary (current-buffer))
5554   (gnus-dead-summary-mode 1)
5555   (let ((name (buffer-name)))
5556     (when (string-match "Summary" name)
5557       (rename-buffer
5558        (concat (substring name 0 (match-beginning 0)) "Dead "
5559                (substring name (match-beginning 0)))
5560        t)
5561       (bury-buffer))))
5562
5563 (defun gnus-kill-or-deaden-summary (buffer)
5564   "Kill or deaden the summary BUFFER."
5565   (save-excursion
5566     (when (and (buffer-name buffer)
5567                (not gnus-single-article-buffer))
5568       (save-excursion
5569         (set-buffer buffer)
5570         (gnus-kill-buffer gnus-article-buffer)
5571         (gnus-kill-buffer gnus-original-article-buffer)))
5572     (cond (gnus-kill-summary-on-exit
5573            (when (and gnus-use-trees
5574                       (gnus-buffer-exists-p buffer))
5575              (save-excursion
5576                (set-buffer buffer)
5577                (gnus-tree-close gnus-newsgroup-name)))
5578            (gnus-kill-buffer buffer))
5579           ((gnus-buffer-exists-p buffer)
5580            (save-excursion
5581              (set-buffer buffer)
5582              (gnus-deaden-summary))))))
5583
5584 (defun gnus-summary-wake-up-the-dead (&rest args)
5585   "Wake up the dead summary buffer."
5586   (interactive)
5587   (gnus-dead-summary-mode -1)
5588   (let ((name (buffer-name)))
5589     (when (string-match "Dead " name)
5590       (rename-buffer
5591        (concat (substring name 0 (match-beginning 0))
5592                (substring name (match-end 0)))
5593        t)))
5594   (gnus-message 3 "This dead summary is now alive again"))
5595
5596 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5597 (defun gnus-summary-fetch-faq (&optional faq-dir)
5598   "Fetch the FAQ for the current group.
5599 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5600 in."
5601   (interactive
5602    (list
5603     (when current-prefix-arg
5604       (completing-read
5605        "Faq dir: " (and (listp gnus-group-faq-directory)
5606                         (mapcar (lambda (file) (list file))
5607                                 gnus-group-faq-directory))))))
5608   (let (gnus-faq-buffer)
5609     (when (setq gnus-faq-buffer
5610                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5611       (gnus-configure-windows 'summary-faq))))
5612
5613 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5614 (defun gnus-summary-describe-group (&optional force)
5615   "Describe the current newsgroup."
5616   (interactive "P")
5617   (gnus-group-describe-group force gnus-newsgroup-name))
5618
5619 (defun gnus-summary-describe-briefly ()
5620   "Describe summary mode commands briefly."
5621   (interactive)
5622   (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")))
5623
5624 ;; Walking around group mode buffer from summary mode.
5625
5626 (defun gnus-summary-next-group (&optional no-article target-group backward)
5627   "Exit current newsgroup and then select next unread newsgroup.
5628 If prefix argument NO-ARTICLE is non-nil, no article is selected
5629 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5630 previous group instead."
5631   (interactive "P")
5632   ;; Stop pre-fetching.
5633   (gnus-async-halt-prefetch)
5634   (let ((current-group gnus-newsgroup-name)
5635         (current-buffer (current-buffer))
5636         entered)
5637     ;; First we semi-exit this group to update Xrefs and all variables.
5638     ;; We can't do a real exit, because the window conf must remain
5639     ;; the same in case the user is prompted for info, and we don't
5640     ;; want the window conf to change before that...
5641     (gnus-summary-exit t)
5642     (while (not entered)
5643       ;; Then we find what group we are supposed to enter.
5644       (set-buffer gnus-group-buffer)
5645       (gnus-group-jump-to-group current-group)
5646       (setq target-group
5647             (or target-group
5648                 (if (eq gnus-keep-same-level 'best)
5649                     (gnus-summary-best-group gnus-newsgroup-name)
5650                   (gnus-summary-search-group backward gnus-keep-same-level))))
5651       (if (not target-group)
5652           ;; There are no further groups, so we return to the group
5653           ;; buffer.
5654           (progn
5655             (gnus-message 5 "Returning to the group buffer")
5656             (setq entered t)
5657             (when (gnus-buffer-live-p current-buffer)
5658               (set-buffer current-buffer)
5659               (gnus-summary-exit))
5660             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5661         ;; We try to enter the target group.
5662         (gnus-group-jump-to-group target-group)
5663         (let ((unreads (gnus-group-group-unread)))
5664           (if (and (or (eq t unreads)
5665                        (and unreads (not (zerop unreads))))
5666                    (gnus-summary-read-group
5667                     target-group nil no-article
5668                     (and (buffer-name current-buffer) current-buffer)
5669                     nil backward))
5670               (setq entered t)
5671             (setq current-group target-group
5672                   target-group nil)))))))
5673
5674 (defun gnus-summary-prev-group (&optional no-article)
5675   "Exit current newsgroup and then select previous unread newsgroup.
5676 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5677   (interactive "P")
5678   (gnus-summary-next-group no-article nil t))
5679
5680 ;; Walking around summary lines.
5681
5682 (defun gnus-summary-first-subject (&optional unread undownloaded)
5683   "Go to the first unread subject.
5684 If UNREAD is non-nil, go to the first unread article.
5685 Returns the article selected or nil if there are no unread articles."
5686   (interactive "P")
5687   (prog1
5688       (cond
5689        ;; Empty summary.
5690        ((null gnus-newsgroup-data)
5691         (gnus-message 3 "No articles in the group")
5692         nil)
5693        ;; Pick the first article.
5694        ((not unread)
5695         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5696         (gnus-data-number (car gnus-newsgroup-data)))
5697        ;; No unread articles.
5698        ((null gnus-newsgroup-unreads)
5699         (gnus-message 3 "No more unread articles")
5700         nil)
5701        ;; Find the first unread article.
5702        (t
5703         (let ((data gnus-newsgroup-data))
5704           (while (and data
5705                       (and (not (and undownloaded
5706                                      (eq gnus-undownloaded-mark
5707                                          (gnus-data-mark (car data)))))
5708                            (not (gnus-data-unread-p (car data)))))
5709             (setq data (cdr data)))
5710           (when data
5711             (goto-char (gnus-data-pos (car data)))
5712             (gnus-data-number (car data))))))
5713     (gnus-summary-position-point)))
5714
5715 (defun gnus-summary-next-subject (n &optional unread dont-display)
5716   "Go to next N'th summary line.
5717 If N is negative, go to the previous N'th subject line.
5718 If UNREAD is non-nil, only unread articles are selected.
5719 The difference between N and the actual number of steps taken is
5720 returned."
5721   (interactive "p")
5722   (let ((backward (< n 0))
5723         (n (abs n)))
5724     (while (and (> n 0)
5725                 (if backward
5726                     (gnus-summary-find-prev unread)
5727                   (gnus-summary-find-next unread)))
5728       (unless (zerop (setq n (1- n)))
5729         (gnus-summary-show-thread)))
5730     (when (/= 0 n)
5731       (gnus-message 7 "No more%s articles"
5732                     (if unread " unread" "")))
5733     (unless dont-display
5734       (gnus-summary-recenter)
5735       (gnus-summary-position-point))
5736     n))
5737
5738 (defun gnus-summary-next-unread-subject (n)
5739   "Go to next N'th unread summary line."
5740   (interactive "p")
5741   (gnus-summary-next-subject n t))
5742
5743 (defun gnus-summary-prev-subject (n &optional unread)
5744   "Go to previous N'th summary line.
5745 If optional argument UNREAD is non-nil, only unread article is selected."
5746   (interactive "p")
5747   (gnus-summary-next-subject (- n) unread))
5748
5749 (defun gnus-summary-prev-unread-subject (n)
5750   "Go to previous N'th unread summary line."
5751   (interactive "p")
5752   (gnus-summary-next-subject (- n) t))
5753
5754 (defun gnus-summary-goto-subject (article &optional force silent)
5755   "Go the subject line of ARTICLE.
5756 If FORCE, also allow jumping to articles not currently shown."
5757   (interactive "nArticle number: ")
5758   (let ((b (point))
5759         (data (gnus-data-find article)))
5760     ;; We read in the article if we have to.
5761     (and (not data)
5762          force
5763          (gnus-summary-insert-subject
5764           article
5765           (if (or (numberp force) (vectorp force)) force)
5766           t)
5767          (setq data (gnus-data-find article)))
5768     (goto-char b)
5769     (if (not data)
5770         (progn
5771           (unless silent
5772             (gnus-message 3 "Can't find article %d" article))
5773           nil)
5774       (goto-char (gnus-data-pos data))
5775       (gnus-summary-position-point)
5776       article)))
5777
5778 ;; Walking around summary lines with displaying articles.
5779
5780 (defun gnus-summary-expand-window (&optional arg)
5781   "Make the summary buffer take up the entire Emacs frame.
5782 Given a prefix, will force an `article' buffer configuration."
5783   (interactive "P")
5784   (if arg
5785       (gnus-configure-windows 'article 'force)
5786     (gnus-configure-windows 'summary 'force)))
5787
5788 (defun gnus-summary-display-article (article &optional all-header)
5789   "Display ARTICLE in article buffer."
5790   (gnus-set-global-variables)
5791   (if (null article)
5792       nil
5793     (prog1
5794         (if gnus-summary-display-article-function
5795             (funcall gnus-summary-display-article-function article all-header)
5796           (gnus-article-prepare article all-header))
5797       (gnus-run-hooks 'gnus-select-article-hook)
5798       (when (and gnus-current-article
5799                  (not (zerop gnus-current-article)))
5800         (gnus-summary-goto-subject gnus-current-article))
5801       (gnus-summary-recenter)
5802       (when (and gnus-use-trees gnus-show-threads)
5803         (gnus-possibly-generate-tree article)
5804         (gnus-highlight-selected-tree article))
5805       ;; Successfully display article.
5806       (gnus-article-set-window-start
5807        (cdr (assq article gnus-newsgroup-bookmarks))))))
5808
5809 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5810   "Select the current article.
5811 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5812 non-nil, the article will be re-fetched even if it already present in
5813 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5814 be displayed."
5815   ;; Make sure we are in the summary buffer to work around bbdb bug.
5816   (unless (eq major-mode 'gnus-summary-mode)
5817     (set-buffer gnus-summary-buffer))
5818   (let ((article (or article (gnus-summary-article-number)))
5819         (all-headers (not (not all-headers))) ;Must be T or NIL.
5820         gnus-summary-display-article-function)
5821     (and (not pseudo)
5822          (gnus-summary-article-pseudo-p article)
5823          (error "This is a pseudo-article"))
5824     (save-excursion
5825       (set-buffer gnus-summary-buffer)
5826       (if (or (and gnus-single-article-buffer
5827                    (or (null gnus-current-article)
5828                        (null gnus-article-current)
5829                        (null (get-buffer gnus-article-buffer))
5830                        (not (eq article (cdr gnus-article-current)))
5831                        (not (equal (car gnus-article-current)
5832                                    gnus-newsgroup-name))))
5833               (and (not gnus-single-article-buffer)
5834                    (or (null gnus-current-article)
5835                        (not (eq gnus-current-article article))))
5836               force)
5837           ;; The requested article is different from the current article.
5838           (progn
5839             (gnus-summary-display-article article all-headers)
5840             (when (or all-headers gnus-show-all-headers)
5841               (gnus-article-show-all-headers))
5842             (gnus-article-set-window-start
5843              (cdr (assq article gnus-newsgroup-bookmarks)))
5844             article)
5845         (when (or all-headers gnus-show-all-headers)
5846           (gnus-article-show-all-headers))
5847         'old))))
5848
5849 (defun gnus-summary-set-current-mark (&optional current-mark)
5850   "Obsolete function."
5851   nil)
5852
5853 (defun gnus-summary-next-article (&optional unread subject backward push)
5854   "Select the next article.
5855 If UNREAD, only unread articles are selected.
5856 If SUBJECT, only articles with SUBJECT are selected.
5857 If BACKWARD, the previous article is selected instead of the next."
5858   (interactive "P")
5859   (cond
5860    ;; Is there such an article?
5861    ((and (gnus-summary-search-forward unread subject backward)
5862          (or (gnus-summary-display-article (gnus-summary-article-number))
5863              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5864     (gnus-summary-position-point))
5865    ;; If not, we try the first unread, if that is wanted.
5866    ((and subject
5867          gnus-auto-select-same
5868          (gnus-summary-first-unread-article))
5869     (gnus-summary-position-point)
5870     (gnus-message 6 "Wrapped"))
5871    ;; Try to get next/previous article not displayed in this group.
5872    ((and gnus-auto-extend-newsgroup
5873          (not unread) (not subject))
5874     (gnus-summary-goto-article
5875      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5876      nil (count-lines (point-min) (point))))
5877    ;; Go to next/previous group.
5878    (t
5879     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5880       (gnus-summary-jump-to-group gnus-newsgroup-name))
5881     (let ((cmd last-command-char)
5882           (point
5883            (save-excursion
5884              (set-buffer gnus-group-buffer)
5885              (point)))
5886           (group
5887            (if (eq gnus-keep-same-level 'best)
5888                (gnus-summary-best-group gnus-newsgroup-name)
5889              (gnus-summary-search-group backward gnus-keep-same-level))))
5890       ;; For some reason, the group window gets selected.  We change
5891       ;; it back.
5892       (select-window (get-buffer-window (current-buffer)))
5893       ;; Select next unread newsgroup automagically.
5894       (cond
5895        ((or (not gnus-auto-select-next)
5896             (not cmd))
5897         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5898        ((or (eq gnus-auto-select-next 'quietly)
5899             (and (eq gnus-auto-select-next 'slightly-quietly)
5900                  push)
5901             (and (eq gnus-auto-select-next 'almost-quietly)
5902                  (gnus-summary-last-article-p)))
5903         ;; Select quietly.
5904         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5905             (gnus-summary-exit)
5906           (gnus-message 7 "No more%s articles (%s)..."
5907                         (if unread " unread" "")
5908                         (if group (concat "selecting " group)
5909                           "exiting"))
5910           (gnus-summary-next-group nil group backward)))
5911        (t
5912         (when (gnus-key-press-event-p last-input-event)
5913           (gnus-summary-walk-group-buffer
5914            gnus-newsgroup-name cmd unread backward point))))))))
5915
5916 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5917   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5918                       (?\C-p (gnus-group-prev-unread-group 1))))
5919         (cursor-in-echo-area t)
5920         keve key group ended)
5921     (save-excursion
5922       (set-buffer gnus-group-buffer)
5923       (goto-char start)
5924       (setq group
5925             (if (eq gnus-keep-same-level 'best)
5926                 (gnus-summary-best-group gnus-newsgroup-name)
5927               (gnus-summary-search-group backward gnus-keep-same-level))))
5928     (while (not ended)
5929       (gnus-message
5930        5 "No more%s articles%s" (if unread " unread" "")
5931        (if (and group
5932                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5933            (format " (Type %s for %s [%s])"
5934                    (single-key-description cmd) group
5935                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5936          (format " (Type %s to exit %s)"
5937                  (single-key-description cmd)
5938                  gnus-newsgroup-name)))
5939       ;; Confirm auto selection.
5940       (setq key (car (setq keve (gnus-read-event-char))))
5941       (setq ended t)
5942       (cond
5943        ((assq key keystrokes)
5944         (let ((obuf (current-buffer)))
5945           (switch-to-buffer gnus-group-buffer)
5946           (when group
5947             (gnus-group-jump-to-group group))
5948           (eval (cadr (assq key keystrokes)))
5949           (setq group (gnus-group-group-name))
5950           (switch-to-buffer obuf))
5951         (setq ended nil))
5952        ((equal key cmd)
5953         (if (or (not group)
5954                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5955             (gnus-summary-exit)
5956           (gnus-summary-next-group nil group backward)))
5957        (t
5958         (push (cdr keve) unread-command-events))))))
5959
5960 (defun gnus-summary-next-unread-article ()
5961   "Select unread article after current one."
5962   (interactive)
5963   (gnus-summary-next-article
5964    (or (not (eq gnus-summary-goto-unread 'never))
5965        (gnus-summary-last-article-p (gnus-summary-article-number)))
5966    (and gnus-auto-select-same
5967         (gnus-summary-article-subject))))
5968
5969 (defun gnus-summary-prev-article (&optional unread subject)
5970   "Select the article after the current one.
5971 If UNREAD is non-nil, only unread articles are selected."
5972   (interactive "P")
5973   (gnus-summary-next-article unread subject t))
5974
5975 (defun gnus-summary-prev-unread-article ()
5976   "Select unread article before current one."
5977   (interactive)
5978   (gnus-summary-prev-article
5979    (or (not (eq gnus-summary-goto-unread 'never))
5980        (gnus-summary-first-article-p (gnus-summary-article-number)))
5981    (and gnus-auto-select-same
5982         (gnus-summary-article-subject))))
5983
5984 (defun gnus-summary-next-page (&optional lines circular)
5985   "Show next page of the selected article.
5986 If at the end of the current article, select the next article.
5987 LINES says how many lines should be scrolled up.
5988
5989 If CIRCULAR is non-nil, go to the start of the article instead of
5990 selecting the next article when reaching the end of the current
5991 article."
5992   (interactive "P")
5993   (setq gnus-summary-buffer (current-buffer))
5994   (gnus-set-global-variables)
5995   (let ((article (gnus-summary-article-number))
5996         (article-window (get-buffer-window gnus-article-buffer t))
5997         endp)
5998     ;; If the buffer is empty, we have no article.
5999     (unless article
6000       (error "No article to select"))
6001     (gnus-configure-windows 'article)
6002     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6003         (if (and (eq gnus-summary-goto-unread 'never)
6004                  (not (gnus-summary-last-article-p article)))
6005             (gnus-summary-next-article)
6006           (gnus-summary-next-unread-article))
6007       (if (or (null gnus-current-article)
6008               (null gnus-article-current)
6009               (/= article (cdr gnus-article-current))
6010               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6011           ;; Selected subject is different from current article's.
6012           (gnus-summary-display-article article)
6013         (when article-window
6014           (gnus-eval-in-buffer-window gnus-article-buffer
6015             (setq endp (gnus-article-next-page lines)))
6016           (when endp
6017             (cond (circular
6018                    (gnus-summary-beginning-of-article))
6019                   (lines
6020                    (gnus-message 3 "End of message"))
6021                   ((null lines)
6022                    (if (and (eq gnus-summary-goto-unread 'never)
6023                             (not (gnus-summary-last-article-p article)))
6024                        (gnus-summary-next-article)
6025                      (gnus-summary-next-unread-article))))))))
6026     (gnus-summary-recenter)
6027     (gnus-summary-position-point)))
6028
6029 (defun gnus-summary-prev-page (&optional lines move)
6030   "Show previous page of selected article.
6031 Argument LINES specifies lines to be scrolled down.
6032 If MOVE, move to the previous unread article if point is at
6033 the beginning of the buffer."
6034   (interactive "P")
6035   (let ((article (gnus-summary-article-number))
6036         (article-window (get-buffer-window gnus-article-buffer t))
6037         endp)
6038     (gnus-configure-windows 'article)
6039     (if (or (null gnus-current-article)
6040             (null gnus-article-current)
6041             (/= article (cdr gnus-article-current))
6042             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6043         ;; Selected subject is different from current article's.
6044         (gnus-summary-display-article article)
6045       (gnus-summary-recenter)
6046       (when article-window
6047         (gnus-eval-in-buffer-window gnus-article-buffer
6048           (setq endp (gnus-article-prev-page lines)))
6049         (when (and move endp)
6050           (cond (lines
6051                  (gnus-message 3 "Beginning of message"))
6052                 ((null lines)
6053                  (if (and (eq gnus-summary-goto-unread 'never)
6054                           (not (gnus-summary-first-article-p article)))
6055                      (gnus-summary-prev-article)
6056                    (gnus-summary-prev-unread-article))))))))
6057   (gnus-summary-position-point))
6058
6059 (defun gnus-summary-prev-page-or-article (&optional lines)
6060   "Show previous page of selected article.
6061 Argument LINES specifies lines to be scrolled down.
6062 If at the beginning of the article, go to the next article."
6063   (interactive "P")
6064   (gnus-summary-prev-page lines t))
6065
6066 (defun gnus-summary-scroll-up (lines)
6067   "Scroll up (or down) one line current article.
6068 Argument LINES specifies lines to be scrolled up (or down if negative)."
6069   (interactive "p")
6070   (gnus-configure-windows 'article)
6071   (gnus-summary-show-thread)
6072   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6073     (gnus-eval-in-buffer-window gnus-article-buffer
6074       (cond ((> lines 0)
6075              (when (gnus-article-next-page lines)
6076                (gnus-message 3 "End of message")))
6077             ((< lines 0)
6078              (gnus-article-prev-page (- lines))))))
6079   (gnus-summary-recenter)
6080   (gnus-summary-position-point))
6081
6082 (defun gnus-summary-scroll-down (lines)
6083   "Scroll down (or up) one line current article.
6084 Argument LINES specifies lines to be scrolled down (or up if negative)."
6085   (interactive "p")
6086   (gnus-summary-scroll-up (- lines)))
6087
6088 (defun gnus-summary-next-same-subject ()
6089   "Select next article which has the same subject as current one."
6090   (interactive)
6091   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6092
6093 (defun gnus-summary-prev-same-subject ()
6094   "Select previous article which has the same subject as current one."
6095   (interactive)
6096   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6097
6098 (defun gnus-summary-next-unread-same-subject ()
6099   "Select next unread article which has the same subject as current one."
6100   (interactive)
6101   (gnus-summary-next-article t (gnus-summary-article-subject)))
6102
6103 (defun gnus-summary-prev-unread-same-subject ()
6104   "Select previous unread article which has the same subject as current one."
6105   (interactive)
6106   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6107
6108 (defun gnus-summary-first-unread-article ()
6109   "Select the first unread article.
6110 Return nil if there are no unread articles."
6111   (interactive)
6112   (prog1
6113       (when (gnus-summary-first-subject t)
6114         (gnus-summary-show-thread)
6115         (gnus-summary-first-subject t)
6116         (gnus-summary-display-article (gnus-summary-article-number)))
6117     (gnus-summary-position-point)))
6118
6119 (defun gnus-summary-first-unread-subject ()
6120   "Place the point on the subject line of the first unread article.
6121 Return nil if there are no unread articles."
6122   (interactive)
6123   (prog1
6124       (when (gnus-summary-first-subject t)
6125         (gnus-summary-show-thread)
6126         (gnus-summary-first-subject t))
6127     (gnus-summary-position-point)))
6128
6129 (defun gnus-summary-first-article ()
6130   "Select the first article.
6131 Return nil if there are no articles."
6132   (interactive)
6133   (prog1
6134       (when (gnus-summary-first-subject)
6135         (gnus-summary-show-thread)
6136         (gnus-summary-first-subject)
6137         (gnus-summary-display-article (gnus-summary-article-number)))
6138     (gnus-summary-position-point)))
6139
6140 (defun gnus-summary-best-unread-article ()
6141   "Select the unread article with the highest score."
6142   (interactive)
6143   (let ((best -1000000)
6144         (data gnus-newsgroup-data)
6145         article score)
6146     (while data
6147       (and (gnus-data-unread-p (car data))
6148            (> (setq score
6149                     (gnus-summary-article-score (gnus-data-number (car data))))
6150               best)
6151            (setq best score
6152                  article (gnus-data-number (car data))))
6153       (setq data (cdr data)))
6154     (prog1
6155         (if article
6156             (gnus-summary-goto-article article)
6157           (error "No unread articles"))
6158       (gnus-summary-position-point))))
6159
6160 (defun gnus-summary-last-subject ()
6161   "Go to the last displayed subject line in the group."
6162   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6163     (when article
6164       (gnus-summary-goto-subject article))))
6165
6166 (defun gnus-summary-goto-article (article &optional all-headers force)
6167   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6168 If ALL-HEADERS is non-nil, no header lines are hidden.
6169 If FORCE, go to the article even if it isn't displayed.  If FORCE
6170 is a number, it is the line the article is to be displayed on."
6171   (interactive
6172    (list
6173     (completing-read
6174      "Article number or Message-ID: "
6175      (mapcar (lambda (number) (list (int-to-string number)))
6176              gnus-newsgroup-limit))
6177     current-prefix-arg
6178     t))
6179   (prog1
6180       (if (and (stringp article)
6181                (string-match "@" article))
6182           (gnus-summary-refer-article article)
6183         (when (stringp article)
6184           (setq article (string-to-number article)))
6185         (if (gnus-summary-goto-subject article force)
6186             (gnus-summary-display-article article all-headers)
6187           (gnus-message 4 "Couldn't go to article %s" article) nil))
6188     (gnus-summary-position-point)))
6189
6190 (defun gnus-summary-goto-last-article ()
6191   "Go to the previously read article."
6192   (interactive)
6193   (prog1
6194       (when gnus-last-article
6195         (gnus-summary-goto-article gnus-last-article nil t))
6196     (gnus-summary-position-point)))
6197
6198 (defun gnus-summary-pop-article (number)
6199   "Pop one article off the history and go to the previous.
6200 NUMBER articles will be popped off."
6201   (interactive "p")
6202   (let (to)
6203     (setq gnus-newsgroup-history
6204           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6205     (if to
6206         (gnus-summary-goto-article (car to) nil t)
6207       (error "Article history empty")))
6208   (gnus-summary-position-point))
6209
6210 ;; Summary commands and functions for limiting the summary buffer.
6211
6212 (defun gnus-summary-limit-to-articles (n)
6213   "Limit the summary buffer to the next N articles.
6214 If not given a prefix, use the process marked articles instead."
6215   (interactive "P")
6216   (prog1
6217       (let ((articles (gnus-summary-work-articles n)))
6218         (setq gnus-newsgroup-processable nil)
6219         (gnus-summary-limit articles))
6220     (gnus-summary-position-point)))
6221
6222 (defun gnus-summary-pop-limit (&optional total)
6223   "Restore the previous limit.
6224 If given a prefix, remove all limits."
6225   (interactive "P")
6226   (when total
6227     (setq gnus-newsgroup-limits
6228           (list (mapcar (lambda (h) (mail-header-number h))
6229                         gnus-newsgroup-headers))))
6230   (unless gnus-newsgroup-limits
6231     (error "No limit to pop"))
6232   (prog1
6233       (gnus-summary-limit nil 'pop)
6234     (gnus-summary-position-point)))
6235
6236 (defun gnus-summary-limit-to-subject (subject &optional header)
6237   "Limit the summary buffer to articles that have subjects that match a regexp."
6238   (interactive "sLimit to subject (regexp): ")
6239   (unless header
6240     (setq header "subject"))
6241   (when (not (equal "" subject))
6242     (prog1
6243         (let ((articles (gnus-summary-find-matching
6244                          (or header "subject") subject 'all)))
6245           (unless articles
6246             (error "Found no matches for \"%s\"" subject))
6247           (gnus-summary-limit articles))
6248       (gnus-summary-position-point))))
6249
6250 (defun gnus-summary-limit-to-author (from)
6251   "Limit the summary buffer to articles that have authors that match a regexp."
6252   (interactive "sLimit to author (regexp): ")
6253   (gnus-summary-limit-to-subject from "from"))
6254
6255 (defun gnus-summary-limit-to-age (age &optional younger-p)
6256   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6257 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6258 articles that are younger than AGE days."
6259   (interactive
6260    (let ((younger current-prefix-arg)
6261          (days-got nil)
6262          days)
6263      (while (not days-got)
6264        (setq days (if younger
6265                       (read-string "Limit to articles within (in days): ")
6266                     (read-string "Limit to articles old than (in days): ")))
6267        (when (> (length days) 0)
6268          (setq days (read days)))
6269        (if (numberp days)
6270            (setq days-got t)
6271          (message "Please enter a number.")
6272          (sleep-for 1)))
6273      (list days younger)))
6274   (prog1
6275       (let ((data gnus-newsgroup-data)
6276             (cutoff (days-to-time age))
6277             articles d date is-younger)
6278         (while (setq d (pop data))
6279           (when (and (vectorp (gnus-data-header d))
6280                      (setq date (mail-header-date (gnus-data-header d))))
6281             (setq is-younger (time-less-p
6282                               (time-since (condition-case ()
6283                                               (date-to-time date)
6284                                             (error '(0 0))))
6285                               cutoff))
6286             (when (if younger-p
6287                       is-younger
6288                     (not is-younger))
6289               (push (gnus-data-number d) articles))))
6290         (gnus-summary-limit (nreverse articles)))
6291     (gnus-summary-position-point)))
6292
6293 (defun gnus-summary-limit-to-extra (header regexp)
6294   "Limit the summary buffer to articles that match an 'extra' header."
6295   (interactive
6296    (let ((header
6297           (intern
6298            (gnus-completing-read
6299             (symbol-name (car gnus-extra-headers))      
6300             "Limit extra header:"       
6301             (mapcar (lambda (x) 
6302                       (cons (symbol-name x) x))
6303                     gnus-extra-headers)
6304             nil                 
6305             t))))
6306      (list header
6307            (read-string (format "Limit to header %s (regexp): " header)))))
6308   (when (not (equal "" regexp))
6309     (prog1
6310         (let ((articles (gnus-summary-find-matching
6311                          (cons 'extra header) regexp 'all)))
6312           (unless articles
6313             (error "Found no matches for \"%s\"" regexp))
6314           (gnus-summary-limit articles))
6315       (gnus-summary-position-point))))
6316
6317 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6318 (make-obsolete
6319  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6320
6321 (defun gnus-summary-limit-to-unread (&optional all)
6322   "Limit the summary buffer to articles that are not marked as read.
6323 If ALL is non-nil, limit strictly to unread articles."
6324   (interactive "P")
6325   (if all
6326       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6327     (gnus-summary-limit-to-marks
6328      ;; Concat all the marks that say that an article is read and have
6329      ;; those removed.
6330      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6331            gnus-killed-mark gnus-kill-file-mark
6332            gnus-low-score-mark gnus-expirable-mark
6333            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6334            gnus-duplicate-mark gnus-souped-mark)
6335      'reverse)))
6336
6337 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6338 (make-obsolete 'gnus-summary-delete-marked-with
6339                'gnus-summary-limit-exlude-marks)
6340
6341 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6342   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6343 If REVERSE, limit the summary buffer to articles that are marked
6344 with MARKS.  MARKS can either be a string of marks or a list of marks.
6345 Returns how many articles were removed."
6346   (interactive "sMarks: ")
6347   (gnus-summary-limit-to-marks marks t))
6348
6349 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6350   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6351 If REVERSE (the prefix), limit the summary buffer to articles that are
6352 not marked with MARKS.  MARKS can either be a string of marks or a
6353 list of marks.
6354 Returns how many articles were removed."
6355   (interactive "sMarks: \nP")
6356   (prog1
6357       (let ((data gnus-newsgroup-data)
6358             (marks (if (listp marks) marks
6359                      (append marks nil))) ; Transform to list.
6360             articles)
6361         (while data
6362           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6363                   (memq (gnus-data-mark (car data)) marks))
6364             (push (gnus-data-number (car data)) articles))
6365           (setq data (cdr data)))
6366         (gnus-summary-limit articles))
6367     (gnus-summary-position-point)))
6368
6369 (defun gnus-summary-limit-to-score (&optional score)
6370   "Limit to articles with score at or above SCORE."
6371   (interactive "P")
6372   (setq score (if score
6373                   (prefix-numeric-value score)
6374                 (or gnus-summary-default-score 0)))
6375   (let ((data gnus-newsgroup-data)
6376         articles)
6377     (while data
6378       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6379                 score)
6380         (push (gnus-data-number (car data)) articles))
6381       (setq data (cdr data)))
6382     (prog1
6383         (gnus-summary-limit articles)
6384       (gnus-summary-position-point))))
6385
6386 (defun gnus-summary-limit-include-thread (id)
6387   "Display all the hidden articles that in the current thread."
6388   (interactive (list (mail-header-id (gnus-summary-article-header))))
6389   (let ((articles (gnus-articles-in-thread
6390                    (gnus-id-to-thread (gnus-root-id id)))))
6391     (prog1
6392         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6393       (gnus-summary-position-point))))
6394
6395 (defun gnus-summary-limit-include-dormant ()
6396   "Display all the hidden articles that are marked as dormant.
6397 Note that this command only works on a subset of the articles currently
6398 fetched for this group."
6399   (interactive)
6400   (unless gnus-newsgroup-dormant
6401     (error "There are no dormant articles in this group"))
6402   (prog1
6403       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6404     (gnus-summary-position-point)))
6405
6406 (defun gnus-summary-limit-exclude-dormant ()
6407   "Hide all dormant articles."
6408   (interactive)
6409   (prog1
6410       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6411     (gnus-summary-position-point)))
6412
6413 (defun gnus-summary-limit-exclude-childless-dormant ()
6414   "Hide all dormant articles that have no children."
6415   (interactive)
6416   (let ((data (gnus-data-list t))
6417         articles d children)
6418     ;; Find all articles that are either not dormant or have
6419     ;; children.
6420     (while (setq d (pop data))
6421       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6422                 (and (setq children
6423                            (gnus-article-children (gnus-data-number d)))
6424                      (let (found)
6425                        (while children
6426                          (when (memq (car children) articles)
6427                            (setq children nil
6428                                  found t))
6429                          (pop children))
6430                        found)))
6431         (push (gnus-data-number d) articles)))
6432     ;; Do the limiting.
6433     (prog1
6434         (gnus-summary-limit articles)
6435       (gnus-summary-position-point))))
6436
6437 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6438   "Mark all unread excluded articles as read.
6439 If ALL, mark even excluded ticked and dormants as read."
6440   (interactive "P")
6441   (let ((articles (gnus-sorted-complement
6442                    (sort
6443                     (mapcar (lambda (h) (mail-header-number h))
6444                             gnus-newsgroup-headers)
6445                     '<)
6446                    (sort gnus-newsgroup-limit '<)))
6447         article)
6448     (setq gnus-newsgroup-unreads
6449           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6450     (if all
6451         (setq gnus-newsgroup-dormant nil
6452               gnus-newsgroup-marked nil
6453               gnus-newsgroup-reads
6454               (nconc
6455                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6456                gnus-newsgroup-reads))
6457       (while (setq article (pop articles))
6458         (unless (or (memq article gnus-newsgroup-dormant)
6459                     (memq article gnus-newsgroup-marked))
6460           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6461
6462 (defun gnus-summary-limit (articles &optional pop)
6463   (if pop
6464       ;; We pop the previous limit off the stack and use that.
6465       (setq articles (car gnus-newsgroup-limits)
6466             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6467     ;; We use the new limit, so we push the old limit on the stack.
6468     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6469   ;; Set the limit.
6470   (setq gnus-newsgroup-limit articles)
6471   (let ((total (length gnus-newsgroup-data))
6472         (data (gnus-data-find-list (gnus-summary-article-number)))
6473         (gnus-summary-mark-below nil)   ; Inhibit this.
6474         found)
6475     ;; This will do all the work of generating the new summary buffer
6476     ;; according to the new limit.
6477     (gnus-summary-prepare)
6478     ;; Hide any threads, possibly.
6479     (and gnus-show-threads
6480          gnus-thread-hide-subtree
6481          (gnus-summary-hide-all-threads))
6482     ;; Try to return to the article you were at, or one in the
6483     ;; neighborhood.
6484     (when data
6485       ;; We try to find some article after the current one.
6486       (while data
6487         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6488           (setq data nil
6489                 found t))
6490         (setq data (cdr data))))
6491     (unless found
6492       ;; If there is no data, that means that we were after the last
6493       ;; article.  The same goes when we can't find any articles
6494       ;; after the current one.
6495       (goto-char (point-max))
6496       (gnus-summary-find-prev))
6497     (gnus-set-mode-line 'summary)
6498     ;; We return how many articles were removed from the summary
6499     ;; buffer as a result of the new limit.
6500     (- total (length gnus-newsgroup-data))))
6501
6502 (defsubst gnus-invisible-cut-children (threads)
6503   (let ((num 0))
6504     (while threads
6505       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6506         (incf num))
6507       (pop threads))
6508     (< num 2)))
6509
6510 (defsubst gnus-cut-thread (thread)
6511   "Go forwards in the thread until we find an article that we want to display."
6512   (when (or (eq gnus-fetch-old-headers 'some)
6513             (eq gnus-fetch-old-headers 'invisible)
6514             (numberp gnus-fetch-old-headers)
6515             (eq gnus-build-sparse-threads 'some)
6516             (eq gnus-build-sparse-threads 'more))
6517     ;; Deal with old-fetched headers and sparse threads.
6518     (while (and
6519             thread
6520             (or
6521              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6522              (gnus-summary-article-ancient-p
6523               (mail-header-number (car thread))))
6524             (if (or (<= (length (cdr thread)) 1)
6525                     (eq gnus-fetch-old-headers 'invisible))
6526                 (setq gnus-newsgroup-limit
6527                       (delq (mail-header-number (car thread))
6528                             gnus-newsgroup-limit)
6529                       thread (cadr thread))
6530               (when (gnus-invisible-cut-children (cdr thread))
6531                 (let ((th (cdr thread)))
6532                   (while th
6533                     (if (memq (mail-header-number (caar th))
6534                               gnus-newsgroup-limit)
6535                         (setq thread (car th)
6536                               th nil)
6537                       (setq th (cdr th))))))))))
6538   thread)
6539
6540 (defun gnus-cut-threads (threads)
6541   "Cut off all uninteresting articles from the beginning of threads."
6542   (when (or (eq gnus-fetch-old-headers 'some)
6543             (eq gnus-fetch-old-headers 'invisible)
6544             (numberp gnus-fetch-old-headers)
6545             (eq gnus-build-sparse-threads 'some)
6546             (eq gnus-build-sparse-threads 'more))
6547     (let ((th threads))
6548       (while th
6549         (setcar th (gnus-cut-thread (car th)))
6550         (setq th (cdr th)))))
6551   ;; Remove nixed out threads.
6552   (delq nil threads))
6553
6554 (defun gnus-summary-initial-limit (&optional show-if-empty)
6555   "Figure out what the initial limit is supposed to be on group entry.
6556 This entails weeding out unwanted dormants, low-scored articles,
6557 fetch-old-headers verbiage, and so on."
6558   ;; Most groups have nothing to remove.
6559   (if (or gnus-inhibit-limiting
6560           (and (null gnus-newsgroup-dormant)
6561                (not (eq gnus-fetch-old-headers 'some))
6562                (not (numberp gnus-fetch-old-headers))
6563                (not (eq gnus-fetch-old-headers 'invisible))
6564                (null gnus-summary-expunge-below)
6565                (not (eq gnus-build-sparse-threads 'some))
6566                (not (eq gnus-build-sparse-threads 'more))
6567                (null gnus-thread-expunge-below)
6568                (not gnus-use-nocem)))
6569       ()                                ; Do nothing.
6570     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6571     (setq gnus-newsgroup-limit nil)
6572     (mapatoms
6573      (lambda (node)
6574        (unless (car (symbol-value node))
6575          ;; These threads have no parents -- they are roots.
6576          (let ((nodes (cdr (symbol-value node)))
6577                thread)
6578            (while nodes
6579              (if (and gnus-thread-expunge-below
6580                       (< (gnus-thread-total-score (car nodes))
6581                          gnus-thread-expunge-below))
6582                  (gnus-expunge-thread (pop nodes))
6583                (setq thread (pop nodes))
6584                (gnus-summary-limit-children thread))))))
6585      gnus-newsgroup-dependencies)
6586     ;; If this limitation resulted in an empty group, we might
6587     ;; pop the previous limit and use it instead.
6588     (when (and (not gnus-newsgroup-limit)
6589                show-if-empty)
6590       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6591     gnus-newsgroup-limit))
6592
6593 (defun gnus-summary-limit-children (thread)
6594   "Return 1 if this subthread is visible and 0 if it is not."
6595   ;; First we get the number of visible children to this thread.  This
6596   ;; is done by recursing down the thread using this function, so this
6597   ;; will really go down to a leaf article first, before slowly
6598   ;; working its way up towards the root.
6599   (when thread
6600     (let ((children
6601            (if (cdr thread)
6602                (apply '+ (mapcar 'gnus-summary-limit-children
6603                                  (cdr thread)))
6604              0))
6605           (number (mail-header-number (car thread)))
6606           score)
6607       (if (and
6608            (not (memq number gnus-newsgroup-marked))
6609            (or
6610             ;; If this article is dormant and has absolutely no visible
6611             ;; children, then this article isn't visible.
6612             (and (memq number gnus-newsgroup-dormant)
6613                  (zerop children))
6614             ;; If this is "fetch-old-headered" and there is no
6615             ;; visible children, then we don't want this article.
6616             (and (or (eq gnus-fetch-old-headers 'some)
6617                      (numberp gnus-fetch-old-headers))
6618                  (gnus-summary-article-ancient-p number)
6619                  (zerop children))
6620             ;; If this is "fetch-old-headered" and `invisible', then
6621             ;; we don't want this article.
6622             (and (eq gnus-fetch-old-headers 'invisible)
6623                  (gnus-summary-article-ancient-p number))
6624             ;; If this is a sparsely inserted article with no children,
6625             ;; we don't want it.
6626             (and (eq gnus-build-sparse-threads 'some)
6627                  (gnus-summary-article-sparse-p number)
6628                  (zerop children))
6629             ;; If we use expunging, and this article is really
6630             ;; low-scored, then we don't want this article.
6631             (when (and gnus-summary-expunge-below
6632                        (< (setq score
6633                                 (or (cdr (assq number gnus-newsgroup-scored))
6634                                     gnus-summary-default-score))
6635                           gnus-summary-expunge-below))
6636               ;; We increase the expunge-tally here, but that has
6637               ;; nothing to do with the limits, really.
6638               (incf gnus-newsgroup-expunged-tally)
6639               ;; We also mark as read here, if that's wanted.
6640               (when (and gnus-summary-mark-below
6641                          (< score gnus-summary-mark-below))
6642                 (setq gnus-newsgroup-unreads
6643                       (delq number gnus-newsgroup-unreads))
6644                 (if gnus-newsgroup-auto-expire
6645                     (push number gnus-newsgroup-expirable)
6646                   (push (cons number gnus-low-score-mark)
6647                         gnus-newsgroup-reads)))
6648               t)
6649             ;; Check NoCeM things.
6650             (if (and gnus-use-nocem
6651                      (gnus-nocem-unwanted-article-p
6652                       (mail-header-id (car thread))))
6653                 (progn
6654                   (setq gnus-newsgroup-unreads
6655                         (delq number gnus-newsgroup-unreads))
6656                   t))))
6657           ;; Nope, invisible article.
6658           0
6659         ;; Ok, this article is to be visible, so we add it to the limit
6660         ;; and return 1.
6661         (push number gnus-newsgroup-limit)
6662         1))))
6663
6664 (defun gnus-expunge-thread (thread)
6665   "Mark all articles in THREAD as read."
6666   (let* ((number (mail-header-number (car thread))))
6667     (incf gnus-newsgroup-expunged-tally)
6668     ;; We also mark as read here, if that's wanted.
6669     (setq gnus-newsgroup-unreads
6670           (delq number gnus-newsgroup-unreads))
6671     (if gnus-newsgroup-auto-expire
6672         (push number gnus-newsgroup-expirable)
6673       (push (cons number gnus-low-score-mark)
6674             gnus-newsgroup-reads)))
6675   ;; Go recursively through all subthreads.
6676   (mapcar 'gnus-expunge-thread (cdr thread)))
6677
6678 ;; Summary article oriented commands
6679
6680 (defun gnus-summary-refer-parent-article (n)
6681   "Refer parent article N times.
6682 If N is negative, go to ancestor -N instead.
6683 The difference between N and the number of articles fetched is returned."
6684   (interactive "p")
6685   (let ((skip 1)
6686         error header ref)
6687     (when (not (natnump n))
6688       (setq skip (abs n)
6689             n 1))
6690     (while (and (> n 0)
6691                 (not error))
6692       (setq header (gnus-summary-article-header))
6693       (if (and (eq (mail-header-number header)
6694                    (cdr gnus-article-current))
6695                (equal gnus-newsgroup-name
6696                       (car gnus-article-current)))
6697           ;; If we try to find the parent of the currently
6698           ;; displayed article, then we take a look at the actual
6699           ;; References header, since this is slightly more
6700           ;; reliable than the References field we got from the
6701           ;; server.
6702           (save-excursion
6703             (set-buffer gnus-original-article-buffer)
6704             (nnheader-narrow-to-headers)
6705             (unless (setq ref (message-fetch-field "references"))
6706               (setq ref (message-fetch-field "in-reply-to")))
6707             (widen))
6708         (setq ref
6709               ;; It's not the current article, so we take a bet on
6710               ;; the value we got from the server.
6711               (mail-header-references header)))
6712       (if (and ref
6713                (not (equal ref "")))
6714           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6715             (gnus-message 1 "Couldn't find parent"))
6716         (gnus-message 1 "No references in article %d"
6717                       (gnus-summary-article-number))
6718         (setq error t))
6719       (decf n))
6720     (gnus-summary-position-point)
6721     n))
6722
6723 (defun gnus-summary-refer-references ()
6724   "Fetch all articles mentioned in the References header.
6725 Return the number of articles fetched."
6726   (interactive)
6727   (let ((ref (mail-header-references (gnus-summary-article-header)))
6728         (current (gnus-summary-article-number))
6729         (n 0))
6730     (if (or (not ref)
6731             (equal ref ""))
6732         (error "No References in the current article")
6733       ;; For each Message-ID in the References header...
6734       (while (string-match "<[^>]*>" ref)
6735         (incf n)
6736         ;; ... fetch that article.
6737         (gnus-summary-refer-article
6738          (prog1 (match-string 0 ref)
6739            (setq ref (substring ref (match-end 0))))))
6740       (gnus-summary-goto-subject current)
6741       (gnus-summary-position-point)
6742       n)))
6743
6744 (defun gnus-summary-refer-thread (&optional limit)
6745   "Fetch all articles in the current thread.
6746 If LIMIT (the numerical prefix), fetch that many old headers instead
6747 of what's specified by the `gnus-refer-thread-limit' variable."
6748   (interactive "P")
6749   (let ((id (mail-header-id (gnus-summary-article-header)))
6750         (limit (if limit (prefix-numeric-value limit)
6751                  gnus-refer-thread-limit)))
6752     ;; We want to fetch LIMIT *old* headers, but we also have to
6753     ;; re-fetch all the headers in the current buffer, because many of
6754     ;; them may be undisplayed.  So we adjust LIMIT.
6755     (when (numberp limit)
6756       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6757     (unless (eq gnus-fetch-old-headers 'invisible)
6758       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6759       ;; Retrieve the headers and read them in.
6760       (if (eq (gnus-retrieve-headers
6761                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6762               'nov)
6763           (gnus-build-all-threads)
6764         (error "Can't fetch thread from backends that don't support NOV"))
6765       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6766     (gnus-summary-limit-include-thread id)))
6767
6768 (defun gnus-summary-refer-article (message-id)
6769   "Fetch an article specified by MESSAGE-ID."
6770   (interactive "sMessage-ID: ")
6771   (when (and (stringp message-id)
6772              (not (zerop (length message-id))))
6773     ;; Construct the correct Message-ID if necessary.
6774     ;; Suggested by tale@pawl.rpi.edu.
6775     (unless (string-match "^<" message-id)
6776       (setq message-id (concat "<" message-id)))
6777     (unless (string-match ">$" message-id)
6778       (setq message-id (concat message-id ">")))
6779     (let* ((header (gnus-id-to-header message-id))
6780            (sparse (and header
6781                         (gnus-summary-article-sparse-p
6782                          (mail-header-number header))
6783                         (memq (mail-header-number header)
6784                               gnus-newsgroup-limit)))
6785            number)
6786       (cond
6787        ;; If the article is present in the buffer we just go to it.
6788        ((and header
6789              (or (not (gnus-summary-article-sparse-p
6790                        (mail-header-number header)))
6791                  sparse))
6792         (prog1
6793             (gnus-summary-goto-article
6794              (mail-header-number header) nil t)
6795           (when sparse
6796             (gnus-summary-update-article (mail-header-number header)))))
6797        (t
6798         ;; We fetch the article.
6799         (catch 'found
6800           (dolist (gnus-override-method (gnus-refer-article-methods))
6801             (gnus-check-server gnus-override-method)
6802             ;; Fetch the header, and display the article.
6803             (when (setq number (gnus-summary-insert-subject message-id))
6804               (gnus-summary-select-article nil nil nil number)
6805               (throw 'found t)))
6806           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6807
6808 (defun gnus-refer-article-methods ()
6809   "Return a list of referrable methods."
6810   (cond
6811    ;; No method, so we default to current and native.
6812    ((null gnus-refer-article-method)
6813     (list gnus-current-select-method gnus-select-method))
6814    ;; Current.
6815    ((eq 'current gnus-refer-article-method)
6816     (list gnus-current-select-method))
6817    ;; List of select methods.
6818    ((not (stringp (cadr gnus-refer-article-method)))
6819     (let (out)
6820       (dolist (method gnus-refer-article-method)
6821         (push (if (eq 'current method)
6822                   gnus-current-select-method
6823                 method)
6824               out))
6825       (nreverse out)))
6826    ;; One single select method.
6827    (t
6828     (list gnus-refer-article-method))))
6829
6830 (defun gnus-summary-edit-parameters ()
6831   "Edit the group parameters of the current group."
6832   (interactive)
6833   (gnus-group-edit-group gnus-newsgroup-name 'params))
6834
6835 (defun gnus-summary-customize-parameters ()
6836   "Customize the group parameters of the current group."
6837   (interactive)
6838   (gnus-group-customize gnus-newsgroup-name))
6839
6840 (defun gnus-summary-enter-digest-group (&optional force)
6841   "Enter an nndoc group based on the current article.
6842 If FORCE, force a digest interpretation.  If not, try
6843 to guess what the document format is."
6844   (interactive "P")
6845   (let ((conf gnus-current-window-configuration))
6846     (save-excursion
6847       (gnus-summary-select-article))
6848     (setq gnus-current-window-configuration conf)
6849     (let* ((name (format "%s-%d"
6850                          (gnus-group-prefixed-name
6851                           gnus-newsgroup-name (list 'nndoc ""))
6852                          (save-excursion
6853                            (set-buffer gnus-summary-buffer)
6854                            gnus-current-article)))
6855            (ogroup gnus-newsgroup-name)
6856            (params (append (gnus-info-params (gnus-get-info ogroup))
6857                            (list (cons 'to-group ogroup))
6858                            (list (cons 'save-article-group ogroup))))
6859            (case-fold-search t)
6860            (buf (current-buffer))
6861            dig to-address)
6862       (save-excursion
6863         (set-buffer gnus-original-article-buffer)
6864         ;; Have the digest group inherit the main mail address of
6865         ;; the parent article.
6866         (when (setq to-address (or (message-fetch-field "reply-to")
6867                                    (message-fetch-field "from")))
6868           (setq params (append (list (cons 'to-address to-address)))))
6869         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6870         (insert-buffer-substring gnus-original-article-buffer)
6871         ;; Remove lines that may lead nndoc to misinterpret the
6872         ;; document type.
6873         (narrow-to-region
6874          (goto-char (point-min))
6875          (or (search-forward "\n\n" nil t) (point)))
6876         (goto-char (point-min))
6877         (delete-matching-lines "^Path:\\|^From ")
6878         (widen))
6879       (unwind-protect
6880           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6881                     (gnus-newsgroup-ephemeral-ignored-charsets
6882                      gnus-newsgroup-ignored-charsets))
6883                 (gnus-group-read-ephemeral-group
6884                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6885                               (nndoc-article-type
6886                                ,(if force 'mbox 'guess))) t))
6887               ;; Make all postings to this group go to the parent group.
6888               (nconc (gnus-info-params (gnus-get-info name))
6889                      params)
6890             ;; Couldn't select this doc group.
6891             (switch-to-buffer buf)
6892             (gnus-set-global-variables)
6893             (gnus-configure-windows 'summary)
6894             (gnus-message 3 "Article couldn't be entered?"))
6895         (kill-buffer dig)))))
6896
6897 (defun gnus-summary-read-document (n)
6898   "Open a new group based on the current article(s).
6899 This will allow you to read digests and other similar
6900 documents as newsgroups.
6901 Obeys the standard process/prefix convention."
6902   (interactive "P")
6903   (let* ((articles (gnus-summary-work-articles n))
6904          (ogroup gnus-newsgroup-name)
6905          (params (append (gnus-info-params (gnus-get-info ogroup))
6906                          (list (cons 'to-group ogroup))))
6907          article group egroup groups vgroup)
6908     (while (setq article (pop articles))
6909       (setq group (format "%s-%d" gnus-newsgroup-name article))
6910       (gnus-summary-remove-process-mark article)
6911       (when (gnus-summary-display-article article)
6912         (save-excursion
6913           (with-temp-buffer
6914             (insert-buffer-substring gnus-original-article-buffer)
6915             ;; Remove some headers that may lead nndoc to make
6916             ;; the wrong guess.
6917             (message-narrow-to-head)
6918             (goto-char (point-min))
6919             (delete-matching-lines "^\\(Path\\):\\|^From ")
6920             (widen)
6921             (if (setq egroup
6922                       (gnus-group-read-ephemeral-group
6923                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6924                                      (nndoc-article-type guess))
6925                        t nil t))
6926                 (progn
6927                   ;; Make all postings to this group go to the parent group.
6928                   (nconc (gnus-info-params (gnus-get-info egroup))
6929                          params)
6930                   (push egroup groups))
6931               ;; Couldn't select this doc group.
6932               (gnus-error 3 "Article couldn't be entered"))))))
6933     ;; Now we have selected all the documents.
6934     (cond
6935      ((not groups)
6936       (error "None of the articles could be interpreted as documents"))
6937      ((gnus-group-read-ephemeral-group
6938        (setq vgroup (format
6939                      "nnvirtual:%s-%s" gnus-newsgroup-name
6940                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6941        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6942        t
6943        (cons (current-buffer) 'summary)))
6944      (t
6945       (error "Couldn't select virtual nndoc group")))))
6946
6947 (defun gnus-summary-isearch-article (&optional regexp-p)
6948   "Do incremental search forward on the current article.
6949 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6950   (interactive "P")
6951   (gnus-summary-select-article)
6952   (gnus-configure-windows 'article)
6953   (gnus-eval-in-buffer-window gnus-article-buffer
6954     (save-restriction
6955       (widen)
6956       (isearch-forward regexp-p))))
6957
6958 (defun gnus-summary-search-article-forward (regexp &optional backward)
6959   "Search for an article containing REGEXP forward.
6960 If BACKWARD, search backward instead."
6961   (interactive
6962    (list (read-string
6963           (format "Search article %s (regexp%s): "
6964                   (if current-prefix-arg "backward" "forward")
6965                   (if gnus-last-search-regexp
6966                       (concat ", default " gnus-last-search-regexp)
6967                     "")))
6968          current-prefix-arg))
6969   (if (string-equal regexp "")
6970       (setq regexp (or gnus-last-search-regexp ""))
6971     (setq gnus-last-search-regexp regexp))
6972   (if (gnus-summary-search-article regexp backward)
6973       (gnus-summary-show-thread)
6974     (error "Search failed: \"%s\"" regexp)))
6975
6976 (defun gnus-summary-search-article-backward (regexp)
6977   "Search for an article containing REGEXP backward."
6978   (interactive
6979    (list (read-string
6980           (format "Search article backward (regexp%s): "
6981                   (if gnus-last-search-regexp
6982                       (concat ", default " gnus-last-search-regexp)
6983                     "")))))
6984   (gnus-summary-search-article-forward regexp 'backward))
6985
6986 (defun gnus-summary-search-article (regexp &optional backward)
6987   "Search for an article containing REGEXP.
6988 Optional argument BACKWARD means do search for backward.
6989 `gnus-select-article-hook' is not called during the search."
6990   ;; We have to require this here to make sure that the following
6991   ;; dynamic binding isn't shadowed by autoloading.
6992   (require 'gnus-async)
6993   (require 'gnus-art)
6994   (let ((gnus-select-article-hook nil)  ;Disable hook.
6995         (gnus-article-prepare-hook nil)
6996         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6997         (gnus-use-article-prefetch nil)
6998         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6999         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7000         (sum (current-buffer))
7001         (gnus-display-mime-function nil)
7002         (found nil)
7003         point)
7004     (gnus-save-hidden-threads
7005       (gnus-summary-select-article)
7006       (set-buffer gnus-article-buffer)
7007       (goto-char (window-point (get-buffer-window (current-buffer))))
7008       (when backward
7009         (forward-line -1))
7010       (while (not found)
7011         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7012         (if (if backward
7013                 (re-search-backward regexp nil t)
7014               (re-search-forward regexp nil t))
7015             ;; We found the regexp.
7016             (progn
7017               (setq found 'found)
7018               (beginning-of-line)
7019               (set-window-start
7020                (get-buffer-window (current-buffer))
7021                (point))
7022               (forward-line 1)
7023               (set-window-point
7024                (get-buffer-window (current-buffer))
7025                (point))
7026               (set-buffer sum)
7027               (setq point (point)))
7028           ;; We didn't find it, so we go to the next article.
7029           (set-buffer sum)
7030           (setq found 'not)
7031           (while (eq found 'not)
7032             (if (not (if backward (gnus-summary-find-prev)
7033                        (gnus-summary-find-next)))
7034                 ;; No more articles.
7035                 (setq found t)
7036               ;; Select the next article and adjust point.
7037               (unless (gnus-summary-article-sparse-p
7038                        (gnus-summary-article-number))
7039                 (setq found nil)
7040                 (gnus-summary-select-article)
7041                 (set-buffer gnus-article-buffer)
7042                 (widen)
7043                 (goto-char (if backward (point-max) (point-min))))))))
7044       (gnus-message 7 ""))
7045     ;; Return whether we found the regexp.
7046     (when (eq found 'found)
7047       (goto-char point)
7048       (gnus-summary-show-thread)
7049       (gnus-summary-goto-subject gnus-current-article)
7050       (gnus-summary-position-point)
7051       t)))
7052
7053 (defun gnus-summary-find-matching (header regexp &optional backward unread
7054                                           not-case-fold)
7055   "Return a list of all articles that match REGEXP on HEADER.
7056 The search stars on the current article and goes forwards unless
7057 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7058 If UNREAD is non-nil, only unread articles will
7059 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7060 in the comparisons."
7061   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7062                 (gnus-data-find-list
7063                  (gnus-summary-article-number) (gnus-data-list backward))))
7064         (case-fold-search (not not-case-fold))
7065         articles d func)
7066     (if (consp header)
7067         (if (eq (car header) 'extra)
7068             (setq func
7069                   `(lambda (h)
7070                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7071                          "")))
7072           (error "%s is an invalid header" header))
7073       (unless (fboundp (intern (concat "mail-header-" header)))
7074         (error "%s is not a valid header" header))
7075       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7076     (while data
7077       (setq d (car data))
7078       (and (or (not unread)             ; We want all articles...
7079                (gnus-data-unread-p d))  ; Or just unreads.
7080            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7081            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7082            (push (gnus-data-number d) articles)) ; Success!
7083       (setq data (cdr data)))
7084     (nreverse articles)))
7085
7086 (defun gnus-summary-execute-command (header regexp command &optional backward)
7087   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7088 If HEADER is an empty string (or nil), the match is done on the entire
7089 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7090   (interactive
7091    (list (let ((completion-ignore-case t))
7092            (completing-read
7093             "Header name: "
7094             (mapcar (lambda (string) (list string))
7095                     '("Number" "Subject" "From" "Lines" "Date"
7096                       "Message-ID" "Xref" "References" "Body"))
7097             nil 'require-match))
7098          (read-string "Regexp: ")
7099          (read-key-sequence "Command: ")
7100          current-prefix-arg))
7101   (when (equal header "Body")
7102     (setq header ""))
7103   ;; Hidden thread subtrees must be searched as well.
7104   (gnus-summary-show-all-threads)
7105   ;; We don't want to change current point nor window configuration.
7106   (save-excursion
7107     (save-window-excursion
7108       (gnus-message 6 "Executing %s..." (key-description command))
7109       ;; We'd like to execute COMMAND interactively so as to give arguments.
7110       (gnus-execute header regexp
7111                     `(call-interactively ',(key-binding command))
7112                     backward)
7113       (gnus-message 6 "Executing %s...done" (key-description command)))))
7114
7115 (defun gnus-summary-beginning-of-article ()
7116   "Scroll the article back to the beginning."
7117   (interactive)
7118   (gnus-summary-select-article)
7119   (gnus-configure-windows 'article)
7120   (gnus-eval-in-buffer-window gnus-article-buffer
7121     (widen)
7122     (goto-char (point-min))
7123     (when gnus-page-broken
7124       (gnus-narrow-to-page))))
7125
7126 (defun gnus-summary-end-of-article ()
7127   "Scroll to the end of the article."
7128   (interactive)
7129   (gnus-summary-select-article)
7130   (gnus-configure-windows 'article)
7131   (gnus-eval-in-buffer-window gnus-article-buffer
7132     (widen)
7133     (goto-char (point-max))
7134     (recenter -3)
7135     (when gnus-page-broken
7136       (gnus-narrow-to-page))))
7137
7138 (defun gnus-summary-print-article (&optional filename n)
7139   "Generate and print a PostScript image of the N next (mail) articles.
7140
7141 If N is negative, print the N previous articles.  If N is nil and articles
7142 have been marked with the process mark, print these instead.
7143
7144 If the optional first argument FILENAME is nil, send the image to the
7145 printer.  If FILENAME is a string, save the PostScript image in a file with
7146 that name.  If FILENAME is a number, prompt the user for the name of the file
7147 to save in."
7148   (interactive (list (ps-print-preprint current-prefix-arg)
7149                      current-prefix-arg))
7150   (dolist (article (gnus-summary-work-articles n))
7151     (gnus-summary-select-article nil nil 'pseudo article)
7152     (gnus-eval-in-buffer-window gnus-article-buffer
7153       (let ((buffer (generate-new-buffer " *print*")))
7154         (unwind-protect
7155             (progn
7156               (copy-to-buffer buffer (point-min) (point-max))
7157               (set-buffer buffer)
7158               (gnus-article-delete-invisible-text)
7159               (let ((ps-left-header
7160                      (list
7161                       (concat "("
7162                               (mail-header-subject gnus-current-headers) ")")
7163                       (concat "("
7164                               (mail-header-from gnus-current-headers) ")")))
7165                     (ps-right-header
7166                      (list
7167                       "/pagenumberstring load"
7168                       (concat "("
7169                               (mail-header-date gnus-current-headers) ")"))))
7170                 (gnus-run-hooks 'gnus-ps-print-hook)
7171                 (save-excursion
7172                   (ps-print-buffer-with-faces filename))))
7173           (kill-buffer buffer))))))
7174
7175 (defun gnus-summary-show-article (&optional arg)
7176   "Force re-fetching of the current article.
7177 If ARG (the prefix) is a number, show the article with the charset 
7178 defined in `gnus-summary-show-article-charset-alist', or the charset
7179 inputed.
7180 If ARG (the prefix) is non-nil and not a number, show the raw article 
7181 without any article massaging functions being run."
7182   (interactive "P")
7183   (cond 
7184    ((numberp arg)
7185     (let ((gnus-newsgroup-charset 
7186            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7187                (read-coding-system "Charset: ")))
7188           (gnus-newsgroup-ignored-charsets 'gnus-all))
7189       (gnus-summary-select-article nil 'force)))
7190    ((not arg)
7191     ;; Select the article the normal way.
7192     (gnus-summary-select-article nil 'force))
7193    (t
7194     ;; We have to require this here to make sure that the following
7195     ;; dynamic binding isn't shadowed by autoloading.
7196     (require 'gnus-async)
7197     (require 'gnus-art)
7198     ;; Bind the article treatment functions to nil.
7199     (let ((gnus-have-all-headers t)
7200           gnus-article-prepare-hook
7201           gnus-article-decode-hook
7202           gnus-display-mime-function
7203           gnus-break-pages)
7204       ;; Destroy any MIME parts.
7205       (when (gnus-buffer-live-p gnus-article-buffer)
7206         (save-excursion
7207           (set-buffer gnus-article-buffer)
7208           (mm-destroy-parts gnus-article-mime-handles)))
7209       (gnus-summary-select-article nil 'force))))
7210   (gnus-summary-goto-subject gnus-current-article)
7211   (gnus-summary-position-point))
7212
7213 (defun gnus-summary-verbose-headers (&optional arg)
7214   "Toggle permanent full header display.
7215 If ARG is a positive number, turn header display on.
7216 If ARG is a negative number, turn header display off."
7217   (interactive "P")
7218   (setq gnus-show-all-headers
7219         (cond ((or (not (numberp arg))
7220                    (zerop arg))
7221                (not gnus-show-all-headers))
7222               ((natnump arg)
7223                t)))
7224   (gnus-summary-show-article))
7225
7226 (defun gnus-summary-toggle-header (&optional arg)
7227   "Show the headers if they are hidden, or hide them if they are shown.
7228 If ARG is a positive number, show the entire header.
7229 If ARG is a negative number, hide the unwanted header lines."
7230   (interactive "P")
7231   (save-excursion
7232     (set-buffer gnus-article-buffer)
7233     (save-restriction
7234       (let* ((buffer-read-only nil)
7235              (inhibit-point-motion-hooks t)
7236              hidden e)
7237         (setq hidden
7238               (if (numberp arg)
7239                   (>= arg 0)
7240                 (save-restriction 
7241                   (article-narrow-to-head)
7242                   (gnus-article-hidden-text-p 'headers))))
7243         (goto-char (point-min))
7244         (when (search-forward "\n\n" nil t)
7245           (delete-region (point-min) (1- (point))))
7246         (goto-char (point-min))
7247         (save-excursion
7248           (set-buffer gnus-original-article-buffer)
7249           (goto-char (point-min))
7250           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7251         (insert-buffer-substring gnus-original-article-buffer 1 e)
7252         (save-restriction
7253           (narrow-to-region (point-min) (point))
7254           (article-decode-encoded-words)
7255           (if  hidden
7256               (let ((gnus-treat-hide-headers nil)
7257                     (gnus-treat-hide-boring-headers nil))
7258                 (setq gnus-article-wash-types
7259                       (delq 'headers gnus-article-wash-types))
7260                 (gnus-treat-article 'head))
7261             (gnus-treat-article 'head)))
7262         (gnus-set-mode-line 'article)))))
7263
7264 (defun gnus-summary-show-all-headers ()
7265   "Make all header lines visible."
7266   (interactive)
7267   (gnus-article-show-all-headers))
7268
7269 (defun gnus-summary-caesar-message (&optional arg)
7270   "Caesar rotate the current article by 13.
7271 The numerical prefix specifies how many places to rotate each letter
7272 forward."
7273   (interactive "P")
7274   (gnus-summary-select-article)
7275   (let ((mail-header-separator ""))
7276     (gnus-eval-in-buffer-window gnus-article-buffer
7277       (save-restriction
7278         (widen)
7279         (let ((start (window-start))
7280               buffer-read-only)
7281           (message-caesar-buffer-body arg)
7282           (set-window-start (get-buffer-window (current-buffer)) start))))))
7283
7284 (defun gnus-summary-stop-page-breaking ()
7285   "Stop page breaking in the current article."
7286   (interactive)
7287   (gnus-summary-select-article)
7288   (gnus-eval-in-buffer-window gnus-article-buffer
7289     (widen)
7290     (when (gnus-visual-p 'page-marker)
7291       (let ((buffer-read-only nil))
7292         (gnus-remove-text-with-property 'gnus-prev)
7293         (gnus-remove-text-with-property 'gnus-next))
7294       (setq gnus-page-broken nil))))
7295
7296 (defun gnus-summary-move-article (&optional n to-newsgroup
7297                                             select-method action)
7298   "Move the current article to a different newsgroup.
7299 If N is a positive number, move the N next articles.
7300 If N is a negative number, move the N previous articles.
7301 If N is nil and any articles have been marked with the process mark,
7302 move those articles instead.
7303 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7304 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7305 re-spool using this method.
7306
7307 For this function to work, both the current newsgroup and the
7308 newsgroup that you want to move to have to support the `request-move'
7309 and `request-accept' functions.
7310
7311 ACTION can be either `move' (the default), `crosspost' or `copy'."
7312   (interactive "P")
7313   (unless action
7314     (setq action 'move))
7315   ;; Disable marking as read.
7316   (let (gnus-mark-article-hook)
7317     (save-window-excursion
7318       (gnus-summary-select-article)))
7319   ;; Check whether the source group supports the required functions.
7320   (cond ((and (eq action 'move)
7321               (not (gnus-check-backend-function
7322                     'request-move-article gnus-newsgroup-name)))
7323          (error "The current group does not support article moving"))
7324         ((and (eq action 'crosspost)
7325               (not (gnus-check-backend-function
7326                     'request-replace-article gnus-newsgroup-name)))
7327          (error "The current group does not support article editing")))
7328   (let ((articles (gnus-summary-work-articles n))
7329         (prefix (if (gnus-check-backend-function
7330                     'request-move-article gnus-newsgroup-name)
7331                     (gnus-group-real-prefix gnus-newsgroup-name)
7332                   ""))
7333         (names '((move "Move" "Moving")
7334                  (copy "Copy" "Copying")
7335                  (crosspost "Crosspost" "Crossposting")))
7336         (copy-buf (save-excursion
7337                     (nnheader-set-temp-buffer " *copy article*")))
7338         art-group to-method new-xref article to-groups)
7339     (unless (assq action names)
7340       (error "Unknown action %s" action))
7341     ;; Read the newsgroup name.
7342     (when (and (not to-newsgroup)
7343                (not select-method))
7344       (setq to-newsgroup
7345             (gnus-read-move-group-name
7346              (cadr (assq action names))
7347              (symbol-value (intern (format "gnus-current-%s-group" action)))
7348              articles prefix))
7349       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7350     (setq to-method (or select-method
7351                         (gnus-server-to-method
7352                          (gnus-group-method to-newsgroup))))
7353     ;; Check the method we are to move this article to...
7354     (unless (gnus-check-backend-function
7355              'request-accept-article (car to-method))
7356       (error "%s does not support article copying" (car to-method)))
7357     (unless (gnus-check-server to-method)
7358       (error "Can't open server %s" (car to-method)))
7359     (gnus-message 6 "%s to %s: %s..."
7360                   (caddr (assq action names))
7361                   (or (car select-method) to-newsgroup) articles)
7362     (while articles
7363       (setq article (pop articles))
7364       (setq
7365        art-group
7366        (cond
7367         ;; Move the article.
7368         ((eq action 'move)
7369          ;; Remove this article from future suppression.
7370          (gnus-dup-unsuppress-article article)
7371          (gnus-request-move-article
7372           article                       ; Article to move
7373           gnus-newsgroup-name           ; From newsgroup
7374           (nth 1 (gnus-find-method-for-group
7375                   gnus-newsgroup-name)) ; Server
7376           (list 'gnus-request-accept-article
7377                 to-newsgroup (list 'quote select-method)
7378                 (not articles) t)       ; Accept form
7379           (not articles)))              ; Only save nov last time
7380         ;; Copy the article.
7381         ((eq action 'copy)
7382          (save-excursion
7383            (set-buffer copy-buf)
7384            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7385              (gnus-request-accept-article
7386               to-newsgroup select-method (not articles) t))))
7387         ;; Crosspost the article.
7388         ((eq action 'crosspost)
7389          (let ((xref (message-tokenize-header
7390                       (mail-header-xref (gnus-summary-article-header article))
7391                       " ")))
7392            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7393                                   ":" article))
7394            (unless xref
7395              (setq xref (list (system-name))))
7396            (setq new-xref
7397                  (concat
7398                   (mapconcat 'identity
7399                              (delete "Xref:" (delete new-xref xref))
7400                              " ")
7401                   " " new-xref))
7402            (save-excursion
7403              (set-buffer copy-buf)
7404              ;; First put the article in the destination group.
7405              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7406              (when (consp (setq art-group
7407                                 (gnus-request-accept-article
7408                                  to-newsgroup select-method (not articles))))
7409                (setq new-xref (concat new-xref " " (car art-group)
7410                                       ":" (cdr art-group)))
7411                ;; Now we have the new Xrefs header, so we insert
7412                ;; it and replace the new article.
7413                (nnheader-replace-header "Xref" new-xref)
7414                (gnus-request-replace-article
7415                 (cdr art-group) to-newsgroup (current-buffer))
7416                art-group))))))
7417       (cond
7418        ((not art-group)
7419         (gnus-message 1 "Couldn't %s article %s: %s"
7420                       (cadr (assq action names)) article
7421                       (nnheader-get-report (car to-method))))
7422        ((eq art-group 'junk)
7423         (when (eq action 'move)
7424           (gnus-summary-mark-article article gnus-canceled-mark)
7425           (gnus-message 4 "Deleted article %s" article)))
7426        (t
7427         (let* ((pto-group (gnus-group-prefixed-name
7428                            (car art-group) to-method))
7429                (entry
7430                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7431                (info (nth 2 entry))
7432                (to-group (gnus-info-group info))
7433                to-marks)
7434           ;; Update the group that has been moved to.
7435           (when (and info
7436                      (memq action '(move copy)))
7437             (unless (member to-group to-groups)
7438               (push to-group to-groups))
7439
7440             (unless (memq article gnus-newsgroup-unreads)
7441               (push 'read to-marks)
7442               (gnus-info-set-read
7443                info (gnus-add-to-range (gnus-info-read info)
7444                                        (list (cdr art-group)))))
7445
7446             ;; See whether the article is to be put in the cache.
7447             (let ((marks gnus-article-mark-lists)
7448                   (to-article (cdr art-group)))
7449
7450               ;; Enter the article into the cache in the new group,
7451               ;; if that is required.
7452               (when gnus-use-cache
7453                 (gnus-cache-possibly-enter-article
7454                  to-group to-article
7455                  (memq article gnus-newsgroup-marked)
7456                  (memq article gnus-newsgroup-dormant)
7457                  (memq article gnus-newsgroup-unreads)))
7458
7459               (when gnus-preserve-marks 
7460                 ;; Copy any marks over to the new group.
7461                 (when (and (equal to-group gnus-newsgroup-name)
7462                            (not (memq article gnus-newsgroup-unreads)))
7463                   ;; Mark this article as read in this group.
7464                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7465                   (setcdr (gnus-active to-group) to-article)
7466                   (setcdr gnus-newsgroup-active to-article))
7467
7468                 (while marks
7469                   (when (memq article (symbol-value
7470                                        (intern (format "gnus-newsgroup-%s"
7471                                                        (caar marks)))))
7472                     (push (cdar marks) to-marks)
7473                     ;; If the other group is the same as this group,
7474                     ;; then we have to add the mark to the list.
7475                     (when (equal to-group gnus-newsgroup-name)
7476                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7477                            (cons to-article
7478                                  (symbol-value
7479                                   (intern (format "gnus-newsgroup-%s"
7480                                                   (caar marks)))))))
7481                     ;; Copy the marks to other group.
7482                     (gnus-add-marked-articles
7483                      to-group (cdar marks) (list to-article) info))
7484                   (setq marks (cdr marks)))
7485
7486                 (gnus-request-set-mark to-group (list (list (list to-article)
7487                                                             'set
7488                                                             to-marks))))
7489
7490               (gnus-dribble-enter
7491                (concat "(gnus-group-set-info '"
7492                        (gnus-prin1-to-string (gnus-get-info to-group))
7493                        ")"))))
7494
7495           ;; Update the Xref header in this article to point to
7496           ;; the new crossposted article we have just created.
7497           (when (eq action 'crosspost)
7498             (save-excursion
7499               (set-buffer copy-buf)
7500               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7501               (nnheader-replace-header "Xref" new-xref)
7502               (gnus-request-replace-article
7503                article gnus-newsgroup-name (current-buffer)))))
7504
7505         ;;;!!!Why is this necessary?
7506         (set-buffer gnus-summary-buffer)
7507
7508         (gnus-summary-goto-subject article)
7509         (when (eq action 'move)
7510           (gnus-summary-mark-article article gnus-canceled-mark))))
7511       (gnus-summary-remove-process-mark article))
7512     ;; Re-activate all groups that have been moved to.
7513     (while to-groups
7514       (save-excursion
7515         (set-buffer gnus-group-buffer)
7516         (when (gnus-group-goto-group (car to-groups) t)
7517           (gnus-group-get-new-news-this-group 1 t))
7518         (pop to-groups)))
7519
7520     (gnus-kill-buffer copy-buf)
7521     (gnus-summary-position-point)
7522     (gnus-set-mode-line 'summary)))
7523
7524 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7525   "Move the current article to a different newsgroup.
7526 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7527 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7528 re-spool using this method."
7529   (interactive "P")
7530   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7531
7532 (defun gnus-summary-crosspost-article (&optional n)
7533   "Crosspost the current article to some other group."
7534   (interactive "P")
7535   (gnus-summary-move-article n nil nil 'crosspost))
7536
7537 (defcustom gnus-summary-respool-default-method nil
7538   "Default method for respooling an article.
7539 If nil, use to the current newsgroup method."
7540   :type '(choice (gnus-select-method :value (nnml ""))
7541                  (const nil))
7542   :group 'gnus-summary-mail)
7543
7544 (defun gnus-summary-respool-article (&optional n method)
7545   "Respool the current article.
7546 The article will be squeezed through the mail spooling process again,
7547 which means that it will be put in some mail newsgroup or other
7548 depending on `nnmail-split-methods'.
7549 If N is a positive number, respool the N next articles.
7550 If N is a negative number, respool the N previous articles.
7551 If N is nil and any articles have been marked with the process mark,
7552 respool those articles instead.
7553
7554 Respooling can be done both from mail groups and \"real\" newsgroups.
7555 In the former case, the articles in question will be moved from the
7556 current group into whatever groups they are destined to.  In the
7557 latter case, they will be copied into the relevant groups."
7558   (interactive
7559    (list current-prefix-arg
7560          (let* ((methods (gnus-methods-using 'respool))
7561                 (methname
7562                  (symbol-name (or gnus-summary-respool-default-method
7563                                   (car (gnus-find-method-for-group
7564                                         gnus-newsgroup-name)))))
7565                 (method
7566                  (gnus-completing-read
7567                   methname "What backend do you want to use when respooling?"
7568                   methods nil t nil 'gnus-mail-method-history))
7569                 ms)
7570            (cond
7571             ((zerop (length (setq ms (gnus-servers-using-backend
7572                                       (intern method)))))
7573              (list (intern method) ""))
7574             ((= 1 (length ms))
7575              (car ms))
7576             (t
7577              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7578                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7579                            ms-alist))))))))
7580   (unless method
7581     (error "No method given for respooling"))
7582   (if (assoc (symbol-name
7583               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7584              (gnus-methods-using 'respool))
7585       (gnus-summary-move-article n nil method)
7586     (gnus-summary-copy-article n nil method)))
7587
7588 (defun gnus-summary-import-article (file)
7589   "Import an arbitrary file into a mail newsgroup."
7590   (interactive "fImport file: ")
7591   (let ((group gnus-newsgroup-name)
7592         (now (current-time))
7593         atts lines)
7594     (unless (gnus-check-backend-function 'request-accept-article group)
7595       (error "%s does not support article importing" group))
7596     (or (file-readable-p file)
7597         (not (file-regular-p file))
7598         (error "Can't read %s" file))
7599     (save-excursion
7600       (set-buffer (gnus-get-buffer-create " *import file*"))
7601       (erase-buffer)
7602       (insert-file-contents file)
7603       (goto-char (point-min))
7604       (unless (nnheader-article-p)
7605         ;; This doesn't look like an article, so we fudge some headers.
7606         (setq atts (file-attributes file)
7607               lines (count-lines (point-min) (point-max)))
7608         (insert "From: " (read-string "From: ") "\n"
7609                 "Subject: " (read-string "Subject: ") "\n"
7610                 "Date: " (message-make-date (nth 5 atts))
7611                 "\n"
7612                 "Message-ID: " (message-make-message-id) "\n"
7613                 "Lines: " (int-to-string lines) "\n"
7614                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7615       (gnus-request-accept-article group nil t)
7616       (kill-buffer (current-buffer)))))
7617
7618 (defun gnus-summary-article-posted-p ()
7619   "Say whether the current (mail) article is available from news as well.
7620 This will be the case if the article has both been mailed and posted."
7621   (interactive)
7622   (let ((id (mail-header-references (gnus-summary-article-header)))
7623         (gnus-override-method (car (gnus-refer-article-methods))))
7624     (if (gnus-request-head id "")
7625         (gnus-message 2 "The current message was found on %s"
7626                       gnus-override-method)
7627       (gnus-message 2 "The current message couldn't be found on %s"
7628                     gnus-override-method)
7629       nil)))
7630
7631 (defun gnus-summary-expire-articles (&optional now)
7632   "Expire all articles that are marked as expirable in the current group."
7633   (interactive)
7634   (when (gnus-check-backend-function
7635          'request-expire-articles gnus-newsgroup-name)
7636     ;; This backend supports expiry.
7637     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7638            (expirable (if total
7639                           (progn
7640                             ;; We need to update the info for
7641                             ;; this group for `gnus-list-of-read-articles'
7642                             ;; to give us the right answer.
7643                             (gnus-run-hooks 'gnus-exit-group-hook)
7644                             (gnus-summary-update-info)
7645                             (gnus-list-of-read-articles gnus-newsgroup-name))
7646                         (setq gnus-newsgroup-expirable
7647                               (sort gnus-newsgroup-expirable '<))))
7648            (expiry-wait (if now 'immediate
7649                           (gnus-group-find-parameter
7650                            gnus-newsgroup-name 'expiry-wait)))
7651            es)
7652       (when expirable
7653         ;; There are expirable articles in this group, so we run them
7654         ;; through the expiry process.
7655         (gnus-message 6 "Expiring articles...")
7656         (unless (gnus-check-group gnus-newsgroup-name)
7657           (error "Can't open server for %s" gnus-newsgroup-name))
7658         ;; The list of articles that weren't expired is returned.
7659         (save-excursion
7660           (if expiry-wait
7661               (let ((nnmail-expiry-wait-function nil)
7662                     (nnmail-expiry-wait expiry-wait))
7663                 (setq es (gnus-request-expire-articles
7664                           expirable gnus-newsgroup-name)))
7665             (setq es (gnus-request-expire-articles
7666                       expirable gnus-newsgroup-name))))
7667         (unless total
7668           (setq gnus-newsgroup-expirable es))
7669         ;; We go through the old list of expirable, and mark all
7670         ;; really expired articles as nonexistent.
7671         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7672           (let ((gnus-use-cache nil))
7673             (while expirable
7674               (unless (memq (car expirable) es)
7675                 (when (gnus-data-find (car expirable))
7676                   (gnus-summary-mark-article
7677                    (car expirable) gnus-canceled-mark)))
7678               (setq expirable (cdr expirable)))))
7679         (gnus-message 6 "Expiring articles...done")))))
7680
7681 (defun gnus-summary-expire-articles-now ()
7682   "Expunge all expirable articles in the current group.
7683 This means that *all* articles that are marked as expirable will be
7684 deleted forever, right now."
7685   (interactive)
7686   (or gnus-expert-user
7687       (gnus-yes-or-no-p
7688        "Are you really, really, really sure you want to delete all these messages? ")
7689       (error "Phew!"))
7690   (gnus-summary-expire-articles t))
7691
7692 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7693 (defun gnus-summary-delete-article (&optional n)
7694   "Delete the N next (mail) articles.
7695 This command actually deletes articles.  This is not a marking
7696 command.  The article will disappear forever from your life, never to
7697 return.
7698 If N is negative, delete backwards.
7699 If N is nil and articles have been marked with the process mark,
7700 delete these instead."
7701   (interactive "P")
7702   (unless (gnus-check-backend-function 'request-expire-articles
7703                                        gnus-newsgroup-name)
7704     (error "The current newsgroup does not support article deletion"))
7705   ;; Compute the list of articles to delete.
7706   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7707         not-deleted)
7708     (if (and gnus-novice-user
7709              (not (gnus-yes-or-no-p
7710                    (format "Do you really want to delete %s forever? "
7711                            (if (> (length articles) 1)
7712                                (format "these %s articles" (length articles))
7713                              "this article")))))
7714         ()
7715       ;; Delete the articles.
7716       (setq not-deleted (gnus-request-expire-articles
7717                          articles gnus-newsgroup-name 'force))
7718       (while articles
7719         (gnus-summary-remove-process-mark (car articles))
7720         ;; The backend might not have been able to delete the article
7721         ;; after all.
7722         (unless (memq (car articles) not-deleted)
7723           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7724         (setq articles (cdr articles)))
7725       (when not-deleted
7726         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7727     (gnus-summary-position-point)
7728     (gnus-set-mode-line 'summary)
7729     not-deleted))
7730
7731 (defun gnus-summary-edit-article (&optional force)
7732   "Edit the current article.
7733 This will have permanent effect only in mail groups.
7734 If FORCE is non-nil, allow editing of articles even in read-only
7735 groups."
7736   (interactive "P")
7737   (save-excursion
7738     (set-buffer gnus-summary-buffer)
7739     (let ((mail-parse-charset gnus-newsgroup-charset)
7740           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7741       (gnus-set-global-variables)
7742       (when (and (not force)
7743                  (gnus-group-read-only-p))
7744         (error "The current newsgroup does not support article editing"))
7745       (gnus-summary-show-article t)
7746       (gnus-article-edit-article
7747        'mime-to-mml
7748        `(lambda (no-highlight)
7749           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7750                 (mail-parse-ignored-charsets 
7751                  ',gnus-newsgroup-ignored-charsets))
7752             (mml-to-mime)
7753             (gnus-summary-edit-article-done
7754              ,(or (mail-header-references gnus-current-headers) "")
7755              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7756
7757 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7758
7759 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7760                                                  no-highlight)
7761   "Make edits to the current article permanent."
7762   (interactive)
7763   ;; Replace the article.
7764   (let ((buf (current-buffer)))
7765     (with-temp-buffer
7766       (insert-buffer-substring buf)
7767       (if (and (not read-only)
7768                (not (gnus-request-replace-article
7769                      (cdr gnus-article-current) (car gnus-article-current)
7770                      (current-buffer) t)))
7771           (error "Couldn't replace article")
7772         ;; Update the summary buffer.
7773         (if (and references
7774                  (equal (message-tokenize-header references " ")
7775                         (message-tokenize-header
7776                          (or (message-fetch-field "references") "") " ")))
7777             ;; We only have to update this line.
7778             (save-excursion
7779               (save-restriction
7780                 (message-narrow-to-head)
7781                 (let ((head (buffer-string))
7782                       header)
7783                   (with-temp-buffer
7784                     (insert (format "211 %d Article retrieved.\n"
7785                                     (cdr gnus-article-current)))
7786                     (insert head)
7787                     (insert ".\n")
7788                     (let ((nntp-server-buffer (current-buffer)))
7789                       (setq header (car (gnus-get-newsgroup-headers
7790                                          (save-excursion
7791                                            (set-buffer gnus-summary-buffer)
7792                                            gnus-newsgroup-dependencies)
7793                                          t))))
7794                     (save-excursion
7795                       (set-buffer gnus-summary-buffer)
7796                       (gnus-data-set-header
7797                        (gnus-data-find (cdr gnus-article-current))
7798                        header)
7799                       (gnus-summary-update-article-line
7800                        (cdr gnus-article-current) header))))))
7801           ;; Update threads.
7802           (set-buffer (or buffer gnus-summary-buffer))
7803           (gnus-summary-update-article (cdr gnus-article-current)))
7804         ;; Prettify the article buffer again.
7805         (unless no-highlight
7806           (save-excursion
7807             (set-buffer gnus-article-buffer)
7808             ;;;!!! Fix this -- article should be rehighlighted.
7809             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7810             (set-buffer gnus-original-article-buffer)
7811             (gnus-request-article
7812              (cdr gnus-article-current)
7813              (car gnus-article-current) (current-buffer))))
7814         ;; Prettify the summary buffer line.
7815         (when (gnus-visual-p 'summary-highlight 'highlight)
7816           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7817
7818 (defun gnus-summary-edit-wash (key)
7819   "Perform editing command KEY in the article buffer."
7820   (interactive
7821    (list
7822     (progn
7823       (message "%s" (concat (this-command-keys) "- "))
7824       (read-char))))
7825   (message "")
7826   (gnus-summary-edit-article)
7827   (execute-kbd-macro (concat (this-command-keys) key))
7828   (gnus-article-edit-done))
7829
7830 ;;; Respooling
7831
7832 (defun gnus-summary-respool-query (&optional silent trace)
7833   "Query where the respool algorithm would put this article."
7834   (interactive)
7835   (let (gnus-mark-article-hook)
7836     (gnus-summary-select-article)
7837     (save-excursion
7838       (set-buffer gnus-original-article-buffer)
7839       (save-restriction
7840         (message-narrow-to-head)
7841         (let ((groups (nnmail-article-group 'identity trace)))
7842           (unless silent
7843             (if groups
7844                 (message "This message would go to %s"
7845                          (mapconcat 'car groups ", "))
7846               (message "This message would go to no groups"))
7847             groups))))))
7848
7849 (defun gnus-summary-respool-trace ()
7850   "Trace where the respool algorithm would put this article.
7851 Display a buffer showing all fancy splitting patterns which matched."
7852   (interactive)
7853   (gnus-summary-respool-query nil t))
7854
7855 ;; Summary marking commands.
7856
7857 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7858   "Mark articles which has the same subject as read, and then select the next.
7859 If UNMARK is positive, remove any kind of mark.
7860 If UNMARK is negative, tick articles."
7861   (interactive "P")
7862   (when unmark
7863     (setq unmark (prefix-numeric-value unmark)))
7864   (let ((count
7865          (gnus-summary-mark-same-subject
7866           (gnus-summary-article-subject) unmark)))
7867     ;; Select next unread article.  If auto-select-same mode, should
7868     ;; select the first unread article.
7869     (gnus-summary-next-article t (and gnus-auto-select-same
7870                                       (gnus-summary-article-subject)))
7871     (gnus-message 7 "%d article%s marked as %s"
7872                   count (if (= count 1) " is" "s are")
7873                   (if unmark "unread" "read"))))
7874
7875 (defun gnus-summary-kill-same-subject (&optional unmark)
7876   "Mark articles which has the same subject as read.
7877 If UNMARK is positive, remove any kind of mark.
7878 If UNMARK is negative, tick articles."
7879   (interactive "P")
7880   (when unmark
7881     (setq unmark (prefix-numeric-value unmark)))
7882   (let ((count
7883          (gnus-summary-mark-same-subject
7884           (gnus-summary-article-subject) unmark)))
7885     ;; If marked as read, go to next unread subject.
7886     (when (null unmark)
7887       ;; Go to next unread subject.
7888       (gnus-summary-next-subject 1 t))
7889     (gnus-message 7 "%d articles are marked as %s"
7890                   count (if unmark "unread" "read"))))
7891
7892 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7893   "Mark articles with same SUBJECT as read, and return marked number.
7894 If optional argument UNMARK is positive, remove any kinds of marks.
7895 If optional argument UNMARK is negative, mark articles as unread instead."
7896   (let ((count 1))
7897     (save-excursion
7898       (cond
7899        ((null unmark)                   ; Mark as read.
7900         (while (and
7901                 (progn
7902                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7903                   (gnus-summary-show-thread) t)
7904                 (gnus-summary-find-subject subject))
7905           (setq count (1+ count))))
7906        ((> unmark 0)                    ; Tick.
7907         (while (and
7908                 (progn
7909                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7910                   (gnus-summary-show-thread) t)
7911                 (gnus-summary-find-subject subject))
7912           (setq count (1+ count))))
7913        (t                               ; Mark as unread.
7914         (while (and
7915                 (progn
7916                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7917                   (gnus-summary-show-thread) t)
7918                 (gnus-summary-find-subject subject))
7919           (setq count (1+ count)))))
7920       (gnus-set-mode-line 'summary)
7921       ;; Return the number of marked articles.
7922       count)))
7923
7924 (defun gnus-summary-mark-as-processable (n &optional unmark)
7925   "Set the process mark on the next N articles.
7926 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7927 the process mark instead.  The difference between N and the actual
7928 number of articles marked is returned."
7929   (interactive "p")
7930   (let ((backward (< n 0))
7931         (n (abs n)))
7932     (while (and
7933             (> n 0)
7934             (if unmark
7935                 (gnus-summary-remove-process-mark
7936                  (gnus-summary-article-number))
7937               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7938             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7939       (setq n (1- n)))
7940     (when (/= 0 n)
7941       (gnus-message 7 "No more articles"))
7942     (gnus-summary-recenter)
7943     (gnus-summary-position-point)
7944     n))
7945
7946 (defun gnus-summary-unmark-as-processable (n)
7947   "Remove the process mark from the next N articles.
7948 If N is negative, unmark backward instead.  The difference between N and
7949 the actual number of articles unmarked is returned."
7950   (interactive "p")
7951   (gnus-summary-mark-as-processable n t))
7952
7953 (defun gnus-summary-unmark-all-processable ()
7954   "Remove the process mark from all articles."
7955   (interactive)
7956   (save-excursion
7957     (while gnus-newsgroup-processable
7958       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7959   (gnus-summary-position-point))
7960
7961 (defun gnus-summary-mark-as-expirable (n)
7962   "Mark N articles forward as expirable.
7963 If N is negative, mark backward instead.  The difference between N and
7964 the actual number of articles marked is returned."
7965   (interactive "p")
7966   (gnus-summary-mark-forward n gnus-expirable-mark))
7967
7968 (defun gnus-summary-mark-article-as-replied (article)
7969   "Mark ARTICLE replied and update the summary line."
7970   (push article gnus-newsgroup-replied)
7971   (let ((buffer-read-only nil))
7972     (when (gnus-summary-goto-subject article nil t)
7973       (gnus-summary-update-secondary-mark article))))
7974
7975 (defun gnus-summary-set-bookmark (article)
7976   "Set a bookmark in current article."
7977   (interactive (list (gnus-summary-article-number)))
7978   (when (or (not (get-buffer gnus-article-buffer))
7979             (not gnus-current-article)
7980             (not gnus-article-current)
7981             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7982     (error "No current article selected"))
7983   ;; Remove old bookmark, if one exists.
7984   (let ((old (assq article gnus-newsgroup-bookmarks)))
7985     (when old
7986       (setq gnus-newsgroup-bookmarks
7987             (delq old gnus-newsgroup-bookmarks))))
7988   ;; Set the new bookmark, which is on the form
7989   ;; (article-number . line-number-in-body).
7990   (push
7991    (cons article
7992          (save-excursion
7993            (set-buffer gnus-article-buffer)
7994            (count-lines
7995             (min (point)
7996                  (save-excursion
7997                    (goto-char (point-min))
7998                    (search-forward "\n\n" nil t)
7999                    (point)))
8000             (point))))
8001    gnus-newsgroup-bookmarks)
8002   (gnus-message 6 "A bookmark has been added to the current article."))
8003
8004 (defun gnus-summary-remove-bookmark (article)
8005   "Remove the bookmark from the current article."
8006   (interactive (list (gnus-summary-article-number)))
8007   ;; Remove old bookmark, if one exists.
8008   (let ((old (assq article gnus-newsgroup-bookmarks)))
8009     (if old
8010         (progn
8011           (setq gnus-newsgroup-bookmarks
8012                 (delq old gnus-newsgroup-bookmarks))
8013           (gnus-message 6 "Removed bookmark."))
8014       (gnus-message 6 "No bookmark in current article."))))
8015
8016 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8017 (defun gnus-summary-mark-as-dormant (n)
8018   "Mark N articles forward as dormant.
8019 If N is negative, mark backward instead.  The difference between N and
8020 the actual number of articles marked is returned."
8021   (interactive "p")
8022   (gnus-summary-mark-forward n gnus-dormant-mark))
8023
8024 (defun gnus-summary-set-process-mark (article)
8025   "Set the process mark on ARTICLE and update the summary line."
8026   (setq gnus-newsgroup-processable
8027         (cons article
8028               (delq article gnus-newsgroup-processable)))
8029   (when (gnus-summary-goto-subject article)
8030     (gnus-summary-show-thread)
8031     (gnus-summary-goto-subject article)
8032     (gnus-summary-update-secondary-mark article)))
8033
8034 (defun gnus-summary-remove-process-mark (article)
8035   "Remove the process mark from ARTICLE and update the summary line."
8036   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8037   (when (gnus-summary-goto-subject article)
8038     (gnus-summary-show-thread)
8039     (gnus-summary-goto-subject article)
8040     (gnus-summary-update-secondary-mark article)))
8041
8042 (defun gnus-summary-set-saved-mark (article)
8043   "Set the process mark on ARTICLE and update the summary line."
8044   (push article gnus-newsgroup-saved)
8045   (when (gnus-summary-goto-subject article)
8046     (gnus-summary-update-secondary-mark article)))
8047
8048 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8049   "Mark N articles as read forwards.
8050 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8051 The difference between N and the actual number of articles marked is
8052 returned.
8053 Iff NO-EXPIRE, auto-expiry will be inhibited."
8054   (interactive "p")
8055   (gnus-summary-show-thread)
8056   (let ((backward (< n 0))
8057         (gnus-summary-goto-unread
8058          (and gnus-summary-goto-unread
8059               (not (eq gnus-summary-goto-unread 'never))
8060               (not (memq mark (list gnus-unread-mark
8061                                     gnus-ticked-mark gnus-dormant-mark)))))
8062         (n (abs n))
8063         (mark (or mark gnus-del-mark)))
8064     (while (and (> n 0)
8065                 (gnus-summary-mark-article nil mark no-expire)
8066                 (zerop (gnus-summary-next-subject
8067                         (if backward -1 1)
8068                         (and gnus-summary-goto-unread
8069                              (not (eq gnus-summary-goto-unread 'never)))
8070                         t)))
8071       (setq n (1- n)))
8072     (when (/= 0 n)
8073       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8074     (gnus-summary-recenter)
8075     (gnus-summary-position-point)
8076     (gnus-set-mode-line 'summary)
8077     n))
8078
8079 (defun gnus-summary-mark-article-as-read (mark)
8080   "Mark the current article quickly as read with MARK."
8081   (let ((article (gnus-summary-article-number)))
8082     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8083     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8084     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8085     (push (cons article mark) gnus-newsgroup-reads)
8086     ;; Possibly remove from cache, if that is used.
8087     (when gnus-use-cache
8088       (gnus-cache-enter-remove-article article))
8089     ;; Allow the backend to change the mark.
8090     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8091     ;; Check for auto-expiry.
8092     (when (and gnus-newsgroup-auto-expire
8093                (memq mark gnus-auto-expirable-marks))
8094       (setq mark gnus-expirable-mark)
8095       ;; Let the backend know about the mark change.
8096       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8097       (push article gnus-newsgroup-expirable))
8098     ;; Set the mark in the buffer.
8099     (gnus-summary-update-mark mark 'unread)
8100     t))
8101
8102 (defun gnus-summary-mark-article-as-unread (mark)
8103   "Mark the current article quickly as unread with MARK."
8104   (let* ((article (gnus-summary-article-number))
8105          (old-mark (gnus-summary-article-mark article)))
8106     ;; Allow the backend to change the mark.
8107     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8108     (if (eq mark old-mark)
8109         t
8110       (if (<= article 0)
8111           (progn
8112             (gnus-error 1 "Can't mark negative article numbers")
8113             nil)
8114         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8115         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8116         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8117         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8118         (cond ((= mark gnus-ticked-mark)
8119                (push article gnus-newsgroup-marked))
8120               ((= mark gnus-dormant-mark)
8121                (push article gnus-newsgroup-dormant))
8122               (t
8123                (push article gnus-newsgroup-unreads)))
8124         (gnus-pull article gnus-newsgroup-reads)
8125
8126         ;; See whether the article is to be put in the cache.
8127         (and gnus-use-cache
8128              (vectorp (gnus-summary-article-header article))
8129              (save-excursion
8130                (gnus-cache-possibly-enter-article
8131                 gnus-newsgroup-name article
8132                 (= mark gnus-ticked-mark)
8133                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8134
8135         ;; Fix the mark.
8136         (gnus-summary-update-mark mark 'unread)
8137         t))))
8138
8139 (defun gnus-summary-mark-article (&optional article mark no-expire)
8140   "Mark ARTICLE with MARK.  MARK can be any character.
8141 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8142 `??' (dormant) and `?E' (expirable).
8143 If MARK is nil, then the default character `?r' is used.
8144 If ARTICLE is nil, then the article on the current line will be
8145 marked.
8146 Iff NO-EXPIRE, auto-expiry will be inhibited."
8147   ;; The mark might be a string.
8148   (when (stringp mark)
8149     (setq mark (aref mark 0)))
8150   ;; If no mark is given, then we check auto-expiring.
8151   (when (null mark)
8152     (setq mark gnus-del-mark))
8153   (when (and (not no-expire)
8154              gnus-newsgroup-auto-expire
8155              (memq mark gnus-auto-expirable-marks))
8156     (setq mark gnus-expirable-mark))
8157   (let ((article (or article (gnus-summary-article-number)))
8158         (old-mark (gnus-summary-article-mark article)))
8159     ;; Allow the backend to change the mark.
8160     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8161     (if (eq mark old-mark)
8162         t
8163       (unless article
8164         (error "No article on current line"))
8165       (if (not (if (or (= mark gnus-unread-mark)
8166                        (= mark gnus-ticked-mark)
8167                        (= mark gnus-dormant-mark))
8168                    (gnus-mark-article-as-unread article mark)
8169                  (gnus-mark-article-as-read article mark)))
8170           t
8171         ;; See whether the article is to be put in the cache.
8172         (and gnus-use-cache
8173              (not (= mark gnus-canceled-mark))
8174              (vectorp (gnus-summary-article-header article))
8175              (save-excursion
8176                (gnus-cache-possibly-enter-article
8177                 gnus-newsgroup-name article
8178                 (= mark gnus-ticked-mark)
8179                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8180
8181         (when (gnus-summary-goto-subject article nil t)
8182           (let ((buffer-read-only nil))
8183             (gnus-summary-show-thread)
8184             ;; Fix the mark.
8185             (gnus-summary-update-mark mark 'unread)
8186             t))))))
8187
8188 (defun gnus-summary-update-secondary-mark (article)
8189   "Update the secondary (read, process, cache) mark."
8190   (gnus-summary-update-mark
8191    (cond ((memq article gnus-newsgroup-processable)
8192           gnus-process-mark)
8193          ((memq article gnus-newsgroup-cached)
8194           gnus-cached-mark)
8195          ((memq article gnus-newsgroup-replied)
8196           gnus-replied-mark)
8197          ((memq article gnus-newsgroup-saved)
8198           gnus-saved-mark)
8199          (t gnus-unread-mark))
8200    'replied)
8201   (when (gnus-visual-p 'summary-highlight 'highlight)
8202     (gnus-run-hooks 'gnus-summary-update-hook))
8203   t)
8204
8205 (defun gnus-summary-update-mark (mark type)
8206   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8207         (buffer-read-only nil))
8208     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8209     (when forward
8210       (when (looking-at "\r")
8211         (incf forward))
8212       (when (<= (+ forward (point)) (point-max))
8213         ;; Go to the right position on the line.
8214         (goto-char (+ forward (point)))
8215         ;; Replace the old mark with the new mark.
8216         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8217         ;; Optionally update the marks by some user rule.
8218         (when (eq type 'unread)
8219           (gnus-data-set-mark
8220            (gnus-data-find (gnus-summary-article-number)) mark)
8221           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8222
8223 (defun gnus-mark-article-as-read (article &optional mark)
8224   "Enter ARTICLE in the pertinent lists and remove it from others."
8225   ;; Make the article expirable.
8226   (let ((mark (or mark gnus-del-mark)))
8227     (if (= mark gnus-expirable-mark)
8228         (push article gnus-newsgroup-expirable)
8229       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8230     ;; Remove from unread and marked lists.
8231     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8232     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8233     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8234     (push (cons article mark) gnus-newsgroup-reads)
8235     ;; Possibly remove from cache, if that is used.
8236     (when gnus-use-cache
8237       (gnus-cache-enter-remove-article article))
8238     t))
8239
8240 (defun gnus-mark-article-as-unread (article &optional mark)
8241   "Enter ARTICLE in the pertinent lists and remove it from others."
8242   (let ((mark (or mark gnus-ticked-mark)))
8243     (if (<= article 0)
8244         (progn
8245           (gnus-error 1 "Can't mark negative article numbers")
8246           nil)
8247       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8248             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8249             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8250             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8251
8252       ;; Unsuppress duplicates?
8253       (when gnus-suppress-duplicates
8254         (gnus-dup-unsuppress-article article))
8255
8256       (cond ((= mark gnus-ticked-mark)
8257              (push article gnus-newsgroup-marked))
8258             ((= mark gnus-dormant-mark)
8259              (push article gnus-newsgroup-dormant))
8260             (t
8261              (push article gnus-newsgroup-unreads)))
8262       (gnus-pull article gnus-newsgroup-reads)
8263       t)))
8264
8265 (defalias 'gnus-summary-mark-as-unread-forward
8266   'gnus-summary-tick-article-forward)
8267 (make-obsolete 'gnus-summary-mark-as-unread-forward
8268                'gnus-summary-tick-article-forward)
8269 (defun gnus-summary-tick-article-forward (n)
8270   "Tick N articles forwards.
8271 If N is negative, tick backwards instead.
8272 The difference between N and the number of articles ticked is returned."
8273   (interactive "p")
8274   (gnus-summary-mark-forward n gnus-ticked-mark))
8275
8276 (defalias 'gnus-summary-mark-as-unread-backward
8277   'gnus-summary-tick-article-backward)
8278 (make-obsolete 'gnus-summary-mark-as-unread-backward
8279                'gnus-summary-tick-article-backward)
8280 (defun gnus-summary-tick-article-backward (n)
8281   "Tick N articles backwards.
8282 The difference between N and the number of articles ticked is returned."
8283   (interactive "p")
8284   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8285
8286 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8287 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8288 (defun gnus-summary-tick-article (&optional article clear-mark)
8289   "Mark current article as unread.
8290 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8291 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8292   (interactive)
8293   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8294                                        gnus-ticked-mark)))
8295
8296 (defun gnus-summary-mark-as-read-forward (n)
8297   "Mark N articles as read forwards.
8298 If N is negative, mark backwards instead.
8299 The difference between N and the actual number of articles marked is
8300 returned."
8301   (interactive "p")
8302   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8303
8304 (defun gnus-summary-mark-as-read-backward (n)
8305   "Mark the N articles as read backwards.
8306 The difference between N and the actual number of articles marked is
8307 returned."
8308   (interactive "p")
8309   (gnus-summary-mark-forward
8310    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8311
8312 (defun gnus-summary-mark-as-read (&optional article mark)
8313   "Mark current article as read.
8314 ARTICLE specifies the article to be marked as read.
8315 MARK specifies a string to be inserted at the beginning of the line."
8316   (gnus-summary-mark-article article mark))
8317
8318 (defun gnus-summary-clear-mark-forward (n)
8319   "Clear marks from N articles forward.
8320 If N is negative, clear backward instead.
8321 The difference between N and the number of marks cleared is returned."
8322   (interactive "p")
8323   (gnus-summary-mark-forward n gnus-unread-mark))
8324
8325 (defun gnus-summary-clear-mark-backward (n)
8326   "Clear marks from N articles backward.
8327 The difference between N and the number of marks cleared is returned."
8328   (interactive "p")
8329   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8330
8331 (defun gnus-summary-mark-unread-as-read ()
8332   "Intended to be used by `gnus-summary-mark-article-hook'."
8333   (when (memq gnus-current-article gnus-newsgroup-unreads)
8334     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8335
8336 (defun gnus-summary-mark-read-and-unread-as-read ()
8337   "Intended to be used by `gnus-summary-mark-article-hook'."
8338   (let ((mark (gnus-summary-article-mark)))
8339     (when (or (gnus-unread-mark-p mark)
8340               (gnus-read-mark-p mark))
8341       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8342
8343 (defun gnus-summary-mark-region-as-read (point mark all)
8344   "Mark all unread articles between point and mark as read.
8345 If given a prefix, mark all articles between point and mark as read,
8346 even ticked and dormant ones."
8347   (interactive "r\nP")
8348   (save-excursion
8349     (let (article)
8350       (goto-char point)
8351       (beginning-of-line)
8352       (while (and
8353               (< (point) mark)
8354               (progn
8355                 (when (or all
8356                           (memq (setq article (gnus-summary-article-number))
8357                                 gnus-newsgroup-unreads))
8358                   (gnus-summary-mark-article article gnus-del-mark))
8359                 t)
8360               (gnus-summary-find-next))))))
8361
8362 (defun gnus-summary-mark-below (score mark)
8363   "Mark articles with score less than SCORE with MARK."
8364   (interactive "P\ncMark: ")
8365   (setq score (if score
8366                   (prefix-numeric-value score)
8367                 (or gnus-summary-default-score 0)))
8368   (save-excursion
8369     (set-buffer gnus-summary-buffer)
8370     (goto-char (point-min))
8371     (while
8372         (progn
8373           (and (< (gnus-summary-article-score) score)
8374                (gnus-summary-mark-article nil mark))
8375           (gnus-summary-find-next)))))
8376
8377 (defun gnus-summary-kill-below (&optional score)
8378   "Mark articles with score below SCORE as read."
8379   (interactive "P")
8380   (gnus-summary-mark-below score gnus-killed-mark))
8381
8382 (defun gnus-summary-clear-above (&optional score)
8383   "Clear all marks from articles with score above SCORE."
8384   (interactive "P")
8385   (gnus-summary-mark-above score gnus-unread-mark))
8386
8387 (defun gnus-summary-tick-above (&optional score)
8388   "Tick all articles with score above SCORE."
8389   (interactive "P")
8390   (gnus-summary-mark-above score gnus-ticked-mark))
8391
8392 (defun gnus-summary-mark-above (score mark)
8393   "Mark articles with score over SCORE with MARK."
8394   (interactive "P\ncMark: ")
8395   (setq score (if score
8396                   (prefix-numeric-value score)
8397                 (or gnus-summary-default-score 0)))
8398   (save-excursion
8399     (set-buffer gnus-summary-buffer)
8400     (goto-char (point-min))
8401     (while (and (progn
8402                   (when (> (gnus-summary-article-score) score)
8403                     (gnus-summary-mark-article nil mark))
8404                   t)
8405                 (gnus-summary-find-next)))))
8406
8407 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8408 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8409 (defun gnus-summary-limit-include-expunged (&optional no-error)
8410   "Display all the hidden articles that were expunged for low scores."
8411   (interactive)
8412   (let ((buffer-read-only nil))
8413     (let ((scored gnus-newsgroup-scored)
8414           headers h)
8415       (while scored
8416         (unless (gnus-summary-goto-subject (caar scored))
8417           (and (setq h (gnus-summary-article-header (caar scored)))
8418                (< (cdar scored) gnus-summary-expunge-below)
8419                (push h headers)))
8420         (setq scored (cdr scored)))
8421       (if (not headers)
8422           (when (not no-error)
8423             (error "No expunged articles hidden"))
8424         (goto-char (point-min))
8425         (gnus-summary-prepare-unthreaded (nreverse headers))
8426         (goto-char (point-min))
8427         (gnus-summary-position-point)
8428         t))))
8429
8430 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8431   "Mark all unread articles in this newsgroup as read.
8432 If prefix argument ALL is non-nil, ticked and dormant articles will
8433 also be marked as read.
8434 If QUIETLY is non-nil, no questions will be asked.
8435 If TO-HERE is non-nil, it should be a point in the buffer.  All
8436 articles before this point will be marked as read.
8437 Note that this function will only catch up the unread article
8438 in the current summary buffer limitation.
8439 The number of articles marked as read is returned."
8440   (interactive "P")
8441   (prog1
8442       (save-excursion
8443         (when (or quietly
8444                   (not gnus-interactive-catchup) ;Without confirmation?
8445                   gnus-expert-user
8446                   (gnus-y-or-n-p
8447                    (if all
8448                        "Mark absolutely all articles as read? "
8449                      "Mark all unread articles as read? ")))
8450           (if (and not-mark
8451                    (not gnus-newsgroup-adaptive)
8452                    (not gnus-newsgroup-auto-expire)
8453                    (not gnus-suppress-duplicates)
8454                    (or (not gnus-use-cache)
8455                        (eq gnus-use-cache 'passive)))
8456               (progn
8457                 (when all
8458                   (setq gnus-newsgroup-marked nil
8459                         gnus-newsgroup-dormant nil))
8460                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8461             ;; We actually mark all articles as canceled, which we
8462             ;; have to do when using auto-expiry or adaptive scoring.
8463             (gnus-summary-show-all-threads)
8464             (when (gnus-summary-first-subject (not all) t)
8465               (while (and
8466                       (if to-here (< (point) to-here) t)
8467                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8468                       (gnus-summary-find-next (not all) nil nil t))))
8469             (gnus-set-mode-line 'summary))
8470           t))
8471     (gnus-summary-position-point)))
8472
8473 (defun gnus-summary-catchup-to-here (&optional all)
8474   "Mark all unticked articles before the current one as read.
8475 If ALL is non-nil, also mark ticked and dormant articles as read."
8476   (interactive "P")
8477   (save-excursion
8478     (gnus-save-hidden-threads
8479       (let ((beg (point)))
8480         ;; We check that there are unread articles.
8481         (when (or all (gnus-summary-find-prev))
8482           (gnus-summary-catchup all t beg)))))
8483   (gnus-summary-position-point))
8484
8485 (defun gnus-summary-catchup-all (&optional quietly)
8486   "Mark all articles in this newsgroup as read."
8487   (interactive "P")
8488   (gnus-summary-catchup t quietly))
8489
8490 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8491   "Mark all unread articles in this group as read, then exit.
8492 If prefix argument ALL is non-nil, all articles are marked as read."
8493   (interactive "P")
8494   (when (gnus-summary-catchup all quietly nil 'fast)
8495     ;; Select next newsgroup or exit.
8496     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8497              (eq gnus-auto-select-next 'quietly))
8498         (gnus-summary-next-group nil)
8499       (gnus-summary-exit))))
8500
8501 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8502   "Mark all articles in this newsgroup as read, and then exit."
8503   (interactive "P")
8504   (gnus-summary-catchup-and-exit t quietly))
8505
8506 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8507   "Mark all articles in this group as read and select the next group.
8508 If given a prefix, mark all articles, unread as well as ticked, as
8509 read."
8510   (interactive "P")
8511   (save-excursion
8512     (gnus-summary-catchup all))
8513   (gnus-summary-next-group))
8514
8515 ;; Thread-based commands.
8516
8517 (defun gnus-summary-articles-in-thread (&optional article)
8518   "Return a list of all articles in the current thread.
8519 If ARTICLE is non-nil, return all articles in the thread that starts
8520 with that article."
8521   (let* ((article (or article (gnus-summary-article-number)))
8522          (data (gnus-data-find-list article))
8523          (top-level (gnus-data-level (car data)))
8524          (top-subject
8525           (cond ((null gnus-thread-operation-ignore-subject)
8526                  (gnus-simplify-subject-re
8527                   (mail-header-subject (gnus-data-header (car data)))))
8528                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8529                  (gnus-simplify-subject-fuzzy
8530                   (mail-header-subject (gnus-data-header (car data)))))
8531                 (t nil)))
8532          (end-point (save-excursion
8533                       (if (gnus-summary-go-to-next-thread)
8534                           (point) (point-max))))
8535          articles)
8536     (while (and data
8537                 (< (gnus-data-pos (car data)) end-point))
8538       (when (or (not top-subject)
8539                 (string= top-subject
8540                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8541                              (gnus-simplify-subject-fuzzy
8542                               (mail-header-subject
8543                                (gnus-data-header (car data))))
8544                            (gnus-simplify-subject-re
8545                             (mail-header-subject
8546                              (gnus-data-header (car data)))))))
8547         (push (gnus-data-number (car data)) articles))
8548       (unless (and (setq data (cdr data))
8549                    (> (gnus-data-level (car data)) top-level))
8550         (setq data nil)))
8551     ;; Return the list of articles.
8552     (nreverse articles)))
8553
8554 (defun gnus-summary-rethread-current ()
8555   "Rethread the thread the current article is part of."
8556   (interactive)
8557   (let* ((gnus-show-threads t)
8558          (article (gnus-summary-article-number))
8559          (id (mail-header-id (gnus-summary-article-header)))
8560          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8561     (unless id
8562       (error "No article on the current line"))
8563     (gnus-rebuild-thread id)
8564     (gnus-summary-goto-subject article)))
8565
8566 (defun gnus-summary-reparent-thread ()
8567   "Make the current article child of the marked (or previous) article.
8568
8569 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8570 is non-nil or the Subject: of both articles are the same."
8571   (interactive)
8572   (unless (not (gnus-group-read-only-p))
8573     (error "The current newsgroup does not support article editing"))
8574   (unless (<= (length gnus-newsgroup-processable) 1)
8575     (error "No more than one article may be marked"))
8576   (save-window-excursion
8577     (let ((gnus-article-buffer " *reparent*")
8578           (current-article (gnus-summary-article-number))
8579           ;; First grab the marked article, otherwise one line up.
8580           (parent-article (if (not (null gnus-newsgroup-processable))
8581                               (car gnus-newsgroup-processable)
8582                             (save-excursion
8583                               (if (eq (forward-line -1) 0)
8584                                   (gnus-summary-article-number)
8585                                 (error "Beginning of summary buffer"))))))
8586       (unless (not (eq current-article parent-article))
8587         (error "An article may not be self-referential"))
8588       (let ((message-id (mail-header-id
8589                          (gnus-summary-article-header parent-article))))
8590         (unless (and message-id (not (equal message-id "")))
8591           (error "No message-id in desired parent"))
8592         (gnus-with-article current-article
8593           (goto-char (point-min))
8594           (if (re-search-forward "^References: " nil t)
8595               (progn
8596                 (re-search-forward "^[^ \t]" nil t)
8597                 (forward-line -1)
8598                 (end-of-line)
8599                 (insert " " message-id))
8600             (insert "References: " message-id "\n")))
8601         (set-buffer gnus-summary-buffer)
8602         (gnus-summary-unmark-all-processable)
8603         (gnus-summary-update-article current-article)
8604         (gnus-summary-rethread-current)
8605         (gnus-message 3 "Article %d is now the child of article %d"
8606                       current-article parent-article)))))
8607
8608 (defun gnus-summary-toggle-threads (&optional arg)
8609   "Toggle showing conversation threads.
8610 If ARG is positive number, turn showing conversation threads on."
8611   (interactive "P")
8612   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8613     (setq gnus-show-threads
8614           (if (null arg) (not gnus-show-threads)
8615             (> (prefix-numeric-value arg) 0)))
8616     (gnus-summary-prepare)
8617     (gnus-summary-goto-subject current)
8618     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8619     (gnus-summary-position-point)))
8620
8621 (defun gnus-summary-show-all-threads ()
8622   "Show all threads."
8623   (interactive)
8624   (save-excursion
8625     (let ((buffer-read-only nil))
8626       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8627   (gnus-summary-position-point))
8628
8629 (defun gnus-summary-show-thread ()
8630   "Show thread subtrees.
8631 Returns nil if no thread was there to be shown."
8632   (interactive)
8633   (let ((buffer-read-only nil)
8634         (orig (point))
8635         ;; first goto end then to beg, to have point at beg after let
8636         (end (progn (end-of-line) (point)))
8637         (beg (progn (beginning-of-line) (point))))
8638     (prog1
8639         ;; Any hidden lines here?
8640         (search-forward "\r" end t)
8641       (subst-char-in-region beg end ?\^M ?\n t)
8642       (goto-char orig)
8643       (gnus-summary-position-point))))
8644
8645 (defun gnus-summary-hide-all-threads ()
8646   "Hide all thread subtrees."
8647   (interactive)
8648   (save-excursion
8649     (goto-char (point-min))
8650     (gnus-summary-hide-thread)
8651     (while (zerop (gnus-summary-next-thread 1 t))
8652       (gnus-summary-hide-thread)))
8653   (gnus-summary-position-point))
8654
8655 (defun gnus-summary-hide-thread ()
8656   "Hide thread subtrees.
8657 Returns nil if no threads were there to be hidden."
8658   (interactive)
8659   (let ((buffer-read-only nil)
8660         (start (point))
8661         (article (gnus-summary-article-number)))
8662     (goto-char start)
8663     ;; Go forward until either the buffer ends or the subthread
8664     ;; ends.
8665     (when (and (not (eobp))
8666                (or (zerop (gnus-summary-next-thread 1 t))
8667                    (goto-char (point-max))))
8668       (prog1
8669           (if (and (> (point) start)
8670                    (search-backward "\n" start t))
8671               (progn
8672                 (subst-char-in-region start (point) ?\n ?\^M)
8673                 (gnus-summary-goto-subject article))
8674             (goto-char start)
8675             nil)))))
8676
8677 (defun gnus-summary-go-to-next-thread (&optional previous)
8678   "Go to the same level (or less) next thread.
8679 If PREVIOUS is non-nil, go to previous thread instead.
8680 Return the article number moved to, or nil if moving was impossible."
8681   (let ((level (gnus-summary-thread-level))
8682         (way (if previous -1 1))
8683         (beg (point)))
8684     (forward-line way)
8685     (while (and (not (eobp))
8686                 (< level (gnus-summary-thread-level)))
8687       (forward-line way))
8688     (if (eobp)
8689         (progn
8690           (goto-char beg)
8691           nil)
8692       (setq beg (point))
8693       (prog1
8694           (gnus-summary-article-number)
8695         (goto-char beg)))))
8696
8697 (defun gnus-summary-next-thread (n &optional silent)
8698   "Go to the same level next N'th thread.
8699 If N is negative, search backward instead.
8700 Returns the difference between N and the number of skips actually
8701 done.
8702
8703 If SILENT, don't output messages."
8704   (interactive "p")
8705   (let ((backward (< n 0))
8706         (n (abs n)))
8707     (while (and (> n 0)
8708                 (gnus-summary-go-to-next-thread backward))
8709       (decf n))
8710     (unless silent
8711       (gnus-summary-position-point))
8712     (when (and (not silent) (/= 0 n))
8713       (gnus-message 7 "No more threads"))
8714     n))
8715
8716 (defun gnus-summary-prev-thread (n)
8717   "Go to the same level previous N'th thread.
8718 Returns the difference between N and the number of skips actually
8719 done."
8720   (interactive "p")
8721   (gnus-summary-next-thread (- n)))
8722
8723 (defun gnus-summary-go-down-thread ()
8724   "Go down one level in the current thread."
8725   (let ((children (gnus-summary-article-children)))
8726     (when children
8727       (gnus-summary-goto-subject (car children)))))
8728
8729 (defun gnus-summary-go-up-thread ()
8730   "Go up one level in the current thread."
8731   (let ((parent (gnus-summary-article-parent)))
8732     (when parent
8733       (gnus-summary-goto-subject parent))))
8734
8735 (defun gnus-summary-down-thread (n)
8736   "Go down thread N steps.
8737 If N is negative, go up instead.
8738 Returns the difference between N and how many steps down that were
8739 taken."
8740   (interactive "p")
8741   (let ((up (< n 0))
8742         (n (abs n)))
8743     (while (and (> n 0)
8744                 (if up (gnus-summary-go-up-thread)
8745                   (gnus-summary-go-down-thread)))
8746       (setq n (1- n)))
8747     (gnus-summary-position-point)
8748     (when (/= 0 n)
8749       (gnus-message 7 "Can't go further"))
8750     n))
8751
8752 (defun gnus-summary-up-thread (n)
8753   "Go up thread N steps.
8754 If N is negative, go up instead.
8755 Returns the difference between N and how many steps down that were
8756 taken."
8757   (interactive "p")
8758   (gnus-summary-down-thread (- n)))
8759
8760 (defun gnus-summary-top-thread ()
8761   "Go to the top of the thread."
8762   (interactive)
8763   (while (gnus-summary-go-up-thread))
8764   (gnus-summary-article-number))
8765
8766 (defun gnus-summary-kill-thread (&optional unmark)
8767   "Mark articles under current thread as read.
8768 If the prefix argument is positive, remove any kinds of marks.
8769 If the prefix argument is negative, tick articles instead."
8770   (interactive "P")
8771   (when unmark
8772     (setq unmark (prefix-numeric-value unmark)))
8773   (let ((articles (gnus-summary-articles-in-thread)))
8774     (save-excursion
8775       ;; Expand the thread.
8776       (gnus-summary-show-thread)
8777       ;; Mark all the articles.
8778       (while articles
8779         (gnus-summary-goto-subject (car articles))
8780         (cond ((null unmark)
8781                (gnus-summary-mark-article-as-read gnus-killed-mark))
8782               ((> unmark 0)
8783                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8784               (t
8785                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8786         (setq articles (cdr articles))))
8787     ;; Hide killed subtrees.
8788     (and (null unmark)
8789          gnus-thread-hide-killed
8790          (gnus-summary-hide-thread))
8791     ;; If marked as read, go to next unread subject.
8792     (when (null unmark)
8793       ;; Go to next unread subject.
8794       (gnus-summary-next-subject 1 t)))
8795   (gnus-set-mode-line 'summary))
8796
8797 ;; Summary sorting commands
8798
8799 (defun gnus-summary-sort-by-number (&optional reverse)
8800   "Sort the summary buffer by article number.
8801 Argument REVERSE means reverse order."
8802   (interactive "P")
8803   (gnus-summary-sort 'number reverse))
8804
8805 (defun gnus-summary-sort-by-author (&optional reverse)
8806   "Sort the summary buffer by author name alphabetically.
8807 If case-fold-search is non-nil, case of letters is ignored.
8808 Argument REVERSE means reverse order."
8809   (interactive "P")
8810   (gnus-summary-sort 'author reverse))
8811
8812 (defun gnus-summary-sort-by-subject (&optional reverse)
8813   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8814 If case-fold-search is non-nil, case of letters is ignored.
8815 Argument REVERSE means reverse order."
8816   (interactive "P")
8817   (gnus-summary-sort 'subject reverse))
8818
8819 (defun gnus-summary-sort-by-date (&optional reverse)
8820   "Sort the summary buffer by date.
8821 Argument REVERSE means reverse order."
8822   (interactive "P")
8823   (gnus-summary-sort 'date reverse))
8824
8825 (defun gnus-summary-sort-by-score (&optional reverse)
8826   "Sort the summary buffer by score.
8827 Argument REVERSE means reverse order."
8828   (interactive "P")
8829   (gnus-summary-sort 'score reverse))
8830
8831 (defun gnus-summary-sort-by-lines (&optional reverse)
8832   "Sort the summary buffer by the number of lines.
8833 Argument REVERSE means reverse order."
8834   (interactive "P")
8835   (gnus-summary-sort 'lines reverse))
8836
8837 (defun gnus-summary-sort-by-chars (&optional reverse)
8838   "Sort the summary buffer by article length.
8839 Argument REVERSE means reverse order."
8840   (interactive "P")
8841   (gnus-summary-sort 'chars reverse))   
8842
8843 (defun gnus-summary-sort (predicate reverse)
8844   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8845   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8846          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8847          (gnus-thread-sort-functions
8848           (if (not reverse)
8849               thread
8850             `(lambda (t1 t2)
8851                (,thread t2 t1))))
8852          (gnus-article-sort-functions
8853           (if (not reverse)
8854               article
8855             `(lambda (t1 t2)
8856                (,article t2 t1))))
8857          (buffer-read-only)
8858          (gnus-summary-prepare-hook nil))
8859     ;; We do the sorting by regenerating the threads.
8860     (gnus-summary-prepare)
8861     ;; Hide subthreads if needed.
8862     (when (and gnus-show-threads gnus-thread-hide-subtree)
8863       (gnus-summary-hide-all-threads))))
8864
8865 ;; Summary saving commands.
8866
8867 (defun gnus-summary-save-article (&optional n not-saved)
8868   "Save the current article using the default saver function.
8869 If N is a positive number, save the N next articles.
8870 If N is a negative number, save the N previous articles.
8871 If N is nil and any articles have been marked with the process mark,
8872 save those articles instead.
8873 The variable `gnus-default-article-saver' specifies the saver function."
8874   (interactive "P")
8875   (let* ((articles (gnus-summary-work-articles n))
8876          (save-buffer (save-excursion
8877                         (nnheader-set-temp-buffer " *Gnus Save*")))
8878          (num (length articles))
8879          header file)
8880     (dolist (article articles)
8881       (setq header (gnus-summary-article-header article))
8882       (if (not (vectorp header))
8883           ;; This is a pseudo-article.
8884           (if (assq 'name header)
8885               (gnus-copy-file (cdr (assq 'name header)))
8886             (gnus-message 1 "Article %d is unsaveable" article))
8887         ;; This is a real article.
8888         (save-window-excursion
8889           (gnus-summary-select-article t nil nil article))
8890         (save-excursion
8891           (set-buffer save-buffer)
8892           (erase-buffer)
8893           (insert-buffer-substring gnus-original-article-buffer))
8894         (setq file (gnus-article-save save-buffer file num))
8895         (gnus-summary-remove-process-mark article)
8896         (unless not-saved
8897           (gnus-summary-set-saved-mark article))))
8898     (gnus-kill-buffer save-buffer)
8899     (gnus-summary-position-point)
8900     (gnus-set-mode-line 'summary)
8901     n))
8902
8903 (defun gnus-summary-pipe-output (&optional arg)
8904   "Pipe the current article to a subprocess.
8905 If N is a positive number, pipe the N next articles.
8906 If N is a negative number, pipe the N previous articles.
8907 If N is nil and any articles have been marked with the process mark,
8908 pipe those articles instead."
8909   (interactive "P")
8910   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8911     (gnus-summary-save-article arg t))
8912   (gnus-configure-windows 'pipe))
8913
8914 (defun gnus-summary-save-article-mail (&optional arg)
8915   "Append the current article to an mail file.
8916 If N is a positive number, save the N next articles.
8917 If N is a negative number, save the N previous articles.
8918 If N is nil and any articles have been marked with the process mark,
8919 save those articles instead."
8920   (interactive "P")
8921   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8922     (gnus-summary-save-article arg)))
8923
8924 (defun gnus-summary-save-article-rmail (&optional arg)
8925   "Append the current article to an rmail file.
8926 If N is a positive number, save the N next articles.
8927 If N is a negative number, save the N previous articles.
8928 If N is nil and any articles have been marked with the process mark,
8929 save those articles instead."
8930   (interactive "P")
8931   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8932     (gnus-summary-save-article arg)))
8933
8934 (defun gnus-summary-save-article-file (&optional arg)
8935   "Append the current article to a file.
8936 If N is a positive number, save the N next articles.
8937 If N is a negative number, save the N previous articles.
8938 If N is nil and any articles have been marked with the process mark,
8939 save those articles instead."
8940   (interactive "P")
8941   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8942     (gnus-summary-save-article arg)))
8943
8944 (defun gnus-summary-write-article-file (&optional arg)
8945   "Write the current article to a file, deleting the previous file.
8946 If N is a positive number, save the N next articles.
8947 If N is a negative number, save the N previous articles.
8948 If N is nil and any articles have been marked with the process mark,
8949 save those articles instead."
8950   (interactive "P")
8951   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8952     (gnus-summary-save-article arg)))
8953
8954 (defun gnus-summary-save-article-body-file (&optional arg)
8955   "Append the current article body to a file.
8956 If N is a positive number, save the N next articles.
8957 If N is a negative number, save the N previous articles.
8958 If N is nil and any articles have been marked with the process mark,
8959 save those articles instead."
8960   (interactive "P")
8961   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8962     (gnus-summary-save-article arg)))
8963
8964 (defun gnus-summary-pipe-message (program)
8965   "Pipe the current article through PROGRAM."
8966   (interactive "sProgram: ")
8967   (gnus-summary-select-article)
8968   (let ((mail-header-separator ""))
8969     (gnus-eval-in-buffer-window gnus-article-buffer
8970       (save-restriction
8971         (widen)
8972         (let ((start (window-start))
8973               buffer-read-only)
8974           (message-pipe-buffer-body program)
8975           (set-window-start (get-buffer-window (current-buffer)) start))))))
8976
8977 (defun gnus-get-split-value (methods)
8978   "Return a value based on the split METHODS."
8979   (let (split-name method result match)
8980     (when methods
8981       (save-excursion
8982         (set-buffer gnus-original-article-buffer)
8983         (save-restriction
8984           (nnheader-narrow-to-headers)
8985           (while methods
8986             (goto-char (point-min))
8987             (setq method (pop methods))
8988             (setq match (car method))
8989             (when (cond
8990                    ((stringp match)
8991                     ;; Regular expression.
8992                     (ignore-errors
8993                       (re-search-forward match nil t)))
8994                    ((gnus-functionp match)
8995                     ;; Function.
8996                     (save-restriction
8997                       (widen)
8998                       (setq result (funcall match gnus-newsgroup-name))))
8999                    ((consp match)
9000                     ;; Form.
9001                     (save-restriction
9002                       (widen)
9003                       (setq result (eval match)))))
9004               (setq split-name (append (cdr method) split-name))
9005               (cond ((stringp result)
9006                      (push (expand-file-name
9007                             result gnus-article-save-directory)
9008                            split-name))
9009                     ((consp result)
9010                      (setq split-name (append result split-name)))))))))
9011     (nreverse split-name)))
9012
9013 (defun gnus-valid-move-group-p (group)
9014   (and (boundp group)
9015        (symbol-name group)
9016        (symbol-value group)
9017        (gnus-get-function (gnus-find-method-for-group
9018                            (symbol-name group)) 'request-accept-article t)))
9019
9020 (defun gnus-read-move-group-name (prompt default articles prefix)
9021   "Read a group name."
9022   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9023          (minibuffer-confirm-incomplete nil) ; XEmacs
9024          (prom
9025           (format "%s %s to:"
9026                   prompt
9027                   (if (> (length articles) 1)
9028                       (format "these %d articles" (length articles))
9029                     "this article")))
9030          (to-newsgroup
9031           (cond
9032            ((null split-name)
9033             (gnus-completing-read default prom
9034                                   gnus-active-hashtb
9035                                   'gnus-valid-move-group-p
9036                                   nil prefix
9037                                   'gnus-group-history))
9038            ((= 1 (length split-name))
9039             (gnus-completing-read (car split-name) prom
9040                                   gnus-active-hashtb
9041                                   'gnus-valid-move-group-p
9042                                   nil nil
9043                                   'gnus-group-history))
9044            (t
9045             (gnus-completing-read nil prom
9046                                   (mapcar (lambda (el) (list el))
9047                                           (nreverse split-name))
9048                                   nil nil nil
9049                                   'gnus-group-history))))
9050          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9051     (when to-newsgroup
9052       (if (or (string= to-newsgroup "")
9053               (string= to-newsgroup prefix))
9054           (setq to-newsgroup default))
9055       (unless to-newsgroup
9056         (error "No group name entered"))
9057       (or (gnus-active to-newsgroup)
9058           (gnus-activate-group to-newsgroup nil nil to-method)
9059           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9060                                      to-newsgroup))
9061               (or (and (gnus-request-create-group to-newsgroup to-method)
9062                        (gnus-activate-group
9063                         to-newsgroup nil nil to-method)
9064                        (gnus-subscribe-group to-newsgroup))
9065                   (error "Couldn't create group %s" to-newsgroup)))
9066           (error "No such group: %s" to-newsgroup)))
9067     to-newsgroup))
9068
9069 (defun gnus-summary-save-parts (type dir n &optional reverse)
9070   "Save parts matching TYPE to DIR.
9071 If REVERSE, save parts that do not match TYPE."
9072   (interactive
9073    (list (read-string "Save parts of type: " "image/.*")
9074          (read-file-name "Save to directory: " nil nil t)
9075          current-prefix-arg))
9076   (gnus-summary-iterate n
9077     (let ((gnus-display-mime-function nil)
9078           (gnus-inhibit-treatment t))
9079       (gnus-summary-select-article))
9080     (save-excursion
9081       (set-buffer gnus-article-buffer)
9082       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9083         (when handles
9084           (gnus-summary-save-parts-1 type dir handles reverse)
9085           (mm-destroy-parts handles))))))
9086
9087 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9088   (if (stringp (car handle))
9089       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9090               (cdr handle))
9091     (when (if reverse
9092               (not (string-match type (mm-handle-media-type handle)))
9093             (string-match type (mm-handle-media-type handle)))
9094       (let ((file (expand-file-name
9095                    (file-name-nondirectory
9096                     (or
9097                      (mail-content-type-get
9098                       (mm-handle-disposition handle) 'filename)
9099                      (concat gnus-newsgroup-name "." gnus-current-article)))
9100                    dir)))
9101         (unless (file-exists-p file)
9102           (mm-save-part-to-file handle file))))))
9103
9104 ;; Summary extract commands
9105
9106 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9107   (let ((buffer-read-only nil)
9108         (article (gnus-summary-article-number))
9109         after-article b e)
9110     (unless (gnus-summary-goto-subject article)
9111       (error "No such article: %d" article))
9112     (gnus-summary-position-point)
9113     ;; If all commands are to be bunched up on one line, we collect
9114     ;; them here.
9115     (unless gnus-view-pseudos-separately
9116       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9117             files action)
9118         (while ps
9119           (setq action (cdr (assq 'action (car ps))))
9120           (setq files (list (cdr (assq 'name (car ps)))))
9121           (while (and ps (cdr ps)
9122                       (string= (or action "1")
9123                                (or (cdr (assq 'action (cadr ps))) "2")))
9124             (push (cdr (assq 'name (cadr ps))) files)
9125             (setcdr ps (cddr ps)))
9126           (when files
9127             (when (not (string-match "%s" action))
9128               (push " " files))
9129             (push " " files)
9130             (when (assq 'execute (car ps))
9131               (setcdr (assq 'execute (car ps))
9132                       (funcall (if (string-match "%s" action)
9133                                    'format 'concat)
9134                                action
9135                                (mapconcat
9136                                 (lambda (f)
9137                                   (if (equal f " ")
9138                                       f
9139                                     (mm-quote-arg f)))
9140                                 files " ")))))
9141           (setq ps (cdr ps)))))
9142     (if (and gnus-view-pseudos (not not-view))
9143         (while pslist
9144           (when (assq 'execute (car pslist))
9145             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9146                                   (eq gnus-view-pseudos 'not-confirm)))
9147           (setq pslist (cdr pslist)))
9148       (save-excursion
9149         (while pslist
9150           (setq after-article (or (cdr (assq 'article (car pslist)))
9151                                   (gnus-summary-article-number)))
9152           (gnus-summary-goto-subject after-article)
9153           (forward-line 1)
9154           (setq b (point))
9155           (insert "    " (file-name-nondirectory
9156                           (cdr (assq 'name (car pslist))))
9157                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9158           (setq e (point))
9159           (forward-line -1)             ; back to `b'
9160           (gnus-add-text-properties
9161            b (1- e) (list 'gnus-number gnus-reffed-article-number
9162                           gnus-mouse-face-prop gnus-mouse-face))
9163           (gnus-data-enter
9164            after-article gnus-reffed-article-number
9165            gnus-unread-mark b (car pslist) 0 (- e b))
9166           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9167           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9168           (setq pslist (cdr pslist)))))))
9169
9170 (defun gnus-pseudos< (p1 p2)
9171   (let ((c1 (cdr (assq 'action p1)))
9172         (c2 (cdr (assq 'action p2))))
9173     (and c1 c2 (string< c1 c2))))
9174
9175 (defun gnus-request-pseudo-article (props)
9176   (cond ((assq 'execute props)
9177          (gnus-execute-command (cdr (assq 'execute props)))))
9178   (let ((gnus-current-article (gnus-summary-article-number)))
9179     (gnus-run-hooks 'gnus-mark-article-hook)))
9180
9181 (defun gnus-execute-command (command &optional automatic)
9182   (save-excursion
9183     (gnus-article-setup-buffer)
9184     (set-buffer gnus-article-buffer)
9185     (setq buffer-read-only nil)
9186     (let ((command (if automatic command
9187                      (read-string "Command: " (cons command 0)))))
9188       (erase-buffer)
9189       (insert "$ " command "\n\n")
9190       (if gnus-view-pseudo-asynchronously
9191           (start-process "gnus-execute" (current-buffer) shell-file-name
9192                          shell-command-switch command)
9193         (call-process shell-file-name nil t nil
9194                       shell-command-switch command)))))
9195
9196 ;; Summary kill commands.
9197
9198 (defun gnus-summary-edit-global-kill (article)
9199   "Edit the \"global\" kill file."
9200   (interactive (list (gnus-summary-article-number)))
9201   (gnus-group-edit-global-kill article))
9202
9203 (defun gnus-summary-edit-local-kill ()
9204   "Edit a local kill file applied to the current newsgroup."
9205   (interactive)
9206   (setq gnus-current-headers (gnus-summary-article-header))
9207   (gnus-group-edit-local-kill
9208    (gnus-summary-article-number) gnus-newsgroup-name))
9209
9210 ;;; Header reading.
9211
9212 (defun gnus-read-header (id &optional header)
9213   "Read the headers of article ID and enter them into the Gnus system."
9214   (let ((group gnus-newsgroup-name)
9215         (gnus-override-method
9216          (or
9217           gnus-override-method
9218           (and (gnus-news-group-p gnus-newsgroup-name)
9219                (car (gnus-refer-article-methods)))))
9220         where)
9221     ;; First we check to see whether the header in question is already
9222     ;; fetched.
9223     (if (stringp id)
9224         ;; This is a Message-ID.
9225         (setq header (or header (gnus-id-to-header id)))
9226       ;; This is an article number.
9227       (setq header (or header (gnus-summary-article-header id))))
9228     (if (and header
9229              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9230         ;; We have found the header.
9231         header
9232       ;; If this is a sparse article, we have to nix out its
9233       ;; previous entry in the thread hashtb.
9234       (when (and header
9235                  (gnus-summary-article-sparse-p (mail-header-number header)))
9236         (let* ((parent (gnus-parent-id (mail-header-references header)))
9237                (thread (and parent (gnus-id-to-thread parent))))
9238           (when thread
9239             (delq (assq header thread) thread))))
9240       ;; We have to really fetch the header to this article.
9241       (save-excursion
9242         (set-buffer nntp-server-buffer)
9243         (when (setq where (gnus-request-head id group))
9244           (nnheader-fold-continuation-lines)
9245           (goto-char (point-max))
9246           (insert ".\n")
9247           (goto-char (point-min))
9248           (insert "211 ")
9249           (princ (cond
9250                   ((numberp id) id)
9251                   ((cdr where) (cdr where))
9252                   (header (mail-header-number header))
9253                   (t gnus-reffed-article-number))
9254                  (current-buffer))
9255           (insert " Article retrieved.\n"))
9256         (if (or (not where)
9257                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9258             ()                          ; Malformed head.
9259           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9260             (when (and (stringp id)
9261                        (not (string= (gnus-group-real-name group)
9262                                      (car where))))
9263               ;; If we fetched by Message-ID and the article came
9264               ;; from a different group, we fudge some bogus article
9265               ;; numbers for this article.
9266               (mail-header-set-number header gnus-reffed-article-number))
9267             (save-excursion
9268               (set-buffer gnus-summary-buffer)
9269               (decf gnus-reffed-article-number)
9270               (gnus-remove-header (mail-header-number header))
9271               (push header gnus-newsgroup-headers)
9272               (setq gnus-current-headers header)
9273               (push (mail-header-number header) gnus-newsgroup-limit)))
9274           header)))))
9275
9276 (defun gnus-remove-header (number)
9277   "Remove header NUMBER from `gnus-newsgroup-headers'."
9278   (if (and gnus-newsgroup-headers
9279            (= number (mail-header-number (car gnus-newsgroup-headers))))
9280       (pop gnus-newsgroup-headers)
9281     (let ((headers gnus-newsgroup-headers))
9282       (while (and (cdr headers)
9283                   (not (= number (mail-header-number (cadr headers)))))
9284         (pop headers))
9285       (when (cdr headers)
9286         (setcdr headers (cddr headers))))))
9287
9288 ;;;
9289 ;;; summary highlights
9290 ;;;
9291
9292 (defun gnus-highlight-selected-summary ()
9293   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9294   ;; Highlight selected article in summary buffer
9295   (when gnus-summary-selected-face
9296     (save-excursion
9297       (let* ((beg (progn (beginning-of-line) (point)))
9298              (end (progn (end-of-line) (point)))
9299              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9300              (from (if (get-text-property beg gnus-mouse-face-prop)
9301                        beg
9302                      (or (next-single-property-change
9303                           beg gnus-mouse-face-prop nil end)
9304                          beg)))
9305              (to
9306               (if (= from end)
9307                   (- from 2)
9308                 (or (next-single-property-change
9309                      from gnus-mouse-face-prop nil end)
9310                     end))))
9311         ;; If no mouse-face prop on line we will have to = from = end,
9312         ;; so we highlight the entire line instead.
9313         (when (= (+ to 2) from)
9314           (setq from beg)
9315           (setq to end))
9316         (if gnus-newsgroup-selected-overlay
9317             ;; Move old overlay.
9318             (gnus-move-overlay
9319              gnus-newsgroup-selected-overlay from to (current-buffer))
9320           ;; Create new overlay.
9321           (gnus-overlay-put
9322            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9323            'face gnus-summary-selected-face))))))
9324
9325 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9326 (defun gnus-summary-highlight-line ()
9327   "Highlight current line according to `gnus-summary-highlight'."
9328   (let* ((list gnus-summary-highlight)
9329          (p (point))
9330          (end (progn (end-of-line) (point)))
9331          ;; now find out where the line starts and leave point there.
9332          (beg (progn (beginning-of-line) (point)))
9333          (article (gnus-summary-article-number))
9334          (score (or (cdr (assq (or article gnus-current-article)
9335                                gnus-newsgroup-scored))
9336                     gnus-summary-default-score 0))
9337          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9338          (inhibit-read-only t))
9339     ;; Eval the cars of the lists until we find a match.
9340     (let ((default gnus-summary-default-score))
9341       (while (and list
9342                   (not (eval (caar list))))
9343         (setq list (cdr list))))
9344     (let ((face (cdar list)))
9345       (unless (eq face (get-text-property beg 'face))
9346         (gnus-put-text-property-excluding-characters-with-faces
9347          beg end 'face
9348          (setq face (if (boundp face) (symbol-value face) face)))
9349         (when gnus-summary-highlight-line-function
9350           (funcall gnus-summary-highlight-line-function article face))))
9351     (goto-char p)))
9352
9353 (defun gnus-update-read-articles (group unread &optional compute)
9354   "Update the list of read articles in GROUP."
9355   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9356          (entry (gnus-gethash group gnus-newsrc-hashtb))
9357          (info (nth 2 entry))
9358          (prev 1)
9359          (unread (sort (copy-sequence unread) '<))
9360          read)
9361     (if (or (not info) (not active))
9362         ;; There is no info on this group if it was, in fact,
9363         ;; killed.  Gnus stores no information on killed groups, so
9364         ;; there's nothing to be done.
9365         ;; One could store the information somewhere temporarily,
9366         ;; perhaps...  Hmmm...
9367         ()
9368       ;; Remove any negative articles numbers.
9369       (while (and unread (< (car unread) 0))
9370         (setq unread (cdr unread)))
9371       ;; Remove any expired article numbers
9372       (while (and unread (< (car unread) (car active)))
9373         (setq unread (cdr unread)))
9374       ;; Compute the ranges of read articles by looking at the list of
9375       ;; unread articles.
9376       (while unread
9377         (when (/= (car unread) prev)
9378           (push (if (= prev (1- (car unread))) prev
9379                   (cons prev (1- (car unread))))
9380                 read))
9381         (setq prev (1+ (car unread)))
9382         (setq unread (cdr unread)))
9383       (when (<= prev (cdr active))
9384         (push (cons prev (cdr active)) read))
9385       (setq read (if (> (length read) 1) (nreverse read) read))
9386       (if compute
9387           read
9388         (save-excursion
9389           (set-buffer gnus-group-buffer)
9390           (gnus-undo-register
9391             `(progn
9392                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9393                (gnus-info-set-read ',info ',(gnus-info-read info))
9394                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9395                (gnus-group-update-group ,group t))))
9396         ;; Propagate the read marks to the backend.
9397         (if (gnus-check-backend-function 'request-set-mark group)
9398             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9399                   (add (gnus-remove-from-range read (gnus-info-read info))))
9400               (when (or add del)
9401                 (unless (gnus-check-group group)
9402                   (error "Can't open server for %s" group))
9403                 (gnus-request-set-mark
9404                  group (delq nil (list (if add (list add 'add '(read)))
9405                                        (if del (list del 'del '(read)))))))))
9406         ;; Enter this list into the group info.
9407         (gnus-info-set-read info read)
9408         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9409         (gnus-get-unread-articles-in-group info (gnus-active group))
9410         t))))
9411
9412 (defun gnus-offer-save-summaries ()
9413   "Offer to save all active summary buffers."
9414   (save-excursion
9415     (let ((buflist (buffer-list))
9416           buffers bufname)
9417       ;; Go through all buffers and find all summaries.
9418       (while buflist
9419         (and (setq bufname (buffer-name (car buflist)))
9420              (string-match "Summary" bufname)
9421              (save-excursion
9422                (set-buffer bufname)
9423                ;; We check that this is, indeed, a summary buffer.
9424                (and (eq major-mode 'gnus-summary-mode)
9425                     ;; Also make sure this isn't bogus.
9426                     gnus-newsgroup-prepared
9427                     ;; Also make sure that this isn't a dead summary buffer.
9428                     (not gnus-dead-summary-mode)))
9429              (push bufname buffers))
9430         (setq buflist (cdr buflist)))
9431       ;; Go through all these summary buffers and offer to save them.
9432       (when buffers
9433         (map-y-or-n-p
9434          "Update summary buffer %s? "
9435          (lambda (buf)
9436            (switch-to-buffer buf)
9437            (gnus-summary-exit))
9438          buffers)))))
9439
9440 (defun gnus-summary-setup-default-charset ()
9441   "Setup newsgroup default charset."
9442   (if (equal gnus-newsgroup-name "nndraft:drafts")
9443       (setq gnus-newsgroup-charset nil)
9444     (let* ((name (and gnus-newsgroup-name
9445                       (gnus-group-real-name gnus-newsgroup-name)))
9446            (ignored-charsets 
9447             (or gnus-newsgroup-ephemeral-ignored-charsets
9448                 (append
9449                  (and gnus-newsgroup-name
9450                       (or (gnus-group-find-parameter gnus-newsgroup-name
9451                                                      'ignored-charsets t)
9452                           (let ((alist gnus-group-ignored-charsets-alist)
9453                                 elem (charsets nil))
9454                             (while (setq elem (pop alist))
9455                               (when (and name
9456                                          (string-match (car elem) name))
9457                                 (setq alist nil
9458                                       charsets (cdr elem))))
9459                             charsets)))
9460                  gnus-newsgroup-ignored-charsets))))
9461       (setq gnus-newsgroup-charset
9462             (or gnus-newsgroup-ephemeral-charset
9463                 (and gnus-newsgroup-name
9464                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9465                          (let ((alist gnus-group-charset-alist)
9466                                elem charset)
9467                            (while (setq elem (pop alist))
9468                              (when (and name
9469                                         (string-match (car elem) name))
9470                                (setq alist nil
9471                                      charset (cadr elem))))
9472                            charset)))
9473                 gnus-default-charset))
9474       (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9475            ignored-charsets))))
9476
9477 ;;;
9478 ;;; Mime Commands
9479 ;;;
9480
9481 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9482   "Display the current article buffer fully MIME-buttonized.
9483 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9484 treated as multipart/mixed."
9485   (interactive "P")
9486   (require 'gnus-art)
9487   (let ((gnus-unbuttonized-mime-types nil)
9488         (gnus-mime-display-multipart-as-mixed show-all-parts))
9489     (gnus-summary-show-article)))
9490
9491 (defun gnus-summary-repair-multipart (article)
9492   "Add a Content-Type header to a multipart article without one."
9493   (interactive (list (gnus-summary-article-number)))
9494   (gnus-with-article article
9495     (message-narrow-to-head)
9496     (goto-char (point-max))
9497     (widen)
9498     (when (search-forward "\n--" nil t)
9499       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9500         (message-narrow-to-head)
9501         (message-remove-header "Mime-Version")
9502         (message-remove-header "Content-Type")
9503         (goto-char (point-max))
9504         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9505                         separator))
9506         (insert "Mime-Version: 1.0\n")
9507         (widen))))
9508   (let (gnus-mark-article-hook)
9509     (gnus-summary-select-article t t nil article)))
9510
9511 (defun gnus-summary-toggle-display-buttonized ()
9512   "Toggle the buttonizing of the article buffer."
9513   (interactive)
9514   (require 'gnus-art)
9515   (if (setq gnus-inhibit-mime-unbuttonizing
9516             (not gnus-inhibit-mime-unbuttonizing))
9517       (let ((gnus-unbuttonized-mime-types nil))
9518         (gnus-summary-show-article))
9519     (gnus-summary-show-article)))
9520
9521 ;;;
9522 ;;; with article
9523 ;;;
9524
9525 (defmacro gnus-with-article (article &rest forms)
9526   "Select ARTICLE and perform FORMS in the original article buffer.
9527 Then replace the article with the result."
9528   `(progn
9529      ;; We don't want the article to be marked as read.
9530      (let (gnus-mark-article-hook)
9531        (gnus-summary-select-article t t nil ,article))
9532      (set-buffer gnus-original-article-buffer)
9533      ,@forms
9534      (if (not (gnus-check-backend-function
9535                'request-replace-article (car gnus-article-current)))
9536          (gnus-message 5 "Read-only group; not replacing")
9537        (unless (gnus-request-replace-article
9538                 ,article (car gnus-article-current)
9539                 (current-buffer) t)
9540          (error "Couldn't replace article")))
9541      ;; The cache and backlog have to be flushed somewhat.
9542      (when gnus-keep-backlog
9543        (gnus-backlog-remove-article
9544         (car gnus-article-current) (cdr gnus-article-current)))
9545      (when gnus-use-cache
9546        (gnus-cache-update-article
9547         (car gnus-article-current) (cdr gnus-article-current)))))
9548
9549 (put 'gnus-with-article 'lisp-indent-function 1)
9550 (put 'gnus-with-article 'edebug-form-spec '(form body))
9551
9552 ;;;
9553 ;;; Generic summary marking commands
9554 ;;;
9555
9556 (defvar gnus-summary-marking-alist
9557   '((read gnus-del-mark "d")
9558     (unread gnus-unread-mark "u")
9559     (ticked gnus-ticked-mark "!")
9560     (dormant gnus-dormant-mark "?")
9561     (expirable gnus-expirable-mark "e"))
9562   "An alist of names/marks/keystrokes.")
9563
9564 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9565 (defvar gnus-summary-mark-map)
9566
9567 (defun gnus-summary-make-all-marking-commands ()
9568   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9569   (dolist (elem gnus-summary-marking-alist)
9570     (apply 'gnus-summary-make-marking-command elem)))
9571
9572 (defun gnus-summary-make-marking-command (name mark keystroke)
9573   (let ((map (make-sparse-keymap)))
9574     (define-key gnus-summary-generic-mark-map keystroke map)
9575     (dolist (lway `((next "next" next nil "n")
9576                     (next-unread "next unread" next t "N")
9577                     (prev "previous" prev nil "p")
9578                     (prev-unread "previous unread" prev t "P")
9579                     (nomove "" nil nil ,keystroke)))
9580       (let ((func (gnus-summary-make-marking-command-1
9581                    mark (car lway) lway name)))
9582         (setq func (eval func))
9583         (define-key map (nth 4 lway) func)))))
9584       
9585 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9586   `(defun ,(intern
9587             (format "gnus-summary-put-mark-as-%s%s"
9588                     name (if (eq way 'nomove)
9589                              ""
9590                            (concat "-" (symbol-name way)))))
9591      (n)
9592      ,(format
9593        "Mark the current article as %s%s.
9594 If N, the prefix, then repeat N times.
9595 If N is negative, move in reverse order.
9596 The difference between N and the actual number of articles marked is
9597 returned."
9598        name (cadr lway))
9599      (interactive "p")
9600      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9601     
9602 (defun gnus-summary-generic-mark (n mark move unread)
9603   "Mark N articles with MARK."
9604   (unless (eq major-mode 'gnus-summary-mode)
9605     (error "This command can only be used in the summary buffer"))
9606   (gnus-summary-show-thread)
9607   (let ((nummove
9608          (cond
9609           ((eq move 'next) 1)
9610           ((eq move 'prev) -1)
9611           (t 0))))
9612     (if (zerop nummove)
9613         (setq n 1)
9614       (when (< n 0)
9615         (setq n (abs n)
9616               nummove (* -1 nummove))))
9617     (while (and (> n 0)
9618                 (gnus-summary-mark-article nil mark)
9619                 (zerop (gnus-summary-next-subject nummove unread t)))
9620       (setq n (1- n)))
9621     (when (/= 0 n)
9622       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9623     (gnus-summary-recenter)
9624     (gnus-summary-position-point)
9625     (gnus-set-mode-line 'summary)
9626     n))
9627
9628 (gnus-summary-make-all-marking-commands)
9629
9630 (gnus-ems-redefine)
9631
9632 (provide 'gnus-sum)
9633
9634 (run-hooks 'gnus-sum-load-hook)
9635
9636 ;;; gnus-sum.el ends here