Sync up with gnus-5_8_3.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98,99 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 'mcharset)
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
40
41 (defcustom gnus-kill-summary-on-exit t
42   "*If non-nil, kill the summary buffer when you exit from it.
43 If nil, the summary will become a \"*Dead Summary*\" buffer, and
44 it will be killed sometime later."
45   :group 'gnus-summary-exit
46   :type 'boolean)
47
48 (defcustom gnus-fetch-old-headers nil
49   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
50 If an unread article in the group refers to an older, already read (or
51 just marked as read) article, the old article will not normally be
52 displayed in the Summary buffer.  If this variable is non-nil, Gnus
53 will attempt to grab the headers to the old articles, and thereby
54 build complete threads.  If it has the value `some', only enough
55 headers to connect otherwise loose threads will be displayed.  This
56 variable can also be a number.  In that case, no more than that number
57 of old headers will be fetched.  If it has the value `invisible', all
58 old headers will be fetched, but none will be displayed.
59
60 The server has to support NOV for any of this to work."
61   :group 'gnus-thread
62   :type '(choice (const :tag "off" nil)
63                  (const some)
64                  number
65                  (sexp :menu-tag "other" t)))
66
67 (defcustom gnus-refer-thread-limit 200
68   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
69 If t, fetch all the available old headers."
70   :group 'gnus-thread
71   :type '(choice number
72                  (sexp :menu-tag "other" t)))
73
74 (defcustom gnus-summary-make-false-root 'adopt
75   "*nil means that Gnus won't gather loose threads.
76 If the root of a thread has expired or been read in a previous
77 session, the information necessary to build a complete thread has been
78 lost.  Instead of having many small sub-threads from this original thread
79 scattered all over the summary buffer, Gnus can gather them.
80
81 If non-nil, Gnus will try to gather all loose sub-threads from an
82 original thread into one large thread.
83
84 If this variable is non-nil, it should be one of `none', `adopt',
85 `dummy' or `empty'.
86
87 If this variable is `none', Gnus will not make a false root, but just
88 present the sub-threads after another.
89 If this variable is `dummy', Gnus will create a dummy root that will
90 have all the sub-threads as children.
91 If this variable is `adopt', Gnus will make one of the \"children\"
92 the parent and mark all the step-children as such.
93 If this variable is `empty', the \"children\" are printed with empty
94 subject fields.  (Or rather, they will be printed with a string
95 given by the `gnus-summary-same-subject' variable.)"
96   :group 'gnus-thread
97   :type '(choice (const :tag "off" nil)
98                  (const none)
99                  (const dummy)
100                  (const adopt)
101                  (const empty)))
102
103 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
104   "*A regexp to match subjects to be excluded from loose thread gathering.
105 As loose thread gathering is done on subjects only, that means that
106 there can be many false gatherings performed.  By rooting out certain
107 common subjects, gathering might become saner."
108   :group 'gnus-thread
109   :type 'regexp)
110
111 (defcustom gnus-summary-gather-subject-limit nil
112   "*Maximum length of subject comparisons when gathering loose threads.
113 Use nil to compare full subjects.  Setting this variable to a low
114 number will help gather threads that have been corrupted by
115 newsreaders chopping off subject lines, but it might also mean that
116 unrelated articles that have subject that happen to begin with the
117 same few characters will be incorrectly gathered.
118
119 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
120 comparing subjects."
121   :group 'gnus-thread
122   :type '(choice (const :tag "off" nil)
123                  (const fuzzy)
124                  (sexp :menu-tag "on" t)))
125
126 (defcustom gnus-simplify-subject-functions nil
127   "List of functions taking a string argument that simplify subjects.
128 The functions are applied recursively.
129
130 Useful functions to put in this list include: `gnus-simplify-subject-re',
131 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
132   :group 'gnus-thread
133   :type '(repeat function))
134
135 (defcustom gnus-simplify-ignored-prefixes nil
136   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
137   :group 'gnus-thread
138   :type '(choice (const :tag "off" nil)
139                  regexp))
140
141 (defcustom gnus-build-sparse-threads nil
142   "*If non-nil, fill in the gaps in threads.
143 If `some', only fill in the gaps that are needed to tie loose threads
144 together.  If `more', fill in all leaf nodes that Gnus can find.  If
145 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
146   :group 'gnus-thread
147   :type '(choice (const :tag "off" nil)
148                  (const some)
149                  (const more)
150                  (sexp :menu-tag "all" t)))
151
152 (defcustom gnus-summary-thread-gathering-function
153   'gnus-gather-threads-by-subject
154   "*Function used for gathering loose threads.
155 There are two pre-defined functions: `gnus-gather-threads-by-subject',
156 which only takes Subjects into consideration; and
157 `gnus-gather-threads-by-references', which compared the References
158 headers of the articles to find matches."
159   :group 'gnus-thread
160   :type '(radio (function-item gnus-gather-threads-by-subject)
161                 (function-item gnus-gather-threads-by-references)
162                 (function :tag "other")))
163
164 (defcustom gnus-summary-same-subject ""
165   "*String indicating that the current article has the same subject as the previous.
166 This variable will only be used if the value of
167 `gnus-summary-make-false-root' is `empty'."
168   :group 'gnus-summary-format
169   :type 'string)
170
171 (defcustom gnus-summary-goto-unread t
172   "*If t, many commands will go to the next unread article.
173 This applies to marking commands as well as other commands that
174 \"naturally\" select the next article, like, for instance, `SPC' at
175 the end of an article.
176 If nil, only the marking commands will go to the next (un)read article.
177 If `never', commands that usually go to the next unread article, will
178 go to the next article, whether it is read or not."
179   :group 'gnus-summary-marks
180   :link '(custom-manual "(gnus)Setting Marks")
181   :type '(choice (const :tag "off" nil)
182                  (const never)
183                  (sexp :menu-tag "on" t)))
184
185 (defcustom gnus-summary-default-score 0
186   "*Default article score level.
187 All scores generated by the score files will be added to this score.
188 If this variable is nil, scoring will be disabled."
189   :group 'gnus-score-default
190   :type '(choice (const :tag "disable")
191                  integer))
192
193 (defcustom gnus-summary-zcore-fuzz 0
194   "*Fuzziness factor for the zcore in the summary buffer.
195 Articles with scores closer than this to `gnus-summary-default-score'
196 will not be marked."
197   :group 'gnus-summary-format
198   :type 'integer)
199
200 (defcustom gnus-simplify-subject-fuzzy-regexp nil
201   "*Strings to be removed when doing fuzzy matches.
202 This can either be a regular expression or list of regular expressions
203 that will be removed from subject strings if fuzzy subject
204 simplification is selected."
205   :group 'gnus-thread
206   :type '(repeat regexp))
207
208 (defcustom gnus-show-threads t
209   "*If non-nil, display threads in summary mode."
210   :group 'gnus-thread
211   :type 'boolean)
212
213 (defcustom gnus-thread-hide-subtree nil
214   "*If non-nil, hide all threads initially.
215 If threads are hidden, you have to run the command
216 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
217 to expose hidden threads."
218   :group 'gnus-thread
219   :type 'boolean)
220
221 (defcustom gnus-thread-hide-killed t
222   "*If non-nil, hide killed threads automatically."
223   :group 'gnus-thread
224   :type 'boolean)
225
226 (defcustom gnus-thread-ignore-subject t
227   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
228 If nil, articles that have different subjects from their parents will
229 start separate threads."
230   :group 'gnus-thread
231   :type 'boolean)
232
233 (defcustom gnus-thread-operation-ignore-subject t
234   "*If non-nil, subjects will be ignored when doing thread commands.
235 This affects commands like `gnus-summary-kill-thread' and
236 `gnus-summary-lower-thread'.
237
238 If this variable is nil, articles in the same thread with different
239 subjects will not be included in the operation in question.  If this
240 variable is `fuzzy', only articles that have subjects that are fuzzily
241 equal will be included."
242   :group 'gnus-thread
243   :type '(choice (const :tag "off" nil)
244                  (const fuzzy)
245                  (sexp :tag "on" t)))
246
247 (defcustom gnus-thread-indent-level 4
248   "*Number that says how much each sub-thread should be indented."
249   :group 'gnus-thread
250   :type 'integer)
251
252 (defcustom gnus-auto-extend-newsgroup t
253   "*If non-nil, extend newsgroup forward and backward when requested."
254   :group 'gnus-summary-choose
255   :type 'boolean)
256
257 (defcustom gnus-auto-select-first t
258   "*If nil, don't select the first unread article when entering a group.
259 If this variable is `best', select the highest-scored unread article
260 in the group.  If t, select the first unread article.
261
262 This variable can also be a function to place point on a likely
263 subject line.  Useful values include `gnus-summary-first-unread-subject',
264 `gnus-summary-first-unread-article' and
265 `gnus-summary-best-unread-article'.
266
267 If you want to prevent automatic selection of the first unread article
268 in some newsgroups, set the variable to nil in
269 `gnus-select-group-hook'."
270   :group 'gnus-group-select
271   :type '(choice (const :tag "none" nil)
272                  (const best)
273                  (sexp :menu-tag "first" t)
274                  (function-item gnus-summary-first-unread-subject)
275                  (function-item gnus-summary-first-unread-article)
276                  (function-item gnus-summary-best-unread-article)))
277
278 (defcustom gnus-auto-select-next t
279   "*If non-nil, offer to go to the next group from the end of the previous.
280 If the value is t and the next newsgroup is empty, Gnus will exit
281 summary mode and go back to group mode.  If the value is neither nil
282 nor t, Gnus will select the following unread newsgroup.  In
283 particular, if the value is the symbol `quietly', the next unread
284 newsgroup will be selected without any confirmation, and if it is
285 `almost-quietly', the next group will be selected without any
286 confirmation if you are located on the last article in the group.
287 Finally, if this variable is `slightly-quietly', the `Z n' command
288 will go to the next group without confirmation."
289   :group 'gnus-summary-maneuvering
290   :type '(choice (const :tag "off" nil)
291                  (const quietly)
292                  (const almost-quietly)
293                  (const slightly-quietly)
294                  (sexp :menu-tag "on" t)))
295
296 (defcustom gnus-auto-select-same nil
297   "*If non-nil, select the next article with the same subject.
298 If there are no more articles with the same subject, go to
299 the first unread article."
300   :group 'gnus-summary-maneuvering
301   :type 'boolean)
302
303 (defcustom gnus-summary-check-current nil
304   "*If non-nil, consider the current article when moving.
305 The \"unread\" movement commands will stay on the same line if the
306 current article is unread."
307   :group 'gnus-summary-maneuvering
308   :type 'boolean)
309
310 (defcustom gnus-auto-center-summary t
311   "*If non-nil, always center the current summary buffer.
312 In particular, if `vertical' do only vertical recentering.  If non-nil
313 and non-`vertical', do both horizontal and vertical recentering."
314   :group 'gnus-summary-maneuvering
315   :type '(choice (const :tag "none" nil)
316                  (const vertical)
317                  (integer :tag "height")
318                  (sexp :menu-tag "both" t)))
319
320 (defcustom gnus-show-all-headers nil
321   "*If non-nil, don't hide any headers."
322   :group 'gnus-article-hiding
323   :group 'gnus-article-headers
324   :type 'boolean)
325
326 (defcustom gnus-summary-ignore-duplicates nil
327   "*If non-nil, ignore articles with identical Message-ID headers."
328   :group 'gnus-summary
329   :type 'boolean)
330
331 (defcustom gnus-single-article-buffer t
332   "*If non-nil, display all articles in the same buffer.
333 If nil, each group will get its own article buffer."
334   :group 'gnus-article-various
335   :type 'boolean)
336
337 (defcustom gnus-break-pages t
338   "*If non-nil, do page breaking on articles.
339 The page delimiter is specified by the `gnus-page-delimiter'
340 variable."
341   :group 'gnus-article-various
342   :type 'boolean)
343
344 (defcustom gnus-move-split-methods nil
345   "*Variable used to suggest where articles are to be moved to.
346 It uses the same syntax as the `gnus-split-methods' variable."
347   :group 'gnus-summary-mail
348   :type '(repeat (choice (list :value (fun) function)
349                          (cons :value ("" "") regexp (repeat string))
350                          (sexp :value nil))))
351
352 (defcustom gnus-unread-mark ?  ;Whitespace
353   "*Mark used for unread articles."
354   :group 'gnus-summary-marks
355   :type 'character)
356
357 (defcustom gnus-ticked-mark ?!
358   "*Mark used for ticked articles."
359   :group 'gnus-summary-marks
360   :type 'character)
361
362 (defcustom gnus-dormant-mark ??
363   "*Mark used for dormant articles."
364   :group 'gnus-summary-marks
365   :type 'character)
366
367 (defcustom gnus-del-mark ?r
368   "*Mark used for del'd articles."
369   :group 'gnus-summary-marks
370   :type 'character)
371
372 (defcustom gnus-read-mark ?R
373   "*Mark used for read articles."
374   :group 'gnus-summary-marks
375   :type 'character)
376
377 (defcustom gnus-expirable-mark ?E
378   "*Mark used for expirable articles."
379   :group 'gnus-summary-marks
380   :type 'character)
381
382 (defcustom gnus-killed-mark ?K
383   "*Mark used for killed articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-souped-mark ?F
388   "*Mark used for killed articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-kill-file-mark ?X
393   "*Mark used for articles killed by kill files."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-low-score-mark ?Y
398   "*Mark used for articles with a low score."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-catchup-mark ?C
403   "*Mark used for articles that are caught up."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-replied-mark ?A
408   "*Mark used for articles that have been replied to."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-cached-mark ?*
413   "*Mark used for articles that are in the cache."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-saved-mark ?S
418   "*Mark used for articles that have been saved to."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-ancient-mark ?O
423   "*Mark used for ancient articles."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-sparse-mark ?Q
428   "*Mark used for sparsely reffed articles."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-canceled-mark ?G
433   "*Mark used for canceled articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-duplicate-mark ?M
438   "*Mark used for duplicate articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-undownloaded-mark ?@
443   "*Mark used for articles that weren't downloaded."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-downloadable-mark ?%
448   "*Mark used for articles that are to be downloaded."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-unsendable-mark ?=
453   "*Mark used for articles that won't be sent."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-score-over-mark ?+
458   "*Score mark used for articles with high scores."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-score-below-mark ?-
463   "*Score mark used for articles with low scores."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-empty-thread-mark ?  ;Whitespace
468   "*There is no thread under the article."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-not-empty-thread-mark ?=
473   "*There is a thread under the article."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-view-pseudo-asynchronously nil
478   "*If non-nil, Gnus will view pseudo-articles asynchronously."
479   :group 'gnus-extract-view
480   :type 'boolean)
481
482 (defcustom gnus-auto-expirable-marks
483   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
484         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
485         gnus-souped-mark gnus-duplicate-mark)
486   "*The list of marks converted into expiration if a group is auto-expirable."
487   :group 'gnus-summary
488   :type '(repeat character))
489
490 (defcustom gnus-inhibit-user-auto-expire t
491   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
492   :group 'gnus-summary
493   :type 'boolean)
494
495 (defcustom gnus-view-pseudos nil
496   "*If `automatic', pseudo-articles will be viewed automatically.
497 If `not-confirm', pseudos will be viewed automatically, and the user
498 will not be asked to confirm the command."
499   :group 'gnus-extract-view
500   :type '(choice (const :tag "off" nil)
501                  (const automatic)
502                  (const not-confirm)))
503
504 (defcustom gnus-view-pseudos-separately t
505   "*If non-nil, one pseudo-article will be created for each file to be viewed.
506 If nil, all files that use the same viewing command will be given as a
507 list of parameters to that command."
508   :group 'gnus-extract-view
509   :type 'boolean)
510
511 (defcustom gnus-insert-pseudo-articles t
512   "*If non-nil, insert pseudo-articles when decoding articles."
513   :group 'gnus-extract-view
514   :type 'boolean)
515
516 (defcustom gnus-summary-dummy-line-format
517   "  %(:                          :%) %S\n"
518   "*The format specification for the dummy roots in the summary buffer.
519 It works along the same lines as a normal formatting string,
520 with some simple extensions.
521
522 %S  The subject"
523   :group 'gnus-threading
524   :type 'string)
525
526 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
527   "*The format specification for the summary mode line.
528 It works along the same lines as a normal formatting string,
529 with some simple extensions:
530
531 %G  Group name
532 %p  Unprefixed group name
533 %A  Current article number
534 %z  Current article score
535 %V  Gnus version
536 %U  Number of unread articles in the group
537 %e  Number of unselected articles in the group
538 %Z  A string with unread/unselected article counts
539 %g  Shortish group name
540 %S  Subject of the current article
541 %u  User-defined spec
542 %s  Current score file name
543 %d  Number of dormant articles
544 %r  Number of articles that have been marked as read in this session
545 %E  Number of articles expunged by the score files"
546   :group 'gnus-summary-format
547   :type 'string)
548
549 (defcustom gnus-list-identifiers nil
550   "Regexp that matches list identifiers to be removed from subject.
551 This can also be a list of regexps."
552   :group 'gnus-summary-format
553   :group 'gnus-article-hiding
554   :type '(choice (const :tag "none" nil)
555                  (regexp :value ".*")
556                  (repeat :value (".*") regexp)))
557
558 (defcustom gnus-summary-mark-below 0
559   "*Mark all articles with a score below this variable as read.
560 This variable is local to each summary buffer and usually set by the
561 score file."
562   :group 'gnus-score-default
563   :type 'integer)
564
565 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
566   "*List of functions used for sorting articles in the summary buffer.
567 This variable is only used when not using a threaded display."
568   :group 'gnus-summary-sort
569   :type '(repeat (choice (function-item gnus-article-sort-by-number)
570                          (function-item gnus-article-sort-by-author)
571                          (function-item gnus-article-sort-by-subject)
572                          (function-item gnus-article-sort-by-date)
573                          (function-item gnus-article-sort-by-score)
574                          (function :tag "other"))))
575
576 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
577   "*List of functions used for sorting threads in the summary buffer.
578 By default, threads are sorted by article number.
579
580 Each function takes two threads and return non-nil if the first thread
581 should be sorted before the other.  If you use more than one function,
582 the primary sort function should be the last.  You should probably
583 always include `gnus-thread-sort-by-number' in the list of sorting
584 functions -- preferably first.
585
586 Ready-made functions include `gnus-thread-sort-by-number',
587 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
588 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
589 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
590   :group 'gnus-summary-sort
591   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
592                          (function-item gnus-thread-sort-by-author)
593                          (function-item gnus-thread-sort-by-subject)
594                          (function-item gnus-thread-sort-by-date)
595                          (function-item gnus-thread-sort-by-score)
596                          (function-item gnus-thread-sort-by-total-score)
597                          (function :tag "other"))))
598
599 (defcustom gnus-thread-score-function '+
600   "*Function used for calculating the total score of a thread.
601
602 The function is called with the scores of the article and each
603 subthread and should then return the score of the thread.
604
605 Some functions you can use are `+', `max', or `min'."
606   :group 'gnus-summary-sort
607   :type 'function)
608
609 (defcustom gnus-summary-expunge-below nil
610   "All articles that have a score less than this variable will be expunged.
611 This variable is local to the summary buffers."
612   :group 'gnus-score-default
613   :type '(choice (const :tag "off" nil)
614                  integer))
615
616 (defcustom gnus-thread-expunge-below nil
617   "All threads that have a total score less than this variable will be expunged.
618 See `gnus-thread-score-function' for en explanation of what a
619 \"thread score\" is.
620
621 This variable is local to the summary buffers."
622   :group 'gnus-threading
623   :group 'gnus-score-default
624   :type '(choice (const :tag "off" nil)
625                  integer))
626
627 (defcustom gnus-summary-mode-hook nil
628   "*A hook for Gnus summary mode.
629 This hook is run before any variables are set in the summary buffer."
630   :group 'gnus-summary-various
631   :type 'hook)
632
633 (defcustom gnus-summary-menu-hook nil
634   "*Hook run after the creation of the summary mode menu."
635   :group 'gnus-summary-visual
636   :type 'hook)
637
638 (defcustom gnus-summary-exit-hook nil
639   "*A hook called on exit from the summary buffer.
640 It will be called with point in the group buffer."
641   :group 'gnus-summary-exit
642   :type 'hook)
643
644 (defcustom gnus-summary-prepare-hook nil
645   "*A hook called after the summary buffer has been generated.
646 If you want to modify the summary buffer, you can use this hook."
647   :group 'gnus-summary-various
648   :type 'hook)
649
650 (defcustom gnus-summary-prepared-hook nil
651   "*A hook called as the last thing after the summary buffer has been generated."
652   :group 'gnus-summary-various
653   :type 'hook)
654
655 (defcustom gnus-summary-generate-hook nil
656   "*A hook run just before generating the summary buffer.
657 This hook is commonly used to customize threading variables and the
658 like."
659   :group 'gnus-summary-various
660   :type 'hook)
661
662 (defcustom gnus-select-group-hook nil
663   "*A hook called when a newsgroup is selected.
664
665 If you'd like to simplify subjects like the
666 `gnus-summary-next-same-subject' command does, you can use the
667 following hook:
668
669  (setq gnus-select-group-hook
670       (list
671         (lambda ()
672           (mapcar (lambda (header)
673                      (mail-header-set-subject
674                       header
675                       (gnus-simplify-subject
676                        (mail-header-subject header) 're-only)))
677                   gnus-newsgroup-headers))))"
678   :group 'gnus-group-select
679   :type 'hook)
680
681 (defcustom gnus-select-article-hook nil
682   "*A hook called when an article is selected."
683   :group 'gnus-summary-choose
684   :type 'hook)
685
686 (defcustom gnus-visual-mark-article-hook
687   (list 'gnus-highlight-selected-summary)
688   "*Hook run after selecting an article in the summary buffer.
689 It is meant to be used for highlighting the article in some way.  It
690 is not run if `gnus-visual' is nil."
691   :group 'gnus-summary-visual
692   :type 'hook)
693
694 (defcustom gnus-parse-headers-hook nil
695   "*A hook called before parsing the headers."
696   :group 'gnus-various
697   :type 'hook)
698
699 (defcustom gnus-exit-group-hook nil
700   "*A hook called when exiting (not quitting) summary mode."
701   :group 'gnus-various
702   :type 'hook)
703
704 (defcustom gnus-summary-update-hook
705   (list 'gnus-summary-highlight-line)
706   "*A hook called when a summary line is changed.
707 The hook will not be called if `gnus-visual' is nil.
708
709 The default function `gnus-summary-highlight-line' will
710 highlight the line according to the `gnus-summary-highlight'
711 variable."
712   :group 'gnus-summary-visual
713   :type 'hook)
714
715 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
716   "*A hook called when an article is selected for the first time.
717 The hook is intended to mark an article as read (or unread)
718 automatically when it is selected."
719   :group 'gnus-summary-choose
720   :type 'hook)
721
722 (defcustom gnus-group-no-more-groups-hook nil
723   "*A hook run when returning to group mode having no more (unread) groups."
724   :group 'gnus-group-select
725   :type 'hook)
726
727 (defcustom gnus-ps-print-hook nil
728   "*A hook run before ps-printing something from Gnus."
729   :group 'gnus-summary
730   :type 'hook)
731
732 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
733   "Face used for highlighting the current article in the summary buffer."
734   :group 'gnus-summary-visual
735   :type 'face)
736
737 (defcustom gnus-summary-highlight
738   '(((= mark gnus-canceled-mark)
739      . gnus-summary-cancelled-face)
740     ((and (> score default)
741           (or (= mark gnus-dormant-mark)
742               (= mark gnus-ticked-mark)))
743      . gnus-summary-high-ticked-face)
744     ((and (< score default)
745           (or (= mark gnus-dormant-mark)
746               (= mark gnus-ticked-mark)))
747      . gnus-summary-low-ticked-face)
748     ((or (= mark gnus-dormant-mark)
749          (= mark gnus-ticked-mark))
750      . gnus-summary-normal-ticked-face)
751     ((and (> score default) (= mark gnus-ancient-mark))
752      . gnus-summary-high-ancient-face)
753     ((and (< score default) (= mark gnus-ancient-mark))
754      . gnus-summary-low-ancient-face)
755     ((= mark gnus-ancient-mark)
756      . gnus-summary-normal-ancient-face)
757     ((and (> score default) (= mark gnus-unread-mark))
758      . gnus-summary-high-unread-face)
759     ((and (< score default) (= mark gnus-unread-mark))
760      . gnus-summary-low-unread-face)
761     ((= mark gnus-unread-mark)
762      . gnus-summary-normal-unread-face)
763     ((and (> score default) (memq mark (list gnus-downloadable-mark
764                                              gnus-undownloaded-mark)))
765      . gnus-summary-high-unread-face)
766     ((and (< score default) (memq mark (list gnus-downloadable-mark
767                                              gnus-undownloaded-mark)))
768      . gnus-summary-low-unread-face)
769     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
770      . gnus-summary-normal-unread-face)
771     ((> score default)
772      . gnus-summary-high-read-face)
773     ((< score default)
774      . gnus-summary-low-read-face)
775     (t
776      . gnus-summary-normal-read-face))
777   "*Controls the highlighting of summary buffer lines.
778
779 A list of (FORM . FACE) pairs.  When deciding how a a particular
780 summary line should be displayed, each form is evaluated.  The content
781 of the face field after the first true form is used.  You can change
782 how those summary lines are displayed, by editing the face field.
783
784 You can use the following variables in the FORM field.
785
786 score:   The articles score
787 default: The default article score.
788 below:   The score below which articles are automatically marked as read.
789 mark:    The articles mark."
790   :group 'gnus-summary-visual
791   :type '(repeat (cons (sexp :tag "Form" nil)
792                        face)))
793
794 (defcustom gnus-alter-header-function nil
795   "Function called to allow alteration of article header structures.
796 The function is called with one parameter, the article header vector,
797 which it may alter in any way.")
798
799 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
800   "Variable that says which function should be used to decode a string with encoded words.")
801
802 (defcustom gnus-extra-headers nil
803   "*Extra headers to parse."
804   :group 'gnus-summary
805   :type '(repeat symbol))
806
807 (defcustom gnus-ignored-from-addresses
808   (and user-mail-address (regexp-quote user-mail-address))
809   "*Regexp of From headers that may be suppressed in favor of To headers."
810   :group 'gnus-summary
811   :type 'regexp)
812
813 (defcustom gnus-group-charset-alist
814   `(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
815     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
816     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
817     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
818     ("^relcom\\>" koi8-r)
819     ("^fido7\\>" koi8-r)
820     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
821     ("^israel\\>" iso-8859-1)
822     ("^han\\>" euc-kr)
823     ("^alt.chinese.text.big5\\>" chinese-big5)
824     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
825     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
826     (".*" ,default-mime-charset))
827   "Alist of regexps (to match group names) and default charsets to be used when reading."
828   :type '(repeat (list (regexp :tag "Group")
829                        (symbol :tag "Charset")))
830   :group 'gnus-charset)
831
832 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
833   "List of charsets that should be ignored.
834 When these charsets are used in the \"charset\" parameter, the
835 default charset will be used instead."
836   :type '(repeat symbol)
837   :group 'gnus-charset)
838
839 (defcustom gnus-group-ignored-charsets-alist 
840   '(("alt\\.chinese\\.text" iso-8859-1))
841   "Alist of regexps (to match group names) and charsets that should be ignored.
842 When these charsets are used in the \"charset\" parameter, the
843 default charset will be used instead."
844   :type '(repeat (cons (regexp :tag "Group")
845                        (repeat symbol)))
846   :group 'gnus-charset)
847
848 (defcustom gnus-group-highlight-words-alist nil
849   "Alist of group regexps and highlight regexps.
850 This variable uses the same syntax as `gnus-emphasis-alist'."
851   :type '(repeat (cons (regexp :tag "Group")
852                        (repeat (list (regexp :tag "Highlight regexp")
853                                      (number :tag "Group for entire word" 0)
854                                      (number :tag "Group for displayed part" 0)
855                                      (symbol :tag "Face" 
856                                              gnus-emphasis-highlight-words)))))
857   :group 'gnus-summary-visual)
858
859 (defcustom gnus-summary-show-article-charset-alist
860   nil
861   "Alist of number and charset.
862 The article will be shown with the charset corresponding to the
863 numbered argument.
864 For example: ((1 . cn-gb-2312) (2 . big5))."
865   :type '(repeat (cons (number :tag "Argument" 1)
866                        (symbol :tag "Charset")))
867   :group 'gnus-charset)
868
869 (defcustom gnus-preserve-marks t
870   "Whether marks are preserved when moving, copying and respooling messages."
871   :type 'boolean
872   :group 'gnus-summary-marks)
873
874 ;;; Internal variables
875
876 (defvar gnus-article-mime-handles nil)
877 (defvar gnus-article-decoded-p nil)
878 (defvar gnus-scores-exclude-files nil)
879 (defvar gnus-page-broken nil)
880 (defvar gnus-inhibit-mime-unbuttonizing nil)
881
882 (defvar gnus-original-article nil)
883 (defvar gnus-article-internal-prepare-hook nil)
884 (defvar gnus-newsgroup-process-stack nil)
885
886 (defvar gnus-thread-indent-array nil)
887 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
888 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
889   "Function called to sort the articles within a thread after it has been gathered together.")
890
891 ;; Avoid highlighting in kill files.
892 (defvar gnus-summary-inhibit-highlight nil)
893 (defvar gnus-newsgroup-selected-overlay nil)
894 (defvar gnus-inhibit-limiting nil)
895 (defvar gnus-newsgroup-adaptive-score-file nil)
896 (defvar gnus-current-score-file nil)
897 (defvar gnus-current-move-group nil)
898 (defvar gnus-current-copy-group nil)
899 (defvar gnus-current-crosspost-group nil)
900
901 (defvar gnus-newsgroup-dependencies nil)
902 (defvar gnus-newsgroup-adaptive nil)
903 (defvar gnus-summary-display-article-function nil)
904 (defvar gnus-summary-highlight-line-function nil
905   "Function called after highlighting a summary line.")
906
907 (defvar gnus-summary-line-format-alist
908   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
909     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
910     (?s gnus-tmp-subject-or-nil ?s)
911     (?n gnus-tmp-name ?s)
912     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
913         ?s)
914     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
915             gnus-tmp-from) ?s)
916     (?F gnus-tmp-from ?s)
917     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
918     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
919     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
920     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
921     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
922     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
923     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
924     (?L gnus-tmp-lines ?d)
925     (?I gnus-tmp-indentation ?s)
926     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
927     (?R gnus-tmp-replied ?c)
928     (?\[ gnus-tmp-opening-bracket ?c)
929     (?\] gnus-tmp-closing-bracket ?c)
930     (?\> (make-string gnus-tmp-level ? ) ?s)
931     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
932     (?i gnus-tmp-score ?d)
933     (?z gnus-tmp-score-char ?c)
934     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
935     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
936     (?U gnus-tmp-unread ?c)
937     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
938     (?t (gnus-summary-number-of-articles-in-thread
939          (and (boundp 'thread) (car thread)) gnus-tmp-level)
940         ?d)
941     (?e (gnus-summary-number-of-articles-in-thread
942          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
943         ?c)
944     (?u gnus-tmp-user-defined ?s)
945     (?P (gnus-pick-line-number) ?d))
946   "An alist of format specifications that can appear in summary lines,
947 and what variables they correspond with, along with the type of the
948 variable (string, integer, character, etc).")
949
950 (defvar gnus-summary-dummy-line-format-alist
951   `((?S gnus-tmp-subject ?s)
952     (?N gnus-tmp-number ?d)
953     (?u gnus-tmp-user-defined ?s)))
954
955 (defvar gnus-summary-mode-line-format-alist
956   `((?G gnus-tmp-group-name ?s)
957     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
958     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
959     (?A gnus-tmp-article-number ?d)
960     (?Z gnus-tmp-unread-and-unselected ?s)
961     (?V gnus-version ?s)
962     (?U gnus-tmp-unread-and-unticked ?d)
963     (?S gnus-tmp-subject ?s)
964     (?e gnus-tmp-unselected ?d)
965     (?u gnus-tmp-user-defined ?s)
966     (?d (length gnus-newsgroup-dormant) ?d)
967     (?t (length gnus-newsgroup-marked) ?d)
968     (?r (length gnus-newsgroup-reads) ?d)
969     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
970     (?E gnus-newsgroup-expunged-tally ?d)
971     (?s (gnus-current-score-file-nondirectory) ?s)))
972
973 (defvar gnus-last-search-regexp nil
974   "Default regexp for article search command.")
975
976 (defvar gnus-last-shell-command nil
977   "Default shell command on article.")
978
979 (defvar gnus-newsgroup-begin nil)
980 (defvar gnus-newsgroup-end nil)
981 (defvar gnus-newsgroup-last-rmail nil)
982 (defvar gnus-newsgroup-last-mail nil)
983 (defvar gnus-newsgroup-last-folder nil)
984 (defvar gnus-newsgroup-last-file nil)
985 (defvar gnus-newsgroup-auto-expire nil)
986 (defvar gnus-newsgroup-active nil)
987
988 (defvar gnus-newsgroup-data nil)
989 (defvar gnus-newsgroup-data-reverse nil)
990 (defvar gnus-newsgroup-limit nil)
991 (defvar gnus-newsgroup-limits nil)
992
993 (defvar gnus-newsgroup-unreads nil
994   "List of unread articles in the current newsgroup.")
995
996 (defvar gnus-newsgroup-unselected nil
997   "List of unselected unread articles in the current newsgroup.")
998
999 (defvar gnus-newsgroup-reads nil
1000   "Alist of read articles and article marks in the current newsgroup.")
1001
1002 (defvar gnus-newsgroup-expunged-tally nil)
1003
1004 (defvar gnus-newsgroup-marked nil
1005   "List of ticked articles in the current newsgroup (a subset of unread art).")
1006
1007 (defvar gnus-newsgroup-killed nil
1008   "List of ranges of articles that have been through the scoring process.")
1009
1010 (defvar gnus-newsgroup-cached nil
1011   "List of articles that come from the article cache.")
1012
1013 (defvar gnus-newsgroup-saved nil
1014   "List of articles that have been saved.")
1015
1016 (defvar gnus-newsgroup-kill-headers nil)
1017
1018 (defvar gnus-newsgroup-replied nil
1019   "List of articles that have been replied to in the current newsgroup.")
1020
1021 (defvar gnus-newsgroup-expirable nil
1022   "List of articles in the current newsgroup that can be expired.")
1023
1024 (defvar gnus-newsgroup-processable nil
1025   "List of articles in the current newsgroup that can be processed.")
1026
1027 (defvar gnus-newsgroup-downloadable nil
1028   "List of articles in the current newsgroup that can be processed.")
1029
1030 (defvar gnus-newsgroup-undownloaded nil
1031   "List of articles in the current newsgroup that haven't been downloaded..")
1032
1033 (defvar gnus-newsgroup-unsendable nil
1034   "List of articles in the current newsgroup that won't be sent.")
1035
1036 (defvar gnus-newsgroup-bookmarks nil
1037   "List of articles in the current newsgroup that have bookmarks.")
1038
1039 (defvar gnus-newsgroup-dormant nil
1040   "List of dormant articles in the current newsgroup.")
1041
1042 (defvar gnus-newsgroup-scored nil
1043   "List of scored articles in the current newsgroup.")
1044
1045 (defvar gnus-newsgroup-headers nil
1046   "List of article headers in the current newsgroup.")
1047
1048 (defvar gnus-newsgroup-threads nil)
1049
1050 (defvar gnus-newsgroup-prepared nil
1051   "Whether the current group has been prepared properly.")
1052
1053 (defvar gnus-newsgroup-ancient nil
1054   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1055
1056 (defvar gnus-newsgroup-sparse nil)
1057
1058 (defvar gnus-current-article nil)
1059 (defvar gnus-article-current nil)
1060 (defvar gnus-current-headers nil)
1061 (defvar gnus-have-all-headers nil)
1062 (defvar gnus-last-article nil)
1063 (defvar gnus-newsgroup-history nil)
1064 (defvar gnus-newsgroup-charset nil)
1065 (defvar gnus-newsgroup-ephemeral-charset nil)
1066 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1067
1068 (defconst gnus-summary-local-variables
1069   '(gnus-newsgroup-name
1070     gnus-newsgroup-begin gnus-newsgroup-end
1071     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1072     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1073     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1074     gnus-newsgroup-unselected gnus-newsgroup-marked
1075     gnus-newsgroup-reads gnus-newsgroup-saved
1076     gnus-newsgroup-replied gnus-newsgroup-expirable
1077     gnus-newsgroup-processable gnus-newsgroup-killed
1078     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1079     gnus-newsgroup-unsendable
1080     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1081     gnus-newsgroup-headers gnus-newsgroup-threads
1082     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1083     gnus-current-article gnus-current-headers gnus-have-all-headers
1084     gnus-last-article gnus-article-internal-prepare-hook
1085     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1086     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1087     gnus-thread-expunge-below
1088     gnus-score-alist gnus-current-score-file
1089     (gnus-summary-expunge-below . global)
1090     (gnus-summary-mark-below . global)
1091     gnus-newsgroup-active gnus-scores-exclude-files
1092     gnus-newsgroup-history gnus-newsgroup-ancient
1093     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1094     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1095     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1096     (gnus-newsgroup-expunged-tally . 0)
1097     gnus-cache-removable-articles gnus-newsgroup-cached
1098     gnus-newsgroup-data gnus-newsgroup-data-reverse
1099     gnus-newsgroup-limit gnus-newsgroup-limits
1100     gnus-newsgroup-charset)
1101   "Variables that are buffer-local to the summary buffers.")
1102
1103 ;; Byte-compiler warning.
1104 (defvar gnus-article-mode-map)
1105
1106 ;; MIME stuff.
1107
1108 (defvar gnus-decode-encoded-word-methods
1109   '(mail-decode-encoded-word-string)
1110   "List of methods used to decode encoded words.
1111
1112 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1113 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1114 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1115 whose names match REGEXP.
1116
1117 For example:
1118 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1119  mail-decode-encoded-word-string
1120  (\"chinese\" . rfc1843-decode-string))
1121 ")
1122
1123 (defvar gnus-decode-encoded-word-methods-cache nil)
1124
1125 (defun gnus-multi-decode-encoded-word-string (string)
1126   "Apply the functions from `gnus-encoded-word-methods' that match."
1127   (unless (and gnus-decode-encoded-word-methods-cache
1128                (eq gnus-newsgroup-name
1129                    (car gnus-decode-encoded-word-methods-cache)))
1130     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1131     (mapcar '(lambda (x)
1132                (if (symbolp x)
1133                    (nconc gnus-decode-encoded-word-methods-cache (list x))
1134                  (if (and gnus-newsgroup-name
1135                           (string-match (car x) gnus-newsgroup-name))
1136                      (nconc gnus-decode-encoded-word-methods-cache
1137                             (list (cdr x))))))
1138           gnus-decode-encoded-word-methods))
1139   (let ((xlist gnus-decode-encoded-word-methods-cache))
1140     (pop xlist)
1141     (while xlist
1142       (setq string (funcall (pop xlist) string))))
1143   string)
1144
1145 ;; Subject simplification.
1146
1147 (defun gnus-simplify-whitespace (str)
1148   "Remove excessive whitespace."
1149   (let ((mystr str))
1150     ;; Multiple spaces.
1151     (while (string-match "[ \t][ \t]+" mystr)
1152       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1153                           " "
1154                           (substring mystr (match-end 0)))))
1155     ;; Leading spaces.
1156     (when (string-match "^[ \t]+" mystr)
1157       (setq mystr (substring mystr (match-end 0))))
1158     ;; Trailing spaces.
1159     (when (string-match "[ \t]+$" mystr)
1160       (setq mystr (substring mystr 0 (match-beginning 0))))
1161     mystr))
1162
1163 (defsubst gnus-simplify-subject-re (subject)
1164   "Remove \"Re:\" from subject lines."
1165   (if (string-match "^[Rr][Ee]: *" subject)
1166       (substring subject (match-end 0))
1167     subject))
1168
1169 (defun gnus-simplify-subject (subject &optional re-only)
1170   "Remove `Re:' and words in parentheses.
1171 If RE-ONLY is non-nil, strip leading `Re:'s only."
1172   (let ((case-fold-search t))           ;Ignore case.
1173     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1174     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1175       (setq subject (substring subject (match-end 0))))
1176     ;; Remove uninteresting prefixes.
1177     (when (and (not re-only)
1178                gnus-simplify-ignored-prefixes
1179                (string-match gnus-simplify-ignored-prefixes subject))
1180       (setq subject (substring subject (match-end 0))))
1181     ;; Remove words in parentheses from end.
1182     (unless re-only
1183       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1184         (setq subject (substring subject 0 (match-beginning 0)))))
1185     ;; Return subject string.
1186     subject))
1187
1188 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1189 ;; all whitespace.
1190 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1191   (goto-char (point-min))
1192   (while (re-search-forward regexp nil t)
1193     (replace-match (or newtext ""))))
1194
1195 (defun gnus-simplify-buffer-fuzzy ()
1196   "Simplify string in the buffer fuzzily.
1197 The string in the accessible portion of the current buffer is simplified.
1198 It is assumed to be a single-line subject.
1199 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1200 matter is removed.  Additional things can be deleted by setting
1201 gnus-simplify-subject-fuzzy-regexp."
1202   (let ((case-fold-search t)
1203         (modified-tick))
1204     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1205
1206     (while (not (eq modified-tick (buffer-modified-tick)))
1207       (setq modified-tick (buffer-modified-tick))
1208       (cond
1209        ((listp gnus-simplify-subject-fuzzy-regexp)
1210         (mapcar 'gnus-simplify-buffer-fuzzy-step
1211                 gnus-simplify-subject-fuzzy-regexp))
1212        (gnus-simplify-subject-fuzzy-regexp
1213         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1214       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1215       (gnus-simplify-buffer-fuzzy-step
1216        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1217       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1218
1219     (goto-char (point-min))
1220     (while (re-search-forward " +" nil t)
1221       (delete-region (match-beginning 0) (match-end 0))
1222       (or (bolp) (eobp)
1223           (> (char-after) 127) (> (char-before) 127)
1224           (insert " ")))
1225     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1226     (gnus-simplify-buffer-fuzzy-step " $")
1227     (gnus-simplify-buffer-fuzzy-step "^ +")))
1228
1229 (defun gnus-simplify-subject-fuzzy (subject)
1230   "Simplify a subject string fuzzily.
1231 See `gnus-simplify-buffer-fuzzy' for details."
1232   (let ((fuzzy-regexp gnus-simplify-subject-fuzzy-regexp)) ; Group parameter.
1233     ;; Remove uninteresting prefixes.
1234     (when (and gnus-simplify-ignored-prefixes
1235                (let ((case-fold-search t))
1236                  (string-match gnus-simplify-ignored-prefixes subject)))
1237       (setq subject (substring subject (match-end 0))))
1238     (save-excursion
1239       (gnus-set-work-buffer)
1240       (let ((case-fold-search t)
1241             (gnus-simplify-subject-fuzzy-regexp fuzzy-regexp))
1242         (insert subject)
1243         (inline (gnus-simplify-buffer-fuzzy))
1244         (buffer-string)))))
1245
1246 (defsubst gnus-simplify-subject-fully (subject)
1247   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1248   (cond
1249    (gnus-simplify-subject-functions
1250     (gnus-map-function gnus-simplify-subject-functions subject))
1251    ((null gnus-summary-gather-subject-limit)
1252     (gnus-simplify-subject-re subject))
1253    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1254     (gnus-simplify-subject-fuzzy subject))
1255    ((numberp gnus-summary-gather-subject-limit)
1256     (gnus-limit-string (gnus-simplify-subject-re subject)
1257                        gnus-summary-gather-subject-limit))
1258    (t
1259     subject)))
1260
1261 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1262   "Check whether two subjects are equal.
1263 If optional argument simple-first is t, first argument is already
1264 simplified."
1265   (cond
1266    ((null simple-first)
1267     (equal (gnus-simplify-subject-fully s1)
1268            (gnus-simplify-subject-fully s2)))
1269    (t
1270     (equal s1
1271            (gnus-simplify-subject-fully s2)))))
1272
1273 (defun gnus-summary-bubble-group ()
1274   "Increase the score of the current group.
1275 This is a handy function to add to `gnus-summary-exit-hook' to
1276 increase the score of each group you read."
1277   (gnus-group-add-score gnus-newsgroup-name))
1278
1279 \f
1280 ;;;
1281 ;;; Gnus summary mode
1282 ;;;
1283
1284 (put 'gnus-summary-mode 'mode-class 'special)
1285
1286 (when t
1287   ;; Non-orthogonal keys
1288
1289   (gnus-define-keys gnus-summary-mode-map
1290     " " gnus-summary-next-page
1291     "\177" gnus-summary-prev-page
1292     [delete] gnus-summary-prev-page
1293     [backspace] gnus-summary-prev-page
1294     "\r" gnus-summary-scroll-up
1295     "\M-\r" gnus-summary-scroll-down
1296     "n" gnus-summary-next-unread-article
1297     "p" gnus-summary-prev-unread-article
1298     "N" gnus-summary-next-article
1299     "P" gnus-summary-prev-article
1300     "\M-\C-n" gnus-summary-next-same-subject
1301     "\M-\C-p" gnus-summary-prev-same-subject
1302     "\M-n" gnus-summary-next-unread-subject
1303     "\M-p" gnus-summary-prev-unread-subject
1304     "." gnus-summary-first-unread-article
1305     "," gnus-summary-best-unread-article
1306     "\M-s" gnus-summary-search-article-forward
1307     "\M-r" gnus-summary-search-article-backward
1308     "<" gnus-summary-beginning-of-article
1309     ">" gnus-summary-end-of-article
1310     "j" gnus-summary-goto-article
1311     "^" gnus-summary-refer-parent-article
1312     "\M-^" gnus-summary-refer-article
1313     "u" gnus-summary-tick-article-forward
1314     "!" gnus-summary-tick-article-forward
1315     "U" gnus-summary-tick-article-backward
1316     "d" gnus-summary-mark-as-read-forward
1317     "D" gnus-summary-mark-as-read-backward
1318     "E" gnus-summary-mark-as-expirable
1319     "\M-u" gnus-summary-clear-mark-forward
1320     "\M-U" gnus-summary-clear-mark-backward
1321     "k" gnus-summary-kill-same-subject-and-select
1322     "\C-k" gnus-summary-kill-same-subject
1323     "\M-\C-k" gnus-summary-kill-thread
1324     "\M-\C-l" gnus-summary-lower-thread
1325     "e" gnus-summary-edit-article
1326     "#" gnus-summary-mark-as-processable
1327     "\M-#" gnus-summary-unmark-as-processable
1328     "\M-\C-t" gnus-summary-toggle-threads
1329     "\M-\C-s" gnus-summary-show-thread
1330     "\M-\C-h" gnus-summary-hide-thread
1331     "\M-\C-f" gnus-summary-next-thread
1332     "\M-\C-b" gnus-summary-prev-thread
1333     "\M-\C-u" gnus-summary-up-thread
1334     "\M-\C-d" gnus-summary-down-thread
1335     "&" gnus-summary-execute-command
1336     "c" gnus-summary-catchup-and-exit
1337     "\C-w" gnus-summary-mark-region-as-read
1338     "\C-t" gnus-summary-toggle-truncation
1339     "?" gnus-summary-mark-as-dormant
1340     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1341     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1342     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1343     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1344     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1345     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1346     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1347     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1348     "=" gnus-summary-expand-window
1349     "\C-x\C-s" gnus-summary-reselect-current-group
1350     "\M-g" gnus-summary-rescan-group
1351     "w" gnus-summary-stop-page-breaking
1352     "\C-c\C-r" gnus-summary-caesar-message
1353     "f" gnus-summary-followup
1354     "F" gnus-summary-followup-with-original
1355     "C" gnus-summary-cancel-article
1356     "r" gnus-summary-reply
1357     "R" gnus-summary-reply-with-original
1358     "\C-c\C-f" gnus-summary-mail-forward
1359     "o" gnus-summary-save-article
1360     "\C-o" gnus-summary-save-article-mail
1361     "|" gnus-summary-pipe-output
1362     "\M-k" gnus-summary-edit-local-kill
1363     "\M-K" gnus-summary-edit-global-kill
1364     ;; "V" gnus-version
1365     "\C-c\C-d" gnus-summary-describe-group
1366     "q" gnus-summary-exit
1367     "Q" gnus-summary-exit-no-update
1368     "\C-c\C-i" gnus-info-find-node
1369     gnus-mouse-2 gnus-mouse-pick-article
1370     "m" gnus-summary-mail-other-window
1371     "a" gnus-summary-post-news
1372     "x" gnus-summary-limit-to-unread
1373     "s" gnus-summary-isearch-article
1374     "t" gnus-summary-toggle-header
1375     "g" gnus-summary-show-article
1376     "l" gnus-summary-goto-last-article
1377     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1378     "\C-d" gnus-summary-enter-digest-group
1379     "\M-\C-d" gnus-summary-read-document
1380     "\M-\C-e" gnus-summary-edit-parameters
1381     "\M-\C-a" gnus-summary-customize-parameters
1382     "\C-c\C-b" gnus-bug
1383     "*" gnus-cache-enter-article
1384     "\M-*" gnus-cache-remove-article
1385     "\M-&" gnus-summary-universal-argument
1386     "\C-l" gnus-recenter
1387     "I" gnus-summary-increase-score
1388     "L" gnus-summary-lower-score
1389     "\M-i" gnus-symbolic-argument
1390     "h" gnus-summary-select-article-buffer
1391
1392     "b" gnus-article-view-part
1393     "\M-t" gnus-summary-toggle-display-buttonized
1394
1395     "V" gnus-summary-score-map
1396     "X" gnus-uu-extract-map
1397     "S" gnus-summary-send-map)
1398
1399   ;; Sort of orthogonal keymap
1400   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1401     "t" gnus-summary-tick-article-forward
1402     "!" gnus-summary-tick-article-forward
1403     "d" gnus-summary-mark-as-read-forward
1404     "r" gnus-summary-mark-as-read-forward
1405     "c" gnus-summary-clear-mark-forward
1406     " " gnus-summary-clear-mark-forward
1407     "e" gnus-summary-mark-as-expirable
1408     "x" gnus-summary-mark-as-expirable
1409     "?" gnus-summary-mark-as-dormant
1410     "b" gnus-summary-set-bookmark
1411     "B" gnus-summary-remove-bookmark
1412     "#" gnus-summary-mark-as-processable
1413     "\M-#" gnus-summary-unmark-as-processable
1414     "S" gnus-summary-limit-include-expunged
1415     "C" gnus-summary-catchup
1416     "H" gnus-summary-catchup-to-here
1417     "\C-c" gnus-summary-catchup-all
1418     "k" gnus-summary-kill-same-subject-and-select
1419     "K" gnus-summary-kill-same-subject
1420     "P" gnus-uu-mark-map)
1421
1422   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1423     "c" gnus-summary-clear-above
1424     "u" gnus-summary-tick-above
1425     "m" gnus-summary-mark-above
1426     "k" gnus-summary-kill-below)
1427
1428   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1429     "/" gnus-summary-limit-to-subject
1430     "n" gnus-summary-limit-to-articles
1431     "w" gnus-summary-pop-limit
1432     "s" gnus-summary-limit-to-subject
1433     "a" gnus-summary-limit-to-author
1434     "u" gnus-summary-limit-to-unread
1435     "m" gnus-summary-limit-to-marks
1436     "M" gnus-summary-limit-exclude-marks
1437     "v" gnus-summary-limit-to-score
1438     "*" gnus-summary-limit-include-cached
1439     "D" gnus-summary-limit-include-dormant
1440     "T" gnus-summary-limit-include-thread
1441     "d" gnus-summary-limit-exclude-dormant
1442     "t" gnus-summary-limit-to-age
1443     "x" gnus-summary-limit-to-extra 
1444     "E" gnus-summary-limit-include-expunged
1445     "c" gnus-summary-limit-exclude-childless-dormant
1446     "C" gnus-summary-limit-mark-excluded-as-read)
1447
1448   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1449     "n" gnus-summary-next-unread-article
1450     "p" gnus-summary-prev-unread-article
1451     "N" gnus-summary-next-article
1452     "P" gnus-summary-prev-article
1453     "\C-n" gnus-summary-next-same-subject
1454     "\C-p" gnus-summary-prev-same-subject
1455     "\M-n" gnus-summary-next-unread-subject
1456     "\M-p" gnus-summary-prev-unread-subject
1457     "f" gnus-summary-first-unread-article
1458     "b" gnus-summary-best-unread-article
1459     "j" gnus-summary-goto-article
1460     "g" gnus-summary-goto-subject
1461     "l" gnus-summary-goto-last-article
1462     "o" gnus-summary-pop-article)
1463
1464   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1465     "k" gnus-summary-kill-thread
1466     "l" gnus-summary-lower-thread
1467     "i" gnus-summary-raise-thread
1468     "T" gnus-summary-toggle-threads
1469     "t" gnus-summary-rethread-current
1470     "^" gnus-summary-reparent-thread
1471     "s" gnus-summary-show-thread
1472     "S" gnus-summary-show-all-threads
1473     "h" gnus-summary-hide-thread
1474     "H" gnus-summary-hide-all-threads
1475     "n" gnus-summary-next-thread
1476     "p" gnus-summary-prev-thread
1477     "u" gnus-summary-up-thread
1478     "o" gnus-summary-top-thread
1479     "d" gnus-summary-down-thread
1480     "#" gnus-uu-mark-thread
1481     "\M-#" gnus-uu-unmark-thread)
1482
1483   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1484     "g" gnus-summary-prepare
1485     "c" gnus-summary-insert-cached-articles)
1486
1487   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1488     "c" gnus-summary-catchup-and-exit
1489     "C" gnus-summary-catchup-all-and-exit
1490     "E" gnus-summary-exit-no-update
1491     "Q" gnus-summary-exit
1492     "Z" gnus-summary-exit
1493     "n" gnus-summary-catchup-and-goto-next-group
1494     "R" gnus-summary-reselect-current-group
1495     "G" gnus-summary-rescan-group
1496     "N" gnus-summary-next-group
1497     "s" gnus-summary-save-newsrc
1498     "P" gnus-summary-prev-group)
1499
1500   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1501     " " gnus-summary-next-page
1502     "n" gnus-summary-next-page
1503     "\177" gnus-summary-prev-page
1504     [delete] gnus-summary-prev-page
1505     "p" gnus-summary-prev-page
1506     "\r" gnus-summary-scroll-up
1507     "\M-\r" gnus-summary-scroll-down
1508     "<" gnus-summary-beginning-of-article
1509     ">" gnus-summary-end-of-article
1510     "b" gnus-summary-beginning-of-article
1511     "e" gnus-summary-end-of-article
1512     "^" gnus-summary-refer-parent-article
1513     "r" gnus-summary-refer-parent-article
1514     "D" gnus-summary-enter-digest-group
1515     "R" gnus-summary-refer-references
1516     "T" gnus-summary-refer-thread
1517     "g" gnus-summary-show-article
1518     "s" gnus-summary-isearch-article
1519     "P" gnus-summary-print-article
1520     "t" gnus-article-babel)
1521
1522   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1523     "b" gnus-article-add-buttons
1524     "B" gnus-article-add-buttons-to-head
1525     "o" gnus-article-treat-overstrike
1526     "e" gnus-article-emphasize
1527     "w" gnus-article-fill-cited-article
1528     "Q" gnus-article-fill-long-lines
1529     "C" gnus-article-capitalize-sentences
1530     "c" gnus-article-remove-cr
1531     "q" gnus-article-de-quoted-unreadable
1532     "f" gnus-article-display-x-face
1533     "l" gnus-summary-stop-page-breaking
1534     "r" gnus-summary-caesar-message
1535     "t" gnus-article-hide-headers
1536     "v" gnus-summary-verbose-headers
1537     "H" gnus-article-strip-headers-in-body
1538     "d" gnus-article-treat-dumbquotes)
1539
1540   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1541     "a" gnus-article-hide
1542     "h" gnus-article-hide-headers
1543     "b" gnus-article-hide-boring-headers
1544     "s" gnus-article-hide-signature
1545     "c" gnus-article-hide-citation
1546     "C" gnus-article-hide-citation-in-followups
1547     "l" gnus-article-hide-list-identifiers
1548     "p" gnus-article-hide-pgp
1549     "B" gnus-article-strip-banner
1550     "P" gnus-article-hide-pem
1551     "\C-c" gnus-article-hide-citation-maybe)
1552
1553   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1554     "a" gnus-article-highlight
1555     "h" gnus-article-highlight-headers
1556     "c" gnus-article-highlight-citation
1557     "s" gnus-article-highlight-signature)
1558
1559   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1560     "w" gnus-article-decode-mime-words
1561     "c" gnus-article-decode-charset
1562     "v" gnus-mime-view-all-parts
1563     "b" gnus-article-view-part)
1564
1565   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1566     "z" gnus-article-date-ut
1567     "u" gnus-article-date-ut
1568     "l" gnus-article-date-local
1569     "e" gnus-article-date-lapsed
1570     "o" gnus-article-date-original
1571     "i" gnus-article-date-iso8601
1572     "s" gnus-article-date-user)
1573
1574   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1575     "t" gnus-article-remove-trailing-blank-lines
1576     "l" gnus-article-strip-leading-blank-lines
1577     "m" gnus-article-strip-multiple-blank-lines
1578     "a" gnus-article-strip-blank-lines
1579     "A" gnus-article-strip-all-blank-lines
1580     "s" gnus-article-strip-leading-space
1581     "e" gnus-article-strip-trailing-space)
1582
1583   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1584     "v" gnus-version
1585     "f" gnus-summary-fetch-faq
1586     "d" gnus-summary-describe-group
1587     "h" gnus-summary-describe-briefly
1588     "i" gnus-info-find-node)
1589
1590   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1591     "e" gnus-summary-expire-articles
1592     "\M-\C-e" gnus-summary-expire-articles-now
1593     "\177" gnus-summary-delete-article
1594     [delete] gnus-summary-delete-article
1595     [backspace] gnus-summary-delete-article
1596     "m" gnus-summary-move-article
1597     "r" gnus-summary-respool-article
1598     "w" gnus-summary-edit-article
1599     "c" gnus-summary-copy-article
1600     "B" gnus-summary-crosspost-article
1601     "q" gnus-summary-respool-query
1602     "t" gnus-summary-respool-trace
1603     "i" gnus-summary-import-article
1604     "p" gnus-summary-article-posted-p)
1605
1606   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1607     "o" gnus-summary-save-article
1608     "m" gnus-summary-save-article-mail
1609     "F" gnus-summary-write-article-file
1610     "r" gnus-summary-save-article-rmail
1611     "f" gnus-summary-save-article-file
1612     "b" gnus-summary-save-article-body-file
1613     "h" gnus-summary-save-article-folder
1614     "v" gnus-summary-save-article-vm
1615     "p" gnus-summary-pipe-output
1616     "s" gnus-soup-add-article)
1617
1618   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1619     "b" gnus-summary-display-buttonized
1620     "m" gnus-summary-repair-multipart
1621     "v" gnus-article-view-part
1622     "o" gnus-article-save-part
1623     "c" gnus-article-copy-part
1624     "e" gnus-article-externalize-part
1625     "i" gnus-article-inline-part
1626     "|" gnus-article-pipe-part))
1627
1628 (defun gnus-summary-make-menu-bar ()
1629   (gnus-turn-off-edit-menu 'summary)
1630
1631   (unless (boundp 'gnus-summary-misc-menu)
1632
1633     (easy-menu-define
1634      gnus-summary-kill-menu gnus-summary-mode-map ""
1635      (cons
1636       "Score"
1637       (nconc
1638        (list
1639         ["Enter score..." gnus-summary-score-entry t]
1640         ["Customize" gnus-score-customize t])
1641        (gnus-make-score-map 'increase)
1642        (gnus-make-score-map 'lower)
1643        '(("Mark"
1644           ["Kill below" gnus-summary-kill-below t]
1645           ["Mark above" gnus-summary-mark-above t]
1646           ["Tick above" gnus-summary-tick-above t]
1647           ["Clear above" gnus-summary-clear-above t])
1648          ["Current score" gnus-summary-current-score t]
1649          ["Set score" gnus-summary-set-score t]
1650          ["Switch current score file..." gnus-score-change-score-file t]
1651          ["Set mark below..." gnus-score-set-mark-below t]
1652          ["Set expunge below..." gnus-score-set-expunge-below t]
1653          ["Edit current score file" gnus-score-edit-current-scores t]
1654          ["Edit score file" gnus-score-edit-file t]
1655          ["Trace score" gnus-score-find-trace t]
1656          ["Find words" gnus-score-find-favourite-words t]
1657          ["Rescore buffer" gnus-summary-rescore t]
1658          ["Increase score..." gnus-summary-increase-score t]
1659          ["Lower score..." gnus-summary-lower-score t]))))
1660
1661     ;; Define both the Article menu in the summary buffer and the equivalent
1662     ;; Commands menu in the article buffer here for consistency.
1663     (let ((innards
1664            '(("Hide"
1665               ["All" gnus-article-hide t]
1666               ["Headers" gnus-article-hide-headers t]
1667               ["Signature" gnus-article-hide-signature t]
1668               ["Citation" gnus-article-hide-citation t]
1669               ["List identifiers" gnus-article-hide-list-identifiers t]
1670               ["PGP" gnus-article-hide-pgp t]
1671               ["Banner" gnus-article-strip-banner t]
1672               ["Boring headers" gnus-article-hide-boring-headers t])
1673              ("Highlight"
1674               ["All" gnus-article-highlight t]
1675               ["Headers" gnus-article-highlight-headers t]
1676               ["Signature" gnus-article-highlight-signature t]
1677               ["Citation" gnus-article-highlight-citation t])
1678              ("MIME"
1679               ["Words" gnus-article-decode-mime-words t]
1680               ["Charset" gnus-article-decode-charset t]
1681               ["QP" gnus-article-de-quoted-unreadable t]
1682               ["View all" gnus-mime-view-all-parts t])
1683              ("Date"
1684               ["Local" gnus-article-date-local t]
1685               ["ISO8601" gnus-article-date-iso8601 t]
1686               ["UT" gnus-article-date-ut t]
1687               ["Original" gnus-article-date-original t]
1688               ["Lapsed" gnus-article-date-lapsed t]
1689               ["User-defined" gnus-article-date-user t])
1690              ("Washing"
1691               ("Remove Blanks"
1692                ["Leading" gnus-article-strip-leading-blank-lines t]
1693                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1694                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1695                ["All of the above" gnus-article-strip-blank-lines t]
1696                ["All" gnus-article-strip-all-blank-lines t]
1697                ["Leading space" gnus-article-strip-leading-space t]
1698                ["Trailing space" gnus-article-strip-trailing-space t])
1699               ["Overstrike" gnus-article-treat-overstrike t]
1700               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1701               ["Emphasis" gnus-article-emphasize t]
1702               ["Word wrap" gnus-article-fill-cited-article t]
1703               ["Fill long lines" gnus-article-fill-long-lines t]
1704               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1705               ["CR" gnus-article-remove-cr t]
1706               ["Show X-Face" gnus-article-display-x-face t]
1707               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1708               ["Rot 13" gnus-summary-caesar-message t]
1709               ["Unix pipe" gnus-summary-pipe-message t]
1710               ["Add buttons" gnus-article-add-buttons t]
1711               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1712               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1713               ["Verbose header" gnus-summary-verbose-headers t]
1714               ["Toggle header" gnus-summary-toggle-header t]
1715               ["HZ" gnus-article-decode-HZ t])
1716              ("Output"
1717               ["Save in default format" gnus-summary-save-article t]
1718               ["Save in file" gnus-summary-save-article-file t]
1719               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1720               ["Save in MH folder" gnus-summary-save-article-folder t]
1721               ["Save in VM folder" gnus-summary-save-article-vm t]
1722               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1723               ["Save body in file" gnus-summary-save-article-body-file t]
1724               ["Pipe through a filter" gnus-summary-pipe-output t]
1725               ["Add to SOUP packet" gnus-soup-add-article t]
1726               ["Print" gnus-summary-print-article t])
1727              ("Backend"
1728               ["Respool article..." gnus-summary-respool-article t]
1729               ["Move article..." gnus-summary-move-article
1730                (gnus-check-backend-function
1731                 'request-move-article gnus-newsgroup-name)]
1732               ["Copy article..." gnus-summary-copy-article t]
1733               ["Crosspost article..." gnus-summary-crosspost-article
1734                (gnus-check-backend-function
1735                 'request-replace-article gnus-newsgroup-name)]
1736               ["Import file..." gnus-summary-import-article t]
1737               ["Check if posted" gnus-summary-article-posted-p t]
1738               ["Edit article" gnus-summary-edit-article
1739                (not (gnus-group-read-only-p))]
1740               ["Delete article" gnus-summary-delete-article
1741                (gnus-check-backend-function
1742                 'request-expire-articles gnus-newsgroup-name)]
1743               ["Query respool" gnus-summary-respool-query t]
1744               ["Trace respool" gnus-summary-respool-trace t]
1745               ["Delete expirable articles" gnus-summary-expire-articles-now
1746                (gnus-check-backend-function
1747                 'request-expire-articles gnus-newsgroup-name)])
1748              ("Extract"
1749               ["Uudecode" gnus-uu-decode-uu t]
1750               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1751               ["Unshar" gnus-uu-decode-unshar t]
1752               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1753               ["Save" gnus-uu-decode-save t]
1754               ["Binhex" gnus-uu-decode-binhex t]
1755               ["Postscript" gnus-uu-decode-postscript t])
1756              ("Cache"
1757               ["Enter article" gnus-cache-enter-article t]
1758               ["Remove article" gnus-cache-remove-article t])
1759              ["Translate" gnus-article-babel t]
1760              ["Select article buffer" gnus-summary-select-article-buffer t]
1761              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1762              ["Isearch article..." gnus-summary-isearch-article t]
1763              ["Beginning of the article" gnus-summary-beginning-of-article t]
1764              ["End of the article" gnus-summary-end-of-article t]
1765              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1766              ["Fetch referenced articles" gnus-summary-refer-references t]
1767              ["Fetch current thread" gnus-summary-refer-thread t]
1768              ["Fetch article with id..." gnus-summary-refer-article t]
1769              ["Redisplay" gnus-summary-show-article t])))
1770       (easy-menu-define
1771        gnus-summary-article-menu gnus-summary-mode-map ""
1772        (cons "Article" innards))
1773
1774       (easy-menu-define
1775        gnus-article-commands-menu gnus-article-mode-map ""
1776        (cons "Commands" innards)))
1777
1778     (easy-menu-define
1779      gnus-summary-thread-menu gnus-summary-mode-map ""
1780      '("Threads"
1781        ["Toggle threading" gnus-summary-toggle-threads t]
1782        ["Hide threads" gnus-summary-hide-all-threads t]
1783        ["Show threads" gnus-summary-show-all-threads t]
1784        ["Hide thread" gnus-summary-hide-thread t]
1785        ["Show thread" gnus-summary-show-thread t]
1786        ["Go to next thread" gnus-summary-next-thread t]
1787        ["Go to previous thread" gnus-summary-prev-thread t]
1788        ["Go down thread" gnus-summary-down-thread t]
1789        ["Go up thread" gnus-summary-up-thread t]
1790        ["Top of thread" gnus-summary-top-thread t]
1791        ["Mark thread as read" gnus-summary-kill-thread t]
1792        ["Lower thread score" gnus-summary-lower-thread t]
1793        ["Raise thread score" gnus-summary-raise-thread t]
1794        ["Rethread current" gnus-summary-rethread-current t]))
1795
1796     (easy-menu-define
1797      gnus-summary-post-menu gnus-summary-mode-map ""
1798      '("Post"
1799        ["Post an article" gnus-summary-post-news t]
1800        ["Followup" gnus-summary-followup t]
1801        ["Followup and yank" gnus-summary-followup-with-original t]
1802        ["Supersede article" gnus-summary-supersede-article t]
1803        ["Cancel article" gnus-summary-cancel-article t]
1804        ["Reply" gnus-summary-reply t]
1805        ["Reply and yank" gnus-summary-reply-with-original t]
1806        ["Wide reply" gnus-summary-wide-reply t]
1807        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1808        ["Mail forward" gnus-summary-mail-forward t]
1809        ["Post forward" gnus-summary-post-forward t]
1810        ["Digest and mail" gnus-uu-digest-mail-forward t]
1811        ["Digest and post" gnus-uu-digest-post-forward t]
1812        ["Resend message" gnus-summary-resend-message t]
1813        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1814        ["Send a mail" gnus-summary-mail-other-window t]
1815        ["Uuencode and post" gnus-uu-post-news t]
1816        ["Followup via news" gnus-summary-followup-to-mail t]
1817        ["Followup via news and yank"
1818         gnus-summary-followup-to-mail-with-original t]
1819        ;;("Draft"
1820        ;;["Send" gnus-summary-send-draft t]
1821        ;;["Send bounced" gnus-resend-bounced-mail t])
1822        ))
1823
1824     (easy-menu-define
1825      gnus-summary-misc-menu gnus-summary-mode-map ""
1826      '("Misc"
1827        ("Mark Read"
1828         ["Mark as read" gnus-summary-mark-as-read-forward t]
1829         ["Mark same subject and select"
1830          gnus-summary-kill-same-subject-and-select t]
1831         ["Mark same subject" gnus-summary-kill-same-subject t]
1832         ["Catchup" gnus-summary-catchup t]
1833         ["Catchup all" gnus-summary-catchup-all t]
1834         ["Catchup to here" gnus-summary-catchup-to-here t]
1835         ["Catchup region" gnus-summary-mark-region-as-read t]
1836         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1837        ("Mark Various"
1838         ["Tick" gnus-summary-tick-article-forward t]
1839         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1840         ["Remove marks" gnus-summary-clear-mark-forward t]
1841         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1842         ["Set bookmark" gnus-summary-set-bookmark t]
1843         ["Remove bookmark" gnus-summary-remove-bookmark t])
1844        ("Mark Limit"
1845         ["Marks..." gnus-summary-limit-to-marks t]
1846         ["Subject..." gnus-summary-limit-to-subject t]
1847         ["Author..." gnus-summary-limit-to-author t]
1848         ["Age..." gnus-summary-limit-to-age t]
1849         ["Extra..." gnus-summary-limit-to-extra t]
1850         ["Score" gnus-summary-limit-to-score t]
1851         ["Unread" gnus-summary-limit-to-unread t]
1852         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1853         ["Articles" gnus-summary-limit-to-articles t]
1854         ["Pop limit" gnus-summary-pop-limit t]
1855         ["Show dormant" gnus-summary-limit-include-dormant t]
1856         ["Hide childless dormant"
1857          gnus-summary-limit-exclude-childless-dormant t]
1858         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1859         ["Hide marked" gnus-summary-limit-exclude-marks t]
1860         ["Show expunged" gnus-summary-show-all-expunged t])
1861        ("Process Mark"
1862         ["Set mark" gnus-summary-mark-as-processable t]
1863         ["Remove mark" gnus-summary-unmark-as-processable t]
1864         ["Remove all marks" gnus-summary-unmark-all-processable t]
1865         ["Mark above" gnus-uu-mark-over t]
1866         ["Mark series" gnus-uu-mark-series t]
1867         ["Mark region" gnus-uu-mark-region t]
1868         ["Unmark region" gnus-uu-unmark-region t]
1869         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1870         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1871         ["Mark all" gnus-uu-mark-all t]
1872         ["Mark buffer" gnus-uu-mark-buffer t]
1873         ["Mark sparse" gnus-uu-mark-sparse t]
1874         ["Mark thread" gnus-uu-mark-thread t]
1875         ["Unmark thread" gnus-uu-unmark-thread t]
1876         ("Process Mark Sets"
1877          ["Kill" gnus-summary-kill-process-mark t]
1878          ["Yank" gnus-summary-yank-process-mark
1879           gnus-newsgroup-process-stack]
1880          ["Save" gnus-summary-save-process-mark t]))
1881        ("Scroll article"
1882         ["Page forward" gnus-summary-next-page t]
1883         ["Page backward" gnus-summary-prev-page t]
1884         ["Line forward" gnus-summary-scroll-up t])
1885        ("Move"
1886         ["Next unread article" gnus-summary-next-unread-article t]
1887         ["Previous unread article" gnus-summary-prev-unread-article t]
1888         ["Next article" gnus-summary-next-article t]
1889         ["Previous article" gnus-summary-prev-article t]
1890         ["Next unread subject" gnus-summary-next-unread-subject t]
1891         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1892         ["Next article same subject" gnus-summary-next-same-subject t]
1893         ["Previous article same subject" gnus-summary-prev-same-subject t]
1894         ["First unread article" gnus-summary-first-unread-article t]
1895         ["Best unread article" gnus-summary-best-unread-article t]
1896         ["Go to subject number..." gnus-summary-goto-subject t]
1897         ["Go to article number..." gnus-summary-goto-article t]
1898         ["Go to the last article" gnus-summary-goto-last-article t]
1899         ["Pop article off history" gnus-summary-pop-article t])
1900        ("Sort"
1901         ["Sort by number" gnus-summary-sort-by-number t]
1902         ["Sort by author" gnus-summary-sort-by-author t]
1903         ["Sort by subject" gnus-summary-sort-by-subject t]
1904         ["Sort by date" gnus-summary-sort-by-date t]
1905         ["Sort by score" gnus-summary-sort-by-score t]
1906         ["Sort by lines" gnus-summary-sort-by-lines t]
1907         ["Sort by characters" gnus-summary-sort-by-chars t])
1908        ("Help"
1909         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1910         ["Describe group" gnus-summary-describe-group t]
1911         ["Read manual" gnus-info-find-node t])
1912        ("Modes"
1913         ["Pick and read" gnus-pick-mode t]
1914         ["Binary" gnus-binary-mode t])
1915        ("Regeneration"
1916         ["Regenerate" gnus-summary-prepare t]
1917         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1918         ["Toggle threading" gnus-summary-toggle-threads t])
1919        ["Filter articles..." gnus-summary-execute-command t]
1920        ["Run command on subjects..." gnus-summary-universal-argument t]
1921        ["Search articles forward..." gnus-summary-search-article-forward t]
1922        ["Search articles backward..." gnus-summary-search-article-backward t]
1923        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1924        ["Expand window" gnus-summary-expand-window t]
1925        ["Expire expirable articles" gnus-summary-expire-articles
1926         (gnus-check-backend-function
1927          'request-expire-articles gnus-newsgroup-name)]
1928        ["Edit local kill file" gnus-summary-edit-local-kill t]
1929        ["Edit main kill file" gnus-summary-edit-global-kill t]
1930        ["Edit group parameters" gnus-summary-edit-parameters t]
1931        ["Customize group parameters" gnus-summary-customize-parameters t]
1932        ["Send a bug report" gnus-bug t]
1933        ("Exit"
1934         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1935         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1936         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1937         ["Exit group" gnus-summary-exit t]
1938         ["Exit group without updating" gnus-summary-exit-no-update t]
1939         ["Exit and goto next group" gnus-summary-next-group t]
1940         ["Exit and goto prev group" gnus-summary-prev-group t]
1941         ["Reselect group" gnus-summary-reselect-current-group t]
1942         ["Rescan group" gnus-summary-rescan-group t]
1943         ["Update dribble" gnus-summary-save-newsrc t])))
1944
1945     (gnus-run-hooks 'gnus-summary-menu-hook)))
1946
1947 (defun gnus-score-set-default (var value)
1948   "A version of set that updates the GNU Emacs menu-bar."
1949   (set var value)
1950   ;; It is the message that forces the active status to be updated.
1951   (message ""))
1952
1953 (defun gnus-make-score-map (type)
1954   "Make a summary score map of type TYPE."
1955   (if t
1956       nil
1957     (let ((headers '(("author" "from" string)
1958                      ("subject" "subject" string)
1959                      ("article body" "body" string)
1960                      ("article head" "head" string)
1961                      ("xref" "xref" string)
1962                      ("extra header" "extra" string)
1963                      ("lines" "lines" number)
1964                      ("followups to author" "followup" string)))
1965           (types '((number ("less than" <)
1966                            ("greater than" >)
1967                            ("equal" =))
1968                    (string ("substring" s)
1969                            ("exact string" e)
1970                            ("fuzzy string" f)
1971                            ("regexp" r))))
1972           (perms '(("temporary" (current-time-string))
1973                    ("permanent" nil)
1974                    ("immediate" now)))
1975           header)
1976       (list
1977        (apply
1978         'nconc
1979         (list
1980          (if (eq type 'lower)
1981              "Lower score"
1982            "Increase score"))
1983         (let (outh)
1984           (while headers
1985             (setq header (car headers))
1986             (setq outh
1987                   (cons
1988                    (apply
1989                     'nconc
1990                     (list (car header))
1991                     (let ((ts (cdr (assoc (nth 2 header) types)))
1992                           outt)
1993                       (while ts
1994                         (setq outt
1995                               (cons
1996                                (apply
1997                                 'nconc
1998                                 (list (caar ts))
1999                                 (let ((ps perms)
2000                                       outp)
2001                                   (while ps
2002                                     (setq outp
2003                                           (cons
2004                                            (vector
2005                                             (caar ps)
2006                                             (list
2007                                              'gnus-summary-score-entry
2008                                              (nth 1 header)
2009                                              (if (or (string= (nth 1 header)
2010                                                               "head")
2011                                                      (string= (nth 1 header)
2012                                                               "body"))
2013                                                  ""
2014                                                (list 'gnus-summary-header
2015                                                      (nth 1 header)))
2016                                              (list 'quote (nth 1 (car ts)))
2017                                              (list 'gnus-score-delta-default
2018                                                    nil)
2019                                              (nth 1 (car ps))
2020                                              t)
2021                                             t)
2022                                            outp))
2023                                     (setq ps (cdr ps)))
2024                                   (list (nreverse outp))))
2025                                outt))
2026                         (setq ts (cdr ts)))
2027                       (list (nreverse outt))))
2028                    outh))
2029             (setq headers (cdr headers)))
2030           (list (nreverse outh))))))))
2031
2032 \f
2033
2034 (defun gnus-summary-mode (&optional group)
2035   "Major mode for reading articles.
2036
2037 All normal editing commands are switched off.
2038 \\<gnus-summary-mode-map>
2039 Each line in this buffer represents one article.  To read an
2040 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2041 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2042 respectively.
2043
2044 You can also post articles and send mail from this buffer.  To
2045 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2046 of an article, type `\\[gnus-summary-reply]'.
2047
2048 There are approx. one gazillion commands you can execute in this
2049 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2050
2051 The following commands are available:
2052
2053 \\{gnus-summary-mode-map}"
2054   (interactive)
2055   (when (gnus-visual-p 'summary-menu 'menu)
2056     (gnus-summary-make-menu-bar))
2057   (kill-all-local-variables)
2058   (gnus-summary-make-local-variables)
2059   (gnus-make-thread-indent-array)
2060   (gnus-simplify-mode-line)
2061   (setq major-mode 'gnus-summary-mode)
2062   (setq mode-name "Summary")
2063   (make-local-variable 'minor-mode-alist)
2064   (use-local-map gnus-summary-mode-map)
2065   (buffer-disable-undo)
2066   (setq buffer-read-only t)             ;Disable modification
2067   (setq truncate-lines t)
2068   (setq selective-display t)
2069   (setq selective-display-ellipses t)   ;Display `...'
2070   (gnus-summary-set-display-table)
2071   (gnus-set-default-directory)
2072   (setq gnus-newsgroup-name group)
2073   (make-local-variable 'gnus-summary-line-format)
2074   (make-local-variable 'gnus-summary-line-format-spec)
2075   (make-local-variable 'gnus-summary-dummy-line-format)
2076   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2077   (make-local-variable 'gnus-summary-mark-positions)
2078   (make-local-hook 'pre-command-hook)
2079   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2080   (gnus-run-hooks 'gnus-summary-mode-hook)
2081   (mm-enable-multibyte)
2082   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2083   (gnus-update-summary-mark-positions))
2084
2085 (defun gnus-summary-make-local-variables ()
2086   "Make all the local summary buffer variables."
2087   (let (global)
2088     (dolist (local gnus-summary-local-variables)
2089       (if (consp local)
2090           (progn
2091             (if (eq (cdr local) 'global)
2092                 ;; Copy the global value of the variable.
2093                 (setq global (symbol-value (car local)))
2094               ;; Use the value from the list.
2095               (setq global (eval (cdr local))))
2096             (set (make-local-variable (car local)) global))
2097         ;; Simple nil-valued local variable.
2098         (set (make-local-variable local) nil)))))
2099
2100 (defun gnus-summary-clear-local-variables ()
2101   (let ((locals gnus-summary-local-variables))
2102     (while locals
2103       (if (consp (car locals))
2104           (and (vectorp (caar locals))
2105                (set (caar locals) nil))
2106         (and (vectorp (car locals))
2107              (set (car locals) nil)))
2108       (setq locals (cdr locals)))))
2109
2110 ;; Summary data functions.
2111
2112 (defmacro gnus-data-number (data)
2113   `(car ,data))
2114
2115 (defmacro gnus-data-set-number (data number)
2116   `(setcar ,data ,number))
2117
2118 (defmacro gnus-data-mark (data)
2119   `(nth 1 ,data))
2120
2121 (defmacro gnus-data-set-mark (data mark)
2122   `(setcar (nthcdr 1 ,data) ,mark))
2123
2124 (defmacro gnus-data-pos (data)
2125   `(nth 2 ,data))
2126
2127 (defmacro gnus-data-set-pos (data pos)
2128   `(setcar (nthcdr 2 ,data) ,pos))
2129
2130 (defmacro gnus-data-header (data)
2131   `(nth 3 ,data))
2132
2133 (defmacro gnus-data-set-header (data header)
2134   `(setf (nth 3 ,data) ,header))
2135
2136 (defmacro gnus-data-level (data)
2137   `(nth 4 ,data))
2138
2139 (defmacro gnus-data-unread-p (data)
2140   `(= (nth 1 ,data) gnus-unread-mark))
2141
2142 (defmacro gnus-data-read-p (data)
2143   `(/= (nth 1 ,data) gnus-unread-mark))
2144
2145 (defmacro gnus-data-pseudo-p (data)
2146   `(consp (nth 3 ,data)))
2147
2148 (defmacro gnus-data-find (number)
2149   `(assq ,number gnus-newsgroup-data))
2150
2151 (defmacro gnus-data-find-list (number &optional data)
2152   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2153      (memq (assq ,number bdata)
2154            bdata)))
2155
2156 (defmacro gnus-data-make (number mark pos header level)
2157   `(list ,number ,mark ,pos ,header ,level))
2158
2159 (defun gnus-data-enter (after-article number mark pos header level offset)
2160   (let ((data (gnus-data-find-list after-article)))
2161     (unless data
2162       (error "No such article: %d" after-article))
2163     (setcdr data (cons (gnus-data-make number mark pos header level)
2164                        (cdr data)))
2165     (setq gnus-newsgroup-data-reverse nil)
2166     (gnus-data-update-list (cddr data) offset)))
2167
2168 (defun gnus-data-enter-list (after-article list &optional offset)
2169   (when list
2170     (let ((data (and after-article (gnus-data-find-list after-article)))
2171           (ilist list))
2172       (if (not (or data
2173                    after-article))
2174           (let ((odata gnus-newsgroup-data))
2175             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2176             (when offset
2177               (gnus-data-update-list odata offset)))
2178         ;; Find the last element in the list to be spliced into the main
2179         ;; list.
2180         (while (cdr list)
2181           (setq list (cdr list)))
2182         (if (not data)
2183             (progn
2184               (setcdr list gnus-newsgroup-data)
2185               (setq gnus-newsgroup-data ilist)
2186               (when offset
2187                 (gnus-data-update-list (cdr list) offset)))
2188           (setcdr list (cdr data))
2189           (setcdr data ilist)
2190           (when offset
2191             (gnus-data-update-list (cdr list) offset))))
2192       (setq gnus-newsgroup-data-reverse nil))))
2193
2194 (defun gnus-data-remove (article &optional offset)
2195   (let ((data gnus-newsgroup-data))
2196     (if (= (gnus-data-number (car data)) article)
2197         (progn
2198           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2199                 gnus-newsgroup-data-reverse nil)
2200           (when offset
2201             (gnus-data-update-list gnus-newsgroup-data offset)))
2202       (while (cdr data)
2203         (when (= (gnus-data-number (cadr data)) article)
2204           (setcdr data (cddr data))
2205           (when offset
2206             (gnus-data-update-list (cdr data) offset))
2207           (setq data nil
2208                 gnus-newsgroup-data-reverse nil))
2209         (setq data (cdr data))))))
2210
2211 (defmacro gnus-data-list (backward)
2212   `(if ,backward
2213        (or gnus-newsgroup-data-reverse
2214            (setq gnus-newsgroup-data-reverse
2215                  (reverse gnus-newsgroup-data)))
2216      gnus-newsgroup-data))
2217
2218 (defun gnus-data-update-list (data offset)
2219   "Add OFFSET to the POS of all data entries in DATA."
2220   (setq gnus-newsgroup-data-reverse nil)
2221   (while data
2222     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2223     (setq data (cdr data))))
2224
2225 (defun gnus-summary-article-pseudo-p (article)
2226   "Say whether this article is a pseudo article or not."
2227   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2228
2229 (defmacro gnus-summary-article-sparse-p (article)
2230   "Say whether this article is a sparse article or not."
2231   `(memq ,article gnus-newsgroup-sparse))
2232
2233 (defmacro gnus-summary-article-ancient-p (article)
2234   "Say whether this article is a sparse article or not."
2235   `(memq ,article gnus-newsgroup-ancient))
2236
2237 (defun gnus-article-parent-p (number)
2238   "Say whether this article is a parent or not."
2239   (let ((data (gnus-data-find-list number)))
2240     (and (cdr data)                     ; There has to be an article after...
2241          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2242             (gnus-data-level (nth 1 data))))))
2243
2244 (defun gnus-article-children (number)
2245   "Return a list of all children to NUMBER."
2246   (let* ((data (gnus-data-find-list number))
2247          (level (gnus-data-level (car data)))
2248          children)
2249     (setq data (cdr data))
2250     (while (and data
2251                 (= (gnus-data-level (car data)) (1+ level)))
2252       (push (gnus-data-number (car data)) children)
2253       (setq data (cdr data)))
2254     children))
2255
2256 (defmacro gnus-summary-skip-intangible ()
2257   "If the current article is intangible, then jump to a different article."
2258   '(let ((to (get-text-property (point) 'gnus-intangible)))
2259      (and to (gnus-summary-goto-subject to))))
2260
2261 (defmacro gnus-summary-article-intangible-p ()
2262   "Say whether this article is intangible or not."
2263   '(get-text-property (point) 'gnus-intangible))
2264
2265 (defun gnus-article-read-p (article)
2266   "Say whether ARTICLE is read or not."
2267   (not (or (memq article gnus-newsgroup-marked)
2268            (memq article gnus-newsgroup-unreads)
2269            (memq article gnus-newsgroup-unselected)
2270            (memq article gnus-newsgroup-dormant))))
2271
2272 ;; Some summary mode macros.
2273
2274 (defmacro gnus-summary-article-number ()
2275   "The article number of the article on the current line.
2276 If there isn's an article number here, then we return the current
2277 article number."
2278   '(progn
2279      (gnus-summary-skip-intangible)
2280      (or (get-text-property (point) 'gnus-number)
2281          (gnus-summary-last-subject))))
2282
2283 (defmacro gnus-summary-article-header (&optional number)
2284   "Return the header of article NUMBER."
2285   `(gnus-data-header (gnus-data-find
2286                       ,(or number '(gnus-summary-article-number)))))
2287
2288 (defmacro gnus-summary-thread-level (&optional number)
2289   "Return the level of thread that starts with article NUMBER."
2290   `(if (and (eq gnus-summary-make-false-root 'dummy)
2291             (get-text-property (point) 'gnus-intangible))
2292        0
2293      (gnus-data-level (gnus-data-find
2294                        ,(or number '(gnus-summary-article-number))))))
2295
2296 (defmacro gnus-summary-article-mark (&optional number)
2297   "Return the mark of article NUMBER."
2298   `(gnus-data-mark (gnus-data-find
2299                     ,(or number '(gnus-summary-article-number)))))
2300
2301 (defmacro gnus-summary-article-pos (&optional number)
2302   "Return the position of the line of article NUMBER."
2303   `(gnus-data-pos (gnus-data-find
2304                    ,(or number '(gnus-summary-article-number)))))
2305
2306 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2307 (defmacro gnus-summary-article-subject (&optional number)
2308   "Return current subject string or nil if nothing."
2309   `(let ((headers
2310           ,(if number
2311                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2312              '(gnus-data-header (assq (gnus-summary-article-number)
2313                                       gnus-newsgroup-data)))))
2314      (and headers
2315           (vectorp headers)
2316           (mail-header-subject headers))))
2317
2318 (defmacro gnus-summary-article-score (&optional number)
2319   "Return current article score."
2320   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2321                   gnus-newsgroup-scored))
2322        gnus-summary-default-score 0))
2323
2324 (defun gnus-summary-article-children (&optional number)
2325   "Return a list of article numbers that are children of article NUMBER."
2326   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2327          (level (gnus-data-level (car data)))
2328          l children)
2329     (while (and (setq data (cdr data))
2330                 (> (setq l (gnus-data-level (car data))) level))
2331       (and (= (1+ level) l)
2332            (push (gnus-data-number (car data))
2333                  children)))
2334     (nreverse children)))
2335
2336 (defun gnus-summary-article-parent (&optional number)
2337   "Return the article number of the parent of article NUMBER."
2338   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2339                                     (gnus-data-list t)))
2340          (level (gnus-data-level (car data))))
2341     (if (zerop level)
2342         ()                              ; This is a root.
2343       ;; We search until we find an article with a level less than
2344       ;; this one.  That function has to be the parent.
2345       (while (and (setq data (cdr data))
2346                   (not (< (gnus-data-level (car data)) level))))
2347       (and data (gnus-data-number (car data))))))
2348
2349 (defun gnus-unread-mark-p (mark)
2350   "Say whether MARK is the unread mark."
2351   (= mark gnus-unread-mark))
2352
2353 (defun gnus-read-mark-p (mark)
2354   "Say whether MARK is one of the marks that mark as read.
2355 This is all marks except unread, ticked, dormant, and expirable."
2356   (not (or (= mark gnus-unread-mark)
2357            (= mark gnus-ticked-mark)
2358            (= mark gnus-dormant-mark)
2359            (= mark gnus-expirable-mark))))
2360
2361 (defmacro gnus-article-mark (number)
2362   "Return the MARK of article NUMBER.
2363 This macro should only be used when computing the mark the \"first\"
2364 time; i.e., when generating the summary lines.  After that,
2365 `gnus-summary-article-mark' should be used to examine the
2366 marks of articles."
2367   `(cond
2368     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2369     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2370     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2371     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2372     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2373     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2374     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2375     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2376            gnus-ancient-mark))))
2377
2378 ;; Saving hidden threads.
2379
2380 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2381 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2382
2383 (defmacro gnus-save-hidden-threads (&rest forms)
2384   "Save hidden threads, eval FORMS, and restore the hidden threads."
2385   (let ((config (make-symbol "config")))
2386     `(let ((,config (gnus-hidden-threads-configuration)))
2387        (unwind-protect
2388            (save-excursion
2389              ,@forms)
2390          (gnus-restore-hidden-threads-configuration ,config)))))
2391
2392 (defun gnus-data-compute-positions ()
2393   "Compute the positions of all articles."
2394   (setq gnus-newsgroup-data-reverse nil)
2395   (let ((data gnus-newsgroup-data))
2396     (save-excursion
2397       (gnus-save-hidden-threads
2398         (gnus-summary-show-all-threads)
2399         (goto-char (point-min))
2400         (while data
2401           (while (get-text-property (point) 'gnus-intangible)
2402             (forward-line 1))
2403           (gnus-data-set-pos (car data) (+ (point) 3))
2404           (setq data (cdr data))
2405           (forward-line 1))))))
2406
2407 (defun gnus-hidden-threads-configuration ()
2408   "Return the current hidden threads configuration."
2409   (save-excursion
2410     (let (config)
2411       (goto-char (point-min))
2412       (while (search-forward "\r" nil t)
2413         (push (1- (point)) config))
2414       config)))
2415
2416 (defun gnus-restore-hidden-threads-configuration (config)
2417   "Restore hidden threads configuration from CONFIG."
2418   (let (point buffer-read-only)
2419     (while (setq point (pop config))
2420       (when (and (< point (point-max))
2421                  (goto-char point)
2422                  (eq (char-after) ?\n))
2423         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2424
2425 ;; Various summary mode internalish functions.
2426
2427 (defun gnus-mouse-pick-article (e)
2428   (interactive "e")
2429   (mouse-set-point e)
2430   (gnus-summary-next-page nil t))
2431
2432 (defun gnus-summary-set-display-table ()
2433   ;; Change the display table.  Odd characters have a tendency to mess
2434   ;; up nicely formatted displays - we make all possible glyphs
2435   ;; display only a single character.
2436
2437   ;; We start from the standard display table, if any.
2438   (let ((table (or (copy-sequence standard-display-table)
2439                    (make-display-table)))
2440         (i 32))
2441     ;; Nix out all the control chars...
2442     (while (>= (setq i (1- i)) 0)
2443       (aset table i [??]))
2444     ;; ... but not newline and cr, of course.  (cr is necessary for the
2445     ;; selective display).
2446     (aset table ?\n nil)
2447     (aset table ?\r nil)
2448     ;; We keep TAB as well.
2449     (aset table ?\t nil)
2450     ;; We nix out any glyphs over 126 that are not set already.
2451     (let ((i 256))
2452       (while (>= (setq i (1- i)) 127)
2453         ;; Only modify if the entry is nil.
2454         (unless (aref table i)
2455           (aset table i [??]))))
2456     (setq buffer-display-table table)))
2457
2458 (defun gnus-summary-setup-buffer (group)
2459   "Initialize summary buffer."
2460   (let ((buffer (concat "*Summary " group "*")))
2461     (if (get-buffer buffer)
2462         (progn
2463           (set-buffer buffer)
2464           (setq gnus-summary-buffer (current-buffer))
2465           (not gnus-newsgroup-prepared))
2466       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2467       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2468       (gnus-summary-mode group)
2469       (when gnus-carpal
2470         (gnus-carpal-setup-buffer 'summary))
2471       (unless gnus-single-article-buffer
2472         (make-local-variable 'gnus-article-buffer)
2473         (make-local-variable 'gnus-article-current)
2474         (make-local-variable 'gnus-original-article-buffer))
2475       (setq gnus-newsgroup-name group)
2476       t)))
2477
2478 (defun gnus-set-global-variables ()
2479   ;; Set the global equivalents of the summary buffer-local variables
2480   ;; to the latest values they had.  These reflect the summary buffer
2481   ;; that was in action when the last article was fetched.
2482   (when (eq major-mode 'gnus-summary-mode)
2483     (setq gnus-summary-buffer (current-buffer))
2484     (let ((name gnus-newsgroup-name)
2485           (marked gnus-newsgroup-marked)
2486           (unread gnus-newsgroup-unreads)
2487           (headers gnus-current-headers)
2488           (data gnus-newsgroup-data)
2489           (summary gnus-summary-buffer)
2490           (article-buffer gnus-article-buffer)
2491           (original gnus-original-article-buffer)
2492           (gac gnus-article-current)
2493           (reffed gnus-reffed-article-number)
2494           (score-file gnus-current-score-file)
2495           (default-charset gnus-newsgroup-charset))
2496       (save-excursion
2497         (set-buffer gnus-group-buffer)
2498         (setq gnus-newsgroup-name name
2499               gnus-newsgroup-marked marked
2500               gnus-newsgroup-unreads unread
2501               gnus-current-headers headers
2502               gnus-newsgroup-data data
2503               gnus-article-current gac
2504               gnus-summary-buffer summary
2505               gnus-article-buffer article-buffer
2506               gnus-original-article-buffer original
2507               gnus-reffed-article-number reffed
2508               gnus-current-score-file score-file
2509               gnus-newsgroup-charset default-charset)
2510         ;; The article buffer also has local variables.
2511         (when (gnus-buffer-live-p gnus-article-buffer)
2512           (set-buffer gnus-article-buffer)
2513           (setq gnus-summary-buffer summary))))))
2514
2515 (defun gnus-summary-article-unread-p (article)
2516   "Say whether ARTICLE is unread or not."
2517   (memq article gnus-newsgroup-unreads))
2518
2519 (defun gnus-summary-first-article-p (&optional article)
2520   "Return whether ARTICLE is the first article in the buffer."
2521   (if (not (setq article (or article (gnus-summary-article-number))))
2522       nil
2523     (eq article (caar gnus-newsgroup-data))))
2524
2525 (defun gnus-summary-last-article-p (&optional article)
2526   "Return whether ARTICLE is the last article in the buffer."
2527   (if (not (setq article (or article (gnus-summary-article-number))))
2528       ;; All non-existent numbers are the last article.  :-)
2529       t
2530     (not (cdr (gnus-data-find-list article)))))
2531
2532 (defun gnus-make-thread-indent-array ()
2533   (let ((n 200))
2534     (unless (and gnus-thread-indent-array
2535                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2536       (setq gnus-thread-indent-array (make-vector 201 "")
2537             gnus-thread-indent-array-level gnus-thread-indent-level)
2538       (while (>= n 0)
2539         (aset gnus-thread-indent-array n
2540               (make-string (* n gnus-thread-indent-level) ? ))
2541         (setq n (1- n))))))
2542
2543 (defun gnus-update-summary-mark-positions ()
2544   "Compute where the summary marks are to go."
2545   (save-excursion
2546     (when (gnus-buffer-exists-p gnus-summary-buffer)
2547       (set-buffer gnus-summary-buffer))
2548     (let ((gnus-replied-mark 129)
2549           (gnus-score-below-mark 130)
2550           (gnus-score-over-mark 130)
2551           (gnus-download-mark 131)
2552           (spec gnus-summary-line-format-spec)
2553           gnus-visual pos)
2554       (save-excursion
2555         (gnus-set-work-buffer)
2556         (let ((gnus-summary-line-format-spec spec)
2557               (gnus-newsgroup-downloadable '((0 . t))))
2558           (gnus-summary-insert-line
2559            (make-full-mail-header 0 "" "" "" "" "" 0 0 "" nil)
2560            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 to))
2598                   to)))
2599      ((and newsgroups
2600            gnus-ignored-from-addresses
2601            (string-match gnus-ignored-from-addresses
2602                          (mail-header-from header)))
2603       (concat "=> " newsgroups))
2604      (t
2605       (or (car (funcall gnus-extract-address-components
2606                         (mail-header-from header)))
2607           (mail-header-from header))))))
2608
2609 (defun gnus-summary-insert-line (gnus-tmp-header
2610                                  gnus-tmp-level gnus-tmp-current
2611                                  gnus-tmp-unread gnus-tmp-replied
2612                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2613                                  &optional gnus-tmp-dummy gnus-tmp-score
2614                                  gnus-tmp-process)
2615   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2616          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2617          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2618          (gnus-tmp-score-char
2619           (if (or (null gnus-summary-default-score)
2620                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2621                       gnus-summary-zcore-fuzz))
2622               ?  ;Whitespace
2623             (if (< gnus-tmp-score gnus-summary-default-score)
2624                 gnus-score-below-mark gnus-score-over-mark)))
2625          (gnus-tmp-replied
2626           (cond (gnus-tmp-process gnus-process-mark)
2627                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2628                  gnus-cached-mark)
2629                 (gnus-tmp-replied gnus-replied-mark)
2630                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2631                  gnus-saved-mark)
2632                 (t gnus-unread-mark)))
2633          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2634          (gnus-tmp-name
2635           (cond
2636            ((string-match "<[^>]+> *$" gnus-tmp-from)
2637             (let ((beg (match-beginning 0)))
2638               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2639                        (substring gnus-tmp-from (1+ (match-beginning 0))
2640                                   (1- (match-end 0))))
2641                   (substring gnus-tmp-from 0 beg))))
2642            ((string-match "(.+)" gnus-tmp-from)
2643             (substring gnus-tmp-from
2644                        (1+ (match-beginning 0)) (1- (match-end 0))))
2645            (t gnus-tmp-from)))
2646          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2647          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2648          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2649          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2650          (buffer-read-only nil))
2651     (when (string= gnus-tmp-name "")
2652       (setq gnus-tmp-name gnus-tmp-from))
2653     (unless (numberp gnus-tmp-lines)
2654       (setq gnus-tmp-lines 0))
2655     (gnus-put-text-property
2656      (point)
2657      (progn (eval gnus-summary-line-format-spec) (point))
2658      'gnus-number gnus-tmp-number)
2659     (when (gnus-visual-p 'summary-highlight 'highlight)
2660       (forward-line -1)
2661       (gnus-run-hooks 'gnus-summary-update-hook)
2662       (forward-line 1))))
2663
2664 (defun gnus-summary-update-line (&optional dont-update)
2665   ;; Update summary line after change.
2666   (when (and gnus-summary-default-score
2667              (not gnus-summary-inhibit-highlight))
2668     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2669            (article (gnus-summary-article-number))
2670            (score (gnus-summary-article-score article)))
2671       (unless dont-update
2672         (if (and gnus-summary-mark-below
2673                  (< (gnus-summary-article-score)
2674                     gnus-summary-mark-below))
2675             ;; This article has a low score, so we mark it as read.
2676             (when (memq article gnus-newsgroup-unreads)
2677               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2678           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2679             ;; This article was previously marked as read on account
2680             ;; of a low score, but now it has risen, so we mark it as
2681             ;; unread.
2682             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2683         (gnus-summary-update-mark
2684          (if (or (null gnus-summary-default-score)
2685                  (<= (abs (- score gnus-summary-default-score))
2686                      gnus-summary-zcore-fuzz))
2687              ?  ;Whitespace
2688            (if (< score gnus-summary-default-score)
2689                gnus-score-below-mark gnus-score-over-mark))
2690          'score))
2691       ;; Do visual highlighting.
2692       (when (gnus-visual-p 'summary-highlight 'highlight)
2693         (gnus-run-hooks 'gnus-summary-update-hook)))))
2694
2695 (defvar gnus-tmp-new-adopts nil)
2696
2697 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2698   "Return the number of articles in THREAD.
2699 This may be 0 in some cases -- if none of the articles in
2700 the thread are to be displayed."
2701   (let* ((number
2702           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2703           (cond
2704            ((not (listp thread))
2705             1)
2706            ((and (consp thread) (cdr thread))
2707             (apply
2708              '+ 1 (mapcar
2709                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2710            ((null thread)
2711             1)
2712            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2713             1)
2714            (t 0))))
2715     (when (and level (zerop level) gnus-tmp-new-adopts)
2716       (incf number
2717             (apply '+ (mapcar
2718                        'gnus-summary-number-of-articles-in-thread
2719                        gnus-tmp-new-adopts))))
2720     (if char
2721         (if (> number 1) gnus-not-empty-thread-mark
2722           gnus-empty-thread-mark)
2723       number)))
2724
2725 (defun gnus-summary-set-local-parameters (group)
2726   "Go through the local params of GROUP and set all variable specs in that list."
2727   (let ((params (gnus-group-find-parameter group))
2728         elem)
2729     (while params
2730       (setq elem (car params)
2731             params (cdr params))
2732       (and (consp elem)                 ; Has to be a cons.
2733            (consp (cdr elem))           ; The cdr has to be a list.
2734            (symbolp (car elem))         ; Has to be a symbol in there.
2735            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2736            (ignore-errors               ; So we set it.
2737              (make-local-variable (car elem))
2738              (set (car elem) (eval (nth 1 elem))))))))
2739
2740 (defun gnus-summary-read-group (group &optional show-all no-article
2741                                       kill-buffer no-display backward
2742                                       select-articles)
2743   "Start reading news in newsgroup GROUP.
2744 If SHOW-ALL is non-nil, already read articles are also listed.
2745 If NO-ARTICLE is non-nil, no article is selected initially.
2746 If NO-DISPLAY, don't generate a summary buffer."
2747   (let (result)
2748     (while (and group
2749                 (null (setq result
2750                             (let ((gnus-auto-select-next nil))
2751                               (or (gnus-summary-read-group-1
2752                                    group show-all no-article
2753                                    kill-buffer no-display
2754                                    select-articles)
2755                                   (setq show-all nil
2756                                         select-articles nil)))))
2757                 (eq gnus-auto-select-next 'quietly))
2758       (set-buffer gnus-group-buffer)
2759       ;; The entry function called above goes to the next
2760       ;; group automatically, so we go two groups back
2761       ;; if we are searching for the previous group.
2762       (when backward
2763         (gnus-group-prev-unread-group 2))
2764       (if (not (equal group (gnus-group-group-name)))
2765           (setq group (gnus-group-group-name))
2766         (setq group nil)))
2767     result))
2768
2769 (defun gnus-summary-read-group-1 (group show-all no-article
2770                                         kill-buffer no-display
2771                                         &optional select-articles)
2772   ;; Killed foreign groups can't be entered.
2773   (when (and (not (gnus-group-native-p group))
2774              (not (gnus-gethash group gnus-newsrc-hashtb)))
2775     (error "Dead non-native groups can't be entered"))
2776   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2777   (let* ((new-group (gnus-summary-setup-buffer group))
2778          (quit-config (gnus-group-quit-config group))
2779          (did-select (and new-group (gnus-select-newsgroup
2780                                      group show-all select-articles))))
2781     (cond
2782      ;; This summary buffer exists already, so we just select it.
2783      ((not new-group)
2784       (gnus-set-global-variables)
2785       (when kill-buffer
2786         (gnus-kill-or-deaden-summary kill-buffer))
2787       (gnus-configure-windows 'summary 'force)
2788       (gnus-set-mode-line 'summary)
2789       (gnus-summary-position-point)
2790       (message "")
2791       t)
2792      ;; We couldn't select this group.
2793      ((null did-select)
2794       (when (and (eq major-mode 'gnus-summary-mode)
2795                  (not (equal (current-buffer) kill-buffer)))
2796         (kill-buffer (current-buffer))
2797         (if (not quit-config)
2798             (progn
2799               ;; Update the info -- marks might need to be removed,
2800               ;; for instance.
2801               (gnus-summary-update-info)
2802               (set-buffer gnus-group-buffer)
2803               (gnus-group-jump-to-group group)
2804               (gnus-group-next-unread-group 1))
2805           (gnus-handle-ephemeral-exit quit-config)))
2806       (gnus-message 3 "Can't select group")
2807       nil)
2808      ;; The user did a `C-g' while prompting for number of articles,
2809      ;; so we exit this group.
2810      ((eq did-select 'quit)
2811       (and (eq major-mode 'gnus-summary-mode)
2812            (not (equal (current-buffer) kill-buffer))
2813            (kill-buffer (current-buffer)))
2814       (when kill-buffer
2815         (gnus-kill-or-deaden-summary kill-buffer))
2816       (if (not quit-config)
2817           (progn
2818             (set-buffer gnus-group-buffer)
2819             (gnus-group-jump-to-group group)
2820             (gnus-group-next-unread-group 1)
2821             (gnus-configure-windows 'group 'force))
2822         (gnus-handle-ephemeral-exit quit-config))
2823       ;; Finally signal the quit.
2824       (signal 'quit nil))
2825      ;; The group was successfully selected.
2826      (t
2827       (gnus-set-global-variables)
2828       ;; Save the active value in effect when the group was entered.
2829       (setq gnus-newsgroup-active
2830             (gnus-copy-sequence
2831              (gnus-active gnus-newsgroup-name)))
2832       ;; You can change the summary buffer in some way with this hook.
2833       (gnus-run-hooks 'gnus-select-group-hook)
2834       ;; Set any local variables in the group parameters.
2835       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2836       (gnus-update-format-specifications
2837        nil 'summary 'summary-mode 'summary-dummy)
2838       (gnus-update-summary-mark-positions)
2839       ;; Do score processing.
2840       (when gnus-use-scoring
2841         (gnus-possibly-score-headers))
2842       ;; Check whether to fill in the gaps in the threads.
2843       (when gnus-build-sparse-threads
2844         (gnus-build-sparse-threads))
2845       ;; Find the initial limit.
2846       (if gnus-show-threads
2847           (if show-all
2848               (let ((gnus-newsgroup-dormant nil))
2849                 (gnus-summary-initial-limit show-all))
2850             (gnus-summary-initial-limit show-all))
2851         ;; When untreaded, all articles are always shown.
2852         (setq gnus-newsgroup-limit
2853               (mapcar
2854                (lambda (header) (mail-header-number header))
2855                gnus-newsgroup-headers)))
2856       ;; Generate the summary buffer.
2857       (unless no-display
2858         (gnus-summary-prepare))
2859       (when gnus-use-trees
2860         (gnus-tree-open group)
2861         (setq gnus-summary-highlight-line-function
2862               'gnus-tree-highlight-article))
2863       ;; If the summary buffer is empty, but there are some low-scored
2864       ;; articles or some excluded dormants, we include these in the
2865       ;; buffer.
2866       (when (and (zerop (buffer-size))
2867                  (not no-display))
2868         (cond (gnus-newsgroup-dormant
2869                (gnus-summary-limit-include-dormant))
2870               ((and gnus-newsgroup-scored show-all)
2871                (gnus-summary-limit-include-expunged t))))
2872       ;; Function `gnus-apply-kill-file' must be called in this hook.
2873       (gnus-run-hooks 'gnus-apply-kill-hook)
2874       (if (and (zerop (buffer-size))
2875                (not no-display))
2876           (progn
2877             ;; This newsgroup is empty.
2878             (gnus-summary-catchup-and-exit nil t)
2879             (gnus-message 6 "No unread news")
2880             (when kill-buffer
2881               (gnus-kill-or-deaden-summary kill-buffer))
2882             ;; Return nil from this function.
2883             nil)
2884         ;; Hide conversation thread subtrees.  We cannot do this in
2885         ;; gnus-summary-prepare-hook since kill processing may not
2886         ;; work with hidden articles.
2887         (and gnus-show-threads
2888              gnus-thread-hide-subtree
2889              (gnus-summary-hide-all-threads))
2890         (when kill-buffer
2891           (gnus-kill-or-deaden-summary kill-buffer))
2892         ;; Show first unread article if requested.
2893         (if (and (not no-article)
2894                  (not no-display)
2895                  gnus-newsgroup-unreads
2896                  gnus-auto-select-first)
2897             (progn
2898               (gnus-configure-windows 'summary)
2899               (cond
2900                ((eq gnus-auto-select-first 'best)
2901                 (gnus-summary-best-unread-article))
2902                ((eq gnus-auto-select-first t)
2903                 (gnus-summary-first-unread-article))
2904                ((gnus-functionp gnus-auto-select-first)
2905                 (funcall gnus-auto-select-first))))
2906           ;; Don't select any articles, just move point to the first
2907           ;; article in the group.
2908           (goto-char (point-min))
2909           (gnus-summary-position-point)
2910           (gnus-configure-windows 'summary 'force)
2911           (gnus-set-mode-line 'summary))
2912         (when (get-buffer-window gnus-group-buffer t)
2913           ;; Gotta use windows, because recenter does weird stuff if
2914           ;; the current buffer ain't the displayed window.
2915           (let ((owin (selected-window)))
2916             (select-window (get-buffer-window gnus-group-buffer t))
2917             (when (gnus-group-goto-group group)
2918               (recenter))
2919             (select-window owin)))
2920         ;; Mark this buffer as "prepared".
2921         (setq gnus-newsgroup-prepared t)
2922         (gnus-run-hooks 'gnus-summary-prepared-hook)
2923         t)))))
2924
2925 (defun gnus-summary-prepare ()
2926   "Generate the summary buffer."
2927   (interactive)
2928   (let ((buffer-read-only nil))
2929     (erase-buffer)
2930     (setq gnus-newsgroup-data nil
2931           gnus-newsgroup-data-reverse nil)
2932     (gnus-run-hooks 'gnus-summary-generate-hook)
2933     ;; Generate the buffer, either with threads or without.
2934     (when gnus-newsgroup-headers
2935       (gnus-summary-prepare-threads
2936        (if gnus-show-threads
2937            (gnus-sort-gathered-threads
2938             (funcall gnus-summary-thread-gathering-function
2939                      (gnus-sort-threads
2940                       (gnus-cut-threads (gnus-make-threads)))))
2941          ;; Unthreaded display.
2942          (gnus-sort-articles gnus-newsgroup-headers))))
2943     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2944     ;; Call hooks for modifying summary buffer.
2945     (goto-char (point-min))
2946     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2947
2948 (defsubst gnus-general-simplify-subject (subject)
2949   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2950   (setq subject
2951         (cond
2952          ;; Truncate the subject.
2953          (gnus-simplify-subject-functions
2954           (gnus-map-function gnus-simplify-subject-functions subject))
2955          ((numberp gnus-summary-gather-subject-limit)
2956           (setq subject (gnus-simplify-subject-re subject))
2957           (if (> (length subject) gnus-summary-gather-subject-limit)
2958               (substring subject 0 gnus-summary-gather-subject-limit)
2959             subject))
2960          ;; Fuzzily simplify it.
2961          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2962           (gnus-simplify-subject-fuzzy subject))
2963          ;; Just remove the leading "Re:".
2964          (t
2965           (gnus-simplify-subject-re subject))))
2966
2967   (if (and gnus-summary-gather-exclude-subject
2968            (string-match gnus-summary-gather-exclude-subject subject))
2969       nil                               ; This article shouldn't be gathered
2970     subject))
2971
2972 (defun gnus-summary-simplify-subject-query ()
2973   "Query where the respool algorithm would put this article."
2974   (interactive)
2975   (gnus-summary-select-article)
2976   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2977
2978 (defun gnus-gather-threads-by-subject (threads)
2979   "Gather threads by looking at Subject headers."
2980   (if (not gnus-summary-make-false-root)
2981       threads
2982     (let ((hashtb (gnus-make-hashtable 1024))
2983           (prev threads)
2984           (result threads)
2985           subject hthread whole-subject)
2986       (while threads
2987         (setq subject (gnus-general-simplify-subject
2988                        (setq whole-subject (mail-header-subject
2989                                             (caar threads)))))
2990         (when subject
2991           (if (setq hthread (gnus-gethash subject hashtb))
2992               (progn
2993                 ;; We enter a dummy root into the thread, if we
2994                 ;; haven't done that already.
2995                 (unless (stringp (caar hthread))
2996                   (setcar hthread (list whole-subject (car hthread))))
2997                 ;; We add this new gathered thread to this gathered
2998                 ;; thread.
2999                 (setcdr (car hthread)
3000                         (nconc (cdar hthread) (list (car threads))))
3001                 ;; Remove it from the list of threads.
3002                 (setcdr prev (cdr threads))
3003                 (setq threads prev))
3004             ;; Enter this thread into the hash table.
3005             (gnus-sethash subject threads hashtb)))
3006         (setq prev threads)
3007         (setq threads (cdr threads)))
3008       result)))
3009
3010 (defun gnus-gather-threads-by-references (threads)
3011   "Gather threads by looking at References headers."
3012   (let ((idhashtb (gnus-make-hashtable 1024))
3013         (thhashtb (gnus-make-hashtable 1024))
3014         (prev threads)
3015         (result threads)
3016         ids references id gthread gid entered ref)
3017     (while threads
3018       (when (setq references (mail-header-references (caar threads)))
3019         (setq id (mail-header-id (caar threads))
3020               ids (gnus-split-references references)
3021               entered nil)
3022         (while (setq ref (pop ids))
3023           (setq ids (delete ref ids))
3024           (if (not (setq gid (gnus-gethash ref idhashtb)))
3025               (progn
3026                 (gnus-sethash ref id idhashtb)
3027                 (gnus-sethash id threads thhashtb))
3028             (setq gthread (gnus-gethash gid thhashtb))
3029             (unless entered
3030               ;; We enter a dummy root into the thread, if we
3031               ;; haven't done that already.
3032               (unless (stringp (caar gthread))
3033                 (setcar gthread (list (mail-header-subject (caar gthread))
3034                                       (car gthread))))
3035               ;; We add this new gathered thread to this gathered
3036               ;; thread.
3037               (setcdr (car gthread)
3038                       (nconc (cdar gthread) (list (car threads)))))
3039             ;; Add it into the thread hash table.
3040             (gnus-sethash id gthread thhashtb)
3041             (setq entered t)
3042             ;; Remove it from the list of threads.
3043             (setcdr prev (cdr threads))
3044             (setq threads prev))))
3045       (setq prev threads)
3046       (setq threads (cdr threads)))
3047     result))
3048
3049 (defun gnus-sort-gathered-threads (threads)
3050   "Sort subtreads inside each gathered thread by article number."
3051   (let ((result threads))
3052     (while threads
3053       (when (stringp (caar threads))
3054         (setcdr (car threads)
3055                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3056       (setq threads (cdr threads)))
3057     result))
3058
3059 (defun gnus-thread-loop-p (root thread)
3060   "Say whether ROOT is in THREAD."
3061   (let ((stack (list thread))
3062         (infloop 0)
3063         th)
3064     (while (setq thread (pop stack))
3065       (setq th (cdr thread))
3066       (while (and th
3067                   (not (eq (caar th) root)))
3068         (pop th))
3069       (if th
3070           ;; We have found a loop.
3071           (let (ref-dep)
3072             (setcdr thread (delq (car th) (cdr thread)))
3073             (if (boundp (setq ref-dep (intern "none"
3074                                               gnus-newsgroup-dependencies)))
3075                 (setcdr (symbol-value ref-dep)
3076                         (nconc (cdr (symbol-value ref-dep))
3077                                (list (car th))))
3078               (set ref-dep (list nil (car th))))
3079             (setq infloop 1
3080                   stack nil))
3081         ;; Push all the subthreads onto the stack.
3082         (push (cdr thread) stack)))
3083     infloop))
3084
3085 (defun gnus-make-threads ()
3086   "Go through the dependency hashtb and find the roots.  Return all threads."
3087   (let (threads)
3088     (while (catch 'infloop
3089              (mapatoms
3090               (lambda (refs)
3091                 ;; Deal with self-referencing References loops.
3092                 (when (and (car (symbol-value refs))
3093                            (not (zerop
3094                                  (apply
3095                                   '+
3096                                   (mapcar
3097                                    (lambda (thread)
3098                                      (gnus-thread-loop-p
3099                                       (car (symbol-value refs)) thread))
3100                                    (cdr (symbol-value refs)))))))
3101                   (setq threads nil)
3102                   (throw 'infloop t))
3103                 (unless (car (symbol-value refs))
3104                   ;; These threads do not refer back to any other articles,
3105                   ;; so they're roots.
3106                   (setq threads (append (cdr (symbol-value refs)) threads))))
3107               gnus-newsgroup-dependencies)))
3108     threads))
3109
3110 ;; Build the thread tree.
3111 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3112   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3113
3114 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3115 if it was already present.
3116
3117 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3118 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3119 Message-IDs will be renamed be renamed to a unique Message-ID before
3120 being entered.
3121
3122 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3123   (let* ((id (mail-header-id header))
3124          (id-dep (and id (intern id dependencies)))
3125          ref ref-dep ref-header)
3126     ;; Enter this `header' in the `dependencies' table.
3127     (cond
3128      ((not id-dep)
3129       (setq header nil))
3130      ;; The first two cases do the normal part: enter a new `header'
3131      ;; in the `dependencies' table.
3132      ((not (boundp id-dep))
3133       (set id-dep (list header)))
3134      ((null (car (symbol-value id-dep)))
3135       (setcar (symbol-value id-dep) header))
3136
3137      ;; From here the `header' was already present in the
3138      ;; `dependencies' table.
3139      (force-new
3140       ;; Overrides an existing entry;
3141       ;; just set the header part of the entry.
3142       (setcar (symbol-value id-dep) header))
3143
3144      ;; Renames the existing `header' to a unique Message-ID.
3145      ((not gnus-summary-ignore-duplicates)
3146       ;; An article with this Message-ID has already been seen.
3147       ;; We rename the Message-ID.
3148       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3149            (list header))
3150       (mail-header-set-id header id))
3151
3152      ;; The last case ignores an existing entry, except it adds any
3153      ;; additional Xrefs (in case the two articles came from different
3154      ;; servers.
3155      ;; Also sets `header' to `nil' meaning that the `dependencies'
3156      ;; table was *not* modified.
3157      (t
3158       (mail-header-set-xref
3159        (car (symbol-value id-dep))
3160        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3161                    "")
3162                (or (mail-header-xref header) "")))
3163       (setq header nil)))
3164
3165     (when header
3166       ;; First check if that we are not creating a References loop.
3167       (setq ref (gnus-parent-id (mail-header-references header)))
3168       (while (and ref
3169                   (setq ref-dep (intern-soft ref dependencies))
3170                   (boundp ref-dep)
3171                   (setq ref-header (car (symbol-value ref-dep))))
3172         (if (string= id ref)
3173             ;; Yuk!  This is a reference loop.  Make the article be a
3174             ;; root article.
3175             (progn
3176               (mail-header-set-references (car (symbol-value id-dep)) "none")
3177               (setq ref nil))
3178           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3179       (setq ref (gnus-parent-id (mail-header-references header)))
3180       (setq ref-dep (intern (or ref "none") dependencies))
3181       (if (boundp ref-dep)
3182           (setcdr (symbol-value ref-dep)
3183                   (nconc (cdr (symbol-value ref-dep))
3184                          (list (symbol-value id-dep))))
3185         (set ref-dep (list nil (symbol-value id-dep)))))
3186     header))
3187
3188 (defun gnus-build-sparse-threads ()
3189   (let ((headers gnus-newsgroup-headers)
3190         (mail-parse-charset gnus-newsgroup-charset)
3191         (gnus-summary-ignore-duplicates t)
3192         header references generation relations
3193         subject child end new-child date)
3194     ;; First we create an alist of generations/relations, where
3195     ;; generations is how much we trust the relation, and the relation
3196     ;; is parent/child.
3197     (gnus-message 7 "Making sparse threads...")
3198     (save-excursion
3199       (nnheader-set-temp-buffer " *gnus sparse threads*")
3200       (while (setq header (pop headers))
3201         (when (and (setq references (mail-header-references header))
3202                    (not (string= references "")))
3203           (insert references)
3204           (setq child (mail-header-id header)
3205                 subject (mail-header-subject header)
3206                 date (mail-header-date header)
3207                 generation 0)
3208           (while (search-backward ">" nil t)
3209             (setq end (1+ (point)))
3210             (when (search-backward "<" nil t)
3211               (setq new-child (buffer-substring (point) end))
3212               (push (list (incf generation)
3213                           child (setq child new-child)
3214                           subject date)
3215                     relations)))
3216           (when child
3217             (push (list (1+ generation) child nil subject) relations))
3218           (erase-buffer)))
3219       (kill-buffer (current-buffer)))
3220     ;; Sort over trustworthiness.
3221     (mapcar
3222      (lambda (relation)
3223        (when (gnus-dependencies-add-header
3224               (make-full-mail-header
3225                gnus-reffed-article-number
3226                (nth 3 relation) "" (or (nth 4 relation) "")
3227                (nth 1 relation)
3228                (or (nth 2 relation) "") 0 0 "")
3229               gnus-newsgroup-dependencies nil)
3230          (push gnus-reffed-article-number gnus-newsgroup-limit)
3231          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3232          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3233                gnus-newsgroup-reads)
3234          (decf gnus-reffed-article-number)))
3235      (sort relations 'car-less-than-car))
3236     (gnus-message 7 "Making sparse threads...done")))
3237
3238 (defun gnus-build-old-threads ()
3239   ;; Look at all the articles that refer back to old articles, and
3240   ;; fetch the headers for the articles that aren't there.  This will
3241   ;; build complete threads - if the roots haven't been expired by the
3242   ;; server, that is.
3243   (let ((mail-parse-charset gnus-newsgroup-charset)
3244         id heads)
3245     (mapatoms
3246      (lambda (refs)
3247        (when (not (car (symbol-value refs)))
3248          (setq heads (cdr (symbol-value refs)))
3249          (while heads
3250            (if (memq (mail-header-number (caar heads))
3251                      gnus-newsgroup-dormant)
3252                (setq heads (cdr heads))
3253              (setq id (symbol-name refs))
3254              (while (and (setq id (gnus-build-get-header id))
3255                          (not (car (gnus-id-to-thread id)))))
3256              (setq heads nil)))))
3257      gnus-newsgroup-dependencies)))
3258
3259 ;; This function has to be called with point after the article number
3260 ;; on the beginning of the line.
3261 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3262   (let ((eol (gnus-point-at-eol))
3263         (buffer (current-buffer))
3264         header)
3265
3266     ;; overview: [num subject from date id refs chars lines misc]
3267     (unwind-protect
3268         (progn
3269           (narrow-to-region (point) eol)
3270           (unless (eobp)
3271             (forward-char))
3272
3273           (setq header
3274                 (make-full-mail-header
3275                  number                 ; number
3276                  (nnheader-nov-field)   ; subject
3277                  (nnheader-nov-field)   ; from
3278                  (nnheader-nov-field)   ; date
3279                  (nnheader-nov-read-message-id) ; id
3280                  (nnheader-nov-field)   ; refs
3281                  (nnheader-nov-read-integer) ; chars
3282                  (nnheader-nov-read-integer) ; lines
3283                  (unless (eobp)
3284                    (nnheader-nov-field)) ; misc
3285                  (nnheader-nov-parse-extra)))) ; extra
3286
3287       (widen))
3288
3289     (when gnus-alter-header-function
3290       (funcall gnus-alter-header-function header))
3291     (gnus-dependencies-add-header header dependencies force-new)))
3292
3293 (defun gnus-build-get-header (id)
3294   ;; Look through the buffer of NOV lines and find the header to
3295   ;; ID.  Enter this line into the dependencies hash table, and return
3296   ;; the id of the parent article (if any).
3297   (let ((deps gnus-newsgroup-dependencies)
3298         found header)
3299     (prog1
3300         (save-excursion
3301           (set-buffer nntp-server-buffer)
3302           (let ((case-fold-search nil))
3303             (goto-char (point-min))
3304             (while (and (not found)
3305                         (search-forward id nil t))
3306               (beginning-of-line)
3307               (setq found (looking-at
3308                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3309                                    (regexp-quote id))))
3310               (or found (beginning-of-line 2)))
3311             (when found
3312               (beginning-of-line)
3313               (and
3314                (setq header (gnus-nov-parse-line
3315                              (read (current-buffer)) deps))
3316                (gnus-parent-id (mail-header-references header))))))
3317       (when header
3318         (let ((number (mail-header-number header)))
3319           (push number gnus-newsgroup-limit)
3320           (push header gnus-newsgroup-headers)
3321           (if (memq number gnus-newsgroup-unselected)
3322               (progn
3323                 (push number gnus-newsgroup-unreads)
3324                 (setq gnus-newsgroup-unselected
3325                       (delq number gnus-newsgroup-unselected)))
3326             (push number gnus-newsgroup-ancient)))))))
3327
3328 (defun gnus-build-all-threads ()
3329   "Read all the headers."
3330   (let ((gnus-summary-ignore-duplicates t)
3331         (mail-parse-charset gnus-newsgroup-charset)
3332         (dependencies gnus-newsgroup-dependencies)
3333         header article)
3334     (save-excursion
3335       (set-buffer nntp-server-buffer)
3336       (let ((case-fold-search nil))
3337         (goto-char (point-min))
3338         (while (not (eobp))
3339           (ignore-errors
3340             (setq article (read (current-buffer))
3341                   header (gnus-nov-parse-line article dependencies)))
3342           (when header
3343             (save-excursion
3344               (set-buffer gnus-summary-buffer)
3345               (push header gnus-newsgroup-headers)
3346               (if (memq (setq article (mail-header-number header))
3347                         gnus-newsgroup-unselected)
3348                   (progn
3349                     (push article gnus-newsgroup-unreads)
3350                     (setq gnus-newsgroup-unselected
3351                           (delq article gnus-newsgroup-unselected)))
3352                 (push article gnus-newsgroup-ancient)))
3353             (forward-line 1)))))))
3354
3355 (defun gnus-summary-update-article-line (article header)
3356   "Update the line for ARTICLE using HEADERS."
3357   (let* ((id (mail-header-id header))
3358          (thread (gnus-id-to-thread id)))
3359     (unless thread
3360       (error "Article in no thread"))
3361     ;; Update the thread.
3362     (setcar thread header)
3363     (gnus-summary-goto-subject article)
3364     (let* ((datal (gnus-data-find-list article))
3365            (data (car datal))
3366            (length (when (cdr datal)
3367                      (- (gnus-data-pos data)
3368                         (gnus-data-pos (cadr datal)))))
3369            (buffer-read-only nil)
3370            (level (gnus-summary-thread-level)))
3371       (gnus-delete-line)
3372       (gnus-summary-insert-line
3373        header level nil (gnus-article-mark article)
3374        (memq article gnus-newsgroup-replied)
3375        (memq article gnus-newsgroup-expirable)
3376        ;; Only insert the Subject string when it's different
3377        ;; from the previous Subject string.
3378        (if (gnus-subject-equal
3379             (condition-case ()
3380                 (mail-header-subject
3381                  (gnus-data-header
3382                   (cadr
3383                    (gnus-data-find-list
3384                     article
3385                     (gnus-data-list t)))))
3386               ;; Error on the side of excessive subjects.
3387               (error ""))
3388             (mail-header-subject header))
3389            ""
3390          (mail-header-subject header))
3391        nil (cdr (assq article gnus-newsgroup-scored))
3392        (memq article gnus-newsgroup-processable))
3393       (when length
3394         (gnus-data-update-list
3395          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3396
3397 (defun gnus-summary-update-article (article &optional iheader)
3398   "Update ARTICLE in the summary buffer."
3399   (set-buffer gnus-summary-buffer)
3400   (let* ((header (gnus-summary-article-header article))
3401          (id (mail-header-id header))
3402          (data (gnus-data-find article))
3403          (thread (gnus-id-to-thread id))
3404          (references (mail-header-references header))
3405          (parent
3406           (gnus-id-to-thread
3407            (or (gnus-parent-id
3408                 (when (and references
3409                            (not (equal "" references)))
3410                   references))
3411                "none")))
3412          (buffer-read-only nil)
3413          (old (car thread)))
3414     (when thread
3415       (unless iheader
3416         (setcar thread nil)
3417         (when parent
3418           (delq thread parent)))
3419       (if (gnus-summary-insert-subject id header)
3420           ;; Set the (possibly) new article number in the data structure.
3421           (gnus-data-set-number data (gnus-id-to-article id))
3422         (setcar thread old)
3423         nil))))
3424
3425 (defun gnus-rebuild-thread (id &optional line)
3426   "Rebuild the thread containing ID.
3427 If LINE, insert the rebuilt thread starting on line LINE."
3428   (let ((buffer-read-only nil)
3429         old-pos current thread data)
3430     (if (not gnus-show-threads)
3431         (setq thread (list (car (gnus-id-to-thread id))))
3432       ;; Get the thread this article is part of.
3433       (setq thread (gnus-remove-thread id)))
3434     (setq old-pos (gnus-point-at-bol))
3435     (setq current (save-excursion
3436                     (and (zerop (forward-line -1))
3437                          (gnus-summary-article-number))))
3438     ;; If this is a gathered thread, we have to go some re-gathering.
3439     (when (stringp (car thread))
3440       (let ((subject (car thread))
3441             roots thr)
3442         (setq thread (cdr thread))
3443         (while thread
3444           (unless (memq (setq thr (gnus-id-to-thread
3445                                    (gnus-root-id
3446                                     (mail-header-id (caar thread)))))
3447                         roots)
3448             (push thr roots))
3449           (setq thread (cdr thread)))
3450         ;; We now have all (unique) roots.
3451         (if (= (length roots) 1)
3452             ;; All the loose roots are now one solid root.
3453             (setq thread (car roots))
3454           (setq thread (cons subject (gnus-sort-threads roots))))))
3455     (let (threads)
3456       ;; We then insert this thread into the summary buffer.
3457       (when line
3458         (goto-char (point-min))
3459         (forward-line (1- line)))
3460       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3461         (if gnus-show-threads
3462             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3463           (gnus-summary-prepare-unthreaded thread))
3464         (setq data (nreverse gnus-newsgroup-data))
3465         (setq threads gnus-newsgroup-threads))
3466       ;; We splice the new data into the data structure.
3467       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3468       ;;!!! then we want to insert at the beginning of the buffer.
3469       ;;!!! That happens to be true with Gnus now, but that may
3470       ;;!!! change in the future.  Perhaps.
3471       (gnus-data-enter-list
3472        (if line nil current) data (- (point) old-pos))
3473       (setq gnus-newsgroup-threads
3474             (nconc threads gnus-newsgroup-threads))
3475       (gnus-data-compute-positions))))
3476
3477 (defun gnus-number-to-header (number)
3478   "Return the header for article NUMBER."
3479   (let ((headers gnus-newsgroup-headers))
3480     (while (and headers
3481                 (not (= number (mail-header-number (car headers)))))
3482       (pop headers))
3483     (when headers
3484       (car headers))))
3485
3486 (defun gnus-parent-headers (in-headers &optional generation)
3487   "Return the headers of the GENERATIONeth parent of HEADERS."
3488   (unless generation
3489     (setq generation 1))
3490   (let ((parent t)
3491         (headers in-headers)
3492         references)
3493     (while (and parent
3494                 (not (zerop generation))
3495                 (setq references (mail-header-references headers)))
3496       (setq headers (if (and references
3497                              (setq parent (gnus-parent-id references)))
3498                         (car (gnus-id-to-thread parent))
3499                       nil))
3500       (decf generation))
3501     (and (not (eq headers in-headers))
3502          headers)))
3503
3504 (defun gnus-id-to-thread (id)
3505   "Return the (sub-)thread where ID appears."
3506   (gnus-gethash id gnus-newsgroup-dependencies))
3507
3508 (defun gnus-id-to-article (id)
3509   "Return the article number of ID."
3510   (let ((thread (gnus-id-to-thread id)))
3511     (when (and thread
3512                (car thread))
3513       (mail-header-number (car thread)))))
3514
3515 (defun gnus-id-to-header (id)
3516   "Return the article headers of ID."
3517   (car (gnus-id-to-thread id)))
3518
3519 (defun gnus-article-displayed-root-p (article)
3520   "Say whether ARTICLE is a root(ish) article."
3521   (let ((level (gnus-summary-thread-level article))
3522         (refs (mail-header-references  (gnus-summary-article-header article)))
3523         particle)
3524     (cond
3525      ((null level) nil)
3526      ((zerop level) t)
3527      ((null refs) t)
3528      ((null (gnus-parent-id refs)) t)
3529      ((and (= 1 level)
3530            (null (setq particle (gnus-id-to-article
3531                                  (gnus-parent-id refs))))
3532            (null (gnus-summary-thread-level particle)))))))
3533
3534 (defun gnus-root-id (id)
3535   "Return the id of the root of the thread where ID appears."
3536   (let (last-id prev)
3537     (while (and id (setq prev (car (gnus-id-to-thread id))))
3538       (setq last-id id
3539             id (gnus-parent-id (mail-header-references prev))))
3540     last-id))
3541
3542 (defun gnus-articles-in-thread (thread)
3543   "Return the list of articles in THREAD."
3544   (cons (mail-header-number (car thread))
3545         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3546
3547 (defun gnus-remove-thread (id &optional dont-remove)
3548   "Remove the thread that has ID in it."
3549   (let (headers thread last-id)
3550     ;; First go up in this thread until we find the root.
3551     (setq last-id (gnus-root-id id)
3552           headers (message-flatten-list (gnus-id-to-thread last-id)))
3553     ;; We have now found the real root of this thread.  It might have
3554     ;; been gathered into some loose thread, so we have to search
3555     ;; through the threads to find the thread we wanted.
3556     (let ((threads gnus-newsgroup-threads)
3557           sub)
3558       (while threads
3559         (setq sub (car threads))
3560         (if (stringp (car sub))
3561             ;; This is a gathered thread, so we look at the roots
3562             ;; below it to find whether this article is in this
3563             ;; gathered root.
3564             (progn
3565               (setq sub (cdr sub))
3566               (while sub
3567                 (when (member (caar sub) headers)
3568                   (setq thread (car threads)
3569                         threads nil
3570                         sub nil))
3571                 (setq sub (cdr sub))))
3572           ;; It's an ordinary thread, so we check it.
3573           (when (eq (car sub) (car headers))
3574             (setq thread sub
3575                   threads nil)))
3576         (setq threads (cdr threads)))
3577       ;; If this article is in no thread, then it's a root.
3578       (if thread
3579           (unless dont-remove
3580             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3581         (setq thread (gnus-id-to-thread last-id)))
3582       (when thread
3583         (prog1
3584             thread                      ; We return this thread.
3585           (unless dont-remove
3586             (if (stringp (car thread))
3587                 (progn
3588                   ;; If we use dummy roots, then we have to remove the
3589                   ;; dummy root as well.
3590                   (when (eq gnus-summary-make-false-root 'dummy)
3591                     ;; We go to the dummy root by going to
3592                     ;; the first sub-"thread", and then one line up.
3593                     (gnus-summary-goto-article
3594                      (mail-header-number (caadr thread)))
3595                     (forward-line -1)
3596                     (gnus-delete-line)
3597                     (gnus-data-compute-positions))
3598                   (setq thread (cdr thread))
3599                   (while thread
3600                     (gnus-remove-thread-1 (car thread))
3601                     (setq thread (cdr thread))))
3602               (gnus-summary-show-all-threads)
3603               (gnus-remove-thread-1 thread))))))))
3604
3605 (defun gnus-remove-thread-1 (thread)
3606   "Remove the thread THREAD recursively."
3607   (let ((number (mail-header-number (pop thread)))
3608         d)
3609     (setq thread (reverse thread))
3610     (while thread
3611       (gnus-remove-thread-1 (pop thread)))
3612     (when (setq d (gnus-data-find number))
3613       (goto-char (gnus-data-pos d))
3614       (gnus-data-remove
3615        number
3616        (- (gnus-point-at-bol)
3617           (prog1
3618               (1+ (gnus-point-at-eol))
3619             (gnus-delete-line)))))))
3620
3621 (defun gnus-sort-threads (threads)
3622   "Sort THREADS."
3623   (if (not gnus-thread-sort-functions)
3624       threads
3625     (gnus-message 8 "Sorting threads...")
3626     (prog1
3627         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3628       (gnus-message 8 "Sorting threads...done"))))
3629
3630 (defun gnus-sort-articles (articles)
3631   "Sort ARTICLES."
3632   (when gnus-article-sort-functions
3633     (gnus-message 7 "Sorting articles...")
3634     (prog1
3635         (setq gnus-newsgroup-headers
3636               (sort articles (gnus-make-sort-function
3637                               gnus-article-sort-functions)))
3638       (gnus-message 7 "Sorting articles...done"))))
3639
3640 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3641 (defmacro gnus-thread-header (thread)
3642   ;; Return header of first article in THREAD.
3643   ;; Note that THREAD must never, ever be anything else than a variable -
3644   ;; using some other form will lead to serious barfage.
3645   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3646   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3647   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3648         (vector thread) 2))
3649
3650 (defsubst gnus-article-sort-by-number (h1 h2)
3651   "Sort articles by article number."
3652   (< (mail-header-number h1)
3653      (mail-header-number h2)))
3654
3655 (defun gnus-thread-sort-by-number (h1 h2)
3656   "Sort threads by root article number."
3657   (gnus-article-sort-by-number
3658    (gnus-thread-header h1) (gnus-thread-header h2)))
3659
3660 (defsubst gnus-article-sort-by-lines (h1 h2)
3661   "Sort articles by article Lines header."
3662   (< (mail-header-lines h1)
3663      (mail-header-lines h2)))
3664
3665 (defun gnus-thread-sort-by-lines (h1 h2)
3666   "Sort threads by root article Lines header."
3667   (gnus-article-sort-by-lines
3668    (gnus-thread-header h1) (gnus-thread-header h2)))
3669
3670 (defsubst gnus-article-sort-by-chars (h1 h2)
3671   "Sort articles by octet length."
3672   (< (mail-header-chars h1)
3673      (mail-header-chars h2)))
3674
3675 (defun gnus-thread-sort-by-chars (h1 h2)
3676   "Sort threads by root article octet length."
3677   (gnus-article-sort-by-chars
3678    (gnus-thread-header h1) (gnus-thread-header h2)))
3679
3680 (defsubst gnus-article-sort-by-author (h1 h2)
3681   "Sort articles by root author."
3682   (string-lessp
3683    (let ((extract (funcall
3684                    gnus-extract-address-components
3685                    (mail-header-from h1))))
3686      (or (car extract) (cadr extract) ""))
3687    (let ((extract (funcall
3688                    gnus-extract-address-components
3689                    (mail-header-from h2))))
3690      (or (car extract) (cadr extract) ""))))
3691
3692 (defun gnus-thread-sort-by-author (h1 h2)
3693   "Sort threads by root author."
3694   (gnus-article-sort-by-author
3695    (gnus-thread-header h1)  (gnus-thread-header h2)))
3696
3697 (defsubst gnus-article-sort-by-subject (h1 h2)
3698   "Sort articles by root subject."
3699   (string-lessp
3700    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3701    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3702
3703 (defun gnus-thread-sort-by-subject (h1 h2)
3704   "Sort threads by root subject."
3705   (gnus-article-sort-by-subject
3706    (gnus-thread-header h1) (gnus-thread-header h2)))
3707
3708 (defsubst gnus-article-sort-by-date (h1 h2)
3709   "Sort articles by root article date."
3710   (time-less-p
3711    (gnus-date-get-time (mail-header-date h1))
3712    (gnus-date-get-time (mail-header-date h2))))
3713
3714 (defun gnus-thread-sort-by-date (h1 h2)
3715   "Sort threads by root article date."
3716   (gnus-article-sort-by-date
3717    (gnus-thread-header h1) (gnus-thread-header h2)))
3718
3719 (defsubst gnus-article-sort-by-score (h1 h2)
3720   "Sort articles by root article score.
3721 Unscored articles will be counted as having a score of zero."
3722   (> (or (cdr (assq (mail-header-number h1)
3723                     gnus-newsgroup-scored))
3724          gnus-summary-default-score 0)
3725      (or (cdr (assq (mail-header-number h2)
3726                     gnus-newsgroup-scored))
3727          gnus-summary-default-score 0)))
3728
3729 (defun gnus-thread-sort-by-score (h1 h2)
3730   "Sort threads by root article score."
3731   (gnus-article-sort-by-score
3732    (gnus-thread-header h1) (gnus-thread-header h2)))
3733
3734 (defun gnus-thread-sort-by-total-score (h1 h2)
3735   "Sort threads by the sum of all scores in the thread.
3736 Unscored articles will be counted as having a score of zero."
3737   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3738
3739 (defun gnus-thread-total-score (thread)
3740   ;; This function find the total score of THREAD.
3741   (cond ((null thread)
3742          0)
3743         ((consp thread)
3744          (if (stringp (car thread))
3745              (apply gnus-thread-score-function 0
3746                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3747            (gnus-thread-total-score-1 thread)))
3748         (t
3749          (gnus-thread-total-score-1 (list thread)))))
3750
3751 (defun gnus-thread-total-score-1 (root)
3752   ;; This function find the total score of the thread below ROOT.
3753   (setq root (car root))
3754   (apply gnus-thread-score-function
3755          (or (append
3756               (mapcar 'gnus-thread-total-score
3757                       (cdr (gnus-id-to-thread (mail-header-id root))))
3758               (when (> (mail-header-number root) 0)
3759                 (list (or (cdr (assq (mail-header-number root)
3760                                      gnus-newsgroup-scored))
3761                           gnus-summary-default-score 0))))
3762              (list gnus-summary-default-score)
3763              '(0))))
3764
3765 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3766 (defvar gnus-tmp-prev-subject nil)
3767 (defvar gnus-tmp-false-parent nil)
3768 (defvar gnus-tmp-root-expunged nil)
3769 (defvar gnus-tmp-dummy-line nil)
3770
3771 (defvar gnus-tmp-header)
3772 (defun gnus-extra-header (type &optional header)
3773   "Return the extra header of TYPE."
3774   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3775       ""))
3776
3777 (defun gnus-summary-prepare-threads (threads)
3778   "Prepare summary buffer from THREADS and indentation LEVEL.
3779 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3780 or a straight list of headers."
3781   (gnus-message 7 "Generating summary...")
3782
3783   (setq gnus-newsgroup-threads threads)
3784   (beginning-of-line)
3785
3786   (let ((gnus-tmp-level 0)
3787         (default-score (or gnus-summary-default-score 0))
3788         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3789         thread number subject stack state gnus-tmp-gathered beg-match
3790         new-roots gnus-tmp-new-adopts thread-end
3791         gnus-tmp-header gnus-tmp-unread
3792         gnus-tmp-replied gnus-tmp-subject-or-nil
3793         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3794         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3795         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3796
3797     (setq gnus-tmp-prev-subject nil)
3798
3799     (if (vectorp (car threads))
3800         ;; If this is a straight (sic) list of headers, then a
3801         ;; threaded summary display isn't required, so we just create
3802         ;; an unthreaded one.
3803         (gnus-summary-prepare-unthreaded threads)
3804
3805       ;; Do the threaded display.
3806
3807       (while (or threads stack gnus-tmp-new-adopts new-roots)
3808
3809         (if (and (= gnus-tmp-level 0)
3810                  (or (not stack)
3811                      (= (caar stack) 0))
3812                  (not gnus-tmp-false-parent)
3813                  (or gnus-tmp-new-adopts new-roots))
3814             (if gnus-tmp-new-adopts
3815                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3816                       thread (list (car gnus-tmp-new-adopts))
3817                       gnus-tmp-header (caar thread)
3818                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3819               (when new-roots
3820                 (setq thread (list (car new-roots))
3821                       gnus-tmp-header (caar thread)
3822                       new-roots (cdr new-roots))))
3823
3824           (if threads
3825               ;; If there are some threads, we do them before the
3826               ;; threads on the stack.
3827               (setq thread threads
3828                     gnus-tmp-header (caar thread))
3829             ;; There were no current threads, so we pop something off
3830             ;; the stack.
3831             (setq state (car stack)
3832                   gnus-tmp-level (car state)
3833                   thread (cdr state)
3834                   stack (cdr stack)
3835                   gnus-tmp-header (caar thread))))
3836
3837         (setq gnus-tmp-false-parent nil)
3838         (setq gnus-tmp-root-expunged nil)
3839         (setq thread-end nil)
3840
3841         (if (stringp gnus-tmp-header)
3842             ;; The header is a dummy root.
3843             (cond
3844              ((eq gnus-summary-make-false-root 'adopt)
3845               ;; We let the first article adopt the rest.
3846               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3847                                                (cddar thread)))
3848               (setq gnus-tmp-gathered
3849                     (nconc (mapcar
3850                             (lambda (h) (mail-header-number (car h)))
3851                             (cddar thread))
3852                            gnus-tmp-gathered))
3853               (setq thread (cons (list (caar thread)
3854                                        (cadar thread))
3855                                  (cdr thread)))
3856               (setq gnus-tmp-level -1
3857                     gnus-tmp-false-parent t))
3858              ((eq gnus-summary-make-false-root 'empty)
3859               ;; We print adopted articles with empty subject fields.
3860               (setq gnus-tmp-gathered
3861                     (nconc (mapcar
3862                             (lambda (h) (mail-header-number (car h)))
3863                             (cddar thread))
3864                            gnus-tmp-gathered))
3865               (setq gnus-tmp-level -1))
3866              ((eq gnus-summary-make-false-root 'dummy)
3867               ;; We remember that we probably want to output a dummy
3868               ;; root.
3869               (setq gnus-tmp-dummy-line gnus-tmp-header)
3870               (setq gnus-tmp-prev-subject gnus-tmp-header))
3871              (t
3872               ;; We do not make a root for the gathered
3873               ;; sub-threads at all.
3874               (setq gnus-tmp-level -1)))
3875
3876           (setq number (mail-header-number gnus-tmp-header)
3877                 subject (mail-header-subject gnus-tmp-header))
3878
3879           (cond
3880            ;; If the thread has changed subject, we might want to make
3881            ;; this subthread into a root.
3882            ((and (null gnus-thread-ignore-subject)
3883                  (not (zerop gnus-tmp-level))
3884                  gnus-tmp-prev-subject
3885                  (not (inline
3886                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3887             (setq new-roots (nconc new-roots (list (car thread)))
3888                   thread-end t
3889                   gnus-tmp-header nil))
3890            ;; If the article lies outside the current limit,
3891            ;; then we do not display it.
3892            ((not (memq number gnus-newsgroup-limit))
3893             (setq gnus-tmp-gathered
3894                   (nconc (mapcar
3895                           (lambda (h) (mail-header-number (car h)))
3896                           (cdar thread))
3897                          gnus-tmp-gathered))
3898             (setq gnus-tmp-new-adopts (if (cdar thread)
3899                                           (append gnus-tmp-new-adopts
3900                                                   (cdar thread))
3901                                         gnus-tmp-new-adopts)
3902                   thread-end t
3903                   gnus-tmp-header nil)
3904             (when (zerop gnus-tmp-level)
3905               (setq gnus-tmp-root-expunged t)))
3906            ;; Perhaps this article is to be marked as read?
3907            ((and gnus-summary-mark-below
3908                  (< (or (cdr (assq number gnus-newsgroup-scored))
3909                         default-score)
3910                     gnus-summary-mark-below)
3911                  ;; Don't touch sparse articles.
3912                  (not (gnus-summary-article-sparse-p number))
3913                  (not (gnus-summary-article-ancient-p number)))
3914             (setq gnus-newsgroup-unreads
3915                   (delq number gnus-newsgroup-unreads))
3916             (if gnus-newsgroup-auto-expire
3917                 (push number gnus-newsgroup-expirable)
3918               (push (cons number gnus-low-score-mark)
3919                     gnus-newsgroup-reads))))
3920
3921           (when gnus-tmp-header
3922             ;; We may have an old dummy line to output before this
3923             ;; article.
3924             (when (and gnus-tmp-dummy-line
3925                        (gnus-subject-equal
3926                         gnus-tmp-dummy-line
3927                         (mail-header-subject gnus-tmp-header)))
3928               (gnus-summary-insert-dummy-line
3929                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3930               (setq gnus-tmp-dummy-line nil))
3931
3932             ;; Compute the mark.
3933             (setq gnus-tmp-unread (gnus-article-mark number))
3934
3935             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3936                                   gnus-tmp-header gnus-tmp-level)
3937                   gnus-newsgroup-data)
3938
3939             ;; Actually insert the line.
3940             (setq
3941              gnus-tmp-subject-or-nil
3942              (cond
3943               ((and gnus-thread-ignore-subject
3944                     gnus-tmp-prev-subject
3945                     (not (inline (gnus-subject-equal
3946                                   gnus-tmp-prev-subject subject))))
3947                subject)
3948               ((zerop gnus-tmp-level)
3949                (if (and (eq gnus-summary-make-false-root 'empty)
3950                         (memq number gnus-tmp-gathered)
3951                         gnus-tmp-prev-subject
3952                         (inline (gnus-subject-equal
3953                                  gnus-tmp-prev-subject subject)))
3954                    gnus-summary-same-subject
3955                  subject))
3956               (t gnus-summary-same-subject)))
3957             (if (and (eq gnus-summary-make-false-root 'adopt)
3958                      (= gnus-tmp-level 1)
3959                      (memq number gnus-tmp-gathered))
3960                 (setq gnus-tmp-opening-bracket ?\<
3961                       gnus-tmp-closing-bracket ?\>)
3962               (setq gnus-tmp-opening-bracket ?\[
3963                     gnus-tmp-closing-bracket ?\]))
3964             (setq
3965              gnus-tmp-indentation
3966              (aref gnus-thread-indent-array gnus-tmp-level)
3967              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3968              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3969                                 gnus-summary-default-score 0)
3970              gnus-tmp-score-char
3971              (if (or (null gnus-summary-default-score)
3972                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3973                          gnus-summary-zcore-fuzz))
3974                  ?  ;Whitespace
3975                (if (< gnus-tmp-score gnus-summary-default-score)
3976                    gnus-score-below-mark gnus-score-over-mark))
3977              gnus-tmp-replied
3978              (cond ((memq number gnus-newsgroup-processable)
3979                     gnus-process-mark)
3980                    ((memq number gnus-newsgroup-cached)
3981                     gnus-cached-mark)
3982                    ((memq number gnus-newsgroup-replied)
3983                     gnus-replied-mark)
3984                    ((memq number gnus-newsgroup-saved)
3985                     gnus-saved-mark)
3986                    (t gnus-unread-mark))
3987              gnus-tmp-from (mail-header-from gnus-tmp-header)
3988              gnus-tmp-name
3989              (cond
3990               ((string-match "<[^>]+> *$" gnus-tmp-from)
3991                (setq beg-match (match-beginning 0))
3992                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3993                         (substring gnus-tmp-from (1+ (match-beginning 0))
3994                                    (1- (match-end 0))))
3995                    (substring gnus-tmp-from 0 beg-match)))
3996               ((string-match "(.+)" gnus-tmp-from)
3997                (substring gnus-tmp-from
3998                           (1+ (match-beginning 0)) (1- (match-end 0))))
3999               (t gnus-tmp-from)))
4000             (when (string= gnus-tmp-name "")
4001               (setq gnus-tmp-name gnus-tmp-from))
4002             (unless (numberp gnus-tmp-lines)
4003               (setq gnus-tmp-lines 0))
4004             (gnus-put-text-property
4005              (point)
4006              (progn (eval gnus-summary-line-format-spec) (point))
4007              'gnus-number number)
4008             (when gnus-visual-p
4009               (forward-line -1)
4010               (gnus-run-hooks 'gnus-summary-update-hook)
4011               (forward-line 1))
4012
4013             (setq gnus-tmp-prev-subject subject)))
4014
4015         (when (nth 1 thread)
4016           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4017         (incf gnus-tmp-level)
4018         (setq threads (if thread-end nil (cdar thread)))
4019         (unless threads
4020           (setq gnus-tmp-level 0)))))
4021   (gnus-message 7 "Generating summary...done"))
4022
4023 (defun gnus-summary-prepare-unthreaded (headers)
4024   "Generate an unthreaded summary buffer based on HEADERS."
4025   (let (header number mark)
4026
4027     (beginning-of-line)
4028
4029     (while headers
4030       ;; We may have to root out some bad articles...
4031       (when (memq (setq number (mail-header-number
4032                                 (setq header (pop headers))))
4033                   gnus-newsgroup-limit)
4034         ;; Mark article as read when it has a low score.
4035         (when (and gnus-summary-mark-below
4036                    (< (or (cdr (assq number gnus-newsgroup-scored))
4037                           gnus-summary-default-score 0)
4038                       gnus-summary-mark-below)
4039                    (not (gnus-summary-article-ancient-p number)))
4040           (setq gnus-newsgroup-unreads
4041                 (delq number gnus-newsgroup-unreads))
4042           (if gnus-newsgroup-auto-expire
4043               (push number gnus-newsgroup-expirable)
4044             (push (cons number gnus-low-score-mark)
4045                   gnus-newsgroup-reads)))
4046
4047         (setq mark (gnus-article-mark number))
4048         (push (gnus-data-make number mark (1+ (point)) header 0)
4049               gnus-newsgroup-data)
4050         (gnus-summary-insert-line
4051          header 0 number
4052          mark (memq number gnus-newsgroup-replied)
4053          (memq number gnus-newsgroup-expirable)
4054          (mail-header-subject header) nil
4055          (cdr (assq number gnus-newsgroup-scored))
4056          (memq number gnus-newsgroup-processable))))))
4057
4058 (defun gnus-summary-remove-list-identifiers ()
4059   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4060   (let ((regexp (if (stringp gnus-list-identifiers)
4061                     gnus-list-identifiers
4062                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4063     (dolist (header gnus-newsgroup-headers)
4064       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4065                           (mail-header-subject header))
4066         (mail-header-set-subject
4067          header (concat (substring (mail-header-subject header)
4068                                    0 (match-beginning 2))
4069                         (substring (mail-header-subject header)
4070                                    (match-end 2))))))))
4071
4072 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4073   "Select newsgroup GROUP.
4074 If READ-ALL is non-nil, all articles in the group are selected.
4075 If SELECT-ARTICLES, only select those articles from GROUP."
4076   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4077          ;;!!! Dirty hack; should be removed.
4078          (gnus-summary-ignore-duplicates
4079           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4080               t
4081             gnus-summary-ignore-duplicates))
4082          (info (nth 2 entry))
4083          articles fetched-articles cached)
4084
4085     (unless (gnus-check-server
4086              (setq gnus-current-select-method
4087                    (gnus-find-method-for-group group)))
4088       (error "Couldn't open server"))
4089
4090     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4091         (gnus-activate-group group)     ; Or we can activate it...
4092         (progn                          ; Or we bug out.
4093           (when (equal major-mode 'gnus-summary-mode)
4094             (kill-buffer (current-buffer)))
4095           (error "Couldn't request group %s: %s"
4096                  group (gnus-status-message group))))
4097
4098     (unless (gnus-request-group group t)
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     (setq gnus-newsgroup-name group)
4105     (setq gnus-newsgroup-unselected nil)
4106     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4107     (gnus-summary-setup-default-charset)
4108
4109     ;; Adjust and set lists of article marks.
4110     (when info
4111       (gnus-adjust-marked-articles info))
4112
4113     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4114     (when (gnus-virtual-group-p group)
4115       (setq cached gnus-newsgroup-cached))
4116
4117     (setq gnus-newsgroup-unreads
4118           (gnus-set-difference
4119            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4120            gnus-newsgroup-dormant))
4121
4122     (setq gnus-newsgroup-processable nil)
4123
4124     (gnus-update-read-articles group gnus-newsgroup-unreads)
4125
4126     (if (setq articles select-articles)
4127         (setq gnus-newsgroup-unselected
4128               (gnus-sorted-intersection
4129                gnus-newsgroup-unreads
4130                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4131       (setq articles (gnus-articles-to-read group read-all)))
4132
4133     (cond
4134      ((null articles)
4135       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4136       'quit)
4137      ((eq articles 0) nil)
4138      (t
4139       ;; Init the dependencies hash table.
4140       (setq gnus-newsgroup-dependencies
4141             (gnus-make-hashtable (length articles)))
4142       (gnus-set-global-variables)
4143       ;; Retrieve the headers and read them in.
4144       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4145       (setq gnus-newsgroup-headers
4146             (if (eq 'nov
4147                     (setq gnus-headers-retrieved-by
4148                           (gnus-retrieve-headers
4149                            articles gnus-newsgroup-name
4150                            ;; We might want to fetch old headers, but
4151                            ;; not if there is only 1 article.
4152                            (and (or (and
4153                                      (not (eq gnus-fetch-old-headers 'some))
4154                                      (not (numberp gnus-fetch-old-headers)))
4155                                     (> (length articles) 1))
4156                                 gnus-fetch-old-headers))))
4157                 (gnus-get-newsgroup-headers-xover
4158                  articles nil nil gnus-newsgroup-name t)
4159               (gnus-get-newsgroup-headers)))
4160       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4161
4162       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4163       (when cached
4164         (setq gnus-newsgroup-cached cached))
4165
4166       ;; Suppress duplicates?
4167       (when gnus-suppress-duplicates
4168         (gnus-dup-suppress-articles))
4169
4170       ;; Set the initial limit.
4171       (setq gnus-newsgroup-limit (copy-sequence articles))
4172       ;; Remove canceled articles from the list of unread articles.
4173       (setq gnus-newsgroup-unreads
4174             (gnus-set-sorted-intersection
4175              gnus-newsgroup-unreads
4176              (setq fetched-articles
4177                    (mapcar (lambda (headers) (mail-header-number headers))
4178                            gnus-newsgroup-headers))))
4179       ;; Removed marked articles that do not exist.
4180       (gnus-update-missing-marks
4181        (gnus-sorted-complement fetched-articles articles))
4182       ;; We might want to build some more threads first.
4183       (when (and gnus-fetch-old-headers
4184                  (eq gnus-headers-retrieved-by 'nov))
4185         (if (eq gnus-fetch-old-headers 'invisible)
4186             (gnus-build-all-threads)
4187           (gnus-build-old-threads)))
4188       ;; Let the Gnus agent mark articles as read.
4189       (when gnus-agent
4190         (gnus-agent-get-undownloaded-list))
4191       ;; Remove list identifiers from subject
4192       (when gnus-list-identifiers
4193         (gnus-summary-remove-list-identifiers))
4194       ;; Check whether auto-expire is to be done in this group.
4195       (setq gnus-newsgroup-auto-expire
4196             (gnus-group-auto-expirable-p group))
4197       ;; Set up the article buffer now, if necessary.
4198       (unless gnus-single-article-buffer
4199         (gnus-article-setup-buffer))
4200       ;; First and last article in this newsgroup.
4201       (when gnus-newsgroup-headers
4202         (setq gnus-newsgroup-begin
4203               (mail-header-number (car gnus-newsgroup-headers))
4204               gnus-newsgroup-end
4205               (mail-header-number
4206                (gnus-last-element gnus-newsgroup-headers))))
4207       ;; GROUP is successfully selected.
4208       (or gnus-newsgroup-headers t)))))
4209
4210 (defun gnus-articles-to-read (group &optional read-all)
4211   ;; Find out what articles the user wants to read.
4212   (let* ((articles
4213           ;; Select all articles if `read-all' is non-nil, or if there
4214           ;; are no unread articles.
4215           (if (or read-all
4216                   (and (zerop (length gnus-newsgroup-marked))
4217                        (zerop (length gnus-newsgroup-unreads)))
4218                   (eq (gnus-group-find-parameter group 'display)
4219                       'all))
4220               (or
4221                (gnus-uncompress-range (gnus-active group))
4222                (gnus-cache-articles-in-group group))
4223             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4224                           (copy-sequence gnus-newsgroup-unreads))
4225                   '<)))
4226          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4227          (scored (length scored-list))
4228          (number (length articles))
4229          (marked (+ (length gnus-newsgroup-marked)
4230                     (length gnus-newsgroup-dormant)))
4231          (select
4232           (cond
4233            ((numberp read-all)
4234             read-all)
4235            (t
4236             (condition-case ()
4237                 (cond
4238                  ((and (or (<= scored marked) (= scored number))
4239                        (numberp gnus-large-newsgroup)
4240                        (> number gnus-large-newsgroup))
4241                   (let ((input
4242                          (read-string
4243                           (format
4244                            "How many articles from %s (default %d): "
4245                            (gnus-limit-string gnus-newsgroup-name 35)
4246                            number))))
4247                     (if (string-match "^[ \t]*$" input) number input)))
4248                  ((and (> scored marked) (< scored number)
4249                        (> (- scored number) 20))
4250                   (let ((input
4251                          (read-string
4252                           (format "%s %s (%d scored, %d total): "
4253                                   "How many articles from"
4254                                   group scored number))))
4255                     (if (string-match "^[ \t]*$" input)
4256                         number input)))
4257                  (t number))
4258               (quit nil))))))
4259     (setq select (if (stringp select) (string-to-number select) select))
4260     (if (or (null select) (zerop select))
4261         select
4262       (if (and (not (zerop scored)) (<= (abs select) scored))
4263           (progn
4264             (setq articles (sort scored-list '<))
4265             (setq number (length articles)))
4266         (setq articles (copy-sequence articles)))
4267
4268       (when (< (abs select) number)
4269         (if (< select 0)
4270             ;; Select the N oldest articles.
4271             (setcdr (nthcdr (1- (abs select)) articles) nil)
4272           ;; Select the N most recent articles.
4273           (setq articles (nthcdr (- number select) articles))))
4274       (setq gnus-newsgroup-unselected
4275             (gnus-sorted-intersection
4276              gnus-newsgroup-unreads
4277              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4278       articles)))
4279
4280 (defun gnus-killed-articles (killed articles)
4281   (let (out)
4282     (while articles
4283       (when (inline (gnus-member-of-range (car articles) killed))
4284         (push (car articles) out))
4285       (setq articles (cdr articles)))
4286     out))
4287
4288 (defun gnus-uncompress-marks (marks)
4289   "Uncompress the mark ranges in MARKS."
4290   (let ((uncompressed '(score bookmark))
4291         out)
4292     (while marks
4293       (if (memq (caar marks) uncompressed)
4294           (push (car marks) out)
4295         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4296       (setq marks (cdr marks)))
4297     out))
4298
4299 (defun gnus-adjust-marked-articles (info)
4300   "Set all article lists and remove all marks that are no longer valid."
4301   (let* ((marked-lists (gnus-info-marks info))
4302          (active (gnus-active (gnus-info-group info)))
4303          (min (car active))
4304          (max (cdr active))
4305          (types gnus-article-mark-lists)
4306          (uncompressed '(score bookmark killed))
4307          marks var articles article mark)
4308
4309     (while marked-lists
4310       (setq marks (pop marked-lists))
4311       (set (setq var (intern (format "gnus-newsgroup-%s"
4312                                      (car (rassq (setq mark (car marks))
4313                                                  types)))))
4314            (if (memq (car marks) uncompressed) (cdr marks)
4315              (gnus-uncompress-range (cdr marks))))
4316
4317       (setq articles (symbol-value var))
4318
4319       ;; All articles have to be subsets of the active articles.
4320       (cond
4321        ;; Adjust "simple" lists.
4322        ((memq mark '(tick dormant expire reply save))
4323         (while articles
4324           (when (or (< (setq article (pop articles)) min) (> article max))
4325             (set var (delq article (symbol-value var))))))
4326        ;; Adjust assocs.
4327        ((memq mark uncompressed)
4328         (when (not (listp (cdr (symbol-value var))))
4329           (set var (list (symbol-value var))))
4330         (when (not (listp (cdr articles)))
4331           (setq articles (list articles)))
4332         (while articles
4333           (when (or (not (consp (setq article (pop articles))))
4334                     (< (car article) min)
4335                     (> (car article) max))
4336             (set var (delq article (symbol-value var))))))))))
4337
4338 (defun gnus-update-missing-marks (missing)
4339   "Go through the list of MISSING articles and remove them from the mark lists."
4340   (when missing
4341     (let ((types gnus-article-mark-lists)
4342           var m)
4343       ;; Go through all types.
4344       (while types
4345         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4346         (when (symbol-value var)
4347           ;; This list has articles.  So we delete all missing articles
4348           ;; from it.
4349           (setq m missing)
4350           (while m
4351             (set var (delq (pop m) (symbol-value var)))))))))
4352
4353 (defun gnus-update-marks ()
4354   "Enter the various lists of marked articles into the newsgroup info list."
4355   (let ((types gnus-article-mark-lists)
4356         (info (gnus-get-info gnus-newsgroup-name))
4357         (uncompressed '(score bookmark killed))
4358         type list newmarked symbol delta-marks)
4359     (when info
4360       ;; Add all marks lists to the list of marks lists.
4361       (while (setq type (pop types))
4362         (setq list (symbol-value
4363                     (setq symbol
4364                           (intern (format "gnus-newsgroup-%s"
4365                                           (car type))))))
4366
4367         (when list
4368           ;; Get rid of the entries of the articles that have the
4369           ;; default score.
4370           (when (and (eq (cdr type) 'score)
4371                      gnus-save-score
4372                      list)
4373             (let* ((arts list)
4374                    (prev (cons nil list))
4375                    (all prev))
4376               (while arts
4377                 (if (or (not (consp (car arts)))
4378                         (= (cdar arts) gnus-summary-default-score))
4379                     (setcdr prev (cdr arts))
4380                   (setq prev arts))
4381                 (setq arts (cdr arts)))
4382               (setq list (cdr all)))))
4383
4384         (unless (memq (cdr type) uncompressed)
4385           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4386        
4387         (when (gnus-check-backend-function
4388                'request-set-mark gnus-newsgroup-name)
4389           ;; uncompressed:s are not proper flags (they are cons cells)
4390           ;; cache is a internal gnus flag
4391           (unless (memq (cdr type) (cons 'cache uncompressed))
4392             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4393                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4394                    (add (gnus-remove-from-range
4395                          (gnus-copy-sequence list) old)))
4396               (when add
4397                 (push (list add 'add (list (cdr type))) delta-marks))
4398               (when del
4399                 (push (list del 'del (list (cdr type))) delta-marks)))))
4400           
4401         (when list
4402           (push (cons (cdr type) list) newmarked)))
4403
4404       (when delta-marks
4405         (unless (gnus-check-group gnus-newsgroup-name)
4406           (error "Can't open server for %s" gnus-newsgroup-name))
4407         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4408           
4409       ;; Enter these new marks into the info of the group.
4410       (if (nthcdr 3 info)
4411           (setcar (nthcdr 3 info) newmarked)
4412         ;; Add the marks lists to the end of the info.
4413         (when newmarked
4414           (setcdr (nthcdr 2 info) (list newmarked))))
4415
4416       ;; Cut off the end of the info if there's nothing else there.
4417       (let ((i 5))
4418         (while (and (> i 2)
4419                     (not (nth i info)))
4420           (when (nthcdr (decf i) info)
4421             (setcdr (nthcdr i info) nil)))))))
4422
4423 (defun gnus-set-mode-line (where)
4424   "This function sets the mode line of the article or summary buffers.
4425 If WHERE is `summary', the summary mode line format will be used."
4426   ;; Is this mode line one we keep updated?
4427   (when (and (memq where gnus-updated-mode-lines)
4428              (symbol-value
4429               (intern (format "gnus-%s-mode-line-format-spec" where))))
4430     (let (mode-string)
4431       (save-excursion
4432         ;; We evaluate this in the summary buffer since these
4433         ;; variables are buffer-local to that buffer.
4434         (set-buffer gnus-summary-buffer)
4435         ;; We bind all these variables that are used in the `eval' form
4436         ;; below.
4437         (let* ((mformat (symbol-value
4438                          (intern
4439                           (format "gnus-%s-mode-line-format-spec" where))))
4440                (gnus-tmp-group-name gnus-newsgroup-name)
4441                (gnus-tmp-article-number (or gnus-current-article 0))
4442                (gnus-tmp-unread gnus-newsgroup-unreads)
4443                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4444                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4445                (gnus-tmp-unread-and-unselected
4446                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4447                             (zerop gnus-tmp-unselected))
4448                        "")
4449                       ((zerop gnus-tmp-unselected)
4450                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4451                       (t (format "{%d(+%d) more}"
4452                                  gnus-tmp-unread-and-unticked
4453                                  gnus-tmp-unselected))))
4454                (gnus-tmp-subject
4455                 (if (and gnus-current-headers
4456                          (vectorp gnus-current-headers))
4457                     (gnus-mode-string-quote
4458                      (mail-header-subject gnus-current-headers))
4459                   ""))
4460                bufname-length max-len
4461                gnus-tmp-header);; passed as argument to any user-format-funcs
4462           (setq mode-string (eval mformat))
4463           (setq bufname-length (if (string-match "%b" mode-string)
4464                                    (- (length
4465                                        (buffer-name
4466                                         (if (eq where 'summary)
4467                                             nil
4468                                           (get-buffer gnus-article-buffer))))
4469                                       2)
4470                                  0))
4471           (setq max-len (max 4 (if gnus-mode-non-string-length
4472                                    (- (window-width)
4473                                       gnus-mode-non-string-length
4474                                       bufname-length)
4475                                  (length mode-string))))
4476           ;; We might have to chop a bit of the string off...
4477           (when (> (length mode-string) max-len)
4478             (setq mode-string
4479                   (concat (truncate-string-to-width mode-string (- max-len 3))
4480                           "...")))
4481           ;; Pad the mode string a bit.
4482           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4483       ;; Update the mode line.
4484       (setq mode-line-buffer-identification
4485             (gnus-mode-line-buffer-identification (list mode-string)))
4486       (set-buffer-modified-p t))))
4487
4488 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4489   "Go through the HEADERS list and add all Xrefs to a hash table.
4490 The resulting hash table is returned, or nil if no Xrefs were found."
4491   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4492          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4493          (xref-hashtb (gnus-make-hashtable))
4494          start group entry number xrefs header)
4495     (while headers
4496       (setq header (pop headers))
4497       (when (and (setq xrefs (mail-header-xref header))
4498                  (not (memq (setq number (mail-header-number header))
4499                             unreads)))
4500         (setq start 0)
4501         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4502           (setq start (match-end 0))
4503           (setq group (if prefix
4504                           (concat prefix (substring xrefs (match-beginning 1)
4505                                                     (match-end 1)))
4506                         (substring xrefs (match-beginning 1) (match-end 1))))
4507           (setq number
4508                 (string-to-int (substring xrefs (match-beginning 2)
4509                                           (match-end 2))))
4510           (if (setq entry (gnus-gethash group xref-hashtb))
4511               (setcdr entry (cons number (cdr entry)))
4512             (gnus-sethash group (cons number nil) xref-hashtb)))))
4513     (and start xref-hashtb)))
4514
4515 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4516   "Look through all the headers and mark the Xrefs as read."
4517   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4518         name entry info xref-hashtb idlist method nth4)
4519     (save-excursion
4520       (set-buffer gnus-group-buffer)
4521       (when (setq xref-hashtb
4522                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4523         (mapatoms
4524          (lambda (group)
4525            (unless (string= from-newsgroup (setq name (symbol-name group)))
4526              (setq idlist (symbol-value group))
4527              ;; Dead groups are not updated.
4528              (and (prog1
4529                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4530                             info (nth 2 entry))
4531                     (when (stringp (setq nth4 (gnus-info-method info)))
4532                       (setq nth4 (gnus-server-to-method nth4))))
4533                   ;; Only do the xrefs if the group has the same
4534                   ;; select method as the group we have just read.
4535                   (or (gnus-methods-equal-p
4536                        nth4 (gnus-find-method-for-group from-newsgroup))
4537                       virtual
4538                       (equal nth4 (setq method (gnus-find-method-for-group
4539                                                 from-newsgroup)))
4540                       (and (equal (car nth4) (car method))
4541                            (equal (nth 1 nth4) (nth 1 method))))
4542                   gnus-use-cross-reference
4543                   (or (not (eq gnus-use-cross-reference t))
4544                       virtual
4545                       ;; Only do cross-references on subscribed
4546                       ;; groups, if that is what is wanted.
4547                       (<= (gnus-info-level info) gnus-level-subscribed))
4548                   (gnus-group-make-articles-read name idlist))))
4549          xref-hashtb)))))
4550
4551 (defun gnus-compute-read-articles (group articles)
4552   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4553          (info (nth 2 entry))
4554          (active (gnus-active group))
4555          ninfo)
4556     (when entry
4557       ;; First peel off all invalid article numbers.
4558       (when active
4559         (let ((ids articles)
4560               id first)
4561           (while (setq id (pop ids))
4562             (when (and first (> id (cdr active)))
4563               ;; We'll end up in this situation in one particular
4564               ;; obscure situation.  If you re-scan a group and get
4565               ;; a new article that is cross-posted to a different
4566               ;; group that has not been re-scanned, you might get
4567               ;; crossposted article that has a higher number than
4568               ;; Gnus believes possible.  So we re-activate this
4569               ;; group as well.  This might mean doing the
4570               ;; crossposting thingy will *increase* the number
4571               ;; of articles in some groups.  Tsk, tsk.
4572               (setq active (or (gnus-activate-group group) active)))
4573             (when (or (> id (cdr active))
4574                       (< id (car active)))
4575               (setq articles (delq id articles))))))
4576       ;; If the read list is nil, we init it.
4577       (if (and active
4578                (null (gnus-info-read info))
4579                (> (car active) 1))
4580           (setq ninfo (cons 1 (1- (car active))))
4581         (setq ninfo (gnus-info-read info)))
4582       ;; Then we add the read articles to the range.
4583       (gnus-add-to-range
4584        ninfo (setq articles (sort articles '<))))))
4585
4586 (defun gnus-group-make-articles-read (group articles)
4587   "Update the info of GROUP to say that ARTICLES are read."
4588   (let* ((num 0)
4589          (entry (gnus-gethash group gnus-newsrc-hashtb))
4590          (info (nth 2 entry))
4591          (active (gnus-active group))
4592          range)
4593     (when entry
4594       (setq range (gnus-compute-read-articles group articles))
4595       (save-excursion
4596         (set-buffer gnus-group-buffer)
4597         (gnus-undo-register
4598           `(progn
4599              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4600              (gnus-info-set-read ',info ',(gnus-info-read info))
4601              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4602              (gnus-group-update-group ,group t))))
4603       ;; Add the read articles to the range.
4604       (gnus-info-set-read info range)
4605       ;; Then we have to re-compute how many unread
4606       ;; articles there are in this group.
4607       (when active
4608         (cond
4609          ((not range)
4610           (setq num (- (1+ (cdr active)) (car active))))
4611          ((not (listp (cdr range)))
4612           (setq num (- (cdr active) (- (1+ (cdr range))
4613                                        (car range)))))
4614          (t
4615           (while range
4616             (if (numberp (car range))
4617                 (setq num (1+ num))
4618               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4619             (setq range (cdr range)))
4620           (setq num (- (cdr active) num))))
4621         ;; Update the number of unread articles.
4622         (setcar entry num)
4623         ;; Update the group buffer.
4624         (gnus-group-update-group group t)))))
4625
4626 (defvar gnus-newsgroup-none-id 0)
4627
4628 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4629   (let ((cur nntp-server-buffer)
4630         (dependencies
4631          (or dependencies
4632              (save-excursion (set-buffer gnus-summary-buffer)
4633                              gnus-newsgroup-dependencies)))
4634         headers id end ref
4635         (mail-parse-charset gnus-newsgroup-charset)
4636         (mail-parse-ignored-charsets 
4637          (save-excursion (condition-case nil
4638                              (set-buffer gnus-summary-buffer)
4639                            (error))
4640                          gnus-newsgroup-ignored-charsets)))
4641     (save-excursion
4642       (set-buffer nntp-server-buffer)
4643       ;; Translate all TAB characters into SPACE characters.
4644       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4645       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4646       (gnus-run-hooks 'gnus-parse-headers-hook)
4647       (let ((case-fold-search t)
4648             in-reply-to header p lines chars)
4649         (goto-char (point-min))
4650         ;; Search to the beginning of the next header.  Error messages
4651         ;; do not begin with 2 or 3.
4652         (while (re-search-forward "^[23][0-9]+ " nil t)
4653           (setq id nil
4654                 ref nil)
4655           ;; This implementation of this function, with nine
4656           ;; search-forwards instead of the one re-search-forward and
4657           ;; a case (which basically was the old function) is actually
4658           ;; about twice as fast, even though it looks messier.  You
4659           ;; can't have everything, I guess.  Speed and elegance
4660           ;; doesn't always go hand in hand.
4661           (setq
4662            header
4663            (make-full-mail-header
4664             ;; Number.
4665             (prog1
4666                 (read cur)
4667               (end-of-line)
4668               (setq p (point))
4669               (narrow-to-region (point)
4670                                 (or (and (search-forward "\n.\n" nil t)
4671                                          (- (point) 2))
4672                                     (point))))
4673             ;; Subject.
4674             (progn
4675               (goto-char p)
4676               (if (search-forward "\nsubject: " nil t)
4677                   (nnheader-header-value)
4678                 "(none)"))
4679             ;; From.
4680             (progn
4681               (goto-char p)
4682               (if (search-forward "\nfrom: " nil t)
4683                   (nnheader-header-value)
4684                 "(nobody)"))
4685             ;; Date.
4686             (progn
4687               (goto-char p)
4688               (if (search-forward "\ndate: " nil t)
4689                   (nnheader-header-value) ""))
4690             ;; Message-ID.
4691             (progn
4692               (goto-char p)
4693               (setq id (if (re-search-forward
4694                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4695                            ;; We do it this way to make sure the Message-ID
4696                            ;; is (somewhat) syntactically valid.
4697                            (buffer-substring (match-beginning 1)
4698                                              (match-end 1))
4699                          ;; If there was no message-id, we just fake one
4700                          ;; to make subsequent routines simpler.
4701                          (nnheader-generate-fake-message-id))))
4702             ;; References.
4703             (progn
4704               (goto-char p)
4705               (if (search-forward "\nreferences: " nil t)
4706                   (progn
4707                     (setq end (point))
4708                     (prog1
4709                         (nnheader-header-value)
4710                       (setq ref
4711                             (buffer-substring
4712                              (progn
4713                                (end-of-line)
4714                                (search-backward ">" end t)
4715                                (1+ (point)))
4716                              (progn
4717                                (search-backward "<" end t)
4718                                (point))))))
4719                 ;; Get the references from the in-reply-to header if there
4720                 ;; were no references and the in-reply-to header looks
4721                 ;; promising.
4722                 (if (and (search-forward "\nin-reply-to: " nil t)
4723                          (setq in-reply-to (nnheader-header-value))
4724                          (string-match "<[^>]+>" in-reply-to))
4725                     (let (ref2)
4726                       (setq ref (substring in-reply-to (match-beginning 0)
4727                                            (match-end 0)))
4728                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4729                         (setq ref2 (substring in-reply-to (match-beginning 0)
4730                                               (match-end 0)))
4731                         (when (> (length ref2) (length ref))
4732                           (setq ref ref2)))
4733                       ref)
4734                   (setq ref nil))))
4735             ;; Chars.
4736             (progn
4737               (goto-char p)
4738               (if (search-forward "\nchars: " nil t)
4739                   (if (numberp (setq chars (ignore-errors (read cur))))
4740                       chars 0)
4741                 0))
4742             ;; Lines.
4743             (progn
4744               (goto-char p)
4745               (if (search-forward "\nlines: " nil t)
4746                   (if (numberp (setq lines (ignore-errors (read cur))))
4747                       lines 0)
4748                 0))
4749             ;; Xref.
4750             (progn
4751               (goto-char p)
4752               (and (search-forward "\nxref: " nil t)
4753                    (nnheader-header-value)))
4754             ;; Extra.
4755             (when gnus-extra-headers
4756               (let ((extra gnus-extra-headers)
4757                     out)
4758                 (while extra
4759                   (goto-char p)
4760                   (when (search-forward
4761                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4762                     (push (cons (car extra) (nnheader-header-value))
4763                           out))
4764                   (pop extra))
4765                 out))))
4766           (when (equal id ref)
4767             (setq ref nil))
4768
4769           (when gnus-alter-header-function
4770             (funcall gnus-alter-header-function header)
4771             (setq id (mail-header-id header)
4772                   ref (gnus-parent-id (mail-header-references header))))
4773
4774           (when (setq header
4775                       (gnus-dependencies-add-header
4776                        header dependencies force-new))
4777             (push header headers))
4778           (goto-char (point-max))
4779           (widen))
4780         (nreverse headers)))))
4781
4782 ;; Goes through the xover lines and returns a list of vectors
4783 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4784                                                   force-new dependencies
4785                                                   group also-fetch-heads)
4786   "Parse the news overview data in the server buffer, and return a
4787 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4788   ;; Get the Xref when the users reads the articles since most/some
4789   ;; NNTP servers do not include Xrefs when using XOVER.
4790   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4791   (let ((mail-parse-charset gnus-newsgroup-charset)
4792         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4793         (cur nntp-server-buffer)
4794         (dependencies (or dependencies gnus-newsgroup-dependencies))
4795         number headers header)
4796     (save-excursion
4797       (set-buffer nntp-server-buffer)
4798       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4799       ;; Allow the user to mangle the headers before parsing them.
4800       (gnus-run-hooks 'gnus-parse-headers-hook)
4801       (goto-char (point-min))
4802       (while (not (eobp))
4803         (condition-case ()
4804             (while (and sequence (not (eobp)))
4805               (setq number (read cur))
4806               (while (and sequence
4807                           (< (car sequence) number))
4808                 (setq sequence (cdr sequence)))
4809               (and sequence
4810                    (eq number (car sequence))
4811                    (progn
4812                      (setq sequence (cdr sequence))
4813                      (setq header (inline
4814                                     (gnus-nov-parse-line
4815                                      number dependencies force-new))))
4816                    (push header headers))
4817               (forward-line 1))
4818           (error
4819            (gnus-error 4 "Strange nov line (%d)"
4820                        (count-lines (point-min) (point)))))
4821         (forward-line 1))
4822       ;; A common bug in inn is that if you have posted an article and
4823       ;; then retrieves the active file, it will answer correctly --
4824       ;; the new article is included.  However, a NOV entry for the
4825       ;; article may not have been generated yet, so this may fail.
4826       ;; We work around this problem by retrieving the last few
4827       ;; headers using HEAD.
4828       (if (or (not also-fetch-heads)
4829               (not sequence))
4830           ;; We (probably) got all the headers.
4831           (nreverse headers)
4832         (let ((gnus-nov-is-evil t))
4833           (nconc
4834            (nreverse headers)
4835            (when (gnus-retrieve-headers sequence group)
4836              (gnus-get-newsgroup-headers))))))))
4837
4838 (defun gnus-article-get-xrefs ()
4839   "Fill in the Xref value in `gnus-current-headers', if necessary.
4840 This is meant to be called in `gnus-article-internal-prepare-hook'."
4841   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4842                                  gnus-current-headers)))
4843     (or (not gnus-use-cross-reference)
4844         (not headers)
4845         (and (mail-header-xref headers)
4846              (not (string= (mail-header-xref headers) "")))
4847         (let ((case-fold-search t)
4848               xref)
4849           (save-restriction
4850             (nnheader-narrow-to-headers)
4851             (goto-char (point-min))
4852             (when (or (and (not (eobp))
4853                            (eq (downcase (char-after)) ?x)
4854                            (looking-at "Xref:"))
4855                       (search-forward "\nXref:" nil t))
4856               (goto-char (1+ (match-end 0)))
4857               (setq xref (buffer-substring (point)
4858                                            (progn (end-of-line) (point))))
4859               (mail-header-set-xref headers xref)))))))
4860
4861 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4862   "Find article ID and insert the summary line for that article.
4863 OLD-HEADER can either be a header or a line number to insert
4864 the subject line on."
4865   (let* ((line (and (numberp old-header) old-header))
4866          (old-header (and (vectorp old-header) old-header))
4867          (header (cond ((and old-header use-old-header)
4868                         old-header)
4869                        ((and (numberp id)
4870                              (gnus-number-to-header id))
4871                         (gnus-number-to-header id))
4872                        (t
4873                         (gnus-read-header id))))
4874          (number (and (numberp id) id))
4875          d)
4876     (when header
4877       ;; Rebuild the thread that this article is part of and go to the
4878       ;; article we have fetched.
4879       (when (and (not gnus-show-threads)
4880                  old-header)
4881         (when (and number
4882                    (setq d (gnus-data-find (mail-header-number old-header))))
4883           (goto-char (gnus-data-pos d))
4884           (gnus-data-remove
4885            number
4886            (- (gnus-point-at-bol)
4887               (prog1
4888                   (1+ (gnus-point-at-eol))
4889                 (gnus-delete-line))))))
4890       (when old-header
4891         (mail-header-set-number header (mail-header-number old-header)))
4892       (setq gnus-newsgroup-sparse
4893             (delq (setq number (mail-header-number header))
4894                   gnus-newsgroup-sparse))
4895       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4896       (push number gnus-newsgroup-limit)
4897       (gnus-rebuild-thread (mail-header-id header) line)
4898       (gnus-summary-goto-subject number nil t))
4899     (when (and (numberp number)
4900                (> number 0))
4901       ;; We have to update the boundaries even if we can't fetch the
4902       ;; article if ID is a number -- so that the next `P' or `N'
4903       ;; command will fetch the previous (or next) article even
4904       ;; if the one we tried to fetch this time has been canceled.
4905       (when (> number gnus-newsgroup-end)
4906         (setq gnus-newsgroup-end number))
4907       (when (< number gnus-newsgroup-begin)
4908         (setq gnus-newsgroup-begin number))
4909       (setq gnus-newsgroup-unselected
4910             (delq number gnus-newsgroup-unselected)))
4911     ;; Report back a success?
4912     (and header (mail-header-number header))))
4913
4914 ;;; Process/prefix in the summary buffer
4915
4916 (defun gnus-summary-work-articles (n)
4917   "Return a list of articles to be worked upon.
4918 The prefix argument, the list of process marked articles, and the
4919 current article will be taken into consideration."
4920   (save-excursion
4921     (set-buffer gnus-summary-buffer)
4922     (cond
4923      (n
4924       ;; A numerical prefix has been given.
4925       (setq n (prefix-numeric-value n))
4926       (let ((backward (< n 0))
4927             (n (abs (prefix-numeric-value n)))
4928             articles article)
4929         (save-excursion
4930           (while
4931               (and (> n 0)
4932                    (push (setq article (gnus-summary-article-number))
4933                          articles)
4934                    (if backward
4935                        (gnus-summary-find-prev nil article)
4936                      (gnus-summary-find-next nil article)))
4937             (decf n)))
4938         (nreverse articles)))
4939      ((and (gnus-region-active-p) (mark))
4940       (message "region active")
4941       ;; Work on the region between point and mark.
4942       (let ((max (max (point) (mark)))
4943             articles article)
4944         (save-excursion
4945           (goto-char (min (min (point) (mark))))
4946           (while
4947               (and
4948                (push (setq article (gnus-summary-article-number)) articles)
4949                (gnus-summary-find-next nil article)
4950                (< (point) max)))
4951           (nreverse articles))))
4952      (gnus-newsgroup-processable
4953       ;; There are process-marked articles present.
4954       ;; Save current state.
4955       (gnus-summary-save-process-mark)
4956       ;; Return the list.
4957       (reverse gnus-newsgroup-processable))
4958      (t
4959       ;; Just return the current article.
4960       (list (gnus-summary-article-number))))))
4961
4962 (defmacro gnus-summary-iterate (arg &rest forms)
4963   "Iterate over the process/prefixed articles and do FORMS.
4964 ARG is the interactive prefix given to the command.  FORMS will be
4965 executed with point over the summary line of the articles."
4966   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4967     `(let ((,articles (gnus-summary-work-articles ,arg)))
4968        (while ,articles
4969          (gnus-summary-goto-subject (car ,articles))
4970          ,@forms
4971          (pop ,articles)))))
4972
4973 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4974 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4975
4976 (defun gnus-summary-save-process-mark ()
4977   "Push the current set of process marked articles on the stack."
4978   (interactive)
4979   (push (copy-sequence gnus-newsgroup-processable)
4980         gnus-newsgroup-process-stack))
4981
4982 (defun gnus-summary-kill-process-mark ()
4983   "Push the current set of process marked articles on the stack and unmark."
4984   (interactive)
4985   (gnus-summary-save-process-mark)
4986   (gnus-summary-unmark-all-processable))
4987
4988 (defun gnus-summary-yank-process-mark ()
4989   "Pop the last process mark state off the stack and restore it."
4990   (interactive)
4991   (unless gnus-newsgroup-process-stack
4992     (error "Empty mark stack"))
4993   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4994
4995 (defun gnus-summary-process-mark-set (set)
4996   "Make SET into the current process marked articles."
4997   (gnus-summary-unmark-all-processable)
4998   (while set
4999     (gnus-summary-set-process-mark (pop set))))
5000
5001 ;;; Searching and stuff
5002
5003 (defun gnus-summary-search-group (&optional backward use-level)
5004   "Search for next unread newsgroup.
5005 If optional argument BACKWARD is non-nil, search backward instead."
5006   (save-excursion
5007     (set-buffer gnus-group-buffer)
5008     (when (gnus-group-search-forward
5009            backward nil (if use-level (gnus-group-group-level) nil))
5010       (gnus-group-group-name))))
5011
5012 (defun gnus-summary-best-group (&optional exclude-group)
5013   "Find the name of the best unread group.
5014 If EXCLUDE-GROUP, do not go to this group."
5015   (save-excursion
5016     (set-buffer gnus-group-buffer)
5017     (save-excursion
5018       (gnus-group-best-unread-group exclude-group))))
5019
5020 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5021   (if backward (gnus-summary-find-prev)
5022     (let* ((dummy (gnus-summary-article-intangible-p))
5023            (article (or article (gnus-summary-article-number)))
5024            (arts (gnus-data-find-list article))
5025            result)
5026       (when (and (not dummy)
5027                  (or (not gnus-summary-check-current)
5028                      (not unread)
5029                      (not (gnus-data-unread-p (car arts)))))
5030         (setq arts (cdr arts)))
5031       (when (setq result
5032                   (if unread
5033                       (progn
5034                         (while arts
5035                           (when (or (and undownloaded
5036                                          (eq gnus-undownloaded-mark
5037                                              (gnus-data-mark (car arts))))
5038                                     (gnus-data-unread-p (car arts)))
5039                             (setq result (car arts)
5040                                   arts nil))
5041                           (setq arts (cdr arts)))
5042                         result)
5043                     (car arts)))
5044         (goto-char (gnus-data-pos result))
5045         (gnus-data-number result)))))
5046
5047 (defun gnus-summary-find-prev (&optional unread article)
5048   (let* ((eobp (eobp))
5049          (article (or article (gnus-summary-article-number)))
5050          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5051          result)
5052     (when (and (not eobp)
5053                (or (not gnus-summary-check-current)
5054                    (not unread)
5055                    (not (gnus-data-unread-p (car arts)))))
5056       (setq arts (cdr arts)))
5057     (when (setq result
5058                 (if unread
5059                     (progn
5060                       (while arts
5061                         (when (gnus-data-unread-p (car arts))
5062                           (setq result (car arts)
5063                                 arts nil))
5064                         (setq arts (cdr arts)))
5065                       result)
5066                   (car arts)))
5067       (goto-char (gnus-data-pos result))
5068       (gnus-data-number result))))
5069
5070 (defun gnus-summary-find-subject (subject &optional unread backward article)
5071   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5072          (article (or article (gnus-summary-article-number)))
5073          (articles (gnus-data-list backward))
5074          (arts (gnus-data-find-list article articles))
5075          result)
5076     (when (or (not gnus-summary-check-current)
5077               (not unread)
5078               (not (gnus-data-unread-p (car arts))))
5079       (setq arts (cdr arts)))
5080     (while arts
5081       (and (or (not unread)
5082                (gnus-data-unread-p (car arts)))
5083            (vectorp (gnus-data-header (car arts)))
5084            (gnus-subject-equal
5085             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5086            (setq result (car arts)
5087                  arts nil))
5088       (setq arts (cdr arts)))
5089     (and result
5090          (goto-char (gnus-data-pos result))
5091          (gnus-data-number result))))
5092
5093 (defun gnus-summary-search-forward (&optional unread subject backward)
5094   "Search forward for an article.
5095 If UNREAD, look for unread articles.  If SUBJECT, look for
5096 articles with that subject.  If BACKWARD, search backward instead."
5097   (cond (subject (gnus-summary-find-subject subject unread backward))
5098         (backward (gnus-summary-find-prev unread))
5099         (t (gnus-summary-find-next unread))))
5100
5101 (defun gnus-recenter (&optional n)
5102   "Center point in window and redisplay frame.
5103 Also do horizontal recentering."
5104   (interactive "P")
5105   (when (and gnus-auto-center-summary
5106              (not (eq gnus-auto-center-summary 'vertical)))
5107     (gnus-horizontal-recenter))
5108   (recenter n))
5109
5110 (defun gnus-summary-recenter ()
5111   "Center point in the summary window.
5112 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5113 displayed, no centering will be performed."
5114   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5115   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5116   (let* ((top (cond ((< (window-height) 4) 0)
5117                     ((< (window-height) 7) 1)
5118                     (t (if (numberp gnus-auto-center-summary)
5119                            gnus-auto-center-summary
5120                          2))))
5121          (height (1- (window-height)))
5122          (bottom (save-excursion (goto-char (point-max))
5123                                  (forward-line (- height))
5124                                  (point)))
5125          (window (get-buffer-window (current-buffer))))
5126     ;; The user has to want it.
5127     (when gnus-auto-center-summary
5128       (when (get-buffer-window gnus-article-buffer)
5129         ;; Only do recentering when the article buffer is displayed,
5130         ;; Set the window start to either `bottom', which is the biggest
5131         ;; possible valid number, or the second line from the top,
5132         ;; whichever is the least.
5133         (set-window-start
5134          window (min bottom (save-excursion
5135                               (forward-line (- top)) (point)))
5136          t))
5137       ;; Do horizontal recentering while we're at it.
5138       (when (and (get-buffer-window (current-buffer) t)
5139                  (not (eq gnus-auto-center-summary 'vertical)))
5140         (let ((selected (selected-window)))
5141           (select-window (get-buffer-window (current-buffer) t))
5142           (gnus-summary-position-point)
5143           (gnus-horizontal-recenter)
5144           (select-window selected))))))
5145
5146 (defun gnus-summary-jump-to-group (newsgroup)
5147   "Move point to NEWSGROUP in group mode buffer."
5148   ;; Keep update point of group mode buffer if visible.
5149   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5150       (save-window-excursion
5151         ;; Take care of tree window mode.
5152         (when (get-buffer-window gnus-group-buffer)
5153           (pop-to-buffer gnus-group-buffer))
5154         (gnus-group-jump-to-group newsgroup))
5155     (save-excursion
5156       ;; Take care of tree window mode.
5157       (if (get-buffer-window gnus-group-buffer)
5158           (pop-to-buffer gnus-group-buffer)
5159         (set-buffer gnus-group-buffer))
5160       (gnus-group-jump-to-group newsgroup))))
5161
5162 ;; This function returns a list of article numbers based on the
5163 ;; difference between the ranges of read articles in this group and
5164 ;; the range of active articles.
5165 (defun gnus-list-of-unread-articles (group)
5166   (let* ((read (gnus-info-read (gnus-get-info group)))
5167          (active (or (gnus-active group) (gnus-activate-group group)))
5168          (last (cdr active))
5169          first nlast unread)
5170     ;; If none are read, then all are unread.
5171     (if (not read)
5172         (setq first (car active))
5173       ;; If the range of read articles is a single range, then the
5174       ;; first unread article is the article after the last read
5175       ;; article.  Sounds logical, doesn't it?
5176       (if (and (not (listp (cdr read)))
5177                (or (< (car read) (car active))
5178                    (progn (setq read (list read))
5179                           nil)))
5180           (setq first (max (car active) (1+ (cdr read))))
5181         ;; `read' is a list of ranges.
5182         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5183                                   (caar read)))
5184                   1)
5185           (setq first (car active)))
5186         (while read
5187           (when first
5188             (while (< first nlast)
5189               (push first unread)
5190               (setq first (1+ first))))
5191           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5192           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5193           (setq read (cdr read)))))
5194     ;; And add the last unread articles.
5195     (while (<= first last)
5196       (push first unread)
5197       (setq first (1+ first)))
5198     ;; Return the list of unread articles.
5199     (delq 0 (nreverse unread))))
5200
5201 (defun gnus-list-of-read-articles (group)
5202   "Return a list of unread, unticked and non-dormant articles."
5203   (let* ((info (gnus-get-info group))
5204          (marked (gnus-info-marks info))
5205          (active (gnus-active group)))
5206     (and info active
5207          (gnus-set-difference
5208           (gnus-sorted-complement
5209            (gnus-uncompress-range active)
5210            (gnus-list-of-unread-articles group))
5211           (append
5212            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5213            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5214
5215 ;; Various summary commands
5216
5217 (defun gnus-summary-select-article-buffer ()
5218   "Reconfigure windows to show article buffer."
5219   (interactive)
5220   (if (not (gnus-buffer-live-p gnus-article-buffer))
5221       (error "There is no article buffer for this summary buffer")
5222     (gnus-configure-windows 'article)
5223     (select-window (get-buffer-window gnus-article-buffer))))
5224
5225 (defun gnus-summary-universal-argument (arg)
5226   "Perform any operation on all articles that are process/prefixed."
5227   (interactive "P")
5228   (let ((articles (gnus-summary-work-articles arg))
5229         func article)
5230     (if (eq
5231          (setq
5232           func
5233           (key-binding
5234            (read-key-sequence
5235             (substitute-command-keys
5236              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5237          'undefined)
5238         (gnus-error 1 "Undefined key")
5239       (save-excursion
5240         (while articles
5241           (gnus-summary-goto-subject (setq article (pop articles)))
5242           (let (gnus-newsgroup-processable)
5243             (command-execute func))
5244           (gnus-summary-remove-process-mark article)))))
5245   (gnus-summary-position-point))
5246
5247 (defun gnus-summary-toggle-truncation (&optional arg)
5248   "Toggle truncation of summary lines.
5249 With arg, turn line truncation on iff arg is positive."
5250   (interactive "P")
5251   (setq truncate-lines
5252         (if (null arg) (not truncate-lines)
5253           (> (prefix-numeric-value arg) 0)))
5254   (redraw-display))
5255
5256 (defun gnus-summary-reselect-current-group (&optional all rescan)
5257   "Exit and then reselect the current newsgroup.
5258 The prefix argument ALL means to select all articles."
5259   (interactive "P")
5260   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5261     (error "Ephemeral groups can't be reselected"))
5262   (let ((current-subject (gnus-summary-article-number))
5263         (group gnus-newsgroup-name))
5264     (setq gnus-newsgroup-begin nil)
5265     (gnus-summary-exit)
5266     ;; We have to adjust the point of group mode buffer because
5267     ;; point was moved to the next unread newsgroup by exiting.
5268     (gnus-summary-jump-to-group group)
5269     (when rescan
5270       (save-excursion
5271         (gnus-group-get-new-news-this-group 1)))
5272     (gnus-group-read-group all t)
5273     (gnus-summary-goto-subject current-subject nil t)))
5274
5275 (defun gnus-summary-rescan-group (&optional all)
5276   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5277   (interactive "P")
5278   (gnus-summary-reselect-current-group all t))
5279
5280 (defun gnus-summary-update-info (&optional non-destructive)
5281   (save-excursion
5282     (let ((group gnus-newsgroup-name))
5283       (when group
5284         (when gnus-newsgroup-kill-headers
5285           (setq gnus-newsgroup-killed
5286                 (gnus-compress-sequence
5287                  (nconc
5288                   (gnus-set-sorted-intersection
5289                    (gnus-uncompress-range gnus-newsgroup-killed)
5290                    (setq gnus-newsgroup-unselected
5291                          (sort gnus-newsgroup-unselected '<)))
5292                   (setq gnus-newsgroup-unreads
5293                         (sort gnus-newsgroup-unreads '<)))
5294                  t)))
5295         (unless (listp (cdr gnus-newsgroup-killed))
5296           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5297         (let ((headers gnus-newsgroup-headers))
5298           ;; Set the new ranges of read articles.
5299           (save-excursion
5300             (set-buffer gnus-group-buffer)
5301             (gnus-undo-force-boundary))
5302           (gnus-update-read-articles
5303            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5304           ;; Set the current article marks.
5305           (let ((gnus-newsgroup-scored
5306                  (if (and (not gnus-save-score)
5307                           (not non-destructive))
5308                      nil
5309                    gnus-newsgroup-scored)))
5310             (save-excursion
5311               (gnus-update-marks)))
5312           ;; Do the cross-ref thing.
5313           (when gnus-use-cross-reference
5314             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5315           ;; Do not switch windows but change the buffer to work.
5316           (set-buffer gnus-group-buffer)
5317           (unless (gnus-ephemeral-group-p group)
5318             (gnus-group-update-group group)))))))
5319
5320 (defun gnus-summary-save-newsrc (&optional force)
5321   "Save the current number of read/marked articles in the dribble buffer.
5322 The dribble buffer will then be saved.
5323 If FORCE (the prefix), also save the .newsrc file(s)."
5324   (interactive "P")
5325   (gnus-summary-update-info t)
5326   (if force
5327       (gnus-save-newsrc-file)
5328     (gnus-dribble-save)))
5329
5330 (defun gnus-summary-exit (&optional temporary)
5331   "Exit reading current newsgroup, and then return to group selection mode.
5332 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5333   (interactive)
5334   (gnus-set-global-variables)
5335   (when (gnus-buffer-live-p gnus-article-buffer)
5336     (save-excursion
5337       (set-buffer gnus-article-buffer)
5338       (mm-destroy-parts gnus-article-mime-handles)))
5339   (gnus-kill-save-kill-buffer)
5340   (gnus-async-halt-prefetch)
5341   (let* ((group gnus-newsgroup-name)
5342          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5343          (mode major-mode)
5344          (group-point nil)
5345          (buf (current-buffer)))
5346     (unless quit-config
5347       ;; Do adaptive scoring, and possibly save score files.
5348       (when gnus-newsgroup-adaptive
5349         (gnus-score-adaptive))
5350       (when gnus-use-scoring
5351         (gnus-score-save)))
5352     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5353     ;; If we have several article buffers, we kill them at exit.
5354     (unless gnus-single-article-buffer
5355       (gnus-kill-buffer gnus-original-article-buffer)
5356       (setq gnus-article-current nil))
5357     (when gnus-use-cache
5358       (gnus-cache-possibly-remove-articles)
5359       (gnus-cache-save-buffers))
5360     (gnus-async-prefetch-remove-group group)
5361     (when gnus-suppress-duplicates
5362       (gnus-dup-enter-articles))
5363     (when gnus-use-trees
5364       (gnus-tree-close group))
5365     (when gnus-use-cache
5366       (gnus-cache-write-active))
5367     ;; Remove entries for this group.
5368     (nnmail-purge-split-history (gnus-group-real-name group))
5369     ;; Make all changes in this group permanent.
5370     (unless quit-config
5371       (gnus-run-hooks 'gnus-exit-group-hook)
5372       (gnus-summary-update-info))
5373     (gnus-close-group group)
5374     ;; Make sure where we were, and go to next newsgroup.
5375     (set-buffer gnus-group-buffer)
5376     (unless quit-config
5377       (gnus-group-jump-to-group group))
5378     (gnus-run-hooks 'gnus-summary-exit-hook)
5379     (unless (or quit-config
5380                 ;; If this group has disappeared from the summary
5381                 ;; buffer, don't skip forwards.
5382                 (not (string= group (gnus-group-group-name))))
5383       (gnus-group-next-unread-group 1))
5384     (setq group-point (point))
5385     (if temporary
5386         nil                             ;Nothing to do.
5387       ;; If we have several article buffers, we kill them at exit.
5388       (unless gnus-single-article-buffer
5389         (gnus-kill-buffer gnus-article-buffer)
5390         (gnus-kill-buffer gnus-original-article-buffer)
5391         (setq gnus-article-current nil))
5392       (set-buffer buf)
5393       (if (not gnus-kill-summary-on-exit)
5394           (gnus-deaden-summary)
5395         ;; We set all buffer-local variables to nil.  It is unclear why
5396         ;; this is needed, but if we don't, buffer-local variables are
5397         ;; not garbage-collected, it seems.  This would the lead to en
5398         ;; ever-growing Emacs.
5399         (gnus-summary-clear-local-variables)
5400         (when (get-buffer gnus-article-buffer)
5401           (bury-buffer gnus-article-buffer))
5402         ;; We clear the global counterparts of the buffer-local
5403         ;; variables as well, just to be on the safe side.
5404         (set-buffer gnus-group-buffer)
5405         (gnus-summary-clear-local-variables)
5406         ;; Return to group mode buffer.
5407         (when (eq mode 'gnus-summary-mode)
5408           (gnus-kill-buffer buf)))
5409       (setq gnus-current-select-method gnus-select-method)
5410       (pop-to-buffer gnus-group-buffer)
5411       (if (not quit-config)
5412           (progn
5413             (goto-char group-point)
5414             (gnus-configure-windows 'group 'force))
5415         (gnus-handle-ephemeral-exit quit-config))
5416       ;; Clear the current group name.
5417       (unless quit-config
5418         (setq gnus-newsgroup-name nil)))))
5419
5420 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5421 (defun gnus-summary-exit-no-update (&optional no-questions)
5422   "Quit reading current newsgroup without updating read article info."
5423   (interactive)
5424   (let* ((group gnus-newsgroup-name)
5425          (quit-config (gnus-group-quit-config group)))
5426     (when (or no-questions
5427               gnus-expert-user
5428               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5429       (gnus-async-halt-prefetch)
5430       (mapcar 'funcall
5431               (delq 'gnus-summary-expire-articles
5432                     (copy-sequence gnus-summary-prepare-exit-hook)))
5433       (when (gnus-buffer-live-p gnus-article-buffer)
5434         (save-excursion
5435           (set-buffer gnus-article-buffer)
5436           (mm-destroy-parts gnus-article-mime-handles)))
5437       ;; If we have several article buffers, we kill them at exit.
5438       (unless gnus-single-article-buffer
5439         (gnus-kill-buffer gnus-article-buffer)
5440         (gnus-kill-buffer gnus-original-article-buffer)
5441         (setq gnus-article-current nil))
5442       (if (not gnus-kill-summary-on-exit)
5443           (gnus-deaden-summary)
5444         (gnus-close-group group)
5445         (gnus-summary-clear-local-variables)
5446         (set-buffer gnus-group-buffer)
5447         (gnus-summary-clear-local-variables)
5448         (when (get-buffer gnus-summary-buffer)
5449           (kill-buffer gnus-summary-buffer)))
5450       (unless gnus-single-article-buffer
5451         (setq gnus-article-current nil))
5452       (when gnus-use-trees
5453         (gnus-tree-close group))
5454       (gnus-async-prefetch-remove-group group)
5455       (when (get-buffer gnus-article-buffer)
5456         (bury-buffer gnus-article-buffer))
5457       ;; Return to the group buffer.
5458       (gnus-configure-windows 'group 'force)
5459       ;; Clear the current group name.
5460       (setq gnus-newsgroup-name nil)
5461       (when (equal (gnus-group-group-name) group)
5462         (gnus-group-next-unread-group 1))
5463       (when quit-config
5464         (gnus-handle-ephemeral-exit quit-config)))))
5465
5466 (defun gnus-handle-ephemeral-exit (quit-config)
5467   "Handle movement when leaving an ephemeral group.
5468 The state which existed when entering the ephemeral is reset."
5469   (if (not (buffer-name (car quit-config)))
5470       (gnus-configure-windows 'group 'force)
5471     (set-buffer (car quit-config))
5472     (cond ((eq major-mode 'gnus-summary-mode)
5473            (gnus-set-global-variables))
5474           ((eq major-mode 'gnus-article-mode)
5475            (save-excursion
5476              ;; The `gnus-summary-buffer' variable may point
5477              ;; to the old summary buffer when using a single
5478              ;; article buffer.
5479              (unless (gnus-buffer-live-p gnus-summary-buffer)
5480                (set-buffer gnus-group-buffer))
5481              (set-buffer gnus-summary-buffer)
5482              (gnus-set-global-variables))))
5483     (if (or (eq (cdr quit-config) 'article)
5484             (eq (cdr quit-config) 'pick))
5485         (progn
5486           ;; The current article may be from the ephemeral group
5487           ;; thus it is best that we reload this article
5488           (gnus-summary-show-article)
5489           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5490               (gnus-configure-windows 'pick 'force)
5491             (gnus-configure-windows (cdr quit-config) 'force)))
5492       (gnus-configure-windows (cdr quit-config) 'force))
5493     (when (eq major-mode 'gnus-summary-mode)
5494       (gnus-summary-next-subject 1 nil t)
5495       (gnus-summary-recenter)
5496       (gnus-summary-position-point))))
5497
5498 ;;; Dead summaries.
5499
5500 (defvar gnus-dead-summary-mode-map nil)
5501
5502 (unless gnus-dead-summary-mode-map
5503   (setq gnus-dead-summary-mode-map (make-keymap))
5504   (suppress-keymap gnus-dead-summary-mode-map)
5505   (substitute-key-definition
5506    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5507   (let ((keys '("\C-d" "\r" "\177" [delete])))
5508     (while keys
5509       (define-key gnus-dead-summary-mode-map
5510         (pop keys) 'gnus-summary-wake-up-the-dead))))
5511
5512 (defvar gnus-dead-summary-mode nil
5513   "Minor mode for Gnus summary buffers.")
5514
5515 (defun gnus-dead-summary-mode (&optional arg)
5516   "Minor mode for Gnus summary buffers."
5517   (interactive "P")
5518   (when (eq major-mode 'gnus-summary-mode)
5519     (make-local-variable 'gnus-dead-summary-mode)
5520     (setq gnus-dead-summary-mode
5521           (if (null arg) (not gnus-dead-summary-mode)
5522             (> (prefix-numeric-value arg) 0)))
5523     (when gnus-dead-summary-mode
5524       (gnus-add-minor-mode
5525        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5526
5527 (defun gnus-deaden-summary ()
5528   "Make the current summary buffer into a dead summary buffer."
5529   ;; Kill any previous dead summary buffer.
5530   (when (and gnus-dead-summary
5531              (buffer-name gnus-dead-summary))
5532     (save-excursion
5533       (set-buffer gnus-dead-summary)
5534       (when gnus-dead-summary-mode
5535         (kill-buffer (current-buffer)))))
5536   ;; Make this the current dead summary.
5537   (setq gnus-dead-summary (current-buffer))
5538   (gnus-dead-summary-mode 1)
5539   (let ((name (buffer-name)))
5540     (when (string-match "Summary" name)
5541       (rename-buffer
5542        (concat (substring name 0 (match-beginning 0)) "Dead "
5543                (substring name (match-beginning 0)))
5544        t))))
5545
5546 (defun gnus-kill-or-deaden-summary (buffer)
5547   "Kill or deaden the summary BUFFER."
5548   (save-excursion
5549     (when (and (buffer-name buffer)
5550                (not gnus-single-article-buffer))
5551       (save-excursion
5552         (set-buffer buffer)
5553         (gnus-kill-buffer gnus-article-buffer)
5554         (gnus-kill-buffer gnus-original-article-buffer)))
5555     (cond (gnus-kill-summary-on-exit
5556            (when (and gnus-use-trees
5557                       (gnus-buffer-exists-p buffer))
5558              (save-excursion
5559                (set-buffer buffer)
5560                (gnus-tree-close gnus-newsgroup-name)))
5561            (gnus-kill-buffer buffer))
5562           ((gnus-buffer-exists-p buffer)
5563            (save-excursion
5564              (set-buffer buffer)
5565              (gnus-deaden-summary))))))
5566
5567 (defun gnus-summary-wake-up-the-dead (&rest args)
5568   "Wake up the dead summary buffer."
5569   (interactive)
5570   (gnus-dead-summary-mode -1)
5571   (let ((name (buffer-name)))
5572     (when (string-match "Dead " name)
5573       (rename-buffer
5574        (concat (substring name 0 (match-beginning 0))
5575                (substring name (match-end 0)))
5576        t)))
5577   (gnus-message 3 "This dead summary is now alive again"))
5578
5579 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5580 (defun gnus-summary-fetch-faq (&optional faq-dir)
5581   "Fetch the FAQ for the current group.
5582 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5583 in."
5584   (interactive
5585    (list
5586     (when current-prefix-arg
5587       (completing-read
5588        "Faq dir: " (and (listp gnus-group-faq-directory)
5589                         (mapcar (lambda (file) (list file))
5590                                 gnus-group-faq-directory))))))
5591   (let (gnus-faq-buffer)
5592     (when (setq gnus-faq-buffer
5593                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5594       (gnus-configure-windows 'summary-faq))))
5595
5596 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5597 (defun gnus-summary-describe-group (&optional force)
5598   "Describe the current newsgroup."
5599   (interactive "P")
5600   (gnus-group-describe-group force gnus-newsgroup-name))
5601
5602 (defun gnus-summary-describe-briefly ()
5603   "Describe summary mode commands briefly."
5604   (interactive)
5605   (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")))
5606
5607 ;; Walking around group mode buffer from summary mode.
5608
5609 (defun gnus-summary-next-group (&optional no-article target-group backward)
5610   "Exit current newsgroup and then select next unread newsgroup.
5611 If prefix argument NO-ARTICLE is non-nil, no article is selected
5612 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5613 previous group instead."
5614   (interactive "P")
5615   ;; Stop pre-fetching.
5616   (gnus-async-halt-prefetch)
5617   (let ((current-group gnus-newsgroup-name)
5618         (current-buffer (current-buffer))
5619         entered)
5620     ;; First we semi-exit this group to update Xrefs and all variables.
5621     ;; We can't do a real exit, because the window conf must remain
5622     ;; the same in case the user is prompted for info, and we don't
5623     ;; want the window conf to change before that...
5624     (gnus-summary-exit t)
5625     (while (not entered)
5626       ;; Then we find what group we are supposed to enter.
5627       (set-buffer gnus-group-buffer)
5628       (gnus-group-jump-to-group current-group)
5629       (setq target-group
5630             (or target-group
5631                 (if (eq gnus-keep-same-level 'best)
5632                     (gnus-summary-best-group gnus-newsgroup-name)
5633                   (gnus-summary-search-group backward gnus-keep-same-level))))
5634       (if (not target-group)
5635           ;; There are no further groups, so we return to the group
5636           ;; buffer.
5637           (progn
5638             (gnus-message 5 "Returning to the group buffer")
5639             (setq entered t)
5640             (when (gnus-buffer-live-p current-buffer)
5641               (set-buffer current-buffer)
5642               (gnus-summary-exit))
5643             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5644         ;; We try to enter the target group.
5645         (gnus-group-jump-to-group target-group)
5646         (let ((unreads (gnus-group-group-unread)))
5647           (if (and (or (eq t unreads)
5648                        (and unreads (not (zerop unreads))))
5649                    (gnus-summary-read-group
5650                     target-group nil no-article
5651                     (and (buffer-name current-buffer) current-buffer)
5652                     nil backward))
5653               (setq entered t)
5654             (setq current-group target-group
5655                   target-group nil)))))))
5656
5657 (defun gnus-summary-prev-group (&optional no-article)
5658   "Exit current newsgroup and then select previous unread newsgroup.
5659 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5660   (interactive "P")
5661   (gnus-summary-next-group no-article nil t))
5662
5663 ;; Walking around summary lines.
5664
5665 (defun gnus-summary-first-subject (&optional unread undownloaded)
5666   "Go to the first unread subject.
5667 If UNREAD is non-nil, go to the first unread article.
5668 Returns the article selected or nil if there are no unread articles."
5669   (interactive "P")
5670   (prog1
5671       (cond
5672        ;; Empty summary.
5673        ((null gnus-newsgroup-data)
5674         (gnus-message 3 "No articles in the group")
5675         nil)
5676        ;; Pick the first article.
5677        ((not unread)
5678         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5679         (gnus-data-number (car gnus-newsgroup-data)))
5680        ;; No unread articles.
5681        ((null gnus-newsgroup-unreads)
5682         (gnus-message 3 "No more unread articles")
5683         nil)
5684        ;; Find the first unread article.
5685        (t
5686         (let ((data gnus-newsgroup-data))
5687           (while (and data
5688                       (and (not (and undownloaded
5689                                      (eq gnus-undownloaded-mark
5690                                          (gnus-data-mark (car data)))))
5691                            (not (gnus-data-unread-p (car data)))))
5692             (setq data (cdr data)))
5693           (when data
5694             (goto-char (gnus-data-pos (car data)))
5695             (gnus-data-number (car data))))))
5696     (gnus-summary-position-point)))
5697
5698 (defun gnus-summary-next-subject (n &optional unread dont-display)
5699   "Go to next N'th summary line.
5700 If N is negative, go to the previous N'th subject line.
5701 If UNREAD is non-nil, only unread articles are selected.
5702 The difference between N and the actual number of steps taken is
5703 returned."
5704   (interactive "p")
5705   (let ((backward (< n 0))
5706         (n (abs n)))
5707     (while (and (> n 0)
5708                 (if backward
5709                     (gnus-summary-find-prev unread)
5710                   (gnus-summary-find-next unread)))
5711       (unless (zerop (setq n (1- n)))
5712         (gnus-summary-show-thread)))
5713     (when (/= 0 n)
5714       (gnus-message 7 "No more%s articles"
5715                     (if unread " unread" "")))
5716     (unless dont-display
5717       (gnus-summary-recenter)
5718       (gnus-summary-position-point))
5719     n))
5720
5721 (defun gnus-summary-next-unread-subject (n)
5722   "Go to next N'th unread summary line."
5723   (interactive "p")
5724   (gnus-summary-next-subject n t))
5725
5726 (defun gnus-summary-prev-subject (n &optional unread)
5727   "Go to previous N'th summary line.
5728 If optional argument UNREAD is non-nil, only unread article is selected."
5729   (interactive "p")
5730   (gnus-summary-next-subject (- n) unread))
5731
5732 (defun gnus-summary-prev-unread-subject (n)
5733   "Go to previous N'th unread summary line."
5734   (interactive "p")
5735   (gnus-summary-next-subject (- n) t))
5736
5737 (defun gnus-summary-goto-subject (article &optional force silent)
5738   "Go the subject line of ARTICLE.
5739 If FORCE, also allow jumping to articles not currently shown."
5740   (interactive "nArticle number: ")
5741   (let ((b (point))
5742         (data (gnus-data-find article)))
5743     ;; We read in the article if we have to.
5744     (and (not data)
5745          force
5746          (gnus-summary-insert-subject
5747           article
5748           (if (or (numberp force) (vectorp force)) force)
5749           t)
5750          (setq data (gnus-data-find article)))
5751     (goto-char b)
5752     (if (not data)
5753         (progn
5754           (unless silent
5755             (gnus-message 3 "Can't find article %d" article))
5756           nil)
5757       (goto-char (gnus-data-pos data))
5758       (gnus-summary-position-point)
5759       article)))
5760
5761 ;; Walking around summary lines with displaying articles.
5762
5763 (defun gnus-summary-expand-window (&optional arg)
5764   "Make the summary buffer take up the entire Emacs frame.
5765 Given a prefix, will force an `article' buffer configuration."
5766   (interactive "P")
5767   (if arg
5768       (gnus-configure-windows 'article 'force)
5769     (gnus-configure-windows 'summary 'force)))
5770
5771 (defun gnus-summary-display-article (article &optional all-header)
5772   "Display ARTICLE in article buffer."
5773   (gnus-set-global-variables)
5774   (if (null article)
5775       nil
5776     (prog1
5777         (if gnus-summary-display-article-function
5778             (funcall gnus-summary-display-article-function article all-header)
5779           (gnus-article-prepare article all-header))
5780       (gnus-run-hooks 'gnus-select-article-hook)
5781       (when (and gnus-current-article
5782                  (not (zerop gnus-current-article)))
5783         (gnus-summary-goto-subject gnus-current-article))
5784       (gnus-summary-recenter)
5785       (when (and gnus-use-trees gnus-show-threads)
5786         (gnus-possibly-generate-tree article)
5787         (gnus-highlight-selected-tree article))
5788       ;; Successfully display article.
5789       (gnus-article-set-window-start
5790        (cdr (assq article gnus-newsgroup-bookmarks))))))
5791
5792 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5793   "Select the current article.
5794 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5795 non-nil, the article will be re-fetched even if it already present in
5796 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5797 be displayed."
5798   ;; Make sure we are in the summary buffer to work around bbdb bug.
5799   (unless (eq major-mode 'gnus-summary-mode)
5800     (set-buffer gnus-summary-buffer))
5801   (let ((article (or article (gnus-summary-article-number)))
5802         (all-headers (not (not all-headers))) ;Must be T or NIL.
5803         gnus-summary-display-article-function
5804         did)
5805     (and (not pseudo)
5806          (gnus-summary-article-pseudo-p article)
5807          (error "This is a pseudo-article"))
5808     (prog1
5809         (save-excursion
5810           (set-buffer gnus-summary-buffer)
5811           (if (or (and gnus-single-article-buffer
5812                        (or (null gnus-current-article)
5813                            (null gnus-article-current)
5814                            (null (get-buffer gnus-article-buffer))
5815                            (not (eq article (cdr gnus-article-current)))
5816                            (not (equal (car gnus-article-current)
5817                                        gnus-newsgroup-name))))
5818                   (and (not gnus-single-article-buffer)
5819                        (or (null gnus-current-article)
5820                            (not (eq gnus-current-article article))))
5821                   force)
5822               ;; The requested article is different from the current article.
5823               (prog1
5824                   (gnus-summary-display-article article all-headers)
5825                 (setq did article)
5826                 (when (or all-headers gnus-show-all-headers)
5827                   (gnus-article-show-all-headers)))
5828             (when (or all-headers gnus-show-all-headers)
5829               (gnus-article-show-all-headers))
5830             'old))
5831       (when did
5832         (gnus-article-set-window-start
5833          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5834
5835 (defun gnus-summary-set-current-mark (&optional current-mark)
5836   "Obsolete function."
5837   nil)
5838
5839 (defun gnus-summary-next-article (&optional unread subject backward push)
5840   "Select the next article.
5841 If UNREAD, only unread articles are selected.
5842 If SUBJECT, only articles with SUBJECT are selected.
5843 If BACKWARD, the previous article is selected instead of the next."
5844   (interactive "P")
5845   (cond
5846    ;; Is there such an article?
5847    ((and (gnus-summary-search-forward unread subject backward)
5848          (or (gnus-summary-display-article (gnus-summary-article-number))
5849              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5850     (gnus-summary-position-point))
5851    ;; If not, we try the first unread, if that is wanted.
5852    ((and subject
5853          gnus-auto-select-same
5854          (gnus-summary-first-unread-article))
5855     (gnus-summary-position-point)
5856     (gnus-message 6 "Wrapped"))
5857    ;; Try to get next/previous article not displayed in this group.
5858    ((and gnus-auto-extend-newsgroup
5859          (not unread) (not subject))
5860     (gnus-summary-goto-article
5861      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5862      nil (count-lines (point-min) (point))))
5863    ;; Go to next/previous group.
5864    (t
5865     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5866       (gnus-summary-jump-to-group gnus-newsgroup-name))
5867     (let ((cmd last-command-char)
5868           (point
5869            (save-excursion
5870              (set-buffer gnus-group-buffer)
5871              (point)))
5872           (group
5873            (if (eq gnus-keep-same-level 'best)
5874                (gnus-summary-best-group gnus-newsgroup-name)
5875              (gnus-summary-search-group backward gnus-keep-same-level))))
5876       ;; For some reason, the group window gets selected.  We change
5877       ;; it back.
5878       (select-window (get-buffer-window (current-buffer)))
5879       ;; Select next unread newsgroup automagically.
5880       (cond
5881        ((or (not gnus-auto-select-next)
5882             (not cmd))
5883         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5884        ((or (eq gnus-auto-select-next 'quietly)
5885             (and (eq gnus-auto-select-next 'slightly-quietly)
5886                  push)
5887             (and (eq gnus-auto-select-next 'almost-quietly)
5888                  (gnus-summary-last-article-p)))
5889         ;; Select quietly.
5890         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5891             (gnus-summary-exit)
5892           (gnus-message 7 "No more%s articles (%s)..."
5893                         (if unread " unread" "")
5894                         (if group (concat "selecting " group)
5895                           "exiting"))
5896           (gnus-summary-next-group nil group backward)))
5897        (t
5898         (when (gnus-key-press-event-p last-input-event)
5899           (gnus-summary-walk-group-buffer
5900            gnus-newsgroup-name cmd unread backward point))))))))
5901
5902 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5903   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5904                       (?\C-p (gnus-group-prev-unread-group 1))))
5905         (cursor-in-echo-area t)
5906         keve key group ended)
5907     (save-excursion
5908       (set-buffer gnus-group-buffer)
5909       (goto-char start)
5910       (setq group
5911             (if (eq gnus-keep-same-level 'best)
5912                 (gnus-summary-best-group gnus-newsgroup-name)
5913               (gnus-summary-search-group backward gnus-keep-same-level))))
5914     (while (not ended)
5915       (gnus-message
5916        5 "No more%s articles%s" (if unread " unread" "")
5917        (if (and group
5918                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5919            (format " (Type %s for %s [%s])"
5920                    (single-key-description cmd) group
5921                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5922          (format " (Type %s to exit %s)"
5923                  (single-key-description cmd)
5924                  gnus-newsgroup-name)))
5925       ;; Confirm auto selection.
5926       (setq key (car (setq keve (gnus-read-event-char))))
5927       (setq ended t)
5928       (cond
5929        ((assq key keystrokes)
5930         (let ((obuf (current-buffer)))
5931           (switch-to-buffer gnus-group-buffer)
5932           (when group
5933             (gnus-group-jump-to-group group))
5934           (eval (cadr (assq key keystrokes)))
5935           (setq group (gnus-group-group-name))
5936           (switch-to-buffer obuf))
5937         (setq ended nil))
5938        ((equal key cmd)
5939         (if (or (not group)
5940                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5941             (gnus-summary-exit)
5942           (gnus-summary-next-group nil group backward)))
5943        (t
5944         (push (cdr keve) unread-command-events))))))
5945
5946 (defun gnus-summary-next-unread-article ()
5947   "Select unread article after current one."
5948   (interactive)
5949   (gnus-summary-next-article
5950    (or (not (eq gnus-summary-goto-unread 'never))
5951        (gnus-summary-last-article-p (gnus-summary-article-number)))
5952    (and gnus-auto-select-same
5953         (gnus-summary-article-subject))))
5954
5955 (defun gnus-summary-prev-article (&optional unread subject)
5956   "Select the article after the current one.
5957 If UNREAD is non-nil, only unread articles are selected."
5958   (interactive "P")
5959   (gnus-summary-next-article unread subject t))
5960
5961 (defun gnus-summary-prev-unread-article ()
5962   "Select unread article before current one."
5963   (interactive)
5964   (gnus-summary-prev-article
5965    (or (not (eq gnus-summary-goto-unread 'never))
5966        (gnus-summary-first-article-p (gnus-summary-article-number)))
5967    (and gnus-auto-select-same
5968         (gnus-summary-article-subject))))
5969
5970 (defun gnus-summary-next-page (&optional lines circular)
5971   "Show next page of the selected article.
5972 If at the end of the current article, select the next article.
5973 LINES says how many lines should be scrolled up.
5974
5975 If CIRCULAR is non-nil, go to the start of the article instead of
5976 selecting the next article when reaching the end of the current
5977 article."
5978   (interactive "P")
5979   (setq gnus-summary-buffer (current-buffer))
5980   (gnus-set-global-variables)
5981   (let ((article (gnus-summary-article-number))
5982         (article-window (get-buffer-window gnus-article-buffer t))
5983         endp)
5984     ;; If the buffer is empty, we have no article.
5985     (unless article
5986       (error "No article to select"))
5987     (gnus-configure-windows 'article)
5988     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5989         (if (and (eq gnus-summary-goto-unread 'never)
5990                  (not (gnus-summary-last-article-p article)))
5991             (gnus-summary-next-article)
5992           (gnus-summary-next-unread-article))
5993       (if (or (null gnus-current-article)
5994               (null gnus-article-current)
5995               (/= article (cdr gnus-article-current))
5996               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5997           ;; Selected subject is different from current article's.
5998           (gnus-summary-display-article article)
5999         (when article-window
6000           (gnus-eval-in-buffer-window gnus-article-buffer
6001             (setq endp (gnus-article-next-page lines)))
6002           (when endp
6003             (cond (circular
6004                    (gnus-summary-beginning-of-article))
6005                   (lines
6006                    (gnus-message 3 "End of message"))
6007                   ((null lines)
6008                    (if (and (eq gnus-summary-goto-unread 'never)
6009                             (not (gnus-summary-last-article-p article)))
6010                        (gnus-summary-next-article)
6011                      (gnus-summary-next-unread-article))))))))
6012     (gnus-summary-recenter)
6013     (gnus-summary-position-point)))
6014
6015 (defun gnus-summary-prev-page (&optional lines move)
6016   "Show previous page of selected article.
6017 Argument LINES specifies lines to be scrolled down.
6018 If MOVE, move to the previous unread article if point is at
6019 the beginning of the buffer."
6020   (interactive "P")
6021   (let ((article (gnus-summary-article-number))
6022         (article-window (get-buffer-window gnus-article-buffer t))
6023         endp)
6024     (gnus-configure-windows 'article)
6025     (if (or (null gnus-current-article)
6026             (null gnus-article-current)
6027             (/= article (cdr gnus-article-current))
6028             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6029         ;; Selected subject is different from current article's.
6030         (gnus-summary-display-article article)
6031       (gnus-summary-recenter)
6032       (when article-window
6033         (gnus-eval-in-buffer-window gnus-article-buffer
6034           (setq endp (gnus-article-prev-page lines)))
6035         (when (and move endp)
6036           (cond (lines
6037                  (gnus-message 3 "Beginning of message"))
6038                 ((null lines)
6039                  (if (and (eq gnus-summary-goto-unread 'never)
6040                           (not (gnus-summary-first-article-p article)))
6041                      (gnus-summary-prev-article)
6042                    (gnus-summary-prev-unread-article))))))))
6043   (gnus-summary-position-point))
6044
6045 (defun gnus-summary-prev-page-or-article (&optional lines)
6046   "Show previous page of selected article.
6047 Argument LINES specifies lines to be scrolled down.
6048 If at the beginning of the article, go to the next article."
6049   (interactive "P")
6050   (gnus-summary-prev-page lines t))
6051
6052 (defun gnus-summary-scroll-up (lines)
6053   "Scroll up (or down) one line current article.
6054 Argument LINES specifies lines to be scrolled up (or down if negative)."
6055   (interactive "p")
6056   (gnus-configure-windows 'article)
6057   (gnus-summary-show-thread)
6058   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6059     (gnus-eval-in-buffer-window gnus-article-buffer
6060       (cond ((> lines 0)
6061              (when (gnus-article-next-page lines)
6062                (gnus-message 3 "End of message")))
6063             ((< lines 0)
6064              (gnus-article-prev-page (- lines))))))
6065   (gnus-summary-recenter)
6066   (gnus-summary-position-point))
6067
6068 (defun gnus-summary-scroll-down (lines)
6069   "Scroll down (or up) one line current article.
6070 Argument LINES specifies lines to be scrolled down (or up if negative)."
6071   (interactive "p")
6072   (gnus-summary-scroll-up (- lines)))
6073
6074 (defun gnus-summary-next-same-subject ()
6075   "Select next article which has the same subject as current one."
6076   (interactive)
6077   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6078
6079 (defun gnus-summary-prev-same-subject ()
6080   "Select previous article which has the same subject as current one."
6081   (interactive)
6082   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6083
6084 (defun gnus-summary-next-unread-same-subject ()
6085   "Select next unread article which has the same subject as current one."
6086   (interactive)
6087   (gnus-summary-next-article t (gnus-summary-article-subject)))
6088
6089 (defun gnus-summary-prev-unread-same-subject ()
6090   "Select previous unread article which has the same subject as current one."
6091   (interactive)
6092   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6093
6094 (defun gnus-summary-first-unread-article ()
6095   "Select the first unread article.
6096 Return nil if there are no unread articles."
6097   (interactive)
6098   (prog1
6099       (when (gnus-summary-first-subject t)
6100         (gnus-summary-show-thread)
6101         (gnus-summary-first-subject t)
6102         (gnus-summary-display-article (gnus-summary-article-number)))
6103     (gnus-summary-position-point)))
6104
6105 (defun gnus-summary-first-unread-subject ()
6106   "Place the point on the subject line of the first unread article.
6107 Return nil if there are no unread articles."
6108   (interactive)
6109   (prog1
6110       (when (gnus-summary-first-subject t)
6111         (gnus-summary-show-thread)
6112         (gnus-summary-first-subject t))
6113     (gnus-summary-position-point)))
6114
6115 (defun gnus-summary-first-article ()
6116   "Select the first article.
6117 Return nil if there are no articles."
6118   (interactive)
6119   (prog1
6120       (when (gnus-summary-first-subject)
6121         (gnus-summary-show-thread)
6122         (gnus-summary-first-subject)
6123         (gnus-summary-display-article (gnus-summary-article-number)))
6124     (gnus-summary-position-point)))
6125
6126 (defun gnus-summary-best-unread-article ()
6127   "Select the unread article with the highest score."
6128   (interactive)
6129   (let ((best -1000000)
6130         (data gnus-newsgroup-data)
6131         article score)
6132     (while data
6133       (and (gnus-data-unread-p (car data))
6134            (> (setq score
6135                     (gnus-summary-article-score (gnus-data-number (car data))))
6136               best)
6137            (setq best score
6138                  article (gnus-data-number (car data))))
6139       (setq data (cdr data)))
6140     (prog1
6141         (if article
6142             (gnus-summary-goto-article article)
6143           (error "No unread articles"))
6144       (gnus-summary-position-point))))
6145
6146 (defun gnus-summary-last-subject ()
6147   "Go to the last displayed subject line in the group."
6148   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6149     (when article
6150       (gnus-summary-goto-subject article))))
6151
6152 (defun gnus-summary-goto-article (article &optional all-headers force)
6153   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6154 If ALL-HEADERS is non-nil, no header lines are hidden.
6155 If FORCE, go to the article even if it isn't displayed.  If FORCE
6156 is a number, it is the line the article is to be displayed on."
6157   (interactive
6158    (list
6159     (completing-read
6160      "Article number or Message-ID: "
6161      (mapcar (lambda (number) (list (int-to-string number)))
6162              gnus-newsgroup-limit))
6163     current-prefix-arg
6164     t))
6165   (prog1
6166       (if (and (stringp article)
6167                (string-match "@" article))
6168           (gnus-summary-refer-article article)
6169         (when (stringp article)
6170           (setq article (string-to-number article)))
6171         (if (gnus-summary-goto-subject article force)
6172             (gnus-summary-display-article article all-headers)
6173           (gnus-message 4 "Couldn't go to article %s" article) nil))
6174     (gnus-summary-position-point)))
6175
6176 (defun gnus-summary-goto-last-article ()
6177   "Go to the previously read article."
6178   (interactive)
6179   (prog1
6180       (when gnus-last-article
6181         (gnus-summary-goto-article gnus-last-article nil t))
6182     (gnus-summary-position-point)))
6183
6184 (defun gnus-summary-pop-article (number)
6185   "Pop one article off the history and go to the previous.
6186 NUMBER articles will be popped off."
6187   (interactive "p")
6188   (let (to)
6189     (setq gnus-newsgroup-history
6190           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6191     (if to
6192         (gnus-summary-goto-article (car to) nil t)
6193       (error "Article history empty")))
6194   (gnus-summary-position-point))
6195
6196 ;; Summary commands and functions for limiting the summary buffer.
6197
6198 (defun gnus-summary-limit-to-articles (n)
6199   "Limit the summary buffer to the next N articles.
6200 If not given a prefix, use the process marked articles instead."
6201   (interactive "P")
6202   (prog1
6203       (let ((articles (gnus-summary-work-articles n)))
6204         (setq gnus-newsgroup-processable nil)
6205         (gnus-summary-limit articles))
6206     (gnus-summary-position-point)))
6207
6208 (defun gnus-summary-pop-limit (&optional total)
6209   "Restore the previous limit.
6210 If given a prefix, remove all limits."
6211   (interactive "P")
6212   (when total
6213     (setq gnus-newsgroup-limits
6214           (list (mapcar (lambda (h) (mail-header-number h))
6215                         gnus-newsgroup-headers))))
6216   (unless gnus-newsgroup-limits
6217     (error "No limit to pop"))
6218   (prog1
6219       (gnus-summary-limit nil 'pop)
6220     (gnus-summary-position-point)))
6221
6222 (defun gnus-summary-limit-to-subject (subject &optional header)
6223   "Limit the summary buffer to articles that have subjects that match a regexp."
6224   (interactive "sLimit to subject (regexp): ")
6225   (unless header
6226     (setq header "subject"))
6227   (when (not (equal "" subject))
6228     (prog1
6229         (let ((articles (gnus-summary-find-matching
6230                          (or header "subject") subject 'all)))
6231           (unless articles
6232             (error "Found no matches for \"%s\"" subject))
6233           (gnus-summary-limit articles))
6234       (gnus-summary-position-point))))
6235
6236 (defun gnus-summary-limit-to-author (from)
6237   "Limit the summary buffer to articles that have authors that match a regexp."
6238   (interactive "sLimit to author (regexp): ")
6239   (gnus-summary-limit-to-subject from "from"))
6240
6241 (defun gnus-summary-limit-to-age (age &optional younger-p)
6242   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6243 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6244 articles that are younger than AGE days."
6245   (interactive
6246    (let ((younger current-prefix-arg)
6247          (days-got nil)
6248          days)
6249      (while (not days-got)
6250        (setq days (if younger
6251                       (read-string "Limit to articles within (in days): ")
6252                     (read-string "Limit to articles old than (in days): ")))
6253        (when (> (length days) 0)
6254          (setq days (read days)))
6255        (if (numberp days)
6256            (setq days-got t)
6257          (message "Please enter a number.")
6258          (sleep-for 1)))
6259      (list days younger)))
6260   (prog1
6261       (let ((data gnus-newsgroup-data)
6262             (cutoff (days-to-time age))
6263             articles d date is-younger)
6264         (while (setq d (pop data))
6265           (when (and (vectorp (gnus-data-header d))
6266                      (setq date (mail-header-date (gnus-data-header d))))
6267             (setq is-younger (time-less-p
6268                               (time-since (condition-case ()
6269                                               (date-to-time date)
6270                                             (error '(0 0))))
6271                               cutoff))
6272             (when (if younger-p
6273                       is-younger
6274                     (not is-younger))
6275               (push (gnus-data-number d) articles))))
6276         (gnus-summary-limit (nreverse articles)))
6277     (gnus-summary-position-point)))
6278
6279 (defun gnus-summary-limit-to-extra (header regexp)
6280   "Limit the summary buffer to articles that match an 'extra' header."
6281   (interactive
6282    (let ((header
6283           (intern
6284            (gnus-completing-read
6285             (symbol-name (car gnus-extra-headers))      
6286             "Limit extra header:"       
6287             (mapcar (lambda (x) 
6288                       (cons (symbol-name x) x))
6289                     gnus-extra-headers)
6290             nil                 
6291             t))))
6292      (list header
6293            (read-string (format "Limit to header %s (regexp): " header)))))
6294   (when (not (equal "" regexp))
6295     (prog1
6296         (let ((articles (gnus-summary-find-matching
6297                          (cons 'extra header) regexp 'all)))
6298           (unless articles
6299             (error "Found no matches for \"%s\"" regexp))
6300           (gnus-summary-limit articles))
6301       (gnus-summary-position-point))))
6302
6303 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6304 (make-obsolete
6305  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6306
6307 (defun gnus-summary-limit-to-unread (&optional all)
6308   "Limit the summary buffer to articles that are not marked as read.
6309 If ALL is non-nil, limit strictly to unread articles."
6310   (interactive "P")
6311   (if all
6312       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6313     (gnus-summary-limit-to-marks
6314      ;; Concat all the marks that say that an article is read and have
6315      ;; those removed.
6316      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6317            gnus-killed-mark gnus-kill-file-mark
6318            gnus-low-score-mark gnus-expirable-mark
6319            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6320            gnus-duplicate-mark gnus-souped-mark)
6321      'reverse)))
6322
6323 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6324 (make-obsolete 'gnus-summary-delete-marked-with
6325                'gnus-summary-limit-exlude-marks)
6326
6327 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6328   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6329 If REVERSE, limit the summary buffer to articles that are marked
6330 with MARKS.  MARKS can either be a string of marks or a list of marks.
6331 Returns how many articles were removed."
6332   (interactive "sMarks: ")
6333   (gnus-summary-limit-to-marks marks t))
6334
6335 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6336   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6337 If REVERSE (the prefix), limit the summary buffer to articles that are
6338 not marked with MARKS.  MARKS can either be a string of marks or a
6339 list of marks.
6340 Returns how many articles were removed."
6341   (interactive "sMarks: \nP")
6342   (prog1
6343       (let ((data gnus-newsgroup-data)
6344             (marks (if (listp marks) marks
6345                      (append marks nil))) ; Transform to list.
6346             articles)
6347         (while data
6348           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6349                   (memq (gnus-data-mark (car data)) marks))
6350             (push (gnus-data-number (car data)) articles))
6351           (setq data (cdr data)))
6352         (gnus-summary-limit articles))
6353     (gnus-summary-position-point)))
6354
6355 (defun gnus-summary-limit-to-score (&optional score)
6356   "Limit to articles with score at or above SCORE."
6357   (interactive "P")
6358   (setq score (if score
6359                   (prefix-numeric-value score)
6360                 (or gnus-summary-default-score 0)))
6361   (let ((data gnus-newsgroup-data)
6362         articles)
6363     (while data
6364       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6365                 score)
6366         (push (gnus-data-number (car data)) articles))
6367       (setq data (cdr data)))
6368     (prog1
6369         (gnus-summary-limit articles)
6370       (gnus-summary-position-point))))
6371
6372 (defun gnus-summary-limit-include-thread (id)
6373   "Display all the hidden articles that in the current thread."
6374   (interactive (list (mail-header-id (gnus-summary-article-header))))
6375   (let ((articles (gnus-articles-in-thread
6376                    (gnus-id-to-thread (gnus-root-id id)))))
6377     (prog1
6378         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6379       (gnus-summary-position-point))))
6380
6381 (defun gnus-summary-limit-include-dormant ()
6382   "Display all the hidden articles that are marked as dormant.
6383 Note that this command only works on a subset of the articles currently
6384 fetched for this group."
6385   (interactive)
6386   (unless gnus-newsgroup-dormant
6387     (error "There are no dormant articles in this group"))
6388   (prog1
6389       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6390     (gnus-summary-position-point)))
6391
6392 (defun gnus-summary-limit-exclude-dormant ()
6393   "Hide all dormant articles."
6394   (interactive)
6395   (prog1
6396       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6397     (gnus-summary-position-point)))
6398
6399 (defun gnus-summary-limit-exclude-childless-dormant ()
6400   "Hide all dormant articles that have no children."
6401   (interactive)
6402   (let ((data (gnus-data-list t))
6403         articles d children)
6404     ;; Find all articles that are either not dormant or have
6405     ;; children.
6406     (while (setq d (pop data))
6407       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6408                 (and (setq children
6409                            (gnus-article-children (gnus-data-number d)))
6410                      (let (found)
6411                        (while children
6412                          (when (memq (car children) articles)
6413                            (setq children nil
6414                                  found t))
6415                          (pop children))
6416                        found)))
6417         (push (gnus-data-number d) articles)))
6418     ;; Do the limiting.
6419     (prog1
6420         (gnus-summary-limit articles)
6421       (gnus-summary-position-point))))
6422
6423 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6424   "Mark all unread excluded articles as read.
6425 If ALL, mark even excluded ticked and dormants as read."
6426   (interactive "P")
6427   (let ((articles (gnus-sorted-complement
6428                    (sort
6429                     (mapcar (lambda (h) (mail-header-number h))
6430                             gnus-newsgroup-headers)
6431                     '<)
6432                    (sort gnus-newsgroup-limit '<)))
6433         article)
6434     (setq gnus-newsgroup-unreads
6435           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6436     (if all
6437         (setq gnus-newsgroup-dormant nil
6438               gnus-newsgroup-marked nil
6439               gnus-newsgroup-reads
6440               (nconc
6441                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6442                gnus-newsgroup-reads))
6443       (while (setq article (pop articles))
6444         (unless (or (memq article gnus-newsgroup-dormant)
6445                     (memq article gnus-newsgroup-marked))
6446           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6447
6448 (defun gnus-summary-limit (articles &optional pop)
6449   (if pop
6450       ;; We pop the previous limit off the stack and use that.
6451       (setq articles (car gnus-newsgroup-limits)
6452             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6453     ;; We use the new limit, so we push the old limit on the stack.
6454     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6455   ;; Set the limit.
6456   (setq gnus-newsgroup-limit articles)
6457   (let ((total (length gnus-newsgroup-data))
6458         (data (gnus-data-find-list (gnus-summary-article-number)))
6459         (gnus-summary-mark-below nil)   ; Inhibit this.
6460         found)
6461     ;; This will do all the work of generating the new summary buffer
6462     ;; according to the new limit.
6463     (gnus-summary-prepare)
6464     ;; Hide any threads, possibly.
6465     (and gnus-show-threads
6466          gnus-thread-hide-subtree
6467          (gnus-summary-hide-all-threads))
6468     ;; Try to return to the article you were at, or one in the
6469     ;; neighborhood.
6470     (when data
6471       ;; We try to find some article after the current one.
6472       (while data
6473         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6474           (setq data nil
6475                 found t))
6476         (setq data (cdr data))))
6477     (unless found
6478       ;; If there is no data, that means that we were after the last
6479       ;; article.  The same goes when we can't find any articles
6480       ;; after the current one.
6481       (goto-char (point-max))
6482       (gnus-summary-find-prev))
6483     (gnus-set-mode-line 'summary)
6484     ;; We return how many articles were removed from the summary
6485     ;; buffer as a result of the new limit.
6486     (- total (length gnus-newsgroup-data))))
6487
6488 (defsubst gnus-invisible-cut-children (threads)
6489   (let ((num 0))
6490     (while threads
6491       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6492         (incf num))
6493       (pop threads))
6494     (< num 2)))
6495
6496 (defsubst gnus-cut-thread (thread)
6497   "Go forwards in the thread until we find an article that we want to display."
6498   (when (or (eq gnus-fetch-old-headers 'some)
6499             (eq gnus-fetch-old-headers 'invisible)
6500             (numberp gnus-fetch-old-headers)
6501             (eq gnus-build-sparse-threads 'some)
6502             (eq gnus-build-sparse-threads 'more))
6503     ;; Deal with old-fetched headers and sparse threads.
6504     (while (and
6505             thread
6506             (or
6507              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6508              (gnus-summary-article-ancient-p
6509               (mail-header-number (car thread))))
6510             (if (or (<= (length (cdr thread)) 1)
6511                     (eq gnus-fetch-old-headers 'invisible))
6512                 (setq gnus-newsgroup-limit
6513                       (delq (mail-header-number (car thread))
6514                             gnus-newsgroup-limit)
6515                       thread (cadr thread))
6516               (when (gnus-invisible-cut-children (cdr thread))
6517                 (let ((th (cdr thread)))
6518                   (while th
6519                     (if (memq (mail-header-number (caar th))
6520                               gnus-newsgroup-limit)
6521                         (setq thread (car th)
6522                               th nil)
6523                       (setq th (cdr th))))))))))
6524   thread)
6525
6526 (defun gnus-cut-threads (threads)
6527   "Cut off all uninteresting articles from the beginning of threads."
6528   (when (or (eq gnus-fetch-old-headers 'some)
6529             (eq gnus-fetch-old-headers 'invisible)
6530             (numberp gnus-fetch-old-headers)
6531             (eq gnus-build-sparse-threads 'some)
6532             (eq gnus-build-sparse-threads 'more))
6533     (let ((th threads))
6534       (while th
6535         (setcar th (gnus-cut-thread (car th)))
6536         (setq th (cdr th)))))
6537   ;; Remove nixed out threads.
6538   (delq nil threads))
6539
6540 (defun gnus-summary-initial-limit (&optional show-if-empty)
6541   "Figure out what the initial limit is supposed to be on group entry.
6542 This entails weeding out unwanted dormants, low-scored articles,
6543 fetch-old-headers verbiage, and so on."
6544   ;; Most groups have nothing to remove.
6545   (if (or gnus-inhibit-limiting
6546           (and (null gnus-newsgroup-dormant)
6547                (not (eq gnus-fetch-old-headers 'some))
6548                (not (numberp gnus-fetch-old-headers))
6549                (not (eq gnus-fetch-old-headers 'invisible))
6550                (null gnus-summary-expunge-below)
6551                (not (eq gnus-build-sparse-threads 'some))
6552                (not (eq gnus-build-sparse-threads 'more))
6553                (null gnus-thread-expunge-below)
6554                (not gnus-use-nocem)))
6555       ()                                ; Do nothing.
6556     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6557     (setq gnus-newsgroup-limit nil)
6558     (mapatoms
6559      (lambda (node)
6560        (unless (car (symbol-value node))
6561          ;; These threads have no parents -- they are roots.
6562          (let ((nodes (cdr (symbol-value node)))
6563                thread)
6564            (while nodes
6565              (if (and gnus-thread-expunge-below
6566                       (< (gnus-thread-total-score (car nodes))
6567                          gnus-thread-expunge-below))
6568                  (gnus-expunge-thread (pop nodes))
6569                (setq thread (pop nodes))
6570                (gnus-summary-limit-children thread))))))
6571      gnus-newsgroup-dependencies)
6572     ;; If this limitation resulted in an empty group, we might
6573     ;; pop the previous limit and use it instead.
6574     (when (and (not gnus-newsgroup-limit)
6575                show-if-empty)
6576       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6577     gnus-newsgroup-limit))
6578
6579 (defun gnus-summary-limit-children (thread)
6580   "Return 1 if this subthread is visible and 0 if it is not."
6581   ;; First we get the number of visible children to this thread.  This
6582   ;; is done by recursing down the thread using this function, so this
6583   ;; will really go down to a leaf article first, before slowly
6584   ;; working its way up towards the root.
6585   (when thread
6586     (let ((children
6587            (if (cdr thread)
6588                (apply '+ (mapcar 'gnus-summary-limit-children
6589                                  (cdr thread)))
6590              0))
6591           (number (mail-header-number (car thread)))
6592           score)
6593       (if (and
6594            (not (memq number gnus-newsgroup-marked))
6595            (or
6596             ;; If this article is dormant and has absolutely no visible
6597             ;; children, then this article isn't visible.
6598             (and (memq number gnus-newsgroup-dormant)
6599                  (zerop children))
6600             ;; If this is "fetch-old-headered" and there is no
6601             ;; visible children, then we don't want this article.
6602             (and (or (eq gnus-fetch-old-headers 'some)
6603                      (numberp gnus-fetch-old-headers))
6604                  (gnus-summary-article-ancient-p number)
6605                  (zerop children))
6606             ;; If this is "fetch-old-headered" and `invisible', then
6607             ;; we don't want this article.
6608             (and (eq gnus-fetch-old-headers 'invisible)
6609                  (gnus-summary-article-ancient-p number))
6610             ;; If this is a sparsely inserted article with no children,
6611             ;; we don't want it.
6612             (and (eq gnus-build-sparse-threads 'some)
6613                  (gnus-summary-article-sparse-p number)
6614                  (zerop children))
6615             ;; If we use expunging, and this article is really
6616             ;; low-scored, then we don't want this article.
6617             (when (and gnus-summary-expunge-below
6618                        (< (setq score
6619                                 (or (cdr (assq number gnus-newsgroup-scored))
6620                                     gnus-summary-default-score))
6621                           gnus-summary-expunge-below))
6622               ;; We increase the expunge-tally here, but that has
6623               ;; nothing to do with the limits, really.
6624               (incf gnus-newsgroup-expunged-tally)
6625               ;; We also mark as read here, if that's wanted.
6626               (when (and gnus-summary-mark-below
6627                          (< score gnus-summary-mark-below))
6628                 (setq gnus-newsgroup-unreads
6629                       (delq number gnus-newsgroup-unreads))
6630                 (if gnus-newsgroup-auto-expire
6631                     (push number gnus-newsgroup-expirable)
6632                   (push (cons number gnus-low-score-mark)
6633                         gnus-newsgroup-reads)))
6634               t)
6635             ;; Check NoCeM things.
6636             (if (and gnus-use-nocem
6637                      (gnus-nocem-unwanted-article-p
6638                       (mail-header-id (car thread))))
6639                 (progn
6640                   (setq gnus-newsgroup-unreads
6641                         (delq number gnus-newsgroup-unreads))
6642                   t))))
6643           ;; Nope, invisible article.
6644           0
6645         ;; Ok, this article is to be visible, so we add it to the limit
6646         ;; and return 1.
6647         (push number gnus-newsgroup-limit)
6648         1))))
6649
6650 (defun gnus-expunge-thread (thread)
6651   "Mark all articles in THREAD as read."
6652   (let* ((number (mail-header-number (car thread))))
6653     (incf gnus-newsgroup-expunged-tally)
6654     ;; We also mark as read here, if that's wanted.
6655     (setq gnus-newsgroup-unreads
6656           (delq number gnus-newsgroup-unreads))
6657     (if gnus-newsgroup-auto-expire
6658         (push number gnus-newsgroup-expirable)
6659       (push (cons number gnus-low-score-mark)
6660             gnus-newsgroup-reads)))
6661   ;; Go recursively through all subthreads.
6662   (mapcar 'gnus-expunge-thread (cdr thread)))
6663
6664 ;; Summary article oriented commands
6665
6666 (defun gnus-summary-refer-parent-article (n)
6667   "Refer parent article N times.
6668 If N is negative, go to ancestor -N instead.
6669 The difference between N and the number of articles fetched is returned."
6670   (interactive "p")
6671   (let ((skip 1)
6672         error header ref)
6673     (when (not (natnump n))
6674       (setq skip (abs n)
6675             n 1))
6676     (while (and (> n 0)
6677                 (not error))
6678       (setq header (gnus-summary-article-header))
6679       (if (and (eq (mail-header-number header)
6680                    (cdr gnus-article-current))
6681                (equal gnus-newsgroup-name
6682                       (car gnus-article-current)))
6683           ;; If we try to find the parent of the currently
6684           ;; displayed article, then we take a look at the actual
6685           ;; References header, since this is slightly more
6686           ;; reliable than the References field we got from the
6687           ;; server.
6688           (save-excursion
6689             (set-buffer gnus-original-article-buffer)
6690             (nnheader-narrow-to-headers)
6691             (unless (setq ref (message-fetch-field "references"))
6692               (setq ref (message-fetch-field "in-reply-to")))
6693             (widen))
6694         (setq ref
6695               ;; It's not the current article, so we take a bet on
6696               ;; the value we got from the server.
6697               (mail-header-references header)))
6698       (if (and ref
6699                (not (equal ref "")))
6700           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6701             (gnus-message 1 "Couldn't find parent"))
6702         (gnus-message 1 "No references in article %d"
6703                       (gnus-summary-article-number))
6704         (setq error t))
6705       (decf n))
6706     (gnus-summary-position-point)
6707     n))
6708
6709 (defun gnus-summary-refer-references ()
6710   "Fetch all articles mentioned in the References header.
6711 Return the number of articles fetched."
6712   (interactive)
6713   (let ((ref (mail-header-references (gnus-summary-article-header)))
6714         (current (gnus-summary-article-number))
6715         (n 0))
6716     (if (or (not ref)
6717             (equal ref ""))
6718         (error "No References in the current article")
6719       ;; For each Message-ID in the References header...
6720       (while (string-match "<[^>]*>" ref)
6721         (incf n)
6722         ;; ... fetch that article.
6723         (gnus-summary-refer-article
6724          (prog1 (match-string 0 ref)
6725            (setq ref (substring ref (match-end 0))))))
6726       (gnus-summary-goto-subject current)
6727       (gnus-summary-position-point)
6728       n)))
6729
6730 (defun gnus-summary-refer-thread (&optional limit)
6731   "Fetch all articles in the current thread.
6732 If LIMIT (the numerical prefix), fetch that many old headers instead
6733 of what's specified by the `gnus-refer-thread-limit' variable."
6734   (interactive "P")
6735   (let ((id (mail-header-id (gnus-summary-article-header)))
6736         (limit (if limit (prefix-numeric-value limit)
6737                  gnus-refer-thread-limit)))
6738     ;; We want to fetch LIMIT *old* headers, but we also have to
6739     ;; re-fetch all the headers in the current buffer, because many of
6740     ;; them may be undisplayed.  So we adjust LIMIT.
6741     (when (numberp limit)
6742       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6743     (unless (eq gnus-fetch-old-headers 'invisible)
6744       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6745       ;; Retrieve the headers and read them in.
6746       (if (eq (gnus-retrieve-headers
6747                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6748               'nov)
6749           (gnus-build-all-threads)
6750         (error "Can't fetch thread from backends that don't support NOV"))
6751       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6752     (gnus-summary-limit-include-thread id)))
6753
6754 (defun gnus-summary-refer-article (message-id)
6755   "Fetch an article specified by MESSAGE-ID."
6756   (interactive "sMessage-ID: ")
6757   (when (and (stringp message-id)
6758              (not (zerop (length message-id))))
6759     ;; Construct the correct Message-ID if necessary.
6760     ;; Suggested by tale@pawl.rpi.edu.
6761     (unless (string-match "^<" message-id)
6762       (setq message-id (concat "<" message-id)))
6763     (unless (string-match ">$" message-id)
6764       (setq message-id (concat message-id ">")))
6765     (let* ((header (gnus-id-to-header message-id))
6766            (sparse (and header
6767                         (gnus-summary-article-sparse-p
6768                          (mail-header-number header))
6769                         (memq (mail-header-number header)
6770                               gnus-newsgroup-limit)))
6771            number)
6772       (cond
6773        ;; If the article is present in the buffer we just go to it.
6774        ((and header
6775              (or (not (gnus-summary-article-sparse-p
6776                        (mail-header-number header)))
6777                  sparse))
6778         (prog1
6779             (gnus-summary-goto-article
6780              (mail-header-number header) nil t)
6781           (when sparse
6782             (gnus-summary-update-article (mail-header-number header)))))
6783        (t
6784         ;; We fetch the article.
6785         (catch 'found
6786           (dolist (gnus-override-method (gnus-refer-article-methods))
6787             (gnus-check-server gnus-override-method)
6788             ;; Fetch the header, and display the article.
6789             (when (setq number (gnus-summary-insert-subject message-id))
6790               (gnus-summary-select-article nil nil nil number)
6791               (throw 'found t)))
6792           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6793
6794 (defun gnus-refer-article-methods ()
6795   "Return a list of referrable methods."
6796   (cond
6797    ;; No method, so we default to current and native.
6798    ((null gnus-refer-article-method)
6799     (list gnus-current-select-method gnus-select-method))
6800    ;; Current.
6801    ((eq 'current gnus-refer-article-method)
6802     (list gnus-current-select-method))
6803    ;; List of select methods.
6804    ((not (stringp (cadr gnus-refer-article-method)))
6805     (let (out)
6806       (dolist (method gnus-refer-article-method)
6807         (push (if (eq 'current method)
6808                   gnus-current-select-method
6809                 method)
6810               out))
6811       (nreverse out)))
6812    ;; One single select method.
6813    (t
6814     (list gnus-refer-article-method))))
6815
6816 (defun gnus-summary-edit-parameters ()
6817   "Edit the group parameters of the current group."
6818   (interactive)
6819   (gnus-group-edit-group gnus-newsgroup-name 'params))
6820
6821 (defun gnus-summary-customize-parameters ()
6822   "Customize the group parameters of the current group."
6823   (interactive)
6824   (gnus-group-customize gnus-newsgroup-name))
6825
6826 (defun gnus-summary-enter-digest-group (&optional force)
6827   "Enter an nndoc group based on the current article.
6828 If FORCE, force a digest interpretation.  If not, try
6829 to guess what the document format is."
6830   (interactive "P")
6831   (let ((conf gnus-current-window-configuration))
6832     (save-excursion
6833       (gnus-summary-select-article))
6834     (setq gnus-current-window-configuration conf)
6835     (let* ((name (format "%s-%d"
6836                          (gnus-group-prefixed-name
6837                           gnus-newsgroup-name (list 'nndoc ""))
6838                          (save-excursion
6839                            (set-buffer gnus-summary-buffer)
6840                            gnus-current-article)))
6841            (ogroup gnus-newsgroup-name)
6842            (params (append (gnus-info-params (gnus-get-info ogroup))
6843                            (list (cons 'to-group ogroup))
6844                            (list (cons 'save-article-group ogroup))))
6845            (case-fold-search t)
6846            (buf (current-buffer))
6847            dig to-address)
6848       (save-excursion
6849         (set-buffer gnus-original-article-buffer)
6850         ;; Have the digest group inherit the main mail address of
6851         ;; the parent article.
6852         (when (setq to-address (or (message-fetch-field "reply-to")
6853                                    (message-fetch-field "from")))
6854           (setq params (append (list (cons 'to-address to-address)))))
6855         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6856         (insert-buffer-substring gnus-original-article-buffer)
6857         ;; Remove lines that may lead nndoc to misinterpret the
6858         ;; document type.
6859         (narrow-to-region
6860          (goto-char (point-min))
6861          (or (search-forward "\n\n" nil t) (point)))
6862         (goto-char (point-min))
6863         (delete-matching-lines "^Path:\\|^From ")
6864         (widen))
6865       (unwind-protect
6866           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6867                     (gnus-newsgroup-ephemeral-ignored-charsets
6868                      gnus-newsgroup-ignored-charsets))
6869                 (gnus-group-read-ephemeral-group
6870                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6871                               (nndoc-article-type
6872                                ,(if force 'mbox 'guess))) t))
6873               ;; Make all postings to this group go to the parent group.
6874               (nconc (gnus-info-params (gnus-get-info name))
6875                      params)
6876             ;; Couldn't select this doc group.
6877             (switch-to-buffer buf)
6878             (gnus-set-global-variables)
6879             (gnus-configure-windows 'summary)
6880             (gnus-message 3 "Article couldn't be entered?"))
6881         (kill-buffer dig)))))
6882
6883 (defun gnus-summary-read-document (n)
6884   "Open a new group based on the current article(s).
6885 This will allow you to read digests and other similar
6886 documents as newsgroups.
6887 Obeys the standard process/prefix convention."
6888   (interactive "P")
6889   (let* ((articles (gnus-summary-work-articles n))
6890          (ogroup gnus-newsgroup-name)
6891          (params (append (gnus-info-params (gnus-get-info ogroup))
6892                          (list (cons 'to-group ogroup))))
6893          article group egroup groups vgroup)
6894     (while (setq article (pop articles))
6895       (setq group (format "%s-%d" gnus-newsgroup-name article))
6896       (gnus-summary-remove-process-mark article)
6897       (when (gnus-summary-display-article article)
6898         (save-excursion
6899           (with-temp-buffer
6900             (insert-buffer-substring gnus-original-article-buffer)
6901             ;; Remove some headers that may lead nndoc to make
6902             ;; the wrong guess.
6903             (message-narrow-to-head)
6904             (goto-char (point-min))
6905             (delete-matching-lines "^\\(Path\\):\\|^From ")
6906             (widen)
6907             (if (setq egroup
6908                       (gnus-group-read-ephemeral-group
6909                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6910                                      (nndoc-article-type guess))
6911                        t nil t))
6912                 (progn
6913                   ;; Make all postings to this group go to the parent group.
6914                   (nconc (gnus-info-params (gnus-get-info egroup))
6915                          params)
6916                   (push egroup groups))
6917               ;; Couldn't select this doc group.
6918               (gnus-error 3 "Article couldn't be entered"))))))
6919     ;; Now we have selected all the documents.
6920     (cond
6921      ((not groups)
6922       (error "None of the articles could be interpreted as documents"))
6923      ((gnus-group-read-ephemeral-group
6924        (setq vgroup (format
6925                      "nnvirtual:%s-%s" gnus-newsgroup-name
6926                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6927        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6928        t
6929        (cons (current-buffer) 'summary)))
6930      (t
6931       (error "Couldn't select virtual nndoc group")))))
6932
6933 (defun gnus-summary-isearch-article (&optional regexp-p)
6934   "Do incremental search forward on the current article.
6935 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6936   (interactive "P")
6937   (gnus-summary-select-article)
6938   (gnus-configure-windows 'article)
6939   (gnus-eval-in-buffer-window gnus-article-buffer
6940     (save-restriction
6941       (widen)
6942       (isearch-forward regexp-p))))
6943
6944 (defun gnus-summary-search-article-forward (regexp &optional backward)
6945   "Search for an article containing REGEXP forward.
6946 If BACKWARD, search backward instead."
6947   (interactive
6948    (list (read-string
6949           (format "Search article %s (regexp%s): "
6950                   (if current-prefix-arg "backward" "forward")
6951                   (if gnus-last-search-regexp
6952                       (concat ", default " gnus-last-search-regexp)
6953                     "")))
6954          current-prefix-arg))
6955   (if (string-equal regexp "")
6956       (setq regexp (or gnus-last-search-regexp ""))
6957     (setq gnus-last-search-regexp regexp))
6958   (if (gnus-summary-search-article regexp backward)
6959       (gnus-summary-show-thread)
6960     (error "Search failed: \"%s\"" regexp)))
6961
6962 (defun gnus-summary-search-article-backward (regexp)
6963   "Search for an article containing REGEXP backward."
6964   (interactive
6965    (list (read-string
6966           (format "Search article backward (regexp%s): "
6967                   (if gnus-last-search-regexp
6968                       (concat ", default " gnus-last-search-regexp)
6969                     "")))))
6970   (gnus-summary-search-article-forward regexp 'backward))
6971
6972 (defun gnus-summary-search-article (regexp &optional backward)
6973   "Search for an article containing REGEXP.
6974 Optional argument BACKWARD means do search for backward.
6975 `gnus-select-article-hook' is not called during the search."
6976   ;; We have to require this here to make sure that the following
6977   ;; dynamic binding isn't shadowed by autoloading.
6978   (require 'gnus-async)
6979   (require 'gnus-art)
6980   (let ((gnus-select-article-hook nil)  ;Disable hook.
6981         (gnus-article-prepare-hook nil)
6982         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6983         (gnus-use-article-prefetch nil)
6984         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6985         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6986         (sum (current-buffer))
6987         (gnus-display-mime-function nil)
6988         (found nil)
6989         point)
6990     (gnus-save-hidden-threads
6991       (gnus-summary-select-article)
6992       (set-buffer gnus-article-buffer)
6993       (when backward
6994         (forward-line -1))
6995       (while (not found)
6996         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6997         (if (if backward
6998                 (re-search-backward regexp nil t)
6999               (re-search-forward regexp nil t))
7000             ;; We found the regexp.
7001             (progn
7002               (setq found 'found)
7003               (beginning-of-line)
7004               (set-window-start
7005                (get-buffer-window (current-buffer))
7006                (point))
7007               (forward-line 1)
7008               (set-window-point
7009                (get-buffer-window (current-buffer))
7010                (point))
7011               (set-buffer sum)
7012               (setq point (point)))
7013           ;; We didn't find it, so we go to the next article.
7014           (set-buffer sum)
7015           (setq found 'not)
7016           (while (eq found 'not)
7017             (if (not (if backward (gnus-summary-find-prev)
7018                        (gnus-summary-find-next)))
7019                 ;; No more articles.
7020                 (setq found t)
7021               ;; Select the next article and adjust point.
7022               (unless (gnus-summary-article-sparse-p
7023                        (gnus-summary-article-number))
7024                 (setq found nil)
7025                 (gnus-summary-select-article)
7026                 (set-buffer gnus-article-buffer)
7027                 (widen)
7028                 (goto-char (if backward (point-max) (point-min))))))))
7029       (gnus-message 7 ""))
7030     ;; Return whether we found the regexp.
7031     (when (eq found 'found)
7032       (goto-char point)
7033       (gnus-summary-show-thread)
7034       (gnus-summary-goto-subject gnus-current-article)
7035       (gnus-summary-position-point)
7036       t)))
7037
7038 (defun gnus-summary-find-matching (header regexp &optional backward unread
7039                                           not-case-fold)
7040   "Return a list of all articles that match REGEXP on HEADER.
7041 The search stars on the current article and goes forwards unless
7042 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7043 If UNREAD is non-nil, only unread articles will
7044 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7045 in the comparisons."
7046   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7047                 (gnus-data-find-list
7048                  (gnus-summary-article-number) (gnus-data-list backward))))
7049         (case-fold-search (not not-case-fold))
7050         articles d func)
7051     (if (consp header)
7052         (if (eq (car header) 'extra)
7053             (setq func
7054                   `(lambda (h)
7055                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7056                          "")))
7057           (error "%s is an invalid header" header))
7058       (unless (fboundp (intern (concat "mail-header-" header)))
7059         (error "%s is not a valid header" header))
7060       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7061     (while data
7062       (setq d (car data))
7063       (and (or (not unread)             ; We want all articles...
7064                (gnus-data-unread-p d))  ; Or just unreads.
7065            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7066            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7067            (push (gnus-data-number d) articles)) ; Success!
7068       (setq data (cdr data)))
7069     (nreverse articles)))
7070
7071 (defun gnus-summary-execute-command (header regexp command &optional backward)
7072   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7073 If HEADER is an empty string (or nil), the match is done on the entire
7074 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7075   (interactive
7076    (list (let ((completion-ignore-case t))
7077            (completing-read
7078             "Header name: "
7079             (mapcar (lambda (string) (list string))
7080                     '("Number" "Subject" "From" "Lines" "Date"
7081                       "Message-ID" "Xref" "References" "Body"))
7082             nil 'require-match))
7083          (read-string "Regexp: ")
7084          (read-key-sequence "Command: ")
7085          current-prefix-arg))
7086   (when (equal header "Body")
7087     (setq header ""))
7088   ;; Hidden thread subtrees must be searched as well.
7089   (gnus-summary-show-all-threads)
7090   ;; We don't want to change current point nor window configuration.
7091   (save-excursion
7092     (save-window-excursion
7093       (gnus-message 6 "Executing %s..." (key-description command))
7094       ;; We'd like to execute COMMAND interactively so as to give arguments.
7095       (gnus-execute header regexp
7096                     `(call-interactively ',(key-binding command))
7097                     backward)
7098       (gnus-message 6 "Executing %s...done" (key-description command)))))
7099
7100 (defun gnus-summary-beginning-of-article ()
7101   "Scroll the article back to the beginning."
7102   (interactive)
7103   (gnus-summary-select-article)
7104   (gnus-configure-windows 'article)
7105   (gnus-eval-in-buffer-window gnus-article-buffer
7106     (widen)
7107     (goto-char (point-min))
7108     (when gnus-page-broken
7109       (gnus-narrow-to-page))))
7110
7111 (defun gnus-summary-end-of-article ()
7112   "Scroll to the end of the article."
7113   (interactive)
7114   (gnus-summary-select-article)
7115   (gnus-configure-windows 'article)
7116   (gnus-eval-in-buffer-window gnus-article-buffer
7117     (widen)
7118     (goto-char (point-max))
7119     (recenter -3)
7120     (when gnus-page-broken
7121       (gnus-narrow-to-page))))
7122
7123 (defun gnus-summary-print-article (&optional filename n)
7124   "Generate and print a PostScript image of the N next (mail) articles.
7125
7126 If N is negative, print the N previous articles.  If N is nil and articles
7127 have been marked with the process mark, print these instead.
7128
7129 If the optional first argument FILENAME is nil, send the image to the
7130 printer.  If FILENAME is a string, save the PostScript image in a file with
7131 that name.  If FILENAME is a number, prompt the user for the name of the file
7132 to save in."
7133   (interactive (list (ps-print-preprint current-prefix-arg)
7134                      current-prefix-arg))
7135   (dolist (article (gnus-summary-work-articles n))
7136     (gnus-summary-select-article nil nil 'pseudo article)
7137     (gnus-eval-in-buffer-window gnus-article-buffer
7138       (let ((buffer (generate-new-buffer " *print*")))
7139         (unwind-protect
7140             (progn
7141               (copy-to-buffer buffer (point-min) (point-max))
7142               (set-buffer buffer)
7143               (gnus-article-delete-invisible-text)
7144               (let ((ps-left-header
7145                      (list
7146                       (concat "("
7147                               (mail-header-subject gnus-current-headers) ")")
7148                       (concat "("
7149                               (mail-header-from gnus-current-headers) ")")))
7150                     (ps-right-header
7151                      (list
7152                       "/pagenumberstring load"
7153                       (concat "("
7154                               (mail-header-date gnus-current-headers) ")"))))
7155                 (gnus-run-hooks 'gnus-ps-print-hook)
7156                 (save-excursion
7157                   (ps-print-buffer-with-faces filename))))
7158           (kill-buffer buffer))))))
7159
7160 (defun gnus-summary-show-article (&optional arg)
7161   "Force re-fetching of the current article.
7162 If ARG (the prefix) is a number, show the article with the charset 
7163 defined in `gnus-summary-show-article-charset-alist', or the charset
7164 inputed.
7165 If ARG (the prefix) is non-nil and not a number, show the raw article 
7166 without any article massaging functions being run."
7167   (interactive "P")
7168   (cond 
7169    ((numberp arg)
7170     (let ((gnus-newsgroup-charset 
7171            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7172                (read-coding-system "Charset: ")))
7173           (gnus-newsgroup-ignored-charsets 'gnus-all))
7174       (gnus-summary-select-article nil 'force)))
7175    ((not arg)
7176     ;; Select the article the normal way.
7177     (gnus-summary-select-article nil 'force))
7178    (t
7179     ;; We have to require this here to make sure that the following
7180     ;; dynamic binding isn't shadowed by autoloading.
7181     (require 'gnus-async)
7182     (require 'gnus-art)
7183     ;; Bind the article treatment functions to nil.
7184     (let ((gnus-have-all-headers t)
7185           gnus-article-prepare-hook
7186           gnus-article-decode-hook
7187           gnus-display-mime-function
7188           gnus-break-pages)
7189       ;; Destroy any MIME parts.
7190       (when (gnus-buffer-live-p gnus-article-buffer)
7191         (save-excursion
7192           (set-buffer gnus-article-buffer)
7193           (mm-destroy-parts gnus-article-mime-handles)))
7194       (gnus-summary-select-article nil 'force))))
7195   (gnus-summary-goto-subject gnus-current-article)
7196   (gnus-summary-position-point))
7197
7198 (defun gnus-summary-verbose-headers (&optional arg)
7199   "Toggle permanent full header display.
7200 If ARG is a positive number, turn header display on.
7201 If ARG is a negative number, turn header display off."
7202   (interactive "P")
7203   (setq gnus-show-all-headers
7204         (cond ((or (not (numberp arg))
7205                    (zerop arg))
7206                (not gnus-show-all-headers))
7207               ((natnump arg)
7208                t)))
7209   (gnus-summary-show-article))
7210
7211 (defun gnus-summary-toggle-header (&optional arg)
7212   "Show the headers if they are hidden, or hide them if they are shown.
7213 If ARG is a positive number, show the entire header.
7214 If ARG is a negative number, hide the unwanted header lines."
7215   (interactive "P")
7216   (save-excursion
7217     (set-buffer gnus-article-buffer)
7218     (save-restriction
7219       (let* ((buffer-read-only nil)
7220              (inhibit-point-motion-hooks t)
7221              hidden e)
7222         (setq hidden
7223               (if (numberp arg)
7224                   (>= arg 0)
7225                 (save-restriction 
7226                   (article-narrow-to-head)
7227                   (gnus-article-hidden-text-p 'headers))))
7228         (goto-char (point-min))
7229         (when (search-forward "\n\n" nil t)
7230           (delete-region (point-min) (1- (point))))
7231         (goto-char (point-min))
7232         (save-excursion
7233           (set-buffer gnus-original-article-buffer)
7234           (goto-char (point-min))
7235           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7236         (insert-buffer-substring gnus-original-article-buffer 1 e)
7237         (save-restriction
7238           (narrow-to-region (point-min) (point))
7239           (article-decode-encoded-words)
7240           (if  hidden
7241               (let ((gnus-treat-hide-headers nil)
7242                     (gnus-treat-hide-boring-headers nil))
7243                 (gnus-treat-article 'head))
7244             (gnus-treat-article 'head)))))))
7245
7246 (defun gnus-summary-show-all-headers ()
7247   "Make all header lines visible."
7248   (interactive)
7249   (gnus-article-show-all-headers))
7250
7251 (defun gnus-summary-caesar-message (&optional arg)
7252   "Caesar rotate the current article by 13.
7253 The numerical prefix specifies how many places to rotate each letter
7254 forward."
7255   (interactive "P")
7256   (gnus-summary-select-article)
7257   (let ((mail-header-separator ""))
7258     (gnus-eval-in-buffer-window gnus-article-buffer
7259       (save-restriction
7260         (widen)
7261         (let ((start (window-start))
7262               buffer-read-only)
7263           (message-caesar-buffer-body arg)
7264           (set-window-start (get-buffer-window (current-buffer)) start))))))
7265
7266 (defun gnus-summary-stop-page-breaking ()
7267   "Stop page breaking in the current article."
7268   (interactive)
7269   (gnus-summary-select-article)
7270   (gnus-eval-in-buffer-window gnus-article-buffer
7271     (widen)
7272     (when (gnus-visual-p 'page-marker)
7273       (let ((buffer-read-only nil))
7274         (gnus-remove-text-with-property 'gnus-prev)
7275         (gnus-remove-text-with-property 'gnus-next))
7276       (setq gnus-page-broken nil))))
7277
7278 (defun gnus-summary-move-article (&optional n to-newsgroup
7279                                             select-method action)
7280   "Move the current article to a different newsgroup.
7281 If N is a positive number, move the N next articles.
7282 If N is a negative number, move the N previous articles.
7283 If N is nil and any articles have been marked with the process mark,
7284 move those articles instead.
7285 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7286 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7287 re-spool using this method.
7288
7289 For this function to work, both the current newsgroup and the
7290 newsgroup that you want to move to have to support the `request-move'
7291 and `request-accept' functions.
7292
7293 ACTION can be either `move' (the default), `crosspost' or `copy'."
7294   (interactive "P")
7295   (unless action
7296     (setq action 'move))
7297   ;; Disable marking as read.
7298   (let (gnus-mark-article-hook)
7299     (save-window-excursion
7300       (gnus-summary-select-article)))
7301   ;; Check whether the source group supports the required functions.
7302   (cond ((and (eq action 'move)
7303               (not (gnus-check-backend-function
7304                     'request-move-article gnus-newsgroup-name)))
7305          (error "The current group does not support article moving"))
7306         ((and (eq action 'crosspost)
7307               (not (gnus-check-backend-function
7308                     'request-replace-article gnus-newsgroup-name)))
7309          (error "The current group does not support article editing")))
7310   (let ((articles (gnus-summary-work-articles n))
7311         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7312         (names '((move "Move" "Moving")
7313                  (copy "Copy" "Copying")
7314                  (crosspost "Crosspost" "Crossposting")))
7315         (copy-buf (save-excursion
7316                     (nnheader-set-temp-buffer " *copy article*")))
7317         art-group to-method new-xref article to-groups)
7318     (unless (assq action names)
7319       (error "Unknown action %s" action))
7320     ;; Read the newsgroup name.
7321     (when (and (not to-newsgroup)
7322                (not select-method))
7323       (setq to-newsgroup
7324             (gnus-read-move-group-name
7325              (cadr (assq action names))
7326              (symbol-value (intern (format "gnus-current-%s-group" action)))
7327              articles prefix))
7328       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7329     (setq to-method (or select-method
7330                         (gnus-group-name-to-method to-newsgroup)))
7331     ;; Check the method we are to move this article to...
7332     (unless (gnus-check-backend-function
7333              'request-accept-article (car to-method))
7334       (error "%s does not support article copying" (car to-method)))
7335     (unless (gnus-check-server to-method)
7336       (error "Can't open server %s" (car to-method)))
7337     (gnus-message 6 "%s to %s: %s..."
7338                   (caddr (assq action names))
7339                   (or (car select-method) to-newsgroup) articles)
7340     (while articles
7341       (setq article (pop articles))
7342       (setq
7343        art-group
7344        (cond
7345         ;; Move the article.
7346         ((eq action 'move)
7347          ;; Remove this article from future suppression.
7348          (gnus-dup-unsuppress-article article)
7349          (gnus-request-move-article
7350           article                       ; Article to move
7351           gnus-newsgroup-name           ; From newsgroup
7352           (nth 1 (gnus-find-method-for-group
7353                   gnus-newsgroup-name)) ; Server
7354           (list 'gnus-request-accept-article
7355                 to-newsgroup (list 'quote select-method)
7356                 (not articles) t)       ; Accept form
7357           (not articles)))              ; Only save nov last time
7358         ;; Copy the article.
7359         ((eq action 'copy)
7360          (save-excursion
7361            (set-buffer copy-buf)
7362            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7363              (gnus-request-accept-article
7364               to-newsgroup select-method (not articles) t))))
7365         ;; Crosspost the article.
7366         ((eq action 'crosspost)
7367          (let ((xref (message-tokenize-header
7368                       (mail-header-xref (gnus-summary-article-header article))
7369                       " ")))
7370            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7371                                   ":" article))
7372            (unless xref
7373              (setq xref (list (system-name))))
7374            (setq new-xref
7375                  (concat
7376                   (mapconcat 'identity
7377                              (delete "Xref:" (delete new-xref xref))
7378                              " ")
7379                   " " new-xref))
7380            (save-excursion
7381              (set-buffer copy-buf)
7382              ;; First put the article in the destination group.
7383              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7384              (when (consp (setq art-group
7385                                 (gnus-request-accept-article
7386                                  to-newsgroup select-method (not articles))))
7387                (setq new-xref (concat new-xref " " (car art-group)
7388                                       ":" (cdr art-group)))
7389                ;; Now we have the new Xrefs header, so we insert
7390                ;; it and replace the new article.
7391                (nnheader-replace-header "Xref" new-xref)
7392                (gnus-request-replace-article
7393                 (cdr art-group) to-newsgroup (current-buffer))
7394                art-group))))))
7395       (cond
7396        ((not art-group)
7397         (gnus-message 1 "Couldn't %s article %s: %s"
7398                       (cadr (assq action names)) article
7399                       (nnheader-get-report (car to-method))))
7400        ((and (eq art-group 'junk)
7401              (eq action 'move))
7402         (gnus-summary-mark-article article gnus-canceled-mark)
7403         (gnus-message 4 "Deleted article %s" article))
7404        (t
7405         (let* ((pto-group (gnus-group-prefixed-name
7406                            (car art-group) to-method))
7407                (entry
7408                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7409                (info (nth 2 entry))
7410                (to-group (gnus-info-group info))
7411                to-marks)
7412           ;; Update the group that has been moved to.
7413           (when (and info
7414                      (memq action '(move copy)))
7415             (unless (member to-group to-groups)
7416               (push to-group to-groups))
7417
7418             (unless (memq article gnus-newsgroup-unreads)
7419               (push 'read to-marks)
7420               (gnus-info-set-read
7421                info (gnus-add-to-range (gnus-info-read info)
7422                                        (list (cdr art-group)))))
7423
7424             ;; See whether the article is to be put in the cache.
7425             (let ((marks gnus-article-mark-lists)
7426                   (to-article (cdr art-group)))
7427
7428               ;; Enter the article into the cache in the new group,
7429               ;; if that is required.
7430               (when gnus-use-cache
7431                 (gnus-cache-possibly-enter-article
7432                  to-group to-article
7433                  (memq article gnus-newsgroup-marked)
7434                  (memq article gnus-newsgroup-dormant)
7435                  (memq article gnus-newsgroup-unreads)))
7436
7437               (when gnus-preserve-marks 
7438                 ;; Copy any marks over to the new group.
7439                 (when (and (equal to-group gnus-newsgroup-name)
7440                            (not (memq article gnus-newsgroup-unreads)))
7441                   ;; Mark this article as read in this group.
7442                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7443                   (setcdr (gnus-active to-group) to-article)
7444                   (setcdr gnus-newsgroup-active to-article))
7445
7446                 (while marks
7447                   (when (memq article (symbol-value
7448                                        (intern (format "gnus-newsgroup-%s"
7449                                                        (caar marks)))))
7450                     (push (cdar marks) to-marks)
7451                     ;; If the other group is the same as this group,
7452                     ;; then we have to add the mark to the list.
7453                     (when (equal to-group gnus-newsgroup-name)
7454                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7455                            (cons to-article
7456                                  (symbol-value
7457                                   (intern (format "gnus-newsgroup-%s"
7458                                                   (caar marks)))))))
7459                     ;; Copy the marks to other group.
7460                     (gnus-add-marked-articles
7461                      to-group (cdar marks) (list to-article) info))
7462                   (setq marks (cdr marks)))
7463
7464                 (gnus-request-set-mark to-group (list (list (list to-article)
7465                                                             'set
7466                                                             to-marks))))
7467
7468               (gnus-dribble-enter
7469                (concat "(gnus-group-set-info '"
7470                        (gnus-prin1-to-string (gnus-get-info to-group))
7471                        ")"))))
7472
7473           ;; Update the Xref header in this article to point to
7474           ;; the new crossposted article we have just created.
7475           (when (eq action 'crosspost)
7476             (save-excursion
7477               (set-buffer copy-buf)
7478               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7479               (nnheader-replace-header "Xref" new-xref)
7480               (gnus-request-replace-article
7481                article gnus-newsgroup-name (current-buffer)))))
7482
7483         ;;;!!!Why is this necessary?
7484         (set-buffer gnus-summary-buffer)
7485
7486         (gnus-summary-goto-subject article)
7487         (when (eq action 'move)
7488           (gnus-summary-mark-article article gnus-canceled-mark))))
7489       (gnus-summary-remove-process-mark article))
7490     ;; Re-activate all groups that have been moved to.
7491     (while to-groups
7492       (save-excursion
7493         (set-buffer gnus-group-buffer)
7494         (when (gnus-group-goto-group (car to-groups) t)
7495           (gnus-group-get-new-news-this-group 1 t))
7496         (pop to-groups)))
7497
7498     (gnus-kill-buffer copy-buf)
7499     (gnus-summary-position-point)
7500     (gnus-set-mode-line 'summary)))
7501
7502 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7503   "Move the current article to a different newsgroup.
7504 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7505 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7506 re-spool using this method."
7507   (interactive "P")
7508   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7509
7510 (defun gnus-summary-crosspost-article (&optional n)
7511   "Crosspost the current article to some other group."
7512   (interactive "P")
7513   (gnus-summary-move-article n nil nil 'crosspost))
7514
7515 (defcustom gnus-summary-respool-default-method nil
7516   "Default method for respooling an article.
7517 If nil, use to the current newsgroup method."
7518   :type '(choice (gnus-select-method :value (nnml ""))
7519                  (const nil))
7520   :group 'gnus-summary-mail)
7521
7522 (defun gnus-summary-respool-article (&optional n method)
7523   "Respool the current article.
7524 The article will be squeezed through the mail spooling process again,
7525 which means that it will be put in some mail newsgroup or other
7526 depending on `nnmail-split-methods'.
7527 If N is a positive number, respool the N next articles.
7528 If N is a negative number, respool the N previous articles.
7529 If N is nil and any articles have been marked with the process mark,
7530 respool those articles instead.
7531
7532 Respooling can be done both from mail groups and \"real\" newsgroups.
7533 In the former case, the articles in question will be moved from the
7534 current group into whatever groups they are destined to.  In the
7535 latter case, they will be copied into the relevant groups."
7536   (interactive
7537    (list current-prefix-arg
7538          (let* ((methods (gnus-methods-using 'respool))
7539                 (methname
7540                  (symbol-name (or gnus-summary-respool-default-method
7541                                   (car (gnus-find-method-for-group
7542                                         gnus-newsgroup-name)))))
7543                 (method
7544                  (gnus-completing-read
7545                   methname "What backend do you want to use when respooling?"
7546                   methods nil t nil 'gnus-mail-method-history))
7547                 ms)
7548            (cond
7549             ((zerop (length (setq ms (gnus-servers-using-backend
7550                                       (intern method)))))
7551              (list (intern method) ""))
7552             ((= 1 (length ms))
7553              (car ms))
7554             (t
7555              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7556                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7557                            ms-alist))))))))
7558   (unless method
7559     (error "No method given for respooling"))
7560   (if (assoc (symbol-name
7561               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7562              (gnus-methods-using 'respool))
7563       (gnus-summary-move-article n nil method)
7564     (gnus-summary-copy-article n nil method)))
7565
7566 (defun gnus-summary-import-article (file)
7567   "Import an arbitrary file into a mail newsgroup."
7568   (interactive "fImport file: ")
7569   (let ((group gnus-newsgroup-name)
7570         (now (current-time))
7571         atts lines)
7572     (unless (gnus-check-backend-function 'request-accept-article group)
7573       (error "%s does not support article importing" group))
7574     (or (file-readable-p file)
7575         (not (file-regular-p file))
7576         (error "Can't read %s" file))
7577     (save-excursion
7578       (set-buffer (gnus-get-buffer-create " *import file*"))
7579       (erase-buffer)
7580       (insert-file-contents file)
7581       (goto-char (point-min))
7582       (unless (nnheader-article-p)
7583         ;; This doesn't look like an article, so we fudge some headers.
7584         (setq atts (file-attributes file)
7585               lines (count-lines (point-min) (point-max)))
7586         (insert "From: " (read-string "From: ") "\n"
7587                 "Subject: " (read-string "Subject: ") "\n"
7588                 "Date: " (message-make-date (nth 5 atts))
7589                 "\n"
7590                 "Message-ID: " (message-make-message-id) "\n"
7591                 "Lines: " (int-to-string lines) "\n"
7592                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7593       (gnus-request-accept-article group nil t)
7594       (kill-buffer (current-buffer)))))
7595
7596 (defun gnus-summary-article-posted-p ()
7597   "Say whether the current (mail) article is available from news as well.
7598 This will be the case if the article has both been mailed and posted."
7599   (interactive)
7600   (let ((id (mail-header-references (gnus-summary-article-header)))
7601         (gnus-override-method (car (gnus-refer-article-methods))))
7602     (if (gnus-request-head id "")
7603         (gnus-message 2 "The current message was found on %s"
7604                       gnus-override-method)
7605       (gnus-message 2 "The current message couldn't be found on %s"
7606                     gnus-override-method)
7607       nil)))
7608
7609 (defun gnus-summary-expire-articles (&optional now)
7610   "Expire all articles that are marked as expirable in the current group."
7611   (interactive)
7612   (when (gnus-check-backend-function
7613          'request-expire-articles gnus-newsgroup-name)
7614     ;; This backend supports expiry.
7615     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7616            (expirable (if total
7617                           (progn
7618                             ;; We need to update the info for
7619                             ;; this group for `gnus-list-of-read-articles'
7620                             ;; to give us the right answer.
7621                             (gnus-run-hooks 'gnus-exit-group-hook)
7622                             (gnus-summary-update-info)
7623                             (gnus-list-of-read-articles gnus-newsgroup-name))
7624                         (setq gnus-newsgroup-expirable
7625                               (sort gnus-newsgroup-expirable '<))))
7626            (expiry-wait (if now 'immediate
7627                           (gnus-group-find-parameter
7628                            gnus-newsgroup-name 'expiry-wait)))
7629            es)
7630       (when expirable
7631         ;; There are expirable articles in this group, so we run them
7632         ;; through the expiry process.
7633         (gnus-message 6 "Expiring articles...")
7634         (unless (gnus-check-group gnus-newsgroup-name)
7635           (error "Can't open server for %s" gnus-newsgroup-name))
7636         ;; The list of articles that weren't expired is returned.
7637         (save-excursion
7638           (if expiry-wait
7639               (let ((nnmail-expiry-wait-function nil)
7640                     (nnmail-expiry-wait expiry-wait))
7641                 (setq es (gnus-request-expire-articles
7642                           expirable gnus-newsgroup-name)))
7643             (setq es (gnus-request-expire-articles
7644                       expirable gnus-newsgroup-name))))
7645         (unless total
7646           (setq gnus-newsgroup-expirable es))
7647         ;; We go through the old list of expirable, and mark all
7648         ;; really expired articles as nonexistent.
7649         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7650           (let ((gnus-use-cache nil))
7651             (while expirable
7652               (unless (memq (car expirable) es)
7653                 (when (gnus-data-find (car expirable))
7654                   (gnus-summary-mark-article
7655                    (car expirable) gnus-canceled-mark)))
7656               (setq expirable (cdr expirable)))))
7657         (gnus-message 6 "Expiring articles...done")))))
7658
7659 (defun gnus-summary-expire-articles-now ()
7660   "Expunge all expirable articles in the current group.
7661 This means that *all* articles that are marked as expirable will be
7662 deleted forever, right now."
7663   (interactive)
7664   (or gnus-expert-user
7665       (gnus-yes-or-no-p
7666        "Are you really, really, really sure you want to delete all these messages? ")
7667       (error "Phew!"))
7668   (gnus-summary-expire-articles t))
7669
7670 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7671 (defun gnus-summary-delete-article (&optional n)
7672   "Delete the N next (mail) articles.
7673 This command actually deletes articles.  This is not a marking
7674 command.  The article will disappear forever from your life, never to
7675 return.
7676 If N is negative, delete backwards.
7677 If N is nil and articles have been marked with the process mark,
7678 delete these instead."
7679   (interactive "P")
7680   (unless (gnus-check-backend-function 'request-expire-articles
7681                                        gnus-newsgroup-name)
7682     (error "The current newsgroup does not support article deletion"))
7683   ;; Compute the list of articles to delete.
7684   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7685         not-deleted)
7686     (if (and gnus-novice-user
7687              (not (gnus-yes-or-no-p
7688                    (format "Do you really want to delete %s forever? "
7689                            (if (> (length articles) 1)
7690                                (format "these %s articles" (length articles))
7691                              "this article")))))
7692         ()
7693       ;; Delete the articles.
7694       (setq not-deleted (gnus-request-expire-articles
7695                          articles gnus-newsgroup-name 'force))
7696       (while articles
7697         (gnus-summary-remove-process-mark (car articles))
7698         ;; The backend might not have been able to delete the article
7699         ;; after all.
7700         (unless (memq (car articles) not-deleted)
7701           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7702         (setq articles (cdr articles)))
7703       (when not-deleted
7704         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7705     (gnus-summary-position-point)
7706     (gnus-set-mode-line 'summary)
7707     not-deleted))
7708
7709 (defun gnus-summary-edit-article (&optional force)
7710   "Edit the current article.
7711 This will have permanent effect only in mail groups.
7712 If FORCE is non-nil, allow editing of articles even in read-only
7713 groups."
7714   (interactive "P")
7715   (save-excursion
7716     (set-buffer gnus-summary-buffer)
7717     (let ((mail-parse-charset gnus-newsgroup-charset)
7718           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7719       (gnus-set-global-variables)
7720       (when (and (not force)
7721                  (gnus-group-read-only-p))
7722         (error "The current newsgroup does not support article editing"))
7723       (gnus-summary-show-article t)
7724       (gnus-article-edit-article
7725        'mime-to-mml
7726        `(lambda (no-highlight)
7727           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7728                 (mail-parse-ignored-charsets 
7729                  ',gnus-newsgroup-ignored-charsets))
7730             (mml-to-mime)
7731             (gnus-summary-edit-article-done
7732              ,(or (mail-header-references gnus-current-headers) "")
7733              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7734
7735 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7736
7737 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7738                                                  no-highlight)
7739   "Make edits to the current article permanent."
7740   (interactive)
7741   ;; Replace the article.
7742   (let ((buf (current-buffer)))
7743     (with-temp-buffer
7744       (insert-buffer-substring buf)
7745       (if (and (not read-only)
7746                (not (gnus-request-replace-article
7747                      (cdr gnus-article-current) (car gnus-article-current)
7748                      (current-buffer) t)))
7749           (error "Couldn't replace article")
7750         ;; Update the summary buffer.
7751         (if (and references
7752                  (equal (message-tokenize-header references " ")
7753                         (message-tokenize-header
7754                          (or (message-fetch-field "references") "") " ")))
7755             ;; We only have to update this line.
7756             (save-excursion
7757               (save-restriction
7758                 (message-narrow-to-head)
7759                 (let ((head (buffer-string))
7760                       header)
7761                   (with-temp-buffer
7762                     (insert (format "211 %d Article retrieved.\n"
7763                                     (cdr gnus-article-current)))
7764                     (insert head)
7765                     (insert ".\n")
7766                     (let ((nntp-server-buffer (current-buffer)))
7767                       (setq header (car (gnus-get-newsgroup-headers
7768                                          (save-excursion
7769                                            (set-buffer gnus-summary-buffer)
7770                                            gnus-newsgroup-dependencies)
7771                                          t))))
7772                     (save-excursion
7773                       (set-buffer gnus-summary-buffer)
7774                       (gnus-data-set-header
7775                        (gnus-data-find (cdr gnus-article-current))
7776                        header)
7777                       (gnus-summary-update-article-line
7778                        (cdr gnus-article-current) header))))))
7779           ;; Update threads.
7780           (set-buffer (or buffer gnus-summary-buffer))
7781           (gnus-summary-update-article (cdr gnus-article-current)))
7782         ;; Prettify the article buffer again.
7783         (unless no-highlight
7784           (save-excursion
7785             (set-buffer gnus-article-buffer)
7786             ;;;!!! Fix this -- article should be rehighlighted.
7787             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7788             (set-buffer gnus-original-article-buffer)
7789             (gnus-request-article
7790              (cdr gnus-article-current)
7791              (car gnus-article-current) (current-buffer))))
7792         ;; Prettify the summary buffer line.
7793         (when (gnus-visual-p 'summary-highlight 'highlight)
7794           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7795
7796 (defun gnus-summary-edit-wash (key)
7797   "Perform editing command KEY in the article buffer."
7798   (interactive
7799    (list
7800     (progn
7801       (message "%s" (concat (this-command-keys) "- "))
7802       (read-char))))
7803   (message "")
7804   (gnus-summary-edit-article)
7805   (execute-kbd-macro (concat (this-command-keys) key))
7806   (gnus-article-edit-done))
7807
7808 ;;; Respooling
7809
7810 (defun gnus-summary-respool-query (&optional silent trace)
7811   "Query where the respool algorithm would put this article."
7812   (interactive)
7813   (let (gnus-mark-article-hook)
7814     (gnus-summary-select-article)
7815     (save-excursion
7816       (set-buffer gnus-original-article-buffer)
7817       (save-restriction
7818         (message-narrow-to-head)
7819         (let ((groups (nnmail-article-group 'identity trace)))
7820           (unless silent
7821             (if groups
7822                 (message "This message would go to %s"
7823                          (mapconcat 'car groups ", "))
7824               (message "This message would go to no groups"))
7825             groups))))))
7826
7827 (defun gnus-summary-respool-trace ()
7828   "Trace where the respool algorithm would put this article.
7829 Display a buffer showing all fancy splitting patterns which matched."
7830   (interactive)
7831   (gnus-summary-respool-query nil t))
7832
7833 ;; Summary marking commands.
7834
7835 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7836   "Mark articles which has the same subject as read, and then select the next.
7837 If UNMARK is positive, remove any kind of mark.
7838 If UNMARK is negative, tick articles."
7839   (interactive "P")
7840   (when unmark
7841     (setq unmark (prefix-numeric-value unmark)))
7842   (let ((count
7843          (gnus-summary-mark-same-subject
7844           (gnus-summary-article-subject) unmark)))
7845     ;; Select next unread article.  If auto-select-same mode, should
7846     ;; select the first unread article.
7847     (gnus-summary-next-article t (and gnus-auto-select-same
7848                                       (gnus-summary-article-subject)))
7849     (gnus-message 7 "%d article%s marked as %s"
7850                   count (if (= count 1) " is" "s are")
7851                   (if unmark "unread" "read"))))
7852
7853 (defun gnus-summary-kill-same-subject (&optional unmark)
7854   "Mark articles which has the same subject as read.
7855 If UNMARK is positive, remove any kind of mark.
7856 If UNMARK is negative, tick articles."
7857   (interactive "P")
7858   (when unmark
7859     (setq unmark (prefix-numeric-value unmark)))
7860   (let ((count
7861          (gnus-summary-mark-same-subject
7862           (gnus-summary-article-subject) unmark)))
7863     ;; If marked as read, go to next unread subject.
7864     (when (null unmark)
7865       ;; Go to next unread subject.
7866       (gnus-summary-next-subject 1 t))
7867     (gnus-message 7 "%d articles are marked as %s"
7868                   count (if unmark "unread" "read"))))
7869
7870 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7871   "Mark articles with same SUBJECT as read, and return marked number.
7872 If optional argument UNMARK is positive, remove any kinds of marks.
7873 If optional argument UNMARK is negative, mark articles as unread instead."
7874   (let ((count 1))
7875     (save-excursion
7876       (cond
7877        ((null unmark)                   ; Mark as read.
7878         (while (and
7879                 (progn
7880                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7881                   (gnus-summary-show-thread) t)
7882                 (gnus-summary-find-subject subject))
7883           (setq count (1+ count))))
7884        ((> unmark 0)                    ; Tick.
7885         (while (and
7886                 (progn
7887                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7888                   (gnus-summary-show-thread) t)
7889                 (gnus-summary-find-subject subject))
7890           (setq count (1+ count))))
7891        (t                               ; Mark as unread.
7892         (while (and
7893                 (progn
7894                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7895                   (gnus-summary-show-thread) t)
7896                 (gnus-summary-find-subject subject))
7897           (setq count (1+ count)))))
7898       (gnus-set-mode-line 'summary)
7899       ;; Return the number of marked articles.
7900       count)))
7901
7902 (defun gnus-summary-mark-as-processable (n &optional unmark)
7903   "Set the process mark on the next N articles.
7904 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7905 the process mark instead.  The difference between N and the actual
7906 number of articles marked is returned."
7907   (interactive "p")
7908   (let ((backward (< n 0))
7909         (n (abs n)))
7910     (while (and
7911             (> n 0)
7912             (if unmark
7913                 (gnus-summary-remove-process-mark
7914                  (gnus-summary-article-number))
7915               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7916             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7917       (setq n (1- n)))
7918     (when (/= 0 n)
7919       (gnus-message 7 "No more articles"))
7920     (gnus-summary-recenter)
7921     (gnus-summary-position-point)
7922     n))
7923
7924 (defun gnus-summary-unmark-as-processable (n)
7925   "Remove the process mark from the next N articles.
7926 If N is negative, unmark backward instead.  The difference between N and
7927 the actual number of articles unmarked is returned."
7928   (interactive "p")
7929   (gnus-summary-mark-as-processable n t))
7930
7931 (defun gnus-summary-unmark-all-processable ()
7932   "Remove the process mark from all articles."
7933   (interactive)
7934   (save-excursion
7935     (while gnus-newsgroup-processable
7936       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7937   (gnus-summary-position-point))
7938
7939 (defun gnus-summary-mark-as-expirable (n)
7940   "Mark N articles forward as expirable.
7941 If N is negative, mark backward instead.  The difference between N and
7942 the actual number of articles marked is returned."
7943   (interactive "p")
7944   (gnus-summary-mark-forward n gnus-expirable-mark))
7945
7946 (defun gnus-summary-mark-article-as-replied (article)
7947   "Mark ARTICLE replied and update the summary line."
7948   (push article gnus-newsgroup-replied)
7949   (let ((buffer-read-only nil))
7950     (when (gnus-summary-goto-subject article nil t)
7951       (gnus-summary-update-secondary-mark article))))
7952
7953 (defun gnus-summary-set-bookmark (article)
7954   "Set a bookmark in current article."
7955   (interactive (list (gnus-summary-article-number)))
7956   (when (or (not (get-buffer gnus-article-buffer))
7957             (not gnus-current-article)
7958             (not gnus-article-current)
7959             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7960     (error "No current article selected"))
7961   ;; Remove old bookmark, if one exists.
7962   (let ((old (assq article gnus-newsgroup-bookmarks)))
7963     (when old
7964       (setq gnus-newsgroup-bookmarks
7965             (delq old gnus-newsgroup-bookmarks))))
7966   ;; Set the new bookmark, which is on the form
7967   ;; (article-number . line-number-in-body).
7968   (push
7969    (cons article
7970          (save-excursion
7971            (set-buffer gnus-article-buffer)
7972            (count-lines
7973             (min (point)
7974                  (save-excursion
7975                    (goto-char (point-min))
7976                    (search-forward "\n\n" nil t)
7977                    (point)))
7978             (point))))
7979    gnus-newsgroup-bookmarks)
7980   (gnus-message 6 "A bookmark has been added to the current article."))
7981
7982 (defun gnus-summary-remove-bookmark (article)
7983   "Remove the bookmark from the current article."
7984   (interactive (list (gnus-summary-article-number)))
7985   ;; Remove old bookmark, if one exists.
7986   (let ((old (assq article gnus-newsgroup-bookmarks)))
7987     (if old
7988         (progn
7989           (setq gnus-newsgroup-bookmarks
7990                 (delq old gnus-newsgroup-bookmarks))
7991           (gnus-message 6 "Removed bookmark."))
7992       (gnus-message 6 "No bookmark in current article."))))
7993
7994 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7995 (defun gnus-summary-mark-as-dormant (n)
7996   "Mark N articles forward as dormant.
7997 If N is negative, mark backward instead.  The difference between N and
7998 the actual number of articles marked is returned."
7999   (interactive "p")
8000   (gnus-summary-mark-forward n gnus-dormant-mark))
8001
8002 (defun gnus-summary-set-process-mark (article)
8003   "Set the process mark on ARTICLE and update the summary line."
8004   (setq gnus-newsgroup-processable
8005         (cons article
8006               (delq article gnus-newsgroup-processable)))
8007   (when (gnus-summary-goto-subject article)
8008     (gnus-summary-show-thread)
8009     (gnus-summary-goto-subject article)
8010     (gnus-summary-update-secondary-mark article)))
8011
8012 (defun gnus-summary-remove-process-mark (article)
8013   "Remove the process mark from ARTICLE and update the summary line."
8014   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8015   (when (gnus-summary-goto-subject article)
8016     (gnus-summary-show-thread)
8017     (gnus-summary-goto-subject article)
8018     (gnus-summary-update-secondary-mark article)))
8019
8020 (defun gnus-summary-set-saved-mark (article)
8021   "Set the process mark on ARTICLE and update the summary line."
8022   (push article gnus-newsgroup-saved)
8023   (when (gnus-summary-goto-subject article)
8024     (gnus-summary-update-secondary-mark article)))
8025
8026 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8027   "Mark N articles as read forwards.
8028 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8029 The difference between N and the actual number of articles marked is
8030 returned.
8031 Iff NO-EXPIRE, auto-expiry will be inhibited."
8032   (interactive "p")
8033   (gnus-summary-show-thread)
8034   (let ((backward (< n 0))
8035         (gnus-summary-goto-unread
8036          (and gnus-summary-goto-unread
8037               (not (eq gnus-summary-goto-unread 'never))
8038               (not (memq mark (list gnus-unread-mark
8039                                     gnus-ticked-mark gnus-dormant-mark)))))
8040         (n (abs n))
8041         (mark (or mark gnus-del-mark)))
8042     (while (and (> n 0)
8043                 (gnus-summary-mark-article nil mark no-expire)
8044                 (zerop (gnus-summary-next-subject
8045                         (if backward -1 1)
8046                         (and gnus-summary-goto-unread
8047                              (not (eq gnus-summary-goto-unread 'never)))
8048                         t)))
8049       (setq n (1- n)))
8050     (when (/= 0 n)
8051       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8052     (gnus-summary-recenter)
8053     (gnus-summary-position-point)
8054     (gnus-set-mode-line 'summary)
8055     n))
8056
8057 (defun gnus-summary-mark-article-as-read (mark)
8058   "Mark the current article quickly as read with MARK."
8059   (let ((article (gnus-summary-article-number)))
8060     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8061     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8062     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8063     (push (cons article mark) gnus-newsgroup-reads)
8064     ;; Possibly remove from cache, if that is used.
8065     (when gnus-use-cache
8066       (gnus-cache-enter-remove-article article))
8067     ;; Allow the backend to change the mark.
8068     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8069     ;; Check for auto-expiry.
8070     (when (and gnus-newsgroup-auto-expire
8071                (memq mark gnus-auto-expirable-marks))
8072       (setq mark gnus-expirable-mark)
8073       ;; Let the backend know about the mark change.
8074       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8075       (push article gnus-newsgroup-expirable))
8076     ;; Set the mark in the buffer.
8077     (gnus-summary-update-mark mark 'unread)
8078     t))
8079
8080 (defun gnus-summary-mark-article-as-unread (mark)
8081   "Mark the current article quickly as unread with MARK."
8082   (let* ((article (gnus-summary-article-number))
8083          (old-mark (gnus-summary-article-mark article)))
8084     ;; Allow the backend to change the mark.
8085     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8086     (if (eq mark old-mark)
8087         t
8088       (if (<= article 0)
8089           (progn
8090             (gnus-error 1 "Can't mark negative article numbers")
8091             nil)
8092         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8093         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8094         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8095         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8096         (cond ((= mark gnus-ticked-mark)
8097                (push article gnus-newsgroup-marked))
8098               ((= mark gnus-dormant-mark)
8099                (push article gnus-newsgroup-dormant))
8100               (t
8101                (push article gnus-newsgroup-unreads)))
8102         (gnus-pull article gnus-newsgroup-reads)
8103
8104         ;; See whether the article is to be put in the cache.
8105         (and gnus-use-cache
8106              (vectorp (gnus-summary-article-header article))
8107              (save-excursion
8108                (gnus-cache-possibly-enter-article
8109                 gnus-newsgroup-name article
8110                 (= mark gnus-ticked-mark)
8111                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8112
8113         ;; Fix the mark.
8114         (gnus-summary-update-mark mark 'unread)
8115         t))))
8116
8117 (defun gnus-summary-mark-article (&optional article mark no-expire)
8118   "Mark ARTICLE with MARK.  MARK can be any character.
8119 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8120 `??' (dormant) and `?E' (expirable).
8121 If MARK is nil, then the default character `?r' is used.
8122 If ARTICLE is nil, then the article on the current line will be
8123 marked.
8124 Iff NO-EXPIRE, auto-expiry will be inhibited."
8125   ;; The mark might be a string.
8126   (when (stringp mark)
8127     (setq mark (aref mark 0)))
8128   ;; If no mark is given, then we check auto-expiring.
8129   (when (null mark)
8130     (setq mark gnus-del-mark))
8131   (when (and (not no-expire)
8132              gnus-newsgroup-auto-expire
8133              (memq mark gnus-auto-expirable-marks))
8134     (setq mark gnus-expirable-mark))
8135   (let ((article (or article (gnus-summary-article-number)))
8136         (old-mark (gnus-summary-article-mark article)))
8137     ;; Allow the backend to change the mark.
8138     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8139     (if (eq mark old-mark)
8140         t
8141       (unless article
8142         (error "No article on current line"))
8143       (if (not (if (or (= mark gnus-unread-mark)
8144                        (= mark gnus-ticked-mark)
8145                        (= mark gnus-dormant-mark))
8146                    (gnus-mark-article-as-unread article mark)
8147                  (gnus-mark-article-as-read article mark)))
8148           t
8149         ;; See whether the article is to be put in the cache.
8150         (and gnus-use-cache
8151              (not (= mark gnus-canceled-mark))
8152              (vectorp (gnus-summary-article-header article))
8153              (save-excursion
8154                (gnus-cache-possibly-enter-article
8155                 gnus-newsgroup-name article
8156                 (= mark gnus-ticked-mark)
8157                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8158
8159         (when (gnus-summary-goto-subject article nil t)
8160           (let ((buffer-read-only nil))
8161             (gnus-summary-show-thread)
8162             ;; Fix the mark.
8163             (gnus-summary-update-mark mark 'unread)
8164             t))))))
8165
8166 (defun gnus-summary-update-secondary-mark (article)
8167   "Update the secondary (read, process, cache) mark."
8168   (gnus-summary-update-mark
8169    (cond ((memq article gnus-newsgroup-processable)
8170           gnus-process-mark)
8171          ((memq article gnus-newsgroup-cached)
8172           gnus-cached-mark)
8173          ((memq article gnus-newsgroup-replied)
8174           gnus-replied-mark)
8175          ((memq article gnus-newsgroup-saved)
8176           gnus-saved-mark)
8177          (t gnus-unread-mark))
8178    'replied)
8179   (when (gnus-visual-p 'summary-highlight 'highlight)
8180     (gnus-run-hooks 'gnus-summary-update-hook))
8181   t)
8182
8183 (defun gnus-summary-update-mark (mark type)
8184   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8185         (buffer-read-only nil))
8186     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8187     (when forward
8188       (when (looking-at "\r")
8189         (incf forward))
8190       (when (<= (+ forward (point)) (point-max))
8191         ;; Go to the right position on the line.
8192         (goto-char (+ forward (point)))
8193         ;; Replace the old mark with the new mark.
8194         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8195         ;; Optionally update the marks by some user rule.
8196         (when (eq type 'unread)
8197           (gnus-data-set-mark
8198            (gnus-data-find (gnus-summary-article-number)) mark)
8199           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8200
8201 (defun gnus-mark-article-as-read (article &optional mark)
8202   "Enter ARTICLE in the pertinent lists and remove it from others."
8203   ;; Make the article expirable.
8204   (let ((mark (or mark gnus-del-mark)))
8205     (if (= mark gnus-expirable-mark)
8206         (push article gnus-newsgroup-expirable)
8207       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8208     ;; Remove from unread and marked lists.
8209     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8210     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8211     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8212     (push (cons article mark) gnus-newsgroup-reads)
8213     ;; Possibly remove from cache, if that is used.
8214     (when gnus-use-cache
8215       (gnus-cache-enter-remove-article article))
8216     t))
8217
8218 (defun gnus-mark-article-as-unread (article &optional mark)
8219   "Enter ARTICLE in the pertinent lists and remove it from others."
8220   (let ((mark (or mark gnus-ticked-mark)))
8221     (if (<= article 0)
8222         (progn
8223           (gnus-error 1 "Can't mark negative article numbers")
8224           nil)
8225       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8226             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8227             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8228             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8229
8230       ;; Unsuppress duplicates?
8231       (when gnus-suppress-duplicates
8232         (gnus-dup-unsuppress-article article))
8233
8234       (cond ((= mark gnus-ticked-mark)
8235              (push article gnus-newsgroup-marked))
8236             ((= mark gnus-dormant-mark)
8237              (push article gnus-newsgroup-dormant))
8238             (t
8239              (push article gnus-newsgroup-unreads)))
8240       (gnus-pull article gnus-newsgroup-reads)
8241       t)))
8242
8243 (defalias 'gnus-summary-mark-as-unread-forward
8244   'gnus-summary-tick-article-forward)
8245 (make-obsolete 'gnus-summary-mark-as-unread-forward
8246                'gnus-summary-tick-article-forward)
8247 (defun gnus-summary-tick-article-forward (n)
8248   "Tick N articles forwards.
8249 If N is negative, tick backwards instead.
8250 The difference between N and the number of articles ticked is returned."
8251   (interactive "p")
8252   (gnus-summary-mark-forward n gnus-ticked-mark))
8253
8254 (defalias 'gnus-summary-mark-as-unread-backward
8255   'gnus-summary-tick-article-backward)
8256 (make-obsolete 'gnus-summary-mark-as-unread-backward
8257                'gnus-summary-tick-article-backward)
8258 (defun gnus-summary-tick-article-backward (n)
8259   "Tick N articles backwards.
8260 The difference between N and the number of articles ticked is returned."
8261   (interactive "p")
8262   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8263
8264 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8265 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8266 (defun gnus-summary-tick-article (&optional article clear-mark)
8267   "Mark current article as unread.
8268 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8269 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8270   (interactive)
8271   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8272                                        gnus-ticked-mark)))
8273
8274 (defun gnus-summary-mark-as-read-forward (n)
8275   "Mark N articles as read forwards.
8276 If N is negative, mark backwards instead.
8277 The difference between N and the actual number of articles marked is
8278 returned."
8279   (interactive "p")
8280   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8281
8282 (defun gnus-summary-mark-as-read-backward (n)
8283   "Mark the N articles as read backwards.
8284 The difference between N and the actual number of articles marked is
8285 returned."
8286   (interactive "p")
8287   (gnus-summary-mark-forward
8288    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8289
8290 (defun gnus-summary-mark-as-read (&optional article mark)
8291   "Mark current article as read.
8292 ARTICLE specifies the article to be marked as read.
8293 MARK specifies a string to be inserted at the beginning of the line."
8294   (gnus-summary-mark-article article mark))
8295
8296 (defun gnus-summary-clear-mark-forward (n)
8297   "Clear marks from N articles forward.
8298 If N is negative, clear backward instead.
8299 The difference between N and the number of marks cleared is returned."
8300   (interactive "p")
8301   (gnus-summary-mark-forward n gnus-unread-mark))
8302
8303 (defun gnus-summary-clear-mark-backward (n)
8304   "Clear marks from N articles backward.
8305 The difference between N and the number of marks cleared is returned."
8306   (interactive "p")
8307   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8308
8309 (defun gnus-summary-mark-unread-as-read ()
8310   "Intended to be used by `gnus-summary-mark-article-hook'."
8311   (when (memq gnus-current-article gnus-newsgroup-unreads)
8312     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8313
8314 (defun gnus-summary-mark-read-and-unread-as-read ()
8315   "Intended to be used by `gnus-summary-mark-article-hook'."
8316   (let ((mark (gnus-summary-article-mark)))
8317     (when (or (gnus-unread-mark-p mark)
8318               (gnus-read-mark-p mark))
8319       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8320
8321 (defun gnus-summary-mark-region-as-read (point mark all)
8322   "Mark all unread articles between point and mark as read.
8323 If given a prefix, mark all articles between point and mark as read,
8324 even ticked and dormant ones."
8325   (interactive "r\nP")
8326   (save-excursion
8327     (let (article)
8328       (goto-char point)
8329       (beginning-of-line)
8330       (while (and
8331               (< (point) mark)
8332               (progn
8333                 (when (or all
8334                           (memq (setq article (gnus-summary-article-number))
8335                                 gnus-newsgroup-unreads))
8336                   (gnus-summary-mark-article article gnus-del-mark))
8337                 t)
8338               (gnus-summary-find-next))))))
8339
8340 (defun gnus-summary-mark-below (score mark)
8341   "Mark articles with score less than SCORE with MARK."
8342   (interactive "P\ncMark: ")
8343   (setq score (if score
8344                   (prefix-numeric-value score)
8345                 (or gnus-summary-default-score 0)))
8346   (save-excursion
8347     (set-buffer gnus-summary-buffer)
8348     (goto-char (point-min))
8349     (while
8350         (progn
8351           (and (< (gnus-summary-article-score) score)
8352                (gnus-summary-mark-article nil mark))
8353           (gnus-summary-find-next)))))
8354
8355 (defun gnus-summary-kill-below (&optional score)
8356   "Mark articles with score below SCORE as read."
8357   (interactive "P")
8358   (gnus-summary-mark-below score gnus-killed-mark))
8359
8360 (defun gnus-summary-clear-above (&optional score)
8361   "Clear all marks from articles with score above SCORE."
8362   (interactive "P")
8363   (gnus-summary-mark-above score gnus-unread-mark))
8364
8365 (defun gnus-summary-tick-above (&optional score)
8366   "Tick all articles with score above SCORE."
8367   (interactive "P")
8368   (gnus-summary-mark-above score gnus-ticked-mark))
8369
8370 (defun gnus-summary-mark-above (score mark)
8371   "Mark articles with score over SCORE with MARK."
8372   (interactive "P\ncMark: ")
8373   (setq score (if score
8374                   (prefix-numeric-value score)
8375                 (or gnus-summary-default-score 0)))
8376   (save-excursion
8377     (set-buffer gnus-summary-buffer)
8378     (goto-char (point-min))
8379     (while (and (progn
8380                   (when (> (gnus-summary-article-score) score)
8381                     (gnus-summary-mark-article nil mark))
8382                   t)
8383                 (gnus-summary-find-next)))))
8384
8385 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8386 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8387 (defun gnus-summary-limit-include-expunged (&optional no-error)
8388   "Display all the hidden articles that were expunged for low scores."
8389   (interactive)
8390   (let ((buffer-read-only nil))
8391     (let ((scored gnus-newsgroup-scored)
8392           headers h)
8393       (while scored
8394         (unless (gnus-summary-goto-subject (caar scored))
8395           (and (setq h (gnus-summary-article-header (caar scored)))
8396                (< (cdar scored) gnus-summary-expunge-below)
8397                (push h headers)))
8398         (setq scored (cdr scored)))
8399       (if (not headers)
8400           (when (not no-error)
8401             (error "No expunged articles hidden"))
8402         (goto-char (point-min))
8403         (gnus-summary-prepare-unthreaded (nreverse headers))
8404         (goto-char (point-min))
8405         (gnus-summary-position-point)
8406         t))))
8407
8408 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8409   "Mark all unread articles in this newsgroup as read.
8410 If prefix argument ALL is non-nil, ticked and dormant articles will
8411 also be marked as read.
8412 If QUIETLY is non-nil, no questions will be asked.
8413 If TO-HERE is non-nil, it should be a point in the buffer.  All
8414 articles before this point will be marked as read.
8415 Note that this function will only catch up the unread article
8416 in the current summary buffer limitation.
8417 The number of articles marked as read is returned."
8418   (interactive "P")
8419   (prog1
8420       (save-excursion
8421         (when (or quietly
8422                   (not gnus-interactive-catchup) ;Without confirmation?
8423                   gnus-expert-user
8424                   (gnus-y-or-n-p
8425                    (if all
8426                        "Mark absolutely all articles as read? "
8427                      "Mark all unread articles as read? ")))
8428           (if (and not-mark
8429                    (not gnus-newsgroup-adaptive)
8430                    (not gnus-newsgroup-auto-expire)
8431                    (not gnus-suppress-duplicates)
8432                    (or (not gnus-use-cache)
8433                        (eq gnus-use-cache 'passive)))
8434               (progn
8435                 (when all
8436                   (setq gnus-newsgroup-marked nil
8437                         gnus-newsgroup-dormant nil))
8438                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8439             ;; We actually mark all articles as canceled, which we
8440             ;; have to do when using auto-expiry or adaptive scoring.
8441             (gnus-summary-show-all-threads)
8442             (when (gnus-summary-first-subject (not all) t)
8443               (while (and
8444                       (if to-here (< (point) to-here) t)
8445                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8446                       (gnus-summary-find-next (not all) nil nil t))))
8447             (gnus-set-mode-line 'summary))
8448           t))
8449     (gnus-summary-position-point)))
8450
8451 (defun gnus-summary-catchup-to-here (&optional all)
8452   "Mark all unticked articles before the current one as read.
8453 If ALL is non-nil, also mark ticked and dormant articles as read."
8454   (interactive "P")
8455   (save-excursion
8456     (gnus-save-hidden-threads
8457       (let ((beg (point)))
8458         ;; We check that there are unread articles.
8459         (when (or all (gnus-summary-find-prev))
8460           (gnus-summary-catchup all t beg)))))
8461   (gnus-summary-position-point))
8462
8463 (defun gnus-summary-catchup-all (&optional quietly)
8464   "Mark all articles in this newsgroup as read."
8465   (interactive "P")
8466   (gnus-summary-catchup t quietly))
8467
8468 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8469   "Mark all unread articles in this group as read, then exit.
8470 If prefix argument ALL is non-nil, all articles are marked as read."
8471   (interactive "P")
8472   (when (gnus-summary-catchup all quietly nil 'fast)
8473     ;; Select next newsgroup or exit.
8474     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8475              (eq gnus-auto-select-next 'quietly))
8476         (gnus-summary-next-group nil)
8477       (gnus-summary-exit))))
8478
8479 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8480   "Mark all articles in this newsgroup as read, and then exit."
8481   (interactive "P")
8482   (gnus-summary-catchup-and-exit t quietly))
8483
8484 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8485   "Mark all articles in this group as read and select the next group.
8486 If given a prefix, mark all articles, unread as well as ticked, as
8487 read."
8488   (interactive "P")
8489   (save-excursion
8490     (gnus-summary-catchup all))
8491   (gnus-summary-next-group))
8492
8493 ;; Thread-based commands.
8494
8495 (defun gnus-summary-articles-in-thread (&optional article)
8496   "Return a list of all articles in the current thread.
8497 If ARTICLE is non-nil, return all articles in the thread that starts
8498 with that article."
8499   (let* ((article (or article (gnus-summary-article-number)))
8500          (data (gnus-data-find-list article))
8501          (top-level (gnus-data-level (car data)))
8502          (top-subject
8503           (cond ((null gnus-thread-operation-ignore-subject)
8504                  (gnus-simplify-subject-re
8505                   (mail-header-subject (gnus-data-header (car data)))))
8506                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8507                  (gnus-simplify-subject-fuzzy
8508                   (mail-header-subject (gnus-data-header (car data)))))
8509                 (t nil)))
8510          (end-point (save-excursion
8511                       (if (gnus-summary-go-to-next-thread)
8512                           (point) (point-max))))
8513          articles)
8514     (while (and data
8515                 (< (gnus-data-pos (car data)) end-point))
8516       (when (or (not top-subject)
8517                 (string= top-subject
8518                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8519                              (gnus-simplify-subject-fuzzy
8520                               (mail-header-subject
8521                                (gnus-data-header (car data))))
8522                            (gnus-simplify-subject-re
8523                             (mail-header-subject
8524                              (gnus-data-header (car data)))))))
8525         (push (gnus-data-number (car data)) articles))
8526       (unless (and (setq data (cdr data))
8527                    (> (gnus-data-level (car data)) top-level))
8528         (setq data nil)))
8529     ;; Return the list of articles.
8530     (nreverse articles)))
8531
8532 (defun gnus-summary-rethread-current ()
8533   "Rethread the thread the current article is part of."
8534   (interactive)
8535   (let* ((gnus-show-threads t)
8536          (article (gnus-summary-article-number))
8537          (id (mail-header-id (gnus-summary-article-header)))
8538          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8539     (unless id
8540       (error "No article on the current line"))
8541     (gnus-rebuild-thread id)
8542     (gnus-summary-goto-subject article)))
8543
8544 (defun gnus-summary-reparent-thread ()
8545   "Make the current article child of the marked (or previous) article.
8546
8547 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8548 is non-nil or the Subject: of both articles are the same."
8549   (interactive)
8550   (unless (not (gnus-group-read-only-p))
8551     (error "The current newsgroup does not support article editing"))
8552   (unless (<= (length gnus-newsgroup-processable) 1)
8553     (error "No more than one article may be marked"))
8554   (save-window-excursion
8555     (let ((gnus-article-buffer " *reparent*")
8556           (current-article (gnus-summary-article-number))
8557           ;; First grab the marked article, otherwise one line up.
8558           (parent-article (if (not (null gnus-newsgroup-processable))
8559                               (car gnus-newsgroup-processable)
8560                             (save-excursion
8561                               (if (eq (forward-line -1) 0)
8562                                   (gnus-summary-article-number)
8563                                 (error "Beginning of summary buffer"))))))
8564       (unless (not (eq current-article parent-article))
8565         (error "An article may not be self-referential"))
8566       (let ((message-id (mail-header-id
8567                          (gnus-summary-article-header parent-article))))
8568         (unless (and message-id (not (equal message-id "")))
8569           (error "No message-id in desired parent"))
8570         (gnus-with-article current-article
8571           (goto-char (point-min))
8572           (if (re-search-forward "^References: " nil t)
8573               (progn
8574                 (re-search-forward "^[^ \t]" nil t)
8575                 (forward-line -1)
8576                 (end-of-line)
8577                 (insert " " message-id))
8578             (insert "References: " message-id "\n")))
8579         (set-buffer gnus-summary-buffer)
8580         (gnus-summary-unmark-all-processable)
8581         (gnus-summary-update-article current-article)
8582         (gnus-summary-rethread-current)
8583         (gnus-message 3 "Article %d is now the child of article %d"
8584                       current-article parent-article)))))
8585
8586 (defun gnus-summary-toggle-threads (&optional arg)
8587   "Toggle showing conversation threads.
8588 If ARG is positive number, turn showing conversation threads on."
8589   (interactive "P")
8590   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8591     (setq gnus-show-threads
8592           (if (null arg) (not gnus-show-threads)
8593             (> (prefix-numeric-value arg) 0)))
8594     (gnus-summary-prepare)
8595     (gnus-summary-goto-subject current)
8596     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8597     (gnus-summary-position-point)))
8598
8599 (defun gnus-summary-show-all-threads ()
8600   "Show all threads."
8601   (interactive)
8602   (save-excursion
8603     (let ((buffer-read-only nil))
8604       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8605   (gnus-summary-position-point))
8606
8607 (defun gnus-summary-show-thread ()
8608   "Show thread subtrees.
8609 Returns nil if no thread was there to be shown."
8610   (interactive)
8611   (let ((buffer-read-only nil)
8612         (orig (point))
8613         ;; first goto end then to beg, to have point at beg after let
8614         (end (progn (end-of-line) (point)))
8615         (beg (progn (beginning-of-line) (point))))
8616     (prog1
8617         ;; Any hidden lines here?
8618         (search-forward "\r" end t)
8619       (subst-char-in-region beg end ?\^M ?\n t)
8620       (goto-char orig)
8621       (gnus-summary-position-point))))
8622
8623 (defun gnus-summary-hide-all-threads ()
8624   "Hide all thread subtrees."
8625   (interactive)
8626   (save-excursion
8627     (goto-char (point-min))
8628     (gnus-summary-hide-thread)
8629     (while (zerop (gnus-summary-next-thread 1 t))
8630       (gnus-summary-hide-thread)))
8631   (gnus-summary-position-point))
8632
8633 (defun gnus-summary-hide-thread ()
8634   "Hide thread subtrees.
8635 Returns nil if no threads were there to be hidden."
8636   (interactive)
8637   (let ((buffer-read-only nil)
8638         (start (point))
8639         (article (gnus-summary-article-number)))
8640     (goto-char start)
8641     ;; Go forward until either the buffer ends or the subthread
8642     ;; ends.
8643     (when (and (not (eobp))
8644                (or (zerop (gnus-summary-next-thread 1 t))
8645                    (goto-char (point-max))))
8646       (prog1
8647           (if (and (> (point) start)
8648                    (search-backward "\n" start t))
8649               (progn
8650                 (subst-char-in-region start (point) ?\n ?\^M)
8651                 (gnus-summary-goto-subject article))
8652             (goto-char start)
8653             nil)))))
8654
8655 (defun gnus-summary-go-to-next-thread (&optional previous)
8656   "Go to the same level (or less) next thread.
8657 If PREVIOUS is non-nil, go to previous thread instead.
8658 Return the article number moved to, or nil if moving was impossible."
8659   (let ((level (gnus-summary-thread-level))
8660         (way (if previous -1 1))
8661         (beg (point)))
8662     (forward-line way)
8663     (while (and (not (eobp))
8664                 (< level (gnus-summary-thread-level)))
8665       (forward-line way))
8666     (if (eobp)
8667         (progn
8668           (goto-char beg)
8669           nil)
8670       (setq beg (point))
8671       (prog1
8672           (gnus-summary-article-number)
8673         (goto-char beg)))))
8674
8675 (defun gnus-summary-next-thread (n &optional silent)
8676   "Go to the same level next N'th thread.
8677 If N is negative, search backward instead.
8678 Returns the difference between N and the number of skips actually
8679 done.
8680
8681 If SILENT, don't output messages."
8682   (interactive "p")
8683   (let ((backward (< n 0))
8684         (n (abs n)))
8685     (while (and (> n 0)
8686                 (gnus-summary-go-to-next-thread backward))
8687       (decf n))
8688     (unless silent
8689       (gnus-summary-position-point))
8690     (when (and (not silent) (/= 0 n))
8691       (gnus-message 7 "No more threads"))
8692     n))
8693
8694 (defun gnus-summary-prev-thread (n)
8695   "Go to the same level previous N'th thread.
8696 Returns the difference between N and the number of skips actually
8697 done."
8698   (interactive "p")
8699   (gnus-summary-next-thread (- n)))
8700
8701 (defun gnus-summary-go-down-thread ()
8702   "Go down one level in the current thread."
8703   (let ((children (gnus-summary-article-children)))
8704     (when children
8705       (gnus-summary-goto-subject (car children)))))
8706
8707 (defun gnus-summary-go-up-thread ()
8708   "Go up one level in the current thread."
8709   (let ((parent (gnus-summary-article-parent)))
8710     (when parent
8711       (gnus-summary-goto-subject parent))))
8712
8713 (defun gnus-summary-down-thread (n)
8714   "Go down thread N steps.
8715 If N is negative, go up instead.
8716 Returns the difference between N and how many steps down that were
8717 taken."
8718   (interactive "p")
8719   (let ((up (< n 0))
8720         (n (abs n)))
8721     (while (and (> n 0)
8722                 (if up (gnus-summary-go-up-thread)
8723                   (gnus-summary-go-down-thread)))
8724       (setq n (1- n)))
8725     (gnus-summary-position-point)
8726     (when (/= 0 n)
8727       (gnus-message 7 "Can't go further"))
8728     n))
8729
8730 (defun gnus-summary-up-thread (n)
8731   "Go up thread N steps.
8732 If N is negative, go up instead.
8733 Returns the difference between N and how many steps down that were
8734 taken."
8735   (interactive "p")
8736   (gnus-summary-down-thread (- n)))
8737
8738 (defun gnus-summary-top-thread ()
8739   "Go to the top of the thread."
8740   (interactive)
8741   (while (gnus-summary-go-up-thread))
8742   (gnus-summary-article-number))
8743
8744 (defun gnus-summary-kill-thread (&optional unmark)
8745   "Mark articles under current thread as read.
8746 If the prefix argument is positive, remove any kinds of marks.
8747 If the prefix argument is negative, tick articles instead."
8748   (interactive "P")
8749   (when unmark
8750     (setq unmark (prefix-numeric-value unmark)))
8751   (let ((articles (gnus-summary-articles-in-thread)))
8752     (save-excursion
8753       ;; Expand the thread.
8754       (gnus-summary-show-thread)
8755       ;; Mark all the articles.
8756       (while articles
8757         (gnus-summary-goto-subject (car articles))
8758         (cond ((null unmark)
8759                (gnus-summary-mark-article-as-read gnus-killed-mark))
8760               ((> unmark 0)
8761                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8762               (t
8763                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8764         (setq articles (cdr articles))))
8765     ;; Hide killed subtrees.
8766     (and (null unmark)
8767          gnus-thread-hide-killed
8768          (gnus-summary-hide-thread))
8769     ;; If marked as read, go to next unread subject.
8770     (when (null unmark)
8771       ;; Go to next unread subject.
8772       (gnus-summary-next-subject 1 t)))
8773   (gnus-set-mode-line 'summary))
8774
8775 ;; Summary sorting commands
8776
8777 (defun gnus-summary-sort-by-number (&optional reverse)
8778   "Sort the summary buffer by article number.
8779 Argument REVERSE means reverse order."
8780   (interactive "P")
8781   (gnus-summary-sort 'number reverse))
8782
8783 (defun gnus-summary-sort-by-author (&optional reverse)
8784   "Sort the summary buffer by author name alphabetically.
8785 If case-fold-search is non-nil, case of letters is ignored.
8786 Argument REVERSE means reverse order."
8787   (interactive "P")
8788   (gnus-summary-sort 'author reverse))
8789
8790 (defun gnus-summary-sort-by-subject (&optional reverse)
8791   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8792 If case-fold-search is non-nil, case of letters is ignored.
8793 Argument REVERSE means reverse order."
8794   (interactive "P")
8795   (gnus-summary-sort 'subject reverse))
8796
8797 (defun gnus-summary-sort-by-date (&optional reverse)
8798   "Sort the summary buffer by date.
8799 Argument REVERSE means reverse order."
8800   (interactive "P")
8801   (gnus-summary-sort 'date reverse))
8802
8803 (defun gnus-summary-sort-by-score (&optional reverse)
8804   "Sort the summary buffer by score.
8805 Argument REVERSE means reverse order."
8806   (interactive "P")
8807   (gnus-summary-sort 'score reverse))
8808
8809 (defun gnus-summary-sort-by-lines (&optional reverse)
8810   "Sort the summary buffer by the number of lines.
8811 Argument REVERSE means reverse order."
8812   (interactive "P")
8813   (gnus-summary-sort 'lines reverse))
8814
8815 (defun gnus-summary-sort-by-chars (&optional reverse)
8816   "Sort the summary buffer by article length.
8817 Argument REVERSE means reverse order."
8818   (interactive "P")
8819   (gnus-summary-sort 'chars reverse))   
8820
8821 (defun gnus-summary-sort (predicate reverse)
8822   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8823   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8824          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8825          (gnus-thread-sort-functions
8826           (if (not reverse)
8827               thread
8828             `(lambda (t1 t2)
8829                (,thread t2 t1))))
8830          (gnus-article-sort-functions
8831           (if (not reverse)
8832               article
8833             `(lambda (t1 t2)
8834                (,article t2 t1))))
8835          (buffer-read-only)
8836          (gnus-summary-prepare-hook nil))
8837     ;; We do the sorting by regenerating the threads.
8838     (gnus-summary-prepare)
8839     ;; Hide subthreads if needed.
8840     (when (and gnus-show-threads gnus-thread-hide-subtree)
8841       (gnus-summary-hide-all-threads))))
8842
8843 ;; Summary saving commands.
8844
8845 (defun gnus-summary-save-article (&optional n not-saved)
8846   "Save the current article using the default saver function.
8847 If N is a positive number, save the N next articles.
8848 If N is a negative number, save the N previous articles.
8849 If N is nil and any articles have been marked with the process mark,
8850 save those articles instead.
8851 The variable `gnus-default-article-saver' specifies the saver function."
8852   (interactive "P")
8853   (let* ((articles (gnus-summary-work-articles n))
8854          (save-buffer (save-excursion
8855                         (nnheader-set-temp-buffer " *Gnus Save*")))
8856          (num (length articles))
8857          header file)
8858     (dolist (article articles)
8859       (setq header (gnus-summary-article-header article))
8860       (if (not (vectorp header))
8861           ;; This is a pseudo-article.
8862           (if (assq 'name header)
8863               (gnus-copy-file (cdr (assq 'name header)))
8864             (gnus-message 1 "Article %d is unsaveable" article))
8865         ;; This is a real article.
8866         (save-window-excursion
8867           (gnus-summary-select-article t nil nil article))
8868         (save-excursion
8869           (set-buffer save-buffer)
8870           (erase-buffer)
8871           (insert-buffer-substring gnus-original-article-buffer))
8872         (setq file (gnus-article-save save-buffer file num))
8873         (gnus-summary-remove-process-mark article)
8874         (unless not-saved
8875           (gnus-summary-set-saved-mark article))))
8876     (gnus-kill-buffer save-buffer)
8877     (gnus-summary-position-point)
8878     (gnus-set-mode-line 'summary)
8879     n))
8880
8881 (defun gnus-summary-pipe-output (&optional arg)
8882   "Pipe the current article to a subprocess.
8883 If N is a positive number, pipe the N next articles.
8884 If N is a negative number, pipe the N previous articles.
8885 If N is nil and any articles have been marked with the process mark,
8886 pipe those articles instead."
8887   (interactive "P")
8888   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8889     (gnus-summary-save-article arg t))
8890   (gnus-configure-windows 'pipe))
8891
8892 (defun gnus-summary-save-article-mail (&optional arg)
8893   "Append the current article to an mail file.
8894 If N is a positive number, save the N next articles.
8895 If N is a negative number, save the N previous articles.
8896 If N is nil and any articles have been marked with the process mark,
8897 save those articles instead."
8898   (interactive "P")
8899   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8900     (gnus-summary-save-article arg)))
8901
8902 (defun gnus-summary-save-article-rmail (&optional arg)
8903   "Append the current article to an rmail file.
8904 If N is a positive number, save the N next articles.
8905 If N is a negative number, save the N previous articles.
8906 If N is nil and any articles have been marked with the process mark,
8907 save those articles instead."
8908   (interactive "P")
8909   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8910     (gnus-summary-save-article arg)))
8911
8912 (defun gnus-summary-save-article-file (&optional arg)
8913   "Append the current article to a file.
8914 If N is a positive number, save the N next articles.
8915 If N is a negative number, save the N previous articles.
8916 If N is nil and any articles have been marked with the process mark,
8917 save those articles instead."
8918   (interactive "P")
8919   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8920     (gnus-summary-save-article arg)))
8921
8922 (defun gnus-summary-write-article-file (&optional arg)
8923   "Write the current article to a file, deleting the previous file.
8924 If N is a positive number, save the N next articles.
8925 If N is a negative number, save the N previous articles.
8926 If N is nil and any articles have been marked with the process mark,
8927 save those articles instead."
8928   (interactive "P")
8929   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8930     (gnus-summary-save-article arg)))
8931
8932 (defun gnus-summary-save-article-body-file (&optional arg)
8933   "Append the current article body to a file.
8934 If N is a positive number, save the N next articles.
8935 If N is a negative number, save the N previous articles.
8936 If N is nil and any articles have been marked with the process mark,
8937 save those articles instead."
8938   (interactive "P")
8939   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8940     (gnus-summary-save-article arg)))
8941
8942 (defun gnus-summary-pipe-message (program)
8943   "Pipe the current article through PROGRAM."
8944   (interactive "sProgram: ")
8945   (gnus-summary-select-article)
8946   (let ((mail-header-separator ""))
8947     (gnus-eval-in-buffer-window gnus-article-buffer
8948       (save-restriction
8949         (widen)
8950         (let ((start (window-start))
8951               buffer-read-only)
8952           (message-pipe-buffer-body program)
8953           (set-window-start (get-buffer-window (current-buffer)) start))))))
8954
8955 (defun gnus-get-split-value (methods)
8956   "Return a value based on the split METHODS."
8957   (let (split-name method result match)
8958     (when methods
8959       (save-excursion
8960         (set-buffer gnus-original-article-buffer)
8961         (save-restriction
8962           (nnheader-narrow-to-headers)
8963           (while methods
8964             (goto-char (point-min))
8965             (setq method (pop methods))
8966             (setq match (car method))
8967             (when (cond
8968                    ((stringp match)
8969                     ;; Regular expression.
8970                     (ignore-errors
8971                       (re-search-forward match nil t)))
8972                    ((gnus-functionp match)
8973                     ;; Function.
8974                     (save-restriction
8975                       (widen)
8976                       (setq result (funcall match gnus-newsgroup-name))))
8977                    ((consp match)
8978                     ;; Form.
8979                     (save-restriction
8980                       (widen)
8981                       (setq result (eval match)))))
8982               (setq split-name (append (cdr method) split-name))
8983               (cond ((stringp result)
8984                      (push (expand-file-name
8985                             result gnus-article-save-directory)
8986                            split-name))
8987                     ((consp result)
8988                      (setq split-name (append result split-name)))))))))
8989     (nreverse split-name)))
8990
8991 (defun gnus-valid-move-group-p (group)
8992   (and (boundp group)
8993        (symbol-name group)
8994        (symbol-value group)
8995        (gnus-get-function (gnus-find-method-for-group
8996                            (symbol-name group)) 'request-accept-article t)))
8997
8998 (defun gnus-read-move-group-name (prompt default articles prefix)
8999   "Read a group name."
9000   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9001          (minibuffer-confirm-incomplete nil) ; XEmacs
9002          (prom
9003           (format "%s %s to:"
9004                   prompt
9005                   (if (> (length articles) 1)
9006                       (format "these %d articles" (length articles))
9007                     "this article")))
9008          (to-newsgroup
9009           (cond
9010            ((null split-name)
9011             (gnus-completing-read default prom
9012                                   gnus-active-hashtb
9013                                   'gnus-valid-move-group-p
9014                                   nil prefix
9015                                   'gnus-group-history))
9016            ((= 1 (length split-name))
9017             (gnus-completing-read (car split-name) prom
9018                                   gnus-active-hashtb
9019                                   'gnus-valid-move-group-p
9020                                   nil nil
9021                                   'gnus-group-history))
9022            (t
9023             (gnus-completing-read nil prom
9024                                   (mapcar (lambda (el) (list el))
9025                                           (nreverse split-name))
9026                                   nil nil nil
9027                                   'gnus-group-history)))))
9028     (when to-newsgroup
9029       (if (or (string= to-newsgroup "")
9030               (string= to-newsgroup prefix))
9031           (setq to-newsgroup default))
9032       (unless to-newsgroup
9033         (error "No group name entered"))
9034       (or (gnus-active to-newsgroup)
9035           (gnus-activate-group to-newsgroup)
9036           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9037                                      to-newsgroup))
9038               (or (and (gnus-request-create-group
9039                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
9040                        (gnus-activate-group
9041                         to-newsgroup nil nil
9042                         (gnus-group-name-to-method to-newsgroup))
9043                        (gnus-subscribe-group to-newsgroup))
9044                   (error "Couldn't create group %s" to-newsgroup)))
9045           (error "No such group: %s" to-newsgroup)))
9046     to-newsgroup))
9047
9048 (defun gnus-summary-save-parts (type dir n reverse)
9049   "Save parts matching TYPE to DIR.
9050 If REVERSE, save parts that do not match TYPE."
9051   (interactive
9052    (list (read-string "Save parts of type: " "image/.*")
9053          (read-file-name "Save to directory: " t nil t)
9054          current-prefix-arg))
9055   (gnus-summary-iterate n
9056     (let ((gnus-display-mime-function nil)
9057           (gnus-inhibit-treatment t))
9058       (gnus-summary-select-article))
9059     (save-excursion
9060       (set-buffer gnus-article-buffer)
9061       (let ((handles (or (mm-dissect-buffer gnus-current-headers)
9062                          (mm-uu-dissect))))
9063         (when handles
9064           (gnus-summary-save-parts-1 type dir handles reverse)
9065           (mm-destroy-parts handles))))))
9066
9067 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9068   (if (stringp (car handle))
9069       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9070               (cdr handle))
9071     (when (if reverse
9072               (not (string-match type (mm-handle-media-type handle)))
9073             (string-match type (mm-handle-media-type handle)))
9074       (let ((file (expand-file-name
9075                    (file-name-nondirectory
9076                     (or
9077                      (mail-content-type-get
9078                       (mm-handle-disposition handle) 'filename)
9079                      (concat gnus-newsgroup-name "." gnus-current-article)))
9080                    dir)))
9081         (unless (file-exists-p file)
9082           (mm-save-part-to-file handle file))))))
9083
9084 ;; Summary extract commands
9085
9086 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9087   (let ((buffer-read-only nil)
9088         (article (gnus-summary-article-number))
9089         after-article b e)
9090     (unless (gnus-summary-goto-subject article)
9091       (error "No such article: %d" article))
9092     (gnus-summary-position-point)
9093     ;; If all commands are to be bunched up on one line, we collect
9094     ;; them here.
9095     (unless gnus-view-pseudos-separately
9096       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9097             files action)
9098         (while ps
9099           (setq action (cdr (assq 'action (car ps))))
9100           (setq files (list (cdr (assq 'name (car ps)))))
9101           (while (and ps (cdr ps)
9102                       (string= (or action "1")
9103                                (or (cdr (assq 'action (cadr ps))) "2")))
9104             (push (cdr (assq 'name (cadr ps))) files)
9105             (setcdr ps (cddr ps)))
9106           (when files
9107             (when (not (string-match "%s" action))
9108               (push " " files))
9109             (push " " files)
9110             (when (assq 'execute (car ps))
9111               (setcdr (assq 'execute (car ps))
9112                       (funcall (if (string-match "%s" action)
9113                                    'format 'concat)
9114                                action
9115                                (mapconcat
9116                                 (lambda (f)
9117                                   (if (equal f " ")
9118                                       f
9119                                     (mm-quote-arg f)))
9120                                 files " ")))))
9121           (setq ps (cdr ps)))))
9122     (if (and gnus-view-pseudos (not not-view))
9123         (while pslist
9124           (when (assq 'execute (car pslist))
9125             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9126                                   (eq gnus-view-pseudos 'not-confirm)))
9127           (setq pslist (cdr pslist)))
9128       (save-excursion
9129         (while pslist
9130           (setq after-article (or (cdr (assq 'article (car pslist)))
9131                                   (gnus-summary-article-number)))
9132           (gnus-summary-goto-subject after-article)
9133           (forward-line 1)
9134           (setq b (point))
9135           (insert "    " (file-name-nondirectory
9136                           (cdr (assq 'name (car pslist))))
9137                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9138           (setq e (point))
9139           (forward-line -1)             ; back to `b'
9140           (gnus-add-text-properties
9141            b (1- e) (list 'gnus-number gnus-reffed-article-number
9142                           gnus-mouse-face-prop gnus-mouse-face))
9143           (gnus-data-enter
9144            after-article gnus-reffed-article-number
9145            gnus-unread-mark b (car pslist) 0 (- e b))
9146           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9147           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9148           (setq pslist (cdr pslist)))))))
9149
9150 (defun gnus-pseudos< (p1 p2)
9151   (let ((c1 (cdr (assq 'action p1)))
9152         (c2 (cdr (assq 'action p2))))
9153     (and c1 c2 (string< c1 c2))))
9154
9155 (defun gnus-request-pseudo-article (props)
9156   (cond ((assq 'execute props)
9157          (gnus-execute-command (cdr (assq 'execute props)))))
9158   (let ((gnus-current-article (gnus-summary-article-number)))
9159     (gnus-run-hooks 'gnus-mark-article-hook)))
9160
9161 (defun gnus-execute-command (command &optional automatic)
9162   (save-excursion
9163     (gnus-article-setup-buffer)
9164     (set-buffer gnus-article-buffer)
9165     (setq buffer-read-only nil)
9166     (let ((command (if automatic command
9167                      (read-string "Command: " (cons command 0)))))
9168       (erase-buffer)
9169       (insert "$ " command "\n\n")
9170       (if gnus-view-pseudo-asynchronously
9171           (start-process "gnus-execute" (current-buffer) shell-file-name
9172                          shell-command-switch command)
9173         (call-process shell-file-name nil t nil
9174                       shell-command-switch command)))))
9175
9176 ;; Summary kill commands.
9177
9178 (defun gnus-summary-edit-global-kill (article)
9179   "Edit the \"global\" kill file."
9180   (interactive (list (gnus-summary-article-number)))
9181   (gnus-group-edit-global-kill article))
9182
9183 (defun gnus-summary-edit-local-kill ()
9184   "Edit a local kill file applied to the current newsgroup."
9185   (interactive)
9186   (setq gnus-current-headers (gnus-summary-article-header))
9187   (gnus-group-edit-local-kill
9188    (gnus-summary-article-number) gnus-newsgroup-name))
9189
9190 ;;; Header reading.
9191
9192 (defun gnus-read-header (id &optional header)
9193   "Read the headers of article ID and enter them into the Gnus system."
9194   (let ((group gnus-newsgroup-name)
9195         (gnus-override-method
9196          (or
9197           gnus-override-method
9198           (and (gnus-news-group-p gnus-newsgroup-name)
9199                (car (gnus-refer-article-methods)))))
9200         where)
9201     ;; First we check to see whether the header in question is already
9202     ;; fetched.
9203     (if (stringp id)
9204         ;; This is a Message-ID.
9205         (setq header (or header (gnus-id-to-header id)))
9206       ;; This is an article number.
9207       (setq header (or header (gnus-summary-article-header id))))
9208     (if (and header
9209              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9210         ;; We have found the header.
9211         header
9212       ;; If this is a sparse article, we have to nix out its
9213       ;; previous entry in the thread hashtb.
9214       (when (and header
9215                  (gnus-summary-article-sparse-p (mail-header-number header)))
9216         (let* ((parent (gnus-parent-id (mail-header-references header)))
9217                (thread (and parent (gnus-id-to-thread parent))))
9218           (when thread
9219             (delq (assq header thread) thread))))
9220       ;; We have to really fetch the header to this article.
9221       (save-excursion
9222         (set-buffer nntp-server-buffer)
9223         (when (setq where (gnus-request-head id group))
9224           (nnheader-fold-continuation-lines)
9225           (goto-char (point-max))
9226           (insert ".\n")
9227           (goto-char (point-min))
9228           (insert "211 ")
9229           (princ (cond
9230                   ((numberp id) id)
9231                   ((cdr where) (cdr where))
9232                   (header (mail-header-number header))
9233                   (t gnus-reffed-article-number))
9234                  (current-buffer))
9235           (insert " Article retrieved.\n"))
9236         (if (or (not where)
9237                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9238             ()                          ; Malformed head.
9239           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9240             (when (and (stringp id)
9241                        (not (string= (gnus-group-real-name group)
9242                                      (car where))))
9243               ;; If we fetched by Message-ID and the article came
9244               ;; from a different group, we fudge some bogus article
9245               ;; numbers for this article.
9246               (mail-header-set-number header gnus-reffed-article-number))
9247             (save-excursion
9248               (set-buffer gnus-summary-buffer)
9249               (decf gnus-reffed-article-number)
9250               (gnus-remove-header (mail-header-number header))
9251               (push header gnus-newsgroup-headers)
9252               (setq gnus-current-headers header)
9253               (push (mail-header-number header) gnus-newsgroup-limit)))
9254           header)))))
9255
9256 (defun gnus-remove-header (number)
9257   "Remove header NUMBER from `gnus-newsgroup-headers'."
9258   (if (and gnus-newsgroup-headers
9259            (= number (mail-header-number (car gnus-newsgroup-headers))))
9260       (pop gnus-newsgroup-headers)
9261     (let ((headers gnus-newsgroup-headers))
9262       (while (and (cdr headers)
9263                   (not (= number (mail-header-number (cadr headers)))))
9264         (pop headers))
9265       (when (cdr headers)
9266         (setcdr headers (cddr headers))))))
9267
9268 ;;;
9269 ;;; summary highlights
9270 ;;;
9271
9272 (defun gnus-highlight-selected-summary ()
9273   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9274   ;; Highlight selected article in summary buffer
9275   (when gnus-summary-selected-face
9276     (save-excursion
9277       (let* ((beg (progn (beginning-of-line) (point)))
9278              (end (progn (end-of-line) (point)))
9279              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9280              (from (if (get-text-property beg gnus-mouse-face-prop)
9281                        beg
9282                      (or (next-single-property-change
9283                           beg gnus-mouse-face-prop nil end)
9284                          beg)))
9285              (to
9286               (if (= from end)
9287                   (- from 2)
9288                 (or (next-single-property-change
9289                      from gnus-mouse-face-prop nil end)
9290                     end))))
9291         ;; If no mouse-face prop on line we will have to = from = end,
9292         ;; so we highlight the entire line instead.
9293         (when (= (+ to 2) from)
9294           (setq from beg)
9295           (setq to end))
9296         (if gnus-newsgroup-selected-overlay
9297             ;; Move old overlay.
9298             (gnus-move-overlay
9299              gnus-newsgroup-selected-overlay from to (current-buffer))
9300           ;; Create new overlay.
9301           (gnus-overlay-put
9302            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9303            'face gnus-summary-selected-face))))))
9304
9305 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9306 (defun gnus-summary-highlight-line ()
9307   "Highlight current line according to `gnus-summary-highlight'."
9308   (let* ((list gnus-summary-highlight)
9309          (p (point))
9310          (end (progn (end-of-line) (point)))
9311          ;; now find out where the line starts and leave point there.
9312          (beg (progn (beginning-of-line) (point)))
9313          (article (gnus-summary-article-number))
9314          (score (or (cdr (assq (or article gnus-current-article)
9315                                gnus-newsgroup-scored))
9316                     gnus-summary-default-score 0))
9317          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9318          (inhibit-read-only t))
9319     ;; Eval the cars of the lists until we find a match.
9320     (let ((default gnus-summary-default-score))
9321       (while (and list
9322                   (not (eval (caar list))))
9323         (setq list (cdr list))))
9324     (let ((face (cdar list)))
9325       (unless (eq face (get-text-property beg 'face))
9326         (gnus-put-text-property-excluding-characters-with-faces
9327          beg end 'face
9328          (setq face (if (boundp face) (symbol-value face) face)))
9329         (when gnus-summary-highlight-line-function
9330           (funcall gnus-summary-highlight-line-function article face))))
9331     (goto-char p)))
9332
9333 (defun gnus-update-read-articles (group unread &optional compute)
9334   "Update the list of read articles in GROUP."
9335   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9336          (entry (gnus-gethash group gnus-newsrc-hashtb))
9337          (info (nth 2 entry))
9338          (prev 1)
9339          (unread (sort (copy-sequence unread) '<))
9340          read)
9341     (if (or (not info) (not active))
9342         ;; There is no info on this group if it was, in fact,
9343         ;; killed.  Gnus stores no information on killed groups, so
9344         ;; there's nothing to be done.
9345         ;; One could store the information somewhere temporarily,
9346         ;; perhaps...  Hmmm...
9347         ()
9348       ;; Remove any negative articles numbers.
9349       (while (and unread (< (car unread) 0))
9350         (setq unread (cdr unread)))
9351       ;; Remove any expired article numbers
9352       (while (and unread (< (car unread) (car active)))
9353         (setq unread (cdr unread)))
9354       ;; Compute the ranges of read articles by looking at the list of
9355       ;; unread articles.
9356       (while unread
9357         (when (/= (car unread) prev)
9358           (push (if (= prev (1- (car unread))) prev
9359                   (cons prev (1- (car unread))))
9360                 read))
9361         (setq prev (1+ (car unread)))
9362         (setq unread (cdr unread)))
9363       (when (<= prev (cdr active))
9364         (push (cons prev (cdr active)) read))
9365       (setq read (if (> (length read) 1) (nreverse read) read))
9366       (if compute
9367           read
9368         (save-excursion
9369           (set-buffer gnus-group-buffer)
9370           (gnus-undo-register
9371             `(progn
9372                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9373                (gnus-info-set-read ',info ',(gnus-info-read info))
9374                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9375                (gnus-group-update-group ,group t))))
9376         ;; Propagate the read marks to the backend.
9377         (if (gnus-check-backend-function 'request-set-mark group)
9378             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9379                   (add (gnus-remove-from-range read (gnus-info-read info))))
9380               (when (or add del)
9381                 (unless (gnus-check-group group)
9382                   (error "Can't open server for %s" group))
9383                 (gnus-request-set-mark
9384                  group (delq nil (list (if add (list add 'add '(read)))
9385                                        (if del (list del 'del '(read)))))))))
9386         ;; Enter this list into the group info.
9387         (gnus-info-set-read info read)
9388         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9389         (gnus-get-unread-articles-in-group info (gnus-active group))
9390         t))))
9391
9392 (defun gnus-offer-save-summaries ()
9393   "Offer to save all active summary buffers."
9394   (save-excursion
9395     (let ((buflist (buffer-list))
9396           buffers bufname)
9397       ;; Go through all buffers and find all summaries.
9398       (while buflist
9399         (and (setq bufname (buffer-name (car buflist)))
9400              (string-match "Summary" bufname)
9401              (save-excursion
9402                (set-buffer bufname)
9403                ;; We check that this is, indeed, a summary buffer.
9404                (and (eq major-mode 'gnus-summary-mode)
9405                     ;; Also make sure this isn't bogus.
9406                     gnus-newsgroup-prepared
9407                     ;; Also make sure that this isn't a dead summary buffer.
9408                     (not gnus-dead-summary-mode)))
9409              (push bufname buffers))
9410         (setq buflist (cdr buflist)))
9411       ;; Go through all these summary buffers and offer to save them.
9412       (when buffers
9413         (map-y-or-n-p
9414          "Update summary buffer %s? "
9415          (lambda (buf)
9416            (switch-to-buffer buf)
9417            (gnus-summary-exit))
9418          buffers)))))
9419
9420 (defun gnus-summary-setup-default-charset ()
9421   "Setup newsgroup default charset."
9422   (if (equal gnus-newsgroup-name "nndraft:drafts")
9423       (setq gnus-newsgroup-charset nil)
9424     (let* ((name (and gnus-newsgroup-name
9425                       (gnus-group-real-name gnus-newsgroup-name)))
9426            (ignored-charsets 
9427             (or gnus-newsgroup-ephemeral-ignored-charsets
9428                 (append
9429                  (and gnus-newsgroup-name
9430                       (or (gnus-group-find-parameter gnus-newsgroup-name
9431                                                      'ignored-charsets t)
9432                           (let ((alist gnus-group-ignored-charsets-alist)
9433                                 elem (charsets nil))
9434                             (while (setq elem (pop alist))
9435                               (when (and name
9436                                          (string-match (car elem) name))
9437                                 (setq alist nil
9438                                       charsets (cdr elem))))
9439                             charsets))))
9440                 gnus-newsgroup-ignored-charsets)))
9441       (setq gnus-newsgroup-charset
9442             (or gnus-newsgroup-ephemeral-charset
9443                 (and gnus-newsgroup-name
9444                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9445                          (let ((alist gnus-group-charset-alist)
9446                                elem charset)
9447                            (while (setq elem (pop alist))
9448                              (when (and name
9449                                         (string-match (car elem) name))
9450                                (setq alist nil
9451                                      charset (cadr elem))))
9452                            charset)))
9453                 gnus-default-charset))
9454       (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9455            ignored-charsets))))
9456
9457 ;;;
9458 ;;; Mime Commands
9459 ;;;
9460
9461 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9462   "Display the current article buffer fully MIME-buttonized.
9463 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9464 treated as multipart/mixed."
9465   (interactive "P")
9466   (require 'gnus-art)
9467   (let ((gnus-unbuttonized-mime-types nil)
9468         (gnus-mime-display-multipart-as-mixed show-all-parts))
9469     (gnus-summary-show-article)))
9470
9471 (defun gnus-summary-repair-multipart (article)
9472   "Add a Content-Type header to a multipart article without one."
9473   (interactive (list (gnus-summary-article-number)))
9474   (gnus-with-article article
9475     (message-narrow-to-head)
9476     (goto-char (point-max))
9477     (widen)
9478     (when (search-forward "\n--" nil t)
9479       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9480         (message-narrow-to-head)
9481         (message-remove-header "Mime-Version")
9482         (message-remove-header "Content-Type")
9483         (goto-char (point-max))
9484         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9485                         separator))
9486         (insert "Mime-Version: 1.0\n")
9487         (widen))))
9488   (let (gnus-mark-article-hook)
9489     (gnus-summary-select-article t t nil article)))
9490
9491 (defun gnus-summary-toggle-display-buttonized ()
9492   "Toggle the buttonizing of the article buffer."
9493   (interactive)
9494   (require 'gnus-art)
9495   (if (setq gnus-inhibit-mime-unbuttonizing
9496             (not gnus-inhibit-mime-unbuttonizing))
9497       (let ((gnus-unbuttonized-mime-types nil))
9498         (gnus-summary-show-article))
9499     (gnus-summary-show-article)))
9500
9501 ;;;
9502 ;;; with article
9503 ;;;
9504
9505 (defmacro gnus-with-article (article &rest forms)
9506   "Select ARTICLE and perform FORMS in the original article buffer.
9507 Then replace the article with the result."
9508   `(progn
9509      ;; We don't want the article to be marked as read.
9510      (let (gnus-mark-article-hook)
9511        (gnus-summary-select-article t t nil ,article))
9512      (set-buffer gnus-original-article-buffer)
9513      ,@forms
9514      (if (not (gnus-check-backend-function
9515                'request-replace-article (car gnus-article-current)))
9516          (gnus-message 5 "Read-only group; not replacing")
9517        (unless (gnus-request-replace-article
9518                 ,article (car gnus-article-current)
9519                 (current-buffer) t)
9520          (error "Couldn't replace article")))
9521      ;; The cache and backlog have to be flushed somewhat.
9522      (when gnus-keep-backlog
9523        (gnus-backlog-remove-article
9524         (car gnus-article-current) (cdr gnus-article-current)))
9525      (when gnus-use-cache
9526        (gnus-cache-update-article
9527         (car gnus-article-current) (cdr gnus-article-current)))))
9528
9529 (put 'gnus-with-article 'lisp-indent-function 1)
9530 (put 'gnus-with-article 'edebug-form-spec '(form body))
9531
9532 ;;;
9533 ;;; Generic summary marking commands
9534 ;;;
9535
9536 (defvar gnus-summary-marking-alist
9537   '((read gnus-del-mark "d")
9538     (unread gnus-unread-mark "u")
9539     (ticked gnus-ticked-mark "!")
9540     (dormant gnus-dormant-mark "?")
9541     (expirable gnus-expirable-mark "e"))
9542   "An alist of names/marks/keystrokes.")
9543
9544 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9545 (defvar gnus-summary-mark-map)
9546
9547 (defun gnus-summary-make-all-marking-commands ()
9548   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9549   (dolist (elem gnus-summary-marking-alist)
9550     (apply 'gnus-summary-make-marking-command elem)))
9551
9552 (defun gnus-summary-make-marking-command (name mark keystroke)
9553   (let ((map (make-sparse-keymap)))
9554     (define-key gnus-summary-generic-mark-map keystroke map)
9555     (dolist (lway `((next "next" next nil "n")
9556                     (next-unread "next unread" next t "N")
9557                     (prev "previous" prev nil "p")
9558                     (prev-unread "previous unread" prev t "P")
9559                     (nomove "" nil nil ,keystroke)))
9560       (let ((func (gnus-summary-make-marking-command-1
9561                    mark (car lway) lway name)))
9562         (setq func (eval func))
9563         (define-key map (nth 4 lway) func)))))
9564       
9565 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9566   `(defun ,(intern
9567             (format "gnus-summary-put-mark-as-%s%s"
9568                     name (if (eq way 'nomove)
9569                              ""
9570                            (concat "-" (symbol-name way)))))
9571      (n)
9572      ,(format
9573        "Mark the current article as %s%s.
9574 If N, the prefix, then repeat N times.
9575 If N is negative, move in reverse order.
9576 The difference between N and the actual number of articles marked is
9577 returned."
9578        name (cadr lway))
9579      (interactive "p")
9580      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9581     
9582 (defun gnus-summary-generic-mark (n mark move unread)
9583   "Mark N articles with MARK."
9584   (unless (eq major-mode 'gnus-summary-mode)
9585     (error "This command can only be used in the summary buffer"))
9586   (gnus-summary-show-thread)
9587   (let ((nummove
9588          (cond
9589           ((eq move 'next) 1)
9590           ((eq move 'prev) -1)
9591           (t 0))))
9592     (if (zerop nummove)
9593         (setq n 1)
9594       (when (< n 0)
9595         (setq n (abs n)
9596               nummove (* -1 nummove))))
9597     (while (and (> n 0)
9598                 (gnus-summary-mark-article nil mark)
9599                 (zerop (gnus-summary-next-subject nummove unread t)))
9600       (setq n (1- n)))
9601     (when (/= 0 n)
9602       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9603     (gnus-summary-recenter)
9604     (gnus-summary-position-point)
9605     (gnus-set-mode-line 'summary)
9606     n))
9607
9608 (gnus-summary-make-all-marking-commands)
9609
9610 (gnus-ems-redefine)
9611
9612 (provide 'gnus-sum)
9613
9614 (run-hooks 'gnus-sum-load-hook)
9615
9616 ;;; gnus-sum.el ends here