Support `e' in nndraft:drafts.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
40
41 (defcustom gnus-kill-summary-on-exit t
42   "*If non-nil, kill the summary buffer when you exit from it.
43 If nil, the summary will become a \"*Dead Summary*\" buffer, and
44 it will be killed sometime later."
45   :group 'gnus-summary-exit
46   :type 'boolean)
47
48 (defcustom gnus-fetch-old-headers nil
49   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
50 If an unread article in the group refers to an older, already read (or
51 just marked as read) article, the old article will not normally be
52 displayed in the Summary buffer.  If this variable is non-nil, Gnus
53 will attempt to grab the headers to the old articles, and thereby
54 build complete threads.  If it has the value `some', only enough
55 headers to connect otherwise loose threads will be displayed.  This
56 variable can also be a number.  In that case, no more than that number
57 of old headers will be fetched.  If it has the value `invisible', all
58 old headers will be fetched, but none will be displayed.
59
60 The server has to support NOV for any of this to work."
61   :group 'gnus-thread
62   :type '(choice (const :tag "off" nil)
63                  (const some)
64                  number
65                  (sexp :menu-tag "other" t)))
66
67 (defcustom gnus-refer-thread-limit 200
68   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
69 If t, fetch all the available old headers."
70   :group 'gnus-thread
71   :type '(choice number
72                  (sexp :menu-tag "other" t)))
73
74 (defcustom gnus-summary-make-false-root 'adopt
75   "*nil means that Gnus won't gather loose threads.
76 If the root of a thread has expired or been read in a previous
77 session, the information necessary to build a complete thread has been
78 lost.  Instead of having many small sub-threads from this original thread
79 scattered all over the summary buffer, Gnus can gather them.
80
81 If non-nil, Gnus will try to gather all loose sub-threads from an
82 original thread into one large thread.
83
84 If this variable is non-nil, it should be one of `none', `adopt',
85 `dummy' or `empty'.
86
87 If this variable is `none', Gnus will not make a false root, but just
88 present the sub-threads after another.
89 If this variable is `dummy', Gnus will create a dummy root that will
90 have all the sub-threads as children.
91 If this variable is `adopt', Gnus will make one of the \"children\"
92 the parent and mark all the step-children as such.
93 If this variable is `empty', the \"children\" are printed with empty
94 subject fields.  (Or rather, they will be printed with a string
95 given by the `gnus-summary-same-subject' variable.)"
96   :group 'gnus-thread
97   :type '(choice (const :tag "off" nil)
98                  (const none)
99                  (const dummy)
100                  (const adopt)
101                  (const empty)))
102
103 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
104   "*A regexp to match subjects to be excluded from loose thread gathering.
105 As loose thread gathering is done on subjects only, that means that
106 there can be many false gatherings performed.  By rooting out certain
107 common subjects, gathering might become saner."
108   :group 'gnus-thread
109   :type 'regexp)
110
111 (defcustom gnus-summary-gather-subject-limit nil
112   "*Maximum length of subject comparisons when gathering loose threads.
113 Use nil to compare full subjects.  Setting this variable to a low
114 number will help gather threads that have been corrupted by
115 newsreaders chopping off subject lines, but it might also mean that
116 unrelated articles that have subject that happen to begin with the
117 same few characters will be incorrectly gathered.
118
119 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
120 comparing subjects."
121   :group 'gnus-thread
122   :type '(choice (const :tag "off" nil)
123                  (const fuzzy)
124                  (sexp :menu-tag "on" t)))
125
126 (defcustom gnus-simplify-subject-functions nil
127   "List of functions taking a string argument that simplify subjects.
128 The functions are applied recursively.
129
130 Useful functions to put in this list include: `gnus-simplify-subject-re',
131 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
132   :group 'gnus-thread
133   :type '(repeat function))
134
135 (defcustom gnus-simplify-ignored-prefixes nil
136   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
137   :group 'gnus-thread
138   :type '(choice (const :tag "off" nil)
139                  regexp))
140
141 (defcustom gnus-build-sparse-threads nil
142   "*If non-nil, fill in the gaps in threads.
143 If `some', only fill in the gaps that are needed to tie loose threads
144 together.  If `more', fill in all leaf nodes that Gnus can find.  If
145 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
146   :group 'gnus-thread
147   :type '(choice (const :tag "off" nil)
148                  (const some)
149                  (const more)
150                  (sexp :menu-tag "all" t)))
151
152 (defcustom gnus-summary-thread-gathering-function
153   'gnus-gather-threads-by-subject
154   "*Function used for gathering loose threads.
155 There are two pre-defined functions: `gnus-gather-threads-by-subject',
156 which only takes Subjects into consideration; and
157 `gnus-gather-threads-by-references', which compared the References
158 headers of the articles to find matches."
159   :group 'gnus-thread
160   :type '(radio (function-item gnus-gather-threads-by-subject)
161                 (function-item gnus-gather-threads-by-references)
162                 (function :tag "other")))
163
164 (defcustom gnus-summary-same-subject ""
165   "*String indicating that the current article has the same subject as the previous.
166 This variable will only be used if the value of
167 `gnus-summary-make-false-root' is `empty'."
168   :group 'gnus-summary-format
169   :type 'string)
170
171 (defcustom gnus-summary-goto-unread t
172   "*If t, many commands will go to the next unread article.
173 This applies to marking commands as well as other commands that
174 \"naturally\" select the next article, like, for instance, `SPC' at
175 the end of an article.
176
177 If nil, the marking commands do NOT go to the next unread article
178 (they go to the next article instead).  If `never', commands that
179 usually go to the next unread article, will go to the next article,
180 whether it is read or not."
181   :group 'gnus-summary-marks
182   :link '(custom-manual "(gnus)Setting Marks")
183   :type '(choice (const :tag "off" nil)
184                  (const never)
185                  (sexp :menu-tag "on" t)))
186
187 (defcustom gnus-summary-default-score 0
188   "*Default article score level.
189 All scores generated by the score files will be added to this score.
190 If this variable is nil, scoring will be disabled."
191   :group 'gnus-score-default
192   :type '(choice (const :tag "disable")
193                  integer))
194
195 (defcustom gnus-summary-zcore-fuzz 0
196   "*Fuzziness factor for the zcore in the summary buffer.
197 Articles with scores closer than this to `gnus-summary-default-score'
198 will not be marked."
199   :group 'gnus-summary-format
200   :type 'integer)
201
202 (defcustom gnus-simplify-subject-fuzzy-regexp nil
203   "*Strings to be removed when doing fuzzy matches.
204 This can either be a regular expression or list of regular expressions
205 that will be removed from subject strings if fuzzy subject
206 simplification is selected."
207   :group 'gnus-thread
208   :type '(repeat regexp))
209
210 (defcustom gnus-show-threads t
211   "*If non-nil, display threads in summary mode."
212   :group 'gnus-thread
213   :type 'boolean)
214
215 (defcustom gnus-thread-hide-subtree nil
216   "*If non-nil, hide all threads initially.
217 If threads are hidden, you have to run the command
218 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
219 to expose hidden threads."
220   :group 'gnus-thread
221   :type 'boolean)
222
223 (defcustom gnus-thread-hide-killed t
224   "*If non-nil, hide killed threads automatically."
225   :group 'gnus-thread
226   :type 'boolean)
227
228 (defcustom gnus-thread-ignore-subject t
229   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
230 If nil, articles that have different subjects from their parents will
231 start separate threads."
232   :group 'gnus-thread
233   :type 'boolean)
234
235 (defcustom gnus-thread-operation-ignore-subject t
236   "*If non-nil, subjects will be ignored when doing thread commands.
237 This affects commands like `gnus-summary-kill-thread' and
238 `gnus-summary-lower-thread'.
239
240 If this variable is nil, articles in the same thread with different
241 subjects will not be included in the operation in question.  If this
242 variable is `fuzzy', only articles that have subjects that are fuzzily
243 equal will be included."
244   :group 'gnus-thread
245   :type '(choice (const :tag "off" nil)
246                  (const fuzzy)
247                  (sexp :tag "on" t)))
248
249 (defcustom gnus-thread-indent-level 4
250   "*Number that says how much each sub-thread should be indented."
251   :group 'gnus-thread
252   :type 'integer)
253
254 (defcustom gnus-auto-extend-newsgroup t
255   "*If non-nil, extend newsgroup forward and backward when requested."
256   :group 'gnus-summary-choose
257   :type 'boolean)
258
259 (defcustom gnus-auto-select-first t
260   "*If nil, don't select the first unread article when entering a group.
261 If this variable is `best', select the highest-scored unread article
262 in the group.  If t, select the first unread article.
263
264 This variable can also be a function to place point on a likely
265 subject line.  Useful values include `gnus-summary-first-unread-subject',
266 `gnus-summary-first-unread-article' and
267 `gnus-summary-best-unread-article'.
268
269 If you want to prevent automatic selection of the first unread article
270 in some newsgroups, set the variable to nil in
271 `gnus-select-group-hook'."
272   :group 'gnus-group-select
273   :type '(choice (const :tag "none" nil)
274                  (const best)
275                  (sexp :menu-tag "first" t)
276                  (function-item gnus-summary-first-unread-subject)
277                  (function-item gnus-summary-first-unread-article)
278                  (function-item gnus-summary-best-unread-article)))
279
280 (defcustom gnus-auto-select-next t
281   "*If non-nil, offer to go to the next group from the end of the previous.
282 If the value is t and the next newsgroup is empty, Gnus will exit
283 summary mode and go back to group mode.  If the value is neither nil
284 nor t, Gnus will select the following unread newsgroup.  In
285 particular, if the value is the symbol `quietly', the next unread
286 newsgroup will be selected without any confirmation, and if it is
287 `almost-quietly', the next group will be selected without any
288 confirmation if you are located on the last article in the group.
289 Finally, if this variable is `slightly-quietly', the `Z n' command
290 will go to the next group without confirmation."
291   :group 'gnus-summary-maneuvering
292   :type '(choice (const :tag "off" nil)
293                  (const quietly)
294                  (const almost-quietly)
295                  (const slightly-quietly)
296                  (sexp :menu-tag "on" t)))
297
298 (defcustom gnus-auto-select-same nil
299   "*If non-nil, select the next article with the same subject.
300 If there are no more articles with the same subject, go to
301 the first unread article."
302   :group 'gnus-summary-maneuvering
303   :type 'boolean)
304
305 (defcustom gnus-summary-check-current nil
306   "*If non-nil, consider the current article when moving.
307 The \"unread\" movement commands will stay on the same line if the
308 current article is unread."
309   :group 'gnus-summary-maneuvering
310   :type 'boolean)
311
312 (defcustom gnus-auto-center-summary t
313   "*If non-nil, always center the current summary buffer.
314 In particular, if `vertical' do only vertical recentering.  If non-nil
315 and non-`vertical', do both horizontal and vertical recentering."
316   :group 'gnus-summary-maneuvering
317   :type '(choice (const :tag "none" nil)
318                  (const vertical)
319                  (integer :tag "height")
320                  (sexp :menu-tag "both" t)))
321
322 (defcustom gnus-show-all-headers nil
323   "*If non-nil, don't hide any headers."
324   :group 'gnus-article-hiding
325   :group 'gnus-article-headers
326   :type 'boolean)
327
328 (defcustom gnus-summary-ignore-duplicates nil
329   "*If non-nil, ignore articles with identical Message-ID headers."
330   :group 'gnus-summary
331   :type 'boolean)
332
333 (defcustom gnus-single-article-buffer t
334   "*If non-nil, display all articles in the same buffer.
335 If nil, each group will get its own article buffer."
336   :group 'gnus-article-various
337   :type 'boolean)
338
339 (defcustom gnus-break-pages t
340   "*If non-nil, do page breaking on articles.
341 The page delimiter is specified by the `gnus-page-delimiter'
342 variable."
343   :group 'gnus-article-various
344   :type 'boolean)
345
346 (defcustom gnus-move-split-methods nil
347   "*Variable used to suggest where articles are to be moved to.
348 It uses the same syntax as the `gnus-split-methods' variable."
349   :group 'gnus-summary-mail
350   :type '(repeat (choice (list :value (fun) function)
351                          (cons :value ("" "") regexp (repeat string))
352                          (sexp :value nil))))
353
354 (defcustom gnus-unread-mark ?  ;Whitespace
355   "*Mark used for unread articles."
356   :group 'gnus-summary-marks
357   :type 'character)
358
359 (defcustom gnus-ticked-mark ?!
360   "*Mark used for ticked articles."
361   :group 'gnus-summary-marks
362   :type 'character)
363
364 (defcustom gnus-dormant-mark ??
365   "*Mark used for dormant articles."
366   :group 'gnus-summary-marks
367   :type 'character)
368
369 (defcustom gnus-del-mark ?r
370   "*Mark used for del'd articles."
371   :group 'gnus-summary-marks
372   :type 'character)
373
374 (defcustom gnus-read-mark ?R
375   "*Mark used for read articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-expirable-mark ?E
380   "*Mark used for expirable articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-killed-mark ?K
385   "*Mark used for killed articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-souped-mark ?F
390   "*Mark used for killed articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-kill-file-mark ?X
395   "*Mark used for articles killed by kill files."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-low-score-mark ?Y
400   "*Mark used for articles with a low score."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-catchup-mark ?C
405   "*Mark used for articles that are caught up."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-replied-mark ?A
410   "*Mark used for articles that have been replied to."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-cached-mark ?*
415   "*Mark used for articles that are in the cache."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-saved-mark ?S
420   "*Mark used for articles that have been saved to."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-ancient-mark ?O
425   "*Mark used for ancient articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-sparse-mark ?Q
430   "*Mark used for sparsely reffed articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-canceled-mark ?G
435   "*Mark used for canceled articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-duplicate-mark ?M
440   "*Mark used for duplicate articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-undownloaded-mark ?@
445   "*Mark used for articles that weren't downloaded."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-downloadable-mark ?%
450   "*Mark used for articles that are to be downloaded."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-unsendable-mark ?=
455   "*Mark used for articles that won't be sent."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-score-over-mark ?+
460   "*Score mark used for articles with high scores."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-score-below-mark ?-
465   "*Score mark used for articles with low scores."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-empty-thread-mark ?  ;Whitespace
470   "*There is no thread under the article."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-not-empty-thread-mark ?=
475   "*There is a thread under the article."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-view-pseudo-asynchronously nil
480   "*If non-nil, Gnus will view pseudo-articles asynchronously."
481   :group 'gnus-extract-view
482   :type 'boolean)
483
484 (defcustom gnus-auto-expirable-marks
485   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
486         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
487         gnus-souped-mark gnus-duplicate-mark)
488   "*The list of marks converted into expiration if a group is auto-expirable."
489   :group 'gnus-summary
490   :type '(repeat character))
491
492 (defcustom gnus-inhibit-user-auto-expire t
493   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
494   :group 'gnus-summary
495   :type 'boolean)
496
497 (defcustom gnus-view-pseudos nil
498   "*If `automatic', pseudo-articles will be viewed automatically.
499 If `not-confirm', pseudos will be viewed automatically, and the user
500 will not be asked to confirm the command."
501   :group 'gnus-extract-view
502   :type '(choice (const :tag "off" nil)
503                  (const automatic)
504                  (const not-confirm)))
505
506 (defcustom gnus-view-pseudos-separately t
507   "*If non-nil, one pseudo-article will be created for each file to be viewed.
508 If nil, all files that use the same viewing command will be given as a
509 list of parameters to that command."
510   :group 'gnus-extract-view
511   :type 'boolean)
512
513 (defcustom gnus-insert-pseudo-articles t
514   "*If non-nil, insert pseudo-articles when decoding articles."
515   :group 'gnus-extract-view
516   :type 'boolean)
517
518 (defcustom gnus-summary-dummy-line-format
519   "  %(:                          :%) %S\n"
520   "*The format specification for the dummy roots in the summary buffer.
521 It works along the same lines as a normal formatting string,
522 with some simple extensions.
523
524 %S  The subject"
525   :group 'gnus-threading
526   :type 'string)
527
528 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
529   "*The format specification for the summary mode line.
530 It works along the same lines as a normal formatting string,
531 with some simple extensions:
532
533 %G  Group name
534 %p  Unprefixed group name
535 %A  Current article number
536 %z  Current article score
537 %V  Gnus version
538 %U  Number of unread articles in the group
539 %e  Number of unselected articles in the group
540 %Z  A string with unread/unselected article counts
541 %g  Shortish group name
542 %S  Subject of the current article
543 %u  User-defined spec
544 %s  Current score file name
545 %d  Number of dormant articles
546 %r  Number of articles that have been marked as read in this session
547 %E  Number of articles expunged by the score files"
548   :group 'gnus-summary-format
549   :type 'string)
550
551 (defcustom gnus-list-identifiers nil
552   "Regexp that matches list identifiers to be removed from subject.
553 This can also be a list of regexps."
554   :group 'gnus-summary-format
555   :group 'gnus-article-hiding
556   :type '(choice (const :tag "none" nil)
557                  (regexp :value ".*")
558                  (repeat :value (".*") regexp)))
559
560 (defcustom gnus-summary-mark-below 0
561   "*Mark all articles with a score below this variable as read.
562 This variable is local to each summary buffer and usually set by the
563 score file."
564   :group 'gnus-score-default
565   :type 'integer)
566
567 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
568   "*List of functions used for sorting articles in the summary buffer.
569 This variable is only used when not using a threaded display."
570   :group 'gnus-summary-sort
571   :type '(repeat (choice (function-item gnus-article-sort-by-number)
572                          (function-item gnus-article-sort-by-author)
573                          (function-item gnus-article-sort-by-subject)
574                          (function-item gnus-article-sort-by-date)
575                          (function-item gnus-article-sort-by-score)
576                          (function :tag "other"))))
577
578 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
579   "*List of functions used for sorting threads in the summary buffer.
580 By default, threads are sorted by article number.
581
582 Each function takes two threads and return non-nil if the first thread
583 should be sorted before the other.  If you use more than one function,
584 the primary sort function should be the last.  You should probably
585 always include `gnus-thread-sort-by-number' in the list of sorting
586 functions -- preferably first.
587
588 Ready-made functions include `gnus-thread-sort-by-number',
589 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
590 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
591 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
592   :group 'gnus-summary-sort
593   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
594                          (function-item gnus-thread-sort-by-author)
595                          (function-item gnus-thread-sort-by-subject)
596                          (function-item gnus-thread-sort-by-date)
597                          (function-item gnus-thread-sort-by-score)
598                          (function-item gnus-thread-sort-by-total-score)
599                          (function :tag "other"))))
600
601 (defcustom gnus-thread-score-function '+
602   "*Function used for calculating the total score of a thread.
603
604 The function is called with the scores of the article and each
605 subthread and should then return the score of the thread.
606
607 Some functions you can use are `+', `max', or `min'."
608   :group 'gnus-summary-sort
609   :type 'function)
610
611 (defcustom gnus-summary-expunge-below nil
612   "All articles that have a score less than this variable will be expunged.
613 This variable is local to the summary buffers."
614   :group 'gnus-score-default
615   :type '(choice (const :tag "off" nil)
616                  integer))
617
618 (defcustom gnus-thread-expunge-below nil
619   "All threads that have a total score less than this variable will be expunged.
620 See `gnus-thread-score-function' for en explanation of what a
621 \"thread score\" is.
622
623 This variable is local to the summary buffers."
624   :group 'gnus-threading
625   :group 'gnus-score-default
626   :type '(choice (const :tag "off" nil)
627                  integer))
628
629 (defcustom gnus-summary-mode-hook nil
630   "*A hook for Gnus summary mode.
631 This hook is run before any variables are set in the summary buffer."
632   :group 'gnus-summary-various
633   :type 'hook)
634
635 (defcustom gnus-summary-menu-hook nil
636   "*Hook run after the creation of the summary mode menu."
637   :group 'gnus-summary-visual
638   :type 'hook)
639
640 (defcustom gnus-summary-exit-hook nil
641   "*A hook called on exit from the summary buffer.
642 It will be called with point in the group buffer."
643   :group 'gnus-summary-exit
644   :type 'hook)
645
646 (defcustom gnus-summary-prepare-hook nil
647   "*A hook called after the summary buffer has been generated.
648 If you want to modify the summary buffer, you can use this hook."
649   :group 'gnus-summary-various
650   :type 'hook)
651
652 (defcustom gnus-summary-prepared-hook nil
653   "*A hook called as the last thing after the summary buffer has been generated."
654   :group 'gnus-summary-various
655   :type 'hook)
656
657 (defcustom gnus-summary-generate-hook nil
658   "*A hook run just before generating the summary buffer.
659 This hook is commonly used to customize threading variables and the
660 like."
661   :group 'gnus-summary-various
662   :type 'hook)
663
664 (defcustom gnus-select-group-hook nil
665   "*A hook called when a newsgroup is selected.
666
667 If you'd like to simplify subjects like the
668 `gnus-summary-next-same-subject' command does, you can use the
669 following hook:
670
671  (setq gnus-select-group-hook
672       (list
673         (lambda ()
674           (mapcar (lambda (header)
675                      (mail-header-set-subject
676                       header
677                       (gnus-simplify-subject
678                        (mail-header-subject header) 're-only)))
679                   gnus-newsgroup-headers))))"
680   :group 'gnus-group-select
681   :type 'hook)
682
683 (defcustom gnus-select-article-hook nil
684   "*A hook called when an article is selected."
685   :group 'gnus-summary-choose
686   :type 'hook)
687
688 (defcustom gnus-visual-mark-article-hook
689   (list 'gnus-highlight-selected-summary)
690   "*Hook run after selecting an article in the summary buffer.
691 It is meant to be used for highlighting the article in some way.  It
692 is not run if `gnus-visual' is nil."
693   :group 'gnus-summary-visual
694   :type 'hook)
695
696 (defcustom gnus-parse-headers-hook nil
697   "*A hook called before parsing the headers."
698   :group 'gnus-various
699   :type 'hook)
700
701 (defcustom gnus-exit-group-hook nil
702   "*A hook called when exiting summary mode.
703 This hook is not called from the non-updating exit commands like `Q'."
704   :group 'gnus-various
705   :type 'hook)
706
707 (defcustom gnus-summary-update-hook
708   (list 'gnus-summary-highlight-line)
709   "*A hook called when a summary line is changed.
710 The hook will not be called if `gnus-visual' is nil.
711
712 The default function `gnus-summary-highlight-line' will
713 highlight the line according to the `gnus-summary-highlight'
714 variable."
715   :group 'gnus-summary-visual
716   :type 'hook)
717
718 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
719   "*A hook called when an article is selected for the first time.
720 The hook is intended to mark an article as read (or unread)
721 automatically when it is selected."
722   :group 'gnus-summary-choose
723   :type 'hook)
724
725 (defcustom gnus-group-no-more-groups-hook nil
726   "*A hook run when returning to group mode having no more (unread) groups."
727   :group 'gnus-group-select
728   :type 'hook)
729
730 (defcustom gnus-ps-print-hook nil
731   "*A hook run before ps-printing something from Gnus."
732   :group 'gnus-summary
733   :type 'hook)
734
735 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
736   "Face used for highlighting the current article in the summary buffer."
737   :group 'gnus-summary-visual
738   :type 'face)
739
740 (defcustom gnus-summary-highlight
741   '(((= mark gnus-canceled-mark)
742      . gnus-summary-cancelled-face)
743     ((and (> score default)
744           (or (= mark gnus-dormant-mark)
745               (= mark gnus-ticked-mark)))
746      . gnus-summary-high-ticked-face)
747     ((and (< score default)
748           (or (= mark gnus-dormant-mark)
749               (= mark gnus-ticked-mark)))
750      . gnus-summary-low-ticked-face)
751     ((or (= mark gnus-dormant-mark)
752          (= mark gnus-ticked-mark))
753      . gnus-summary-normal-ticked-face)
754     ((and (> score default) (= mark gnus-ancient-mark))
755      . gnus-summary-high-ancient-face)
756     ((and (< score default) (= mark gnus-ancient-mark))
757      . gnus-summary-low-ancient-face)
758     ((= mark gnus-ancient-mark)
759      . gnus-summary-normal-ancient-face)
760     ((and (> score default) (= mark gnus-unread-mark))
761      . gnus-summary-high-unread-face)
762     ((and (< score default) (= mark gnus-unread-mark))
763      . gnus-summary-low-unread-face)
764     ((= mark gnus-unread-mark)
765      . gnus-summary-normal-unread-face)
766     ((and (> score default) (memq mark (list gnus-downloadable-mark
767                                              gnus-undownloaded-mark)))
768      . gnus-summary-high-unread-face)
769     ((and (< score default) (memq mark (list gnus-downloadable-mark
770                                              gnus-undownloaded-mark)))
771      . gnus-summary-low-unread-face)
772     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
773      . gnus-summary-normal-unread-face)
774     ((> score default)
775      . gnus-summary-high-read-face)
776     ((< score default)
777      . gnus-summary-low-read-face)
778     (t
779      . gnus-summary-normal-read-face))
780   "*Controls the highlighting of summary buffer lines.
781
782 A list of (FORM . FACE) pairs.  When deciding how a a particular
783 summary line should be displayed, each form is evaluated.  The content
784 of the face field after the first true form is used.  You can change
785 how those summary lines are displayed, by editing the face field.
786
787 You can use the following variables in the FORM field.
788
789 score:   The articles score
790 default: The default article score.
791 below:   The score below which articles are automatically marked as read.
792 mark:    The articles mark."
793   :group 'gnus-summary-visual
794   :type '(repeat (cons (sexp :tag "Form" nil)
795                        face)))
796
797 (defcustom gnus-alter-header-function nil
798   "Function called to allow alteration of article header structures.
799 The function is called with one parameter, the article header vector,
800 which it may alter in any way.")
801
802 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
803   "Variable that says which function should be used to decode a string with encoded words.")
804
805 (defcustom gnus-extra-headers nil
806   "*Extra headers to parse."
807   :group 'gnus-summary
808   :type '(repeat symbol))
809
810 (defcustom gnus-ignored-from-addresses
811   (and user-mail-address (regexp-quote user-mail-address))
812   "*Regexp of From headers that may be suppressed in favor of To headers."
813   :group 'gnus-summary
814   :type 'regexp)
815
816 (defcustom gnus-group-charset-alist
817   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
818     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
819     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
820     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
821     ("^relcom\\>" koi8-r)
822     ("^fido7\\>" koi8-r)
823     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
824     ("^israel\\>" iso-8859-1)
825     ("^han\\>" euc-kr)
826     ("^alt.chinese.text.big5\\>" chinese-big5)
827     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
828     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
829     (".*" iso-8859-1))
830   "Alist of regexps (to match group names) and default charsets to be used when reading."
831   :type '(repeat (list (regexp :tag "Group")
832                        (symbol :tag "Charset")))
833   :group 'gnus-charset)
834
835 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
836   "List of charsets that should be ignored.
837 When these charsets are used in the \"charset\" parameter, the
838 default charset will be used instead."
839   :type '(repeat symbol)
840   :group 'gnus-charset)
841
842 (defcustom gnus-group-ignored-charsets-alist 
843   '(("alt\\.chinese\\.text" iso-8859-1))
844   "Alist of regexps (to match group names) and charsets that should be ignored.
845 When these charsets are used in the \"charset\" parameter, the
846 default charset will be used instead."
847   :type '(repeat (cons (regexp :tag "Group")
848                        (repeat symbol)))
849   :group 'gnus-charset)
850
851 (defcustom gnus-group-highlight-words-alist nil
852   "Alist of group regexps and highlight regexps.
853 This variable uses the same syntax as `gnus-emphasis-alist'."
854   :type '(repeat (cons (regexp :tag "Group")
855                        (repeat (list (regexp :tag "Highlight regexp")
856                                      (number :tag "Group for entire word" 0)
857                                      (number :tag "Group for displayed part" 0)
858                                      (symbol :tag "Face" 
859                                              gnus-emphasis-highlight-words)))))
860   :group 'gnus-summary-visual)
861
862 (defcustom gnus-summary-show-article-charset-alist
863   nil
864   "Alist of number and charset.
865 The article will be shown with the charset corresponding to the
866 numbered argument.
867 For example: ((1 . cn-gb-2312) (2 . big5))."
868   :type '(repeat (cons (number :tag "Argument" 1)
869                        (symbol :tag "Charset")))
870   :group 'gnus-charset)
871
872 (defcustom gnus-preserve-marks t
873   "Whether marks are preserved when moving, copying and respooling messages."
874   :type 'boolean
875   :group 'gnus-summary-marks)
876
877 (defcustom gnus-alter-articles-to-read-function nil
878   "Function to be called to alter the list of articles to be selected."
879   :type 'function
880   :group 'gnus-summary)
881
882 ;;; Internal variables
883
884 (defvar gnus-article-mime-handles nil)
885 (defvar gnus-article-decoded-p nil)
886 (defvar gnus-scores-exclude-files nil)
887 (defvar gnus-page-broken nil)
888 (defvar gnus-inhibit-mime-unbuttonizing nil)
889
890 (defvar gnus-original-article nil)
891 (defvar gnus-article-internal-prepare-hook nil)
892 (defvar gnus-newsgroup-process-stack nil)
893
894 (defvar gnus-thread-indent-array nil)
895 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
896 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
897   "Function called to sort the articles within a thread after it has been gathered together.")
898
899 ;; Avoid highlighting in kill files.
900 (defvar gnus-summary-inhibit-highlight nil)
901 (defvar gnus-newsgroup-selected-overlay nil)
902 (defvar gnus-inhibit-limiting nil)
903 (defvar gnus-newsgroup-adaptive-score-file nil)
904 (defvar gnus-current-score-file nil)
905 (defvar gnus-current-move-group nil)
906 (defvar gnus-current-copy-group nil)
907 (defvar gnus-current-crosspost-group nil)
908
909 (defvar gnus-newsgroup-dependencies nil)
910 (defvar gnus-newsgroup-adaptive nil)
911 (defvar gnus-summary-display-article-function nil)
912 (defvar gnus-summary-highlight-line-function nil
913   "Function called after highlighting a summary line.")
914
915 (defvar gnus-summary-line-format-alist
916   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
917     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
918     (?s gnus-tmp-subject-or-nil ?s)
919     (?n gnus-tmp-name ?s)
920     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
921         ?s)
922     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
923             gnus-tmp-from) ?s)
924     (?F gnus-tmp-from ?s)
925     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
926     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
927     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
928     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
929     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
930     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
931     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
932     (?L gnus-tmp-lines ?d)
933     (?I gnus-tmp-indentation ?s)
934     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
935     (?R gnus-tmp-replied ?c)
936     (?\[ gnus-tmp-opening-bracket ?c)
937     (?\] gnus-tmp-closing-bracket ?c)
938     (?\> (make-string gnus-tmp-level ? ) ?s)
939     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
940     (?i gnus-tmp-score ?d)
941     (?z gnus-tmp-score-char ?c)
942     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
943     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
944     (?U gnus-tmp-unread ?c)
945     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
946     (?t (gnus-summary-number-of-articles-in-thread
947          (and (boundp 'thread) (car thread)) gnus-tmp-level)
948         ?d)
949     (?e (gnus-summary-number-of-articles-in-thread
950          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
951         ?c)
952     (?u gnus-tmp-user-defined ?s)
953     (?P (gnus-pick-line-number) ?d))
954   "An alist of format specifications that can appear in summary lines,
955 and what variables they correspond with, along with the type of the
956 variable (string, integer, character, etc).")
957
958 (defvar gnus-summary-dummy-line-format-alist
959   `((?S gnus-tmp-subject ?s)
960     (?N gnus-tmp-number ?d)
961     (?u gnus-tmp-user-defined ?s)))
962
963 (defvar gnus-summary-mode-line-format-alist
964   `((?G gnus-tmp-group-name ?s)
965     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
966     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
967     (?A gnus-tmp-article-number ?d)
968     (?Z gnus-tmp-unread-and-unselected ?s)
969     (?V gnus-version ?s)
970     (?U gnus-tmp-unread-and-unticked ?d)
971     (?S gnus-tmp-subject ?s)
972     (?e gnus-tmp-unselected ?d)
973     (?u gnus-tmp-user-defined ?s)
974     (?d (length gnus-newsgroup-dormant) ?d)
975     (?t (length gnus-newsgroup-marked) ?d)
976     (?r (length gnus-newsgroup-reads) ?d)
977     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
978     (?E gnus-newsgroup-expunged-tally ?d)
979     (?s (gnus-current-score-file-nondirectory) ?s)))
980
981 (defvar gnus-last-search-regexp nil
982   "Default regexp for article search command.")
983
984 (defvar gnus-last-shell-command nil
985   "Default shell command on article.")
986
987 (defvar gnus-newsgroup-begin nil)
988 (defvar gnus-newsgroup-end nil)
989 (defvar gnus-newsgroup-last-rmail nil)
990 (defvar gnus-newsgroup-last-mail nil)
991 (defvar gnus-newsgroup-last-folder nil)
992 (defvar gnus-newsgroup-last-file nil)
993 (defvar gnus-newsgroup-auto-expire nil)
994 (defvar gnus-newsgroup-active nil)
995
996 (defvar gnus-newsgroup-data nil)
997 (defvar gnus-newsgroup-data-reverse nil)
998 (defvar gnus-newsgroup-limit nil)
999 (defvar gnus-newsgroup-limits nil)
1000
1001 (defvar gnus-newsgroup-unreads nil
1002   "List of unread articles in the current newsgroup.")
1003
1004 (defvar gnus-newsgroup-unselected nil
1005   "List of unselected unread articles in the current newsgroup.")
1006
1007 (defvar gnus-newsgroup-reads nil
1008   "Alist of read articles and article marks in the current newsgroup.")
1009
1010 (defvar gnus-newsgroup-expunged-tally nil)
1011
1012 (defvar gnus-newsgroup-marked nil
1013   "List of ticked articles in the current newsgroup (a subset of unread art).")
1014
1015 (defvar gnus-newsgroup-killed nil
1016   "List of ranges of articles that have been through the scoring process.")
1017
1018 (defvar gnus-newsgroup-cached nil
1019   "List of articles that come from the article cache.")
1020
1021 (defvar gnus-newsgroup-saved nil
1022   "List of articles that have been saved.")
1023
1024 (defvar gnus-newsgroup-kill-headers nil)
1025
1026 (defvar gnus-newsgroup-replied nil
1027   "List of articles that have been replied to in the current newsgroup.")
1028
1029 (defvar gnus-newsgroup-expirable nil
1030   "List of articles in the current newsgroup that can be expired.")
1031
1032 (defvar gnus-newsgroup-processable nil
1033   "List of articles in the current newsgroup that can be processed.")
1034
1035 (defvar gnus-newsgroup-downloadable nil
1036   "List of articles in the current newsgroup that can be processed.")
1037
1038 (defvar gnus-newsgroup-undownloaded nil
1039   "List of articles in the current newsgroup that haven't been downloaded..")
1040
1041 (defvar gnus-newsgroup-unsendable nil
1042   "List of articles in the current newsgroup that won't be sent.")
1043
1044 (defvar gnus-newsgroup-bookmarks nil
1045   "List of articles in the current newsgroup that have bookmarks.")
1046
1047 (defvar gnus-newsgroup-dormant nil
1048   "List of dormant articles in the current newsgroup.")
1049
1050 (defvar gnus-newsgroup-scored nil
1051   "List of scored articles in the current newsgroup.")
1052
1053 (defvar gnus-newsgroup-headers nil
1054   "List of article headers in the current newsgroup.")
1055
1056 (defvar gnus-newsgroup-threads nil)
1057
1058 (defvar gnus-newsgroup-prepared nil
1059   "Whether the current group has been prepared properly.")
1060
1061 (defvar gnus-newsgroup-ancient nil
1062   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1063
1064 (defvar gnus-newsgroup-sparse nil)
1065
1066 (defvar gnus-current-article nil)
1067 (defvar gnus-article-current nil)
1068 (defvar gnus-current-headers nil)
1069 (defvar gnus-have-all-headers nil)
1070 (defvar gnus-last-article nil)
1071 (defvar gnus-newsgroup-history nil)
1072 (defvar gnus-newsgroup-charset nil)
1073 (defvar gnus-newsgroup-ephemeral-charset nil)
1074 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1075
1076 (defconst gnus-summary-local-variables
1077   '(gnus-newsgroup-name
1078     gnus-newsgroup-begin gnus-newsgroup-end
1079     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1080     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1081     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1082     gnus-newsgroup-unselected gnus-newsgroup-marked
1083     gnus-newsgroup-reads gnus-newsgroup-saved
1084     gnus-newsgroup-replied gnus-newsgroup-expirable
1085     gnus-newsgroup-processable gnus-newsgroup-killed
1086     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1087     gnus-newsgroup-unsendable
1088     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1089     gnus-newsgroup-headers gnus-newsgroup-threads
1090     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1091     gnus-current-article gnus-current-headers gnus-have-all-headers
1092     gnus-last-article gnus-article-internal-prepare-hook
1093     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1094     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1095     gnus-thread-expunge-below
1096     gnus-score-alist gnus-current-score-file
1097     (gnus-summary-expunge-below . global)
1098     (gnus-summary-mark-below . global)
1099     gnus-newsgroup-active gnus-scores-exclude-files
1100     gnus-newsgroup-history gnus-newsgroup-ancient
1101     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1102     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1103     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1104     (gnus-newsgroup-expunged-tally . 0)
1105     gnus-cache-removable-articles gnus-newsgroup-cached
1106     gnus-newsgroup-data gnus-newsgroup-data-reverse
1107     gnus-newsgroup-limit gnus-newsgroup-limits
1108     gnus-newsgroup-charset)
1109   "Variables that are buffer-local to the summary buffers.")
1110
1111 ;; Byte-compiler warning.
1112 (defvar gnus-article-mode-map)
1113
1114 ;; MIME stuff.
1115
1116 (defvar gnus-decode-encoded-word-methods
1117   '(mail-decode-encoded-word-string)
1118   "List of methods used to decode encoded words.
1119
1120 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1121 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1122 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1123 whose names match REGEXP.
1124
1125 For example:
1126 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1127  mail-decode-encoded-word-string
1128  (\"chinese\" . rfc1843-decode-string))
1129 ")
1130
1131 (defvar gnus-decode-encoded-word-methods-cache nil)
1132
1133 (defun gnus-multi-decode-encoded-word-string (string)
1134   "Apply the functions from `gnus-encoded-word-methods' that match."
1135   (unless (and gnus-decode-encoded-word-methods-cache
1136                (eq gnus-newsgroup-name
1137                    (car gnus-decode-encoded-word-methods-cache)))
1138     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1139     (mapcar (lambda (x)
1140               (if (symbolp x)
1141                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1142                 (if (and gnus-newsgroup-name
1143                          (string-match (car x) gnus-newsgroup-name))
1144                     (nconc gnus-decode-encoded-word-methods-cache
1145                            (list (cdr x))))))
1146           gnus-decode-encoded-word-methods))
1147   (let ((xlist gnus-decode-encoded-word-methods-cache))
1148     (pop xlist)
1149     (while xlist
1150       (setq string (funcall (pop xlist) string))))
1151   string)
1152
1153 ;; Subject simplification.
1154
1155 (defun gnus-simplify-whitespace (str)
1156   "Remove excessive whitespace."
1157   (let ((mystr str))
1158     ;; Multiple spaces.
1159     (while (string-match "[ \t][ \t]+" mystr)
1160       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1161                           " "
1162                           (substring mystr (match-end 0)))))
1163     ;; Leading spaces.
1164     (when (string-match "^[ \t]+" mystr)
1165       (setq mystr (substring mystr (match-end 0))))
1166     ;; Trailing spaces.
1167     (when (string-match "[ \t]+$" mystr)
1168       (setq mystr (substring mystr 0 (match-beginning 0))))
1169     mystr))
1170
1171 (defsubst gnus-simplify-subject-re (subject)
1172   "Remove \"Re:\" from subject lines."
1173   (if (string-match "^[Rr][Ee]: *" subject)
1174       (substring subject (match-end 0))
1175     subject))
1176
1177 (defun gnus-simplify-subject (subject &optional re-only)
1178   "Remove `Re:' and words in parentheses.
1179 If RE-ONLY is non-nil, strip leading `Re:'s only."
1180   (let ((case-fold-search t))           ;Ignore case.
1181     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1182     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1183       (setq subject (substring subject (match-end 0))))
1184     ;; Remove uninteresting prefixes.
1185     (when (and (not re-only)
1186                gnus-simplify-ignored-prefixes
1187                (string-match gnus-simplify-ignored-prefixes subject))
1188       (setq subject (substring subject (match-end 0))))
1189     ;; Remove words in parentheses from end.
1190     (unless re-only
1191       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1192         (setq subject (substring subject 0 (match-beginning 0)))))
1193     ;; Return subject string.
1194     subject))
1195
1196 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1197 ;; all whitespace.
1198 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1199   (goto-char (point-min))
1200   (while (re-search-forward regexp nil t)
1201     (replace-match (or newtext ""))))
1202
1203 (defun gnus-simplify-buffer-fuzzy ()
1204   "Simplify string in the buffer fuzzily.
1205 The string in the accessible portion of the current buffer is simplified.
1206 It is assumed to be a single-line subject.
1207 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1208 matter is removed.  Additional things can be deleted by setting
1209 gnus-simplify-subject-fuzzy-regexp."
1210   (let ((case-fold-search t)
1211         (modified-tick))
1212     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1213
1214     (while (not (eq modified-tick (buffer-modified-tick)))
1215       (setq modified-tick (buffer-modified-tick))
1216       (cond
1217        ((listp gnus-simplify-subject-fuzzy-regexp)
1218         (mapcar 'gnus-simplify-buffer-fuzzy-step
1219                 gnus-simplify-subject-fuzzy-regexp))
1220        (gnus-simplify-subject-fuzzy-regexp
1221         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1222       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1223       (gnus-simplify-buffer-fuzzy-step
1224        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1225       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1226
1227     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1228     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1229     (gnus-simplify-buffer-fuzzy-step " $")
1230     (gnus-simplify-buffer-fuzzy-step "^ +")))
1231
1232 (defun gnus-simplify-subject-fuzzy (subject)
1233   "Simplify a subject string fuzzily.
1234 See `gnus-simplify-buffer-fuzzy' for details."
1235   (save-excursion
1236     (gnus-set-work-buffer)
1237     (let ((case-fold-search t))
1238       ;; Remove uninteresting prefixes.
1239       (when (and gnus-simplify-ignored-prefixes
1240                  (string-match gnus-simplify-ignored-prefixes subject))
1241         (setq subject (substring subject (match-end 0))))
1242       (insert subject)
1243       (inline (gnus-simplify-buffer-fuzzy))
1244       (buffer-string))))
1245
1246 (defsubst gnus-simplify-subject-fully (subject)
1247   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1248   (cond
1249    (gnus-simplify-subject-functions
1250     (gnus-map-function gnus-simplify-subject-functions subject))
1251    ((null gnus-summary-gather-subject-limit)
1252     (gnus-simplify-subject-re subject))
1253    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1254     (gnus-simplify-subject-fuzzy subject))
1255    ((numberp gnus-summary-gather-subject-limit)
1256     (gnus-limit-string (gnus-simplify-subject-re subject)
1257                        gnus-summary-gather-subject-limit))
1258    (t
1259     subject)))
1260
1261 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1262   "Check whether two subjects are equal.
1263 If optional argument simple-first is t, first argument is already
1264 simplified."
1265   (cond
1266    ((null simple-first)
1267     (equal (gnus-simplify-subject-fully s1)
1268            (gnus-simplify-subject-fully s2)))
1269    (t
1270     (equal s1
1271            (gnus-simplify-subject-fully s2)))))
1272
1273 (defun gnus-summary-bubble-group ()
1274   "Increase the score of the current group.
1275 This is a handy function to add to `gnus-summary-exit-hook' to
1276 increase the score of each group you read."
1277   (gnus-group-add-score gnus-newsgroup-name))
1278
1279 \f
1280 ;;;
1281 ;;; Gnus summary mode
1282 ;;;
1283
1284 (put 'gnus-summary-mode 'mode-class 'special)
1285
1286 (when t
1287   ;; Non-orthogonal keys
1288
1289   (gnus-define-keys gnus-summary-mode-map
1290     " " gnus-summary-next-page
1291     "\177" gnus-summary-prev-page
1292     [delete] gnus-summary-prev-page
1293     [backspace] gnus-summary-prev-page
1294     "\r" gnus-summary-scroll-up
1295     "\M-\r" gnus-summary-scroll-down
1296     "n" gnus-summary-next-unread-article
1297     "p" gnus-summary-prev-unread-article
1298     "N" gnus-summary-next-article
1299     "P" gnus-summary-prev-article
1300     "\M-\C-n" gnus-summary-next-same-subject
1301     "\M-\C-p" gnus-summary-prev-same-subject
1302     "\M-n" gnus-summary-next-unread-subject
1303     "\M-p" gnus-summary-prev-unread-subject
1304     "." gnus-summary-first-unread-article
1305     "," gnus-summary-best-unread-article
1306     "\M-s" gnus-summary-search-article-forward
1307     "\M-r" gnus-summary-search-article-backward
1308     "<" gnus-summary-beginning-of-article
1309     ">" gnus-summary-end-of-article
1310     "j" gnus-summary-goto-article
1311     "^" gnus-summary-refer-parent-article
1312     "\M-^" gnus-summary-refer-article
1313     "u" gnus-summary-tick-article-forward
1314     "!" gnus-summary-tick-article-forward
1315     "U" gnus-summary-tick-article-backward
1316     "d" gnus-summary-mark-as-read-forward
1317     "D" gnus-summary-mark-as-read-backward
1318     "E" gnus-summary-mark-as-expirable
1319     "\M-u" gnus-summary-clear-mark-forward
1320     "\M-U" gnus-summary-clear-mark-backward
1321     "k" gnus-summary-kill-same-subject-and-select
1322     "\C-k" gnus-summary-kill-same-subject
1323     "\M-\C-k" gnus-summary-kill-thread
1324     "\M-\C-l" gnus-summary-lower-thread
1325     "e" gnus-summary-edit-article
1326     "#" gnus-summary-mark-as-processable
1327     "\M-#" gnus-summary-unmark-as-processable
1328     "\M-\C-t" gnus-summary-toggle-threads
1329     "\M-\C-s" gnus-summary-show-thread
1330     "\M-\C-h" gnus-summary-hide-thread
1331     "\M-\C-f" gnus-summary-next-thread
1332     "\M-\C-b" gnus-summary-prev-thread
1333     [(meta down)] gnus-summary-next-thread
1334     [(meta up)] gnus-summary-prev-thread
1335     "\M-\C-u" gnus-summary-up-thread
1336     "\M-\C-d" gnus-summary-down-thread
1337     "&" gnus-summary-execute-command
1338     "c" gnus-summary-catchup-and-exit
1339     "\C-w" gnus-summary-mark-region-as-read
1340     "\C-t" gnus-summary-toggle-truncation
1341     "?" gnus-summary-mark-as-dormant
1342     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1343     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1344     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1345     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1346     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1347     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1348     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1349     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1350     "=" gnus-summary-expand-window
1351     "\C-x\C-s" gnus-summary-reselect-current-group
1352     "\M-g" gnus-summary-rescan-group
1353     "w" gnus-summary-stop-page-breaking
1354     "\C-c\C-r" gnus-summary-caesar-message
1355     "f" gnus-summary-followup
1356     "F" gnus-summary-followup-with-original
1357     "C" gnus-summary-cancel-article
1358     "r" gnus-summary-reply
1359     "R" gnus-summary-reply-with-original
1360     "\C-c\C-f" gnus-summary-mail-forward
1361     "o" gnus-summary-save-article
1362     "\C-o" gnus-summary-save-article-mail
1363     "|" gnus-summary-pipe-output
1364     "\M-k" gnus-summary-edit-local-kill
1365     "\M-K" gnus-summary-edit-global-kill
1366     ;; "V" gnus-version
1367     "\C-c\C-d" gnus-summary-describe-group
1368     "q" gnus-summary-exit
1369     "Q" gnus-summary-exit-no-update
1370     "\C-c\C-i" gnus-info-find-node
1371     gnus-mouse-2 gnus-mouse-pick-article
1372     "m" gnus-summary-mail-other-window
1373     "a" gnus-summary-post-news
1374     "x" gnus-summary-limit-to-unread
1375     "s" gnus-summary-isearch-article
1376     "t" gnus-summary-toggle-header
1377     "g" gnus-summary-show-article
1378     "l" gnus-summary-goto-last-article
1379     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1380     "\C-d" gnus-summary-enter-digest-group
1381     "\M-\C-d" gnus-summary-read-document
1382     "\M-\C-e" gnus-summary-edit-parameters
1383     "\M-\C-a" gnus-summary-customize-parameters
1384     "\C-c\C-b" gnus-bug
1385     "*" gnus-cache-enter-article
1386     "\M-*" gnus-cache-remove-article
1387     "\M-&" gnus-summary-universal-argument
1388     "\C-l" gnus-recenter
1389     "I" gnus-summary-increase-score
1390     "L" gnus-summary-lower-score
1391     "\M-i" gnus-symbolic-argument
1392     "h" gnus-summary-select-article-buffer
1393
1394     "b" gnus-article-view-part
1395     "\M-t" gnus-summary-toggle-display-buttonized
1396
1397     "V" gnus-summary-score-map
1398     "X" gnus-uu-extract-map
1399     "S" gnus-summary-send-map)
1400
1401   ;; Sort of orthogonal keymap
1402   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1403     "t" gnus-summary-tick-article-forward
1404     "!" gnus-summary-tick-article-forward
1405     "d" gnus-summary-mark-as-read-forward
1406     "r" gnus-summary-mark-as-read-forward
1407     "c" gnus-summary-clear-mark-forward
1408     " " gnus-summary-clear-mark-forward
1409     "e" gnus-summary-mark-as-expirable
1410     "x" gnus-summary-mark-as-expirable
1411     "?" gnus-summary-mark-as-dormant
1412     "b" gnus-summary-set-bookmark
1413     "B" gnus-summary-remove-bookmark
1414     "#" gnus-summary-mark-as-processable
1415     "\M-#" gnus-summary-unmark-as-processable
1416     "S" gnus-summary-limit-include-expunged
1417     "C" gnus-summary-catchup
1418     "H" gnus-summary-catchup-to-here
1419     "\C-c" gnus-summary-catchup-all
1420     "k" gnus-summary-kill-same-subject-and-select
1421     "K" gnus-summary-kill-same-subject
1422     "P" gnus-uu-mark-map)
1423
1424   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1425     "c" gnus-summary-clear-above
1426     "u" gnus-summary-tick-above
1427     "m" gnus-summary-mark-above
1428     "k" gnus-summary-kill-below)
1429
1430   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1431     "/" gnus-summary-limit-to-subject
1432     "n" gnus-summary-limit-to-articles
1433     "w" gnus-summary-pop-limit
1434     "s" gnus-summary-limit-to-subject
1435     "a" gnus-summary-limit-to-author
1436     "u" gnus-summary-limit-to-unread
1437     "m" gnus-summary-limit-to-marks
1438     "M" gnus-summary-limit-exclude-marks
1439     "v" gnus-summary-limit-to-score
1440     "*" gnus-summary-limit-include-cached
1441     "D" gnus-summary-limit-include-dormant
1442     "T" gnus-summary-limit-include-thread
1443     "d" gnus-summary-limit-exclude-dormant
1444     "t" gnus-summary-limit-to-age
1445     "x" gnus-summary-limit-to-extra 
1446     "E" gnus-summary-limit-include-expunged
1447     "c" gnus-summary-limit-exclude-childless-dormant
1448     "C" gnus-summary-limit-mark-excluded-as-read)
1449
1450   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1451     "n" gnus-summary-next-unread-article
1452     "p" gnus-summary-prev-unread-article
1453     "N" gnus-summary-next-article
1454     "P" gnus-summary-prev-article
1455     "\C-n" gnus-summary-next-same-subject
1456     "\C-p" gnus-summary-prev-same-subject
1457     "\M-n" gnus-summary-next-unread-subject
1458     "\M-p" gnus-summary-prev-unread-subject
1459     "f" gnus-summary-first-unread-article
1460     "b" gnus-summary-best-unread-article
1461     "j" gnus-summary-goto-article
1462     "g" gnus-summary-goto-subject
1463     "l" gnus-summary-goto-last-article
1464     "o" gnus-summary-pop-article)
1465
1466   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1467     "k" gnus-summary-kill-thread
1468     "l" gnus-summary-lower-thread
1469     "i" gnus-summary-raise-thread
1470     "T" gnus-summary-toggle-threads
1471     "t" gnus-summary-rethread-current
1472     "^" gnus-summary-reparent-thread
1473     "s" gnus-summary-show-thread
1474     "S" gnus-summary-show-all-threads
1475     "h" gnus-summary-hide-thread
1476     "H" gnus-summary-hide-all-threads
1477     "n" gnus-summary-next-thread
1478     "p" gnus-summary-prev-thread
1479     "u" gnus-summary-up-thread
1480     "o" gnus-summary-top-thread
1481     "d" gnus-summary-down-thread
1482     "#" gnus-uu-mark-thread
1483     "\M-#" gnus-uu-unmark-thread)
1484
1485   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1486     "g" gnus-summary-prepare
1487     "c" gnus-summary-insert-cached-articles)
1488
1489   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1490     "c" gnus-summary-catchup-and-exit
1491     "C" gnus-summary-catchup-all-and-exit
1492     "E" gnus-summary-exit-no-update
1493     "Q" gnus-summary-exit
1494     "Z" gnus-summary-exit
1495     "n" gnus-summary-catchup-and-goto-next-group
1496     "R" gnus-summary-reselect-current-group
1497     "G" gnus-summary-rescan-group
1498     "N" gnus-summary-next-group
1499     "s" gnus-summary-save-newsrc
1500     "P" gnus-summary-prev-group)
1501
1502   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1503     " " gnus-summary-next-page
1504     "n" gnus-summary-next-page
1505     "\177" gnus-summary-prev-page
1506     [delete] gnus-summary-prev-page
1507     "p" gnus-summary-prev-page
1508     "\r" gnus-summary-scroll-up
1509     "\M-\r" gnus-summary-scroll-down
1510     "<" gnus-summary-beginning-of-article
1511     ">" gnus-summary-end-of-article
1512     "b" gnus-summary-beginning-of-article
1513     "e" gnus-summary-end-of-article
1514     "^" gnus-summary-refer-parent-article
1515     "r" gnus-summary-refer-parent-article
1516     "D" gnus-summary-enter-digest-group
1517     "R" gnus-summary-refer-references
1518     "T" gnus-summary-refer-thread
1519     "g" gnus-summary-show-article
1520     "s" gnus-summary-isearch-article
1521     "P" gnus-summary-print-article
1522     "t" gnus-article-babel)
1523
1524   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1525     "b" gnus-article-add-buttons
1526     "B" gnus-article-add-buttons-to-head
1527     "o" gnus-article-treat-overstrike
1528     "e" gnus-article-emphasize
1529     "w" gnus-article-fill-cited-article
1530     "Q" gnus-article-fill-long-lines
1531     "C" gnus-article-capitalize-sentences
1532     "c" gnus-article-remove-cr
1533     "q" gnus-article-de-quoted-unreadable
1534     "6" gnus-article-de-base64-unreadable
1535     "Z" gnus-article-decode-HZ
1536     "h" gnus-article-wash-html
1537     "f" gnus-article-display-x-face
1538     "l" gnus-summary-stop-page-breaking
1539     "r" gnus-summary-caesar-message
1540     "t" gnus-article-hide-headers
1541     "v" gnus-summary-verbose-headers
1542     "H" gnus-article-strip-headers-in-body
1543     "d" gnus-article-treat-dumbquotes)
1544
1545   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1546     "a" gnus-article-hide
1547     "h" gnus-article-hide-headers
1548     "b" gnus-article-hide-boring-headers
1549     "s" gnus-article-hide-signature
1550     "c" gnus-article-hide-citation
1551     "C" gnus-article-hide-citation-in-followups
1552     "l" gnus-article-hide-list-identifiers
1553     "p" gnus-article-hide-pgp
1554     "B" gnus-article-strip-banner
1555     "P" gnus-article-hide-pem
1556     "\C-c" gnus-article-hide-citation-maybe)
1557
1558   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1559     "a" gnus-article-highlight
1560     "h" gnus-article-highlight-headers
1561     "c" gnus-article-highlight-citation
1562     "s" gnus-article-highlight-signature)
1563
1564   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1565     "w" gnus-article-decode-mime-words
1566     "c" gnus-article-decode-charset
1567     "v" gnus-mime-view-all-parts
1568     "b" gnus-article-view-part)
1569
1570   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1571     "z" gnus-article-date-ut
1572     "u" gnus-article-date-ut
1573     "l" gnus-article-date-local
1574     "e" gnus-article-date-lapsed
1575     "o" gnus-article-date-original
1576     "i" gnus-article-date-iso8601
1577     "s" gnus-article-date-user)
1578
1579   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1580     "t" gnus-article-remove-trailing-blank-lines
1581     "l" gnus-article-strip-leading-blank-lines
1582     "m" gnus-article-strip-multiple-blank-lines
1583     "a" gnus-article-strip-blank-lines
1584     "A" gnus-article-strip-all-blank-lines
1585     "s" gnus-article-strip-leading-space
1586     "e" gnus-article-strip-trailing-space)
1587
1588   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1589     "v" gnus-version
1590     "f" gnus-summary-fetch-faq
1591     "d" gnus-summary-describe-group
1592     "h" gnus-summary-describe-briefly
1593     "i" gnus-info-find-node)
1594
1595   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1596     "e" gnus-summary-expire-articles
1597     "\M-\C-e" gnus-summary-expire-articles-now
1598     "\177" gnus-summary-delete-article
1599     [delete] gnus-summary-delete-article
1600     [backspace] gnus-summary-delete-article
1601     "m" gnus-summary-move-article
1602     "r" gnus-summary-respool-article
1603     "w" gnus-summary-edit-article
1604     "c" gnus-summary-copy-article
1605     "B" gnus-summary-crosspost-article
1606     "q" gnus-summary-respool-query
1607     "t" gnus-summary-respool-trace
1608     "i" gnus-summary-import-article
1609     "p" gnus-summary-article-posted-p)
1610
1611   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1612     "o" gnus-summary-save-article
1613     "m" gnus-summary-save-article-mail
1614     "F" gnus-summary-write-article-file
1615     "r" gnus-summary-save-article-rmail
1616     "f" gnus-summary-save-article-file
1617     "b" gnus-summary-save-article-body-file
1618     "h" gnus-summary-save-article-folder
1619     "v" gnus-summary-save-article-vm
1620     "p" gnus-summary-pipe-output
1621     "s" gnus-soup-add-article)
1622
1623   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1624     "b" gnus-summary-display-buttonized
1625     "m" gnus-summary-repair-multipart
1626     "v" gnus-article-view-part
1627     "o" gnus-article-save-part
1628     "c" gnus-article-copy-part
1629     "e" gnus-article-externalize-part
1630     "i" gnus-article-inline-part
1631     "|" gnus-article-pipe-part))
1632
1633 (defun gnus-summary-make-menu-bar ()
1634   (gnus-turn-off-edit-menu 'summary)
1635
1636   (unless (boundp 'gnus-summary-misc-menu)
1637
1638     (easy-menu-define
1639      gnus-summary-kill-menu gnus-summary-mode-map ""
1640      (cons
1641       "Score"
1642       (nconc
1643        (list
1644         ["Enter score..." gnus-summary-score-entry t]
1645         ["Customize" gnus-score-customize t])
1646        (gnus-make-score-map 'increase)
1647        (gnus-make-score-map 'lower)
1648        '(("Mark"
1649           ["Kill below" gnus-summary-kill-below t]
1650           ["Mark above" gnus-summary-mark-above t]
1651           ["Tick above" gnus-summary-tick-above t]
1652           ["Clear above" gnus-summary-clear-above t])
1653          ["Current score" gnus-summary-current-score t]
1654          ["Set score" gnus-summary-set-score t]
1655          ["Switch current score file..." gnus-score-change-score-file t]
1656          ["Set mark below..." gnus-score-set-mark-below t]
1657          ["Set expunge below..." gnus-score-set-expunge-below t]
1658          ["Edit current score file" gnus-score-edit-current-scores t]
1659          ["Edit score file" gnus-score-edit-file t]
1660          ["Trace score" gnus-score-find-trace t]
1661          ["Find words" gnus-score-find-favourite-words t]
1662          ["Rescore buffer" gnus-summary-rescore t]
1663          ["Increase score..." gnus-summary-increase-score t]
1664          ["Lower score..." gnus-summary-lower-score t]))))
1665
1666     ;; Define both the Article menu in the summary buffer and the equivalent
1667     ;; Commands menu in the article buffer here for consistency.
1668     (let ((innards
1669            '(("Hide"
1670               ["All" gnus-article-hide t]
1671               ["Headers" gnus-article-hide-headers t]
1672               ["Signature" gnus-article-hide-signature t]
1673               ["Citation" gnus-article-hide-citation t]
1674               ["List identifiers" gnus-article-hide-list-identifiers t]
1675               ["PGP" gnus-article-hide-pgp t]
1676               ["Banner" gnus-article-strip-banner t]
1677               ["Boring headers" gnus-article-hide-boring-headers t])
1678              ("Highlight"
1679               ["All" gnus-article-highlight t]
1680               ["Headers" gnus-article-highlight-headers t]
1681               ["Signature" gnus-article-highlight-signature t]
1682               ["Citation" gnus-article-highlight-citation t])
1683              ("MIME"
1684               ["Words" gnus-article-decode-mime-words t]
1685               ["Charset" gnus-article-decode-charset t]
1686               ["QP" gnus-article-de-quoted-unreadable t]
1687               ["Base64" gnus-article-de-base64-unreadable t]
1688               ["View all" gnus-mime-view-all-parts t])
1689              ("Date"
1690               ["Local" gnus-article-date-local t]
1691               ["ISO8601" gnus-article-date-iso8601 t]
1692               ["UT" gnus-article-date-ut t]
1693               ["Original" gnus-article-date-original t]
1694               ["Lapsed" gnus-article-date-lapsed t]
1695               ["User-defined" gnus-article-date-user t])
1696              ("Washing"
1697               ("Remove Blanks"
1698                ["Leading" gnus-article-strip-leading-blank-lines t]
1699                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1700                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1701                ["All of the above" gnus-article-strip-blank-lines t]
1702                ["All" gnus-article-strip-all-blank-lines t]
1703                ["Leading space" gnus-article-strip-leading-space t]
1704                ["Trailing space" gnus-article-strip-trailing-space t])
1705               ["Overstrike" gnus-article-treat-overstrike t]
1706               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1707               ["Emphasis" gnus-article-emphasize t]
1708               ["Word wrap" gnus-article-fill-cited-article t]
1709               ["Fill long lines" gnus-article-fill-long-lines t]
1710               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1711               ["CR" gnus-article-remove-cr t]
1712               ["Show X-Face" gnus-article-display-x-face t]
1713               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1714               ["Base64" gnus-article-de-base64-unreadable t]
1715               ["Rot 13" gnus-summary-caesar-message t]
1716               ["Unix pipe" gnus-summary-pipe-message t]
1717               ["Add buttons" gnus-article-add-buttons t]
1718               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1719               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1720               ["Verbose header" gnus-summary-verbose-headers t]
1721               ["Toggle header" gnus-summary-toggle-header t]
1722               ["Html" gnus-article-wash-html t]
1723               ["HZ" gnus-article-decode-HZ t])
1724              ("Output"
1725               ["Save in default format" gnus-summary-save-article t]
1726               ["Save in file" gnus-summary-save-article-file t]
1727               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1728               ["Save in MH folder" gnus-summary-save-article-folder t]
1729               ["Save in VM folder" gnus-summary-save-article-vm t]
1730               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1731               ["Save body in file" gnus-summary-save-article-body-file t]
1732               ["Pipe through a filter" gnus-summary-pipe-output t]
1733               ["Add to SOUP packet" gnus-soup-add-article t]
1734               ["Print" gnus-summary-print-article t])
1735              ("Backend"
1736               ["Respool article..." gnus-summary-respool-article t]
1737               ["Move article..." gnus-summary-move-article
1738                (gnus-check-backend-function
1739                 'request-move-article gnus-newsgroup-name)]
1740               ["Copy article..." gnus-summary-copy-article t]
1741               ["Crosspost article..." gnus-summary-crosspost-article
1742                (gnus-check-backend-function
1743                 'request-replace-article gnus-newsgroup-name)]
1744               ["Import file..." gnus-summary-import-article t]
1745               ["Check if posted" gnus-summary-article-posted-p t]
1746               ["Edit article" gnus-summary-edit-article
1747                (not (gnus-group-read-only-p))]
1748               ["Delete article" gnus-summary-delete-article
1749                (gnus-check-backend-function
1750                 'request-expire-articles gnus-newsgroup-name)]
1751               ["Query respool" gnus-summary-respool-query t]
1752               ["Trace respool" gnus-summary-respool-trace t]
1753               ["Delete expirable articles" gnus-summary-expire-articles-now
1754                (gnus-check-backend-function
1755                 'request-expire-articles gnus-newsgroup-name)])
1756              ("Extract"
1757               ["Uudecode" gnus-uu-decode-uu t]
1758               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1759               ["Unshar" gnus-uu-decode-unshar t]
1760               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1761               ["Save" gnus-uu-decode-save t]
1762               ["Binhex" gnus-uu-decode-binhex t]
1763               ["Postscript" gnus-uu-decode-postscript t])
1764              ("Cache"
1765               ["Enter article" gnus-cache-enter-article t]
1766               ["Remove article" gnus-cache-remove-article t])
1767              ["Translate" gnus-article-babel t]
1768              ["Select article buffer" gnus-summary-select-article-buffer t]
1769              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1770              ["Isearch article..." gnus-summary-isearch-article t]
1771              ["Beginning of the article" gnus-summary-beginning-of-article t]
1772              ["End of the article" gnus-summary-end-of-article t]
1773              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1774              ["Fetch referenced articles" gnus-summary-refer-references t]
1775              ["Fetch current thread" gnus-summary-refer-thread t]
1776              ["Fetch article with id..." gnus-summary-refer-article t]
1777              ["Redisplay" gnus-summary-show-article t])))
1778       (easy-menu-define
1779        gnus-summary-article-menu gnus-summary-mode-map ""
1780        (cons "Article" innards))
1781
1782       (easy-menu-define
1783        gnus-article-commands-menu gnus-article-mode-map ""
1784        (cons "Commands" innards)))
1785
1786     (easy-menu-define
1787      gnus-summary-thread-menu gnus-summary-mode-map ""
1788      '("Threads"
1789        ["Toggle threading" gnus-summary-toggle-threads t]
1790        ["Hide threads" gnus-summary-hide-all-threads t]
1791        ["Show threads" gnus-summary-show-all-threads t]
1792        ["Hide thread" gnus-summary-hide-thread t]
1793        ["Show thread" gnus-summary-show-thread t]
1794        ["Go to next thread" gnus-summary-next-thread t]
1795        ["Go to previous thread" gnus-summary-prev-thread t]
1796        ["Go down thread" gnus-summary-down-thread t]
1797        ["Go up thread" gnus-summary-up-thread t]
1798        ["Top of thread" gnus-summary-top-thread t]
1799        ["Mark thread as read" gnus-summary-kill-thread t]
1800        ["Lower thread score" gnus-summary-lower-thread t]
1801        ["Raise thread score" gnus-summary-raise-thread t]
1802        ["Rethread current" gnus-summary-rethread-current t]))
1803
1804     (easy-menu-define
1805      gnus-summary-post-menu gnus-summary-mode-map ""
1806      '("Post"
1807        ["Post an article" gnus-summary-post-news t]
1808        ["Followup" gnus-summary-followup t]
1809        ["Followup and yank" gnus-summary-followup-with-original t]
1810        ["Supersede article" gnus-summary-supersede-article t]
1811        ["Cancel article" gnus-summary-cancel-article t]
1812        ["Reply" gnus-summary-reply t]
1813        ["Reply and yank" gnus-summary-reply-with-original t]
1814        ["Wide reply" gnus-summary-wide-reply t]
1815        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1816        ["Mail forward" gnus-summary-mail-forward t]
1817        ["Post forward" gnus-summary-post-forward t]
1818        ["Digest and mail" gnus-uu-digest-mail-forward t]
1819        ["Digest and post" gnus-uu-digest-post-forward t]
1820        ["Resend message" gnus-summary-resend-message t]
1821        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1822        ["Send a mail" gnus-summary-mail-other-window t]
1823        ["Uuencode and post" gnus-uu-post-news t]
1824        ["Followup via news" gnus-summary-followup-to-mail t]
1825        ["Followup via news and yank"
1826         gnus-summary-followup-to-mail-with-original t]
1827        ;;("Draft"
1828        ;;["Send" gnus-summary-send-draft t]
1829        ;;["Send bounced" gnus-resend-bounced-mail t])
1830        ))
1831
1832     (easy-menu-define
1833      gnus-summary-misc-menu gnus-summary-mode-map ""
1834      '("Misc"
1835        ("Mark Read"
1836         ["Mark as read" gnus-summary-mark-as-read-forward t]
1837         ["Mark same subject and select"
1838          gnus-summary-kill-same-subject-and-select t]
1839         ["Mark same subject" gnus-summary-kill-same-subject t]
1840         ["Catchup" gnus-summary-catchup t]
1841         ["Catchup all" gnus-summary-catchup-all t]
1842         ["Catchup to here" gnus-summary-catchup-to-here t]
1843         ["Catchup region" gnus-summary-mark-region-as-read t]
1844         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1845        ("Mark Various"
1846         ["Tick" gnus-summary-tick-article-forward t]
1847         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1848         ["Remove marks" gnus-summary-clear-mark-forward t]
1849         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1850         ["Set bookmark" gnus-summary-set-bookmark t]
1851         ["Remove bookmark" gnus-summary-remove-bookmark t])
1852        ("Mark Limit"
1853         ["Marks..." gnus-summary-limit-to-marks t]
1854         ["Subject..." gnus-summary-limit-to-subject t]
1855         ["Author..." gnus-summary-limit-to-author t]
1856         ["Age..." gnus-summary-limit-to-age t]
1857         ["Extra..." gnus-summary-limit-to-extra t]
1858         ["Score" gnus-summary-limit-to-score t]
1859         ["Unread" gnus-summary-limit-to-unread t]
1860         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1861         ["Articles" gnus-summary-limit-to-articles t]
1862         ["Pop limit" gnus-summary-pop-limit t]
1863         ["Show dormant" gnus-summary-limit-include-dormant t]
1864         ["Hide childless dormant"
1865          gnus-summary-limit-exclude-childless-dormant t]
1866         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1867         ["Hide marked" gnus-summary-limit-exclude-marks t]
1868         ["Show expunged" gnus-summary-show-all-expunged t])
1869        ("Process Mark"
1870         ["Set mark" gnus-summary-mark-as-processable t]
1871         ["Remove mark" gnus-summary-unmark-as-processable t]
1872         ["Remove all marks" gnus-summary-unmark-all-processable t]
1873         ["Mark above" gnus-uu-mark-over t]
1874         ["Mark series" gnus-uu-mark-series t]
1875         ["Mark region" gnus-uu-mark-region t]
1876         ["Unmark region" gnus-uu-unmark-region t]
1877         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1878         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1879         ["Mark all" gnus-uu-mark-all t]
1880         ["Mark buffer" gnus-uu-mark-buffer t]
1881         ["Mark sparse" gnus-uu-mark-sparse t]
1882         ["Mark thread" gnus-uu-mark-thread t]
1883         ["Unmark thread" gnus-uu-unmark-thread t]
1884         ("Process Mark Sets"
1885          ["Kill" gnus-summary-kill-process-mark t]
1886          ["Yank" gnus-summary-yank-process-mark
1887           gnus-newsgroup-process-stack]
1888          ["Save" gnus-summary-save-process-mark t]))
1889        ("Scroll article"
1890         ["Page forward" gnus-summary-next-page t]
1891         ["Page backward" gnus-summary-prev-page t]
1892         ["Line forward" gnus-summary-scroll-up t])
1893        ("Move"
1894         ["Next unread article" gnus-summary-next-unread-article t]
1895         ["Previous unread article" gnus-summary-prev-unread-article t]
1896         ["Next article" gnus-summary-next-article t]
1897         ["Previous article" gnus-summary-prev-article t]
1898         ["Next unread subject" gnus-summary-next-unread-subject t]
1899         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1900         ["Next article same subject" gnus-summary-next-same-subject t]
1901         ["Previous article same subject" gnus-summary-prev-same-subject t]
1902         ["First unread article" gnus-summary-first-unread-article t]
1903         ["Best unread article" gnus-summary-best-unread-article t]
1904         ["Go to subject number..." gnus-summary-goto-subject t]
1905         ["Go to article number..." gnus-summary-goto-article t]
1906         ["Go to the last article" gnus-summary-goto-last-article t]
1907         ["Pop article off history" gnus-summary-pop-article t])
1908        ("Sort"
1909         ["Sort by number" gnus-summary-sort-by-number t]
1910         ["Sort by author" gnus-summary-sort-by-author t]
1911         ["Sort by subject" gnus-summary-sort-by-subject t]
1912         ["Sort by date" gnus-summary-sort-by-date t]
1913         ["Sort by score" gnus-summary-sort-by-score t]
1914         ["Sort by lines" gnus-summary-sort-by-lines t]
1915         ["Sort by characters" gnus-summary-sort-by-chars t])
1916        ("Help"
1917         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1918         ["Describe group" gnus-summary-describe-group t]
1919         ["Read manual" gnus-info-find-node t])
1920        ("Modes"
1921         ["Pick and read" gnus-pick-mode t]
1922         ["Binary" gnus-binary-mode t])
1923        ("Regeneration"
1924         ["Regenerate" gnus-summary-prepare t]
1925         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1926         ["Toggle threading" gnus-summary-toggle-threads t])
1927        ["Filter articles..." gnus-summary-execute-command t]
1928        ["Run command on subjects..." gnus-summary-universal-argument t]
1929        ["Search articles forward..." gnus-summary-search-article-forward t]
1930        ["Search articles backward..." gnus-summary-search-article-backward t]
1931        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1932        ["Expand window" gnus-summary-expand-window t]
1933        ["Expire expirable articles" gnus-summary-expire-articles
1934         (gnus-check-backend-function
1935          'request-expire-articles gnus-newsgroup-name)]
1936        ["Edit local kill file" gnus-summary-edit-local-kill t]
1937        ["Edit main kill file" gnus-summary-edit-global-kill t]
1938        ["Edit group parameters" gnus-summary-edit-parameters t]
1939        ["Customize group parameters" gnus-summary-customize-parameters t]
1940        ["Send a bug report" gnus-bug t]
1941        ("Exit"
1942         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1943         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1944         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1945         ["Exit group" gnus-summary-exit t]
1946         ["Exit group without updating" gnus-summary-exit-no-update t]
1947         ["Exit and goto next group" gnus-summary-next-group t]
1948         ["Exit and goto prev group" gnus-summary-prev-group t]
1949         ["Reselect group" gnus-summary-reselect-current-group t]
1950         ["Rescan group" gnus-summary-rescan-group t]
1951         ["Update dribble" gnus-summary-save-newsrc t])))
1952
1953     (gnus-run-hooks 'gnus-summary-menu-hook)))
1954
1955 (defun gnus-score-set-default (var value)
1956   "A version of set that updates the GNU Emacs menu-bar."
1957   (set var value)
1958   ;; It is the message that forces the active status to be updated.
1959   (message ""))
1960
1961 (defun gnus-make-score-map (type)
1962   "Make a summary score map of type TYPE."
1963   (if t
1964       nil
1965     (let ((headers '(("author" "from" string)
1966                      ("subject" "subject" string)
1967                      ("article body" "body" string)
1968                      ("article head" "head" string)
1969                      ("xref" "xref" string)
1970                      ("extra header" "extra" string)
1971                      ("lines" "lines" number)
1972                      ("followups to author" "followup" string)))
1973           (types '((number ("less than" <)
1974                            ("greater than" >)
1975                            ("equal" =))
1976                    (string ("substring" s)
1977                            ("exact string" e)
1978                            ("fuzzy string" f)
1979                            ("regexp" r))))
1980           (perms '(("temporary" (current-time-string))
1981                    ("permanent" nil)
1982                    ("immediate" now)))
1983           header)
1984       (list
1985        (apply
1986         'nconc
1987         (list
1988          (if (eq type 'lower)
1989              "Lower score"
1990            "Increase score"))
1991         (let (outh)
1992           (while headers
1993             (setq header (car headers))
1994             (setq outh
1995                   (cons
1996                    (apply
1997                     'nconc
1998                     (list (car header))
1999                     (let ((ts (cdr (assoc (nth 2 header) types)))
2000                           outt)
2001                       (while ts
2002                         (setq outt
2003                               (cons
2004                                (apply
2005                                 'nconc
2006                                 (list (caar ts))
2007                                 (let ((ps perms)
2008                                       outp)
2009                                   (while ps
2010                                     (setq outp
2011                                           (cons
2012                                            (vector
2013                                             (caar ps)
2014                                             (list
2015                                              'gnus-summary-score-entry
2016                                              (nth 1 header)
2017                                              (if (or (string= (nth 1 header)
2018                                                               "head")
2019                                                      (string= (nth 1 header)
2020                                                               "body"))
2021                                                  ""
2022                                                (list 'gnus-summary-header
2023                                                      (nth 1 header)))
2024                                              (list 'quote (nth 1 (car ts)))
2025                                              (list 'gnus-score-delta-default
2026                                                    nil)
2027                                              (nth 1 (car ps))
2028                                              t)
2029                                             t)
2030                                            outp))
2031                                     (setq ps (cdr ps)))
2032                                   (list (nreverse outp))))
2033                                outt))
2034                         (setq ts (cdr ts)))
2035                       (list (nreverse outt))))
2036                    outh))
2037             (setq headers (cdr headers)))
2038           (list (nreverse outh))))))))
2039
2040 \f
2041
2042 (defun gnus-summary-mode (&optional group)
2043   "Major mode for reading articles.
2044
2045 All normal editing commands are switched off.
2046 \\<gnus-summary-mode-map>
2047 Each line in this buffer represents one article.  To read an
2048 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2049 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2050 respectively.
2051
2052 You can also post articles and send mail from this buffer.  To
2053 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2054 of an article, type `\\[gnus-summary-reply]'.
2055
2056 There are approx. one gazillion commands you can execute in this
2057 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2058
2059 The following commands are available:
2060
2061 \\{gnus-summary-mode-map}"
2062   (interactive)
2063   (when (gnus-visual-p 'summary-menu 'menu)
2064     (gnus-summary-make-menu-bar))
2065   (kill-all-local-variables)
2066   (gnus-summary-make-local-variables)
2067   (gnus-make-thread-indent-array)
2068   (gnus-simplify-mode-line)
2069   (setq major-mode 'gnus-summary-mode)
2070   (setq mode-name "Summary")
2071   (make-local-variable 'minor-mode-alist)
2072   (use-local-map gnus-summary-mode-map)
2073   (buffer-disable-undo)
2074   (setq buffer-read-only t)             ;Disable modification
2075   (setq truncate-lines t)
2076   (setq selective-display t)
2077   (setq selective-display-ellipses t)   ;Display `...'
2078   (gnus-summary-set-display-table)
2079   (gnus-set-default-directory)
2080   (setq gnus-newsgroup-name group)
2081   (make-local-variable 'gnus-summary-line-format)
2082   (make-local-variable 'gnus-summary-line-format-spec)
2083   (make-local-variable 'gnus-summary-dummy-line-format)
2084   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2085   (make-local-variable 'gnus-summary-mark-positions)
2086   (make-local-hook 'pre-command-hook)
2087   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2088   (gnus-run-hooks 'gnus-summary-mode-hook)
2089   (mm-enable-multibyte)
2090   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2091   (gnus-update-summary-mark-positions))
2092
2093 (defun gnus-summary-make-local-variables ()
2094   "Make all the local summary buffer variables."
2095   (let (global)
2096     (dolist (local gnus-summary-local-variables)
2097       (if (consp local)
2098           (progn
2099             (if (eq (cdr local) 'global)
2100                 ;; Copy the global value of the variable.
2101                 (setq global (symbol-value (car local)))
2102               ;; Use the value from the list.
2103               (setq global (eval (cdr local))))
2104             (set (make-local-variable (car local)) global))
2105         ;; Simple nil-valued local variable.
2106         (set (make-local-variable local) nil)))))
2107
2108 (defun gnus-summary-clear-local-variables ()
2109   (let ((locals gnus-summary-local-variables))
2110     (while locals
2111       (if (consp (car locals))
2112           (and (vectorp (caar locals))
2113                (set (caar locals) nil))
2114         (and (vectorp (car locals))
2115              (set (car locals) nil)))
2116       (setq locals (cdr locals)))))
2117
2118 ;; Summary data functions.
2119
2120 (defmacro gnus-data-number (data)
2121   `(car ,data))
2122
2123 (defmacro gnus-data-set-number (data number)
2124   `(setcar ,data ,number))
2125
2126 (defmacro gnus-data-mark (data)
2127   `(nth 1 ,data))
2128
2129 (defmacro gnus-data-set-mark (data mark)
2130   `(setcar (nthcdr 1 ,data) ,mark))
2131
2132 (defmacro gnus-data-pos (data)
2133   `(nth 2 ,data))
2134
2135 (defmacro gnus-data-set-pos (data pos)
2136   `(setcar (nthcdr 2 ,data) ,pos))
2137
2138 (defmacro gnus-data-header (data)
2139   `(nth 3 ,data))
2140
2141 (defmacro gnus-data-set-header (data header)
2142   `(setf (nth 3 ,data) ,header))
2143
2144 (defmacro gnus-data-level (data)
2145   `(nth 4 ,data))
2146
2147 (defmacro gnus-data-unread-p (data)
2148   `(= (nth 1 ,data) gnus-unread-mark))
2149
2150 (defmacro gnus-data-read-p (data)
2151   `(/= (nth 1 ,data) gnus-unread-mark))
2152
2153 (defmacro gnus-data-pseudo-p (data)
2154   `(consp (nth 3 ,data)))
2155
2156 (defmacro gnus-data-find (number)
2157   `(assq ,number gnus-newsgroup-data))
2158
2159 (defmacro gnus-data-find-list (number &optional data)
2160   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2161      (memq (assq ,number bdata)
2162            bdata)))
2163
2164 (defmacro gnus-data-make (number mark pos header level)
2165   `(list ,number ,mark ,pos ,header ,level))
2166
2167 (defun gnus-data-enter (after-article number mark pos header level offset)
2168   (let ((data (gnus-data-find-list after-article)))
2169     (unless data
2170       (error "No such article: %d" after-article))
2171     (setcdr data (cons (gnus-data-make number mark pos header level)
2172                        (cdr data)))
2173     (setq gnus-newsgroup-data-reverse nil)
2174     (gnus-data-update-list (cddr data) offset)))
2175
2176 (defun gnus-data-enter-list (after-article list &optional offset)
2177   (when list
2178     (let ((data (and after-article (gnus-data-find-list after-article)))
2179           (ilist list))
2180       (if (not (or data
2181                    after-article))
2182           (let ((odata gnus-newsgroup-data))
2183             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2184             (when offset
2185               (gnus-data-update-list odata offset)))
2186         ;; Find the last element in the list to be spliced into the main
2187         ;; list.
2188         (while (cdr list)
2189           (setq list (cdr list)))
2190         (if (not data)
2191             (progn
2192               (setcdr list gnus-newsgroup-data)
2193               (setq gnus-newsgroup-data ilist)
2194               (when offset
2195                 (gnus-data-update-list (cdr list) offset)))
2196           (setcdr list (cdr data))
2197           (setcdr data ilist)
2198           (when offset
2199             (gnus-data-update-list (cdr list) offset))))
2200       (setq gnus-newsgroup-data-reverse nil))))
2201
2202 (defun gnus-data-remove (article &optional offset)
2203   (let ((data gnus-newsgroup-data))
2204     (if (= (gnus-data-number (car data)) article)
2205         (progn
2206           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2207                 gnus-newsgroup-data-reverse nil)
2208           (when offset
2209             (gnus-data-update-list gnus-newsgroup-data offset)))
2210       (while (cdr data)
2211         (when (= (gnus-data-number (cadr data)) article)
2212           (setcdr data (cddr data))
2213           (when offset
2214             (gnus-data-update-list (cdr data) offset))
2215           (setq data nil
2216                 gnus-newsgroup-data-reverse nil))
2217         (setq data (cdr data))))))
2218
2219 (defmacro gnus-data-list (backward)
2220   `(if ,backward
2221        (or gnus-newsgroup-data-reverse
2222            (setq gnus-newsgroup-data-reverse
2223                  (reverse gnus-newsgroup-data)))
2224      gnus-newsgroup-data))
2225
2226 (defun gnus-data-update-list (data offset)
2227   "Add OFFSET to the POS of all data entries in DATA."
2228   (setq gnus-newsgroup-data-reverse nil)
2229   (while data
2230     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2231     (setq data (cdr data))))
2232
2233 (defun gnus-summary-article-pseudo-p (article)
2234   "Say whether this article is a pseudo article or not."
2235   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2236
2237 (defmacro gnus-summary-article-sparse-p (article)
2238   "Say whether this article is a sparse article or not."
2239   `(memq ,article gnus-newsgroup-sparse))
2240
2241 (defmacro gnus-summary-article-ancient-p (article)
2242   "Say whether this article is a sparse article or not."
2243   `(memq ,article gnus-newsgroup-ancient))
2244
2245 (defun gnus-article-parent-p (number)
2246   "Say whether this article is a parent or not."
2247   (let ((data (gnus-data-find-list number)))
2248     (and (cdr data)                     ; There has to be an article after...
2249          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2250             (gnus-data-level (nth 1 data))))))
2251
2252 (defun gnus-article-children (number)
2253   "Return a list of all children to NUMBER."
2254   (let* ((data (gnus-data-find-list number))
2255          (level (gnus-data-level (car data)))
2256          children)
2257     (setq data (cdr data))
2258     (while (and data
2259                 (= (gnus-data-level (car data)) (1+ level)))
2260       (push (gnus-data-number (car data)) children)
2261       (setq data (cdr data)))
2262     children))
2263
2264 (defmacro gnus-summary-skip-intangible ()
2265   "If the current article is intangible, then jump to a different article."
2266   '(let ((to (get-text-property (point) 'gnus-intangible)))
2267      (and to (gnus-summary-goto-subject to))))
2268
2269 (defmacro gnus-summary-article-intangible-p ()
2270   "Say whether this article is intangible or not."
2271   '(get-text-property (point) 'gnus-intangible))
2272
2273 (defun gnus-article-read-p (article)
2274   "Say whether ARTICLE is read or not."
2275   (not (or (memq article gnus-newsgroup-marked)
2276            (memq article gnus-newsgroup-unreads)
2277            (memq article gnus-newsgroup-unselected)
2278            (memq article gnus-newsgroup-dormant))))
2279
2280 ;; Some summary mode macros.
2281
2282 (defmacro gnus-summary-article-number ()
2283   "The article number of the article on the current line.
2284 If there isn's an article number here, then we return the current
2285 article number."
2286   '(progn
2287      (gnus-summary-skip-intangible)
2288      (or (get-text-property (point) 'gnus-number)
2289          (gnus-summary-last-subject))))
2290
2291 (defmacro gnus-summary-article-header (&optional number)
2292   "Return the header of article NUMBER."
2293   `(gnus-data-header (gnus-data-find
2294                       ,(or number '(gnus-summary-article-number)))))
2295
2296 (defmacro gnus-summary-thread-level (&optional number)
2297   "Return the level of thread that starts with article NUMBER."
2298   `(if (and (eq gnus-summary-make-false-root 'dummy)
2299             (get-text-property (point) 'gnus-intangible))
2300        0
2301      (gnus-data-level (gnus-data-find
2302                        ,(or number '(gnus-summary-article-number))))))
2303
2304 (defmacro gnus-summary-article-mark (&optional number)
2305   "Return the mark of article NUMBER."
2306   `(gnus-data-mark (gnus-data-find
2307                     ,(or number '(gnus-summary-article-number)))))
2308
2309 (defmacro gnus-summary-article-pos (&optional number)
2310   "Return the position of the line of article NUMBER."
2311   `(gnus-data-pos (gnus-data-find
2312                    ,(or number '(gnus-summary-article-number)))))
2313
2314 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2315 (defmacro gnus-summary-article-subject (&optional number)
2316   "Return current subject string or nil if nothing."
2317   `(let ((headers
2318           ,(if number
2319                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2320              '(gnus-data-header (assq (gnus-summary-article-number)
2321                                       gnus-newsgroup-data)))))
2322      (and headers
2323           (vectorp headers)
2324           (mail-header-subject headers))))
2325
2326 (defmacro gnus-summary-article-score (&optional number)
2327   "Return current article score."
2328   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2329                   gnus-newsgroup-scored))
2330        gnus-summary-default-score 0))
2331
2332 (defun gnus-summary-article-children (&optional number)
2333   "Return a list of article numbers that are children of article NUMBER."
2334   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2335          (level (gnus-data-level (car data)))
2336          l children)
2337     (while (and (setq data (cdr data))
2338                 (> (setq l (gnus-data-level (car data))) level))
2339       (and (= (1+ level) l)
2340            (push (gnus-data-number (car data))
2341                  children)))
2342     (nreverse children)))
2343
2344 (defun gnus-summary-article-parent (&optional number)
2345   "Return the article number of the parent of article NUMBER."
2346   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2347                                     (gnus-data-list t)))
2348          (level (gnus-data-level (car data))))
2349     (if (zerop level)
2350         ()                              ; This is a root.
2351       ;; We search until we find an article with a level less than
2352       ;; this one.  That function has to be the parent.
2353       (while (and (setq data (cdr data))
2354                   (not (< (gnus-data-level (car data)) level))))
2355       (and data (gnus-data-number (car data))))))
2356
2357 (defun gnus-unread-mark-p (mark)
2358   "Say whether MARK is the unread mark."
2359   (= mark gnus-unread-mark))
2360
2361 (defun gnus-read-mark-p (mark)
2362   "Say whether MARK is one of the marks that mark as read.
2363 This is all marks except unread, ticked, dormant, and expirable."
2364   (not (or (= mark gnus-unread-mark)
2365            (= mark gnus-ticked-mark)
2366            (= mark gnus-dormant-mark)
2367            (= mark gnus-expirable-mark))))
2368
2369 (defmacro gnus-article-mark (number)
2370   "Return the MARK of article NUMBER.
2371 This macro should only be used when computing the mark the \"first\"
2372 time; i.e., when generating the summary lines.  After that,
2373 `gnus-summary-article-mark' should be used to examine the
2374 marks of articles."
2375   `(cond
2376     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2377     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2378     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2379     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2380     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2381     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2382     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2383     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2384            gnus-ancient-mark))))
2385
2386 ;; Saving hidden threads.
2387
2388 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2389 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2390
2391 (defmacro gnus-save-hidden-threads (&rest forms)
2392   "Save hidden threads, eval FORMS, and restore the hidden threads."
2393   (let ((config (make-symbol "config")))
2394     `(let ((,config (gnus-hidden-threads-configuration)))
2395        (unwind-protect
2396            (save-excursion
2397              ,@forms)
2398          (gnus-restore-hidden-threads-configuration ,config)))))
2399
2400 (defun gnus-data-compute-positions ()
2401   "Compute the positions of all articles."
2402   (setq gnus-newsgroup-data-reverse nil)
2403   (let ((data gnus-newsgroup-data))
2404     (save-excursion
2405       (gnus-save-hidden-threads
2406         (gnus-summary-show-all-threads)
2407         (goto-char (point-min))
2408         (while data
2409           (while (get-text-property (point) 'gnus-intangible)
2410             (forward-line 1))
2411           (gnus-data-set-pos (car data) (+ (point) 3))
2412           (setq data (cdr data))
2413           (forward-line 1))))))
2414
2415 (defun gnus-hidden-threads-configuration ()
2416   "Return the current hidden threads configuration."
2417   (save-excursion
2418     (let (config)
2419       (goto-char (point-min))
2420       (while (search-forward "\r" nil t)
2421         (push (1- (point)) config))
2422       config)))
2423
2424 (defun gnus-restore-hidden-threads-configuration (config)
2425   "Restore hidden threads configuration from CONFIG."
2426   (let (point buffer-read-only)
2427     (while (setq point (pop config))
2428       (when (and (< point (point-max))
2429                  (goto-char point)
2430                  (eq (char-after) ?\n))
2431         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2432
2433 ;; Various summary mode internalish functions.
2434
2435 (defun gnus-mouse-pick-article (e)
2436   (interactive "e")
2437   (mouse-set-point e)
2438   (gnus-summary-next-page nil t))
2439
2440 (defun gnus-summary-set-display-table ()
2441   ;; Change the display table.  Odd characters have a tendency to mess
2442   ;; up nicely formatted displays - we make all possible glyphs
2443   ;; display only a single character.
2444
2445   ;; We start from the standard display table, if any.
2446   (let ((table (or (copy-sequence standard-display-table)
2447                    (make-display-table)))
2448         (i 32))
2449     ;; Nix out all the control chars...
2450     (while (>= (setq i (1- i)) 0)
2451       (aset table i [??]))
2452     ;; ... but not newline and cr, of course.  (cr is necessary for the
2453     ;; selective display).
2454     (aset table ?\n nil)
2455     (aset table ?\r nil)
2456     ;; We keep TAB as well.
2457     (aset table ?\t nil)
2458     ;; We nix out any glyphs over 126 that are not set already.
2459     (let ((i 256))
2460       (while (>= (setq i (1- i)) 127)
2461         ;; Only modify if the entry is nil.
2462         (unless (aref table i)
2463           (aset table i [??]))))
2464     (setq buffer-display-table table)))
2465
2466 (defun gnus-summary-setup-buffer (group)
2467   "Initialize summary buffer."
2468   (let ((buffer (concat "*Summary " group "*")))
2469     (if (get-buffer buffer)
2470         (progn
2471           (set-buffer buffer)
2472           (setq gnus-summary-buffer (current-buffer))
2473           (not gnus-newsgroup-prepared))
2474       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2475       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2476       (gnus-summary-mode group)
2477       (when gnus-carpal
2478         (gnus-carpal-setup-buffer 'summary))
2479       (unless gnus-single-article-buffer
2480         (make-local-variable 'gnus-article-buffer)
2481         (make-local-variable 'gnus-article-current)
2482         (make-local-variable 'gnus-original-article-buffer))
2483       (setq gnus-newsgroup-name group)
2484       t)))
2485
2486 (defun gnus-set-global-variables ()
2487   ;; Set the global equivalents of the summary buffer-local variables
2488   ;; to the latest values they had.  These reflect the summary buffer
2489   ;; that was in action when the last article was fetched.
2490   (when (eq major-mode 'gnus-summary-mode)
2491     (setq gnus-summary-buffer (current-buffer))
2492     (let ((name gnus-newsgroup-name)
2493           (marked gnus-newsgroup-marked)
2494           (unread gnus-newsgroup-unreads)
2495           (headers gnus-current-headers)
2496           (data gnus-newsgroup-data)
2497           (summary gnus-summary-buffer)
2498           (article-buffer gnus-article-buffer)
2499           (original gnus-original-article-buffer)
2500           (gac gnus-article-current)
2501           (reffed gnus-reffed-article-number)
2502           (score-file gnus-current-score-file)
2503           (default-charset gnus-newsgroup-charset))
2504       (save-excursion
2505         (set-buffer gnus-group-buffer)
2506         (setq gnus-newsgroup-name name
2507               gnus-newsgroup-marked marked
2508               gnus-newsgroup-unreads unread
2509               gnus-current-headers headers
2510               gnus-newsgroup-data data
2511               gnus-article-current gac
2512               gnus-summary-buffer summary
2513               gnus-article-buffer article-buffer
2514               gnus-original-article-buffer original
2515               gnus-reffed-article-number reffed
2516               gnus-current-score-file score-file
2517               gnus-newsgroup-charset default-charset)
2518         ;; The article buffer also has local variables.
2519         (when (gnus-buffer-live-p gnus-article-buffer)
2520           (set-buffer gnus-article-buffer)
2521           (setq gnus-summary-buffer summary))))))
2522
2523 (defun gnus-summary-article-unread-p (article)
2524   "Say whether ARTICLE is unread or not."
2525   (memq article gnus-newsgroup-unreads))
2526
2527 (defun gnus-summary-first-article-p (&optional article)
2528   "Return whether ARTICLE is the first article in the buffer."
2529   (if (not (setq article (or article (gnus-summary-article-number))))
2530       nil
2531     (eq article (caar gnus-newsgroup-data))))
2532
2533 (defun gnus-summary-last-article-p (&optional article)
2534   "Return whether ARTICLE is the last article in the buffer."
2535   (if (not (setq article (or article (gnus-summary-article-number))))
2536       ;; All non-existent numbers are the last article.  :-)
2537       t
2538     (not (cdr (gnus-data-find-list article)))))
2539
2540 (defun gnus-make-thread-indent-array ()
2541   (let ((n 200))
2542     (unless (and gnus-thread-indent-array
2543                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2544       (setq gnus-thread-indent-array (make-vector 201 "")
2545             gnus-thread-indent-array-level gnus-thread-indent-level)
2546       (while (>= n 0)
2547         (aset gnus-thread-indent-array n
2548               (make-string (* n gnus-thread-indent-level) ? ))
2549         (setq n (1- n))))))
2550
2551 (defun gnus-update-summary-mark-positions ()
2552   "Compute where the summary marks are to go."
2553   (save-excursion
2554     (when (gnus-buffer-exists-p gnus-summary-buffer)
2555       (set-buffer gnus-summary-buffer))
2556     (let ((gnus-replied-mark 129)
2557           (gnus-score-below-mark 130)
2558           (gnus-score-over-mark 130)
2559           (gnus-download-mark 131)
2560           (spec gnus-summary-line-format-spec)
2561           gnus-visual pos)
2562       (save-excursion
2563         (gnus-set-work-buffer)
2564         (let ((gnus-summary-line-format-spec spec)
2565               (gnus-newsgroup-downloadable '((0 . t))))
2566           (gnus-summary-insert-line
2567            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2568           (goto-char (point-min))
2569           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2570                                              (- (point) 2)))))
2571           (goto-char (point-min))
2572           (push (cons 'replied (and (search-forward "\201" nil t)
2573                                     (- (point) 2)))
2574                 pos)
2575           (goto-char (point-min))
2576           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2577                 pos)
2578           (goto-char (point-min))
2579           (push (cons 'download
2580                       (and (search-forward "\203" nil t) (- (point) 2)))
2581                 pos)))
2582       (setq gnus-summary-mark-positions pos))))
2583
2584 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2585   "Insert a dummy root in the summary buffer."
2586   (beginning-of-line)
2587   (gnus-add-text-properties
2588    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2589    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2590
2591 (defun gnus-summary-from-or-to-or-newsgroups (header)
2592   (let ((to (cdr (assq 'To (mail-header-extra header))))
2593         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2594         (mail-parse-charset gnus-newsgroup-charset)
2595         (mail-parse-ignored-charsets 
2596          (save-excursion (set-buffer gnus-summary-buffer)
2597                          gnus-newsgroup-ignored-charsets)))
2598     (cond
2599      ((and to
2600            gnus-ignored-from-addresses
2601            (string-match gnus-ignored-from-addresses
2602                          (mail-header-from header)))
2603       (concat "-> "
2604               (or (car (funcall gnus-extract-address-components
2605                                 (funcall
2606                                  gnus-decode-encoded-word-function to)))
2607                   (funcall gnus-decode-encoded-word-function to))))
2608      ((and newsgroups
2609            gnus-ignored-from-addresses
2610            (string-match gnus-ignored-from-addresses
2611                          (mail-header-from header)))
2612       (concat "=> " newsgroups))
2613      (t
2614       (or (car (funcall gnus-extract-address-components
2615                         (mail-header-from header)))
2616           (mail-header-from header))))))
2617
2618 (defun gnus-summary-insert-line (gnus-tmp-header
2619                                  gnus-tmp-level gnus-tmp-current
2620                                  gnus-tmp-unread gnus-tmp-replied
2621                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2622                                  &optional gnus-tmp-dummy gnus-tmp-score
2623                                  gnus-tmp-process)
2624   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2625          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2626          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2627          (gnus-tmp-score-char
2628           (if (or (null gnus-summary-default-score)
2629                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2630                       gnus-summary-zcore-fuzz))
2631               ?  ;Whitespace
2632             (if (< gnus-tmp-score gnus-summary-default-score)
2633                 gnus-score-below-mark gnus-score-over-mark)))
2634          (gnus-tmp-replied
2635           (cond (gnus-tmp-process gnus-process-mark)
2636                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2637                  gnus-cached-mark)
2638                 (gnus-tmp-replied gnus-replied-mark)
2639                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2640                  gnus-saved-mark)
2641                 (t gnus-unread-mark)))
2642          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2643          (gnus-tmp-name
2644           (cond
2645            ((string-match "<[^>]+> *$" gnus-tmp-from)
2646             (let ((beg (match-beginning 0)))
2647               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2648                        (substring gnus-tmp-from (1+ (match-beginning 0))
2649                                   (1- (match-end 0))))
2650                   (substring gnus-tmp-from 0 beg))))
2651            ((string-match "(.+)" gnus-tmp-from)
2652             (substring gnus-tmp-from
2653                        (1+ (match-beginning 0)) (1- (match-end 0))))
2654            (t gnus-tmp-from)))
2655          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2656          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2657          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2658          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2659          (buffer-read-only nil))
2660     (when (string= gnus-tmp-name "")
2661       (setq gnus-tmp-name gnus-tmp-from))
2662     (unless (numberp gnus-tmp-lines)
2663       (setq gnus-tmp-lines 0))
2664     (gnus-put-text-property
2665      (point)
2666      (progn (eval gnus-summary-line-format-spec) (point))
2667      'gnus-number gnus-tmp-number)
2668     (when (gnus-visual-p 'summary-highlight 'highlight)
2669       (forward-line -1)
2670       (gnus-run-hooks 'gnus-summary-update-hook)
2671       (forward-line 1))))
2672
2673 (defun gnus-summary-update-line (&optional dont-update)
2674   ;; Update summary line after change.
2675   (when (and gnus-summary-default-score
2676              (not gnus-summary-inhibit-highlight))
2677     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2678            (article (gnus-summary-article-number))
2679            (score (gnus-summary-article-score article)))
2680       (unless dont-update
2681         (if (and gnus-summary-mark-below
2682                  (< (gnus-summary-article-score)
2683                     gnus-summary-mark-below))
2684             ;; This article has a low score, so we mark it as read.
2685             (when (memq article gnus-newsgroup-unreads)
2686               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2687           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2688             ;; This article was previously marked as read on account
2689             ;; of a low score, but now it has risen, so we mark it as
2690             ;; unread.
2691             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2692         (gnus-summary-update-mark
2693          (if (or (null gnus-summary-default-score)
2694                  (<= (abs (- score gnus-summary-default-score))
2695                      gnus-summary-zcore-fuzz))
2696              ?  ;Whitespace
2697            (if (< score gnus-summary-default-score)
2698                gnus-score-below-mark gnus-score-over-mark))
2699          'score))
2700       ;; Do visual highlighting.
2701       (when (gnus-visual-p 'summary-highlight 'highlight)
2702         (gnus-run-hooks 'gnus-summary-update-hook)))))
2703
2704 (defvar gnus-tmp-new-adopts nil)
2705
2706 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2707   "Return the number of articles in THREAD.
2708 This may be 0 in some cases -- if none of the articles in
2709 the thread are to be displayed."
2710   (let* ((number
2711           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2712           (cond
2713            ((not (listp thread))
2714             1)
2715            ((and (consp thread) (cdr thread))
2716             (apply
2717              '+ 1 (mapcar
2718                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2719            ((null thread)
2720             1)
2721            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2722             1)
2723            (t 0))))
2724     (when (and level (zerop level) gnus-tmp-new-adopts)
2725       (incf number
2726             (apply '+ (mapcar
2727                        'gnus-summary-number-of-articles-in-thread
2728                        gnus-tmp-new-adopts))))
2729     (if char
2730         (if (> number 1) gnus-not-empty-thread-mark
2731           gnus-empty-thread-mark)
2732       number)))
2733
2734 (defun gnus-summary-set-local-parameters (group)
2735   "Go through the local params of GROUP and set all variable specs in that list."
2736   (let ((params (gnus-group-find-parameter group))
2737         elem)
2738     (while params
2739       (setq elem (car params)
2740             params (cdr params))
2741       (and (consp elem)                 ; Has to be a cons.
2742            (consp (cdr elem))           ; The cdr has to be a list.
2743            (symbolp (car elem))         ; Has to be a symbol in there.
2744            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2745            (ignore-errors               ; So we set it.
2746              (make-local-variable (car elem))
2747              (set (car elem) (eval (nth 1 elem))))))))
2748
2749 (defun gnus-summary-read-group (group &optional show-all no-article
2750                                       kill-buffer no-display backward
2751                                       select-articles)
2752   "Start reading news in newsgroup GROUP.
2753 If SHOW-ALL is non-nil, already read articles are also listed.
2754 If NO-ARTICLE is non-nil, no article is selected initially.
2755 If NO-DISPLAY, don't generate a summary buffer."
2756   (let (result)
2757     (while (and group
2758                 (null (setq result
2759                             (let ((gnus-auto-select-next nil))
2760                               (or (gnus-summary-read-group-1
2761                                    group show-all no-article
2762                                    kill-buffer no-display
2763                                    select-articles)
2764                                   (setq show-all nil
2765                                         select-articles nil)))))
2766                 (eq gnus-auto-select-next 'quietly))
2767       (set-buffer gnus-group-buffer)
2768       ;; The entry function called above goes to the next
2769       ;; group automatically, so we go two groups back
2770       ;; if we are searching for the previous group.
2771       (when backward
2772         (gnus-group-prev-unread-group 2))
2773       (if (not (equal group (gnus-group-group-name)))
2774           (setq group (gnus-group-group-name))
2775         (setq group nil)))
2776     result))
2777
2778 (defun gnus-summary-read-group-1 (group show-all no-article
2779                                         kill-buffer no-display
2780                                         &optional select-articles)
2781   ;; Killed foreign groups can't be entered.
2782   (when (and (not (gnus-group-native-p group))
2783              (not (gnus-gethash group gnus-newsrc-hashtb)))
2784     (error "Dead non-native groups can't be entered"))
2785   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2786   (let* ((new-group (gnus-summary-setup-buffer group))
2787          (quit-config (gnus-group-quit-config group))
2788          (did-select (and new-group (gnus-select-newsgroup
2789                                      group show-all select-articles))))
2790     (cond
2791      ;; This summary buffer exists already, so we just select it.
2792      ((not new-group)
2793       (gnus-set-global-variables)
2794       (when kill-buffer
2795         (gnus-kill-or-deaden-summary kill-buffer))
2796       (gnus-configure-windows 'summary 'force)
2797       (gnus-set-mode-line 'summary)
2798       (gnus-summary-position-point)
2799       (message "")
2800       t)
2801      ;; We couldn't select this group.
2802      ((null did-select)
2803       (when (and (eq major-mode 'gnus-summary-mode)
2804                  (not (equal (current-buffer) kill-buffer)))
2805         (kill-buffer (current-buffer))
2806         (if (not quit-config)
2807             (progn
2808               ;; Update the info -- marks might need to be removed,
2809               ;; for instance.
2810               (gnus-summary-update-info)
2811               (set-buffer gnus-group-buffer)
2812               (gnus-group-jump-to-group group)
2813               (gnus-group-next-unread-group 1))
2814           (gnus-handle-ephemeral-exit quit-config)))
2815       (gnus-message 3 "Can't select group")
2816       nil)
2817      ;; The user did a `C-g' while prompting for number of articles,
2818      ;; so we exit this group.
2819      ((eq did-select 'quit)
2820       (and (eq major-mode 'gnus-summary-mode)
2821            (not (equal (current-buffer) kill-buffer))
2822            (kill-buffer (current-buffer)))
2823       (when kill-buffer
2824         (gnus-kill-or-deaden-summary kill-buffer))
2825       (if (not quit-config)
2826           (progn
2827             (set-buffer gnus-group-buffer)
2828             (gnus-group-jump-to-group group)
2829             (gnus-group-next-unread-group 1)
2830             (gnus-configure-windows 'group 'force))
2831         (gnus-handle-ephemeral-exit quit-config))
2832       ;; Finally signal the quit.
2833       (signal 'quit nil))
2834      ;; The group was successfully selected.
2835      (t
2836       (gnus-set-global-variables)
2837       ;; Save the active value in effect when the group was entered.
2838       (setq gnus-newsgroup-active
2839             (gnus-copy-sequence
2840              (gnus-active gnus-newsgroup-name)))
2841       ;; You can change the summary buffer in some way with this hook.
2842       (gnus-run-hooks 'gnus-select-group-hook)
2843       ;; Set any local variables in the group parameters.
2844       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2845       (gnus-update-format-specifications
2846        nil 'summary 'summary-mode 'summary-dummy)
2847       (gnus-update-summary-mark-positions)
2848       ;; Do score processing.
2849       (when gnus-use-scoring
2850         (gnus-possibly-score-headers))
2851       ;; Check whether to fill in the gaps in the threads.
2852       (when gnus-build-sparse-threads
2853         (gnus-build-sparse-threads))
2854       ;; Find the initial limit.
2855       (if gnus-show-threads
2856           (if show-all
2857               (let ((gnus-newsgroup-dormant nil))
2858                 (gnus-summary-initial-limit show-all))
2859             (gnus-summary-initial-limit show-all))
2860         ;; When untreaded, all articles are always shown.
2861         (setq gnus-newsgroup-limit
2862               (mapcar
2863                (lambda (header) (mail-header-number header))
2864                gnus-newsgroup-headers)))
2865       ;; Generate the summary buffer.
2866       (unless no-display
2867         (gnus-summary-prepare))
2868       (when gnus-use-trees
2869         (gnus-tree-open group)
2870         (setq gnus-summary-highlight-line-function
2871               'gnus-tree-highlight-article))
2872       ;; If the summary buffer is empty, but there are some low-scored
2873       ;; articles or some excluded dormants, we include these in the
2874       ;; buffer.
2875       (when (and (zerop (buffer-size))
2876                  (not no-display))
2877         (cond (gnus-newsgroup-dormant
2878                (gnus-summary-limit-include-dormant))
2879               ((and gnus-newsgroup-scored show-all)
2880                (gnus-summary-limit-include-expunged t))))
2881       ;; Function `gnus-apply-kill-file' must be called in this hook.
2882       (gnus-run-hooks 'gnus-apply-kill-hook)
2883       (if (and (zerop (buffer-size))
2884                (not no-display))
2885           (progn
2886             ;; This newsgroup is empty.
2887             (gnus-summary-catchup-and-exit nil t)
2888             (gnus-message 6 "No unread news")
2889             (when kill-buffer
2890               (gnus-kill-or-deaden-summary kill-buffer))
2891             ;; Return nil from this function.
2892             nil)
2893         ;; Hide conversation thread subtrees.  We cannot do this in
2894         ;; gnus-summary-prepare-hook since kill processing may not
2895         ;; work with hidden articles.
2896         (and gnus-show-threads
2897              gnus-thread-hide-subtree
2898              (gnus-summary-hide-all-threads))
2899         (when kill-buffer
2900           (gnus-kill-or-deaden-summary kill-buffer))
2901         ;; Show first unread article if requested.
2902         (if (and (not no-article)
2903                  (not no-display)
2904                  gnus-newsgroup-unreads
2905                  gnus-auto-select-first)
2906             (progn
2907               (gnus-configure-windows 'summary)
2908               (cond
2909                ((eq gnus-auto-select-first 'best)
2910                 (gnus-summary-best-unread-article))
2911                ((eq gnus-auto-select-first t)
2912                 (gnus-summary-first-unread-article))
2913                ((gnus-functionp gnus-auto-select-first)
2914                 (funcall gnus-auto-select-first))))
2915           ;; Don't select any articles, just move point to the first
2916           ;; article in the group.
2917           (goto-char (point-min))
2918           (gnus-summary-position-point)
2919           (gnus-configure-windows 'summary 'force)
2920           (gnus-set-mode-line 'summary))
2921         (when (get-buffer-window gnus-group-buffer t)
2922           ;; Gotta use windows, because recenter does weird stuff if
2923           ;; the current buffer ain't the displayed window.
2924           (let ((owin (selected-window)))
2925             (select-window (get-buffer-window gnus-group-buffer t))
2926             (when (gnus-group-goto-group group)
2927               (recenter))
2928             (select-window owin)))
2929         ;; Mark this buffer as "prepared".
2930         (setq gnus-newsgroup-prepared t)
2931         (gnus-run-hooks 'gnus-summary-prepared-hook)
2932         t)))))
2933
2934 (defun gnus-summary-prepare ()
2935   "Generate the summary buffer."
2936   (interactive)
2937   (let ((buffer-read-only nil))
2938     (erase-buffer)
2939     (setq gnus-newsgroup-data nil
2940           gnus-newsgroup-data-reverse nil)
2941     (gnus-run-hooks 'gnus-summary-generate-hook)
2942     ;; Generate the buffer, either with threads or without.
2943     (when gnus-newsgroup-headers
2944       (gnus-summary-prepare-threads
2945        (if gnus-show-threads
2946            (gnus-sort-gathered-threads
2947             (funcall gnus-summary-thread-gathering-function
2948                      (gnus-sort-threads
2949                       (gnus-cut-threads (gnus-make-threads)))))
2950          ;; Unthreaded display.
2951          (gnus-sort-articles gnus-newsgroup-headers))))
2952     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2953     ;; Call hooks for modifying summary buffer.
2954     (goto-char (point-min))
2955     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2956
2957 (defsubst gnus-general-simplify-subject (subject)
2958   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2959   (setq subject
2960         (cond
2961          ;; Truncate the subject.
2962          (gnus-simplify-subject-functions
2963           (gnus-map-function gnus-simplify-subject-functions subject))
2964          ((numberp gnus-summary-gather-subject-limit)
2965           (setq subject (gnus-simplify-subject-re subject))
2966           (if (> (length subject) gnus-summary-gather-subject-limit)
2967               (substring subject 0 gnus-summary-gather-subject-limit)
2968             subject))
2969          ;; Fuzzily simplify it.
2970          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2971           (gnus-simplify-subject-fuzzy subject))
2972          ;; Just remove the leading "Re:".
2973          (t
2974           (gnus-simplify-subject-re subject))))
2975
2976   (if (and gnus-summary-gather-exclude-subject
2977            (string-match gnus-summary-gather-exclude-subject subject))
2978       nil                               ; This article shouldn't be gathered
2979     subject))
2980
2981 (defun gnus-summary-simplify-subject-query ()
2982   "Query where the respool algorithm would put this article."
2983   (interactive)
2984   (gnus-summary-select-article)
2985   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2986
2987 (defun gnus-gather-threads-by-subject (threads)
2988   "Gather threads by looking at Subject headers."
2989   (if (not gnus-summary-make-false-root)
2990       threads
2991     (let ((hashtb (gnus-make-hashtable 1024))
2992           (prev threads)
2993           (result threads)
2994           subject hthread whole-subject)
2995       (while threads
2996         (setq subject (gnus-general-simplify-subject
2997                        (setq whole-subject (mail-header-subject
2998                                             (caar threads)))))
2999         (when subject
3000           (if (setq hthread (gnus-gethash subject hashtb))
3001               (progn
3002                 ;; We enter a dummy root into the thread, if we
3003                 ;; haven't done that already.
3004                 (unless (stringp (caar hthread))
3005                   (setcar hthread (list whole-subject (car hthread))))
3006                 ;; We add this new gathered thread to this gathered
3007                 ;; thread.
3008                 (setcdr (car hthread)
3009                         (nconc (cdar hthread) (list (car threads))))
3010                 ;; Remove it from the list of threads.
3011                 (setcdr prev (cdr threads))
3012                 (setq threads prev))
3013             ;; Enter this thread into the hash table.
3014             (gnus-sethash subject threads hashtb)))
3015         (setq prev threads)
3016         (setq threads (cdr threads)))
3017       result)))
3018
3019 (defun gnus-gather-threads-by-references (threads)
3020   "Gather threads by looking at References headers."
3021   (let ((idhashtb (gnus-make-hashtable 1024))
3022         (thhashtb (gnus-make-hashtable 1024))
3023         (prev threads)
3024         (result threads)
3025         ids references id gthread gid entered ref)
3026     (while threads
3027       (when (setq references (mail-header-references (caar threads)))
3028         (setq id (mail-header-id (caar threads))
3029               ids (gnus-split-references references)
3030               entered nil)
3031         (while (setq ref (pop ids))
3032           (setq ids (delete ref ids))
3033           (if (not (setq gid (gnus-gethash ref idhashtb)))
3034               (progn
3035                 (gnus-sethash ref id idhashtb)
3036                 (gnus-sethash id threads thhashtb))
3037             (setq gthread (gnus-gethash gid thhashtb))
3038             (unless entered
3039               ;; We enter a dummy root into the thread, if we
3040               ;; haven't done that already.
3041               (unless (stringp (caar gthread))
3042                 (setcar gthread (list (mail-header-subject (caar gthread))
3043                                       (car gthread))))
3044               ;; We add this new gathered thread to this gathered
3045               ;; thread.
3046               (setcdr (car gthread)
3047                       (nconc (cdar gthread) (list (car threads)))))
3048             ;; Add it into the thread hash table.
3049             (gnus-sethash id gthread thhashtb)
3050             (setq entered t)
3051             ;; Remove it from the list of threads.
3052             (setcdr prev (cdr threads))
3053             (setq threads prev))))
3054       (setq prev threads)
3055       (setq threads (cdr threads)))
3056     result))
3057
3058 (defun gnus-sort-gathered-threads (threads)
3059   "Sort subtreads inside each gathered thread by article number."
3060   (let ((result threads))
3061     (while threads
3062       (when (stringp (caar threads))
3063         (setcdr (car threads)
3064                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3065       (setq threads (cdr threads)))
3066     result))
3067
3068 (defun gnus-thread-loop-p (root thread)
3069   "Say whether ROOT is in THREAD."
3070   (let ((stack (list thread))
3071         (infloop 0)
3072         th)
3073     (while (setq thread (pop stack))
3074       (setq th (cdr thread))
3075       (while (and th
3076                   (not (eq (caar th) root)))
3077         (pop th))
3078       (if th
3079           ;; We have found a loop.
3080           (let (ref-dep)
3081             (setcdr thread (delq (car th) (cdr thread)))
3082             (if (boundp (setq ref-dep (intern "none"
3083                                               gnus-newsgroup-dependencies)))
3084                 (setcdr (symbol-value ref-dep)
3085                         (nconc (cdr (symbol-value ref-dep))
3086                                (list (car th))))
3087               (set ref-dep (list nil (car th))))
3088             (setq infloop 1
3089                   stack nil))
3090         ;; Push all the subthreads onto the stack.
3091         (push (cdr thread) stack)))
3092     infloop))
3093
3094 (defun gnus-make-threads ()
3095   "Go through the dependency hashtb and find the roots.  Return all threads."
3096   (let (threads)
3097     (while (catch 'infloop
3098              (mapatoms
3099               (lambda (refs)
3100                 ;; Deal with self-referencing References loops.
3101                 (when (and (car (symbol-value refs))
3102                            (not (zerop
3103                                  (apply
3104                                   '+
3105                                   (mapcar
3106                                    (lambda (thread)
3107                                      (gnus-thread-loop-p
3108                                       (car (symbol-value refs)) thread))
3109                                    (cdr (symbol-value refs)))))))
3110                   (setq threads nil)
3111                   (throw 'infloop t))
3112                 (unless (car (symbol-value refs))
3113                   ;; These threads do not refer back to any other articles,
3114                   ;; so they're roots.
3115                   (setq threads (append (cdr (symbol-value refs)) threads))))
3116               gnus-newsgroup-dependencies)))
3117     threads))
3118
3119 ;; Build the thread tree.
3120 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3121   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3122
3123 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3124 if it was already present.
3125
3126 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3127 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3128 Message-IDs will be renamed be renamed to a unique Message-ID before
3129 being entered.
3130
3131 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3132   (let* ((id (mail-header-id header))
3133          (id-dep (and id (intern id dependencies)))
3134          ref ref-dep ref-header)
3135     ;; Enter this `header' in the `dependencies' table.
3136     (cond
3137      ((not id-dep)
3138       (setq header nil))
3139      ;; The first two cases do the normal part: enter a new `header'
3140      ;; in the `dependencies' table.
3141      ((not (boundp id-dep))
3142       (set id-dep (list header)))
3143      ((null (car (symbol-value id-dep)))
3144       (setcar (symbol-value id-dep) header))
3145
3146      ;; From here the `header' was already present in the
3147      ;; `dependencies' table.
3148      (force-new
3149       ;; Overrides an existing entry;
3150       ;; just set the header part of the entry.
3151       (setcar (symbol-value id-dep) header))
3152
3153      ;; Renames the existing `header' to a unique Message-ID.
3154      ((not gnus-summary-ignore-duplicates)
3155       ;; An article with this Message-ID has already been seen.
3156       ;; We rename the Message-ID.
3157       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3158            (list header))
3159       (mail-header-set-id header id))
3160
3161      ;; The last case ignores an existing entry, except it adds any
3162      ;; additional Xrefs (in case the two articles came from different
3163      ;; servers.
3164      ;; Also sets `header' to `nil' meaning that the `dependencies'
3165      ;; table was *not* modified.
3166      (t
3167       (mail-header-set-xref
3168        (car (symbol-value id-dep))
3169        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3170                    "")
3171                (or (mail-header-xref header) "")))
3172       (setq header nil)))
3173
3174     (when header
3175       ;; First check if that we are not creating a References loop.
3176       (setq ref (gnus-parent-id (mail-header-references header)))
3177       (while (and ref
3178                   (setq ref-dep (intern-soft ref dependencies))
3179                   (boundp ref-dep)
3180                   (setq ref-header (car (symbol-value ref-dep))))
3181         (if (string= id ref)
3182             ;; Yuk!  This is a reference loop.  Make the article be a
3183             ;; root article.
3184             (progn
3185               (mail-header-set-references (car (symbol-value id-dep)) "none")
3186               (setq ref nil))
3187           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3188       (setq ref (gnus-parent-id (mail-header-references header)))
3189       (setq ref-dep (intern (or ref "none") dependencies))
3190       (if (boundp ref-dep)
3191           (setcdr (symbol-value ref-dep)
3192                   (nconc (cdr (symbol-value ref-dep))
3193                          (list (symbol-value id-dep))))
3194         (set ref-dep (list nil (symbol-value id-dep)))))
3195     header))
3196
3197 (defun gnus-build-sparse-threads ()
3198   (let ((headers gnus-newsgroup-headers)
3199         (mail-parse-charset gnus-newsgroup-charset)
3200         (gnus-summary-ignore-duplicates t)
3201         header references generation relations
3202         subject child end new-child date)
3203     ;; First we create an alist of generations/relations, where
3204     ;; generations is how much we trust the relation, and the relation
3205     ;; is parent/child.
3206     (gnus-message 7 "Making sparse threads...")
3207     (save-excursion
3208       (nnheader-set-temp-buffer " *gnus sparse threads*")
3209       (while (setq header (pop headers))
3210         (when (and (setq references (mail-header-references header))
3211                    (not (string= references "")))
3212           (insert references)
3213           (setq child (mail-header-id header)
3214                 subject (mail-header-subject header)
3215                 date (mail-header-date header)
3216                 generation 0)
3217           (while (search-backward ">" nil t)
3218             (setq end (1+ (point)))
3219             (when (search-backward "<" nil t)
3220               (setq new-child (buffer-substring (point) end))
3221               (push (list (incf generation)
3222                           child (setq child new-child)
3223                           subject date)
3224                     relations)))
3225           (when child
3226             (push (list (1+ generation) child nil subject) relations))
3227           (erase-buffer)))
3228       (kill-buffer (current-buffer)))
3229     ;; Sort over trustworthiness.
3230     (mapcar
3231      (lambda (relation)
3232        (when (gnus-dependencies-add-header
3233               (make-full-mail-header
3234                gnus-reffed-article-number
3235                (nth 3 relation) "" (or (nth 4 relation) "")
3236                (nth 1 relation)
3237                (or (nth 2 relation) "") 0 0 "")
3238               gnus-newsgroup-dependencies nil)
3239          (push gnus-reffed-article-number gnus-newsgroup-limit)
3240          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3241          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3242                gnus-newsgroup-reads)
3243          (decf gnus-reffed-article-number)))
3244      (sort relations 'car-less-than-car))
3245     (gnus-message 7 "Making sparse threads...done")))
3246
3247 (defun gnus-build-old-threads ()
3248   ;; Look at all the articles that refer back to old articles, and
3249   ;; fetch the headers for the articles that aren't there.  This will
3250   ;; build complete threads - if the roots haven't been expired by the
3251   ;; server, that is.
3252   (let ((mail-parse-charset gnus-newsgroup-charset)
3253         id heads)
3254     (mapatoms
3255      (lambda (refs)
3256        (when (not (car (symbol-value refs)))
3257          (setq heads (cdr (symbol-value refs)))
3258          (while heads
3259            (if (memq (mail-header-number (caar heads))
3260                      gnus-newsgroup-dormant)
3261                (setq heads (cdr heads))
3262              (setq id (symbol-name refs))
3263              (while (and (setq id (gnus-build-get-header id))
3264                          (not (car (gnus-id-to-thread id)))))
3265              (setq heads nil)))))
3266      gnus-newsgroup-dependencies)))
3267
3268 ;; This function has to be called with point after the article number
3269 ;; on the beginning of the line.
3270 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3271   (let ((eol (gnus-point-at-eol))
3272         (buffer (current-buffer))
3273         header)
3274
3275     ;; overview: [num subject from date id refs chars lines misc]
3276     (unwind-protect
3277         (progn
3278           (narrow-to-region (point) eol)
3279           (unless (eobp)
3280             (forward-char))
3281
3282           (setq header
3283                 (make-full-mail-header
3284                  number                 ; number
3285                  (funcall gnus-decode-encoded-word-function
3286                           (nnheader-nov-field)) ; subject
3287                  (funcall gnus-decode-encoded-word-function
3288                           (nnheader-nov-field)) ; from
3289                  (nnheader-nov-field)   ; date
3290                  (nnheader-nov-read-message-id) ; id
3291                  (nnheader-nov-field)   ; refs
3292                  (nnheader-nov-read-integer) ; chars
3293                  (nnheader-nov-read-integer) ; lines
3294                  (unless (eobp)
3295                    (nnheader-nov-field)) ; misc
3296                  (nnheader-nov-parse-extra)))) ; extra
3297
3298       (widen))
3299
3300     (when gnus-alter-header-function
3301       (funcall gnus-alter-header-function header))
3302     (gnus-dependencies-add-header header dependencies force-new)))
3303
3304 (defun gnus-build-get-header (id)
3305   ;; Look through the buffer of NOV lines and find the header to
3306   ;; ID.  Enter this line into the dependencies hash table, and return
3307   ;; the id of the parent article (if any).
3308   (let ((deps gnus-newsgroup-dependencies)
3309         found header)
3310     (prog1
3311         (save-excursion
3312           (set-buffer nntp-server-buffer)
3313           (let ((case-fold-search nil))
3314             (goto-char (point-min))
3315             (while (and (not found)
3316                         (search-forward id nil t))
3317               (beginning-of-line)
3318               (setq found (looking-at
3319                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3320                                    (regexp-quote id))))
3321               (or found (beginning-of-line 2)))
3322             (when found
3323               (beginning-of-line)
3324               (and
3325                (setq header (gnus-nov-parse-line
3326                              (read (current-buffer)) deps))
3327                (gnus-parent-id (mail-header-references header))))))
3328       (when header
3329         (let ((number (mail-header-number header)))
3330           (push number gnus-newsgroup-limit)
3331           (push header gnus-newsgroup-headers)
3332           (if (memq number gnus-newsgroup-unselected)
3333               (progn
3334                 (push number gnus-newsgroup-unreads)
3335                 (setq gnus-newsgroup-unselected
3336                       (delq number gnus-newsgroup-unselected)))
3337             (push number gnus-newsgroup-ancient)))))))
3338
3339 (defun gnus-build-all-threads ()
3340   "Read all the headers."
3341   (let ((gnus-summary-ignore-duplicates t)
3342         (mail-parse-charset gnus-newsgroup-charset)
3343         (dependencies gnus-newsgroup-dependencies)
3344         header article)
3345     (save-excursion
3346       (set-buffer nntp-server-buffer)
3347       (let ((case-fold-search nil))
3348         (goto-char (point-min))
3349         (while (not (eobp))
3350           (ignore-errors
3351             (setq article (read (current-buffer))
3352                   header (gnus-nov-parse-line article dependencies)))
3353           (when header
3354             (save-excursion
3355               (set-buffer gnus-summary-buffer)
3356               (push header gnus-newsgroup-headers)
3357               (if (memq (setq article (mail-header-number header))
3358                         gnus-newsgroup-unselected)
3359                   (progn
3360                     (push article gnus-newsgroup-unreads)
3361                     (setq gnus-newsgroup-unselected
3362                           (delq article gnus-newsgroup-unselected)))
3363                 (push article gnus-newsgroup-ancient)))
3364             (forward-line 1)))))))
3365
3366 (defun gnus-summary-update-article-line (article header)
3367   "Update the line for ARTICLE using HEADERS."
3368   (let* ((id (mail-header-id header))
3369          (thread (gnus-id-to-thread id)))
3370     (unless thread
3371       (error "Article in no thread"))
3372     ;; Update the thread.
3373     (setcar thread header)
3374     (gnus-summary-goto-subject article)
3375     (let* ((datal (gnus-data-find-list article))
3376            (data (car datal))
3377            (length (when (cdr datal)
3378                      (- (gnus-data-pos data)
3379                         (gnus-data-pos (cadr datal)))))
3380            (buffer-read-only nil)
3381            (level (gnus-summary-thread-level)))
3382       (gnus-delete-line)
3383       (gnus-summary-insert-line
3384        header level nil (gnus-article-mark article)
3385        (memq article gnus-newsgroup-replied)
3386        (memq article gnus-newsgroup-expirable)
3387        ;; Only insert the Subject string when it's different
3388        ;; from the previous Subject string.
3389        (if (and
3390             gnus-show-threads
3391             (gnus-subject-equal
3392              (condition-case ()
3393                  (mail-header-subject
3394                   (gnus-data-header
3395                    (cadr
3396                     (gnus-data-find-list
3397                      article
3398                      (gnus-data-list t)))))
3399                ;; Error on the side of excessive subjects.
3400                (error ""))
3401              (mail-header-subject header)))
3402            ""
3403          (mail-header-subject header))
3404        nil (cdr (assq article gnus-newsgroup-scored))
3405        (memq article gnus-newsgroup-processable))
3406       (when length
3407         (gnus-data-update-list
3408          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3409
3410 (defun gnus-summary-update-article (article &optional iheader)
3411   "Update ARTICLE in the summary buffer."
3412   (set-buffer gnus-summary-buffer)
3413   (let* ((header (gnus-summary-article-header article))
3414          (id (mail-header-id header))
3415          (data (gnus-data-find article))
3416          (thread (gnus-id-to-thread id))
3417          (references (mail-header-references header))
3418          (parent
3419           (gnus-id-to-thread
3420            (or (gnus-parent-id
3421                 (when (and references
3422                            (not (equal "" references)))
3423                   references))
3424                "none")))
3425          (buffer-read-only nil)
3426          (old (car thread)))
3427     (when thread
3428       (unless iheader
3429         (setcar thread nil)
3430         (when parent
3431           (delq thread parent)))
3432       (if (gnus-summary-insert-subject id header)
3433           ;; Set the (possibly) new article number in the data structure.
3434           (gnus-data-set-number data (gnus-id-to-article id))
3435         (setcar thread old)
3436         nil))))
3437
3438 (defun gnus-rebuild-thread (id &optional line)
3439   "Rebuild the thread containing ID.
3440 If LINE, insert the rebuilt thread starting on line LINE."
3441   (let ((buffer-read-only nil)
3442         old-pos current thread data)
3443     (if (not gnus-show-threads)
3444         (setq thread (list (car (gnus-id-to-thread id))))
3445       ;; Get the thread this article is part of.
3446       (setq thread (gnus-remove-thread id)))
3447     (setq old-pos (gnus-point-at-bol))
3448     (setq current (save-excursion
3449                     (and (zerop (forward-line -1))
3450                          (gnus-summary-article-number))))
3451     ;; If this is a gathered thread, we have to go some re-gathering.
3452     (when (stringp (car thread))
3453       (let ((subject (car thread))
3454             roots thr)
3455         (setq thread (cdr thread))
3456         (while thread
3457           (unless (memq (setq thr (gnus-id-to-thread
3458                                    (gnus-root-id
3459                                     (mail-header-id (caar thread)))))
3460                         roots)
3461             (push thr roots))
3462           (setq thread (cdr thread)))
3463         ;; We now have all (unique) roots.
3464         (if (= (length roots) 1)
3465             ;; All the loose roots are now one solid root.
3466             (setq thread (car roots))
3467           (setq thread (cons subject (gnus-sort-threads roots))))))
3468     (let (threads)
3469       ;; We then insert this thread into the summary buffer.
3470       (when line
3471         (goto-char (point-min))
3472         (forward-line (1- line)))
3473       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3474         (if gnus-show-threads
3475             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3476           (gnus-summary-prepare-unthreaded thread))
3477         (setq data (nreverse gnus-newsgroup-data))
3478         (setq threads gnus-newsgroup-threads))
3479       ;; We splice the new data into the data structure.
3480       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3481       ;;!!! then we want to insert at the beginning of the buffer.
3482       ;;!!! That happens to be true with Gnus now, but that may
3483       ;;!!! change in the future.  Perhaps.
3484       (gnus-data-enter-list
3485        (if line nil current) data (- (point) old-pos))
3486       (setq gnus-newsgroup-threads
3487             (nconc threads gnus-newsgroup-threads))
3488       (gnus-data-compute-positions))))
3489
3490 (defun gnus-number-to-header (number)
3491   "Return the header for article NUMBER."
3492   (let ((headers gnus-newsgroup-headers))
3493     (while (and headers
3494                 (not (= number (mail-header-number (car headers)))))
3495       (pop headers))
3496     (when headers
3497       (car headers))))
3498
3499 (defun gnus-parent-headers (in-headers &optional generation)
3500   "Return the headers of the GENERATIONeth parent of HEADERS."
3501   (unless generation
3502     (setq generation 1))
3503   (let ((parent t)
3504         (headers in-headers)
3505         references)
3506     (while (and parent
3507                 (not (zerop generation))
3508                 (setq references (mail-header-references headers)))
3509       (setq headers (if (and references
3510                              (setq parent (gnus-parent-id references)))
3511                         (car (gnus-id-to-thread parent))
3512                       nil))
3513       (decf generation))
3514     (and (not (eq headers in-headers))
3515          headers)))
3516
3517 (defun gnus-id-to-thread (id)
3518   "Return the (sub-)thread where ID appears."
3519   (gnus-gethash id gnus-newsgroup-dependencies))
3520
3521 (defun gnus-id-to-article (id)
3522   "Return the article number of ID."
3523   (let ((thread (gnus-id-to-thread id)))
3524     (when (and thread
3525                (car thread))
3526       (mail-header-number (car thread)))))
3527
3528 (defun gnus-id-to-header (id)
3529   "Return the article headers of ID."
3530   (car (gnus-id-to-thread id)))
3531
3532 (defun gnus-article-displayed-root-p (article)
3533   "Say whether ARTICLE is a root(ish) article."
3534   (let ((level (gnus-summary-thread-level article))
3535         (refs (mail-header-references  (gnus-summary-article-header article)))
3536         particle)
3537     (cond
3538      ((null level) nil)
3539      ((zerop level) t)
3540      ((null refs) t)
3541      ((null (gnus-parent-id refs)) t)
3542      ((and (= 1 level)
3543            (null (setq particle (gnus-id-to-article
3544                                  (gnus-parent-id refs))))
3545            (null (gnus-summary-thread-level particle)))))))
3546
3547 (defun gnus-root-id (id)
3548   "Return the id of the root of the thread where ID appears."
3549   (let (last-id prev)
3550     (while (and id (setq prev (car (gnus-id-to-thread id))))
3551       (setq last-id id
3552             id (gnus-parent-id (mail-header-references prev))))
3553     last-id))
3554
3555 (defun gnus-articles-in-thread (thread)
3556   "Return the list of articles in THREAD."
3557   (cons (mail-header-number (car thread))
3558         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3559
3560 (defun gnus-remove-thread (id &optional dont-remove)
3561   "Remove the thread that has ID in it."
3562   (let (headers thread last-id)
3563     ;; First go up in this thread until we find the root.
3564     (setq last-id (gnus-root-id id)
3565           headers (message-flatten-list (gnus-id-to-thread last-id)))
3566     ;; We have now found the real root of this thread.  It might have
3567     ;; been gathered into some loose thread, so we have to search
3568     ;; through the threads to find the thread we wanted.
3569     (let ((threads gnus-newsgroup-threads)
3570           sub)
3571       (while threads
3572         (setq sub (car threads))
3573         (if (stringp (car sub))
3574             ;; This is a gathered thread, so we look at the roots
3575             ;; below it to find whether this article is in this
3576             ;; gathered root.
3577             (progn
3578               (setq sub (cdr sub))
3579               (while sub
3580                 (when (member (caar sub) headers)
3581                   (setq thread (car threads)
3582                         threads nil
3583                         sub nil))
3584                 (setq sub (cdr sub))))
3585           ;; It's an ordinary thread, so we check it.
3586           (when (eq (car sub) (car headers))
3587             (setq thread sub
3588                   threads nil)))
3589         (setq threads (cdr threads)))
3590       ;; If this article is in no thread, then it's a root.
3591       (if thread
3592           (unless dont-remove
3593             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3594         (setq thread (gnus-id-to-thread last-id)))
3595       (when thread
3596         (prog1
3597             thread                      ; We return this thread.
3598           (unless dont-remove
3599             (if (stringp (car thread))
3600                 (progn
3601                   ;; If we use dummy roots, then we have to remove the
3602                   ;; dummy root as well.
3603                   (when (eq gnus-summary-make-false-root 'dummy)
3604                     ;; We go to the dummy root by going to
3605                     ;; the first sub-"thread", and then one line up.
3606                     (gnus-summary-goto-article
3607                      (mail-header-number (caadr thread)))
3608                     (forward-line -1)
3609                     (gnus-delete-line)
3610                     (gnus-data-compute-positions))
3611                   (setq thread (cdr thread))
3612                   (while thread
3613                     (gnus-remove-thread-1 (car thread))
3614                     (setq thread (cdr thread))))
3615               (gnus-remove-thread-1 thread))))))))
3616
3617 (defun gnus-remove-thread-1 (thread)
3618   "Remove the thread THREAD recursively."
3619   (let ((number (mail-header-number (pop thread)))
3620         d)
3621     (setq thread (reverse thread))
3622     (while thread
3623       (gnus-remove-thread-1 (pop thread)))
3624     (when (setq d (gnus-data-find number))
3625       (goto-char (gnus-data-pos d))
3626       (gnus-summary-show-thread)
3627       (gnus-data-remove
3628        number
3629        (- (gnus-point-at-bol)
3630           (prog1
3631               (1+ (gnus-point-at-eol))
3632             (gnus-delete-line)))))))
3633
3634 (defun gnus-sort-threads (threads)
3635   "Sort THREADS."
3636   (if (not gnus-thread-sort-functions)
3637       threads
3638     (gnus-message 8 "Sorting threads...")
3639     (prog1
3640         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3641       (gnus-message 8 "Sorting threads...done"))))
3642
3643 (defun gnus-sort-articles (articles)
3644   "Sort ARTICLES."
3645   (when gnus-article-sort-functions
3646     (gnus-message 7 "Sorting articles...")
3647     (prog1
3648         (setq gnus-newsgroup-headers
3649               (sort articles (gnus-make-sort-function
3650                               gnus-article-sort-functions)))
3651       (gnus-message 7 "Sorting articles...done"))))
3652
3653 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3654 (defmacro gnus-thread-header (thread)
3655   ;; Return header of first article in THREAD.
3656   ;; Note that THREAD must never, ever be anything else than a variable -
3657   ;; using some other form will lead to serious barfage.
3658   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3659   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3660   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" 
3661         (vector thread) 2))
3662
3663 (defsubst gnus-article-sort-by-number (h1 h2)
3664   "Sort articles by article number."
3665   (< (mail-header-number h1)
3666      (mail-header-number h2)))
3667
3668 (defun gnus-thread-sort-by-number (h1 h2)
3669   "Sort threads by root article number."
3670   (gnus-article-sort-by-number
3671    (gnus-thread-header h1) (gnus-thread-header h2)))
3672
3673 (defsubst gnus-article-sort-by-lines (h1 h2)
3674   "Sort articles by article Lines header."
3675   (< (mail-header-lines h1)
3676      (mail-header-lines h2)))
3677
3678 (defun gnus-thread-sort-by-lines (h1 h2)
3679   "Sort threads by root article Lines header."
3680   (gnus-article-sort-by-lines
3681    (gnus-thread-header h1) (gnus-thread-header h2)))
3682
3683 (defsubst gnus-article-sort-by-chars (h1 h2)
3684   "Sort articles by octet length."
3685   (< (mail-header-chars h1)
3686      (mail-header-chars h2)))
3687
3688 (defun gnus-thread-sort-by-chars (h1 h2)
3689   "Sort threads by root article octet length."
3690   (gnus-article-sort-by-chars
3691    (gnus-thread-header h1) (gnus-thread-header h2)))
3692
3693 (defsubst gnus-article-sort-by-author (h1 h2)
3694   "Sort articles by root author."
3695   (string-lessp
3696    (let ((extract (funcall
3697                    gnus-extract-address-components
3698                    (mail-header-from h1))))
3699      (or (car extract) (cadr extract) ""))
3700    (let ((extract (funcall
3701                    gnus-extract-address-components
3702                    (mail-header-from h2))))
3703      (or (car extract) (cadr extract) ""))))
3704
3705 (defun gnus-thread-sort-by-author (h1 h2)
3706   "Sort threads by root author."
3707   (gnus-article-sort-by-author
3708    (gnus-thread-header h1)  (gnus-thread-header h2)))
3709
3710 (defsubst gnus-article-sort-by-subject (h1 h2)
3711   "Sort articles by root subject."
3712   (string-lessp
3713    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3714    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3715
3716 (defun gnus-thread-sort-by-subject (h1 h2)
3717   "Sort threads by root subject."
3718   (gnus-article-sort-by-subject
3719    (gnus-thread-header h1) (gnus-thread-header h2)))
3720
3721 (defsubst gnus-article-sort-by-date (h1 h2)
3722   "Sort articles by root article date."
3723   (time-less-p
3724    (gnus-date-get-time (mail-header-date h1))
3725    (gnus-date-get-time (mail-header-date h2))))
3726
3727 (defun gnus-thread-sort-by-date (h1 h2)
3728   "Sort threads by root article date."
3729   (gnus-article-sort-by-date
3730    (gnus-thread-header h1) (gnus-thread-header h2)))
3731
3732 (defsubst gnus-article-sort-by-score (h1 h2)
3733   "Sort articles by root article score.
3734 Unscored articles will be counted as having a score of zero."
3735   (> (or (cdr (assq (mail-header-number h1)
3736                     gnus-newsgroup-scored))
3737          gnus-summary-default-score 0)
3738      (or (cdr (assq (mail-header-number h2)
3739                     gnus-newsgroup-scored))
3740          gnus-summary-default-score 0)))
3741
3742 (defun gnus-thread-sort-by-score (h1 h2)
3743   "Sort threads by root article score."
3744   (gnus-article-sort-by-score
3745    (gnus-thread-header h1) (gnus-thread-header h2)))
3746
3747 (defun gnus-thread-sort-by-total-score (h1 h2)
3748   "Sort threads by the sum of all scores in the thread.
3749 Unscored articles will be counted as having a score of zero."
3750   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3751
3752 (defun gnus-thread-total-score (thread)
3753   ;; This function find the total score of THREAD.
3754   (cond ((null thread)
3755          0)
3756         ((consp thread)
3757          (if (stringp (car thread))
3758              (apply gnus-thread-score-function 0
3759                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3760            (gnus-thread-total-score-1 thread)))
3761         (t
3762          (gnus-thread-total-score-1 (list thread)))))
3763
3764 (defun gnus-thread-total-score-1 (root)
3765   ;; This function find the total score of the thread below ROOT.
3766   (setq root (car root))
3767   (apply gnus-thread-score-function
3768          (or (append
3769               (mapcar 'gnus-thread-total-score
3770                       (cdr (gnus-id-to-thread (mail-header-id root))))
3771               (when (> (mail-header-number root) 0)
3772                 (list (or (cdr (assq (mail-header-number root)
3773                                      gnus-newsgroup-scored))
3774                           gnus-summary-default-score 0))))
3775              (list gnus-summary-default-score)
3776              '(0))))
3777
3778 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3779 (defvar gnus-tmp-prev-subject nil)
3780 (defvar gnus-tmp-false-parent nil)
3781 (defvar gnus-tmp-root-expunged nil)
3782 (defvar gnus-tmp-dummy-line nil)
3783
3784 (defvar gnus-tmp-header)
3785 (defun gnus-extra-header (type &optional header)
3786   "Return the extra header of TYPE."
3787   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3788       ""))
3789
3790 (defun gnus-summary-prepare-threads (threads)
3791   "Prepare summary buffer from THREADS and indentation LEVEL.
3792 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3793 or a straight list of headers."
3794   (gnus-message 7 "Generating summary...")
3795
3796   (setq gnus-newsgroup-threads threads)
3797   (beginning-of-line)
3798
3799   (let ((gnus-tmp-level 0)
3800         (default-score (or gnus-summary-default-score 0))
3801         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3802         thread number subject stack state gnus-tmp-gathered beg-match
3803         new-roots gnus-tmp-new-adopts thread-end
3804         gnus-tmp-header gnus-tmp-unread
3805         gnus-tmp-replied gnus-tmp-subject-or-nil
3806         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3807         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3808         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3809
3810     (setq gnus-tmp-prev-subject nil)
3811
3812     (if (vectorp (car threads))
3813         ;; If this is a straight (sic) list of headers, then a
3814         ;; threaded summary display isn't required, so we just create
3815         ;; an unthreaded one.
3816         (gnus-summary-prepare-unthreaded threads)
3817
3818       ;; Do the threaded display.
3819
3820       (while (or threads stack gnus-tmp-new-adopts new-roots)
3821
3822         (if (and (= gnus-tmp-level 0)
3823                  (or (not stack)
3824                      (= (caar stack) 0))
3825                  (not gnus-tmp-false-parent)
3826                  (or gnus-tmp-new-adopts new-roots))
3827             (if gnus-tmp-new-adopts
3828                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3829                       thread (list (car gnus-tmp-new-adopts))
3830                       gnus-tmp-header (caar thread)
3831                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3832               (when new-roots
3833                 (setq thread (list (car new-roots))
3834                       gnus-tmp-header (caar thread)
3835                       new-roots (cdr new-roots))))
3836
3837           (if threads
3838               ;; If there are some threads, we do them before the
3839               ;; threads on the stack.
3840               (setq thread threads
3841                     gnus-tmp-header (caar thread))
3842             ;; There were no current threads, so we pop something off
3843             ;; the stack.
3844             (setq state (car stack)
3845                   gnus-tmp-level (car state)
3846                   thread (cdr state)
3847                   stack (cdr stack)
3848                   gnus-tmp-header (caar thread))))
3849
3850         (setq gnus-tmp-false-parent nil)
3851         (setq gnus-tmp-root-expunged nil)
3852         (setq thread-end nil)
3853
3854         (if (stringp gnus-tmp-header)
3855             ;; The header is a dummy root.
3856             (cond
3857              ((eq gnus-summary-make-false-root 'adopt)
3858               ;; We let the first article adopt the rest.
3859               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3860                                                (cddar thread)))
3861               (setq gnus-tmp-gathered
3862                     (nconc (mapcar
3863                             (lambda (h) (mail-header-number (car h)))
3864                             (cddar thread))
3865                            gnus-tmp-gathered))
3866               (setq thread (cons (list (caar thread)
3867                                        (cadar thread))
3868                                  (cdr thread)))
3869               (setq gnus-tmp-level -1
3870                     gnus-tmp-false-parent t))
3871              ((eq gnus-summary-make-false-root 'empty)
3872               ;; We print adopted articles with empty subject fields.
3873               (setq gnus-tmp-gathered
3874                     (nconc (mapcar
3875                             (lambda (h) (mail-header-number (car h)))
3876                             (cddar thread))
3877                            gnus-tmp-gathered))
3878               (setq gnus-tmp-level -1))
3879              ((eq gnus-summary-make-false-root 'dummy)
3880               ;; We remember that we probably want to output a dummy
3881               ;; root.
3882               (setq gnus-tmp-dummy-line gnus-tmp-header)
3883               (setq gnus-tmp-prev-subject gnus-tmp-header))
3884              (t
3885               ;; We do not make a root for the gathered
3886               ;; sub-threads at all.
3887               (setq gnus-tmp-level -1)))
3888
3889           (setq number (mail-header-number gnus-tmp-header)
3890                 subject (mail-header-subject gnus-tmp-header))
3891
3892           (cond
3893            ;; If the thread has changed subject, we might want to make
3894            ;; this subthread into a root.
3895            ((and (null gnus-thread-ignore-subject)
3896                  (not (zerop gnus-tmp-level))
3897                  gnus-tmp-prev-subject
3898                  (not (inline
3899                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3900             (setq new-roots (nconc new-roots (list (car thread)))
3901                   thread-end t
3902                   gnus-tmp-header nil))
3903            ;; If the article lies outside the current limit,
3904            ;; then we do not display it.
3905            ((not (memq number gnus-newsgroup-limit))
3906             (setq gnus-tmp-gathered
3907                   (nconc (mapcar
3908                           (lambda (h) (mail-header-number (car h)))
3909                           (cdar thread))
3910                          gnus-tmp-gathered))
3911             (setq gnus-tmp-new-adopts (if (cdar thread)
3912                                           (append gnus-tmp-new-adopts
3913                                                   (cdar thread))
3914                                         gnus-tmp-new-adopts)
3915                   thread-end t
3916                   gnus-tmp-header nil)
3917             (when (zerop gnus-tmp-level)
3918               (setq gnus-tmp-root-expunged t)))
3919            ;; Perhaps this article is to be marked as read?
3920            ((and gnus-summary-mark-below
3921                  (< (or (cdr (assq number gnus-newsgroup-scored))
3922                         default-score)
3923                     gnus-summary-mark-below)
3924                  ;; Don't touch sparse articles.
3925                  (not (gnus-summary-article-sparse-p number))
3926                  (not (gnus-summary-article-ancient-p number)))
3927             (setq gnus-newsgroup-unreads
3928                   (delq number gnus-newsgroup-unreads))
3929             (if gnus-newsgroup-auto-expire
3930                 (push number gnus-newsgroup-expirable)
3931               (push (cons number gnus-low-score-mark)
3932                     gnus-newsgroup-reads))))
3933
3934           (when gnus-tmp-header
3935             ;; We may have an old dummy line to output before this
3936             ;; article.
3937             (when (and gnus-tmp-dummy-line
3938                        (gnus-subject-equal
3939                         gnus-tmp-dummy-line
3940                         (mail-header-subject gnus-tmp-header)))
3941               (gnus-summary-insert-dummy-line
3942                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3943               (setq gnus-tmp-dummy-line nil))
3944
3945             ;; Compute the mark.
3946             (setq gnus-tmp-unread (gnus-article-mark number))
3947
3948             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3949                                   gnus-tmp-header gnus-tmp-level)
3950                   gnus-newsgroup-data)
3951
3952             ;; Actually insert the line.
3953             (setq
3954              gnus-tmp-subject-or-nil
3955              (cond
3956               ((and gnus-thread-ignore-subject
3957                     gnus-tmp-prev-subject
3958                     (not (inline (gnus-subject-equal
3959                                   gnus-tmp-prev-subject subject))))
3960                subject)
3961               ((zerop gnus-tmp-level)
3962                (if (and (eq gnus-summary-make-false-root 'empty)
3963                         (memq number gnus-tmp-gathered)
3964                         gnus-tmp-prev-subject
3965                         (inline (gnus-subject-equal
3966                                  gnus-tmp-prev-subject subject)))
3967                    gnus-summary-same-subject
3968                  subject))
3969               (t gnus-summary-same-subject)))
3970             (if (and (eq gnus-summary-make-false-root 'adopt)
3971                      (= gnus-tmp-level 1)
3972                      (memq number gnus-tmp-gathered))
3973                 (setq gnus-tmp-opening-bracket ?\<
3974                       gnus-tmp-closing-bracket ?\>)
3975               (setq gnus-tmp-opening-bracket ?\[
3976                     gnus-tmp-closing-bracket ?\]))
3977             (setq
3978              gnus-tmp-indentation
3979              (aref gnus-thread-indent-array gnus-tmp-level)
3980              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3981              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3982                                 gnus-summary-default-score 0)
3983              gnus-tmp-score-char
3984              (if (or (null gnus-summary-default-score)
3985                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3986                          gnus-summary-zcore-fuzz))
3987                  ?  ;Whitespace
3988                (if (< gnus-tmp-score gnus-summary-default-score)
3989                    gnus-score-below-mark gnus-score-over-mark))
3990              gnus-tmp-replied
3991              (cond ((memq number gnus-newsgroup-processable)
3992                     gnus-process-mark)
3993                    ((memq number gnus-newsgroup-cached)
3994                     gnus-cached-mark)
3995                    ((memq number gnus-newsgroup-replied)
3996                     gnus-replied-mark)
3997                    ((memq number gnus-newsgroup-saved)
3998                     gnus-saved-mark)
3999                    (t gnus-unread-mark))
4000              gnus-tmp-from (mail-header-from gnus-tmp-header)
4001              gnus-tmp-name
4002              (cond
4003               ((string-match "<[^>]+> *$" gnus-tmp-from)
4004                (setq beg-match (match-beginning 0))
4005                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4006                         (substring gnus-tmp-from (1+ (match-beginning 0))
4007                                    (1- (match-end 0))))
4008                    (substring gnus-tmp-from 0 beg-match)))
4009               ((string-match "(.+)" gnus-tmp-from)
4010                (substring gnus-tmp-from
4011                           (1+ (match-beginning 0)) (1- (match-end 0))))
4012               (t gnus-tmp-from)))
4013             (when (string= gnus-tmp-name "")
4014               (setq gnus-tmp-name gnus-tmp-from))
4015             (unless (numberp gnus-tmp-lines)
4016               (setq gnus-tmp-lines 0))
4017             (gnus-put-text-property
4018              (point)
4019              (progn (eval gnus-summary-line-format-spec) (point))
4020              'gnus-number number)
4021             (when gnus-visual-p
4022               (forward-line -1)
4023               (gnus-run-hooks 'gnus-summary-update-hook)
4024               (forward-line 1))
4025
4026             (setq gnus-tmp-prev-subject subject)))
4027
4028         (when (nth 1 thread)
4029           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4030         (incf gnus-tmp-level)
4031         (setq threads (if thread-end nil (cdar thread)))
4032         (unless threads
4033           (setq gnus-tmp-level 0)))))
4034   (gnus-message 7 "Generating summary...done"))
4035
4036 (defun gnus-summary-prepare-unthreaded (headers)
4037   "Generate an unthreaded summary buffer based on HEADERS."
4038   (let (header number mark)
4039
4040     (beginning-of-line)
4041
4042     (while headers
4043       ;; We may have to root out some bad articles...
4044       (when (memq (setq number (mail-header-number
4045                                 (setq header (pop headers))))
4046                   gnus-newsgroup-limit)
4047         ;; Mark article as read when it has a low score.
4048         (when (and gnus-summary-mark-below
4049                    (< (or (cdr (assq number gnus-newsgroup-scored))
4050                           gnus-summary-default-score 0)
4051                       gnus-summary-mark-below)
4052                    (not (gnus-summary-article-ancient-p number)))
4053           (setq gnus-newsgroup-unreads
4054                 (delq number gnus-newsgroup-unreads))
4055           (if gnus-newsgroup-auto-expire
4056               (push number gnus-newsgroup-expirable)
4057             (push (cons number gnus-low-score-mark)
4058                   gnus-newsgroup-reads)))
4059
4060         (setq mark (gnus-article-mark number))
4061         (push (gnus-data-make number mark (1+ (point)) header 0)
4062               gnus-newsgroup-data)
4063         (gnus-summary-insert-line
4064          header 0 number
4065          mark (memq number gnus-newsgroup-replied)
4066          (memq number gnus-newsgroup-expirable)
4067          (mail-header-subject header) nil
4068          (cdr (assq number gnus-newsgroup-scored))
4069          (memq number gnus-newsgroup-processable))))))
4070
4071 (defun gnus-summary-remove-list-identifiers ()
4072   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4073   (let ((regexp (if (stringp gnus-list-identifiers)
4074                     gnus-list-identifiers
4075                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4076     (dolist (header gnus-newsgroup-headers)
4077       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4078                                   " *\\)\\)+\\(Re: +\\)?\\)")
4079                           (mail-header-subject header))
4080         (mail-header-set-subject
4081          header (concat (substring (mail-header-subject header)
4082                                    0 (match-beginning 1))
4083                         (or
4084                          (match-string 3 (mail-header-subject header))
4085                          (match-string 5 (mail-header-subject header)))
4086                         (substring (mail-header-subject header)
4087                                    (match-end 1))))))))
4088
4089 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4090   "Select newsgroup GROUP.
4091 If READ-ALL is non-nil, all articles in the group are selected.
4092 If SELECT-ARTICLES, only select those articles from GROUP."
4093   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4094          ;;!!! Dirty hack; should be removed.
4095          (gnus-summary-ignore-duplicates
4096           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4097               t
4098             gnus-summary-ignore-duplicates))
4099          (info (nth 2 entry))
4100          articles fetched-articles cached)
4101
4102     (unless (gnus-check-server
4103              (setq gnus-current-select-method
4104                    (gnus-find-method-for-group group)))
4105       (error "Couldn't open server"))
4106
4107     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4108         (gnus-activate-group group)     ; Or we can activate it...
4109         (progn                          ; Or we bug out.
4110           (when (equal major-mode 'gnus-summary-mode)
4111             (kill-buffer (current-buffer)))
4112           (error "Couldn't request group %s: %s"
4113                  group (gnus-status-message group))))
4114
4115     (unless (gnus-request-group group t)
4116       (when (equal major-mode 'gnus-summary-mode)
4117         (kill-buffer (current-buffer)))
4118       (error "Couldn't request group %s: %s"
4119              group (gnus-status-message group)))
4120
4121     (setq gnus-newsgroup-name group)
4122     (setq gnus-newsgroup-unselected nil)
4123     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4124     (gnus-summary-setup-default-charset)
4125
4126     ;; Adjust and set lists of article marks.
4127     (when info
4128       (gnus-adjust-marked-articles info))
4129
4130     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4131     (when (gnus-virtual-group-p group)
4132       (setq cached gnus-newsgroup-cached))
4133
4134     (setq gnus-newsgroup-unreads
4135           (gnus-set-difference
4136            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4137            gnus-newsgroup-dormant))
4138
4139     (setq gnus-newsgroup-processable nil)
4140
4141     (gnus-update-read-articles group gnus-newsgroup-unreads)
4142
4143     (if (setq articles select-articles)
4144         (setq gnus-newsgroup-unselected
4145               (gnus-sorted-intersection
4146                gnus-newsgroup-unreads
4147                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4148       (setq articles (gnus-articles-to-read group read-all)))
4149
4150     (cond
4151      ((null articles)
4152       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4153       'quit)
4154      ((eq articles 0) nil)
4155      (t
4156       ;; Init the dependencies hash table.
4157       (setq gnus-newsgroup-dependencies
4158             (gnus-make-hashtable (length articles)))
4159       (gnus-set-global-variables)
4160       ;; Retrieve the headers and read them in.
4161       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4162       (setq gnus-newsgroup-headers
4163             (if (eq 'nov
4164                     (setq gnus-headers-retrieved-by
4165                           (gnus-retrieve-headers
4166                            articles gnus-newsgroup-name
4167                            ;; We might want to fetch old headers, but
4168                            ;; not if there is only 1 article.
4169                            (and (or (and
4170                                      (not (eq gnus-fetch-old-headers 'some))
4171                                      (not (numberp gnus-fetch-old-headers)))
4172                                     (> (length articles) 1))
4173                                 gnus-fetch-old-headers))))
4174                 (gnus-get-newsgroup-headers-xover
4175                  articles nil nil gnus-newsgroup-name t)
4176               (gnus-get-newsgroup-headers)))
4177       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4178
4179       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4180       (when cached
4181         (setq gnus-newsgroup-cached cached))
4182
4183       ;; Suppress duplicates?
4184       (when gnus-suppress-duplicates
4185         (gnus-dup-suppress-articles))
4186
4187       ;; Set the initial limit.
4188       (setq gnus-newsgroup-limit (copy-sequence articles))
4189       ;; Remove canceled articles from the list of unread articles.
4190       (setq gnus-newsgroup-unreads
4191             (gnus-set-sorted-intersection
4192              gnus-newsgroup-unreads
4193              (setq fetched-articles
4194                    (mapcar (lambda (headers) (mail-header-number headers))
4195                            gnus-newsgroup-headers))))
4196       ;; Removed marked articles that do not exist.
4197       (gnus-update-missing-marks
4198        (gnus-sorted-complement fetched-articles articles))
4199       ;; We might want to build some more threads first.
4200       (when (and gnus-fetch-old-headers
4201                  (eq gnus-headers-retrieved-by 'nov))
4202         (if (eq gnus-fetch-old-headers 'invisible)
4203             (gnus-build-all-threads)
4204           (gnus-build-old-threads)))
4205       ;; Let the Gnus agent mark articles as read.
4206       (when gnus-agent
4207         (gnus-agent-get-undownloaded-list))
4208       ;; Remove list identifiers from subject
4209       (when gnus-list-identifiers
4210         (gnus-summary-remove-list-identifiers))
4211       ;; Check whether auto-expire is to be done in this group.
4212       (setq gnus-newsgroup-auto-expire
4213             (gnus-group-auto-expirable-p group))
4214       ;; Set up the article buffer now, if necessary.
4215       (unless gnus-single-article-buffer
4216         (gnus-article-setup-buffer))
4217       ;; First and last article in this newsgroup.
4218       (when gnus-newsgroup-headers
4219         (setq gnus-newsgroup-begin
4220               (mail-header-number (car gnus-newsgroup-headers))
4221               gnus-newsgroup-end
4222               (mail-header-number
4223                (gnus-last-element gnus-newsgroup-headers))))
4224       ;; GROUP is successfully selected.
4225       (or gnus-newsgroup-headers t)))))
4226
4227 (defun gnus-articles-to-read (group &optional read-all)
4228   ;; Find out what articles the user wants to read.
4229   (let* ((articles
4230           ;; Select all articles if `read-all' is non-nil, or if there
4231           ;; are no unread articles.
4232           (if (or read-all
4233                   (and (zerop (length gnus-newsgroup-marked))
4234                        (zerop (length gnus-newsgroup-unreads)))
4235                   (eq (gnus-group-find-parameter group 'display)
4236                       'all))
4237               (or
4238                (gnus-uncompress-range (gnus-active group))
4239                (gnus-cache-articles-in-group group))
4240             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4241                           (copy-sequence gnus-newsgroup-unreads))
4242                   '<)))
4243          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4244          (scored (length scored-list))
4245          (number (length articles))
4246          (marked (+ (length gnus-newsgroup-marked)
4247                     (length gnus-newsgroup-dormant)))
4248          (select
4249           (cond
4250            ((numberp read-all)
4251             read-all)
4252            (t
4253             (condition-case ()
4254                 (cond
4255                  ((and (or (<= scored marked) (= scored number))
4256                        (numberp gnus-large-newsgroup)
4257                        (> number gnus-large-newsgroup))
4258                   (let ((input
4259                          (read-string
4260                           (format
4261                            "How many articles from %s (default %d): "
4262                            (gnus-limit-string gnus-newsgroup-name 35)
4263                            number))))
4264                     (if (string-match "^[ \t]*$" input) number input)))
4265                  ((and (> scored marked) (< scored number)
4266                        (> (- scored number) 20))
4267                   (let ((input
4268                          (read-string
4269                           (format "%s %s (%d scored, %d total): "
4270                                   "How many articles from"
4271                                   group scored number))))
4272                     (if (string-match "^[ \t]*$" input)
4273                         number input)))
4274                  (t number))
4275               (quit nil))))))
4276     (setq select (if (stringp select) (string-to-number select) select))
4277     (if (or (null select) (zerop select))
4278         select
4279       (if (and (not (zerop scored)) (<= (abs select) scored))
4280           (progn
4281             (setq articles (sort scored-list '<))
4282             (setq number (length articles)))
4283         (setq articles (copy-sequence articles)))
4284
4285       (when (< (abs select) number)
4286         (if (< select 0)
4287             ;; Select the N oldest articles.
4288             (setcdr (nthcdr (1- (abs select)) articles) nil)
4289           ;; Select the N most recent articles.
4290           (setq articles (nthcdr (- number select) articles))))
4291       (setq gnus-newsgroup-unselected
4292             (gnus-sorted-intersection
4293              gnus-newsgroup-unreads
4294              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4295       (when gnus-alter-articles-to-read-function
4296         (setq gnus-newsgroup-unreads
4297               (sort 
4298                (funcall gnus-alter-articles-to-read-function
4299                         gnus-newsgroup-name gnus-newsgroup-unreads)
4300                '<)))
4301       articles)))
4302
4303 (defun gnus-killed-articles (killed articles)
4304   (let (out)
4305     (while articles
4306       (when (inline (gnus-member-of-range (car articles) killed))
4307         (push (car articles) out))
4308       (setq articles (cdr articles)))
4309     out))
4310
4311 (defun gnus-uncompress-marks (marks)
4312   "Uncompress the mark ranges in MARKS."
4313   (let ((uncompressed '(score bookmark))
4314         out)
4315     (while marks
4316       (if (memq (caar marks) uncompressed)
4317           (push (car marks) out)
4318         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4319       (setq marks (cdr marks)))
4320     out))
4321
4322 (defun gnus-adjust-marked-articles (info)
4323   "Set all article lists and remove all marks that are no longer valid."
4324   (let* ((marked-lists (gnus-info-marks info))
4325          (active (gnus-active (gnus-info-group info)))
4326          (min (car active))
4327          (max (cdr active))
4328          (types gnus-article-mark-lists)
4329          (uncompressed '(score bookmark killed))
4330          marks var articles article mark)
4331
4332     (while marked-lists
4333       (setq marks (pop marked-lists))
4334       (set (setq var (intern (format "gnus-newsgroup-%s"
4335                                      (car (rassq (setq mark (car marks))
4336                                                  types)))))
4337            (if (memq (car marks) uncompressed) (cdr marks)
4338              (gnus-uncompress-range (cdr marks))))
4339
4340       (setq articles (symbol-value var))
4341
4342       ;; All articles have to be subsets of the active articles.
4343       (cond
4344        ;; Adjust "simple" lists.
4345        ((memq mark '(tick dormant expire reply save))
4346         (while articles
4347           (when (or (< (setq article (pop articles)) min) (> article max))
4348             (set var (delq article (symbol-value var))))))
4349        ;; Adjust assocs.
4350        ((memq mark uncompressed)
4351         (when (not (listp (cdr (symbol-value var))))
4352           (set var (list (symbol-value var))))
4353         (when (not (listp (cdr articles)))
4354           (setq articles (list articles)))
4355         (while articles
4356           (when (or (not (consp (setq article (pop articles))))
4357                     (< (car article) min)
4358                     (> (car article) max))
4359             (set var (delq article (symbol-value var))))))))))
4360
4361 (defun gnus-update-missing-marks (missing)
4362   "Go through the list of MISSING articles and remove them from the mark lists."
4363   (when missing
4364     (let ((types gnus-article-mark-lists)
4365           var m)
4366       ;; Go through all types.
4367       (while types
4368         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4369         (when (symbol-value var)
4370           ;; This list has articles.  So we delete all missing articles
4371           ;; from it.
4372           (setq m missing)
4373           (while m
4374             (set var (delq (pop m) (symbol-value var)))))))))
4375
4376 (defun gnus-update-marks ()
4377   "Enter the various lists of marked articles into the newsgroup info list."
4378   (let ((types gnus-article-mark-lists)
4379         (info (gnus-get-info gnus-newsgroup-name))
4380         (uncompressed '(score bookmark killed))
4381         type list newmarked symbol delta-marks)
4382     (when info
4383       ;; Add all marks lists to the list of marks lists.
4384       (while (setq type (pop types))
4385         (setq list (symbol-value
4386                     (setq symbol
4387                           (intern (format "gnus-newsgroup-%s"
4388                                           (car type))))))
4389
4390         (when list
4391           ;; Get rid of the entries of the articles that have the
4392           ;; default score.
4393           (when (and (eq (cdr type) 'score)
4394                      gnus-save-score
4395                      list)
4396             (let* ((arts list)
4397                    (prev (cons nil list))
4398                    (all prev))
4399               (while arts
4400                 (if (or (not (consp (car arts)))
4401                         (= (cdar arts) gnus-summary-default-score))
4402                     (setcdr prev (cdr arts))
4403                   (setq prev arts))
4404                 (setq arts (cdr arts)))
4405               (setq list (cdr all)))))
4406
4407         (unless (memq (cdr type) uncompressed)
4408           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4409        
4410         (when (gnus-check-backend-function
4411                'request-set-mark gnus-newsgroup-name)
4412           ;; uncompressed:s are not proper flags (they are cons cells)
4413           ;; cache is a internal gnus flag
4414           (unless (memq (cdr type) (cons 'cache uncompressed))
4415             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4416                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4417                    (add (gnus-remove-from-range
4418                          (gnus-copy-sequence list) old)))
4419               (when add
4420                 (push (list add 'add (list (cdr type))) delta-marks))
4421               (when del
4422                 (push (list del 'del (list (cdr type))) delta-marks)))))
4423           
4424         (when list
4425           (push (cons (cdr type) list) newmarked)))
4426
4427       (when delta-marks
4428         (unless (gnus-check-group gnus-newsgroup-name)
4429           (error "Can't open server for %s" gnus-newsgroup-name))
4430         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4431           
4432       ;; Enter these new marks into the info of the group.
4433       (if (nthcdr 3 info)
4434           (setcar (nthcdr 3 info) newmarked)
4435         ;; Add the marks lists to the end of the info.
4436         (when newmarked
4437           (setcdr (nthcdr 2 info) (list newmarked))))
4438
4439       ;; Cut off the end of the info if there's nothing else there.
4440       (let ((i 5))
4441         (while (and (> i 2)
4442                     (not (nth i info)))
4443           (when (nthcdr (decf i) info)
4444             (setcdr (nthcdr i info) nil)))))))
4445
4446 (defun gnus-set-mode-line (where)
4447   "This function sets the mode line of the article or summary buffers.
4448 If WHERE is `summary', the summary mode line format will be used."
4449   ;; Is this mode line one we keep updated?
4450   (when (and (memq where gnus-updated-mode-lines)
4451              (symbol-value
4452               (intern (format "gnus-%s-mode-line-format-spec" where))))
4453     (let (mode-string)
4454       (save-excursion
4455         ;; We evaluate this in the summary buffer since these
4456         ;; variables are buffer-local to that buffer.
4457         (set-buffer gnus-summary-buffer)
4458         ;; We bind all these variables that are used in the `eval' form
4459         ;; below.
4460         (let* ((mformat (symbol-value
4461                          (intern
4462                           (format "gnus-%s-mode-line-format-spec" where))))
4463                (gnus-tmp-group-name gnus-newsgroup-name)
4464                (gnus-tmp-article-number (or gnus-current-article 0))
4465                (gnus-tmp-unread gnus-newsgroup-unreads)
4466                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4467                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4468                (gnus-tmp-unread-and-unselected
4469                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4470                             (zerop gnus-tmp-unselected))
4471                        "")
4472                       ((zerop gnus-tmp-unselected)
4473                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4474                       (t (format "{%d(+%d) more}"
4475                                  gnus-tmp-unread-and-unticked
4476                                  gnus-tmp-unselected))))
4477                (gnus-tmp-subject
4478                 (if (and gnus-current-headers
4479                          (vectorp gnus-current-headers))
4480                     (gnus-mode-string-quote
4481                      (mail-header-subject gnus-current-headers))
4482                   ""))
4483                bufname-length max-len
4484                gnus-tmp-header);; passed as argument to any user-format-funcs
4485           (setq mode-string (eval mformat))
4486           (setq bufname-length (if (string-match "%b" mode-string)
4487                                    (- (length
4488                                        (buffer-name
4489                                         (if (eq where 'summary)
4490                                             nil
4491                                           (get-buffer gnus-article-buffer))))
4492                                       2)
4493                                  0))
4494           (setq max-len (max 4 (if gnus-mode-non-string-length
4495                                    (- (window-width)
4496                                       gnus-mode-non-string-length
4497                                       bufname-length)
4498                                  (length mode-string))))
4499           ;; We might have to chop a bit of the string off...
4500           (when (> (length mode-string) max-len)
4501             (setq mode-string
4502                   (concat (truncate-string-to-width mode-string (- max-len 3))
4503                           "...")))
4504           ;; Pad the mode string a bit.
4505           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4506       ;; Update the mode line.
4507       (setq mode-line-buffer-identification
4508             (gnus-mode-line-buffer-identification (list mode-string)))
4509       (set-buffer-modified-p t))))
4510
4511 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4512   "Go through the HEADERS list and add all Xrefs to a hash table.
4513 The resulting hash table is returned, or nil if no Xrefs were found."
4514   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4515          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4516          (xref-hashtb (gnus-make-hashtable))
4517          start group entry number xrefs header)
4518     (while headers
4519       (setq header (pop headers))
4520       (when (and (setq xrefs (mail-header-xref header))
4521                  (not (memq (setq number (mail-header-number header))
4522                             unreads)))
4523         (setq start 0)
4524         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4525           (setq start (match-end 0))
4526           (setq group (if prefix
4527                           (concat prefix (substring xrefs (match-beginning 1)
4528                                                     (match-end 1)))
4529                         (substring xrefs (match-beginning 1) (match-end 1))))
4530           (setq number
4531                 (string-to-int (substring xrefs (match-beginning 2)
4532                                           (match-end 2))))
4533           (if (setq entry (gnus-gethash group xref-hashtb))
4534               (setcdr entry (cons number (cdr entry)))
4535             (gnus-sethash group (cons number nil) xref-hashtb)))))
4536     (and start xref-hashtb)))
4537
4538 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4539   "Look through all the headers and mark the Xrefs as read."
4540   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4541         name entry info xref-hashtb idlist method nth4)
4542     (save-excursion
4543       (set-buffer gnus-group-buffer)
4544       (when (setq xref-hashtb
4545                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4546         (mapatoms
4547          (lambda (group)
4548            (unless (string= from-newsgroup (setq name (symbol-name group)))
4549              (setq idlist (symbol-value group))
4550              ;; Dead groups are not updated.
4551              (and (prog1
4552                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4553                             info (nth 2 entry))
4554                     (when (stringp (setq nth4 (gnus-info-method info)))
4555                       (setq nth4 (gnus-server-to-method nth4))))
4556                   ;; Only do the xrefs if the group has the same
4557                   ;; select method as the group we have just read.
4558                   (or (gnus-methods-equal-p
4559                        nth4 (gnus-find-method-for-group from-newsgroup))
4560                       virtual
4561                       (equal nth4 (setq method (gnus-find-method-for-group
4562                                                 from-newsgroup)))
4563                       (and (equal (car nth4) (car method))
4564                            (equal (nth 1 nth4) (nth 1 method))))
4565                   gnus-use-cross-reference
4566                   (or (not (eq gnus-use-cross-reference t))
4567                       virtual
4568                       ;; Only do cross-references on subscribed
4569                       ;; groups, if that is what is wanted.
4570                       (<= (gnus-info-level info) gnus-level-subscribed))
4571                   (gnus-group-make-articles-read name idlist))))
4572          xref-hashtb)))))
4573
4574 (defun gnus-compute-read-articles (group articles)
4575   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4576          (info (nth 2 entry))
4577          (active (gnus-active group))
4578          ninfo)
4579     (when entry
4580       ;; First peel off all invalid article numbers.
4581       (when active
4582         (let ((ids articles)
4583               id first)
4584           (while (setq id (pop ids))
4585             (when (and first (> id (cdr active)))
4586               ;; We'll end up in this situation in one particular
4587               ;; obscure situation.  If you re-scan a group and get
4588               ;; a new article that is cross-posted to a different
4589               ;; group that has not been re-scanned, you might get
4590               ;; crossposted article that has a higher number than
4591               ;; Gnus believes possible.  So we re-activate this
4592               ;; group as well.  This might mean doing the
4593               ;; crossposting thingy will *increase* the number
4594               ;; of articles in some groups.  Tsk, tsk.
4595               (setq active (or (gnus-activate-group group) active)))
4596             (when (or (> id (cdr active))
4597                       (< id (car active)))
4598               (setq articles (delq id articles))))))
4599       ;; If the read list is nil, we init it.
4600       (if (and active
4601                (null (gnus-info-read info))
4602                (> (car active) 1))
4603           (setq ninfo (cons 1 (1- (car active))))
4604         (setq ninfo (gnus-info-read info)))
4605       ;; Then we add the read articles to the range.
4606       (gnus-add-to-range
4607        ninfo (setq articles (sort articles '<))))))
4608
4609 (defun gnus-group-make-articles-read (group articles)
4610   "Update the info of GROUP to say that ARTICLES are read."
4611   (let* ((num 0)
4612          (entry (gnus-gethash group gnus-newsrc-hashtb))
4613          (info (nth 2 entry))
4614          (active (gnus-active group))
4615          range)
4616     (when entry
4617       (setq range (gnus-compute-read-articles group articles))
4618       (save-excursion
4619         (set-buffer gnus-group-buffer)
4620         (gnus-undo-register
4621           `(progn
4622              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4623              (gnus-info-set-read ',info ',(gnus-info-read info))
4624              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4625              (gnus-group-update-group ,group t))))
4626       ;; Add the read articles to the range.
4627       (gnus-info-set-read info range)
4628       ;; Then we have to re-compute how many unread
4629       ;; articles there are in this group.
4630       (when active
4631         (cond
4632          ((not range)
4633           (setq num (- (1+ (cdr active)) (car active))))
4634          ((not (listp (cdr range)))
4635           (setq num (- (cdr active) (- (1+ (cdr range))
4636                                        (car range)))))
4637          (t
4638           (while range
4639             (if (numberp (car range))
4640                 (setq num (1+ num))
4641               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4642             (setq range (cdr range)))
4643           (setq num (- (cdr active) num))))
4644         ;; Update the number of unread articles.
4645         (setcar entry num)
4646         ;; Update the group buffer.
4647         (gnus-group-update-group group t)))))
4648
4649 (defvar gnus-newsgroup-none-id 0)
4650
4651 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4652   (let ((cur nntp-server-buffer)
4653         (dependencies
4654          (or dependencies
4655              (save-excursion (set-buffer gnus-summary-buffer)
4656                              gnus-newsgroup-dependencies)))
4657         headers id end ref
4658         (mail-parse-charset gnus-newsgroup-charset)
4659         (mail-parse-ignored-charsets 
4660          (save-excursion (condition-case nil
4661                              (set-buffer gnus-summary-buffer)
4662                            (error))
4663                          gnus-newsgroup-ignored-charsets)))
4664     (save-excursion
4665       (set-buffer nntp-server-buffer)
4666       ;; Translate all TAB characters into SPACE characters.
4667       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4668       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4669       (gnus-run-hooks 'gnus-parse-headers-hook)
4670       (let ((case-fold-search t)
4671             in-reply-to header p lines chars)
4672         (goto-char (point-min))
4673         ;; Search to the beginning of the next header.  Error messages
4674         ;; do not begin with 2 or 3.
4675         (while (re-search-forward "^[23][0-9]+ " nil t)
4676           (setq id nil
4677                 ref nil)
4678           ;; This implementation of this function, with nine
4679           ;; search-forwards instead of the one re-search-forward and
4680           ;; a case (which basically was the old function) is actually
4681           ;; about twice as fast, even though it looks messier.  You
4682           ;; can't have everything, I guess.  Speed and elegance
4683           ;; doesn't always go hand in hand.
4684           (setq
4685            header
4686            (vector
4687             ;; Number.
4688             (prog1
4689                 (read cur)
4690               (end-of-line)
4691               (setq p (point))
4692               (narrow-to-region (point)
4693                                 (or (and (search-forward "\n.\n" nil t)
4694                                          (- (point) 2))
4695                                     (point))))
4696             ;; Subject.
4697             (progn
4698               (goto-char p)
4699               (if (search-forward "\nsubject: " nil t)
4700                   (funcall gnus-decode-encoded-word-function
4701                            (nnheader-header-value))
4702                 "(none)"))
4703             ;; From.
4704             (progn
4705               (goto-char p)
4706               (if (search-forward "\nfrom: " nil t)
4707                   (funcall gnus-decode-encoded-word-function
4708                            (nnheader-header-value))
4709                 "(nobody)"))
4710             ;; Date.
4711             (progn
4712               (goto-char p)
4713               (if (search-forward "\ndate: " nil t)
4714                   (nnheader-header-value) ""))
4715             ;; Message-ID.
4716             (progn
4717               (goto-char p)
4718               (setq id (if (re-search-forward
4719                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4720                            ;; We do it this way to make sure the Message-ID
4721                            ;; is (somewhat) syntactically valid.
4722                            (buffer-substring (match-beginning 1)
4723                                              (match-end 1))
4724                          ;; If there was no message-id, we just fake one
4725                          ;; to make subsequent routines simpler.
4726                          (nnheader-generate-fake-message-id))))
4727             ;; References.
4728             (progn
4729               (goto-char p)
4730               (if (search-forward "\nreferences: " nil t)
4731                   (progn
4732                     (setq end (point))
4733                     (prog1
4734                         (nnheader-header-value)
4735                       (setq ref
4736                             (buffer-substring
4737                              (progn
4738                                (end-of-line)
4739                                (search-backward ">" end t)
4740                                (1+ (point)))
4741                              (progn
4742                                (search-backward "<" end t)
4743                                (point))))))
4744                 ;; Get the references from the in-reply-to header if there
4745                 ;; were no references and the in-reply-to header looks
4746                 ;; promising.
4747                 (if (and (search-forward "\nin-reply-to: " nil t)
4748                          (setq in-reply-to (nnheader-header-value))
4749                          (string-match "<[^>]+>" in-reply-to))
4750                     (let (ref2)
4751                       (setq ref (substring in-reply-to (match-beginning 0)
4752                                            (match-end 0)))
4753                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4754                         (setq ref2 (substring in-reply-to (match-beginning 0)
4755                                               (match-end 0)))
4756                         (when (> (length ref2) (length ref))
4757                           (setq ref ref2)))
4758                       ref)
4759                   (setq ref nil))))
4760             ;; Chars.
4761             (progn
4762               (goto-char p)
4763               (if (search-forward "\nchars: " nil t)
4764                   (if (numberp (setq chars (ignore-errors (read cur))))
4765                       chars 0)
4766                 0))
4767             ;; Lines.
4768             (progn
4769               (goto-char p)
4770               (if (search-forward "\nlines: " nil t)
4771                   (if (numberp (setq lines (ignore-errors (read cur))))
4772                       lines 0)
4773                 0))
4774             ;; Xref.
4775             (progn
4776               (goto-char p)
4777               (and (search-forward "\nxref: " nil t)
4778                    (nnheader-header-value)))
4779             ;; Extra.
4780             (when gnus-extra-headers
4781               (let ((extra gnus-extra-headers)
4782                     out)
4783                 (while extra
4784                   (goto-char p)
4785                   (when (search-forward
4786                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4787                     (push (cons (car extra) (nnheader-header-value))
4788                           out))
4789                   (pop extra))
4790                 out))))
4791           (when (equal id ref)
4792             (setq ref nil))
4793
4794           (when gnus-alter-header-function
4795             (funcall gnus-alter-header-function header)
4796             (setq id (mail-header-id header)
4797                   ref (gnus-parent-id (mail-header-references header))))
4798
4799           (when (setq header
4800                       (gnus-dependencies-add-header
4801                        header dependencies force-new))
4802             (push header headers))
4803           (goto-char (point-max))
4804           (widen))
4805         (nreverse headers)))))
4806
4807 ;; Goes through the xover lines and returns a list of vectors
4808 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4809                                                   force-new dependencies
4810                                                   group also-fetch-heads)
4811   "Parse the news overview data in the server buffer, and return a
4812 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4813   ;; Get the Xref when the users reads the articles since most/some
4814   ;; NNTP servers do not include Xrefs when using XOVER.
4815   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4816   (let ((mail-parse-charset gnus-newsgroup-charset)
4817         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4818         (cur nntp-server-buffer)
4819         (dependencies (or dependencies gnus-newsgroup-dependencies))
4820         number headers header)
4821     (save-excursion
4822       (set-buffer nntp-server-buffer)
4823       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4824       ;; Allow the user to mangle the headers before parsing them.
4825       (gnus-run-hooks 'gnus-parse-headers-hook)
4826       (goto-char (point-min))
4827       (while (not (eobp))
4828         (condition-case ()
4829             (while (and sequence (not (eobp)))
4830               (setq number (read cur))
4831               (while (and sequence
4832                           (< (car sequence) number))
4833                 (setq sequence (cdr sequence)))
4834               (and sequence
4835                    (eq number (car sequence))
4836                    (progn
4837                      (setq sequence (cdr sequence))
4838                      (setq header (inline
4839                                     (gnus-nov-parse-line
4840                                      number dependencies force-new))))
4841                    (push header headers))
4842               (forward-line 1))
4843           (error
4844            (gnus-error 4 "Strange nov line (%d)"
4845                        (count-lines (point-min) (point)))))
4846         (forward-line 1))
4847       ;; A common bug in inn is that if you have posted an article and
4848       ;; then retrieves the active file, it will answer correctly --
4849       ;; the new article is included.  However, a NOV entry for the
4850       ;; article may not have been generated yet, so this may fail.
4851       ;; We work around this problem by retrieving the last few
4852       ;; headers using HEAD.
4853       (if (or (not also-fetch-heads)
4854               (not sequence))
4855           ;; We (probably) got all the headers.
4856           (nreverse headers)
4857         (let ((gnus-nov-is-evil t))
4858           (nconc
4859            (nreverse headers)
4860            (when (gnus-retrieve-headers sequence group)
4861              (gnus-get-newsgroup-headers))))))))
4862
4863 (defun gnus-article-get-xrefs ()
4864   "Fill in the Xref value in `gnus-current-headers', if necessary.
4865 This is meant to be called in `gnus-article-internal-prepare-hook'."
4866   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4867                                  gnus-current-headers)))
4868     (or (not gnus-use-cross-reference)
4869         (not headers)
4870         (and (mail-header-xref headers)
4871              (not (string= (mail-header-xref headers) "")))
4872         (let ((case-fold-search t)
4873               xref)
4874           (save-restriction
4875             (nnheader-narrow-to-headers)
4876             (goto-char (point-min))
4877             (when (or (and (not (eobp))
4878                            (eq (downcase (char-after)) ?x)
4879                            (looking-at "Xref:"))
4880                       (search-forward "\nXref:" nil t))
4881               (goto-char (1+ (match-end 0)))
4882               (setq xref (buffer-substring (point)
4883                                            (progn (end-of-line) (point))))
4884               (mail-header-set-xref headers xref)))))))
4885
4886 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4887   "Find article ID and insert the summary line for that article.
4888 OLD-HEADER can either be a header or a line number to insert
4889 the subject line on."
4890   (let* ((line (and (numberp old-header) old-header))
4891          (old-header (and (vectorp old-header) old-header))
4892          (header (cond ((and old-header use-old-header)
4893                         old-header)
4894                        ((and (numberp id)
4895                              (gnus-number-to-header id))
4896                         (gnus-number-to-header id))
4897                        (t
4898                         (gnus-read-header id))))
4899          (number (and (numberp id) id))
4900          d)
4901     (when header
4902       ;; Rebuild the thread that this article is part of and go to the
4903       ;; article we have fetched.
4904       (when (and (not gnus-show-threads)
4905                  old-header)
4906         (when (and number
4907                    (setq d (gnus-data-find (mail-header-number old-header))))
4908           (goto-char (gnus-data-pos d))
4909           (gnus-data-remove
4910            number
4911            (- (gnus-point-at-bol)
4912               (prog1
4913                   (1+ (gnus-point-at-eol))
4914                 (gnus-delete-line))))))
4915       (when old-header
4916         (mail-header-set-number header (mail-header-number old-header)))
4917       (setq gnus-newsgroup-sparse
4918             (delq (setq number (mail-header-number header))
4919                   gnus-newsgroup-sparse))
4920       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4921       (push number gnus-newsgroup-limit)
4922       (gnus-rebuild-thread (mail-header-id header) line)
4923       (gnus-summary-goto-subject number nil t))
4924     (when (and (numberp number)
4925                (> number 0))
4926       ;; We have to update the boundaries even if we can't fetch the
4927       ;; article if ID is a number -- so that the next `P' or `N'
4928       ;; command will fetch the previous (or next) article even
4929       ;; if the one we tried to fetch this time has been canceled.
4930       (when (> number gnus-newsgroup-end)
4931         (setq gnus-newsgroup-end number))
4932       (when (< number gnus-newsgroup-begin)
4933         (setq gnus-newsgroup-begin number))
4934       (setq gnus-newsgroup-unselected
4935             (delq number gnus-newsgroup-unselected)))
4936     ;; Report back a success?
4937     (and header (mail-header-number header))))
4938
4939 ;;; Process/prefix in the summary buffer
4940
4941 (defun gnus-summary-work-articles (n)
4942   "Return a list of articles to be worked upon.
4943 The prefix argument, the list of process marked articles, and the
4944 current article will be taken into consideration."
4945   (save-excursion
4946     (set-buffer gnus-summary-buffer)
4947     (cond
4948      (n
4949       ;; A numerical prefix has been given.
4950       (setq n (prefix-numeric-value n))
4951       (let ((backward (< n 0))
4952             (n (abs (prefix-numeric-value n)))
4953             articles article)
4954         (save-excursion
4955           (while
4956               (and (> n 0)
4957                    (push (setq article (gnus-summary-article-number))
4958                          articles)
4959                    (if backward
4960                        (gnus-summary-find-prev nil article)
4961                      (gnus-summary-find-next nil article)))
4962             (decf n)))
4963         (nreverse articles)))
4964      ((and (gnus-region-active-p) (mark))
4965       (message "region active")
4966       ;; Work on the region between point and mark.
4967       (let ((max (max (point) (mark)))
4968             articles article)
4969         (save-excursion
4970           (goto-char (min (min (point) (mark))))
4971           (while
4972               (and
4973                (push (setq article (gnus-summary-article-number)) articles)
4974                (gnus-summary-find-next nil article)
4975                (< (point) max)))
4976           (nreverse articles))))
4977      (gnus-newsgroup-processable
4978       ;; There are process-marked articles present.
4979       ;; Save current state.
4980       (gnus-summary-save-process-mark)
4981       ;; Return the list.
4982       (reverse gnus-newsgroup-processable))
4983      (t
4984       ;; Just return the current article.
4985       (list (gnus-summary-article-number))))))
4986
4987 (defmacro gnus-summary-iterate (arg &rest forms)
4988   "Iterate over the process/prefixed articles and do FORMS.
4989 ARG is the interactive prefix given to the command.  FORMS will be
4990 executed with point over the summary line of the articles."
4991   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4992     `(let ((,articles (gnus-summary-work-articles ,arg)))
4993        (while ,articles
4994          (gnus-summary-goto-subject (car ,articles))
4995          ,@forms
4996          (pop ,articles)))))
4997
4998 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4999 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5000
5001 (defun gnus-summary-save-process-mark ()
5002   "Push the current set of process marked articles on the stack."
5003   (interactive)
5004   (push (copy-sequence gnus-newsgroup-processable)
5005         gnus-newsgroup-process-stack))
5006
5007 (defun gnus-summary-kill-process-mark ()
5008   "Push the current set of process marked articles on the stack and unmark."
5009   (interactive)
5010   (gnus-summary-save-process-mark)
5011   (gnus-summary-unmark-all-processable))
5012
5013 (defun gnus-summary-yank-process-mark ()
5014   "Pop the last process mark state off the stack and restore it."
5015   (interactive)
5016   (unless gnus-newsgroup-process-stack
5017     (error "Empty mark stack"))
5018   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5019
5020 (defun gnus-summary-process-mark-set (set)
5021   "Make SET into the current process marked articles."
5022   (gnus-summary-unmark-all-processable)
5023   (while set
5024     (gnus-summary-set-process-mark (pop set))))
5025
5026 ;;; Searching and stuff
5027
5028 (defun gnus-summary-search-group (&optional backward use-level)
5029   "Search for next unread newsgroup.
5030 If optional argument BACKWARD is non-nil, search backward instead."
5031   (save-excursion
5032     (set-buffer gnus-group-buffer)
5033     (when (gnus-group-search-forward
5034            backward nil (if use-level (gnus-group-group-level) nil))
5035       (gnus-group-group-name))))
5036
5037 (defun gnus-summary-best-group (&optional exclude-group)
5038   "Find the name of the best unread group.
5039 If EXCLUDE-GROUP, do not go to this group."
5040   (save-excursion
5041     (set-buffer gnus-group-buffer)
5042     (save-excursion
5043       (gnus-group-best-unread-group exclude-group))))
5044
5045 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5046   (if backward (gnus-summary-find-prev)
5047     (let* ((dummy (gnus-summary-article-intangible-p))
5048            (article (or article (gnus-summary-article-number)))
5049            (arts (gnus-data-find-list article))
5050            result)
5051       (when (and (not dummy)
5052                  (or (not gnus-summary-check-current)
5053                      (not unread)
5054                      (not (gnus-data-unread-p (car arts)))))
5055         (setq arts (cdr arts)))
5056       (when (setq result
5057                   (if unread
5058                       (progn
5059                         (while arts
5060                           (when (or (and undownloaded
5061                                          (eq gnus-undownloaded-mark
5062                                              (gnus-data-mark (car arts))))
5063                                     (gnus-data-unread-p (car arts)))
5064                             (setq result (car arts)
5065                                   arts nil))
5066                           (setq arts (cdr arts)))
5067                         result)
5068                     (car arts)))
5069         (goto-char (gnus-data-pos result))
5070         (gnus-data-number result)))))
5071
5072 (defun gnus-summary-find-prev (&optional unread article)
5073   (let* ((eobp (eobp))
5074          (article (or article (gnus-summary-article-number)))
5075          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5076          result)
5077     (when (and (not eobp)
5078                (or (not gnus-summary-check-current)
5079                    (not unread)
5080                    (not (gnus-data-unread-p (car arts)))))
5081       (setq arts (cdr arts)))
5082     (when (setq result
5083                 (if unread
5084                     (progn
5085                       (while arts
5086                         (when (gnus-data-unread-p (car arts))
5087                           (setq result (car arts)
5088                                 arts nil))
5089                         (setq arts (cdr arts)))
5090                       result)
5091                   (car arts)))
5092       (goto-char (gnus-data-pos result))
5093       (gnus-data-number result))))
5094
5095 (defun gnus-summary-find-subject (subject &optional unread backward article)
5096   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5097          (article (or article (gnus-summary-article-number)))
5098          (articles (gnus-data-list backward))
5099          (arts (gnus-data-find-list article articles))
5100          result)
5101     (when (or (not gnus-summary-check-current)
5102               (not unread)
5103               (not (gnus-data-unread-p (car arts))))
5104       (setq arts (cdr arts)))
5105     (while arts
5106       (and (or (not unread)
5107                (gnus-data-unread-p (car arts)))
5108            (vectorp (gnus-data-header (car arts)))
5109            (gnus-subject-equal
5110             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5111            (setq result (car arts)
5112                  arts nil))
5113       (setq arts (cdr arts)))
5114     (and result
5115          (goto-char (gnus-data-pos result))
5116          (gnus-data-number result))))
5117
5118 (defun gnus-summary-search-forward (&optional unread subject backward)
5119   "Search forward for an article.
5120 If UNREAD, look for unread articles.  If SUBJECT, look for
5121 articles with that subject.  If BACKWARD, search backward instead."
5122   (cond (subject (gnus-summary-find-subject subject unread backward))
5123         (backward (gnus-summary-find-prev unread))
5124         (t (gnus-summary-find-next unread))))
5125
5126 (defun gnus-recenter (&optional n)
5127   "Center point in window and redisplay frame.
5128 Also do horizontal recentering."
5129   (interactive "P")
5130   (when (and nil
5131              gnus-auto-center-summary
5132              (not (eq gnus-auto-center-summary 'vertical)))
5133     (gnus-horizontal-recenter))
5134   (recenter n))
5135
5136 (defun gnus-summary-recenter ()
5137   "Center point in the summary window.
5138 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5139 displayed, no centering will be performed."
5140   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5141   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5142   (interactive)
5143   (let* ((top (cond ((< (window-height) 4) 0)
5144                     ((< (window-height) 7) 1)
5145                     (t (if (numberp gnus-auto-center-summary)
5146                            gnus-auto-center-summary
5147                          2))))
5148          (height (1- (window-height)))
5149          (bottom (save-excursion (goto-char (point-max))
5150                                  (forward-line (- height))
5151                                  (point)))
5152          (window (get-buffer-window (current-buffer))))
5153     ;; The user has to want it.
5154     (when gnus-auto-center-summary
5155       (when (get-buffer-window gnus-article-buffer)
5156         ;; Only do recentering when the article buffer is displayed,
5157         ;; Set the window start to either `bottom', which is the biggest
5158         ;; possible valid number, or the second line from the top,
5159         ;; whichever is the least.
5160         (set-window-start
5161          window (min bottom (save-excursion
5162                               (forward-line (- top)) (point)))
5163          t))
5164       ;; Do horizontal recentering while we're at it.
5165       (when (and (get-buffer-window (current-buffer) t)
5166                  (not (eq gnus-auto-center-summary 'vertical)))
5167         (let ((selected (selected-window)))
5168           (select-window (get-buffer-window (current-buffer) t))
5169           (gnus-summary-position-point)
5170           (gnus-horizontal-recenter)
5171           (select-window selected))))))
5172
5173 (defun gnus-summary-jump-to-group (newsgroup)
5174   "Move point to NEWSGROUP in group mode buffer."
5175   ;; Keep update point of group mode buffer if visible.
5176   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5177       (save-window-excursion
5178         ;; Take care of tree window mode.
5179         (when (get-buffer-window gnus-group-buffer)
5180           (pop-to-buffer gnus-group-buffer))
5181         (gnus-group-jump-to-group newsgroup))
5182     (save-excursion
5183       ;; Take care of tree window mode.
5184       (if (get-buffer-window gnus-group-buffer)
5185           (pop-to-buffer gnus-group-buffer)
5186         (set-buffer gnus-group-buffer))
5187       (gnus-group-jump-to-group newsgroup))))
5188
5189 ;; This function returns a list of article numbers based on the
5190 ;; difference between the ranges of read articles in this group and
5191 ;; the range of active articles.
5192 (defun gnus-list-of-unread-articles (group)
5193   (let* ((read (gnus-info-read (gnus-get-info group)))
5194          (active (or (gnus-active group) (gnus-activate-group group)))
5195          (last (cdr active))
5196          first nlast unread)
5197     ;; If none are read, then all are unread.
5198     (if (not read)
5199         (setq first (car active))
5200       ;; If the range of read articles is a single range, then the
5201       ;; first unread article is the article after the last read
5202       ;; article.  Sounds logical, doesn't it?
5203       (if (and (not (listp (cdr read)))
5204                (or (< (car read) (car active))
5205                    (progn (setq read (list read))
5206                           nil)))
5207           (setq first (max (car active) (1+ (cdr read))))
5208         ;; `read' is a list of ranges.
5209         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5210                                   (caar read)))
5211                   1)
5212           (setq first (car active)))
5213         (while read
5214           (when first
5215             (while (< first nlast)
5216               (push first unread)
5217               (setq first (1+ first))))
5218           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5219           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5220           (setq read (cdr read)))))
5221     ;; And add the last unread articles.
5222     (while (<= first last)
5223       (push first unread)
5224       (setq first (1+ first)))
5225     ;; Return the list of unread articles.
5226     (delq 0 (nreverse unread))))
5227
5228 (defun gnus-list-of-read-articles (group)
5229   "Return a list of unread, unticked and non-dormant articles."
5230   (let* ((info (gnus-get-info group))
5231          (marked (gnus-info-marks info))
5232          (active (gnus-active group)))
5233     (and info active
5234          (gnus-set-difference
5235           (gnus-sorted-complement
5236            (gnus-uncompress-range active)
5237            (gnus-list-of-unread-articles group))
5238           (append
5239            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5240            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5241
5242 ;; Various summary commands
5243
5244 (defun gnus-summary-select-article-buffer ()
5245   "Reconfigure windows to show article buffer."
5246   (interactive)
5247   (if (not (gnus-buffer-live-p gnus-article-buffer))
5248       (error "There is no article buffer for this summary buffer")
5249     (gnus-configure-windows 'article)
5250     (select-window (get-buffer-window gnus-article-buffer))))
5251
5252 (defun gnus-summary-universal-argument (arg)
5253   "Perform any operation on all articles that are process/prefixed."
5254   (interactive "P")
5255   (let ((articles (gnus-summary-work-articles arg))
5256         func article)
5257     (if (eq
5258          (setq
5259           func
5260           (key-binding
5261            (read-key-sequence
5262             (substitute-command-keys
5263              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5264          'undefined)
5265         (gnus-error 1 "Undefined key")
5266       (save-excursion
5267         (while articles
5268           (gnus-summary-goto-subject (setq article (pop articles)))
5269           (let (gnus-newsgroup-processable)
5270             (command-execute func))
5271           (gnus-summary-remove-process-mark article)))))
5272   (gnus-summary-position-point))
5273
5274 (defun gnus-summary-toggle-truncation (&optional arg)
5275   "Toggle truncation of summary lines.
5276 With arg, turn line truncation on iff arg is positive."
5277   (interactive "P")
5278   (setq truncate-lines
5279         (if (null arg) (not truncate-lines)
5280           (> (prefix-numeric-value arg) 0)))
5281   (redraw-display))
5282
5283 (defun gnus-summary-reselect-current-group (&optional all rescan)
5284   "Exit and then reselect the current newsgroup.
5285 The prefix argument ALL means to select all articles."
5286   (interactive "P")
5287   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5288     (error "Ephemeral groups can't be reselected"))
5289   (let ((current-subject (gnus-summary-article-number))
5290         (group gnus-newsgroup-name))
5291     (setq gnus-newsgroup-begin nil)
5292     (gnus-summary-exit)
5293     ;; We have to adjust the point of group mode buffer because
5294     ;; point was moved to the next unread newsgroup by exiting.
5295     (gnus-summary-jump-to-group group)
5296     (when rescan
5297       (save-excursion
5298         (gnus-group-get-new-news-this-group 1)))
5299     (gnus-group-read-group all t)
5300     (gnus-summary-goto-subject current-subject nil t)))
5301
5302 (defun gnus-summary-rescan-group (&optional all)
5303   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5304   (interactive "P")
5305   (gnus-summary-reselect-current-group all t))
5306
5307 (defun gnus-summary-update-info (&optional non-destructive)
5308   (save-excursion
5309     (let ((group gnus-newsgroup-name))
5310       (when group
5311         (when gnus-newsgroup-kill-headers
5312           (setq gnus-newsgroup-killed
5313                 (gnus-compress-sequence
5314                  (nconc
5315                   (gnus-set-sorted-intersection
5316                    (gnus-uncompress-range gnus-newsgroup-killed)
5317                    (setq gnus-newsgroup-unselected
5318                          (sort gnus-newsgroup-unselected '<)))
5319                   (setq gnus-newsgroup-unreads
5320                         (sort gnus-newsgroup-unreads '<)))
5321                  t)))
5322         (unless (listp (cdr gnus-newsgroup-killed))
5323           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5324         (let ((headers gnus-newsgroup-headers))
5325           ;; Set the new ranges of read articles.
5326           (save-excursion
5327             (set-buffer gnus-group-buffer)
5328             (gnus-undo-force-boundary))
5329           (gnus-update-read-articles
5330            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5331           ;; Set the current article marks.
5332           (let ((gnus-newsgroup-scored
5333                  (if (and (not gnus-save-score)
5334                           (not non-destructive))
5335                      nil
5336                    gnus-newsgroup-scored)))
5337             (save-excursion
5338               (gnus-update-marks)))
5339           ;; Do the cross-ref thing.
5340           (when gnus-use-cross-reference
5341             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5342           ;; Do not switch windows but change the buffer to work.
5343           (set-buffer gnus-group-buffer)
5344           (unless (gnus-ephemeral-group-p group)
5345             (gnus-group-update-group group)))))))
5346
5347 (defun gnus-summary-save-newsrc (&optional force)
5348   "Save the current number of read/marked articles in the dribble buffer.
5349 The dribble buffer will then be saved.
5350 If FORCE (the prefix), also save the .newsrc file(s)."
5351   (interactive "P")
5352   (gnus-summary-update-info t)
5353   (if force
5354       (gnus-save-newsrc-file)
5355     (gnus-dribble-save)))
5356
5357 (defun gnus-summary-exit (&optional temporary)
5358   "Exit reading current newsgroup, and then return to group selection mode.
5359 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5360   (interactive)
5361   (gnus-set-global-variables)
5362   (when (gnus-buffer-live-p gnus-article-buffer)
5363     (save-excursion
5364       (set-buffer gnus-article-buffer)
5365       (mm-destroy-parts gnus-article-mime-handles)))
5366   (gnus-kill-save-kill-buffer)
5367   (gnus-async-halt-prefetch)
5368   (let* ((group gnus-newsgroup-name)
5369          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5370          (mode major-mode)
5371          (group-point nil)
5372          (buf (current-buffer)))
5373     (unless quit-config
5374       ;; Do adaptive scoring, and possibly save score files.
5375       (when gnus-newsgroup-adaptive
5376         (gnus-score-adaptive))
5377       (when gnus-use-scoring
5378         (gnus-score-save)))
5379     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5380     ;; If we have several article buffers, we kill them at exit.
5381     (unless gnus-single-article-buffer
5382       (gnus-kill-buffer gnus-original-article-buffer)
5383       (setq gnus-article-current nil))
5384     (when gnus-use-cache
5385       (gnus-cache-possibly-remove-articles)
5386       (gnus-cache-save-buffers))
5387     (gnus-async-prefetch-remove-group group)
5388     (when gnus-suppress-duplicates
5389       (gnus-dup-enter-articles))
5390     (when gnus-use-trees
5391       (gnus-tree-close group))
5392     (when gnus-use-cache
5393       (gnus-cache-write-active))
5394     ;; Remove entries for this group.
5395     (nnmail-purge-split-history (gnus-group-real-name group))
5396     ;; Make all changes in this group permanent.
5397     (unless quit-config
5398       (gnus-run-hooks 'gnus-exit-group-hook)
5399       (gnus-summary-update-info))
5400     (gnus-close-group group)
5401     ;; Make sure where we were, and go to next newsgroup.
5402     (set-buffer gnus-group-buffer)
5403     (unless quit-config
5404       (gnus-group-jump-to-group group))
5405     (gnus-run-hooks 'gnus-summary-exit-hook)
5406     (unless (or quit-config
5407                 ;; If this group has disappeared from the summary
5408                 ;; buffer, don't skip forwards.
5409                 (not (string= group (gnus-group-group-name))))
5410       (gnus-group-next-unread-group 1))
5411     (setq group-point (point))
5412     (if temporary
5413         nil                             ;Nothing to do.
5414       ;; If we have several article buffers, we kill them at exit.
5415       (unless gnus-single-article-buffer
5416         (gnus-kill-buffer gnus-article-buffer)
5417         (gnus-kill-buffer gnus-original-article-buffer)
5418         (setq gnus-article-current nil))
5419       (set-buffer buf)
5420       (if (not gnus-kill-summary-on-exit)
5421           (gnus-deaden-summary)
5422         ;; We set all buffer-local variables to nil.  It is unclear why
5423         ;; this is needed, but if we don't, buffer-local variables are
5424         ;; not garbage-collected, it seems.  This would the lead to en
5425         ;; ever-growing Emacs.
5426         (gnus-summary-clear-local-variables)
5427         (when (get-buffer gnus-article-buffer)
5428           (bury-buffer gnus-article-buffer))
5429         ;; We clear the global counterparts of the buffer-local
5430         ;; variables as well, just to be on the safe side.
5431         (set-buffer gnus-group-buffer)
5432         (gnus-summary-clear-local-variables)
5433         ;; Return to group mode buffer.
5434         (when (eq mode 'gnus-summary-mode)
5435           (gnus-kill-buffer buf)))
5436       (setq gnus-current-select-method gnus-select-method)
5437       (pop-to-buffer gnus-group-buffer)
5438       (if (not quit-config)
5439           (progn
5440             (goto-char group-point)
5441             (gnus-configure-windows 'group 'force))
5442         (gnus-handle-ephemeral-exit quit-config))
5443       ;; Clear the current group name.
5444       (unless quit-config
5445         (setq gnus-newsgroup-name nil)))))
5446
5447 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5448 (defun gnus-summary-exit-no-update (&optional no-questions)
5449   "Quit reading current newsgroup without updating read article info."
5450   (interactive)
5451   (let* ((group gnus-newsgroup-name)
5452          (quit-config (gnus-group-quit-config group)))
5453     (when (or no-questions
5454               gnus-expert-user
5455               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5456       (gnus-async-halt-prefetch)
5457       (mapcar 'funcall
5458               (delq 'gnus-summary-expire-articles
5459                     (copy-sequence gnus-summary-prepare-exit-hook)))
5460       (when (gnus-buffer-live-p gnus-article-buffer)
5461         (save-excursion
5462           (set-buffer gnus-article-buffer)
5463           (mm-destroy-parts gnus-article-mime-handles)))
5464       ;; If we have several article buffers, we kill them at exit.
5465       (unless gnus-single-article-buffer
5466         (gnus-kill-buffer gnus-article-buffer)
5467         (gnus-kill-buffer gnus-original-article-buffer)
5468         (setq gnus-article-current nil))
5469       (if (not gnus-kill-summary-on-exit)
5470           (gnus-deaden-summary)
5471         (gnus-close-group group)
5472         (gnus-summary-clear-local-variables)
5473         (set-buffer gnus-group-buffer)
5474         (gnus-summary-clear-local-variables)
5475         (when (get-buffer gnus-summary-buffer)
5476           (kill-buffer gnus-summary-buffer)))
5477       (unless gnus-single-article-buffer
5478         (setq gnus-article-current nil))
5479       (when gnus-use-trees
5480         (gnus-tree-close group))
5481       (gnus-async-prefetch-remove-group group)
5482       (when (get-buffer gnus-article-buffer)
5483         (bury-buffer gnus-article-buffer))
5484       ;; Return to the group buffer.
5485       (gnus-configure-windows 'group 'force)
5486       ;; Clear the current group name.
5487       (setq gnus-newsgroup-name nil)
5488       (when (equal (gnus-group-group-name) group)
5489         (gnus-group-next-unread-group 1))
5490       (when quit-config
5491         (gnus-handle-ephemeral-exit quit-config)))))
5492
5493 (defun gnus-handle-ephemeral-exit (quit-config)
5494   "Handle movement when leaving an ephemeral group.
5495 The state which existed when entering the ephemeral is reset."
5496   (if (not (buffer-name (car quit-config)))
5497       (gnus-configure-windows 'group 'force)
5498     (set-buffer (car quit-config))
5499     (cond ((eq major-mode 'gnus-summary-mode)
5500            (gnus-set-global-variables))
5501           ((eq major-mode 'gnus-article-mode)
5502            (save-excursion
5503              ;; The `gnus-summary-buffer' variable may point
5504              ;; to the old summary buffer when using a single
5505              ;; article buffer.
5506              (unless (gnus-buffer-live-p gnus-summary-buffer)
5507                (set-buffer gnus-group-buffer))
5508              (set-buffer gnus-summary-buffer)
5509              (gnus-set-global-variables))))
5510     (if (or (eq (cdr quit-config) 'article)
5511             (eq (cdr quit-config) 'pick))
5512         (progn
5513           ;; The current article may be from the ephemeral group
5514           ;; thus it is best that we reload this article
5515           (gnus-summary-show-article)
5516           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5517               (gnus-configure-windows 'pick 'force)
5518             (gnus-configure-windows (cdr quit-config) 'force)))
5519       (gnus-configure-windows (cdr quit-config) 'force))
5520     (when (eq major-mode 'gnus-summary-mode)
5521       (gnus-summary-next-subject 1 nil t)
5522       (gnus-summary-recenter)
5523       (gnus-summary-position-point))))
5524
5525 ;;; Dead summaries.
5526
5527 (defvar gnus-dead-summary-mode-map nil)
5528
5529 (unless gnus-dead-summary-mode-map
5530   (setq gnus-dead-summary-mode-map (make-keymap))
5531   (suppress-keymap gnus-dead-summary-mode-map)
5532   (substitute-key-definition
5533    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5534   (let ((keys '("\C-d" "\r" "\177" [delete])))
5535     (while keys
5536       (define-key gnus-dead-summary-mode-map
5537         (pop keys) 'gnus-summary-wake-up-the-dead))))
5538
5539 (defvar gnus-dead-summary-mode nil
5540   "Minor mode for Gnus summary buffers.")
5541
5542 (defun gnus-dead-summary-mode (&optional arg)
5543   "Minor mode for Gnus summary buffers."
5544   (interactive "P")
5545   (when (eq major-mode 'gnus-summary-mode)
5546     (make-local-variable 'gnus-dead-summary-mode)
5547     (setq gnus-dead-summary-mode
5548           (if (null arg) (not gnus-dead-summary-mode)
5549             (> (prefix-numeric-value arg) 0)))
5550     (when gnus-dead-summary-mode
5551       (gnus-add-minor-mode
5552        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5553
5554 (defun gnus-deaden-summary ()
5555   "Make the current summary buffer into a dead summary buffer."
5556   ;; Kill any previous dead summary buffer.
5557   (when (and gnus-dead-summary
5558              (buffer-name gnus-dead-summary))
5559     (save-excursion
5560       (set-buffer gnus-dead-summary)
5561       (when gnus-dead-summary-mode
5562         (kill-buffer (current-buffer)))))
5563   ;; Make this the current dead summary.
5564   (setq gnus-dead-summary (current-buffer))
5565   (gnus-dead-summary-mode 1)
5566   (let ((name (buffer-name)))
5567     (when (string-match "Summary" name)
5568       (rename-buffer
5569        (concat (substring name 0 (match-beginning 0)) "Dead "
5570                (substring name (match-beginning 0)))
5571        t)
5572       (bury-buffer))))
5573
5574 (defun gnus-kill-or-deaden-summary (buffer)
5575   "Kill or deaden the summary BUFFER."
5576   (save-excursion
5577     (when (and (buffer-name buffer)
5578                (not gnus-single-article-buffer))
5579       (save-excursion
5580         (set-buffer buffer)
5581         (gnus-kill-buffer gnus-article-buffer)
5582         (gnus-kill-buffer gnus-original-article-buffer)))
5583     (cond (gnus-kill-summary-on-exit
5584            (when (and gnus-use-trees
5585                       (gnus-buffer-exists-p buffer))
5586              (save-excursion
5587                (set-buffer buffer)
5588                (gnus-tree-close gnus-newsgroup-name)))
5589            (gnus-kill-buffer buffer))
5590           ((gnus-buffer-exists-p buffer)
5591            (save-excursion
5592              (set-buffer buffer)
5593              (gnus-deaden-summary))))))
5594
5595 (defun gnus-summary-wake-up-the-dead (&rest args)
5596   "Wake up the dead summary buffer."
5597   (interactive)
5598   (gnus-dead-summary-mode -1)
5599   (let ((name (buffer-name)))
5600     (when (string-match "Dead " name)
5601       (rename-buffer
5602        (concat (substring name 0 (match-beginning 0))
5603                (substring name (match-end 0)))
5604        t)))
5605   (gnus-message 3 "This dead summary is now alive again"))
5606
5607 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5608 (defun gnus-summary-fetch-faq (&optional faq-dir)
5609   "Fetch the FAQ for the current group.
5610 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5611 in."
5612   (interactive
5613    (list
5614     (when current-prefix-arg
5615       (completing-read
5616        "Faq dir: " (and (listp gnus-group-faq-directory)
5617                         (mapcar (lambda (file) (list file))
5618                                 gnus-group-faq-directory))))))
5619   (let (gnus-faq-buffer)
5620     (when (setq gnus-faq-buffer
5621                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5622       (gnus-configure-windows 'summary-faq))))
5623
5624 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5625 (defun gnus-summary-describe-group (&optional force)
5626   "Describe the current newsgroup."
5627   (interactive "P")
5628   (gnus-group-describe-group force gnus-newsgroup-name))
5629
5630 (defun gnus-summary-describe-briefly ()
5631   "Describe summary mode commands briefly."
5632   (interactive)
5633   (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")))
5634
5635 ;; Walking around group mode buffer from summary mode.
5636
5637 (defun gnus-summary-next-group (&optional no-article target-group backward)
5638   "Exit current newsgroup and then select next unread newsgroup.
5639 If prefix argument NO-ARTICLE is non-nil, no article is selected
5640 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5641 previous group instead."
5642   (interactive "P")
5643   ;; Stop pre-fetching.
5644   (gnus-async-halt-prefetch)
5645   (let ((current-group gnus-newsgroup-name)
5646         (current-buffer (current-buffer))
5647         entered)
5648     ;; First we semi-exit this group to update Xrefs and all variables.
5649     ;; We can't do a real exit, because the window conf must remain
5650     ;; the same in case the user is prompted for info, and we don't
5651     ;; want the window conf to change before that...
5652     (gnus-summary-exit t)
5653     (while (not entered)
5654       ;; Then we find what group we are supposed to enter.
5655       (set-buffer gnus-group-buffer)
5656       (gnus-group-jump-to-group current-group)
5657       (setq target-group
5658             (or target-group
5659                 (if (eq gnus-keep-same-level 'best)
5660                     (gnus-summary-best-group gnus-newsgroup-name)
5661                   (gnus-summary-search-group backward gnus-keep-same-level))))
5662       (if (not target-group)
5663           ;; There are no further groups, so we return to the group
5664           ;; buffer.
5665           (progn
5666             (gnus-message 5 "Returning to the group buffer")
5667             (setq entered t)
5668             (when (gnus-buffer-live-p current-buffer)
5669               (set-buffer current-buffer)
5670               (gnus-summary-exit))
5671             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5672         ;; We try to enter the target group.
5673         (gnus-group-jump-to-group target-group)
5674         (let ((unreads (gnus-group-group-unread)))
5675           (if (and (or (eq t unreads)
5676                        (and unreads (not (zerop unreads))))
5677                    (gnus-summary-read-group
5678                     target-group nil no-article
5679                     (and (buffer-name current-buffer) current-buffer)
5680                     nil backward))
5681               (setq entered t)
5682             (setq current-group target-group
5683                   target-group nil)))))))
5684
5685 (defun gnus-summary-prev-group (&optional no-article)
5686   "Exit current newsgroup and then select previous unread newsgroup.
5687 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5688   (interactive "P")
5689   (gnus-summary-next-group no-article nil t))
5690
5691 ;; Walking around summary lines.
5692
5693 (defun gnus-summary-first-subject (&optional unread undownloaded)
5694   "Go to the first unread subject.
5695 If UNREAD is non-nil, go to the first unread article.
5696 Returns the article selected or nil if there are no unread articles."
5697   (interactive "P")
5698   (prog1
5699       (cond
5700        ;; Empty summary.
5701        ((null gnus-newsgroup-data)
5702         (gnus-message 3 "No articles in the group")
5703         nil)
5704        ;; Pick the first article.
5705        ((not unread)
5706         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5707         (gnus-data-number (car gnus-newsgroup-data)))
5708        ;; No unread articles.
5709        ((null gnus-newsgroup-unreads)
5710         (gnus-message 3 "No more unread articles")
5711         nil)
5712        ;; Find the first unread article.
5713        (t
5714         (let ((data gnus-newsgroup-data))
5715           (while (and data
5716                       (and (not (and undownloaded
5717                                      (eq gnus-undownloaded-mark
5718                                          (gnus-data-mark (car data)))))
5719                            (not (gnus-data-unread-p (car data)))))
5720             (setq data (cdr data)))
5721           (when data
5722             (goto-char (gnus-data-pos (car data)))
5723             (gnus-data-number (car data))))))
5724     (gnus-summary-position-point)))
5725
5726 (defun gnus-summary-next-subject (n &optional unread dont-display)
5727   "Go to next N'th summary line.
5728 If N is negative, go to the previous N'th subject line.
5729 If UNREAD is non-nil, only unread articles are selected.
5730 The difference between N and the actual number of steps taken is
5731 returned."
5732   (interactive "p")
5733   (let ((backward (< n 0))
5734         (n (abs n)))
5735     (while (and (> n 0)
5736                 (if backward
5737                     (gnus-summary-find-prev unread)
5738                   (gnus-summary-find-next unread)))
5739       (unless (zerop (setq n (1- n)))
5740         (gnus-summary-show-thread)))
5741     (when (/= 0 n)
5742       (gnus-message 7 "No more%s articles"
5743                     (if unread " unread" "")))
5744     (unless dont-display
5745       (gnus-summary-recenter)
5746       (gnus-summary-position-point))
5747     n))
5748
5749 (defun gnus-summary-next-unread-subject (n)
5750   "Go to next N'th unread summary line."
5751   (interactive "p")
5752   (gnus-summary-next-subject n t))
5753
5754 (defun gnus-summary-prev-subject (n &optional unread)
5755   "Go to previous N'th summary line.
5756 If optional argument UNREAD is non-nil, only unread article is selected."
5757   (interactive "p")
5758   (gnus-summary-next-subject (- n) unread))
5759
5760 (defun gnus-summary-prev-unread-subject (n)
5761   "Go to previous N'th unread summary line."
5762   (interactive "p")
5763   (gnus-summary-next-subject (- n) t))
5764
5765 (defun gnus-summary-goto-subject (article &optional force silent)
5766   "Go the subject line of ARTICLE.
5767 If FORCE, also allow jumping to articles not currently shown."
5768   (interactive "nArticle number: ")
5769   (let ((b (point))
5770         (data (gnus-data-find article)))
5771     ;; We read in the article if we have to.
5772     (and (not data)
5773          force
5774          (gnus-summary-insert-subject
5775           article
5776           (if (or (numberp force) (vectorp force)) force)
5777           t)
5778          (setq data (gnus-data-find article)))
5779     (goto-char b)
5780     (if (not data)
5781         (progn
5782           (unless silent
5783             (gnus-message 3 "Can't find article %d" article))
5784           nil)
5785       (goto-char (gnus-data-pos data))
5786       (gnus-summary-position-point)
5787       article)))
5788
5789 ;; Walking around summary lines with displaying articles.
5790
5791 (defun gnus-summary-expand-window (&optional arg)
5792   "Make the summary buffer take up the entire Emacs frame.
5793 Given a prefix, will force an `article' buffer configuration."
5794   (interactive "P")
5795   (if arg
5796       (gnus-configure-windows 'article 'force)
5797     (gnus-configure-windows 'summary 'force)))
5798
5799 (defun gnus-summary-display-article (article &optional all-header)
5800   "Display ARTICLE in article buffer."
5801   (gnus-set-global-variables)
5802   (if (null article)
5803       nil
5804     (prog1
5805         (if gnus-summary-display-article-function
5806             (funcall gnus-summary-display-article-function article all-header)
5807           (gnus-article-prepare article all-header))
5808       (gnus-run-hooks 'gnus-select-article-hook)
5809       (when (and gnus-current-article
5810                  (not (zerop gnus-current-article)))
5811         (gnus-summary-goto-subject gnus-current-article))
5812       (gnus-summary-recenter)
5813       (when (and gnus-use-trees gnus-show-threads)
5814         (gnus-possibly-generate-tree article)
5815         (gnus-highlight-selected-tree article))
5816       ;; Successfully display article.
5817       (gnus-article-set-window-start
5818        (cdr (assq article gnus-newsgroup-bookmarks))))))
5819
5820 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5821   "Select the current article.
5822 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5823 non-nil, the article will be re-fetched even if it already present in
5824 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5825 be displayed."
5826   ;; Make sure we are in the summary buffer to work around bbdb bug.
5827   (unless (eq major-mode 'gnus-summary-mode)
5828     (set-buffer gnus-summary-buffer))
5829   (let ((article (or article (gnus-summary-article-number)))
5830         (all-headers (not (not all-headers))) ;Must be T or NIL.
5831         gnus-summary-display-article-function)
5832     (and (not pseudo)
5833          (gnus-summary-article-pseudo-p article)
5834          (error "This is a pseudo-article"))
5835     (save-excursion
5836       (set-buffer gnus-summary-buffer)
5837       (if (or (and gnus-single-article-buffer
5838                    (or (null gnus-current-article)
5839                        (null gnus-article-current)
5840                        (null (get-buffer gnus-article-buffer))
5841                        (not (eq article (cdr gnus-article-current)))
5842                        (not (equal (car gnus-article-current)
5843                                    gnus-newsgroup-name))))
5844               (and (not gnus-single-article-buffer)
5845                    (or (null gnus-current-article)
5846                        (not (eq gnus-current-article article))))
5847               force)
5848           ;; The requested article is different from the current article.
5849           (progn
5850             (when (gnus-buffer-live-p gnus-article-buffer)
5851               (with-current-buffer gnus-article-buffer
5852                 (mm-enable-multibyte)))
5853             (gnus-summary-display-article article all-headers)
5854             (when (gnus-buffer-live-p gnus-article-buffer)
5855               (with-current-buffer gnus-article-buffer
5856                 (if (not gnus-article-decoded-p) ;; a local variable
5857                     (mm-disable-multibyte))))
5858             (when (or all-headers gnus-show-all-headers)
5859               (gnus-article-show-all-headers))
5860             (gnus-article-set-window-start
5861              (cdr (assq article gnus-newsgroup-bookmarks)))
5862             article)
5863         (when (or all-headers gnus-show-all-headers)
5864           (gnus-article-show-all-headers))
5865         'old))))
5866
5867 (defun gnus-summary-set-current-mark (&optional current-mark)
5868   "Obsolete function."
5869   nil)
5870
5871 (defun gnus-summary-next-article (&optional unread subject backward push)
5872   "Select the next article.
5873 If UNREAD, only unread articles are selected.
5874 If SUBJECT, only articles with SUBJECT are selected.
5875 If BACKWARD, the previous article is selected instead of the next."
5876   (interactive "P")
5877   (cond
5878    ;; Is there such an article?
5879    ((and (gnus-summary-search-forward unread subject backward)
5880          (or (gnus-summary-display-article (gnus-summary-article-number))
5881              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5882     (gnus-summary-position-point))
5883    ;; If not, we try the first unread, if that is wanted.
5884    ((and subject
5885          gnus-auto-select-same
5886          (gnus-summary-first-unread-article))
5887     (gnus-summary-position-point)
5888     (gnus-message 6 "Wrapped"))
5889    ;; Try to get next/previous article not displayed in this group.
5890    ((and gnus-auto-extend-newsgroup
5891          (not unread) (not subject))
5892     (gnus-summary-goto-article
5893      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5894      nil (count-lines (point-min) (point))))
5895    ;; Go to next/previous group.
5896    (t
5897     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5898       (gnus-summary-jump-to-group gnus-newsgroup-name))
5899     (let ((cmd last-command-char)
5900           (point
5901            (save-excursion
5902              (set-buffer gnus-group-buffer)
5903              (point)))
5904           (group
5905            (if (eq gnus-keep-same-level 'best)
5906                (gnus-summary-best-group gnus-newsgroup-name)
5907              (gnus-summary-search-group backward gnus-keep-same-level))))
5908       ;; For some reason, the group window gets selected.  We change
5909       ;; it back.
5910       (select-window (get-buffer-window (current-buffer)))
5911       ;; Select next unread newsgroup automagically.
5912       (cond
5913        ((or (not gnus-auto-select-next)
5914             (not cmd))
5915         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5916        ((or (eq gnus-auto-select-next 'quietly)
5917             (and (eq gnus-auto-select-next 'slightly-quietly)
5918                  push)
5919             (and (eq gnus-auto-select-next 'almost-quietly)
5920                  (gnus-summary-last-article-p)))
5921         ;; Select quietly.
5922         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5923             (gnus-summary-exit)
5924           (gnus-message 7 "No more%s articles (%s)..."
5925                         (if unread " unread" "")
5926                         (if group (concat "selecting " group)
5927                           "exiting"))
5928           (gnus-summary-next-group nil group backward)))
5929        (t
5930         (when (gnus-key-press-event-p last-input-event)
5931           (gnus-summary-walk-group-buffer
5932            gnus-newsgroup-name cmd unread backward point))))))))
5933
5934 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5935   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5936                       (?\C-p (gnus-group-prev-unread-group 1))))
5937         (cursor-in-echo-area t)
5938         keve key group ended)
5939     (save-excursion
5940       (set-buffer gnus-group-buffer)
5941       (goto-char start)
5942       (setq group
5943             (if (eq gnus-keep-same-level 'best)
5944                 (gnus-summary-best-group gnus-newsgroup-name)
5945               (gnus-summary-search-group backward gnus-keep-same-level))))
5946     (while (not ended)
5947       (gnus-message
5948        5 "No more%s articles%s" (if unread " unread" "")
5949        (if (and group
5950                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5951            (format " (Type %s for %s [%s])"
5952                    (single-key-description cmd) group
5953                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5954          (format " (Type %s to exit %s)"
5955                  (single-key-description cmd)
5956                  gnus-newsgroup-name)))
5957       ;; Confirm auto selection.
5958       (setq key (car (setq keve (gnus-read-event-char))))
5959       (setq ended t)
5960       (cond
5961        ((assq key keystrokes)
5962         (let ((obuf (current-buffer)))
5963           (switch-to-buffer gnus-group-buffer)
5964           (when group
5965             (gnus-group-jump-to-group group))
5966           (eval (cadr (assq key keystrokes)))
5967           (setq group (gnus-group-group-name))
5968           (switch-to-buffer obuf))
5969         (setq ended nil))
5970        ((equal key cmd)
5971         (if (or (not group)
5972                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5973             (gnus-summary-exit)
5974           (gnus-summary-next-group nil group backward)))
5975        (t
5976         (push (cdr keve) unread-command-events))))))
5977
5978 (defun gnus-summary-next-unread-article ()
5979   "Select unread article after current one."
5980   (interactive)
5981   (gnus-summary-next-article
5982    (or (not (eq gnus-summary-goto-unread 'never))
5983        (gnus-summary-last-article-p (gnus-summary-article-number)))
5984    (and gnus-auto-select-same
5985         (gnus-summary-article-subject))))
5986
5987 (defun gnus-summary-prev-article (&optional unread subject)
5988   "Select the article after the current one.
5989 If UNREAD is non-nil, only unread articles are selected."
5990   (interactive "P")
5991   (gnus-summary-next-article unread subject t))
5992
5993 (defun gnus-summary-prev-unread-article ()
5994   "Select unread article before current one."
5995   (interactive)
5996   (gnus-summary-prev-article
5997    (or (not (eq gnus-summary-goto-unread 'never))
5998        (gnus-summary-first-article-p (gnus-summary-article-number)))
5999    (and gnus-auto-select-same
6000         (gnus-summary-article-subject))))
6001
6002 (defun gnus-summary-next-page (&optional lines circular)
6003   "Show next page of the selected article.
6004 If at the end of the current article, select the next article.
6005 LINES says how many lines should be scrolled up.
6006
6007 If CIRCULAR is non-nil, go to the start of the article instead of
6008 selecting the next article when reaching the end of the current
6009 article."
6010   (interactive "P")
6011   (setq gnus-summary-buffer (current-buffer))
6012   (gnus-set-global-variables)
6013   (let ((article (gnus-summary-article-number))
6014         (article-window (get-buffer-window gnus-article-buffer t))
6015         endp)
6016     ;; If the buffer is empty, we have no article.
6017     (unless article
6018       (error "No article to select"))
6019     (gnus-configure-windows 'article)
6020     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6021         (if (and (eq gnus-summary-goto-unread 'never)
6022                  (not (gnus-summary-last-article-p article)))
6023             (gnus-summary-next-article)
6024           (gnus-summary-next-unread-article))
6025       (if (or (null gnus-current-article)
6026               (null gnus-article-current)
6027               (/= article (cdr gnus-article-current))
6028               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6029           ;; Selected subject is different from current article's.
6030           (gnus-summary-display-article article)
6031         (when article-window
6032           (gnus-eval-in-buffer-window gnus-article-buffer
6033             (setq endp (gnus-article-next-page lines)))
6034           (when endp
6035             (cond (circular
6036                    (gnus-summary-beginning-of-article))
6037                   (lines
6038                    (gnus-message 3 "End of message"))
6039                   ((null lines)
6040                    (if (and (eq gnus-summary-goto-unread 'never)
6041                             (not (gnus-summary-last-article-p article)))
6042                        (gnus-summary-next-article)
6043                      (gnus-summary-next-unread-article))))))))
6044     (gnus-summary-recenter)
6045     (gnus-summary-position-point)))
6046
6047 (defun gnus-summary-prev-page (&optional lines move)
6048   "Show previous page of selected article.
6049 Argument LINES specifies lines to be scrolled down.
6050 If MOVE, move to the previous unread article if point is at
6051 the beginning of the buffer."
6052   (interactive "P")
6053   (let ((article (gnus-summary-article-number))
6054         (article-window (get-buffer-window gnus-article-buffer t))
6055         endp)
6056     (gnus-configure-windows 'article)
6057     (if (or (null gnus-current-article)
6058             (null gnus-article-current)
6059             (/= article (cdr gnus-article-current))
6060             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6061         ;; Selected subject is different from current article's.
6062         (gnus-summary-display-article article)
6063       (gnus-summary-recenter)
6064       (when article-window
6065         (gnus-eval-in-buffer-window gnus-article-buffer
6066           (setq endp (gnus-article-prev-page lines)))
6067         (when (and move endp)
6068           (cond (lines
6069                  (gnus-message 3 "Beginning of message"))
6070                 ((null lines)
6071                  (if (and (eq gnus-summary-goto-unread 'never)
6072                           (not (gnus-summary-first-article-p article)))
6073                      (gnus-summary-prev-article)
6074                    (gnus-summary-prev-unread-article))))))))
6075   (gnus-summary-position-point))
6076
6077 (defun gnus-summary-prev-page-or-article (&optional lines)
6078   "Show previous page of selected article.
6079 Argument LINES specifies lines to be scrolled down.
6080 If at the beginning of the article, go to the next article."
6081   (interactive "P")
6082   (gnus-summary-prev-page lines t))
6083
6084 (defun gnus-summary-scroll-up (lines)
6085   "Scroll up (or down) one line current article.
6086 Argument LINES specifies lines to be scrolled up (or down if negative)."
6087   (interactive "p")
6088   (gnus-configure-windows 'article)
6089   (gnus-summary-show-thread)
6090   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6091     (gnus-eval-in-buffer-window gnus-article-buffer
6092       (cond ((> lines 0)
6093              (when (gnus-article-next-page lines)
6094                (gnus-message 3 "End of message")))
6095             ((< lines 0)
6096              (gnus-article-prev-page (- lines))))))
6097   (gnus-summary-recenter)
6098   (gnus-summary-position-point))
6099
6100 (defun gnus-summary-scroll-down (lines)
6101   "Scroll down (or up) one line current article.
6102 Argument LINES specifies lines to be scrolled down (or up if negative)."
6103   (interactive "p")
6104   (gnus-summary-scroll-up (- lines)))
6105
6106 (defun gnus-summary-next-same-subject ()
6107   "Select next article which has the same subject as current one."
6108   (interactive)
6109   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6110
6111 (defun gnus-summary-prev-same-subject ()
6112   "Select previous article which has the same subject as current one."
6113   (interactive)
6114   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6115
6116 (defun gnus-summary-next-unread-same-subject ()
6117   "Select next unread article which has the same subject as current one."
6118   (interactive)
6119   (gnus-summary-next-article t (gnus-summary-article-subject)))
6120
6121 (defun gnus-summary-prev-unread-same-subject ()
6122   "Select previous unread article which has the same subject as current one."
6123   (interactive)
6124   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6125
6126 (defun gnus-summary-first-unread-article ()
6127   "Select the first unread article.
6128 Return nil if there are no unread articles."
6129   (interactive)
6130   (prog1
6131       (when (gnus-summary-first-subject t)
6132         (gnus-summary-show-thread)
6133         (gnus-summary-first-subject t)
6134         (gnus-summary-display-article (gnus-summary-article-number)))
6135     (gnus-summary-position-point)))
6136
6137 (defun gnus-summary-first-unread-subject ()
6138   "Place the point on the subject line of the first unread article.
6139 Return nil if there are no unread articles."
6140   (interactive)
6141   (prog1
6142       (when (gnus-summary-first-subject t)
6143         (gnus-summary-show-thread)
6144         (gnus-summary-first-subject t))
6145     (gnus-summary-position-point)))
6146
6147 (defun gnus-summary-first-article ()
6148   "Select the first article.
6149 Return nil if there are no articles."
6150   (interactive)
6151   (prog1
6152       (when (gnus-summary-first-subject)
6153         (gnus-summary-show-thread)
6154         (gnus-summary-first-subject)
6155         (gnus-summary-display-article (gnus-summary-article-number)))
6156     (gnus-summary-position-point)))
6157
6158 (defun gnus-summary-best-unread-article ()
6159   "Select the unread article with the highest score."
6160   (interactive)
6161   (let ((best -1000000)
6162         (data gnus-newsgroup-data)
6163         article score)
6164     (while data
6165       (and (gnus-data-unread-p (car data))
6166            (> (setq score
6167                     (gnus-summary-article-score (gnus-data-number (car data))))
6168               best)
6169            (setq best score
6170                  article (gnus-data-number (car data))))
6171       (setq data (cdr data)))
6172     (prog1
6173         (if article
6174             (gnus-summary-goto-article article)
6175           (error "No unread articles"))
6176       (gnus-summary-position-point))))
6177
6178 (defun gnus-summary-last-subject ()
6179   "Go to the last displayed subject line in the group."
6180   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6181     (when article
6182       (gnus-summary-goto-subject article))))
6183
6184 (defun gnus-summary-goto-article (article &optional all-headers force)
6185   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6186 If ALL-HEADERS is non-nil, no header lines are hidden.
6187 If FORCE, go to the article even if it isn't displayed.  If FORCE
6188 is a number, it is the line the article is to be displayed on."
6189   (interactive
6190    (list
6191     (completing-read
6192      "Article number or Message-ID: "
6193      (mapcar (lambda (number) (list (int-to-string number)))
6194              gnus-newsgroup-limit))
6195     current-prefix-arg
6196     t))
6197   (prog1
6198       (if (and (stringp article)
6199                (string-match "@" article))
6200           (gnus-summary-refer-article article)
6201         (when (stringp article)
6202           (setq article (string-to-number article)))
6203         (if (gnus-summary-goto-subject article force)
6204             (gnus-summary-display-article article all-headers)
6205           (gnus-message 4 "Couldn't go to article %s" article) nil))
6206     (gnus-summary-position-point)))
6207
6208 (defun gnus-summary-goto-last-article ()
6209   "Go to the previously read article."
6210   (interactive)
6211   (prog1
6212       (when gnus-last-article
6213         (gnus-summary-goto-article gnus-last-article nil t))
6214     (gnus-summary-position-point)))
6215
6216 (defun gnus-summary-pop-article (number)
6217   "Pop one article off the history and go to the previous.
6218 NUMBER articles will be popped off."
6219   (interactive "p")
6220   (let (to)
6221     (setq gnus-newsgroup-history
6222           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6223     (if to
6224         (gnus-summary-goto-article (car to) nil t)
6225       (error "Article history empty")))
6226   (gnus-summary-position-point))
6227
6228 ;; Summary commands and functions for limiting the summary buffer.
6229
6230 (defun gnus-summary-limit-to-articles (n)
6231   "Limit the summary buffer to the next N articles.
6232 If not given a prefix, use the process marked articles instead."
6233   (interactive "P")
6234   (prog1
6235       (let ((articles (gnus-summary-work-articles n)))
6236         (setq gnus-newsgroup-processable nil)
6237         (gnus-summary-limit articles))
6238     (gnus-summary-position-point)))
6239
6240 (defun gnus-summary-pop-limit (&optional total)
6241   "Restore the previous limit.
6242 If given a prefix, remove all limits."
6243   (interactive "P")
6244   (when total
6245     (setq gnus-newsgroup-limits
6246           (list (mapcar (lambda (h) (mail-header-number h))
6247                         gnus-newsgroup-headers))))
6248   (unless gnus-newsgroup-limits
6249     (error "No limit to pop"))
6250   (prog1
6251       (gnus-summary-limit nil 'pop)
6252     (gnus-summary-position-point)))
6253
6254 (defun gnus-summary-limit-to-subject (subject &optional header)
6255   "Limit the summary buffer to articles that have subjects that match a regexp."
6256   (interactive "sLimit to subject (regexp): ")
6257   (unless header
6258     (setq header "subject"))
6259   (when (not (equal "" subject))
6260     (prog1
6261         (let ((articles (gnus-summary-find-matching
6262                          (or header "subject") subject 'all)))
6263           (unless articles
6264             (error "Found no matches for \"%s\"" subject))
6265           (gnus-summary-limit articles))
6266       (gnus-summary-position-point))))
6267
6268 (defun gnus-summary-limit-to-author (from)
6269   "Limit the summary buffer to articles that have authors that match a regexp."
6270   (interactive "sLimit to author (regexp): ")
6271   (gnus-summary-limit-to-subject from "from"))
6272
6273 (defun gnus-summary-limit-to-age (age &optional younger-p)
6274   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6275 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6276 articles that are younger than AGE days."
6277   (interactive
6278    (let ((younger current-prefix-arg)
6279          (days-got nil)
6280          days)
6281      (while (not days-got)
6282        (setq days (if younger
6283                       (read-string "Limit to articles within (in days): ")
6284                     (read-string "Limit to articles old than (in days): ")))
6285        (when (> (length days) 0)
6286          (setq days (read days)))
6287        (if (numberp days)
6288            (setq days-got t)
6289          (message "Please enter a number.")
6290          (sleep-for 1)))
6291      (list days younger)))
6292   (prog1
6293       (let ((data gnus-newsgroup-data)
6294             (cutoff (days-to-time age))
6295             articles d date is-younger)
6296         (while (setq d (pop data))
6297           (when (and (vectorp (gnus-data-header d))
6298                      (setq date (mail-header-date (gnus-data-header d))))
6299             (setq is-younger (time-less-p
6300                               (time-since (condition-case ()
6301                                               (date-to-time date)
6302                                             (error '(0 0))))
6303                               cutoff))
6304             (when (if younger-p
6305                       is-younger
6306                     (not is-younger))
6307               (push (gnus-data-number d) articles))))
6308         (gnus-summary-limit (nreverse articles)))
6309     (gnus-summary-position-point)))
6310
6311 (defun gnus-summary-limit-to-extra (header regexp)
6312   "Limit the summary buffer to articles that match an 'extra' header."
6313   (interactive
6314    (let ((header
6315           (intern
6316            (gnus-completing-read
6317             (symbol-name (car gnus-extra-headers))      
6318             "Limit extra header:"       
6319             (mapcar (lambda (x) 
6320                       (cons (symbol-name x) x))
6321                     gnus-extra-headers)
6322             nil                 
6323             t))))
6324      (list header
6325            (read-string (format "Limit to header %s (regexp): " header)))))
6326   (when (not (equal "" regexp))
6327     (prog1
6328         (let ((articles (gnus-summary-find-matching
6329                          (cons 'extra header) regexp 'all)))
6330           (unless articles
6331             (error "Found no matches for \"%s\"" regexp))
6332           (gnus-summary-limit articles))
6333       (gnus-summary-position-point))))
6334
6335 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6336 (make-obsolete
6337  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6338
6339 (defun gnus-summary-limit-to-unread (&optional all)
6340   "Limit the summary buffer to articles that are not marked as read.
6341 If ALL is non-nil, limit strictly to unread articles."
6342   (interactive "P")
6343   (if all
6344       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6345     (gnus-summary-limit-to-marks
6346      ;; Concat all the marks that say that an article is read and have
6347      ;; those removed.
6348      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6349            gnus-killed-mark gnus-kill-file-mark
6350            gnus-low-score-mark gnus-expirable-mark
6351            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6352            gnus-duplicate-mark gnus-souped-mark)
6353      'reverse)))
6354
6355 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6356 (make-obsolete 'gnus-summary-delete-marked-with
6357                'gnus-summary-limit-exlude-marks)
6358
6359 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6360   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6361 If REVERSE, limit the summary buffer to articles that are marked
6362 with MARKS.  MARKS can either be a string of marks or a list of marks.
6363 Returns how many articles were removed."
6364   (interactive "sMarks: ")
6365   (gnus-summary-limit-to-marks marks t))
6366
6367 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6368   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6369 If REVERSE (the prefix), limit the summary buffer to articles that are
6370 not marked with MARKS.  MARKS can either be a string of marks or a
6371 list of marks.
6372 Returns how many articles were removed."
6373   (interactive "sMarks: \nP")
6374   (prog1
6375       (let ((data gnus-newsgroup-data)
6376             (marks (if (listp marks) marks
6377                      (append marks nil))) ; Transform to list.
6378             articles)
6379         (while data
6380           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6381                   (memq (gnus-data-mark (car data)) marks))
6382             (push (gnus-data-number (car data)) articles))
6383           (setq data (cdr data)))
6384         (gnus-summary-limit articles))
6385     (gnus-summary-position-point)))
6386
6387 (defun gnus-summary-limit-to-score (&optional score)
6388   "Limit to articles with score at or above SCORE."
6389   (interactive "P")
6390   (setq score (if score
6391                   (prefix-numeric-value score)
6392                 (or gnus-summary-default-score 0)))
6393   (let ((data gnus-newsgroup-data)
6394         articles)
6395     (while data
6396       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6397                 score)
6398         (push (gnus-data-number (car data)) articles))
6399       (setq data (cdr data)))
6400     (prog1
6401         (gnus-summary-limit articles)
6402       (gnus-summary-position-point))))
6403
6404 (defun gnus-summary-limit-include-thread (id)
6405   "Display all the hidden articles that in the current thread."
6406   (interactive (list (mail-header-id (gnus-summary-article-header))))
6407   (let ((articles (gnus-articles-in-thread
6408                    (gnus-id-to-thread (gnus-root-id id)))))
6409     (prog1
6410         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6411       (gnus-summary-position-point))))
6412
6413 (defun gnus-summary-limit-include-dormant ()
6414   "Display all the hidden articles that are marked as dormant.
6415 Note that this command only works on a subset of the articles currently
6416 fetched for this group."
6417   (interactive)
6418   (unless gnus-newsgroup-dormant
6419     (error "There are no dormant articles in this group"))
6420   (prog1
6421       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6422     (gnus-summary-position-point)))
6423
6424 (defun gnus-summary-limit-exclude-dormant ()
6425   "Hide all dormant articles."
6426   (interactive)
6427   (prog1
6428       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6429     (gnus-summary-position-point)))
6430
6431 (defun gnus-summary-limit-exclude-childless-dormant ()
6432   "Hide all dormant articles that have no children."
6433   (interactive)
6434   (let ((data (gnus-data-list t))
6435         articles d children)
6436     ;; Find all articles that are either not dormant or have
6437     ;; children.
6438     (while (setq d (pop data))
6439       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6440                 (and (setq children
6441                            (gnus-article-children (gnus-data-number d)))
6442                      (let (found)
6443                        (while children
6444                          (when (memq (car children) articles)
6445                            (setq children nil
6446                                  found t))
6447                          (pop children))
6448                        found)))
6449         (push (gnus-data-number d) articles)))
6450     ;; Do the limiting.
6451     (prog1
6452         (gnus-summary-limit articles)
6453       (gnus-summary-position-point))))
6454
6455 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6456   "Mark all unread excluded articles as read.
6457 If ALL, mark even excluded ticked and dormants as read."
6458   (interactive "P")
6459   (let ((articles (gnus-sorted-complement
6460                    (sort
6461                     (mapcar (lambda (h) (mail-header-number h))
6462                             gnus-newsgroup-headers)
6463                     '<)
6464                    (sort gnus-newsgroup-limit '<)))
6465         article)
6466     (setq gnus-newsgroup-unreads
6467           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6468     (if all
6469         (setq gnus-newsgroup-dormant nil
6470               gnus-newsgroup-marked nil
6471               gnus-newsgroup-reads
6472               (nconc
6473                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6474                gnus-newsgroup-reads))
6475       (while (setq article (pop articles))
6476         (unless (or (memq article gnus-newsgroup-dormant)
6477                     (memq article gnus-newsgroup-marked))
6478           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6479
6480 (defun gnus-summary-limit (articles &optional pop)
6481   (if pop
6482       ;; We pop the previous limit off the stack and use that.
6483       (setq articles (car gnus-newsgroup-limits)
6484             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6485     ;; We use the new limit, so we push the old limit on the stack.
6486     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6487   ;; Set the limit.
6488   (setq gnus-newsgroup-limit articles)
6489   (let ((total (length gnus-newsgroup-data))
6490         (data (gnus-data-find-list (gnus-summary-article-number)))
6491         (gnus-summary-mark-below nil)   ; Inhibit this.
6492         found)
6493     ;; This will do all the work of generating the new summary buffer
6494     ;; according to the new limit.
6495     (gnus-summary-prepare)
6496     ;; Hide any threads, possibly.
6497     (and gnus-show-threads
6498          gnus-thread-hide-subtree
6499          (gnus-summary-hide-all-threads))
6500     ;; Try to return to the article you were at, or one in the
6501     ;; neighborhood.
6502     (when data
6503       ;; We try to find some article after the current one.
6504       (while data
6505         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6506           (setq data nil
6507                 found t))
6508         (setq data (cdr data))))
6509     (unless found
6510       ;; If there is no data, that means that we were after the last
6511       ;; article.  The same goes when we can't find any articles
6512       ;; after the current one.
6513       (goto-char (point-max))
6514       (gnus-summary-find-prev))
6515     (gnus-set-mode-line 'summary)
6516     ;; We return how many articles were removed from the summary
6517     ;; buffer as a result of the new limit.
6518     (- total (length gnus-newsgroup-data))))
6519
6520 (defsubst gnus-invisible-cut-children (threads)
6521   (let ((num 0))
6522     (while threads
6523       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6524         (incf num))
6525       (pop threads))
6526     (< num 2)))
6527
6528 (defsubst gnus-cut-thread (thread)
6529   "Go forwards in the thread until we find an article that we want to display."
6530   (when (or (eq gnus-fetch-old-headers 'some)
6531             (eq gnus-fetch-old-headers 'invisible)
6532             (numberp gnus-fetch-old-headers)
6533             (eq gnus-build-sparse-threads 'some)
6534             (eq gnus-build-sparse-threads 'more))
6535     ;; Deal with old-fetched headers and sparse threads.
6536     (while (and
6537             thread
6538             (or
6539              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6540              (gnus-summary-article-ancient-p
6541               (mail-header-number (car thread))))
6542             (if (or (<= (length (cdr thread)) 1)
6543                     (eq gnus-fetch-old-headers 'invisible))
6544                 (setq gnus-newsgroup-limit
6545                       (delq (mail-header-number (car thread))
6546                             gnus-newsgroup-limit)
6547                       thread (cadr thread))
6548               (when (gnus-invisible-cut-children (cdr thread))
6549                 (let ((th (cdr thread)))
6550                   (while th
6551                     (if (memq (mail-header-number (caar th))
6552                               gnus-newsgroup-limit)
6553                         (setq thread (car th)
6554                               th nil)
6555                       (setq th (cdr th))))))))))
6556   thread)
6557
6558 (defun gnus-cut-threads (threads)
6559   "Cut off all uninteresting articles from the beginning of threads."
6560   (when (or (eq gnus-fetch-old-headers 'some)
6561             (eq gnus-fetch-old-headers 'invisible)
6562             (numberp gnus-fetch-old-headers)
6563             (eq gnus-build-sparse-threads 'some)
6564             (eq gnus-build-sparse-threads 'more))
6565     (let ((th threads))
6566       (while th
6567         (setcar th (gnus-cut-thread (car th)))
6568         (setq th (cdr th)))))
6569   ;; Remove nixed out threads.
6570   (delq nil threads))
6571
6572 (defun gnus-summary-initial-limit (&optional show-if-empty)
6573   "Figure out what the initial limit is supposed to be on group entry.
6574 This entails weeding out unwanted dormants, low-scored articles,
6575 fetch-old-headers verbiage, and so on."
6576   ;; Most groups have nothing to remove.
6577   (if (or gnus-inhibit-limiting
6578           (and (null gnus-newsgroup-dormant)
6579                (not (eq gnus-fetch-old-headers 'some))
6580                (not (numberp gnus-fetch-old-headers))
6581                (not (eq gnus-fetch-old-headers 'invisible))
6582                (null gnus-summary-expunge-below)
6583                (not (eq gnus-build-sparse-threads 'some))
6584                (not (eq gnus-build-sparse-threads 'more))
6585                (null gnus-thread-expunge-below)
6586                (not gnus-use-nocem)))
6587       ()                                ; Do nothing.
6588     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6589     (setq gnus-newsgroup-limit nil)
6590     (mapatoms
6591      (lambda (node)
6592        (unless (car (symbol-value node))
6593          ;; These threads have no parents -- they are roots.
6594          (let ((nodes (cdr (symbol-value node)))
6595                thread)
6596            (while nodes
6597              (if (and gnus-thread-expunge-below
6598                       (< (gnus-thread-total-score (car nodes))
6599                          gnus-thread-expunge-below))
6600                  (gnus-expunge-thread (pop nodes))
6601                (setq thread (pop nodes))
6602                (gnus-summary-limit-children thread))))))
6603      gnus-newsgroup-dependencies)
6604     ;; If this limitation resulted in an empty group, we might
6605     ;; pop the previous limit and use it instead.
6606     (when (and (not gnus-newsgroup-limit)
6607                show-if-empty)
6608       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6609     gnus-newsgroup-limit))
6610
6611 (defun gnus-summary-limit-children (thread)
6612   "Return 1 if this subthread is visible and 0 if it is not."
6613   ;; First we get the number of visible children to this thread.  This
6614   ;; is done by recursing down the thread using this function, so this
6615   ;; will really go down to a leaf article first, before slowly
6616   ;; working its way up towards the root.
6617   (when thread
6618     (let ((children
6619            (if (cdr thread)
6620                (apply '+ (mapcar 'gnus-summary-limit-children
6621                                  (cdr thread)))
6622              0))
6623           (number (mail-header-number (car thread)))
6624           score)
6625       (if (and
6626            (not (memq number gnus-newsgroup-marked))
6627            (or
6628             ;; If this article is dormant and has absolutely no visible
6629             ;; children, then this article isn't visible.
6630             (and (memq number gnus-newsgroup-dormant)
6631                  (zerop children))
6632             ;; If this is "fetch-old-headered" and there is no
6633             ;; visible children, then we don't want this article.
6634             (and (or (eq gnus-fetch-old-headers 'some)
6635                      (numberp gnus-fetch-old-headers))
6636                  (gnus-summary-article-ancient-p number)
6637                  (zerop children))
6638             ;; If this is "fetch-old-headered" and `invisible', then
6639             ;; we don't want this article.
6640             (and (eq gnus-fetch-old-headers 'invisible)
6641                  (gnus-summary-article-ancient-p number))
6642             ;; If this is a sparsely inserted article with no children,
6643             ;; we don't want it.
6644             (and (eq gnus-build-sparse-threads 'some)
6645                  (gnus-summary-article-sparse-p number)
6646                  (zerop children))
6647             ;; If we use expunging, and this article is really
6648             ;; low-scored, then we don't want this article.
6649             (when (and gnus-summary-expunge-below
6650                        (< (setq score
6651                                 (or (cdr (assq number gnus-newsgroup-scored))
6652                                     gnus-summary-default-score))
6653                           gnus-summary-expunge-below))
6654               ;; We increase the expunge-tally here, but that has
6655               ;; nothing to do with the limits, really.
6656               (incf gnus-newsgroup-expunged-tally)
6657               ;; We also mark as read here, if that's wanted.
6658               (when (and gnus-summary-mark-below
6659                          (< score gnus-summary-mark-below))
6660                 (setq gnus-newsgroup-unreads
6661                       (delq number gnus-newsgroup-unreads))
6662                 (if gnus-newsgroup-auto-expire
6663                     (push number gnus-newsgroup-expirable)
6664                   (push (cons number gnus-low-score-mark)
6665                         gnus-newsgroup-reads)))
6666               t)
6667             ;; Check NoCeM things.
6668             (if (and gnus-use-nocem
6669                      (gnus-nocem-unwanted-article-p
6670                       (mail-header-id (car thread))))
6671                 (progn
6672                   (setq gnus-newsgroup-unreads
6673                         (delq number gnus-newsgroup-unreads))
6674                   t))))
6675           ;; Nope, invisible article.
6676           0
6677         ;; Ok, this article is to be visible, so we add it to the limit
6678         ;; and return 1.
6679         (push number gnus-newsgroup-limit)
6680         1))))
6681
6682 (defun gnus-expunge-thread (thread)
6683   "Mark all articles in THREAD as read."
6684   (let* ((number (mail-header-number (car thread))))
6685     (incf gnus-newsgroup-expunged-tally)
6686     ;; We also mark as read here, if that's wanted.
6687     (setq gnus-newsgroup-unreads
6688           (delq number gnus-newsgroup-unreads))
6689     (if gnus-newsgroup-auto-expire
6690         (push number gnus-newsgroup-expirable)
6691       (push (cons number gnus-low-score-mark)
6692             gnus-newsgroup-reads)))
6693   ;; Go recursively through all subthreads.
6694   (mapcar 'gnus-expunge-thread (cdr thread)))
6695
6696 ;; Summary article oriented commands
6697
6698 (defun gnus-summary-refer-parent-article (n)
6699   "Refer parent article N times.
6700 If N is negative, go to ancestor -N instead.
6701 The difference between N and the number of articles fetched is returned."
6702   (interactive "p")
6703   (let ((skip 1)
6704         error header ref)
6705     (when (not (natnump n))
6706       (setq skip (abs n)
6707             n 1))
6708     (while (and (> n 0)
6709                 (not error))
6710       (setq header (gnus-summary-article-header))
6711       (if (and (eq (mail-header-number header)
6712                    (cdr gnus-article-current))
6713                (equal gnus-newsgroup-name
6714                       (car gnus-article-current)))
6715           ;; If we try to find the parent of the currently
6716           ;; displayed article, then we take a look at the actual
6717           ;; References header, since this is slightly more
6718           ;; reliable than the References field we got from the
6719           ;; server.
6720           (save-excursion
6721             (set-buffer gnus-original-article-buffer)
6722             (nnheader-narrow-to-headers)
6723             (unless (setq ref (message-fetch-field "references"))
6724               (setq ref (message-fetch-field "in-reply-to")))
6725             (widen))
6726         (setq ref
6727               ;; It's not the current article, so we take a bet on
6728               ;; the value we got from the server.
6729               (mail-header-references header)))
6730       (if (and ref
6731                (not (equal ref "")))
6732           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6733             (gnus-message 1 "Couldn't find parent"))
6734         (gnus-message 1 "No references in article %d"
6735                       (gnus-summary-article-number))
6736         (setq error t))
6737       (decf n))
6738     (gnus-summary-position-point)
6739     n))
6740
6741 (defun gnus-summary-refer-references ()
6742   "Fetch all articles mentioned in the References header.
6743 Return the number of articles fetched."
6744   (interactive)
6745   (let ((ref (mail-header-references (gnus-summary-article-header)))
6746         (current (gnus-summary-article-number))
6747         (n 0))
6748     (if (or (not ref)
6749             (equal ref ""))
6750         (error "No References in the current article")
6751       ;; For each Message-ID in the References header...
6752       (while (string-match "<[^>]*>" ref)
6753         (incf n)
6754         ;; ... fetch that article.
6755         (gnus-summary-refer-article
6756          (prog1 (match-string 0 ref)
6757            (setq ref (substring ref (match-end 0))))))
6758       (gnus-summary-goto-subject current)
6759       (gnus-summary-position-point)
6760       n)))
6761
6762 (defun gnus-summary-refer-thread (&optional limit)
6763   "Fetch all articles in the current thread.
6764 If LIMIT (the numerical prefix), fetch that many old headers instead
6765 of what's specified by the `gnus-refer-thread-limit' variable."
6766   (interactive "P")
6767   (let ((id (mail-header-id (gnus-summary-article-header)))
6768         (limit (if limit (prefix-numeric-value limit)
6769                  gnus-refer-thread-limit)))
6770     ;; We want to fetch LIMIT *old* headers, but we also have to
6771     ;; re-fetch all the headers in the current buffer, because many of
6772     ;; them may be undisplayed.  So we adjust LIMIT.
6773     (when (numberp limit)
6774       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6775     (unless (eq gnus-fetch-old-headers 'invisible)
6776       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6777       ;; Retrieve the headers and read them in.
6778       (if (eq (gnus-retrieve-headers
6779                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6780               'nov)
6781           (gnus-build-all-threads)
6782         (error "Can't fetch thread from backends that don't support NOV"))
6783       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6784     (gnus-summary-limit-include-thread id)))
6785
6786 (defun gnus-summary-refer-article (message-id)
6787   "Fetch an article specified by MESSAGE-ID."
6788   (interactive "sMessage-ID: ")
6789   (when (and (stringp message-id)
6790              (not (zerop (length message-id))))
6791     ;; Construct the correct Message-ID if necessary.
6792     ;; Suggested by tale@pawl.rpi.edu.
6793     (unless (string-match "^<" message-id)
6794       (setq message-id (concat "<" message-id)))
6795     (unless (string-match ">$" message-id)
6796       (setq message-id (concat message-id ">")))
6797     (let* ((header (gnus-id-to-header message-id))
6798            (sparse (and header
6799                         (gnus-summary-article-sparse-p
6800                          (mail-header-number header))
6801                         (memq (mail-header-number header)
6802                               gnus-newsgroup-limit)))
6803            number)
6804       (cond
6805        ;; If the article is present in the buffer we just go to it.
6806        ((and header
6807              (or (not (gnus-summary-article-sparse-p
6808                        (mail-header-number header)))
6809                  sparse))
6810         (prog1
6811             (gnus-summary-goto-article
6812              (mail-header-number header) nil t)
6813           (when sparse
6814             (gnus-summary-update-article (mail-header-number header)))))
6815        (t
6816         ;; We fetch the article.
6817         (catch 'found
6818           (dolist (gnus-override-method (gnus-refer-article-methods))
6819             (gnus-check-server gnus-override-method)
6820             ;; Fetch the header, and display the article.
6821             (when (setq number (gnus-summary-insert-subject message-id))
6822               (gnus-summary-select-article nil nil nil number)
6823               (throw 'found t)))
6824           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6825
6826 (defun gnus-refer-article-methods ()
6827   "Return a list of referrable methods."
6828   (cond
6829    ;; No method, so we default to current and native.
6830    ((null gnus-refer-article-method)
6831     (list gnus-current-select-method gnus-select-method))
6832    ;; Current.
6833    ((eq 'current gnus-refer-article-method)
6834     (list gnus-current-select-method))
6835    ;; List of select methods.
6836    ((not (stringp (cadr gnus-refer-article-method)))
6837     (let (out)
6838       (dolist (method gnus-refer-article-method)
6839         (push (if (eq 'current method)
6840                   gnus-current-select-method
6841                 method)
6842               out))
6843       (nreverse out)))
6844    ;; One single select method.
6845    (t
6846     (list gnus-refer-article-method))))
6847
6848 (defun gnus-summary-edit-parameters ()
6849   "Edit the group parameters of the current group."
6850   (interactive)
6851   (gnus-group-edit-group gnus-newsgroup-name 'params))
6852
6853 (defun gnus-summary-customize-parameters ()
6854   "Customize the group parameters of the current group."
6855   (interactive)
6856   (gnus-group-customize gnus-newsgroup-name))
6857
6858 (defun gnus-summary-enter-digest-group (&optional force)
6859   "Enter an nndoc group based on the current article.
6860 If FORCE, force a digest interpretation.  If not, try
6861 to guess what the document format is."
6862   (interactive "P")
6863   (let ((conf gnus-current-window-configuration))
6864     (save-excursion
6865       (gnus-summary-select-article))
6866     (setq gnus-current-window-configuration conf)
6867     (let* ((name (format "%s-%d"
6868                          (gnus-group-prefixed-name
6869                           gnus-newsgroup-name (list 'nndoc ""))
6870                          (save-excursion
6871                            (set-buffer gnus-summary-buffer)
6872                            gnus-current-article)))
6873            (ogroup gnus-newsgroup-name)
6874            (params (append (gnus-info-params (gnus-get-info ogroup))
6875                            (list (cons 'to-group ogroup))
6876                            (list (cons 'save-article-group ogroup))))
6877            (case-fold-search t)
6878            (buf (current-buffer))
6879            dig to-address)
6880       (save-excursion
6881         (set-buffer gnus-original-article-buffer)
6882         ;; Have the digest group inherit the main mail address of
6883         ;; the parent article.
6884         (when (setq to-address (or (message-fetch-field "reply-to")
6885                                    (message-fetch-field "from")))
6886           (setq params (append (list (cons 'to-address to-address)))))
6887         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6888         (insert-buffer-substring gnus-original-article-buffer)
6889         ;; Remove lines that may lead nndoc to misinterpret the
6890         ;; document type.
6891         (narrow-to-region
6892          (goto-char (point-min))
6893          (or (search-forward "\n\n" nil t) (point)))
6894         (goto-char (point-min))
6895         (delete-matching-lines "^Path:\\|^From ")
6896         (widen))
6897       (unwind-protect
6898           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6899                     (gnus-newsgroup-ephemeral-ignored-charsets
6900                      gnus-newsgroup-ignored-charsets))
6901                 (gnus-group-read-ephemeral-group
6902                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6903                               (nndoc-article-type
6904                                ,(if force 'mbox 'guess))) t))
6905               ;; Make all postings to this group go to the parent group.
6906               (nconc (gnus-info-params (gnus-get-info name))
6907                      params)
6908             ;; Couldn't select this doc group.
6909             (switch-to-buffer buf)
6910             (gnus-set-global-variables)
6911             (gnus-configure-windows 'summary)
6912             (gnus-message 3 "Article couldn't be entered?"))
6913         (kill-buffer dig)))))
6914
6915 (defun gnus-summary-read-document (n)
6916   "Open a new group based on the current article(s).
6917 This will allow you to read digests and other similar
6918 documents as newsgroups.
6919 Obeys the standard process/prefix convention."
6920   (interactive "P")
6921   (let* ((articles (gnus-summary-work-articles n))
6922          (ogroup gnus-newsgroup-name)
6923          (params (append (gnus-info-params (gnus-get-info ogroup))
6924                          (list (cons 'to-group ogroup))))
6925          article group egroup groups vgroup)
6926     (while (setq article (pop articles))
6927       (setq group (format "%s-%d" gnus-newsgroup-name article))
6928       (gnus-summary-remove-process-mark article)
6929       (when (gnus-summary-display-article article)
6930         (save-excursion
6931           (with-temp-buffer
6932             (insert-buffer-substring gnus-original-article-buffer)
6933             ;; Remove some headers that may lead nndoc to make
6934             ;; the wrong guess.
6935             (message-narrow-to-head)
6936             (goto-char (point-min))
6937             (delete-matching-lines "^\\(Path\\):\\|^From ")
6938             (widen)
6939             (if (setq egroup
6940                       (gnus-group-read-ephemeral-group
6941                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6942                                      (nndoc-article-type guess))
6943                        t nil t))
6944                 (progn
6945                   ;; Make all postings to this group go to the parent group.
6946                   (nconc (gnus-info-params (gnus-get-info egroup))
6947                          params)
6948                   (push egroup groups))
6949               ;; Couldn't select this doc group.
6950               (gnus-error 3 "Article couldn't be entered"))))))
6951     ;; Now we have selected all the documents.
6952     (cond
6953      ((not groups)
6954       (error "None of the articles could be interpreted as documents"))
6955      ((gnus-group-read-ephemeral-group
6956        (setq vgroup (format
6957                      "nnvirtual:%s-%s" gnus-newsgroup-name
6958                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6959        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6960        t
6961        (cons (current-buffer) 'summary)))
6962      (t
6963       (error "Couldn't select virtual nndoc group")))))
6964
6965 (defun gnus-summary-isearch-article (&optional regexp-p)
6966   "Do incremental search forward on the current article.
6967 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6968   (interactive "P")
6969   (gnus-summary-select-article)
6970   (gnus-configure-windows 'article)
6971   (gnus-eval-in-buffer-window gnus-article-buffer
6972     (save-restriction
6973       (widen)
6974       (isearch-forward regexp-p))))
6975
6976 (defun gnus-summary-search-article-forward (regexp &optional backward)
6977   "Search for an article containing REGEXP forward.
6978 If BACKWARD, search backward instead."
6979   (interactive
6980    (list (read-string
6981           (format "Search article %s (regexp%s): "
6982                   (if current-prefix-arg "backward" "forward")
6983                   (if gnus-last-search-regexp
6984                       (concat ", default " gnus-last-search-regexp)
6985                     "")))
6986          current-prefix-arg))
6987   (if (string-equal regexp "")
6988       (setq regexp (or gnus-last-search-regexp ""))
6989     (setq gnus-last-search-regexp regexp))
6990   (if (gnus-summary-search-article regexp backward)
6991       (gnus-summary-show-thread)
6992     (error "Search failed: \"%s\"" regexp)))
6993
6994 (defun gnus-summary-search-article-backward (regexp)
6995   "Search for an article containing REGEXP backward."
6996   (interactive
6997    (list (read-string
6998           (format "Search article backward (regexp%s): "
6999                   (if gnus-last-search-regexp
7000                       (concat ", default " gnus-last-search-regexp)
7001                     "")))))
7002   (gnus-summary-search-article-forward regexp 'backward))
7003
7004 (defun gnus-summary-search-article (regexp &optional backward)
7005   "Search for an article containing REGEXP.
7006 Optional argument BACKWARD means do search for backward.
7007 `gnus-select-article-hook' is not called during the search."
7008   ;; We have to require this here to make sure that the following
7009   ;; dynamic binding isn't shadowed by autoloading.
7010   (require 'gnus-async)
7011   (require 'gnus-art)
7012   (let ((gnus-select-article-hook nil)  ;Disable hook.
7013         (gnus-article-prepare-hook nil)
7014         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7015         (gnus-use-article-prefetch nil)
7016         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7017         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7018         (sum (current-buffer))
7019         (gnus-display-mime-function nil)
7020         (found nil)
7021         point)
7022     (gnus-save-hidden-threads
7023       (gnus-summary-select-article)
7024       (set-buffer gnus-article-buffer)
7025       (goto-char (window-point (get-buffer-window (current-buffer))))
7026       (when backward
7027         (forward-line -1))
7028       (while (not found)
7029         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7030         (if (if backward
7031                 (re-search-backward regexp nil t)
7032               (re-search-forward regexp nil t))
7033             ;; We found the regexp.
7034             (progn
7035               (setq found 'found)
7036               (beginning-of-line)
7037               (set-window-start
7038                (get-buffer-window (current-buffer))
7039                (point))
7040               (forward-line 1)
7041               (set-window-point
7042                (get-buffer-window (current-buffer))
7043                (point))
7044               (set-buffer sum)
7045               (setq point (point)))
7046           ;; We didn't find it, so we go to the next article.
7047           (set-buffer sum)
7048           (setq found 'not)
7049           (while (eq found 'not)
7050             (if (not (if backward (gnus-summary-find-prev)
7051                        (gnus-summary-find-next)))
7052                 ;; No more articles.
7053                 (setq found t)
7054               ;; Select the next article and adjust point.
7055               (unless (gnus-summary-article-sparse-p
7056                        (gnus-summary-article-number))
7057                 (setq found nil)
7058                 (gnus-summary-select-article)
7059                 (set-buffer gnus-article-buffer)
7060                 (widen)
7061                 (goto-char (if backward (point-max) (point-min))))))))
7062       (gnus-message 7 ""))
7063     ;; Return whether we found the regexp.
7064     (when (eq found 'found)
7065       (goto-char point)
7066       (gnus-summary-show-thread)
7067       (gnus-summary-goto-subject gnus-current-article)
7068       (gnus-summary-position-point)
7069       t)))
7070
7071 (defun gnus-summary-find-matching (header regexp &optional backward unread
7072                                           not-case-fold)
7073   "Return a list of all articles that match REGEXP on HEADER.
7074 The search stars on the current article and goes forwards unless
7075 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7076 If UNREAD is non-nil, only unread articles will
7077 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7078 in the comparisons."
7079   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7080                 (gnus-data-find-list
7081                  (gnus-summary-article-number) (gnus-data-list backward))))
7082         (case-fold-search (not not-case-fold))
7083         articles d func)
7084     (if (consp header)
7085         (if (eq (car header) 'extra)
7086             (setq func
7087                   `(lambda (h)
7088                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7089                          "")))
7090           (error "%s is an invalid header" header))
7091       (unless (fboundp (intern (concat "mail-header-" header)))
7092         (error "%s is not a valid header" header))
7093       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7094     (while data
7095       (setq d (car data))
7096       (and (or (not unread)             ; We want all articles...
7097                (gnus-data-unread-p d))  ; Or just unreads.
7098            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7099            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7100            (push (gnus-data-number d) articles)) ; Success!
7101       (setq data (cdr data)))
7102     (nreverse articles)))
7103
7104 (defun gnus-summary-execute-command (header regexp command &optional backward)
7105   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7106 If HEADER is an empty string (or nil), the match is done on the entire
7107 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7108   (interactive
7109    (list (let ((completion-ignore-case t))
7110            (completing-read
7111             "Header name: "
7112             (mapcar (lambda (string) (list string))
7113                     '("Number" "Subject" "From" "Lines" "Date"
7114                       "Message-ID" "Xref" "References" "Body"))
7115             nil 'require-match))
7116          (read-string "Regexp: ")
7117          (read-key-sequence "Command: ")
7118          current-prefix-arg))
7119   (when (equal header "Body")
7120     (setq header ""))
7121   ;; Hidden thread subtrees must be searched as well.
7122   (gnus-summary-show-all-threads)
7123   ;; We don't want to change current point nor window configuration.
7124   (save-excursion
7125     (save-window-excursion
7126       (gnus-message 6 "Executing %s..." (key-description command))
7127       ;; We'd like to execute COMMAND interactively so as to give arguments.
7128       (gnus-execute header regexp
7129                     `(call-interactively ',(key-binding command))
7130                     backward)
7131       (gnus-message 6 "Executing %s...done" (key-description command)))))
7132
7133 (defun gnus-summary-beginning-of-article ()
7134   "Scroll the article back to the beginning."
7135   (interactive)
7136   (gnus-summary-select-article)
7137   (gnus-configure-windows 'article)
7138   (gnus-eval-in-buffer-window gnus-article-buffer
7139     (widen)
7140     (goto-char (point-min))
7141     (when gnus-page-broken
7142       (gnus-narrow-to-page))))
7143
7144 (defun gnus-summary-end-of-article ()
7145   "Scroll to the end of the article."
7146   (interactive)
7147   (gnus-summary-select-article)
7148   (gnus-configure-windows 'article)
7149   (gnus-eval-in-buffer-window gnus-article-buffer
7150     (widen)
7151     (goto-char (point-max))
7152     (recenter -3)
7153     (when gnus-page-broken
7154       (gnus-narrow-to-page))))
7155
7156 (defun gnus-summary-print-article (&optional filename n)
7157   "Generate and print a PostScript image of the N next (mail) articles.
7158
7159 If N is negative, print the N previous articles.  If N is nil and articles
7160 have been marked with the process mark, print these instead.
7161
7162 If the optional first argument FILENAME is nil, send the image to the
7163 printer.  If FILENAME is a string, save the PostScript image in a file with
7164 that name.  If FILENAME is a number, prompt the user for the name of the file
7165 to save in."
7166   (interactive (list (ps-print-preprint current-prefix-arg)
7167                      current-prefix-arg))
7168   (dolist (article (gnus-summary-work-articles n))
7169     (gnus-summary-select-article nil nil 'pseudo article)
7170     (gnus-eval-in-buffer-window gnus-article-buffer
7171       (let ((buffer (generate-new-buffer " *print*")))
7172         (unwind-protect
7173             (progn
7174               (copy-to-buffer buffer (point-min) (point-max))
7175               (set-buffer buffer)
7176               (gnus-article-delete-invisible-text)
7177               (let ((ps-left-header
7178                      (list
7179                       (concat "("
7180                               (mail-header-subject gnus-current-headers) ")")
7181                       (concat "("
7182                               (mail-header-from gnus-current-headers) ")")))
7183                     (ps-right-header
7184                      (list
7185                       "/pagenumberstring load"
7186                       (concat "("
7187                               (mail-header-date gnus-current-headers) ")"))))
7188                 (gnus-run-hooks 'gnus-ps-print-hook)
7189                 (save-excursion
7190                   (ps-print-buffer-with-faces filename))))
7191           (kill-buffer buffer))))))
7192
7193 (defun gnus-summary-show-article (&optional arg)
7194   "Force re-fetching of the current article.
7195 If ARG (the prefix) is a number, show the article with the charset 
7196 defined in `gnus-summary-show-article-charset-alist', or the charset
7197 inputed.
7198 If ARG (the prefix) is non-nil and not a number, show the raw article 
7199 without any article massaging functions being run."
7200   (interactive "P")
7201   (cond 
7202    ((numberp arg)
7203     (let ((gnus-newsgroup-charset 
7204            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7205                (read-coding-system "Charset: ")))
7206           (gnus-newsgroup-ignored-charsets 'gnus-all))
7207       (gnus-summary-select-article nil 'force)))
7208    ((not arg)
7209     ;; Select the article the normal way.
7210     (gnus-summary-select-article nil 'force))
7211    (t
7212     ;; We have to require this here to make sure that the following
7213     ;; dynamic binding isn't shadowed by autoloading.
7214     (require 'gnus-async)
7215     (require 'gnus-art)
7216     ;; Bind the article treatment functions to nil.
7217     (let ((gnus-have-all-headers t)
7218           gnus-article-prepare-hook
7219           gnus-article-decode-hook
7220           gnus-display-mime-function
7221           gnus-break-pages)
7222       ;; Destroy any MIME parts.
7223       (when (gnus-buffer-live-p gnus-article-buffer)
7224         (save-excursion
7225           (set-buffer gnus-article-buffer)
7226           (mm-destroy-parts gnus-article-mime-handles)))
7227       (gnus-summary-select-article nil 'force))))
7228   (gnus-summary-goto-subject gnus-current-article)
7229   (gnus-summary-position-point))
7230
7231 (defun gnus-summary-verbose-headers (&optional arg)
7232   "Toggle permanent full header display.
7233 If ARG is a positive number, turn header display on.
7234 If ARG is a negative number, turn header display off."
7235   (interactive "P")
7236   (setq gnus-show-all-headers
7237         (cond ((or (not (numberp arg))
7238                    (zerop arg))
7239                (not gnus-show-all-headers))
7240               ((natnump arg)
7241                t)))
7242   (gnus-summary-show-article))
7243
7244 (defun gnus-summary-toggle-header (&optional arg)
7245   "Show the headers if they are hidden, or hide them if they are shown.
7246 If ARG is a positive number, show the entire header.
7247 If ARG is a negative number, hide the unwanted header lines."
7248   (interactive "P")
7249   (save-excursion
7250     (set-buffer gnus-article-buffer)
7251     (save-restriction
7252       (let* ((buffer-read-only nil)
7253              (inhibit-point-motion-hooks t)
7254              hidden e)
7255         (setq hidden
7256               (if (numberp arg)
7257                   (>= arg 0)
7258                 (save-restriction 
7259                   (article-narrow-to-head)
7260                   (gnus-article-hidden-text-p 'headers))))
7261         (goto-char (point-min))
7262         (when (search-forward "\n\n" nil t)
7263           (delete-region (point-min) (1- (point))))
7264         (goto-char (point-min))
7265         (save-excursion
7266           (set-buffer gnus-original-article-buffer)
7267           (goto-char (point-min))
7268           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7269         (insert-buffer-substring gnus-original-article-buffer 1 e)
7270         (save-restriction
7271           (narrow-to-region (point-min) (point))
7272           (article-decode-encoded-words)
7273           (if  hidden
7274               (let ((gnus-treat-hide-headers nil)
7275                     (gnus-treat-hide-boring-headers nil))
7276                 (setq gnus-article-wash-types
7277                       (delq 'headers gnus-article-wash-types))
7278                 (gnus-treat-article 'head))
7279             (gnus-treat-article 'head)))
7280         (gnus-set-mode-line 'article)))))
7281
7282 (defun gnus-summary-show-all-headers ()
7283   "Make all header lines visible."
7284   (interactive)
7285   (gnus-article-show-all-headers))
7286
7287 (defun gnus-summary-caesar-message (&optional arg)
7288   "Caesar rotate the current article by 13.
7289 The numerical prefix specifies how many places to rotate each letter
7290 forward."
7291   (interactive "P")
7292   (gnus-summary-select-article)
7293   (let ((mail-header-separator ""))
7294     (gnus-eval-in-buffer-window gnus-article-buffer
7295       (save-restriction
7296         (widen)
7297         (let ((start (window-start))
7298               buffer-read-only)
7299           (message-caesar-buffer-body arg)
7300           (set-window-start (get-buffer-window (current-buffer)) start))))))
7301
7302 (defun gnus-summary-stop-page-breaking ()
7303   "Stop page breaking in the current article."
7304   (interactive)
7305   (gnus-summary-select-article)
7306   (gnus-eval-in-buffer-window gnus-article-buffer
7307     (widen)
7308     (when (gnus-visual-p 'page-marker)
7309       (let ((buffer-read-only nil))
7310         (gnus-remove-text-with-property 'gnus-prev)
7311         (gnus-remove-text-with-property 'gnus-next))
7312       (setq gnus-page-broken nil))))
7313
7314 (defun gnus-summary-move-article (&optional n to-newsgroup
7315                                             select-method action)
7316   "Move the current article to a different newsgroup.
7317 If N is a positive number, move the N next articles.
7318 If N is a negative number, move the N previous articles.
7319 If N is nil and any articles have been marked with the process mark,
7320 move those articles instead.
7321 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7322 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7323 re-spool using this method.
7324
7325 For this function to work, both the current newsgroup and the
7326 newsgroup that you want to move to have to support the `request-move'
7327 and `request-accept' functions.
7328
7329 ACTION can be either `move' (the default), `crosspost' or `copy'."
7330   (interactive "P")
7331   (unless action
7332     (setq action 'move))
7333   ;; Disable marking as read.
7334   (let (gnus-mark-article-hook)
7335     (save-window-excursion
7336       (gnus-summary-select-article)))
7337   ;; Check whether the source group supports the required functions.
7338   (cond ((and (eq action 'move)
7339               (not (gnus-check-backend-function
7340                     'request-move-article gnus-newsgroup-name)))
7341          (error "The current group does not support article moving"))
7342         ((and (eq action 'crosspost)
7343               (not (gnus-check-backend-function
7344                     'request-replace-article gnus-newsgroup-name)))
7345          (error "The current group does not support article editing")))
7346   (let ((articles (gnus-summary-work-articles n))
7347         (prefix (if (gnus-check-backend-function
7348                     'request-move-article gnus-newsgroup-name)
7349                     (gnus-group-real-prefix gnus-newsgroup-name)
7350                   ""))
7351         (names '((move "Move" "Moving")
7352                  (copy "Copy" "Copying")
7353                  (crosspost "Crosspost" "Crossposting")))
7354         (copy-buf (save-excursion
7355                     (nnheader-set-temp-buffer " *copy article*")))
7356         art-group to-method new-xref article to-groups)
7357     (unless (assq action names)
7358       (error "Unknown action %s" action))
7359     ;; Read the newsgroup name.
7360     (when (and (not to-newsgroup)
7361                (not select-method))
7362       (setq to-newsgroup
7363             (gnus-read-move-group-name
7364              (cadr (assq action names))
7365              (symbol-value (intern (format "gnus-current-%s-group" action)))
7366              articles prefix))
7367       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7368     (setq to-method (or select-method
7369                         (gnus-server-to-method
7370                          (gnus-group-method to-newsgroup))))
7371     ;; Check the method we are to move this article to...
7372     (unless (gnus-check-backend-function
7373              'request-accept-article (car to-method))
7374       (error "%s does not support article copying" (car to-method)))
7375     (unless (gnus-check-server to-method)
7376       (error "Can't open server %s" (car to-method)))
7377     (gnus-message 6 "%s to %s: %s..."
7378                   (caddr (assq action names))
7379                   (or (car select-method) to-newsgroup) articles)
7380     (while articles
7381       (setq article (pop articles))
7382       (setq
7383        art-group
7384        (cond
7385         ;; Move the article.
7386         ((eq action 'move)
7387          ;; Remove this article from future suppression.
7388          (gnus-dup-unsuppress-article article)
7389          (gnus-request-move-article
7390           article                       ; Article to move
7391           gnus-newsgroup-name           ; From newsgroup
7392           (nth 1 (gnus-find-method-for-group
7393                   gnus-newsgroup-name)) ; Server
7394           (list 'gnus-request-accept-article
7395                 to-newsgroup (list 'quote select-method)
7396                 (not articles) t)       ; Accept form
7397           (not articles)))              ; Only save nov last time
7398         ;; Copy the article.
7399         ((eq action 'copy)
7400          (save-excursion
7401            (set-buffer copy-buf)
7402            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7403              (gnus-request-accept-article
7404               to-newsgroup select-method (not articles) t))))
7405         ;; Crosspost the article.
7406         ((eq action 'crosspost)
7407          (let ((xref (message-tokenize-header
7408                       (mail-header-xref (gnus-summary-article-header article))
7409                       " ")))
7410            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7411                                   ":" article))
7412            (unless xref
7413              (setq xref (list (system-name))))
7414            (setq new-xref
7415                  (concat
7416                   (mapconcat 'identity
7417                              (delete "Xref:" (delete new-xref xref))
7418                              " ")
7419                   " " new-xref))
7420            (save-excursion
7421              (set-buffer copy-buf)
7422              ;; First put the article in the destination group.
7423              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7424              (when (consp (setq art-group
7425                                 (gnus-request-accept-article
7426                                  to-newsgroup select-method (not articles))))
7427                (setq new-xref (concat new-xref " " (car art-group)
7428                                       ":" (cdr art-group)))
7429                ;; Now we have the new Xrefs header, so we insert
7430                ;; it and replace the new article.
7431                (nnheader-replace-header "Xref" new-xref)
7432                (gnus-request-replace-article
7433                 (cdr art-group) to-newsgroup (current-buffer))
7434                art-group))))))
7435       (cond
7436        ((not art-group)
7437         (gnus-message 1 "Couldn't %s article %s: %s"
7438                       (cadr (assq action names)) article
7439                       (nnheader-get-report (car to-method))))
7440        ((eq art-group 'junk)
7441         (when (eq action 'move)
7442           (gnus-summary-mark-article article gnus-canceled-mark)
7443           (gnus-message 4 "Deleted article %s" article)))
7444        (t
7445         (let* ((pto-group (gnus-group-prefixed-name
7446                            (car art-group) to-method))
7447                (entry
7448                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7449                (info (nth 2 entry))
7450                (to-group (gnus-info-group info))
7451                to-marks)
7452           ;; Update the group that has been moved to.
7453           (when (and info
7454                      (memq action '(move copy)))
7455             (unless (member to-group to-groups)
7456               (push to-group to-groups))
7457
7458             (unless (memq article gnus-newsgroup-unreads)
7459               (push 'read to-marks)
7460               (gnus-info-set-read
7461                info (gnus-add-to-range (gnus-info-read info)
7462                                        (list (cdr art-group)))))
7463
7464             ;; See whether the article is to be put in the cache.
7465             (let ((marks gnus-article-mark-lists)
7466                   (to-article (cdr art-group)))
7467
7468               ;; Enter the article into the cache in the new group,
7469               ;; if that is required.
7470               (when gnus-use-cache
7471                 (gnus-cache-possibly-enter-article
7472                  to-group to-article
7473                  (memq article gnus-newsgroup-marked)
7474                  (memq article gnus-newsgroup-dormant)
7475                  (memq article gnus-newsgroup-unreads)))
7476
7477               (when gnus-preserve-marks 
7478                 ;; Copy any marks over to the new group.
7479                 (when (and (equal to-group gnus-newsgroup-name)
7480                            (not (memq article gnus-newsgroup-unreads)))
7481                   ;; Mark this article as read in this group.
7482                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7483                   (setcdr (gnus-active to-group) to-article)
7484                   (setcdr gnus-newsgroup-active to-article))
7485
7486                 (while marks
7487                   (when (memq article (symbol-value
7488                                        (intern (format "gnus-newsgroup-%s"
7489                                                        (caar marks)))))
7490                     (push (cdar marks) to-marks)
7491                     ;; If the other group is the same as this group,
7492                     ;; then we have to add the mark to the list.
7493                     (when (equal to-group gnus-newsgroup-name)
7494                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7495                            (cons to-article
7496                                  (symbol-value
7497                                   (intern (format "gnus-newsgroup-%s"
7498                                                   (caar marks)))))))
7499                     ;; Copy the marks to other group.
7500                     (gnus-add-marked-articles
7501                      to-group (cdar marks) (list to-article) info))
7502                   (setq marks (cdr marks)))
7503
7504                 (gnus-request-set-mark to-group (list (list (list to-article)
7505                                                             'set
7506                                                             to-marks))))
7507
7508               (gnus-dribble-enter
7509                (concat "(gnus-group-set-info '"
7510                        (gnus-prin1-to-string (gnus-get-info to-group))
7511                        ")"))))
7512
7513           ;; Update the Xref header in this article to point to
7514           ;; the new crossposted article we have just created.
7515           (when (eq action 'crosspost)
7516             (save-excursion
7517               (set-buffer copy-buf)
7518               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7519               (nnheader-replace-header "Xref" new-xref)
7520               (gnus-request-replace-article
7521                article gnus-newsgroup-name (current-buffer)))))
7522
7523         ;;;!!!Why is this necessary?
7524         (set-buffer gnus-summary-buffer)
7525
7526         (gnus-summary-goto-subject article)
7527         (when (eq action 'move)
7528           (gnus-summary-mark-article article gnus-canceled-mark))))
7529       (gnus-summary-remove-process-mark article))
7530     ;; Re-activate all groups that have been moved to.
7531     (while to-groups
7532       (save-excursion
7533         (set-buffer gnus-group-buffer)
7534         (when (gnus-group-goto-group (car to-groups) t)
7535           (gnus-group-get-new-news-this-group 1 t))
7536         (pop to-groups)))
7537
7538     (gnus-kill-buffer copy-buf)
7539     (gnus-summary-position-point)
7540     (gnus-set-mode-line 'summary)))
7541
7542 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7543   "Move the current article to a different newsgroup.
7544 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7545 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7546 re-spool using this method."
7547   (interactive "P")
7548   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7549
7550 (defun gnus-summary-crosspost-article (&optional n)
7551   "Crosspost the current article to some other group."
7552   (interactive "P")
7553   (gnus-summary-move-article n nil nil 'crosspost))
7554
7555 (defcustom gnus-summary-respool-default-method nil
7556   "Default method for respooling an article.
7557 If nil, use to the current newsgroup method."
7558   :type '(choice (gnus-select-method :value (nnml ""))
7559                  (const nil))
7560   :group 'gnus-summary-mail)
7561
7562 (defun gnus-summary-respool-article (&optional n method)
7563   "Respool the current article.
7564 The article will be squeezed through the mail spooling process again,
7565 which means that it will be put in some mail newsgroup or other
7566 depending on `nnmail-split-methods'.
7567 If N is a positive number, respool the N next articles.
7568 If N is a negative number, respool the N previous articles.
7569 If N is nil and any articles have been marked with the process mark,
7570 respool those articles instead.
7571
7572 Respooling can be done both from mail groups and \"real\" newsgroups.
7573 In the former case, the articles in question will be moved from the
7574 current group into whatever groups they are destined to.  In the
7575 latter case, they will be copied into the relevant groups."
7576   (interactive
7577    (list current-prefix-arg
7578          (let* ((methods (gnus-methods-using 'respool))
7579                 (methname
7580                  (symbol-name (or gnus-summary-respool-default-method
7581                                   (car (gnus-find-method-for-group
7582                                         gnus-newsgroup-name)))))
7583                 (method
7584                  (gnus-completing-read
7585                   methname "What backend do you want to use when respooling?"
7586                   methods nil t nil 'gnus-mail-method-history))
7587                 ms)
7588            (cond
7589             ((zerop (length (setq ms (gnus-servers-using-backend
7590                                       (intern method)))))
7591              (list (intern method) ""))
7592             ((= 1 (length ms))
7593              (car ms))
7594             (t
7595              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7596                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7597                            ms-alist))))))))
7598   (unless method
7599     (error "No method given for respooling"))
7600   (if (assoc (symbol-name
7601               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7602              (gnus-methods-using 'respool))
7603       (gnus-summary-move-article n nil method)
7604     (gnus-summary-copy-article n nil method)))
7605
7606 (defun gnus-summary-import-article (file)
7607   "Import an arbitrary file into a mail newsgroup."
7608   (interactive "fImport file: ")
7609   (let ((group gnus-newsgroup-name)
7610         (now (current-time))
7611         atts lines)
7612     (unless (gnus-check-backend-function 'request-accept-article group)
7613       (error "%s does not support article importing" group))
7614     (or (file-readable-p file)
7615         (not (file-regular-p file))
7616         (error "Can't read %s" file))
7617     (save-excursion
7618       (set-buffer (gnus-get-buffer-create " *import file*"))
7619       (erase-buffer)
7620       (insert-file-contents file)
7621       (goto-char (point-min))
7622       (unless (nnheader-article-p)
7623         ;; This doesn't look like an article, so we fudge some headers.
7624         (setq atts (file-attributes file)
7625               lines (count-lines (point-min) (point-max)))
7626         (insert "From: " (read-string "From: ") "\n"
7627                 "Subject: " (read-string "Subject: ") "\n"
7628                 "Date: " (message-make-date (nth 5 atts))
7629                 "\n"
7630                 "Message-ID: " (message-make-message-id) "\n"
7631                 "Lines: " (int-to-string lines) "\n"
7632                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7633       (gnus-request-accept-article group nil t)
7634       (kill-buffer (current-buffer)))))
7635
7636 (defun gnus-summary-article-posted-p ()
7637   "Say whether the current (mail) article is available from news as well.
7638 This will be the case if the article has both been mailed and posted."
7639   (interactive)
7640   (let ((id (mail-header-references (gnus-summary-article-header)))
7641         (gnus-override-method (car (gnus-refer-article-methods))))
7642     (if (gnus-request-head id "")
7643         (gnus-message 2 "The current message was found on %s"
7644                       gnus-override-method)
7645       (gnus-message 2 "The current message couldn't be found on %s"
7646                     gnus-override-method)
7647       nil)))
7648
7649 (defun gnus-summary-expire-articles (&optional now)
7650   "Expire all articles that are marked as expirable in the current group."
7651   (interactive)
7652   (when (gnus-check-backend-function
7653          'request-expire-articles gnus-newsgroup-name)
7654     ;; This backend supports expiry.
7655     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7656            (expirable (if total
7657                           (progn
7658                             ;; We need to update the info for
7659                             ;; this group for `gnus-list-of-read-articles'
7660                             ;; to give us the right answer.
7661                             (gnus-run-hooks 'gnus-exit-group-hook)
7662                             (gnus-summary-update-info)
7663                             (gnus-list-of-read-articles gnus-newsgroup-name))
7664                         (setq gnus-newsgroup-expirable
7665                               (sort gnus-newsgroup-expirable '<))))
7666            (expiry-wait (if now 'immediate
7667                           (gnus-group-find-parameter
7668                            gnus-newsgroup-name 'expiry-wait)))
7669            es)
7670       (when expirable
7671         ;; There are expirable articles in this group, so we run them
7672         ;; through the expiry process.
7673         (gnus-message 6 "Expiring articles...")
7674         (unless (gnus-check-group gnus-newsgroup-name)
7675           (error "Can't open server for %s" gnus-newsgroup-name))
7676         ;; The list of articles that weren't expired is returned.
7677         (save-excursion
7678           (if expiry-wait
7679               (let ((nnmail-expiry-wait-function nil)
7680                     (nnmail-expiry-wait expiry-wait))
7681                 (setq es (gnus-request-expire-articles
7682                           expirable gnus-newsgroup-name)))
7683             (setq es (gnus-request-expire-articles
7684                       expirable gnus-newsgroup-name))))
7685         (unless total
7686           (setq gnus-newsgroup-expirable es))
7687         ;; We go through the old list of expirable, and mark all
7688         ;; really expired articles as nonexistent.
7689         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7690           (let ((gnus-use-cache nil))
7691             (while expirable
7692               (unless (memq (car expirable) es)
7693                 (when (gnus-data-find (car expirable))
7694                   (gnus-summary-mark-article
7695                    (car expirable) gnus-canceled-mark)))
7696               (setq expirable (cdr expirable)))))
7697         (gnus-message 6 "Expiring articles...done")))))
7698
7699 (defun gnus-summary-expire-articles-now ()
7700   "Expunge all expirable articles in the current group.
7701 This means that *all* articles that are marked as expirable will be
7702 deleted forever, right now."
7703   (interactive)
7704   (or gnus-expert-user
7705       (gnus-yes-or-no-p
7706        "Are you really, really, really sure you want to delete all these messages? ")
7707       (error "Phew!"))
7708   (gnus-summary-expire-articles t))
7709
7710 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7711 (defun gnus-summary-delete-article (&optional n)
7712   "Delete the N next (mail) articles.
7713 This command actually deletes articles.  This is not a marking
7714 command.  The article will disappear forever from your life, never to
7715 return.
7716 If N is negative, delete backwards.
7717 If N is nil and articles have been marked with the process mark,
7718 delete these instead."
7719   (interactive "P")
7720   (unless (gnus-check-backend-function 'request-expire-articles
7721                                        gnus-newsgroup-name)
7722     (error "The current newsgroup does not support article deletion"))
7723   ;; Compute the list of articles to delete.
7724   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7725         not-deleted)
7726     (if (and gnus-novice-user
7727              (not (gnus-yes-or-no-p
7728                    (format "Do you really want to delete %s forever? "
7729                            (if (> (length articles) 1)
7730                                (format "these %s articles" (length articles))
7731                              "this article")))))
7732         ()
7733       ;; Delete the articles.
7734       (setq not-deleted (gnus-request-expire-articles
7735                          articles gnus-newsgroup-name 'force))
7736       (while articles
7737         (gnus-summary-remove-process-mark (car articles))
7738         ;; The backend might not have been able to delete the article
7739         ;; after all.
7740         (unless (memq (car articles) not-deleted)
7741           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7742         (setq articles (cdr articles)))
7743       (when not-deleted
7744         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7745     (gnus-summary-position-point)
7746     (gnus-set-mode-line 'summary)
7747     not-deleted))
7748
7749 (defun gnus-summary-edit-article (&optional arg)
7750   "Edit the current article.
7751 This will have permanent effect only in mail groups.
7752 If ARG is nil, edit the decoded articles.
7753 If ARG is 1, edit the raw articles. 
7754 If ARG is 2, edit the raw articles even in read-only groups.
7755 Otherwise, allow editing of articles even in read-only
7756 groups."
7757   (interactive "P")
7758   (let (force raw)
7759     (cond 
7760      ((null arg))
7761      ((eq arg 1) (setq raw t))
7762      ((eq arg 2) (setq raw t
7763                        force t))
7764      (t (setq force t)))
7765     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
7766         (error "Can't edit the raw article in group nndraft:drafts."))
7767     (save-excursion
7768       (set-buffer gnus-summary-buffer)
7769       (let ((mail-parse-charset gnus-newsgroup-charset)
7770             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7771         (gnus-set-global-variables)
7772         (when (and (not force)
7773                    (gnus-group-read-only-p))
7774           (error "The current newsgroup does not support article editing"))
7775         (gnus-summary-show-article t)
7776         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
7777           (with-current-buffer gnus-article-buffer
7778             (mm-enable-multibyte)))
7779         (if (equal gnus-newsgroup-name "nndraft:drafts")
7780             (setq raw t))
7781         (gnus-article-edit-article
7782          (if raw 'ignore 
7783            #'(lambda () 
7784                (let ((mbl mml-buffer-list))
7785                  (setq mml-buffer-list nil)
7786                  (mime-to-mml)
7787                  (make-local-hook 'kill-buffer-hook)
7788                  (let ((mml-buffer-list mml-buffer-list))
7789                    (setq mml-buffer-list mbl)
7790                    (make-local-variable 'mml-buffer-list))
7791                  (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
7792          `(lambda (no-highlight)
7793             (let ((mail-parse-charset ',gnus-newsgroup-charset)
7794                   (mail-parse-ignored-charsets 
7795                    ',gnus-newsgroup-ignored-charsets))
7796               ,(if (not raw) '(progn 
7797                                 (mml-to-mime)
7798                                 (mml-destroy-buffers)
7799                                 (remove-hook 'kill-buffer-hook 
7800                                              'mml-destroy-buffers t)
7801                                 (kill-local-variable 'mml-buffer-list)))
7802               (gnus-summary-edit-article-done
7803                ,(or (mail-header-references gnus-current-headers) "")
7804                ,(gnus-group-read-only-p) 
7805                ,gnus-summary-buffer no-highlight))))))))
7806
7807 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7808
7809 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7810                                                  no-highlight)
7811   "Make edits to the current article permanent."
7812   (interactive)
7813   ;; Replace the article.
7814   (let ((buf (current-buffer)))
7815     (with-temp-buffer
7816       (insert-buffer-substring buf)
7817       (if (and (not read-only)
7818                (not (gnus-request-replace-article
7819                      (cdr gnus-article-current) (car gnus-article-current)
7820                      (current-buffer) t)))
7821           (error "Couldn't replace article")
7822         ;; Update the summary buffer.
7823         (if (and references
7824                  (equal (message-tokenize-header references " ")
7825                         (message-tokenize-header
7826                          (or (message-fetch-field "references") "") " ")))
7827             ;; We only have to update this line.
7828             (save-excursion
7829               (save-restriction
7830                 (message-narrow-to-head)
7831                 (let ((head (buffer-string))
7832                       header)
7833                   (with-temp-buffer
7834                     (insert (format "211 %d Article retrieved.\n"
7835                                     (cdr gnus-article-current)))
7836                     (insert head)
7837                     (insert ".\n")
7838                     (let ((nntp-server-buffer (current-buffer)))
7839                       (setq header (car (gnus-get-newsgroup-headers
7840                                          (save-excursion
7841                                            (set-buffer gnus-summary-buffer)
7842                                            gnus-newsgroup-dependencies)
7843                                          t))))
7844                     (save-excursion
7845                       (set-buffer gnus-summary-buffer)
7846                       (gnus-data-set-header
7847                        (gnus-data-find (cdr gnus-article-current))
7848                        header)
7849                       (gnus-summary-update-article-line
7850                        (cdr gnus-article-current) header))))))
7851           ;; Update threads.
7852           (set-buffer (or buffer gnus-summary-buffer))
7853           (gnus-summary-update-article (cdr gnus-article-current)))
7854         ;; Prettify the article buffer again.
7855         (unless no-highlight
7856           (save-excursion
7857             (set-buffer gnus-article-buffer)
7858             ;;;!!! Fix this -- article should be rehighlighted.
7859             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7860             (set-buffer gnus-original-article-buffer)
7861             (gnus-request-article
7862              (cdr gnus-article-current)
7863              (car gnus-article-current) (current-buffer))))
7864         ;; Prettify the summary buffer line.
7865         (when (gnus-visual-p 'summary-highlight 'highlight)
7866           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7867
7868 (defun gnus-summary-edit-wash (key)
7869   "Perform editing command KEY in the article buffer."
7870   (interactive
7871    (list
7872     (progn
7873       (message "%s" (concat (this-command-keys) "- "))
7874       (read-char))))
7875   (message "")
7876   (gnus-summary-edit-article)
7877   (execute-kbd-macro (concat (this-command-keys) key))
7878   (gnus-article-edit-done))
7879
7880 ;;; Respooling
7881
7882 (defun gnus-summary-respool-query (&optional silent trace)
7883   "Query where the respool algorithm would put this article."
7884   (interactive)
7885   (let (gnus-mark-article-hook)
7886     (gnus-summary-select-article)
7887     (save-excursion
7888       (set-buffer gnus-original-article-buffer)
7889       (save-restriction
7890         (message-narrow-to-head)
7891         (let ((groups (nnmail-article-group 'identity trace)))
7892           (unless silent
7893             (if groups
7894                 (message "This message would go to %s"
7895                          (mapconcat 'car groups ", "))
7896               (message "This message would go to no groups"))
7897             groups))))))
7898
7899 (defun gnus-summary-respool-trace ()
7900   "Trace where the respool algorithm would put this article.
7901 Display a buffer showing all fancy splitting patterns which matched."
7902   (interactive)
7903   (gnus-summary-respool-query nil t))
7904
7905 ;; Summary marking commands.
7906
7907 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7908   "Mark articles which has the same subject as read, and then select the next.
7909 If UNMARK is positive, remove any kind of mark.
7910 If UNMARK is negative, tick articles."
7911   (interactive "P")
7912   (when unmark
7913     (setq unmark (prefix-numeric-value unmark)))
7914   (let ((count
7915          (gnus-summary-mark-same-subject
7916           (gnus-summary-article-subject) unmark)))
7917     ;; Select next unread article.  If auto-select-same mode, should
7918     ;; select the first unread article.
7919     (gnus-summary-next-article t (and gnus-auto-select-same
7920                                       (gnus-summary-article-subject)))
7921     (gnus-message 7 "%d article%s marked as %s"
7922                   count (if (= count 1) " is" "s are")
7923                   (if unmark "unread" "read"))))
7924
7925 (defun gnus-summary-kill-same-subject (&optional unmark)
7926   "Mark articles which has the same subject as read.
7927 If UNMARK is positive, remove any kind of mark.
7928 If UNMARK is negative, tick articles."
7929   (interactive "P")
7930   (when unmark
7931     (setq unmark (prefix-numeric-value unmark)))
7932   (let ((count
7933          (gnus-summary-mark-same-subject
7934           (gnus-summary-article-subject) unmark)))
7935     ;; If marked as read, go to next unread subject.
7936     (when (null unmark)
7937       ;; Go to next unread subject.
7938       (gnus-summary-next-subject 1 t))
7939     (gnus-message 7 "%d articles are marked as %s"
7940                   count (if unmark "unread" "read"))))
7941
7942 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7943   "Mark articles with same SUBJECT as read, and return marked number.
7944 If optional argument UNMARK is positive, remove any kinds of marks.
7945 If optional argument UNMARK is negative, mark articles as unread instead."
7946   (let ((count 1))
7947     (save-excursion
7948       (cond
7949        ((null unmark)                   ; Mark as read.
7950         (while (and
7951                 (progn
7952                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7953                   (gnus-summary-show-thread) t)
7954                 (gnus-summary-find-subject subject))
7955           (setq count (1+ count))))
7956        ((> unmark 0)                    ; Tick.
7957         (while (and
7958                 (progn
7959                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7960                   (gnus-summary-show-thread) t)
7961                 (gnus-summary-find-subject subject))
7962           (setq count (1+ count))))
7963        (t                               ; Mark as unread.
7964         (while (and
7965                 (progn
7966                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7967                   (gnus-summary-show-thread) t)
7968                 (gnus-summary-find-subject subject))
7969           (setq count (1+ count)))))
7970       (gnus-set-mode-line 'summary)
7971       ;; Return the number of marked articles.
7972       count)))
7973
7974 (defun gnus-summary-mark-as-processable (n &optional unmark)
7975   "Set the process mark on the next N articles.
7976 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7977 the process mark instead.  The difference between N and the actual
7978 number of articles marked is returned."
7979   (interactive "p")
7980   (let ((backward (< n 0))
7981         (n (abs n)))
7982     (while (and
7983             (> n 0)
7984             (if unmark
7985                 (gnus-summary-remove-process-mark
7986                  (gnus-summary-article-number))
7987               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7988             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7989       (setq n (1- n)))
7990     (when (/= 0 n)
7991       (gnus-message 7 "No more articles"))
7992     (gnus-summary-recenter)
7993     (gnus-summary-position-point)
7994     n))
7995
7996 (defun gnus-summary-unmark-as-processable (n)
7997   "Remove the process mark from the next N articles.
7998 If N is negative, unmark backward instead.  The difference between N and
7999 the actual number of articles unmarked is returned."
8000   (interactive "p")
8001   (gnus-summary-mark-as-processable n t))
8002
8003 (defun gnus-summary-unmark-all-processable ()
8004   "Remove the process mark from all articles."
8005   (interactive)
8006   (save-excursion
8007     (while gnus-newsgroup-processable
8008       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8009   (gnus-summary-position-point))
8010
8011 (defun gnus-summary-mark-as-expirable (n)
8012   "Mark N articles forward as expirable.
8013 If N is negative, mark backward instead.  The difference between N and
8014 the actual number of articles marked is returned."
8015   (interactive "p")
8016   (gnus-summary-mark-forward n gnus-expirable-mark))
8017
8018 (defun gnus-summary-mark-article-as-replied (article)
8019   "Mark ARTICLE replied and update the summary line."
8020   (push article gnus-newsgroup-replied)
8021   (let ((buffer-read-only nil))
8022     (when (gnus-summary-goto-subject article nil t)
8023       (gnus-summary-update-secondary-mark article))))
8024
8025 (defun gnus-summary-set-bookmark (article)
8026   "Set a bookmark in current article."
8027   (interactive (list (gnus-summary-article-number)))
8028   (when (or (not (get-buffer gnus-article-buffer))
8029             (not gnus-current-article)
8030             (not gnus-article-current)
8031             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8032     (error "No current article selected"))
8033   ;; Remove old bookmark, if one exists.
8034   (let ((old (assq article gnus-newsgroup-bookmarks)))
8035     (when old
8036       (setq gnus-newsgroup-bookmarks
8037             (delq old gnus-newsgroup-bookmarks))))
8038   ;; Set the new bookmark, which is on the form
8039   ;; (article-number . line-number-in-body).
8040   (push
8041    (cons article
8042          (save-excursion
8043            (set-buffer gnus-article-buffer)
8044            (count-lines
8045             (min (point)
8046                  (save-excursion
8047                    (goto-char (point-min))
8048                    (search-forward "\n\n" nil t)
8049                    (point)))
8050             (point))))
8051    gnus-newsgroup-bookmarks)
8052   (gnus-message 6 "A bookmark has been added to the current article."))
8053
8054 (defun gnus-summary-remove-bookmark (article)
8055   "Remove the bookmark from the current article."
8056   (interactive (list (gnus-summary-article-number)))
8057   ;; Remove old bookmark, if one exists.
8058   (let ((old (assq article gnus-newsgroup-bookmarks)))
8059     (if old
8060         (progn
8061           (setq gnus-newsgroup-bookmarks
8062                 (delq old gnus-newsgroup-bookmarks))
8063           (gnus-message 6 "Removed bookmark."))
8064       (gnus-message 6 "No bookmark in current article."))))
8065
8066 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8067 (defun gnus-summary-mark-as-dormant (n)
8068   "Mark N articles forward as dormant.
8069 If N is negative, mark backward instead.  The difference between N and
8070 the actual number of articles marked is returned."
8071   (interactive "p")
8072   (gnus-summary-mark-forward n gnus-dormant-mark))
8073
8074 (defun gnus-summary-set-process-mark (article)
8075   "Set the process mark on ARTICLE and update the summary line."
8076   (setq gnus-newsgroup-processable
8077         (cons article
8078               (delq article gnus-newsgroup-processable)))
8079   (when (gnus-summary-goto-subject article)
8080     (gnus-summary-show-thread)
8081     (gnus-summary-goto-subject article)
8082     (gnus-summary-update-secondary-mark article)))
8083
8084 (defun gnus-summary-remove-process-mark (article)
8085   "Remove the process mark from ARTICLE and update the summary line."
8086   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8087   (when (gnus-summary-goto-subject article)
8088     (gnus-summary-show-thread)
8089     (gnus-summary-goto-subject article)
8090     (gnus-summary-update-secondary-mark article)))
8091
8092 (defun gnus-summary-set-saved-mark (article)
8093   "Set the process mark on ARTICLE and update the summary line."
8094   (push article gnus-newsgroup-saved)
8095   (when (gnus-summary-goto-subject article)
8096     (gnus-summary-update-secondary-mark article)))
8097
8098 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8099   "Mark N articles as read forwards.
8100 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8101 The difference between N and the actual number of articles marked is
8102 returned.
8103 Iff NO-EXPIRE, auto-expiry will be inhibited."
8104   (interactive "p")
8105   (gnus-summary-show-thread)
8106   (let ((backward (< n 0))
8107         (gnus-summary-goto-unread
8108          (and gnus-summary-goto-unread
8109               (not (eq gnus-summary-goto-unread 'never))
8110               (not (memq mark (list gnus-unread-mark
8111                                     gnus-ticked-mark gnus-dormant-mark)))))
8112         (n (abs n))
8113         (mark (or mark gnus-del-mark)))
8114     (while (and (> n 0)
8115                 (gnus-summary-mark-article nil mark no-expire)
8116                 (zerop (gnus-summary-next-subject
8117                         (if backward -1 1)
8118                         (and gnus-summary-goto-unread
8119                              (not (eq gnus-summary-goto-unread 'never)))
8120                         t)))
8121       (setq n (1- n)))
8122     (when (/= 0 n)
8123       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8124     (gnus-summary-recenter)
8125     (gnus-summary-position-point)
8126     (gnus-set-mode-line 'summary)
8127     n))
8128
8129 (defun gnus-summary-mark-article-as-read (mark)
8130   "Mark the current article quickly as read with MARK."
8131   (let ((article (gnus-summary-article-number)))
8132     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8133     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8134     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8135     (push (cons article mark) gnus-newsgroup-reads)
8136     ;; Possibly remove from cache, if that is used.
8137     (when gnus-use-cache
8138       (gnus-cache-enter-remove-article article))
8139     ;; Allow the backend to change the mark.
8140     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8141     ;; Check for auto-expiry.
8142     (when (and gnus-newsgroup-auto-expire
8143                (memq mark gnus-auto-expirable-marks))
8144       (setq mark gnus-expirable-mark)
8145       ;; Let the backend know about the mark change.
8146       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8147       (push article gnus-newsgroup-expirable))
8148     ;; Set the mark in the buffer.
8149     (gnus-summary-update-mark mark 'unread)
8150     t))
8151
8152 (defun gnus-summary-mark-article-as-unread (mark)
8153   "Mark the current article quickly as unread with MARK."
8154   (let* ((article (gnus-summary-article-number))
8155          (old-mark (gnus-summary-article-mark article)))
8156     ;; Allow the backend to change the mark.
8157     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8158     (if (eq mark old-mark)
8159         t
8160       (if (<= article 0)
8161           (progn
8162             (gnus-error 1 "Can't mark negative article numbers")
8163             nil)
8164         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8165         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8166         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8167         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8168         (cond ((= mark gnus-ticked-mark)
8169                (push article gnus-newsgroup-marked))
8170               ((= mark gnus-dormant-mark)
8171                (push article gnus-newsgroup-dormant))
8172               (t
8173                (push article gnus-newsgroup-unreads)))
8174         (gnus-pull article gnus-newsgroup-reads)
8175
8176         ;; See whether the article is to be put in the cache.
8177         (and gnus-use-cache
8178              (vectorp (gnus-summary-article-header article))
8179              (save-excursion
8180                (gnus-cache-possibly-enter-article
8181                 gnus-newsgroup-name article
8182                 (= mark gnus-ticked-mark)
8183                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8184
8185         ;; Fix the mark.
8186         (gnus-summary-update-mark mark 'unread)
8187         t))))
8188
8189 (defun gnus-summary-mark-article (&optional article mark no-expire)
8190   "Mark ARTICLE with MARK.  MARK can be any character.
8191 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8192 `??' (dormant) and `?E' (expirable).
8193 If MARK is nil, then the default character `?r' is used.
8194 If ARTICLE is nil, then the article on the current line will be
8195 marked.
8196 Iff NO-EXPIRE, auto-expiry will be inhibited."
8197   ;; The mark might be a string.
8198   (when (stringp mark)
8199     (setq mark (aref mark 0)))
8200   ;; If no mark is given, then we check auto-expiring.
8201   (when (null mark)
8202     (setq mark gnus-del-mark))
8203   (when (and (not no-expire)
8204              gnus-newsgroup-auto-expire
8205              (memq mark gnus-auto-expirable-marks))
8206     (setq mark gnus-expirable-mark))
8207   (let ((article (or 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       (unless article
8214         (error "No article on current line"))
8215       (if (not (if (or (= mark gnus-unread-mark)
8216                        (= mark gnus-ticked-mark)
8217                        (= mark gnus-dormant-mark))
8218                    (gnus-mark-article-as-unread article mark)
8219                  (gnus-mark-article-as-read article mark)))
8220           t
8221         ;; See whether the article is to be put in the cache.
8222         (and gnus-use-cache
8223              (not (= mark gnus-canceled-mark))
8224              (vectorp (gnus-summary-article-header article))
8225              (save-excursion
8226                (gnus-cache-possibly-enter-article
8227                 gnus-newsgroup-name article
8228                 (= mark gnus-ticked-mark)
8229                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8230
8231         (when (gnus-summary-goto-subject article nil t)
8232           (let ((buffer-read-only nil))
8233             (gnus-summary-show-thread)
8234             ;; Fix the mark.
8235             (gnus-summary-update-mark mark 'unread)
8236             t))))))
8237
8238 (defun gnus-summary-update-secondary-mark (article)
8239   "Update the secondary (read, process, cache) mark."
8240   (gnus-summary-update-mark
8241    (cond ((memq article gnus-newsgroup-processable)
8242           gnus-process-mark)
8243          ((memq article gnus-newsgroup-cached)
8244           gnus-cached-mark)
8245          ((memq article gnus-newsgroup-replied)
8246           gnus-replied-mark)
8247          ((memq article gnus-newsgroup-saved)
8248           gnus-saved-mark)
8249          (t gnus-unread-mark))
8250    'replied)
8251   (when (gnus-visual-p 'summary-highlight 'highlight)
8252     (gnus-run-hooks 'gnus-summary-update-hook))
8253   t)
8254
8255 (defun gnus-summary-update-mark (mark type)
8256   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8257         (buffer-read-only nil))
8258     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8259     (when forward
8260       (when (looking-at "\r")
8261         (incf forward))
8262       (when (<= (+ forward (point)) (point-max))
8263         ;; Go to the right position on the line.
8264         (goto-char (+ forward (point)))
8265         ;; Replace the old mark with the new mark.
8266         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8267         ;; Optionally update the marks by some user rule.
8268         (when (eq type 'unread)
8269           (gnus-data-set-mark
8270            (gnus-data-find (gnus-summary-article-number)) mark)
8271           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8272
8273 (defun gnus-mark-article-as-read (article &optional mark)
8274   "Enter ARTICLE in the pertinent lists and remove it from others."
8275   ;; Make the article expirable.
8276   (let ((mark (or mark gnus-del-mark)))
8277     (if (= mark gnus-expirable-mark)
8278         (push article gnus-newsgroup-expirable)
8279       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8280     ;; Remove from unread and marked lists.
8281     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8282     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8283     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8284     (push (cons article mark) gnus-newsgroup-reads)
8285     ;; Possibly remove from cache, if that is used.
8286     (when gnus-use-cache
8287       (gnus-cache-enter-remove-article article))
8288     t))
8289
8290 (defun gnus-mark-article-as-unread (article &optional mark)
8291   "Enter ARTICLE in the pertinent lists and remove it from others."
8292   (let ((mark (or mark gnus-ticked-mark)))
8293     (if (<= article 0)
8294         (progn
8295           (gnus-error 1 "Can't mark negative article numbers")
8296           nil)
8297       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8298             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8299             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8300             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8301
8302       ;; Unsuppress duplicates?
8303       (when gnus-suppress-duplicates
8304         (gnus-dup-unsuppress-article article))
8305
8306       (cond ((= mark gnus-ticked-mark)
8307              (push article gnus-newsgroup-marked))
8308             ((= mark gnus-dormant-mark)
8309              (push article gnus-newsgroup-dormant))
8310             (t
8311              (push article gnus-newsgroup-unreads)))
8312       (gnus-pull article gnus-newsgroup-reads)
8313       t)))
8314
8315 (defalias 'gnus-summary-mark-as-unread-forward
8316   'gnus-summary-tick-article-forward)
8317 (make-obsolete 'gnus-summary-mark-as-unread-forward
8318                'gnus-summary-tick-article-forward)
8319 (defun gnus-summary-tick-article-forward (n)
8320   "Tick N articles forwards.
8321 If N is negative, tick backwards instead.
8322 The difference between N and the number of articles ticked is returned."
8323   (interactive "p")
8324   (gnus-summary-mark-forward n gnus-ticked-mark))
8325
8326 (defalias 'gnus-summary-mark-as-unread-backward
8327   'gnus-summary-tick-article-backward)
8328 (make-obsolete 'gnus-summary-mark-as-unread-backward
8329                'gnus-summary-tick-article-backward)
8330 (defun gnus-summary-tick-article-backward (n)
8331   "Tick N articles backwards.
8332 The difference between N and the number of articles ticked is returned."
8333   (interactive "p")
8334   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8335
8336 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8337 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8338 (defun gnus-summary-tick-article (&optional article clear-mark)
8339   "Mark current article as unread.
8340 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8341 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8342   (interactive)
8343   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8344                                        gnus-ticked-mark)))
8345
8346 (defun gnus-summary-mark-as-read-forward (n)
8347   "Mark N articles as read forwards.
8348 If N is negative, mark backwards instead.
8349 The difference between N and the actual number of articles marked is
8350 returned."
8351   (interactive "p")
8352   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8353
8354 (defun gnus-summary-mark-as-read-backward (n)
8355   "Mark the N articles as read backwards.
8356 The difference between N and the actual number of articles marked is
8357 returned."
8358   (interactive "p")
8359   (gnus-summary-mark-forward
8360    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8361
8362 (defun gnus-summary-mark-as-read (&optional article mark)
8363   "Mark current article as read.
8364 ARTICLE specifies the article to be marked as read.
8365 MARK specifies a string to be inserted at the beginning of the line."
8366   (gnus-summary-mark-article article mark))
8367
8368 (defun gnus-summary-clear-mark-forward (n)
8369   "Clear marks from N articles forward.
8370 If N is negative, clear backward instead.
8371 The difference between N and the number of marks cleared is returned."
8372   (interactive "p")
8373   (gnus-summary-mark-forward n gnus-unread-mark))
8374
8375 (defun gnus-summary-clear-mark-backward (n)
8376   "Clear marks from N articles backward.
8377 The difference between N and the number of marks cleared is returned."
8378   (interactive "p")
8379   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8380
8381 (defun gnus-summary-mark-unread-as-read ()
8382   "Intended to be used by `gnus-summary-mark-article-hook'."
8383   (when (memq gnus-current-article gnus-newsgroup-unreads)
8384     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8385
8386 (defun gnus-summary-mark-read-and-unread-as-read ()
8387   "Intended to be used by `gnus-summary-mark-article-hook'."
8388   (let ((mark (gnus-summary-article-mark)))
8389     (when (or (gnus-unread-mark-p mark)
8390               (gnus-read-mark-p mark))
8391       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8392
8393 (defun gnus-summary-mark-region-as-read (point mark all)
8394   "Mark all unread articles between point and mark as read.
8395 If given a prefix, mark all articles between point and mark as read,
8396 even ticked and dormant ones."
8397   (interactive "r\nP")
8398   (save-excursion
8399     (let (article)
8400       (goto-char point)
8401       (beginning-of-line)
8402       (while (and
8403               (< (point) mark)
8404               (progn
8405                 (when (or all
8406                           (memq (setq article (gnus-summary-article-number))
8407                                 gnus-newsgroup-unreads))
8408                   (gnus-summary-mark-article article gnus-del-mark))
8409                 t)
8410               (gnus-summary-find-next))))))
8411
8412 (defun gnus-summary-mark-below (score mark)
8413   "Mark articles with score less than SCORE with MARK."
8414   (interactive "P\ncMark: ")
8415   (setq score (if score
8416                   (prefix-numeric-value score)
8417                 (or gnus-summary-default-score 0)))
8418   (save-excursion
8419     (set-buffer gnus-summary-buffer)
8420     (goto-char (point-min))
8421     (while
8422         (progn
8423           (and (< (gnus-summary-article-score) score)
8424                (gnus-summary-mark-article nil mark))
8425           (gnus-summary-find-next)))))
8426
8427 (defun gnus-summary-kill-below (&optional score)
8428   "Mark articles with score below SCORE as read."
8429   (interactive "P")
8430   (gnus-summary-mark-below score gnus-killed-mark))
8431
8432 (defun gnus-summary-clear-above (&optional score)
8433   "Clear all marks from articles with score above SCORE."
8434   (interactive "P")
8435   (gnus-summary-mark-above score gnus-unread-mark))
8436
8437 (defun gnus-summary-tick-above (&optional score)
8438   "Tick all articles with score above SCORE."
8439   (interactive "P")
8440   (gnus-summary-mark-above score gnus-ticked-mark))
8441
8442 (defun gnus-summary-mark-above (score mark)
8443   "Mark articles with score over SCORE with MARK."
8444   (interactive "P\ncMark: ")
8445   (setq score (if score
8446                   (prefix-numeric-value score)
8447                 (or gnus-summary-default-score 0)))
8448   (save-excursion
8449     (set-buffer gnus-summary-buffer)
8450     (goto-char (point-min))
8451     (while (and (progn
8452                   (when (> (gnus-summary-article-score) score)
8453                     (gnus-summary-mark-article nil mark))
8454                   t)
8455                 (gnus-summary-find-next)))))
8456
8457 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8458 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8459 (defun gnus-summary-limit-include-expunged (&optional no-error)
8460   "Display all the hidden articles that were expunged for low scores."
8461   (interactive)
8462   (let ((buffer-read-only nil))
8463     (let ((scored gnus-newsgroup-scored)
8464           headers h)
8465       (while scored
8466         (unless (gnus-summary-goto-subject (caar scored))
8467           (and (setq h (gnus-summary-article-header (caar scored)))
8468                (< (cdar scored) gnus-summary-expunge-below)
8469                (push h headers)))
8470         (setq scored (cdr scored)))
8471       (if (not headers)
8472           (when (not no-error)
8473             (error "No expunged articles hidden"))
8474         (goto-char (point-min))
8475         (gnus-summary-prepare-unthreaded (nreverse headers))
8476         (goto-char (point-min))
8477         (gnus-summary-position-point)
8478         t))))
8479
8480 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8481   "Mark all unread articles in this newsgroup as read.
8482 If prefix argument ALL is non-nil, ticked and dormant articles will
8483 also be marked as read.
8484 If QUIETLY is non-nil, no questions will be asked.
8485 If TO-HERE is non-nil, it should be a point in the buffer.  All
8486 articles before this point will be marked as read.
8487 Note that this function will only catch up the unread article
8488 in the current summary buffer limitation.
8489 The number of articles marked as read is returned."
8490   (interactive "P")
8491   (prog1
8492       (save-excursion
8493         (when (or quietly
8494                   (not gnus-interactive-catchup) ;Without confirmation?
8495                   gnus-expert-user
8496                   (gnus-y-or-n-p
8497                    (if all
8498                        "Mark absolutely all articles as read? "
8499                      "Mark all unread articles as read? ")))
8500           (if (and not-mark
8501                    (not gnus-newsgroup-adaptive)
8502                    (not gnus-newsgroup-auto-expire)
8503                    (not gnus-suppress-duplicates)
8504                    (or (not gnus-use-cache)
8505                        (eq gnus-use-cache 'passive)))
8506               (progn
8507                 (when all
8508                   (setq gnus-newsgroup-marked nil
8509                         gnus-newsgroup-dormant nil))
8510                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8511             ;; We actually mark all articles as canceled, which we
8512             ;; have to do when using auto-expiry or adaptive scoring.
8513             (gnus-summary-show-all-threads)
8514             (when (gnus-summary-first-subject (not all) t)
8515               (while (and
8516                       (if to-here (< (point) to-here) t)
8517                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8518                       (gnus-summary-find-next (not all) nil nil t))))
8519             (gnus-set-mode-line 'summary))
8520           t))
8521     (gnus-summary-position-point)))
8522
8523 (defun gnus-summary-catchup-to-here (&optional all)
8524   "Mark all unticked articles before the current one as read.
8525 If ALL is non-nil, also mark ticked and dormant articles as read."
8526   (interactive "P")
8527   (save-excursion
8528     (gnus-save-hidden-threads
8529       (let ((beg (point)))
8530         ;; We check that there are unread articles.
8531         (when (or all (gnus-summary-find-prev))
8532           (gnus-summary-catchup all t beg)))))
8533   (gnus-summary-position-point))
8534
8535 (defun gnus-summary-catchup-all (&optional quietly)
8536   "Mark all articles in this newsgroup as read."
8537   (interactive "P")
8538   (gnus-summary-catchup t quietly))
8539
8540 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8541   "Mark all unread articles in this group as read, then exit.
8542 If prefix argument ALL is non-nil, all articles are marked as read."
8543   (interactive "P")
8544   (when (gnus-summary-catchup all quietly nil 'fast)
8545     ;; Select next newsgroup or exit.
8546     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8547              (eq gnus-auto-select-next 'quietly))
8548         (gnus-summary-next-group nil)
8549       (gnus-summary-exit))))
8550
8551 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8552   "Mark all articles in this newsgroup as read, and then exit."
8553   (interactive "P")
8554   (gnus-summary-catchup-and-exit t quietly))
8555
8556 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8557   "Mark all articles in this group as read and select the next group.
8558 If given a prefix, mark all articles, unread as well as ticked, as
8559 read."
8560   (interactive "P")
8561   (save-excursion
8562     (gnus-summary-catchup all))
8563   (gnus-summary-next-group))
8564
8565 ;;;
8566 ;;; with article
8567 ;;;
8568
8569 (defmacro gnus-with-article (article &rest forms)
8570   "Select ARTICLE and perform FORMS in the original article buffer.
8571 Then replace the article with the result."
8572   `(progn
8573      ;; We don't want the article to be marked as read.
8574      (let (gnus-mark-article-hook)
8575        (gnus-summary-select-article t t nil ,article))
8576      (set-buffer gnus-original-article-buffer)
8577      ,@forms
8578      (if (not (gnus-check-backend-function
8579                'request-replace-article (car gnus-article-current)))
8580          (gnus-message 5 "Read-only group; not replacing")
8581        (unless (gnus-request-replace-article
8582                 ,article (car gnus-article-current)
8583                 (current-buffer) t)
8584          (error "Couldn't replace article")))
8585      ;; The cache and backlog have to be flushed somewhat.
8586      (when gnus-keep-backlog
8587        (gnus-backlog-remove-article
8588         (car gnus-article-current) (cdr gnus-article-current)))
8589      (when gnus-use-cache
8590        (gnus-cache-update-article
8591         (car gnus-article-current) (cdr gnus-article-current)))))
8592
8593 (put 'gnus-with-article 'lisp-indent-function 1)
8594 (put 'gnus-with-article 'edebug-form-spec '(form body))
8595
8596 ;; Thread-based commands.
8597
8598 (defun gnus-summary-articles-in-thread (&optional article)
8599   "Return a list of all articles in the current thread.
8600 If ARTICLE is non-nil, return all articles in the thread that starts
8601 with that article."
8602   (let* ((article (or article (gnus-summary-article-number)))
8603          (data (gnus-data-find-list article))
8604          (top-level (gnus-data-level (car data)))
8605          (top-subject
8606           (cond ((null gnus-thread-operation-ignore-subject)
8607                  (gnus-simplify-subject-re
8608                   (mail-header-subject (gnus-data-header (car data)))))
8609                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8610                  (gnus-simplify-subject-fuzzy
8611                   (mail-header-subject (gnus-data-header (car data)))))
8612                 (t nil)))
8613          (end-point (save-excursion
8614                       (if (gnus-summary-go-to-next-thread)
8615                           (point) (point-max))))
8616          articles)
8617     (while (and data
8618                 (< (gnus-data-pos (car data)) end-point))
8619       (when (or (not top-subject)
8620                 (string= top-subject
8621                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8622                              (gnus-simplify-subject-fuzzy
8623                               (mail-header-subject
8624                                (gnus-data-header (car data))))
8625                            (gnus-simplify-subject-re
8626                             (mail-header-subject
8627                              (gnus-data-header (car data)))))))
8628         (push (gnus-data-number (car data)) articles))
8629       (unless (and (setq data (cdr data))
8630                    (> (gnus-data-level (car data)) top-level))
8631         (setq data nil)))
8632     ;; Return the list of articles.
8633     (nreverse articles)))
8634
8635 (defun gnus-summary-rethread-current ()
8636   "Rethread the thread the current article is part of."
8637   (interactive)
8638   (let* ((gnus-show-threads t)
8639          (article (gnus-summary-article-number))
8640          (id (mail-header-id (gnus-summary-article-header)))
8641          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8642     (unless id
8643       (error "No article on the current line"))
8644     (gnus-rebuild-thread id)
8645     (gnus-summary-goto-subject article)))
8646
8647 (defun gnus-summary-reparent-thread ()
8648   "Make the current article child of the marked (or previous) article.
8649
8650 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8651 is non-nil or the Subject: of both articles are the same."
8652   (interactive)
8653   (unless (not (gnus-group-read-only-p))
8654     (error "The current newsgroup does not support article editing"))
8655   (unless (<= (length gnus-newsgroup-processable) 1)
8656     (error "No more than one article may be marked"))
8657   (save-window-excursion
8658     (let ((gnus-article-buffer " *reparent*")
8659           (current-article (gnus-summary-article-number))
8660           ;; First grab the marked article, otherwise one line up.
8661           (parent-article (if (not (null gnus-newsgroup-processable))
8662                               (car gnus-newsgroup-processable)
8663                             (save-excursion
8664                               (if (eq (forward-line -1) 0)
8665                                   (gnus-summary-article-number)
8666                                 (error "Beginning of summary buffer"))))))
8667       (unless (not (eq current-article parent-article))
8668         (error "An article may not be self-referential"))
8669       (let ((message-id (mail-header-id
8670                          (gnus-summary-article-header parent-article))))
8671         (unless (and message-id (not (equal message-id "")))
8672           (error "No message-id in desired parent"))
8673         (gnus-with-article current-article
8674           (goto-char (point-min))
8675           (if (re-search-forward "^References: " nil t)
8676               (progn
8677                 (re-search-forward "^[^ \t]" nil t)
8678                 (forward-line -1)
8679                 (end-of-line)
8680                 (insert " " message-id))
8681             (insert "References: " message-id "\n")))
8682         (set-buffer gnus-summary-buffer)
8683         (gnus-summary-unmark-all-processable)
8684         (gnus-summary-update-article current-article)
8685         (gnus-summary-rethread-current)
8686         (gnus-message 3 "Article %d is now the child of article %d"
8687                       current-article parent-article)))))
8688
8689 (defun gnus-summary-toggle-threads (&optional arg)
8690   "Toggle showing conversation threads.
8691 If ARG is positive number, turn showing conversation threads on."
8692   (interactive "P")
8693   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8694     (setq gnus-show-threads
8695           (if (null arg) (not gnus-show-threads)
8696             (> (prefix-numeric-value arg) 0)))
8697     (gnus-summary-prepare)
8698     (gnus-summary-goto-subject current)
8699     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8700     (gnus-summary-position-point)))
8701
8702 (defun gnus-summary-show-all-threads ()
8703   "Show all threads."
8704   (interactive)
8705   (save-excursion
8706     (let ((buffer-read-only nil))
8707       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8708   (gnus-summary-position-point))
8709
8710 (defun gnus-summary-show-thread ()
8711   "Show thread subtrees.
8712 Returns nil if no thread was there to be shown."
8713   (interactive)
8714   (let ((buffer-read-only nil)
8715         (orig (point))
8716         ;; first goto end then to beg, to have point at beg after let
8717         (end (progn (end-of-line) (point)))
8718         (beg (progn (beginning-of-line) (point))))
8719     (prog1
8720         ;; Any hidden lines here?
8721         (search-forward "\r" end t)
8722       (subst-char-in-region beg end ?\^M ?\n t)
8723       (goto-char orig)
8724       (gnus-summary-position-point))))
8725
8726 (defun gnus-summary-hide-all-threads ()
8727   "Hide all thread subtrees."
8728   (interactive)
8729   (save-excursion
8730     (goto-char (point-min))
8731     (gnus-summary-hide-thread)
8732     (while (zerop (gnus-summary-next-thread 1 t))
8733       (gnus-summary-hide-thread)))
8734   (gnus-summary-position-point))
8735
8736 (defun gnus-summary-hide-thread ()
8737   "Hide thread subtrees.
8738 Returns nil if no threads were there to be hidden."
8739   (interactive)
8740   (let ((buffer-read-only nil)
8741         (start (point))
8742         (article (gnus-summary-article-number)))
8743     (goto-char start)
8744     ;; Go forward until either the buffer ends or the subthread
8745     ;; ends.
8746     (when (and (not (eobp))
8747                (or (zerop (gnus-summary-next-thread 1 t))
8748                    (goto-char (point-max))))
8749       (prog1
8750           (if (and (> (point) start)
8751                    (search-backward "\n" start t))
8752               (progn
8753                 (subst-char-in-region start (point) ?\n ?\^M)
8754                 (gnus-summary-goto-subject article))
8755             (goto-char start)
8756             nil)))))
8757
8758 (defun gnus-summary-go-to-next-thread (&optional previous)
8759   "Go to the same level (or less) next thread.
8760 If PREVIOUS is non-nil, go to previous thread instead.
8761 Return the article number moved to, or nil if moving was impossible."
8762   (let ((level (gnus-summary-thread-level))
8763         (way (if previous -1 1))
8764         (beg (point)))
8765     (forward-line way)
8766     (while (and (not (eobp))
8767                 (< level (gnus-summary-thread-level)))
8768       (forward-line way))
8769     (if (eobp)
8770         (progn
8771           (goto-char beg)
8772           nil)
8773       (setq beg (point))
8774       (prog1
8775           (gnus-summary-article-number)
8776         (goto-char beg)))))
8777
8778 (defun gnus-summary-next-thread (n &optional silent)
8779   "Go to the same level next N'th thread.
8780 If N is negative, search backward instead.
8781 Returns the difference between N and the number of skips actually
8782 done.
8783
8784 If SILENT, don't output messages."
8785   (interactive "p")
8786   (let ((backward (< n 0))
8787         (n (abs n)))
8788     (while (and (> n 0)
8789                 (gnus-summary-go-to-next-thread backward))
8790       (decf n))
8791     (unless silent
8792       (gnus-summary-position-point))
8793     (when (and (not silent) (/= 0 n))
8794       (gnus-message 7 "No more threads"))
8795     n))
8796
8797 (defun gnus-summary-prev-thread (n)
8798   "Go to the same level previous N'th thread.
8799 Returns the difference between N and the number of skips actually
8800 done."
8801   (interactive "p")
8802   (gnus-summary-next-thread (- n)))
8803
8804 (defun gnus-summary-go-down-thread ()
8805   "Go down one level in the current thread."
8806   (let ((children (gnus-summary-article-children)))
8807     (when children
8808       (gnus-summary-goto-subject (car children)))))
8809
8810 (defun gnus-summary-go-up-thread ()
8811   "Go up one level in the current thread."
8812   (let ((parent (gnus-summary-article-parent)))
8813     (when parent
8814       (gnus-summary-goto-subject parent))))
8815
8816 (defun gnus-summary-down-thread (n)
8817   "Go down thread N steps.
8818 If N is negative, go up instead.
8819 Returns the difference between N and how many steps down that were
8820 taken."
8821   (interactive "p")
8822   (let ((up (< n 0))
8823         (n (abs n)))
8824     (while (and (> n 0)
8825                 (if up (gnus-summary-go-up-thread)
8826                   (gnus-summary-go-down-thread)))
8827       (setq n (1- n)))
8828     (gnus-summary-position-point)
8829     (when (/= 0 n)
8830       (gnus-message 7 "Can't go further"))
8831     n))
8832
8833 (defun gnus-summary-up-thread (n)
8834   "Go up thread N steps.
8835 If N is negative, go up instead.
8836 Returns the difference between N and how many steps down that were
8837 taken."
8838   (interactive "p")
8839   (gnus-summary-down-thread (- n)))
8840
8841 (defun gnus-summary-top-thread ()
8842   "Go to the top of the thread."
8843   (interactive)
8844   (while (gnus-summary-go-up-thread))
8845   (gnus-summary-article-number))
8846
8847 (defun gnus-summary-kill-thread (&optional unmark)
8848   "Mark articles under current thread as read.
8849 If the prefix argument is positive, remove any kinds of marks.
8850 If the prefix argument is negative, tick articles instead."
8851   (interactive "P")
8852   (when unmark
8853     (setq unmark (prefix-numeric-value unmark)))
8854   (let ((articles (gnus-summary-articles-in-thread)))
8855     (save-excursion
8856       ;; Expand the thread.
8857       (gnus-summary-show-thread)
8858       ;; Mark all the articles.
8859       (while articles
8860         (gnus-summary-goto-subject (car articles))
8861         (cond ((null unmark)
8862                (gnus-summary-mark-article-as-read gnus-killed-mark))
8863               ((> unmark 0)
8864                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8865               (t
8866                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8867         (setq articles (cdr articles))))
8868     ;; Hide killed subtrees.
8869     (and (null unmark)
8870          gnus-thread-hide-killed
8871          (gnus-summary-hide-thread))
8872     ;; If marked as read, go to next unread subject.
8873     (when (null unmark)
8874       ;; Go to next unread subject.
8875       (gnus-summary-next-subject 1 t)))
8876   (gnus-set-mode-line 'summary))
8877
8878 ;; Summary sorting commands
8879
8880 (defun gnus-summary-sort-by-number (&optional reverse)
8881   "Sort the summary buffer by article number.
8882 Argument REVERSE means reverse order."
8883   (interactive "P")
8884   (gnus-summary-sort 'number reverse))
8885
8886 (defun gnus-summary-sort-by-author (&optional reverse)
8887   "Sort the summary buffer by author name alphabetically.
8888 If case-fold-search is non-nil, case of letters is ignored.
8889 Argument REVERSE means reverse order."
8890   (interactive "P")
8891   (gnus-summary-sort 'author reverse))
8892
8893 (defun gnus-summary-sort-by-subject (&optional reverse)
8894   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8895 If case-fold-search is non-nil, case of letters is ignored.
8896 Argument REVERSE means reverse order."
8897   (interactive "P")
8898   (gnus-summary-sort 'subject reverse))
8899
8900 (defun gnus-summary-sort-by-date (&optional reverse)
8901   "Sort the summary buffer by date.
8902 Argument REVERSE means reverse order."
8903   (interactive "P")
8904   (gnus-summary-sort 'date reverse))
8905
8906 (defun gnus-summary-sort-by-score (&optional reverse)
8907   "Sort the summary buffer by score.
8908 Argument REVERSE means reverse order."
8909   (interactive "P")
8910   (gnus-summary-sort 'score reverse))
8911
8912 (defun gnus-summary-sort-by-lines (&optional reverse)
8913   "Sort the summary buffer by the number of lines.
8914 Argument REVERSE means reverse order."
8915   (interactive "P")
8916   (gnus-summary-sort 'lines reverse))
8917
8918 (defun gnus-summary-sort-by-chars (&optional reverse)
8919   "Sort the summary buffer by article length.
8920 Argument REVERSE means reverse order."
8921   (interactive "P")
8922   (gnus-summary-sort 'chars reverse))   
8923
8924 (defun gnus-summary-sort (predicate reverse)
8925   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8926   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8927          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8928          (gnus-thread-sort-functions
8929           (if (not reverse)
8930               thread
8931             `(lambda (t1 t2)
8932                (,thread t2 t1))))
8933          (gnus-article-sort-functions
8934           (if (not reverse)
8935               article
8936             `(lambda (t1 t2)
8937                (,article t2 t1))))
8938          (buffer-read-only)
8939          (gnus-summary-prepare-hook nil))
8940     ;; We do the sorting by regenerating the threads.
8941     (gnus-summary-prepare)
8942     ;; Hide subthreads if needed.
8943     (when (and gnus-show-threads gnus-thread-hide-subtree)
8944       (gnus-summary-hide-all-threads))))
8945
8946 ;; Summary saving commands.
8947
8948 (defun gnus-summary-save-article (&optional n not-saved)
8949   "Save the current article using the default saver function.
8950 If N is a positive number, save the N next articles.
8951 If N is a negative number, save the N previous articles.
8952 If N is nil and any articles have been marked with the process mark,
8953 save those articles instead.
8954 The variable `gnus-default-article-saver' specifies the saver function."
8955   (interactive "P")
8956   (let* ((articles (gnus-summary-work-articles n))
8957          (save-buffer (save-excursion
8958                         (nnheader-set-temp-buffer " *Gnus Save*")))
8959          (num (length articles))
8960          header file)
8961     (dolist (article articles)
8962       (setq header (gnus-summary-article-header article))
8963       (if (not (vectorp header))
8964           ;; This is a pseudo-article.
8965           (if (assq 'name header)
8966               (gnus-copy-file (cdr (assq 'name header)))
8967             (gnus-message 1 "Article %d is unsaveable" article))
8968         ;; This is a real article.
8969         (save-window-excursion
8970           (gnus-summary-select-article t nil nil article))
8971         (save-excursion
8972           (set-buffer save-buffer)
8973           (erase-buffer)
8974           (insert-buffer-substring gnus-original-article-buffer))
8975         (setq file (gnus-article-save save-buffer file num))
8976         (gnus-summary-remove-process-mark article)
8977         (unless not-saved
8978           (gnus-summary-set-saved-mark article))))
8979     (gnus-kill-buffer save-buffer)
8980     (gnus-summary-position-point)
8981     (gnus-set-mode-line 'summary)
8982     n))
8983
8984 (defun gnus-summary-pipe-output (&optional arg)
8985   "Pipe the current article to a subprocess.
8986 If N is a positive number, pipe the N next articles.
8987 If N is a negative number, pipe the N previous articles.
8988 If N is nil and any articles have been marked with the process mark,
8989 pipe those articles instead."
8990   (interactive "P")
8991   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8992     (gnus-summary-save-article arg t))
8993   (gnus-configure-windows 'pipe))
8994
8995 (defun gnus-summary-save-article-mail (&optional arg)
8996   "Append the current article to an mail file.
8997 If N is a positive number, save the N next articles.
8998 If N is a negative number, save the N previous articles.
8999 If N is nil and any articles have been marked with the process mark,
9000 save those articles instead."
9001   (interactive "P")
9002   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9003     (gnus-summary-save-article arg)))
9004
9005 (defun gnus-summary-save-article-rmail (&optional arg)
9006   "Append the current article to an rmail file.
9007 If N is a positive number, save the N next articles.
9008 If N is a negative number, save the N previous articles.
9009 If N is nil and any articles have been marked with the process mark,
9010 save those articles instead."
9011   (interactive "P")
9012   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
9013     (gnus-summary-save-article arg)))
9014
9015 (defun gnus-summary-save-article-file (&optional arg)
9016   "Append the current article to a file.
9017 If N is a positive number, save the N next articles.
9018 If N is a negative number, save the N previous articles.
9019 If N is nil and any articles have been marked with the process mark,
9020 save those articles instead."
9021   (interactive "P")
9022   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9023     (gnus-summary-save-article arg)))
9024
9025 (defun gnus-summary-write-article-file (&optional arg)
9026   "Write the current article to a file, deleting the previous file.
9027 If N is a positive number, save the N next articles.
9028 If N is a negative number, save the N previous articles.
9029 If N is nil and any articles have been marked with the process mark,
9030 save those articles instead."
9031   (interactive "P")
9032   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9033     (gnus-summary-save-article arg)))
9034
9035 (defun gnus-summary-save-article-body-file (&optional arg)
9036   "Append the current article body to a file.
9037 If N is a positive number, save the N next articles.
9038 If N is a negative number, save the N previous articles.
9039 If N is nil and any articles have been marked with the process mark,
9040 save those articles instead."
9041   (interactive "P")
9042   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9043     (gnus-summary-save-article arg)))
9044
9045 (defun gnus-summary-pipe-message (program)
9046   "Pipe the current article through PROGRAM."
9047   (interactive "sProgram: ")
9048   (gnus-summary-select-article)
9049   (let ((mail-header-separator ""))
9050     (gnus-eval-in-buffer-window gnus-article-buffer
9051       (save-restriction
9052         (widen)
9053         (let ((start (window-start))
9054               buffer-read-only)
9055           (message-pipe-buffer-body program)
9056           (set-window-start (get-buffer-window (current-buffer)) start))))))
9057
9058 (defun gnus-get-split-value (methods)
9059   "Return a value based on the split METHODS."
9060   (let (split-name method result match)
9061     (when methods
9062       (save-excursion
9063         (set-buffer gnus-original-article-buffer)
9064         (save-restriction
9065           (nnheader-narrow-to-headers)
9066           (while methods
9067             (goto-char (point-min))
9068             (setq method (pop methods))
9069             (setq match (car method))
9070             (when (cond
9071                    ((stringp match)
9072                     ;; Regular expression.
9073                     (ignore-errors
9074                       (re-search-forward match nil t)))
9075                    ((gnus-functionp match)
9076                     ;; Function.
9077                     (save-restriction
9078                       (widen)
9079                       (setq result (funcall match gnus-newsgroup-name))))
9080                    ((consp match)
9081                     ;; Form.
9082                     (save-restriction
9083                       (widen)
9084                       (setq result (eval match)))))
9085               (setq split-name (append (cdr method) split-name))
9086               (cond ((stringp result)
9087                      (push (expand-file-name
9088                             result gnus-article-save-directory)
9089                            split-name))
9090                     ((consp result)
9091                      (setq split-name (append result split-name)))))))))
9092     (nreverse split-name)))
9093
9094 (defun gnus-valid-move-group-p (group)
9095   (and (boundp group)
9096        (symbol-name group)
9097        (symbol-value group)
9098        (gnus-get-function (gnus-find-method-for-group
9099                            (symbol-name group)) 'request-accept-article t)))
9100
9101 (defun gnus-read-move-group-name (prompt default articles prefix)
9102   "Read a group name."
9103   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9104          (minibuffer-confirm-incomplete nil) ; XEmacs
9105          (prom
9106           (format "%s %s to:"
9107                   prompt
9108                   (if (> (length articles) 1)
9109                       (format "these %d articles" (length articles))
9110                     "this article")))
9111          (to-newsgroup
9112           (cond
9113            ((null split-name)
9114             (gnus-completing-read default prom
9115                                   gnus-active-hashtb
9116                                   'gnus-valid-move-group-p
9117                                   nil prefix
9118                                   'gnus-group-history))
9119            ((= 1 (length split-name))
9120             (gnus-completing-read (car split-name) prom
9121                                   gnus-active-hashtb
9122                                   'gnus-valid-move-group-p
9123                                   nil nil
9124                                   'gnus-group-history))
9125            (t
9126             (gnus-completing-read nil prom
9127                                   (mapcar (lambda (el) (list el))
9128                                           (nreverse split-name))
9129                                   nil nil nil
9130                                   'gnus-group-history))))
9131          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9132     (when to-newsgroup
9133       (if (or (string= to-newsgroup "")
9134               (string= to-newsgroup prefix))
9135           (setq to-newsgroup default))
9136       (unless to-newsgroup
9137         (error "No group name entered"))
9138       (or (gnus-active to-newsgroup)
9139           (gnus-activate-group to-newsgroup nil nil to-method)
9140           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9141                                      to-newsgroup))
9142               (or (and (gnus-request-create-group to-newsgroup to-method)
9143                        (gnus-activate-group
9144                         to-newsgroup nil nil to-method)
9145                        (gnus-subscribe-group to-newsgroup))
9146                   (error "Couldn't create group %s" to-newsgroup)))
9147           (error "No such group: %s" to-newsgroup)))
9148     to-newsgroup))
9149
9150 (defun gnus-summary-save-parts (type dir n &optional reverse)
9151   "Save parts matching TYPE to DIR.
9152 If REVERSE, save parts that do not match TYPE."
9153   (interactive
9154    (list (read-string "Save parts of type: " "image/.*")
9155          (read-file-name "Save to directory: " nil nil t)
9156          current-prefix-arg))
9157   (gnus-summary-iterate n
9158     (let ((gnus-display-mime-function nil)
9159           (gnus-inhibit-treatment t))
9160       (gnus-summary-select-article))
9161     (save-excursion
9162       (set-buffer gnus-article-buffer)
9163       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9164         (when handles
9165           (gnus-summary-save-parts-1 type dir handles reverse)
9166           (mm-destroy-parts handles))))))
9167
9168 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9169   (if (stringp (car handle))
9170       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9171               (cdr handle))
9172     (when (if reverse
9173               (not (string-match type (mm-handle-media-type handle)))
9174             (string-match type (mm-handle-media-type handle)))
9175       (let ((file (expand-file-name
9176                    (file-name-nondirectory
9177                     (or
9178                      (mail-content-type-get
9179                       (mm-handle-disposition handle) 'filename)
9180                      (concat gnus-newsgroup-name "." gnus-current-article)))
9181                    dir)))
9182         (unless (file-exists-p file)
9183           (mm-save-part-to-file handle file))))))
9184
9185 ;; Summary extract commands
9186
9187 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9188   (let ((buffer-read-only nil)
9189         (article (gnus-summary-article-number))
9190         after-article b e)
9191     (unless (gnus-summary-goto-subject article)
9192       (error "No such article: %d" article))
9193     (gnus-summary-position-point)
9194     ;; If all commands are to be bunched up on one line, we collect
9195     ;; them here.
9196     (unless gnus-view-pseudos-separately
9197       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9198             files action)
9199         (while ps
9200           (setq action (cdr (assq 'action (car ps))))
9201           (setq files (list (cdr (assq 'name (car ps)))))
9202           (while (and ps (cdr ps)
9203                       (string= (or action "1")
9204                                (or (cdr (assq 'action (cadr ps))) "2")))
9205             (push (cdr (assq 'name (cadr ps))) files)
9206             (setcdr ps (cddr ps)))
9207           (when files
9208             (when (not (string-match "%s" action))
9209               (push " " files))
9210             (push " " files)
9211             (when (assq 'execute (car ps))
9212               (setcdr (assq 'execute (car ps))
9213                       (funcall (if (string-match "%s" action)
9214                                    'format 'concat)
9215                                action
9216                                (mapconcat
9217                                 (lambda (f)
9218                                   (if (equal f " ")
9219                                       f
9220                                     (mm-quote-arg f)))
9221                                 files " ")))))
9222           (setq ps (cdr ps)))))
9223     (if (and gnus-view-pseudos (not not-view))
9224         (while pslist
9225           (when (assq 'execute (car pslist))
9226             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9227                                   (eq gnus-view-pseudos 'not-confirm)))
9228           (setq pslist (cdr pslist)))
9229       (save-excursion
9230         (while pslist
9231           (setq after-article (or (cdr (assq 'article (car pslist)))
9232                                   (gnus-summary-article-number)))
9233           (gnus-summary-goto-subject after-article)
9234           (forward-line 1)
9235           (setq b (point))
9236           (insert "    " (file-name-nondirectory
9237                           (cdr (assq 'name (car pslist))))
9238                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9239           (setq e (point))
9240           (forward-line -1)             ; back to `b'
9241           (gnus-add-text-properties
9242            b (1- e) (list 'gnus-number gnus-reffed-article-number
9243                           gnus-mouse-face-prop gnus-mouse-face))
9244           (gnus-data-enter
9245            after-article gnus-reffed-article-number
9246            gnus-unread-mark b (car pslist) 0 (- e b))
9247           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9248           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9249           (setq pslist (cdr pslist)))))))
9250
9251 (defun gnus-pseudos< (p1 p2)
9252   (let ((c1 (cdr (assq 'action p1)))
9253         (c2 (cdr (assq 'action p2))))
9254     (and c1 c2 (string< c1 c2))))
9255
9256 (defun gnus-request-pseudo-article (props)
9257   (cond ((assq 'execute props)
9258          (gnus-execute-command (cdr (assq 'execute props)))))
9259   (let ((gnus-current-article (gnus-summary-article-number)))
9260     (gnus-run-hooks 'gnus-mark-article-hook)))
9261
9262 (defun gnus-execute-command (command &optional automatic)
9263   (save-excursion
9264     (gnus-article-setup-buffer)
9265     (set-buffer gnus-article-buffer)
9266     (setq buffer-read-only nil)
9267     (let ((command (if automatic command
9268                      (read-string "Command: " (cons command 0)))))
9269       (erase-buffer)
9270       (insert "$ " command "\n\n")
9271       (if gnus-view-pseudo-asynchronously
9272           (start-process "gnus-execute" (current-buffer) shell-file-name
9273                          shell-command-switch command)
9274         (call-process shell-file-name nil t nil
9275                       shell-command-switch command)))))
9276
9277 ;; Summary kill commands.
9278
9279 (defun gnus-summary-edit-global-kill (article)
9280   "Edit the \"global\" kill file."
9281   (interactive (list (gnus-summary-article-number)))
9282   (gnus-group-edit-global-kill article))
9283
9284 (defun gnus-summary-edit-local-kill ()
9285   "Edit a local kill file applied to the current newsgroup."
9286   (interactive)
9287   (setq gnus-current-headers (gnus-summary-article-header))
9288   (gnus-group-edit-local-kill
9289    (gnus-summary-article-number) gnus-newsgroup-name))
9290
9291 ;;; Header reading.
9292
9293 (defun gnus-read-header (id &optional header)
9294   "Read the headers of article ID and enter them into the Gnus system."
9295   (let ((group gnus-newsgroup-name)
9296         (gnus-override-method
9297          (or
9298           gnus-override-method
9299           (and (gnus-news-group-p gnus-newsgroup-name)
9300                (car (gnus-refer-article-methods)))))
9301         where)
9302     ;; First we check to see whether the header in question is already
9303     ;; fetched.
9304     (if (stringp id)
9305         ;; This is a Message-ID.
9306         (setq header (or header (gnus-id-to-header id)))
9307       ;; This is an article number.
9308       (setq header (or header (gnus-summary-article-header id))))
9309     (if (and header
9310              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9311         ;; We have found the header.
9312         header
9313       ;; If this is a sparse article, we have to nix out its
9314       ;; previous entry in the thread hashtb.
9315       (when (and header
9316                  (gnus-summary-article-sparse-p (mail-header-number header)))
9317         (let* ((parent (gnus-parent-id (mail-header-references header)))
9318                (thread (and parent (gnus-id-to-thread parent))))
9319           (when thread
9320             (delq (assq header thread) thread))))
9321       ;; We have to really fetch the header to this article.
9322       (save-excursion
9323         (set-buffer nntp-server-buffer)
9324         (when (setq where (gnus-request-head id group))
9325           (nnheader-fold-continuation-lines)
9326           (goto-char (point-max))
9327           (insert ".\n")
9328           (goto-char (point-min))
9329           (insert "211 ")
9330           (princ (cond
9331                   ((numberp id) id)
9332                   ((cdr where) (cdr where))
9333                   (header (mail-header-number header))
9334                   (t gnus-reffed-article-number))
9335                  (current-buffer))
9336           (insert " Article retrieved.\n"))
9337         (if (or (not where)
9338                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9339             ()                          ; Malformed head.
9340           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9341             (when (and (stringp id)
9342                        (not (string= (gnus-group-real-name group)
9343                                      (car where))))
9344               ;; If we fetched by Message-ID and the article came
9345               ;; from a different group, we fudge some bogus article
9346               ;; numbers for this article.
9347               (mail-header-set-number header gnus-reffed-article-number))
9348             (save-excursion
9349               (set-buffer gnus-summary-buffer)
9350               (decf gnus-reffed-article-number)
9351               (gnus-remove-header (mail-header-number header))
9352               (push header gnus-newsgroup-headers)
9353               (setq gnus-current-headers header)
9354               (push (mail-header-number header) gnus-newsgroup-limit)))
9355           header)))))
9356
9357 (defun gnus-remove-header (number)
9358   "Remove header NUMBER from `gnus-newsgroup-headers'."
9359   (if (and gnus-newsgroup-headers
9360            (= number (mail-header-number (car gnus-newsgroup-headers))))
9361       (pop gnus-newsgroup-headers)
9362     (let ((headers gnus-newsgroup-headers))
9363       (while (and (cdr headers)
9364                   (not (= number (mail-header-number (cadr headers)))))
9365         (pop headers))
9366       (when (cdr headers)
9367         (setcdr headers (cddr headers))))))
9368
9369 ;;;
9370 ;;; summary highlights
9371 ;;;
9372
9373 (defun gnus-highlight-selected-summary ()
9374   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9375   ;; Highlight selected article in summary buffer
9376   (when gnus-summary-selected-face
9377     (save-excursion
9378       (let* ((beg (progn (beginning-of-line) (point)))
9379              (end (progn (end-of-line) (point)))
9380              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9381              (from (if (get-text-property beg gnus-mouse-face-prop)
9382                        beg
9383                      (or (next-single-property-change
9384                           beg gnus-mouse-face-prop nil end)
9385                          beg)))
9386              (to
9387               (if (= from end)
9388                   (- from 2)
9389                 (or (next-single-property-change
9390                      from gnus-mouse-face-prop nil end)
9391                     end))))
9392         ;; If no mouse-face prop on line we will have to = from = end,
9393         ;; so we highlight the entire line instead.
9394         (when (= (+ to 2) from)
9395           (setq from beg)
9396           (setq to end))
9397         (if gnus-newsgroup-selected-overlay
9398             ;; Move old overlay.
9399             (gnus-move-overlay
9400              gnus-newsgroup-selected-overlay from to (current-buffer))
9401           ;; Create new overlay.
9402           (gnus-overlay-put
9403            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9404            'face gnus-summary-selected-face))))))
9405
9406 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9407 (defun gnus-summary-highlight-line ()
9408   "Highlight current line according to `gnus-summary-highlight'."
9409   (let* ((list gnus-summary-highlight)
9410          (p (point))
9411          (end (progn (end-of-line) (point)))
9412          ;; now find out where the line starts and leave point there.
9413          (beg (progn (beginning-of-line) (point)))
9414          (article (gnus-summary-article-number))
9415          (score (or (cdr (assq (or article gnus-current-article)
9416                                gnus-newsgroup-scored))
9417                     gnus-summary-default-score 0))
9418          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9419          (inhibit-read-only t))
9420     ;; Eval the cars of the lists until we find a match.
9421     (let ((default gnus-summary-default-score))
9422       (while (and list
9423                   (not (eval (caar list))))
9424         (setq list (cdr list))))
9425     (let ((face (cdar list)))
9426       (unless (eq face (get-text-property beg 'face))
9427         (gnus-put-text-property-excluding-characters-with-faces
9428          beg end 'face
9429          (setq face (if (boundp face) (symbol-value face) face)))
9430         (when gnus-summary-highlight-line-function
9431           (funcall gnus-summary-highlight-line-function article face))))
9432     (goto-char p)))
9433
9434 (defun gnus-update-read-articles (group unread &optional compute)
9435   "Update the list of read articles in GROUP."
9436   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9437          (entry (gnus-gethash group gnus-newsrc-hashtb))
9438          (info (nth 2 entry))
9439          (prev 1)
9440          (unread (sort (copy-sequence unread) '<))
9441          read)
9442     (if (or (not info) (not active))
9443         ;; There is no info on this group if it was, in fact,
9444         ;; killed.  Gnus stores no information on killed groups, so
9445         ;; there's nothing to be done.
9446         ;; One could store the information somewhere temporarily,
9447         ;; perhaps...  Hmmm...
9448         ()
9449       ;; Remove any negative articles numbers.
9450       (while (and unread (< (car unread) 0))
9451         (setq unread (cdr unread)))
9452       ;; Remove any expired article numbers
9453       (while (and unread (< (car unread) (car active)))
9454         (setq unread (cdr unread)))
9455       ;; Compute the ranges of read articles by looking at the list of
9456       ;; unread articles.
9457       (while unread
9458         (when (/= (car unread) prev)
9459           (push (if (= prev (1- (car unread))) prev
9460                   (cons prev (1- (car unread))))
9461                 read))
9462         (setq prev (1+ (car unread)))
9463         (setq unread (cdr unread)))
9464       (when (<= prev (cdr active))
9465         (push (cons prev (cdr active)) read))
9466       (setq read (if (> (length read) 1) (nreverse read) read))
9467       (if compute
9468           read
9469         (save-excursion
9470           (set-buffer gnus-group-buffer)
9471           (gnus-undo-register
9472             `(progn
9473                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9474                (gnus-info-set-read ',info ',(gnus-info-read info))
9475                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9476                (gnus-group-update-group ,group t))))
9477         ;; Propagate the read marks to the backend.
9478         (if (gnus-check-backend-function 'request-set-mark group)
9479             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9480                   (add (gnus-remove-from-range read (gnus-info-read info))))
9481               (when (or add del)
9482                 (unless (gnus-check-group group)
9483                   (error "Can't open server for %s" group))
9484                 (gnus-request-set-mark
9485                  group (delq nil (list (if add (list add 'add '(read)))
9486                                        (if del (list del 'del '(read)))))))))
9487         ;; Enter this list into the group info.
9488         (gnus-info-set-read info read)
9489         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9490         (gnus-get-unread-articles-in-group info (gnus-active group))
9491         t))))
9492
9493 (defun gnus-offer-save-summaries ()
9494   "Offer to save all active summary buffers."
9495   (save-excursion
9496     (let ((buflist (buffer-list))
9497           buffers bufname)
9498       ;; Go through all buffers and find all summaries.
9499       (while buflist
9500         (and (setq bufname (buffer-name (car buflist)))
9501              (string-match "Summary" bufname)
9502              (save-excursion
9503                (set-buffer bufname)
9504                ;; We check that this is, indeed, a summary buffer.
9505                (and (eq major-mode 'gnus-summary-mode)
9506                     ;; Also make sure this isn't bogus.
9507                     gnus-newsgroup-prepared
9508                     ;; Also make sure that this isn't a dead summary buffer.
9509                     (not gnus-dead-summary-mode)))
9510              (push bufname buffers))
9511         (setq buflist (cdr buflist)))
9512       ;; Go through all these summary buffers and offer to save them.
9513       (when buffers
9514         (map-y-or-n-p
9515          "Update summary buffer %s? "
9516          (lambda (buf)
9517            (switch-to-buffer buf)
9518            (gnus-summary-exit))
9519          buffers)))))
9520
9521 (defun gnus-summary-setup-default-charset ()
9522   "Setup newsgroup default charset."
9523   (if (equal gnus-newsgroup-name "nndraft:drafts")
9524       (setq gnus-newsgroup-charset nil)
9525     (let* ((name (and gnus-newsgroup-name
9526                       (gnus-group-real-name gnus-newsgroup-name)))
9527            (ignored-charsets 
9528             (or gnus-newsgroup-ephemeral-ignored-charsets
9529                 (append
9530                  (and gnus-newsgroup-name
9531                       (or (gnus-group-find-parameter gnus-newsgroup-name
9532                                                      'ignored-charsets t)
9533                           (let ((alist gnus-group-ignored-charsets-alist)
9534                                 elem (charsets nil))
9535                             (while (setq elem (pop alist))
9536                               (when (and name
9537                                          (string-match (car elem) name))
9538                                 (setq alist nil
9539                                       charsets (cdr elem))))
9540                             charsets)))
9541                  gnus-newsgroup-ignored-charsets))))
9542       (setq gnus-newsgroup-charset
9543             (or gnus-newsgroup-ephemeral-charset
9544                 (and gnus-newsgroup-name
9545                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9546                          (let ((alist gnus-group-charset-alist)
9547                                elem charset)
9548                            (while (setq elem (pop alist))
9549                              (when (and name
9550                                         (string-match (car elem) name))
9551                                (setq alist nil
9552                                      charset (cadr elem))))
9553                            charset)))
9554                 gnus-default-charset))
9555       (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9556            ignored-charsets))))
9557
9558 ;;;
9559 ;;; Mime Commands
9560 ;;;
9561
9562 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9563   "Display the current article buffer fully MIME-buttonized.
9564 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9565 treated as multipart/mixed."
9566   (interactive "P")
9567   (require 'gnus-art)
9568   (let ((gnus-unbuttonized-mime-types nil)
9569         (gnus-mime-display-multipart-as-mixed show-all-parts))
9570     (gnus-summary-show-article)))
9571
9572 (defun gnus-summary-repair-multipart (article)
9573   "Add a Content-Type header to a multipart article without one."
9574   (interactive (list (gnus-summary-article-number)))
9575   (gnus-with-article article
9576     (message-narrow-to-head)
9577     (goto-char (point-max))
9578     (widen)
9579     (when (search-forward "\n--" nil t)
9580       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9581         (message-narrow-to-head)
9582         (message-remove-header "Mime-Version")
9583         (message-remove-header "Content-Type")
9584         (goto-char (point-max))
9585         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9586                         separator))
9587         (insert "Mime-Version: 1.0\n")
9588         (widen))))
9589   (let (gnus-mark-article-hook)
9590     (gnus-summary-select-article t t nil article)))
9591
9592 (defun gnus-summary-toggle-display-buttonized ()
9593   "Toggle the buttonizing of the article buffer."
9594   (interactive)
9595   (require 'gnus-art)
9596   (if (setq gnus-inhibit-mime-unbuttonizing
9597             (not gnus-inhibit-mime-unbuttonizing))
9598       (let ((gnus-unbuttonized-mime-types nil))
9599         (gnus-summary-show-article))
9600     (gnus-summary-show-article)))
9601
9602 ;;;
9603 ;;; Generic summary marking commands
9604 ;;;
9605
9606 (defvar gnus-summary-marking-alist
9607   '((read gnus-del-mark "d")
9608     (unread gnus-unread-mark "u")
9609     (ticked gnus-ticked-mark "!")
9610     (dormant gnus-dormant-mark "?")
9611     (expirable gnus-expirable-mark "e"))
9612   "An alist of names/marks/keystrokes.")
9613
9614 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9615 (defvar gnus-summary-mark-map)
9616
9617 (defun gnus-summary-make-all-marking-commands ()
9618   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9619   (dolist (elem gnus-summary-marking-alist)
9620     (apply 'gnus-summary-make-marking-command elem)))
9621
9622 (defun gnus-summary-make-marking-command (name mark keystroke)
9623   (let ((map (make-sparse-keymap)))
9624     (define-key gnus-summary-generic-mark-map keystroke map)
9625     (dolist (lway `((next "next" next nil "n")
9626                     (next-unread "next unread" next t "N")
9627                     (prev "previous" prev nil "p")
9628                     (prev-unread "previous unread" prev t "P")
9629                     (nomove "" nil nil ,keystroke)))
9630       (let ((func (gnus-summary-make-marking-command-1
9631                    mark (car lway) lway name)))
9632         (setq func (eval func))
9633         (define-key map (nth 4 lway) func)))))
9634       
9635 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9636   `(defun ,(intern
9637             (format "gnus-summary-put-mark-as-%s%s"
9638                     name (if (eq way 'nomove)
9639                              ""
9640                            (concat "-" (symbol-name way)))))
9641      (n)
9642      ,(format
9643        "Mark the current article as %s%s.
9644 If N, the prefix, then repeat N times.
9645 If N is negative, move in reverse order.
9646 The difference between N and the actual number of articles marked is
9647 returned."
9648        name (cadr lway))
9649      (interactive "p")
9650      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9651     
9652 (defun gnus-summary-generic-mark (n mark move unread)
9653   "Mark N articles with MARK."
9654   (unless (eq major-mode 'gnus-summary-mode)
9655     (error "This command can only be used in the summary buffer"))
9656   (gnus-summary-show-thread)
9657   (let ((nummove
9658          (cond
9659           ((eq move 'next) 1)
9660           ((eq move 'prev) -1)
9661           (t 0))))
9662     (if (zerop nummove)
9663         (setq n 1)
9664       (when (< n 0)
9665         (setq n (abs n)
9666               nummove (* -1 nummove))))
9667     (while (and (> n 0)
9668                 (gnus-summary-mark-article nil mark)
9669                 (zerop (gnus-summary-next-subject nummove unread t)))
9670       (setq n (1- n)))
9671     (when (/= 0 n)
9672       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9673     (gnus-summary-recenter)
9674     (gnus-summary-position-point)
9675     (gnus-set-mode-line 'summary)
9676     n))
9677
9678 (gnus-summary-make-all-marking-commands)
9679
9680 (gnus-ems-redefine)
9681
9682 (provide 'gnus-sum)
9683
9684 (run-hooks 'gnus-sum-load-hook)
9685
9686 ;;; gnus-sum.el ends here