(gnus-vers): Update to 7.1.0.31.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'mcharset)
32 (require 'gnus)
33 (require 'gnus-group)
34 (require 'gnus-spec)
35 (require 'gnus-range)
36 (require 'gnus-int)
37 (require 'gnus-undo)
38 (require 'gnus-util)
39 (require 'mm-decode)
40 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
41
42 (defcustom gnus-kill-summary-on-exit t
43   "*If non-nil, kill the summary buffer when you exit from it.
44 If nil, the summary will become a \"*Dead Summary*\" buffer, and
45 it will be killed sometime later."
46   :group 'gnus-summary-exit
47   :type 'boolean)
48
49 (defcustom gnus-fetch-old-headers nil
50   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
51 If an unread article in the group refers to an older, already read (or
52 just marked as read) article, the old article will not normally be
53 displayed in the Summary buffer.  If this variable is non-nil, Gnus
54 will attempt to grab the headers to the old articles, and thereby
55 build complete threads.  If it has the value `some', only enough
56 headers to connect otherwise loose threads will be displayed.  This
57 variable can also be a number.  In that case, no more than that number
58 of old headers will be fetched.  If it has the value `invisible', all
59 old headers will be fetched, but none will be displayed.
60
61 The server has to support NOV for any of this to work."
62   :group 'gnus-thread
63   :type '(choice (const :tag "off" nil)
64                  (const some)
65                  number
66                  (sexp :menu-tag "other" t)))
67
68 (defcustom gnus-refer-thread-limit 200
69   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
70 If t, fetch all the available old headers."
71   :group 'gnus-thread
72   :type '(choice number
73                  (sexp :menu-tag "other" t)))
74
75 (defcustom gnus-summary-make-false-root 'adopt
76   "*nil means that Gnus won't gather loose threads.
77 If the root of a thread has expired or been read in a previous
78 session, the information necessary to build a complete thread has been
79 lost.  Instead of having many small sub-threads from this original thread
80 scattered all over the summary buffer, Gnus can gather them.
81
82 If non-nil, Gnus will try to gather all loose sub-threads from an
83 original thread into one large thread.
84
85 If this variable is non-nil, it should be one of `none', `adopt',
86 `dummy' or `empty'.
87
88 If this variable is `none', Gnus will not make a false root, but just
89 present the sub-threads after another.
90 If this variable is `dummy', Gnus will create a dummy root that will
91 have all the sub-threads as children.
92 If this variable is `adopt', Gnus will make one of the \"children\"
93 the parent and mark all the step-children as such.
94 If this variable is `empty', the \"children\" are printed with empty
95 subject fields.  (Or rather, they will be printed with a string
96 given by the `gnus-summary-same-subject' variable.)"
97   :group 'gnus-thread
98   :type '(choice (const :tag "off" nil)
99                  (const none)
100                  (const dummy)
101                  (const adopt)
102                  (const empty)))
103
104 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
105   "*A regexp to match subjects to be excluded from loose thread gathering.
106 As loose thread gathering is done on subjects only, that means that
107 there can be many false gatherings performed.  By rooting out certain
108 common subjects, gathering might become saner."
109   :group 'gnus-thread
110   :type 'regexp)
111
112 (defcustom gnus-summary-gather-subject-limit nil
113   "*Maximum length of subject comparisons when gathering loose threads.
114 Use nil to compare full subjects.  Setting this variable to a low
115 number will help gather threads that have been corrupted by
116 newsreaders chopping off subject lines, but it might also mean that
117 unrelated articles that have subject that happen to begin with the
118 same few characters will be incorrectly gathered.
119
120 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
121 comparing subjects."
122   :group 'gnus-thread
123   :type '(choice (const :tag "off" nil)
124                  (const fuzzy)
125                  (sexp :menu-tag "on" t)))
126
127 (defcustom gnus-simplify-subject-functions nil
128   "List of functions taking a string argument that simplify subjects.
129 The functions are applied recursively.
130
131 Useful functions to put in this list include: `gnus-simplify-subject-re',
132 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
133   :group 'gnus-thread
134   :type '(repeat function))
135
136 (defcustom gnus-simplify-ignored-prefixes nil
137   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
138   :group 'gnus-thread
139   :type '(choice (const :tag "off" nil)
140                  regexp))
141
142 (defcustom gnus-build-sparse-threads nil
143   "*If non-nil, fill in the gaps in threads.
144 If `some', only fill in the gaps that are needed to tie loose threads
145 together.  If `more', fill in all leaf nodes that Gnus can find.  If
146 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
147   :group 'gnus-thread
148   :type '(choice (const :tag "off" nil)
149                  (const some)
150                  (const more)
151                  (sexp :menu-tag "all" t)))
152
153 (defcustom gnus-summary-thread-gathering-function
154   'gnus-gather-threads-by-subject
155   "*Function used for gathering loose threads.
156 There are two pre-defined functions: `gnus-gather-threads-by-subject',
157 which only takes Subjects into consideration; and
158 `gnus-gather-threads-by-references', which compared the References
159 headers of the articles to find matches."
160   :group 'gnus-thread
161   :type '(radio (function-item gnus-gather-threads-by-subject)
162                 (function-item gnus-gather-threads-by-references)
163                 (function :tag "other")))
164
165 (defcustom gnus-summary-same-subject ""
166   "*String indicating that the current article has the same subject as the previous.
167 This variable will only be used if the value of
168 `gnus-summary-make-false-root' is `empty'."
169   :group 'gnus-summary-format
170   :type 'string)
171
172 (defcustom gnus-summary-goto-unread t
173   "*If t, many commands will go to the next unread article.
174 This applies to marking commands as well as other commands that
175 \"naturally\" select the next article, like, for instance, `SPC' at
176 the end of an article.
177
178 If nil, the marking commands do NOT go to the next unread article
179 (they go to the next article instead).  If `never', commands that
180 usually go to the next unread article, will go to the next article,
181 whether it is read or not."
182   :group 'gnus-summary-marks
183   :link '(custom-manual "(gnus)Setting Marks")
184   :type '(choice (const :tag "off" nil)
185                  (const never)
186                  (sexp :menu-tag "on" t)))
187
188 (defcustom gnus-summary-default-score 0
189   "*Default article score level.
190 All scores generated by the score files will be added to this score.
191 If this variable is nil, scoring will be disabled."
192   :group 'gnus-score-default
193   :type '(choice (const :tag "disable")
194                  integer))
195
196 (defcustom gnus-summary-zcore-fuzz 0
197   "*Fuzziness factor for the zcore in the summary buffer.
198 Articles with scores closer than this to `gnus-summary-default-score'
199 will not be marked."
200   :group 'gnus-summary-format
201   :type 'integer)
202
203 (defcustom gnus-simplify-subject-fuzzy-regexp nil
204   "*Strings to be removed when doing fuzzy matches.
205 This can either be a regular expression or list of regular expressions
206 that will be removed from subject strings if fuzzy subject
207 simplification is selected."
208   :group 'gnus-thread
209   :type '(repeat regexp))
210
211 (defcustom gnus-show-threads t
212   "*If non-nil, display threads in summary mode."
213   :group 'gnus-thread
214   :type 'boolean)
215
216 (defcustom gnus-thread-hide-subtree nil
217   "*If non-nil, hide all threads initially.
218 If threads are hidden, you have to run the command
219 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
220 to expose hidden threads."
221   :group 'gnus-thread
222   :type 'boolean)
223
224 (defcustom gnus-thread-hide-killed t
225   "*If non-nil, hide killed threads automatically."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-ignore-subject t
230   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
231 If nil, articles that have different subjects from their parents will
232 start separate threads."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-operation-ignore-subject t
237   "*If non-nil, subjects will be ignored when doing thread commands.
238 This affects commands like `gnus-summary-kill-thread' and
239 `gnus-summary-lower-thread'.
240
241 If this variable is nil, articles in the same thread with different
242 subjects will not be included in the operation in question.  If this
243 variable is `fuzzy', only articles that have subjects that are fuzzily
244 equal will be included."
245   :group 'gnus-thread
246   :type '(choice (const :tag "off" nil)
247                  (const fuzzy)
248                  (sexp :tag "on" t)))
249
250 (defcustom gnus-thread-indent-level 4
251   "*Number that says how much each sub-thread should be indented."
252   :group 'gnus-thread
253   :type 'integer)
254
255 (defcustom gnus-auto-extend-newsgroup t
256   "*If non-nil, extend newsgroup forward and backward when requested."
257   :group 'gnus-summary-choose
258   :type 'boolean)
259
260 (defcustom gnus-auto-select-first t
261   "*If nil, don't select the first unread article when entering a group.
262 If this variable is `best', select the highest-scored unread article
263 in the group.  If t, select the first unread article.
264
265 This variable can also be a function to place point on a likely
266 subject line.  Useful values include `gnus-summary-first-unread-subject',
267 `gnus-summary-first-unread-article' and
268 `gnus-summary-best-unread-article'.
269
270 If you want to prevent automatic selection of the first unread article
271 in some newsgroups, set the variable to nil in
272 `gnus-select-group-hook'."
273   :group 'gnus-group-select
274   :type '(choice (const :tag "none" nil)
275                  (const best)
276                  (sexp :menu-tag "first" t)
277                  (function-item gnus-summary-first-unread-subject)
278                  (function-item gnus-summary-first-unread-article)
279                  (function-item gnus-summary-best-unread-article)))
280
281 (defcustom gnus-auto-select-next t
282   "*If non-nil, offer to go to the next group from the end of the previous.
283 If the value is t and the next newsgroup is empty, Gnus will exit
284 summary mode and go back to group mode.  If the value is neither nil
285 nor t, Gnus will select the following unread newsgroup.  In
286 particular, if the value is the symbol `quietly', the next unread
287 newsgroup will be selected without any confirmation, and if it is
288 `almost-quietly', the next group will be selected without any
289 confirmation if you are located on the last article in the group.
290 Finally, if this variable is `slightly-quietly', the `Z n' command
291 will go to the next group without confirmation."
292   :group 'gnus-summary-maneuvering
293   :type '(choice (const :tag "off" nil)
294                  (const quietly)
295                  (const almost-quietly)
296                  (const slightly-quietly)
297                  (sexp :menu-tag "on" t)))
298
299 (defcustom gnus-auto-select-same nil
300   "*If non-nil, select the next article with the same subject.
301 If there are no more articles with the same subject, go to
302 the first unread article."
303   :group 'gnus-summary-maneuvering
304   :type 'boolean)
305
306 (defcustom gnus-summary-check-current nil
307   "*If non-nil, consider the current article when moving.
308 The \"unread\" movement commands will stay on the same line if the
309 current article is unread."
310   :group 'gnus-summary-maneuvering
311   :type 'boolean)
312
313 (defcustom gnus-auto-center-summary t
314   "*If non-nil, always center the current summary buffer.
315 In particular, if `vertical' do only vertical recentering.  If non-nil
316 and non-`vertical', do both horizontal and vertical recentering."
317   :group 'gnus-summary-maneuvering
318   :type '(choice (const :tag "none" nil)
319                  (const vertical)
320                  (integer :tag "height")
321                  (sexp :menu-tag "both" t)))
322
323 (defcustom gnus-show-all-headers nil
324   "*If non-nil, don't hide any headers."
325   :group 'gnus-article-hiding
326   :group 'gnus-article-headers
327   :type 'boolean)
328
329 (defcustom gnus-summary-ignore-duplicates nil
330   "*If non-nil, ignore articles with identical Message-ID headers."
331   :group 'gnus-summary
332   :type 'boolean)
333
334 (defcustom gnus-single-article-buffer t
335   "*If non-nil, display all articles in the same buffer.
336 If nil, each group will get its own article buffer."
337   :group 'gnus-article-various
338   :type 'boolean)
339
340 (defcustom gnus-break-pages t
341   "*If non-nil, do page breaking on articles.
342 The page delimiter is specified by the `gnus-page-delimiter'
343 variable."
344   :group 'gnus-article-various
345   :type 'boolean)
346
347 (defcustom gnus-move-split-methods nil
348   "*Variable used to suggest where articles are to be moved to.
349 It uses the same syntax as the `gnus-split-methods' variable."
350   :group 'gnus-summary-mail
351   :type '(repeat (choice (list :value (fun) function)
352                          (cons :value ("" "") regexp (repeat string))
353                          (sexp :value nil))))
354
355 (defcustom gnus-unread-mark ?  ;Whitespace
356   "*Mark used for unread articles."
357   :group 'gnus-summary-marks
358   :type 'character)
359
360 (defcustom gnus-ticked-mark ?!
361   "*Mark used for ticked articles."
362   :group 'gnus-summary-marks
363   :type 'character)
364
365 (defcustom gnus-dormant-mark ??
366   "*Mark used for dormant articles."
367   :group 'gnus-summary-marks
368   :type 'character)
369
370 (defcustom gnus-del-mark ?r
371   "*Mark used for del'd articles."
372   :group 'gnus-summary-marks
373   :type 'character)
374
375 (defcustom gnus-read-mark ?R
376   "*Mark used for read articles."
377   :group 'gnus-summary-marks
378   :type 'character)
379
380 (defcustom gnus-expirable-mark ?E
381   "*Mark used for expirable articles."
382   :group 'gnus-summary-marks
383   :type 'character)
384
385 (defcustom gnus-killed-mark ?K
386   "*Mark used for killed articles."
387   :group 'gnus-summary-marks
388   :type 'character)
389
390 (defcustom gnus-souped-mark ?F
391   "*Mark used for killed articles."
392   :group 'gnus-summary-marks
393   :type 'character)
394
395 (defcustom gnus-kill-file-mark ?X
396   "*Mark used for articles killed by kill files."
397   :group 'gnus-summary-marks
398   :type 'character)
399
400 (defcustom gnus-low-score-mark ?Y
401   "*Mark used for articles with a low score."
402   :group 'gnus-summary-marks
403   :type 'character)
404
405 (defcustom gnus-catchup-mark ?C
406   "*Mark used for articles that are caught up."
407   :group 'gnus-summary-marks
408   :type 'character)
409
410 (defcustom gnus-replied-mark ?A
411   "*Mark used for articles that have been replied to."
412   :group 'gnus-summary-marks
413   :type 'character)
414
415 (defcustom gnus-cached-mark ?*
416   "*Mark used for articles that are in the cache."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-saved-mark ?S
421   "*Mark used for articles that have been saved to."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-ancient-mark ?O
426   "*Mark used for ancient articles."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-sparse-mark ?Q
431   "*Mark used for sparsely reffed articles."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-canceled-mark ?G
436   "*Mark used for canceled articles."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-duplicate-mark ?M
441   "*Mark used for duplicate articles."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-undownloaded-mark ?@
446   "*Mark used for articles that weren't downloaded."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-downloadable-mark ?%
451   "*Mark used for articles that are to be downloaded."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-unsendable-mark ?=
456   "*Mark used for articles that won't be sent."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-score-over-mark ?+
461   "*Score mark used for articles with high scores."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-score-below-mark ?-
466   "*Score mark used for articles with low scores."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-empty-thread-mark ?  ;Whitespace
471   "*There is no thread under the article."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-not-empty-thread-mark ?=
476   "*There is a thread under the article."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-view-pseudo-asynchronously nil
481   "*If non-nil, Gnus will view pseudo-articles asynchronously."
482   :group 'gnus-extract-view
483   :type 'boolean)
484
485 (defcustom gnus-auto-expirable-marks
486   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
487         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
488         gnus-souped-mark gnus-duplicate-mark)
489   "*The list of marks converted into expiration if a group is auto-expirable."
490   :group 'gnus-summary
491   :type '(repeat character))
492
493 (defcustom gnus-inhibit-user-auto-expire t
494   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
495   :group 'gnus-summary
496   :type 'boolean)
497
498 (defcustom gnus-view-pseudos nil
499   "*If `automatic', pseudo-articles will be viewed automatically.
500 If `not-confirm', pseudos will be viewed automatically, and the user
501 will not be asked to confirm the command."
502   :group 'gnus-extract-view
503   :type '(choice (const :tag "off" nil)
504                  (const automatic)
505                  (const not-confirm)))
506
507 (defcustom gnus-view-pseudos-separately t
508   "*If non-nil, one pseudo-article will be created for each file to be viewed.
509 If nil, all files that use the same viewing command will be given as a
510 list of parameters to that command."
511   :group 'gnus-extract-view
512   :type 'boolean)
513
514 (defcustom gnus-insert-pseudo-articles t
515   "*If non-nil, insert pseudo-articles when decoding articles."
516   :group 'gnus-extract-view
517   :type 'boolean)
518
519 (defcustom gnus-summary-dummy-line-format
520   "  %(:                          :%) %S\n"
521   "*The format specification for the dummy roots in the summary buffer.
522 It works along the same lines as a normal formatting string,
523 with some simple extensions.
524
525 %S  The subject"
526   :group 'gnus-threading
527   :type 'string)
528
529 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
530   "*The format specification for the summary mode line.
531 It works along the same lines as a normal formatting string,
532 with some simple extensions:
533
534 %G  Group name
535 %p  Unprefixed group name
536 %A  Current article number
537 %z  Current article score
538 %V  Gnus version
539 %U  Number of unread articles in the group
540 %e  Number of unselected articles in the group
541 %Z  A string with unread/unselected article counts
542 %g  Shortish group name
543 %S  Subject of the current article
544 %u  User-defined spec
545 %s  Current score file name
546 %d  Number of dormant articles
547 %r  Number of articles that have been marked as read in this session
548 %E  Number of articles expunged by the score files"
549   :group 'gnus-summary-format
550   :type 'string)
551
552 (defcustom gnus-list-identifiers nil
553   "Regexp that matches list identifiers to be removed from subject.
554 This can also be a list of regexps."
555   :group 'gnus-summary-format
556   :group 'gnus-article-hiding
557   :type '(choice (const :tag "none" nil)
558                  (regexp :value ".*")
559                  (repeat :value (".*") regexp)))
560
561 (defcustom gnus-summary-mark-below 0
562   "*Mark all articles with a score below this variable as read.
563 This variable is local to each summary buffer and usually set by the
564 score file."
565   :group 'gnus-score-default
566   :type 'integer)
567
568 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
569   "*List of functions used for sorting articles in the summary buffer.
570 This variable is only used when not using a threaded display."
571   :group 'gnus-summary-sort
572   :type '(repeat (choice (function-item gnus-article-sort-by-number)
573                          (function-item gnus-article-sort-by-author)
574                          (function-item gnus-article-sort-by-subject)
575                          (function-item gnus-article-sort-by-date)
576                          (function-item gnus-article-sort-by-score)
577                          (function :tag "other"))))
578
579 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
580   "*List of functions used for sorting threads in the summary buffer.
581 By default, threads are sorted by article number.
582
583 Each function takes two threads and return non-nil if the first thread
584 should be sorted before the other.  If you use more than one function,
585 the primary sort function should be the last.  You should probably
586 always include `gnus-thread-sort-by-number' in the list of sorting
587 functions -- preferably first.
588
589 Ready-made functions include `gnus-thread-sort-by-number',
590 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
591 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
592 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
593   :group 'gnus-summary-sort
594   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
595                          (function-item gnus-thread-sort-by-author)
596                          (function-item gnus-thread-sort-by-subject)
597                          (function-item gnus-thread-sort-by-date)
598                          (function-item gnus-thread-sort-by-score)
599                          (function-item gnus-thread-sort-by-total-score)
600                          (function :tag "other"))))
601
602 (defcustom gnus-thread-score-function '+
603   "*Function used for calculating the total score of a thread.
604
605 The function is called with the scores of the article and each
606 subthread and should then return the score of the thread.
607
608 Some functions you can use are `+', `max', or `min'."
609   :group 'gnus-summary-sort
610   :type 'function)
611
612 (defcustom gnus-summary-expunge-below nil
613   "All articles that have a score less than this variable will be expunged.
614 This variable is local to the summary buffers."
615   :group 'gnus-score-default
616   :type '(choice (const :tag "off" nil)
617                  integer))
618
619 (defcustom gnus-thread-expunge-below nil
620   "All threads that have a total score less than this variable will be expunged.
621 See `gnus-thread-score-function' for en explanation of what a
622 \"thread score\" is.
623
624 This variable is local to the summary buffers."
625   :group 'gnus-threading
626   :group 'gnus-score-default
627   :type '(choice (const :tag "off" nil)
628                  integer))
629
630 (defcustom gnus-summary-mode-hook nil
631   "*A hook for Gnus summary mode.
632 This hook is run before any variables are set in the summary buffer."
633   :group 'gnus-summary-various
634   :type 'hook)
635
636 (defcustom gnus-summary-menu-hook nil
637   "*Hook run after the creation of the summary mode menu."
638   :group 'gnus-summary-visual
639   :type 'hook)
640
641 (defcustom gnus-summary-exit-hook nil
642   "*A hook called on exit from the summary buffer.
643 It will be called with point in the group buffer."
644   :group 'gnus-summary-exit
645   :type 'hook)
646
647 (defcustom gnus-summary-prepare-hook nil
648   "*A hook called after the summary buffer has been generated.
649 If you want to modify the summary buffer, you can use this hook."
650   :group 'gnus-summary-various
651   :type 'hook)
652
653 (defcustom gnus-summary-prepared-hook nil
654   "*A hook called as the last thing after the summary buffer has been generated."
655   :group 'gnus-summary-various
656   :type 'hook)
657
658 (defcustom gnus-summary-generate-hook nil
659   "*A hook run just before generating the summary buffer.
660 This hook is commonly used to customize threading variables and the
661 like."
662   :group 'gnus-summary-various
663   :type 'hook)
664
665 (defcustom gnus-select-group-hook nil
666   "*A hook called when a newsgroup is selected.
667
668 If you'd like to simplify subjects like the
669 `gnus-summary-next-same-subject' command does, you can use the
670 following hook:
671
672  (setq gnus-select-group-hook
673       (list
674         (lambda ()
675           (mapcar (lambda (header)
676                      (mail-header-set-subject
677                       header
678                       (gnus-simplify-subject
679                        (mail-header-subject header) 're-only)))
680                   gnus-newsgroup-headers))))"
681   :group 'gnus-group-select
682   :type 'hook)
683
684 (defcustom gnus-select-article-hook nil
685   "*A hook called when an article is selected."
686   :group 'gnus-summary-choose
687   :type 'hook)
688
689 (defcustom gnus-visual-mark-article-hook
690   (list 'gnus-highlight-selected-summary)
691   "*Hook run after selecting an article in the summary buffer.
692 It is meant to be used for highlighting the article in some way.  It
693 is not run if `gnus-visual' is nil."
694   :group 'gnus-summary-visual
695   :type 'hook)
696
697 (defcustom gnus-parse-headers-hook nil
698   "*A hook called before parsing the headers."
699   :group 'gnus-various
700   :type 'hook)
701
702 (defcustom gnus-exit-group-hook nil
703   "*A hook called when exiting summary mode.
704 This hook is not called from the non-updating exit commands like `Q'."
705   :group 'gnus-various
706   :type 'hook)
707
708 (defcustom gnus-summary-update-hook
709   (list 'gnus-summary-highlight-line)
710   "*A hook called when a summary line is changed.
711 The hook will not be called if `gnus-visual' is nil.
712
713 The default function `gnus-summary-highlight-line' will
714 highlight the line according to the `gnus-summary-highlight'
715 variable."
716   :group 'gnus-summary-visual
717   :type 'hook)
718
719 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
720   "*A hook called when an article is selected for the first time.
721 The hook is intended to mark an article as read (or unread)
722 automatically when it is selected."
723   :group 'gnus-summary-choose
724   :type 'hook)
725
726 (defcustom gnus-group-no-more-groups-hook nil
727   "*A hook run when returning to group mode having no more (unread) groups."
728   :group 'gnus-group-select
729   :type 'hook)
730
731 (defcustom gnus-ps-print-hook nil
732   "*A hook run before ps-printing something from Gnus."
733   :group 'gnus-summary
734   :type 'hook)
735
736 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
737   "Face used for highlighting the current article in the summary buffer."
738   :group 'gnus-summary-visual
739   :type 'face)
740
741 (defcustom gnus-summary-highlight
742   '(((= mark gnus-canceled-mark)
743      . gnus-summary-cancelled-face)
744     ((and (> score default)
745           (or (= mark gnus-dormant-mark)
746               (= mark gnus-ticked-mark)))
747      . gnus-summary-high-ticked-face)
748     ((and (< score default)
749           (or (= mark gnus-dormant-mark)
750               (= mark gnus-ticked-mark)))
751      . gnus-summary-low-ticked-face)
752     ((or (= mark gnus-dormant-mark)
753          (= mark gnus-ticked-mark))
754      . gnus-summary-normal-ticked-face)
755     ((and (> score default) (= mark gnus-ancient-mark))
756      . gnus-summary-high-ancient-face)
757     ((and (< score default) (= mark gnus-ancient-mark))
758      . gnus-summary-low-ancient-face)
759     ((= mark gnus-ancient-mark)
760      . gnus-summary-normal-ancient-face)
761     ((and (> score default) (= mark gnus-unread-mark))
762      . gnus-summary-high-unread-face)
763     ((and (< score default) (= mark gnus-unread-mark))
764      . gnus-summary-low-unread-face)
765     ((= mark gnus-unread-mark)
766      . gnus-summary-normal-unread-face)
767     ((and (> score default) (memq mark (list gnus-downloadable-mark
768                                              gnus-undownloaded-mark)))
769      . gnus-summary-high-unread-face)
770     ((and (< score default) (memq mark (list gnus-downloadable-mark
771                                              gnus-undownloaded-mark)))
772      . gnus-summary-low-unread-face)
773     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
774      . gnus-summary-normal-unread-face)
775     ((> score default)
776      . gnus-summary-high-read-face)
777     ((< score default)
778      . gnus-summary-low-read-face)
779     (t
780      . gnus-summary-normal-read-face))
781   "*Controls the highlighting of summary buffer lines.
782
783 A list of (FORM . FACE) pairs.  When deciding how a a particular
784 summary line should be displayed, each form is evaluated.  The content
785 of the face field after the first true form is used.  You can change
786 how those summary lines are displayed, by editing the face field.
787
788 You can use the following variables in the FORM field.
789
790 score:   The articles score
791 default: The default article score.
792 below:   The score below which articles are automatically marked as read.
793 mark:    The articles mark."
794   :group 'gnus-summary-visual
795   :type '(repeat (cons (sexp :tag "Form" nil)
796                        face)))
797
798 (defcustom gnus-alter-header-function nil
799   "Function called to allow alteration of article header structures.
800 The function is called with one parameter, the article header vector,
801 which it may alter in any way.")
802
803 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
804   "Variable that says which function should be used to decode a string with encoded words.")
805
806 (defcustom gnus-extra-headers nil
807   "*Extra headers to parse."
808   :group 'gnus-summary
809   :type '(repeat symbol))
810
811 (defcustom gnus-ignored-from-addresses
812   (and user-mail-address (regexp-quote user-mail-address))
813   "*Regexp of From headers that may be suppressed in favor of To headers."
814   :group 'gnus-summary
815   :type 'regexp)
816
817 (defcustom gnus-group-charset-alist
818   `(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
819     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
820     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
821     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
822     ("^relcom\\>" koi8-r)
823     ("^fido7\\>" koi8-r)
824     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
825     ("^israel\\>" iso-8859-1)
826     ("^han\\>" euc-kr)
827     ("^alt.chinese.text.big5\\>" chinese-big5)
828     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
829     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
830     (".*" ,default-mime-charset))
831   "Alist of regexps (to match group names) and default charsets to be used when reading."
832   :type '(repeat (list (regexp :tag "Group")
833                        (symbol :tag "Charset")))
834   :group 'gnus-charset)
835
836 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
837   "List of charsets that should be ignored.
838 When these charsets are used in the \"charset\" parameter, the
839 default charset will be used instead."
840   :type '(repeat symbol)
841   :group 'gnus-charset)
842
843 (defcustom gnus-group-ignored-charsets-alist 
844   '(("alt\\.chinese\\.text" iso-8859-1))
845   "Alist of regexps (to match group names) and charsets that should be ignored.
846 When these charsets are used in the \"charset\" parameter, the
847 default charset will be used instead."
848   :type '(repeat (cons (regexp :tag "Group")
849                        (repeat symbol)))
850   :group 'gnus-charset)
851
852 (defcustom gnus-group-highlight-words-alist nil
853   "Alist of group regexps and highlight regexps.
854 This variable uses the same syntax as `gnus-emphasis-alist'."
855   :type '(repeat (cons (regexp :tag "Group")
856                        (repeat (list (regexp :tag "Highlight regexp")
857                                      (number :tag "Group for entire word" 0)
858                                      (number :tag "Group for displayed part" 0)
859                                      (symbol :tag "Face" 
860                                              gnus-emphasis-highlight-words)))))
861   :group 'gnus-summary-visual)
862
863 (defcustom gnus-summary-show-article-charset-alist
864   nil
865   "Alist of number and charset.
866 The article will be shown with the charset corresponding to the
867 numbered argument.
868 For example: ((1 . cn-gb-2312) (2 . big5))."
869   :type '(repeat (cons (number :tag "Argument" 1)
870                        (symbol :tag "Charset")))
871   :group 'gnus-charset)
872
873 (defcustom gnus-preserve-marks t
874   "Whether marks are preserved when moving, copying and respooling messages."
875   :type 'boolean
876   :group 'gnus-summary-marks)
877
878 (defcustom gnus-alter-articles-to-read-function nil
879   "Function to be called to alter the list of articles to be selected."
880   :type 'function
881   :group 'gnus-summary)
882
883 ;;; Internal variables
884
885 (defvar gnus-article-mime-handles nil)
886 (defvar gnus-article-decoded-p nil)
887 (defvar gnus-scores-exclude-files nil)
888 (defvar gnus-page-broken nil)
889 (defvar gnus-inhibit-mime-unbuttonizing nil)
890
891 (defvar gnus-original-article nil)
892 (defvar gnus-article-internal-prepare-hook nil)
893 (defvar gnus-newsgroup-process-stack nil)
894
895 (defvar gnus-thread-indent-array nil)
896 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
897 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
898   "Function called to sort the articles within a thread after it has been gathered together.")
899
900 ;; Avoid highlighting in kill files.
901 (defvar gnus-summary-inhibit-highlight nil)
902 (defvar gnus-newsgroup-selected-overlay nil)
903 (defvar gnus-inhibit-limiting nil)
904 (defvar gnus-newsgroup-adaptive-score-file nil)
905 (defvar gnus-current-score-file nil)
906 (defvar gnus-current-move-group nil)
907 (defvar gnus-current-copy-group nil)
908 (defvar gnus-current-crosspost-group nil)
909
910 (defvar gnus-newsgroup-dependencies nil)
911 (defvar gnus-newsgroup-adaptive nil)
912 (defvar gnus-summary-display-article-function nil)
913 (defvar gnus-summary-highlight-line-function nil
914   "Function called after highlighting a summary line.")
915
916 (defvar gnus-summary-line-format-alist
917   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
918     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
919     (?s gnus-tmp-subject-or-nil ?s)
920     (?n gnus-tmp-name ?s)
921     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
922         ?s)
923     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
924             gnus-tmp-from) ?s)
925     (?F gnus-tmp-from ?s)
926     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
927     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
928     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
929     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
930     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
931     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
932     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
933     (?L gnus-tmp-lines ?d)
934     (?I gnus-tmp-indentation ?s)
935     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
936     (?R gnus-tmp-replied ?c)
937     (?\[ gnus-tmp-opening-bracket ?c)
938     (?\] gnus-tmp-closing-bracket ?c)
939     (?\> (make-string gnus-tmp-level ? ) ?s)
940     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
941     (?i gnus-tmp-score ?d)
942     (?z gnus-tmp-score-char ?c)
943     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
944     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
945     (?U gnus-tmp-unread ?c)
946     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
947     (?t (gnus-summary-number-of-articles-in-thread
948          (and (boundp 'thread) (car thread)) gnus-tmp-level)
949         ?d)
950     (?e (gnus-summary-number-of-articles-in-thread
951          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
952         ?c)
953     (?u gnus-tmp-user-defined ?s)
954     (?P (gnus-pick-line-number) ?d))
955   "An alist of format specifications that can appear in summary lines,
956 and what variables they correspond with, along with the type of the
957 variable (string, integer, character, etc).")
958
959 (defvar gnus-summary-dummy-line-format-alist
960   `((?S gnus-tmp-subject ?s)
961     (?N gnus-tmp-number ?d)
962     (?u gnus-tmp-user-defined ?s)))
963
964 (defvar gnus-summary-mode-line-format-alist
965   `((?G gnus-tmp-group-name ?s)
966     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
967     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
968     (?A gnus-tmp-article-number ?d)
969     (?Z gnus-tmp-unread-and-unselected ?s)
970     (?V gnus-version ?s)
971     (?U gnus-tmp-unread-and-unticked ?d)
972     (?S gnus-tmp-subject ?s)
973     (?e gnus-tmp-unselected ?d)
974     (?u gnus-tmp-user-defined ?s)
975     (?d (length gnus-newsgroup-dormant) ?d)
976     (?t (length gnus-newsgroup-marked) ?d)
977     (?r (length gnus-newsgroup-reads) ?d)
978     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
979     (?E gnus-newsgroup-expunged-tally ?d)
980     (?s (gnus-current-score-file-nondirectory) ?s)))
981
982 (defvar gnus-last-search-regexp nil
983   "Default regexp for article search command.")
984
985 (defvar gnus-last-shell-command nil
986   "Default shell command on article.")
987
988 (defvar gnus-newsgroup-begin nil)
989 (defvar gnus-newsgroup-end nil)
990 (defvar gnus-newsgroup-last-rmail nil)
991 (defvar gnus-newsgroup-last-mail nil)
992 (defvar gnus-newsgroup-last-folder nil)
993 (defvar gnus-newsgroup-last-file nil)
994 (defvar gnus-newsgroup-auto-expire nil)
995 (defvar gnus-newsgroup-active nil)
996
997 (defvar gnus-newsgroup-data nil)
998 (defvar gnus-newsgroup-data-reverse nil)
999 (defvar gnus-newsgroup-limit nil)
1000 (defvar gnus-newsgroup-limits nil)
1001
1002 (defvar gnus-newsgroup-unreads nil
1003   "List of unread articles in the current newsgroup.")
1004
1005 (defvar gnus-newsgroup-unselected nil
1006   "List of unselected unread articles in the current newsgroup.")
1007
1008 (defvar gnus-newsgroup-reads nil
1009   "Alist of read articles and article marks in the current newsgroup.")
1010
1011 (defvar gnus-newsgroup-expunged-tally nil)
1012
1013 (defvar gnus-newsgroup-marked nil
1014   "List of ticked articles in the current newsgroup (a subset of unread art).")
1015
1016 (defvar gnus-newsgroup-killed nil
1017   "List of ranges of articles that have been through the scoring process.")
1018
1019 (defvar gnus-newsgroup-cached nil
1020   "List of articles that come from the article cache.")
1021
1022 (defvar gnus-newsgroup-saved nil
1023   "List of articles that have been saved.")
1024
1025 (defvar gnus-newsgroup-kill-headers nil)
1026
1027 (defvar gnus-newsgroup-replied nil
1028   "List of articles that have been replied to in the current newsgroup.")
1029
1030 (defvar gnus-newsgroup-expirable nil
1031   "List of articles in the current newsgroup that can be expired.")
1032
1033 (defvar gnus-newsgroup-processable nil
1034   "List of articles in the current newsgroup that can be processed.")
1035
1036 (defvar gnus-newsgroup-downloadable nil
1037   "List of articles in the current newsgroup that can be processed.")
1038
1039 (defvar gnus-newsgroup-undownloaded nil
1040   "List of articles in the current newsgroup that haven't been downloaded..")
1041
1042 (defvar gnus-newsgroup-unsendable nil
1043   "List of articles in the current newsgroup that won't be sent.")
1044
1045 (defvar gnus-newsgroup-bookmarks nil
1046   "List of articles in the current newsgroup that have bookmarks.")
1047
1048 (defvar gnus-newsgroup-dormant nil
1049   "List of dormant articles in the current newsgroup.")
1050
1051 (defvar gnus-newsgroup-scored nil
1052   "List of scored articles in the current newsgroup.")
1053
1054 (defvar gnus-newsgroup-headers nil
1055   "List of article headers in the current newsgroup.")
1056
1057 (defvar gnus-newsgroup-threads nil)
1058
1059 (defvar gnus-newsgroup-prepared nil
1060   "Whether the current group has been prepared properly.")
1061
1062 (defvar gnus-newsgroup-ancient nil
1063   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1064
1065 (defvar gnus-newsgroup-sparse nil)
1066
1067 (defvar gnus-current-article nil)
1068 (defvar gnus-article-current nil)
1069 (defvar gnus-current-headers nil)
1070 (defvar gnus-have-all-headers nil)
1071 (defvar gnus-last-article nil)
1072 (defvar gnus-newsgroup-history nil)
1073 (defvar gnus-newsgroup-charset nil)
1074 (defvar gnus-newsgroup-ephemeral-charset nil)
1075 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1076
1077 (defconst gnus-summary-local-variables
1078   '(gnus-newsgroup-name
1079     gnus-newsgroup-begin gnus-newsgroup-end
1080     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1081     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1082     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1083     gnus-newsgroup-unselected gnus-newsgroup-marked
1084     gnus-newsgroup-reads gnus-newsgroup-saved
1085     gnus-newsgroup-replied gnus-newsgroup-expirable
1086     gnus-newsgroup-processable gnus-newsgroup-killed
1087     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1088     gnus-newsgroup-unsendable
1089     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1090     gnus-newsgroup-headers gnus-newsgroup-threads
1091     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1092     gnus-current-article gnus-current-headers gnus-have-all-headers
1093     gnus-last-article gnus-article-internal-prepare-hook
1094     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1095     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1096     gnus-thread-expunge-below
1097     gnus-score-alist gnus-current-score-file
1098     (gnus-summary-expunge-below . global)
1099     (gnus-summary-mark-below . global)
1100     gnus-newsgroup-active gnus-scores-exclude-files
1101     gnus-newsgroup-history gnus-newsgroup-ancient
1102     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1103     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1104     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1105     (gnus-newsgroup-expunged-tally . 0)
1106     gnus-cache-removable-articles gnus-newsgroup-cached
1107     gnus-newsgroup-data gnus-newsgroup-data-reverse
1108     gnus-newsgroup-limit gnus-newsgroup-limits
1109     gnus-newsgroup-charset)
1110   "Variables that are buffer-local to the summary buffers.")
1111
1112 ;; Byte-compiler warning.
1113 (defvar gnus-article-mode-map)
1114
1115 ;; MIME stuff.
1116
1117 (defvar gnus-decode-encoded-word-methods
1118   '(mail-decode-encoded-word-string)
1119   "List of methods used to decode encoded words.
1120
1121 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1122 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1123 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1124 whose names match REGEXP.
1125
1126 For example:
1127 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1128  mail-decode-encoded-word-string
1129  (\"chinese\" . rfc1843-decode-string))
1130 ")
1131
1132 (defvar gnus-decode-encoded-word-methods-cache nil)
1133
1134 (defun gnus-multi-decode-encoded-word-string (string)
1135   "Apply the functions from `gnus-encoded-word-methods' that match."
1136   (unless (and gnus-decode-encoded-word-methods-cache
1137                (eq gnus-newsgroup-name
1138                    (car gnus-decode-encoded-word-methods-cache)))
1139     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1140     (mapcar '(lambda (x)
1141                (if (symbolp x)
1142                    (nconc gnus-decode-encoded-word-methods-cache (list x))
1143                  (if (and gnus-newsgroup-name
1144                           (string-match (car x) gnus-newsgroup-name))
1145                      (nconc gnus-decode-encoded-word-methods-cache
1146                             (list (cdr x))))))
1147           gnus-decode-encoded-word-methods))
1148   (let ((xlist gnus-decode-encoded-word-methods-cache))
1149     (pop xlist)
1150     (while xlist
1151       (setq string (funcall (pop xlist) string))))
1152   string)
1153
1154 ;; Subject simplification.
1155
1156 (defun gnus-simplify-whitespace (str)
1157   "Remove excessive whitespace."
1158   (let ((mystr str))
1159     ;; Multiple spaces.
1160     (while (string-match "[ \t][ \t]+" mystr)
1161       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1162                           " "
1163                           (substring mystr (match-end 0)))))
1164     ;; Leading spaces.
1165     (when (string-match "^[ \t]+" mystr)
1166       (setq mystr (substring mystr (match-end 0))))
1167     ;; Trailing spaces.
1168     (when (string-match "[ \t]+$" mystr)
1169       (setq mystr (substring mystr 0 (match-beginning 0))))
1170     mystr))
1171
1172 (defsubst gnus-simplify-subject-re (subject)
1173   "Remove \"Re:\" from subject lines."
1174   (if (string-match "^[Rr][Ee]: *" subject)
1175       (substring subject (match-end 0))
1176     subject))
1177
1178 (defun gnus-simplify-subject (subject &optional re-only)
1179   "Remove `Re:' and words in parentheses.
1180 If RE-ONLY is non-nil, strip leading `Re:'s only."
1181   (let ((case-fold-search t))           ;Ignore case.
1182     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1183     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1184       (setq subject (substring subject (match-end 0))))
1185     ;; Remove uninteresting prefixes.
1186     (when (and (not re-only)
1187                gnus-simplify-ignored-prefixes
1188                (string-match gnus-simplify-ignored-prefixes subject))
1189       (setq subject (substring subject (match-end 0))))
1190     ;; Remove words in parentheses from end.
1191     (unless re-only
1192       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1193         (setq subject (substring subject 0 (match-beginning 0)))))
1194     ;; Return subject string.
1195     subject))
1196
1197 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1198 ;; all whitespace.
1199 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1200   (goto-char (point-min))
1201   (while (re-search-forward regexp nil t)
1202     (replace-match (or newtext ""))))
1203
1204 (defun gnus-simplify-buffer-fuzzy ()
1205   "Simplify string in the buffer fuzzily.
1206 The string in the accessible portion of the current buffer is simplified.
1207 It is assumed to be a single-line subject.
1208 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1209 matter is removed.  Additional things can be deleted by setting
1210 gnus-simplify-subject-fuzzy-regexp."
1211   (let ((case-fold-search t)
1212         (modified-tick))
1213     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1214
1215     (while (not (eq modified-tick (buffer-modified-tick)))
1216       (setq modified-tick (buffer-modified-tick))
1217       (cond
1218        ((listp gnus-simplify-subject-fuzzy-regexp)
1219         (mapcar 'gnus-simplify-buffer-fuzzy-step
1220                 gnus-simplify-subject-fuzzy-regexp))
1221        (gnus-simplify-subject-fuzzy-regexp
1222         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1223       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1224       (gnus-simplify-buffer-fuzzy-step
1225        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1226       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1227
1228     (goto-char (point-min))
1229     (while (re-search-forward " +" nil t)
1230       (delete-region (match-beginning 0) (match-end 0))
1231       (or (bolp) (eobp)
1232           (> (char-after) 127) (> (char-before) 127)
1233           (insert " ")))
1234     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1235     (gnus-simplify-buffer-fuzzy-step " $")
1236     (gnus-simplify-buffer-fuzzy-step "^ +")))
1237
1238 (defun gnus-simplify-subject-fuzzy (subject)
1239   "Simplify a subject string fuzzily.
1240 See `gnus-simplify-buffer-fuzzy' for details."
1241   (let ((fuzzy-regexp gnus-simplify-subject-fuzzy-regexp)) ; Group parameter.
1242     ;; Remove uninteresting prefixes.
1243     (when (and gnus-simplify-ignored-prefixes
1244                (let ((case-fold-search t))
1245                  (string-match gnus-simplify-ignored-prefixes subject)))
1246       (setq subject (substring subject (match-end 0))))
1247     (save-excursion
1248       (gnus-set-work-buffer)
1249       (let ((case-fold-search t)
1250             (gnus-simplify-subject-fuzzy-regexp fuzzy-regexp))
1251         (insert subject)
1252         (inline (gnus-simplify-buffer-fuzzy))
1253         (buffer-string)))))
1254
1255 (defsubst gnus-simplify-subject-fully (subject)
1256   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1257   (cond
1258    (gnus-simplify-subject-functions
1259     (gnus-map-function gnus-simplify-subject-functions subject))
1260    ((null gnus-summary-gather-subject-limit)
1261     (gnus-simplify-subject-re subject))
1262    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1263     (gnus-simplify-subject-fuzzy subject))
1264    ((numberp gnus-summary-gather-subject-limit)
1265     (gnus-limit-string (gnus-simplify-subject-re subject)
1266                        gnus-summary-gather-subject-limit))
1267    (t
1268     subject)))
1269
1270 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1271   "Check whether two subjects are equal.
1272 If optional argument simple-first is t, first argument is already
1273 simplified."
1274   (cond
1275    ((null simple-first)
1276     (equal (gnus-simplify-subject-fully s1)
1277            (gnus-simplify-subject-fully s2)))
1278    (t
1279     (equal s1
1280            (gnus-simplify-subject-fully s2)))))
1281
1282 (defun gnus-summary-bubble-group ()
1283   "Increase the score of the current group.
1284 This is a handy function to add to `gnus-summary-exit-hook' to
1285 increase the score of each group you read."
1286   (gnus-group-add-score gnus-newsgroup-name))
1287
1288 \f
1289 ;;;
1290 ;;; Gnus summary mode
1291 ;;;
1292
1293 (put 'gnus-summary-mode 'mode-class 'special)
1294
1295 (when t
1296   ;; Non-orthogonal keys
1297
1298   (gnus-define-keys gnus-summary-mode-map
1299     " " gnus-summary-next-page
1300     "\177" gnus-summary-prev-page
1301     [delete] gnus-summary-prev-page
1302     [backspace] gnus-summary-prev-page
1303     "\r" gnus-summary-scroll-up
1304     "\M-\r" gnus-summary-scroll-down
1305     "n" gnus-summary-next-unread-article
1306     "p" gnus-summary-prev-unread-article
1307     "N" gnus-summary-next-article
1308     "P" gnus-summary-prev-article
1309     "\M-\C-n" gnus-summary-next-same-subject
1310     "\M-\C-p" gnus-summary-prev-same-subject
1311     "\M-n" gnus-summary-next-unread-subject
1312     "\M-p" gnus-summary-prev-unread-subject
1313     "." gnus-summary-first-unread-article
1314     "," gnus-summary-best-unread-article
1315     "\M-s" gnus-summary-search-article-forward
1316     "\M-r" gnus-summary-search-article-backward
1317     "<" gnus-summary-beginning-of-article
1318     ">" gnus-summary-end-of-article
1319     "j" gnus-summary-goto-article
1320     "^" gnus-summary-refer-parent-article
1321     "\M-^" gnus-summary-refer-article
1322     "u" gnus-summary-tick-article-forward
1323     "!" gnus-summary-tick-article-forward
1324     "U" gnus-summary-tick-article-backward
1325     "d" gnus-summary-mark-as-read-forward
1326     "D" gnus-summary-mark-as-read-backward
1327     "E" gnus-summary-mark-as-expirable
1328     "\M-u" gnus-summary-clear-mark-forward
1329     "\M-U" gnus-summary-clear-mark-backward
1330     "k" gnus-summary-kill-same-subject-and-select
1331     "\C-k" gnus-summary-kill-same-subject
1332     "\M-\C-k" gnus-summary-kill-thread
1333     "\M-\C-l" gnus-summary-lower-thread
1334     "e" gnus-summary-edit-article
1335     "#" gnus-summary-mark-as-processable
1336     "\M-#" gnus-summary-unmark-as-processable
1337     "\M-\C-t" gnus-summary-toggle-threads
1338     "\M-\C-s" gnus-summary-show-thread
1339     "\M-\C-h" gnus-summary-hide-thread
1340     "\M-\C-f" gnus-summary-next-thread
1341     "\M-\C-b" gnus-summary-prev-thread
1342     [(meta down)] gnus-summary-next-thread
1343     [(meta up)] gnus-summary-prev-thread
1344     "\M-\C-u" gnus-summary-up-thread
1345     "\M-\C-d" gnus-summary-down-thread
1346     "&" gnus-summary-execute-command
1347     "c" gnus-summary-catchup-and-exit
1348     "\C-w" gnus-summary-mark-region-as-read
1349     "\C-t" gnus-summary-toggle-truncation
1350     "?" gnus-summary-mark-as-dormant
1351     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1352     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1353     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1354     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1355     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1356     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1357     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1358     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1359     "=" gnus-summary-expand-window
1360     "\C-x\C-s" gnus-summary-reselect-current-group
1361     "\M-g" gnus-summary-rescan-group
1362     "w" gnus-summary-stop-page-breaking
1363     "\C-c\C-r" gnus-summary-caesar-message
1364     "f" gnus-summary-followup
1365     "F" gnus-summary-followup-with-original
1366     "C" gnus-summary-cancel-article
1367     "r" gnus-summary-reply
1368     "R" gnus-summary-reply-with-original
1369     "\C-c\C-f" gnus-summary-mail-forward
1370     "o" gnus-summary-save-article
1371     "\C-o" gnus-summary-save-article-mail
1372     "|" gnus-summary-pipe-output
1373     "\M-k" gnus-summary-edit-local-kill
1374     "\M-K" gnus-summary-edit-global-kill
1375     ;; "V" gnus-version
1376     "\C-c\C-d" gnus-summary-describe-group
1377     "q" gnus-summary-exit
1378     "Q" gnus-summary-exit-no-update
1379     "\C-c\C-i" gnus-info-find-node
1380     gnus-mouse-2 gnus-mouse-pick-article
1381     "m" gnus-summary-mail-other-window
1382     "a" gnus-summary-post-news
1383     "x" gnus-summary-limit-to-unread
1384     "s" gnus-summary-isearch-article
1385     "t" gnus-summary-toggle-header
1386     "g" gnus-summary-show-article
1387     "l" gnus-summary-goto-last-article
1388     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1389     "\C-d" gnus-summary-enter-digest-group
1390     "\M-\C-d" gnus-summary-read-document
1391     "\M-\C-e" gnus-summary-edit-parameters
1392     "\M-\C-a" gnus-summary-customize-parameters
1393     "\C-c\C-b" gnus-bug
1394     "\C-c\C-n" gnus-namazu-search
1395     "*" gnus-cache-enter-article
1396     "\M-*" gnus-cache-remove-article
1397     "\M-&" gnus-summary-universal-argument
1398     "\C-l" gnus-recenter
1399     "I" gnus-summary-increase-score
1400     "L" gnus-summary-lower-score
1401     "\M-i" gnus-symbolic-argument
1402     "h" gnus-summary-select-article-buffer
1403
1404     "b" gnus-article-view-part
1405     "\M-t" gnus-summary-toggle-display-buttonized
1406
1407     "V" gnus-summary-score-map
1408     "X" gnus-uu-extract-map
1409     "S" gnus-summary-send-map)
1410
1411   ;; Sort of orthogonal keymap
1412   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1413     "t" gnus-summary-tick-article-forward
1414     "!" gnus-summary-tick-article-forward
1415     "d" gnus-summary-mark-as-read-forward
1416     "r" gnus-summary-mark-as-read-forward
1417     "c" gnus-summary-clear-mark-forward
1418     " " gnus-summary-clear-mark-forward
1419     "e" gnus-summary-mark-as-expirable
1420     "x" gnus-summary-mark-as-expirable
1421     "?" gnus-summary-mark-as-dormant
1422     "b" gnus-summary-set-bookmark
1423     "B" gnus-summary-remove-bookmark
1424     "#" gnus-summary-mark-as-processable
1425     "\M-#" gnus-summary-unmark-as-processable
1426     "S" gnus-summary-limit-include-expunged
1427     "C" gnus-summary-catchup
1428     "H" gnus-summary-catchup-to-here
1429     "\C-c" gnus-summary-catchup-all
1430     "k" gnus-summary-kill-same-subject-and-select
1431     "K" gnus-summary-kill-same-subject
1432     "P" gnus-uu-mark-map)
1433
1434   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1435     "c" gnus-summary-clear-above
1436     "u" gnus-summary-tick-above
1437     "m" gnus-summary-mark-above
1438     "k" gnus-summary-kill-below)
1439
1440   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1441     "/" gnus-summary-limit-to-subject
1442     "n" gnus-summary-limit-to-articles
1443     "w" gnus-summary-pop-limit
1444     "s" gnus-summary-limit-to-subject
1445     "a" gnus-summary-limit-to-author
1446     "u" gnus-summary-limit-to-unread
1447     "m" gnus-summary-limit-to-marks
1448     "M" gnus-summary-limit-exclude-marks
1449     "v" gnus-summary-limit-to-score
1450     "*" gnus-summary-limit-include-cached
1451     "D" gnus-summary-limit-include-dormant
1452     "T" gnus-summary-limit-include-thread
1453     "d" gnus-summary-limit-exclude-dormant
1454     "t" gnus-summary-limit-to-age
1455     "x" gnus-summary-limit-to-extra 
1456     "E" gnus-summary-limit-include-expunged
1457     "c" gnus-summary-limit-exclude-childless-dormant
1458     "C" gnus-summary-limit-mark-excluded-as-read)
1459
1460   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1461     "n" gnus-summary-next-unread-article
1462     "p" gnus-summary-prev-unread-article
1463     "N" gnus-summary-next-article
1464     "P" gnus-summary-prev-article
1465     "\C-n" gnus-summary-next-same-subject
1466     "\C-p" gnus-summary-prev-same-subject
1467     "\M-n" gnus-summary-next-unread-subject
1468     "\M-p" gnus-summary-prev-unread-subject
1469     "f" gnus-summary-first-unread-article
1470     "b" gnus-summary-best-unread-article
1471     "j" gnus-summary-goto-article
1472     "g" gnus-summary-goto-subject
1473     "l" gnus-summary-goto-last-article
1474     "o" gnus-summary-pop-article)
1475
1476   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1477     "k" gnus-summary-kill-thread
1478     "l" gnus-summary-lower-thread
1479     "i" gnus-summary-raise-thread
1480     "T" gnus-summary-toggle-threads
1481     "t" gnus-summary-rethread-current
1482     "^" gnus-summary-reparent-thread
1483     "s" gnus-summary-show-thread
1484     "S" gnus-summary-show-all-threads
1485     "h" gnus-summary-hide-thread
1486     "H" gnus-summary-hide-all-threads
1487     "n" gnus-summary-next-thread
1488     "p" gnus-summary-prev-thread
1489     "u" gnus-summary-up-thread
1490     "o" gnus-summary-top-thread
1491     "d" gnus-summary-down-thread
1492     "#" gnus-uu-mark-thread
1493     "\M-#" gnus-uu-unmark-thread)
1494
1495   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1496     "g" gnus-summary-prepare
1497     "c" gnus-summary-insert-cached-articles)
1498
1499   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1500     "c" gnus-summary-catchup-and-exit
1501     "C" gnus-summary-catchup-all-and-exit
1502     "E" gnus-summary-exit-no-update
1503     "Q" gnus-summary-exit
1504     "Z" gnus-summary-exit
1505     "n" gnus-summary-catchup-and-goto-next-group
1506     "R" gnus-summary-reselect-current-group
1507     "G" gnus-summary-rescan-group
1508     "N" gnus-summary-next-group
1509     "s" gnus-summary-save-newsrc
1510     "P" gnus-summary-prev-group)
1511
1512   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1513     " " gnus-summary-next-page
1514     "n" gnus-summary-next-page
1515     "\177" gnus-summary-prev-page
1516     [delete] gnus-summary-prev-page
1517     "p" gnus-summary-prev-page
1518     "\r" gnus-summary-scroll-up
1519     "\M-\r" gnus-summary-scroll-down
1520     "<" gnus-summary-beginning-of-article
1521     ">" gnus-summary-end-of-article
1522     "b" gnus-summary-beginning-of-article
1523     "e" gnus-summary-end-of-article
1524     "^" gnus-summary-refer-parent-article
1525     "r" gnus-summary-refer-parent-article
1526     "D" gnus-summary-enter-digest-group
1527     "R" gnus-summary-refer-references
1528     "T" gnus-summary-refer-thread
1529     "g" gnus-summary-show-article
1530     "s" gnus-summary-isearch-article
1531     "P" gnus-summary-print-article
1532     "t" gnus-article-babel)
1533
1534   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1535     "b" gnus-article-add-buttons
1536     "B" gnus-article-add-buttons-to-head
1537     "o" gnus-article-treat-overstrike
1538     "e" gnus-article-emphasize
1539     "w" gnus-article-fill-cited-article
1540     "Q" gnus-article-fill-long-lines
1541     "C" gnus-article-capitalize-sentences
1542     "c" gnus-article-remove-cr
1543     "q" gnus-article-de-quoted-unreadable
1544     "f" gnus-article-display-x-face
1545     "l" gnus-summary-stop-page-breaking
1546     "r" gnus-summary-caesar-message
1547     "t" gnus-article-hide-headers
1548     "v" gnus-summary-verbose-headers
1549     "H" gnus-article-strip-headers-in-body
1550     "d" gnus-article-treat-dumbquotes)
1551
1552   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1553     "a" gnus-article-hide
1554     "h" gnus-article-hide-headers
1555     "b" gnus-article-hide-boring-headers
1556     "s" gnus-article-hide-signature
1557     "c" gnus-article-hide-citation
1558     "C" gnus-article-hide-citation-in-followups
1559     "l" gnus-article-hide-list-identifiers
1560     "p" gnus-article-hide-pgp
1561     "B" gnus-article-strip-banner
1562     "P" gnus-article-hide-pem
1563     "\C-c" gnus-article-hide-citation-maybe)
1564
1565   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1566     "a" gnus-article-highlight
1567     "h" gnus-article-highlight-headers
1568     "c" gnus-article-highlight-citation
1569     "s" gnus-article-highlight-signature)
1570
1571   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1572     "w" gnus-article-decode-mime-words
1573     "c" gnus-article-decode-charset
1574     "v" gnus-mime-view-all-parts
1575     "b" gnus-article-view-part)
1576
1577   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1578     "z" gnus-article-date-ut
1579     "u" gnus-article-date-ut
1580     "l" gnus-article-date-local
1581     "e" gnus-article-date-lapsed
1582     "o" gnus-article-date-original
1583     "i" gnus-article-date-iso8601
1584     "s" gnus-article-date-user)
1585
1586   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1587     "t" gnus-article-remove-trailing-blank-lines
1588     "l" gnus-article-strip-leading-blank-lines
1589     "m" gnus-article-strip-multiple-blank-lines
1590     "a" gnus-article-strip-blank-lines
1591     "A" gnus-article-strip-all-blank-lines
1592     "s" gnus-article-strip-leading-space
1593     "e" gnus-article-strip-trailing-space)
1594
1595   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1596     "v" gnus-version
1597     "f" gnus-summary-fetch-faq
1598     "d" gnus-summary-describe-group
1599     "h" gnus-summary-describe-briefly
1600     "i" gnus-info-find-node)
1601
1602   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1603     "e" gnus-summary-expire-articles
1604     "\M-\C-e" gnus-summary-expire-articles-now
1605     "\177" gnus-summary-delete-article
1606     [delete] gnus-summary-delete-article
1607     [backspace] gnus-summary-delete-article
1608     "m" gnus-summary-move-article
1609     "r" gnus-summary-respool-article
1610     "w" gnus-summary-edit-article
1611     "c" gnus-summary-copy-article
1612     "B" gnus-summary-crosspost-article
1613     "q" gnus-summary-respool-query
1614     "t" gnus-summary-respool-trace
1615     "i" gnus-summary-import-article
1616     "p" gnus-summary-article-posted-p)
1617
1618   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1619     "o" gnus-summary-save-article
1620     "m" gnus-summary-save-article-mail
1621     "F" gnus-summary-write-article-file
1622     "r" gnus-summary-save-article-rmail
1623     "f" gnus-summary-save-article-file
1624     "b" gnus-summary-save-article-body-file
1625     "h" gnus-summary-save-article-folder
1626     "v" gnus-summary-save-article-vm
1627     "p" gnus-summary-pipe-output
1628     "s" gnus-soup-add-article)
1629
1630   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1631     "b" gnus-summary-display-buttonized
1632     "m" gnus-summary-repair-multipart
1633     "v" gnus-article-view-part
1634     "o" gnus-article-save-part
1635     "c" gnus-article-copy-part
1636     "e" gnus-article-externalize-part
1637     "i" gnus-article-inline-part
1638     "|" gnus-article-pipe-part))
1639
1640 (defun gnus-summary-make-menu-bar ()
1641   (gnus-turn-off-edit-menu 'summary)
1642
1643   (unless (boundp 'gnus-summary-misc-menu)
1644
1645     (easy-menu-define
1646      gnus-summary-kill-menu gnus-summary-mode-map ""
1647      (cons
1648       "Score"
1649       (nconc
1650        (list
1651         ["Enter score..." gnus-summary-score-entry t]
1652         ["Customize" gnus-score-customize t])
1653        (gnus-make-score-map 'increase)
1654        (gnus-make-score-map 'lower)
1655        '(("Mark"
1656           ["Kill below" gnus-summary-kill-below t]
1657           ["Mark above" gnus-summary-mark-above t]
1658           ["Tick above" gnus-summary-tick-above t]
1659           ["Clear above" gnus-summary-clear-above t])
1660          ["Current score" gnus-summary-current-score t]
1661          ["Set score" gnus-summary-set-score t]
1662          ["Switch current score file..." gnus-score-change-score-file t]
1663          ["Set mark below..." gnus-score-set-mark-below t]
1664          ["Set expunge below..." gnus-score-set-expunge-below t]
1665          ["Edit current score file" gnus-score-edit-current-scores t]
1666          ["Edit score file" gnus-score-edit-file t]
1667          ["Trace score" gnus-score-find-trace t]
1668          ["Find words" gnus-score-find-favourite-words t]
1669          ["Rescore buffer" gnus-summary-rescore t]
1670          ["Increase score..." gnus-summary-increase-score t]
1671          ["Lower score..." gnus-summary-lower-score t]))))
1672
1673     ;; Define both the Article menu in the summary buffer and the equivalent
1674     ;; Commands menu in the article buffer here for consistency.
1675     (let ((innards
1676            '(("Hide"
1677               ["All" gnus-article-hide t]
1678               ["Headers" gnus-article-hide-headers t]
1679               ["Signature" gnus-article-hide-signature t]
1680               ["Citation" gnus-article-hide-citation t]
1681               ["List identifiers" gnus-article-hide-list-identifiers t]
1682               ["PGP" gnus-article-hide-pgp t]
1683               ["Banner" gnus-article-strip-banner t]
1684               ["Boring headers" gnus-article-hide-boring-headers t])
1685              ("Highlight"
1686               ["All" gnus-article-highlight t]
1687               ["Headers" gnus-article-highlight-headers t]
1688               ["Signature" gnus-article-highlight-signature t]
1689               ["Citation" gnus-article-highlight-citation t])
1690              ("MIME"
1691               ["Words" gnus-article-decode-mime-words t]
1692               ["Charset" gnus-article-decode-charset t]
1693               ["QP" gnus-article-de-quoted-unreadable t]
1694               ["View all" gnus-mime-view-all-parts t])
1695              ("Date"
1696               ["Local" gnus-article-date-local t]
1697               ["ISO8601" gnus-article-date-iso8601 t]
1698               ["UT" gnus-article-date-ut t]
1699               ["Original" gnus-article-date-original t]
1700               ["Lapsed" gnus-article-date-lapsed t]
1701               ["User-defined" gnus-article-date-user t])
1702              ("Washing"
1703               ("Remove Blanks"
1704                ["Leading" gnus-article-strip-leading-blank-lines t]
1705                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1706                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1707                ["All of the above" gnus-article-strip-blank-lines t]
1708                ["All" gnus-article-strip-all-blank-lines t]
1709                ["Leading space" gnus-article-strip-leading-space t]
1710                ["Trailing space" gnus-article-strip-trailing-space t])
1711               ["Overstrike" gnus-article-treat-overstrike t]
1712               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1713               ["Emphasis" gnus-article-emphasize t]
1714               ["Word wrap" gnus-article-fill-cited-article t]
1715               ["Fill long lines" gnus-article-fill-long-lines t]
1716               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1717               ["CR" gnus-article-remove-cr t]
1718               ["Show X-Face" gnus-article-display-x-face t]
1719               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1720               ["Rot 13" gnus-summary-caesar-message t]
1721               ["Unix pipe" gnus-summary-pipe-message t]
1722               ["Add buttons" gnus-article-add-buttons t]
1723               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1724               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1725               ["Verbose header" gnus-summary-verbose-headers t]
1726               ["Toggle header" gnus-summary-toggle-header t]
1727               ["HZ" gnus-article-decode-HZ t])
1728              ("Output"
1729               ["Save in default format" gnus-summary-save-article t]
1730               ["Save in file" gnus-summary-save-article-file t]
1731               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1732               ["Save in MH folder" gnus-summary-save-article-folder t]
1733               ["Save in VM folder" gnus-summary-save-article-vm t]
1734               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1735               ["Save body in file" gnus-summary-save-article-body-file t]
1736               ["Pipe through a filter" gnus-summary-pipe-output t]
1737               ["Add to SOUP packet" gnus-soup-add-article t]
1738               ["Print" gnus-summary-print-article t])
1739              ("Backend"
1740               ["Respool article..." gnus-summary-respool-article t]
1741               ["Move article..." gnus-summary-move-article
1742                (gnus-check-backend-function
1743                 'request-move-article gnus-newsgroup-name)]
1744               ["Copy article..." gnus-summary-copy-article t]
1745               ["Crosspost article..." gnus-summary-crosspost-article
1746                (gnus-check-backend-function
1747                 'request-replace-article gnus-newsgroup-name)]
1748               ["Import file..." gnus-summary-import-article t]
1749               ["Check if posted" gnus-summary-article-posted-p t]
1750               ["Edit article" gnus-summary-edit-article
1751                (not (gnus-group-read-only-p))]
1752               ["Delete article" gnus-summary-delete-article
1753                (gnus-check-backend-function
1754                 'request-expire-articles gnus-newsgroup-name)]
1755               ["Query respool" gnus-summary-respool-query t]
1756               ["Trace respool" gnus-summary-respool-trace t]
1757               ["Delete expirable articles" gnus-summary-expire-articles-now
1758                (gnus-check-backend-function
1759                 'request-expire-articles gnus-newsgroup-name)])
1760              ("Extract"
1761               ["Uudecode" gnus-uu-decode-uu t]
1762               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1763               ["Unshar" gnus-uu-decode-unshar t]
1764               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1765               ["Save" gnus-uu-decode-save t]
1766               ["Binhex" gnus-uu-decode-binhex t]
1767               ["Postscript" gnus-uu-decode-postscript t])
1768              ("Cache"
1769               ["Enter article" gnus-cache-enter-article t]
1770               ["Remove article" gnus-cache-remove-article t])
1771              ["Translate" gnus-article-babel t]
1772              ["Select article buffer" gnus-summary-select-article-buffer t]
1773              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1774              ["Isearch article..." gnus-summary-isearch-article t]
1775              ["Beginning of the article" gnus-summary-beginning-of-article t]
1776              ["End of the article" gnus-summary-end-of-article t]
1777              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1778              ["Fetch referenced articles" gnus-summary-refer-references t]
1779              ["Fetch current thread" gnus-summary-refer-thread t]
1780              ["Fetch article with id..." gnus-summary-refer-article t]
1781              ["Redisplay" gnus-summary-show-article t])))
1782       (easy-menu-define
1783        gnus-summary-article-menu gnus-summary-mode-map ""
1784        (cons "Article" innards))
1785
1786       (easy-menu-define
1787        gnus-article-commands-menu gnus-article-mode-map ""
1788        (cons "Commands" innards)))
1789
1790     (easy-menu-define
1791      gnus-summary-thread-menu gnus-summary-mode-map ""
1792      '("Threads"
1793        ["Toggle threading" gnus-summary-toggle-threads t]
1794        ["Hide threads" gnus-summary-hide-all-threads t]
1795        ["Show threads" gnus-summary-show-all-threads t]
1796        ["Hide thread" gnus-summary-hide-thread t]
1797        ["Show thread" gnus-summary-show-thread t]
1798        ["Go to next thread" gnus-summary-next-thread t]
1799        ["Go to previous thread" gnus-summary-prev-thread t]
1800        ["Go down thread" gnus-summary-down-thread t]
1801        ["Go up thread" gnus-summary-up-thread t]
1802        ["Top of thread" gnus-summary-top-thread t]
1803        ["Mark thread as read" gnus-summary-kill-thread t]
1804        ["Lower thread score" gnus-summary-lower-thread t]
1805        ["Raise thread score" gnus-summary-raise-thread t]
1806        ["Rethread current" gnus-summary-rethread-current t]))
1807
1808     (easy-menu-define
1809      gnus-summary-post-menu gnus-summary-mode-map ""
1810      '("Post"
1811        ["Post an article" gnus-summary-post-news t]
1812        ["Followup" gnus-summary-followup t]
1813        ["Followup and yank" gnus-summary-followup-with-original t]
1814        ["Supersede article" gnus-summary-supersede-article t]
1815        ["Cancel article" gnus-summary-cancel-article t]
1816        ["Reply" gnus-summary-reply t]
1817        ["Reply and yank" gnus-summary-reply-with-original t]
1818        ["Wide reply" gnus-summary-wide-reply t]
1819        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1820        ["Mail forward" gnus-summary-mail-forward t]
1821        ["Post forward" gnus-summary-post-forward t]
1822        ["Digest and mail" gnus-uu-digest-mail-forward t]
1823        ["Digest and post" gnus-uu-digest-post-forward t]
1824        ["Resend message" gnus-summary-resend-message t]
1825        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1826        ["Send a mail" gnus-summary-mail-other-window t]
1827        ["Uuencode and post" gnus-uu-post-news t]
1828        ["Followup via news" gnus-summary-followup-to-mail t]
1829        ["Followup via news and yank"
1830         gnus-summary-followup-to-mail-with-original t]
1831        ;;("Draft"
1832        ;;["Send" gnus-summary-send-draft t]
1833        ;;["Send bounced" gnus-resend-bounced-mail t])
1834        ))
1835
1836     (easy-menu-define
1837      gnus-summary-misc-menu gnus-summary-mode-map ""
1838      '("Misc"
1839        ("Mark Read"
1840         ["Mark as read" gnus-summary-mark-as-read-forward t]
1841         ["Mark same subject and select"
1842          gnus-summary-kill-same-subject-and-select t]
1843         ["Mark same subject" gnus-summary-kill-same-subject t]
1844         ["Catchup" gnus-summary-catchup t]
1845         ["Catchup all" gnus-summary-catchup-all t]
1846         ["Catchup to here" gnus-summary-catchup-to-here t]
1847         ["Catchup region" gnus-summary-mark-region-as-read t]
1848         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1849        ("Mark Various"
1850         ["Tick" gnus-summary-tick-article-forward t]
1851         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1852         ["Remove marks" gnus-summary-clear-mark-forward t]
1853         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1854         ["Set bookmark" gnus-summary-set-bookmark t]
1855         ["Remove bookmark" gnus-summary-remove-bookmark t])
1856        ("Mark Limit"
1857         ["Marks..." gnus-summary-limit-to-marks t]
1858         ["Subject..." gnus-summary-limit-to-subject t]
1859         ["Author..." gnus-summary-limit-to-author t]
1860         ["Age..." gnus-summary-limit-to-age t]
1861         ["Extra..." gnus-summary-limit-to-extra t]
1862         ["Score" gnus-summary-limit-to-score t]
1863         ["Unread" gnus-summary-limit-to-unread t]
1864         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1865         ["Articles" gnus-summary-limit-to-articles t]
1866         ["Pop limit" gnus-summary-pop-limit t]
1867         ["Show dormant" gnus-summary-limit-include-dormant t]
1868         ["Hide childless dormant"
1869          gnus-summary-limit-exclude-childless-dormant t]
1870         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1871         ["Hide marked" gnus-summary-limit-exclude-marks t]
1872         ["Show expunged" gnus-summary-show-all-expunged t])
1873        ("Process Mark"
1874         ["Set mark" gnus-summary-mark-as-processable t]
1875         ["Remove mark" gnus-summary-unmark-as-processable t]
1876         ["Remove all marks" gnus-summary-unmark-all-processable t]
1877         ["Mark above" gnus-uu-mark-over t]
1878         ["Mark series" gnus-uu-mark-series t]
1879         ["Mark region" gnus-uu-mark-region t]
1880         ["Unmark region" gnus-uu-unmark-region t]
1881         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1882         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1883         ["Mark all" gnus-uu-mark-all t]
1884         ["Mark buffer" gnus-uu-mark-buffer t]
1885         ["Mark sparse" gnus-uu-mark-sparse t]
1886         ["Mark thread" gnus-uu-mark-thread t]
1887         ["Unmark thread" gnus-uu-unmark-thread t]
1888         ("Process Mark Sets"
1889          ["Kill" gnus-summary-kill-process-mark t]
1890          ["Yank" gnus-summary-yank-process-mark
1891           gnus-newsgroup-process-stack]
1892          ["Save" gnus-summary-save-process-mark t]))
1893        ("Scroll article"
1894         ["Page forward" gnus-summary-next-page t]
1895         ["Page backward" gnus-summary-prev-page t]
1896         ["Line forward" gnus-summary-scroll-up t])
1897        ("Move"
1898         ["Next unread article" gnus-summary-next-unread-article t]
1899         ["Previous unread article" gnus-summary-prev-unread-article t]
1900         ["Next article" gnus-summary-next-article t]
1901         ["Previous article" gnus-summary-prev-article t]
1902         ["Next unread subject" gnus-summary-next-unread-subject t]
1903         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1904         ["Next article same subject" gnus-summary-next-same-subject t]
1905         ["Previous article same subject" gnus-summary-prev-same-subject t]
1906         ["First unread article" gnus-summary-first-unread-article t]
1907         ["Best unread article" gnus-summary-best-unread-article t]
1908         ["Go to subject number..." gnus-summary-goto-subject t]
1909         ["Go to article number..." gnus-summary-goto-article t]
1910         ["Go to the last article" gnus-summary-goto-last-article t]
1911         ["Pop article off history" gnus-summary-pop-article t])
1912        ("Sort"
1913         ["Sort by number" gnus-summary-sort-by-number t]
1914         ["Sort by author" gnus-summary-sort-by-author t]
1915         ["Sort by subject" gnus-summary-sort-by-subject t]
1916         ["Sort by date" gnus-summary-sort-by-date t]
1917         ["Sort by score" gnus-summary-sort-by-score t]
1918         ["Sort by lines" gnus-summary-sort-by-lines t]
1919         ["Sort by characters" gnus-summary-sort-by-chars t])
1920        ("Help"
1921         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1922         ["Describe group" gnus-summary-describe-group t]
1923         ["Read manual" gnus-info-find-node t])
1924        ("Modes"
1925         ["Pick and read" gnus-pick-mode t]
1926         ["Binary" gnus-binary-mode t])
1927        ("Regeneration"
1928         ["Regenerate" gnus-summary-prepare t]
1929         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1930         ["Toggle threading" gnus-summary-toggle-threads t])
1931        ["Filter articles..." gnus-summary-execute-command t]
1932        ["Run command on subjects..." gnus-summary-universal-argument t]
1933        ["Search articles forward..." gnus-summary-search-article-forward t]
1934        ["Search articles backward..." gnus-summary-search-article-backward t]
1935        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1936        ["Expand window" gnus-summary-expand-window t]
1937        ["Expire expirable articles" gnus-summary-expire-articles
1938         (gnus-check-backend-function
1939          'request-expire-articles gnus-newsgroup-name)]
1940        ["Edit local kill file" gnus-summary-edit-local-kill t]
1941        ["Edit main kill file" gnus-summary-edit-global-kill t]
1942        ["Edit group parameters" gnus-summary-edit-parameters t]
1943        ["Customize group parameters" gnus-summary-customize-parameters t]
1944        ["Send a bug report" gnus-bug t]
1945        ("Exit"
1946         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1947         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1948         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1949         ["Exit group" gnus-summary-exit t]
1950         ["Exit group without updating" gnus-summary-exit-no-update t]
1951         ["Exit and goto next group" gnus-summary-next-group t]
1952         ["Exit and goto prev group" gnus-summary-prev-group t]
1953         ["Reselect group" gnus-summary-reselect-current-group t]
1954         ["Rescan group" gnus-summary-rescan-group t]
1955         ["Update dribble" gnus-summary-save-newsrc t])))
1956
1957     (gnus-run-hooks 'gnus-summary-menu-hook)))
1958
1959 (defun gnus-score-set-default (var value)
1960   "A version of set that updates the GNU Emacs menu-bar."
1961   (set var value)
1962   ;; It is the message that forces the active status to be updated.
1963   (message ""))
1964
1965 (defun gnus-make-score-map (type)
1966   "Make a summary score map of type TYPE."
1967   (if t
1968       nil
1969     (let ((headers '(("author" "from" string)
1970                      ("subject" "subject" string)
1971                      ("article body" "body" string)
1972                      ("article head" "head" string)
1973                      ("xref" "xref" string)
1974                      ("extra header" "extra" string)
1975                      ("lines" "lines" number)
1976                      ("followups to author" "followup" string)))
1977           (types '((number ("less than" <)
1978                            ("greater than" >)
1979                            ("equal" =))
1980                    (string ("substring" s)
1981                            ("exact string" e)
1982                            ("fuzzy string" f)
1983                            ("regexp" r))))
1984           (perms '(("temporary" (current-time-string))
1985                    ("permanent" nil)
1986                    ("immediate" now)))
1987           header)
1988       (list
1989        (apply
1990         'nconc
1991         (list
1992          (if (eq type 'lower)
1993              "Lower score"
1994            "Increase score"))
1995         (let (outh)
1996           (while headers
1997             (setq header (car headers))
1998             (setq outh
1999                   (cons
2000                    (apply
2001                     'nconc
2002                     (list (car header))
2003                     (let ((ts (cdr (assoc (nth 2 header) types)))
2004                           outt)
2005                       (while ts
2006                         (setq outt
2007                               (cons
2008                                (apply
2009                                 'nconc
2010                                 (list (caar ts))
2011                                 (let ((ps perms)
2012                                       outp)
2013                                   (while ps
2014                                     (setq outp
2015                                           (cons
2016                                            (vector
2017                                             (caar ps)
2018                                             (list
2019                                              'gnus-summary-score-entry
2020                                              (nth 1 header)
2021                                              (if (or (string= (nth 1 header)
2022                                                               "head")
2023                                                      (string= (nth 1 header)
2024                                                               "body"))
2025                                                  ""
2026                                                (list 'gnus-summary-header
2027                                                      (nth 1 header)))
2028                                              (list 'quote (nth 1 (car ts)))
2029                                              (list 'gnus-score-delta-default
2030                                                    nil)
2031                                              (nth 1 (car ps))
2032                                              t)
2033                                             t)
2034                                            outp))
2035                                     (setq ps (cdr ps)))
2036                                   (list (nreverse outp))))
2037                                outt))
2038                         (setq ts (cdr ts)))
2039                       (list (nreverse outt))))
2040                    outh))
2041             (setq headers (cdr headers)))
2042           (list (nreverse outh))))))))
2043
2044 \f
2045
2046 (defun gnus-summary-mode (&optional group)
2047   "Major mode for reading articles.
2048
2049 All normal editing commands are switched off.
2050 \\<gnus-summary-mode-map>
2051 Each line in this buffer represents one article.  To read an
2052 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2053 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2054 respectively.
2055
2056 You can also post articles and send mail from this buffer.  To
2057 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2058 of an article, type `\\[gnus-summary-reply]'.
2059
2060 There are approx. one gazillion commands you can execute in this
2061 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2062
2063 The following commands are available:
2064
2065 \\{gnus-summary-mode-map}"
2066   (interactive)
2067   (when (gnus-visual-p 'summary-menu 'menu)
2068     (gnus-summary-make-menu-bar))
2069   (kill-all-local-variables)
2070   (gnus-summary-make-local-variables)
2071   (gnus-make-thread-indent-array)
2072   (gnus-simplify-mode-line)
2073   (setq major-mode 'gnus-summary-mode)
2074   (setq mode-name "Summary")
2075   (make-local-variable 'minor-mode-alist)
2076   (use-local-map gnus-summary-mode-map)
2077   (buffer-disable-undo)
2078   (setq buffer-read-only t)             ;Disable modification
2079   (setq truncate-lines t)
2080   (setq selective-display t)
2081   (setq selective-display-ellipses t)   ;Display `...'
2082   (gnus-summary-set-display-table)
2083   (gnus-set-default-directory)
2084   (setq gnus-newsgroup-name group)
2085   (make-local-variable 'gnus-summary-line-format)
2086   (make-local-variable 'gnus-summary-line-format-spec)
2087   (make-local-variable 'gnus-summary-dummy-line-format)
2088   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2089   (make-local-variable 'gnus-summary-mark-positions)
2090   (make-local-hook 'pre-command-hook)
2091   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2092   (gnus-run-hooks 'gnus-summary-mode-hook)
2093   (mm-enable-multibyte)
2094   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2095   (gnus-update-summary-mark-positions))
2096
2097 (defun gnus-summary-make-local-variables ()
2098   "Make all the local summary buffer variables."
2099   (let (global)
2100     (dolist (local gnus-summary-local-variables)
2101       (if (consp local)
2102           (progn
2103             (if (eq (cdr local) 'global)
2104                 ;; Copy the global value of the variable.
2105                 (setq global (symbol-value (car local)))
2106               ;; Use the value from the list.
2107               (setq global (eval (cdr local))))
2108             (set (make-local-variable (car local)) global))
2109         ;; Simple nil-valued local variable.
2110         (set (make-local-variable local) nil)))))
2111
2112 (defun gnus-summary-clear-local-variables ()
2113   (let ((locals gnus-summary-local-variables))
2114     (while locals
2115       (if (consp (car locals))
2116           (and (vectorp (caar locals))
2117                (set (caar locals) nil))
2118         (and (vectorp (car locals))
2119              (set (car locals) nil)))
2120       (setq locals (cdr locals)))))
2121
2122 ;; Summary data functions.
2123
2124 (defmacro gnus-data-number (data)
2125   `(car ,data))
2126
2127 (defmacro gnus-data-set-number (data number)
2128   `(setcar ,data ,number))
2129
2130 (defmacro gnus-data-mark (data)
2131   `(nth 1 ,data))
2132
2133 (defmacro gnus-data-set-mark (data mark)
2134   `(setcar (nthcdr 1 ,data) ,mark))
2135
2136 (defmacro gnus-data-pos (data)
2137   `(nth 2 ,data))
2138
2139 (defmacro gnus-data-set-pos (data pos)
2140   `(setcar (nthcdr 2 ,data) ,pos))
2141
2142 (defmacro gnus-data-header (data)
2143   `(nth 3 ,data))
2144
2145 (defmacro gnus-data-set-header (data header)
2146   `(setf (nth 3 ,data) ,header))
2147
2148 (defmacro gnus-data-level (data)
2149   `(nth 4 ,data))
2150
2151 (defmacro gnus-data-unread-p (data)
2152   `(= (nth 1 ,data) gnus-unread-mark))
2153
2154 (defmacro gnus-data-read-p (data)
2155   `(/= (nth 1 ,data) gnus-unread-mark))
2156
2157 (defmacro gnus-data-pseudo-p (data)
2158   `(consp (nth 3 ,data)))
2159
2160 (defmacro gnus-data-find (number)
2161   `(assq ,number gnus-newsgroup-data))
2162
2163 (defmacro gnus-data-find-list (number &optional data)
2164   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2165      (memq (assq ,number bdata)
2166            bdata)))
2167
2168 (defmacro gnus-data-make (number mark pos header level)
2169   `(list ,number ,mark ,pos ,header ,level))
2170
2171 (defun gnus-data-enter (after-article number mark pos header level offset)
2172   (let ((data (gnus-data-find-list after-article)))
2173     (unless data
2174       (error "No such article: %d" after-article))
2175     (setcdr data (cons (gnus-data-make number mark pos header level)
2176                        (cdr data)))
2177     (setq gnus-newsgroup-data-reverse nil)
2178     (gnus-data-update-list (cddr data) offset)))
2179
2180 (defun gnus-data-enter-list (after-article list &optional offset)
2181   (when list
2182     (let ((data (and after-article (gnus-data-find-list after-article)))
2183           (ilist list))
2184       (if (not (or data
2185                    after-article))
2186           (let ((odata gnus-newsgroup-data))
2187             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2188             (when offset
2189               (gnus-data-update-list odata offset)))
2190         ;; Find the last element in the list to be spliced into the main
2191         ;; list.
2192         (while (cdr list)
2193           (setq list (cdr list)))
2194         (if (not data)
2195             (progn
2196               (setcdr list gnus-newsgroup-data)
2197               (setq gnus-newsgroup-data ilist)
2198               (when offset
2199                 (gnus-data-update-list (cdr list) offset)))
2200           (setcdr list (cdr data))
2201           (setcdr data ilist)
2202           (when offset
2203             (gnus-data-update-list (cdr list) offset))))
2204       (setq gnus-newsgroup-data-reverse nil))))
2205
2206 (defun gnus-data-remove (article &optional offset)
2207   (let ((data gnus-newsgroup-data))
2208     (if (= (gnus-data-number (car data)) article)
2209         (progn
2210           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2211                 gnus-newsgroup-data-reverse nil)
2212           (when offset
2213             (gnus-data-update-list gnus-newsgroup-data offset)))
2214       (while (cdr data)
2215         (when (= (gnus-data-number (cadr data)) article)
2216           (setcdr data (cddr data))
2217           (when offset
2218             (gnus-data-update-list (cdr data) offset))
2219           (setq data nil
2220                 gnus-newsgroup-data-reverse nil))
2221         (setq data (cdr data))))))
2222
2223 (defmacro gnus-data-list (backward)
2224   `(if ,backward
2225        (or gnus-newsgroup-data-reverse
2226            (setq gnus-newsgroup-data-reverse
2227                  (reverse gnus-newsgroup-data)))
2228      gnus-newsgroup-data))
2229
2230 (defun gnus-data-update-list (data offset)
2231   "Add OFFSET to the POS of all data entries in DATA."
2232   (setq gnus-newsgroup-data-reverse nil)
2233   (while data
2234     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2235     (setq data (cdr data))))
2236
2237 (defun gnus-summary-article-pseudo-p (article)
2238   "Say whether this article is a pseudo article or not."
2239   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2240
2241 (defmacro gnus-summary-article-sparse-p (article)
2242   "Say whether this article is a sparse article or not."
2243   `(memq ,article gnus-newsgroup-sparse))
2244
2245 (defmacro gnus-summary-article-ancient-p (article)
2246   "Say whether this article is a sparse article or not."
2247   `(memq ,article gnus-newsgroup-ancient))
2248
2249 (defun gnus-article-parent-p (number)
2250   "Say whether this article is a parent or not."
2251   (let ((data (gnus-data-find-list number)))
2252     (and (cdr data)                     ; There has to be an article after...
2253          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2254             (gnus-data-level (nth 1 data))))))
2255
2256 (defun gnus-article-children (number)
2257   "Return a list of all children to NUMBER."
2258   (let* ((data (gnus-data-find-list number))
2259          (level (gnus-data-level (car data)))
2260          children)
2261     (setq data (cdr data))
2262     (while (and data
2263                 (= (gnus-data-level (car data)) (1+ level)))
2264       (push (gnus-data-number (car data)) children)
2265       (setq data (cdr data)))
2266     children))
2267
2268 (defmacro gnus-summary-skip-intangible ()
2269   "If the current article is intangible, then jump to a different article."
2270   '(let ((to (get-text-property (point) 'gnus-intangible)))
2271      (and to (gnus-summary-goto-subject to))))
2272
2273 (defmacro gnus-summary-article-intangible-p ()
2274   "Say whether this article is intangible or not."
2275   '(get-text-property (point) 'gnus-intangible))
2276
2277 (defun gnus-article-read-p (article)
2278   "Say whether ARTICLE is read or not."
2279   (not (or (memq article gnus-newsgroup-marked)
2280            (memq article gnus-newsgroup-unreads)
2281            (memq article gnus-newsgroup-unselected)
2282            (memq article gnus-newsgroup-dormant))))
2283
2284 ;; Some summary mode macros.
2285
2286 (defmacro gnus-summary-article-number ()
2287   "The article number of the article on the current line.
2288 If there isn's an article number here, then we return the current
2289 article number."
2290   '(progn
2291      (gnus-summary-skip-intangible)
2292      (or (get-text-property (point) 'gnus-number)
2293          (gnus-summary-last-subject))))
2294
2295 (defmacro gnus-summary-article-header (&optional number)
2296   "Return the header of article NUMBER."
2297   `(gnus-data-header (gnus-data-find
2298                       ,(or number '(gnus-summary-article-number)))))
2299
2300 (defmacro gnus-summary-thread-level (&optional number)
2301   "Return the level of thread that starts with article NUMBER."
2302   `(if (and (eq gnus-summary-make-false-root 'dummy)
2303             (get-text-property (point) 'gnus-intangible))
2304        0
2305      (gnus-data-level (gnus-data-find
2306                        ,(or number '(gnus-summary-article-number))))))
2307
2308 (defmacro gnus-summary-article-mark (&optional number)
2309   "Return the mark of article NUMBER."
2310   `(gnus-data-mark (gnus-data-find
2311                     ,(or number '(gnus-summary-article-number)))))
2312
2313 (defmacro gnus-summary-article-pos (&optional number)
2314   "Return the position of the line of article NUMBER."
2315   `(gnus-data-pos (gnus-data-find
2316                    ,(or number '(gnus-summary-article-number)))))
2317
2318 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2319 (defmacro gnus-summary-article-subject (&optional number)
2320   "Return current subject string or nil if nothing."
2321   `(let ((headers
2322           ,(if number
2323                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2324              '(gnus-data-header (assq (gnus-summary-article-number)
2325                                       gnus-newsgroup-data)))))
2326      (and headers
2327           (vectorp headers)
2328           (mail-header-subject headers))))
2329
2330 (defmacro gnus-summary-article-score (&optional number)
2331   "Return current article score."
2332   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2333                   gnus-newsgroup-scored))
2334        gnus-summary-default-score 0))
2335
2336 (defun gnus-summary-article-children (&optional number)
2337   "Return a list of article numbers that are children of article NUMBER."
2338   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2339          (level (gnus-data-level (car data)))
2340          l children)
2341     (while (and (setq data (cdr data))
2342                 (> (setq l (gnus-data-level (car data))) level))
2343       (and (= (1+ level) l)
2344            (push (gnus-data-number (car data))
2345                  children)))
2346     (nreverse children)))
2347
2348 (defun gnus-summary-article-parent (&optional number)
2349   "Return the article number of the parent of article NUMBER."
2350   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2351                                     (gnus-data-list t)))
2352          (level (gnus-data-level (car data))))
2353     (if (zerop level)
2354         ()                              ; This is a root.
2355       ;; We search until we find an article with a level less than
2356       ;; this one.  That function has to be the parent.
2357       (while (and (setq data (cdr data))
2358                   (not (< (gnus-data-level (car data)) level))))
2359       (and data (gnus-data-number (car data))))))
2360
2361 (defun gnus-unread-mark-p (mark)
2362   "Say whether MARK is the unread mark."
2363   (= mark gnus-unread-mark))
2364
2365 (defun gnus-read-mark-p (mark)
2366   "Say whether MARK is one of the marks that mark as read.
2367 This is all marks except unread, ticked, dormant, and expirable."
2368   (not (or (= mark gnus-unread-mark)
2369            (= mark gnus-ticked-mark)
2370            (= mark gnus-dormant-mark)
2371            (= mark gnus-expirable-mark))))
2372
2373 (defmacro gnus-article-mark (number)
2374   "Return the MARK of article NUMBER.
2375 This macro should only be used when computing the mark the \"first\"
2376 time; i.e., when generating the summary lines.  After that,
2377 `gnus-summary-article-mark' should be used to examine the
2378 marks of articles."
2379   `(cond
2380     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2381     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2382     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2383     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2384     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2385     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2386     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2387     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2388            gnus-ancient-mark))))
2389
2390 ;; Saving hidden threads.
2391
2392 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2393 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2394
2395 (defmacro gnus-save-hidden-threads (&rest forms)
2396   "Save hidden threads, eval FORMS, and restore the hidden threads."
2397   (let ((config (make-symbol "config")))
2398     `(let ((,config (gnus-hidden-threads-configuration)))
2399        (unwind-protect
2400            (save-excursion
2401              ,@forms)
2402          (gnus-restore-hidden-threads-configuration ,config)))))
2403
2404 (defun gnus-data-compute-positions ()
2405   "Compute the positions of all articles."
2406   (setq gnus-newsgroup-data-reverse nil)
2407   (let ((data gnus-newsgroup-data))
2408     (save-excursion
2409       (gnus-save-hidden-threads
2410         (gnus-summary-show-all-threads)
2411         (goto-char (point-min))
2412         (while data
2413           (while (get-text-property (point) 'gnus-intangible)
2414             (forward-line 1))
2415           (gnus-data-set-pos (car data) (+ (point) 3))
2416           (setq data (cdr data))
2417           (forward-line 1))))))
2418
2419 (defun gnus-hidden-threads-configuration ()
2420   "Return the current hidden threads configuration."
2421   (save-excursion
2422     (let (config)
2423       (goto-char (point-min))
2424       (while (search-forward "\r" nil t)
2425         (push (1- (point)) config))
2426       config)))
2427
2428 (defun gnus-restore-hidden-threads-configuration (config)
2429   "Restore hidden threads configuration from CONFIG."
2430   (let (point buffer-read-only)
2431     (while (setq point (pop config))
2432       (when (and (< point (point-max))
2433                  (goto-char point)
2434                  (eq (char-after) ?\n))
2435         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2436
2437 ;; Various summary mode internalish functions.
2438
2439 (defun gnus-mouse-pick-article (e)
2440   (interactive "e")
2441   (mouse-set-point e)
2442   (gnus-summary-next-page nil t))
2443
2444 (defun gnus-summary-set-display-table ()
2445   ;; Change the display table.  Odd characters have a tendency to mess
2446   ;; up nicely formatted displays - we make all possible glyphs
2447   ;; display only a single character.
2448
2449   ;; We start from the standard display table, if any.
2450   (let ((table (or (copy-sequence standard-display-table)
2451                    (make-display-table)))
2452         (i 32))
2453     ;; Nix out all the control chars...
2454     (while (>= (setq i (1- i)) 0)
2455       (aset table i [??]))
2456     ;; ... but not newline and cr, of course.  (cr is necessary for the
2457     ;; selective display).
2458     (aset table ?\n nil)
2459     (aset table ?\r nil)
2460     ;; We keep TAB as well.
2461     (aset table ?\t nil)
2462     ;; We nix out any glyphs over 126 that are not set already.
2463     (let ((i 256))
2464       (while (>= (setq i (1- i)) 127)
2465         ;; Only modify if the entry is nil.
2466         (unless (aref table i)
2467           (aset table i [??]))))
2468     (setq buffer-display-table table)))
2469
2470 (defun gnus-summary-setup-buffer (group)
2471   "Initialize summary buffer."
2472   (let ((buffer (concat "*Summary " group "*")))
2473     (if (get-buffer buffer)
2474         (progn
2475           (set-buffer buffer)
2476           (setq gnus-summary-buffer (current-buffer))
2477           (not gnus-newsgroup-prepared))
2478       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2479       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2480       (gnus-summary-mode group)
2481       (when gnus-carpal
2482         (gnus-carpal-setup-buffer 'summary))
2483       (unless gnus-single-article-buffer
2484         (make-local-variable 'gnus-article-buffer)
2485         (make-local-variable 'gnus-article-current)
2486         (make-local-variable 'gnus-original-article-buffer))
2487       (setq gnus-newsgroup-name group)
2488       t)))
2489
2490 (defun gnus-set-global-variables ()
2491   ;; Set the global equivalents of the summary buffer-local variables
2492   ;; to the latest values they had.  These reflect the summary buffer
2493   ;; that was in action when the last article was fetched.
2494   (when (eq major-mode 'gnus-summary-mode)
2495     (setq gnus-summary-buffer (current-buffer))
2496     (let ((name gnus-newsgroup-name)
2497           (marked gnus-newsgroup-marked)
2498           (unread gnus-newsgroup-unreads)
2499           (headers gnus-current-headers)
2500           (data gnus-newsgroup-data)
2501           (summary gnus-summary-buffer)
2502           (article-buffer gnus-article-buffer)
2503           (original gnus-original-article-buffer)
2504           (gac gnus-article-current)
2505           (reffed gnus-reffed-article-number)
2506           (score-file gnus-current-score-file)
2507           (default-charset gnus-newsgroup-charset))
2508       (save-excursion
2509         (set-buffer gnus-group-buffer)
2510         (setq gnus-newsgroup-name name
2511               gnus-newsgroup-marked marked
2512               gnus-newsgroup-unreads unread
2513               gnus-current-headers headers
2514               gnus-newsgroup-data data
2515               gnus-article-current gac
2516               gnus-summary-buffer summary
2517               gnus-article-buffer article-buffer
2518               gnus-original-article-buffer original
2519               gnus-reffed-article-number reffed
2520               gnus-current-score-file score-file
2521               gnus-newsgroup-charset default-charset)
2522         ;; The article buffer also has local variables.
2523         (when (gnus-buffer-live-p gnus-article-buffer)
2524           (set-buffer gnus-article-buffer)
2525           (setq gnus-summary-buffer summary))))))
2526
2527 (defun gnus-summary-article-unread-p (article)
2528   "Say whether ARTICLE is unread or not."
2529   (memq article gnus-newsgroup-unreads))
2530
2531 (defun gnus-summary-first-article-p (&optional article)
2532   "Return whether ARTICLE is the first article in the buffer."
2533   (if (not (setq article (or article (gnus-summary-article-number))))
2534       nil
2535     (eq article (caar gnus-newsgroup-data))))
2536
2537 (defun gnus-summary-last-article-p (&optional article)
2538   "Return whether ARTICLE is the last article in the buffer."
2539   (if (not (setq article (or article (gnus-summary-article-number))))
2540       ;; All non-existent numbers are the last article.  :-)
2541       t
2542     (not (cdr (gnus-data-find-list article)))))
2543
2544 (defun gnus-make-thread-indent-array ()
2545   (let ((n 200))
2546     (unless (and gnus-thread-indent-array
2547                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2548       (setq gnus-thread-indent-array (make-vector 201 "")
2549             gnus-thread-indent-array-level gnus-thread-indent-level)
2550       (while (>= n 0)
2551         (aset gnus-thread-indent-array n
2552               (make-string (* n gnus-thread-indent-level) ? ))
2553         (setq n (1- n))))))
2554
2555 (defun gnus-update-summary-mark-positions ()
2556   "Compute where the summary marks are to go."
2557   (save-excursion
2558     (when (gnus-buffer-exists-p gnus-summary-buffer)
2559       (set-buffer gnus-summary-buffer))
2560     (let ((gnus-replied-mark 129)
2561           (gnus-score-below-mark 130)
2562           (gnus-score-over-mark 130)
2563           (gnus-download-mark 131)
2564           (spec gnus-summary-line-format-spec)
2565           gnus-visual pos)
2566       (save-excursion
2567         (gnus-set-work-buffer)
2568         (let ((gnus-summary-line-format-spec spec)
2569               (gnus-newsgroup-downloadable '((0 . t))))
2570           (gnus-summary-insert-line
2571            (make-full-mail-header 0 "" "" "" "" "" 0 0 "" nil)
2572            0 nil 128 t nil "" nil 1)
2573           (goto-char (point-min))
2574           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2575                                              (- (point) 2)))))
2576           (goto-char (point-min))
2577           (push (cons 'replied (and (search-forward "\201" nil t)
2578                                     (- (point) 2)))
2579                 pos)
2580           (goto-char (point-min))
2581           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2582                 pos)
2583           (goto-char (point-min))
2584           (push (cons 'download
2585                       (and (search-forward "\203" nil t) (- (point) 2)))
2586                 pos)))
2587       (setq gnus-summary-mark-positions pos))))
2588
2589 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2590   "Insert a dummy root in the summary buffer."
2591   (beginning-of-line)
2592   (gnus-add-text-properties
2593    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2594    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2595
2596 (defun gnus-summary-from-or-to-or-newsgroups (header)
2597   (let ((to (cdr (assq 'To (mail-header-extra header))))
2598         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2599         (mail-parse-charset gnus-newsgroup-charset)
2600         (mail-parse-ignored-charsets 
2601          (save-excursion (set-buffer gnus-summary-buffer)
2602                          gnus-newsgroup-ignored-charsets)))
2603     (cond
2604      ((and to
2605            gnus-ignored-from-addresses
2606            (string-match gnus-ignored-from-addresses
2607                          (mail-header-from header)))
2608       (concat "-> "
2609               (or (car (funcall gnus-extract-address-components to))
2610                   to)))
2611      ((and newsgroups
2612            gnus-ignored-from-addresses
2613            (string-match gnus-ignored-from-addresses
2614                          (mail-header-from header)))
2615       (concat "=> " newsgroups))
2616      (t
2617       (or (car (funcall gnus-extract-address-components
2618                         (mail-header-from header)))
2619           (mail-header-from header))))))
2620
2621 (defun gnus-summary-insert-line (gnus-tmp-header
2622                                  gnus-tmp-level gnus-tmp-current
2623                                  gnus-tmp-unread gnus-tmp-replied
2624                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2625                                  &optional gnus-tmp-dummy gnus-tmp-score
2626                                  gnus-tmp-process)
2627   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2628          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2629          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2630          (gnus-tmp-score-char
2631           (if (or (null gnus-summary-default-score)
2632                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2633                       gnus-summary-zcore-fuzz))
2634               ?  ;Whitespace
2635             (if (< gnus-tmp-score gnus-summary-default-score)
2636                 gnus-score-below-mark gnus-score-over-mark)))
2637          (gnus-tmp-replied
2638           (cond (gnus-tmp-process gnus-process-mark)
2639                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2640                  gnus-cached-mark)
2641                 (gnus-tmp-replied gnus-replied-mark)
2642                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2643                  gnus-saved-mark)
2644                 (t gnus-unread-mark)))
2645          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2646          (gnus-tmp-name
2647           (cond
2648            ((string-match "<[^>]+> *$" gnus-tmp-from)
2649             (let ((beg (match-beginning 0)))
2650               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2651                        (substring gnus-tmp-from (1+ (match-beginning 0))
2652                                   (1- (match-end 0))))
2653                   (substring gnus-tmp-from 0 beg))))
2654            ((string-match "(.+)" gnus-tmp-from)
2655             (substring gnus-tmp-from
2656                        (1+ (match-beginning 0)) (1- (match-end 0))))
2657            (t gnus-tmp-from)))
2658          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2659          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2660          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2661          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2662          (buffer-read-only nil))
2663     (when (string= gnus-tmp-name "")
2664       (setq gnus-tmp-name gnus-tmp-from))
2665     (unless (numberp gnus-tmp-lines)
2666       (setq gnus-tmp-lines 0))
2667     (gnus-put-text-property
2668      (point)
2669      (progn (eval gnus-summary-line-format-spec) (point))
2670      'gnus-number gnus-tmp-number)
2671     (when (gnus-visual-p 'summary-highlight 'highlight)
2672       (forward-line -1)
2673       (gnus-run-hooks 'gnus-summary-update-hook)
2674       (forward-line 1))))
2675
2676 (defun gnus-summary-update-line (&optional dont-update)
2677   ;; Update summary line after change.
2678   (when (and gnus-summary-default-score
2679              (not gnus-summary-inhibit-highlight))
2680     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2681            (article (gnus-summary-article-number))
2682            (score (gnus-summary-article-score article)))
2683       (unless dont-update
2684         (if (and gnus-summary-mark-below
2685                  (< (gnus-summary-article-score)
2686                     gnus-summary-mark-below))
2687             ;; This article has a low score, so we mark it as read.
2688             (when (memq article gnus-newsgroup-unreads)
2689               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2690           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2691             ;; This article was previously marked as read on account
2692             ;; of a low score, but now it has risen, so we mark it as
2693             ;; unread.
2694             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2695         (gnus-summary-update-mark
2696          (if (or (null gnus-summary-default-score)
2697                  (<= (abs (- score gnus-summary-default-score))
2698                      gnus-summary-zcore-fuzz))
2699              ?  ;Whitespace
2700            (if (< score gnus-summary-default-score)
2701                gnus-score-below-mark gnus-score-over-mark))
2702          'score))
2703       ;; Do visual highlighting.
2704       (when (gnus-visual-p 'summary-highlight 'highlight)
2705         (gnus-run-hooks 'gnus-summary-update-hook)))))
2706
2707 (defvar gnus-tmp-new-adopts nil)
2708
2709 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2710   "Return the number of articles in THREAD.
2711 This may be 0 in some cases -- if none of the articles in
2712 the thread are to be displayed."
2713   (let* ((number
2714           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2715           (cond
2716            ((not (listp thread))
2717             1)
2718            ((and (consp thread) (cdr thread))
2719             (apply
2720              '+ 1 (mapcar
2721                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2722            ((null thread)
2723             1)
2724            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2725             1)
2726            (t 0))))
2727     (when (and level (zerop level) gnus-tmp-new-adopts)
2728       (incf number
2729             (apply '+ (mapcar
2730                        'gnus-summary-number-of-articles-in-thread
2731                        gnus-tmp-new-adopts))))
2732     (if char
2733         (if (> number 1) gnus-not-empty-thread-mark
2734           gnus-empty-thread-mark)
2735       number)))
2736
2737 (defun gnus-summary-set-local-parameters (group)
2738   "Go through the local params of GROUP and set all variable specs in that list."
2739   (let ((params (gnus-group-find-parameter group))
2740         elem)
2741     (while params
2742       (setq elem (car params)
2743             params (cdr params))
2744       (and (consp elem)                 ; Has to be a cons.
2745            (consp (cdr elem))           ; The cdr has to be a list.
2746            (symbolp (car elem))         ; Has to be a symbol in there.
2747            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2748            (ignore-errors               ; So we set it.
2749              (make-local-variable (car elem))
2750              (set (car elem) (eval (nth 1 elem))))))))
2751
2752 (defun gnus-summary-read-group (group &optional show-all no-article
2753                                       kill-buffer no-display backward
2754                                       select-articles)
2755   "Start reading news in newsgroup GROUP.
2756 If SHOW-ALL is non-nil, already read articles are also listed.
2757 If NO-ARTICLE is non-nil, no article is selected initially.
2758 If NO-DISPLAY, don't generate a summary buffer."
2759   (let (result)
2760     (while (and group
2761                 (null (setq result
2762                             (let ((gnus-auto-select-next nil))
2763                               (or (gnus-summary-read-group-1
2764                                    group show-all no-article
2765                                    kill-buffer no-display
2766                                    select-articles)
2767                                   (setq show-all nil
2768                                         select-articles nil)))))
2769                 (eq gnus-auto-select-next 'quietly))
2770       (set-buffer gnus-group-buffer)
2771       ;; The entry function called above goes to the next
2772       ;; group automatically, so we go two groups back
2773       ;; if we are searching for the previous group.
2774       (when backward
2775         (gnus-group-prev-unread-group 2))
2776       (if (not (equal group (gnus-group-group-name)))
2777           (setq group (gnus-group-group-name))
2778         (setq group nil)))
2779     result))
2780
2781 (defun gnus-summary-read-group-1 (group show-all no-article
2782                                         kill-buffer no-display
2783                                         &optional select-articles)
2784   ;; Killed foreign groups can't be entered.
2785   (when (and (not (gnus-group-native-p group))
2786              (not (gnus-gethash group gnus-newsrc-hashtb)))
2787     (error "Dead non-native groups can't be entered"))
2788   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2789   (let* ((new-group (gnus-summary-setup-buffer group))
2790          (quit-config (gnus-group-quit-config group))
2791          (did-select (and new-group (gnus-select-newsgroup
2792                                      group show-all select-articles))))
2793     (cond
2794      ;; This summary buffer exists already, so we just select it.
2795      ((not new-group)
2796       (gnus-set-global-variables)
2797       (when kill-buffer
2798         (gnus-kill-or-deaden-summary kill-buffer))
2799       (gnus-configure-windows 'summary 'force)
2800       (gnus-set-mode-line 'summary)
2801       (gnus-summary-position-point)
2802       (message "")
2803       t)
2804      ;; We couldn't select this group.
2805      ((null did-select)
2806       (when (and (eq major-mode 'gnus-summary-mode)
2807                  (not (equal (current-buffer) kill-buffer)))
2808         (kill-buffer (current-buffer))
2809         (if (not quit-config)
2810             (progn
2811               ;; Update the info -- marks might need to be removed,
2812               ;; for instance.
2813               (gnus-summary-update-info)
2814               (set-buffer gnus-group-buffer)
2815               (gnus-group-jump-to-group group)
2816               (gnus-group-next-unread-group 1))
2817           (gnus-handle-ephemeral-exit quit-config)))
2818       (gnus-message 3 "Can't select group")
2819       nil)
2820      ;; The user did a `C-g' while prompting for number of articles,
2821      ;; so we exit this group.
2822      ((eq did-select 'quit)
2823       (and (eq major-mode 'gnus-summary-mode)
2824            (not (equal (current-buffer) kill-buffer))
2825            (kill-buffer (current-buffer)))
2826       (when kill-buffer
2827         (gnus-kill-or-deaden-summary kill-buffer))
2828       (if (not quit-config)
2829           (progn
2830             (set-buffer gnus-group-buffer)
2831             (gnus-group-jump-to-group group)
2832             (gnus-group-next-unread-group 1)
2833             (gnus-configure-windows 'group 'force))
2834         (gnus-handle-ephemeral-exit quit-config))
2835       ;; Finally signal the quit.
2836       (signal 'quit nil))
2837      ;; The group was successfully selected.
2838      (t
2839       (gnus-set-global-variables)
2840       ;; Save the active value in effect when the group was entered.
2841       (setq gnus-newsgroup-active
2842             (gnus-copy-sequence
2843              (gnus-active gnus-newsgroup-name)))
2844       ;; You can change the summary buffer in some way with this hook.
2845       (gnus-run-hooks 'gnus-select-group-hook)
2846       ;; Set any local variables in the group parameters.
2847       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2848       (gnus-update-format-specifications
2849        nil 'summary 'summary-mode 'summary-dummy)
2850       (gnus-update-summary-mark-positions)
2851       ;; Do score processing.
2852       (when gnus-use-scoring
2853         (gnus-possibly-score-headers))
2854       ;; Check whether to fill in the gaps in the threads.
2855       (when gnus-build-sparse-threads
2856         (gnus-build-sparse-threads))
2857       ;; Find the initial limit.
2858       (if gnus-show-threads
2859           (if show-all
2860               (let ((gnus-newsgroup-dormant nil))
2861                 (gnus-summary-initial-limit show-all))
2862             (gnus-summary-initial-limit show-all))
2863         ;; When untreaded, all articles are always shown.
2864         (setq gnus-newsgroup-limit
2865               (mapcar
2866                (lambda (header) (mail-header-number header))
2867                gnus-newsgroup-headers)))
2868       ;; Generate the summary buffer.
2869       (unless no-display
2870         (gnus-summary-prepare))
2871       (when gnus-use-trees
2872         (gnus-tree-open group)
2873         (setq gnus-summary-highlight-line-function
2874               'gnus-tree-highlight-article))
2875       ;; If the summary buffer is empty, but there are some low-scored
2876       ;; articles or some excluded dormants, we include these in the
2877       ;; buffer.
2878       (when (and (zerop (buffer-size))
2879                  (not no-display))
2880         (cond (gnus-newsgroup-dormant
2881                (gnus-summary-limit-include-dormant))
2882               ((and gnus-newsgroup-scored show-all)
2883                (gnus-summary-limit-include-expunged t))))
2884       ;; Function `gnus-apply-kill-file' must be called in this hook.
2885       (gnus-run-hooks 'gnus-apply-kill-hook)
2886       (if (and (zerop (buffer-size))
2887                (not no-display))
2888           (progn
2889             ;; This newsgroup is empty.
2890             (gnus-summary-catchup-and-exit nil t)
2891             (gnus-message 6 "No unread news")
2892             (when kill-buffer
2893               (gnus-kill-or-deaden-summary kill-buffer))
2894             ;; Return nil from this function.
2895             nil)
2896         ;; Hide conversation thread subtrees.  We cannot do this in
2897         ;; gnus-summary-prepare-hook since kill processing may not
2898         ;; work with hidden articles.
2899         (and gnus-show-threads
2900              gnus-thread-hide-subtree
2901              (gnus-summary-hide-all-threads))
2902         (when kill-buffer
2903           (gnus-kill-or-deaden-summary kill-buffer))
2904         ;; Show first unread article if requested.
2905         (if (and (not no-article)
2906                  (not no-display)
2907                  gnus-newsgroup-unreads
2908                  gnus-auto-select-first)
2909             (progn
2910               (gnus-configure-windows 'summary)
2911               (cond
2912                ((eq gnus-auto-select-first 'best)
2913                 (gnus-summary-best-unread-article))
2914                ((eq gnus-auto-select-first t)
2915                 (gnus-summary-first-unread-article))
2916                ((gnus-functionp gnus-auto-select-first)
2917                 (funcall gnus-auto-select-first))))
2918           ;; Don't select any articles, just move point to the first
2919           ;; article in the group.
2920           (goto-char (point-min))
2921           (gnus-summary-position-point)
2922           (gnus-configure-windows 'summary 'force)
2923           (gnus-set-mode-line 'summary))
2924         (when (get-buffer-window gnus-group-buffer t)
2925           ;; Gotta use windows, because recenter does weird stuff if
2926           ;; the current buffer ain't the displayed window.
2927           (let ((owin (selected-window)))
2928             (select-window (get-buffer-window gnus-group-buffer t))
2929             (when (gnus-group-goto-group group)
2930               (recenter))
2931             (select-window owin)))
2932         ;; Mark this buffer as "prepared".
2933         (setq gnus-newsgroup-prepared t)
2934         (gnus-run-hooks 'gnus-summary-prepared-hook)
2935         t)))))
2936
2937 (defun gnus-summary-prepare ()
2938   "Generate the summary buffer."
2939   (interactive)
2940   (let ((buffer-read-only nil))
2941     (erase-buffer)
2942     (setq gnus-newsgroup-data nil
2943           gnus-newsgroup-data-reverse nil)
2944     (gnus-run-hooks 'gnus-summary-generate-hook)
2945     ;; Generate the buffer, either with threads or without.
2946     (when gnus-newsgroup-headers
2947       (gnus-summary-prepare-threads
2948        (if gnus-show-threads
2949            (gnus-sort-gathered-threads
2950             (funcall gnus-summary-thread-gathering-function
2951                      (gnus-sort-threads
2952                       (gnus-cut-threads (gnus-make-threads)))))
2953          ;; Unthreaded display.
2954          (gnus-sort-articles gnus-newsgroup-headers))))
2955     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2956     ;; Call hooks for modifying summary buffer.
2957     (goto-char (point-min))
2958     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2959
2960 (defsubst gnus-general-simplify-subject (subject)
2961   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2962   (setq subject
2963         (cond
2964          ;; Truncate the subject.
2965          (gnus-simplify-subject-functions
2966           (gnus-map-function gnus-simplify-subject-functions subject))
2967          ((numberp gnus-summary-gather-subject-limit)
2968           (setq subject (gnus-simplify-subject-re subject))
2969           (if (> (length subject) gnus-summary-gather-subject-limit)
2970               (substring subject 0 gnus-summary-gather-subject-limit)
2971             subject))
2972          ;; Fuzzily simplify it.
2973          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2974           (gnus-simplify-subject-fuzzy subject))
2975          ;; Just remove the leading "Re:".
2976          (t
2977           (gnus-simplify-subject-re subject))))
2978
2979   (if (and gnus-summary-gather-exclude-subject
2980            (string-match gnus-summary-gather-exclude-subject subject))
2981       nil                               ; This article shouldn't be gathered
2982     subject))
2983
2984 (defun gnus-summary-simplify-subject-query ()
2985   "Query where the respool algorithm would put this article."
2986   (interactive)
2987   (gnus-summary-select-article)
2988   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2989
2990 (defun gnus-gather-threads-by-subject (threads)
2991   "Gather threads by looking at Subject headers."
2992   (if (not gnus-summary-make-false-root)
2993       threads
2994     (let ((hashtb (gnus-make-hashtable 1024))
2995           (prev threads)
2996           (result threads)
2997           subject hthread whole-subject)
2998       (while threads
2999         (setq subject (gnus-general-simplify-subject
3000                        (setq whole-subject (mail-header-subject
3001                                             (caar threads)))))
3002         (when subject
3003           (if (setq hthread (gnus-gethash subject hashtb))
3004               (progn
3005                 ;; We enter a dummy root into the thread, if we
3006                 ;; haven't done that already.
3007                 (unless (stringp (caar hthread))
3008                   (setcar hthread (list whole-subject (car hthread))))
3009                 ;; We add this new gathered thread to this gathered
3010                 ;; thread.
3011                 (setcdr (car hthread)
3012                         (nconc (cdar hthread) (list (car threads))))
3013                 ;; Remove it from the list of threads.
3014                 (setcdr prev (cdr threads))
3015                 (setq threads prev))
3016             ;; Enter this thread into the hash table.
3017             (gnus-sethash subject threads hashtb)))
3018         (setq prev threads)
3019         (setq threads (cdr threads)))
3020       result)))
3021
3022 (defun gnus-gather-threads-by-references (threads)
3023   "Gather threads by looking at References headers."
3024   (let ((idhashtb (gnus-make-hashtable 1024))
3025         (thhashtb (gnus-make-hashtable 1024))
3026         (prev threads)
3027         (result threads)
3028         ids references id gthread gid entered ref)
3029     (while threads
3030       (when (setq references (mail-header-references (caar threads)))
3031         (setq id (mail-header-id (caar threads))
3032               ids (gnus-split-references references)
3033               entered nil)
3034         (while (setq ref (pop ids))
3035           (setq ids (delete ref ids))
3036           (if (not (setq gid (gnus-gethash ref idhashtb)))
3037               (progn
3038                 (gnus-sethash ref id idhashtb)
3039                 (gnus-sethash id threads thhashtb))
3040             (setq gthread (gnus-gethash gid thhashtb))
3041             (unless entered
3042               ;; We enter a dummy root into the thread, if we
3043               ;; haven't done that already.
3044               (unless (stringp (caar gthread))
3045                 (setcar gthread (list (mail-header-subject (caar gthread))
3046                                       (car gthread))))
3047               ;; We add this new gathered thread to this gathered
3048               ;; thread.
3049               (setcdr (car gthread)
3050                       (nconc (cdar gthread) (list (car threads)))))
3051             ;; Add it into the thread hash table.
3052             (gnus-sethash id gthread thhashtb)
3053             (setq entered t)
3054             ;; Remove it from the list of threads.
3055             (setcdr prev (cdr threads))
3056             (setq threads prev))))
3057       (setq prev threads)
3058       (setq threads (cdr threads)))
3059     result))
3060
3061 (defun gnus-sort-gathered-threads (threads)
3062   "Sort subtreads inside each gathered thread by article number."
3063   (let ((result threads))
3064     (while threads
3065       (when (stringp (caar threads))
3066         (setcdr (car threads)
3067                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3068       (setq threads (cdr threads)))
3069     result))
3070
3071 (defun gnus-thread-loop-p (root thread)
3072   "Say whether ROOT is in THREAD."
3073   (let ((stack (list thread))
3074         (infloop 0)
3075         th)
3076     (while (setq thread (pop stack))
3077       (setq th (cdr thread))
3078       (while (and th
3079                   (not (eq (caar th) root)))
3080         (pop th))
3081       (if th
3082           ;; We have found a loop.
3083           (let (ref-dep)
3084             (setcdr thread (delq (car th) (cdr thread)))
3085             (if (boundp (setq ref-dep (intern "none"
3086                                               gnus-newsgroup-dependencies)))
3087                 (setcdr (symbol-value ref-dep)
3088                         (nconc (cdr (symbol-value ref-dep))
3089                                (list (car th))))
3090               (set ref-dep (list nil (car th))))
3091             (setq infloop 1
3092                   stack nil))
3093         ;; Push all the subthreads onto the stack.
3094         (push (cdr thread) stack)))
3095     infloop))
3096
3097 (defun gnus-make-threads ()
3098   "Go through the dependency hashtb and find the roots.  Return all threads."
3099   (let (threads)
3100     (while (catch 'infloop
3101              (mapatoms
3102               (lambda (refs)
3103                 ;; Deal with self-referencing References loops.
3104                 (when (and (car (symbol-value refs))
3105                            (not (zerop
3106                                  (apply
3107                                   '+
3108                                   (mapcar
3109                                    (lambda (thread)
3110                                      (gnus-thread-loop-p
3111                                       (car (symbol-value refs)) thread))
3112                                    (cdr (symbol-value refs)))))))
3113                   (setq threads nil)
3114                   (throw 'infloop t))
3115                 (unless (car (symbol-value refs))
3116                   ;; These threads do not refer back to any other articles,
3117                   ;; so they're roots.
3118                   (setq threads (append (cdr (symbol-value refs)) threads))))
3119               gnus-newsgroup-dependencies)))
3120     threads))
3121
3122 ;; Build the thread tree.
3123 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3124   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3125
3126 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3127 if it was already present.
3128
3129 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3130 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3131 Message-IDs will be renamed be renamed to a unique Message-ID before
3132 being entered.
3133
3134 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3135   (let* ((id (mail-header-id header))
3136          (id-dep (and id (intern id dependencies)))
3137          ref ref-dep ref-header)
3138     ;; Enter this `header' in the `dependencies' table.
3139     (cond
3140      ((not id-dep)
3141       (setq header nil))
3142      ;; The first two cases do the normal part: enter a new `header'
3143      ;; in the `dependencies' table.
3144      ((not (boundp id-dep))
3145       (set id-dep (list header)))
3146      ((null (car (symbol-value id-dep)))
3147       (setcar (symbol-value id-dep) header))
3148
3149      ;; From here the `header' was already present in the
3150      ;; `dependencies' table.
3151      (force-new
3152       ;; Overrides an existing entry;
3153       ;; just set the header part of the entry.
3154       (setcar (symbol-value id-dep) header))
3155
3156      ;; Renames the existing `header' to a unique Message-ID.
3157      ((not gnus-summary-ignore-duplicates)
3158       ;; An article with this Message-ID has already been seen.
3159       ;; We rename the Message-ID.
3160       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3161            (list header))
3162       (mail-header-set-id header id))
3163
3164      ;; The last case ignores an existing entry, except it adds any
3165      ;; additional Xrefs (in case the two articles came from different
3166      ;; servers.
3167      ;; Also sets `header' to `nil' meaning that the `dependencies'
3168      ;; table was *not* modified.
3169      (t
3170       (mail-header-set-xref
3171        (car (symbol-value id-dep))
3172        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3173                    "")
3174                (or (mail-header-xref header) "")))
3175       (setq header nil)))
3176
3177     (when header
3178       ;; First check if that we are not creating a References loop.
3179       (setq ref (gnus-parent-id (mail-header-references header)))
3180       (while (and ref
3181                   (setq ref-dep (intern-soft ref dependencies))
3182                   (boundp ref-dep)
3183                   (setq ref-header (car (symbol-value ref-dep))))
3184         (if (string= id ref)
3185             ;; Yuk!  This is a reference loop.  Make the article be a
3186             ;; root article.
3187             (progn
3188               (mail-header-set-references (car (symbol-value id-dep)) "none")
3189               (setq ref nil))
3190           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3191       (setq ref (gnus-parent-id (mail-header-references header)))
3192       (setq ref-dep (intern (or ref "none") dependencies))
3193       (if (boundp ref-dep)
3194           (setcdr (symbol-value ref-dep)
3195                   (nconc (cdr (symbol-value ref-dep))
3196                          (list (symbol-value id-dep))))
3197         (set ref-dep (list nil (symbol-value id-dep)))))
3198     header))
3199
3200 (defun gnus-build-sparse-threads ()
3201   (let ((headers gnus-newsgroup-headers)
3202         (mail-parse-charset gnus-newsgroup-charset)
3203         (gnus-summary-ignore-duplicates t)
3204         header references generation relations
3205         subject child end new-child date)
3206     ;; First we create an alist of generations/relations, where
3207     ;; generations is how much we trust the relation, and the relation
3208     ;; is parent/child.
3209     (gnus-message 7 "Making sparse threads...")
3210     (save-excursion
3211       (nnheader-set-temp-buffer " *gnus sparse threads*")
3212       (while (setq header (pop headers))
3213         (when (and (setq references (mail-header-references header))
3214                    (not (string= references "")))
3215           (insert references)
3216           (setq child (mail-header-id header)
3217                 subject (mail-header-subject header)
3218                 date (mail-header-date header)
3219                 generation 0)
3220           (while (search-backward ">" nil t)
3221             (setq end (1+ (point)))
3222             (when (search-backward "<" nil t)
3223               (setq new-child (buffer-substring (point) end))
3224               (push (list (incf generation)
3225                           child (setq child new-child)
3226                           subject date)
3227                     relations)))
3228           (when child
3229             (push (list (1+ generation) child nil subject) relations))
3230           (erase-buffer)))
3231       (kill-buffer (current-buffer)))
3232     ;; Sort over trustworthiness.
3233     (mapcar
3234      (lambda (relation)
3235        (when (gnus-dependencies-add-header
3236               (make-full-mail-header-from-decoded-header
3237                gnus-reffed-article-number
3238                (nth 3 relation) "" (or (nth 4 relation) "")
3239                (nth 1 relation)
3240                (or (nth 2 relation) "") 0 0 "")
3241               gnus-newsgroup-dependencies nil)
3242          (push gnus-reffed-article-number gnus-newsgroup-limit)
3243          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3244          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3245                gnus-newsgroup-reads)
3246          (decf gnus-reffed-article-number)))
3247      (sort relations 'car-less-than-car))
3248     (gnus-message 7 "Making sparse threads...done")))
3249
3250 (defun gnus-build-old-threads ()
3251   ;; Look at all the articles that refer back to old articles, and
3252   ;; fetch the headers for the articles that aren't there.  This will
3253   ;; build complete threads - if the roots haven't been expired by the
3254   ;; server, that is.
3255   (let ((mail-parse-charset gnus-newsgroup-charset)
3256         id heads)
3257     (mapatoms
3258      (lambda (refs)
3259        (when (not (car (symbol-value refs)))
3260          (setq heads (cdr (symbol-value refs)))
3261          (while heads
3262            (if (memq (mail-header-number (caar heads))
3263                      gnus-newsgroup-dormant)
3264                (setq heads (cdr heads))
3265              (setq id (symbol-name refs))
3266              (while (and (setq id (gnus-build-get-header id))
3267                          (not (car (gnus-id-to-thread id)))))
3268              (setq heads nil)))))
3269      gnus-newsgroup-dependencies)))
3270
3271 ;; This function has to be called with point after the article number
3272 ;; on the beginning of the line.
3273 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3274   (let ((eol (gnus-point-at-eol))
3275         (buffer (current-buffer))
3276         header)
3277
3278     ;; overview: [num subject from date id refs chars lines misc]
3279     (unwind-protect
3280         (progn
3281           (narrow-to-region (point) eol)
3282           (unless (eobp)
3283             (forward-char))
3284
3285           (setq header
3286                 (make-full-mail-header
3287                  number                 ; number
3288                  (nnheader-nov-field)   ; subject
3289                  (nnheader-nov-field)   ; from
3290                  (nnheader-nov-field)   ; date
3291                  (nnheader-nov-read-message-id) ; id
3292                  (nnheader-nov-field)   ; refs
3293                  (nnheader-nov-read-integer) ; chars
3294                  (nnheader-nov-read-integer) ; lines
3295                  (unless (eobp)
3296                    (nnheader-nov-field)) ; misc
3297                  (nnheader-nov-parse-extra)))) ; extra
3298
3299       (widen))
3300
3301     (when gnus-alter-header-function
3302       (funcall gnus-alter-header-function header))
3303     (gnus-dependencies-add-header header dependencies force-new)))
3304
3305 (defun gnus-build-get-header (id)
3306   ;; Look through the buffer of NOV lines and find the header to
3307   ;; ID.  Enter this line into the dependencies hash table, and return
3308   ;; the id of the parent article (if any).
3309   (let ((deps gnus-newsgroup-dependencies)
3310         found header)
3311     (prog1
3312         (save-excursion
3313           (set-buffer nntp-server-buffer)
3314           (let ((case-fold-search nil))
3315             (goto-char (point-min))
3316             (while (and (not found)
3317                         (search-forward id nil t))
3318               (beginning-of-line)
3319               (setq found (looking-at
3320                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3321                                    (regexp-quote id))))
3322               (or found (beginning-of-line 2)))
3323             (when found
3324               (beginning-of-line)
3325               (and
3326                (setq header (gnus-nov-parse-line
3327                              (read (current-buffer)) deps))
3328                (gnus-parent-id (mail-header-references header))))))
3329       (when header
3330         (let ((number (mail-header-number header)))
3331           (push number gnus-newsgroup-limit)
3332           (push header gnus-newsgroup-headers)
3333           (if (memq number gnus-newsgroup-unselected)
3334               (progn
3335                 (push number gnus-newsgroup-unreads)
3336                 (setq gnus-newsgroup-unselected
3337                       (delq number gnus-newsgroup-unselected)))
3338             (push number gnus-newsgroup-ancient)))))))
3339
3340 (defun gnus-build-all-threads ()
3341   "Read all the headers."
3342   (let ((gnus-summary-ignore-duplicates t)
3343         (mail-parse-charset gnus-newsgroup-charset)
3344         (dependencies gnus-newsgroup-dependencies)
3345         header article)
3346     (save-excursion
3347       (set-buffer nntp-server-buffer)
3348       (let ((case-fold-search nil))
3349         (goto-char (point-min))
3350         (while (not (eobp))
3351           (ignore-errors
3352             (setq article (read (current-buffer))
3353                   header (gnus-nov-parse-line article dependencies)))
3354           (when header
3355             (save-excursion
3356               (set-buffer gnus-summary-buffer)
3357               (push header gnus-newsgroup-headers)
3358               (if (memq (setq article (mail-header-number header))
3359                         gnus-newsgroup-unselected)
3360                   (progn
3361                     (push article gnus-newsgroup-unreads)
3362                     (setq gnus-newsgroup-unselected
3363                           (delq article gnus-newsgroup-unselected)))
3364                 (push article gnus-newsgroup-ancient)))
3365             (forward-line 1)))))))
3366
3367 (defun gnus-summary-update-article-line (article header)
3368   "Update the line for ARTICLE using HEADERS."
3369   (let* ((id (mail-header-id header))
3370          (thread (gnus-id-to-thread id)))
3371     (unless thread
3372       (error "Article in no thread"))
3373     ;; Update the thread.
3374     (setcar thread header)
3375     (gnus-summary-goto-subject article)
3376     (let* ((datal (gnus-data-find-list article))
3377            (data (car datal))
3378            (length (when (cdr datal)
3379                      (- (gnus-data-pos data)
3380                         (gnus-data-pos (cadr datal)))))
3381            (buffer-read-only nil)
3382            (level (gnus-summary-thread-level)))
3383       (gnus-delete-line)
3384       (gnus-summary-insert-line
3385        header level nil (gnus-article-mark article)
3386        (memq article gnus-newsgroup-replied)
3387        (memq article gnus-newsgroup-expirable)
3388        ;; Only insert the Subject string when it's different
3389        ;; from the previous Subject string.
3390        (if (and
3391             gnus-show-threads
3392             (gnus-subject-equal
3393              (condition-case ()
3394                  (mail-header-subject
3395                   (gnus-data-header
3396                    (cadr
3397                     (gnus-data-find-list
3398                      article
3399                      (gnus-data-list t)))))
3400                ;; Error on the side of excessive subjects.
3401                (error ""))
3402              (mail-header-subject header)))
3403            ""
3404          (mail-header-subject header))
3405        nil (cdr (assq article gnus-newsgroup-scored))
3406        (memq article gnus-newsgroup-processable))
3407       (when length
3408         (gnus-data-update-list
3409          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3410
3411 (defun gnus-summary-update-article (article &optional iheader)
3412   "Update ARTICLE in the summary buffer."
3413   (set-buffer gnus-summary-buffer)
3414   (let* ((header (gnus-summary-article-header article))
3415          (id (mail-header-id header))
3416          (data (gnus-data-find article))
3417          (thread (gnus-id-to-thread id))
3418          (references (mail-header-references header))
3419          (parent
3420           (gnus-id-to-thread
3421            (or (gnus-parent-id
3422                 (when (and references
3423                            (not (equal "" references)))
3424                   references))
3425                "none")))
3426          (buffer-read-only nil)
3427          (old (car thread)))
3428     (when thread
3429       (unless iheader
3430         (setcar thread nil)
3431         (when parent
3432           (delq thread parent)))
3433       (if (gnus-summary-insert-subject id header)
3434           ;; Set the (possibly) new article number in the data structure.
3435           (gnus-data-set-number data (gnus-id-to-article id))
3436         (setcar thread old)
3437         nil))))
3438
3439 (defun gnus-rebuild-thread (id &optional line)
3440   "Rebuild the thread containing ID.
3441 If LINE, insert the rebuilt thread starting on line LINE."
3442   (let ((buffer-read-only nil)
3443         old-pos current thread data)
3444     (if (not gnus-show-threads)
3445         (setq thread (list (car (gnus-id-to-thread id))))
3446       ;; Get the thread this article is part of.
3447       (setq thread (gnus-remove-thread id)))
3448     (setq old-pos (gnus-point-at-bol))
3449     (setq current (save-excursion
3450                     (and (zerop (forward-line -1))
3451                          (gnus-summary-article-number))))
3452     ;; If this is a gathered thread, we have to go some re-gathering.
3453     (when (stringp (car thread))
3454       (let ((subject (car thread))
3455             roots thr)
3456         (setq thread (cdr thread))
3457         (while thread
3458           (unless (memq (setq thr (gnus-id-to-thread
3459                                    (gnus-root-id
3460                                     (mail-header-id (caar thread)))))
3461                         roots)
3462             (push thr roots))
3463           (setq thread (cdr thread)))
3464         ;; We now have all (unique) roots.
3465         (if (= (length roots) 1)
3466             ;; All the loose roots are now one solid root.
3467             (setq thread (car roots))
3468           (setq thread (cons subject (gnus-sort-threads roots))))))
3469     (let (threads)
3470       ;; We then insert this thread into the summary buffer.
3471       (when line
3472         (goto-char (point-min))
3473         (forward-line (1- line)))
3474       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3475         (if gnus-show-threads
3476             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3477           (gnus-summary-prepare-unthreaded thread))
3478         (setq data (nreverse gnus-newsgroup-data))
3479         (setq threads gnus-newsgroup-threads))
3480       ;; We splice the new data into the data structure.
3481       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3482       ;;!!! then we want to insert at the beginning of the buffer.
3483       ;;!!! That happens to be true with Gnus now, but that may
3484       ;;!!! change in the future.  Perhaps.
3485       (gnus-data-enter-list
3486        (if line nil current) data (- (point) old-pos))
3487       (setq gnus-newsgroup-threads
3488             (nconc threads gnus-newsgroup-threads))
3489       (gnus-data-compute-positions))))
3490
3491 (defun gnus-number-to-header (number)
3492   "Return the header for article NUMBER."
3493   (let ((headers gnus-newsgroup-headers))
3494     (while (and headers
3495                 (not (= number (mail-header-number (car headers)))))
3496       (pop headers))
3497     (when headers
3498       (car headers))))
3499
3500 (defun gnus-parent-headers (in-headers &optional generation)
3501   "Return the headers of the GENERATIONeth parent of HEADERS."
3502   (unless generation
3503     (setq generation 1))
3504   (let ((parent t)
3505         (headers in-headers)
3506         references)
3507     (while (and parent
3508                 (not (zerop generation))
3509                 (setq references (mail-header-references headers)))
3510       (setq headers (if (and references
3511                              (setq parent (gnus-parent-id references)))
3512                         (car (gnus-id-to-thread parent))
3513                       nil))
3514       (decf generation))
3515     (and (not (eq headers in-headers))
3516          headers)))
3517
3518 (defun gnus-id-to-thread (id)
3519   "Return the (sub-)thread where ID appears."
3520   (gnus-gethash id gnus-newsgroup-dependencies))
3521
3522 (defun gnus-id-to-article (id)
3523   "Return the article number of ID."
3524   (let ((thread (gnus-id-to-thread id)))
3525     (when (and thread
3526                (car thread))
3527       (mail-header-number (car thread)))))
3528
3529 (defun gnus-id-to-header (id)
3530   "Return the article headers of ID."
3531   (car (gnus-id-to-thread id)))
3532
3533 (defun gnus-article-displayed-root-p (article)
3534   "Say whether ARTICLE is a root(ish) article."
3535   (let ((level (gnus-summary-thread-level article))
3536         (refs (mail-header-references  (gnus-summary-article-header article)))
3537         particle)
3538     (cond
3539      ((null level) nil)
3540      ((zerop level) t)
3541      ((null refs) t)
3542      ((null (gnus-parent-id refs)) t)
3543      ((and (= 1 level)
3544            (null (setq particle (gnus-id-to-article
3545                                  (gnus-parent-id refs))))
3546            (null (gnus-summary-thread-level particle)))))))
3547
3548 (defun gnus-root-id (id)
3549   "Return the id of the root of the thread where ID appears."
3550   (let (last-id prev)
3551     (while (and id (setq prev (car (gnus-id-to-thread id))))
3552       (setq last-id id
3553             id (gnus-parent-id (mail-header-references prev))))
3554     last-id))
3555
3556 (defun gnus-articles-in-thread (thread)
3557   "Return the list of articles in THREAD."
3558   (cons (mail-header-number (car thread))
3559         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3560
3561 (defun gnus-remove-thread (id &optional dont-remove)
3562   "Remove the thread that has ID in it."
3563   (let (headers thread last-id)
3564     ;; First go up in this thread until we find the root.
3565     (setq last-id (gnus-root-id id)
3566           headers (message-flatten-list (gnus-id-to-thread last-id)))
3567     ;; We have now found the real root of this thread.  It might have
3568     ;; been gathered into some loose thread, so we have to search
3569     ;; through the threads to find the thread we wanted.
3570     (let ((threads gnus-newsgroup-threads)
3571           sub)
3572       (while threads
3573         (setq sub (car threads))
3574         (if (stringp (car sub))
3575             ;; This is a gathered thread, so we look at the roots
3576             ;; below it to find whether this article is in this
3577             ;; gathered root.
3578             (progn
3579               (setq sub (cdr sub))
3580               (while sub
3581                 (when (member (caar sub) headers)
3582                   (setq thread (car threads)
3583                         threads nil
3584                         sub nil))
3585                 (setq sub (cdr sub))))
3586           ;; It's an ordinary thread, so we check it.
3587           (when (eq (car sub) (car headers))
3588             (setq thread sub
3589                   threads nil)))
3590         (setq threads (cdr threads)))
3591       ;; If this article is in no thread, then it's a root.
3592       (if thread
3593           (unless dont-remove
3594             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3595         (setq thread (gnus-id-to-thread last-id)))
3596       (when thread
3597         (prog1
3598             thread                      ; We return this thread.
3599           (unless dont-remove
3600             (if (stringp (car thread))
3601                 (progn
3602                   ;; If we use dummy roots, then we have to remove the
3603                   ;; dummy root as well.
3604                   (when (eq gnus-summary-make-false-root 'dummy)
3605                     ;; We go to the dummy root by going to
3606                     ;; the first sub-"thread", and then one line up.
3607                     (gnus-summary-goto-article
3608                      (mail-header-number (caadr thread)))
3609                     (forward-line -1)
3610                     (gnus-delete-line)
3611                     (gnus-data-compute-positions))
3612                   (setq thread (cdr thread))
3613                   (while thread
3614                     (gnus-remove-thread-1 (car thread))
3615                     (setq thread (cdr thread))))
3616               (gnus-remove-thread-1 thread))))))))
3617
3618 (defun gnus-remove-thread-1 (thread)
3619   "Remove the thread THREAD recursively."
3620   (let ((number (mail-header-number (pop thread)))
3621         d)
3622     (setq thread (reverse thread))
3623     (while thread
3624       (gnus-remove-thread-1 (pop thread)))
3625     (when (setq d (gnus-data-find number))
3626       (goto-char (gnus-data-pos d))
3627       (gnus-summary-show-thread)
3628       (gnus-data-remove
3629        number
3630        (- (gnus-point-at-bol)
3631           (prog1
3632               (1+ (gnus-point-at-eol))
3633             (gnus-delete-line)))))))
3634
3635 (defun gnus-sort-threads (threads)
3636   "Sort THREADS."
3637   (if (not gnus-thread-sort-functions)
3638       threads
3639     (gnus-message 8 "Sorting threads...")
3640     (prog1
3641         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3642       (gnus-message 8 "Sorting threads...done"))))
3643
3644 (defun gnus-sort-articles (articles)
3645   "Sort ARTICLES."
3646   (when gnus-article-sort-functions
3647     (gnus-message 7 "Sorting articles...")
3648     (prog1
3649         (setq gnus-newsgroup-headers
3650               (sort articles (gnus-make-sort-function
3651                               gnus-article-sort-functions)))
3652       (gnus-message 7 "Sorting articles...done"))))
3653
3654 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3655 (defmacro gnus-thread-header (thread)
3656   ;; Return header of first article in THREAD.
3657   ;; Note that THREAD must never, ever be anything else than a variable -
3658   ;; using some other form will lead to serious barfage.
3659   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3660   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3661   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3662         (vector thread) 2))
3663
3664 (defsubst gnus-article-sort-by-number (h1 h2)
3665   "Sort articles by article number."
3666   (< (mail-header-number h1)
3667      (mail-header-number h2)))
3668
3669 (defun gnus-thread-sort-by-number (h1 h2)
3670   "Sort threads by root article number."
3671   (gnus-article-sort-by-number
3672    (gnus-thread-header h1) (gnus-thread-header h2)))
3673
3674 (defsubst gnus-article-sort-by-lines (h1 h2)
3675   "Sort articles by article Lines header."
3676   (< (mail-header-lines h1)
3677      (mail-header-lines h2)))
3678
3679 (defun gnus-thread-sort-by-lines (h1 h2)
3680   "Sort threads by root article Lines header."
3681   (gnus-article-sort-by-lines
3682    (gnus-thread-header h1) (gnus-thread-header h2)))
3683
3684 (defsubst gnus-article-sort-by-chars (h1 h2)
3685   "Sort articles by octet length."
3686   (< (mail-header-chars h1)
3687      (mail-header-chars h2)))
3688
3689 (defun gnus-thread-sort-by-chars (h1 h2)
3690   "Sort threads by root article octet length."
3691   (gnus-article-sort-by-chars
3692    (gnus-thread-header h1) (gnus-thread-header h2)))
3693
3694 (defsubst gnus-article-sort-by-author (h1 h2)
3695   "Sort articles by root author."
3696   (string-lessp
3697    (let ((extract (funcall
3698                    gnus-extract-address-components
3699                    (mail-header-from h1))))
3700      (or (car extract) (cadr extract) ""))
3701    (let ((extract (funcall
3702                    gnus-extract-address-components
3703                    (mail-header-from h2))))
3704      (or (car extract) (cadr extract) ""))))
3705
3706 (defun gnus-thread-sort-by-author (h1 h2)
3707   "Sort threads by root author."
3708   (gnus-article-sort-by-author
3709    (gnus-thread-header h1)  (gnus-thread-header h2)))
3710
3711 (defsubst gnus-article-sort-by-subject (h1 h2)
3712   "Sort articles by root subject."
3713   (string-lessp
3714    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3715    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3716
3717 (defun gnus-thread-sort-by-subject (h1 h2)
3718   "Sort threads by root subject."
3719   (gnus-article-sort-by-subject
3720    (gnus-thread-header h1) (gnus-thread-header h2)))
3721
3722 (defsubst gnus-article-sort-by-date (h1 h2)
3723   "Sort articles by root article date."
3724   (time-less-p
3725    (gnus-date-get-time (mail-header-date h1))
3726    (gnus-date-get-time (mail-header-date h2))))
3727
3728 (defun gnus-thread-sort-by-date (h1 h2)
3729   "Sort threads by root article date."
3730   (gnus-article-sort-by-date
3731    (gnus-thread-header h1) (gnus-thread-header h2)))
3732
3733 (defsubst gnus-article-sort-by-score (h1 h2)
3734   "Sort articles by root article score.
3735 Unscored articles will be counted as having a score of zero."
3736   (> (or (cdr (assq (mail-header-number h1)
3737                     gnus-newsgroup-scored))
3738          gnus-summary-default-score 0)
3739      (or (cdr (assq (mail-header-number h2)
3740                     gnus-newsgroup-scored))
3741          gnus-summary-default-score 0)))
3742
3743 (defun gnus-thread-sort-by-score (h1 h2)
3744   "Sort threads by root article score."
3745   (gnus-article-sort-by-score
3746    (gnus-thread-header h1) (gnus-thread-header h2)))
3747
3748 (defun gnus-thread-sort-by-total-score (h1 h2)
3749   "Sort threads by the sum of all scores in the thread.
3750 Unscored articles will be counted as having a score of zero."
3751   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3752
3753 (defun gnus-thread-total-score (thread)
3754   ;; This function find the total score of THREAD.
3755   (cond ((null thread)
3756          0)
3757         ((consp thread)
3758          (if (stringp (car thread))
3759              (apply gnus-thread-score-function 0
3760                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3761            (gnus-thread-total-score-1 thread)))
3762         (t
3763          (gnus-thread-total-score-1 (list thread)))))
3764
3765 (defun gnus-thread-total-score-1 (root)
3766   ;; This function find the total score of the thread below ROOT.
3767   (setq root (car root))
3768   (apply gnus-thread-score-function
3769          (or (append
3770               (mapcar 'gnus-thread-total-score
3771                       (cdr (gnus-id-to-thread (mail-header-id root))))
3772               (when (> (mail-header-number root) 0)
3773                 (list (or (cdr (assq (mail-header-number root)
3774                                      gnus-newsgroup-scored))
3775                           gnus-summary-default-score 0))))
3776              (list gnus-summary-default-score)
3777              '(0))))
3778
3779 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3780 (defvar gnus-tmp-prev-subject nil)
3781 (defvar gnus-tmp-false-parent nil)
3782 (defvar gnus-tmp-root-expunged nil)
3783 (defvar gnus-tmp-dummy-line nil)
3784
3785 (defvar gnus-tmp-header)
3786 (defun gnus-extra-header (type &optional header)
3787   "Return the extra header of TYPE."
3788   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3789       ""))
3790
3791 (defun gnus-summary-prepare-threads (threads)
3792   "Prepare summary buffer from THREADS and indentation LEVEL.
3793 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3794 or a straight list of headers."
3795   (gnus-message 7 "Generating summary...")
3796
3797   (setq gnus-newsgroup-threads threads)
3798   (beginning-of-line)
3799
3800   (let ((gnus-tmp-level 0)
3801         (default-score (or gnus-summary-default-score 0))
3802         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3803         thread number subject stack state gnus-tmp-gathered beg-match
3804         new-roots gnus-tmp-new-adopts thread-end
3805         gnus-tmp-header gnus-tmp-unread
3806         gnus-tmp-replied gnus-tmp-subject-or-nil
3807         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3808         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3809         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3810
3811     (setq gnus-tmp-prev-subject nil)
3812
3813     (if (vectorp (car threads))
3814         ;; If this is a straight (sic) list of headers, then a
3815         ;; threaded summary display isn't required, so we just create
3816         ;; an unthreaded one.
3817         (gnus-summary-prepare-unthreaded threads)
3818
3819       ;; Do the threaded display.
3820
3821       (while (or threads stack gnus-tmp-new-adopts new-roots)
3822
3823         (if (and (= gnus-tmp-level 0)
3824                  (or (not stack)
3825                      (= (caar stack) 0))
3826                  (not gnus-tmp-false-parent)
3827                  (or gnus-tmp-new-adopts new-roots))
3828             (if gnus-tmp-new-adopts
3829                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3830                       thread (list (car gnus-tmp-new-adopts))
3831                       gnus-tmp-header (caar thread)
3832                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3833               (when new-roots
3834                 (setq thread (list (car new-roots))
3835                       gnus-tmp-header (caar thread)
3836                       new-roots (cdr new-roots))))
3837
3838           (if threads
3839               ;; If there are some threads, we do them before the
3840               ;; threads on the stack.
3841               (setq thread threads
3842                     gnus-tmp-header (caar thread))
3843             ;; There were no current threads, so we pop something off
3844             ;; the stack.
3845             (setq state (car stack)
3846                   gnus-tmp-level (car state)
3847                   thread (cdr state)
3848                   stack (cdr stack)
3849                   gnus-tmp-header (caar thread))))
3850
3851         (setq gnus-tmp-false-parent nil)
3852         (setq gnus-tmp-root-expunged nil)
3853         (setq thread-end nil)
3854
3855         (if (stringp gnus-tmp-header)
3856             ;; The header is a dummy root.
3857             (cond
3858              ((eq gnus-summary-make-false-root 'adopt)
3859               ;; We let the first article adopt the rest.
3860               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3861                                                (cddar thread)))
3862               (setq gnus-tmp-gathered
3863                     (nconc (mapcar
3864                             (lambda (h) (mail-header-number (car h)))
3865                             (cddar thread))
3866                            gnus-tmp-gathered))
3867               (setq thread (cons (list (caar thread)
3868                                        (cadar thread))
3869                                  (cdr thread)))
3870               (setq gnus-tmp-level -1
3871                     gnus-tmp-false-parent t))
3872              ((eq gnus-summary-make-false-root 'empty)
3873               ;; We print adopted articles with empty subject fields.
3874               (setq gnus-tmp-gathered
3875                     (nconc (mapcar
3876                             (lambda (h) (mail-header-number (car h)))
3877                             (cddar thread))
3878                            gnus-tmp-gathered))
3879               (setq gnus-tmp-level -1))
3880              ((eq gnus-summary-make-false-root 'dummy)
3881               ;; We remember that we probably want to output a dummy
3882               ;; root.
3883               (setq gnus-tmp-dummy-line gnus-tmp-header)
3884               (setq gnus-tmp-prev-subject gnus-tmp-header))
3885              (t
3886               ;; We do not make a root for the gathered
3887               ;; sub-threads at all.
3888               (setq gnus-tmp-level -1)))
3889
3890           (setq number (mail-header-number gnus-tmp-header)
3891                 subject (mail-header-subject gnus-tmp-header))
3892
3893           (cond
3894            ;; If the thread has changed subject, we might want to make
3895            ;; this subthread into a root.
3896            ((and (null gnus-thread-ignore-subject)
3897                  (not (zerop gnus-tmp-level))
3898                  gnus-tmp-prev-subject
3899                  (not (inline
3900                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3901             (setq new-roots (nconc new-roots (list (car thread)))
3902                   thread-end t
3903                   gnus-tmp-header nil))
3904            ;; If the article lies outside the current limit,
3905            ;; then we do not display it.
3906            ((not (memq number gnus-newsgroup-limit))
3907             (setq gnus-tmp-gathered
3908                   (nconc (mapcar
3909                           (lambda (h) (mail-header-number (car h)))
3910                           (cdar thread))
3911                          gnus-tmp-gathered))
3912             (setq gnus-tmp-new-adopts (if (cdar thread)
3913                                           (append gnus-tmp-new-adopts
3914                                                   (cdar thread))
3915                                         gnus-tmp-new-adopts)
3916                   thread-end t
3917                   gnus-tmp-header nil)
3918             (when (zerop gnus-tmp-level)
3919               (setq gnus-tmp-root-expunged t)))
3920            ;; Perhaps this article is to be marked as read?
3921            ((and gnus-summary-mark-below
3922                  (< (or (cdr (assq number gnus-newsgroup-scored))
3923                         default-score)
3924                     gnus-summary-mark-below)
3925                  ;; Don't touch sparse articles.
3926                  (not (gnus-summary-article-sparse-p number))
3927                  (not (gnus-summary-article-ancient-p number)))
3928             (setq gnus-newsgroup-unreads
3929                   (delq number gnus-newsgroup-unreads))
3930             (if gnus-newsgroup-auto-expire
3931                 (push number gnus-newsgroup-expirable)
3932               (push (cons number gnus-low-score-mark)
3933                     gnus-newsgroup-reads))))
3934
3935           (when gnus-tmp-header
3936             ;; We may have an old dummy line to output before this
3937             ;; article.
3938             (when (and gnus-tmp-dummy-line
3939                        (gnus-subject-equal
3940                         gnus-tmp-dummy-line
3941                         (mail-header-subject gnus-tmp-header)))
3942               (gnus-summary-insert-dummy-line
3943                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3944               (setq gnus-tmp-dummy-line nil))
3945
3946             ;; Compute the mark.
3947             (setq gnus-tmp-unread (gnus-article-mark number))
3948
3949             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3950                                   gnus-tmp-header gnus-tmp-level)
3951                   gnus-newsgroup-data)
3952
3953             ;; Actually insert the line.
3954             (setq
3955              gnus-tmp-subject-or-nil
3956              (cond
3957               ((and gnus-thread-ignore-subject
3958                     gnus-tmp-prev-subject
3959                     (not (inline (gnus-subject-equal
3960                                   gnus-tmp-prev-subject subject))))
3961                subject)
3962               ((zerop gnus-tmp-level)
3963                (if (and (eq gnus-summary-make-false-root 'empty)
3964                         (memq number gnus-tmp-gathered)
3965                         gnus-tmp-prev-subject
3966                         (inline (gnus-subject-equal
3967                                  gnus-tmp-prev-subject subject)))
3968                    gnus-summary-same-subject
3969                  subject))
3970               (t gnus-summary-same-subject)))
3971             (if (and (eq gnus-summary-make-false-root 'adopt)
3972                      (= gnus-tmp-level 1)
3973                      (memq number gnus-tmp-gathered))
3974                 (setq gnus-tmp-opening-bracket ?\<
3975                       gnus-tmp-closing-bracket ?\>)
3976               (setq gnus-tmp-opening-bracket ?\[
3977                     gnus-tmp-closing-bracket ?\]))
3978             (setq
3979              gnus-tmp-indentation
3980              (aref gnus-thread-indent-array gnus-tmp-level)
3981              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3982              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3983                                 gnus-summary-default-score 0)
3984              gnus-tmp-score-char
3985              (if (or (null gnus-summary-default-score)
3986                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3987                          gnus-summary-zcore-fuzz))
3988                  ?  ;Whitespace
3989                (if (< gnus-tmp-score gnus-summary-default-score)
3990                    gnus-score-below-mark gnus-score-over-mark))
3991              gnus-tmp-replied
3992              (cond ((memq number gnus-newsgroup-processable)
3993                     gnus-process-mark)
3994                    ((memq number gnus-newsgroup-cached)
3995                     gnus-cached-mark)
3996                    ((memq number gnus-newsgroup-replied)
3997                     gnus-replied-mark)
3998                    ((memq number gnus-newsgroup-saved)
3999                     gnus-saved-mark)
4000                    (t gnus-unread-mark))
4001              gnus-tmp-from (mail-header-from gnus-tmp-header)
4002              gnus-tmp-name
4003              (cond
4004               ((string-match "<[^>]+> *$" gnus-tmp-from)
4005                (setq beg-match (match-beginning 0))
4006                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4007                         (substring gnus-tmp-from (1+ (match-beginning 0))
4008                                    (1- (match-end 0))))
4009                    (substring gnus-tmp-from 0 beg-match)))
4010               ((string-match "(.+)" gnus-tmp-from)
4011                (substring gnus-tmp-from
4012                           (1+ (match-beginning 0)) (1- (match-end 0))))
4013               (t gnus-tmp-from)))
4014             (when (string= gnus-tmp-name "")
4015               (setq gnus-tmp-name gnus-tmp-from))
4016             (unless (numberp gnus-tmp-lines)
4017               (setq gnus-tmp-lines 0))
4018             (gnus-put-text-property
4019              (point)
4020              (progn (eval gnus-summary-line-format-spec) (point))
4021              'gnus-number number)
4022             (when gnus-visual-p
4023               (forward-line -1)
4024               (gnus-run-hooks 'gnus-summary-update-hook)
4025               (forward-line 1))
4026
4027             (setq gnus-tmp-prev-subject subject)))
4028
4029         (when (nth 1 thread)
4030           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4031         (incf gnus-tmp-level)
4032         (setq threads (if thread-end nil (cdar thread)))
4033         (unless threads
4034           (setq gnus-tmp-level 0)))))
4035   (gnus-message 7 "Generating summary...done"))
4036
4037 (defun gnus-summary-prepare-unthreaded (headers)
4038   "Generate an unthreaded summary buffer based on HEADERS."
4039   (let (header number mark)
4040
4041     (beginning-of-line)
4042
4043     (while headers
4044       ;; We may have to root out some bad articles...
4045       (when (memq (setq number (mail-header-number
4046                                 (setq header (pop headers))))
4047                   gnus-newsgroup-limit)
4048         ;; Mark article as read when it has a low score.
4049         (when (and gnus-summary-mark-below
4050                    (< (or (cdr (assq number gnus-newsgroup-scored))
4051                           gnus-summary-default-score 0)
4052                       gnus-summary-mark-below)
4053                    (not (gnus-summary-article-ancient-p number)))
4054           (setq gnus-newsgroup-unreads
4055                 (delq number gnus-newsgroup-unreads))
4056           (if gnus-newsgroup-auto-expire
4057               (push number gnus-newsgroup-expirable)
4058             (push (cons number gnus-low-score-mark)
4059                   gnus-newsgroup-reads)))
4060
4061         (setq mark (gnus-article-mark number))
4062         (push (gnus-data-make number mark (1+ (point)) header 0)
4063               gnus-newsgroup-data)
4064         (gnus-summary-insert-line
4065          header 0 number
4066          mark (memq number gnus-newsgroup-replied)
4067          (memq number gnus-newsgroup-expirable)
4068          (mail-header-subject header) nil
4069          (cdr (assq number gnus-newsgroup-scored))
4070          (memq number gnus-newsgroup-processable))))))
4071
4072 (defun gnus-summary-remove-list-identifiers ()
4073   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4074   (let ((regexp (if (stringp gnus-list-identifiers)
4075                     gnus-list-identifiers
4076                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4077     (dolist (header gnus-newsgroup-headers)
4078       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4079                           (mail-header-subject header))
4080         (mail-header-set-subject
4081          header (concat (substring (mail-header-subject header)
4082                                    0 (match-beginning 2))
4083                         (substring (mail-header-subject header)
4084                                    (match-end 2))))))))
4085
4086 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4087   "Select newsgroup GROUP.
4088 If READ-ALL is non-nil, all articles in the group are selected.
4089 If SELECT-ARTICLES, only select those articles from GROUP."
4090   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4091          ;;!!! Dirty hack; should be removed.
4092          (gnus-summary-ignore-duplicates
4093           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4094               t
4095             gnus-summary-ignore-duplicates))
4096          (info (nth 2 entry))
4097          articles fetched-articles cached)
4098
4099     (unless (gnus-check-server
4100              (setq gnus-current-select-method
4101                    (gnus-find-method-for-group group)))
4102       (error "Couldn't open server"))
4103
4104     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4105         (gnus-activate-group group)     ; Or we can activate it...
4106         (progn                          ; Or we bug out.
4107           (when (equal major-mode 'gnus-summary-mode)
4108             (kill-buffer (current-buffer)))
4109           (error "Couldn't request group %s: %s"
4110                  group (gnus-status-message group))))
4111
4112     (unless (gnus-request-group group t)
4113       (when (equal major-mode 'gnus-summary-mode)
4114         (kill-buffer (current-buffer)))
4115       (error "Couldn't request group %s: %s"
4116              group (gnus-status-message group)))
4117
4118     (setq gnus-newsgroup-name group)
4119     (setq gnus-newsgroup-unselected nil)
4120     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4121     (gnus-summary-setup-default-charset)
4122
4123     ;; Adjust and set lists of article marks.
4124     (when info
4125       (gnus-adjust-marked-articles info))
4126
4127     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4128     (when (gnus-virtual-group-p group)
4129       (setq cached gnus-newsgroup-cached))
4130
4131     (setq gnus-newsgroup-unreads
4132           (gnus-set-difference
4133            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4134            gnus-newsgroup-dormant))
4135
4136     (setq gnus-newsgroup-processable nil)
4137
4138     (gnus-update-read-articles group gnus-newsgroup-unreads)
4139
4140     (if (setq articles select-articles)
4141         (setq gnus-newsgroup-unselected
4142               (gnus-sorted-intersection
4143                gnus-newsgroup-unreads
4144                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4145       (setq articles (gnus-articles-to-read group read-all)))
4146
4147     (cond
4148      ((null articles)
4149       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4150       'quit)
4151      ((eq articles 0) nil)
4152      (t
4153       ;; Init the dependencies hash table.
4154       (setq gnus-newsgroup-dependencies
4155             (gnus-make-hashtable (length articles)))
4156       (gnus-set-global-variables)
4157       ;; Retrieve the headers and read them in.
4158       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4159       (setq gnus-newsgroup-headers
4160             (if (eq 'nov
4161                     (setq gnus-headers-retrieved-by
4162                           (gnus-retrieve-headers
4163                            articles gnus-newsgroup-name
4164                            ;; We might want to fetch old headers, but
4165                            ;; not if there is only 1 article.
4166                            (and (or (and
4167                                      (not (eq gnus-fetch-old-headers 'some))
4168                                      (not (numberp gnus-fetch-old-headers)))
4169                                     (> (length articles) 1))
4170                                 gnus-fetch-old-headers))))
4171                 (gnus-get-newsgroup-headers-xover
4172                  articles nil nil gnus-newsgroup-name t)
4173               (gnus-get-newsgroup-headers)))
4174       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4175
4176       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4177       (when cached
4178         (setq gnus-newsgroup-cached cached))
4179
4180       ;; Suppress duplicates?
4181       (when gnus-suppress-duplicates
4182         (gnus-dup-suppress-articles))
4183
4184       ;; Set the initial limit.
4185       (setq gnus-newsgroup-limit (copy-sequence articles))
4186       ;; Remove canceled articles from the list of unread articles.
4187       (setq gnus-newsgroup-unreads
4188             (gnus-set-sorted-intersection
4189              gnus-newsgroup-unreads
4190              (setq fetched-articles
4191                    (mapcar (lambda (headers) (mail-header-number headers))
4192                            gnus-newsgroup-headers))))
4193       ;; Removed marked articles that do not exist.
4194       (gnus-update-missing-marks
4195        (gnus-sorted-complement fetched-articles articles))
4196       ;; We might want to build some more threads first.
4197       (when (and gnus-fetch-old-headers
4198                  (eq gnus-headers-retrieved-by 'nov))
4199         (if (eq gnus-fetch-old-headers 'invisible)
4200             (gnus-build-all-threads)
4201           (gnus-build-old-threads)))
4202       ;; Let the Gnus agent mark articles as read.
4203       (when gnus-agent
4204         (gnus-agent-get-undownloaded-list))
4205       ;; Remove list identifiers from subject
4206       (when gnus-list-identifiers
4207         (gnus-summary-remove-list-identifiers))
4208       ;; Check whether auto-expire is to be done in this group.
4209       (setq gnus-newsgroup-auto-expire
4210             (gnus-group-auto-expirable-p group))
4211       ;; Set up the article buffer now, if necessary.
4212       (unless gnus-single-article-buffer
4213         (gnus-article-setup-buffer))
4214       ;; First and last article in this newsgroup.
4215       (when gnus-newsgroup-headers
4216         (setq gnus-newsgroup-begin
4217               (mail-header-number (car gnus-newsgroup-headers))
4218               gnus-newsgroup-end
4219               (mail-header-number
4220                (gnus-last-element gnus-newsgroup-headers))))
4221       ;; GROUP is successfully selected.
4222       (or gnus-newsgroup-headers t)))))
4223
4224 (defvar gnus-summary-default-select-to-read ?a)
4225 (defvar gnus-summary-select-to-read-help-winconf nil)
4226
4227 (defun gnus-summary-select-to-read (prompt)
4228   (let ((cursor-in-echo-area nil)
4229         (message-log-max nil)
4230         (char-to-method
4231          '((?a "Number of `gnus-large-newsgroup' in the marked and unread."
4232                (cons 'all gnus-large-newsgroup))
4233            (?A "All of the marked and unread." (cons 'all nil))
4234            (?m "Number of `gnus-large-newsgroup' in the marked."
4235                (cons 'marked gnus-large-newsgroup))
4236            (?M "All of the marked." (cons 'marked nil))
4237            (?r "Number of `gnus-large-newsgroup' in the unread."
4238                (cons 'unread gnus-large-newsgroup))
4239            (?R "All of the unread." (cons 'unread nil))))
4240         rest char)
4241     (unwind-protect
4242         (while (not rest)
4243           (message "%s (aAmMnrR, SPC, RET, 0-9, -, ? or C-h): " prompt
4244                    gnus-summary-default-select-to-read)
4245           (setq char (car (gnus-read-event-char)))
4246           (when (memq char '(?\  ?n ?\C-j ?\C-m))
4247             (setq char gnus-summary-default-select-to-read))
4248           (cond
4249            ((setq rest (cddr (assq char char-to-method)))
4250             (setq rest (eval (car rest))
4251                   gnus-summary-default-select-to-read char))
4252            ((memq char '(?? ?\C-h))
4253             (setq gnus-summary-select-to-read-help-winconf
4254                   (current-window-configuration))
4255             (save-excursion
4256               (set-buffer (gnus-get-buffer-create "*Summary select help*"))
4257               (buffer-disable-undo)
4258               (delete-windows-on (current-buffer))
4259               (erase-buffer)
4260               (insert
4261                "Do you want to read:\n\n"
4262                (format
4263                 "SPACE, C-j, C-m, RET: Default method. (currently same as %c)\n"
4264                 gnus-summary-default-select-to-read)
4265                "0-9 or -: Number of input value in the marked and unread.\n")
4266               (let ((list char-to-method)
4267                     elem)
4268                 (while (setq elem (pop list))
4269                   (insert (format "%c: %s\n" (car elem) (cadr elem)))))
4270               (gnus-appt-select-lowest-window)
4271               (split-window)
4272               (pop-to-buffer "*Summary select help*")
4273               (let ((window-min-height 1))
4274                 (shrink-window-if-larger-than-buffer))
4275               (select-window (get-buffer-window gnus-group-buffer t))))
4276            (t
4277             (let (init)
4278               (cond
4279                ((eq char ?-)
4280                 (setq init "-"))
4281                ((eq char ?0)
4282                 (setq init ""))
4283                ((memq char '(?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
4284                 (setq init (char-to-string char)))
4285                (t
4286                 (setq char nil)))
4287               (when char
4288                 (let ((cursor-in-echo-area nil))
4289                   (setq rest (read-from-minibuffer (concat prompt ": ") init))
4290                   (setq rest (cons 'all
4291                                    (if (string-match "^[ \t]*$" rest)
4292                                        nil
4293                                      rest)))))
4294               (setq char nil)))))
4295       (gnus-summary-select-to-read-kill-help-buffer))
4296     (when char
4297       (setq gnus-summary-default-select-to-read char))
4298     rest))
4299
4300 (defun gnus-summary-select-to-read-kill-help-buffer ()
4301   (when (get-buffer "*Summary select help*")
4302     (kill-buffer "*Summary select help*")
4303     (when gnus-summary-select-to-read-help-winconf
4304       (set-window-configuration gnus-summary-select-to-read-help-winconf))))
4305
4306 (defun gnus-articles-to-read (group &optional read-all)
4307   ;; Find out what articles the user wants to read.
4308   (let* ((articles
4309           ;; Select all articles if `read-all' is non-nil, or if there
4310           ;; are no unread articles.
4311           (if (or read-all
4312                   (and (zerop (length gnus-newsgroup-marked))
4313                        (zerop (length gnus-newsgroup-unreads)))
4314                   (eq (gnus-group-find-parameter group 'display)
4315                       'all))
4316               (or
4317                (gnus-uncompress-range (gnus-active group))
4318                (gnus-cache-articles-in-group group))
4319             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4320                           (copy-sequence gnus-newsgroup-unreads))
4321                   '<)))
4322          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4323          (scored (length scored-list))
4324          (number (length articles))
4325          (marked (+ (length gnus-newsgroup-marked)
4326                     (length gnus-newsgroup-dormant)))
4327          (select
4328           (cond
4329            ((numberp read-all)
4330             read-all)
4331            (t
4332             (condition-case ()
4333                 (cond
4334                  ((and (or (<= scored marked) (= scored number))
4335                        (numberp gnus-large-newsgroup)
4336                        (> number gnus-large-newsgroup))
4337                   (let* ((cursor-in-echo-area nil)
4338                          (rest (gnus-summary-select-to-read
4339                                 (format
4340                                  "Which articles from %s"
4341                                  (gnus-limit-string gnus-newsgroup-name 20))))
4342                          (method (car rest)))
4343                     (if (eq method 'all)
4344                         (or (cdr rest) number)
4345                       (setq articles (sort
4346                                       (cond
4347                                        ((eq method 'marked)
4348                                         (append gnus-newsgroup-dormant
4349                                                 gnus-newsgroup-marked))
4350                                        ((eq method 'unread)
4351                                         (copy-sequence
4352                                          gnus-newsgroup-unreads)))
4353                                       '<)
4354                             scored-list (gnus-killed-articles
4355                                          gnus-newsgroup-killed articles)
4356                             scored (length scored-list)
4357                             number (length articles))
4358                       (or (cdr rest) number))))
4359                  ((and (> scored marked) (< scored number)
4360                        (> (- scored number) 20))
4361                   (let ((input
4362                          (read-string
4363                           (format "%s %s (%d scored, %d total): "
4364                                   "How many articles from"
4365                                   group scored number))))
4366                     (if (string-match "^[ \t]*$" input)
4367                         number input)))
4368                  (t number))
4369               (quit nil))))))
4370     (setq select (if (stringp select) (string-to-number select) select))
4371     (if (or (null select) (zerop select))
4372         select
4373       (if (and (not (zerop scored)) (<= (abs select) scored))
4374           (progn
4375             (setq articles (sort scored-list '<))
4376             (setq number (length articles)))
4377         (setq articles (copy-sequence articles)))
4378
4379       (when (< (abs select) number)
4380         (if (< select 0)
4381             ;; Select the N oldest articles.
4382             (setcdr (nthcdr (1- (abs select)) articles) nil)
4383           ;; Select the N most recent articles.
4384           (setq articles (nthcdr (- number select) articles))))
4385       (setq gnus-newsgroup-unselected
4386             (gnus-sorted-intersection
4387              gnus-newsgroup-unreads
4388              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4389       (when gnus-alter-articles-to-read-function
4390         (setq gnus-newsgroup-unreads
4391               (sort 
4392                (funcall gnus-alter-articles-to-read-function
4393                         gnus-newsgroup-name gnus-newsgroup-unreads)
4394                '<)))
4395       articles)))
4396
4397 (defun gnus-killed-articles (killed articles)
4398   (let (out)
4399     (while articles
4400       (when (inline (gnus-member-of-range (car articles) killed))
4401         (push (car articles) out))
4402       (setq articles (cdr articles)))
4403     out))
4404
4405 (defun gnus-uncompress-marks (marks)
4406   "Uncompress the mark ranges in MARKS."
4407   (let ((uncompressed '(score bookmark))
4408         out)
4409     (while marks
4410       (if (memq (caar marks) uncompressed)
4411           (push (car marks) out)
4412         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4413       (setq marks (cdr marks)))
4414     out))
4415
4416 (defun gnus-adjust-marked-articles (info)
4417   "Set all article lists and remove all marks that are no longer valid."
4418   (let* ((marked-lists (gnus-info-marks info))
4419          (active (gnus-active (gnus-info-group info)))
4420          (min (car active))
4421          (max (cdr active))
4422          (types gnus-article-mark-lists)
4423          (uncompressed '(score bookmark killed))
4424          marks var articles article mark)
4425
4426     (while marked-lists
4427       (setq marks (pop marked-lists))
4428       (set (setq var (intern (format "gnus-newsgroup-%s"
4429                                      (car (rassq (setq mark (car marks))
4430                                                  types)))))
4431            (if (memq (car marks) uncompressed) (cdr marks)
4432              (gnus-uncompress-range (cdr marks))))
4433
4434       (setq articles (symbol-value var))
4435
4436       ;; All articles have to be subsets of the active articles.
4437       (cond
4438        ;; Adjust "simple" lists.
4439        ((memq mark '(tick dormant expire reply save))
4440         (while articles
4441           (when (or (< (setq article (pop articles)) min) (> article max))
4442             (set var (delq article (symbol-value var))))))
4443        ;; Adjust assocs.
4444        ((memq mark uncompressed)
4445         (when (not (listp (cdr (symbol-value var))))
4446           (set var (list (symbol-value var))))
4447         (when (not (listp (cdr articles)))
4448           (setq articles (list articles)))
4449         (while articles
4450           (when (or (not (consp (setq article (pop articles))))
4451                     (< (car article) min)
4452                     (> (car article) max))
4453             (set var (delq article (symbol-value var))))))))))
4454
4455 (defun gnus-update-missing-marks (missing)
4456   "Go through the list of MISSING articles and remove them from the mark lists."
4457   (when missing
4458     (let ((types gnus-article-mark-lists)
4459           var m)
4460       ;; Go through all types.
4461       (while types
4462         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4463         (when (symbol-value var)
4464           ;; This list has articles.  So we delete all missing articles
4465           ;; from it.
4466           (setq m missing)
4467           (while m
4468             (set var (delq (pop m) (symbol-value var)))))))))
4469
4470 (defun gnus-update-marks ()
4471   "Enter the various lists of marked articles into the newsgroup info list."
4472   (let ((types gnus-article-mark-lists)
4473         (info (gnus-get-info gnus-newsgroup-name))
4474         (uncompressed '(score bookmark killed))
4475         type list newmarked symbol delta-marks)
4476     (when info
4477       ;; Add all marks lists to the list of marks lists.
4478       (while (setq type (pop types))
4479         (setq list (symbol-value
4480                     (setq symbol
4481                           (intern (format "gnus-newsgroup-%s"
4482                                           (car type))))))
4483
4484         (when list
4485           ;; Get rid of the entries of the articles that have the
4486           ;; default score.
4487           (when (and (eq (cdr type) 'score)
4488                      gnus-save-score
4489                      list)
4490             (let* ((arts list)
4491                    (prev (cons nil list))
4492                    (all prev))
4493               (while arts
4494                 (if (or (not (consp (car arts)))
4495                         (= (cdar arts) gnus-summary-default-score))
4496                     (setcdr prev (cdr arts))
4497                   (setq prev arts))
4498                 (setq arts (cdr arts)))
4499               (setq list (cdr all)))))
4500
4501         (unless (memq (cdr type) uncompressed)
4502           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4503        
4504         (when (gnus-check-backend-function
4505                'request-set-mark gnus-newsgroup-name)
4506           ;; uncompressed:s are not proper flags (they are cons cells)
4507           ;; cache is a internal gnus flag
4508           (unless (memq (cdr type) (cons 'cache uncompressed))
4509             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4510                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4511                    (add (gnus-remove-from-range
4512                          (gnus-copy-sequence list) old)))
4513               (when add
4514                 (push (list add 'add (list (cdr type))) delta-marks))
4515               (when del
4516                 (push (list del 'del (list (cdr type))) delta-marks)))))
4517           
4518         (when list
4519           (push (cons (cdr type) list) newmarked)))
4520
4521       (when delta-marks
4522         (unless (gnus-check-group gnus-newsgroup-name)
4523           (error "Can't open server for %s" gnus-newsgroup-name))
4524         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4525           
4526       ;; Enter these new marks into the info of the group.
4527       (if (nthcdr 3 info)
4528           (setcar (nthcdr 3 info) newmarked)
4529         ;; Add the marks lists to the end of the info.
4530         (when newmarked
4531           (setcdr (nthcdr 2 info) (list newmarked))))
4532
4533       ;; Cut off the end of the info if there's nothing else there.
4534       (let ((i 5))
4535         (while (and (> i 2)
4536                     (not (nth i info)))
4537           (when (nthcdr (decf i) info)
4538             (setcdr (nthcdr i info) nil)))))))
4539
4540 (defun gnus-set-mode-line (where)
4541   "This function sets the mode line of the article or summary buffers.
4542 If WHERE is `summary', the summary mode line format will be used."
4543   ;; Is this mode line one we keep updated?
4544   (when (and (memq where gnus-updated-mode-lines)
4545              (symbol-value
4546               (intern (format "gnus-%s-mode-line-format-spec" where))))
4547     (let (mode-string)
4548       (save-excursion
4549         ;; We evaluate this in the summary buffer since these
4550         ;; variables are buffer-local to that buffer.
4551         (set-buffer gnus-summary-buffer)
4552         ;; We bind all these variables that are used in the `eval' form
4553         ;; below.
4554         (let* ((mformat (symbol-value
4555                          (intern
4556                           (format "gnus-%s-mode-line-format-spec" where))))
4557                (gnus-tmp-group-name gnus-newsgroup-name)
4558                (gnus-tmp-article-number (or gnus-current-article 0))
4559                (gnus-tmp-unread gnus-newsgroup-unreads)
4560                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4561                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4562                (gnus-tmp-unread-and-unselected
4563                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4564                             (zerop gnus-tmp-unselected))
4565                        "")
4566                       ((zerop gnus-tmp-unselected)
4567                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4568                       (t (format "{%d(+%d) more}"
4569                                  gnus-tmp-unread-and-unticked
4570                                  gnus-tmp-unselected))))
4571                (gnus-tmp-subject
4572                 (if (and gnus-current-headers
4573                          (vectorp gnus-current-headers))
4574                     (gnus-mode-string-quote
4575                      (mail-header-subject gnus-current-headers))
4576                   ""))
4577                bufname-length max-len
4578                gnus-tmp-header);; passed as argument to any user-format-funcs
4579           (setq mode-string (eval mformat))
4580           (setq bufname-length (if (string-match "%b" mode-string)
4581                                    (- (length
4582                                        (buffer-name
4583                                         (if (eq where 'summary)
4584                                             nil
4585                                           (get-buffer gnus-article-buffer))))
4586                                       2)
4587                                  0))
4588           (setq max-len (max 4 (if gnus-mode-non-string-length
4589                                    (- (window-width)
4590                                       gnus-mode-non-string-length
4591                                       bufname-length)
4592                                  (length mode-string))))
4593           ;; We might have to chop a bit of the string off...
4594           (when (> (length mode-string) max-len)
4595             (setq mode-string
4596                   (concat (truncate-string-to-width mode-string (- max-len 3))
4597                           "...")))
4598           ;; Pad the mode string a bit.
4599           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4600       ;; Update the mode line.
4601       (setq mode-line-buffer-identification
4602             (gnus-mode-line-buffer-identification (list mode-string)))
4603       (set-buffer-modified-p t))))
4604
4605 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4606   "Go through the HEADERS list and add all Xrefs to a hash table.
4607 The resulting hash table is returned, or nil if no Xrefs were found."
4608   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4609          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4610          (xref-hashtb (gnus-make-hashtable))
4611          start group entry number xrefs header)
4612     (while headers
4613       (setq header (pop headers))
4614       (when (and (setq xrefs (mail-header-xref header))
4615                  (not (memq (setq number (mail-header-number header))
4616                             unreads)))
4617         (setq start 0)
4618         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4619           (setq start (match-end 0))
4620           (setq group (if prefix
4621                           (concat prefix (substring xrefs (match-beginning 1)
4622                                                     (match-end 1)))
4623                         (substring xrefs (match-beginning 1) (match-end 1))))
4624           (setq number
4625                 (string-to-int (substring xrefs (match-beginning 2)
4626                                           (match-end 2))))
4627           (if (setq entry (gnus-gethash group xref-hashtb))
4628               (setcdr entry (cons number (cdr entry)))
4629             (gnus-sethash group (cons number nil) xref-hashtb)))))
4630     (and start xref-hashtb)))
4631
4632 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4633   "Look through all the headers and mark the Xrefs as read."
4634   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4635         name entry info xref-hashtb idlist method nth4)
4636     (save-excursion
4637       (set-buffer gnus-group-buffer)
4638       (when (setq xref-hashtb
4639                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4640         (mapatoms
4641          (lambda (group)
4642            (unless (string= from-newsgroup (setq name (symbol-name group)))
4643              (setq idlist (symbol-value group))
4644              ;; Dead groups are not updated.
4645              (and (prog1
4646                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4647                             info (nth 2 entry))
4648                     (when (stringp (setq nth4 (gnus-info-method info)))
4649                       (setq nth4 (gnus-server-to-method nth4))))
4650                   ;; Only do the xrefs if the group has the same
4651                   ;; select method as the group we have just read.
4652                   (or (gnus-methods-equal-p
4653                        nth4 (gnus-find-method-for-group from-newsgroup))
4654                       virtual
4655                       (equal nth4 (setq method (gnus-find-method-for-group
4656                                                 from-newsgroup)))
4657                       (and (equal (car nth4) (car method))
4658                            (equal (nth 1 nth4) (nth 1 method))))
4659                   gnus-use-cross-reference
4660                   (or (not (eq gnus-use-cross-reference t))
4661                       virtual
4662                       ;; Only do cross-references on subscribed
4663                       ;; groups, if that is what is wanted.
4664                       (<= (gnus-info-level info) gnus-level-subscribed))
4665                   (gnus-group-make-articles-read name idlist))))
4666          xref-hashtb)))))
4667
4668 (defun gnus-compute-read-articles (group articles)
4669   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4670          (info (nth 2 entry))
4671          (active (gnus-active group))
4672          ninfo)
4673     (when entry
4674       ;; First peel off all invalid article numbers.
4675       (when active
4676         (let ((ids articles)
4677               id first)
4678           (while (setq id (pop ids))
4679             (when (and first (> id (cdr active)))
4680               ;; We'll end up in this situation in one particular
4681               ;; obscure situation.  If you re-scan a group and get
4682               ;; a new article that is cross-posted to a different
4683               ;; group that has not been re-scanned, you might get
4684               ;; crossposted article that has a higher number than
4685               ;; Gnus believes possible.  So we re-activate this
4686               ;; group as well.  This might mean doing the
4687               ;; crossposting thingy will *increase* the number
4688               ;; of articles in some groups.  Tsk, tsk.
4689               (setq active (or (gnus-activate-group group) active)))
4690             (when (or (> id (cdr active))
4691                       (< id (car active)))
4692               (setq articles (delq id articles))))))
4693       ;; If the read list is nil, we init it.
4694       (if (and active
4695                (null (gnus-info-read info))
4696                (> (car active) 1))
4697           (setq ninfo (cons 1 (1- (car active))))
4698         (setq ninfo (gnus-info-read info)))
4699       ;; Then we add the read articles to the range.
4700       (gnus-add-to-range
4701        ninfo (setq articles (sort articles '<))))))
4702
4703 (defun gnus-group-make-articles-read (group articles)
4704   "Update the info of GROUP to say that ARTICLES are read."
4705   (let* ((num 0)
4706          (entry (gnus-gethash group gnus-newsrc-hashtb))
4707          (info (nth 2 entry))
4708          (active (gnus-active group))
4709          range)
4710     (when entry
4711       (setq range (gnus-compute-read-articles group articles))
4712       (save-excursion
4713         (set-buffer gnus-group-buffer)
4714         (gnus-undo-register
4715           `(progn
4716              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4717              (gnus-info-set-read ',info ',(gnus-info-read info))
4718              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4719              (gnus-group-update-group ,group t))))
4720       ;; Add the read articles to the range.
4721       (gnus-info-set-read info range)
4722       ;; Then we have to re-compute how many unread
4723       ;; articles there are in this group.
4724       (when active
4725         (cond
4726          ((not range)
4727           (setq num (- (1+ (cdr active)) (car active))))
4728          ((not (listp (cdr range)))
4729           (setq num (- (cdr active) (- (1+ (cdr range))
4730                                        (car range)))))
4731          (t
4732           (while range
4733             (if (numberp (car range))
4734                 (setq num (1+ num))
4735               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4736             (setq range (cdr range)))
4737           (setq num (- (cdr active) num))))
4738         ;; Update the number of unread articles.
4739         (setcar entry num)
4740         ;; Update the group buffer.
4741         (gnus-group-update-group group t)))))
4742
4743 (defvar gnus-newsgroup-none-id 0)
4744
4745 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4746   (let ((cur nntp-server-buffer)
4747         (dependencies
4748          (or dependencies
4749              (save-excursion (set-buffer gnus-summary-buffer)
4750                              gnus-newsgroup-dependencies)))
4751         headers id end ref
4752         (mail-parse-charset gnus-newsgroup-charset)
4753         (mail-parse-ignored-charsets 
4754          (save-excursion (condition-case nil
4755                              (set-buffer gnus-summary-buffer)
4756                            (error))
4757                          gnus-newsgroup-ignored-charsets)))
4758     (save-excursion
4759       (set-buffer nntp-server-buffer)
4760       ;; Translate all TAB characters into SPACE characters.
4761       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4762       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4763       (gnus-run-hooks 'gnus-parse-headers-hook)
4764       (let ((case-fold-search t)
4765             in-reply-to header p lines chars)
4766         (goto-char (point-min))
4767         ;; Search to the beginning of the next header.  Error messages
4768         ;; do not begin with 2 or 3.
4769         (while (re-search-forward "^[23][0-9]+ " nil t)
4770           (setq id nil
4771                 ref nil)
4772           ;; This implementation of this function, with nine
4773           ;; search-forwards instead of the one re-search-forward and
4774           ;; a case (which basically was the old function) is actually
4775           ;; about twice as fast, even though it looks messier.  You
4776           ;; can't have everything, I guess.  Speed and elegance
4777           ;; doesn't always go hand in hand.
4778           (setq
4779            header
4780            (make-full-mail-header
4781             ;; Number.
4782             (prog1
4783                 (read cur)
4784               (end-of-line)
4785               (setq p (point))
4786               (narrow-to-region (point)
4787                                 (or (and (search-forward "\n.\n" nil t)
4788                                          (- (point) 2))
4789                                     (point))))
4790             ;; Subject.
4791             (progn
4792               (goto-char p)
4793               (if (search-forward "\nsubject: " nil t)
4794                   (nnheader-header-value)
4795                 "(none)"))
4796             ;; From.
4797             (progn
4798               (goto-char p)
4799               (if (search-forward "\nfrom: " nil t)
4800                   (nnheader-header-value)
4801                 "(nobody)"))
4802             ;; Date.
4803             (progn
4804               (goto-char p)
4805               (if (search-forward "\ndate: " nil t)
4806                   (nnheader-header-value) ""))
4807             ;; Message-ID.
4808             (progn
4809               (goto-char p)
4810               (setq id (if (re-search-forward
4811                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4812                            ;; We do it this way to make sure the Message-ID
4813                            ;; is (somewhat) syntactically valid.
4814                            (buffer-substring (match-beginning 1)
4815                                              (match-end 1))
4816                          ;; If there was no message-id, we just fake one
4817                          ;; to make subsequent routines simpler.
4818                          (nnheader-generate-fake-message-id))))
4819             ;; References.
4820             (progn
4821               (goto-char p)
4822               (if (search-forward "\nreferences: " nil t)
4823                   (progn
4824                     (setq end (point))
4825                     (prog1
4826                         (nnheader-header-value)
4827                       (setq ref
4828                             (buffer-substring
4829                              (progn
4830                                (end-of-line)
4831                                (search-backward ">" end t)
4832                                (1+ (point)))
4833                              (progn
4834                                (search-backward "<" end t)
4835                                (point))))))
4836                 ;; Get the references from the in-reply-to header if there
4837                 ;; were no references and the in-reply-to header looks
4838                 ;; promising.
4839                 (if (and (search-forward "\nin-reply-to: " nil t)
4840                          (setq in-reply-to (nnheader-header-value))
4841                          (string-match "<[^>]+>" in-reply-to))
4842                     (let (ref2)
4843                       (setq ref (substring in-reply-to (match-beginning 0)
4844                                            (match-end 0)))
4845                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4846                         (setq ref2 (substring in-reply-to (match-beginning 0)
4847                                               (match-end 0)))
4848                         (when (> (length ref2) (length ref))
4849                           (setq ref ref2)))
4850                       ref)
4851                   (setq ref nil))))
4852             ;; Chars.
4853             (progn
4854               (goto-char p)
4855               (if (search-forward "\nchars: " nil t)
4856                   (if (numberp (setq chars (ignore-errors (read cur))))
4857                       chars 0)
4858                 0))
4859             ;; Lines.
4860             (progn
4861               (goto-char p)
4862               (if (search-forward "\nlines: " nil t)
4863                   (if (numberp (setq lines (ignore-errors (read cur))))
4864                       lines 0)
4865                 0))
4866             ;; Xref.
4867             (progn
4868               (goto-char p)
4869               (and (search-forward "\nxref: " nil t)
4870                    (nnheader-header-value)))
4871             ;; Extra.
4872             (when gnus-extra-headers
4873               (let ((extra gnus-extra-headers)
4874                     out)
4875                 (while extra
4876                   (goto-char p)
4877                   (when (search-forward
4878                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4879                     (push (cons (car extra) (nnheader-header-value))
4880                           out))
4881                   (pop extra))
4882                 out))))
4883           (when (equal id ref)
4884             (setq ref nil))
4885
4886           (when gnus-alter-header-function
4887             (funcall gnus-alter-header-function header)
4888             (setq id (mail-header-id header)
4889                   ref (gnus-parent-id (mail-header-references header))))
4890
4891           (when (setq header
4892                       (gnus-dependencies-add-header
4893                        header dependencies force-new))
4894             (push header headers))
4895           (goto-char (point-max))
4896           (widen))
4897         (nreverse headers)))))
4898
4899 ;; Goes through the xover lines and returns a list of vectors
4900 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4901                                                   force-new dependencies
4902                                                   group also-fetch-heads)
4903   "Parse the news overview data in the server buffer, and return a
4904 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4905   ;; Get the Xref when the users reads the articles since most/some
4906   ;; NNTP servers do not include Xrefs when using XOVER.
4907   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4908   (let ((mail-parse-charset gnus-newsgroup-charset)
4909         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4910         (cur nntp-server-buffer)
4911         (dependencies (or dependencies gnus-newsgroup-dependencies))
4912         number headers header)
4913     (save-excursion
4914       (set-buffer nntp-server-buffer)
4915       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4916       ;; Allow the user to mangle the headers before parsing them.
4917       (gnus-run-hooks 'gnus-parse-headers-hook)
4918       (goto-char (point-min))
4919       (while (not (eobp))
4920         (condition-case ()
4921             (while (and sequence (not (eobp)))
4922               (setq number (read cur))
4923               (while (and sequence
4924                           (< (car sequence) number))
4925                 (setq sequence (cdr sequence)))
4926               (and sequence
4927                    (eq number (car sequence))
4928                    (progn
4929                      (setq sequence (cdr sequence))
4930                      (setq header (inline
4931                                     (gnus-nov-parse-line
4932                                      number dependencies force-new))))
4933                    (push header headers))
4934               (forward-line 1))
4935           (error
4936            (gnus-error 4 "Strange nov line (%d)"
4937                        (count-lines (point-min) (point)))))
4938         (forward-line 1))
4939       ;; A common bug in inn is that if you have posted an article and
4940       ;; then retrieves the active file, it will answer correctly --
4941       ;; the new article is included.  However, a NOV entry for the
4942       ;; article may not have been generated yet, so this may fail.
4943       ;; We work around this problem by retrieving the last few
4944       ;; headers using HEAD.
4945       (if (or (not also-fetch-heads)
4946               (not sequence))
4947           ;; We (probably) got all the headers.
4948           (nreverse headers)
4949         (let ((gnus-nov-is-evil t))
4950           (nconc
4951            (nreverse headers)
4952            (when (gnus-retrieve-headers sequence group)
4953              (gnus-get-newsgroup-headers))))))))
4954
4955 (defun gnus-article-get-xrefs ()
4956   "Fill in the Xref value in `gnus-current-headers', if necessary.
4957 This is meant to be called in `gnus-article-internal-prepare-hook'."
4958   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4959                                  gnus-current-headers)))
4960     (or (not gnus-use-cross-reference)
4961         (not headers)
4962         (and (mail-header-xref headers)
4963              (not (string= (mail-header-xref headers) "")))
4964         (let ((case-fold-search t)
4965               xref)
4966           (save-restriction
4967             (nnheader-narrow-to-headers)
4968             (goto-char (point-min))
4969             (when (or (and (not (eobp))
4970                            (eq (downcase (char-after)) ?x)
4971                            (looking-at "Xref:"))
4972                       (search-forward "\nXref:" nil t))
4973               (goto-char (1+ (match-end 0)))
4974               (setq xref (buffer-substring (point)
4975                                            (progn (end-of-line) (point))))
4976               (mail-header-set-xref headers xref)))))))
4977
4978 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4979   "Find article ID and insert the summary line for that article.
4980 OLD-HEADER can either be a header or a line number to insert
4981 the subject line on."
4982   (let* ((line (and (numberp old-header) old-header))
4983          (old-header (and (vectorp old-header) old-header))
4984          (header (cond ((and old-header use-old-header)
4985                         old-header)
4986                        ((and (numberp id)
4987                              (gnus-number-to-header id))
4988                         (gnus-number-to-header id))
4989                        (t
4990                         (gnus-read-header id))))
4991          (number (and (numberp id) id))
4992          d)
4993     (when header
4994       ;; Rebuild the thread that this article is part of and go to the
4995       ;; article we have fetched.
4996       (when (and (not gnus-show-threads)
4997                  old-header)
4998         (when (and number
4999                    (setq d (gnus-data-find (mail-header-number old-header))))
5000           (goto-char (gnus-data-pos d))
5001           (gnus-data-remove
5002            number
5003            (- (gnus-point-at-bol)
5004               (prog1
5005                   (1+ (gnus-point-at-eol))
5006                 (gnus-delete-line))))))
5007       (when old-header
5008         (mail-header-set-number header (mail-header-number old-header)))
5009       (setq gnus-newsgroup-sparse
5010             (delq (setq number (mail-header-number header))
5011                   gnus-newsgroup-sparse))
5012       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5013       (push number gnus-newsgroup-limit)
5014       (gnus-rebuild-thread (mail-header-id header) line)
5015       (gnus-summary-goto-subject number nil t))
5016     (when (and (numberp number)
5017                (> number 0))
5018       ;; We have to update the boundaries even if we can't fetch the
5019       ;; article if ID is a number -- so that the next `P' or `N'
5020       ;; command will fetch the previous (or next) article even
5021       ;; if the one we tried to fetch this time has been canceled.
5022       (when (> number gnus-newsgroup-end)
5023         (setq gnus-newsgroup-end number))
5024       (when (< number gnus-newsgroup-begin)
5025         (setq gnus-newsgroup-begin number))
5026       (setq gnus-newsgroup-unselected
5027             (delq number gnus-newsgroup-unselected)))
5028     ;; Report back a success?
5029     (and header (mail-header-number header))))
5030
5031 ;;; Process/prefix in the summary buffer
5032
5033 (defun gnus-summary-work-articles (n)
5034   "Return a list of articles to be worked upon.
5035 The prefix argument, the list of process marked articles, and the
5036 current article will be taken into consideration."
5037   (save-excursion
5038     (set-buffer gnus-summary-buffer)
5039     (cond
5040      (n
5041       ;; A numerical prefix has been given.
5042       (setq n (prefix-numeric-value n))
5043       (let ((backward (< n 0))
5044             (n (abs (prefix-numeric-value n)))
5045             articles article)
5046         (save-excursion
5047           (while
5048               (and (> n 0)
5049                    (push (setq article (gnus-summary-article-number))
5050                          articles)
5051                    (if backward
5052                        (gnus-summary-find-prev nil article)
5053                      (gnus-summary-find-next nil article)))
5054             (decf n)))
5055         (nreverse articles)))
5056      ((and (gnus-region-active-p) (mark))
5057       (message "region active")
5058       ;; Work on the region between point and mark.
5059       (let ((max (max (point) (mark)))
5060             articles article)
5061         (save-excursion
5062           (goto-char (min (min (point) (mark))))
5063           (while
5064               (and
5065                (push (setq article (gnus-summary-article-number)) articles)
5066                (gnus-summary-find-next nil article)
5067                (< (point) max)))
5068           (nreverse articles))))
5069      (gnus-newsgroup-processable
5070       ;; There are process-marked articles present.
5071       ;; Save current state.
5072       (gnus-summary-save-process-mark)
5073       ;; Return the list.
5074       (reverse gnus-newsgroup-processable))
5075      (t
5076       ;; Just return the current article.
5077       (list (gnus-summary-article-number))))))
5078
5079 (defmacro gnus-summary-iterate (arg &rest forms)
5080   "Iterate over the process/prefixed articles and do FORMS.
5081 ARG is the interactive prefix given to the command.  FORMS will be
5082 executed with point over the summary line of the articles."
5083   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5084     `(let ((,articles (gnus-summary-work-articles ,arg)))
5085        (while ,articles
5086          (gnus-summary-goto-subject (car ,articles))
5087          ,@forms
5088          (pop ,articles)))))
5089
5090 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5091 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5092
5093 (defun gnus-summary-save-process-mark ()
5094   "Push the current set of process marked articles on the stack."
5095   (interactive)
5096   (push (copy-sequence gnus-newsgroup-processable)
5097         gnus-newsgroup-process-stack))
5098
5099 (defun gnus-summary-kill-process-mark ()
5100   "Push the current set of process marked articles on the stack and unmark."
5101   (interactive)
5102   (gnus-summary-save-process-mark)
5103   (gnus-summary-unmark-all-processable))
5104
5105 (defun gnus-summary-yank-process-mark ()
5106   "Pop the last process mark state off the stack and restore it."
5107   (interactive)
5108   (unless gnus-newsgroup-process-stack
5109     (error "Empty mark stack"))
5110   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5111
5112 (defun gnus-summary-process-mark-set (set)
5113   "Make SET into the current process marked articles."
5114   (gnus-summary-unmark-all-processable)
5115   (while set
5116     (gnus-summary-set-process-mark (pop set))))
5117
5118 ;;; Searching and stuff
5119
5120 (defun gnus-summary-search-group (&optional backward use-level)
5121   "Search for next unread newsgroup.
5122 If optional argument BACKWARD is non-nil, search backward instead."
5123   (save-excursion
5124     (set-buffer gnus-group-buffer)
5125     (when (gnus-group-search-forward
5126            backward nil (if use-level (gnus-group-group-level) nil))
5127       (gnus-group-group-name))))
5128
5129 (defun gnus-summary-best-group (&optional exclude-group)
5130   "Find the name of the best unread group.
5131 If EXCLUDE-GROUP, do not go to this group."
5132   (save-excursion
5133     (set-buffer gnus-group-buffer)
5134     (save-excursion
5135       (gnus-group-best-unread-group exclude-group))))
5136
5137 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5138   (if backward (gnus-summary-find-prev)
5139     (let* ((dummy (gnus-summary-article-intangible-p))
5140            (article (or article (gnus-summary-article-number)))
5141            (arts (gnus-data-find-list article))
5142            result)
5143       (when (and (not dummy)
5144                  (or (not gnus-summary-check-current)
5145                      (not unread)
5146                      (not (gnus-data-unread-p (car arts)))))
5147         (setq arts (cdr arts)))
5148       (when (setq result
5149                   (if unread
5150                       (progn
5151                         (while arts
5152                           (when (or (and undownloaded
5153                                          (eq gnus-undownloaded-mark
5154                                              (gnus-data-mark (car arts))))
5155                                     (gnus-data-unread-p (car arts)))
5156                             (setq result (car arts)
5157                                   arts nil))
5158                           (setq arts (cdr arts)))
5159                         result)
5160                     (car arts)))
5161         (goto-char (gnus-data-pos result))
5162         (gnus-data-number result)))))
5163
5164 (defun gnus-summary-find-prev (&optional unread article)
5165   (let* ((eobp (eobp))
5166          (article (or article (gnus-summary-article-number)))
5167          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5168          result)
5169     (when (and (not eobp)
5170                (or (not gnus-summary-check-current)
5171                    (not unread)
5172                    (not (gnus-data-unread-p (car arts)))))
5173       (setq arts (cdr arts)))
5174     (when (setq result
5175                 (if unread
5176                     (progn
5177                       (while arts
5178                         (when (gnus-data-unread-p (car arts))
5179                           (setq result (car arts)
5180                                 arts nil))
5181                         (setq arts (cdr arts)))
5182                       result)
5183                   (car arts)))
5184       (goto-char (gnus-data-pos result))
5185       (gnus-data-number result))))
5186
5187 (defun gnus-summary-find-subject (subject &optional unread backward article)
5188   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5189          (article (or article (gnus-summary-article-number)))
5190          (articles (gnus-data-list backward))
5191          (arts (gnus-data-find-list article articles))
5192          result)
5193     (when (or (not gnus-summary-check-current)
5194               (not unread)
5195               (not (gnus-data-unread-p (car arts))))
5196       (setq arts (cdr arts)))
5197     (while arts
5198       (and (or (not unread)
5199                (gnus-data-unread-p (car arts)))
5200            (vectorp (gnus-data-header (car arts)))
5201            (gnus-subject-equal
5202             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5203            (setq result (car arts)
5204                  arts nil))
5205       (setq arts (cdr arts)))
5206     (and result
5207          (goto-char (gnus-data-pos result))
5208          (gnus-data-number result))))
5209
5210 (defun gnus-summary-search-forward (&optional unread subject backward)
5211   "Search forward for an article.
5212 If UNREAD, look for unread articles.  If SUBJECT, look for
5213 articles with that subject.  If BACKWARD, search backward instead."
5214   (cond (subject (gnus-summary-find-subject subject unread backward))
5215         (backward (gnus-summary-find-prev unread))
5216         (t (gnus-summary-find-next unread))))
5217
5218 (defun gnus-recenter (&optional n)
5219   "Center point in window and redisplay frame.
5220 Also do horizontal recentering."
5221   (interactive "P")
5222   (when (and nil
5223              gnus-auto-center-summary
5224              (not (eq gnus-auto-center-summary 'vertical)))
5225     (gnus-horizontal-recenter))
5226   (recenter n))
5227
5228 (defun gnus-summary-recenter ()
5229   "Center point in the summary window.
5230 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5231 displayed, no centering will be performed."
5232   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5233   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5234   (interactive)
5235   (let* ((top (cond ((< (window-height) 4) 0)
5236                     ((< (window-height) 7) 1)
5237                     (t (if (numberp gnus-auto-center-summary)
5238                            gnus-auto-center-summary
5239                          2))))
5240          (height (1- (window-height)))
5241          (bottom (save-excursion (goto-char (point-max))
5242                                  (forward-line (- height))
5243                                  (point)))
5244          (window (get-buffer-window (current-buffer))))
5245     ;; The user has to want it.
5246     (when gnus-auto-center-summary
5247       (when (get-buffer-window gnus-article-buffer)
5248         ;; Only do recentering when the article buffer is displayed,
5249         ;; Set the window start to either `bottom', which is the biggest
5250         ;; possible valid number, or the second line from the top,
5251         ;; whichever is the least.
5252         (set-window-start
5253          window (min bottom (save-excursion
5254                               (forward-line (- top)) (point)))
5255          t))
5256       ;; Do horizontal recentering while we're at it.
5257       (when (and (get-buffer-window (current-buffer) t)
5258                  (not (eq gnus-auto-center-summary 'vertical)))
5259         (let ((selected (selected-window)))
5260           (select-window (get-buffer-window (current-buffer) t))
5261           (gnus-summary-position-point)
5262           (gnus-horizontal-recenter)
5263           (select-window selected))))))
5264
5265 (defun gnus-summary-jump-to-group (newsgroup)
5266   "Move point to NEWSGROUP in group mode buffer."
5267   ;; Keep update point of group mode buffer if visible.
5268   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5269       (save-window-excursion
5270         ;; Take care of tree window mode.
5271         (when (get-buffer-window gnus-group-buffer)
5272           (pop-to-buffer gnus-group-buffer))
5273         (gnus-group-jump-to-group newsgroup))
5274     (save-excursion
5275       ;; Take care of tree window mode.
5276       (if (get-buffer-window gnus-group-buffer)
5277           (pop-to-buffer gnus-group-buffer)
5278         (set-buffer gnus-group-buffer))
5279       (gnus-group-jump-to-group newsgroup))))
5280
5281 ;; This function returns a list of article numbers based on the
5282 ;; difference between the ranges of read articles in this group and
5283 ;; the range of active articles.
5284 (defun gnus-list-of-unread-articles (group)
5285   (let* ((read (gnus-info-read (gnus-get-info group)))
5286          (active (or (gnus-active group) (gnus-activate-group group)))
5287          (last (cdr active))
5288          first nlast unread)
5289     ;; If none are read, then all are unread.
5290     (if (not read)
5291         (setq first (car active))
5292       ;; If the range of read articles is a single range, then the
5293       ;; first unread article is the article after the last read
5294       ;; article.  Sounds logical, doesn't it?
5295       (if (and (not (listp (cdr read)))
5296                (or (< (car read) (car active))
5297                    (progn (setq read (list read))
5298                           nil)))
5299           (setq first (max (car active) (1+ (cdr read))))
5300         ;; `read' is a list of ranges.
5301         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5302                                   (caar read)))
5303                   1)
5304           (setq first (car active)))
5305         (while read
5306           (when first
5307             (while (< first nlast)
5308               (push first unread)
5309               (setq first (1+ first))))
5310           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5311           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5312           (setq read (cdr read)))))
5313     ;; And add the last unread articles.
5314     (while (<= first last)
5315       (push first unread)
5316       (setq first (1+ first)))
5317     ;; Return the list of unread articles.
5318     (delq 0 (nreverse unread))))
5319
5320 (defun gnus-list-of-read-articles (group)
5321   "Return a list of unread, unticked and non-dormant articles."
5322   (let* ((info (gnus-get-info group))
5323          (marked (gnus-info-marks info))
5324          (active (gnus-active group)))
5325     (and info active
5326          (gnus-set-difference
5327           (gnus-sorted-complement
5328            (gnus-uncompress-range active)
5329            (gnus-list-of-unread-articles group))
5330           (append
5331            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5332            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5333
5334 ;; Various summary commands
5335
5336 (defun gnus-summary-select-article-buffer ()
5337   "Reconfigure windows to show article buffer."
5338   (interactive)
5339   (if (not (gnus-buffer-live-p gnus-article-buffer))
5340       (error "There is no article buffer for this summary buffer")
5341     (gnus-configure-windows 'article)
5342     (select-window (get-buffer-window gnus-article-buffer))))
5343
5344 (defun gnus-summary-universal-argument (arg)
5345   "Perform any operation on all articles that are process/prefixed."
5346   (interactive "P")
5347   (let ((articles (gnus-summary-work-articles arg))
5348         func article)
5349     (if (eq
5350          (setq
5351           func
5352           (key-binding
5353            (read-key-sequence
5354             (substitute-command-keys
5355              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5356          'undefined)
5357         (gnus-error 1 "Undefined key")
5358       (save-excursion
5359         (while articles
5360           (gnus-summary-goto-subject (setq article (pop articles)))
5361           (let (gnus-newsgroup-processable)
5362             (command-execute func))
5363           (gnus-summary-remove-process-mark article)))))
5364   (gnus-summary-position-point))
5365
5366 (defun gnus-summary-toggle-truncation (&optional arg)
5367   "Toggle truncation of summary lines.
5368 With arg, turn line truncation on iff arg is positive."
5369   (interactive "P")
5370   (setq truncate-lines
5371         (if (null arg) (not truncate-lines)
5372           (> (prefix-numeric-value arg) 0)))
5373   (redraw-display))
5374
5375 (defun gnus-summary-reselect-current-group (&optional all rescan)
5376   "Exit and then reselect the current newsgroup.
5377 The prefix argument ALL means to select all articles."
5378   (interactive "P")
5379   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5380     (error "Ephemeral groups can't be reselected"))
5381   (let ((current-subject (gnus-summary-article-number))
5382         (group gnus-newsgroup-name))
5383     (setq gnus-newsgroup-begin nil)
5384     (gnus-summary-exit)
5385     ;; We have to adjust the point of group mode buffer because
5386     ;; point was moved to the next unread newsgroup by exiting.
5387     (gnus-summary-jump-to-group group)
5388     (when rescan
5389       (save-excursion
5390         (gnus-group-get-new-news-this-group 1)))
5391     (gnus-group-read-group all t)
5392     (gnus-summary-goto-subject current-subject nil t)))
5393
5394 (defun gnus-summary-rescan-group (&optional all)
5395   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5396   (interactive "P")
5397   (gnus-summary-reselect-current-group all t))
5398
5399 (defun gnus-summary-update-info (&optional non-destructive)
5400   (save-excursion
5401     (let ((group gnus-newsgroup-name))
5402       (when group
5403         (when gnus-newsgroup-kill-headers
5404           (setq gnus-newsgroup-killed
5405                 (gnus-compress-sequence
5406                  (nconc
5407                   (gnus-set-sorted-intersection
5408                    (gnus-uncompress-range gnus-newsgroup-killed)
5409                    (setq gnus-newsgroup-unselected
5410                          (sort gnus-newsgroup-unselected '<)))
5411                   (setq gnus-newsgroup-unreads
5412                         (sort gnus-newsgroup-unreads '<)))
5413                  t)))
5414         (unless (listp (cdr gnus-newsgroup-killed))
5415           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5416         (let ((headers gnus-newsgroup-headers))
5417           ;; Set the new ranges of read articles.
5418           (save-excursion
5419             (set-buffer gnus-group-buffer)
5420             (gnus-undo-force-boundary))
5421           (gnus-update-read-articles
5422            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5423           ;; Set the current article marks.
5424           (let ((gnus-newsgroup-scored
5425                  (if (and (not gnus-save-score)
5426                           (not non-destructive))
5427                      nil
5428                    gnus-newsgroup-scored)))
5429             (save-excursion
5430               (gnus-update-marks)))
5431           ;; Do the cross-ref thing.
5432           (when gnus-use-cross-reference
5433             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5434           ;; Do not switch windows but change the buffer to work.
5435           (set-buffer gnus-group-buffer)
5436           (unless (gnus-ephemeral-group-p group)
5437             (gnus-group-update-group group)))))))
5438
5439 (defun gnus-summary-save-newsrc (&optional force)
5440   "Save the current number of read/marked articles in the dribble buffer.
5441 The dribble buffer will then be saved.
5442 If FORCE (the prefix), also save the .newsrc file(s)."
5443   (interactive "P")
5444   (gnus-summary-update-info t)
5445   (if force
5446       (gnus-save-newsrc-file)
5447     (gnus-dribble-save)))
5448
5449 (defun gnus-summary-exit (&optional temporary)
5450   "Exit reading current newsgroup, and then return to group selection mode.
5451 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5452   (interactive)
5453   (gnus-set-global-variables)
5454   (when (gnus-buffer-live-p gnus-article-buffer)
5455     (save-excursion
5456       (set-buffer gnus-article-buffer)
5457       (mm-destroy-parts gnus-article-mime-handles)))
5458   (gnus-kill-save-kill-buffer)
5459   (gnus-async-halt-prefetch)
5460   (let* ((group gnus-newsgroup-name)
5461          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5462          (mode major-mode)
5463          (group-point nil)
5464          (buf (current-buffer)))
5465     (unless quit-config
5466       ;; Do adaptive scoring, and possibly save score files.
5467       (when gnus-newsgroup-adaptive
5468         (gnus-score-adaptive))
5469       (when gnus-use-scoring
5470         (gnus-score-save)))
5471     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5472     ;; If we have several article buffers, we kill them at exit.
5473     (unless gnus-single-article-buffer
5474       (gnus-kill-buffer gnus-original-article-buffer)
5475       (setq gnus-article-current nil))
5476     (when gnus-use-cache
5477       (gnus-cache-possibly-remove-articles)
5478       (gnus-cache-save-buffers))
5479     (gnus-async-prefetch-remove-group group)
5480     (when gnus-suppress-duplicates
5481       (gnus-dup-enter-articles))
5482     (when gnus-use-trees
5483       (gnus-tree-close group))
5484     (when gnus-use-cache
5485       (gnus-cache-write-active))
5486     ;; Remove entries for this group.
5487     (nnmail-purge-split-history (gnus-group-real-name group))
5488     ;; Make all changes in this group permanent.
5489     (unless quit-config
5490       (gnus-run-hooks 'gnus-exit-group-hook)
5491       (gnus-summary-update-info))
5492     (gnus-close-group group)
5493     ;; Make sure where we were, and go to next newsgroup.
5494     (set-buffer gnus-group-buffer)
5495     (unless quit-config
5496       (gnus-group-jump-to-group group))
5497     (gnus-run-hooks 'gnus-summary-exit-hook)
5498     (unless (or quit-config
5499                 ;; If this group has disappeared from the summary
5500                 ;; buffer, don't skip forwards.
5501                 (not (string= group (gnus-group-group-name))))
5502       (gnus-group-next-unread-group 1))
5503     (setq group-point (point))
5504     (if temporary
5505         nil                             ;Nothing to do.
5506       ;; If we have several article buffers, we kill them at exit.
5507       (unless gnus-single-article-buffer
5508         (gnus-kill-buffer gnus-article-buffer)
5509         (gnus-kill-buffer gnus-original-article-buffer)
5510         (setq gnus-article-current nil))
5511       (set-buffer buf)
5512       (if (not gnus-kill-summary-on-exit)
5513           (gnus-deaden-summary)
5514         ;; We set all buffer-local variables to nil.  It is unclear why
5515         ;; this is needed, but if we don't, buffer-local variables are
5516         ;; not garbage-collected, it seems.  This would the lead to en
5517         ;; ever-growing Emacs.
5518         (gnus-summary-clear-local-variables)
5519         (when (get-buffer gnus-article-buffer)
5520           (bury-buffer gnus-article-buffer))
5521         ;; We clear the global counterparts of the buffer-local
5522         ;; variables as well, just to be on the safe side.
5523         (set-buffer gnus-group-buffer)
5524         (gnus-summary-clear-local-variables)
5525         ;; Return to group mode buffer.
5526         (when (eq mode 'gnus-summary-mode)
5527           (gnus-kill-buffer buf)))
5528       (setq gnus-current-select-method gnus-select-method)
5529       (pop-to-buffer gnus-group-buffer)
5530       (if (not quit-config)
5531           (progn
5532             (goto-char group-point)
5533             (gnus-configure-windows 'group 'force))
5534         (gnus-handle-ephemeral-exit quit-config))
5535       ;; Clear the current group name.
5536       (unless quit-config
5537         (setq gnus-newsgroup-name nil)))))
5538
5539 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5540 (defun gnus-summary-exit-no-update (&optional no-questions)
5541   "Quit reading current newsgroup without updating read article info."
5542   (interactive)
5543   (let* ((group gnus-newsgroup-name)
5544          (quit-config (gnus-group-quit-config group)))
5545     (when (or no-questions
5546               gnus-expert-user
5547               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5548       (gnus-async-halt-prefetch)
5549       (mapcar 'funcall
5550               (delq 'gnus-summary-expire-articles
5551                     (copy-sequence gnus-summary-prepare-exit-hook)))
5552       (when (gnus-buffer-live-p gnus-article-buffer)
5553         (save-excursion
5554           (set-buffer gnus-article-buffer)
5555           (mm-destroy-parts gnus-article-mime-handles)))
5556       ;; If we have several article buffers, we kill them at exit.
5557       (unless gnus-single-article-buffer
5558         (gnus-kill-buffer gnus-article-buffer)
5559         (gnus-kill-buffer gnus-original-article-buffer)
5560         (setq gnus-article-current nil))
5561       (if (not gnus-kill-summary-on-exit)
5562           (gnus-deaden-summary)
5563         (gnus-close-group group)
5564         (gnus-summary-clear-local-variables)
5565         (set-buffer gnus-group-buffer)
5566         (gnus-summary-clear-local-variables)
5567         (when (get-buffer gnus-summary-buffer)
5568           (kill-buffer gnus-summary-buffer)))
5569       (unless gnus-single-article-buffer
5570         (setq gnus-article-current nil))
5571       (when gnus-use-trees
5572         (gnus-tree-close group))
5573       (gnus-async-prefetch-remove-group group)
5574       (when (get-buffer gnus-article-buffer)
5575         (bury-buffer gnus-article-buffer))
5576       ;; Return to the group buffer.
5577       (gnus-configure-windows 'group 'force)
5578       ;; Clear the current group name.
5579       (setq gnus-newsgroup-name nil)
5580       (when (equal (gnus-group-group-name) group)
5581         (gnus-group-next-unread-group 1))
5582       (when quit-config
5583         (gnus-handle-ephemeral-exit quit-config)))))
5584
5585 (defun gnus-handle-ephemeral-exit (quit-config)
5586   "Handle movement when leaving an ephemeral group.
5587 The state which existed when entering the ephemeral is reset."
5588   (if (not (buffer-name (car quit-config)))
5589       (gnus-configure-windows 'group 'force)
5590     (set-buffer (car quit-config))
5591     (cond ((eq major-mode 'gnus-summary-mode)
5592            (gnus-set-global-variables))
5593           ((eq major-mode 'gnus-article-mode)
5594            (save-excursion
5595              ;; The `gnus-summary-buffer' variable may point
5596              ;; to the old summary buffer when using a single
5597              ;; article buffer.
5598              (unless (gnus-buffer-live-p gnus-summary-buffer)
5599                (set-buffer gnus-group-buffer))
5600              (set-buffer gnus-summary-buffer)
5601              (gnus-set-global-variables))))
5602     (if (or (eq (cdr quit-config) 'article)
5603             (eq (cdr quit-config) 'pick))
5604         (progn
5605           ;; The current article may be from the ephemeral group
5606           ;; thus it is best that we reload this article
5607           (gnus-summary-show-article)
5608           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5609               (gnus-configure-windows 'pick 'force)
5610             (gnus-configure-windows (cdr quit-config) 'force)))
5611       (gnus-configure-windows (cdr quit-config) 'force))
5612     (when (eq major-mode 'gnus-summary-mode)
5613       (gnus-summary-next-subject 1 nil t)
5614       (gnus-summary-recenter)
5615       (gnus-summary-position-point))))
5616
5617 ;;; Dead summaries.
5618
5619 (defvar gnus-dead-summary-mode-map nil)
5620
5621 (unless gnus-dead-summary-mode-map
5622   (setq gnus-dead-summary-mode-map (make-keymap))
5623   (suppress-keymap gnus-dead-summary-mode-map)
5624   (substitute-key-definition
5625    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5626   (let ((keys '("\C-d" "\r" "\177" [delete])))
5627     (while keys
5628       (define-key gnus-dead-summary-mode-map
5629         (pop keys) 'gnus-summary-wake-up-the-dead))))
5630
5631 (defvar gnus-dead-summary-mode nil
5632   "Minor mode for Gnus summary buffers.")
5633
5634 (defun gnus-dead-summary-mode (&optional arg)
5635   "Minor mode for Gnus summary buffers."
5636   (interactive "P")
5637   (when (eq major-mode 'gnus-summary-mode)
5638     (make-local-variable 'gnus-dead-summary-mode)
5639     (setq gnus-dead-summary-mode
5640           (if (null arg) (not gnus-dead-summary-mode)
5641             (> (prefix-numeric-value arg) 0)))
5642     (when gnus-dead-summary-mode
5643       (gnus-add-minor-mode
5644        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5645
5646 (defun gnus-deaden-summary ()
5647   "Make the current summary buffer into a dead summary buffer."
5648   ;; Kill any previous dead summary buffer.
5649   (when (and gnus-dead-summary
5650              (buffer-name gnus-dead-summary))
5651     (save-excursion
5652       (set-buffer gnus-dead-summary)
5653       (when gnus-dead-summary-mode
5654         (kill-buffer (current-buffer)))))
5655   ;; Make this the current dead summary.
5656   (setq gnus-dead-summary (current-buffer))
5657   (gnus-dead-summary-mode 1)
5658   (let ((name (buffer-name)))
5659     (when (string-match "Summary" name)
5660       (rename-buffer
5661        (concat (substring name 0 (match-beginning 0)) "Dead "
5662                (substring name (match-beginning 0)))
5663        t)
5664       (bury-buffer))))
5665
5666 (defun gnus-kill-or-deaden-summary (buffer)
5667   "Kill or deaden the summary BUFFER."
5668   (save-excursion
5669     (when (and (buffer-name buffer)
5670                (not gnus-single-article-buffer))
5671       (save-excursion
5672         (set-buffer buffer)
5673         (gnus-kill-buffer gnus-article-buffer)
5674         (gnus-kill-buffer gnus-original-article-buffer)))
5675     (cond (gnus-kill-summary-on-exit
5676            (when (and gnus-use-trees
5677                       (gnus-buffer-exists-p buffer))
5678              (save-excursion
5679                (set-buffer buffer)
5680                (gnus-tree-close gnus-newsgroup-name)))
5681            (gnus-kill-buffer buffer))
5682           ((gnus-buffer-exists-p buffer)
5683            (save-excursion
5684              (set-buffer buffer)
5685              (gnus-deaden-summary))))))
5686
5687 (defun gnus-summary-wake-up-the-dead (&rest args)
5688   "Wake up the dead summary buffer."
5689   (interactive)
5690   (gnus-dead-summary-mode -1)
5691   (let ((name (buffer-name)))
5692     (when (string-match "Dead " name)
5693       (rename-buffer
5694        (concat (substring name 0 (match-beginning 0))
5695                (substring name (match-end 0)))
5696        t)))
5697   (gnus-message 3 "This dead summary is now alive again"))
5698
5699 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5700 (defun gnus-summary-fetch-faq (&optional faq-dir)
5701   "Fetch the FAQ for the current group.
5702 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5703 in."
5704   (interactive
5705    (list
5706     (when current-prefix-arg
5707       (completing-read
5708        "Faq dir: " (and (listp gnus-group-faq-directory)
5709                         (mapcar (lambda (file) (list file))
5710                                 gnus-group-faq-directory))))))
5711   (let (gnus-faq-buffer)
5712     (when (setq gnus-faq-buffer
5713                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5714       (gnus-configure-windows 'summary-faq))))
5715
5716 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5717 (defun gnus-summary-describe-group (&optional force)
5718   "Describe the current newsgroup."
5719   (interactive "P")
5720   (gnus-group-describe-group force gnus-newsgroup-name))
5721
5722 (defun gnus-summary-describe-briefly ()
5723   "Describe summary mode commands briefly."
5724   (interactive)
5725   (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")))
5726
5727 ;; Walking around group mode buffer from summary mode.
5728
5729 (defun gnus-summary-next-group (&optional no-article target-group backward)
5730   "Exit current newsgroup and then select next unread newsgroup.
5731 If prefix argument NO-ARTICLE is non-nil, no article is selected
5732 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5733 previous group instead."
5734   (interactive "P")
5735   ;; Stop pre-fetching.
5736   (gnus-async-halt-prefetch)
5737   (let ((current-group gnus-newsgroup-name)
5738         (current-buffer (current-buffer))
5739         entered)
5740     ;; First we semi-exit this group to update Xrefs and all variables.
5741     ;; We can't do a real exit, because the window conf must remain
5742     ;; the same in case the user is prompted for info, and we don't
5743     ;; want the window conf to change before that...
5744     (gnus-summary-exit t)
5745     (while (not entered)
5746       ;; Then we find what group we are supposed to enter.
5747       (set-buffer gnus-group-buffer)
5748       (gnus-group-jump-to-group current-group)
5749       (setq target-group
5750             (or target-group
5751                 (if (eq gnus-keep-same-level 'best)
5752                     (gnus-summary-best-group gnus-newsgroup-name)
5753                   (gnus-summary-search-group backward gnus-keep-same-level))))
5754       (if (not target-group)
5755           ;; There are no further groups, so we return to the group
5756           ;; buffer.
5757           (progn
5758             (gnus-message 5 "Returning to the group buffer")
5759             (setq entered t)
5760             (when (gnus-buffer-live-p current-buffer)
5761               (set-buffer current-buffer)
5762               (gnus-summary-exit))
5763             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5764         ;; We try to enter the target group.
5765         (gnus-group-jump-to-group target-group)
5766         (let ((unreads (gnus-group-group-unread)))
5767           (if (and (or (eq t unreads)
5768                        (and unreads (not (zerop unreads))))
5769                    (gnus-summary-read-group
5770                     target-group nil no-article
5771                     (and (buffer-name current-buffer) current-buffer)
5772                     nil backward))
5773               (setq entered t)
5774             (setq current-group target-group
5775                   target-group nil)))))))
5776
5777 (defun gnus-summary-prev-group (&optional no-article)
5778   "Exit current newsgroup and then select previous unread newsgroup.
5779 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5780   (interactive "P")
5781   (gnus-summary-next-group no-article nil t))
5782
5783 ;; Walking around summary lines.
5784
5785 (defun gnus-summary-first-subject (&optional unread undownloaded)
5786   "Go to the first unread subject.
5787 If UNREAD is non-nil, go to the first unread article.
5788 Returns the article selected or nil if there are no unread articles."
5789   (interactive "P")
5790   (prog1
5791       (cond
5792        ;; Empty summary.
5793        ((null gnus-newsgroup-data)
5794         (gnus-message 3 "No articles in the group")
5795         nil)
5796        ;; Pick the first article.
5797        ((not unread)
5798         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5799         (gnus-data-number (car gnus-newsgroup-data)))
5800        ;; No unread articles.
5801        ((null gnus-newsgroup-unreads)
5802         (gnus-message 3 "No more unread articles")
5803         nil)
5804        ;; Find the first unread article.
5805        (t
5806         (let ((data gnus-newsgroup-data))
5807           (while (and data
5808                       (and (not (and undownloaded
5809                                      (eq gnus-undownloaded-mark
5810                                          (gnus-data-mark (car data)))))
5811                            (not (gnus-data-unread-p (car data)))))
5812             (setq data (cdr data)))
5813           (when data
5814             (goto-char (gnus-data-pos (car data)))
5815             (gnus-data-number (car data))))))
5816     (gnus-summary-position-point)))
5817
5818 (defun gnus-summary-next-subject (n &optional unread dont-display)
5819   "Go to next N'th summary line.
5820 If N is negative, go to the previous N'th subject line.
5821 If UNREAD is non-nil, only unread articles are selected.
5822 The difference between N and the actual number of steps taken is
5823 returned."
5824   (interactive "p")
5825   (let ((backward (< n 0))
5826         (n (abs n)))
5827     (while (and (> n 0)
5828                 (if backward
5829                     (gnus-summary-find-prev unread)
5830                   (gnus-summary-find-next unread)))
5831       (unless (zerop (setq n (1- n)))
5832         (gnus-summary-show-thread)))
5833     (when (/= 0 n)
5834       (gnus-message 7 "No more%s articles"
5835                     (if unread " unread" "")))
5836     (unless dont-display
5837       (gnus-summary-recenter)
5838       (gnus-summary-position-point))
5839     n))
5840
5841 (defun gnus-summary-next-unread-subject (n)
5842   "Go to next N'th unread summary line."
5843   (interactive "p")
5844   (gnus-summary-next-subject n t))
5845
5846 (defun gnus-summary-prev-subject (n &optional unread)
5847   "Go to previous N'th summary line.
5848 If optional argument UNREAD is non-nil, only unread article is selected."
5849   (interactive "p")
5850   (gnus-summary-next-subject (- n) unread))
5851
5852 (defun gnus-summary-prev-unread-subject (n)
5853   "Go to previous N'th unread summary line."
5854   (interactive "p")
5855   (gnus-summary-next-subject (- n) t))
5856
5857 (defun gnus-summary-goto-subject (article &optional force silent)
5858   "Go the subject line of ARTICLE.
5859 If FORCE, also allow jumping to articles not currently shown."
5860   (interactive "nArticle number: ")
5861   (let ((b (point))
5862         (data (gnus-data-find article)))
5863     ;; We read in the article if we have to.
5864     (and (not data)
5865          force
5866          (gnus-summary-insert-subject
5867           article
5868           (if (or (numberp force) (vectorp force)) force)
5869           t)
5870          (setq data (gnus-data-find article)))
5871     (goto-char b)
5872     (if (not data)
5873         (progn
5874           (unless silent
5875             (gnus-message 3 "Can't find article %d" article))
5876           nil)
5877       (goto-char (gnus-data-pos data))
5878       (gnus-summary-position-point)
5879       article)))
5880
5881 ;; Walking around summary lines with displaying articles.
5882
5883 (defun gnus-summary-expand-window (&optional arg)
5884   "Make the summary buffer take up the entire Emacs frame.
5885 Given a prefix, will force an `article' buffer configuration."
5886   (interactive "P")
5887   (if arg
5888       (gnus-configure-windows 'article 'force)
5889     (gnus-configure-windows 'summary 'force)))
5890
5891 (defun gnus-summary-display-article (article &optional all-header)
5892   "Display ARTICLE in article buffer."
5893   (gnus-set-global-variables)
5894   (if (null article)
5895       nil
5896     (prog1
5897         (if gnus-summary-display-article-function
5898             (funcall gnus-summary-display-article-function article all-header)
5899           (gnus-article-prepare article all-header))
5900       (gnus-run-hooks 'gnus-select-article-hook)
5901       (when (and gnus-current-article
5902                  (not (zerop gnus-current-article)))
5903         (gnus-summary-goto-subject gnus-current-article))
5904       (gnus-summary-recenter)
5905       (when (and gnus-use-trees gnus-show-threads)
5906         (gnus-possibly-generate-tree article)
5907         (gnus-highlight-selected-tree article))
5908       ;; Successfully display article.
5909       (gnus-article-set-window-start
5910        (cdr (assq article gnus-newsgroup-bookmarks))))))
5911
5912 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5913   "Select the current article.
5914 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5915 non-nil, the article will be re-fetched even if it already present in
5916 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5917 be displayed."
5918   ;; Make sure we are in the summary buffer to work around bbdb bug.
5919   (unless (eq major-mode 'gnus-summary-mode)
5920     (set-buffer gnus-summary-buffer))
5921   (let ((article (or article (gnus-summary-article-number)))
5922         (all-headers (not (not all-headers))) ;Must be T or NIL.
5923         gnus-summary-display-article-function)
5924     (and (not pseudo)
5925          (gnus-summary-article-pseudo-p article)
5926          (error "This is a pseudo-article"))
5927     (save-excursion
5928       (set-buffer gnus-summary-buffer)
5929       (if (or (and gnus-single-article-buffer
5930                    (or (null gnus-current-article)
5931                        (null gnus-article-current)
5932                        (null (get-buffer gnus-article-buffer))
5933                        (not (eq article (cdr gnus-article-current)))
5934                        (not (equal (car gnus-article-current)
5935                                    gnus-newsgroup-name))))
5936               (and (not gnus-single-article-buffer)
5937                    (or (null gnus-current-article)
5938                        (not (eq gnus-current-article article))))
5939               force)
5940           ;; The requested article is different from the current article.
5941           (progn
5942             (gnus-summary-display-article article all-headers)
5943             (when (or all-headers gnus-show-all-headers)
5944               (gnus-article-show-all-headers))
5945             (gnus-article-set-window-start
5946              (cdr (assq article gnus-newsgroup-bookmarks)))
5947             article)
5948         (when (or all-headers gnus-show-all-headers)
5949           (gnus-article-show-all-headers))
5950         'old))))
5951
5952 (defun gnus-summary-set-current-mark (&optional current-mark)
5953   "Obsolete function."
5954   nil)
5955
5956 (defun gnus-summary-next-article (&optional unread subject backward push)
5957   "Select the next article.
5958 If UNREAD, only unread articles are selected.
5959 If SUBJECT, only articles with SUBJECT are selected.
5960 If BACKWARD, the previous article is selected instead of the next."
5961   (interactive "P")
5962   (cond
5963    ;; Is there such an article?
5964    ((and (gnus-summary-search-forward unread subject backward)
5965          (or (gnus-summary-display-article (gnus-summary-article-number))
5966              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5967     (gnus-summary-position-point))
5968    ;; If not, we try the first unread, if that is wanted.
5969    ((and subject
5970          gnus-auto-select-same
5971          (gnus-summary-first-unread-article))
5972     (gnus-summary-position-point)
5973     (gnus-message 6 "Wrapped"))
5974    ;; Try to get next/previous article not displayed in this group.
5975    ((and gnus-auto-extend-newsgroup
5976          (not unread) (not subject))
5977     (gnus-summary-goto-article
5978      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5979      nil (count-lines (point-min) (point))))
5980    ;; Go to next/previous group.
5981    (t
5982     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5983       (gnus-summary-jump-to-group gnus-newsgroup-name))
5984     (let ((cmd last-command-char)
5985           (point
5986            (save-excursion
5987              (set-buffer gnus-group-buffer)
5988              (point)))
5989           (group
5990            (if (eq gnus-keep-same-level 'best)
5991                (gnus-summary-best-group gnus-newsgroup-name)
5992              (gnus-summary-search-group backward gnus-keep-same-level))))
5993       ;; For some reason, the group window gets selected.  We change
5994       ;; it back.
5995       (select-window (get-buffer-window (current-buffer)))
5996       ;; Select next unread newsgroup automagically.
5997       (cond
5998        ((or (not gnus-auto-select-next)
5999             (not cmd))
6000         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6001        ((or (eq gnus-auto-select-next 'quietly)
6002             (and (eq gnus-auto-select-next 'slightly-quietly)
6003                  push)
6004             (and (eq gnus-auto-select-next 'almost-quietly)
6005                  (gnus-summary-last-article-p)))
6006         ;; Select quietly.
6007         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6008             (gnus-summary-exit)
6009           (gnus-message 7 "No more%s articles (%s)..."
6010                         (if unread " unread" "")
6011                         (if group (concat "selecting " group)
6012                           "exiting"))
6013           (gnus-summary-next-group nil group backward)))
6014        (t
6015         (when (gnus-key-press-event-p last-input-event)
6016           (gnus-summary-walk-group-buffer
6017            gnus-newsgroup-name cmd unread backward point))))))))
6018
6019 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6020   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6021                       (?\C-p (gnus-group-prev-unread-group 1))))
6022         (cursor-in-echo-area t)
6023         keve key group ended)
6024     (save-excursion
6025       (set-buffer gnus-group-buffer)
6026       (goto-char start)
6027       (setq group
6028             (if (eq gnus-keep-same-level 'best)
6029                 (gnus-summary-best-group gnus-newsgroup-name)
6030               (gnus-summary-search-group backward gnus-keep-same-level))))
6031     (while (not ended)
6032       (gnus-message
6033        5 "No more%s articles%s" (if unread " unread" "")
6034        (if (and group
6035                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6036            (format " (Type %s for %s [%s])"
6037                    (single-key-description cmd) group
6038                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6039          (format " (Type %s to exit %s)"
6040                  (single-key-description cmd)
6041                  gnus-newsgroup-name)))
6042       ;; Confirm auto selection.
6043       (setq key (car (setq keve (gnus-read-event-char))))
6044       (setq ended t)
6045       (cond
6046        ((assq key keystrokes)
6047         (let ((obuf (current-buffer)))
6048           (switch-to-buffer gnus-group-buffer)
6049           (when group
6050             (gnus-group-jump-to-group group))
6051           (eval (cadr (assq key keystrokes)))
6052           (setq group (gnus-group-group-name))
6053           (switch-to-buffer obuf))
6054         (setq ended nil))
6055        ((equal key cmd)
6056         (if (or (not group)
6057                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6058             (gnus-summary-exit)
6059           (gnus-summary-next-group nil group backward)))
6060        (t
6061         (push (cdr keve) unread-command-events))))))
6062
6063 (defun gnus-summary-next-unread-article ()
6064   "Select unread article after current one."
6065   (interactive)
6066   (gnus-summary-next-article
6067    (or (not (eq gnus-summary-goto-unread 'never))
6068        (gnus-summary-last-article-p (gnus-summary-article-number)))
6069    (and gnus-auto-select-same
6070         (gnus-summary-article-subject))))
6071
6072 (defun gnus-summary-prev-article (&optional unread subject)
6073   "Select the article after the current one.
6074 If UNREAD is non-nil, only unread articles are selected."
6075   (interactive "P")
6076   (gnus-summary-next-article unread subject t))
6077
6078 (defun gnus-summary-prev-unread-article ()
6079   "Select unread article before current one."
6080   (interactive)
6081   (gnus-summary-prev-article
6082    (or (not (eq gnus-summary-goto-unread 'never))
6083        (gnus-summary-first-article-p (gnus-summary-article-number)))
6084    (and gnus-auto-select-same
6085         (gnus-summary-article-subject))))
6086
6087 (defun gnus-summary-next-page (&optional lines circular)
6088   "Show next page of the selected article.
6089 If at the end of the current article, select the next article.
6090 LINES says how many lines should be scrolled up.
6091
6092 If CIRCULAR is non-nil, go to the start of the article instead of
6093 selecting the next article when reaching the end of the current
6094 article."
6095   (interactive "P")
6096   (setq gnus-summary-buffer (current-buffer))
6097   (gnus-set-global-variables)
6098   (let ((article (gnus-summary-article-number))
6099         (article-window (get-buffer-window gnus-article-buffer t))
6100         endp)
6101     ;; If the buffer is empty, we have no article.
6102     (unless article
6103       (error "No article to select"))
6104     (gnus-configure-windows 'article)
6105     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6106         (if (and (eq gnus-summary-goto-unread 'never)
6107                  (not (gnus-summary-last-article-p article)))
6108             (gnus-summary-next-article)
6109           (gnus-summary-next-unread-article))
6110       (if (or (null gnus-current-article)
6111               (null gnus-article-current)
6112               (/= article (cdr gnus-article-current))
6113               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6114           ;; Selected subject is different from current article's.
6115           (gnus-summary-display-article article)
6116         (when article-window
6117           (gnus-eval-in-buffer-window gnus-article-buffer
6118             (setq endp (gnus-article-next-page lines)))
6119           (when endp
6120             (cond (circular
6121                    (gnus-summary-beginning-of-article))
6122                   (lines
6123                    (gnus-message 3 "End of message"))
6124                   ((null lines)
6125                    (if (and (eq gnus-summary-goto-unread 'never)
6126                             (not (gnus-summary-last-article-p article)))
6127                        (gnus-summary-next-article)
6128                      (gnus-summary-next-unread-article))))))))
6129     (gnus-summary-recenter)
6130     (gnus-summary-position-point)))
6131
6132 (defun gnus-summary-prev-page (&optional lines move)
6133   "Show previous page of selected article.
6134 Argument LINES specifies lines to be scrolled down.
6135 If MOVE, move to the previous unread article if point is at
6136 the beginning of the buffer."
6137   (interactive "P")
6138   (let ((article (gnus-summary-article-number))
6139         (article-window (get-buffer-window gnus-article-buffer t))
6140         endp)
6141     (gnus-configure-windows 'article)
6142     (if (or (null gnus-current-article)
6143             (null gnus-article-current)
6144             (/= article (cdr gnus-article-current))
6145             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6146         ;; Selected subject is different from current article's.
6147         (gnus-summary-display-article article)
6148       (gnus-summary-recenter)
6149       (when article-window
6150         (gnus-eval-in-buffer-window gnus-article-buffer
6151           (setq endp (gnus-article-prev-page lines)))
6152         (when (and move endp)
6153           (cond (lines
6154                  (gnus-message 3 "Beginning of message"))
6155                 ((null lines)
6156                  (if (and (eq gnus-summary-goto-unread 'never)
6157                           (not (gnus-summary-first-article-p article)))
6158                      (gnus-summary-prev-article)
6159                    (gnus-summary-prev-unread-article))))))))
6160   (gnus-summary-position-point))
6161
6162 (defun gnus-summary-prev-page-or-article (&optional lines)
6163   "Show previous page of selected article.
6164 Argument LINES specifies lines to be scrolled down.
6165 If at the beginning of the article, go to the next article."
6166   (interactive "P")
6167   (gnus-summary-prev-page lines t))
6168
6169 (defun gnus-summary-scroll-up (lines)
6170   "Scroll up (or down) one line current article.
6171 Argument LINES specifies lines to be scrolled up (or down if negative)."
6172   (interactive "p")
6173   (gnus-configure-windows 'article)
6174   (gnus-summary-show-thread)
6175   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6176     (gnus-eval-in-buffer-window gnus-article-buffer
6177       (cond ((> lines 0)
6178              (when (gnus-article-next-page lines)
6179                (gnus-message 3 "End of message")))
6180             ((< lines 0)
6181              (gnus-article-prev-page (- lines))))))
6182   (gnus-summary-recenter)
6183   (gnus-summary-position-point))
6184
6185 (defun gnus-summary-scroll-down (lines)
6186   "Scroll down (or up) one line current article.
6187 Argument LINES specifies lines to be scrolled down (or up if negative)."
6188   (interactive "p")
6189   (gnus-summary-scroll-up (- lines)))
6190
6191 (defun gnus-summary-next-same-subject ()
6192   "Select next article which has the same subject as current one."
6193   (interactive)
6194   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6195
6196 (defun gnus-summary-prev-same-subject ()
6197   "Select previous article which has the same subject as current one."
6198   (interactive)
6199   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6200
6201 (defun gnus-summary-next-unread-same-subject ()
6202   "Select next unread article which has the same subject as current one."
6203   (interactive)
6204   (gnus-summary-next-article t (gnus-summary-article-subject)))
6205
6206 (defun gnus-summary-prev-unread-same-subject ()
6207   "Select previous unread article which has the same subject as current one."
6208   (interactive)
6209   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6210
6211 (defun gnus-summary-first-unread-article ()
6212   "Select the first unread article.
6213 Return nil if there are no unread articles."
6214   (interactive)
6215   (prog1
6216       (when (gnus-summary-first-subject t)
6217         (gnus-summary-show-thread)
6218         (gnus-summary-first-subject t)
6219         (gnus-summary-display-article (gnus-summary-article-number)))
6220     (gnus-summary-position-point)))
6221
6222 (defun gnus-summary-first-unread-subject ()
6223   "Place the point on the subject line of the first unread article.
6224 Return nil if there are no unread articles."
6225   (interactive)
6226   (prog1
6227       (when (gnus-summary-first-subject t)
6228         (gnus-summary-show-thread)
6229         (gnus-summary-first-subject t))
6230     (gnus-summary-position-point)))
6231
6232 (defun gnus-summary-first-article ()
6233   "Select the first article.
6234 Return nil if there are no articles."
6235   (interactive)
6236   (prog1
6237       (when (gnus-summary-first-subject)
6238         (gnus-summary-show-thread)
6239         (gnus-summary-first-subject)
6240         (gnus-summary-display-article (gnus-summary-article-number)))
6241     (gnus-summary-position-point)))
6242
6243 (defun gnus-summary-best-unread-article ()
6244   "Select the unread article with the highest score."
6245   (interactive)
6246   (let ((best -1000000)
6247         (data gnus-newsgroup-data)
6248         article score)
6249     (while data
6250       (and (gnus-data-unread-p (car data))
6251            (> (setq score
6252                     (gnus-summary-article-score (gnus-data-number (car data))))
6253               best)
6254            (setq best score
6255                  article (gnus-data-number (car data))))
6256       (setq data (cdr data)))
6257     (prog1
6258         (if article
6259             (gnus-summary-goto-article article)
6260           (error "No unread articles"))
6261       (gnus-summary-position-point))))
6262
6263 (defun gnus-summary-last-subject ()
6264   "Go to the last displayed subject line in the group."
6265   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6266     (when article
6267       (gnus-summary-goto-subject article))))
6268
6269 (defun gnus-summary-goto-article (article &optional all-headers force)
6270   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6271 If ALL-HEADERS is non-nil, no header lines are hidden.
6272 If FORCE, go to the article even if it isn't displayed.  If FORCE
6273 is a number, it is the line the article is to be displayed on."
6274   (interactive
6275    (list
6276     (completing-read
6277      "Article number or Message-ID: "
6278      (mapcar (lambda (number) (list (int-to-string number)))
6279              gnus-newsgroup-limit))
6280     current-prefix-arg
6281     t))
6282   (prog1
6283       (if (and (stringp article)
6284                (string-match "@" article))
6285           (gnus-summary-refer-article article)
6286         (when (stringp article)
6287           (setq article (string-to-number article)))
6288         (if (gnus-summary-goto-subject article force)
6289             (gnus-summary-display-article article all-headers)
6290           (gnus-message 4 "Couldn't go to article %s" article) nil))
6291     (gnus-summary-position-point)))
6292
6293 (defun gnus-summary-goto-last-article ()
6294   "Go to the previously read article."
6295   (interactive)
6296   (prog1
6297       (when gnus-last-article
6298         (gnus-summary-goto-article gnus-last-article nil t))
6299     (gnus-summary-position-point)))
6300
6301 (defun gnus-summary-pop-article (number)
6302   "Pop one article off the history and go to the previous.
6303 NUMBER articles will be popped off."
6304   (interactive "p")
6305   (let (to)
6306     (setq gnus-newsgroup-history
6307           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6308     (if to
6309         (gnus-summary-goto-article (car to) nil t)
6310       (error "Article history empty")))
6311   (gnus-summary-position-point))
6312
6313 ;; Summary commands and functions for limiting the summary buffer.
6314
6315 (defun gnus-summary-limit-to-articles (n)
6316   "Limit the summary buffer to the next N articles.
6317 If not given a prefix, use the process marked articles instead."
6318   (interactive "P")
6319   (prog1
6320       (let ((articles (gnus-summary-work-articles n)))
6321         (setq gnus-newsgroup-processable nil)
6322         (gnus-summary-limit articles))
6323     (gnus-summary-position-point)))
6324
6325 (defun gnus-summary-pop-limit (&optional total)
6326   "Restore the previous limit.
6327 If given a prefix, remove all limits."
6328   (interactive "P")
6329   (when total
6330     (setq gnus-newsgroup-limits
6331           (list (mapcar (lambda (h) (mail-header-number h))
6332                         gnus-newsgroup-headers))))
6333   (unless gnus-newsgroup-limits
6334     (error "No limit to pop"))
6335   (prog1
6336       (gnus-summary-limit nil 'pop)
6337     (gnus-summary-position-point)))
6338
6339 (defun gnus-summary-limit-to-subject (subject &optional header)
6340   "Limit the summary buffer to articles that have subjects that match a regexp."
6341   (interactive "sLimit to subject (regexp): ")
6342   (unless header
6343     (setq header "subject"))
6344   (when (not (equal "" subject))
6345     (prog1
6346         (let ((articles (gnus-summary-find-matching
6347                          (or header "subject") subject 'all)))
6348           (unless articles
6349             (error "Found no matches for \"%s\"" subject))
6350           (gnus-summary-limit articles))
6351       (gnus-summary-position-point))))
6352
6353 (defun gnus-summary-limit-to-author (from)
6354   "Limit the summary buffer to articles that have authors that match a regexp."
6355   (interactive "sLimit to author (regexp): ")
6356   (gnus-summary-limit-to-subject from "from"))
6357
6358 (defun gnus-summary-limit-to-age (age &optional younger-p)
6359   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6360 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6361 articles that are younger than AGE days."
6362   (interactive
6363    (let ((younger current-prefix-arg)
6364          (days-got nil)
6365          days)
6366      (while (not days-got)
6367        (setq days (if younger
6368                       (read-string "Limit to articles within (in days): ")
6369                     (read-string "Limit to articles old than (in days): ")))
6370        (when (> (length days) 0)
6371          (setq days (read days)))
6372        (if (numberp days)
6373            (setq days-got t)
6374          (message "Please enter a number.")
6375          (sleep-for 1)))
6376      (list days younger)))
6377   (prog1
6378       (let ((data gnus-newsgroup-data)
6379             (cutoff (days-to-time age))
6380             articles d date is-younger)
6381         (while (setq d (pop data))
6382           (when (and (vectorp (gnus-data-header d))
6383                      (setq date (mail-header-date (gnus-data-header d))))
6384             (setq is-younger (time-less-p
6385                               (time-since (condition-case ()
6386                                               (date-to-time date)
6387                                             (error '(0 0))))
6388                               cutoff))
6389             (when (if younger-p
6390                       is-younger
6391                     (not is-younger))
6392               (push (gnus-data-number d) articles))))
6393         (gnus-summary-limit (nreverse articles)))
6394     (gnus-summary-position-point)))
6395
6396 (defun gnus-summary-limit-to-extra (header regexp)
6397   "Limit the summary buffer to articles that match an 'extra' header."
6398   (interactive
6399    (let ((header
6400           (intern
6401            (gnus-completing-read
6402             (symbol-name (car gnus-extra-headers))      
6403             "Limit extra header:"       
6404             (mapcar (lambda (x) 
6405                       (cons (symbol-name x) x))
6406                     gnus-extra-headers)
6407             nil                 
6408             t))))
6409      (list header
6410            (read-string (format "Limit to header %s (regexp): " header)))))
6411   (when (not (equal "" regexp))
6412     (prog1
6413         (let ((articles (gnus-summary-find-matching
6414                          (cons 'extra header) regexp 'all)))
6415           (unless articles
6416             (error "Found no matches for \"%s\"" regexp))
6417           (gnus-summary-limit articles))
6418       (gnus-summary-position-point))))
6419
6420 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6421 (make-obsolete
6422  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6423
6424 (defun gnus-summary-limit-to-unread (&optional all)
6425   "Limit the summary buffer to articles that are not marked as read.
6426 If ALL is non-nil, limit strictly to unread articles."
6427   (interactive "P")
6428   (if all
6429       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6430     (gnus-summary-limit-to-marks
6431      ;; Concat all the marks that say that an article is read and have
6432      ;; those removed.
6433      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6434            gnus-killed-mark gnus-kill-file-mark
6435            gnus-low-score-mark gnus-expirable-mark
6436            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6437            gnus-duplicate-mark gnus-souped-mark)
6438      'reverse)))
6439
6440 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6441 (make-obsolete 'gnus-summary-delete-marked-with
6442                'gnus-summary-limit-exlude-marks)
6443
6444 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6445   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6446 If REVERSE, limit the summary buffer to articles that are marked
6447 with MARKS.  MARKS can either be a string of marks or a list of marks.
6448 Returns how many articles were removed."
6449   (interactive "sMarks: ")
6450   (gnus-summary-limit-to-marks marks t))
6451
6452 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6453   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6454 If REVERSE (the prefix), limit the summary buffer to articles that are
6455 not marked with MARKS.  MARKS can either be a string of marks or a
6456 list of marks.
6457 Returns how many articles were removed."
6458   (interactive "sMarks: \nP")
6459   (prog1
6460       (let ((data gnus-newsgroup-data)
6461             (marks (if (listp marks) marks
6462                      (append marks nil))) ; Transform to list.
6463             articles)
6464         (while data
6465           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6466                   (memq (gnus-data-mark (car data)) marks))
6467             (push (gnus-data-number (car data)) articles))
6468           (setq data (cdr data)))
6469         (gnus-summary-limit articles))
6470     (gnus-summary-position-point)))
6471
6472 (defun gnus-summary-limit-to-score (&optional score)
6473   "Limit to articles with score at or above SCORE."
6474   (interactive "P")
6475   (setq score (if score
6476                   (prefix-numeric-value score)
6477                 (or gnus-summary-default-score 0)))
6478   (let ((data gnus-newsgroup-data)
6479         articles)
6480     (while data
6481       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6482                 score)
6483         (push (gnus-data-number (car data)) articles))
6484       (setq data (cdr data)))
6485     (prog1
6486         (gnus-summary-limit articles)
6487       (gnus-summary-position-point))))
6488
6489 (defun gnus-summary-limit-include-thread (id)
6490   "Display all the hidden articles that in the current thread."
6491   (interactive (list (mail-header-id (gnus-summary-article-header))))
6492   (let ((articles (gnus-articles-in-thread
6493                    (gnus-id-to-thread (gnus-root-id id)))))
6494     (prog1
6495         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6496       (gnus-summary-position-point))))
6497
6498 (defun gnus-summary-limit-include-dormant ()
6499   "Display all the hidden articles that are marked as dormant.
6500 Note that this command only works on a subset of the articles currently
6501 fetched for this group."
6502   (interactive)
6503   (unless gnus-newsgroup-dormant
6504     (error "There are no dormant articles in this group"))
6505   (prog1
6506       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6507     (gnus-summary-position-point)))
6508
6509 (defun gnus-summary-limit-exclude-dormant ()
6510   "Hide all dormant articles."
6511   (interactive)
6512   (prog1
6513       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6514     (gnus-summary-position-point)))
6515
6516 (defun gnus-summary-limit-exclude-childless-dormant ()
6517   "Hide all dormant articles that have no children."
6518   (interactive)
6519   (let ((data (gnus-data-list t))
6520         articles d children)
6521     ;; Find all articles that are either not dormant or have
6522     ;; children.
6523     (while (setq d (pop data))
6524       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6525                 (and (setq children
6526                            (gnus-article-children (gnus-data-number d)))
6527                      (let (found)
6528                        (while children
6529                          (when (memq (car children) articles)
6530                            (setq children nil
6531                                  found t))
6532                          (pop children))
6533                        found)))
6534         (push (gnus-data-number d) articles)))
6535     ;; Do the limiting.
6536     (prog1
6537         (gnus-summary-limit articles)
6538       (gnus-summary-position-point))))
6539
6540 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6541   "Mark all unread excluded articles as read.
6542 If ALL, mark even excluded ticked and dormants as read."
6543   (interactive "P")
6544   (let ((articles (gnus-sorted-complement
6545                    (sort
6546                     (mapcar (lambda (h) (mail-header-number h))
6547                             gnus-newsgroup-headers)
6548                     '<)
6549                    (sort gnus-newsgroup-limit '<)))
6550         article)
6551     (setq gnus-newsgroup-unreads
6552           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6553     (if all
6554         (setq gnus-newsgroup-dormant nil
6555               gnus-newsgroup-marked nil
6556               gnus-newsgroup-reads
6557               (nconc
6558                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6559                gnus-newsgroup-reads))
6560       (while (setq article (pop articles))
6561         (unless (or (memq article gnus-newsgroup-dormant)
6562                     (memq article gnus-newsgroup-marked))
6563           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6564
6565 (defun gnus-summary-limit (articles &optional pop)
6566   (if pop
6567       ;; We pop the previous limit off the stack and use that.
6568       (setq articles (car gnus-newsgroup-limits)
6569             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6570     ;; We use the new limit, so we push the old limit on the stack.
6571     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6572   ;; Set the limit.
6573   (setq gnus-newsgroup-limit articles)
6574   (let ((total (length gnus-newsgroup-data))
6575         (data (gnus-data-find-list (gnus-summary-article-number)))
6576         (gnus-summary-mark-below nil)   ; Inhibit this.
6577         found)
6578     ;; This will do all the work of generating the new summary buffer
6579     ;; according to the new limit.
6580     (gnus-summary-prepare)
6581     ;; Hide any threads, possibly.
6582     (and gnus-show-threads
6583          gnus-thread-hide-subtree
6584          (gnus-summary-hide-all-threads))
6585     ;; Try to return to the article you were at, or one in the
6586     ;; neighborhood.
6587     (when data
6588       ;; We try to find some article after the current one.
6589       (while data
6590         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6591           (setq data nil
6592                 found t))
6593         (setq data (cdr data))))
6594     (unless found
6595       ;; If there is no data, that means that we were after the last
6596       ;; article.  The same goes when we can't find any articles
6597       ;; after the current one.
6598       (goto-char (point-max))
6599       (gnus-summary-find-prev))
6600     (gnus-set-mode-line 'summary)
6601     ;; We return how many articles were removed from the summary
6602     ;; buffer as a result of the new limit.
6603     (- total (length gnus-newsgroup-data))))
6604
6605 (defsubst gnus-invisible-cut-children (threads)
6606   (let ((num 0))
6607     (while threads
6608       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6609         (incf num))
6610       (pop threads))
6611     (< num 2)))
6612
6613 (defsubst gnus-cut-thread (thread)
6614   "Go forwards in the thread until we find an article that we want to display."
6615   (when (or (eq gnus-fetch-old-headers 'some)
6616             (eq gnus-fetch-old-headers 'invisible)
6617             (numberp gnus-fetch-old-headers)
6618             (eq gnus-build-sparse-threads 'some)
6619             (eq gnus-build-sparse-threads 'more))
6620     ;; Deal with old-fetched headers and sparse threads.
6621     (while (and
6622             thread
6623             (or
6624              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6625              (gnus-summary-article-ancient-p
6626               (mail-header-number (car thread))))
6627             (if (or (<= (length (cdr thread)) 1)
6628                     (eq gnus-fetch-old-headers 'invisible))
6629                 (setq gnus-newsgroup-limit
6630                       (delq (mail-header-number (car thread))
6631                             gnus-newsgroup-limit)
6632                       thread (cadr thread))
6633               (when (gnus-invisible-cut-children (cdr thread))
6634                 (let ((th (cdr thread)))
6635                   (while th
6636                     (if (memq (mail-header-number (caar th))
6637                               gnus-newsgroup-limit)
6638                         (setq thread (car th)
6639                               th nil)
6640                       (setq th (cdr th))))))))))
6641   thread)
6642
6643 (defun gnus-cut-threads (threads)
6644   "Cut off all uninteresting articles from the beginning of threads."
6645   (when (or (eq gnus-fetch-old-headers 'some)
6646             (eq gnus-fetch-old-headers 'invisible)
6647             (numberp gnus-fetch-old-headers)
6648             (eq gnus-build-sparse-threads 'some)
6649             (eq gnus-build-sparse-threads 'more))
6650     (let ((th threads))
6651       (while th
6652         (setcar th (gnus-cut-thread (car th)))
6653         (setq th (cdr th)))))
6654   ;; Remove nixed out threads.
6655   (delq nil threads))
6656
6657 (defun gnus-summary-initial-limit (&optional show-if-empty)
6658   "Figure out what the initial limit is supposed to be on group entry.
6659 This entails weeding out unwanted dormants, low-scored articles,
6660 fetch-old-headers verbiage, and so on."
6661   ;; Most groups have nothing to remove.
6662   (if (or gnus-inhibit-limiting
6663           (and (null gnus-newsgroup-dormant)
6664                (not (eq gnus-fetch-old-headers 'some))
6665                (not (numberp gnus-fetch-old-headers))
6666                (not (eq gnus-fetch-old-headers 'invisible))
6667                (null gnus-summary-expunge-below)
6668                (not (eq gnus-build-sparse-threads 'some))
6669                (not (eq gnus-build-sparse-threads 'more))
6670                (null gnus-thread-expunge-below)
6671                (not gnus-use-nocem)))
6672       ()                                ; Do nothing.
6673     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6674     (setq gnus-newsgroup-limit nil)
6675     (mapatoms
6676      (lambda (node)
6677        (unless (car (symbol-value node))
6678          ;; These threads have no parents -- they are roots.
6679          (let ((nodes (cdr (symbol-value node)))
6680                thread)
6681            (while nodes
6682              (if (and gnus-thread-expunge-below
6683                       (< (gnus-thread-total-score (car nodes))
6684                          gnus-thread-expunge-below))
6685                  (gnus-expunge-thread (pop nodes))
6686                (setq thread (pop nodes))
6687                (gnus-summary-limit-children thread))))))
6688      gnus-newsgroup-dependencies)
6689     ;; If this limitation resulted in an empty group, we might
6690     ;; pop the previous limit and use it instead.
6691     (when (and (not gnus-newsgroup-limit)
6692                show-if-empty)
6693       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6694     gnus-newsgroup-limit))
6695
6696 (defun gnus-summary-limit-children (thread)
6697   "Return 1 if this subthread is visible and 0 if it is not."
6698   ;; First we get the number of visible children to this thread.  This
6699   ;; is done by recursing down the thread using this function, so this
6700   ;; will really go down to a leaf article first, before slowly
6701   ;; working its way up towards the root.
6702   (when thread
6703     (let ((children
6704            (if (cdr thread)
6705                (apply '+ (mapcar 'gnus-summary-limit-children
6706                                  (cdr thread)))
6707              0))
6708           (number (mail-header-number (car thread)))
6709           score)
6710       (if (and
6711            (not (memq number gnus-newsgroup-marked))
6712            (or
6713             ;; If this article is dormant and has absolutely no visible
6714             ;; children, then this article isn't visible.
6715             (and (memq number gnus-newsgroup-dormant)
6716                  (zerop children))
6717             ;; If this is "fetch-old-headered" and there is no
6718             ;; visible children, then we don't want this article.
6719             (and (or (eq gnus-fetch-old-headers 'some)
6720                      (numberp gnus-fetch-old-headers))
6721                  (gnus-summary-article-ancient-p number)
6722                  (zerop children))
6723             ;; If this is "fetch-old-headered" and `invisible', then
6724             ;; we don't want this article.
6725             (and (eq gnus-fetch-old-headers 'invisible)
6726                  (gnus-summary-article-ancient-p number))
6727             ;; If this is a sparsely inserted article with no children,
6728             ;; we don't want it.
6729             (and (eq gnus-build-sparse-threads 'some)
6730                  (gnus-summary-article-sparse-p number)
6731                  (zerop children))
6732             ;; If we use expunging, and this article is really
6733             ;; low-scored, then we don't want this article.
6734             (when (and gnus-summary-expunge-below
6735                        (< (setq score
6736                                 (or (cdr (assq number gnus-newsgroup-scored))
6737                                     gnus-summary-default-score))
6738                           gnus-summary-expunge-below))
6739               ;; We increase the expunge-tally here, but that has
6740               ;; nothing to do with the limits, really.
6741               (incf gnus-newsgroup-expunged-tally)
6742               ;; We also mark as read here, if that's wanted.
6743               (when (and gnus-summary-mark-below
6744                          (< score gnus-summary-mark-below))
6745                 (setq gnus-newsgroup-unreads
6746                       (delq number gnus-newsgroup-unreads))
6747                 (if gnus-newsgroup-auto-expire
6748                     (push number gnus-newsgroup-expirable)
6749                   (push (cons number gnus-low-score-mark)
6750                         gnus-newsgroup-reads)))
6751               t)
6752             ;; Check NoCeM things.
6753             (if (and gnus-use-nocem
6754                      (gnus-nocem-unwanted-article-p
6755                       (mail-header-id (car thread))))
6756                 (progn
6757                   (setq gnus-newsgroup-unreads
6758                         (delq number gnus-newsgroup-unreads))
6759                   t))))
6760           ;; Nope, invisible article.
6761           0
6762         ;; Ok, this article is to be visible, so we add it to the limit
6763         ;; and return 1.
6764         (push number gnus-newsgroup-limit)
6765         1))))
6766
6767 (defun gnus-expunge-thread (thread)
6768   "Mark all articles in THREAD as read."
6769   (let* ((number (mail-header-number (car thread))))
6770     (incf gnus-newsgroup-expunged-tally)
6771     ;; We also mark as read here, if that's wanted.
6772     (setq gnus-newsgroup-unreads
6773           (delq number gnus-newsgroup-unreads))
6774     (if gnus-newsgroup-auto-expire
6775         (push number gnus-newsgroup-expirable)
6776       (push (cons number gnus-low-score-mark)
6777             gnus-newsgroup-reads)))
6778   ;; Go recursively through all subthreads.
6779   (mapcar 'gnus-expunge-thread (cdr thread)))
6780
6781 ;; Summary article oriented commands
6782
6783 (defun gnus-summary-refer-parent-article (n)
6784   "Refer parent article N times.
6785 If N is negative, go to ancestor -N instead.
6786 The difference between N and the number of articles fetched is returned."
6787   (interactive "p")
6788   (let ((skip 1)
6789         error header ref)
6790     (when (not (natnump n))
6791       (setq skip (abs n)
6792             n 1))
6793     (while (and (> n 0)
6794                 (not error))
6795       (setq header (gnus-summary-article-header))
6796       (if (and (eq (mail-header-number header)
6797                    (cdr gnus-article-current))
6798                (equal gnus-newsgroup-name
6799                       (car gnus-article-current)))
6800           ;; If we try to find the parent of the currently
6801           ;; displayed article, then we take a look at the actual
6802           ;; References header, since this is slightly more
6803           ;; reliable than the References field we got from the
6804           ;; server.
6805           (save-excursion
6806             (set-buffer gnus-original-article-buffer)
6807             (nnheader-narrow-to-headers)
6808             (unless (setq ref (message-fetch-field "references"))
6809               (setq ref (message-fetch-field "in-reply-to")))
6810             (widen))
6811         (setq ref
6812               ;; It's not the current article, so we take a bet on
6813               ;; the value we got from the server.
6814               (mail-header-references header)))
6815       (if (and ref
6816                (not (equal ref "")))
6817           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6818             (gnus-message 1 "Couldn't find parent"))
6819         (gnus-message 1 "No references in article %d"
6820                       (gnus-summary-article-number))
6821         (setq error t))
6822       (decf n))
6823     (gnus-summary-position-point)
6824     n))
6825
6826 (defun gnus-summary-refer-references ()
6827   "Fetch all articles mentioned in the References header.
6828 Return the number of articles fetched."
6829   (interactive)
6830   (let ((ref (mail-header-references (gnus-summary-article-header)))
6831         (current (gnus-summary-article-number))
6832         (n 0))
6833     (if (or (not ref)
6834             (equal ref ""))
6835         (error "No References in the current article")
6836       ;; For each Message-ID in the References header...
6837       (while (string-match "<[^>]*>" ref)
6838         (incf n)
6839         ;; ... fetch that article.
6840         (gnus-summary-refer-article
6841          (prog1 (match-string 0 ref)
6842            (setq ref (substring ref (match-end 0))))))
6843       (gnus-summary-goto-subject current)
6844       (gnus-summary-position-point)
6845       n)))
6846
6847 (defun gnus-summary-refer-thread (&optional limit)
6848   "Fetch all articles in the current thread.
6849 If LIMIT (the numerical prefix), fetch that many old headers instead
6850 of what's specified by the `gnus-refer-thread-limit' variable."
6851   (interactive "P")
6852   (let ((id (mail-header-id (gnus-summary-article-header)))
6853         (limit (if limit (prefix-numeric-value limit)
6854                  gnus-refer-thread-limit)))
6855     ;; We want to fetch LIMIT *old* headers, but we also have to
6856     ;; re-fetch all the headers in the current buffer, because many of
6857     ;; them may be undisplayed.  So we adjust LIMIT.
6858     (when (numberp limit)
6859       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6860     (unless (eq gnus-fetch-old-headers 'invisible)
6861       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6862       ;; Retrieve the headers and read them in.
6863       (if (eq (gnus-retrieve-headers
6864                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6865               'nov)
6866           (gnus-build-all-threads)
6867         (error "Can't fetch thread from backends that don't support NOV"))
6868       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6869     (gnus-summary-limit-include-thread id)))
6870
6871 (defun gnus-summary-refer-article (message-id)
6872   "Fetch an article specified by MESSAGE-ID."
6873   (interactive "sMessage-ID: ")
6874   (when (and (stringp message-id)
6875              (not (zerop (length message-id))))
6876     ;; Construct the correct Message-ID if necessary.
6877     ;; Suggested by tale@pawl.rpi.edu.
6878     (unless (string-match "^<" message-id)
6879       (setq message-id (concat "<" message-id)))
6880     (unless (string-match ">$" message-id)
6881       (setq message-id (concat message-id ">")))
6882     (let* ((header (gnus-id-to-header message-id))
6883            (sparse (and header
6884                         (gnus-summary-article-sparse-p
6885                          (mail-header-number header))
6886                         (memq (mail-header-number header)
6887                               gnus-newsgroup-limit)))
6888            number)
6889       (cond
6890        ;; If the article is present in the buffer we just go to it.
6891        ((and header
6892              (or (not (gnus-summary-article-sparse-p
6893                        (mail-header-number header)))
6894                  sparse))
6895         (prog1
6896             (gnus-summary-goto-article
6897              (mail-header-number header) nil t)
6898           (when sparse
6899             (gnus-summary-update-article (mail-header-number header)))))
6900        (t
6901         ;; We fetch the article.
6902         (catch 'found
6903           (dolist (gnus-override-method (gnus-refer-article-methods))
6904             (gnus-check-server gnus-override-method)
6905             ;; Fetch the header, and display the article.
6906             (when (setq number (gnus-summary-insert-subject message-id))
6907               (gnus-summary-select-article nil nil nil number)
6908               (throw 'found t)))
6909           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6910
6911 (defun gnus-refer-article-methods ()
6912   "Return a list of referrable methods."
6913   (cond
6914    ;; No method, so we default to current and native.
6915    ((null gnus-refer-article-method)
6916     (list gnus-current-select-method gnus-select-method))
6917    ;; Current.
6918    ((eq 'current gnus-refer-article-method)
6919     (list gnus-current-select-method))
6920    ;; List of select methods.
6921    ((not (stringp (cadr gnus-refer-article-method)))
6922     (let (out)
6923       (dolist (method gnus-refer-article-method)
6924         (push (if (eq 'current method)
6925                   gnus-current-select-method
6926                 method)
6927               out))
6928       (nreverse out)))
6929    ;; One single select method.
6930    (t
6931     (list gnus-refer-article-method))))
6932
6933 (defun gnus-summary-edit-parameters ()
6934   "Edit the group parameters of the current group."
6935   (interactive)
6936   (gnus-group-edit-group gnus-newsgroup-name 'params))
6937
6938 (defun gnus-summary-customize-parameters ()
6939   "Customize the group parameters of the current group."
6940   (interactive)
6941   (gnus-group-customize gnus-newsgroup-name))
6942
6943 (defun gnus-summary-enter-digest-group (&optional force)
6944   "Enter an nndoc group based on the current article.
6945 If FORCE, force a digest interpretation.  If not, try
6946 to guess what the document format is."
6947   (interactive "P")
6948   (let ((conf gnus-current-window-configuration))
6949     (save-excursion
6950       (gnus-summary-select-article))
6951     (setq gnus-current-window-configuration conf)
6952     (let* ((name (format "%s-%d"
6953                          (gnus-group-prefixed-name
6954                           gnus-newsgroup-name (list 'nndoc ""))
6955                          (save-excursion
6956                            (set-buffer gnus-summary-buffer)
6957                            gnus-current-article)))
6958            (ogroup gnus-newsgroup-name)
6959            (params (append (gnus-info-params (gnus-get-info ogroup))
6960                            (list (cons 'to-group ogroup))
6961                            (list (cons 'save-article-group ogroup))))
6962            (case-fold-search t)
6963            (buf (current-buffer))
6964            dig to-address)
6965       (save-excursion
6966         (set-buffer gnus-original-article-buffer)
6967         ;; Have the digest group inherit the main mail address of
6968         ;; the parent article.
6969         (when (setq to-address (or (message-fetch-field "reply-to")
6970                                    (message-fetch-field "from")))
6971           (setq params (append (list (cons 'to-address to-address)))))
6972         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6973         (insert-buffer-substring gnus-original-article-buffer)
6974         ;; Remove lines that may lead nndoc to misinterpret the
6975         ;; document type.
6976         (narrow-to-region
6977          (goto-char (point-min))
6978          (or (search-forward "\n\n" nil t) (point)))
6979         (goto-char (point-min))
6980         (delete-matching-lines "^Path:\\|^From ")
6981         (widen))
6982       (unwind-protect
6983           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6984                     (gnus-newsgroup-ephemeral-ignored-charsets
6985                      gnus-newsgroup-ignored-charsets))
6986                 (gnus-group-read-ephemeral-group
6987                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6988                               (nndoc-article-type
6989                                ,(if force 'mbox 'guess))) t))
6990               ;; Make all postings to this group go to the parent group.
6991               (nconc (gnus-info-params (gnus-get-info name))
6992                      params)
6993             ;; Couldn't select this doc group.
6994             (switch-to-buffer buf)
6995             (gnus-set-global-variables)
6996             (gnus-configure-windows 'summary)
6997             (gnus-message 3 "Article couldn't be entered?"))
6998         (kill-buffer dig)))))
6999
7000 (defun gnus-summary-read-document (n)
7001   "Open a new group based on the current article(s).
7002 This will allow you to read digests and other similar
7003 documents as newsgroups.
7004 Obeys the standard process/prefix convention."
7005   (interactive "P")
7006   (let* ((articles (gnus-summary-work-articles n))
7007          (ogroup gnus-newsgroup-name)
7008          (params (append (gnus-info-params (gnus-get-info ogroup))
7009                          (list (cons 'to-group ogroup))))
7010          article group egroup groups vgroup)
7011     (while (setq article (pop articles))
7012       (setq group (format "%s-%d" gnus-newsgroup-name article))
7013       (gnus-summary-remove-process-mark article)
7014       (when (gnus-summary-display-article article)
7015         (save-excursion
7016           (with-temp-buffer
7017             (insert-buffer-substring gnus-original-article-buffer)
7018             ;; Remove some headers that may lead nndoc to make
7019             ;; the wrong guess.
7020             (message-narrow-to-head)
7021             (goto-char (point-min))
7022             (delete-matching-lines "^\\(Path\\):\\|^From ")
7023             (widen)
7024             (if (setq egroup
7025                       (gnus-group-read-ephemeral-group
7026                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7027                                      (nndoc-article-type guess))
7028                        t nil t))
7029                 (progn
7030                   ;; Make all postings to this group go to the parent group.
7031                   (nconc (gnus-info-params (gnus-get-info egroup))
7032                          params)
7033                   (push egroup groups))
7034               ;; Couldn't select this doc group.
7035               (gnus-error 3 "Article couldn't be entered"))))))
7036     ;; Now we have selected all the documents.
7037     (cond
7038      ((not groups)
7039       (error "None of the articles could be interpreted as documents"))
7040      ((gnus-group-read-ephemeral-group
7041        (setq vgroup (format
7042                      "nnvirtual:%s-%s" gnus-newsgroup-name
7043                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7044        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7045        t
7046        (cons (current-buffer) 'summary)))
7047      (t
7048       (error "Couldn't select virtual nndoc group")))))
7049
7050 (defun gnus-summary-isearch-article (&optional regexp-p)
7051   "Do incremental search forward on the current article.
7052 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7053   (interactive "P")
7054   (gnus-summary-select-article)
7055   (gnus-configure-windows 'article)
7056   (gnus-eval-in-buffer-window gnus-article-buffer
7057     (save-restriction
7058       (widen)
7059       (isearch-forward regexp-p))))
7060
7061 (defun gnus-summary-search-article-forward (regexp &optional backward)
7062   "Search for an article containing REGEXP forward.
7063 If BACKWARD, search backward instead."
7064   (interactive
7065    (list (read-string
7066           (format "Search article %s (regexp%s): "
7067                   (if current-prefix-arg "backward" "forward")
7068                   (if gnus-last-search-regexp
7069                       (concat ", default " gnus-last-search-regexp)
7070                     "")))
7071          current-prefix-arg))
7072   (if (string-equal regexp "")
7073       (setq regexp (or gnus-last-search-regexp ""))
7074     (setq gnus-last-search-regexp regexp))
7075   (if (gnus-summary-search-article regexp backward)
7076       (gnus-summary-show-thread)
7077     (error "Search failed: \"%s\"" regexp)))
7078
7079 (defun gnus-summary-search-article-backward (regexp)
7080   "Search for an article containing REGEXP backward."
7081   (interactive
7082    (list (read-string
7083           (format "Search article backward (regexp%s): "
7084                   (if gnus-last-search-regexp
7085                       (concat ", default " gnus-last-search-regexp)
7086                     "")))))
7087   (gnus-summary-search-article-forward regexp 'backward))
7088
7089 (defun gnus-summary-search-article (regexp &optional backward)
7090   "Search for an article containing REGEXP.
7091 Optional argument BACKWARD means do search for backward.
7092 `gnus-select-article-hook' is not called during the search."
7093   ;; We have to require this here to make sure that the following
7094   ;; dynamic binding isn't shadowed by autoloading.
7095   (require 'gnus-async)
7096   (require 'gnus-art)
7097   (let ((gnus-select-article-hook nil)  ;Disable hook.
7098         (gnus-article-prepare-hook nil)
7099         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7100         (gnus-use-article-prefetch nil)
7101         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7102         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7103         (sum (current-buffer))
7104         (gnus-display-mime-function nil)
7105         (found nil)
7106         point)
7107     (gnus-save-hidden-threads
7108       (gnus-summary-select-article)
7109       (set-buffer gnus-article-buffer)
7110       (goto-char (window-point (get-buffer-window (current-buffer))))
7111       (when backward
7112         (forward-line -1))
7113       (while (not found)
7114         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7115         (if (if backward
7116                 (re-search-backward regexp nil t)
7117               (re-search-forward regexp nil t))
7118             ;; We found the regexp.
7119             (progn
7120               (setq found 'found)
7121               (beginning-of-line)
7122               (set-window-start
7123                (get-buffer-window (current-buffer))
7124                (point))
7125               (forward-line 1)
7126               (set-window-point
7127                (get-buffer-window (current-buffer))
7128                (point))
7129               (set-buffer sum)
7130               (setq point (point)))
7131           ;; We didn't find it, so we go to the next article.
7132           (set-buffer sum)
7133           (setq found 'not)
7134           (while (eq found 'not)
7135             (if (not (if backward (gnus-summary-find-prev)
7136                        (gnus-summary-find-next)))
7137                 ;; No more articles.
7138                 (setq found t)
7139               ;; Select the next article and adjust point.
7140               (unless (gnus-summary-article-sparse-p
7141                        (gnus-summary-article-number))
7142                 (setq found nil)
7143                 (gnus-summary-select-article)
7144                 (set-buffer gnus-article-buffer)
7145                 (widen)
7146                 (goto-char (if backward (point-max) (point-min))))))))
7147       (gnus-message 7 ""))
7148     ;; Return whether we found the regexp.
7149     (when (eq found 'found)
7150       (goto-char point)
7151       (gnus-summary-show-thread)
7152       (gnus-summary-goto-subject gnus-current-article)
7153       (gnus-summary-position-point)
7154       t)))
7155
7156 (defun gnus-summary-find-matching (header regexp &optional backward unread
7157                                           not-case-fold)
7158   "Return a list of all articles that match REGEXP on HEADER.
7159 The search stars on the current article and goes forwards unless
7160 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7161 If UNREAD is non-nil, only unread articles will
7162 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7163 in the comparisons."
7164   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7165                 (gnus-data-find-list
7166                  (gnus-summary-article-number) (gnus-data-list backward))))
7167         (case-fold-search (not not-case-fold))
7168         articles d func)
7169     (if (consp header)
7170         (if (eq (car header) 'extra)
7171             (setq func
7172                   `(lambda (h)
7173                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7174                          "")))
7175           (error "%s is an invalid header" header))
7176       (unless (fboundp (intern (concat "mail-header-" header)))
7177         (error "%s is not a valid header" header))
7178       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7179     (while data
7180       (setq d (car data))
7181       (and (or (not unread)             ; We want all articles...
7182                (gnus-data-unread-p d))  ; Or just unreads.
7183            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7184            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7185            (push (gnus-data-number d) articles)) ; Success!
7186       (setq data (cdr data)))
7187     (nreverse articles)))
7188
7189 (defun gnus-summary-execute-command (header regexp command &optional backward)
7190   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7191 If HEADER is an empty string (or nil), the match is done on the entire
7192 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7193   (interactive
7194    (list (let ((completion-ignore-case t))
7195            (completing-read
7196             "Header name: "
7197             (mapcar (lambda (string) (list string))
7198                     '("Number" "Subject" "From" "Lines" "Date"
7199                       "Message-ID" "Xref" "References" "Body"))
7200             nil 'require-match))
7201          (read-string "Regexp: ")
7202          (read-key-sequence "Command: ")
7203          current-prefix-arg))
7204   (when (equal header "Body")
7205     (setq header ""))
7206   ;; Hidden thread subtrees must be searched as well.
7207   (gnus-summary-show-all-threads)
7208   ;; We don't want to change current point nor window configuration.
7209   (save-excursion
7210     (save-window-excursion
7211       (gnus-message 6 "Executing %s..." (key-description command))
7212       ;; We'd like to execute COMMAND interactively so as to give arguments.
7213       (gnus-execute header regexp
7214                     `(call-interactively ',(key-binding command))
7215                     backward)
7216       (gnus-message 6 "Executing %s...done" (key-description command)))))
7217
7218 (defun gnus-summary-beginning-of-article ()
7219   "Scroll the article back to the beginning."
7220   (interactive)
7221   (gnus-summary-select-article)
7222   (gnus-configure-windows 'article)
7223   (gnus-eval-in-buffer-window gnus-article-buffer
7224     (widen)
7225     (goto-char (point-min))
7226     (when gnus-page-broken
7227       (gnus-narrow-to-page))))
7228
7229 (defun gnus-summary-end-of-article ()
7230   "Scroll to the end of the article."
7231   (interactive)
7232   (gnus-summary-select-article)
7233   (gnus-configure-windows 'article)
7234   (gnus-eval-in-buffer-window gnus-article-buffer
7235     (widen)
7236     (goto-char (point-max))
7237     (recenter -3)
7238     (when gnus-page-broken
7239       (gnus-narrow-to-page))))
7240
7241 (defun gnus-summary-print-article (&optional filename n)
7242   "Generate and print a PostScript image of the N next (mail) articles.
7243
7244 If N is negative, print the N previous articles.  If N is nil and articles
7245 have been marked with the process mark, print these instead.
7246
7247 If the optional first argument FILENAME is nil, send the image to the
7248 printer.  If FILENAME is a string, save the PostScript image in a file with
7249 that name.  If FILENAME is a number, prompt the user for the name of the file
7250 to save in."
7251   (interactive (list (ps-print-preprint current-prefix-arg)
7252                      current-prefix-arg))
7253   (dolist (article (gnus-summary-work-articles n))
7254     (gnus-summary-select-article nil nil 'pseudo article)
7255     (gnus-eval-in-buffer-window gnus-article-buffer
7256       (let ((buffer (generate-new-buffer " *print*")))
7257         (unwind-protect
7258             (progn
7259               (copy-to-buffer buffer (point-min) (point-max))
7260               (set-buffer buffer)
7261               (gnus-article-delete-invisible-text)
7262               (let ((ps-left-header
7263                      (list
7264                       (concat "("
7265                               (mail-header-subject gnus-current-headers) ")")
7266                       (concat "("
7267                               (mail-header-from gnus-current-headers) ")")))
7268                     (ps-right-header
7269                      (list
7270                       "/pagenumberstring load"
7271                       (concat "("
7272                               (mail-header-date gnus-current-headers) ")"))))
7273                 (gnus-run-hooks 'gnus-ps-print-hook)
7274                 (save-excursion
7275                   (ps-print-buffer-with-faces filename))))
7276           (kill-buffer buffer))))))
7277
7278 (defun gnus-summary-show-article (&optional arg)
7279   "Force re-fetching of the current article.
7280 If ARG (the prefix) is a number, show the article with the charset 
7281 defined in `gnus-summary-show-article-charset-alist', or the charset
7282 inputed.
7283 If ARG (the prefix) is non-nil and not a number, show the raw article 
7284 without any article massaging functions being run."
7285   (interactive "P")
7286   (cond 
7287    ((numberp arg)
7288     (let ((gnus-newsgroup-charset 
7289            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7290                (read-coding-system "Charset: ")))
7291           (gnus-newsgroup-ignored-charsets 'gnus-all))
7292       (gnus-summary-select-article nil 'force)))
7293    ((not arg)
7294     ;; Select the article the normal way.
7295     (gnus-summary-select-article nil 'force))
7296    (t
7297     ;; We have to require this here to make sure that the following
7298     ;; dynamic binding isn't shadowed by autoloading.
7299     (require 'gnus-async)
7300     (require 'gnus-art)
7301     ;; Bind the article treatment functions to nil.
7302     (let ((gnus-have-all-headers t)
7303           gnus-article-prepare-hook
7304           gnus-article-decode-hook
7305           gnus-display-mime-function
7306           gnus-break-pages)
7307       ;; Destroy any MIME parts.
7308       (when (gnus-buffer-live-p gnus-article-buffer)
7309         (save-excursion
7310           (set-buffer gnus-article-buffer)
7311           (mm-destroy-parts gnus-article-mime-handles)))
7312       (gnus-summary-select-article nil 'force))))
7313   (gnus-summary-goto-subject gnus-current-article)
7314   (gnus-summary-position-point))
7315
7316 (defun gnus-summary-verbose-headers (&optional arg)
7317   "Toggle permanent full header display.
7318 If ARG is a positive number, turn header display on.
7319 If ARG is a negative number, turn header display off."
7320   (interactive "P")
7321   (setq gnus-show-all-headers
7322         (cond ((or (not (numberp arg))
7323                    (zerop arg))
7324                (not gnus-show-all-headers))
7325               ((natnump arg)
7326                t)))
7327   (gnus-summary-show-article))
7328
7329 (defun gnus-summary-toggle-header (&optional arg)
7330   "Show the headers if they are hidden, or hide them if they are shown.
7331 If ARG is a positive number, show the entire header.
7332 If ARG is a negative number, hide the unwanted header lines."
7333   (interactive "P")
7334   (save-excursion
7335     (set-buffer gnus-article-buffer)
7336     (save-restriction
7337       (let* ((buffer-read-only nil)
7338              (inhibit-point-motion-hooks t)
7339              hidden e)
7340         (setq hidden
7341               (if (numberp arg)
7342                   (>= arg 0)
7343                 (save-restriction 
7344                   (article-narrow-to-head)
7345                   (gnus-article-hidden-text-p 'headers))))
7346         (goto-char (point-min))
7347         (when (search-forward "\n\n" nil t)
7348           (delete-region (point-min) (1- (point))))
7349         (goto-char (point-min))
7350         (save-excursion
7351           (set-buffer gnus-original-article-buffer)
7352           (goto-char (point-min))
7353           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7354         (insert-buffer-substring gnus-original-article-buffer 1 e)
7355         (save-restriction
7356           (narrow-to-region (point-min) (point))
7357           (article-decode-encoded-words)
7358           (if  hidden
7359               (let ((gnus-treat-hide-headers nil)
7360                     (gnus-treat-hide-boring-headers nil))
7361                 (setq gnus-article-wash-types
7362                       (delq 'headers gnus-article-wash-types))
7363                 (gnus-treat-article 'head))
7364             (gnus-treat-article 'head)))
7365         (gnus-set-mode-line 'article)))))
7366
7367 (defun gnus-summary-show-all-headers ()
7368   "Make all header lines visible."
7369   (interactive)
7370   (gnus-article-show-all-headers))
7371
7372 (defun gnus-summary-caesar-message (&optional arg)
7373   "Caesar rotate the current article by 13.
7374 The numerical prefix specifies how many places to rotate each letter
7375 forward."
7376   (interactive "P")
7377   (gnus-summary-select-article)
7378   (let ((mail-header-separator ""))
7379     (gnus-eval-in-buffer-window gnus-article-buffer
7380       (save-restriction
7381         (widen)
7382         (let ((start (window-start))
7383               buffer-read-only)
7384           (message-caesar-buffer-body arg)
7385           (set-window-start (get-buffer-window (current-buffer)) start))))))
7386
7387 (defun gnus-summary-stop-page-breaking ()
7388   "Stop page breaking in the current article."
7389   (interactive)
7390   (gnus-summary-select-article)
7391   (gnus-eval-in-buffer-window gnus-article-buffer
7392     (widen)
7393     (when (gnus-visual-p 'page-marker)
7394       (let ((buffer-read-only nil))
7395         (gnus-remove-text-with-property 'gnus-prev)
7396         (gnus-remove-text-with-property 'gnus-next))
7397       (setq gnus-page-broken nil))))
7398
7399 (defun gnus-summary-move-article (&optional n to-newsgroup
7400                                             select-method action)
7401   "Move the current article to a different newsgroup.
7402 If N is a positive number, move the N next articles.
7403 If N is a negative number, move the N previous articles.
7404 If N is nil and any articles have been marked with the process mark,
7405 move those articles instead.
7406 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7407 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7408 re-spool using this method.
7409
7410 For this function to work, both the current newsgroup and the
7411 newsgroup that you want to move to have to support the `request-move'
7412 and `request-accept' functions.
7413
7414 ACTION can be either `move' (the default), `crosspost' or `copy'."
7415   (interactive "P")
7416   (unless action
7417     (setq action 'move))
7418   ;; Disable marking as read.
7419   (let (gnus-mark-article-hook)
7420     (save-window-excursion
7421       (gnus-summary-select-article)))
7422   ;; Check whether the source group supports the required functions.
7423   (cond ((and (eq action 'move)
7424               (not (gnus-check-backend-function
7425                     'request-move-article gnus-newsgroup-name)))
7426          (error "The current group does not support article moving"))
7427         ((and (eq action 'crosspost)
7428               (not (gnus-check-backend-function
7429                     'request-replace-article gnus-newsgroup-name)))
7430          (error "The current group does not support article editing")))
7431   (let ((articles (gnus-summary-work-articles n))
7432         (prefix (if (gnus-check-backend-function
7433                     'request-move-article gnus-newsgroup-name)
7434                     (gnus-group-real-prefix gnus-newsgroup-name)
7435                   ""))
7436         (names '((move "Move" "Moving")
7437                  (copy "Copy" "Copying")
7438                  (crosspost "Crosspost" "Crossposting")))
7439         (copy-buf (save-excursion
7440                     (nnheader-set-temp-buffer " *copy article*")))
7441         art-group to-method new-xref article to-groups)
7442     (unless (assq action names)
7443       (error "Unknown action %s" action))
7444     ;; Read the newsgroup name.
7445     (when (and (not to-newsgroup)
7446                (not select-method))
7447       (setq to-newsgroup
7448             (gnus-read-move-group-name
7449              (cadr (assq action names))
7450              (symbol-value (intern (format "gnus-current-%s-group" action)))
7451              articles prefix))
7452       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7453     (setq to-method (or select-method
7454                         (gnus-server-to-method
7455                          (gnus-group-method to-newsgroup))))
7456     ;; Check the method we are to move this article to...
7457     (unless (gnus-check-backend-function
7458              'request-accept-article (car to-method))
7459       (error "%s does not support article copying" (car to-method)))
7460     (unless (gnus-check-server to-method)
7461       (error "Can't open server %s" (car to-method)))
7462     (gnus-message 6 "%s to %s: %s..."
7463                   (caddr (assq action names))
7464                   (or (car select-method) to-newsgroup) articles)
7465     (while articles
7466       (setq article (pop articles))
7467       (setq
7468        art-group
7469        (cond
7470         ;; Move the article.
7471         ((eq action 'move)
7472          ;; Remove this article from future suppression.
7473          (gnus-dup-unsuppress-article article)
7474          (gnus-request-move-article
7475           article                       ; Article to move
7476           gnus-newsgroup-name           ; From newsgroup
7477           (nth 1 (gnus-find-method-for-group
7478                   gnus-newsgroup-name)) ; Server
7479           (list 'gnus-request-accept-article
7480                 to-newsgroup (list 'quote select-method)
7481                 (not articles) t)       ; Accept form
7482           (not articles)))              ; Only save nov last time
7483         ;; Copy the article.
7484         ((eq action 'copy)
7485          (save-excursion
7486            (set-buffer copy-buf)
7487            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7488              (gnus-request-accept-article
7489               to-newsgroup select-method (not articles) t))))
7490         ;; Crosspost the article.
7491         ((eq action 'crosspost)
7492          (let ((xref (message-tokenize-header
7493                       (mail-header-xref (gnus-summary-article-header article))
7494                       " ")))
7495            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7496                                   ":" article))
7497            (unless xref
7498              (setq xref (list (system-name))))
7499            (setq new-xref
7500                  (concat
7501                   (mapconcat 'identity
7502                              (delete "Xref:" (delete new-xref xref))
7503                              " ")
7504                   " " new-xref))
7505            (save-excursion
7506              (set-buffer copy-buf)
7507              ;; First put the article in the destination group.
7508              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7509              (when (consp (setq art-group
7510                                 (gnus-request-accept-article
7511                                  to-newsgroup select-method (not articles))))
7512                (setq new-xref (concat new-xref " " (car art-group)
7513                                       ":" (cdr art-group)))
7514                ;; Now we have the new Xrefs header, so we insert
7515                ;; it and replace the new article.
7516                (nnheader-replace-header "Xref" new-xref)
7517                (gnus-request-replace-article
7518                 (cdr art-group) to-newsgroup (current-buffer))
7519                art-group))))))
7520       (cond
7521        ((not art-group)
7522         (gnus-message 1 "Couldn't %s article %s: %s"
7523                       (cadr (assq action names)) article
7524                       (nnheader-get-report (car to-method))))
7525        ((eq art-group 'junk)
7526         (when (eq action 'move)
7527           (gnus-summary-mark-article article gnus-canceled-mark)
7528           (gnus-message 4 "Deleted article %s" article)))
7529        (t
7530         (let* ((pto-group (gnus-group-prefixed-name
7531                            (car art-group) to-method))
7532                (entry
7533                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7534                (info (nth 2 entry))
7535                (to-group (gnus-info-group info))
7536                to-marks)
7537           ;; Update the group that has been moved to.
7538           (when (and info
7539                      (memq action '(move copy)))
7540             (unless (member to-group to-groups)
7541               (push to-group to-groups))
7542
7543             (unless (memq article gnus-newsgroup-unreads)
7544               (push 'read to-marks)
7545               (gnus-info-set-read
7546                info (gnus-add-to-range (gnus-info-read info)
7547                                        (list (cdr art-group)))))
7548
7549             ;; See whether the article is to be put in the cache.
7550             (let ((marks gnus-article-mark-lists)
7551                   (to-article (cdr art-group)))
7552
7553               ;; Enter the article into the cache in the new group,
7554               ;; if that is required.
7555               (when gnus-use-cache
7556                 (gnus-cache-possibly-enter-article
7557                  to-group to-article
7558                  (memq article gnus-newsgroup-marked)
7559                  (memq article gnus-newsgroup-dormant)
7560                  (memq article gnus-newsgroup-unreads)))
7561
7562               (when gnus-preserve-marks 
7563                 ;; Copy any marks over to the new group.
7564                 (when (and (equal to-group gnus-newsgroup-name)
7565                            (not (memq article gnus-newsgroup-unreads)))
7566                   ;; Mark this article as read in this group.
7567                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7568                   (setcdr (gnus-active to-group) to-article)
7569                   (setcdr gnus-newsgroup-active to-article))
7570
7571                 (while marks
7572                   (when (memq article (symbol-value
7573                                        (intern (format "gnus-newsgroup-%s"
7574                                                        (caar marks)))))
7575                     (push (cdar marks) to-marks)
7576                     ;; If the other group is the same as this group,
7577                     ;; then we have to add the mark to the list.
7578                     (when (equal to-group gnus-newsgroup-name)
7579                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7580                            (cons to-article
7581                                  (symbol-value
7582                                   (intern (format "gnus-newsgroup-%s"
7583                                                   (caar marks)))))))
7584                     ;; Copy the marks to other group.
7585                     (gnus-add-marked-articles
7586                      to-group (cdar marks) (list to-article) info))
7587                   (setq marks (cdr marks)))
7588
7589                 (gnus-request-set-mark to-group (list (list (list to-article)
7590                                                             'set
7591                                                             to-marks))))
7592
7593               (gnus-dribble-enter
7594                (concat "(gnus-group-set-info '"
7595                        (gnus-prin1-to-string (gnus-get-info to-group))
7596                        ")"))))
7597
7598           ;; Update the Xref header in this article to point to
7599           ;; the new crossposted article we have just created.
7600           (when (eq action 'crosspost)
7601             (save-excursion
7602               (set-buffer copy-buf)
7603               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7604               (nnheader-replace-header "Xref" new-xref)
7605               (gnus-request-replace-article
7606                article gnus-newsgroup-name (current-buffer)))))
7607
7608         ;;;!!!Why is this necessary?
7609         (set-buffer gnus-summary-buffer)
7610
7611         (gnus-summary-goto-subject article)
7612         (when (eq action 'move)
7613           (gnus-summary-mark-article article gnus-canceled-mark))))
7614       (gnus-summary-remove-process-mark article))
7615     ;; Re-activate all groups that have been moved to.
7616     (while to-groups
7617       (save-excursion
7618         (set-buffer gnus-group-buffer)
7619         (when (gnus-group-goto-group (car to-groups) t)
7620           (gnus-group-get-new-news-this-group 1 t))
7621         (pop to-groups)))
7622
7623     (gnus-kill-buffer copy-buf)
7624     (gnus-summary-position-point)
7625     (gnus-set-mode-line 'summary)))
7626
7627 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7628   "Move the current article to a different newsgroup.
7629 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7630 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7631 re-spool using this method."
7632   (interactive "P")
7633   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7634
7635 (defun gnus-summary-crosspost-article (&optional n)
7636   "Crosspost the current article to some other group."
7637   (interactive "P")
7638   (gnus-summary-move-article n nil nil 'crosspost))
7639
7640 (defcustom gnus-summary-respool-default-method nil
7641   "Default method for respooling an article.
7642 If nil, use to the current newsgroup method."
7643   :type '(choice (gnus-select-method :value (nnml ""))
7644                  (const nil))
7645   :group 'gnus-summary-mail)
7646
7647 (defun gnus-summary-respool-article (&optional n method)
7648   "Respool the current article.
7649 The article will be squeezed through the mail spooling process again,
7650 which means that it will be put in some mail newsgroup or other
7651 depending on `nnmail-split-methods'.
7652 If N is a positive number, respool the N next articles.
7653 If N is a negative number, respool the N previous articles.
7654 If N is nil and any articles have been marked with the process mark,
7655 respool those articles instead.
7656
7657 Respooling can be done both from mail groups and \"real\" newsgroups.
7658 In the former case, the articles in question will be moved from the
7659 current group into whatever groups they are destined to.  In the
7660 latter case, they will be copied into the relevant groups."
7661   (interactive
7662    (list current-prefix-arg
7663          (let* ((methods (gnus-methods-using 'respool))
7664                 (methname
7665                  (symbol-name (or gnus-summary-respool-default-method
7666                                   (car (gnus-find-method-for-group
7667                                         gnus-newsgroup-name)))))
7668                 (method
7669                  (gnus-completing-read
7670                   methname "What backend do you want to use when respooling?"
7671                   methods nil t nil 'gnus-mail-method-history))
7672                 ms)
7673            (cond
7674             ((zerop (length (setq ms (gnus-servers-using-backend
7675                                       (intern method)))))
7676              (list (intern method) ""))
7677             ((= 1 (length ms))
7678              (car ms))
7679             (t
7680              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7681                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7682                            ms-alist))))))))
7683   (unless method
7684     (error "No method given for respooling"))
7685   (if (assoc (symbol-name
7686               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7687              (gnus-methods-using 'respool))
7688       (gnus-summary-move-article n nil method)
7689     (gnus-summary-copy-article n nil method)))
7690
7691 (defun gnus-summary-import-article (file)
7692   "Import an arbitrary file into a mail newsgroup."
7693   (interactive "fImport file: ")
7694   (let ((group gnus-newsgroup-name)
7695         (now (current-time))
7696         atts lines)
7697     (unless (gnus-check-backend-function 'request-accept-article group)
7698       (error "%s does not support article importing" group))
7699     (or (file-readable-p file)
7700         (not (file-regular-p file))
7701         (error "Can't read %s" file))
7702     (save-excursion
7703       (set-buffer (gnus-get-buffer-create " *import file*"))
7704       (erase-buffer)
7705       (insert-file-contents file)
7706       (goto-char (point-min))
7707       (unless (nnheader-article-p)
7708         ;; This doesn't look like an article, so we fudge some headers.
7709         (setq atts (file-attributes file)
7710               lines (count-lines (point-min) (point-max)))
7711         (insert "From: " (read-string "From: ") "\n"
7712                 "Subject: " (read-string "Subject: ") "\n"
7713                 "Date: " (message-make-date (nth 5 atts))
7714                 "\n"
7715                 "Message-ID: " (message-make-message-id) "\n"
7716                 "Lines: " (int-to-string lines) "\n"
7717                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7718       (gnus-request-accept-article group nil t)
7719       (kill-buffer (current-buffer)))))
7720
7721 (defun gnus-summary-article-posted-p ()
7722   "Say whether the current (mail) article is available from news as well.
7723 This will be the case if the article has both been mailed and posted."
7724   (interactive)
7725   (let ((id (mail-header-references (gnus-summary-article-header)))
7726         (gnus-override-method (car (gnus-refer-article-methods))))
7727     (if (gnus-request-head id "")
7728         (gnus-message 2 "The current message was found on %s"
7729                       gnus-override-method)
7730       (gnus-message 2 "The current message couldn't be found on %s"
7731                     gnus-override-method)
7732       nil)))
7733
7734 (defun gnus-summary-expire-articles (&optional now)
7735   "Expire all articles that are marked as expirable in the current group."
7736   (interactive)
7737   (when (gnus-check-backend-function
7738          'request-expire-articles gnus-newsgroup-name)
7739     ;; This backend supports expiry.
7740     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7741            (expirable (if total
7742                           (progn
7743                             ;; We need to update the info for
7744                             ;; this group for `gnus-list-of-read-articles'
7745                             ;; to give us the right answer.
7746                             (gnus-run-hooks 'gnus-exit-group-hook)
7747                             (gnus-summary-update-info)
7748                             (gnus-list-of-read-articles gnus-newsgroup-name))
7749                         (setq gnus-newsgroup-expirable
7750                               (sort gnus-newsgroup-expirable '<))))
7751            (expiry-wait (if now 'immediate
7752                           (gnus-group-find-parameter
7753                            gnus-newsgroup-name 'expiry-wait)))
7754            es)
7755       (when expirable
7756         ;; There are expirable articles in this group, so we run them
7757         ;; through the expiry process.
7758         (gnus-message 6 "Expiring articles...")
7759         (unless (gnus-check-group gnus-newsgroup-name)
7760           (error "Can't open server for %s" gnus-newsgroup-name))
7761         ;; The list of articles that weren't expired is returned.
7762         (save-excursion
7763           (if expiry-wait
7764               (let ((nnmail-expiry-wait-function nil)
7765                     (nnmail-expiry-wait expiry-wait))
7766                 (setq es (gnus-request-expire-articles
7767                           expirable gnus-newsgroup-name)))
7768             (setq es (gnus-request-expire-articles
7769                       expirable gnus-newsgroup-name))))
7770         (unless total
7771           (setq gnus-newsgroup-expirable es))
7772         ;; We go through the old list of expirable, and mark all
7773         ;; really expired articles as nonexistent.
7774         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7775           (let ((gnus-use-cache nil))
7776             (while expirable
7777               (unless (memq (car expirable) es)
7778                 (when (gnus-data-find (car expirable))
7779                   (gnus-summary-mark-article
7780                    (car expirable) gnus-canceled-mark)))
7781               (setq expirable (cdr expirable)))))
7782         (gnus-message 6 "Expiring articles...done")))))
7783
7784 (defun gnus-summary-expire-articles-now ()
7785   "Expunge all expirable articles in the current group.
7786 This means that *all* articles that are marked as expirable will be
7787 deleted forever, right now."
7788   (interactive)
7789   (or gnus-expert-user
7790       (gnus-yes-or-no-p
7791        "Are you really, really, really sure you want to delete all these messages? ")
7792       (error "Phew!"))
7793   (gnus-summary-expire-articles t))
7794
7795 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7796 (defun gnus-summary-delete-article (&optional n)
7797   "Delete the N next (mail) articles.
7798 This command actually deletes articles.  This is not a marking
7799 command.  The article will disappear forever from your life, never to
7800 return.
7801 If N is negative, delete backwards.
7802 If N is nil and articles have been marked with the process mark,
7803 delete these instead."
7804   (interactive "P")
7805   (unless (gnus-check-backend-function 'request-expire-articles
7806                                        gnus-newsgroup-name)
7807     (error "The current newsgroup does not support article deletion"))
7808   ;; Compute the list of articles to delete.
7809   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7810         not-deleted)
7811     (if (and gnus-novice-user
7812              (not (gnus-yes-or-no-p
7813                    (format "Do you really want to delete %s forever? "
7814                            (if (> (length articles) 1)
7815                                (format "these %s articles" (length articles))
7816                              "this article")))))
7817         ()
7818       ;; Delete the articles.
7819       (setq not-deleted (gnus-request-expire-articles
7820                          articles gnus-newsgroup-name 'force))
7821       (while articles
7822         (gnus-summary-remove-process-mark (car articles))
7823         ;; The backend might not have been able to delete the article
7824         ;; after all.
7825         (unless (memq (car articles) not-deleted)
7826           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7827         (setq articles (cdr articles)))
7828       (when not-deleted
7829         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7830     (gnus-summary-position-point)
7831     (gnus-set-mode-line 'summary)
7832     not-deleted))
7833
7834 (defun gnus-summary-edit-article (&optional force)
7835   "Edit the current article.
7836 This will have permanent effect only in mail groups.
7837 If FORCE is non-nil, allow editing of articles even in read-only
7838 groups."
7839   (interactive "P")
7840   (save-excursion
7841     (set-buffer gnus-summary-buffer)
7842     (let ((mail-parse-charset gnus-newsgroup-charset)
7843           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7844       (gnus-set-global-variables)
7845       (when (and (not force)
7846                  (gnus-group-read-only-p))
7847         (error "The current newsgroup does not support article editing"))
7848       (gnus-summary-show-article t)
7849       (gnus-article-edit-article
7850        'mime-to-mml
7851        `(lambda (no-highlight)
7852           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7853                 (mail-parse-ignored-charsets 
7854                  ',gnus-newsgroup-ignored-charsets))
7855             (mml-to-mime)
7856             (gnus-summary-edit-article-done
7857              ,(or (mail-header-references gnus-current-headers) "")
7858              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7859
7860 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7861
7862 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7863                                                  no-highlight)
7864   "Make edits to the current article permanent."
7865   (interactive)
7866   ;; Replace the article.
7867   (let ((buf (current-buffer)))
7868     (with-temp-buffer
7869       (insert-buffer-substring buf)
7870       (if (and (not read-only)
7871                (not (gnus-request-replace-article
7872                      (cdr gnus-article-current) (car gnus-article-current)
7873                      (current-buffer) t)))
7874           (error "Couldn't replace article")
7875         ;; Update the summary buffer.
7876         (if (and references
7877                  (equal (message-tokenize-header references " ")
7878                         (message-tokenize-header
7879                          (or (message-fetch-field "references") "") " ")))
7880             ;; We only have to update this line.
7881             (save-excursion
7882               (save-restriction
7883                 (message-narrow-to-head)
7884                 (let ((head (buffer-string))
7885                       header)
7886                   (with-temp-buffer
7887                     (insert (format "211 %d Article retrieved.\n"
7888                                     (cdr gnus-article-current)))
7889                     (insert head)
7890                     (insert ".\n")
7891                     (let ((nntp-server-buffer (current-buffer)))
7892                       (setq header (car (gnus-get-newsgroup-headers
7893                                          (save-excursion
7894                                            (set-buffer gnus-summary-buffer)
7895                                            gnus-newsgroup-dependencies)
7896                                          t))))
7897                     (save-excursion
7898                       (set-buffer gnus-summary-buffer)
7899                       (gnus-data-set-header
7900                        (gnus-data-find (cdr gnus-article-current))
7901                        header)
7902                       (gnus-summary-update-article-line
7903                        (cdr gnus-article-current) header))))))
7904           ;; Update threads.
7905           (set-buffer (or buffer gnus-summary-buffer))
7906           (gnus-summary-update-article (cdr gnus-article-current)))
7907         ;; Prettify the article buffer again.
7908         (unless no-highlight
7909           (save-excursion
7910             (set-buffer gnus-article-buffer)
7911             ;;;!!! Fix this -- article should be rehighlighted.
7912             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7913             (set-buffer gnus-original-article-buffer)
7914             (gnus-request-article
7915              (cdr gnus-article-current)
7916              (car gnus-article-current) (current-buffer))))
7917         ;; Prettify the summary buffer line.
7918         (when (gnus-visual-p 'summary-highlight 'highlight)
7919           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7920
7921 (defun gnus-summary-edit-wash (key)
7922   "Perform editing command KEY in the article buffer."
7923   (interactive
7924    (list
7925     (progn
7926       (message "%s" (concat (this-command-keys) "- "))
7927       (read-char))))
7928   (message "")
7929   (gnus-summary-edit-article)
7930   (execute-kbd-macro (concat (this-command-keys) key))
7931   (gnus-article-edit-done))
7932
7933 ;;; Respooling
7934
7935 (defun gnus-summary-respool-query (&optional silent trace)
7936   "Query where the respool algorithm would put this article."
7937   (interactive)
7938   (let (gnus-mark-article-hook)
7939     (gnus-summary-select-article)
7940     (save-excursion
7941       (set-buffer gnus-original-article-buffer)
7942       (save-restriction
7943         (message-narrow-to-head)
7944         (let ((groups (nnmail-article-group 'identity trace)))
7945           (unless silent
7946             (if groups
7947                 (message "This message would go to %s"
7948                          (mapconcat 'car groups ", "))
7949               (message "This message would go to no groups"))
7950             groups))))))
7951
7952 (defun gnus-summary-respool-trace ()
7953   "Trace where the respool algorithm would put this article.
7954 Display a buffer showing all fancy splitting patterns which matched."
7955   (interactive)
7956   (gnus-summary-respool-query nil t))
7957
7958 ;; Summary marking commands.
7959
7960 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7961   "Mark articles which has the same subject as read, and then select the next.
7962 If UNMARK is positive, remove any kind of mark.
7963 If UNMARK is negative, tick articles."
7964   (interactive "P")
7965   (when unmark
7966     (setq unmark (prefix-numeric-value unmark)))
7967   (let ((count
7968          (gnus-summary-mark-same-subject
7969           (gnus-summary-article-subject) unmark)))
7970     ;; Select next unread article.  If auto-select-same mode, should
7971     ;; select the first unread article.
7972     (gnus-summary-next-article t (and gnus-auto-select-same
7973                                       (gnus-summary-article-subject)))
7974     (gnus-message 7 "%d article%s marked as %s"
7975                   count (if (= count 1) " is" "s are")
7976                   (if unmark "unread" "read"))))
7977
7978 (defun gnus-summary-kill-same-subject (&optional unmark)
7979   "Mark articles which has the same subject as read.
7980 If UNMARK is positive, remove any kind of mark.
7981 If UNMARK is negative, tick articles."
7982   (interactive "P")
7983   (when unmark
7984     (setq unmark (prefix-numeric-value unmark)))
7985   (let ((count
7986          (gnus-summary-mark-same-subject
7987           (gnus-summary-article-subject) unmark)))
7988     ;; If marked as read, go to next unread subject.
7989     (when (null unmark)
7990       ;; Go to next unread subject.
7991       (gnus-summary-next-subject 1 t))
7992     (gnus-message 7 "%d articles are marked as %s"
7993                   count (if unmark "unread" "read"))))
7994
7995 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7996   "Mark articles with same SUBJECT as read, and return marked number.
7997 If optional argument UNMARK is positive, remove any kinds of marks.
7998 If optional argument UNMARK is negative, mark articles as unread instead."
7999   (let ((count 1))
8000     (save-excursion
8001       (cond
8002        ((null unmark)                   ; Mark as read.
8003         (while (and
8004                 (progn
8005                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8006                   (gnus-summary-show-thread) t)
8007                 (gnus-summary-find-subject subject))
8008           (setq count (1+ count))))
8009        ((> unmark 0)                    ; Tick.
8010         (while (and
8011                 (progn
8012                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8013                   (gnus-summary-show-thread) t)
8014                 (gnus-summary-find-subject subject))
8015           (setq count (1+ count))))
8016        (t                               ; Mark as unread.
8017         (while (and
8018                 (progn
8019                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8020                   (gnus-summary-show-thread) t)
8021                 (gnus-summary-find-subject subject))
8022           (setq count (1+ count)))))
8023       (gnus-set-mode-line 'summary)
8024       ;; Return the number of marked articles.
8025       count)))
8026
8027 (defun gnus-summary-mark-as-processable (n &optional unmark)
8028   "Set the process mark on the next N articles.
8029 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8030 the process mark instead.  The difference between N and the actual
8031 number of articles marked is returned."
8032   (interactive "p")
8033   (let ((backward (< n 0))
8034         (n (abs n)))
8035     (while (and
8036             (> n 0)
8037             (if unmark
8038                 (gnus-summary-remove-process-mark
8039                  (gnus-summary-article-number))
8040               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8041             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8042       (setq n (1- n)))
8043     (when (/= 0 n)
8044       (gnus-message 7 "No more articles"))
8045     (gnus-summary-recenter)
8046     (gnus-summary-position-point)
8047     n))
8048
8049 (defun gnus-summary-unmark-as-processable (n)
8050   "Remove the process mark from the next N articles.
8051 If N is negative, unmark backward instead.  The difference between N and
8052 the actual number of articles unmarked is returned."
8053   (interactive "p")
8054   (gnus-summary-mark-as-processable n t))
8055
8056 (defun gnus-summary-unmark-all-processable ()
8057   "Remove the process mark from all articles."
8058   (interactive)
8059   (save-excursion
8060     (while gnus-newsgroup-processable
8061       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8062   (gnus-summary-position-point))
8063
8064 (defun gnus-summary-mark-as-expirable (n)
8065   "Mark N articles forward as expirable.
8066 If N is negative, mark backward instead.  The difference between N and
8067 the actual number of articles marked is returned."
8068   (interactive "p")
8069   (gnus-summary-mark-forward n gnus-expirable-mark))
8070
8071 (defun gnus-summary-mark-article-as-replied (article)
8072   "Mark ARTICLE replied and update the summary line."
8073   (push article gnus-newsgroup-replied)
8074   (let ((buffer-read-only nil))
8075     (when (gnus-summary-goto-subject article nil t)
8076       (gnus-summary-update-secondary-mark article))))
8077
8078 (defun gnus-summary-set-bookmark (article)
8079   "Set a bookmark in current article."
8080   (interactive (list (gnus-summary-article-number)))
8081   (when (or (not (get-buffer gnus-article-buffer))
8082             (not gnus-current-article)
8083             (not gnus-article-current)
8084             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8085     (error "No current article selected"))
8086   ;; Remove old bookmark, if one exists.
8087   (let ((old (assq article gnus-newsgroup-bookmarks)))
8088     (when old
8089       (setq gnus-newsgroup-bookmarks
8090             (delq old gnus-newsgroup-bookmarks))))
8091   ;; Set the new bookmark, which is on the form
8092   ;; (article-number . line-number-in-body).
8093   (push
8094    (cons article
8095          (save-excursion
8096            (set-buffer gnus-article-buffer)
8097            (count-lines
8098             (min (point)
8099                  (save-excursion
8100                    (goto-char (point-min))
8101                    (search-forward "\n\n" nil t)
8102                    (point)))
8103             (point))))
8104    gnus-newsgroup-bookmarks)
8105   (gnus-message 6 "A bookmark has been added to the current article."))
8106
8107 (defun gnus-summary-remove-bookmark (article)
8108   "Remove the bookmark from the current article."
8109   (interactive (list (gnus-summary-article-number)))
8110   ;; Remove old bookmark, if one exists.
8111   (let ((old (assq article gnus-newsgroup-bookmarks)))
8112     (if old
8113         (progn
8114           (setq gnus-newsgroup-bookmarks
8115                 (delq old gnus-newsgroup-bookmarks))
8116           (gnus-message 6 "Removed bookmark."))
8117       (gnus-message 6 "No bookmark in current article."))))
8118
8119 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8120 (defun gnus-summary-mark-as-dormant (n)
8121   "Mark N articles forward as dormant.
8122 If N is negative, mark backward instead.  The difference between N and
8123 the actual number of articles marked is returned."
8124   (interactive "p")
8125   (gnus-summary-mark-forward n gnus-dormant-mark))
8126
8127 (defun gnus-summary-set-process-mark (article)
8128   "Set the process mark on ARTICLE and update the summary line."
8129   (setq gnus-newsgroup-processable
8130         (cons article
8131               (delq article gnus-newsgroup-processable)))
8132   (when (gnus-summary-goto-subject article)
8133     (gnus-summary-show-thread)
8134     (gnus-summary-goto-subject article)
8135     (gnus-summary-update-secondary-mark article)))
8136
8137 (defun gnus-summary-remove-process-mark (article)
8138   "Remove the process mark from ARTICLE and update the summary line."
8139   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8140   (when (gnus-summary-goto-subject article)
8141     (gnus-summary-show-thread)
8142     (gnus-summary-goto-subject article)
8143     (gnus-summary-update-secondary-mark article)))
8144
8145 (defun gnus-summary-set-saved-mark (article)
8146   "Set the process mark on ARTICLE and update the summary line."
8147   (push article gnus-newsgroup-saved)
8148   (when (gnus-summary-goto-subject article)
8149     (gnus-summary-update-secondary-mark article)))
8150
8151 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8152   "Mark N articles as read forwards.
8153 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8154 The difference between N and the actual number of articles marked is
8155 returned.
8156 Iff NO-EXPIRE, auto-expiry will be inhibited."
8157   (interactive "p")
8158   (gnus-summary-show-thread)
8159   (let ((backward (< n 0))
8160         (gnus-summary-goto-unread
8161          (and gnus-summary-goto-unread
8162               (not (eq gnus-summary-goto-unread 'never))
8163               (not (memq mark (list gnus-unread-mark
8164                                     gnus-ticked-mark gnus-dormant-mark)))))
8165         (n (abs n))
8166         (mark (or mark gnus-del-mark)))
8167     (while (and (> n 0)
8168                 (gnus-summary-mark-article nil mark no-expire)
8169                 (zerop (gnus-summary-next-subject
8170                         (if backward -1 1)
8171                         (and gnus-summary-goto-unread
8172                              (not (eq gnus-summary-goto-unread 'never)))
8173                         t)))
8174       (setq n (1- n)))
8175     (when (/= 0 n)
8176       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8177     (gnus-summary-recenter)
8178     (gnus-summary-position-point)
8179     (gnus-set-mode-line 'summary)
8180     n))
8181
8182 (defun gnus-summary-mark-article-as-read (mark)
8183   "Mark the current article quickly as read with MARK."
8184   (let ((article (gnus-summary-article-number)))
8185     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8186     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8187     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8188     (push (cons article mark) gnus-newsgroup-reads)
8189     ;; Possibly remove from cache, if that is used.
8190     (when gnus-use-cache
8191       (gnus-cache-enter-remove-article article))
8192     ;; Allow the backend to change the mark.
8193     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8194     ;; Check for auto-expiry.
8195     (when (and gnus-newsgroup-auto-expire
8196                (memq mark gnus-auto-expirable-marks))
8197       (setq mark gnus-expirable-mark)
8198       ;; Let the backend know about the mark change.
8199       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8200       (push article gnus-newsgroup-expirable))
8201     ;; Set the mark in the buffer.
8202     (gnus-summary-update-mark mark 'unread)
8203     t))
8204
8205 (defun gnus-summary-mark-article-as-unread (mark)
8206   "Mark the current article quickly as unread with MARK."
8207   (let* ((article (gnus-summary-article-number))
8208          (old-mark (gnus-summary-article-mark article)))
8209     ;; Allow the backend to change the mark.
8210     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8211     (if (eq mark old-mark)
8212         t
8213       (if (<= article 0)
8214           (progn
8215             (gnus-error 1 "Can't mark negative article numbers")
8216             nil)
8217         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8218         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8219         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8220         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8221         (cond ((= mark gnus-ticked-mark)
8222                (push article gnus-newsgroup-marked))
8223               ((= mark gnus-dormant-mark)
8224                (push article gnus-newsgroup-dormant))
8225               (t
8226                (push article gnus-newsgroup-unreads)))
8227         (gnus-pull article gnus-newsgroup-reads)
8228
8229         ;; See whether the article is to be put in the cache.
8230         (and gnus-use-cache
8231              (vectorp (gnus-summary-article-header article))
8232              (save-excursion
8233                (gnus-cache-possibly-enter-article
8234                 gnus-newsgroup-name article
8235                 (= mark gnus-ticked-mark)
8236                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8237
8238         ;; Fix the mark.
8239         (gnus-summary-update-mark mark 'unread)
8240         t))))
8241
8242 (defun gnus-summary-mark-article (&optional article mark no-expire)
8243   "Mark ARTICLE with MARK.  MARK can be any character.
8244 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8245 `??' (dormant) and `?E' (expirable).
8246 If MARK is nil, then the default character `?r' is used.
8247 If ARTICLE is nil, then the article on the current line will be
8248 marked.
8249 Iff NO-EXPIRE, auto-expiry will be inhibited."
8250   ;; The mark might be a string.
8251   (when (stringp mark)
8252     (setq mark (aref mark 0)))
8253   ;; If no mark is given, then we check auto-expiring.
8254   (when (null mark)
8255     (setq mark gnus-del-mark))
8256   (when (and (not no-expire)
8257              gnus-newsgroup-auto-expire
8258              (memq mark gnus-auto-expirable-marks))
8259     (setq mark gnus-expirable-mark))
8260   (let ((article (or article (gnus-summary-article-number)))
8261         (old-mark (gnus-summary-article-mark article)))
8262     ;; Allow the backend to change the mark.
8263     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8264     (if (eq mark old-mark)
8265         t
8266       (unless article
8267         (error "No article on current line"))
8268       (if (not (if (or (= mark gnus-unread-mark)
8269                        (= mark gnus-ticked-mark)
8270                        (= mark gnus-dormant-mark))
8271                    (gnus-mark-article-as-unread article mark)
8272                  (gnus-mark-article-as-read article mark)))
8273           t
8274         ;; See whether the article is to be put in the cache.
8275         (and gnus-use-cache
8276              (not (= mark gnus-canceled-mark))
8277              (vectorp (gnus-summary-article-header article))
8278              (save-excursion
8279                (gnus-cache-possibly-enter-article
8280                 gnus-newsgroup-name article
8281                 (= mark gnus-ticked-mark)
8282                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8283
8284         (when (gnus-summary-goto-subject article nil t)
8285           (let ((buffer-read-only nil))
8286             (gnus-summary-show-thread)
8287             ;; Fix the mark.
8288             (gnus-summary-update-mark mark 'unread)
8289             t))))))
8290
8291 (defun gnus-summary-update-secondary-mark (article)
8292   "Update the secondary (read, process, cache) mark."
8293   (gnus-summary-update-mark
8294    (cond ((memq article gnus-newsgroup-processable)
8295           gnus-process-mark)
8296          ((memq article gnus-newsgroup-cached)
8297           gnus-cached-mark)
8298          ((memq article gnus-newsgroup-replied)
8299           gnus-replied-mark)
8300          ((memq article gnus-newsgroup-saved)
8301           gnus-saved-mark)
8302          (t gnus-unread-mark))
8303    'replied)
8304   (when (gnus-visual-p 'summary-highlight 'highlight)
8305     (gnus-run-hooks 'gnus-summary-update-hook))
8306   t)
8307
8308 (defun gnus-summary-update-mark (mark type)
8309   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8310         (buffer-read-only nil))
8311     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8312     (when forward
8313       (when (looking-at "\r")
8314         (incf forward))
8315       (when (<= (+ forward (point)) (point-max))
8316         ;; Go to the right position on the line.
8317         (goto-char (+ forward (point)))
8318         ;; Replace the old mark with the new mark.
8319         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8320         ;; Optionally update the marks by some user rule.
8321         (when (eq type 'unread)
8322           (gnus-data-set-mark
8323            (gnus-data-find (gnus-summary-article-number)) mark)
8324           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8325
8326 (defun gnus-mark-article-as-read (article &optional mark)
8327   "Enter ARTICLE in the pertinent lists and remove it from others."
8328   ;; Make the article expirable.
8329   (let ((mark (or mark gnus-del-mark)))
8330     (if (= mark gnus-expirable-mark)
8331         (push article gnus-newsgroup-expirable)
8332       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8333     ;; Remove from unread and marked lists.
8334     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8335     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8336     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8337     (push (cons article mark) gnus-newsgroup-reads)
8338     ;; Possibly remove from cache, if that is used.
8339     (when gnus-use-cache
8340       (gnus-cache-enter-remove-article article))
8341     t))
8342
8343 (defun gnus-mark-article-as-unread (article &optional mark)
8344   "Enter ARTICLE in the pertinent lists and remove it from others."
8345   (let ((mark (or mark gnus-ticked-mark)))
8346     (if (<= article 0)
8347         (progn
8348           (gnus-error 1 "Can't mark negative article numbers")
8349           nil)
8350       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8351             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8352             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8353             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8354
8355       ;; Unsuppress duplicates?
8356       (when gnus-suppress-duplicates
8357         (gnus-dup-unsuppress-article article))
8358
8359       (cond ((= mark gnus-ticked-mark)
8360              (push article gnus-newsgroup-marked))
8361             ((= mark gnus-dormant-mark)
8362              (push article gnus-newsgroup-dormant))
8363             (t
8364              (push article gnus-newsgroup-unreads)))
8365       (gnus-pull article gnus-newsgroup-reads)
8366       t)))
8367
8368 (defalias 'gnus-summary-mark-as-unread-forward
8369   'gnus-summary-tick-article-forward)
8370 (make-obsolete 'gnus-summary-mark-as-unread-forward
8371                'gnus-summary-tick-article-forward)
8372 (defun gnus-summary-tick-article-forward (n)
8373   "Tick N articles forwards.
8374 If N is negative, tick backwards instead.
8375 The difference between N and the number of articles ticked is returned."
8376   (interactive "p")
8377   (gnus-summary-mark-forward n gnus-ticked-mark))
8378
8379 (defalias 'gnus-summary-mark-as-unread-backward
8380   'gnus-summary-tick-article-backward)
8381 (make-obsolete 'gnus-summary-mark-as-unread-backward
8382                'gnus-summary-tick-article-backward)
8383 (defun gnus-summary-tick-article-backward (n)
8384   "Tick N articles backwards.
8385 The difference between N and the number of articles ticked is returned."
8386   (interactive "p")
8387   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8388
8389 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8390 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8391 (defun gnus-summary-tick-article (&optional article clear-mark)
8392   "Mark current article as unread.
8393 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8394 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8395   (interactive)
8396   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8397                                        gnus-ticked-mark)))
8398
8399 (defun gnus-summary-mark-as-read-forward (n)
8400   "Mark N articles as read forwards.
8401 If N is negative, mark backwards instead.
8402 The difference between N and the actual number of articles marked is
8403 returned."
8404   (interactive "p")
8405   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8406
8407 (defun gnus-summary-mark-as-read-backward (n)
8408   "Mark the N articles as read backwards.
8409 The difference between N and the actual number of articles marked is
8410 returned."
8411   (interactive "p")
8412   (gnus-summary-mark-forward
8413    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8414
8415 (defun gnus-summary-mark-as-read (&optional article mark)
8416   "Mark current article as read.
8417 ARTICLE specifies the article to be marked as read.
8418 MARK specifies a string to be inserted at the beginning of the line."
8419   (gnus-summary-mark-article article mark))
8420
8421 (defun gnus-summary-clear-mark-forward (n)
8422   "Clear marks from N articles forward.
8423 If N is negative, clear backward instead.
8424 The difference between N and the number of marks cleared is returned."
8425   (interactive "p")
8426   (gnus-summary-mark-forward n gnus-unread-mark))
8427
8428 (defun gnus-summary-clear-mark-backward (n)
8429   "Clear marks from N articles backward.
8430 The difference between N and the number of marks cleared is returned."
8431   (interactive "p")
8432   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8433
8434 (defun gnus-summary-mark-unread-as-read ()
8435   "Intended to be used by `gnus-summary-mark-article-hook'."
8436   (when (memq gnus-current-article gnus-newsgroup-unreads)
8437     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8438
8439 (defun gnus-summary-mark-read-and-unread-as-read ()
8440   "Intended to be used by `gnus-summary-mark-article-hook'."
8441   (let ((mark (gnus-summary-article-mark)))
8442     (when (or (gnus-unread-mark-p mark)
8443               (gnus-read-mark-p mark))
8444       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8445
8446 (defun gnus-summary-mark-region-as-read (point mark all)
8447   "Mark all unread articles between point and mark as read.
8448 If given a prefix, mark all articles between point and mark as read,
8449 even ticked and dormant ones."
8450   (interactive "r\nP")
8451   (save-excursion
8452     (let (article)
8453       (goto-char point)
8454       (beginning-of-line)
8455       (while (and
8456               (< (point) mark)
8457               (progn
8458                 (when (or all
8459                           (memq (setq article (gnus-summary-article-number))
8460                                 gnus-newsgroup-unreads))
8461                   (gnus-summary-mark-article article gnus-del-mark))
8462                 t)
8463               (gnus-summary-find-next))))))
8464
8465 (defun gnus-summary-mark-below (score mark)
8466   "Mark articles with score less than SCORE with MARK."
8467   (interactive "P\ncMark: ")
8468   (setq score (if score
8469                   (prefix-numeric-value score)
8470                 (or gnus-summary-default-score 0)))
8471   (save-excursion
8472     (set-buffer gnus-summary-buffer)
8473     (goto-char (point-min))
8474     (while
8475         (progn
8476           (and (< (gnus-summary-article-score) score)
8477                (gnus-summary-mark-article nil mark))
8478           (gnus-summary-find-next)))))
8479
8480 (defun gnus-summary-kill-below (&optional score)
8481   "Mark articles with score below SCORE as read."
8482   (interactive "P")
8483   (gnus-summary-mark-below score gnus-killed-mark))
8484
8485 (defun gnus-summary-clear-above (&optional score)
8486   "Clear all marks from articles with score above SCORE."
8487   (interactive "P")
8488   (gnus-summary-mark-above score gnus-unread-mark))
8489
8490 (defun gnus-summary-tick-above (&optional score)
8491   "Tick all articles with score above SCORE."
8492   (interactive "P")
8493   (gnus-summary-mark-above score gnus-ticked-mark))
8494
8495 (defun gnus-summary-mark-above (score mark)
8496   "Mark articles with score over SCORE with MARK."
8497   (interactive "P\ncMark: ")
8498   (setq score (if score
8499                   (prefix-numeric-value score)
8500                 (or gnus-summary-default-score 0)))
8501   (save-excursion
8502     (set-buffer gnus-summary-buffer)
8503     (goto-char (point-min))
8504     (while (and (progn
8505                   (when (> (gnus-summary-article-score) score)
8506                     (gnus-summary-mark-article nil mark))
8507                   t)
8508                 (gnus-summary-find-next)))))
8509
8510 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8511 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8512 (defun gnus-summary-limit-include-expunged (&optional no-error)
8513   "Display all the hidden articles that were expunged for low scores."
8514   (interactive)
8515   (let ((buffer-read-only nil))
8516     (let ((scored gnus-newsgroup-scored)
8517           headers h)
8518       (while scored
8519         (unless (gnus-summary-goto-subject (caar scored))
8520           (and (setq h (gnus-summary-article-header (caar scored)))
8521                (< (cdar scored) gnus-summary-expunge-below)
8522                (push h headers)))
8523         (setq scored (cdr scored)))
8524       (if (not headers)
8525           (when (not no-error)
8526             (error "No expunged articles hidden"))
8527         (goto-char (point-min))
8528         (gnus-summary-prepare-unthreaded (nreverse headers))
8529         (goto-char (point-min))
8530         (gnus-summary-position-point)
8531         t))))
8532
8533 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8534   "Mark all unread articles in this newsgroup as read.
8535 If prefix argument ALL is non-nil, ticked and dormant articles will
8536 also be marked as read.
8537 If QUIETLY is non-nil, no questions will be asked.
8538 If TO-HERE is non-nil, it should be a point in the buffer.  All
8539 articles before this point will be marked as read.
8540 Note that this function will only catch up the unread article
8541 in the current summary buffer limitation.
8542 The number of articles marked as read is returned."
8543   (interactive "P")
8544   (prog1
8545       (save-excursion
8546         (when (or quietly
8547                   (not gnus-interactive-catchup) ;Without confirmation?
8548                   gnus-expert-user
8549                   (gnus-y-or-n-p
8550                    (if all
8551                        "Mark absolutely all articles as read? "
8552                      "Mark all unread articles as read? ")))
8553           (if (and not-mark
8554                    (not gnus-newsgroup-adaptive)
8555                    (not gnus-newsgroup-auto-expire)
8556                    (not gnus-suppress-duplicates)
8557                    (or (not gnus-use-cache)
8558                        (eq gnus-use-cache 'passive)))
8559               (progn
8560                 (when all
8561                   (setq gnus-newsgroup-marked nil
8562                         gnus-newsgroup-dormant nil))
8563                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8564             ;; We actually mark all articles as canceled, which we
8565             ;; have to do when using auto-expiry or adaptive scoring.
8566             (gnus-summary-show-all-threads)
8567             (when (gnus-summary-first-subject (not all) t)
8568               (while (and
8569                       (if to-here (< (point) to-here) t)
8570                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8571                       (gnus-summary-find-next (not all) nil nil t))))
8572             (gnus-set-mode-line 'summary))
8573           t))
8574     (gnus-summary-position-point)))
8575
8576 (defun gnus-summary-catchup-to-here (&optional all)
8577   "Mark all unticked articles before the current one as read.
8578 If ALL is non-nil, also mark ticked and dormant articles as read."
8579   (interactive "P")
8580   (save-excursion
8581     (gnus-save-hidden-threads
8582       (let ((beg (point)))
8583         ;; We check that there are unread articles.
8584         (when (or all (gnus-summary-find-prev))
8585           (gnus-summary-catchup all t beg)))))
8586   (gnus-summary-position-point))
8587
8588 (defun gnus-summary-catchup-all (&optional quietly)
8589   "Mark all articles in this newsgroup as read."
8590   (interactive "P")
8591   (gnus-summary-catchup t quietly))
8592
8593 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8594   "Mark all unread articles in this group as read, then exit.
8595 If prefix argument ALL is non-nil, all articles are marked as read."
8596   (interactive "P")
8597   (when (gnus-summary-catchup all quietly nil 'fast)
8598     ;; Select next newsgroup or exit.
8599     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8600              (eq gnus-auto-select-next 'quietly))
8601         (gnus-summary-next-group nil)
8602       (gnus-summary-exit))))
8603
8604 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8605   "Mark all articles in this newsgroup as read, and then exit."
8606   (interactive "P")
8607   (gnus-summary-catchup-and-exit t quietly))
8608
8609 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8610   "Mark all articles in this group as read and select the next group.
8611 If given a prefix, mark all articles, unread as well as ticked, as
8612 read."
8613   (interactive "P")
8614   (save-excursion
8615     (gnus-summary-catchup all))
8616   (gnus-summary-next-group))
8617
8618 ;; Thread-based commands.
8619
8620 (defun gnus-summary-articles-in-thread (&optional article)
8621   "Return a list of all articles in the current thread.
8622 If ARTICLE is non-nil, return all articles in the thread that starts
8623 with that article."
8624   (let* ((article (or article (gnus-summary-article-number)))
8625          (data (gnus-data-find-list article))
8626          (top-level (gnus-data-level (car data)))
8627          (top-subject
8628           (cond ((null gnus-thread-operation-ignore-subject)
8629                  (gnus-simplify-subject-re
8630                   (mail-header-subject (gnus-data-header (car data)))))
8631                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8632                  (gnus-simplify-subject-fuzzy
8633                   (mail-header-subject (gnus-data-header (car data)))))
8634                 (t nil)))
8635          (end-point (save-excursion
8636                       (if (gnus-summary-go-to-next-thread)
8637                           (point) (point-max))))
8638          articles)
8639     (while (and data
8640                 (< (gnus-data-pos (car data)) end-point))
8641       (when (or (not top-subject)
8642                 (string= top-subject
8643                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8644                              (gnus-simplify-subject-fuzzy
8645                               (mail-header-subject
8646                                (gnus-data-header (car data))))
8647                            (gnus-simplify-subject-re
8648                             (mail-header-subject
8649                              (gnus-data-header (car data)))))))
8650         (push (gnus-data-number (car data)) articles))
8651       (unless (and (setq data (cdr data))
8652                    (> (gnus-data-level (car data)) top-level))
8653         (setq data nil)))
8654     ;; Return the list of articles.
8655     (nreverse articles)))
8656
8657 (defun gnus-summary-rethread-current ()
8658   "Rethread the thread the current article is part of."
8659   (interactive)
8660   (let* ((gnus-show-threads t)
8661          (article (gnus-summary-article-number))
8662          (id (mail-header-id (gnus-summary-article-header)))
8663          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8664     (unless id
8665       (error "No article on the current line"))
8666     (gnus-rebuild-thread id)
8667     (gnus-summary-goto-subject article)))
8668
8669 (defun gnus-summary-reparent-thread ()
8670   "Make the current article child of the marked (or previous) article.
8671
8672 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8673 is non-nil or the Subject: of both articles are the same."
8674   (interactive)
8675   (unless (not (gnus-group-read-only-p))
8676     (error "The current newsgroup does not support article editing"))
8677   (unless (<= (length gnus-newsgroup-processable) 1)
8678     (error "No more than one article may be marked"))
8679   (save-window-excursion
8680     (let ((gnus-article-buffer " *reparent*")
8681           (current-article (gnus-summary-article-number))
8682           ;; First grab the marked article, otherwise one line up.
8683           (parent-article (if (not (null gnus-newsgroup-processable))
8684                               (car gnus-newsgroup-processable)
8685                             (save-excursion
8686                               (if (eq (forward-line -1) 0)
8687                                   (gnus-summary-article-number)
8688                                 (error "Beginning of summary buffer"))))))
8689       (unless (not (eq current-article parent-article))
8690         (error "An article may not be self-referential"))
8691       (let ((message-id (mail-header-id
8692                          (gnus-summary-article-header parent-article))))
8693         (unless (and message-id (not (equal message-id "")))
8694           (error "No message-id in desired parent"))
8695         (gnus-with-article current-article
8696           (goto-char (point-min))
8697           (if (re-search-forward "^References: " nil t)
8698               (progn
8699                 (re-search-forward "^[^ \t]" nil t)
8700                 (forward-line -1)
8701                 (end-of-line)
8702                 (insert " " message-id))
8703             (insert "References: " message-id "\n")))
8704         (set-buffer gnus-summary-buffer)
8705         (gnus-summary-unmark-all-processable)
8706         (gnus-summary-update-article current-article)
8707         (gnus-summary-rethread-current)
8708         (gnus-message 3 "Article %d is now the child of article %d"
8709                       current-article parent-article)))))
8710
8711 (defun gnus-summary-toggle-threads (&optional arg)
8712   "Toggle showing conversation threads.
8713 If ARG is positive number, turn showing conversation threads on."
8714   (interactive "P")
8715   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8716     (setq gnus-show-threads
8717           (if (null arg) (not gnus-show-threads)
8718             (> (prefix-numeric-value arg) 0)))
8719     (gnus-summary-prepare)
8720     (gnus-summary-goto-subject current)
8721     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8722     (gnus-summary-position-point)))
8723
8724 (defun gnus-summary-show-all-threads ()
8725   "Show all threads."
8726   (interactive)
8727   (save-excursion
8728     (let ((buffer-read-only nil))
8729       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8730   (gnus-summary-position-point))
8731
8732 (defun gnus-summary-show-thread ()
8733   "Show thread subtrees.
8734 Returns nil if no thread was there to be shown."
8735   (interactive)
8736   (let ((buffer-read-only nil)
8737         (orig (point))
8738         ;; first goto end then to beg, to have point at beg after let
8739         (end (progn (end-of-line) (point)))
8740         (beg (progn (beginning-of-line) (point))))
8741     (prog1
8742         ;; Any hidden lines here?
8743         (search-forward "\r" end t)
8744       (subst-char-in-region beg end ?\^M ?\n t)
8745       (goto-char orig)
8746       (gnus-summary-position-point))))
8747
8748 (defun gnus-summary-hide-all-threads ()
8749   "Hide all thread subtrees."
8750   (interactive)
8751   (save-excursion
8752     (goto-char (point-min))
8753     (gnus-summary-hide-thread)
8754     (while (zerop (gnus-summary-next-thread 1 t))
8755       (gnus-summary-hide-thread)))
8756   (gnus-summary-position-point))
8757
8758 (defun gnus-summary-hide-thread ()
8759   "Hide thread subtrees.
8760 Returns nil if no threads were there to be hidden."
8761   (interactive)
8762   (let ((buffer-read-only nil)
8763         (start (point))
8764         (article (gnus-summary-article-number)))
8765     (goto-char start)
8766     ;; Go forward until either the buffer ends or the subthread
8767     ;; ends.
8768     (when (and (not (eobp))
8769                (or (zerop (gnus-summary-next-thread 1 t))
8770                    (goto-char (point-max))))
8771       (prog1
8772           (if (and (> (point) start)
8773                    (search-backward "\n" start t))
8774               (progn
8775                 (subst-char-in-region start (point) ?\n ?\^M)
8776                 (gnus-summary-goto-subject article))
8777             (goto-char start)
8778             nil)))))
8779
8780 (defun gnus-summary-go-to-next-thread (&optional previous)
8781   "Go to the same level (or less) next thread.
8782 If PREVIOUS is non-nil, go to previous thread instead.
8783 Return the article number moved to, or nil if moving was impossible."
8784   (let ((level (gnus-summary-thread-level))
8785         (way (if previous -1 1))
8786         (beg (point)))
8787     (forward-line way)
8788     (while (and (not (eobp))
8789                 (< level (gnus-summary-thread-level)))
8790       (forward-line way))
8791     (if (eobp)
8792         (progn
8793           (goto-char beg)
8794           nil)
8795       (setq beg (point))
8796       (prog1
8797           (gnus-summary-article-number)
8798         (goto-char beg)))))
8799
8800 (defun gnus-summary-next-thread (n &optional silent)
8801   "Go to the same level next N'th thread.
8802 If N is negative, search backward instead.
8803 Returns the difference between N and the number of skips actually
8804 done.
8805
8806 If SILENT, don't output messages."
8807   (interactive "p")
8808   (let ((backward (< n 0))
8809         (n (abs n)))
8810     (while (and (> n 0)
8811                 (gnus-summary-go-to-next-thread backward))
8812       (decf n))
8813     (unless silent
8814       (gnus-summary-position-point))
8815     (when (and (not silent) (/= 0 n))
8816       (gnus-message 7 "No more threads"))
8817     n))
8818
8819 (defun gnus-summary-prev-thread (n)
8820   "Go to the same level previous N'th thread.
8821 Returns the difference between N and the number of skips actually
8822 done."
8823   (interactive "p")
8824   (gnus-summary-next-thread (- n)))
8825
8826 (defun gnus-summary-go-down-thread ()
8827   "Go down one level in the current thread."
8828   (let ((children (gnus-summary-article-children)))
8829     (when children
8830       (gnus-summary-goto-subject (car children)))))
8831
8832 (defun gnus-summary-go-up-thread ()
8833   "Go up one level in the current thread."
8834   (let ((parent (gnus-summary-article-parent)))
8835     (when parent
8836       (gnus-summary-goto-subject parent))))
8837
8838 (defun gnus-summary-down-thread (n)
8839   "Go down thread N steps.
8840 If N is negative, go up instead.
8841 Returns the difference between N and how many steps down that were
8842 taken."
8843   (interactive "p")
8844   (let ((up (< n 0))
8845         (n (abs n)))
8846     (while (and (> n 0)
8847                 (if up (gnus-summary-go-up-thread)
8848                   (gnus-summary-go-down-thread)))
8849       (setq n (1- n)))
8850     (gnus-summary-position-point)
8851     (when (/= 0 n)
8852       (gnus-message 7 "Can't go further"))
8853     n))
8854
8855 (defun gnus-summary-up-thread (n)
8856   "Go up thread N steps.
8857 If N is negative, go up instead.
8858 Returns the difference between N and how many steps down that were
8859 taken."
8860   (interactive "p")
8861   (gnus-summary-down-thread (- n)))
8862
8863 (defun gnus-summary-top-thread ()
8864   "Go to the top of the thread."
8865   (interactive)
8866   (while (gnus-summary-go-up-thread))
8867   (gnus-summary-article-number))
8868
8869 (defun gnus-summary-kill-thread (&optional unmark)
8870   "Mark articles under current thread as read.
8871 If the prefix argument is positive, remove any kinds of marks.
8872 If the prefix argument is negative, tick articles instead."
8873   (interactive "P")
8874   (when unmark
8875     (setq unmark (prefix-numeric-value unmark)))
8876   (let ((articles (gnus-summary-articles-in-thread)))
8877     (save-excursion
8878       ;; Expand the thread.
8879       (gnus-summary-show-thread)
8880       ;; Mark all the articles.
8881       (while articles
8882         (gnus-summary-goto-subject (car articles))
8883         (cond ((null unmark)
8884                (gnus-summary-mark-article-as-read gnus-killed-mark))
8885               ((> unmark 0)
8886                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8887               (t
8888                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8889         (setq articles (cdr articles))))
8890     ;; Hide killed subtrees.
8891     (and (null unmark)
8892          gnus-thread-hide-killed
8893          (gnus-summary-hide-thread))
8894     ;; If marked as read, go to next unread subject.
8895     (when (null unmark)
8896       ;; Go to next unread subject.
8897       (gnus-summary-next-subject 1 t)))
8898   (gnus-set-mode-line 'summary))
8899
8900 ;; Summary sorting commands
8901
8902 (defun gnus-summary-sort-by-number (&optional reverse)
8903   "Sort the summary buffer by article number.
8904 Argument REVERSE means reverse order."
8905   (interactive "P")
8906   (gnus-summary-sort 'number reverse))
8907
8908 (defun gnus-summary-sort-by-author (&optional reverse)
8909   "Sort the summary buffer by author name alphabetically.
8910 If case-fold-search is non-nil, case of letters is ignored.
8911 Argument REVERSE means reverse order."
8912   (interactive "P")
8913   (gnus-summary-sort 'author reverse))
8914
8915 (defun gnus-summary-sort-by-subject (&optional reverse)
8916   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8917 If case-fold-search is non-nil, case of letters is ignored.
8918 Argument REVERSE means reverse order."
8919   (interactive "P")
8920   (gnus-summary-sort 'subject reverse))
8921
8922 (defun gnus-summary-sort-by-date (&optional reverse)
8923   "Sort the summary buffer by date.
8924 Argument REVERSE means reverse order."
8925   (interactive "P")
8926   (gnus-summary-sort 'date reverse))
8927
8928 (defun gnus-summary-sort-by-score (&optional reverse)
8929   "Sort the summary buffer by score.
8930 Argument REVERSE means reverse order."
8931   (interactive "P")
8932   (gnus-summary-sort 'score reverse))
8933
8934 (defun gnus-summary-sort-by-lines (&optional reverse)
8935   "Sort the summary buffer by the number of lines.
8936 Argument REVERSE means reverse order."
8937   (interactive "P")
8938   (gnus-summary-sort 'lines reverse))
8939
8940 (defun gnus-summary-sort-by-chars (&optional reverse)
8941   "Sort the summary buffer by article length.
8942 Argument REVERSE means reverse order."
8943   (interactive "P")
8944   (gnus-summary-sort 'chars reverse))   
8945
8946 (defun gnus-summary-sort (predicate reverse)
8947   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8948   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8949          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8950          (gnus-thread-sort-functions
8951           (if (not reverse)
8952               thread
8953             `(lambda (t1 t2)
8954                (,thread t2 t1))))
8955          (gnus-article-sort-functions
8956           (if (not reverse)
8957               article
8958             `(lambda (t1 t2)
8959                (,article t2 t1))))
8960          (buffer-read-only)
8961          (gnus-summary-prepare-hook nil))
8962     ;; We do the sorting by regenerating the threads.
8963     (gnus-summary-prepare)
8964     ;; Hide subthreads if needed.
8965     (when (and gnus-show-threads gnus-thread-hide-subtree)
8966       (gnus-summary-hide-all-threads))))
8967
8968 ;; Summary saving commands.
8969
8970 (defun gnus-summary-save-article (&optional n not-saved)
8971   "Save the current article using the default saver function.
8972 If N is a positive number, save the N next articles.
8973 If N is a negative number, save the N previous articles.
8974 If N is nil and any articles have been marked with the process mark,
8975 save those articles instead.
8976 The variable `gnus-default-article-saver' specifies the saver function."
8977   (interactive "P")
8978   (let* ((articles (gnus-summary-work-articles n))
8979          (save-buffer (save-excursion
8980                         (nnheader-set-temp-buffer " *Gnus Save*")))
8981          (num (length articles))
8982          header file)
8983     (dolist (article articles)
8984       (setq header (gnus-summary-article-header article))
8985       (if (not (vectorp header))
8986           ;; This is a pseudo-article.
8987           (if (assq 'name header)
8988               (gnus-copy-file (cdr (assq 'name header)))
8989             (gnus-message 1 "Article %d is unsaveable" article))
8990         ;; This is a real article.
8991         (save-window-excursion
8992           (gnus-summary-select-article t nil nil article))
8993         (save-excursion
8994           (set-buffer save-buffer)
8995           (erase-buffer)
8996           (insert-buffer-substring gnus-original-article-buffer))
8997         (setq file (gnus-article-save save-buffer file num))
8998         (gnus-summary-remove-process-mark article)
8999         (unless not-saved
9000           (gnus-summary-set-saved-mark article))))
9001     (gnus-kill-buffer save-buffer)
9002     (gnus-summary-position-point)
9003     (gnus-set-mode-line 'summary)
9004     n))
9005
9006 (defun gnus-summary-pipe-output (&optional arg)
9007   "Pipe the current article to a subprocess.
9008 If N is a positive number, pipe the N next articles.
9009 If N is a negative number, pipe the N previous articles.
9010 If N is nil and any articles have been marked with the process mark,
9011 pipe those articles instead."
9012   (interactive "P")
9013   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9014     (gnus-summary-save-article arg t))
9015   (gnus-configure-windows 'pipe))
9016
9017 (defun gnus-summary-save-article-mail (&optional arg)
9018   "Append the current article to an mail file.
9019 If N is a positive number, save the N next articles.
9020 If N is a negative number, save the N previous articles.
9021 If N is nil and any articles have been marked with the process mark,
9022 save those articles instead."
9023   (interactive "P")
9024   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9025     (gnus-summary-save-article arg)))
9026
9027 (defun gnus-summary-save-article-rmail (&optional arg)
9028   "Append the current article to an rmail file.
9029 If N is a positive number, save the N next articles.
9030 If N is a negative number, save the N previous articles.
9031 If N is nil and any articles have been marked with the process mark,
9032 save those articles instead."
9033   (interactive "P")
9034   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
9035     (gnus-summary-save-article arg)))
9036
9037 (defun gnus-summary-save-article-file (&optional arg)
9038   "Append the current article to a file.
9039 If N is a positive number, save the N next articles.
9040 If N is a negative number, save the N previous articles.
9041 If N is nil and any articles have been marked with the process mark,
9042 save those articles instead."
9043   (interactive "P")
9044   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9045     (gnus-summary-save-article arg)))
9046
9047 (defun gnus-summary-write-article-file (&optional arg)
9048   "Write the current article to a file, deleting the previous file.
9049 If N is a positive number, save the N next articles.
9050 If N is a negative number, save the N previous articles.
9051 If N is nil and any articles have been marked with the process mark,
9052 save those articles instead."
9053   (interactive "P")
9054   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9055     (gnus-summary-save-article arg)))
9056
9057 (defun gnus-summary-save-article-body-file (&optional arg)
9058   "Append the current article body to a file.
9059 If N is a positive number, save the N next articles.
9060 If N is a negative number, save the N previous articles.
9061 If N is nil and any articles have been marked with the process mark,
9062 save those articles instead."
9063   (interactive "P")
9064   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9065     (gnus-summary-save-article arg)))
9066
9067 (defun gnus-summary-pipe-message (program)
9068   "Pipe the current article through PROGRAM."
9069   (interactive "sProgram: ")
9070   (gnus-summary-select-article)
9071   (let ((mail-header-separator ""))
9072     (gnus-eval-in-buffer-window gnus-article-buffer
9073       (save-restriction
9074         (widen)
9075         (let ((start (window-start))
9076               buffer-read-only)
9077           (message-pipe-buffer-body program)
9078           (set-window-start (get-buffer-window (current-buffer)) start))))))
9079
9080 (defun gnus-get-split-value (methods)
9081   "Return a value based on the split METHODS."
9082   (let (split-name method result match)
9083     (when methods
9084       (save-excursion
9085         (set-buffer gnus-original-article-buffer)
9086         (save-restriction
9087           (nnheader-narrow-to-headers)
9088           (while methods
9089             (goto-char (point-min))
9090             (setq method (pop methods))
9091             (setq match (car method))
9092             (when (cond
9093                    ((stringp match)
9094                     ;; Regular expression.
9095                     (ignore-errors
9096                       (re-search-forward match nil t)))
9097                    ((gnus-functionp match)
9098                     ;; Function.
9099                     (save-restriction
9100                       (widen)
9101                       (setq result (funcall match gnus-newsgroup-name))))
9102                    ((consp match)
9103                     ;; Form.
9104                     (save-restriction
9105                       (widen)
9106                       (setq result (eval match)))))
9107               (setq split-name (append (cdr method) split-name))
9108               (cond ((stringp result)
9109                      (push (expand-file-name
9110                             result gnus-article-save-directory)
9111                            split-name))
9112                     ((consp result)
9113                      (setq split-name (append result split-name)))))))))
9114     (nreverse split-name)))
9115
9116 (defun gnus-valid-move-group-p (group)
9117   (and (boundp group)
9118        (symbol-name group)
9119        (symbol-value group)
9120        (gnus-get-function (gnus-find-method-for-group
9121                            (symbol-name group)) 'request-accept-article t)))
9122
9123 (defun gnus-read-move-group-name (prompt default articles prefix)
9124   "Read a group name."
9125   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9126          (minibuffer-confirm-incomplete nil) ; XEmacs
9127          (prom
9128           (format "%s %s to:"
9129                   prompt
9130                   (if (> (length articles) 1)
9131                       (format "these %d articles" (length articles))
9132                     "this article")))
9133          (to-newsgroup
9134           (cond
9135            ((null split-name)
9136             (gnus-completing-read default prom
9137                                   gnus-active-hashtb
9138                                   'gnus-valid-move-group-p
9139                                   nil prefix
9140                                   'gnus-group-history))
9141            ((= 1 (length split-name))
9142             (gnus-completing-read (car split-name) prom
9143                                   gnus-active-hashtb
9144                                   'gnus-valid-move-group-p
9145                                   nil nil
9146                                   'gnus-group-history))
9147            (t
9148             (gnus-completing-read nil prom
9149                                   (mapcar (lambda (el) (list el))
9150                                           (nreverse split-name))
9151                                   nil nil nil
9152                                   'gnus-group-history))))
9153          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9154     (when to-newsgroup
9155       (if (or (string= to-newsgroup "")
9156               (string= to-newsgroup prefix))
9157           (setq to-newsgroup default))
9158       (unless to-newsgroup
9159         (error "No group name entered"))
9160       (or (gnus-active to-newsgroup)
9161           (gnus-activate-group to-newsgroup nil nil to-method)
9162           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9163                                      to-newsgroup))
9164               (or (and (gnus-request-create-group to-newsgroup to-method)
9165                        (gnus-activate-group
9166                         to-newsgroup nil nil to-method)
9167                        (gnus-subscribe-group to-newsgroup))
9168                   (error "Couldn't create group %s" to-newsgroup)))
9169           (error "No such group: %s" to-newsgroup)))
9170     to-newsgroup))
9171
9172 (defun gnus-summary-save-parts (type dir n &optional reverse)
9173   "Save parts matching TYPE to DIR.
9174 If REVERSE, save parts that do not match TYPE."
9175   (interactive
9176    (list (read-string "Save parts of type: " "image/.*")
9177          (read-file-name "Save to directory: " nil nil t)
9178          current-prefix-arg))
9179   (gnus-summary-iterate n
9180     (let ((gnus-display-mime-function nil)
9181           (gnus-inhibit-treatment t))
9182       (gnus-summary-select-article))
9183     (save-excursion
9184       (set-buffer gnus-article-buffer)
9185       (let ((handles (or (mm-dissect-buffer gnus-current-headers)
9186                          (mm-uu-dissect))))
9187         (when handles
9188           (gnus-summary-save-parts-1 type dir handles reverse)
9189           (mm-destroy-parts handles))))))
9190
9191 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9192   (if (stringp (car handle))
9193       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9194               (cdr handle))
9195     (when (if reverse
9196               (not (string-match type (mm-handle-media-type handle)))
9197             (string-match type (mm-handle-media-type handle)))
9198       (let ((file (expand-file-name
9199                    (file-name-nondirectory
9200                     (or
9201                      (mail-content-type-get
9202                       (mm-handle-disposition handle) 'filename)
9203                      (concat gnus-newsgroup-name "." gnus-current-article)))
9204                    dir)))
9205         (unless (file-exists-p file)
9206           (mm-save-part-to-file handle file))))))
9207
9208 ;; Summary extract commands
9209
9210 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9211   (let ((buffer-read-only nil)
9212         (article (gnus-summary-article-number))
9213         after-article b e)
9214     (unless (gnus-summary-goto-subject article)
9215       (error "No such article: %d" article))
9216     (gnus-summary-position-point)
9217     ;; If all commands are to be bunched up on one line, we collect
9218     ;; them here.
9219     (unless gnus-view-pseudos-separately
9220       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9221             files action)
9222         (while ps
9223           (setq action (cdr (assq 'action (car ps))))
9224           (setq files (list (cdr (assq 'name (car ps)))))
9225           (while (and ps (cdr ps)
9226                       (string= (or action "1")
9227                                (or (cdr (assq 'action (cadr ps))) "2")))
9228             (push (cdr (assq 'name (cadr ps))) files)
9229             (setcdr ps (cddr ps)))
9230           (when files
9231             (when (not (string-match "%s" action))
9232               (push " " files))
9233             (push " " files)
9234             (when (assq 'execute (car ps))
9235               (setcdr (assq 'execute (car ps))
9236                       (funcall (if (string-match "%s" action)
9237                                    'format 'concat)
9238                                action
9239                                (mapconcat
9240                                 (lambda (f)
9241                                   (if (equal f " ")
9242                                       f
9243                                     (mm-quote-arg f)))
9244                                 files " ")))))
9245           (setq ps (cdr ps)))))
9246     (if (and gnus-view-pseudos (not not-view))
9247         (while pslist
9248           (when (assq 'execute (car pslist))
9249             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9250                                   (eq gnus-view-pseudos 'not-confirm)))
9251           (setq pslist (cdr pslist)))
9252       (save-excursion
9253         (while pslist
9254           (setq after-article (or (cdr (assq 'article (car pslist)))
9255                                   (gnus-summary-article-number)))
9256           (gnus-summary-goto-subject after-article)
9257           (forward-line 1)
9258           (setq b (point))
9259           (insert "    " (file-name-nondirectory
9260                           (cdr (assq 'name (car pslist))))
9261                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9262           (setq e (point))
9263           (forward-line -1)             ; back to `b'
9264           (gnus-add-text-properties
9265            b (1- e) (list 'gnus-number gnus-reffed-article-number
9266                           gnus-mouse-face-prop gnus-mouse-face))
9267           (gnus-data-enter
9268            after-article gnus-reffed-article-number
9269            gnus-unread-mark b (car pslist) 0 (- e b))
9270           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9271           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9272           (setq pslist (cdr pslist)))))))
9273
9274 (defun gnus-pseudos< (p1 p2)
9275   (let ((c1 (cdr (assq 'action p1)))
9276         (c2 (cdr (assq 'action p2))))
9277     (and c1 c2 (string< c1 c2))))
9278
9279 (defun gnus-request-pseudo-article (props)
9280   (cond ((assq 'execute props)
9281          (gnus-execute-command (cdr (assq 'execute props)))))
9282   (let ((gnus-current-article (gnus-summary-article-number)))
9283     (gnus-run-hooks 'gnus-mark-article-hook)))
9284
9285 (defun gnus-execute-command (command &optional automatic)
9286   (save-excursion
9287     (gnus-article-setup-buffer)
9288     (set-buffer gnus-article-buffer)
9289     (setq buffer-read-only nil)
9290     (let ((command (if automatic command
9291                      (read-string "Command: " (cons command 0)))))
9292       (erase-buffer)
9293       (insert "$ " command "\n\n")
9294       (if gnus-view-pseudo-asynchronously
9295           (start-process "gnus-execute" (current-buffer) shell-file-name
9296                          shell-command-switch command)
9297         (call-process shell-file-name nil t nil
9298                       shell-command-switch command)))))
9299
9300 ;; Summary kill commands.
9301
9302 (defun gnus-summary-edit-global-kill (article)
9303   "Edit the \"global\" kill file."
9304   (interactive (list (gnus-summary-article-number)))
9305   (gnus-group-edit-global-kill article))
9306
9307 (defun gnus-summary-edit-local-kill ()
9308   "Edit a local kill file applied to the current newsgroup."
9309   (interactive)
9310   (setq gnus-current-headers (gnus-summary-article-header))
9311   (gnus-group-edit-local-kill
9312    (gnus-summary-article-number) gnus-newsgroup-name))
9313
9314 ;;; Header reading.
9315
9316 (defun gnus-read-header (id &optional header)
9317   "Read the headers of article ID and enter them into the Gnus system."
9318   (let ((group gnus-newsgroup-name)
9319         (gnus-override-method
9320          (or
9321           gnus-override-method
9322           (and (gnus-news-group-p gnus-newsgroup-name)
9323                (car (gnus-refer-article-methods)))))
9324         where)
9325     ;; First we check to see whether the header in question is already
9326     ;; fetched.
9327     (if (stringp id)
9328         ;; This is a Message-ID.
9329         (setq header (or header (gnus-id-to-header id)))
9330       ;; This is an article number.
9331       (setq header (or header (gnus-summary-article-header id))))
9332     (if (and header
9333              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9334         ;; We have found the header.
9335         header
9336       ;; If this is a sparse article, we have to nix out its
9337       ;; previous entry in the thread hashtb.
9338       (when (and header
9339                  (gnus-summary-article-sparse-p (mail-header-number header)))
9340         (let* ((parent (gnus-parent-id (mail-header-references header)))
9341                (thread (and parent (gnus-id-to-thread parent))))
9342           (when thread
9343             (delq (assq header thread) thread))))
9344       ;; We have to really fetch the header to this article.
9345       (save-excursion
9346         (set-buffer nntp-server-buffer)
9347         (when (setq where (gnus-request-head id group))
9348           (nnheader-fold-continuation-lines)
9349           (goto-char (point-max))
9350           (insert ".\n")
9351           (goto-char (point-min))
9352           (insert "211 ")
9353           (princ (cond
9354                   ((numberp id) id)
9355                   ((cdr where) (cdr where))
9356                   (header (mail-header-number header))
9357                   (t gnus-reffed-article-number))
9358                  (current-buffer))
9359           (insert " Article retrieved.\n"))
9360         (if (or (not where)
9361                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9362             ()                          ; Malformed head.
9363           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9364             (when (and (stringp id)
9365                        (not (string= (gnus-group-real-name group)
9366                                      (car where))))
9367               ;; If we fetched by Message-ID and the article came
9368               ;; from a different group, we fudge some bogus article
9369               ;; numbers for this article.
9370               (mail-header-set-number header gnus-reffed-article-number))
9371             (save-excursion
9372               (set-buffer gnus-summary-buffer)
9373               (decf gnus-reffed-article-number)
9374               (gnus-remove-header (mail-header-number header))
9375               (push header gnus-newsgroup-headers)
9376               (setq gnus-current-headers header)
9377               (push (mail-header-number header) gnus-newsgroup-limit)))
9378           header)))))
9379
9380 (defun gnus-remove-header (number)
9381   "Remove header NUMBER from `gnus-newsgroup-headers'."
9382   (if (and gnus-newsgroup-headers
9383            (= number (mail-header-number (car gnus-newsgroup-headers))))
9384       (pop gnus-newsgroup-headers)
9385     (let ((headers gnus-newsgroup-headers))
9386       (while (and (cdr headers)
9387                   (not (= number (mail-header-number (cadr headers)))))
9388         (pop headers))
9389       (when (cdr headers)
9390         (setcdr headers (cddr headers))))))
9391
9392 ;;;
9393 ;;; summary highlights
9394 ;;;
9395
9396 (defun gnus-highlight-selected-summary ()
9397   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9398   ;; Highlight selected article in summary buffer
9399   (when gnus-summary-selected-face
9400     (save-excursion
9401       (let* ((beg (progn (beginning-of-line) (point)))
9402              (end (progn (end-of-line) (point)))
9403              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9404              (from (if (get-text-property beg gnus-mouse-face-prop)
9405                        beg
9406                      (or (next-single-property-change
9407                           beg gnus-mouse-face-prop nil end)
9408                          beg)))
9409              (to
9410               (if (= from end)
9411                   (- from 2)
9412                 (or (next-single-property-change
9413                      from gnus-mouse-face-prop nil end)
9414                     end))))
9415         ;; If no mouse-face prop on line we will have to = from = end,
9416         ;; so we highlight the entire line instead.
9417         (when (= (+ to 2) from)
9418           (setq from beg)
9419           (setq to end))
9420         (if gnus-newsgroup-selected-overlay
9421             ;; Move old overlay.
9422             (gnus-move-overlay
9423              gnus-newsgroup-selected-overlay from to (current-buffer))
9424           ;; Create new overlay.
9425           (gnus-overlay-put
9426            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9427            'face gnus-summary-selected-face))))))
9428
9429 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9430 (defun gnus-summary-highlight-line ()
9431   "Highlight current line according to `gnus-summary-highlight'."
9432   (let* ((list gnus-summary-highlight)
9433          (p (point))
9434          (end (progn (end-of-line) (point)))
9435          ;; now find out where the line starts and leave point there.
9436          (beg (progn (beginning-of-line) (point)))
9437          (article (gnus-summary-article-number))
9438          (score (or (cdr (assq (or article gnus-current-article)
9439                                gnus-newsgroup-scored))
9440                     gnus-summary-default-score 0))
9441          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9442          (inhibit-read-only t))
9443     ;; Eval the cars of the lists until we find a match.
9444     (let ((default gnus-summary-default-score))
9445       (while (and list
9446                   (not (eval (caar list))))
9447         (setq list (cdr list))))
9448     (let ((face (cdar list)))
9449       (unless (eq face (get-text-property beg 'face))
9450         (gnus-put-text-property-excluding-characters-with-faces
9451          beg end 'face
9452          (setq face (if (boundp face) (symbol-value face) face)))
9453         (when gnus-summary-highlight-line-function
9454           (funcall gnus-summary-highlight-line-function article face))))
9455     (goto-char p)))
9456
9457 (defun gnus-update-read-articles (group unread &optional compute)
9458   "Update the list of read articles in GROUP."
9459   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9460          (entry (gnus-gethash group gnus-newsrc-hashtb))
9461          (info (nth 2 entry))
9462          (prev 1)
9463          (unread (sort (copy-sequence unread) '<))
9464          read)
9465     (if (or (not info) (not active))
9466         ;; There is no info on this group if it was, in fact,
9467         ;; killed.  Gnus stores no information on killed groups, so
9468         ;; there's nothing to be done.
9469         ;; One could store the information somewhere temporarily,
9470         ;; perhaps...  Hmmm...
9471         ()
9472       ;; Remove any negative articles numbers.
9473       (while (and unread (< (car unread) 0))
9474         (setq unread (cdr unread)))
9475       ;; Remove any expired article numbers
9476       (while (and unread (< (car unread) (car active)))
9477         (setq unread (cdr unread)))
9478       ;; Compute the ranges of read articles by looking at the list of
9479       ;; unread articles.
9480       (while unread
9481         (when (/= (car unread) prev)
9482           (push (if (= prev (1- (car unread))) prev
9483                   (cons prev (1- (car unread))))
9484                 read))
9485         (setq prev (1+ (car unread)))
9486         (setq unread (cdr unread)))
9487       (when (<= prev (cdr active))
9488         (push (cons prev (cdr active)) read))
9489       (setq read (if (> (length read) 1) (nreverse read) read))
9490       (if compute
9491           read
9492         (save-excursion
9493           (set-buffer gnus-group-buffer)
9494           (gnus-undo-register
9495             `(progn
9496                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9497                (gnus-info-set-read ',info ',(gnus-info-read info))
9498                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9499                (gnus-group-update-group ,group t))))
9500         ;; Propagate the read marks to the backend.
9501         (if (gnus-check-backend-function 'request-set-mark group)
9502             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9503                   (add (gnus-remove-from-range read (gnus-info-read info))))
9504               (when (or add del)
9505                 (unless (gnus-check-group group)
9506                   (error "Can't open server for %s" group))
9507                 (gnus-request-set-mark
9508                  group (delq nil (list (if add (list add 'add '(read)))
9509                                        (if del (list del 'del '(read)))))))))
9510         ;; Enter this list into the group info.
9511         (gnus-info-set-read info read)
9512         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9513         (gnus-get-unread-articles-in-group info (gnus-active group))
9514         t))))
9515
9516 (defun gnus-offer-save-summaries ()
9517   "Offer to save all active summary buffers."
9518   (save-excursion
9519     (let ((buflist (buffer-list))
9520           buffers bufname)
9521       ;; Go through all buffers and find all summaries.
9522       (while buflist
9523         (and (setq bufname (buffer-name (car buflist)))
9524              (string-match "Summary" bufname)
9525              (save-excursion
9526                (set-buffer bufname)
9527                ;; We check that this is, indeed, a summary buffer.
9528                (and (eq major-mode 'gnus-summary-mode)
9529                     ;; Also make sure this isn't bogus.
9530                     gnus-newsgroup-prepared
9531                     ;; Also make sure that this isn't a dead summary buffer.
9532                     (not gnus-dead-summary-mode)))
9533              (push bufname buffers))
9534         (setq buflist (cdr buflist)))
9535       ;; Go through all these summary buffers and offer to save them.
9536       (when buffers
9537         (map-y-or-n-p
9538          "Update summary buffer %s? "
9539          (lambda (buf)
9540            (switch-to-buffer buf)
9541            (gnus-summary-exit))
9542          buffers)))))
9543
9544 (defun gnus-summary-setup-default-charset ()
9545   "Setup newsgroup default charset."
9546   (if (equal gnus-newsgroup-name "nndraft:drafts")
9547       (setq gnus-newsgroup-charset nil)
9548     (let* ((name (and gnus-newsgroup-name
9549                       (gnus-group-real-name gnus-newsgroup-name)))
9550            (ignored-charsets 
9551             (or gnus-newsgroup-ephemeral-ignored-charsets
9552                 (append
9553                  (and gnus-newsgroup-name
9554                       (or (gnus-group-find-parameter gnus-newsgroup-name
9555                                                      'ignored-charsets t)
9556                           (let ((alist gnus-group-ignored-charsets-alist)
9557                                 elem (charsets nil))
9558                             (while (setq elem (pop alist))
9559                               (when (and name
9560                                          (string-match (car elem) name))
9561                                 (setq alist nil
9562                                       charsets (cdr elem))))
9563                             charsets))))
9564                 gnus-newsgroup-ignored-charsets)))
9565       (setq gnus-newsgroup-charset
9566             (or gnus-newsgroup-ephemeral-charset
9567                 (and gnus-newsgroup-name
9568                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9569                          (let ((alist gnus-group-charset-alist)
9570                                elem charset)
9571                            (while (setq elem (pop alist))
9572                              (when (and name
9573                                         (string-match (car elem) name))
9574                                (setq alist nil
9575                                      charset (cadr elem))))
9576                            charset)))
9577                 gnus-default-charset))
9578       (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9579            ignored-charsets))))
9580
9581 ;;;
9582 ;;; Mime Commands
9583 ;;;
9584
9585 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9586   "Display the current article buffer fully MIME-buttonized.
9587 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9588 treated as multipart/mixed."
9589   (interactive "P")
9590   (require 'gnus-art)
9591   (let ((gnus-unbuttonized-mime-types nil)
9592         (gnus-mime-display-multipart-as-mixed show-all-parts))
9593     (gnus-summary-show-article)))
9594
9595 (defun gnus-summary-repair-multipart (article)
9596   "Add a Content-Type header to a multipart article without one."
9597   (interactive (list (gnus-summary-article-number)))
9598   (gnus-with-article article
9599     (message-narrow-to-head)
9600     (goto-char (point-max))
9601     (widen)
9602     (when (search-forward "\n--" nil t)
9603       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9604         (message-narrow-to-head)
9605         (message-remove-header "Mime-Version")
9606         (message-remove-header "Content-Type")
9607         (goto-char (point-max))
9608         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9609                         separator))
9610         (insert "Mime-Version: 1.0\n")
9611         (widen))))
9612   (let (gnus-mark-article-hook)
9613     (gnus-summary-select-article t t nil article)))
9614
9615 (defun gnus-summary-toggle-display-buttonized ()
9616   "Toggle the buttonizing of the article buffer."
9617   (interactive)
9618   (require 'gnus-art)
9619   (if (setq gnus-inhibit-mime-unbuttonizing
9620             (not gnus-inhibit-mime-unbuttonizing))
9621       (let ((gnus-unbuttonized-mime-types nil))
9622         (gnus-summary-show-article))
9623     (gnus-summary-show-article)))
9624
9625 ;;;
9626 ;;; with article
9627 ;;;
9628
9629 (defmacro gnus-with-article (article &rest forms)
9630   "Select ARTICLE and perform FORMS in the original article buffer.
9631 Then replace the article with the result."
9632   `(progn
9633      ;; We don't want the article to be marked as read.
9634      (let (gnus-mark-article-hook)
9635        (gnus-summary-select-article t t nil ,article))
9636      (set-buffer gnus-original-article-buffer)
9637      ,@forms
9638      (if (not (gnus-check-backend-function
9639                'request-replace-article (car gnus-article-current)))
9640          (gnus-message 5 "Read-only group; not replacing")
9641        (unless (gnus-request-replace-article
9642                 ,article (car gnus-article-current)
9643                 (current-buffer) t)
9644          (error "Couldn't replace article")))
9645      ;; The cache and backlog have to be flushed somewhat.
9646      (when gnus-keep-backlog
9647        (gnus-backlog-remove-article
9648         (car gnus-article-current) (cdr gnus-article-current)))
9649      (when gnus-use-cache
9650        (gnus-cache-update-article
9651         (car gnus-article-current) (cdr gnus-article-current)))))
9652
9653 (put 'gnus-with-article 'lisp-indent-function 1)
9654 (put 'gnus-with-article 'edebug-form-spec '(form body))
9655
9656 ;;;
9657 ;;; Generic summary marking commands
9658 ;;;
9659
9660 (defvar gnus-summary-marking-alist
9661   '((read gnus-del-mark "d")
9662     (unread gnus-unread-mark "u")
9663     (ticked gnus-ticked-mark "!")
9664     (dormant gnus-dormant-mark "?")
9665     (expirable gnus-expirable-mark "e"))
9666   "An alist of names/marks/keystrokes.")
9667
9668 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9669 (defvar gnus-summary-mark-map)
9670
9671 (defun gnus-summary-make-all-marking-commands ()
9672   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9673   (dolist (elem gnus-summary-marking-alist)
9674     (apply 'gnus-summary-make-marking-command elem)))
9675
9676 (defun gnus-summary-make-marking-command (name mark keystroke)
9677   (let ((map (make-sparse-keymap)))
9678     (define-key gnus-summary-generic-mark-map keystroke map)
9679     (dolist (lway `((next "next" next nil "n")
9680                     (next-unread "next unread" next t "N")
9681                     (prev "previous" prev nil "p")
9682                     (prev-unread "previous unread" prev t "P")
9683                     (nomove "" nil nil ,keystroke)))
9684       (let ((func (gnus-summary-make-marking-command-1
9685                    mark (car lway) lway name)))
9686         (setq func (eval func))
9687         (define-key map (nth 4 lway) func)))))
9688       
9689 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9690   `(defun ,(intern
9691             (format "gnus-summary-put-mark-as-%s%s"
9692                     name (if (eq way 'nomove)
9693                              ""
9694                            (concat "-" (symbol-name way)))))
9695      (n)
9696      ,(format
9697        "Mark the current article as %s%s.
9698 If N, the prefix, then repeat N times.
9699 If N is negative, move in reverse order.
9700 The difference between N and the actual number of articles marked is
9701 returned."
9702        name (cadr lway))
9703      (interactive "p")
9704      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9705     
9706 (defun gnus-summary-generic-mark (n mark move unread)
9707   "Mark N articles with MARK."
9708   (unless (eq major-mode 'gnus-summary-mode)
9709     (error "This command can only be used in the summary buffer"))
9710   (gnus-summary-show-thread)
9711   (let ((nummove
9712          (cond
9713           ((eq move 'next) 1)
9714           ((eq move 'prev) -1)
9715           (t 0))))
9716     (if (zerop nummove)
9717         (setq n 1)
9718       (when (< n 0)
9719         (setq n (abs n)
9720               nummove (* -1 nummove))))
9721     (while (and (> n 0)
9722                 (gnus-summary-mark-article nil mark)
9723                 (zerop (gnus-summary-next-subject nummove unread t)))
9724       (setq n (1- n)))
9725     (when (/= 0 n)
9726       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9727     (gnus-summary-recenter)
9728     (gnus-summary-position-point)
9729     (gnus-set-mode-line 'summary)
9730     n))
9731
9732 (gnus-summary-make-all-marking-commands)
9733
9734 (gnus-ems-redefine)
9735
9736 (provide 'gnus-sum)
9737
9738 (run-hooks 'gnus-sum-load-hook)
9739
9740 ;;; gnus-sum.el ends here