* gnus-sum.el (gnus-summary-reparent-thread): Narrow to the
[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 (defcustom gnus-orphan-score nil
883   "*All orphans get this score added.  Set in the score file."
884   :group 'gnus-score-default
885   :type '(choice (const nil)
886                  integer))
887
888 ;;; Internal variables
889
890 (defvar gnus-article-mime-handles nil)
891 (defvar gnus-article-decoded-p nil)
892 (defvar gnus-scores-exclude-files nil)
893 (defvar gnus-page-broken nil)
894 (defvar gnus-inhibit-mime-unbuttonizing nil)
895
896 (defvar gnus-original-article nil)
897 (defvar gnus-article-internal-prepare-hook nil)
898 (defvar gnus-newsgroup-process-stack nil)
899
900 (defvar gnus-thread-indent-array nil)
901 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
902 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
903   "Function called to sort the articles within a thread after it has been gathered together.")
904
905 ;; Avoid highlighting in kill files.
906 (defvar gnus-summary-inhibit-highlight nil)
907 (defvar gnus-newsgroup-selected-overlay nil)
908 (defvar gnus-inhibit-limiting nil)
909 (defvar gnus-newsgroup-adaptive-score-file nil)
910 (defvar gnus-current-score-file nil)
911 (defvar gnus-current-move-group nil)
912 (defvar gnus-current-copy-group nil)
913 (defvar gnus-current-crosspost-group nil)
914
915 (defvar gnus-newsgroup-dependencies nil)
916 (defvar gnus-newsgroup-adaptive nil)
917 (defvar gnus-summary-display-article-function nil)
918 (defvar gnus-summary-highlight-line-function nil
919   "Function called after highlighting a summary line.")
920
921 (defvar gnus-summary-line-format-alist
922   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
923     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
924     (?s gnus-tmp-subject-or-nil ?s)
925     (?n gnus-tmp-name ?s)
926     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
927         ?s)
928     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
929             gnus-tmp-from) ?s)
930     (?F gnus-tmp-from ?s)
931     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
932     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
933     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
934     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
935     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
936     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
937     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
938     (?L gnus-tmp-lines ?d)
939     (?I gnus-tmp-indentation ?s)
940     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
941     (?R gnus-tmp-replied ?c)
942     (?\[ gnus-tmp-opening-bracket ?c)
943     (?\] gnus-tmp-closing-bracket ?c)
944     (?\> (make-string gnus-tmp-level ? ) ?s)
945     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
946     (?i gnus-tmp-score ?d)
947     (?z gnus-tmp-score-char ?c)
948     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
949     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
950     (?U gnus-tmp-unread ?c)
951     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
952     (?t (gnus-summary-number-of-articles-in-thread
953          (and (boundp 'thread) (car thread)) gnus-tmp-level)
954         ?d)
955     (?e (gnus-summary-number-of-articles-in-thread
956          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
957         ?c)
958     (?u gnus-tmp-user-defined ?s)
959     (?P (gnus-pick-line-number) ?d))
960   "An alist of format specifications that can appear in summary lines.
961 These are paired with what variables they correspond with, along with
962 the type of the variable (string, integer, character, etc).")
963
964 (defvar gnus-summary-dummy-line-format-alist
965   `((?S gnus-tmp-subject ?s)
966     (?N gnus-tmp-number ?d)
967     (?u gnus-tmp-user-defined ?s)))
968
969 (defvar gnus-summary-mode-line-format-alist
970   `((?G gnus-tmp-group-name ?s)
971     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
972     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
973     (?A gnus-tmp-article-number ?d)
974     (?Z gnus-tmp-unread-and-unselected ?s)
975     (?V gnus-version ?s)
976     (?U gnus-tmp-unread-and-unticked ?d)
977     (?S gnus-tmp-subject ?s)
978     (?e gnus-tmp-unselected ?d)
979     (?u gnus-tmp-user-defined ?s)
980     (?d (length gnus-newsgroup-dormant) ?d)
981     (?t (length gnus-newsgroup-marked) ?d)
982     (?r (length gnus-newsgroup-reads) ?d)
983     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
984     (?E gnus-newsgroup-expunged-tally ?d)
985     (?s (gnus-current-score-file-nondirectory) ?s)))
986
987 (defvar gnus-last-search-regexp nil
988   "Default regexp for article search command.")
989
990 (defvar gnus-last-shell-command nil
991   "Default shell command on article.")
992
993 (defvar gnus-newsgroup-begin nil)
994 (defvar gnus-newsgroup-end nil)
995 (defvar gnus-newsgroup-last-rmail nil)
996 (defvar gnus-newsgroup-last-mail nil)
997 (defvar gnus-newsgroup-last-folder nil)
998 (defvar gnus-newsgroup-last-file nil)
999 (defvar gnus-newsgroup-auto-expire nil)
1000 (defvar gnus-newsgroup-active nil)
1001
1002 (defvar gnus-newsgroup-data nil)
1003 (defvar gnus-newsgroup-data-reverse nil)
1004 (defvar gnus-newsgroup-limit nil)
1005 (defvar gnus-newsgroup-limits nil)
1006
1007 (defvar gnus-newsgroup-unreads nil
1008   "List of unread articles in the current newsgroup.")
1009
1010 (defvar gnus-newsgroup-unselected nil
1011   "List of unselected unread articles in the current newsgroup.")
1012
1013 (defvar gnus-newsgroup-reads nil
1014   "Alist of read articles and article marks in the current newsgroup.")
1015
1016 (defvar gnus-newsgroup-expunged-tally nil)
1017
1018 (defvar gnus-newsgroup-marked nil
1019   "List of ticked articles in the current newsgroup (a subset of unread art).")
1020
1021 (defvar gnus-newsgroup-killed nil
1022   "List of ranges of articles that have been through the scoring process.")
1023
1024 (defvar gnus-newsgroup-cached nil
1025   "List of articles that come from the article cache.")
1026
1027 (defvar gnus-newsgroup-saved nil
1028   "List of articles that have been saved.")
1029
1030 (defvar gnus-newsgroup-kill-headers nil)
1031
1032 (defvar gnus-newsgroup-replied nil
1033   "List of articles that have been replied to in the current newsgroup.")
1034
1035 (defvar gnus-newsgroup-expirable nil
1036   "List of articles in the current newsgroup that can be expired.")
1037
1038 (defvar gnus-newsgroup-processable nil
1039   "List of articles in the current newsgroup that can be processed.")
1040
1041 (defvar gnus-newsgroup-downloadable nil
1042   "List of articles in the current newsgroup that can be processed.")
1043
1044 (defvar gnus-newsgroup-undownloaded nil
1045   "List of articles in the current newsgroup that haven't been downloaded..")
1046
1047 (defvar gnus-newsgroup-unsendable nil
1048   "List of articles in the current newsgroup that won't be sent.")
1049
1050 (defvar gnus-newsgroup-bookmarks nil
1051   "List of articles in the current newsgroup that have bookmarks.")
1052
1053 (defvar gnus-newsgroup-dormant nil
1054   "List of dormant articles in the current newsgroup.")
1055
1056 (defvar gnus-newsgroup-scored nil
1057   "List of scored articles in the current newsgroup.")
1058
1059 (defvar gnus-newsgroup-headers nil
1060   "List of article headers in the current newsgroup.")
1061
1062 (defvar gnus-newsgroup-threads nil)
1063
1064 (defvar gnus-newsgroup-prepared nil
1065   "Whether the current group has been prepared properly.")
1066
1067 (defvar gnus-newsgroup-ancient nil
1068   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1069
1070 (defvar gnus-newsgroup-sparse nil)
1071
1072 (defvar gnus-current-article nil)
1073 (defvar gnus-article-current nil)
1074 (defvar gnus-current-headers nil)
1075 (defvar gnus-have-all-headers nil)
1076 (defvar gnus-last-article nil)
1077 (defvar gnus-newsgroup-history nil)
1078 (defvar gnus-newsgroup-charset nil)
1079 (defvar gnus-newsgroup-ephemeral-charset nil)
1080 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1081
1082 (defconst gnus-summary-local-variables
1083   '(gnus-newsgroup-name
1084     gnus-newsgroup-begin gnus-newsgroup-end
1085     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1086     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1087     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1088     gnus-newsgroup-unselected gnus-newsgroup-marked
1089     gnus-newsgroup-reads gnus-newsgroup-saved
1090     gnus-newsgroup-replied gnus-newsgroup-expirable
1091     gnus-newsgroup-processable gnus-newsgroup-killed
1092     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1093     gnus-newsgroup-unsendable
1094     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1095     gnus-newsgroup-headers gnus-newsgroup-threads
1096     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1097     gnus-current-article gnus-current-headers gnus-have-all-headers
1098     gnus-last-article gnus-article-internal-prepare-hook
1099     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1100     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1101     gnus-thread-expunge-below
1102     gnus-score-alist gnus-current-score-file
1103     (gnus-summary-expunge-below . global)
1104     (gnus-summary-mark-below . global)
1105     (gnus-orphan-score . global)
1106     gnus-newsgroup-active gnus-scores-exclude-files
1107     gnus-newsgroup-history gnus-newsgroup-ancient
1108     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1109     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1110     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1111     (gnus-newsgroup-expunged-tally . 0)
1112     gnus-cache-removable-articles gnus-newsgroup-cached
1113     gnus-newsgroup-data gnus-newsgroup-data-reverse
1114     gnus-newsgroup-limit gnus-newsgroup-limits
1115     gnus-newsgroup-charset)
1116   "Variables that are buffer-local to the summary buffers.")
1117
1118 ;; Byte-compiler warning.
1119 (defvar gnus-article-mode-map)
1120
1121 ;; MIME stuff.
1122
1123 (defvar gnus-decode-encoded-word-methods
1124   '(mail-decode-encoded-word-string)
1125   "List of methods used to decode encoded words.
1126
1127 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1128 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1129 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1130 whose names match REGEXP.
1131
1132 For example:
1133 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1134  mail-decode-encoded-word-string
1135  (\"chinese\" . rfc1843-decode-string))")
1136
1137 (defvar gnus-decode-encoded-word-methods-cache nil)
1138
1139 (defun gnus-multi-decode-encoded-word-string (string)
1140   "Apply the functions from `gnus-encoded-word-methods' that match."
1141   (unless (and gnus-decode-encoded-word-methods-cache
1142                (eq gnus-newsgroup-name
1143                    (car gnus-decode-encoded-word-methods-cache)))
1144     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1145     (mapcar (lambda (x)
1146               (if (symbolp x)
1147                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1148                 (if (and gnus-newsgroup-name
1149                          (string-match (car x) gnus-newsgroup-name))
1150                     (nconc gnus-decode-encoded-word-methods-cache
1151                            (list (cdr x))))))
1152           gnus-decode-encoded-word-methods))
1153   (let ((xlist gnus-decode-encoded-word-methods-cache))
1154     (pop xlist)
1155     (while xlist
1156       (setq string (funcall (pop xlist) string))))
1157   string)
1158
1159 ;; Subject simplification.
1160
1161 (defun gnus-simplify-whitespace (str)
1162   "Remove excessive whitespace from STR."
1163   (let ((mystr str))
1164     ;; Multiple spaces.
1165     (while (string-match "[ \t][ \t]+" mystr)
1166       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1167                           " "
1168                           (substring mystr (match-end 0)))))
1169     ;; Leading spaces.
1170     (when (string-match "^[ \t]+" mystr)
1171       (setq mystr (substring mystr (match-end 0))))
1172     ;; Trailing spaces.
1173     (when (string-match "[ \t]+$" mystr)
1174       (setq mystr (substring mystr 0 (match-beginning 0))))
1175     mystr))
1176
1177 (defsubst gnus-simplify-subject-re (subject)
1178   "Remove \"Re:\" from subject lines."
1179   (if (string-match "^[Rr][Ee]: *" subject)
1180       (substring subject (match-end 0))
1181     subject))
1182
1183 (defun gnus-simplify-subject (subject &optional re-only)
1184   "Remove `Re:' and words in parentheses.
1185 If RE-ONLY is non-nil, strip leading `Re:'s only."
1186   (let ((case-fold-search t))           ;Ignore case.
1187     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1188     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1189       (setq subject (substring subject (match-end 0))))
1190     ;; Remove uninteresting prefixes.
1191     (when (and (not re-only)
1192                gnus-simplify-ignored-prefixes
1193                (string-match gnus-simplify-ignored-prefixes subject))
1194       (setq subject (substring subject (match-end 0))))
1195     ;; Remove words in parentheses from end.
1196     (unless re-only
1197       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1198         (setq subject (substring subject 0 (match-beginning 0)))))
1199     ;; Return subject string.
1200     subject))
1201
1202 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1203 ;; all whitespace.
1204 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1205   (goto-char (point-min))
1206   (while (re-search-forward regexp nil t)
1207     (replace-match (or newtext ""))))
1208
1209 (defun gnus-simplify-buffer-fuzzy ()
1210   "Simplify string in the buffer fuzzily.
1211 The string in the accessible portion of the current buffer is simplified.
1212 It is assumed to be a single-line subject.
1213 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1214 matter is removed.  Additional things can be deleted by setting
1215 `gnus-simplify-subject-fuzzy-regexp'."
1216   (let ((case-fold-search t)
1217         (modified-tick))
1218     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1219
1220     (while (not (eq modified-tick (buffer-modified-tick)))
1221       (setq modified-tick (buffer-modified-tick))
1222       (cond
1223        ((listp gnus-simplify-subject-fuzzy-regexp)
1224         (mapcar 'gnus-simplify-buffer-fuzzy-step
1225                 gnus-simplify-subject-fuzzy-regexp))
1226        (gnus-simplify-subject-fuzzy-regexp
1227         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1228       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1229       (gnus-simplify-buffer-fuzzy-step
1230        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1231       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1232
1233     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1234     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1235     (gnus-simplify-buffer-fuzzy-step " $")
1236     (gnus-simplify-buffer-fuzzy-step "^ +")))
1237
1238 (defun gnus-simplify-subject-fuzzy (subject)
1239   "Simplify a subject string fuzzily.
1240 See `gnus-simplify-buffer-fuzzy' for details."
1241   (save-excursion
1242     (gnus-set-work-buffer)
1243     (let ((case-fold-search t))
1244       ;; Remove uninteresting prefixes.
1245       (when (and gnus-simplify-ignored-prefixes
1246                  (string-match gnus-simplify-ignored-prefixes subject))
1247         (setq subject (substring subject (match-end 0))))
1248       (insert subject)
1249       (inline (gnus-simplify-buffer-fuzzy))
1250       (buffer-string))))
1251
1252 (defsubst gnus-simplify-subject-fully (subject)
1253   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1254   (cond
1255    (gnus-simplify-subject-functions
1256     (gnus-map-function gnus-simplify-subject-functions subject))
1257    ((null gnus-summary-gather-subject-limit)
1258     (gnus-simplify-subject-re subject))
1259    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1260     (gnus-simplify-subject-fuzzy subject))
1261    ((numberp gnus-summary-gather-subject-limit)
1262     (gnus-limit-string (gnus-simplify-subject-re subject)
1263                        gnus-summary-gather-subject-limit))
1264    (t
1265     subject)))
1266
1267 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1268   "Check whether two subjects are equal.
1269 If optional argument simple-first is t, first argument is already
1270 simplified."
1271   (cond
1272    ((null simple-first)
1273     (equal (gnus-simplify-subject-fully s1)
1274            (gnus-simplify-subject-fully s2)))
1275    (t
1276     (equal s1
1277            (gnus-simplify-subject-fully s2)))))
1278
1279 (defun gnus-summary-bubble-group ()
1280   "Increase the score of the current group.
1281 This is a handy function to add to `gnus-summary-exit-hook' to
1282 increase the score of each group you read."
1283   (gnus-group-add-score gnus-newsgroup-name))
1284
1285 \f
1286 ;;;
1287 ;;; Gnus summary mode
1288 ;;;
1289
1290 (put 'gnus-summary-mode 'mode-class 'special)
1291
1292 (when t
1293   ;; Non-orthogonal keys
1294
1295   (gnus-define-keys gnus-summary-mode-map
1296     " " gnus-summary-next-page
1297     "\177" gnus-summary-prev-page
1298     [delete] gnus-summary-prev-page
1299     [backspace] gnus-summary-prev-page
1300     "\r" gnus-summary-scroll-up
1301     "\M-\r" gnus-summary-scroll-down
1302     "n" gnus-summary-next-unread-article
1303     "p" gnus-summary-prev-unread-article
1304     "N" gnus-summary-next-article
1305     "P" gnus-summary-prev-article
1306     "\M-\C-n" gnus-summary-next-same-subject
1307     "\M-\C-p" gnus-summary-prev-same-subject
1308     "\M-n" gnus-summary-next-unread-subject
1309     "\M-p" gnus-summary-prev-unread-subject
1310     "." gnus-summary-first-unread-article
1311     "," gnus-summary-best-unread-article
1312     "\M-s" gnus-summary-search-article-forward
1313     "\M-r" gnus-summary-search-article-backward
1314     "<" gnus-summary-beginning-of-article
1315     ">" gnus-summary-end-of-article
1316     "j" gnus-summary-goto-article
1317     "^" gnus-summary-refer-parent-article
1318     "\M-^" gnus-summary-refer-article
1319     "u" gnus-summary-tick-article-forward
1320     "!" gnus-summary-tick-article-forward
1321     "U" gnus-summary-tick-article-backward
1322     "d" gnus-summary-mark-as-read-forward
1323     "D" gnus-summary-mark-as-read-backward
1324     "E" gnus-summary-mark-as-expirable
1325     "\M-u" gnus-summary-clear-mark-forward
1326     "\M-U" gnus-summary-clear-mark-backward
1327     "k" gnus-summary-kill-same-subject-and-select
1328     "\C-k" gnus-summary-kill-same-subject
1329     "\M-\C-k" gnus-summary-kill-thread
1330     "\M-\C-l" gnus-summary-lower-thread
1331     "e" gnus-summary-edit-article
1332     "#" gnus-summary-mark-as-processable
1333     "\M-#" gnus-summary-unmark-as-processable
1334     "\M-\C-t" gnus-summary-toggle-threads
1335     "\M-\C-s" gnus-summary-show-thread
1336     "\M-\C-h" gnus-summary-hide-thread
1337     "\M-\C-f" gnus-summary-next-thread
1338     "\M-\C-b" gnus-summary-prev-thread
1339     [(meta down)] gnus-summary-next-thread
1340     [(meta up)] gnus-summary-prev-thread
1341     "\M-\C-u" gnus-summary-up-thread
1342     "\M-\C-d" gnus-summary-down-thread
1343     "&" gnus-summary-execute-command
1344     "c" gnus-summary-catchup-and-exit
1345     "\C-w" gnus-summary-mark-region-as-read
1346     "\C-t" gnus-summary-toggle-truncation
1347     "?" gnus-summary-mark-as-dormant
1348     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1349     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1350     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1351     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1352     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1353     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1354     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1355     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1356     "=" gnus-summary-expand-window
1357     "\C-x\C-s" gnus-summary-reselect-current-group
1358     "\M-g" gnus-summary-rescan-group
1359     "w" gnus-summary-stop-page-breaking
1360     "\C-c\C-r" gnus-summary-caesar-message
1361     "f" gnus-summary-followup
1362     "F" gnus-summary-followup-with-original
1363     "C" gnus-summary-cancel-article
1364     "r" gnus-summary-reply
1365     "R" gnus-summary-reply-with-original
1366     "\C-c\C-f" gnus-summary-mail-forward
1367     "o" gnus-summary-save-article
1368     "\C-o" gnus-summary-save-article-mail
1369     "|" gnus-summary-pipe-output
1370     "\M-k" gnus-summary-edit-local-kill
1371     "\M-K" gnus-summary-edit-global-kill
1372     ;; "V" gnus-version
1373     "\C-c\C-d" gnus-summary-describe-group
1374     "q" gnus-summary-exit
1375     "Q" gnus-summary-exit-no-update
1376     "\C-c\C-i" gnus-info-find-node
1377     gnus-mouse-2 gnus-mouse-pick-article
1378     "m" gnus-summary-mail-other-window
1379     "a" gnus-summary-post-news
1380     "x" gnus-summary-limit-to-unread
1381     "s" gnus-summary-isearch-article
1382     "t" gnus-summary-toggle-header
1383     "g" gnus-summary-show-article
1384     "l" gnus-summary-goto-last-article
1385     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1386     "\C-d" gnus-summary-enter-digest-group
1387     "\M-\C-d" gnus-summary-read-document
1388     "\M-\C-e" gnus-summary-edit-parameters
1389     "\M-\C-a" gnus-summary-customize-parameters
1390     "\C-c\C-b" gnus-bug
1391     "*" gnus-cache-enter-article
1392     "\M-*" gnus-cache-remove-article
1393     "\M-&" gnus-summary-universal-argument
1394     "\C-l" gnus-recenter
1395     "I" gnus-summary-increase-score
1396     "L" gnus-summary-lower-score
1397     "\M-i" gnus-symbolic-argument
1398     "h" gnus-summary-select-article-buffer
1399
1400     "b" gnus-article-view-part
1401     "\M-t" gnus-summary-toggle-display-buttonized
1402
1403     "V" gnus-summary-score-map
1404     "X" gnus-uu-extract-map
1405     "S" gnus-summary-send-map)
1406
1407   ;; Sort of orthogonal keymap
1408   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1409     "t" gnus-summary-tick-article-forward
1410     "!" gnus-summary-tick-article-forward
1411     "d" gnus-summary-mark-as-read-forward
1412     "r" gnus-summary-mark-as-read-forward
1413     "c" gnus-summary-clear-mark-forward
1414     " " gnus-summary-clear-mark-forward
1415     "e" gnus-summary-mark-as-expirable
1416     "x" gnus-summary-mark-as-expirable
1417     "?" gnus-summary-mark-as-dormant
1418     "b" gnus-summary-set-bookmark
1419     "B" gnus-summary-remove-bookmark
1420     "#" gnus-summary-mark-as-processable
1421     "\M-#" gnus-summary-unmark-as-processable
1422     "S" gnus-summary-limit-include-expunged
1423     "C" gnus-summary-catchup
1424     "H" gnus-summary-catchup-to-here
1425     "\C-c" gnus-summary-catchup-all
1426     "k" gnus-summary-kill-same-subject-and-select
1427     "K" gnus-summary-kill-same-subject
1428     "P" gnus-uu-mark-map)
1429
1430   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1431     "c" gnus-summary-clear-above
1432     "u" gnus-summary-tick-above
1433     "m" gnus-summary-mark-above
1434     "k" gnus-summary-kill-below)
1435
1436   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1437     "/" gnus-summary-limit-to-subject
1438     "n" gnus-summary-limit-to-articles
1439     "w" gnus-summary-pop-limit
1440     "s" gnus-summary-limit-to-subject
1441     "a" gnus-summary-limit-to-author
1442     "u" gnus-summary-limit-to-unread
1443     "m" gnus-summary-limit-to-marks
1444     "M" gnus-summary-limit-exclude-marks
1445     "v" gnus-summary-limit-to-score
1446     "*" gnus-summary-limit-include-cached
1447     "D" gnus-summary-limit-include-dormant
1448     "T" gnus-summary-limit-include-thread
1449     "d" gnus-summary-limit-exclude-dormant
1450     "t" gnus-summary-limit-to-age
1451     "x" gnus-summary-limit-to-extra
1452     "E" gnus-summary-limit-include-expunged
1453     "c" gnus-summary-limit-exclude-childless-dormant
1454     "C" gnus-summary-limit-mark-excluded-as-read)
1455
1456   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1457     "n" gnus-summary-next-unread-article
1458     "p" gnus-summary-prev-unread-article
1459     "N" gnus-summary-next-article
1460     "P" gnus-summary-prev-article
1461     "\C-n" gnus-summary-next-same-subject
1462     "\C-p" gnus-summary-prev-same-subject
1463     "\M-n" gnus-summary-next-unread-subject
1464     "\M-p" gnus-summary-prev-unread-subject
1465     "f" gnus-summary-first-unread-article
1466     "b" gnus-summary-best-unread-article
1467     "j" gnus-summary-goto-article
1468     "g" gnus-summary-goto-subject
1469     "l" gnus-summary-goto-last-article
1470     "o" gnus-summary-pop-article)
1471
1472   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1473     "k" gnus-summary-kill-thread
1474     "l" gnus-summary-lower-thread
1475     "i" gnus-summary-raise-thread
1476     "T" gnus-summary-toggle-threads
1477     "t" gnus-summary-rethread-current
1478     "^" gnus-summary-reparent-thread
1479     "s" gnus-summary-show-thread
1480     "S" gnus-summary-show-all-threads
1481     "h" gnus-summary-hide-thread
1482     "H" gnus-summary-hide-all-threads
1483     "n" gnus-summary-next-thread
1484     "p" gnus-summary-prev-thread
1485     "u" gnus-summary-up-thread
1486     "o" gnus-summary-top-thread
1487     "d" gnus-summary-down-thread
1488     "#" gnus-uu-mark-thread
1489     "\M-#" gnus-uu-unmark-thread)
1490
1491   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1492     "g" gnus-summary-prepare
1493     "c" gnus-summary-insert-cached-articles)
1494
1495   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1496     "c" gnus-summary-catchup-and-exit
1497     "C" gnus-summary-catchup-all-and-exit
1498     "E" gnus-summary-exit-no-update
1499     "Q" gnus-summary-exit
1500     "Z" gnus-summary-exit
1501     "n" gnus-summary-catchup-and-goto-next-group
1502     "R" gnus-summary-reselect-current-group
1503     "G" gnus-summary-rescan-group
1504     "N" gnus-summary-next-group
1505     "s" gnus-summary-save-newsrc
1506     "P" gnus-summary-prev-group)
1507
1508   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1509     " " gnus-summary-next-page
1510     "n" gnus-summary-next-page
1511     "\177" gnus-summary-prev-page
1512     [delete] gnus-summary-prev-page
1513     "p" gnus-summary-prev-page
1514     "\r" gnus-summary-scroll-up
1515     "\M-\r" gnus-summary-scroll-down
1516     "<" gnus-summary-beginning-of-article
1517     ">" gnus-summary-end-of-article
1518     "b" gnus-summary-beginning-of-article
1519     "e" gnus-summary-end-of-article
1520     "^" gnus-summary-refer-parent-article
1521     "r" gnus-summary-refer-parent-article
1522     "D" gnus-summary-enter-digest-group
1523     "R" gnus-summary-refer-references
1524     "T" gnus-summary-refer-thread
1525     "g" gnus-summary-show-article
1526     "s" gnus-summary-isearch-article
1527     "P" gnus-summary-print-article
1528     "t" gnus-article-babel)
1529
1530   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1531     "b" gnus-article-add-buttons
1532     "B" gnus-article-add-buttons-to-head
1533     "o" gnus-article-treat-overstrike
1534     "e" gnus-article-emphasize
1535     "w" gnus-article-fill-cited-article
1536     "Q" gnus-article-fill-long-lines
1537     "C" gnus-article-capitalize-sentences
1538     "c" gnus-article-remove-cr
1539     "q" gnus-article-de-quoted-unreadable
1540     "6" gnus-article-de-base64-unreadable
1541     "Z" gnus-article-decode-HZ
1542     "h" gnus-article-wash-html
1543     "f" gnus-article-display-x-face
1544     "l" gnus-summary-stop-page-breaking
1545     "r" gnus-summary-caesar-message
1546     "t" gnus-summary-toggle-header
1547     "v" gnus-summary-verbose-headers
1548     "H" gnus-article-strip-headers-in-body
1549     "d" gnus-article-treat-dumbquotes)
1550
1551   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1552     "a" gnus-article-hide
1553     "h" gnus-article-hide-headers
1554     "b" gnus-article-hide-boring-headers
1555     "s" gnus-article-hide-signature
1556     "c" gnus-article-hide-citation
1557     "C" gnus-article-hide-citation-in-followups
1558     "l" gnus-article-hide-list-identifiers
1559     "p" gnus-article-hide-pgp
1560     "B" gnus-article-strip-banner
1561     "P" gnus-article-hide-pem
1562     "\C-c" gnus-article-hide-citation-maybe)
1563
1564   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1565     "a" gnus-article-highlight
1566     "h" gnus-article-highlight-headers
1567     "c" gnus-article-highlight-citation
1568     "s" gnus-article-highlight-signature)
1569
1570   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1571     "w" gnus-article-decode-mime-words
1572     "c" gnus-article-decode-charset
1573     "v" gnus-mime-view-all-parts
1574     "b" gnus-article-view-part)
1575
1576   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1577     "z" gnus-article-date-ut
1578     "u" gnus-article-date-ut
1579     "l" gnus-article-date-local
1580     "e" gnus-article-date-lapsed
1581     "o" gnus-article-date-original
1582     "i" gnus-article-date-iso8601
1583     "s" gnus-article-date-user)
1584
1585   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1586     "t" gnus-article-remove-trailing-blank-lines
1587     "l" gnus-article-strip-leading-blank-lines
1588     "m" gnus-article-strip-multiple-blank-lines
1589     "a" gnus-article-strip-blank-lines
1590     "A" gnus-article-strip-all-blank-lines
1591     "s" gnus-article-strip-leading-space
1592     "e" gnus-article-strip-trailing-space)
1593
1594   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1595     "v" gnus-version
1596     "f" gnus-summary-fetch-faq
1597     "d" gnus-summary-describe-group
1598     "h" gnus-summary-describe-briefly
1599     "i" gnus-info-find-node)
1600
1601   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1602     "e" gnus-summary-expire-articles
1603     "\M-\C-e" gnus-summary-expire-articles-now
1604     "\177" gnus-summary-delete-article
1605     [delete] gnus-summary-delete-article
1606     [backspace] gnus-summary-delete-article
1607     "m" gnus-summary-move-article
1608     "r" gnus-summary-respool-article
1609     "w" gnus-summary-edit-article
1610     "c" gnus-summary-copy-article
1611     "B" gnus-summary-crosspost-article
1612     "q" gnus-summary-respool-query
1613     "t" gnus-summary-respool-trace
1614     "i" gnus-summary-import-article
1615     "p" gnus-summary-article-posted-p)
1616
1617   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1618     "o" gnus-summary-save-article
1619     "m" gnus-summary-save-article-mail
1620     "F" gnus-summary-write-article-file
1621     "r" gnus-summary-save-article-rmail
1622     "f" gnus-summary-save-article-file
1623     "b" gnus-summary-save-article-body-file
1624     "h" gnus-summary-save-article-folder
1625     "v" gnus-summary-save-article-vm
1626     "p" gnus-summary-pipe-output
1627     "s" gnus-soup-add-article)
1628
1629   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1630     "b" gnus-summary-display-buttonized
1631     "m" gnus-summary-repair-multipart
1632     "v" gnus-article-view-part
1633     "o" gnus-article-save-part
1634     "c" gnus-article-copy-part
1635     "e" gnus-article-externalize-part
1636     "i" gnus-article-inline-part
1637     "|" gnus-article-pipe-part))
1638
1639 (defun gnus-summary-make-menu-bar ()
1640   (gnus-turn-off-edit-menu 'summary)
1641
1642   (unless (boundp 'gnus-summary-misc-menu)
1643
1644     (easy-menu-define
1645      gnus-summary-kill-menu gnus-summary-mode-map ""
1646      (cons
1647       "Score"
1648       (nconc
1649        (list
1650         ["Enter score..." gnus-summary-score-entry t]
1651         ["Customize" gnus-score-customize t])
1652        (gnus-make-score-map 'increase)
1653        (gnus-make-score-map 'lower)
1654        '(("Mark"
1655           ["Kill below" gnus-summary-kill-below t]
1656           ["Mark above" gnus-summary-mark-above t]
1657           ["Tick above" gnus-summary-tick-above t]
1658           ["Clear above" gnus-summary-clear-above t])
1659          ["Current score" gnus-summary-current-score t]
1660          ["Set score" gnus-summary-set-score t]
1661          ["Switch current score file..." gnus-score-change-score-file t]
1662          ["Set mark below..." gnus-score-set-mark-below t]
1663          ["Set expunge below..." gnus-score-set-expunge-below t]
1664          ["Edit current score file" gnus-score-edit-current-scores t]
1665          ["Edit score file" gnus-score-edit-file t]
1666          ["Trace score" gnus-score-find-trace t]
1667          ["Find words" gnus-score-find-favourite-words t]
1668          ["Rescore buffer" gnus-summary-rescore t]
1669          ["Increase score..." gnus-summary-increase-score t]
1670          ["Lower score..." gnus-summary-lower-score t]))))
1671
1672     ;; Define both the Article menu in the summary buffer and the equivalent
1673     ;; Commands menu in the article buffer here for consistency.
1674     (let ((innards
1675            '(("Hide"
1676               ["All" gnus-article-hide t]
1677               ["Headers" gnus-article-hide-headers t]
1678               ["Signature" gnus-article-hide-signature t]
1679               ["Citation" gnus-article-hide-citation t]
1680               ["List identifiers" gnus-article-hide-list-identifiers t]
1681               ["PGP" gnus-article-hide-pgp t]
1682               ["Banner" gnus-article-strip-banner t]
1683               ["Boring headers" gnus-article-hide-boring-headers t])
1684              ("Highlight"
1685               ["All" gnus-article-highlight t]
1686               ["Headers" gnus-article-highlight-headers t]
1687               ["Signature" gnus-article-highlight-signature t]
1688               ["Citation" gnus-article-highlight-citation t])
1689              ("MIME"
1690               ["Words" gnus-article-decode-mime-words t]
1691               ["Charset" gnus-article-decode-charset t]
1692               ["QP" gnus-article-de-quoted-unreadable t]
1693               ["Base64" gnus-article-de-base64-unreadable t]
1694               ["View all" gnus-mime-view-all-parts t])
1695              ("Date"
1696               ["Local" gnus-article-date-local t]
1697               ["ISO8601" gnus-article-date-iso8601 t]
1698               ["UT" gnus-article-date-ut t]
1699               ["Original" gnus-article-date-original t]
1700               ["Lapsed" gnus-article-date-lapsed t]
1701               ["User-defined" gnus-article-date-user t])
1702              ("Washing"
1703               ("Remove Blanks"
1704                ["Leading" gnus-article-strip-leading-blank-lines t]
1705                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1706                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1707                ["All of the above" gnus-article-strip-blank-lines t]
1708                ["All" gnus-article-strip-all-blank-lines t]
1709                ["Leading space" gnus-article-strip-leading-space t]
1710                ["Trailing space" gnus-article-strip-trailing-space t])
1711               ["Overstrike" gnus-article-treat-overstrike t]
1712               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1713               ["Emphasis" gnus-article-emphasize t]
1714               ["Word wrap" gnus-article-fill-cited-article t]
1715               ["Fill long lines" gnus-article-fill-long-lines t]
1716               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1717               ["CR" gnus-article-remove-cr t]
1718               ["Show X-Face" gnus-article-display-x-face t]
1719               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1720               ["Base64" gnus-article-de-base64-unreadable t]
1721               ["Rot 13" gnus-summary-caesar-message t]
1722               ["Unix pipe" gnus-summary-pipe-message t]
1723               ["Add buttons" gnus-article-add-buttons t]
1724               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1725               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1726               ["Verbose header" gnus-summary-verbose-headers t]
1727               ["Toggle header" gnus-summary-toggle-header t]
1728               ["Html" gnus-article-wash-html t]
1729               ["HZ" gnus-article-decode-HZ t])
1730              ("Output"
1731               ["Save in default format" gnus-summary-save-article t]
1732               ["Save in file" gnus-summary-save-article-file t]
1733               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1734               ["Save in MH folder" gnus-summary-save-article-folder t]
1735               ["Save in VM folder" gnus-summary-save-article-vm t]
1736               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1737               ["Save body in file" gnus-summary-save-article-body-file t]
1738               ["Pipe through a filter" gnus-summary-pipe-output t]
1739               ["Add to SOUP packet" gnus-soup-add-article t]
1740               ["Print" gnus-summary-print-article t])
1741              ("Backend"
1742               ["Respool article..." gnus-summary-respool-article t]
1743               ["Move article..." gnus-summary-move-article
1744                (gnus-check-backend-function
1745                 'request-move-article gnus-newsgroup-name)]
1746               ["Copy article..." gnus-summary-copy-article t]
1747               ["Crosspost article..." gnus-summary-crosspost-article
1748                (gnus-check-backend-function
1749                 'request-replace-article gnus-newsgroup-name)]
1750               ["Import file..." gnus-summary-import-article t]
1751               ["Check if posted" gnus-summary-article-posted-p t]
1752               ["Edit article" gnus-summary-edit-article
1753                (not (gnus-group-read-only-p))]
1754               ["Delete article" gnus-summary-delete-article
1755                (gnus-check-backend-function
1756                 'request-expire-articles gnus-newsgroup-name)]
1757               ["Query respool" gnus-summary-respool-query t]
1758               ["Trace respool" gnus-summary-respool-trace t]
1759               ["Delete expirable articles" gnus-summary-expire-articles-now
1760                (gnus-check-backend-function
1761                 'request-expire-articles gnus-newsgroup-name)])
1762              ("Extract"
1763               ["Uudecode" gnus-uu-decode-uu t]
1764               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1765               ["Unshar" gnus-uu-decode-unshar t]
1766               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1767               ["Save" gnus-uu-decode-save t]
1768               ["Binhex" gnus-uu-decode-binhex t]
1769               ["Postscript" gnus-uu-decode-postscript t])
1770              ("Cache"
1771               ["Enter article" gnus-cache-enter-article t]
1772               ["Remove article" gnus-cache-remove-article t])
1773              ["Translate" gnus-article-babel t]
1774              ["Select article buffer" gnus-summary-select-article-buffer t]
1775              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1776              ["Isearch article..." gnus-summary-isearch-article t]
1777              ["Beginning of the article" gnus-summary-beginning-of-article t]
1778              ["End of the article" gnus-summary-end-of-article t]
1779              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1780              ["Fetch referenced articles" gnus-summary-refer-references t]
1781              ["Fetch current thread" gnus-summary-refer-thread t]
1782              ["Fetch article with id..." gnus-summary-refer-article t]
1783              ["Redisplay" gnus-summary-show-article t])))
1784       (easy-menu-define
1785        gnus-summary-article-menu gnus-summary-mode-map ""
1786        (cons "Article" innards))
1787
1788       (easy-menu-define
1789        gnus-article-commands-menu gnus-article-mode-map ""
1790        (cons "Commands" innards)))
1791
1792     (easy-menu-define
1793      gnus-summary-thread-menu gnus-summary-mode-map ""
1794      '("Threads"
1795        ["Toggle threading" gnus-summary-toggle-threads t]
1796        ["Hide threads" gnus-summary-hide-all-threads t]
1797        ["Show threads" gnus-summary-show-all-threads t]
1798        ["Hide thread" gnus-summary-hide-thread t]
1799        ["Show thread" gnus-summary-show-thread t]
1800        ["Go to next thread" gnus-summary-next-thread t]
1801        ["Go to previous thread" gnus-summary-prev-thread t]
1802        ["Go down thread" gnus-summary-down-thread t]
1803        ["Go up thread" gnus-summary-up-thread t]
1804        ["Top of thread" gnus-summary-top-thread t]
1805        ["Mark thread as read" gnus-summary-kill-thread t]
1806        ["Lower thread score" gnus-summary-lower-thread t]
1807        ["Raise thread score" gnus-summary-raise-thread t]
1808        ["Rethread current" gnus-summary-rethread-current t]))
1809
1810     (easy-menu-define
1811      gnus-summary-post-menu gnus-summary-mode-map ""
1812      '("Post"
1813        ["Post an article" gnus-summary-post-news t]
1814        ["Followup" gnus-summary-followup t]
1815        ["Followup and yank" gnus-summary-followup-with-original t]
1816        ["Supersede article" gnus-summary-supersede-article t]
1817        ["Cancel article" gnus-summary-cancel-article t]
1818        ["Reply" gnus-summary-reply t]
1819        ["Reply and yank" gnus-summary-reply-with-original t]
1820        ["Wide reply" gnus-summary-wide-reply t]
1821        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1822        ["Mail forward" gnus-summary-mail-forward t]
1823        ["Post forward" gnus-summary-post-forward t]
1824        ["Digest and mail" gnus-uu-digest-mail-forward t]
1825        ["Digest and post" gnus-uu-digest-post-forward t]
1826        ["Resend message" gnus-summary-resend-message t]
1827        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1828        ["Send a mail" gnus-summary-mail-other-window t]
1829        ["Uuencode and post" gnus-uu-post-news t]
1830        ["Followup via news" gnus-summary-followup-to-mail t]
1831        ["Followup via news and yank"
1832         gnus-summary-followup-to-mail-with-original t]
1833        ;;("Draft"
1834        ;;["Send" gnus-summary-send-draft t]
1835        ;;["Send bounced" gnus-resend-bounced-mail t])
1836        ))
1837
1838     (easy-menu-define
1839      gnus-summary-misc-menu gnus-summary-mode-map ""
1840      '("Misc"
1841        ("Mark Read"
1842         ["Mark as read" gnus-summary-mark-as-read-forward t]
1843         ["Mark same subject and select"
1844          gnus-summary-kill-same-subject-and-select t]
1845         ["Mark same subject" gnus-summary-kill-same-subject t]
1846         ["Catchup" gnus-summary-catchup t]
1847         ["Catchup all" gnus-summary-catchup-all t]
1848         ["Catchup to here" gnus-summary-catchup-to-here t]
1849         ["Catchup region" gnus-summary-mark-region-as-read t]
1850         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1851        ("Mark Various"
1852         ["Tick" gnus-summary-tick-article-forward t]
1853         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1854         ["Remove marks" gnus-summary-clear-mark-forward t]
1855         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1856         ["Set bookmark" gnus-summary-set-bookmark t]
1857         ["Remove bookmark" gnus-summary-remove-bookmark t])
1858        ("Mark Limit"
1859         ["Marks..." gnus-summary-limit-to-marks t]
1860         ["Subject..." gnus-summary-limit-to-subject t]
1861         ["Author..." gnus-summary-limit-to-author t]
1862         ["Age..." gnus-summary-limit-to-age t]
1863         ["Extra..." gnus-summary-limit-to-extra t]
1864         ["Score" gnus-summary-limit-to-score t]
1865         ["Unread" gnus-summary-limit-to-unread t]
1866         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1867         ["Articles" gnus-summary-limit-to-articles t]
1868         ["Pop limit" gnus-summary-pop-limit t]
1869         ["Show dormant" gnus-summary-limit-include-dormant t]
1870         ["Hide childless dormant"
1871          gnus-summary-limit-exclude-childless-dormant t]
1872         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1873         ["Hide marked" gnus-summary-limit-exclude-marks t]
1874         ["Show expunged" gnus-summary-show-all-expunged t])
1875        ("Process Mark"
1876         ["Set mark" gnus-summary-mark-as-processable t]
1877         ["Remove mark" gnus-summary-unmark-as-processable t]
1878         ["Remove all marks" gnus-summary-unmark-all-processable t]
1879         ["Mark above" gnus-uu-mark-over t]
1880         ["Mark series" gnus-uu-mark-series t]
1881         ["Mark region" gnus-uu-mark-region t]
1882         ["Unmark region" gnus-uu-unmark-region t]
1883         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1884         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1885         ["Mark all" gnus-uu-mark-all t]
1886         ["Mark buffer" gnus-uu-mark-buffer t]
1887         ["Mark sparse" gnus-uu-mark-sparse t]
1888         ["Mark thread" gnus-uu-mark-thread t]
1889         ["Unmark thread" gnus-uu-unmark-thread t]
1890         ("Process Mark Sets"
1891          ["Kill" gnus-summary-kill-process-mark t]
1892          ["Yank" gnus-summary-yank-process-mark
1893           gnus-newsgroup-process-stack]
1894          ["Save" gnus-summary-save-process-mark t]))
1895        ("Scroll article"
1896         ["Page forward" gnus-summary-next-page t]
1897         ["Page backward" gnus-summary-prev-page t]
1898         ["Line forward" gnus-summary-scroll-up t])
1899        ("Move"
1900         ["Next unread article" gnus-summary-next-unread-article t]
1901         ["Previous unread article" gnus-summary-prev-unread-article t]
1902         ["Next article" gnus-summary-next-article t]
1903         ["Previous article" gnus-summary-prev-article t]
1904         ["Next unread subject" gnus-summary-next-unread-subject t]
1905         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1906         ["Next article same subject" gnus-summary-next-same-subject t]
1907         ["Previous article same subject" gnus-summary-prev-same-subject t]
1908         ["First unread article" gnus-summary-first-unread-article t]
1909         ["Best unread article" gnus-summary-best-unread-article t]
1910         ["Go to subject number..." gnus-summary-goto-subject t]
1911         ["Go to article number..." gnus-summary-goto-article t]
1912         ["Go to the last article" gnus-summary-goto-last-article t]
1913         ["Pop article off history" gnus-summary-pop-article t])
1914        ("Sort"
1915         ["Sort by number" gnus-summary-sort-by-number t]
1916         ["Sort by author" gnus-summary-sort-by-author t]
1917         ["Sort by subject" gnus-summary-sort-by-subject t]
1918         ["Sort by date" gnus-summary-sort-by-date t]
1919         ["Sort by score" gnus-summary-sort-by-score t]
1920         ["Sort by lines" gnus-summary-sort-by-lines t]
1921         ["Sort by characters" gnus-summary-sort-by-chars t])
1922        ("Help"
1923         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1924         ["Describe group" gnus-summary-describe-group t]
1925         ["Read manual" gnus-info-find-node t])
1926        ("Modes"
1927         ["Pick and read" gnus-pick-mode t]
1928         ["Binary" gnus-binary-mode t])
1929        ("Regeneration"
1930         ["Regenerate" gnus-summary-prepare t]
1931         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1932         ["Toggle threading" gnus-summary-toggle-threads t])
1933        ["Filter articles..." gnus-summary-execute-command t]
1934        ["Run command on subjects..." gnus-summary-universal-argument t]
1935        ["Search articles forward..." gnus-summary-search-article-forward t]
1936        ["Search articles backward..." gnus-summary-search-article-backward t]
1937        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1938        ["Expand window" gnus-summary-expand-window t]
1939        ["Expire expirable articles" gnus-summary-expire-articles
1940         (gnus-check-backend-function
1941          'request-expire-articles gnus-newsgroup-name)]
1942        ["Edit local kill file" gnus-summary-edit-local-kill t]
1943        ["Edit main kill file" gnus-summary-edit-global-kill t]
1944        ["Edit group parameters" gnus-summary-edit-parameters t]
1945        ["Customize group parameters" gnus-summary-customize-parameters t]
1946        ["Send a bug report" gnus-bug t]
1947        ("Exit"
1948         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1949         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1950         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1951         ["Exit group" gnus-summary-exit t]
1952         ["Exit group without updating" gnus-summary-exit-no-update t]
1953         ["Exit and goto next group" gnus-summary-next-group t]
1954         ["Exit and goto prev group" gnus-summary-prev-group t]
1955         ["Reselect group" gnus-summary-reselect-current-group t]
1956         ["Rescan group" gnus-summary-rescan-group t]
1957         ["Update dribble" gnus-summary-save-newsrc t])))
1958
1959     (gnus-run-hooks 'gnus-summary-menu-hook)))
1960
1961 (defun gnus-score-set-default (var value)
1962   "A version of set that updates the GNU Emacs menu-bar."
1963   (set var value)
1964   ;; It is the message that forces the active status to be updated.
1965   (message ""))
1966
1967 (defun gnus-make-score-map (type)
1968   "Make a summary score map of type TYPE."
1969   (if t
1970       nil
1971     (let ((headers '(("author" "from" string)
1972                      ("subject" "subject" string)
1973                      ("article body" "body" string)
1974                      ("article head" "head" string)
1975                      ("xref" "xref" string)
1976                      ("extra header" "extra" string)
1977                      ("lines" "lines" number)
1978                      ("followups to author" "followup" string)))
1979           (types '((number ("less than" <)
1980                            ("greater than" >)
1981                            ("equal" =))
1982                    (string ("substring" s)
1983                            ("exact string" e)
1984                            ("fuzzy string" f)
1985                            ("regexp" r))))
1986           (perms '(("temporary" (current-time-string))
1987                    ("permanent" nil)
1988                    ("immediate" now)))
1989           header)
1990       (list
1991        (apply
1992         'nconc
1993         (list
1994          (if (eq type 'lower)
1995              "Lower score"
1996            "Increase score"))
1997         (let (outh)
1998           (while headers
1999             (setq header (car headers))
2000             (setq outh
2001                   (cons
2002                    (apply
2003                     'nconc
2004                     (list (car header))
2005                     (let ((ts (cdr (assoc (nth 2 header) types)))
2006                           outt)
2007                       (while ts
2008                         (setq outt
2009                               (cons
2010                                (apply
2011                                 'nconc
2012                                 (list (caar ts))
2013                                 (let ((ps perms)
2014                                       outp)
2015                                   (while ps
2016                                     (setq outp
2017                                           (cons
2018                                            (vector
2019                                             (caar ps)
2020                                             (list
2021                                              'gnus-summary-score-entry
2022                                              (nth 1 header)
2023                                              (if (or (string= (nth 1 header)
2024                                                               "head")
2025                                                      (string= (nth 1 header)
2026                                                               "body"))
2027                                                  ""
2028                                                (list 'gnus-summary-header
2029                                                      (nth 1 header)))
2030                                              (list 'quote (nth 1 (car ts)))
2031                                              (list 'gnus-score-delta-default
2032                                                    nil)
2033                                              (nth 1 (car ps))
2034                                              t)
2035                                             t)
2036                                            outp))
2037                                     (setq ps (cdr ps)))
2038                                   (list (nreverse outp))))
2039                                outt))
2040                         (setq ts (cdr ts)))
2041                       (list (nreverse outt))))
2042                    outh))
2043             (setq headers (cdr headers)))
2044           (list (nreverse outh))))))))
2045
2046 \f
2047
2048 (defun gnus-summary-mode (&optional group)
2049   "Major mode for reading articles.
2050
2051 All normal editing commands are switched off.
2052 \\<gnus-summary-mode-map>
2053 Each line in this buffer represents one article.  To read an
2054 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2055 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2056 respectively.
2057
2058 You can also post articles and send mail from this buffer.  To
2059 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2060 of an article, type `\\[gnus-summary-reply]'.
2061
2062 There are approx. one gazillion commands you can execute in this
2063 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2064
2065 The following commands are available:
2066
2067 \\{gnus-summary-mode-map}"
2068   (interactive)
2069   (when (gnus-visual-p 'summary-menu 'menu)
2070     (gnus-summary-make-menu-bar))
2071   (kill-all-local-variables)
2072   (gnus-summary-make-local-variables)
2073   (gnus-make-thread-indent-array)
2074   (gnus-simplify-mode-line)
2075   (setq major-mode 'gnus-summary-mode)
2076   (setq mode-name "Summary")
2077   (make-local-variable 'minor-mode-alist)
2078   (use-local-map gnus-summary-mode-map)
2079   (buffer-disable-undo)
2080   (setq buffer-read-only t)             ;Disable modification
2081   (setq truncate-lines t)
2082   (setq selective-display t)
2083   (setq selective-display-ellipses t)   ;Display `...'
2084   (gnus-summary-set-display-table)
2085   (gnus-set-default-directory)
2086   (setq gnus-newsgroup-name group)
2087   (make-local-variable 'gnus-summary-line-format)
2088   (make-local-variable 'gnus-summary-line-format-spec)
2089   (make-local-variable 'gnus-summary-dummy-line-format)
2090   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2091   (make-local-variable 'gnus-summary-mark-positions)
2092   (make-local-hook 'pre-command-hook)
2093   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2094   (gnus-run-hooks 'gnus-summary-mode-hook)
2095   (mm-enable-multibyte)
2096   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2097   (gnus-update-summary-mark-positions))
2098
2099 (defun gnus-summary-make-local-variables ()
2100   "Make all the local summary buffer variables."
2101   (let (global)
2102     (dolist (local gnus-summary-local-variables)
2103       (if (consp local)
2104           (progn
2105             (if (eq (cdr local) 'global)
2106                 ;; Copy the global value of the variable.
2107                 (setq global (symbol-value (car local)))
2108               ;; Use the value from the list.
2109               (setq global (eval (cdr local))))
2110             (set (make-local-variable (car local)) global))
2111         ;; Simple nil-valued local variable.
2112         (set (make-local-variable local) nil)))))
2113
2114 (defun gnus-summary-clear-local-variables ()
2115   (let ((locals gnus-summary-local-variables))
2116     (while locals
2117       (if (consp (car locals))
2118           (and (vectorp (caar locals))
2119                (set (caar locals) nil))
2120         (and (vectorp (car locals))
2121              (set (car locals) nil)))
2122       (setq locals (cdr locals)))))
2123
2124 ;; Summary data functions.
2125
2126 (defmacro gnus-data-number (data)
2127   `(car ,data))
2128
2129 (defmacro gnus-data-set-number (data number)
2130   `(setcar ,data ,number))
2131
2132 (defmacro gnus-data-mark (data)
2133   `(nth 1 ,data))
2134
2135 (defmacro gnus-data-set-mark (data mark)
2136   `(setcar (nthcdr 1 ,data) ,mark))
2137
2138 (defmacro gnus-data-pos (data)
2139   `(nth 2 ,data))
2140
2141 (defmacro gnus-data-set-pos (data pos)
2142   `(setcar (nthcdr 2 ,data) ,pos))
2143
2144 (defmacro gnus-data-header (data)
2145   `(nth 3 ,data))
2146
2147 (defmacro gnus-data-set-header (data header)
2148   `(setf (nth 3 ,data) ,header))
2149
2150 (defmacro gnus-data-level (data)
2151   `(nth 4 ,data))
2152
2153 (defmacro gnus-data-unread-p (data)
2154   `(= (nth 1 ,data) gnus-unread-mark))
2155
2156 (defmacro gnus-data-read-p (data)
2157   `(/= (nth 1 ,data) gnus-unread-mark))
2158
2159 (defmacro gnus-data-pseudo-p (data)
2160   `(consp (nth 3 ,data)))
2161
2162 (defmacro gnus-data-find (number)
2163   `(assq ,number gnus-newsgroup-data))
2164
2165 (defmacro gnus-data-find-list (number &optional data)
2166   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2167      (memq (assq ,number bdata)
2168            bdata)))
2169
2170 (defmacro gnus-data-make (number mark pos header level)
2171   `(list ,number ,mark ,pos ,header ,level))
2172
2173 (defun gnus-data-enter (after-article number mark pos header level offset)
2174   (let ((data (gnus-data-find-list after-article)))
2175     (unless data
2176       (error "No such article: %d" after-article))
2177     (setcdr data (cons (gnus-data-make number mark pos header level)
2178                        (cdr data)))
2179     (setq gnus-newsgroup-data-reverse nil)
2180     (gnus-data-update-list (cddr data) offset)))
2181
2182 (defun gnus-data-enter-list (after-article list &optional offset)
2183   (when list
2184     (let ((data (and after-article (gnus-data-find-list after-article)))
2185           (ilist list))
2186       (if (not (or data
2187                    after-article))
2188           (let ((odata gnus-newsgroup-data))
2189             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2190             (when offset
2191               (gnus-data-update-list odata offset)))
2192         ;; Find the last element in the list to be spliced into the main
2193         ;; list.
2194         (while (cdr list)
2195           (setq list (cdr list)))
2196         (if (not data)
2197             (progn
2198               (setcdr list gnus-newsgroup-data)
2199               (setq gnus-newsgroup-data ilist)
2200               (when offset
2201                 (gnus-data-update-list (cdr list) offset)))
2202           (setcdr list (cdr data))
2203           (setcdr data ilist)
2204           (when offset
2205             (gnus-data-update-list (cdr list) offset))))
2206       (setq gnus-newsgroup-data-reverse nil))))
2207
2208 (defun gnus-data-remove (article &optional offset)
2209   (let ((data gnus-newsgroup-data))
2210     (if (= (gnus-data-number (car data)) article)
2211         (progn
2212           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2213                 gnus-newsgroup-data-reverse nil)
2214           (when offset
2215             (gnus-data-update-list gnus-newsgroup-data offset)))
2216       (while (cdr data)
2217         (when (= (gnus-data-number (cadr data)) article)
2218           (setcdr data (cddr data))
2219           (when offset
2220             (gnus-data-update-list (cdr data) offset))
2221           (setq data nil
2222                 gnus-newsgroup-data-reverse nil))
2223         (setq data (cdr data))))))
2224
2225 (defmacro gnus-data-list (backward)
2226   `(if ,backward
2227        (or gnus-newsgroup-data-reverse
2228            (setq gnus-newsgroup-data-reverse
2229                  (reverse gnus-newsgroup-data)))
2230      gnus-newsgroup-data))
2231
2232 (defun gnus-data-update-list (data offset)
2233   "Add OFFSET to the POS of all data entries in DATA."
2234   (setq gnus-newsgroup-data-reverse nil)
2235   (while data
2236     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2237     (setq data (cdr data))))
2238
2239 (defun gnus-summary-article-pseudo-p (article)
2240   "Say whether this article is a pseudo article or not."
2241   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2242
2243 (defmacro gnus-summary-article-sparse-p (article)
2244   "Say whether this article is a sparse article or not."
2245   `(memq ,article gnus-newsgroup-sparse))
2246
2247 (defmacro gnus-summary-article-ancient-p (article)
2248   "Say whether this article is a sparse article or not."
2249   `(memq ,article gnus-newsgroup-ancient))
2250
2251 (defun gnus-article-parent-p (number)
2252   "Say whether this article is a parent or not."
2253   (let ((data (gnus-data-find-list number)))
2254     (and (cdr data)                     ; There has to be an article after...
2255          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2256             (gnus-data-level (nth 1 data))))))
2257
2258 (defun gnus-article-children (number)
2259   "Return a list of all children to NUMBER."
2260   (let* ((data (gnus-data-find-list number))
2261          (level (gnus-data-level (car data)))
2262          children)
2263     (setq data (cdr data))
2264     (while (and data
2265                 (= (gnus-data-level (car data)) (1+ level)))
2266       (push (gnus-data-number (car data)) children)
2267       (setq data (cdr data)))
2268     children))
2269
2270 (defmacro gnus-summary-skip-intangible ()
2271   "If the current article is intangible, then jump to a different article."
2272   '(let ((to (get-text-property (point) 'gnus-intangible)))
2273      (and to (gnus-summary-goto-subject to))))
2274
2275 (defmacro gnus-summary-article-intangible-p ()
2276   "Say whether this article is intangible or not."
2277   '(get-text-property (point) 'gnus-intangible))
2278
2279 (defun gnus-article-read-p (article)
2280   "Say whether ARTICLE is read or not."
2281   (not (or (memq article gnus-newsgroup-marked)
2282            (memq article gnus-newsgroup-unreads)
2283            (memq article gnus-newsgroup-unselected)
2284            (memq article gnus-newsgroup-dormant))))
2285
2286 ;; Some summary mode macros.
2287
2288 (defmacro gnus-summary-article-number ()
2289   "The article number of the article on the current line.
2290 If there isn's an article number here, then we return the current
2291 article number."
2292   '(progn
2293      (gnus-summary-skip-intangible)
2294      (or (get-text-property (point) 'gnus-number)
2295          (gnus-summary-last-subject))))
2296
2297 (defmacro gnus-summary-article-header (&optional number)
2298   "Return the header of article NUMBER."
2299   `(gnus-data-header (gnus-data-find
2300                       ,(or number '(gnus-summary-article-number)))))
2301
2302 (defmacro gnus-summary-thread-level (&optional number)
2303   "Return the level of thread that starts with article NUMBER."
2304   `(if (and (eq gnus-summary-make-false-root 'dummy)
2305             (get-text-property (point) 'gnus-intangible))
2306        0
2307      (gnus-data-level (gnus-data-find
2308                        ,(or number '(gnus-summary-article-number))))))
2309
2310 (defmacro gnus-summary-article-mark (&optional number)
2311   "Return the mark of article NUMBER."
2312   `(gnus-data-mark (gnus-data-find
2313                     ,(or number '(gnus-summary-article-number)))))
2314
2315 (defmacro gnus-summary-article-pos (&optional number)
2316   "Return the position of the line of article NUMBER."
2317   `(gnus-data-pos (gnus-data-find
2318                    ,(or number '(gnus-summary-article-number)))))
2319
2320 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2321 (defmacro gnus-summary-article-subject (&optional number)
2322   "Return current subject string or nil if nothing."
2323   `(let ((headers
2324           ,(if number
2325                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2326              '(gnus-data-header (assq (gnus-summary-article-number)
2327                                       gnus-newsgroup-data)))))
2328      (and headers
2329           (vectorp headers)
2330           (mail-header-subject headers))))
2331
2332 (defmacro gnus-summary-article-score (&optional number)
2333   "Return current article score."
2334   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2335                   gnus-newsgroup-scored))
2336        gnus-summary-default-score 0))
2337
2338 (defun gnus-summary-article-children (&optional number)
2339   "Return a list of article numbers that are children of article NUMBER."
2340   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2341          (level (gnus-data-level (car data)))
2342          l children)
2343     (while (and (setq data (cdr data))
2344                 (> (setq l (gnus-data-level (car data))) level))
2345       (and (= (1+ level) l)
2346            (push (gnus-data-number (car data))
2347                  children)))
2348     (nreverse children)))
2349
2350 (defun gnus-summary-article-parent (&optional number)
2351   "Return the article number of the parent of article NUMBER."
2352   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2353                                     (gnus-data-list t)))
2354          (level (gnus-data-level (car data))))
2355     (if (zerop level)
2356         ()                              ; This is a root.
2357       ;; We search until we find an article with a level less than
2358       ;; this one.  That function has to be the parent.
2359       (while (and (setq data (cdr data))
2360                   (not (< (gnus-data-level (car data)) level))))
2361       (and data (gnus-data-number (car data))))))
2362
2363 (defun gnus-unread-mark-p (mark)
2364   "Say whether MARK is the unread mark."
2365   (= mark gnus-unread-mark))
2366
2367 (defun gnus-read-mark-p (mark)
2368   "Say whether MARK is one of the marks that mark as read.
2369 This is all marks except unread, ticked, dormant, and expirable."
2370   (not (or (= mark gnus-unread-mark)
2371            (= mark gnus-ticked-mark)
2372            (= mark gnus-dormant-mark)
2373            (= mark gnus-expirable-mark))))
2374
2375 (defmacro gnus-article-mark (number)
2376   "Return the MARK of article NUMBER.
2377 This macro should only be used when computing the mark the \"first\"
2378 time; i.e., when generating the summary lines.  After that,
2379 `gnus-summary-article-mark' should be used to examine the
2380 marks of articles."
2381   `(cond
2382     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2383     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2384     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2385     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2386     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2387     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2388     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2389     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2390            gnus-ancient-mark))))
2391
2392 ;; Saving hidden threads.
2393
2394 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2395 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2396
2397 (defmacro gnus-save-hidden-threads (&rest forms)
2398   "Save hidden threads, eval FORMS, and restore the hidden threads."
2399   (let ((config (make-symbol "config")))
2400     `(let ((,config (gnus-hidden-threads-configuration)))
2401        (unwind-protect
2402            (save-excursion
2403              ,@forms)
2404          (gnus-restore-hidden-threads-configuration ,config)))))
2405
2406 (defun gnus-data-compute-positions ()
2407   "Compute the positions of all articles."
2408   (setq gnus-newsgroup-data-reverse nil)
2409   (let ((data gnus-newsgroup-data))
2410     (save-excursion
2411       (gnus-save-hidden-threads
2412         (gnus-summary-show-all-threads)
2413         (goto-char (point-min))
2414         (while data
2415           (while (get-text-property (point) 'gnus-intangible)
2416             (forward-line 1))
2417           (gnus-data-set-pos (car data) (+ (point) 3))
2418           (setq data (cdr data))
2419           (forward-line 1))))))
2420
2421 (defun gnus-hidden-threads-configuration ()
2422   "Return the current hidden threads configuration."
2423   (save-excursion
2424     (let (config)
2425       (goto-char (point-min))
2426       (while (search-forward "\r" nil t)
2427         (push (1- (point)) config))
2428       config)))
2429
2430 (defun gnus-restore-hidden-threads-configuration (config)
2431   "Restore hidden threads configuration from CONFIG."
2432   (save-excursion
2433     (let (point buffer-read-only)
2434       (while (setq point (pop config))
2435         (when (and (< point (point-max))
2436                    (goto-char point)
2437                    (eq (char-after) ?\n))
2438           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2439
2440 ;; Various summary mode internalish functions.
2441
2442 (defun gnus-mouse-pick-article (e)
2443   (interactive "e")
2444   (mouse-set-point e)
2445   (gnus-summary-next-page nil t))
2446
2447 (defun gnus-summary-set-display-table ()
2448   "Change the display table.
2449 Odd characters have a tendency to mess
2450 up nicely formatted displays - we make all possible glyphs
2451 display only a single character."
2452
2453   ;; We start from the standard display table, if any.
2454   (let ((table (or (copy-sequence standard-display-table)
2455                    (make-display-table)))
2456         (i 32))
2457     ;; Nix out all the control chars...
2458     (while (>= (setq i (1- i)) 0)
2459       (aset table i [??]))
2460     ;; ... but not newline and cr, of course.  (cr is necessary for the
2461     ;; selective display).
2462     (aset table ?\n nil)
2463     (aset table ?\r nil)
2464     ;; We keep TAB as well.
2465     (aset table ?\t nil)
2466     ;; We nix out any glyphs over 126 that are not set already.
2467     (let ((i 256))
2468       (while (>= (setq i (1- i)) 127)
2469         ;; Only modify if the entry is nil.
2470         (unless (aref table i)
2471           (aset table i [??]))))
2472     (setq buffer-display-table table)))
2473
2474 (defun gnus-summary-setup-buffer (group)
2475   "Initialize summary buffer."
2476   (let ((buffer (concat "*Summary " group "*")))
2477     (if (get-buffer buffer)
2478         (progn
2479           (set-buffer buffer)
2480           (setq gnus-summary-buffer (current-buffer))
2481           (not gnus-newsgroup-prepared))
2482       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2483       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2484       (gnus-summary-mode group)
2485       (when gnus-carpal
2486         (gnus-carpal-setup-buffer 'summary))
2487       (unless gnus-single-article-buffer
2488         (make-local-variable 'gnus-article-buffer)
2489         (make-local-variable 'gnus-article-current)
2490         (make-local-variable 'gnus-original-article-buffer))
2491       (setq gnus-newsgroup-name group)
2492       t)))
2493
2494 (defun gnus-set-global-variables ()
2495   "Set the global equivalents of the buffer-local variables.
2496 They are set to the latest values they had.  These reflect the summary
2497 buffer that was in action when the last article was fetched."
2498   (when (eq major-mode 'gnus-summary-mode)
2499     (setq gnus-summary-buffer (current-buffer))
2500     (let ((name gnus-newsgroup-name)
2501           (marked gnus-newsgroup-marked)
2502           (unread gnus-newsgroup-unreads)
2503           (headers gnus-current-headers)
2504           (data gnus-newsgroup-data)
2505           (summary gnus-summary-buffer)
2506           (article-buffer gnus-article-buffer)
2507           (original gnus-original-article-buffer)
2508           (gac gnus-article-current)
2509           (reffed gnus-reffed-article-number)
2510           (score-file gnus-current-score-file)
2511           (default-charset gnus-newsgroup-charset))
2512       (save-excursion
2513         (set-buffer gnus-group-buffer)
2514         (setq gnus-newsgroup-name name
2515               gnus-newsgroup-marked marked
2516               gnus-newsgroup-unreads unread
2517               gnus-current-headers headers
2518               gnus-newsgroup-data data
2519               gnus-article-current gac
2520               gnus-summary-buffer summary
2521               gnus-article-buffer article-buffer
2522               gnus-original-article-buffer original
2523               gnus-reffed-article-number reffed
2524               gnus-current-score-file score-file
2525               gnus-newsgroup-charset default-charset)
2526         ;; The article buffer also has local variables.
2527         (when (gnus-buffer-live-p gnus-article-buffer)
2528           (set-buffer gnus-article-buffer)
2529           (setq gnus-summary-buffer summary))))))
2530
2531 (defun gnus-summary-article-unread-p (article)
2532   "Say whether ARTICLE is unread or not."
2533   (memq article gnus-newsgroup-unreads))
2534
2535 (defun gnus-summary-first-article-p (&optional article)
2536   "Return whether ARTICLE is the first article in the buffer."
2537   (if (not (setq article (or article (gnus-summary-article-number))))
2538       nil
2539     (eq article (caar gnus-newsgroup-data))))
2540
2541 (defun gnus-summary-last-article-p (&optional article)
2542   "Return whether ARTICLE is the last article in the buffer."
2543   (if (not (setq article (or article (gnus-summary-article-number))))
2544       ;; All non-existent numbers are the last article.  :-)
2545       t
2546     (not (cdr (gnus-data-find-list article)))))
2547
2548 (defun gnus-make-thread-indent-array ()
2549   (let ((n 200))
2550     (unless (and gnus-thread-indent-array
2551                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2552       (setq gnus-thread-indent-array (make-vector 201 "")
2553             gnus-thread-indent-array-level gnus-thread-indent-level)
2554       (while (>= n 0)
2555         (aset gnus-thread-indent-array n
2556               (make-string (* n gnus-thread-indent-level) ? ))
2557         (setq n (1- n))))))
2558
2559 (defun gnus-update-summary-mark-positions ()
2560   "Compute where the summary marks are to go."
2561   (save-excursion
2562     (when (gnus-buffer-exists-p gnus-summary-buffer)
2563       (set-buffer gnus-summary-buffer))
2564     (let ((gnus-replied-mark 129)
2565           (gnus-score-below-mark 130)
2566           (gnus-score-over-mark 130)
2567           (gnus-download-mark 131)
2568           (spec gnus-summary-line-format-spec)
2569           gnus-visual pos)
2570       (save-excursion
2571         (gnus-set-work-buffer)
2572         (let ((gnus-summary-line-format-spec spec)
2573               (gnus-newsgroup-downloadable '((0 . t))))
2574           (gnus-summary-insert-line
2575            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2576           (goto-char (point-min))
2577           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2578                                              (- (point) 2)))))
2579           (goto-char (point-min))
2580           (push (cons 'replied (and (search-forward "\201" nil t)
2581                                     (- (point) 2)))
2582                 pos)
2583           (goto-char (point-min))
2584           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2585                 pos)
2586           (goto-char (point-min))
2587           (push (cons 'download
2588                       (and (search-forward "\203" nil t) (- (point) 2)))
2589                 pos)))
2590       (setq gnus-summary-mark-positions pos))))
2591
2592 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2593   "Insert a dummy root in the summary buffer."
2594   (beginning-of-line)
2595   (gnus-add-text-properties
2596    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2597    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2598
2599 (defun gnus-summary-from-or-to-or-newsgroups (header)
2600   (let ((to (cdr (assq 'To (mail-header-extra header))))
2601         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2602         (mail-parse-charset gnus-newsgroup-charset)
2603         (mail-parse-ignored-charsets
2604          (save-excursion (set-buffer gnus-summary-buffer)
2605                          gnus-newsgroup-ignored-charsets)))
2606     (cond
2607      ((and to
2608            gnus-ignored-from-addresses
2609            (string-match gnus-ignored-from-addresses
2610                          (mail-header-from header)))
2611       (concat "-> "
2612               (or (car (funcall gnus-extract-address-components
2613                                 (funcall
2614                                  gnus-decode-encoded-word-function to)))
2615                   (funcall gnus-decode-encoded-word-function to))))
2616      ((and newsgroups
2617            gnus-ignored-from-addresses
2618            (string-match gnus-ignored-from-addresses
2619                          (mail-header-from header)))
2620       (concat "=> " newsgroups))
2621      (t
2622       (or (car (funcall gnus-extract-address-components
2623                         (mail-header-from header)))
2624           (mail-header-from header))))))
2625
2626 (defun gnus-summary-insert-line (gnus-tmp-header
2627                                  gnus-tmp-level gnus-tmp-current
2628                                  gnus-tmp-unread gnus-tmp-replied
2629                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2630                                  &optional gnus-tmp-dummy gnus-tmp-score
2631                                  gnus-tmp-process)
2632   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2633          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2634          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2635          (gnus-tmp-score-char
2636           (if (or (null gnus-summary-default-score)
2637                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2638                       gnus-summary-zcore-fuzz))
2639               ?  ;Whitespace
2640             (if (< gnus-tmp-score gnus-summary-default-score)
2641                 gnus-score-below-mark gnus-score-over-mark)))
2642          (gnus-tmp-replied
2643           (cond (gnus-tmp-process gnus-process-mark)
2644                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2645                  gnus-cached-mark)
2646                 (gnus-tmp-replied gnus-replied-mark)
2647                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2648                  gnus-saved-mark)
2649                 (t gnus-unread-mark)))
2650          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2651          (gnus-tmp-name
2652           (cond
2653            ((string-match "<[^>]+> *$" gnus-tmp-from)
2654             (let ((beg (match-beginning 0)))
2655               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2656                        (substring gnus-tmp-from (1+ (match-beginning 0))
2657                                   (1- (match-end 0))))
2658                   (substring gnus-tmp-from 0 beg))))
2659            ((string-match "(.+)" gnus-tmp-from)
2660             (substring gnus-tmp-from
2661                        (1+ (match-beginning 0)) (1- (match-end 0))))
2662            (t gnus-tmp-from)))
2663          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2664          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2665          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2666          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2667          (buffer-read-only nil))
2668     (when (string= gnus-tmp-name "")
2669       (setq gnus-tmp-name gnus-tmp-from))
2670     (unless (numberp gnus-tmp-lines)
2671       (setq gnus-tmp-lines 0))
2672     (gnus-put-text-property
2673      (point)
2674      (progn (eval gnus-summary-line-format-spec) (point))
2675      'gnus-number gnus-tmp-number)
2676     (when (gnus-visual-p 'summary-highlight 'highlight)
2677       (forward-line -1)
2678       (gnus-run-hooks 'gnus-summary-update-hook)
2679       (forward-line 1))))
2680
2681 (defun gnus-summary-update-line (&optional dont-update)
2682   "Update summary line after change."
2683   (when (and gnus-summary-default-score
2684              (not gnus-summary-inhibit-highlight))
2685     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2686            (article (gnus-summary-article-number))
2687            (score (gnus-summary-article-score article)))
2688       (unless dont-update
2689         (if (and gnus-summary-mark-below
2690                  (< (gnus-summary-article-score)
2691                     gnus-summary-mark-below))
2692             ;; This article has a low score, so we mark it as read.
2693             (when (memq article gnus-newsgroup-unreads)
2694               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2695           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2696             ;; This article was previously marked as read on account
2697             ;; of a low score, but now it has risen, so we mark it as
2698             ;; unread.
2699             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2700         (gnus-summary-update-mark
2701          (if (or (null gnus-summary-default-score)
2702                  (<= (abs (- score gnus-summary-default-score))
2703                      gnus-summary-zcore-fuzz))
2704              ?  ;Whitespace
2705            (if (< score gnus-summary-default-score)
2706                gnus-score-below-mark gnus-score-over-mark))
2707          'score))
2708       ;; Do visual highlighting.
2709       (when (gnus-visual-p 'summary-highlight 'highlight)
2710         (gnus-run-hooks 'gnus-summary-update-hook)))))
2711
2712 (defvar gnus-tmp-new-adopts nil)
2713
2714 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2715   "Return the number of articles in THREAD.
2716 This may be 0 in some cases -- if none of the articles in
2717 the thread are to be displayed."
2718   (let* ((number
2719           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2720           (cond
2721            ((not (listp thread))
2722             1)
2723            ((and (consp thread) (cdr thread))
2724             (apply
2725              '+ 1 (mapcar
2726                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2727            ((null thread)
2728             1)
2729            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2730             1)
2731            (t 0))))
2732     (when (and level (zerop level) gnus-tmp-new-adopts)
2733       (incf number
2734             (apply '+ (mapcar
2735                        'gnus-summary-number-of-articles-in-thread
2736                        gnus-tmp-new-adopts))))
2737     (if char
2738         (if (> number 1) gnus-not-empty-thread-mark
2739           gnus-empty-thread-mark)
2740       number)))
2741
2742 (defun gnus-summary-set-local-parameters (group)
2743   "Go through the local params of GROUP and set all variable specs in that list."
2744   (let ((params (gnus-group-find-parameter group))
2745         elem)
2746     (while params
2747       (setq elem (car params)
2748             params (cdr params))
2749       (and (consp elem)                 ; Has to be a cons.
2750            (consp (cdr elem))           ; The cdr has to be a list.
2751            (symbolp (car elem))         ; Has to be a symbol in there.
2752            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2753            (ignore-errors               ; So we set it.
2754              (make-local-variable (car elem))
2755              (set (car elem) (eval (nth 1 elem))))))))
2756
2757 (defun gnus-summary-read-group (group &optional show-all no-article
2758                                       kill-buffer no-display backward
2759                                       select-articles)
2760   "Start reading news in newsgroup GROUP.
2761 If SHOW-ALL is non-nil, already read articles are also listed.
2762 If NO-ARTICLE is non-nil, no article is selected initially.
2763 If NO-DISPLAY, don't generate a summary buffer."
2764   (let (result)
2765     (while (and group
2766                 (null (setq result
2767                             (let ((gnus-auto-select-next nil))
2768                               (or (gnus-summary-read-group-1
2769                                    group show-all no-article
2770                                    kill-buffer no-display
2771                                    select-articles)
2772                                   (setq show-all nil
2773                                         select-articles nil)))))
2774                 (eq gnus-auto-select-next 'quietly))
2775       (set-buffer gnus-group-buffer)
2776       ;; The entry function called above goes to the next
2777       ;; group automatically, so we go two groups back
2778       ;; if we are searching for the previous group.
2779       (when backward
2780         (gnus-group-prev-unread-group 2))
2781       (if (not (equal group (gnus-group-group-name)))
2782           (setq group (gnus-group-group-name))
2783         (setq group nil)))
2784     result))
2785
2786 (defun gnus-summary-read-group-1 (group show-all no-article
2787                                         kill-buffer no-display
2788                                         &optional select-articles)
2789   ;; Killed foreign groups can't be entered.
2790   (when (and (not (gnus-group-native-p group))
2791              (not (gnus-gethash group gnus-newsrc-hashtb)))
2792     (error "Dead non-native groups can't be entered"))
2793   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2794   (let* ((new-group (gnus-summary-setup-buffer group))
2795          (quit-config (gnus-group-quit-config group))
2796          (did-select (and new-group (gnus-select-newsgroup
2797                                      group show-all select-articles))))
2798     (cond
2799      ;; This summary buffer exists already, so we just select it.
2800      ((not new-group)
2801       (gnus-set-global-variables)
2802       (when kill-buffer
2803         (gnus-kill-or-deaden-summary kill-buffer))
2804       (gnus-configure-windows 'summary 'force)
2805       (gnus-set-mode-line 'summary)
2806       (gnus-summary-position-point)
2807       (message "")
2808       t)
2809      ;; We couldn't select this group.
2810      ((null did-select)
2811       (when (and (eq major-mode 'gnus-summary-mode)
2812                  (not (equal (current-buffer) kill-buffer)))
2813         (kill-buffer (current-buffer))
2814         (if (not quit-config)
2815             (progn
2816               ;; Update the info -- marks might need to be removed,
2817               ;; for instance.
2818               (gnus-summary-update-info)
2819               (set-buffer gnus-group-buffer)
2820               (gnus-group-jump-to-group group)
2821               (gnus-group-next-unread-group 1))
2822           (gnus-handle-ephemeral-exit quit-config)))
2823       (gnus-message 3 "Can't select group")
2824       nil)
2825      ;; The user did a `C-g' while prompting for number of articles,
2826      ;; so we exit this group.
2827      ((eq did-select 'quit)
2828       (and (eq major-mode 'gnus-summary-mode)
2829            (not (equal (current-buffer) kill-buffer))
2830            (kill-buffer (current-buffer)))
2831       (when kill-buffer
2832         (gnus-kill-or-deaden-summary kill-buffer))
2833       (if (not quit-config)
2834           (progn
2835             (set-buffer gnus-group-buffer)
2836             (gnus-group-jump-to-group group)
2837             (gnus-group-next-unread-group 1)
2838             (gnus-configure-windows 'group 'force))
2839         (gnus-handle-ephemeral-exit quit-config))
2840       ;; Finally signal the quit.
2841       (signal 'quit nil))
2842      ;; The group was successfully selected.
2843      (t
2844       (gnus-set-global-variables)
2845       ;; Save the active value in effect when the group was entered.
2846       (setq gnus-newsgroup-active
2847             (gnus-copy-sequence
2848              (gnus-active gnus-newsgroup-name)))
2849       ;; You can change the summary buffer in some way with this hook.
2850       (gnus-run-hooks 'gnus-select-group-hook)
2851       ;; Set any local variables in the group parameters.
2852       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2853       (gnus-update-format-specifications
2854        nil 'summary 'summary-mode 'summary-dummy)
2855       (gnus-update-summary-mark-positions)
2856       ;; Do score processing.
2857       (when gnus-use-scoring
2858         (gnus-possibly-score-headers))
2859       ;; Check whether to fill in the gaps in the threads.
2860       (when gnus-build-sparse-threads
2861         (gnus-build-sparse-threads))
2862       ;; Find the initial limit.
2863       (if gnus-show-threads
2864           (if show-all
2865               (let ((gnus-newsgroup-dormant nil))
2866                 (gnus-summary-initial-limit show-all))
2867             (gnus-summary-initial-limit show-all))
2868         ;; When untreaded, all articles are always shown.
2869         (setq gnus-newsgroup-limit
2870               (mapcar
2871                (lambda (header) (mail-header-number header))
2872                gnus-newsgroup-headers)))
2873       ;; Generate the summary buffer.
2874       (unless no-display
2875         (gnus-summary-prepare))
2876       (when gnus-use-trees
2877         (gnus-tree-open group)
2878         (setq gnus-summary-highlight-line-function
2879               'gnus-tree-highlight-article))
2880       ;; If the summary buffer is empty, but there are some low-scored
2881       ;; articles or some excluded dormants, we include these in the
2882       ;; buffer.
2883       (when (and (zerop (buffer-size))
2884                  (not no-display))
2885         (cond (gnus-newsgroup-dormant
2886                (gnus-summary-limit-include-dormant))
2887               ((and gnus-newsgroup-scored show-all)
2888                (gnus-summary-limit-include-expunged t))))
2889       ;; Function `gnus-apply-kill-file' must be called in this hook.
2890       (gnus-run-hooks 'gnus-apply-kill-hook)
2891       (if (and (zerop (buffer-size))
2892                (not no-display))
2893           (progn
2894             ;; This newsgroup is empty.
2895             (gnus-summary-catchup-and-exit nil t)
2896             (gnus-message 6 "No unread news")
2897             (when kill-buffer
2898               (gnus-kill-or-deaden-summary kill-buffer))
2899             ;; Return nil from this function.
2900             nil)
2901         ;; Hide conversation thread subtrees.  We cannot do this in
2902         ;; gnus-summary-prepare-hook since kill processing may not
2903         ;; work with hidden articles.
2904         (and gnus-show-threads
2905              gnus-thread-hide-subtree
2906              (gnus-summary-hide-all-threads))
2907         (when kill-buffer
2908           (gnus-kill-or-deaden-summary kill-buffer))
2909         ;; Show first unread article if requested.
2910         (if (and (not no-article)
2911                  (not no-display)
2912                  gnus-newsgroup-unreads
2913                  gnus-auto-select-first)
2914             (progn
2915               (gnus-configure-windows 'summary)
2916               (cond
2917                ((eq gnus-auto-select-first 'best)
2918                 (gnus-summary-best-unread-article))
2919                ((eq gnus-auto-select-first t)
2920                 (gnus-summary-first-unread-article))
2921                ((gnus-functionp gnus-auto-select-first)
2922                 (funcall gnus-auto-select-first))))
2923           ;; Don't select any articles, just move point to the first
2924           ;; article in the group.
2925           (goto-char (point-min))
2926           (gnus-summary-position-point)
2927           (gnus-configure-windows 'summary 'force)
2928           (gnus-set-mode-line 'summary))
2929         (when (get-buffer-window gnus-group-buffer t)
2930           ;; Gotta use windows, because recenter does weird stuff if
2931           ;; the current buffer ain't the displayed window.
2932           (let ((owin (selected-window)))
2933             (select-window (get-buffer-window gnus-group-buffer t))
2934             (when (gnus-group-goto-group group)
2935               (recenter))
2936             (select-window owin)))
2937         ;; Mark this buffer as "prepared".
2938         (setq gnus-newsgroup-prepared t)
2939         (gnus-run-hooks 'gnus-summary-prepared-hook)
2940         t)))))
2941
2942 (defun gnus-summary-prepare ()
2943   "Generate the summary buffer."
2944   (interactive)
2945   (let ((buffer-read-only nil))
2946     (erase-buffer)
2947     (setq gnus-newsgroup-data nil
2948           gnus-newsgroup-data-reverse nil)
2949     (gnus-run-hooks 'gnus-summary-generate-hook)
2950     ;; Generate the buffer, either with threads or without.
2951     (when gnus-newsgroup-headers
2952       (gnus-summary-prepare-threads
2953        (if gnus-show-threads
2954            (gnus-sort-gathered-threads
2955             (funcall gnus-summary-thread-gathering-function
2956                      (gnus-sort-threads
2957                       (gnus-cut-threads (gnus-make-threads)))))
2958          ;; Unthreaded display.
2959          (gnus-sort-articles gnus-newsgroup-headers))))
2960     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2961     ;; Call hooks for modifying summary buffer.
2962     (goto-char (point-min))
2963     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2964
2965 (defsubst gnus-general-simplify-subject (subject)
2966   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2967   (setq subject
2968         (cond
2969          ;; Truncate the subject.
2970          (gnus-simplify-subject-functions
2971           (gnus-map-function gnus-simplify-subject-functions subject))
2972          ((numberp gnus-summary-gather-subject-limit)
2973           (setq subject (gnus-simplify-subject-re subject))
2974           (if (> (length subject) gnus-summary-gather-subject-limit)
2975               (substring subject 0 gnus-summary-gather-subject-limit)
2976             subject))
2977          ;; Fuzzily simplify it.
2978          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2979           (gnus-simplify-subject-fuzzy subject))
2980          ;; Just remove the leading "Re:".
2981          (t
2982           (gnus-simplify-subject-re subject))))
2983
2984   (if (and gnus-summary-gather-exclude-subject
2985            (string-match gnus-summary-gather-exclude-subject subject))
2986       nil                               ; This article shouldn't be gathered
2987     subject))
2988
2989 (defun gnus-summary-simplify-subject-query ()
2990   "Query where the respool algorithm would put this article."
2991   (interactive)
2992   (gnus-summary-select-article)
2993   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2994
2995 (defun gnus-gather-threads-by-subject (threads)
2996   "Gather threads by looking at Subject headers."
2997   (if (not gnus-summary-make-false-root)
2998       threads
2999     (let ((hashtb (gnus-make-hashtable 1024))
3000           (prev threads)
3001           (result threads)
3002           subject hthread whole-subject)
3003       (while threads
3004         (setq subject (gnus-general-simplify-subject
3005                        (setq whole-subject (mail-header-subject
3006                                             (caar threads)))))
3007         (when subject
3008           (if (setq hthread (gnus-gethash subject hashtb))
3009               (progn
3010                 ;; We enter a dummy root into the thread, if we
3011                 ;; haven't done that already.
3012                 (unless (stringp (caar hthread))
3013                   (setcar hthread (list whole-subject (car hthread))))
3014                 ;; We add this new gathered thread to this gathered
3015                 ;; thread.
3016                 (setcdr (car hthread)
3017                         (nconc (cdar hthread) (list (car threads))))
3018                 ;; Remove it from the list of threads.
3019                 (setcdr prev (cdr threads))
3020                 (setq threads prev))
3021             ;; Enter this thread into the hash table.
3022             (gnus-sethash subject threads hashtb)))
3023         (setq prev threads)
3024         (setq threads (cdr threads)))
3025       result)))
3026
3027 (defun gnus-gather-threads-by-references (threads)
3028   "Gather threads by looking at References headers."
3029   (let ((idhashtb (gnus-make-hashtable 1024))
3030         (thhashtb (gnus-make-hashtable 1024))
3031         (prev threads)
3032         (result threads)
3033         ids references id gthread gid entered ref)
3034     (while threads
3035       (when (setq references (mail-header-references (caar threads)))
3036         (setq id (mail-header-id (caar threads))
3037               ids (gnus-split-references references)
3038               entered nil)
3039         (while (setq ref (pop ids))
3040           (setq ids (delete ref ids))
3041           (if (not (setq gid (gnus-gethash ref idhashtb)))
3042               (progn
3043                 (gnus-sethash ref id idhashtb)
3044                 (gnus-sethash id threads thhashtb))
3045             (setq gthread (gnus-gethash gid thhashtb))
3046             (unless entered
3047               ;; We enter a dummy root into the thread, if we
3048               ;; haven't done that already.
3049               (unless (stringp (caar gthread))
3050                 (setcar gthread (list (mail-header-subject (caar gthread))
3051                                       (car gthread))))
3052               ;; We add this new gathered thread to this gathered
3053               ;; thread.
3054               (setcdr (car gthread)
3055                       (nconc (cdar gthread) (list (car threads)))))
3056             ;; Add it into the thread hash table.
3057             (gnus-sethash id gthread thhashtb)
3058             (setq entered t)
3059             ;; Remove it from the list of threads.
3060             (setcdr prev (cdr threads))
3061             (setq threads prev))))
3062       (setq prev threads)
3063       (setq threads (cdr threads)))
3064     result))
3065
3066 (defun gnus-sort-gathered-threads (threads)
3067   "Sort subtreads inside each gathered thread by article number."
3068   (let ((result threads))
3069     (while threads
3070       (when (stringp (caar threads))
3071         (setcdr (car threads)
3072                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3073       (setq threads (cdr threads)))
3074     result))
3075
3076 (defun gnus-thread-loop-p (root thread)
3077   "Say whether ROOT is in THREAD."
3078   (let ((stack (list thread))
3079         (infloop 0)
3080         th)
3081     (while (setq thread (pop stack))
3082       (setq th (cdr thread))
3083       (while (and th
3084                   (not (eq (caar th) root)))
3085         (pop th))
3086       (if th
3087           ;; We have found a loop.
3088           (let (ref-dep)
3089             (setcdr thread (delq (car th) (cdr thread)))
3090             (if (boundp (setq ref-dep (intern "none"
3091                                               gnus-newsgroup-dependencies)))
3092                 (setcdr (symbol-value ref-dep)
3093                         (nconc (cdr (symbol-value ref-dep))
3094                                (list (car th))))
3095               (set ref-dep (list nil (car th))))
3096             (setq infloop 1
3097                   stack nil))
3098         ;; Push all the subthreads onto the stack.
3099         (push (cdr thread) stack)))
3100     infloop))
3101
3102 (defun gnus-make-threads ()
3103   "Go through the dependency hashtb and find the roots.  Return all threads."
3104   (let (threads)
3105     (while (catch 'infloop
3106              (mapatoms
3107               (lambda (refs)
3108                 ;; Deal with self-referencing References loops.
3109                 (when (and (car (symbol-value refs))
3110                            (not (zerop
3111                                  (apply
3112                                   '+
3113                                   (mapcar
3114                                    (lambda (thread)
3115                                      (gnus-thread-loop-p
3116                                       (car (symbol-value refs)) thread))
3117                                    (cdr (symbol-value refs)))))))
3118                   (setq threads nil)
3119                   (throw 'infloop t))
3120                 (unless (car (symbol-value refs))
3121                   ;; These threads do not refer back to any other articles,
3122                   ;; so they're roots.
3123                   (setq threads (append (cdr (symbol-value refs)) threads))))
3124               gnus-newsgroup-dependencies)))
3125     threads))
3126
3127 ;; Build the thread tree.
3128 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3129   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3130
3131 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3132 if it was already present.
3133
3134 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3135 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3136 Message-IDs will be renamed be renamed to a unique Message-ID before
3137 being entered.
3138
3139 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3140   (let* ((id (mail-header-id header))
3141          (id-dep (and id (intern id dependencies)))
3142          ref ref-dep ref-header)
3143     ;; Enter this `header' in the `dependencies' table.
3144     (cond
3145      ((not id-dep)
3146       (setq header nil))
3147      ;; The first two cases do the normal part: enter a new `header'
3148      ;; in the `dependencies' table.
3149      ((not (boundp id-dep))
3150       (set id-dep (list header)))
3151      ((null (car (symbol-value id-dep)))
3152       (setcar (symbol-value id-dep) header))
3153
3154      ;; From here the `header' was already present in the
3155      ;; `dependencies' table.
3156      (force-new
3157       ;; Overrides an existing entry;
3158       ;; just set the header part of the entry.
3159       (setcar (symbol-value id-dep) header))
3160
3161      ;; Renames the existing `header' to a unique Message-ID.
3162      ((not gnus-summary-ignore-duplicates)
3163       ;; An article with this Message-ID has already been seen.
3164       ;; We rename the Message-ID.
3165       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3166            (list header))
3167       (mail-header-set-id header id))
3168
3169      ;; The last case ignores an existing entry, except it adds any
3170      ;; additional Xrefs (in case the two articles came from different
3171      ;; servers.
3172      ;; Also sets `header' to `nil' meaning that the `dependencies'
3173      ;; table was *not* modified.
3174      (t
3175       (mail-header-set-xref
3176        (car (symbol-value id-dep))
3177        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3178                    "")
3179                (or (mail-header-xref header) "")))
3180       (setq header nil)))
3181
3182     (when header
3183       ;; First check if that we are not creating a References loop.
3184       (setq ref (gnus-parent-id (mail-header-references header)))
3185       (while (and ref
3186                   (setq ref-dep (intern-soft ref dependencies))
3187                   (boundp ref-dep)
3188                   (setq ref-header (car (symbol-value ref-dep))))
3189         (if (string= id ref)
3190             ;; Yuk!  This is a reference loop.  Make the article be a
3191             ;; root article.
3192             (progn
3193               (mail-header-set-references (car (symbol-value id-dep)) "none")
3194               (setq ref nil))
3195           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3196       (setq ref (gnus-parent-id (mail-header-references header)))
3197       (setq ref-dep (intern (or ref "none") dependencies))
3198       (if (boundp ref-dep)
3199           (setcdr (symbol-value ref-dep)
3200                   (nconc (cdr (symbol-value ref-dep))
3201                          (list (symbol-value id-dep))))
3202         (set ref-dep (list nil (symbol-value id-dep)))))
3203     header))
3204
3205 (defun gnus-build-sparse-threads ()
3206   (let ((headers gnus-newsgroup-headers)
3207         (mail-parse-charset gnus-newsgroup-charset)
3208         (gnus-summary-ignore-duplicates t)
3209         header references generation relations
3210         subject child end new-child date)
3211     ;; First we create an alist of generations/relations, where
3212     ;; generations is how much we trust the relation, and the relation
3213     ;; is parent/child.
3214     (gnus-message 7 "Making sparse threads...")
3215     (save-excursion
3216       (nnheader-set-temp-buffer " *gnus sparse threads*")
3217       (while (setq header (pop headers))
3218         (when (and (setq references (mail-header-references header))
3219                    (not (string= references "")))
3220           (insert references)
3221           (setq child (mail-header-id header)
3222                 subject (mail-header-subject header)
3223                 date (mail-header-date header)
3224                 generation 0)
3225           (while (search-backward ">" nil t)
3226             (setq end (1+ (point)))
3227             (when (search-backward "<" nil t)
3228               (setq new-child (buffer-substring (point) end))
3229               (push (list (incf generation)
3230                           child (setq child new-child)
3231                           subject date)
3232                     relations)))
3233           (when child
3234             (push (list (1+ generation) child nil subject) relations))
3235           (erase-buffer)))
3236       (kill-buffer (current-buffer)))
3237     ;; Sort over trustworthiness.
3238     (mapcar
3239      (lambda (relation)
3240        (when (gnus-dependencies-add-header
3241               (make-full-mail-header
3242                gnus-reffed-article-number
3243                (nth 3 relation) "" (or (nth 4 relation) "")
3244                (nth 1 relation)
3245                (or (nth 2 relation) "") 0 0 "")
3246               gnus-newsgroup-dependencies nil)
3247          (push gnus-reffed-article-number gnus-newsgroup-limit)
3248          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3249          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3250                gnus-newsgroup-reads)
3251          (decf gnus-reffed-article-number)))
3252      (sort relations 'car-less-than-car))
3253     (gnus-message 7 "Making sparse threads...done")))
3254
3255 (defun gnus-build-old-threads ()
3256   ;; Look at all the articles that refer back to old articles, and
3257   ;; fetch the headers for the articles that aren't there.  This will
3258   ;; build complete threads - if the roots haven't been expired by the
3259   ;; server, that is.
3260   (let ((mail-parse-charset gnus-newsgroup-charset)
3261         id heads)
3262     (mapatoms
3263      (lambda (refs)
3264        (when (not (car (symbol-value refs)))
3265          (setq heads (cdr (symbol-value refs)))
3266          (while heads
3267            (if (memq (mail-header-number (caar heads))
3268                      gnus-newsgroup-dormant)
3269                (setq heads (cdr heads))
3270              (setq id (symbol-name refs))
3271              (while (and (setq id (gnus-build-get-header id))
3272                          (not (car (gnus-id-to-thread id)))))
3273              (setq heads nil)))))
3274      gnus-newsgroup-dependencies)))
3275
3276 ;; This function has to be called with point after the article number
3277 ;; on the beginning of the line.
3278 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3279   (let ((eol (gnus-point-at-eol))
3280         (buffer (current-buffer))
3281         header)
3282
3283     ;; overview: [num subject from date id refs chars lines misc]
3284     (unwind-protect
3285         (progn
3286           (narrow-to-region (point) eol)
3287           (unless (eobp)
3288             (forward-char))
3289
3290           (setq header
3291                 (make-full-mail-header
3292                  number                 ; number
3293                  (funcall gnus-decode-encoded-word-function
3294                           (nnheader-nov-field)) ; subject
3295                  (funcall gnus-decode-encoded-word-function
3296                           (nnheader-nov-field)) ; from
3297                  (nnheader-nov-field)   ; date
3298                  (nnheader-nov-read-message-id) ; id
3299                  (nnheader-nov-field)   ; refs
3300                  (nnheader-nov-read-integer) ; chars
3301                  (nnheader-nov-read-integer) ; lines
3302                  (unless (eobp)
3303                    (nnheader-nov-field)) ; misc
3304                  (nnheader-nov-parse-extra)))) ; extra
3305
3306       (widen))
3307
3308     (when gnus-alter-header-function
3309       (funcall gnus-alter-header-function header))
3310     (gnus-dependencies-add-header header dependencies force-new)))
3311
3312 (defun gnus-build-get-header (id)
3313   "Look through the buffer of NOV lines and find the header to ID.
3314 Enter this line into the dependencies hash table, and return
3315 the id of the parent article (if any)."
3316   (let ((deps gnus-newsgroup-dependencies)
3317         found header)
3318     (prog1
3319         (save-excursion
3320           (set-buffer nntp-server-buffer)
3321           (let ((case-fold-search nil))
3322             (goto-char (point-min))
3323             (while (and (not found)
3324                         (search-forward id nil t))
3325               (beginning-of-line)
3326               (setq found (looking-at
3327                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3328                                    (regexp-quote id))))
3329               (or found (beginning-of-line 2)))
3330             (when found
3331               (beginning-of-line)
3332               (and
3333                (setq header (gnus-nov-parse-line
3334                              (read (current-buffer)) deps))
3335                (gnus-parent-id (mail-header-references header))))))
3336       (when header
3337         (let ((number (mail-header-number header)))
3338           (push number gnus-newsgroup-limit)
3339           (push header gnus-newsgroup-headers)
3340           (if (memq number gnus-newsgroup-unselected)
3341               (progn
3342                 (push number gnus-newsgroup-unreads)
3343                 (setq gnus-newsgroup-unselected
3344                       (delq number gnus-newsgroup-unselected)))
3345             (push number gnus-newsgroup-ancient)))))))
3346
3347 (defun gnus-build-all-threads ()
3348   "Read all the headers."
3349   (let ((gnus-summary-ignore-duplicates t)
3350         (mail-parse-charset gnus-newsgroup-charset)
3351         (dependencies gnus-newsgroup-dependencies)
3352         header article)
3353     (save-excursion
3354       (set-buffer nntp-server-buffer)
3355       (let ((case-fold-search nil))
3356         (goto-char (point-min))
3357         (while (not (eobp))
3358           (ignore-errors
3359             (setq article (read (current-buffer))
3360                   header (gnus-nov-parse-line article dependencies)))
3361           (when header
3362             (save-excursion
3363               (set-buffer gnus-summary-buffer)
3364               (push header gnus-newsgroup-headers)
3365               (if (memq (setq article (mail-header-number header))
3366                         gnus-newsgroup-unselected)
3367                   (progn
3368                     (push article gnus-newsgroup-unreads)
3369                     (setq gnus-newsgroup-unselected
3370                           (delq article gnus-newsgroup-unselected)))
3371                 (push article gnus-newsgroup-ancient)))
3372             (forward-line 1)))))))
3373
3374 (defun gnus-summary-update-article-line (article header)
3375   "Update the line for ARTICLE using HEADERS."
3376   (let* ((id (mail-header-id header))
3377          (thread (gnus-id-to-thread id)))
3378     (unless thread
3379       (error "Article in no thread"))
3380     ;; Update the thread.
3381     (setcar thread header)
3382     (gnus-summary-goto-subject article)
3383     (let* ((datal (gnus-data-find-list article))
3384            (data (car datal))
3385            (length (when (cdr datal)
3386                      (- (gnus-data-pos data)
3387                         (gnus-data-pos (cadr datal)))))
3388            (buffer-read-only nil)
3389            (level (gnus-summary-thread-level)))
3390       (gnus-delete-line)
3391       (gnus-summary-insert-line
3392        header level nil (gnus-article-mark article)
3393        (memq article gnus-newsgroup-replied)
3394        (memq article gnus-newsgroup-expirable)
3395        ;; Only insert the Subject string when it's different
3396        ;; from the previous Subject string.
3397        (if (and
3398             gnus-show-threads
3399             (gnus-subject-equal
3400              (condition-case ()
3401                  (mail-header-subject
3402                   (gnus-data-header
3403                    (cadr
3404                     (gnus-data-find-list
3405                      article
3406                      (gnus-data-list t)))))
3407                ;; Error on the side of excessive subjects.
3408                (error ""))
3409              (mail-header-subject header)))
3410            ""
3411          (mail-header-subject header))
3412        nil (cdr (assq article gnus-newsgroup-scored))
3413        (memq article gnus-newsgroup-processable))
3414       (when length
3415         (gnus-data-update-list
3416          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3417
3418 (defun gnus-summary-update-article (article &optional iheader)
3419   "Update ARTICLE in the summary buffer."
3420   (set-buffer gnus-summary-buffer)
3421   (let* ((header (gnus-summary-article-header article))
3422          (id (mail-header-id header))
3423          (data (gnus-data-find article))
3424          (thread (gnus-id-to-thread id))
3425          (references (mail-header-references header))
3426          (parent
3427           (gnus-id-to-thread
3428            (or (gnus-parent-id
3429                 (when (and references
3430                            (not (equal "" references)))
3431                   references))
3432                "none")))
3433          (buffer-read-only nil)
3434          (old (car thread)))
3435     (when thread
3436       (unless iheader
3437         (setcar thread nil)
3438         (when parent
3439           (delq thread parent)))
3440       (if (gnus-summary-insert-subject id header)
3441           ;; Set the (possibly) new article number in the data structure.
3442           (gnus-data-set-number data (gnus-id-to-article id))
3443         (setcar thread old)
3444         nil))))
3445
3446 (defun gnus-rebuild-thread (id &optional line)
3447   "Rebuild the thread containing ID.
3448 If LINE, insert the rebuilt thread starting on line LINE."
3449   (let ((buffer-read-only nil)
3450         old-pos current thread data)
3451     (if (not gnus-show-threads)
3452         (setq thread (list (car (gnus-id-to-thread id))))
3453       ;; Get the thread this article is part of.
3454       (setq thread (gnus-remove-thread id)))
3455     (setq old-pos (gnus-point-at-bol))
3456     (setq current (save-excursion
3457                     (and (zerop (forward-line -1))
3458                          (gnus-summary-article-number))))
3459     ;; If this is a gathered thread, we have to go some re-gathering.
3460     (when (stringp (car thread))
3461       (let ((subject (car thread))
3462             roots thr)
3463         (setq thread (cdr thread))
3464         (while thread
3465           (unless (memq (setq thr (gnus-id-to-thread
3466                                    (gnus-root-id
3467                                     (mail-header-id (caar thread)))))
3468                         roots)
3469             (push thr roots))
3470           (setq thread (cdr thread)))
3471         ;; We now have all (unique) roots.
3472         (if (= (length roots) 1)
3473             ;; All the loose roots are now one solid root.
3474             (setq thread (car roots))
3475           (setq thread (cons subject (gnus-sort-threads roots))))))
3476     (let (threads)
3477       ;; We then insert this thread into the summary buffer.
3478       (when line
3479         (goto-char (point-min))
3480         (forward-line (1- line)))
3481       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3482         (if gnus-show-threads
3483             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3484           (gnus-summary-prepare-unthreaded thread))
3485         (setq data (nreverse gnus-newsgroup-data))
3486         (setq threads gnus-newsgroup-threads))
3487       ;; We splice the new data into the data structure.
3488       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3489       ;;!!! then we want to insert at the beginning of the buffer.
3490       ;;!!! That happens to be true with Gnus now, but that may
3491       ;;!!! change in the future.  Perhaps.
3492       (gnus-data-enter-list
3493        (if line nil current) data (- (point) old-pos))
3494       (setq gnus-newsgroup-threads
3495             (nconc threads gnus-newsgroup-threads))
3496       (gnus-data-compute-positions))))
3497
3498 (defun gnus-number-to-header (number)
3499   "Return the header for article NUMBER."
3500   (let ((headers gnus-newsgroup-headers))
3501     (while (and headers
3502                 (not (= number (mail-header-number (car headers)))))
3503       (pop headers))
3504     (when headers
3505       (car headers))))
3506
3507 (defun gnus-parent-headers (in-headers &optional generation)
3508   "Return the headers of the GENERATIONeth parent of HEADERS."
3509   (unless generation
3510     (setq generation 1))
3511   (let ((parent t)
3512         (headers in-headers)
3513         references)
3514     (while (and parent
3515                 (not (zerop generation))
3516                 (setq references (mail-header-references headers)))
3517       (setq headers (if (and references
3518                              (setq parent (gnus-parent-id references)))
3519                         (car (gnus-id-to-thread parent))
3520                       nil))
3521       (decf generation))
3522     (and (not (eq headers in-headers))
3523          headers)))
3524
3525 (defun gnus-id-to-thread (id)
3526   "Return the (sub-)thread where ID appears."
3527   (gnus-gethash id gnus-newsgroup-dependencies))
3528
3529 (defun gnus-id-to-article (id)
3530   "Return the article number of ID."
3531   (let ((thread (gnus-id-to-thread id)))
3532     (when (and thread
3533                (car thread))
3534       (mail-header-number (car thread)))))
3535
3536 (defun gnus-id-to-header (id)
3537   "Return the article headers of ID."
3538   (car (gnus-id-to-thread id)))
3539
3540 (defun gnus-article-displayed-root-p (article)
3541   "Say whether ARTICLE is a root(ish) article."
3542   (let ((level (gnus-summary-thread-level article))
3543         (refs (mail-header-references  (gnus-summary-article-header article)))
3544         particle)
3545     (cond
3546      ((null level) nil)
3547      ((zerop level) t)
3548      ((null refs) t)
3549      ((null (gnus-parent-id refs)) t)
3550      ((and (= 1 level)
3551            (null (setq particle (gnus-id-to-article
3552                                  (gnus-parent-id refs))))
3553            (null (gnus-summary-thread-level particle)))))))
3554
3555 (defun gnus-root-id (id)
3556   "Return the id of the root of the thread where ID appears."
3557   (let (last-id prev)
3558     (while (and id (setq prev (car (gnus-id-to-thread id))))
3559       (setq last-id id
3560             id (gnus-parent-id (mail-header-references prev))))
3561     last-id))
3562
3563 (defun gnus-articles-in-thread (thread)
3564   "Return the list of articles in THREAD."
3565   (cons (mail-header-number (car thread))
3566         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3567
3568 (defun gnus-remove-thread (id &optional dont-remove)
3569   "Remove the thread that has ID in it."
3570   (let (headers thread last-id)
3571     ;; First go up in this thread until we find the root.
3572     (setq last-id (gnus-root-id id)
3573           headers (message-flatten-list (gnus-id-to-thread last-id)))
3574     ;; We have now found the real root of this thread.  It might have
3575     ;; been gathered into some loose thread, so we have to search
3576     ;; through the threads to find the thread we wanted.
3577     (let ((threads gnus-newsgroup-threads)
3578           sub)
3579       (while threads
3580         (setq sub (car threads))
3581         (if (stringp (car sub))
3582             ;; This is a gathered thread, so we look at the roots
3583             ;; below it to find whether this article is in this
3584             ;; gathered root.
3585             (progn
3586               (setq sub (cdr sub))
3587               (while sub
3588                 (when (member (caar sub) headers)
3589                   (setq thread (car threads)
3590                         threads nil
3591                         sub nil))
3592                 (setq sub (cdr sub))))
3593           ;; It's an ordinary thread, so we check it.
3594           (when (eq (car sub) (car headers))
3595             (setq thread sub
3596                   threads nil)))
3597         (setq threads (cdr threads)))
3598       ;; If this article is in no thread, then it's a root.
3599       (if thread
3600           (unless dont-remove
3601             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3602         (setq thread (gnus-id-to-thread last-id)))
3603       (when thread
3604         (prog1
3605             thread                      ; We return this thread.
3606           (unless dont-remove
3607             (if (stringp (car thread))
3608                 (progn
3609                   ;; If we use dummy roots, then we have to remove the
3610                   ;; dummy root as well.
3611                   (when (eq gnus-summary-make-false-root 'dummy)
3612                     ;; We go to the dummy root by going to
3613                     ;; the first sub-"thread", and then one line up.
3614                     (gnus-summary-goto-article
3615                      (mail-header-number (caadr thread)))
3616                     (forward-line -1)
3617                     (gnus-delete-line)
3618                     (gnus-data-compute-positions))
3619                   (setq thread (cdr thread))
3620                   (while thread
3621                     (gnus-remove-thread-1 (car thread))
3622                     (setq thread (cdr thread))))
3623               (gnus-remove-thread-1 thread))))))))
3624
3625 (defun gnus-remove-thread-1 (thread)
3626   "Remove the thread THREAD recursively."
3627   (let ((number (mail-header-number (pop thread)))
3628         d)
3629     (setq thread (reverse thread))
3630     (while thread
3631       (gnus-remove-thread-1 (pop thread)))
3632     (when (setq d (gnus-data-find number))
3633       (goto-char (gnus-data-pos d))
3634       (gnus-summary-show-thread)
3635       (gnus-data-remove
3636        number
3637        (- (gnus-point-at-bol)
3638           (prog1
3639               (1+ (gnus-point-at-eol))
3640             (gnus-delete-line)))))))
3641
3642 (defun gnus-sort-threads (threads)
3643   "Sort THREADS."
3644   (if (not gnus-thread-sort-functions)
3645       threads
3646     (gnus-message 8 "Sorting threads...")
3647     (prog1
3648         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3649       (gnus-message 8 "Sorting threads...done"))))
3650
3651 (defun gnus-sort-articles (articles)
3652   "Sort ARTICLES."
3653   (when gnus-article-sort-functions
3654     (gnus-message 7 "Sorting articles...")
3655     (prog1
3656         (setq gnus-newsgroup-headers
3657               (sort articles (gnus-make-sort-function
3658                               gnus-article-sort-functions)))
3659       (gnus-message 7 "Sorting articles...done"))))
3660
3661 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3662 (defmacro gnus-thread-header (thread)
3663   "Return header of first article in THREAD.
3664 Note that THREAD must never, ever be anything else than a variable -
3665 using some other form will lead to serious barfage."
3666   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3667   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3668   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3669         (vector thread) 2))
3670
3671 (defsubst gnus-article-sort-by-number (h1 h2)
3672   "Sort articles by article number."
3673   (< (mail-header-number h1)
3674      (mail-header-number h2)))
3675
3676 (defun gnus-thread-sort-by-number (h1 h2)
3677   "Sort threads by root article number."
3678   (gnus-article-sort-by-number
3679    (gnus-thread-header h1) (gnus-thread-header h2)))
3680
3681 (defsubst gnus-article-sort-by-lines (h1 h2)
3682   "Sort articles by article Lines header."
3683   (< (mail-header-lines h1)
3684      (mail-header-lines h2)))
3685
3686 (defun gnus-thread-sort-by-lines (h1 h2)
3687   "Sort threads by root article Lines header."
3688   (gnus-article-sort-by-lines
3689    (gnus-thread-header h1) (gnus-thread-header h2)))
3690
3691 (defsubst gnus-article-sort-by-chars (h1 h2)
3692   "Sort articles by octet length."
3693   (< (mail-header-chars h1)
3694      (mail-header-chars h2)))
3695
3696 (defun gnus-thread-sort-by-chars (h1 h2)
3697   "Sort threads by root article octet length."
3698   (gnus-article-sort-by-chars
3699    (gnus-thread-header h1) (gnus-thread-header h2)))
3700
3701 (defsubst gnus-article-sort-by-author (h1 h2)
3702   "Sort articles by root author."
3703   (string-lessp
3704    (let ((extract (funcall
3705                    gnus-extract-address-components
3706                    (mail-header-from h1))))
3707      (or (car extract) (cadr extract) ""))
3708    (let ((extract (funcall
3709                    gnus-extract-address-components
3710                    (mail-header-from h2))))
3711      (or (car extract) (cadr extract) ""))))
3712
3713 (defun gnus-thread-sort-by-author (h1 h2)
3714   "Sort threads by root author."
3715   (gnus-article-sort-by-author
3716    (gnus-thread-header h1)  (gnus-thread-header h2)))
3717
3718 (defsubst gnus-article-sort-by-subject (h1 h2)
3719   "Sort articles by root subject."
3720   (string-lessp
3721    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3722    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3723
3724 (defun gnus-thread-sort-by-subject (h1 h2)
3725   "Sort threads by root subject."
3726   (gnus-article-sort-by-subject
3727    (gnus-thread-header h1) (gnus-thread-header h2)))
3728
3729 (defsubst gnus-article-sort-by-date (h1 h2)
3730   "Sort articles by root article date."
3731   (time-less-p
3732    (gnus-date-get-time (mail-header-date h1))
3733    (gnus-date-get-time (mail-header-date h2))))
3734
3735 (defun gnus-thread-sort-by-date (h1 h2)
3736   "Sort threads by root article date."
3737   (gnus-article-sort-by-date
3738    (gnus-thread-header h1) (gnus-thread-header h2)))
3739
3740 (defsubst gnus-article-sort-by-score (h1 h2)
3741   "Sort articles by root article score.
3742 Unscored articles will be counted as having a score of zero."
3743   (> (or (cdr (assq (mail-header-number h1)
3744                     gnus-newsgroup-scored))
3745          gnus-summary-default-score 0)
3746      (or (cdr (assq (mail-header-number h2)
3747                     gnus-newsgroup-scored))
3748          gnus-summary-default-score 0)))
3749
3750 (defun gnus-thread-sort-by-score (h1 h2)
3751   "Sort threads by root article score."
3752   (gnus-article-sort-by-score
3753    (gnus-thread-header h1) (gnus-thread-header h2)))
3754
3755 (defun gnus-thread-sort-by-total-score (h1 h2)
3756   "Sort threads by the sum of all scores in the thread.
3757 Unscored articles will be counted as having a score of zero."
3758   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3759
3760 (defun gnus-thread-total-score (thread)
3761   ;; This function find the total score of THREAD.
3762   (cond ((null thread)
3763          0)
3764         ((consp thread)
3765          (if (stringp (car thread))
3766              (apply gnus-thread-score-function 0
3767                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3768            (gnus-thread-total-score-1 thread)))
3769         (t
3770          (gnus-thread-total-score-1 (list thread)))))
3771
3772 (defun gnus-thread-total-score-1 (root)
3773   ;; This function find the total score of the thread below ROOT.
3774   (setq root (car root))
3775   (apply gnus-thread-score-function
3776          (or (append
3777               (mapcar 'gnus-thread-total-score
3778                       (cdr (gnus-id-to-thread (mail-header-id root))))
3779               (when (> (mail-header-number root) 0)
3780                 (list (or (cdr (assq (mail-header-number root)
3781                                      gnus-newsgroup-scored))
3782                           gnus-summary-default-score 0))))
3783              (list gnus-summary-default-score)
3784              '(0))))
3785
3786 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3787 (defvar gnus-tmp-prev-subject nil)
3788 (defvar gnus-tmp-false-parent nil)
3789 (defvar gnus-tmp-root-expunged nil)
3790 (defvar gnus-tmp-dummy-line nil)
3791
3792 (defvar gnus-tmp-header)
3793 (defun gnus-extra-header (type &optional header)
3794   "Return the extra header of TYPE."
3795   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3796       ""))
3797
3798 (defun gnus-summary-prepare-threads (threads)
3799   "Prepare summary buffer from THREADS and indentation LEVEL.
3800 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3801 or a straight list of headers."
3802   (gnus-message 7 "Generating summary...")
3803
3804   (setq gnus-newsgroup-threads threads)
3805   (beginning-of-line)
3806
3807   (let ((gnus-tmp-level 0)
3808         (default-score (or gnus-summary-default-score 0))
3809         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3810         thread number subject stack state gnus-tmp-gathered beg-match
3811         new-roots gnus-tmp-new-adopts thread-end
3812         gnus-tmp-header gnus-tmp-unread
3813         gnus-tmp-replied gnus-tmp-subject-or-nil
3814         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3815         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3816         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3817
3818     (setq gnus-tmp-prev-subject nil)
3819
3820     (if (vectorp (car threads))
3821         ;; If this is a straight (sic) list of headers, then a
3822         ;; threaded summary display isn't required, so we just create
3823         ;; an unthreaded one.
3824         (gnus-summary-prepare-unthreaded threads)
3825
3826       ;; Do the threaded display.
3827
3828       (while (or threads stack gnus-tmp-new-adopts new-roots)
3829
3830         (if (and (= gnus-tmp-level 0)
3831                  (or (not stack)
3832                      (= (caar stack) 0))
3833                  (not gnus-tmp-false-parent)
3834                  (or gnus-tmp-new-adopts new-roots))
3835             (if gnus-tmp-new-adopts
3836                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3837                       thread (list (car gnus-tmp-new-adopts))
3838                       gnus-tmp-header (caar thread)
3839                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3840               (when new-roots
3841                 (setq thread (list (car new-roots))
3842                       gnus-tmp-header (caar thread)
3843                       new-roots (cdr new-roots))))
3844
3845           (if threads
3846               ;; If there are some threads, we do them before the
3847               ;; threads on the stack.
3848               (setq thread threads
3849                     gnus-tmp-header (caar thread))
3850             ;; There were no current threads, so we pop something off
3851             ;; the stack.
3852             (setq state (car stack)
3853                   gnus-tmp-level (car state)
3854                   thread (cdr state)
3855                   stack (cdr stack)
3856                   gnus-tmp-header (caar thread))))
3857
3858         (setq gnus-tmp-false-parent nil)
3859         (setq gnus-tmp-root-expunged nil)
3860         (setq thread-end nil)
3861
3862         (if (stringp gnus-tmp-header)
3863             ;; The header is a dummy root.
3864             (cond
3865              ((eq gnus-summary-make-false-root 'adopt)
3866               ;; We let the first article adopt the rest.
3867               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3868                                                (cddar thread)))
3869               (setq gnus-tmp-gathered
3870                     (nconc (mapcar
3871                             (lambda (h) (mail-header-number (car h)))
3872                             (cddar thread))
3873                            gnus-tmp-gathered))
3874               (setq thread (cons (list (caar thread)
3875                                        (cadar thread))
3876                                  (cdr thread)))
3877               (setq gnus-tmp-level -1
3878                     gnus-tmp-false-parent t))
3879              ((eq gnus-summary-make-false-root 'empty)
3880               ;; We print adopted articles with empty subject fields.
3881               (setq gnus-tmp-gathered
3882                     (nconc (mapcar
3883                             (lambda (h) (mail-header-number (car h)))
3884                             (cddar thread))
3885                            gnus-tmp-gathered))
3886               (setq gnus-tmp-level -1))
3887              ((eq gnus-summary-make-false-root 'dummy)
3888               ;; We remember that we probably want to output a dummy
3889               ;; root.
3890               (setq gnus-tmp-dummy-line gnus-tmp-header)
3891               (setq gnus-tmp-prev-subject gnus-tmp-header))
3892              (t
3893               ;; We do not make a root for the gathered
3894               ;; sub-threads at all.
3895               (setq gnus-tmp-level -1)))
3896
3897           (setq number (mail-header-number gnus-tmp-header)
3898                 subject (mail-header-subject gnus-tmp-header))
3899
3900           (cond
3901            ;; If the thread has changed subject, we might want to make
3902            ;; this subthread into a root.
3903            ((and (null gnus-thread-ignore-subject)
3904                  (not (zerop gnus-tmp-level))
3905                  gnus-tmp-prev-subject
3906                  (not (inline
3907                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3908             (setq new-roots (nconc new-roots (list (car thread)))
3909                   thread-end t
3910                   gnus-tmp-header nil))
3911            ;; If the article lies outside the current limit,
3912            ;; then we do not display it.
3913            ((not (memq number gnus-newsgroup-limit))
3914             (setq gnus-tmp-gathered
3915                   (nconc (mapcar
3916                           (lambda (h) (mail-header-number (car h)))
3917                           (cdar thread))
3918                          gnus-tmp-gathered))
3919             (setq gnus-tmp-new-adopts (if (cdar thread)
3920                                           (append gnus-tmp-new-adopts
3921                                                   (cdar thread))
3922                                         gnus-tmp-new-adopts)
3923                   thread-end t
3924                   gnus-tmp-header nil)
3925             (when (zerop gnus-tmp-level)
3926               (setq gnus-tmp-root-expunged t)))
3927            ;; Perhaps this article is to be marked as read?
3928            ((and gnus-summary-mark-below
3929                  (< (or (cdr (assq number gnus-newsgroup-scored))
3930                         default-score)
3931                     gnus-summary-mark-below)
3932                  ;; Don't touch sparse articles.
3933                  (not (gnus-summary-article-sparse-p number))
3934                  (not (gnus-summary-article-ancient-p number)))
3935             (setq gnus-newsgroup-unreads
3936                   (delq number gnus-newsgroup-unreads))
3937             (if gnus-newsgroup-auto-expire
3938                 (push number gnus-newsgroup-expirable)
3939               (push (cons number gnus-low-score-mark)
3940                     gnus-newsgroup-reads))))
3941
3942           (when gnus-tmp-header
3943             ;; We may have an old dummy line to output before this
3944             ;; article.
3945             (when (and gnus-tmp-dummy-line
3946                        (gnus-subject-equal
3947                         gnus-tmp-dummy-line
3948                         (mail-header-subject gnus-tmp-header)))
3949               (gnus-summary-insert-dummy-line
3950                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3951               (setq gnus-tmp-dummy-line nil))
3952
3953             ;; Compute the mark.
3954             (setq gnus-tmp-unread (gnus-article-mark number))
3955
3956             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3957                                   gnus-tmp-header gnus-tmp-level)
3958                   gnus-newsgroup-data)
3959
3960             ;; Actually insert the line.
3961             (setq
3962              gnus-tmp-subject-or-nil
3963              (cond
3964               ((and gnus-thread-ignore-subject
3965                     gnus-tmp-prev-subject
3966                     (not (inline (gnus-subject-equal
3967                                   gnus-tmp-prev-subject subject))))
3968                subject)
3969               ((zerop gnus-tmp-level)
3970                (if (and (eq gnus-summary-make-false-root 'empty)
3971                         (memq number gnus-tmp-gathered)
3972                         gnus-tmp-prev-subject
3973                         (inline (gnus-subject-equal
3974                                  gnus-tmp-prev-subject subject)))
3975                    gnus-summary-same-subject
3976                  subject))
3977               (t gnus-summary-same-subject)))
3978             (if (and (eq gnus-summary-make-false-root 'adopt)
3979                      (= gnus-tmp-level 1)
3980                      (memq number gnus-tmp-gathered))
3981                 (setq gnus-tmp-opening-bracket ?\<
3982                       gnus-tmp-closing-bracket ?\>)
3983               (setq gnus-tmp-opening-bracket ?\[
3984                     gnus-tmp-closing-bracket ?\]))
3985             (setq
3986              gnus-tmp-indentation
3987              (aref gnus-thread-indent-array gnus-tmp-level)
3988              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3989              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3990                                 gnus-summary-default-score 0)
3991              gnus-tmp-score-char
3992              (if (or (null gnus-summary-default-score)
3993                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3994                          gnus-summary-zcore-fuzz))
3995                  ?  ;Whitespace
3996                (if (< gnus-tmp-score gnus-summary-default-score)
3997                    gnus-score-below-mark gnus-score-over-mark))
3998              gnus-tmp-replied
3999              (cond ((memq number gnus-newsgroup-processable)
4000                     gnus-process-mark)
4001                    ((memq number gnus-newsgroup-cached)
4002                     gnus-cached-mark)
4003                    ((memq number gnus-newsgroup-replied)
4004                     gnus-replied-mark)
4005                    ((memq number gnus-newsgroup-saved)
4006                     gnus-saved-mark)
4007                    (t gnus-unread-mark))
4008              gnus-tmp-from (mail-header-from gnus-tmp-header)
4009              gnus-tmp-name
4010              (cond
4011               ((string-match "<[^>]+> *$" gnus-tmp-from)
4012                (setq beg-match (match-beginning 0))
4013                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4014                         (substring gnus-tmp-from (1+ (match-beginning 0))
4015                                    (1- (match-end 0))))
4016                    (substring gnus-tmp-from 0 beg-match)))
4017               ((string-match "(.+)" gnus-tmp-from)
4018                (substring gnus-tmp-from
4019                           (1+ (match-beginning 0)) (1- (match-end 0))))
4020               (t gnus-tmp-from)))
4021             (when (string= gnus-tmp-name "")
4022               (setq gnus-tmp-name gnus-tmp-from))
4023             (unless (numberp gnus-tmp-lines)
4024               (setq gnus-tmp-lines 0))
4025             (gnus-put-text-property
4026              (point)
4027              (progn (eval gnus-summary-line-format-spec) (point))
4028              'gnus-number number)
4029             (when gnus-visual-p
4030               (forward-line -1)
4031               (gnus-run-hooks 'gnus-summary-update-hook)
4032               (forward-line 1))
4033
4034             (setq gnus-tmp-prev-subject subject)))
4035
4036         (when (nth 1 thread)
4037           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4038         (incf gnus-tmp-level)
4039         (setq threads (if thread-end nil (cdar thread)))
4040         (unless threads
4041           (setq gnus-tmp-level 0)))))
4042   (gnus-message 7 "Generating summary...done"))
4043
4044 (defun gnus-summary-prepare-unthreaded (headers)
4045   "Generate an unthreaded summary buffer based on HEADERS."
4046   (let (header number mark)
4047
4048     (beginning-of-line)
4049
4050     (while headers
4051       ;; We may have to root out some bad articles...
4052       (when (memq (setq number (mail-header-number
4053                                 (setq header (pop headers))))
4054                   gnus-newsgroup-limit)
4055         ;; Mark article as read when it has a low score.
4056         (when (and gnus-summary-mark-below
4057                    (< (or (cdr (assq number gnus-newsgroup-scored))
4058                           gnus-summary-default-score 0)
4059                       gnus-summary-mark-below)
4060                    (not (gnus-summary-article-ancient-p number)))
4061           (setq gnus-newsgroup-unreads
4062                 (delq number gnus-newsgroup-unreads))
4063           (if gnus-newsgroup-auto-expire
4064               (push number gnus-newsgroup-expirable)
4065             (push (cons number gnus-low-score-mark)
4066                   gnus-newsgroup-reads)))
4067
4068         (setq mark (gnus-article-mark number))
4069         (push (gnus-data-make number mark (1+ (point)) header 0)
4070               gnus-newsgroup-data)
4071         (gnus-summary-insert-line
4072          header 0 number
4073          mark (memq number gnus-newsgroup-replied)
4074          (memq number gnus-newsgroup-expirable)
4075          (mail-header-subject header) nil
4076          (cdr (assq number gnus-newsgroup-scored))
4077          (memq number gnus-newsgroup-processable))))))
4078
4079 (defun gnus-summary-remove-list-identifiers ()
4080   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4081   (let ((regexp (if (stringp gnus-list-identifiers)
4082                     gnus-list-identifiers
4083                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4084     (dolist (header gnus-newsgroup-headers)
4085       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4086                                   " *\\)\\)+\\(Re: +\\)?\\)")
4087                           (mail-header-subject header))
4088         (mail-header-set-subject
4089          header (concat (substring (mail-header-subject header)
4090                                    0 (match-beginning 1))
4091                         (or
4092                          (match-string 3 (mail-header-subject header))
4093                          (match-string 5 (mail-header-subject header)))
4094                         (substring (mail-header-subject header)
4095                                    (match-end 1))))))))
4096
4097 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4098   "Select newsgroup GROUP.
4099 If READ-ALL is non-nil, all articles in the group are selected.
4100 If SELECT-ARTICLES, only select those articles from GROUP."
4101   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4102          ;;!!! Dirty hack; should be removed.
4103          (gnus-summary-ignore-duplicates
4104           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4105               t
4106             gnus-summary-ignore-duplicates))
4107          (info (nth 2 entry))
4108          articles fetched-articles cached)
4109
4110     (unless (gnus-check-server
4111              (setq gnus-current-select-method
4112                    (gnus-find-method-for-group group)))
4113       (error "Couldn't open server"))
4114
4115     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4116         (gnus-activate-group group)     ; Or we can activate it...
4117         (progn                          ; Or we bug out.
4118           (when (equal major-mode 'gnus-summary-mode)
4119             (kill-buffer (current-buffer)))
4120           (error "Couldn't request group %s: %s"
4121                  group (gnus-status-message group))))
4122
4123     (unless (gnus-request-group group t)
4124       (when (equal major-mode 'gnus-summary-mode)
4125         (kill-buffer (current-buffer)))
4126       (error "Couldn't request group %s: %s"
4127              group (gnus-status-message group)))
4128
4129     (setq gnus-newsgroup-name group)
4130     (setq gnus-newsgroup-unselected nil)
4131     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4132     (gnus-summary-setup-default-charset)
4133
4134     ;; Adjust and set lists of article marks.
4135     (when info
4136       (gnus-adjust-marked-articles info))
4137
4138     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4139     (when (gnus-virtual-group-p group)
4140       (setq cached gnus-newsgroup-cached))
4141
4142     (setq gnus-newsgroup-unreads
4143           (gnus-set-difference
4144            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4145            gnus-newsgroup-dormant))
4146
4147     (setq gnus-newsgroup-processable nil)
4148
4149     (gnus-update-read-articles group gnus-newsgroup-unreads)
4150
4151     (if (setq articles select-articles)
4152         (setq gnus-newsgroup-unselected
4153               (gnus-sorted-intersection
4154                gnus-newsgroup-unreads
4155                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4156       (setq articles (gnus-articles-to-read group read-all)))
4157
4158     (cond
4159      ((null articles)
4160       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4161       'quit)
4162      ((eq articles 0) nil)
4163      (t
4164       ;; Init the dependencies hash table.
4165       (setq gnus-newsgroup-dependencies
4166             (gnus-make-hashtable (length articles)))
4167       (gnus-set-global-variables)
4168       ;; Retrieve the headers and read them in.
4169       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4170       (setq gnus-newsgroup-headers
4171             (if (eq 'nov
4172                     (setq gnus-headers-retrieved-by
4173                           (gnus-retrieve-headers
4174                            articles gnus-newsgroup-name
4175                            ;; We might want to fetch old headers, but
4176                            ;; not if there is only 1 article.
4177                            (and (or (and
4178                                      (not (eq gnus-fetch-old-headers 'some))
4179                                      (not (numberp gnus-fetch-old-headers)))
4180                                     (> (length articles) 1))
4181                                 gnus-fetch-old-headers))))
4182                 (gnus-get-newsgroup-headers-xover
4183                  articles nil nil gnus-newsgroup-name t)
4184               (gnus-get-newsgroup-headers)))
4185       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4186
4187       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4188       (when cached
4189         (setq gnus-newsgroup-cached cached))
4190
4191       ;; Suppress duplicates?
4192       (when gnus-suppress-duplicates
4193         (gnus-dup-suppress-articles))
4194
4195       ;; Set the initial limit.
4196       (setq gnus-newsgroup-limit (copy-sequence articles))
4197       ;; Remove canceled articles from the list of unread articles.
4198       (setq gnus-newsgroup-unreads
4199             (gnus-set-sorted-intersection
4200              gnus-newsgroup-unreads
4201              (setq fetched-articles
4202                    (mapcar (lambda (headers) (mail-header-number headers))
4203                            gnus-newsgroup-headers))))
4204       ;; Removed marked articles that do not exist.
4205       (gnus-update-missing-marks
4206        (gnus-sorted-complement fetched-articles articles))
4207       ;; We might want to build some more threads first.
4208       (when (and gnus-fetch-old-headers
4209                  (eq gnus-headers-retrieved-by 'nov))
4210         (if (eq gnus-fetch-old-headers 'invisible)
4211             (gnus-build-all-threads)
4212           (gnus-build-old-threads)))
4213       ;; Let the Gnus agent mark articles as read.
4214       (when gnus-agent
4215         (gnus-agent-get-undownloaded-list))
4216       ;; Remove list identifiers from subject
4217       (when gnus-list-identifiers
4218         (gnus-summary-remove-list-identifiers))
4219       ;; Check whether auto-expire is to be done in this group.
4220       (setq gnus-newsgroup-auto-expire
4221             (gnus-group-auto-expirable-p group))
4222       ;; Set up the article buffer now, if necessary.
4223       (unless gnus-single-article-buffer
4224         (gnus-article-setup-buffer))
4225       ;; First and last article in this newsgroup.
4226       (when gnus-newsgroup-headers
4227         (setq gnus-newsgroup-begin
4228               (mail-header-number (car gnus-newsgroup-headers))
4229               gnus-newsgroup-end
4230               (mail-header-number
4231                (gnus-last-element gnus-newsgroup-headers))))
4232       ;; GROUP is successfully selected.
4233       (or gnus-newsgroup-headers t)))))
4234
4235 (defun gnus-articles-to-read (group &optional read-all)
4236   "Find out what articles the user wants to read."
4237   (let* ((articles
4238           ;; Select all articles if `read-all' is non-nil, or if there
4239           ;; are no unread articles.
4240           (if (or read-all
4241                   (and (zerop (length gnus-newsgroup-marked))
4242                        (zerop (length gnus-newsgroup-unreads)))
4243                   (eq (gnus-group-find-parameter group 'display)
4244                       'all))
4245               (or
4246                (gnus-uncompress-range (gnus-active group))
4247                (gnus-cache-articles-in-group group))
4248             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4249                           (copy-sequence gnus-newsgroup-unreads))
4250                   '<)))
4251          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4252          (scored (length scored-list))
4253          (number (length articles))
4254          (marked (+ (length gnus-newsgroup-marked)
4255                     (length gnus-newsgroup-dormant)))
4256          (select
4257           (cond
4258            ((numberp read-all)
4259             read-all)
4260            (t
4261             (condition-case ()
4262                 (cond
4263                  ((and (or (<= scored marked) (= scored number))
4264                        (numberp gnus-large-newsgroup)
4265                        (> number gnus-large-newsgroup))
4266                   (let ((input
4267                          (read-string
4268                           (format
4269                            "How many articles from %s (default %d): "
4270                            (gnus-limit-string gnus-newsgroup-name 35)
4271                            number))))
4272                     (if (string-match "^[ \t]*$" input) number input)))
4273                  ((and (> scored marked) (< scored number)
4274                        (> (- scored number) 20))
4275                   (let ((input
4276                          (read-string
4277                           (format "%s %s (%d scored, %d total): "
4278                                   "How many articles from"
4279                                   group scored number))))
4280                     (if (string-match "^[ \t]*$" input)
4281                         number input)))
4282                  (t number))
4283               (quit nil))))))
4284     (setq select (if (stringp select) (string-to-number select) select))
4285     (if (or (null select) (zerop select))
4286         select
4287       (if (and (not (zerop scored)) (<= (abs select) scored))
4288           (progn
4289             (setq articles (sort scored-list '<))
4290             (setq number (length articles)))
4291         (setq articles (copy-sequence articles)))
4292
4293       (when (< (abs select) number)
4294         (if (< select 0)
4295             ;; Select the N oldest articles.
4296             (setcdr (nthcdr (1- (abs select)) articles) nil)
4297           ;; Select the N most recent articles.
4298           (setq articles (nthcdr (- number select) articles))))
4299       (setq gnus-newsgroup-unselected
4300             (gnus-sorted-intersection
4301              gnus-newsgroup-unreads
4302              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4303       (when gnus-alter-articles-to-read-function
4304         (setq gnus-newsgroup-unreads
4305               (sort 
4306                (funcall gnus-alter-articles-to-read-function
4307                         gnus-newsgroup-name gnus-newsgroup-unreads)
4308                '<)))
4309       articles)))
4310
4311 (defun gnus-killed-articles (killed articles)
4312   (let (out)
4313     (while articles
4314       (when (inline (gnus-member-of-range (car articles) killed))
4315         (push (car articles) out))
4316       (setq articles (cdr articles)))
4317     out))
4318
4319 (defun gnus-uncompress-marks (marks)
4320   "Uncompress the mark ranges in MARKS."
4321   (let ((uncompressed '(score bookmark))
4322         out)
4323     (while marks
4324       (if (memq (caar marks) uncompressed)
4325           (push (car marks) out)
4326         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4327       (setq marks (cdr marks)))
4328     out))
4329
4330 (defun gnus-adjust-marked-articles (info)
4331   "Set all article lists and remove all marks that are no longer valid."
4332   (let* ((marked-lists (gnus-info-marks info))
4333          (active (gnus-active (gnus-info-group info)))
4334          (min (car active))
4335          (max (cdr active))
4336          (types gnus-article-mark-lists)
4337          (uncompressed '(score bookmark killed))
4338          marks var articles article mark)
4339
4340     (while marked-lists
4341       (setq marks (pop marked-lists))
4342       (set (setq var (intern (format "gnus-newsgroup-%s"
4343                                      (car (rassq (setq mark (car marks))
4344                                                  types)))))
4345            (if (memq (car marks) uncompressed) (cdr marks)
4346              (gnus-uncompress-range (cdr marks))))
4347
4348       (setq articles (symbol-value var))
4349
4350       ;; All articles have to be subsets of the active articles.
4351       (cond
4352        ;; Adjust "simple" lists.
4353        ((memq mark '(tick dormant expire reply save))
4354         (while articles
4355           (when (or (< (setq article (pop articles)) min) (> article max))
4356             (set var (delq article (symbol-value var))))))
4357        ;; Adjust assocs.
4358        ((memq mark uncompressed)
4359         (when (not (listp (cdr (symbol-value var))))
4360           (set var (list (symbol-value var))))
4361         (when (not (listp (cdr articles)))
4362           (setq articles (list articles)))
4363         (while articles
4364           (when (or (not (consp (setq article (pop articles))))
4365                     (< (car article) min)
4366                     (> (car article) max))
4367             (set var (delq article (symbol-value var))))))))))
4368
4369 (defun gnus-update-missing-marks (missing)
4370   "Go through the list of MISSING articles and remove them from the mark lists."
4371   (when missing
4372     (let ((types gnus-article-mark-lists)
4373           var m)
4374       ;; Go through all types.
4375       (while types
4376         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4377         (when (symbol-value var)
4378           ;; This list has articles.  So we delete all missing articles
4379           ;; from it.
4380           (setq m missing)
4381           (while m
4382             (set var (delq (pop m) (symbol-value var)))))))))
4383
4384 (defun gnus-update-marks ()
4385   "Enter the various lists of marked articles into the newsgroup info list."
4386   (let ((types gnus-article-mark-lists)
4387         (info (gnus-get-info gnus-newsgroup-name))
4388         (uncompressed '(score bookmark killed))
4389         type list newmarked symbol delta-marks)
4390     (when info
4391       ;; Add all marks lists to the list of marks lists.
4392       (while (setq type (pop types))
4393         (setq list (symbol-value
4394                     (setq symbol
4395                           (intern (format "gnus-newsgroup-%s"
4396                                           (car type))))))
4397
4398         (when list
4399           ;; Get rid of the entries of the articles that have the
4400           ;; default score.
4401           (when (and (eq (cdr type) 'score)
4402                      gnus-save-score
4403                      list)
4404             (let* ((arts list)
4405                    (prev (cons nil list))
4406                    (all prev))
4407               (while arts
4408                 (if (or (not (consp (car arts)))
4409                         (= (cdar arts) gnus-summary-default-score))
4410                     (setcdr prev (cdr arts))
4411                   (setq prev arts))
4412                 (setq arts (cdr arts)))
4413               (setq list (cdr all)))))
4414
4415         (unless (memq (cdr type) uncompressed)
4416           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4417        
4418         (when (gnus-check-backend-function
4419                'request-set-mark gnus-newsgroup-name)
4420           ;; propagate flags to server, with the following exceptions:
4421           ;; uncompressed:s are not proper flags (they are cons cells)
4422           ;; cache is a internal gnus flag
4423           ;; download are local to one gnus installation (well)
4424           ;; unsend are for nndraft groups only
4425           ;; xxx: generality of this?  this suits nnimap anyway
4426           (unless (memq (cdr type) (append '(cache download unsend)
4427                                            uncompressed))
4428             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4429                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4430                    (add (gnus-remove-from-range
4431                          (gnus-copy-sequence list) old)))
4432               (when add
4433                 (push (list add 'add (list (cdr type))) delta-marks))
4434               (when del
4435                 (push (list del 'del (list (cdr type))) delta-marks)))))
4436           
4437         (when list
4438           (push (cons (cdr type) list) newmarked)))
4439
4440       (when delta-marks
4441         (unless (gnus-check-group gnus-newsgroup-name)
4442           (error "Can't open server for %s" gnus-newsgroup-name))
4443         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4444           
4445       ;; Enter these new marks into the info of the group.
4446       (if (nthcdr 3 info)
4447           (setcar (nthcdr 3 info) newmarked)
4448         ;; Add the marks lists to the end of the info.
4449         (when newmarked
4450           (setcdr (nthcdr 2 info) (list newmarked))))
4451
4452       ;; Cut off the end of the info if there's nothing else there.
4453       (let ((i 5))
4454         (while (and (> i 2)
4455                     (not (nth i info)))
4456           (when (nthcdr (decf i) info)
4457             (setcdr (nthcdr i info) nil)))))))
4458
4459 (defun gnus-set-mode-line (where)
4460   "Set the mode line of the article or summary buffers.
4461 If WHERE is `summary', the summary mode line format will be used."
4462   ;; Is this mode line one we keep updated?
4463   (when (and (memq where gnus-updated-mode-lines)
4464              (symbol-value
4465               (intern (format "gnus-%s-mode-line-format-spec" where))))
4466     (let (mode-string)
4467       (save-excursion
4468         ;; We evaluate this in the summary buffer since these
4469         ;; variables are buffer-local to that buffer.
4470         (set-buffer gnus-summary-buffer)
4471         ;; We bind all these variables that are used in the `eval' form
4472         ;; below.
4473         (let* ((mformat (symbol-value
4474                          (intern
4475                           (format "gnus-%s-mode-line-format-spec" where))))
4476                (gnus-tmp-group-name (gnus-group-name-decode 
4477                                      gnus-newsgroup-name
4478                                      (gnus-group-name-charset 
4479                                       nil
4480                                       gnus-newsgroup-name)))
4481                (gnus-tmp-article-number (or gnus-current-article 0))
4482                (gnus-tmp-unread gnus-newsgroup-unreads)
4483                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4484                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4485                (gnus-tmp-unread-and-unselected
4486                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4487                             (zerop gnus-tmp-unselected))
4488                        "")
4489                       ((zerop gnus-tmp-unselected)
4490                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4491                       (t (format "{%d(+%d) more}"
4492                                  gnus-tmp-unread-and-unticked
4493                                  gnus-tmp-unselected))))
4494                (gnus-tmp-subject
4495                 (if (and gnus-current-headers
4496                          (vectorp gnus-current-headers))
4497                     (gnus-mode-string-quote
4498                      (mail-header-subject gnus-current-headers))
4499                   ""))
4500                bufname-length max-len
4501                gnus-tmp-header);; passed as argument to any user-format-funcs
4502           (setq mode-string (eval mformat))
4503           (setq bufname-length (if (string-match "%b" mode-string)
4504                                    (- (length
4505                                        (buffer-name
4506                                         (if (eq where 'summary)
4507                                             nil
4508                                           (get-buffer gnus-article-buffer))))
4509                                       2)
4510                                  0))
4511           (setq max-len (max 4 (if gnus-mode-non-string-length
4512                                    (- (window-width)
4513                                       gnus-mode-non-string-length
4514                                       bufname-length)
4515                                  (length mode-string))))
4516           ;; We might have to chop a bit of the string off...
4517           (when (> (length mode-string) max-len)
4518             (setq mode-string
4519                   (concat (truncate-string-to-width mode-string (- max-len 3))
4520                           "...")))
4521           ;; Pad the mode string a bit.
4522           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4523       ;; Update the mode line.
4524       (setq mode-line-buffer-identification
4525             (gnus-mode-line-buffer-identification (list mode-string)))
4526       (set-buffer-modified-p t))))
4527
4528 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4529   "Go through the HEADERS list and add all Xrefs to a hash table.
4530 The resulting hash table is returned, or nil if no Xrefs were found."
4531   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4532          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4533          (xref-hashtb (gnus-make-hashtable))
4534          start group entry number xrefs header)
4535     (while headers
4536       (setq header (pop headers))
4537       (when (and (setq xrefs (mail-header-xref header))
4538                  (not (memq (setq number (mail-header-number header))
4539                             unreads)))
4540         (setq start 0)
4541         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4542           (setq start (match-end 0))
4543           (setq group (if prefix
4544                           (concat prefix (substring xrefs (match-beginning 1)
4545                                                     (match-end 1)))
4546                         (substring xrefs (match-beginning 1) (match-end 1))))
4547           (setq number
4548                 (string-to-int (substring xrefs (match-beginning 2)
4549                                           (match-end 2))))
4550           (if (setq entry (gnus-gethash group xref-hashtb))
4551               (setcdr entry (cons number (cdr entry)))
4552             (gnus-sethash group (cons number nil) xref-hashtb)))))
4553     (and start xref-hashtb)))
4554
4555 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4556   "Look through all the headers and mark the Xrefs as read."
4557   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4558         name entry info xref-hashtb idlist method nth4)
4559     (save-excursion
4560       (set-buffer gnus-group-buffer)
4561       (when (setq xref-hashtb
4562                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4563         (mapatoms
4564          (lambda (group)
4565            (unless (string= from-newsgroup (setq name (symbol-name group)))
4566              (setq idlist (symbol-value group))
4567              ;; Dead groups are not updated.
4568              (and (prog1
4569                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4570                             info (nth 2 entry))
4571                     (when (stringp (setq nth4 (gnus-info-method info)))
4572                       (setq nth4 (gnus-server-to-method nth4))))
4573                   ;; Only do the xrefs if the group has the same
4574                   ;; select method as the group we have just read.
4575                   (or (gnus-methods-equal-p
4576                        nth4 (gnus-find-method-for-group from-newsgroup))
4577                       virtual
4578                       (equal nth4 (setq method (gnus-find-method-for-group
4579                                                 from-newsgroup)))
4580                       (and (equal (car nth4) (car method))
4581                            (equal (nth 1 nth4) (nth 1 method))))
4582                   gnus-use-cross-reference
4583                   (or (not (eq gnus-use-cross-reference t))
4584                       virtual
4585                       ;; Only do cross-references on subscribed
4586                       ;; groups, if that is what is wanted.
4587                       (<= (gnus-info-level info) gnus-level-subscribed))
4588                   (gnus-group-make-articles-read name idlist))))
4589          xref-hashtb)))))
4590
4591 (defun gnus-compute-read-articles (group articles)
4592   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4593          (info (nth 2 entry))
4594          (active (gnus-active group))
4595          ninfo)
4596     (when entry
4597       ;; First peel off all invalid article numbers.
4598       (when active
4599         (let ((ids articles)
4600               id first)
4601           (while (setq id (pop ids))
4602             (when (and first (> id (cdr active)))
4603               ;; We'll end up in this situation in one particular
4604               ;; obscure situation.  If you re-scan a group and get
4605               ;; a new article that is cross-posted to a different
4606               ;; group that has not been re-scanned, you might get
4607               ;; crossposted article that has a higher number than
4608               ;; Gnus believes possible.  So we re-activate this
4609               ;; group as well.  This might mean doing the
4610               ;; crossposting thingy will *increase* the number
4611               ;; of articles in some groups.  Tsk, tsk.
4612               (setq active (or (gnus-activate-group group) active)))
4613             (when (or (> id (cdr active))
4614                       (< id (car active)))
4615               (setq articles (delq id articles))))))
4616       ;; If the read list is nil, we init it.
4617       (if (and active
4618                (null (gnus-info-read info))
4619                (> (car active) 1))
4620           (setq ninfo (cons 1 (1- (car active))))
4621         (setq ninfo (gnus-info-read info)))
4622       ;; Then we add the read articles to the range.
4623       (gnus-add-to-range
4624        ninfo (setq articles (sort articles '<))))))
4625
4626 (defun gnus-group-make-articles-read (group articles)
4627   "Update the info of GROUP to say that ARTICLES are read."
4628   (let* ((num 0)
4629          (entry (gnus-gethash group gnus-newsrc-hashtb))
4630          (info (nth 2 entry))
4631          (active (gnus-active group))
4632          range)
4633     (when entry
4634       (setq range (gnus-compute-read-articles group articles))
4635       (save-excursion
4636         (set-buffer gnus-group-buffer)
4637         (gnus-undo-register
4638           `(progn
4639              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4640              (gnus-info-set-read ',info ',(gnus-info-read info))
4641              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4642              (gnus-group-update-group ,group t))))
4643       ;; Add the read articles to the range.
4644       (gnus-info-set-read info range)
4645       ;; Then we have to re-compute how many unread
4646       ;; articles there are in this group.
4647       (when active
4648         (cond
4649          ((not range)
4650           (setq num (- (1+ (cdr active)) (car active))))
4651          ((not (listp (cdr range)))
4652           (setq num (- (cdr active) (- (1+ (cdr range))
4653                                        (car range)))))
4654          (t
4655           (while range
4656             (if (numberp (car range))
4657                 (setq num (1+ num))
4658               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4659             (setq range (cdr range)))
4660           (setq num (- (cdr active) num))))
4661         ;; Update the number of unread articles.
4662         (setcar entry num)
4663         ;; Update the group buffer.
4664         (gnus-group-update-group group t)))))
4665
4666 (defvar gnus-newsgroup-none-id 0)
4667
4668 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4669   (let ((cur nntp-server-buffer)
4670         (dependencies
4671          (or dependencies
4672              (save-excursion (set-buffer gnus-summary-buffer)
4673                              gnus-newsgroup-dependencies)))
4674         headers id end ref
4675         (mail-parse-charset gnus-newsgroup-charset)
4676         (mail-parse-ignored-charsets
4677          (save-excursion (condition-case nil
4678                              (set-buffer gnus-summary-buffer)
4679                            (error))
4680                          gnus-newsgroup-ignored-charsets)))
4681     (save-excursion
4682       (set-buffer nntp-server-buffer)
4683       ;; Translate all TAB characters into SPACE characters.
4684       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4685       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4686       (gnus-run-hooks 'gnus-parse-headers-hook)
4687       (let ((case-fold-search t)
4688             in-reply-to header p lines chars)
4689         (goto-char (point-min))
4690         ;; Search to the beginning of the next header.  Error messages
4691         ;; do not begin with 2 or 3.
4692         (while (re-search-forward "^[23][0-9]+ " nil t)
4693           (setq id nil
4694                 ref nil)
4695           ;; This implementation of this function, with nine
4696           ;; search-forwards instead of the one re-search-forward and
4697           ;; a case (which basically was the old function) is actually
4698           ;; about twice as fast, even though it looks messier.  You
4699           ;; can't have everything, I guess.  Speed and elegance
4700           ;; doesn't always go hand in hand.
4701           (setq
4702            header
4703            (vector
4704             ;; Number.
4705             (prog1
4706                 (read cur)
4707               (end-of-line)
4708               (setq p (point))
4709               (narrow-to-region (point)
4710                                 (or (and (search-forward "\n.\n" nil t)
4711                                          (- (point) 2))
4712                                     (point))))
4713             ;; Subject.
4714             (progn
4715               (goto-char p)
4716               (if (search-forward "\nsubject: " nil t)
4717                   (funcall gnus-decode-encoded-word-function
4718                            (nnheader-header-value))
4719                 "(none)"))
4720             ;; From.
4721             (progn
4722               (goto-char p)
4723               (if (search-forward "\nfrom: " nil t)
4724                   (funcall gnus-decode-encoded-word-function
4725                            (nnheader-header-value))
4726                 "(nobody)"))
4727             ;; Date.
4728             (progn
4729               (goto-char p)
4730               (if (search-forward "\ndate: " nil t)
4731                   (nnheader-header-value) ""))
4732             ;; Message-ID.
4733             (progn
4734               (goto-char p)
4735               (setq id (if (re-search-forward
4736                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4737                            ;; We do it this way to make sure the Message-ID
4738                            ;; is (somewhat) syntactically valid.
4739                            (buffer-substring (match-beginning 1)
4740                                              (match-end 1))
4741                          ;; If there was no message-id, we just fake one
4742                          ;; to make subsequent routines simpler.
4743                          (nnheader-generate-fake-message-id))))
4744             ;; References.
4745             (progn
4746               (goto-char p)
4747               (if (search-forward "\nreferences: " nil t)
4748                   (progn
4749                     (setq end (point))
4750                     (prog1
4751                         (nnheader-header-value)
4752                       (setq ref
4753                             (buffer-substring
4754                              (progn
4755                                (end-of-line)
4756                                (search-backward ">" end t)
4757                                (1+ (point)))
4758                              (progn
4759                                (search-backward "<" end t)
4760                                (point))))))
4761                 ;; Get the references from the in-reply-to header if there
4762                 ;; were no references and the in-reply-to header looks
4763                 ;; promising.
4764                 (if (and (search-forward "\nin-reply-to: " nil t)
4765                          (setq in-reply-to (nnheader-header-value))
4766                          (string-match "<[^>]+>" in-reply-to))
4767                     (let (ref2)
4768                       (setq ref (substring in-reply-to (match-beginning 0)
4769                                            (match-end 0)))
4770                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4771                         (setq ref2 (substring in-reply-to (match-beginning 0)
4772                                               (match-end 0)))
4773                         (when (> (length ref2) (length ref))
4774                           (setq ref ref2)))
4775                       ref)
4776                   (setq ref nil))))
4777             ;; Chars.
4778             (progn
4779               (goto-char p)
4780               (if (search-forward "\nchars: " nil t)
4781                   (if (numberp (setq chars (ignore-errors (read cur))))
4782                       chars 0)
4783                 0))
4784             ;; Lines.
4785             (progn
4786               (goto-char p)
4787               (if (search-forward "\nlines: " nil t)
4788                   (if (numberp (setq lines (ignore-errors (read cur))))
4789                       lines 0)
4790                 0))
4791             ;; Xref.
4792             (progn
4793               (goto-char p)
4794               (and (search-forward "\nxref: " nil t)
4795                    (nnheader-header-value)))
4796             ;; Extra.
4797             (when gnus-extra-headers
4798               (let ((extra gnus-extra-headers)
4799                     out)
4800                 (while extra
4801                   (goto-char p)
4802                   (when (search-forward
4803                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4804                     (push (cons (car extra) (nnheader-header-value))
4805                           out))
4806                   (pop extra))
4807                 out))))
4808           (when (equal id ref)
4809             (setq ref nil))
4810
4811           (when gnus-alter-header-function
4812             (funcall gnus-alter-header-function header)
4813             (setq id (mail-header-id header)
4814                   ref (gnus-parent-id (mail-header-references header))))
4815
4816           (when (setq header
4817                       (gnus-dependencies-add-header
4818                        header dependencies force-new))
4819             (push header headers))
4820           (goto-char (point-max))
4821           (widen))
4822         (nreverse headers)))))
4823
4824 ;; Goes through the xover lines and returns a list of vectors
4825 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4826                                                   force-new dependencies
4827                                                   group also-fetch-heads)
4828   "Parse the news overview data in the server buffer.
4829 Return a list of headers that match SEQUENCE (see
4830 `nntp-retrieve-headers')."
4831   ;; Get the Xref when the users reads the articles since most/some
4832   ;; NNTP servers do not include Xrefs when using XOVER.
4833   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4834   (let ((mail-parse-charset gnus-newsgroup-charset)
4835         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4836         (cur nntp-server-buffer)
4837         (dependencies (or dependencies gnus-newsgroup-dependencies))
4838         number headers header)
4839     (save-excursion
4840       (set-buffer nntp-server-buffer)
4841       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4842       ;; Allow the user to mangle the headers before parsing them.
4843       (gnus-run-hooks 'gnus-parse-headers-hook)
4844       (goto-char (point-min))
4845       (while (not (eobp))
4846         (condition-case ()
4847             (while (and sequence (not (eobp)))
4848               (setq number (read cur))
4849               (while (and sequence
4850                           (< (car sequence) number))
4851                 (setq sequence (cdr sequence)))
4852               (and sequence
4853                    (eq number (car sequence))
4854                    (progn
4855                      (setq sequence (cdr sequence))
4856                      (setq header (inline
4857                                     (gnus-nov-parse-line
4858                                      number dependencies force-new))))
4859                    (push header headers))
4860               (forward-line 1))
4861           (error
4862            (gnus-error 4 "Strange nov line (%d)"
4863                        (count-lines (point-min) (point)))))
4864         (forward-line 1))
4865       ;; A common bug in inn is that if you have posted an article and
4866       ;; then retrieves the active file, it will answer correctly --
4867       ;; the new article is included.  However, a NOV entry for the
4868       ;; article may not have been generated yet, so this may fail.
4869       ;; We work around this problem by retrieving the last few
4870       ;; headers using HEAD.
4871       (if (or (not also-fetch-heads)
4872               (not sequence))
4873           ;; We (probably) got all the headers.
4874           (nreverse headers)
4875         (let ((gnus-nov-is-evil t))
4876           (nconc
4877            (nreverse headers)
4878            (when (gnus-retrieve-headers sequence group)
4879              (gnus-get-newsgroup-headers))))))))
4880
4881 (defun gnus-article-get-xrefs ()
4882   "Fill in the Xref value in `gnus-current-headers', if necessary.
4883 This is meant to be called in `gnus-article-internal-prepare-hook'."
4884   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4885                                  gnus-current-headers)))
4886     (or (not gnus-use-cross-reference)
4887         (not headers)
4888         (and (mail-header-xref headers)
4889              (not (string= (mail-header-xref headers) "")))
4890         (let ((case-fold-search t)
4891               xref)
4892           (save-restriction
4893             (nnheader-narrow-to-headers)
4894             (goto-char (point-min))
4895             (when (or (and (not (eobp))
4896                            (eq (downcase (char-after)) ?x)
4897                            (looking-at "Xref:"))
4898                       (search-forward "\nXref:" nil t))
4899               (goto-char (1+ (match-end 0)))
4900               (setq xref (buffer-substring (point)
4901                                            (progn (end-of-line) (point))))
4902               (mail-header-set-xref headers xref)))))))
4903
4904 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4905   "Find article ID and insert the summary line for that article.
4906 OLD-HEADER can either be a header or a line number to insert
4907 the subject line on."
4908   (let* ((line (and (numberp old-header) old-header))
4909          (old-header (and (vectorp old-header) old-header))
4910          (header (cond ((and old-header use-old-header)
4911                         old-header)
4912                        ((and (numberp id)
4913                              (gnus-number-to-header id))
4914                         (gnus-number-to-header id))
4915                        (t
4916                         (gnus-read-header id))))
4917          (number (and (numberp id) id))
4918          d)
4919     (when header
4920       ;; Rebuild the thread that this article is part of and go to the
4921       ;; article we have fetched.
4922       (when (and (not gnus-show-threads)
4923                  old-header)
4924         (when (and number
4925                    (setq d (gnus-data-find (mail-header-number old-header))))
4926           (goto-char (gnus-data-pos d))
4927           (gnus-data-remove
4928            number
4929            (- (gnus-point-at-bol)
4930               (prog1
4931                   (1+ (gnus-point-at-eol))
4932                 (gnus-delete-line))))))
4933       (when old-header
4934         (mail-header-set-number header (mail-header-number old-header)))
4935       (setq gnus-newsgroup-sparse
4936             (delq (setq number (mail-header-number header))
4937                   gnus-newsgroup-sparse))
4938       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4939       (push number gnus-newsgroup-limit)
4940       (gnus-rebuild-thread (mail-header-id header) line)
4941       (gnus-summary-goto-subject number nil t))
4942     (when (and (numberp number)
4943                (> number 0))
4944       ;; We have to update the boundaries even if we can't fetch the
4945       ;; article if ID is a number -- so that the next `P' or `N'
4946       ;; command will fetch the previous (or next) article even
4947       ;; if the one we tried to fetch this time has been canceled.
4948       (when (> number gnus-newsgroup-end)
4949         (setq gnus-newsgroup-end number))
4950       (when (< number gnus-newsgroup-begin)
4951         (setq gnus-newsgroup-begin number))
4952       (setq gnus-newsgroup-unselected
4953             (delq number gnus-newsgroup-unselected)))
4954     ;; Report back a success?
4955     (and header (mail-header-number header))))
4956
4957 ;;; Process/prefix in the summary buffer
4958
4959 (defun gnus-summary-work-articles (n)
4960   "Return a list of articles to be worked upon.
4961 The prefix argument, the list of process marked articles, and the
4962 current article will be taken into consideration."
4963   (save-excursion
4964     (set-buffer gnus-summary-buffer)
4965     (cond
4966      (n
4967       ;; A numerical prefix has been given.
4968       (setq n (prefix-numeric-value n))
4969       (let ((backward (< n 0))
4970             (n (abs (prefix-numeric-value n)))
4971             articles article)
4972         (save-excursion
4973           (while
4974               (and (> n 0)
4975                    (push (setq article (gnus-summary-article-number))
4976                          articles)
4977                    (if backward
4978                        (gnus-summary-find-prev nil article)
4979                      (gnus-summary-find-next nil article)))
4980             (decf n)))
4981         (nreverse articles)))
4982      ((and (gnus-region-active-p) (mark))
4983       (message "region active")
4984       ;; Work on the region between point and mark.
4985       (let ((max (max (point) (mark)))
4986             articles article)
4987         (save-excursion
4988           (goto-char (min (min (point) (mark))))
4989           (while
4990               (and
4991                (push (setq article (gnus-summary-article-number)) articles)
4992                (gnus-summary-find-next nil article)
4993                (< (point) max)))
4994           (nreverse articles))))
4995      (gnus-newsgroup-processable
4996       ;; There are process-marked articles present.
4997       ;; Save current state.
4998       (gnus-summary-save-process-mark)
4999       ;; Return the list.
5000       (reverse gnus-newsgroup-processable))
5001      (t
5002       ;; Just return the current article.
5003       (list (gnus-summary-article-number))))))
5004
5005 (defmacro gnus-summary-iterate (arg &rest forms)
5006   "Iterate over the process/prefixed articles and do FORMS.
5007 ARG is the interactive prefix given to the command.  FORMS will be
5008 executed with point over the summary line of the articles."
5009   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5010     `(let ((,articles (gnus-summary-work-articles ,arg)))
5011        (while ,articles
5012          (gnus-summary-goto-subject (car ,articles))
5013          ,@forms
5014          (pop ,articles)))))
5015
5016 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5017 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5018
5019 (defun gnus-summary-save-process-mark ()
5020   "Push the current set of process marked articles on the stack."
5021   (interactive)
5022   (push (copy-sequence gnus-newsgroup-processable)
5023         gnus-newsgroup-process-stack))
5024
5025 (defun gnus-summary-kill-process-mark ()
5026   "Push the current set of process marked articles on the stack and unmark."
5027   (interactive)
5028   (gnus-summary-save-process-mark)
5029   (gnus-summary-unmark-all-processable))
5030
5031 (defun gnus-summary-yank-process-mark ()
5032   "Pop the last process mark state off the stack and restore it."
5033   (interactive)
5034   (unless gnus-newsgroup-process-stack
5035     (error "Empty mark stack"))
5036   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5037
5038 (defun gnus-summary-process-mark-set (set)
5039   "Make SET into the current process marked articles."
5040   (gnus-summary-unmark-all-processable)
5041   (while set
5042     (gnus-summary-set-process-mark (pop set))))
5043
5044 ;;; Searching and stuff
5045
5046 (defun gnus-summary-search-group (&optional backward use-level)
5047   "Search for next unread newsgroup.
5048 If optional argument BACKWARD is non-nil, search backward instead."
5049   (save-excursion
5050     (set-buffer gnus-group-buffer)
5051     (when (gnus-group-search-forward
5052            backward nil (if use-level (gnus-group-group-level) nil))
5053       (gnus-group-group-name))))
5054
5055 (defun gnus-summary-best-group (&optional exclude-group)
5056   "Find the name of the best unread group.
5057 If EXCLUDE-GROUP, do not go to this group."
5058   (save-excursion
5059     (set-buffer gnus-group-buffer)
5060     (save-excursion
5061       (gnus-group-best-unread-group exclude-group))))
5062
5063 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5064   (if backward (gnus-summary-find-prev)
5065     (let* ((dummy (gnus-summary-article-intangible-p))
5066            (article (or article (gnus-summary-article-number)))
5067            (arts (gnus-data-find-list article))
5068            result)
5069       (when (and (not dummy)
5070                  (or (not gnus-summary-check-current)
5071                      (not unread)
5072                      (not (gnus-data-unread-p (car arts)))))
5073         (setq arts (cdr arts)))
5074       (when (setq result
5075                   (if unread
5076                       (progn
5077                         (while arts
5078                           (when (or (and undownloaded
5079                                          (eq gnus-undownloaded-mark
5080                                              (gnus-data-mark (car arts))))
5081                                     (gnus-data-unread-p (car arts)))
5082                             (setq result (car arts)
5083                                   arts nil))
5084                           (setq arts (cdr arts)))
5085                         result)
5086                     (car arts)))
5087         (goto-char (gnus-data-pos result))
5088         (gnus-data-number result)))))
5089
5090 (defun gnus-summary-find-prev (&optional unread article)
5091   (let* ((eobp (eobp))
5092          (article (or article (gnus-summary-article-number)))
5093          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5094          result)
5095     (when (and (not eobp)
5096                (or (not gnus-summary-check-current)
5097                    (not unread)
5098                    (not (gnus-data-unread-p (car arts)))))
5099       (setq arts (cdr arts)))
5100     (when (setq result
5101                 (if unread
5102                     (progn
5103                       (while arts
5104                         (when (gnus-data-unread-p (car arts))
5105                           (setq result (car arts)
5106                                 arts nil))
5107                         (setq arts (cdr arts)))
5108                       result)
5109                   (car arts)))
5110       (goto-char (gnus-data-pos result))
5111       (gnus-data-number result))))
5112
5113 (defun gnus-summary-find-subject (subject &optional unread backward article)
5114   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5115          (article (or article (gnus-summary-article-number)))
5116          (articles (gnus-data-list backward))
5117          (arts (gnus-data-find-list article articles))
5118          result)
5119     (when (or (not gnus-summary-check-current)
5120               (not unread)
5121               (not (gnus-data-unread-p (car arts))))
5122       (setq arts (cdr arts)))
5123     (while arts
5124       (and (or (not unread)
5125                (gnus-data-unread-p (car arts)))
5126            (vectorp (gnus-data-header (car arts)))
5127            (gnus-subject-equal
5128             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5129            (setq result (car arts)
5130                  arts nil))
5131       (setq arts (cdr arts)))
5132     (and result
5133          (goto-char (gnus-data-pos result))
5134          (gnus-data-number result))))
5135
5136 (defun gnus-summary-search-forward (&optional unread subject backward)
5137   "Search forward for an article.
5138 If UNREAD, look for unread articles.  If SUBJECT, look for
5139 articles with that subject.  If BACKWARD, search backward instead."
5140   (cond (subject (gnus-summary-find-subject subject unread backward))
5141         (backward (gnus-summary-find-prev unread))
5142         (t (gnus-summary-find-next unread))))
5143
5144 (defun gnus-recenter (&optional n)
5145   "Center point in window and redisplay frame.
5146 Also do horizontal recentering."
5147   (interactive "P")
5148   (when (and gnus-auto-center-summary
5149              (not (eq gnus-auto-center-summary 'vertical)))
5150     (gnus-horizontal-recenter))
5151   (recenter n))
5152
5153 (defun gnus-summary-recenter ()
5154   "Center point in the summary window.
5155 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5156 displayed, no centering will be performed."
5157   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5158   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5159   (interactive)
5160   (let* ((top (cond ((< (window-height) 4) 0)
5161                     ((< (window-height) 7) 1)
5162                     (t (if (numberp gnus-auto-center-summary)
5163                            gnus-auto-center-summary
5164                          2))))
5165          (height (1- (window-height)))
5166          (bottom (save-excursion (goto-char (point-max))
5167                                  (forward-line (- height))
5168                                  (point)))
5169          (window (get-buffer-window (current-buffer))))
5170     ;; The user has to want it.
5171     (when gnus-auto-center-summary
5172       (when (get-buffer-window gnus-article-buffer)
5173         ;; Only do recentering when the article buffer is displayed,
5174         ;; Set the window start to either `bottom', which is the biggest
5175         ;; possible valid number, or the second line from the top,
5176         ;; whichever is the least.
5177         (set-window-start
5178          window (min bottom (save-excursion
5179                               (forward-line (- top)) (point)))
5180          t))
5181       ;; Do horizontal recentering while we're at it.
5182       (when (and (get-buffer-window (current-buffer) t)
5183                  (not (eq gnus-auto-center-summary 'vertical)))
5184         (let ((selected (selected-window)))
5185           (select-window (get-buffer-window (current-buffer) t))
5186           (gnus-summary-position-point)
5187           (gnus-horizontal-recenter)
5188           (select-window selected))))))
5189
5190 (defun gnus-summary-jump-to-group (newsgroup)
5191   "Move point to NEWSGROUP in group mode buffer."
5192   ;; Keep update point of group mode buffer if visible.
5193   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5194       (save-window-excursion
5195         ;; Take care of tree window mode.
5196         (when (get-buffer-window gnus-group-buffer)
5197           (pop-to-buffer gnus-group-buffer))
5198         (gnus-group-jump-to-group newsgroup))
5199     (save-excursion
5200       ;; Take care of tree window mode.
5201       (if (get-buffer-window gnus-group-buffer)
5202           (pop-to-buffer gnus-group-buffer)
5203         (set-buffer gnus-group-buffer))
5204       (gnus-group-jump-to-group newsgroup))))
5205
5206 ;; This function returns a list of article numbers based on the
5207 ;; difference between the ranges of read articles in this group and
5208 ;; the range of active articles.
5209 (defun gnus-list-of-unread-articles (group)
5210   (let* ((read (gnus-info-read (gnus-get-info group)))
5211          (active (or (gnus-active group) (gnus-activate-group group)))
5212          (last (cdr active))
5213          first nlast unread)
5214     ;; If none are read, then all are unread.
5215     (if (not read)
5216         (setq first (car active))
5217       ;; If the range of read articles is a single range, then the
5218       ;; first unread article is the article after the last read
5219       ;; article.  Sounds logical, doesn't it?
5220       (if (and (not (listp (cdr read)))
5221                (or (< (car read) (car active))
5222                    (progn (setq read (list read))
5223                           nil)))
5224           (setq first (max (car active) (1+ (cdr read))))
5225         ;; `read' is a list of ranges.
5226         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5227                                   (caar read)))
5228                   1)
5229           (setq first (car active)))
5230         (while read
5231           (when first
5232             (while (< first nlast)
5233               (push first unread)
5234               (setq first (1+ first))))
5235           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5236           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5237           (setq read (cdr read)))))
5238     ;; And add the last unread articles.
5239     (while (<= first last)
5240       (push first unread)
5241       (setq first (1+ first)))
5242     ;; Return the list of unread articles.
5243     (delq 0 (nreverse unread))))
5244
5245 (defun gnus-list-of-read-articles (group)
5246   "Return a list of unread, unticked and non-dormant articles."
5247   (let* ((info (gnus-get-info group))
5248          (marked (gnus-info-marks info))
5249          (active (gnus-active group)))
5250     (and info active
5251          (gnus-set-difference
5252           (gnus-sorted-complement
5253            (gnus-uncompress-range active)
5254            (gnus-list-of-unread-articles group))
5255           (append
5256            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5257            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5258
5259 ;; Various summary commands
5260
5261 (defun gnus-summary-select-article-buffer ()
5262   "Reconfigure windows to show article buffer."
5263   (interactive)
5264   (if (not (gnus-buffer-live-p gnus-article-buffer))
5265       (error "There is no article buffer for this summary buffer")
5266     (gnus-configure-windows 'article)
5267     (select-window (get-buffer-window gnus-article-buffer))))
5268
5269 (defun gnus-summary-universal-argument (arg)
5270   "Perform any operation on all articles that are process/prefixed."
5271   (interactive "P")
5272   (let ((articles (gnus-summary-work-articles arg))
5273         func article)
5274     (if (eq
5275          (setq
5276           func
5277           (key-binding
5278            (read-key-sequence
5279             (substitute-command-keys
5280              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5281          'undefined)
5282         (gnus-error 1 "Undefined key")
5283       (save-excursion
5284         (while articles
5285           (gnus-summary-goto-subject (setq article (pop articles)))
5286           (let (gnus-newsgroup-processable)
5287             (command-execute func))
5288           (gnus-summary-remove-process-mark article)))))
5289   (gnus-summary-position-point))
5290
5291 (defun gnus-summary-toggle-truncation (&optional arg)
5292   "Toggle truncation of summary lines.
5293 With arg, turn line truncation on iff arg is positive."
5294   (interactive "P")
5295   (setq truncate-lines
5296         (if (null arg) (not truncate-lines)
5297           (> (prefix-numeric-value arg) 0)))
5298   (redraw-display))
5299
5300 (defun gnus-summary-reselect-current-group (&optional all rescan)
5301   "Exit and then reselect the current newsgroup.
5302 The prefix argument ALL means to select all articles."
5303   (interactive "P")
5304   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5305     (error "Ephemeral groups can't be reselected"))
5306   (let ((current-subject (gnus-summary-article-number))
5307         (group gnus-newsgroup-name))
5308     (setq gnus-newsgroup-begin nil)
5309     (gnus-summary-exit)
5310     ;; We have to adjust the point of group mode buffer because
5311     ;; point was moved to the next unread newsgroup by exiting.
5312     (gnus-summary-jump-to-group group)
5313     (when rescan
5314       (save-excursion
5315         (gnus-group-get-new-news-this-group 1)))
5316     (gnus-group-read-group all t)
5317     (gnus-summary-goto-subject current-subject nil t)))
5318
5319 (defun gnus-summary-rescan-group (&optional all)
5320   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5321   (interactive "P")
5322   (gnus-summary-reselect-current-group all t))
5323
5324 (defun gnus-summary-update-info (&optional non-destructive)
5325   (save-excursion
5326     (let ((group gnus-newsgroup-name))
5327       (when group
5328         (when gnus-newsgroup-kill-headers
5329           (setq gnus-newsgroup-killed
5330                 (gnus-compress-sequence
5331                  (nconc
5332                   (gnus-set-sorted-intersection
5333                    (gnus-uncompress-range gnus-newsgroup-killed)
5334                    (setq gnus-newsgroup-unselected
5335                          (sort gnus-newsgroup-unselected '<)))
5336                   (setq gnus-newsgroup-unreads
5337                         (sort gnus-newsgroup-unreads '<)))
5338                  t)))
5339         (unless (listp (cdr gnus-newsgroup-killed))
5340           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5341         (let ((headers gnus-newsgroup-headers))
5342           ;; Set the new ranges of read articles.
5343           (save-excursion
5344             (set-buffer gnus-group-buffer)
5345             (gnus-undo-force-boundary))
5346           (gnus-update-read-articles
5347            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5348           ;; Set the current article marks.
5349           (let ((gnus-newsgroup-scored
5350                  (if (and (not gnus-save-score)
5351                           (not non-destructive))
5352                      nil
5353                    gnus-newsgroup-scored)))
5354             (save-excursion
5355               (gnus-update-marks)))
5356           ;; Do the cross-ref thing.
5357           (when gnus-use-cross-reference
5358             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5359           ;; Do not switch windows but change the buffer to work.
5360           (set-buffer gnus-group-buffer)
5361           (unless (gnus-ephemeral-group-p group)
5362             (gnus-group-update-group group)))))))
5363
5364 (defun gnus-summary-save-newsrc (&optional force)
5365   "Save the current number of read/marked articles in the dribble buffer.
5366 The dribble buffer will then be saved.
5367 If FORCE (the prefix), also save the .newsrc file(s)."
5368   (interactive "P")
5369   (gnus-summary-update-info t)
5370   (if force
5371       (gnus-save-newsrc-file)
5372     (gnus-dribble-save)))
5373
5374 (defun gnus-summary-exit (&optional temporary)
5375   "Exit reading current newsgroup, and then return to group selection mode.
5376 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5377   (interactive)
5378   (gnus-set-global-variables)
5379   (when (gnus-buffer-live-p gnus-article-buffer)
5380     (save-excursion
5381       (set-buffer gnus-article-buffer)
5382       (mm-destroy-parts gnus-article-mime-handles)))
5383   (gnus-kill-save-kill-buffer)
5384   (gnus-async-halt-prefetch)
5385   (let* ((group gnus-newsgroup-name)
5386          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5387          (mode major-mode)
5388          (group-point nil)
5389          (buf (current-buffer)))
5390     (unless quit-config
5391       ;; Do adaptive scoring, and possibly save score files.
5392       (when gnus-newsgroup-adaptive
5393         (gnus-score-adaptive))
5394       (when gnus-use-scoring
5395         (gnus-score-save)))
5396     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5397     ;; If we have several article buffers, we kill them at exit.
5398     (unless gnus-single-article-buffer
5399       (gnus-kill-buffer gnus-original-article-buffer)
5400       (setq gnus-article-current nil))
5401     (when gnus-use-cache
5402       (gnus-cache-possibly-remove-articles)
5403       (gnus-cache-save-buffers))
5404     (gnus-async-prefetch-remove-group group)
5405     (when gnus-suppress-duplicates
5406       (gnus-dup-enter-articles))
5407     (when gnus-use-trees
5408       (gnus-tree-close group))
5409     (when gnus-use-cache
5410       (gnus-cache-write-active))
5411     ;; Remove entries for this group.
5412     (nnmail-purge-split-history (gnus-group-real-name group))
5413     ;; Make all changes in this group permanent.
5414     (unless quit-config
5415       (gnus-run-hooks 'gnus-exit-group-hook)
5416       (gnus-summary-update-info))
5417     (gnus-close-group group)
5418     ;; Make sure where we were, and go to next newsgroup.
5419     (set-buffer gnus-group-buffer)
5420     (unless quit-config
5421       (gnus-group-jump-to-group group))
5422     (gnus-run-hooks 'gnus-summary-exit-hook)
5423     (unless (or quit-config
5424                 ;; If this group has disappeared from the summary
5425                 ;; buffer, don't skip forwards.
5426                 (not (string= group (gnus-group-group-name))))
5427       (gnus-group-next-unread-group 1))
5428     (setq group-point (point))
5429     (if temporary
5430         nil                             ;Nothing to do.
5431       ;; If we have several article buffers, we kill them at exit.
5432       (unless gnus-single-article-buffer
5433         (gnus-kill-buffer gnus-article-buffer)
5434         (gnus-kill-buffer gnus-original-article-buffer)
5435         (setq gnus-article-current nil))
5436       (set-buffer buf)
5437       (if (not gnus-kill-summary-on-exit)
5438           (gnus-deaden-summary)
5439         ;; We set all buffer-local variables to nil.  It is unclear why
5440         ;; this is needed, but if we don't, buffer-local variables are
5441         ;; not garbage-collected, it seems.  This would the lead to en
5442         ;; ever-growing Emacs.
5443         (gnus-summary-clear-local-variables)
5444         (when (get-buffer gnus-article-buffer)
5445           (bury-buffer gnus-article-buffer))
5446         ;; We clear the global counterparts of the buffer-local
5447         ;; variables as well, just to be on the safe side.
5448         (set-buffer gnus-group-buffer)
5449         (gnus-summary-clear-local-variables)
5450         ;; Return to group mode buffer.
5451         (when (eq mode 'gnus-summary-mode)
5452           (gnus-kill-buffer buf)))
5453       (setq gnus-current-select-method gnus-select-method)
5454       (pop-to-buffer gnus-group-buffer)
5455       (if (not quit-config)
5456           (progn
5457             (goto-char group-point)
5458             (gnus-configure-windows 'group 'force))
5459         (gnus-handle-ephemeral-exit quit-config))
5460       ;; Clear the current group name.
5461       (unless quit-config
5462         (setq gnus-newsgroup-name nil)))))
5463
5464 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5465 (defun gnus-summary-exit-no-update (&optional no-questions)
5466   "Quit reading current newsgroup without updating read article info."
5467   (interactive)
5468   (let* ((group gnus-newsgroup-name)
5469          (quit-config (gnus-group-quit-config group)))
5470     (when (or no-questions
5471               gnus-expert-user
5472               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5473       (gnus-async-halt-prefetch)
5474       (mapcar 'funcall
5475               (delq 'gnus-summary-expire-articles
5476                     (copy-sequence gnus-summary-prepare-exit-hook)))
5477       (when (gnus-buffer-live-p gnus-article-buffer)
5478         (save-excursion
5479           (set-buffer gnus-article-buffer)
5480           (mm-destroy-parts gnus-article-mime-handles)))
5481       ;; If we have several article buffers, we kill them at exit.
5482       (unless gnus-single-article-buffer
5483         (gnus-kill-buffer gnus-article-buffer)
5484         (gnus-kill-buffer gnus-original-article-buffer)
5485         (setq gnus-article-current nil))
5486       (if (not gnus-kill-summary-on-exit)
5487           (gnus-deaden-summary)
5488         (gnus-close-group group)
5489         (gnus-summary-clear-local-variables)
5490         (set-buffer gnus-group-buffer)
5491         (gnus-summary-clear-local-variables)
5492         (when (get-buffer gnus-summary-buffer)
5493           (kill-buffer gnus-summary-buffer)))
5494       (unless gnus-single-article-buffer
5495         (setq gnus-article-current nil))
5496       (when gnus-use-trees
5497         (gnus-tree-close group))
5498       (gnus-async-prefetch-remove-group group)
5499       (when (get-buffer gnus-article-buffer)
5500         (bury-buffer gnus-article-buffer))
5501       ;; Return to the group buffer.
5502       (gnus-configure-windows 'group 'force)
5503       ;; Clear the current group name.
5504       (setq gnus-newsgroup-name nil)
5505       (when (equal (gnus-group-group-name) group)
5506         (gnus-group-next-unread-group 1))
5507       (when quit-config
5508         (gnus-handle-ephemeral-exit quit-config)))))
5509
5510 (defun gnus-handle-ephemeral-exit (quit-config)
5511   "Handle movement when leaving an ephemeral group.
5512 The state which existed when entering the ephemeral is reset."
5513   (if (not (buffer-name (car quit-config)))
5514       (gnus-configure-windows 'group 'force)
5515     (set-buffer (car quit-config))
5516     (cond ((eq major-mode 'gnus-summary-mode)
5517            (gnus-set-global-variables))
5518           ((eq major-mode 'gnus-article-mode)
5519            (save-excursion
5520              ;; The `gnus-summary-buffer' variable may point
5521              ;; to the old summary buffer when using a single
5522              ;; article buffer.
5523              (unless (gnus-buffer-live-p gnus-summary-buffer)
5524                (set-buffer gnus-group-buffer))
5525              (set-buffer gnus-summary-buffer)
5526              (gnus-set-global-variables))))
5527     (if (or (eq (cdr quit-config) 'article)
5528             (eq (cdr quit-config) 'pick))
5529         (progn
5530           ;; The current article may be from the ephemeral group
5531           ;; thus it is best that we reload this article
5532           (gnus-summary-show-article)
5533           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5534               (gnus-configure-windows 'pick 'force)
5535             (gnus-configure-windows (cdr quit-config) 'force)))
5536       (gnus-configure-windows (cdr quit-config) 'force))
5537     (when (eq major-mode 'gnus-summary-mode)
5538       (gnus-summary-next-subject 1 nil t)
5539       (gnus-summary-recenter)
5540       (gnus-summary-position-point))))
5541
5542 ;;; Dead summaries.
5543
5544 (defvar gnus-dead-summary-mode-map nil)
5545
5546 (unless gnus-dead-summary-mode-map
5547   (setq gnus-dead-summary-mode-map (make-keymap))
5548   (suppress-keymap gnus-dead-summary-mode-map)
5549   (substitute-key-definition
5550    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5551   (let ((keys '("\C-d" "\r" "\177" [delete])))
5552     (while keys
5553       (define-key gnus-dead-summary-mode-map
5554         (pop keys) 'gnus-summary-wake-up-the-dead))))
5555
5556 (defvar gnus-dead-summary-mode nil
5557   "Minor mode for Gnus summary buffers.")
5558
5559 (defun gnus-dead-summary-mode (&optional arg)
5560   "Minor mode for Gnus summary buffers."
5561   (interactive "P")
5562   (when (eq major-mode 'gnus-summary-mode)
5563     (make-local-variable 'gnus-dead-summary-mode)
5564     (setq gnus-dead-summary-mode
5565           (if (null arg) (not gnus-dead-summary-mode)
5566             (> (prefix-numeric-value arg) 0)))
5567     (when gnus-dead-summary-mode
5568       (gnus-add-minor-mode
5569        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5570
5571 (defun gnus-deaden-summary ()
5572   "Make the current summary buffer into a dead summary buffer."
5573   ;; Kill any previous dead summary buffer.
5574   (when (and gnus-dead-summary
5575              (buffer-name gnus-dead-summary))
5576     (save-excursion
5577       (set-buffer gnus-dead-summary)
5578       (when gnus-dead-summary-mode
5579         (kill-buffer (current-buffer)))))
5580   ;; Make this the current dead summary.
5581   (setq gnus-dead-summary (current-buffer))
5582   (gnus-dead-summary-mode 1)
5583   (let ((name (buffer-name)))
5584     (when (string-match "Summary" name)
5585       (rename-buffer
5586        (concat (substring name 0 (match-beginning 0)) "Dead "
5587                (substring name (match-beginning 0)))
5588        t)
5589       (bury-buffer))))
5590
5591 (defun gnus-kill-or-deaden-summary (buffer)
5592   "Kill or deaden the summary BUFFER."
5593   (save-excursion
5594     (when (and (buffer-name buffer)
5595                (not gnus-single-article-buffer))
5596       (save-excursion
5597         (set-buffer buffer)
5598         (gnus-kill-buffer gnus-article-buffer)
5599         (gnus-kill-buffer gnus-original-article-buffer)))
5600     (cond (gnus-kill-summary-on-exit
5601            (when (and gnus-use-trees
5602                       (gnus-buffer-exists-p buffer))
5603              (save-excursion
5604                (set-buffer buffer)
5605                (gnus-tree-close gnus-newsgroup-name)))
5606            (gnus-kill-buffer buffer))
5607           ((gnus-buffer-exists-p buffer)
5608            (save-excursion
5609              (set-buffer buffer)
5610              (gnus-deaden-summary))))))
5611
5612 (defun gnus-summary-wake-up-the-dead (&rest args)
5613   "Wake up the dead summary buffer."
5614   (interactive)
5615   (gnus-dead-summary-mode -1)
5616   (let ((name (buffer-name)))
5617     (when (string-match "Dead " name)
5618       (rename-buffer
5619        (concat (substring name 0 (match-beginning 0))
5620                (substring name (match-end 0)))
5621        t)))
5622   (gnus-message 3 "This dead summary is now alive again"))
5623
5624 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5625 (defun gnus-summary-fetch-faq (&optional faq-dir)
5626   "Fetch the FAQ for the current group.
5627 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5628 in."
5629   (interactive
5630    (list
5631     (when current-prefix-arg
5632       (completing-read
5633        "Faq dir: " (and (listp gnus-group-faq-directory)
5634                         (mapcar (lambda (file) (list file))
5635                                 gnus-group-faq-directory))))))
5636   (let (gnus-faq-buffer)
5637     (when (setq gnus-faq-buffer
5638                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5639       (gnus-configure-windows 'summary-faq))))
5640
5641 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5642 (defun gnus-summary-describe-group (&optional force)
5643   "Describe the current newsgroup."
5644   (interactive "P")
5645   (gnus-group-describe-group force gnus-newsgroup-name))
5646
5647 (defun gnus-summary-describe-briefly ()
5648   "Describe summary mode commands briefly."
5649   (interactive)
5650   (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")))
5651
5652 ;; Walking around group mode buffer from summary mode.
5653
5654 (defun gnus-summary-next-group (&optional no-article target-group backward)
5655   "Exit current newsgroup and then select next unread newsgroup.
5656 If prefix argument NO-ARTICLE is non-nil, no article is selected
5657 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5658 previous group instead."
5659   (interactive "P")
5660   ;; Stop pre-fetching.
5661   (gnus-async-halt-prefetch)
5662   (let ((current-group gnus-newsgroup-name)
5663         (current-buffer (current-buffer))
5664         entered)
5665     ;; First we semi-exit this group to update Xrefs and all variables.
5666     ;; We can't do a real exit, because the window conf must remain
5667     ;; the same in case the user is prompted for info, and we don't
5668     ;; want the window conf to change before that...
5669     (gnus-summary-exit t)
5670     (while (not entered)
5671       ;; Then we find what group we are supposed to enter.
5672       (set-buffer gnus-group-buffer)
5673       (gnus-group-jump-to-group current-group)
5674       (setq target-group
5675             (or target-group
5676                 (if (eq gnus-keep-same-level 'best)
5677                     (gnus-summary-best-group gnus-newsgroup-name)
5678                   (gnus-summary-search-group backward gnus-keep-same-level))))
5679       (if (not target-group)
5680           ;; There are no further groups, so we return to the group
5681           ;; buffer.
5682           (progn
5683             (gnus-message 5 "Returning to the group buffer")
5684             (setq entered t)
5685             (when (gnus-buffer-live-p current-buffer)
5686               (set-buffer current-buffer)
5687               (gnus-summary-exit))
5688             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5689         ;; We try to enter the target group.
5690         (gnus-group-jump-to-group target-group)
5691         (let ((unreads (gnus-group-group-unread)))
5692           (if (and (or (eq t unreads)
5693                        (and unreads (not (zerop unreads))))
5694                    (gnus-summary-read-group
5695                     target-group nil no-article
5696                     (and (buffer-name current-buffer) current-buffer)
5697                     nil backward))
5698               (setq entered t)
5699             (setq current-group target-group
5700                   target-group nil)))))))
5701
5702 (defun gnus-summary-prev-group (&optional no-article)
5703   "Exit current newsgroup and then select previous unread newsgroup.
5704 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5705   (interactive "P")
5706   (gnus-summary-next-group no-article nil t))
5707
5708 ;; Walking around summary lines.
5709
5710 (defun gnus-summary-first-subject (&optional unread undownloaded)
5711   "Go to the first unread subject.
5712 If UNREAD is non-nil, go to the first unread article.
5713 Returns the article selected or nil if there are no unread articles."
5714   (interactive "P")
5715   (prog1
5716       (cond
5717        ;; Empty summary.
5718        ((null gnus-newsgroup-data)
5719         (gnus-message 3 "No articles in the group")
5720         nil)
5721        ;; Pick the first article.
5722        ((not unread)
5723         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5724         (gnus-data-number (car gnus-newsgroup-data)))
5725        ;; No unread articles.
5726        ((null gnus-newsgroup-unreads)
5727         (gnus-message 3 "No more unread articles")
5728         nil)
5729        ;; Find the first unread article.
5730        (t
5731         (let ((data gnus-newsgroup-data))
5732           (while (and data
5733                       (and (not (and undownloaded
5734                                      (eq gnus-undownloaded-mark
5735                                          (gnus-data-mark (car data)))))
5736                            (not (gnus-data-unread-p (car data)))))
5737             (setq data (cdr data)))
5738           (when data
5739             (goto-char (gnus-data-pos (car data)))
5740             (gnus-data-number (car data))))))
5741     (gnus-summary-position-point)))
5742
5743 (defun gnus-summary-next-subject (n &optional unread dont-display)
5744   "Go to next N'th summary line.
5745 If N is negative, go to the previous N'th subject line.
5746 If UNREAD is non-nil, only unread articles are selected.
5747 The difference between N and the actual number of steps taken is
5748 returned."
5749   (interactive "p")
5750   (let ((backward (< n 0))
5751         (n (abs n)))
5752     (while (and (> n 0)
5753                 (if backward
5754                     (gnus-summary-find-prev unread)
5755                   (gnus-summary-find-next unread)))
5756       (unless (zerop (setq n (1- n)))
5757         (gnus-summary-show-thread)))
5758     (when (/= 0 n)
5759       (gnus-message 7 "No more%s articles"
5760                     (if unread " unread" "")))
5761     (unless dont-display
5762       (gnus-summary-recenter)
5763       (gnus-summary-position-point))
5764     n))
5765
5766 (defun gnus-summary-next-unread-subject (n)
5767   "Go to next N'th unread summary line."
5768   (interactive "p")
5769   (gnus-summary-next-subject n t))
5770
5771 (defun gnus-summary-prev-subject (n &optional unread)
5772   "Go to previous N'th summary line.
5773 If optional argument UNREAD is non-nil, only unread article is selected."
5774   (interactive "p")
5775   (gnus-summary-next-subject (- n) unread))
5776
5777 (defun gnus-summary-prev-unread-subject (n)
5778   "Go to previous N'th unread summary line."
5779   (interactive "p")
5780   (gnus-summary-next-subject (- n) t))
5781
5782 (defun gnus-summary-goto-subject (article &optional force silent)
5783   "Go the subject line of ARTICLE.
5784 If FORCE, also allow jumping to articles not currently shown."
5785   (interactive "nArticle number: ")
5786   (let ((b (point))
5787         (data (gnus-data-find article)))
5788     ;; We read in the article if we have to.
5789     (and (not data)
5790          force
5791          (gnus-summary-insert-subject
5792           article
5793           (if (or (numberp force) (vectorp force)) force)
5794           t)
5795          (setq data (gnus-data-find article)))
5796     (goto-char b)
5797     (if (not data)
5798         (progn
5799           (unless silent
5800             (gnus-message 3 "Can't find article %d" article))
5801           nil)
5802       (goto-char (gnus-data-pos data))
5803       (gnus-summary-position-point)
5804       article)))
5805
5806 ;; Walking around summary lines with displaying articles.
5807
5808 (defun gnus-summary-expand-window (&optional arg)
5809   "Make the summary buffer take up the entire Emacs frame.
5810 Given a prefix, will force an `article' buffer configuration."
5811   (interactive "P")
5812   (if arg
5813       (gnus-configure-windows 'article 'force)
5814     (gnus-configure-windows 'summary 'force)))
5815
5816 (defun gnus-summary-display-article (article &optional all-header)
5817   "Display ARTICLE in article buffer."
5818   (gnus-set-global-variables)
5819   (if (null article)
5820       nil
5821     (prog1
5822         (if gnus-summary-display-article-function
5823             (funcall gnus-summary-display-article-function article all-header)
5824           (gnus-article-prepare article all-header))
5825       (gnus-run-hooks 'gnus-select-article-hook)
5826       (when (and gnus-current-article
5827                  (not (zerop gnus-current-article)))
5828         (gnus-summary-goto-subject gnus-current-article))
5829       (gnus-summary-recenter)
5830       (when (and gnus-use-trees gnus-show-threads)
5831         (gnus-possibly-generate-tree article)
5832         (gnus-highlight-selected-tree article))
5833       ;; Successfully display article.
5834       (gnus-article-set-window-start
5835        (cdr (assq article gnus-newsgroup-bookmarks))))))
5836
5837 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5838   "Select the current article.
5839 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5840 non-nil, the article will be re-fetched even if it already present in
5841 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5842 be displayed."
5843   ;; Make sure we are in the summary buffer to work around bbdb bug.
5844   (unless (eq major-mode 'gnus-summary-mode)
5845     (set-buffer gnus-summary-buffer))
5846   (let ((article (or article (gnus-summary-article-number)))
5847         (all-headers (not (not all-headers))) ;Must be T or NIL.
5848         gnus-summary-display-article-function)
5849     (and (not pseudo)
5850          (gnus-summary-article-pseudo-p article)
5851          (error "This is a pseudo-article"))
5852     (save-excursion
5853       (set-buffer gnus-summary-buffer)
5854       (if (or (and gnus-single-article-buffer
5855                    (or (null gnus-current-article)
5856                        (null gnus-article-current)
5857                        (null (get-buffer gnus-article-buffer))
5858                        (not (eq article (cdr gnus-article-current)))
5859                        (not (equal (car gnus-article-current)
5860                                    gnus-newsgroup-name))))
5861               (and (not gnus-single-article-buffer)
5862                    (or (null gnus-current-article)
5863                        (not (eq gnus-current-article article))))
5864               force)
5865           ;; The requested article is different from the current article.
5866           (progn
5867             (when (gnus-buffer-live-p gnus-article-buffer)
5868               (with-current-buffer gnus-article-buffer
5869                 (mm-enable-multibyte)))
5870             (gnus-summary-display-article article all-headers)
5871             (when (gnus-buffer-live-p gnus-article-buffer)
5872               (with-current-buffer gnus-article-buffer
5873                 (if (not gnus-article-decoded-p) ;; a local variable
5874                     (mm-disable-multibyte))))
5875             (when (or all-headers gnus-show-all-headers)
5876               (gnus-article-show-all-headers))
5877             (gnus-article-set-window-start
5878              (cdr (assq article gnus-newsgroup-bookmarks)))
5879             article)
5880         (when (or all-headers gnus-show-all-headers)
5881           (gnus-article-show-all-headers))
5882         'old))))
5883
5884 (defun gnus-summary-set-current-mark (&optional current-mark)
5885   "Obsolete function."
5886   nil)
5887
5888 (defun gnus-summary-next-article (&optional unread subject backward push)
5889   "Select the next article.
5890 If UNREAD, only unread articles are selected.
5891 If SUBJECT, only articles with SUBJECT are selected.
5892 If BACKWARD, the previous article is selected instead of the next."
5893   (interactive "P")
5894   (cond
5895    ;; Is there such an article?
5896    ((and (gnus-summary-search-forward unread subject backward)
5897          (or (gnus-summary-display-article (gnus-summary-article-number))
5898              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5899     (gnus-summary-position-point))
5900    ;; If not, we try the first unread, if that is wanted.
5901    ((and subject
5902          gnus-auto-select-same
5903          (gnus-summary-first-unread-article))
5904     (gnus-summary-position-point)
5905     (gnus-message 6 "Wrapped"))
5906    ;; Try to get next/previous article not displayed in this group.
5907    ((and gnus-auto-extend-newsgroup
5908          (not unread) (not subject))
5909     (gnus-summary-goto-article
5910      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5911      nil (count-lines (point-min) (point))))
5912    ;; Go to next/previous group.
5913    (t
5914     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5915       (gnus-summary-jump-to-group gnus-newsgroup-name))
5916     (let ((cmd last-command-char)
5917           (point
5918            (save-excursion
5919              (set-buffer gnus-group-buffer)
5920              (point)))
5921           (group
5922            (if (eq gnus-keep-same-level 'best)
5923                (gnus-summary-best-group gnus-newsgroup-name)
5924              (gnus-summary-search-group backward gnus-keep-same-level))))
5925       ;; For some reason, the group window gets selected.  We change
5926       ;; it back.
5927       (select-window (get-buffer-window (current-buffer)))
5928       ;; Select next unread newsgroup automagically.
5929       (cond
5930        ((or (not gnus-auto-select-next)
5931             (not cmd))
5932         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5933        ((or (eq gnus-auto-select-next 'quietly)
5934             (and (eq gnus-auto-select-next 'slightly-quietly)
5935                  push)
5936             (and (eq gnus-auto-select-next 'almost-quietly)
5937                  (gnus-summary-last-article-p)))
5938         ;; Select quietly.
5939         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5940             (gnus-summary-exit)
5941           (gnus-message 7 "No more%s articles (%s)..."
5942                         (if unread " unread" "")
5943                         (if group (concat "selecting " group)
5944                           "exiting"))
5945           (gnus-summary-next-group nil group backward)))
5946        (t
5947         (when (gnus-key-press-event-p last-input-event)
5948           (gnus-summary-walk-group-buffer
5949            gnus-newsgroup-name cmd unread backward point))))))))
5950
5951 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5952   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5953                       (?\C-p (gnus-group-prev-unread-group 1))))
5954         (cursor-in-echo-area t)
5955         keve key group ended)
5956     (save-excursion
5957       (set-buffer gnus-group-buffer)
5958       (goto-char start)
5959       (setq group
5960             (if (eq gnus-keep-same-level 'best)
5961                 (gnus-summary-best-group gnus-newsgroup-name)
5962               (gnus-summary-search-group backward gnus-keep-same-level))))
5963     (while (not ended)
5964       (gnus-message
5965        5 "No more%s articles%s" (if unread " unread" "")
5966        (if (and group
5967                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5968            (format " (Type %s for %s [%s])"
5969                    (single-key-description cmd) group
5970                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5971          (format " (Type %s to exit %s)"
5972                  (single-key-description cmd)
5973                  gnus-newsgroup-name)))
5974       ;; Confirm auto selection.
5975       (setq key (car (setq keve (gnus-read-event-char))))
5976       (setq ended t)
5977       (cond
5978        ((assq key keystrokes)
5979         (let ((obuf (current-buffer)))
5980           (switch-to-buffer gnus-group-buffer)
5981           (when group
5982             (gnus-group-jump-to-group group))
5983           (eval (cadr (assq key keystrokes)))
5984           (setq group (gnus-group-group-name))
5985           (switch-to-buffer obuf))
5986         (setq ended nil))
5987        ((equal key cmd)
5988         (if (or (not group)
5989                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5990             (gnus-summary-exit)
5991           (gnus-summary-next-group nil group backward)))
5992        (t
5993         (push (cdr keve) unread-command-events))))))
5994
5995 (defun gnus-summary-next-unread-article ()
5996   "Select unread article after current one."
5997   (interactive)
5998   (gnus-summary-next-article
5999    (or (not (eq gnus-summary-goto-unread 'never))
6000        (gnus-summary-last-article-p (gnus-summary-article-number)))
6001    (and gnus-auto-select-same
6002         (gnus-summary-article-subject))))
6003
6004 (defun gnus-summary-prev-article (&optional unread subject)
6005   "Select the article after the current one.
6006 If UNREAD is non-nil, only unread articles are selected."
6007   (interactive "P")
6008   (gnus-summary-next-article unread subject t))
6009
6010 (defun gnus-summary-prev-unread-article ()
6011   "Select unread article before current one."
6012   (interactive)
6013   (gnus-summary-prev-article
6014    (or (not (eq gnus-summary-goto-unread 'never))
6015        (gnus-summary-first-article-p (gnus-summary-article-number)))
6016    (and gnus-auto-select-same
6017         (gnus-summary-article-subject))))
6018
6019 (defun gnus-summary-next-page (&optional lines circular)
6020   "Show next page of the selected article.
6021 If at the end of the current article, select the next article.
6022 LINES says how many lines should be scrolled up.
6023
6024 If CIRCULAR is non-nil, go to the start of the article instead of
6025 selecting the next article when reaching the end of the current
6026 article."
6027   (interactive "P")
6028   (setq gnus-summary-buffer (current-buffer))
6029   (gnus-set-global-variables)
6030   (let ((article (gnus-summary-article-number))
6031         (article-window (get-buffer-window gnus-article-buffer t))
6032         endp)
6033     ;; If the buffer is empty, we have no article.
6034     (unless article
6035       (error "No article to select"))
6036     (gnus-configure-windows 'article)
6037     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6038         (if (and (eq gnus-summary-goto-unread 'never)
6039                  (not (gnus-summary-last-article-p article)))
6040             (gnus-summary-next-article)
6041           (gnus-summary-next-unread-article))
6042       (if (or (null gnus-current-article)
6043               (null gnus-article-current)
6044               (/= article (cdr gnus-article-current))
6045               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6046           ;; Selected subject is different from current article's.
6047           (gnus-summary-display-article article)
6048         (when article-window
6049           (gnus-eval-in-buffer-window gnus-article-buffer
6050             (setq endp (gnus-article-next-page lines)))
6051           (when endp
6052             (cond (circular
6053                    (gnus-summary-beginning-of-article))
6054                   (lines
6055                    (gnus-message 3 "End of message"))
6056                   ((null lines)
6057                    (if (and (eq gnus-summary-goto-unread 'never)
6058                             (not (gnus-summary-last-article-p article)))
6059                        (gnus-summary-next-article)
6060                      (gnus-summary-next-unread-article))))))))
6061     (gnus-summary-recenter)
6062     (gnus-summary-position-point)))
6063
6064 (defun gnus-summary-prev-page (&optional lines move)
6065   "Show previous page of selected article.
6066 Argument LINES specifies lines to be scrolled down.
6067 If MOVE, move to the previous unread article if point is at
6068 the beginning of the buffer."
6069   (interactive "P")
6070   (let ((article (gnus-summary-article-number))
6071         (article-window (get-buffer-window gnus-article-buffer t))
6072         endp)
6073     (gnus-configure-windows 'article)
6074     (if (or (null gnus-current-article)
6075             (null gnus-article-current)
6076             (/= article (cdr gnus-article-current))
6077             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6078         ;; Selected subject is different from current article's.
6079         (gnus-summary-display-article article)
6080       (gnus-summary-recenter)
6081       (when article-window
6082         (gnus-eval-in-buffer-window gnus-article-buffer
6083           (setq endp (gnus-article-prev-page lines)))
6084         (when (and move endp)
6085           (cond (lines
6086                  (gnus-message 3 "Beginning of message"))
6087                 ((null lines)
6088                  (if (and (eq gnus-summary-goto-unread 'never)
6089                           (not (gnus-summary-first-article-p article)))
6090                      (gnus-summary-prev-article)
6091                    (gnus-summary-prev-unread-article))))))))
6092   (gnus-summary-position-point))
6093
6094 (defun gnus-summary-prev-page-or-article (&optional lines)
6095   "Show previous page of selected article.
6096 Argument LINES specifies lines to be scrolled down.
6097 If at the beginning of the article, go to the next article."
6098   (interactive "P")
6099   (gnus-summary-prev-page lines t))
6100
6101 (defun gnus-summary-scroll-up (lines)
6102   "Scroll up (or down) one line current article.
6103 Argument LINES specifies lines to be scrolled up (or down if negative)."
6104   (interactive "p")
6105   (gnus-configure-windows 'article)
6106   (gnus-summary-show-thread)
6107   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6108     (gnus-eval-in-buffer-window gnus-article-buffer
6109       (cond ((> lines 0)
6110              (when (gnus-article-next-page lines)
6111                (gnus-message 3 "End of message")))
6112             ((< lines 0)
6113              (gnus-article-prev-page (- lines))))))
6114   (gnus-summary-recenter)
6115   (gnus-summary-position-point))
6116
6117 (defun gnus-summary-scroll-down (lines)
6118   "Scroll down (or up) one line current article.
6119 Argument LINES specifies lines to be scrolled down (or up if negative)."
6120   (interactive "p")
6121   (gnus-summary-scroll-up (- lines)))
6122
6123 (defun gnus-summary-next-same-subject ()
6124   "Select next article which has the same subject as current one."
6125   (interactive)
6126   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6127
6128 (defun gnus-summary-prev-same-subject ()
6129   "Select previous article which has the same subject as current one."
6130   (interactive)
6131   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6132
6133 (defun gnus-summary-next-unread-same-subject ()
6134   "Select next unread article which has the same subject as current one."
6135   (interactive)
6136   (gnus-summary-next-article t (gnus-summary-article-subject)))
6137
6138 (defun gnus-summary-prev-unread-same-subject ()
6139   "Select previous unread article which has the same subject as current one."
6140   (interactive)
6141   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6142
6143 (defun gnus-summary-first-unread-article ()
6144   "Select the first unread article.
6145 Return nil if there are no unread articles."
6146   (interactive)
6147   (prog1
6148       (when (gnus-summary-first-subject t)
6149         (gnus-summary-show-thread)
6150         (gnus-summary-first-subject t)
6151         (gnus-summary-display-article (gnus-summary-article-number)))
6152     (gnus-summary-position-point)))
6153
6154 (defun gnus-summary-first-unread-subject ()
6155   "Place the point on the subject line of the first unread article.
6156 Return nil if there are no unread articles."
6157   (interactive)
6158   (prog1
6159       (when (gnus-summary-first-subject t)
6160         (gnus-summary-show-thread)
6161         (gnus-summary-first-subject t))
6162     (gnus-summary-position-point)))
6163
6164 (defun gnus-summary-first-article ()
6165   "Select the first article.
6166 Return nil if there are no articles."
6167   (interactive)
6168   (prog1
6169       (when (gnus-summary-first-subject)
6170         (gnus-summary-show-thread)
6171         (gnus-summary-first-subject)
6172         (gnus-summary-display-article (gnus-summary-article-number)))
6173     (gnus-summary-position-point)))
6174
6175 (defun gnus-summary-best-unread-article ()
6176   "Select the unread article with the highest score."
6177   (interactive)
6178   (let ((best -1000000)
6179         (data gnus-newsgroup-data)
6180         article score)
6181     (while data
6182       (and (gnus-data-unread-p (car data))
6183            (> (setq score
6184                     (gnus-summary-article-score (gnus-data-number (car data))))
6185               best)
6186            (setq best score
6187                  article (gnus-data-number (car data))))
6188       (setq data (cdr data)))
6189     (prog1
6190         (if article
6191             (gnus-summary-goto-article article)
6192           (error "No unread articles"))
6193       (gnus-summary-position-point))))
6194
6195 (defun gnus-summary-last-subject ()
6196   "Go to the last displayed subject line in the group."
6197   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6198     (when article
6199       (gnus-summary-goto-subject article))))
6200
6201 (defun gnus-summary-goto-article (article &optional all-headers force)
6202   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6203 If ALL-HEADERS is non-nil, no header lines are hidden.
6204 If FORCE, go to the article even if it isn't displayed.  If FORCE
6205 is a number, it is the line the article is to be displayed on."
6206   (interactive
6207    (list
6208     (completing-read
6209      "Article number or Message-ID: "
6210      (mapcar (lambda (number) (list (int-to-string number)))
6211              gnus-newsgroup-limit))
6212     current-prefix-arg
6213     t))
6214   (prog1
6215       (if (and (stringp article)
6216                (string-match "@" article))
6217           (gnus-summary-refer-article article)
6218         (when (stringp article)
6219           (setq article (string-to-number article)))
6220         (if (gnus-summary-goto-subject article force)
6221             (gnus-summary-display-article article all-headers)
6222           (gnus-message 4 "Couldn't go to article %s" article) nil))
6223     (gnus-summary-position-point)))
6224
6225 (defun gnus-summary-goto-last-article ()
6226   "Go to the previously read article."
6227   (interactive)
6228   (prog1
6229       (when gnus-last-article
6230         (gnus-summary-goto-article gnus-last-article nil t))
6231     (gnus-summary-position-point)))
6232
6233 (defun gnus-summary-pop-article (number)
6234   "Pop one article off the history and go to the previous.
6235 NUMBER articles will be popped off."
6236   (interactive "p")
6237   (let (to)
6238     (setq gnus-newsgroup-history
6239           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6240     (if to
6241         (gnus-summary-goto-article (car to) nil t)
6242       (error "Article history empty")))
6243   (gnus-summary-position-point))
6244
6245 ;; Summary commands and functions for limiting the summary buffer.
6246
6247 (defun gnus-summary-limit-to-articles (n)
6248   "Limit the summary buffer to the next N articles.
6249 If not given a prefix, use the process marked articles instead."
6250   (interactive "P")
6251   (prog1
6252       (let ((articles (gnus-summary-work-articles n)))
6253         (setq gnus-newsgroup-processable nil)
6254         (gnus-summary-limit articles))
6255     (gnus-summary-position-point)))
6256
6257 (defun gnus-summary-pop-limit (&optional total)
6258   "Restore the previous limit.
6259 If given a prefix, remove all limits."
6260   (interactive "P")
6261   (when total
6262     (setq gnus-newsgroup-limits
6263           (list (mapcar (lambda (h) (mail-header-number h))
6264                         gnus-newsgroup-headers))))
6265   (unless gnus-newsgroup-limits
6266     (error "No limit to pop"))
6267   (prog1
6268       (gnus-summary-limit nil 'pop)
6269     (gnus-summary-position-point)))
6270
6271 (defun gnus-summary-limit-to-subject (subject &optional header)
6272   "Limit the summary buffer to articles that have subjects that match a regexp."
6273   (interactive "sLimit to subject (regexp): ")
6274   (unless header
6275     (setq header "subject"))
6276   (when (not (equal "" subject))
6277     (prog1
6278         (let ((articles (gnus-summary-find-matching
6279                          (or header "subject") subject 'all)))
6280           (unless articles
6281             (error "Found no matches for \"%s\"" subject))
6282           (gnus-summary-limit articles))
6283       (gnus-summary-position-point))))
6284
6285 (defun gnus-summary-limit-to-author (from)
6286   "Limit the summary buffer to articles that have authors that match a regexp."
6287   (interactive "sLimit to author (regexp): ")
6288   (gnus-summary-limit-to-subject from "from"))
6289
6290 (defun gnus-summary-limit-to-age (age &optional younger-p)
6291   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6292 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6293 articles that are younger than AGE days."
6294   (interactive
6295    (let ((younger current-prefix-arg)
6296          (days-got nil)
6297          days)
6298      (while (not days-got)
6299        (setq days (if younger
6300                       (read-string "Limit to articles within (in days): ")
6301                     (read-string "Limit to articles old than (in days): ")))
6302        (when (> (length days) 0)
6303          (setq days (read days)))
6304        (if (numberp days)
6305            (setq days-got t)
6306          (message "Please enter a number.")
6307          (sleep-for 1)))
6308      (list days younger)))
6309   (prog1
6310       (let ((data gnus-newsgroup-data)
6311             (cutoff (days-to-time age))
6312             articles d date is-younger)
6313         (while (setq d (pop data))
6314           (when (and (vectorp (gnus-data-header d))
6315                      (setq date (mail-header-date (gnus-data-header d))))
6316             (setq is-younger (time-less-p
6317                               (time-since (condition-case ()
6318                                               (date-to-time date)
6319                                             (error '(0 0))))
6320                               cutoff))
6321             (when (if younger-p
6322                       is-younger
6323                     (not is-younger))
6324               (push (gnus-data-number d) articles))))
6325         (gnus-summary-limit (nreverse articles)))
6326     (gnus-summary-position-point)))
6327
6328 (defun gnus-summary-limit-to-extra (header regexp)
6329   "Limit the summary buffer to articles that match an 'extra' header."
6330   (interactive
6331    (let ((header
6332           (intern
6333            (gnus-completing-read
6334             (symbol-name (car gnus-extra-headers))
6335             "Limit extra header:"
6336             (mapcar (lambda (x)
6337                       (cons (symbol-name x) x))
6338                     gnus-extra-headers)
6339             nil
6340             t))))
6341      (list header
6342            (read-string (format "Limit to header %s (regexp): " header)))))
6343   (when (not (equal "" regexp))
6344     (prog1
6345         (let ((articles (gnus-summary-find-matching
6346                          (cons 'extra header) regexp 'all)))
6347           (unless articles
6348             (error "Found no matches for \"%s\"" regexp))
6349           (gnus-summary-limit articles))
6350       (gnus-summary-position-point))))
6351
6352 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6353 (make-obsolete
6354  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6355
6356 (defun gnus-summary-limit-to-unread (&optional all)
6357   "Limit the summary buffer to articles that are not marked as read.
6358 If ALL is non-nil, limit strictly to unread articles."
6359   (interactive "P")
6360   (if all
6361       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6362     (gnus-summary-limit-to-marks
6363      ;; Concat all the marks that say that an article is read and have
6364      ;; those removed.
6365      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6366            gnus-killed-mark gnus-kill-file-mark
6367            gnus-low-score-mark gnus-expirable-mark
6368            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6369            gnus-duplicate-mark gnus-souped-mark)
6370      'reverse)))
6371
6372 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6373 (make-obsolete 'gnus-summary-delete-marked-with
6374                'gnus-summary-limit-exlude-marks)
6375
6376 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6377   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6378 If REVERSE, limit the summary buffer to articles that are marked
6379 with MARKS.  MARKS can either be a string of marks or a list of marks.
6380 Returns how many articles were removed."
6381   (interactive "sMarks: ")
6382   (gnus-summary-limit-to-marks marks t))
6383
6384 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6385   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6386 If REVERSE (the prefix), limit the summary buffer to articles that are
6387 not marked with MARKS.  MARKS can either be a string of marks or a
6388 list of marks.
6389 Returns how many articles were removed."
6390   (interactive "sMarks: \nP")
6391   (prog1
6392       (let ((data gnus-newsgroup-data)
6393             (marks (if (listp marks) marks
6394                      (append marks nil))) ; Transform to list.
6395             articles)
6396         (while data
6397           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6398                   (memq (gnus-data-mark (car data)) marks))
6399             (push (gnus-data-number (car data)) articles))
6400           (setq data (cdr data)))
6401         (gnus-summary-limit articles))
6402     (gnus-summary-position-point)))
6403
6404 (defun gnus-summary-limit-to-score (&optional score)
6405   "Limit to articles with score at or above SCORE."
6406   (interactive "P")
6407   (setq score (if score
6408                   (prefix-numeric-value score)
6409                 (or gnus-summary-default-score 0)))
6410   (let ((data gnus-newsgroup-data)
6411         articles)
6412     (while data
6413       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6414                 score)
6415         (push (gnus-data-number (car data)) articles))
6416       (setq data (cdr data)))
6417     (prog1
6418         (gnus-summary-limit articles)
6419       (gnus-summary-position-point))))
6420
6421 (defun gnus-summary-limit-include-thread (id)
6422   "Display all the hidden articles that in the current thread."
6423   (interactive (list (mail-header-id (gnus-summary-article-header))))
6424   (let ((articles (gnus-articles-in-thread
6425                    (gnus-id-to-thread (gnus-root-id id)))))
6426     (prog1
6427         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6428       (gnus-summary-position-point))))
6429
6430 (defun gnus-summary-limit-include-dormant ()
6431   "Display all the hidden articles that are marked as dormant.
6432 Note that this command only works on a subset of the articles currently
6433 fetched for this group."
6434   (interactive)
6435   (unless gnus-newsgroup-dormant
6436     (error "There are no dormant articles in this group"))
6437   (prog1
6438       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6439     (gnus-summary-position-point)))
6440
6441 (defun gnus-summary-limit-exclude-dormant ()
6442   "Hide all dormant articles."
6443   (interactive)
6444   (prog1
6445       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6446     (gnus-summary-position-point)))
6447
6448 (defun gnus-summary-limit-exclude-childless-dormant ()
6449   "Hide all dormant articles that have no children."
6450   (interactive)
6451   (let ((data (gnus-data-list t))
6452         articles d children)
6453     ;; Find all articles that are either not dormant or have
6454     ;; children.
6455     (while (setq d (pop data))
6456       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6457                 (and (setq children
6458                            (gnus-article-children (gnus-data-number d)))
6459                      (let (found)
6460                        (while children
6461                          (when (memq (car children) articles)
6462                            (setq children nil
6463                                  found t))
6464                          (pop children))
6465                        found)))
6466         (push (gnus-data-number d) articles)))
6467     ;; Do the limiting.
6468     (prog1
6469         (gnus-summary-limit articles)
6470       (gnus-summary-position-point))))
6471
6472 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6473   "Mark all unread excluded articles as read.
6474 If ALL, mark even excluded ticked and dormants as read."
6475   (interactive "P")
6476   (let ((articles (gnus-sorted-complement
6477                    (sort
6478                     (mapcar (lambda (h) (mail-header-number h))
6479                             gnus-newsgroup-headers)
6480                     '<)
6481                    (sort gnus-newsgroup-limit '<)))
6482         article)
6483     (setq gnus-newsgroup-unreads
6484           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6485     (if all
6486         (setq gnus-newsgroup-dormant nil
6487               gnus-newsgroup-marked nil
6488               gnus-newsgroup-reads
6489               (nconc
6490                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6491                gnus-newsgroup-reads))
6492       (while (setq article (pop articles))
6493         (unless (or (memq article gnus-newsgroup-dormant)
6494                     (memq article gnus-newsgroup-marked))
6495           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6496
6497 (defun gnus-summary-limit (articles &optional pop)
6498   (if pop
6499       ;; We pop the previous limit off the stack and use that.
6500       (setq articles (car gnus-newsgroup-limits)
6501             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6502     ;; We use the new limit, so we push the old limit on the stack.
6503     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6504   ;; Set the limit.
6505   (setq gnus-newsgroup-limit articles)
6506   (let ((total (length gnus-newsgroup-data))
6507         (data (gnus-data-find-list (gnus-summary-article-number)))
6508         (gnus-summary-mark-below nil)   ; Inhibit this.
6509         found)
6510     ;; This will do all the work of generating the new summary buffer
6511     ;; according to the new limit.
6512     (gnus-summary-prepare)
6513     ;; Hide any threads, possibly.
6514     (and gnus-show-threads
6515          gnus-thread-hide-subtree
6516          (gnus-summary-hide-all-threads))
6517     ;; Try to return to the article you were at, or one in the
6518     ;; neighborhood.
6519     (when data
6520       ;; We try to find some article after the current one.
6521       (while data
6522         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6523           (setq data nil
6524                 found t))
6525         (setq data (cdr data))))
6526     (unless found
6527       ;; If there is no data, that means that we were after the last
6528       ;; article.  The same goes when we can't find any articles
6529       ;; after the current one.
6530       (goto-char (point-max))
6531       (gnus-summary-find-prev))
6532     (gnus-set-mode-line 'summary)
6533     ;; We return how many articles were removed from the summary
6534     ;; buffer as a result of the new limit.
6535     (- total (length gnus-newsgroup-data))))
6536
6537 (defsubst gnus-invisible-cut-children (threads)
6538   (let ((num 0))
6539     (while threads
6540       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6541         (incf num))
6542       (pop threads))
6543     (< num 2)))
6544
6545 (defsubst gnus-cut-thread (thread)
6546   "Go forwards in the thread until we find an article that we want to display."
6547   (when (or (eq gnus-fetch-old-headers 'some)
6548             (eq gnus-fetch-old-headers 'invisible)
6549             (numberp gnus-fetch-old-headers)
6550             (eq gnus-build-sparse-threads 'some)
6551             (eq gnus-build-sparse-threads 'more))
6552     ;; Deal with old-fetched headers and sparse threads.
6553     (while (and
6554             thread
6555             (or
6556              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6557              (gnus-summary-article-ancient-p
6558               (mail-header-number (car thread))))
6559             (if (or (<= (length (cdr thread)) 1)
6560                     (eq gnus-fetch-old-headers 'invisible))
6561                 (setq gnus-newsgroup-limit
6562                       (delq (mail-header-number (car thread))
6563                             gnus-newsgroup-limit)
6564                       thread (cadr thread))
6565               (when (gnus-invisible-cut-children (cdr thread))
6566                 (let ((th (cdr thread)))
6567                   (while th
6568                     (if (memq (mail-header-number (caar th))
6569                               gnus-newsgroup-limit)
6570                         (setq thread (car th)
6571                               th nil)
6572                       (setq th (cdr th))))))))))
6573   thread)
6574
6575 (defun gnus-cut-threads (threads)
6576   "Cut off all uninteresting articles from the beginning of threads."
6577   (when (or (eq gnus-fetch-old-headers 'some)
6578             (eq gnus-fetch-old-headers 'invisible)
6579             (numberp gnus-fetch-old-headers)
6580             (eq gnus-build-sparse-threads 'some)
6581             (eq gnus-build-sparse-threads 'more))
6582     (let ((th threads))
6583       (while th
6584         (setcar th (gnus-cut-thread (car th)))
6585         (setq th (cdr th)))))
6586   ;; Remove nixed out threads.
6587   (delq nil threads))
6588
6589 (defun gnus-summary-initial-limit (&optional show-if-empty)
6590   "Figure out what the initial limit is supposed to be on group entry.
6591 This entails weeding out unwanted dormants, low-scored articles,
6592 fetch-old-headers verbiage, and so on."
6593   ;; Most groups have nothing to remove.
6594   (if (or gnus-inhibit-limiting
6595           (and (null gnus-newsgroup-dormant)
6596                (not (eq gnus-fetch-old-headers 'some))
6597                (not (numberp gnus-fetch-old-headers))
6598                (not (eq gnus-fetch-old-headers 'invisible))
6599                (null gnus-summary-expunge-below)
6600                (not (eq gnus-build-sparse-threads 'some))
6601                (not (eq gnus-build-sparse-threads 'more))
6602                (null gnus-thread-expunge-below)
6603                (not gnus-use-nocem)))
6604       ()                                ; Do nothing.
6605     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6606     (setq gnus-newsgroup-limit nil)
6607     (mapatoms
6608      (lambda (node)
6609        (unless (car (symbol-value node))
6610          ;; These threads have no parents -- they are roots.
6611          (let ((nodes (cdr (symbol-value node)))
6612                thread)
6613            (while nodes
6614              (if (and gnus-thread-expunge-below
6615                       (< (gnus-thread-total-score (car nodes))
6616                          gnus-thread-expunge-below))
6617                  (gnus-expunge-thread (pop nodes))
6618                (setq thread (pop nodes))
6619                (gnus-summary-limit-children thread))))))
6620      gnus-newsgroup-dependencies)
6621     ;; If this limitation resulted in an empty group, we might
6622     ;; pop the previous limit and use it instead.
6623     (when (and (not gnus-newsgroup-limit)
6624                show-if-empty)
6625       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6626     gnus-newsgroup-limit))
6627
6628 (defun gnus-summary-limit-children (thread)
6629   "Return 1 if this subthread is visible and 0 if it is not."
6630   ;; First we get the number of visible children to this thread.  This
6631   ;; is done by recursing down the thread using this function, so this
6632   ;; will really go down to a leaf article first, before slowly
6633   ;; working its way up towards the root.
6634   (when thread
6635     (let ((children
6636            (if (cdr thread)
6637                (apply '+ (mapcar 'gnus-summary-limit-children
6638                                  (cdr thread)))
6639              0))
6640           (number (mail-header-number (car thread)))
6641           score)
6642       (if (and
6643            (not (memq number gnus-newsgroup-marked))
6644            (or
6645             ;; If this article is dormant and has absolutely no visible
6646             ;; children, then this article isn't visible.
6647             (and (memq number gnus-newsgroup-dormant)
6648                  (zerop children))
6649             ;; If this is "fetch-old-headered" and there is no
6650             ;; visible children, then we don't want this article.
6651             (and (or (eq gnus-fetch-old-headers 'some)
6652                      (numberp gnus-fetch-old-headers))
6653                  (gnus-summary-article-ancient-p number)
6654                  (zerop children))
6655             ;; If this is "fetch-old-headered" and `invisible', then
6656             ;; we don't want this article.
6657             (and (eq gnus-fetch-old-headers 'invisible)
6658                  (gnus-summary-article-ancient-p number))
6659             ;; If this is a sparsely inserted article with no children,
6660             ;; we don't want it.
6661             (and (eq gnus-build-sparse-threads 'some)
6662                  (gnus-summary-article-sparse-p number)
6663                  (zerop children))
6664             ;; If we use expunging, and this article is really
6665             ;; low-scored, then we don't want this article.
6666             (when (and gnus-summary-expunge-below
6667                        (< (setq score
6668                                 (or (cdr (assq number gnus-newsgroup-scored))
6669                                     gnus-summary-default-score))
6670                           gnus-summary-expunge-below))
6671               ;; We increase the expunge-tally here, but that has
6672               ;; nothing to do with the limits, really.
6673               (incf gnus-newsgroup-expunged-tally)
6674               ;; We also mark as read here, if that's wanted.
6675               (when (and gnus-summary-mark-below
6676                          (< score gnus-summary-mark-below))
6677                 (setq gnus-newsgroup-unreads
6678                       (delq number gnus-newsgroup-unreads))
6679                 (if gnus-newsgroup-auto-expire
6680                     (push number gnus-newsgroup-expirable)
6681                   (push (cons number gnus-low-score-mark)
6682                         gnus-newsgroup-reads)))
6683               t)
6684             ;; Check NoCeM things.
6685             (if (and gnus-use-nocem
6686                      (gnus-nocem-unwanted-article-p
6687                       (mail-header-id (car thread))))
6688                 (progn
6689                   (setq gnus-newsgroup-unreads
6690                         (delq number gnus-newsgroup-unreads))
6691                   t))))
6692           ;; Nope, invisible article.
6693           0
6694         ;; Ok, this article is to be visible, so we add it to the limit
6695         ;; and return 1.
6696         (push number gnus-newsgroup-limit)
6697         1))))
6698
6699 (defun gnus-expunge-thread (thread)
6700   "Mark all articles in THREAD as read."
6701   (let* ((number (mail-header-number (car thread))))
6702     (incf gnus-newsgroup-expunged-tally)
6703     ;; We also mark as read here, if that's wanted.
6704     (setq gnus-newsgroup-unreads
6705           (delq number gnus-newsgroup-unreads))
6706     (if gnus-newsgroup-auto-expire
6707         (push number gnus-newsgroup-expirable)
6708       (push (cons number gnus-low-score-mark)
6709             gnus-newsgroup-reads)))
6710   ;; Go recursively through all subthreads.
6711   (mapcar 'gnus-expunge-thread (cdr thread)))
6712
6713 ;; Summary article oriented commands
6714
6715 (defun gnus-summary-refer-parent-article (n)
6716   "Refer parent article N times.
6717 If N is negative, go to ancestor -N instead.
6718 The difference between N and the number of articles fetched is returned."
6719   (interactive "p")
6720   (let ((skip 1)
6721         error header ref)
6722     (when (not (natnump n))
6723       (setq skip (abs n)
6724             n 1))
6725     (while (and (> n 0)
6726                 (not error))
6727       (setq header (gnus-summary-article-header))
6728       (if (and (eq (mail-header-number header)
6729                    (cdr gnus-article-current))
6730                (equal gnus-newsgroup-name
6731                       (car gnus-article-current)))
6732           ;; If we try to find the parent of the currently
6733           ;; displayed article, then we take a look at the actual
6734           ;; References header, since this is slightly more
6735           ;; reliable than the References field we got from the
6736           ;; server.
6737           (save-excursion
6738             (set-buffer gnus-original-article-buffer)
6739             (nnheader-narrow-to-headers)
6740             (unless (setq ref (message-fetch-field "references"))
6741               (setq ref (message-fetch-field "in-reply-to")))
6742             (widen))
6743         (setq ref
6744               ;; It's not the current article, so we take a bet on
6745               ;; the value we got from the server.
6746               (mail-header-references header)))
6747       (if (and ref
6748                (not (equal ref "")))
6749           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6750             (gnus-message 1 "Couldn't find parent"))
6751         (gnus-message 1 "No references in article %d"
6752                       (gnus-summary-article-number))
6753         (setq error t))
6754       (decf n))
6755     (gnus-summary-position-point)
6756     n))
6757
6758 (defun gnus-summary-refer-references ()
6759   "Fetch all articles mentioned in the References header.
6760 Return the number of articles fetched."
6761   (interactive)
6762   (let ((ref (mail-header-references (gnus-summary-article-header)))
6763         (current (gnus-summary-article-number))
6764         (n 0))
6765     (if (or (not ref)
6766             (equal ref ""))
6767         (error "No References in the current article")
6768       ;; For each Message-ID in the References header...
6769       (while (string-match "<[^>]*>" ref)
6770         (incf n)
6771         ;; ... fetch that article.
6772         (gnus-summary-refer-article
6773          (prog1 (match-string 0 ref)
6774            (setq ref (substring ref (match-end 0))))))
6775       (gnus-summary-goto-subject current)
6776       (gnus-summary-position-point)
6777       n)))
6778
6779 (defun gnus-summary-refer-thread (&optional limit)
6780   "Fetch all articles in the current thread.
6781 If LIMIT (the numerical prefix), fetch that many old headers instead
6782 of what's specified by the `gnus-refer-thread-limit' variable."
6783   (interactive "P")
6784   (let ((id (mail-header-id (gnus-summary-article-header)))
6785         (limit (if limit (prefix-numeric-value limit)
6786                  gnus-refer-thread-limit)))
6787     ;; We want to fetch LIMIT *old* headers, but we also have to
6788     ;; re-fetch all the headers in the current buffer, because many of
6789     ;; them may be undisplayed.  So we adjust LIMIT.
6790     (when (numberp limit)
6791       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6792     (unless (eq gnus-fetch-old-headers 'invisible)
6793       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6794       ;; Retrieve the headers and read them in.
6795       (if (eq (gnus-retrieve-headers
6796                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6797               'nov)
6798           (gnus-build-all-threads)
6799         (error "Can't fetch thread from backends that don't support NOV"))
6800       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6801     (gnus-summary-limit-include-thread id)))
6802
6803 (defun gnus-summary-refer-article (message-id)
6804   "Fetch an article specified by MESSAGE-ID."
6805   (interactive "sMessage-ID: ")
6806   (when (and (stringp message-id)
6807              (not (zerop (length message-id))))
6808     ;; Construct the correct Message-ID if necessary.
6809     ;; Suggested by tale@pawl.rpi.edu.
6810     (unless (string-match "^<" message-id)
6811       (setq message-id (concat "<" message-id)))
6812     (unless (string-match ">$" message-id)
6813       (setq message-id (concat message-id ">")))
6814     (let* ((header (gnus-id-to-header message-id))
6815            (sparse (and header
6816                         (gnus-summary-article-sparse-p
6817                          (mail-header-number header))
6818                         (memq (mail-header-number header)
6819                               gnus-newsgroup-limit)))
6820            number)
6821       (cond
6822        ;; If the article is present in the buffer we just go to it.
6823        ((and header
6824              (or (not (gnus-summary-article-sparse-p
6825                        (mail-header-number header)))
6826                  sparse))
6827         (prog1
6828             (gnus-summary-goto-article
6829              (mail-header-number header) nil t)
6830           (when sparse
6831             (gnus-summary-update-article (mail-header-number header)))))
6832        (t
6833         ;; We fetch the article.
6834         (catch 'found
6835           (dolist (gnus-override-method (gnus-refer-article-methods))
6836             (gnus-check-server gnus-override-method)
6837             ;; Fetch the header, and display the article.
6838             (when (setq number (gnus-summary-insert-subject message-id))
6839               (gnus-summary-select-article nil nil nil number)
6840               (throw 'found t)))
6841           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6842
6843 (defun gnus-refer-article-methods ()
6844   "Return a list of referrable methods."
6845   (cond
6846    ;; No method, so we default to current and native.
6847    ((null gnus-refer-article-method)
6848     (list gnus-current-select-method gnus-select-method))
6849    ;; Current.
6850    ((eq 'current gnus-refer-article-method)
6851     (list gnus-current-select-method))
6852    ;; List of select methods.
6853    ((not (stringp (cadr gnus-refer-article-method)))
6854     (let (out)
6855       (dolist (method gnus-refer-article-method)
6856         (push (if (eq 'current method)
6857                   gnus-current-select-method
6858                 method)
6859               out))
6860       (nreverse out)))
6861    ;; One single select method.
6862    (t
6863     (list gnus-refer-article-method))))
6864
6865 (defun gnus-summary-edit-parameters ()
6866   "Edit the group parameters of the current group."
6867   (interactive)
6868   (gnus-group-edit-group gnus-newsgroup-name 'params))
6869
6870 (defun gnus-summary-customize-parameters ()
6871   "Customize the group parameters of the current group."
6872   (interactive)
6873   (gnus-group-customize gnus-newsgroup-name))
6874
6875 (defun gnus-summary-enter-digest-group (&optional force)
6876   "Enter an nndoc group based on the current article.
6877 If FORCE, force a digest interpretation.  If not, try
6878 to guess what the document format is."
6879   (interactive "P")
6880   (let ((conf gnus-current-window-configuration))
6881     (save-excursion
6882       (gnus-summary-select-article))
6883     (setq gnus-current-window-configuration conf)
6884     (let* ((name (format "%s-%d"
6885                          (gnus-group-prefixed-name
6886                           gnus-newsgroup-name (list 'nndoc ""))
6887                          (save-excursion
6888                            (set-buffer gnus-summary-buffer)
6889                            gnus-current-article)))
6890            (ogroup gnus-newsgroup-name)
6891            (params (append (gnus-info-params (gnus-get-info ogroup))
6892                            (list (cons 'to-group ogroup))
6893                            (list (cons 'save-article-group ogroup))))
6894            (case-fold-search t)
6895            (buf (current-buffer))
6896            dig to-address)
6897       (save-excursion
6898         (set-buffer gnus-original-article-buffer)
6899         ;; Have the digest group inherit the main mail address of
6900         ;; the parent article.
6901         (when (setq to-address (or (message-fetch-field "reply-to")
6902                                    (message-fetch-field "from")))
6903           (setq params (append (list (cons 'to-address to-address)))))
6904         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6905         (insert-buffer-substring gnus-original-article-buffer)
6906         ;; Remove lines that may lead nndoc to misinterpret the
6907         ;; document type.
6908         (narrow-to-region
6909          (goto-char (point-min))
6910          (or (search-forward "\n\n" nil t) (point)))
6911         (goto-char (point-min))
6912         (delete-matching-lines "^Path:\\|^From ")
6913         (widen))
6914       (unwind-protect
6915           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6916                     (gnus-newsgroup-ephemeral-ignored-charsets
6917                      gnus-newsgroup-ignored-charsets))
6918                 (gnus-group-read-ephemeral-group
6919                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6920                               (nndoc-article-type
6921                                ,(if force 'mbox 'guess))) t))
6922               ;; Make all postings to this group go to the parent group.
6923               (nconc (gnus-info-params (gnus-get-info name))
6924                      params)
6925             ;; Couldn't select this doc group.
6926             (switch-to-buffer buf)
6927             (gnus-set-global-variables)
6928             (gnus-configure-windows 'summary)
6929             (gnus-message 3 "Article couldn't be entered?"))
6930         (kill-buffer dig)))))
6931
6932 (defun gnus-summary-read-document (n)
6933   "Open a new group based on the current article(s).
6934 This will allow you to read digests and other similar
6935 documents as newsgroups.
6936 Obeys the standard process/prefix convention."
6937   (interactive "P")
6938   (let* ((articles (gnus-summary-work-articles n))
6939          (ogroup gnus-newsgroup-name)
6940          (params (append (gnus-info-params (gnus-get-info ogroup))
6941                          (list (cons 'to-group ogroup))))
6942          article group egroup groups vgroup)
6943     (while (setq article (pop articles))
6944       (setq group (format "%s-%d" gnus-newsgroup-name article))
6945       (gnus-summary-remove-process-mark article)
6946       (when (gnus-summary-display-article article)
6947         (save-excursion
6948           (with-temp-buffer
6949             (insert-buffer-substring gnus-original-article-buffer)
6950             ;; Remove some headers that may lead nndoc to make
6951             ;; the wrong guess.
6952             (message-narrow-to-head)
6953             (goto-char (point-min))
6954             (delete-matching-lines "^\\(Path\\):\\|^From ")
6955             (widen)
6956             (if (setq egroup
6957                       (gnus-group-read-ephemeral-group
6958                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6959                                      (nndoc-article-type guess))
6960                        t nil t))
6961                 (progn
6962                   ;; Make all postings to this group go to the parent group.
6963                   (nconc (gnus-info-params (gnus-get-info egroup))
6964                          params)
6965                   (push egroup groups))
6966               ;; Couldn't select this doc group.
6967               (gnus-error 3 "Article couldn't be entered"))))))
6968     ;; Now we have selected all the documents.
6969     (cond
6970      ((not groups)
6971       (error "None of the articles could be interpreted as documents"))
6972      ((gnus-group-read-ephemeral-group
6973        (setq vgroup (format
6974                      "nnvirtual:%s-%s" gnus-newsgroup-name
6975                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6976        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6977        t
6978        (cons (current-buffer) 'summary)))
6979      (t
6980       (error "Couldn't select virtual nndoc group")))))
6981
6982 (defun gnus-summary-isearch-article (&optional regexp-p)
6983   "Do incremental search forward on the current article.
6984 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6985   (interactive "P")
6986   (gnus-summary-select-article)
6987   (gnus-configure-windows 'article)
6988   (gnus-eval-in-buffer-window gnus-article-buffer
6989     (save-restriction
6990       (widen)
6991       (isearch-forward regexp-p))))
6992
6993 (defun gnus-summary-search-article-forward (regexp &optional backward)
6994   "Search for an article containing REGEXP forward.
6995 If BACKWARD, search backward instead."
6996   (interactive
6997    (list (read-string
6998           (format "Search article %s (regexp%s): "
6999                   (if current-prefix-arg "backward" "forward")
7000                   (if gnus-last-search-regexp
7001                       (concat ", default " gnus-last-search-regexp)
7002                     "")))
7003          current-prefix-arg))
7004   (if (string-equal regexp "")
7005       (setq regexp (or gnus-last-search-regexp ""))
7006     (setq gnus-last-search-regexp regexp))
7007   (if (gnus-summary-search-article regexp backward)
7008       (gnus-summary-show-thread)
7009     (error "Search failed: \"%s\"" regexp)))
7010
7011 (defun gnus-summary-search-article-backward (regexp)
7012   "Search for an article containing REGEXP backward."
7013   (interactive
7014    (list (read-string
7015           (format "Search article backward (regexp%s): "
7016                   (if gnus-last-search-regexp
7017                       (concat ", default " gnus-last-search-regexp)
7018                     "")))))
7019   (gnus-summary-search-article-forward regexp 'backward))
7020
7021 (defun gnus-summary-search-article (regexp &optional backward)
7022   "Search for an article containing REGEXP.
7023 Optional argument BACKWARD means do search for backward.
7024 `gnus-select-article-hook' is not called during the search."
7025   ;; We have to require this here to make sure that the following
7026   ;; dynamic binding isn't shadowed by autoloading.
7027   (require 'gnus-async)
7028   (require 'gnus-art)
7029   (let ((gnus-select-article-hook nil)  ;Disable hook.
7030         (gnus-article-prepare-hook nil)
7031         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7032         (gnus-use-article-prefetch nil)
7033         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7034         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7035         (sum (current-buffer))
7036         (gnus-display-mime-function nil)
7037         (found nil)
7038         point)
7039     (gnus-save-hidden-threads
7040       (gnus-summary-select-article)
7041       (set-buffer gnus-article-buffer)
7042       (goto-char (window-point (get-buffer-window (current-buffer))))
7043       (when backward
7044         (forward-line -1))
7045       (while (not found)
7046         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7047         (if (if backward
7048                 (re-search-backward regexp nil t)
7049               (re-search-forward regexp nil t))
7050             ;; We found the regexp.
7051             (progn
7052               (setq found 'found)
7053               (beginning-of-line)
7054               (set-window-start
7055                (get-buffer-window (current-buffer))
7056                (point))
7057               (forward-line 1)
7058               (set-window-point
7059                (get-buffer-window (current-buffer))
7060                (point))
7061               (set-buffer sum)
7062               (setq point (point)))
7063           ;; We didn't find it, so we go to the next article.
7064           (set-buffer sum)
7065           (setq found 'not)
7066           (while (eq found 'not)
7067             (if (not (if backward (gnus-summary-find-prev)
7068                        (gnus-summary-find-next)))
7069                 ;; No more articles.
7070                 (setq found t)
7071               ;; Select the next article and adjust point.
7072               (unless (gnus-summary-article-sparse-p
7073                        (gnus-summary-article-number))
7074                 (setq found nil)
7075                 (gnus-summary-select-article)
7076                 (set-buffer gnus-article-buffer)
7077                 (widen)
7078                 (goto-char (if backward (point-max) (point-min))))))))
7079       (gnus-message 7 ""))
7080     ;; Return whether we found the regexp.
7081     (when (eq found 'found)
7082       (goto-char point)
7083       (gnus-summary-show-thread)
7084       (gnus-summary-goto-subject gnus-current-article)
7085       (gnus-summary-position-point)
7086       t)))
7087
7088 (defun gnus-summary-find-matching (header regexp &optional backward unread
7089                                           not-case-fold)
7090   "Return a list of all articles that match REGEXP on HEADER.
7091 The search stars on the current article and goes forwards unless
7092 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7093 If UNREAD is non-nil, only unread articles will
7094 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7095 in the comparisons."
7096   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7097                 (gnus-data-find-list
7098                  (gnus-summary-article-number) (gnus-data-list backward))))
7099         (case-fold-search (not not-case-fold))
7100         articles d func)
7101     (if (consp header)
7102         (if (eq (car header) 'extra)
7103             (setq func
7104                   `(lambda (h)
7105                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7106                          "")))
7107           (error "%s is an invalid header" header))
7108       (unless (fboundp (intern (concat "mail-header-" header)))
7109         (error "%s is not a valid header" header))
7110       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7111     (while data
7112       (setq d (car data))
7113       (and (or (not unread)             ; We want all articles...
7114                (gnus-data-unread-p d))  ; Or just unreads.
7115            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7116            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7117            (push (gnus-data-number d) articles)) ; Success!
7118       (setq data (cdr data)))
7119     (nreverse articles)))
7120
7121 (defun gnus-summary-execute-command (header regexp command &optional backward)
7122   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7123 If HEADER is an empty string (or nil), the match is done on the entire
7124 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7125   (interactive
7126    (list (let ((completion-ignore-case t))
7127            (completing-read
7128             "Header name: "
7129             (mapcar (lambda (string) (list string))
7130                     '("Number" "Subject" "From" "Lines" "Date"
7131                       "Message-ID" "Xref" "References" "Body"))
7132             nil 'require-match))
7133          (read-string "Regexp: ")
7134          (read-key-sequence "Command: ")
7135          current-prefix-arg))
7136   (when (equal header "Body")
7137     (setq header ""))
7138   ;; Hidden thread subtrees must be searched as well.
7139   (gnus-summary-show-all-threads)
7140   ;; We don't want to change current point nor window configuration.
7141   (save-excursion
7142     (save-window-excursion
7143       (gnus-message 6 "Executing %s..." (key-description command))
7144       ;; We'd like to execute COMMAND interactively so as to give arguments.
7145       (gnus-execute header regexp
7146                     `(call-interactively ',(key-binding command))
7147                     backward)
7148       (gnus-message 6 "Executing %s...done" (key-description command)))))
7149
7150 (defun gnus-summary-beginning-of-article ()
7151   "Scroll the article back to the beginning."
7152   (interactive)
7153   (gnus-summary-select-article)
7154   (gnus-configure-windows 'article)
7155   (gnus-eval-in-buffer-window gnus-article-buffer
7156     (widen)
7157     (goto-char (point-min))
7158     (when gnus-page-broken
7159       (gnus-narrow-to-page))))
7160
7161 (defun gnus-summary-end-of-article ()
7162   "Scroll to the end of the article."
7163   (interactive)
7164   (gnus-summary-select-article)
7165   (gnus-configure-windows 'article)
7166   (gnus-eval-in-buffer-window gnus-article-buffer
7167     (widen)
7168     (goto-char (point-max))
7169     (recenter -3)
7170     (when gnus-page-broken
7171       (gnus-narrow-to-page))))
7172
7173 (defun gnus-summary-print-article (&optional filename n)
7174   "Generate and print a PostScript image of the N next (mail) articles.
7175
7176 If N is negative, print the N previous articles.  If N is nil and articles
7177 have been marked with the process mark, print these instead.
7178
7179 If the optional first argument FILENAME is nil, send the image to the
7180 printer.  If FILENAME is a string, save the PostScript image in a file with
7181 that name.  If FILENAME is a number, prompt the user for the name of the file
7182 to save in."
7183   (interactive (list (ps-print-preprint current-prefix-arg)
7184                      current-prefix-arg))
7185   (dolist (article (gnus-summary-work-articles n))
7186     (gnus-summary-select-article nil nil 'pseudo article)
7187     (gnus-eval-in-buffer-window gnus-article-buffer
7188       (let ((buffer (generate-new-buffer " *print*")))
7189         (unwind-protect
7190             (progn
7191               (copy-to-buffer buffer (point-min) (point-max))
7192               (set-buffer buffer)
7193               (gnus-article-delete-invisible-text)
7194               (let ((ps-left-header
7195                      (list
7196                       (concat "("
7197                               (mail-header-subject gnus-current-headers) ")")
7198                       (concat "("
7199                               (mail-header-from gnus-current-headers) ")")))
7200                     (ps-right-header
7201                      (list
7202                       "/pagenumberstring load"
7203                       (concat "("
7204                               (mail-header-date gnus-current-headers) ")"))))
7205                 (gnus-run-hooks 'gnus-ps-print-hook)
7206                 (save-excursion
7207                   (ps-print-buffer-with-faces filename))))
7208           (kill-buffer buffer))))))
7209
7210 (defun gnus-summary-show-article (&optional arg)
7211   "Force re-fetching of the current article.
7212 If ARG (the prefix) is a number, show the article with the charset
7213 defined in `gnus-summary-show-article-charset-alist', or the charset
7214 inputed.
7215 If ARG (the prefix) is non-nil and not a number, show the raw article
7216 without any article massaging functions being run."
7217   (interactive "P")
7218   (cond
7219    ((numberp arg)
7220     (let ((gnus-newsgroup-charset
7221            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7222                (read-coding-system "Charset: ")))
7223           (gnus-newsgroup-ignored-charsets 'gnus-all))
7224       (gnus-summary-select-article nil 'force)))
7225    ((not arg)
7226     ;; Select the article the normal way.
7227     (gnus-summary-select-article nil 'force))
7228    (t
7229     ;; We have to require this here to make sure that the following
7230     ;; dynamic binding isn't shadowed by autoloading.
7231     (require 'gnus-async)
7232     (require 'gnus-art)
7233     ;; Bind the article treatment functions to nil.
7234     (let ((gnus-have-all-headers t)
7235           gnus-article-prepare-hook
7236           gnus-article-decode-hook
7237           gnus-display-mime-function
7238           gnus-break-pages)
7239       ;; Destroy any MIME parts.
7240       (when (gnus-buffer-live-p gnus-article-buffer)
7241         (save-excursion
7242           (set-buffer gnus-article-buffer)
7243           (mm-destroy-parts gnus-article-mime-handles)))
7244       (gnus-summary-select-article nil 'force))))
7245   (gnus-summary-goto-subject gnus-current-article)
7246   (gnus-summary-position-point))
7247
7248 (defun gnus-summary-verbose-headers (&optional arg)
7249   "Toggle permanent full header display.
7250 If ARG is a positive number, turn header display on.
7251 If ARG is a negative number, turn header display off."
7252   (interactive "P")
7253   (setq gnus-show-all-headers
7254         (cond ((or (not (numberp arg))
7255                    (zerop arg))
7256                (not gnus-show-all-headers))
7257               ((natnump arg)
7258                t)))
7259   (gnus-summary-show-article))
7260
7261 (defun gnus-summary-toggle-header (&optional arg)
7262   "Show the headers if they are hidden, or hide them if they are shown.
7263 If ARG is a positive number, show the entire header.
7264 If ARG is a negative number, hide the unwanted header lines."
7265   (interactive "P")
7266   (save-excursion
7267     (set-buffer gnus-article-buffer)
7268     (save-restriction
7269       (let* ((buffer-read-only nil)
7270              (inhibit-point-motion-hooks t)
7271              hidden e)
7272         (setq hidden
7273               (if (numberp arg)
7274                   (>= arg 0)
7275                 (save-restriction
7276                   (article-narrow-to-head)
7277                   (gnus-article-hidden-text-p 'headers))))
7278         (goto-char (point-min))
7279         (when (search-forward "\n\n" nil t)
7280           (delete-region (point-min) (1- (point))))
7281         (goto-char (point-min))
7282         (save-excursion
7283           (set-buffer gnus-original-article-buffer)
7284           (goto-char (point-min))
7285           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7286         (insert-buffer-substring gnus-original-article-buffer 1 e)
7287         (save-restriction
7288           (narrow-to-region (point-min) (point))
7289           (article-decode-encoded-words)
7290           (if  hidden
7291               (let ((gnus-treat-hide-headers nil)
7292                     (gnus-treat-hide-boring-headers nil))
7293                 (setq gnus-article-wash-types
7294                       (delq 'headers gnus-article-wash-types))
7295                 (gnus-treat-article 'head))
7296             (gnus-treat-article 'head)))
7297         (gnus-set-mode-line 'article)))))
7298
7299 (defun gnus-summary-show-all-headers ()
7300   "Make all header lines visible."
7301   (interactive)
7302   (gnus-article-show-all-headers))
7303
7304 (defun gnus-summary-caesar-message (&optional arg)
7305   "Caesar rotate the current article by 13.
7306 The numerical prefix specifies how many places to rotate each letter
7307 forward."
7308   (interactive "P")
7309   (gnus-summary-select-article)
7310   (let ((mail-header-separator ""))
7311     (gnus-eval-in-buffer-window gnus-article-buffer
7312       (save-restriction
7313         (widen)
7314         (let ((start (window-start))
7315               buffer-read-only)
7316           (message-caesar-buffer-body arg)
7317           (set-window-start (get-buffer-window (current-buffer)) start))))))
7318
7319 (defun gnus-summary-stop-page-breaking ()
7320   "Stop page breaking in the current article."
7321   (interactive)
7322   (gnus-summary-select-article)
7323   (gnus-eval-in-buffer-window gnus-article-buffer
7324     (widen)
7325     (when (gnus-visual-p 'page-marker)
7326       (let ((buffer-read-only nil))
7327         (gnus-remove-text-with-property 'gnus-prev)
7328         (gnus-remove-text-with-property 'gnus-next))
7329       (setq gnus-page-broken nil))))
7330
7331 (defun gnus-summary-move-article (&optional n to-newsgroup
7332                                             select-method action)
7333   "Move the current article to a different newsgroup.
7334 If N is a positive number, move the N next articles.
7335 If N is a negative number, move the N previous articles.
7336 If N is nil and any articles have been marked with the process mark,
7337 move those articles instead.
7338 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7339 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7340 re-spool using this method.
7341
7342 For this function to work, both the current newsgroup and the
7343 newsgroup that you want to move to have to support the `request-move'
7344 and `request-accept' functions.
7345
7346 ACTION can be either `move' (the default), `crosspost' or `copy'."
7347   (interactive "P")
7348   (unless action
7349     (setq action 'move))
7350   ;; Disable marking as read.
7351   (let (gnus-mark-article-hook)
7352     (save-window-excursion
7353       (gnus-summary-select-article)))
7354   ;; Check whether the source group supports the required functions.
7355   (cond ((and (eq action 'move)
7356               (not (gnus-check-backend-function
7357                     'request-move-article gnus-newsgroup-name)))
7358          (error "The current group does not support article moving"))
7359         ((and (eq action 'crosspost)
7360               (not (gnus-check-backend-function
7361                     'request-replace-article gnus-newsgroup-name)))
7362          (error "The current group does not support article editing")))
7363   (let ((articles (gnus-summary-work-articles n))
7364         (prefix (if (gnus-check-backend-function
7365                     'request-move-article gnus-newsgroup-name)
7366                     (gnus-group-real-prefix gnus-newsgroup-name)
7367                   ""))
7368         (names '((move "Move" "Moving")
7369                  (copy "Copy" "Copying")
7370                  (crosspost "Crosspost" "Crossposting")))
7371         (copy-buf (save-excursion
7372                     (nnheader-set-temp-buffer " *copy article*")))
7373         art-group to-method new-xref article to-groups)
7374     (unless (assq action names)
7375       (error "Unknown action %s" action))
7376     ;; Read the newsgroup name.
7377     (when (and (not to-newsgroup)
7378                (not select-method))
7379       (setq to-newsgroup
7380             (gnus-read-move-group-name
7381              (cadr (assq action names))
7382              (symbol-value (intern (format "gnus-current-%s-group" action)))
7383              articles prefix))
7384       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7385     (setq to-method (or select-method
7386                         (gnus-server-to-method
7387                          (gnus-group-method to-newsgroup))))
7388     ;; Check the method we are to move this article to...
7389     (unless (gnus-check-backend-function
7390              'request-accept-article (car to-method))
7391       (error "%s does not support article copying" (car to-method)))
7392     (unless (gnus-check-server to-method)
7393       (error "Can't open server %s" (car to-method)))
7394     (gnus-message 6 "%s to %s: %s..."
7395                   (caddr (assq action names))
7396                   (or (car select-method) to-newsgroup) articles)
7397     (while articles
7398       (setq article (pop articles))
7399       (setq
7400        art-group
7401        (cond
7402         ;; Move the article.
7403         ((eq action 'move)
7404          ;; Remove this article from future suppression.
7405          (gnus-dup-unsuppress-article article)
7406          (gnus-request-move-article
7407           article                       ; Article to move
7408           gnus-newsgroup-name           ; From newsgroup
7409           (nth 1 (gnus-find-method-for-group
7410                   gnus-newsgroup-name)) ; Server
7411           (list 'gnus-request-accept-article
7412                 to-newsgroup (list 'quote select-method)
7413                 (not articles) t)       ; Accept form
7414           (not articles)))              ; Only save nov last time
7415         ;; Copy the article.
7416         ((eq action 'copy)
7417          (save-excursion
7418            (set-buffer copy-buf)
7419            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7420              (gnus-request-accept-article
7421               to-newsgroup select-method (not articles) t))))
7422         ;; Crosspost the article.
7423         ((eq action 'crosspost)
7424          (let ((xref (message-tokenize-header
7425                       (mail-header-xref (gnus-summary-article-header article))
7426                       " ")))
7427            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7428                                   ":" article))
7429            (unless xref
7430              (setq xref (list (system-name))))
7431            (setq new-xref
7432                  (concat
7433                   (mapconcat 'identity
7434                              (delete "Xref:" (delete new-xref xref))
7435                              " ")
7436                   " " new-xref))
7437            (save-excursion
7438              (set-buffer copy-buf)
7439              ;; First put the article in the destination group.
7440              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7441              (when (consp (setq art-group
7442                                 (gnus-request-accept-article
7443                                  to-newsgroup select-method (not articles))))
7444                (setq new-xref (concat new-xref " " (car art-group)
7445                                       ":" (cdr art-group)))
7446                ;; Now we have the new Xrefs header, so we insert
7447                ;; it and replace the new article.
7448                (nnheader-replace-header "Xref" new-xref)
7449                (gnus-request-replace-article
7450                 (cdr art-group) to-newsgroup (current-buffer))
7451                art-group))))))
7452       (cond
7453        ((not art-group)
7454         (gnus-message 1 "Couldn't %s article %s: %s"
7455                       (cadr (assq action names)) article
7456                       (nnheader-get-report (car to-method))))
7457        ((eq art-group 'junk)
7458         (when (eq action 'move)
7459           (gnus-summary-mark-article article gnus-canceled-mark)
7460           (gnus-message 4 "Deleted article %s" article)))
7461        (t
7462         (let* ((pto-group (gnus-group-prefixed-name
7463                            (car art-group) to-method))
7464                (entry
7465                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7466                (info (nth 2 entry))
7467                (to-group (gnus-info-group info))
7468                to-marks)
7469           ;; Update the group that has been moved to.
7470           (when (and info
7471                      (memq action '(move copy)))
7472             (unless (member to-group to-groups)
7473               (push to-group to-groups))
7474
7475             (unless (memq article gnus-newsgroup-unreads)
7476               (push 'read to-marks)
7477               (gnus-info-set-read
7478                info (gnus-add-to-range (gnus-info-read info)
7479                                        (list (cdr art-group)))))
7480
7481             ;; See whether the article is to be put in the cache.
7482             (let ((marks gnus-article-mark-lists)
7483                   (to-article (cdr art-group)))
7484
7485               ;; Enter the article into the cache in the new group,
7486               ;; if that is required.
7487               (when gnus-use-cache
7488                 (gnus-cache-possibly-enter-article
7489                  to-group to-article
7490                  (memq article gnus-newsgroup-marked)
7491                  (memq article gnus-newsgroup-dormant)
7492                  (memq article gnus-newsgroup-unreads)))
7493
7494               (when gnus-preserve-marks
7495                 ;; Copy any marks over to the new group.
7496                 (when (and (equal to-group gnus-newsgroup-name)
7497                            (not (memq article gnus-newsgroup-unreads)))
7498                   ;; Mark this article as read in this group.
7499                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7500                   (setcdr (gnus-active to-group) to-article)
7501                   (setcdr gnus-newsgroup-active to-article))
7502
7503                 (while marks
7504                   (when (memq article (symbol-value
7505                                        (intern (format "gnus-newsgroup-%s"
7506                                                        (caar marks)))))
7507                     (push (cdar marks) to-marks)
7508                     ;; If the other group is the same as this group,
7509                     ;; then we have to add the mark to the list.
7510                     (when (equal to-group gnus-newsgroup-name)
7511                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7512                            (cons to-article
7513                                  (symbol-value
7514                                   (intern (format "gnus-newsgroup-%s"
7515                                                   (caar marks)))))))
7516                     ;; Copy the marks to other group.
7517                     (gnus-add-marked-articles
7518                      to-group (cdar marks) (list to-article) info))
7519                   (setq marks (cdr marks)))
7520
7521                 (gnus-request-set-mark to-group (list (list (list to-article)
7522                                                             'set
7523                                                             to-marks))))
7524
7525               (gnus-dribble-enter
7526                (concat "(gnus-group-set-info '"
7527                        (gnus-prin1-to-string (gnus-get-info to-group))
7528                        ")"))))
7529
7530           ;; Update the Xref header in this article to point to
7531           ;; the new crossposted article we have just created.
7532           (when (eq action 'crosspost)
7533             (save-excursion
7534               (set-buffer copy-buf)
7535               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7536               (nnheader-replace-header "Xref" new-xref)
7537               (gnus-request-replace-article
7538                article gnus-newsgroup-name (current-buffer)))))
7539
7540         ;;;!!!Why is this necessary?
7541         (set-buffer gnus-summary-buffer)
7542
7543         (gnus-summary-goto-subject article)
7544         (when (eq action 'move)
7545           (gnus-summary-mark-article article gnus-canceled-mark))))
7546       (gnus-summary-remove-process-mark article))
7547     ;; Re-activate all groups that have been moved to.
7548     (while to-groups
7549       (save-excursion
7550         (set-buffer gnus-group-buffer)
7551         (when (gnus-group-goto-group (car to-groups) t)
7552           (gnus-group-get-new-news-this-group 1 t))
7553         (pop to-groups)))
7554
7555     (gnus-kill-buffer copy-buf)
7556     (gnus-summary-position-point)
7557     (gnus-set-mode-line 'summary)))
7558
7559 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7560   "Move the current article to a different newsgroup.
7561 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7562 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7563 re-spool using this method."
7564   (interactive "P")
7565   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7566
7567 (defun gnus-summary-crosspost-article (&optional n)
7568   "Crosspost the current article to some other group."
7569   (interactive "P")
7570   (gnus-summary-move-article n nil nil 'crosspost))
7571
7572 (defcustom gnus-summary-respool-default-method nil
7573   "Default method for respooling an article.
7574 If nil, use to the current newsgroup method."
7575   :type '(choice (gnus-select-method :value (nnml ""))
7576                  (const nil))
7577   :group 'gnus-summary-mail)
7578
7579 (defun gnus-summary-respool-article (&optional n method)
7580   "Respool the current article.
7581 The article will be squeezed through the mail spooling process again,
7582 which means that it will be put in some mail newsgroup or other
7583 depending on `nnmail-split-methods'.
7584 If N is a positive number, respool the N next articles.
7585 If N is a negative number, respool the N previous articles.
7586 If N is nil and any articles have been marked with the process mark,
7587 respool those articles instead.
7588
7589 Respooling can be done both from mail groups and \"real\" newsgroups.
7590 In the former case, the articles in question will be moved from the
7591 current group into whatever groups they are destined to.  In the
7592 latter case, they will be copied into the relevant groups."
7593   (interactive
7594    (list current-prefix-arg
7595          (let* ((methods (gnus-methods-using 'respool))
7596                 (methname
7597                  (symbol-name (or gnus-summary-respool-default-method
7598                                   (car (gnus-find-method-for-group
7599                                         gnus-newsgroup-name)))))
7600                 (method
7601                  (gnus-completing-read
7602                   methname "What backend do you want to use when respooling?"
7603                   methods nil t nil 'gnus-mail-method-history))
7604                 ms)
7605            (cond
7606             ((zerop (length (setq ms (gnus-servers-using-backend
7607                                       (intern method)))))
7608              (list (intern method) ""))
7609             ((= 1 (length ms))
7610              (car ms))
7611             (t
7612              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7613                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7614                            ms-alist))))))))
7615   (unless method
7616     (error "No method given for respooling"))
7617   (if (assoc (symbol-name
7618               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7619              (gnus-methods-using 'respool))
7620       (gnus-summary-move-article n nil method)
7621     (gnus-summary-copy-article n nil method)))
7622
7623 (defun gnus-summary-import-article (file)
7624   "Import an arbitrary file into a mail newsgroup."
7625   (interactive "fImport file: ")
7626   (let ((group gnus-newsgroup-name)
7627         (now (current-time))
7628         atts lines)
7629     (unless (gnus-check-backend-function 'request-accept-article group)
7630       (error "%s does not support article importing" group))
7631     (or (file-readable-p file)
7632         (not (file-regular-p file))
7633         (error "Can't read %s" file))
7634     (save-excursion
7635       (set-buffer (gnus-get-buffer-create " *import file*"))
7636       (erase-buffer)
7637       (nnheader-insert-file-contents file)
7638       (goto-char (point-min))
7639       (unless (nnheader-article-p)
7640         ;; This doesn't look like an article, so we fudge some headers.
7641         (setq atts (file-attributes file)
7642               lines (count-lines (point-min) (point-max)))
7643         (insert "From: " (read-string "From: ") "\n"
7644                 "Subject: " (read-string "Subject: ") "\n"
7645                 "Date: " (message-make-date (nth 5 atts))
7646                 "\n"
7647                 "Message-ID: " (message-make-message-id) "\n"
7648                 "Lines: " (int-to-string lines) "\n"
7649                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7650       (gnus-request-accept-article group nil t)
7651       (kill-buffer (current-buffer)))))
7652
7653 (defun gnus-summary-article-posted-p ()
7654   "Say whether the current (mail) article is available from news as well.
7655 This will be the case if the article has both been mailed and posted."
7656   (interactive)
7657   (let ((id (mail-header-references (gnus-summary-article-header)))
7658         (gnus-override-method (car (gnus-refer-article-methods))))
7659     (if (gnus-request-head id "")
7660         (gnus-message 2 "The current message was found on %s"
7661                       gnus-override-method)
7662       (gnus-message 2 "The current message couldn't be found on %s"
7663                     gnus-override-method)
7664       nil)))
7665
7666 (defun gnus-summary-expire-articles (&optional now)
7667   "Expire all articles that are marked as expirable in the current group."
7668   (interactive)
7669   (when (gnus-check-backend-function
7670          'request-expire-articles gnus-newsgroup-name)
7671     ;; This backend supports expiry.
7672     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7673            (expirable (if total
7674                           (progn
7675                             ;; We need to update the info for
7676                             ;; this group for `gnus-list-of-read-articles'
7677                             ;; to give us the right answer.
7678                             (gnus-run-hooks 'gnus-exit-group-hook)
7679                             (gnus-summary-update-info)
7680                             (gnus-list-of-read-articles gnus-newsgroup-name))
7681                         (setq gnus-newsgroup-expirable
7682                               (sort gnus-newsgroup-expirable '<))))
7683            (expiry-wait (if now 'immediate
7684                           (gnus-group-find-parameter
7685                            gnus-newsgroup-name 'expiry-wait)))
7686            es)
7687       (when expirable
7688         ;; There are expirable articles in this group, so we run them
7689         ;; through the expiry process.
7690         (gnus-message 6 "Expiring articles...")
7691         (unless (gnus-check-group gnus-newsgroup-name)
7692           (error "Can't open server for %s" gnus-newsgroup-name))
7693         ;; The list of articles that weren't expired is returned.
7694         (save-excursion
7695           (if expiry-wait
7696               (let ((nnmail-expiry-wait-function nil)
7697                     (nnmail-expiry-wait expiry-wait))
7698                 (setq es (gnus-request-expire-articles
7699                           expirable gnus-newsgroup-name)))
7700             (setq es (gnus-request-expire-articles
7701                       expirable gnus-newsgroup-name))))
7702         (unless total
7703           (setq gnus-newsgroup-expirable es))
7704         ;; We go through the old list of expirable, and mark all
7705         ;; really expired articles as nonexistent.
7706         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7707           (let ((gnus-use-cache nil))
7708             (while expirable
7709               (unless (memq (car expirable) es)
7710                 (when (gnus-data-find (car expirable))
7711                   (gnus-summary-mark-article
7712                    (car expirable) gnus-canceled-mark)))
7713               (setq expirable (cdr expirable)))))
7714         (gnus-message 6 "Expiring articles...done")))))
7715
7716 (defun gnus-summary-expire-articles-now ()
7717   "Expunge all expirable articles in the current group.
7718 This means that *all* articles that are marked as expirable will be
7719 deleted forever, right now."
7720   (interactive)
7721   (or gnus-expert-user
7722       (gnus-yes-or-no-p
7723        "Are you really, really, really sure you want to delete all these messages? ")
7724       (error "Phew!"))
7725   (gnus-summary-expire-articles t))
7726
7727 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7728 (defun gnus-summary-delete-article (&optional n)
7729   "Delete the N next (mail) articles.
7730 This command actually deletes articles.  This is not a marking
7731 command.  The article will disappear forever from your life, never to
7732 return.
7733 If N is negative, delete backwards.
7734 If N is nil and articles have been marked with the process mark,
7735 delete these instead."
7736   (interactive "P")
7737   (unless (gnus-check-backend-function 'request-expire-articles
7738                                        gnus-newsgroup-name)
7739     (error "The current newsgroup does not support article deletion"))
7740   ;; Compute the list of articles to delete.
7741   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7742         not-deleted)
7743     (if (and gnus-novice-user
7744              (not (gnus-yes-or-no-p
7745                    (format "Do you really want to delete %s forever? "
7746                            (if (> (length articles) 1)
7747                                (format "these %s articles" (length articles))
7748                              "this article")))))
7749         ()
7750       ;; Delete the articles.
7751       (setq not-deleted (gnus-request-expire-articles
7752                          articles gnus-newsgroup-name 'force))
7753       (while articles
7754         (gnus-summary-remove-process-mark (car articles))
7755         ;; The backend might not have been able to delete the article
7756         ;; after all.
7757         (unless (memq (car articles) not-deleted)
7758           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7759         (setq articles (cdr articles)))
7760       (when not-deleted
7761         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7762     (gnus-summary-position-point)
7763     (gnus-set-mode-line 'summary)
7764     not-deleted))
7765
7766 (defun gnus-summary-edit-article (&optional arg)
7767   "Edit the current article.
7768 This will have permanent effect only in mail groups.
7769 If ARG is nil, edit the decoded articles.
7770 If ARG is 1, edit the raw articles. 
7771 If ARG is 2, edit the raw articles even in read-only groups.
7772 Otherwise, allow editing of articles even in read-only
7773 groups."
7774   (interactive "P")
7775   (let (force raw)
7776     (cond 
7777      ((null arg))
7778      ((eq arg 1) (setq raw t))
7779      ((eq arg 2) (setq raw t
7780                        force t))
7781      (t (setq force t)))
7782     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
7783         (error "Can't edit the raw article in group nndraft:drafts."))
7784     (save-excursion
7785       (set-buffer gnus-summary-buffer)
7786       (let ((mail-parse-charset gnus-newsgroup-charset)
7787             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7788         (gnus-set-global-variables)
7789         (when (and (not force)
7790                    (gnus-group-read-only-p))
7791           (error "The current newsgroup does not support article editing"))
7792         (gnus-summary-show-article t)
7793         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
7794           (with-current-buffer gnus-article-buffer
7795             (mm-enable-multibyte)))
7796         (if (equal gnus-newsgroup-name "nndraft:drafts")
7797             (setq raw t))
7798         (gnus-article-edit-article
7799          (if raw 'ignore 
7800            #'(lambda () 
7801                (let ((mbl mml-buffer-list))
7802                  (setq mml-buffer-list nil)
7803                  (mime-to-mml)
7804                  (make-local-hook 'kill-buffer-hook)
7805                  (let ((mml-buffer-list mml-buffer-list))
7806                    (setq mml-buffer-list mbl)
7807                    (make-local-variable 'mml-buffer-list))
7808                  (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
7809          `(lambda (no-highlight)
7810             (let ((mail-parse-charset ',gnus-newsgroup-charset)
7811                   (mail-parse-ignored-charsets 
7812                    ',gnus-newsgroup-ignored-charsets))
7813               ,(if (not raw) '(progn 
7814                                 (mml-to-mime)
7815                                 (mml-destroy-buffers)
7816                                 (remove-hook 'kill-buffer-hook 
7817                                              'mml-destroy-buffers t)
7818                                 (kill-local-variable 'mml-buffer-list)))
7819               (gnus-summary-edit-article-done
7820                ,(or (mail-header-references gnus-current-headers) "")
7821                ,(gnus-group-read-only-p) 
7822                ,gnus-summary-buffer no-highlight))))))))
7823
7824 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7825
7826 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7827                                                  no-highlight)
7828   "Make edits to the current article permanent."
7829   (interactive)
7830   ;; Replace the article.
7831   (let ((buf (current-buffer)))
7832     (with-temp-buffer
7833       (insert-buffer-substring buf)
7834       (if (and (not read-only)
7835                (not (gnus-request-replace-article
7836                      (cdr gnus-article-current) (car gnus-article-current)
7837                      (current-buffer) t)))
7838           (error "Couldn't replace article")
7839         ;; Update the summary buffer.
7840         (if (and references
7841                  (equal (message-tokenize-header references " ")
7842                         (message-tokenize-header
7843                          (or (message-fetch-field "references") "") " ")))
7844             ;; We only have to update this line.
7845             (save-excursion
7846               (save-restriction
7847                 (message-narrow-to-head)
7848                 (let ((head (buffer-string))
7849                       header)
7850                   (with-temp-buffer
7851                     (insert (format "211 %d Article retrieved.\n"
7852                                     (cdr gnus-article-current)))
7853                     (insert head)
7854                     (insert ".\n")
7855                     (let ((nntp-server-buffer (current-buffer)))
7856                       (setq header (car (gnus-get-newsgroup-headers
7857                                          (save-excursion
7858                                            (set-buffer gnus-summary-buffer)
7859                                            gnus-newsgroup-dependencies)
7860                                          t))))
7861                     (save-excursion
7862                       (set-buffer gnus-summary-buffer)
7863                       (gnus-data-set-header
7864                        (gnus-data-find (cdr gnus-article-current))
7865                        header)
7866                       (gnus-summary-update-article-line
7867                        (cdr gnus-article-current) header))))))
7868           ;; Update threads.
7869           (set-buffer (or buffer gnus-summary-buffer))
7870           (gnus-summary-update-article (cdr gnus-article-current)))
7871         ;; Prettify the article buffer again.
7872         (unless no-highlight
7873           (save-excursion
7874             (set-buffer gnus-article-buffer)
7875             ;;;!!! Fix this -- article should be rehighlighted.
7876             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7877             (set-buffer gnus-original-article-buffer)
7878             (gnus-request-article
7879              (cdr gnus-article-current)
7880              (car gnus-article-current) (current-buffer))))
7881         ;; Prettify the summary buffer line.
7882         (when (gnus-visual-p 'summary-highlight 'highlight)
7883           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7884
7885 (defun gnus-summary-edit-wash (key)
7886   "Perform editing command KEY in the article buffer."
7887   (interactive
7888    (list
7889     (progn
7890       (message "%s" (concat (this-command-keys) "- "))
7891       (read-char))))
7892   (message "")
7893   (gnus-summary-edit-article)
7894   (execute-kbd-macro (concat (this-command-keys) key))
7895   (gnus-article-edit-done))
7896
7897 ;;; Respooling
7898
7899 (defun gnus-summary-respool-query (&optional silent trace)
7900   "Query where the respool algorithm would put this article."
7901   (interactive)
7902   (let (gnus-mark-article-hook)
7903     (gnus-summary-select-article)
7904     (save-excursion
7905       (set-buffer gnus-original-article-buffer)
7906       (save-restriction
7907         (message-narrow-to-head)
7908         (let ((groups (nnmail-article-group 'identity trace)))
7909           (unless silent
7910             (if groups
7911                 (message "This message would go to %s"
7912                          (mapconcat 'car groups ", "))
7913               (message "This message would go to no groups"))
7914             groups))))))
7915
7916 (defun gnus-summary-respool-trace ()
7917   "Trace where the respool algorithm would put this article.
7918 Display a buffer showing all fancy splitting patterns which matched."
7919   (interactive)
7920   (gnus-summary-respool-query nil t))
7921
7922 ;; Summary marking commands.
7923
7924 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7925   "Mark articles which has the same subject as read, and then select the next.
7926 If UNMARK is positive, remove any kind of mark.
7927 If UNMARK is negative, tick articles."
7928   (interactive "P")
7929   (when unmark
7930     (setq unmark (prefix-numeric-value unmark)))
7931   (let ((count
7932          (gnus-summary-mark-same-subject
7933           (gnus-summary-article-subject) unmark)))
7934     ;; Select next unread article.  If auto-select-same mode, should
7935     ;; select the first unread article.
7936     (gnus-summary-next-article t (and gnus-auto-select-same
7937                                       (gnus-summary-article-subject)))
7938     (gnus-message 7 "%d article%s marked as %s"
7939                   count (if (= count 1) " is" "s are")
7940                   (if unmark "unread" "read"))))
7941
7942 (defun gnus-summary-kill-same-subject (&optional unmark)
7943   "Mark articles which has the same subject as read.
7944 If UNMARK is positive, remove any kind of mark.
7945 If UNMARK is negative, tick articles."
7946   (interactive "P")
7947   (when unmark
7948     (setq unmark (prefix-numeric-value unmark)))
7949   (let ((count
7950          (gnus-summary-mark-same-subject
7951           (gnus-summary-article-subject) unmark)))
7952     ;; If marked as read, go to next unread subject.
7953     (when (null unmark)
7954       ;; Go to next unread subject.
7955       (gnus-summary-next-subject 1 t))
7956     (gnus-message 7 "%d articles are marked as %s"
7957                   count (if unmark "unread" "read"))))
7958
7959 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7960   "Mark articles with same SUBJECT as read, and return marked number.
7961 If optional argument UNMARK is positive, remove any kinds of marks.
7962 If optional argument UNMARK is negative, mark articles as unread instead."
7963   (let ((count 1))
7964     (save-excursion
7965       (cond
7966        ((null unmark)                   ; Mark as read.
7967         (while (and
7968                 (progn
7969                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7970                   (gnus-summary-show-thread) t)
7971                 (gnus-summary-find-subject subject))
7972           (setq count (1+ count))))
7973        ((> unmark 0)                    ; Tick.
7974         (while (and
7975                 (progn
7976                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7977                   (gnus-summary-show-thread) t)
7978                 (gnus-summary-find-subject subject))
7979           (setq count (1+ count))))
7980        (t                               ; Mark as unread.
7981         (while (and
7982                 (progn
7983                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7984                   (gnus-summary-show-thread) t)
7985                 (gnus-summary-find-subject subject))
7986           (setq count (1+ count)))))
7987       (gnus-set-mode-line 'summary)
7988       ;; Return the number of marked articles.
7989       count)))
7990
7991 (defun gnus-summary-mark-as-processable (n &optional unmark)
7992   "Set the process mark on the next N articles.
7993 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7994 the process mark instead.  The difference between N and the actual
7995 number of articles marked is returned."
7996   (interactive "p")
7997   (let ((backward (< n 0))
7998         (n (abs n)))
7999     (while (and
8000             (> n 0)
8001             (if unmark
8002                 (gnus-summary-remove-process-mark
8003                  (gnus-summary-article-number))
8004               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8005             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8006       (setq n (1- n)))
8007     (when (/= 0 n)
8008       (gnus-message 7 "No more articles"))
8009     (gnus-summary-recenter)
8010     (gnus-summary-position-point)
8011     n))
8012
8013 (defun gnus-summary-unmark-as-processable (n)
8014   "Remove the process mark from the next N articles.
8015 If N is negative, unmark backward instead.  The difference between N and
8016 the actual number of articles unmarked is returned."
8017   (interactive "p")
8018   (gnus-summary-mark-as-processable n t))
8019
8020 (defun gnus-summary-unmark-all-processable ()
8021   "Remove the process mark from all articles."
8022   (interactive)
8023   (save-excursion
8024     (while gnus-newsgroup-processable
8025       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8026   (gnus-summary-position-point))
8027
8028 (defun gnus-summary-mark-as-expirable (n)
8029   "Mark N articles forward as expirable.
8030 If N is negative, mark backward instead.  The difference between N and
8031 the actual number of articles marked is returned."
8032   (interactive "p")
8033   (gnus-summary-mark-forward n gnus-expirable-mark))
8034
8035 (defun gnus-summary-mark-article-as-replied (article)
8036   "Mark ARTICLE replied and update the summary line."
8037   (push article gnus-newsgroup-replied)
8038   (let ((buffer-read-only nil))
8039     (when (gnus-summary-goto-subject article nil t)
8040       (gnus-summary-update-secondary-mark article))))
8041
8042 (defun gnus-summary-set-bookmark (article)
8043   "Set a bookmark in current article."
8044   (interactive (list (gnus-summary-article-number)))
8045   (when (or (not (get-buffer gnus-article-buffer))
8046             (not gnus-current-article)
8047             (not gnus-article-current)
8048             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8049     (error "No current article selected"))
8050   ;; Remove old bookmark, if one exists.
8051   (let ((old (assq article gnus-newsgroup-bookmarks)))
8052     (when old
8053       (setq gnus-newsgroup-bookmarks
8054             (delq old gnus-newsgroup-bookmarks))))
8055   ;; Set the new bookmark, which is on the form
8056   ;; (article-number . line-number-in-body).
8057   (push
8058    (cons article
8059          (save-excursion
8060            (set-buffer gnus-article-buffer)
8061            (count-lines
8062             (min (point)
8063                  (save-excursion
8064                    (goto-char (point-min))
8065                    (search-forward "\n\n" nil t)
8066                    (point)))
8067             (point))))
8068    gnus-newsgroup-bookmarks)
8069   (gnus-message 6 "A bookmark has been added to the current article."))
8070
8071 (defun gnus-summary-remove-bookmark (article)
8072   "Remove the bookmark from the current article."
8073   (interactive (list (gnus-summary-article-number)))
8074   ;; Remove old bookmark, if one exists.
8075   (let ((old (assq article gnus-newsgroup-bookmarks)))
8076     (if old
8077         (progn
8078           (setq gnus-newsgroup-bookmarks
8079                 (delq old gnus-newsgroup-bookmarks))
8080           (gnus-message 6 "Removed bookmark."))
8081       (gnus-message 6 "No bookmark in current article."))))
8082
8083 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8084 (defun gnus-summary-mark-as-dormant (n)
8085   "Mark N articles forward as dormant.
8086 If N is negative, mark backward instead.  The difference between N and
8087 the actual number of articles marked is returned."
8088   (interactive "p")
8089   (gnus-summary-mark-forward n gnus-dormant-mark))
8090
8091 (defun gnus-summary-set-process-mark (article)
8092   "Set the process mark on ARTICLE and update the summary line."
8093   (setq gnus-newsgroup-processable
8094         (cons article
8095               (delq article gnus-newsgroup-processable)))
8096   (when (gnus-summary-goto-subject article)
8097     (gnus-summary-show-thread)
8098     (gnus-summary-goto-subject article)
8099     (gnus-summary-update-secondary-mark article)))
8100
8101 (defun gnus-summary-remove-process-mark (article)
8102   "Remove the process mark from ARTICLE and update the summary line."
8103   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8104   (when (gnus-summary-goto-subject article)
8105     (gnus-summary-show-thread)
8106     (gnus-summary-goto-subject article)
8107     (gnus-summary-update-secondary-mark article)))
8108
8109 (defun gnus-summary-set-saved-mark (article)
8110   "Set the process mark on ARTICLE and update the summary line."
8111   (push article gnus-newsgroup-saved)
8112   (when (gnus-summary-goto-subject article)
8113     (gnus-summary-update-secondary-mark article)))
8114
8115 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8116   "Mark N articles as read forwards.
8117 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8118 The difference between N and the actual number of articles marked is
8119 returned.
8120 Iff NO-EXPIRE, auto-expiry will be inhibited."
8121   (interactive "p")
8122   (gnus-summary-show-thread)
8123   (let ((backward (< n 0))
8124         (gnus-summary-goto-unread
8125          (and gnus-summary-goto-unread
8126               (not (eq gnus-summary-goto-unread 'never))
8127               (not (memq mark (list gnus-unread-mark
8128                                     gnus-ticked-mark gnus-dormant-mark)))))
8129         (n (abs n))
8130         (mark (or mark gnus-del-mark)))
8131     (while (and (> n 0)
8132                 (gnus-summary-mark-article nil mark no-expire)
8133                 (zerop (gnus-summary-next-subject
8134                         (if backward -1 1)
8135                         (and gnus-summary-goto-unread
8136                              (not (eq gnus-summary-goto-unread 'never)))
8137                         t)))
8138       (setq n (1- n)))
8139     (when (/= 0 n)
8140       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8141     (gnus-summary-recenter)
8142     (gnus-summary-position-point)
8143     (gnus-set-mode-line 'summary)
8144     n))
8145
8146 (defun gnus-summary-mark-article-as-read (mark)
8147   "Mark the current article quickly as read with MARK."
8148   (let ((article (gnus-summary-article-number)))
8149     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8150     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8151     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8152     (push (cons article mark) gnus-newsgroup-reads)
8153     ;; Possibly remove from cache, if that is used.
8154     (when gnus-use-cache
8155       (gnus-cache-enter-remove-article article))
8156     ;; Allow the backend to change the mark.
8157     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8158     ;; Check for auto-expiry.
8159     (when (and gnus-newsgroup-auto-expire
8160                (memq mark gnus-auto-expirable-marks))
8161       (setq mark gnus-expirable-mark)
8162       ;; Let the backend know about the mark change.
8163       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8164       (push article gnus-newsgroup-expirable))
8165     ;; Set the mark in the buffer.
8166     (gnus-summary-update-mark mark 'unread)
8167     t))
8168
8169 (defun gnus-summary-mark-article-as-unread (mark)
8170   "Mark the current article quickly as unread with MARK."
8171   (let* ((article (gnus-summary-article-number))
8172          (old-mark (gnus-summary-article-mark article)))
8173     ;; Allow the backend to change the mark.
8174     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8175     (if (eq mark old-mark)
8176         t
8177       (if (<= article 0)
8178           (progn
8179             (gnus-error 1 "Can't mark negative article numbers")
8180             nil)
8181         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8182         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8183         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8184         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8185         (cond ((= mark gnus-ticked-mark)
8186                (push article gnus-newsgroup-marked))
8187               ((= mark gnus-dormant-mark)
8188                (push article gnus-newsgroup-dormant))
8189               (t
8190                (push article gnus-newsgroup-unreads)))
8191         (gnus-pull article gnus-newsgroup-reads)
8192
8193         ;; See whether the article is to be put in the cache.
8194         (and gnus-use-cache
8195              (vectorp (gnus-summary-article-header article))
8196              (save-excursion
8197                (gnus-cache-possibly-enter-article
8198                 gnus-newsgroup-name article
8199                 (= mark gnus-ticked-mark)
8200                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8201
8202         ;; Fix the mark.
8203         (gnus-summary-update-mark mark 'unread)
8204         t))))
8205
8206 (defun gnus-summary-mark-article (&optional article mark no-expire)
8207   "Mark ARTICLE with MARK.  MARK can be any character.
8208 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8209 `??' (dormant) and `?E' (expirable).
8210 If MARK is nil, then the default character `?r' is used.
8211 If ARTICLE is nil, then the article on the current line will be
8212 marked.
8213 Iff NO-EXPIRE, auto-expiry will be inhibited."
8214   ;; The mark might be a string.
8215   (when (stringp mark)
8216     (setq mark (aref mark 0)))
8217   ;; If no mark is given, then we check auto-expiring.
8218   (when (null mark)
8219     (setq mark gnus-del-mark))
8220   (when (and (not no-expire)
8221              gnus-newsgroup-auto-expire
8222              (memq mark gnus-auto-expirable-marks))
8223     (setq mark gnus-expirable-mark))
8224   (let ((article (or article (gnus-summary-article-number)))
8225         (old-mark (gnus-summary-article-mark article)))
8226     ;; Allow the backend to change the mark.
8227     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8228     (if (eq mark old-mark)
8229         t
8230       (unless article
8231         (error "No article on current line"))
8232       (if (not (if (or (= mark gnus-unread-mark)
8233                        (= mark gnus-ticked-mark)
8234                        (= mark gnus-dormant-mark))
8235                    (gnus-mark-article-as-unread article mark)
8236                  (gnus-mark-article-as-read article mark)))
8237           t
8238         ;; See whether the article is to be put in the cache.
8239         (and gnus-use-cache
8240              (not (= mark gnus-canceled-mark))
8241              (vectorp (gnus-summary-article-header article))
8242              (save-excursion
8243                (gnus-cache-possibly-enter-article
8244                 gnus-newsgroup-name article
8245                 (= mark gnus-ticked-mark)
8246                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8247
8248         (when (gnus-summary-goto-subject article nil t)
8249           (let ((buffer-read-only nil))
8250             (gnus-summary-show-thread)
8251             ;; Fix the mark.
8252             (gnus-summary-update-mark mark 'unread)
8253             t))))))
8254
8255 (defun gnus-summary-update-secondary-mark (article)
8256   "Update the secondary (read, process, cache) mark."
8257   (gnus-summary-update-mark
8258    (cond ((memq article gnus-newsgroup-processable)
8259           gnus-process-mark)
8260          ((memq article gnus-newsgroup-cached)
8261           gnus-cached-mark)
8262          ((memq article gnus-newsgroup-replied)
8263           gnus-replied-mark)
8264          ((memq article gnus-newsgroup-saved)
8265           gnus-saved-mark)
8266          (t gnus-unread-mark))
8267    'replied)
8268   (when (gnus-visual-p 'summary-highlight 'highlight)
8269     (gnus-run-hooks 'gnus-summary-update-hook))
8270   t)
8271
8272 (defun gnus-summary-update-mark (mark type)
8273   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8274         (buffer-read-only nil))
8275     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8276     (when forward
8277       (when (looking-at "\r")
8278         (incf forward))
8279       (when (<= (+ forward (point)) (point-max))
8280         ;; Go to the right position on the line.
8281         (goto-char (+ forward (point)))
8282         ;; Replace the old mark with the new mark.
8283         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8284         ;; Optionally update the marks by some user rule.
8285         (when (eq type 'unread)
8286           (gnus-data-set-mark
8287            (gnus-data-find (gnus-summary-article-number)) mark)
8288           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8289
8290 (defun gnus-mark-article-as-read (article &optional mark)
8291   "Enter ARTICLE in the pertinent lists and remove it from others."
8292   ;; Make the article expirable.
8293   (let ((mark (or mark gnus-del-mark)))
8294     (if (= mark gnus-expirable-mark)
8295         (push article gnus-newsgroup-expirable)
8296       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8297     ;; Remove from unread and marked lists.
8298     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8299     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8300     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8301     (push (cons article mark) gnus-newsgroup-reads)
8302     ;; Possibly remove from cache, if that is used.
8303     (when gnus-use-cache
8304       (gnus-cache-enter-remove-article article))
8305     t))
8306
8307 (defun gnus-mark-article-as-unread (article &optional mark)
8308   "Enter ARTICLE in the pertinent lists and remove it from others."
8309   (let ((mark (or mark gnus-ticked-mark)))
8310     (if (<= article 0)
8311         (progn
8312           (gnus-error 1 "Can't mark negative article numbers")
8313           nil)
8314       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8315             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8316             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8317             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8318
8319       ;; Unsuppress duplicates?
8320       (when gnus-suppress-duplicates
8321         (gnus-dup-unsuppress-article article))
8322
8323       (cond ((= mark gnus-ticked-mark)
8324              (push article gnus-newsgroup-marked))
8325             ((= mark gnus-dormant-mark)
8326              (push article gnus-newsgroup-dormant))
8327             (t
8328              (push article gnus-newsgroup-unreads)))
8329       (gnus-pull article gnus-newsgroup-reads)
8330       t)))
8331
8332 (defalias 'gnus-summary-mark-as-unread-forward
8333   'gnus-summary-tick-article-forward)
8334 (make-obsolete 'gnus-summary-mark-as-unread-forward
8335                'gnus-summary-tick-article-forward)
8336 (defun gnus-summary-tick-article-forward (n)
8337   "Tick N articles forwards.
8338 If N is negative, tick backwards instead.
8339 The difference between N and the number of articles ticked is returned."
8340   (interactive "p")
8341   (gnus-summary-mark-forward n gnus-ticked-mark))
8342
8343 (defalias 'gnus-summary-mark-as-unread-backward
8344   'gnus-summary-tick-article-backward)
8345 (make-obsolete 'gnus-summary-mark-as-unread-backward
8346                'gnus-summary-tick-article-backward)
8347 (defun gnus-summary-tick-article-backward (n)
8348   "Tick N articles backwards.
8349 The difference between N and the number of articles ticked is returned."
8350   (interactive "p")
8351   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8352
8353 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8354 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8355 (defun gnus-summary-tick-article (&optional article clear-mark)
8356   "Mark current article as unread.
8357 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8358 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8359   (interactive)
8360   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8361                                        gnus-ticked-mark)))
8362
8363 (defun gnus-summary-mark-as-read-forward (n)
8364   "Mark N articles as read forwards.
8365 If N is negative, mark backwards instead.
8366 The difference between N and the actual number of articles marked is
8367 returned."
8368   (interactive "p")
8369   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8370
8371 (defun gnus-summary-mark-as-read-backward (n)
8372   "Mark the N articles as read backwards.
8373 The difference between N and the actual number of articles marked is
8374 returned."
8375   (interactive "p")
8376   (gnus-summary-mark-forward
8377    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8378
8379 (defun gnus-summary-mark-as-read (&optional article mark)
8380   "Mark current article as read.
8381 ARTICLE specifies the article to be marked as read.
8382 MARK specifies a string to be inserted at the beginning of the line."
8383   (gnus-summary-mark-article article mark))
8384
8385 (defun gnus-summary-clear-mark-forward (n)
8386   "Clear marks from N articles forward.
8387 If N is negative, clear backward instead.
8388 The difference between N and the number of marks cleared is returned."
8389   (interactive "p")
8390   (gnus-summary-mark-forward n gnus-unread-mark))
8391
8392 (defun gnus-summary-clear-mark-backward (n)
8393   "Clear marks from N articles backward.
8394 The difference between N and the number of marks cleared is returned."
8395   (interactive "p")
8396   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8397
8398 (defun gnus-summary-mark-unread-as-read ()
8399   "Intended to be used by `gnus-summary-mark-article-hook'."
8400   (when (memq gnus-current-article gnus-newsgroup-unreads)
8401     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8402
8403 (defun gnus-summary-mark-read-and-unread-as-read ()
8404   "Intended to be used by `gnus-summary-mark-article-hook'."
8405   (let ((mark (gnus-summary-article-mark)))
8406     (when (or (gnus-unread-mark-p mark)
8407               (gnus-read-mark-p mark))
8408       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8409
8410 (defun gnus-summary-mark-region-as-read (point mark all)
8411   "Mark all unread articles between point and mark as read.
8412 If given a prefix, mark all articles between point and mark as read,
8413 even ticked and dormant ones."
8414   (interactive "r\nP")
8415   (save-excursion
8416     (let (article)
8417       (goto-char point)
8418       (beginning-of-line)
8419       (while (and
8420               (< (point) mark)
8421               (progn
8422                 (when (or all
8423                           (memq (setq article (gnus-summary-article-number))
8424                                 gnus-newsgroup-unreads))
8425                   (gnus-summary-mark-article article gnus-del-mark))
8426                 t)
8427               (gnus-summary-find-next))))))
8428
8429 (defun gnus-summary-mark-below (score mark)
8430   "Mark articles with score less than SCORE with MARK."
8431   (interactive "P\ncMark: ")
8432   (setq score (if score
8433                   (prefix-numeric-value score)
8434                 (or gnus-summary-default-score 0)))
8435   (save-excursion
8436     (set-buffer gnus-summary-buffer)
8437     (goto-char (point-min))
8438     (while
8439         (progn
8440           (and (< (gnus-summary-article-score) score)
8441                (gnus-summary-mark-article nil mark))
8442           (gnus-summary-find-next)))))
8443
8444 (defun gnus-summary-kill-below (&optional score)
8445   "Mark articles with score below SCORE as read."
8446   (interactive "P")
8447   (gnus-summary-mark-below score gnus-killed-mark))
8448
8449 (defun gnus-summary-clear-above (&optional score)
8450   "Clear all marks from articles with score above SCORE."
8451   (interactive "P")
8452   (gnus-summary-mark-above score gnus-unread-mark))
8453
8454 (defun gnus-summary-tick-above (&optional score)
8455   "Tick all articles with score above SCORE."
8456   (interactive "P")
8457   (gnus-summary-mark-above score gnus-ticked-mark))
8458
8459 (defun gnus-summary-mark-above (score mark)
8460   "Mark articles with score over SCORE with MARK."
8461   (interactive "P\ncMark: ")
8462   (setq score (if score
8463                   (prefix-numeric-value score)
8464                 (or gnus-summary-default-score 0)))
8465   (save-excursion
8466     (set-buffer gnus-summary-buffer)
8467     (goto-char (point-min))
8468     (while (and (progn
8469                   (when (> (gnus-summary-article-score) score)
8470                     (gnus-summary-mark-article nil mark))
8471                   t)
8472                 (gnus-summary-find-next)))))
8473
8474 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8475 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8476 (defun gnus-summary-limit-include-expunged (&optional no-error)
8477   "Display all the hidden articles that were expunged for low scores."
8478   (interactive)
8479   (let ((buffer-read-only nil))
8480     (let ((scored gnus-newsgroup-scored)
8481           headers h)
8482       (while scored
8483         (unless (gnus-summary-goto-subject (caar scored))
8484           (and (setq h (gnus-summary-article-header (caar scored)))
8485                (< (cdar scored) gnus-summary-expunge-below)
8486                (push h headers)))
8487         (setq scored (cdr scored)))
8488       (if (not headers)
8489           (when (not no-error)
8490             (error "No expunged articles hidden"))
8491         (goto-char (point-min))
8492         (gnus-summary-prepare-unthreaded (nreverse headers))
8493         (goto-char (point-min))
8494         (gnus-summary-position-point)
8495         t))))
8496
8497 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8498   "Mark all unread articles in this newsgroup as read.
8499 If prefix argument ALL is non-nil, ticked and dormant articles will
8500 also be marked as read.
8501 If QUIETLY is non-nil, no questions will be asked.
8502 If TO-HERE is non-nil, it should be a point in the buffer.  All
8503 articles before this point will be marked as read.
8504 Note that this function will only catch up the unread article
8505 in the current summary buffer limitation.
8506 The number of articles marked as read is returned."
8507   (interactive "P")
8508   (prog1
8509       (save-excursion
8510         (when (or quietly
8511                   (not gnus-interactive-catchup) ;Without confirmation?
8512                   gnus-expert-user
8513                   (gnus-y-or-n-p
8514                    (if all
8515                        "Mark absolutely all articles as read? "
8516                      "Mark all unread articles as read? ")))
8517           (if (and not-mark
8518                    (not gnus-newsgroup-adaptive)
8519                    (not gnus-newsgroup-auto-expire)
8520                    (not gnus-suppress-duplicates)
8521                    (or (not gnus-use-cache)
8522                        (eq gnus-use-cache 'passive)))
8523               (progn
8524                 (when all
8525                   (setq gnus-newsgroup-marked nil
8526                         gnus-newsgroup-dormant nil))
8527                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8528             ;; We actually mark all articles as canceled, which we
8529             ;; have to do when using auto-expiry or adaptive scoring.
8530             (gnus-summary-show-all-threads)
8531             (when (gnus-summary-first-subject (not all) t)
8532               (while (and
8533                       (if to-here (< (point) to-here) t)
8534                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8535                       (gnus-summary-find-next (not all) nil nil t))))
8536             (gnus-set-mode-line 'summary))
8537           t))
8538     (gnus-summary-position-point)))
8539
8540 (defun gnus-summary-catchup-to-here (&optional all)
8541   "Mark all unticked articles before the current one as read.
8542 If ALL is non-nil, also mark ticked and dormant articles as read."
8543   (interactive "P")
8544   (save-excursion
8545     (gnus-save-hidden-threads
8546       (let ((beg (point)))
8547         ;; We check that there are unread articles.
8548         (when (or all (gnus-summary-find-prev))
8549           (gnus-summary-catchup all t beg)))))
8550   (gnus-summary-position-point))
8551
8552 (defun gnus-summary-catchup-all (&optional quietly)
8553   "Mark all articles in this newsgroup as read."
8554   (interactive "P")
8555   (gnus-summary-catchup t quietly))
8556
8557 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8558   "Mark all unread articles in this group as read, then exit.
8559 If prefix argument ALL is non-nil, all articles are marked as read."
8560   (interactive "P")
8561   (when (gnus-summary-catchup all quietly nil 'fast)
8562     ;; Select next newsgroup or exit.
8563     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8564              (eq gnus-auto-select-next 'quietly))
8565         (gnus-summary-next-group nil)
8566       (gnus-summary-exit))))
8567
8568 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8569   "Mark all articles in this newsgroup as read, and then exit."
8570   (interactive "P")
8571   (gnus-summary-catchup-and-exit t quietly))
8572
8573 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8574   "Mark all articles in this group as read and select the next group.
8575 If given a prefix, mark all articles, unread as well as ticked, as
8576 read."
8577   (interactive "P")
8578   (save-excursion
8579     (gnus-summary-catchup all))
8580   (gnus-summary-next-group))
8581
8582 ;;;
8583 ;;; with article
8584 ;;;
8585
8586 (defmacro gnus-with-article (article &rest forms)
8587   "Select ARTICLE and perform FORMS in the original article buffer.
8588 Then replace the article with the result."
8589   `(progn
8590      ;; We don't want the article to be marked as read.
8591      (let (gnus-mark-article-hook)
8592        (gnus-summary-select-article t t nil ,article))
8593      (set-buffer gnus-original-article-buffer)
8594      ,@forms
8595      (if (not (gnus-check-backend-function
8596                'request-replace-article (car gnus-article-current)))
8597          (gnus-message 5 "Read-only group; not replacing")
8598        (unless (gnus-request-replace-article
8599                 ,article (car gnus-article-current)
8600                 (current-buffer) t)
8601          (error "Couldn't replace article")))
8602      ;; The cache and backlog have to be flushed somewhat.
8603      (when gnus-keep-backlog
8604        (gnus-backlog-remove-article
8605         (car gnus-article-current) (cdr gnus-article-current)))
8606      (when gnus-use-cache
8607        (gnus-cache-update-article
8608         (car gnus-article-current) (cdr gnus-article-current)))))
8609
8610 (put 'gnus-with-article 'lisp-indent-function 1)
8611 (put 'gnus-with-article 'edebug-form-spec '(form body))
8612
8613 ;; Thread-based commands.
8614
8615 (defun gnus-summary-articles-in-thread (&optional article)
8616   "Return a list of all articles in the current thread.
8617 If ARTICLE is non-nil, return all articles in the thread that starts
8618 with that article."
8619   (let* ((article (or article (gnus-summary-article-number)))
8620          (data (gnus-data-find-list article))
8621          (top-level (gnus-data-level (car data)))
8622          (top-subject
8623           (cond ((null gnus-thread-operation-ignore-subject)
8624                  (gnus-simplify-subject-re
8625                   (mail-header-subject (gnus-data-header (car data)))))
8626                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8627                  (gnus-simplify-subject-fuzzy
8628                   (mail-header-subject (gnus-data-header (car data)))))
8629                 (t nil)))
8630          (end-point (save-excursion
8631                       (if (gnus-summary-go-to-next-thread)
8632                           (point) (point-max))))
8633          articles)
8634     (while (and data
8635                 (< (gnus-data-pos (car data)) end-point))
8636       (when (or (not top-subject)
8637                 (string= top-subject
8638                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8639                              (gnus-simplify-subject-fuzzy
8640                               (mail-header-subject
8641                                (gnus-data-header (car data))))
8642                            (gnus-simplify-subject-re
8643                             (mail-header-subject
8644                              (gnus-data-header (car data)))))))
8645         (push (gnus-data-number (car data)) articles))
8646       (unless (and (setq data (cdr data))
8647                    (> (gnus-data-level (car data)) top-level))
8648         (setq data nil)))
8649     ;; Return the list of articles.
8650     (nreverse articles)))
8651
8652 (defun gnus-summary-rethread-current ()
8653   "Rethread the thread the current article is part of."
8654   (interactive)
8655   (let* ((gnus-show-threads t)
8656          (article (gnus-summary-article-number))
8657          (id (mail-header-id (gnus-summary-article-header)))
8658          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8659     (unless id
8660       (error "No article on the current line"))
8661     (gnus-rebuild-thread id)
8662     (gnus-summary-goto-subject article)))
8663
8664 (defun gnus-summary-reparent-thread ()
8665   "Make the current article child of the marked (or previous) article.
8666
8667 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8668 is non-nil or the Subject: of both articles are the same."
8669   (interactive)
8670   (unless (not (gnus-group-read-only-p))
8671     (error "The current newsgroup does not support article editing"))
8672   (unless (<= (length gnus-newsgroup-processable) 1)
8673     (error "No more than one article may be marked"))
8674   (save-window-excursion
8675     (let ((gnus-article-buffer " *reparent*")
8676           (current-article (gnus-summary-article-number))
8677           ;; First grab the marked article, otherwise one line up.
8678           (parent-article (if (not (null gnus-newsgroup-processable))
8679                               (car gnus-newsgroup-processable)
8680                             (save-excursion
8681                               (if (eq (forward-line -1) 0)
8682                                   (gnus-summary-article-number)
8683                                 (error "Beginning of summary buffer"))))))
8684       (unless (not (eq current-article parent-article))
8685         (error "An article may not be self-referential"))
8686       (let ((message-id (mail-header-id
8687                          (gnus-summary-article-header parent-article))))
8688         (unless (and message-id (not (equal message-id "")))
8689           (error "No message-id in desired parent"))
8690         (gnus-with-article current-article
8691           (save-restriction
8692             (goto-char (point-min))
8693             (message-narrow-to-head)
8694             (if (re-search-forward "^References: " nil t)
8695                 (progn
8696                   (re-search-forward "^[^ \t]" nil t)
8697                   (forward-line -1)
8698                   (end-of-line)
8699                   (insert " " message-id))
8700               (insert "References: " message-id "\n"))))
8701         (set-buffer gnus-summary-buffer)
8702         (gnus-summary-unmark-all-processable)
8703         (gnus-summary-update-article current-article)
8704         (gnus-summary-rethread-current)
8705         (gnus-message 3 "Article %d is now the child of article %d"
8706                       current-article parent-article)))))
8707
8708 (defun gnus-summary-toggle-threads (&optional arg)
8709   "Toggle showing conversation threads.
8710 If ARG is positive number, turn showing conversation threads on."
8711   (interactive "P")
8712   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8713     (setq gnus-show-threads
8714           (if (null arg) (not gnus-show-threads)
8715             (> (prefix-numeric-value arg) 0)))
8716     (gnus-summary-prepare)
8717     (gnus-summary-goto-subject current)
8718     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8719     (gnus-summary-position-point)))
8720
8721 (defun gnus-summary-show-all-threads ()
8722   "Show all threads."
8723   (interactive)
8724   (save-excursion
8725     (let ((buffer-read-only nil))
8726       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8727   (gnus-summary-position-point))
8728
8729 (defun gnus-summary-show-thread ()
8730   "Show thread subtrees.
8731 Returns nil if no thread was there to be shown."
8732   (interactive)
8733   (let ((buffer-read-only nil)
8734         (orig (point))
8735         ;; first goto end then to beg, to have point at beg after let
8736         (end (progn (end-of-line) (point)))
8737         (beg (progn (beginning-of-line) (point))))
8738     (prog1
8739         ;; Any hidden lines here?
8740         (search-forward "\r" end t)
8741       (subst-char-in-region beg end ?\^M ?\n t)
8742       (goto-char orig)
8743       (gnus-summary-position-point))))
8744
8745 (defun gnus-summary-hide-all-threads ()
8746   "Hide all thread subtrees."
8747   (interactive)
8748   (save-excursion
8749     (goto-char (point-min))
8750     (gnus-summary-hide-thread)
8751     (while (zerop (gnus-summary-next-thread 1 t))
8752       (gnus-summary-hide-thread)))
8753   (gnus-summary-position-point))
8754
8755 (defun gnus-summary-hide-thread ()
8756   "Hide thread subtrees.
8757 Returns nil if no threads were there to be hidden."
8758   (interactive)
8759   (let ((buffer-read-only nil)
8760         (start (point))
8761         (article (gnus-summary-article-number)))
8762     (goto-char start)
8763     ;; Go forward until either the buffer ends or the subthread
8764     ;; ends.
8765     (when (and (not (eobp))
8766                (or (zerop (gnus-summary-next-thread 1 t))
8767                    (goto-char (point-max))))
8768       (prog1
8769           (if (and (> (point) start)
8770                    (search-backward "\n" start t))
8771               (progn
8772                 (subst-char-in-region start (point) ?\n ?\^M)
8773                 (gnus-summary-goto-subject article))
8774             (goto-char start)
8775             nil)))))
8776
8777 (defun gnus-summary-go-to-next-thread (&optional previous)
8778   "Go to the same level (or less) next thread.
8779 If PREVIOUS is non-nil, go to previous thread instead.
8780 Return the article number moved to, or nil if moving was impossible."
8781   (let ((level (gnus-summary-thread-level))
8782         (way (if previous -1 1))
8783         (beg (point)))
8784     (forward-line way)
8785     (while (and (not (eobp))
8786                 (< level (gnus-summary-thread-level)))
8787       (forward-line way))
8788     (if (eobp)
8789         (progn
8790           (goto-char beg)
8791           nil)
8792       (setq beg (point))
8793       (prog1
8794           (gnus-summary-article-number)
8795         (goto-char beg)))))
8796
8797 (defun gnus-summary-next-thread (n &optional silent)
8798   "Go to the same level next N'th thread.
8799 If N is negative, search backward instead.
8800 Returns the difference between N and the number of skips actually
8801 done.
8802
8803 If SILENT, don't output messages."
8804   (interactive "p")
8805   (let ((backward (< n 0))
8806         (n (abs n)))
8807     (while (and (> n 0)
8808                 (gnus-summary-go-to-next-thread backward))
8809       (decf n))
8810     (unless silent
8811       (gnus-summary-position-point))
8812     (when (and (not silent) (/= 0 n))
8813       (gnus-message 7 "No more threads"))
8814     n))
8815
8816 (defun gnus-summary-prev-thread (n)
8817   "Go to the same level previous N'th thread.
8818 Returns the difference between N and the number of skips actually
8819 done."
8820   (interactive "p")
8821   (gnus-summary-next-thread (- n)))
8822
8823 (defun gnus-summary-go-down-thread ()
8824   "Go down one level in the current thread."
8825   (let ((children (gnus-summary-article-children)))
8826     (when children
8827       (gnus-summary-goto-subject (car children)))))
8828
8829 (defun gnus-summary-go-up-thread ()
8830   "Go up one level in the current thread."
8831   (let ((parent (gnus-summary-article-parent)))
8832     (when parent
8833       (gnus-summary-goto-subject parent))))
8834
8835 (defun gnus-summary-down-thread (n)
8836   "Go down thread N steps.
8837 If N is negative, go up instead.
8838 Returns the difference between N and how many steps down that were
8839 taken."
8840   (interactive "p")
8841   (let ((up (< n 0))
8842         (n (abs n)))
8843     (while (and (> n 0)
8844                 (if up (gnus-summary-go-up-thread)
8845                   (gnus-summary-go-down-thread)))
8846       (setq n (1- n)))
8847     (gnus-summary-position-point)
8848     (when (/= 0 n)
8849       (gnus-message 7 "Can't go further"))
8850     n))
8851
8852 (defun gnus-summary-up-thread (n)
8853   "Go up thread N steps.
8854 If N is negative, go up instead.
8855 Returns the difference between N and how many steps down that were
8856 taken."
8857   (interactive "p")
8858   (gnus-summary-down-thread (- n)))
8859
8860 (defun gnus-summary-top-thread ()
8861   "Go to the top of the thread."
8862   (interactive)
8863   (while (gnus-summary-go-up-thread))
8864   (gnus-summary-article-number))
8865
8866 (defun gnus-summary-kill-thread (&optional unmark)
8867   "Mark articles under current thread as read.
8868 If the prefix argument is positive, remove any kinds of marks.
8869 If the prefix argument is negative, tick articles instead."
8870   (interactive "P")
8871   (when unmark
8872     (setq unmark (prefix-numeric-value unmark)))
8873   (let ((articles (gnus-summary-articles-in-thread)))
8874     (save-excursion
8875       ;; Expand the thread.
8876       (gnus-summary-show-thread)
8877       ;; Mark all the articles.
8878       (while articles
8879         (gnus-summary-goto-subject (car articles))
8880         (cond ((null unmark)
8881                (gnus-summary-mark-article-as-read gnus-killed-mark))
8882               ((> unmark 0)
8883                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8884               (t
8885                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8886         (setq articles (cdr articles))))
8887     ;; Hide killed subtrees.
8888     (and (null unmark)
8889          gnus-thread-hide-killed
8890          (gnus-summary-hide-thread))
8891     ;; If marked as read, go to next unread subject.
8892     (when (null unmark)
8893       ;; Go to next unread subject.
8894       (gnus-summary-next-subject 1 t)))
8895   (gnus-set-mode-line 'summary))
8896
8897 ;; Summary sorting commands
8898
8899 (defun gnus-summary-sort-by-number (&optional reverse)
8900   "Sort the summary buffer by article number.
8901 Argument REVERSE means reverse order."
8902   (interactive "P")
8903   (gnus-summary-sort 'number reverse))
8904
8905 (defun gnus-summary-sort-by-author (&optional reverse)
8906   "Sort the summary buffer by author name alphabetically.
8907 If `case-fold-search' is non-nil, case of letters is ignored.
8908 Argument REVERSE means reverse order."
8909   (interactive "P")
8910   (gnus-summary-sort 'author reverse))
8911
8912 (defun gnus-summary-sort-by-subject (&optional reverse)
8913   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8914 If `case-fold-search' is non-nil, case of letters is ignored.
8915 Argument REVERSE means reverse order."
8916   (interactive "P")
8917   (gnus-summary-sort 'subject reverse))
8918
8919 (defun gnus-summary-sort-by-date (&optional reverse)
8920   "Sort the summary buffer by date.
8921 Argument REVERSE means reverse order."
8922   (interactive "P")
8923   (gnus-summary-sort 'date reverse))
8924
8925 (defun gnus-summary-sort-by-score (&optional reverse)
8926   "Sort the summary buffer by score.
8927 Argument REVERSE means reverse order."
8928   (interactive "P")
8929   (gnus-summary-sort 'score reverse))
8930
8931 (defun gnus-summary-sort-by-lines (&optional reverse)
8932   "Sort the summary buffer by the number of lines.
8933 Argument REVERSE means reverse order."
8934   (interactive "P")
8935   (gnus-summary-sort 'lines reverse))
8936
8937 (defun gnus-summary-sort-by-chars (&optional reverse)
8938   "Sort the summary buffer by article length.
8939 Argument REVERSE means reverse order."
8940   (interactive "P")
8941   (gnus-summary-sort 'chars reverse))
8942
8943 (defun gnus-summary-sort (predicate reverse)
8944   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8945   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8946          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8947          (gnus-thread-sort-functions
8948           (if (not reverse)
8949               thread
8950             `(lambda (t1 t2)
8951                (,thread t2 t1))))
8952          (gnus-article-sort-functions
8953           (if (not reverse)
8954               article
8955             `(lambda (t1 t2)
8956                (,article t2 t1))))
8957          (buffer-read-only)
8958          (gnus-summary-prepare-hook nil))
8959     ;; We do the sorting by regenerating the threads.
8960     (gnus-summary-prepare)
8961     ;; Hide subthreads if needed.
8962     (when (and gnus-show-threads gnus-thread-hide-subtree)
8963       (gnus-summary-hide-all-threads))))
8964
8965 ;; Summary saving commands.
8966
8967 (defun gnus-summary-save-article (&optional n not-saved)
8968   "Save the current article using the default saver function.
8969 If N is a positive number, save the N next articles.
8970 If N is a negative number, save the N previous articles.
8971 If N is nil and any articles have been marked with the process mark,
8972 save those articles instead.
8973 The variable `gnus-default-article-saver' specifies the saver function."
8974   (interactive "P")
8975   (let* ((articles (gnus-summary-work-articles n))
8976          (save-buffer (save-excursion
8977                         (nnheader-set-temp-buffer " *Gnus Save*")))
8978          (num (length articles))
8979          header file)
8980     (dolist (article articles)
8981       (setq header (gnus-summary-article-header article))
8982       (if (not (vectorp header))
8983           ;; This is a pseudo-article.
8984           (if (assq 'name header)
8985               (gnus-copy-file (cdr (assq 'name header)))
8986             (gnus-message 1 "Article %d is unsaveable" article))
8987         ;; This is a real article.
8988         (save-window-excursion
8989           (gnus-summary-select-article t nil nil article))
8990         (save-excursion
8991           (set-buffer save-buffer)
8992           (erase-buffer)
8993           (insert-buffer-substring gnus-original-article-buffer))
8994         (setq file (gnus-article-save save-buffer file num))
8995         (gnus-summary-remove-process-mark article)
8996         (unless not-saved
8997           (gnus-summary-set-saved-mark article))))
8998     (gnus-kill-buffer save-buffer)
8999     (gnus-summary-position-point)
9000     (gnus-set-mode-line 'summary)
9001     n))
9002
9003 (defun gnus-summary-pipe-output (&optional arg)
9004   "Pipe the current article to a subprocess.
9005 If N is a positive number, pipe the N next articles.
9006 If N is a negative number, pipe the N previous articles.
9007 If N is nil and any articles have been marked with the process mark,
9008 pipe those articles instead."
9009   (interactive "P")
9010   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9011     (gnus-summary-save-article arg t))
9012   (gnus-configure-windows 'pipe))
9013
9014 (defun gnus-summary-save-article-mail (&optional arg)
9015   "Append the current article to an mail file.
9016 If N is a positive number, save the N next articles.
9017 If N is a negative number, save the N previous articles.
9018 If N is nil and any articles have been marked with the process mark,
9019 save those articles instead."
9020   (interactive "P")
9021   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9022     (gnus-summary-save-article arg)))
9023
9024 (defun gnus-summary-save-article-rmail (&optional arg)
9025   "Append the current article to an rmail file.
9026 If N is a positive number, save the N next articles.
9027 If N is a negative number, save the N previous articles.
9028 If N is nil and any articles have been marked with the process mark,
9029 save those articles instead."
9030   (interactive "P")
9031   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9032     (gnus-summary-save-article arg)))
9033
9034 (defun gnus-summary-save-article-file (&optional arg)
9035   "Append the current article to a file.
9036 If N is a positive number, save the N next articles.
9037 If N is a negative number, save the N previous articles.
9038 If N is nil and any articles have been marked with the process mark,
9039 save those articles instead."
9040   (interactive "P")
9041   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9042     (gnus-summary-save-article arg)))
9043
9044 (defun gnus-summary-write-article-file (&optional arg)
9045   "Write the current article to a file, deleting the previous file.
9046 If N is a positive number, save the N next articles.
9047 If N is a negative number, save the N previous articles.
9048 If N is nil and any articles have been marked with the process mark,
9049 save those articles instead."
9050   (interactive "P")
9051   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9052     (gnus-summary-save-article arg)))
9053
9054 (defun gnus-summary-save-article-body-file (&optional arg)
9055   "Append the current article body to a file.
9056 If N is a positive number, save the N next articles.
9057 If N is a negative number, save the N previous articles.
9058 If N is nil and any articles have been marked with the process mark,
9059 save those articles instead."
9060   (interactive "P")
9061   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9062     (gnus-summary-save-article arg)))
9063
9064 (defun gnus-summary-pipe-message (program)
9065   "Pipe the current article through PROGRAM."
9066   (interactive "sProgram: ")
9067   (gnus-summary-select-article)
9068   (let ((mail-header-separator ""))
9069     (gnus-eval-in-buffer-window gnus-article-buffer
9070       (save-restriction
9071         (widen)
9072         (let ((start (window-start))
9073               buffer-read-only)
9074           (message-pipe-buffer-body program)
9075           (set-window-start (get-buffer-window (current-buffer)) start))))))
9076
9077 (defun gnus-get-split-value (methods)
9078   "Return a value based on the split METHODS."
9079   (let (split-name method result match)
9080     (when methods
9081       (save-excursion
9082         (set-buffer gnus-original-article-buffer)
9083         (save-restriction
9084           (nnheader-narrow-to-headers)
9085           (while methods
9086             (goto-char (point-min))
9087             (setq method (pop methods))
9088             (setq match (car method))
9089             (when (cond
9090                    ((stringp match)
9091                     ;; Regular expression.
9092                     (ignore-errors
9093                       (re-search-forward match nil t)))
9094                    ((gnus-functionp match)
9095                     ;; Function.
9096                     (save-restriction
9097                       (widen)
9098                       (setq result (funcall match gnus-newsgroup-name))))
9099                    ((consp match)
9100                     ;; Form.
9101                     (save-restriction
9102                       (widen)
9103                       (setq result (eval match)))))
9104               (setq split-name (append (cdr method) split-name))
9105               (cond ((stringp result)
9106                      (push (expand-file-name
9107                             result gnus-article-save-directory)
9108                            split-name))
9109                     ((consp result)
9110                      (setq split-name (append result split-name)))))))))
9111     (nreverse split-name)))
9112
9113 (defun gnus-valid-move-group-p (group)
9114   (and (boundp group)
9115        (symbol-name group)
9116        (symbol-value group)
9117        (gnus-get-function (gnus-find-method-for-group
9118                            (symbol-name group)) 'request-accept-article t)))
9119
9120 (defun gnus-read-move-group-name (prompt default articles prefix)
9121   "Read a group name."
9122   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9123          (minibuffer-confirm-incomplete nil) ; XEmacs
9124          (prom
9125           (format "%s %s to:"
9126                   prompt
9127                   (if (> (length articles) 1)
9128                       (format "these %d articles" (length articles))
9129                     "this article")))
9130          (to-newsgroup
9131           (cond
9132            ((null split-name)
9133             (gnus-completing-read default prom
9134                                   gnus-active-hashtb
9135                                   'gnus-valid-move-group-p
9136                                   nil prefix
9137                                   'gnus-group-history))
9138            ((= 1 (length split-name))
9139             (gnus-completing-read (car split-name) prom
9140                                   gnus-active-hashtb
9141                                   'gnus-valid-move-group-p
9142                                   nil nil
9143                                   'gnus-group-history))
9144            (t
9145             (gnus-completing-read nil prom
9146                                   (mapcar (lambda (el) (list el))
9147                                           (nreverse split-name))
9148                                   nil nil nil
9149                                   'gnus-group-history))))
9150          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9151     (when to-newsgroup
9152       (if (or (string= to-newsgroup "")
9153               (string= to-newsgroup prefix))
9154           (setq to-newsgroup default))
9155       (unless to-newsgroup
9156         (error "No group name entered"))
9157       (or (gnus-active to-newsgroup)
9158           (gnus-activate-group to-newsgroup nil nil to-method)
9159           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9160                                      to-newsgroup))
9161               (or (and (gnus-request-create-group to-newsgroup to-method)
9162                        (gnus-activate-group
9163                         to-newsgroup nil nil to-method)
9164                        (gnus-subscribe-group to-newsgroup))
9165                   (error "Couldn't create group %s" to-newsgroup)))
9166           (error "No such group: %s" to-newsgroup)))
9167     to-newsgroup))
9168
9169 (defun gnus-summary-save-parts (type dir n &optional reverse)
9170   "Save parts matching TYPE to DIR.
9171 If REVERSE, save parts that do not match TYPE."
9172   (interactive
9173    (list (read-string "Save parts of type: " "image/.*")
9174          (read-file-name "Save to directory: " nil nil t)
9175          current-prefix-arg))
9176   (gnus-summary-iterate n
9177     (let ((gnus-display-mime-function nil)
9178           (gnus-inhibit-treatment t))
9179       (gnus-summary-select-article))
9180     (save-excursion
9181       (set-buffer gnus-article-buffer)
9182       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9183         (when handles
9184           (gnus-summary-save-parts-1 type dir handles reverse)
9185           (mm-destroy-parts handles))))))
9186
9187 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9188   (if (stringp (car handle))
9189       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9190               (cdr handle))
9191     (when (if reverse
9192               (not (string-match type (mm-handle-media-type handle)))
9193             (string-match type (mm-handle-media-type handle)))
9194       (let ((file (expand-file-name
9195                    (file-name-nondirectory
9196                     (or
9197                      (mail-content-type-get
9198                       (mm-handle-disposition handle) 'filename)
9199                      (concat gnus-newsgroup-name
9200                              "." (number-to-string
9201                                   (cdr gnus-article-current)))))
9202                    dir)))
9203         (unless (file-exists-p file)
9204           (mm-save-part-to-file handle file))))))
9205
9206 ;; Summary extract commands
9207
9208 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9209   (let ((buffer-read-only nil)
9210         (article (gnus-summary-article-number))
9211         after-article b e)
9212     (unless (gnus-summary-goto-subject article)
9213       (error "No such article: %d" article))
9214     (gnus-summary-position-point)
9215     ;; If all commands are to be bunched up on one line, we collect
9216     ;; them here.
9217     (unless gnus-view-pseudos-separately
9218       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9219             files action)
9220         (while ps
9221           (setq action (cdr (assq 'action (car ps))))
9222           (setq files (list (cdr (assq 'name (car ps)))))
9223           (while (and ps (cdr ps)
9224                       (string= (or action "1")
9225                                (or (cdr (assq 'action (cadr ps))) "2")))
9226             (push (cdr (assq 'name (cadr ps))) files)
9227             (setcdr ps (cddr ps)))
9228           (when files
9229             (when (not (string-match "%s" action))
9230               (push " " files))
9231             (push " " files)
9232             (when (assq 'execute (car ps))
9233               (setcdr (assq 'execute (car ps))
9234                       (funcall (if (string-match "%s" action)
9235                                    'format 'concat)
9236                                action
9237                                (mapconcat
9238                                 (lambda (f)
9239                                   (if (equal f " ")
9240                                       f
9241                                     (mm-quote-arg f)))
9242                                 files " ")))))
9243           (setq ps (cdr ps)))))
9244     (if (and gnus-view-pseudos (not not-view))
9245         (while pslist
9246           (when (assq 'execute (car pslist))
9247             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9248                                   (eq gnus-view-pseudos 'not-confirm)))
9249           (setq pslist (cdr pslist)))
9250       (save-excursion
9251         (while pslist
9252           (setq after-article (or (cdr (assq 'article (car pslist)))
9253                                   (gnus-summary-article-number)))
9254           (gnus-summary-goto-subject after-article)
9255           (forward-line 1)
9256           (setq b (point))
9257           (insert "    " (file-name-nondirectory
9258                           (cdr (assq 'name (car pslist))))
9259                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9260           (setq e (point))
9261           (forward-line -1)             ; back to `b'
9262           (gnus-add-text-properties
9263            b (1- e) (list 'gnus-number gnus-reffed-article-number
9264                           gnus-mouse-face-prop gnus-mouse-face))
9265           (gnus-data-enter
9266            after-article gnus-reffed-article-number
9267            gnus-unread-mark b (car pslist) 0 (- e b))
9268           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9269           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9270           (setq pslist (cdr pslist)))))))
9271
9272 (defun gnus-pseudos< (p1 p2)
9273   (let ((c1 (cdr (assq 'action p1)))
9274         (c2 (cdr (assq 'action p2))))
9275     (and c1 c2 (string< c1 c2))))
9276
9277 (defun gnus-request-pseudo-article (props)
9278   (cond ((assq 'execute props)
9279          (gnus-execute-command (cdr (assq 'execute props)))))
9280   (let ((gnus-current-article (gnus-summary-article-number)))
9281     (gnus-run-hooks 'gnus-mark-article-hook)))
9282
9283 (defun gnus-execute-command (command &optional automatic)
9284   (save-excursion
9285     (gnus-article-setup-buffer)
9286     (set-buffer gnus-article-buffer)
9287     (setq buffer-read-only nil)
9288     (let ((command (if automatic command
9289                      (read-string "Command: " (cons command 0)))))
9290       (erase-buffer)
9291       (insert "$ " command "\n\n")
9292       (if gnus-view-pseudo-asynchronously
9293           (start-process "gnus-execute" (current-buffer) shell-file-name
9294                          shell-command-switch command)
9295         (call-process shell-file-name nil t nil
9296                       shell-command-switch command)))))
9297
9298 ;; Summary kill commands.
9299
9300 (defun gnus-summary-edit-global-kill (article)
9301   "Edit the \"global\" kill file."
9302   (interactive (list (gnus-summary-article-number)))
9303   (gnus-group-edit-global-kill article))
9304
9305 (defun gnus-summary-edit-local-kill ()
9306   "Edit a local kill file applied to the current newsgroup."
9307   (interactive)
9308   (setq gnus-current-headers (gnus-summary-article-header))
9309   (gnus-group-edit-local-kill
9310    (gnus-summary-article-number) gnus-newsgroup-name))
9311
9312 ;;; Header reading.
9313
9314 (defun gnus-read-header (id &optional header)
9315   "Read the headers of article ID and enter them into the Gnus system."
9316   (let ((group gnus-newsgroup-name)
9317         (gnus-override-method
9318          (or
9319           gnus-override-method
9320           (and (gnus-news-group-p gnus-newsgroup-name)
9321                (car (gnus-refer-article-methods)))))
9322         where)
9323     ;; First we check to see whether the header in question is already
9324     ;; fetched.
9325     (if (stringp id)
9326         ;; This is a Message-ID.
9327         (setq header (or header (gnus-id-to-header id)))
9328       ;; This is an article number.
9329       (setq header (or header (gnus-summary-article-header id))))
9330     (if (and header
9331              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9332         ;; We have found the header.
9333         header
9334       ;; If this is a sparse article, we have to nix out its
9335       ;; previous entry in the thread hashtb.
9336       (when (and header
9337                  (gnus-summary-article-sparse-p (mail-header-number header)))
9338         (let* ((parent (gnus-parent-id (mail-header-references header)))
9339                (thread (and parent (gnus-id-to-thread parent))))
9340           (when thread
9341             (delq (assq header thread) thread))))
9342       ;; We have to really fetch the header to this article.
9343       (save-excursion
9344         (set-buffer nntp-server-buffer)
9345         (when (setq where (gnus-request-head id group))
9346           (nnheader-fold-continuation-lines)
9347           (goto-char (point-max))
9348           (insert ".\n")
9349           (goto-char (point-min))
9350           (insert "211 ")
9351           (princ (cond
9352                   ((numberp id) id)
9353                   ((cdr where) (cdr where))
9354                   (header (mail-header-number header))
9355                   (t gnus-reffed-article-number))
9356                  (current-buffer))
9357           (insert " Article retrieved.\n"))
9358         (if (or (not where)
9359                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9360             ()                          ; Malformed head.
9361           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9362             (when (and (stringp id)
9363                        (not (string= (gnus-group-real-name group)
9364                                      (car where))))
9365               ;; If we fetched by Message-ID and the article came
9366               ;; from a different group, we fudge some bogus article
9367               ;; numbers for this article.
9368               (mail-header-set-number header gnus-reffed-article-number))
9369             (save-excursion
9370               (set-buffer gnus-summary-buffer)
9371               (decf gnus-reffed-article-number)
9372               (gnus-remove-header (mail-header-number header))
9373               (push header gnus-newsgroup-headers)
9374               (setq gnus-current-headers header)
9375               (push (mail-header-number header) gnus-newsgroup-limit)))
9376           header)))))
9377
9378 (defun gnus-remove-header (number)
9379   "Remove header NUMBER from `gnus-newsgroup-headers'."
9380   (if (and gnus-newsgroup-headers
9381            (= number (mail-header-number (car gnus-newsgroup-headers))))
9382       (pop gnus-newsgroup-headers)
9383     (let ((headers gnus-newsgroup-headers))
9384       (while (and (cdr headers)
9385                   (not (= number (mail-header-number (cadr headers)))))
9386         (pop headers))
9387       (when (cdr headers)
9388         (setcdr headers (cddr headers))))))
9389
9390 ;;;
9391 ;;; summary highlights
9392 ;;;
9393
9394 (defun gnus-highlight-selected-summary ()
9395   "Highlight selected article in summary buffer."
9396   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9397   (when gnus-summary-selected-face
9398     (save-excursion
9399       (let* ((beg (progn (beginning-of-line) (point)))
9400              (end (progn (end-of-line) (point)))
9401              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9402              (from (if (get-text-property beg gnus-mouse-face-prop)
9403                        beg
9404                      (or (next-single-property-change
9405                           beg gnus-mouse-face-prop nil end)
9406                          beg)))
9407              (to
9408               (if (= from end)
9409                   (- from 2)
9410                 (or (next-single-property-change
9411                      from gnus-mouse-face-prop nil end)
9412                     end))))
9413         ;; If no mouse-face prop on line we will have to = from = end,
9414         ;; so we highlight the entire line instead.
9415         (when (= (+ to 2) from)
9416           (setq from beg)
9417           (setq to end))
9418         (if gnus-newsgroup-selected-overlay
9419             ;; Move old overlay.
9420             (gnus-move-overlay
9421              gnus-newsgroup-selected-overlay from to (current-buffer))
9422           ;; Create new overlay.
9423           (gnus-overlay-put
9424            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9425            'face gnus-summary-selected-face))))))
9426
9427 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9428 (defun gnus-summary-highlight-line ()
9429   "Highlight current line according to `gnus-summary-highlight'."
9430   (let* ((list gnus-summary-highlight)
9431          (p (point))
9432          (end (progn (end-of-line) (point)))
9433          ;; now find out where the line starts and leave point there.
9434          (beg (progn (beginning-of-line) (point)))
9435          (article (gnus-summary-article-number))
9436          (score (or (cdr (assq (or article gnus-current-article)
9437                                gnus-newsgroup-scored))
9438                     gnus-summary-default-score 0))
9439          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9440          (inhibit-read-only t))
9441     ;; Eval the cars of the lists until we find a match.
9442     (let ((default gnus-summary-default-score))
9443       (while (and list
9444                   (not (eval (caar list))))
9445         (setq list (cdr list))))
9446     (let ((face (cdar list)))
9447       (unless (eq face (get-text-property beg 'face))
9448         (gnus-put-text-property-excluding-characters-with-faces
9449          beg end 'face
9450          (setq face (if (boundp face) (symbol-value face) face)))
9451         (when gnus-summary-highlight-line-function
9452           (funcall gnus-summary-highlight-line-function article face))))
9453     (goto-char p)))
9454
9455 (defun gnus-update-read-articles (group unread &optional compute)
9456   "Update the list of read articles in GROUP."
9457   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9458          (entry (gnus-gethash group gnus-newsrc-hashtb))
9459          (info (nth 2 entry))
9460          (prev 1)
9461          (unread (sort (copy-sequence unread) '<))
9462          read)
9463     (if (or (not info) (not active))
9464         ;; There is no info on this group if it was, in fact,
9465         ;; killed.  Gnus stores no information on killed groups, so
9466         ;; there's nothing to be done.
9467         ;; One could store the information somewhere temporarily,
9468         ;; perhaps...  Hmmm...
9469         ()
9470       ;; Remove any negative articles numbers.
9471       (while (and unread (< (car unread) 0))
9472         (setq unread (cdr unread)))
9473       ;; Remove any expired article numbers
9474       (while (and unread (< (car unread) (car active)))
9475         (setq unread (cdr unread)))
9476       ;; Compute the ranges of read articles by looking at the list of
9477       ;; unread articles.
9478       (while unread
9479         (when (/= (car unread) prev)
9480           (push (if (= prev (1- (car unread))) prev
9481                   (cons prev (1- (car unread))))
9482                 read))
9483         (setq prev (1+ (car unread)))
9484         (setq unread (cdr unread)))
9485       (when (<= prev (cdr active))
9486         (push (cons prev (cdr active)) read))
9487       (setq read (if (> (length read) 1) (nreverse read) read))
9488       (if compute
9489           read
9490         (save-excursion
9491           (set-buffer gnus-group-buffer)
9492           (gnus-undo-register
9493             `(progn
9494                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9495                (gnus-info-set-read ',info ',(gnus-info-read info))
9496                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9497                (gnus-group-update-group ,group t))))
9498         ;; Propagate the read marks to the backend.
9499         (if (gnus-check-backend-function 'request-set-mark group)
9500             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9501                   (add (gnus-remove-from-range read (gnus-info-read info))))
9502               (when (or add del)
9503                 (unless (gnus-check-group group)
9504                   (error "Can't open server for %s" group))
9505                 (gnus-request-set-mark
9506                  group (delq nil (list (if add (list add 'add '(read)))
9507                                        (if del (list del 'del '(read)))))))))
9508         ;; Enter this list into the group info.
9509         (gnus-info-set-read info read)
9510         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9511         (gnus-get-unread-articles-in-group info (gnus-active group))
9512         t))))
9513
9514 (defun gnus-offer-save-summaries ()
9515   "Offer to save all active summary buffers."
9516   (save-excursion
9517     (let ((buflist (buffer-list))
9518           buffers bufname)
9519       ;; Go through all buffers and find all summaries.
9520       (while buflist
9521         (and (setq bufname (buffer-name (car buflist)))
9522              (string-match "Summary" bufname)
9523              (save-excursion
9524                (set-buffer bufname)
9525                ;; We check that this is, indeed, a summary buffer.
9526                (and (eq major-mode 'gnus-summary-mode)
9527                     ;; Also make sure this isn't bogus.
9528                     gnus-newsgroup-prepared
9529                     ;; Also make sure that this isn't a dead summary buffer.
9530                     (not gnus-dead-summary-mode)))
9531              (push bufname buffers))
9532         (setq buflist (cdr buflist)))
9533       ;; Go through all these summary buffers and offer to save them.
9534       (when buffers
9535         (map-y-or-n-p
9536          "Update summary buffer %s? "
9537          (lambda (buf)
9538            (switch-to-buffer buf)
9539            (gnus-summary-exit))
9540          buffers)))))
9541
9542 (defun gnus-summary-setup-default-charset ()
9543   "Setup newsgroup default charset."
9544   (if (equal gnus-newsgroup-name "nndraft:drafts")
9545       (setq gnus-newsgroup-charset nil)
9546     (let* ((name (and gnus-newsgroup-name
9547                       (gnus-group-real-name gnus-newsgroup-name)))
9548            (ignored-charsets
9549             (or gnus-newsgroup-ephemeral-ignored-charsets
9550                 (append
9551                  (and gnus-newsgroup-name
9552                       (or (gnus-group-find-parameter gnus-newsgroup-name
9553                                                      'ignored-charsets t)
9554                           (let ((alist gnus-group-ignored-charsets-alist)
9555                                 elem (charsets nil))
9556                             (while (setq elem (pop alist))
9557                               (when (and name
9558                                          (string-match (car elem) name))
9559                                 (setq alist nil
9560                                       charsets (cdr elem))))
9561                             charsets)))
9562                  gnus-newsgroup-ignored-charsets))))
9563       (setq gnus-newsgroup-charset
9564             (or gnus-newsgroup-ephemeral-charset
9565                 (and gnus-newsgroup-name
9566                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9567                          (let ((alist gnus-group-charset-alist)
9568                                elem charset)
9569                            (while (setq elem (pop alist))
9570                              (when (and name
9571                                         (string-match (car elem) name))
9572                                (setq alist nil
9573                                      charset (cadr elem))))
9574                            charset)))
9575                 gnus-default-charset))
9576       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9577            ignored-charsets))))
9578
9579 ;;;
9580 ;;; Mime Commands
9581 ;;;
9582
9583 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9584   "Display the current article buffer fully MIME-buttonized.
9585 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9586 treated as multipart/mixed."
9587   (interactive "P")
9588   (require 'gnus-art)
9589   (let ((gnus-unbuttonized-mime-types nil)
9590         (gnus-mime-display-multipart-as-mixed show-all-parts))
9591     (gnus-summary-show-article)))
9592
9593 (defun gnus-summary-repair-multipart (article)
9594   "Add a Content-Type header to a multipart article without one."
9595   (interactive (list (gnus-summary-article-number)))
9596   (gnus-with-article article
9597     (message-narrow-to-head)
9598     (goto-char (point-max))
9599     (widen)
9600     (when (search-forward "\n--" nil t)
9601       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9602         (message-narrow-to-head)
9603         (message-remove-header "Mime-Version")
9604         (message-remove-header "Content-Type")
9605         (goto-char (point-max))
9606         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9607                         separator))
9608         (insert "Mime-Version: 1.0\n")
9609         (widen))))
9610   (let (gnus-mark-article-hook)
9611     (gnus-summary-select-article t t nil article)))
9612
9613 (defun gnus-summary-toggle-display-buttonized ()
9614   "Toggle the buttonizing of the article buffer."
9615   (interactive)
9616   (require 'gnus-art)
9617   (if (setq gnus-inhibit-mime-unbuttonizing
9618             (not gnus-inhibit-mime-unbuttonizing))
9619       (let ((gnus-unbuttonized-mime-types nil))
9620         (gnus-summary-show-article))
9621     (gnus-summary-show-article)))
9622
9623 ;;;
9624 ;;; Generic summary marking commands
9625 ;;;
9626
9627 (defvar gnus-summary-marking-alist
9628   '((read gnus-del-mark "d")
9629     (unread gnus-unread-mark "u")
9630     (ticked gnus-ticked-mark "!")
9631     (dormant gnus-dormant-mark "?")
9632     (expirable gnus-expirable-mark "e"))
9633   "An alist of names/marks/keystrokes.")
9634
9635 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9636 (defvar gnus-summary-mark-map)
9637
9638 (defun gnus-summary-make-all-marking-commands ()
9639   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9640   (dolist (elem gnus-summary-marking-alist)
9641     (apply 'gnus-summary-make-marking-command elem)))
9642
9643 (defun gnus-summary-make-marking-command (name mark keystroke)
9644   (let ((map (make-sparse-keymap)))
9645     (define-key gnus-summary-generic-mark-map keystroke map)
9646     (dolist (lway `((next "next" next nil "n")
9647                     (next-unread "next unread" next t "N")
9648                     (prev "previous" prev nil "p")
9649                     (prev-unread "previous unread" prev t "P")
9650                     (nomove "" nil nil ,keystroke)))
9651       (let ((func (gnus-summary-make-marking-command-1
9652                    mark (car lway) lway name)))
9653         (setq func (eval func))
9654         (define-key map (nth 4 lway) func)))))
9655       
9656 (defun gnus-summary-make-marking-command-1 (mark way lway name)
9657   `(defun ,(intern
9658             (format "gnus-summary-put-mark-as-%s%s"
9659                     name (if (eq way 'nomove)
9660                              ""
9661                            (concat "-" (symbol-name way)))))
9662      (n)
9663      ,(format
9664        "Mark the current article as %s%s.
9665 If N, the prefix, then repeat N times.
9666 If N is negative, move in reverse order.
9667 The difference between N and the actual number of articles marked is
9668 returned."
9669        name (cadr lway))
9670      (interactive "p")
9671      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9672     
9673 (defun gnus-summary-generic-mark (n mark move unread)
9674   "Mark N articles with MARK."
9675   (unless (eq major-mode 'gnus-summary-mode)
9676     (error "This command can only be used in the summary buffer"))
9677   (gnus-summary-show-thread)
9678   (let ((nummove
9679          (cond
9680           ((eq move 'next) 1)
9681           ((eq move 'prev) -1)
9682           (t 0))))
9683     (if (zerop nummove)
9684         (setq n 1)
9685       (when (< n 0)
9686         (setq n (abs n)
9687               nummove (* -1 nummove))))
9688     (while (and (> n 0)
9689                 (gnus-summary-mark-article nil mark)
9690                 (zerop (gnus-summary-next-subject nummove unread t)))
9691       (setq n (1- n)))
9692     (when (/= 0 n)
9693       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9694     (gnus-summary-recenter)
9695     (gnus-summary-position-point)
9696     (gnus-set-mode-line 'summary)
9697     n))
9698
9699 (gnus-summary-make-all-marking-commands)
9700
9701 (gnus-ems-redefine)
9702
9703 (provide 'gnus-sum)
9704
9705 (run-hooks 'gnus-sum-load-hook)
9706
9707 ;;; gnus-sum.el ends here