* gnus.el: Fix copyright statements.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
40
41 (defcustom gnus-kill-summary-on-exit t
42   "*If non-nil, kill the summary buffer when you exit from it.
43 If nil, the summary will become a \"*Dead Summary*\" buffer, and
44 it will be killed sometime later."
45   :group 'gnus-summary-exit
46   :type 'boolean)
47
48 (defcustom gnus-fetch-old-headers nil
49   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
50 If an unread article in the group refers to an older, already read (or
51 just marked as read) article, the old article will not normally be
52 displayed in the Summary buffer.  If this variable is non-nil, Gnus
53 will attempt to grab the headers to the old articles, and thereby
54 build complete threads.  If it has the value `some', only enough
55 headers to connect otherwise loose threads will be displayed.  This
56 variable can also be a number.  In that case, no more than that number
57 of old headers will be fetched.  If it has the value `invisible', all
58 old headers will be fetched, but none will be displayed.
59
60 The server has to support NOV for any of this to work."
61   :group 'gnus-thread
62   :type '(choice (const :tag "off" nil)
63                  (const some)
64                  number
65                  (sexp :menu-tag "other" t)))
66
67 (defcustom gnus-refer-thread-limit 200
68   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
69 If t, fetch all the available old headers."
70   :group 'gnus-thread
71   :type '(choice number
72                  (sexp :menu-tag "other" t)))
73
74 (defcustom gnus-summary-make-false-root 'adopt
75   "*nil means that Gnus won't gather loose threads.
76 If the root of a thread has expired or been read in a previous
77 session, the information necessary to build a complete thread has been
78 lost.  Instead of having many small sub-threads from this original thread
79 scattered all over the summary buffer, Gnus can gather them.
80
81 If non-nil, Gnus will try to gather all loose sub-threads from an
82 original thread into one large thread.
83
84 If this variable is non-nil, it should be one of `none', `adopt',
85 `dummy' or `empty'.
86
87 If this variable is `none', Gnus will not make a false root, but just
88 present the sub-threads after another.
89 If this variable is `dummy', Gnus will create a dummy root that will
90 have all the sub-threads as children.
91 If this variable is `adopt', Gnus will make one of the \"children\"
92 the parent and mark all the step-children as such.
93 If this variable is `empty', the \"children\" are printed with empty
94 subject fields.  (Or rather, they will be printed with a string
95 given by the `gnus-summary-same-subject' variable.)"
96   :group 'gnus-thread
97   :type '(choice (const :tag "off" nil)
98                  (const none)
99                  (const dummy)
100                  (const adopt)
101                  (const empty)))
102
103 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
104   "*A regexp to match subjects to be excluded from loose thread gathering.
105 As loose thread gathering is done on subjects only, that means that
106 there can be many false gatherings performed.  By rooting out certain
107 common subjects, gathering might become saner."
108   :group 'gnus-thread
109   :type 'regexp)
110
111 (defcustom gnus-summary-gather-subject-limit nil
112   "*Maximum length of subject comparisons when gathering loose threads.
113 Use nil to compare full subjects.  Setting this variable to a low
114 number will help gather threads that have been corrupted by
115 newsreaders chopping off subject lines, but it might also mean that
116 unrelated articles that have subject that happen to begin with the
117 same few characters will be incorrectly gathered.
118
119 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
120 comparing subjects."
121   :group 'gnus-thread
122   :type '(choice (const :tag "off" nil)
123                  (const fuzzy)
124                  (sexp :menu-tag "on" t)))
125
126 (defcustom gnus-simplify-subject-functions nil
127   "List of functions taking a string argument that simplify subjects.
128 The functions are applied recursively.
129
130 Useful functions to put in this list include: `gnus-simplify-subject-re',
131 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
132   :group 'gnus-thread
133   :type '(repeat function))
134
135 (defcustom gnus-simplify-ignored-prefixes nil
136   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
137   :group 'gnus-thread
138   :type '(choice (const :tag "off" nil)
139                  regexp))
140
141 (defcustom gnus-build-sparse-threads nil
142   "*If non-nil, fill in the gaps in threads.
143 If `some', only fill in the gaps that are needed to tie loose threads
144 together.  If `more', fill in all leaf nodes that Gnus can find.  If
145 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
146   :group 'gnus-thread
147   :type '(choice (const :tag "off" nil)
148                  (const some)
149                  (const more)
150                  (sexp :menu-tag "all" t)))
151
152 (defcustom gnus-summary-thread-gathering-function
153   'gnus-gather-threads-by-subject
154   "*Function used for gathering loose threads.
155 There are two pre-defined functions: `gnus-gather-threads-by-subject',
156 which only takes Subjects into consideration; and
157 `gnus-gather-threads-by-references', which compared the References
158 headers of the articles to find matches."
159   :group 'gnus-thread
160   :type '(radio (function-item gnus-gather-threads-by-subject)
161                 (function-item gnus-gather-threads-by-references)
162                 (function :tag "other")))
163
164 (defcustom gnus-summary-same-subject ""
165   "*String indicating that the current article has the same subject as the previous.
166 This variable will only be used if the value of
167 `gnus-summary-make-false-root' is `empty'."
168   :group 'gnus-summary-format
169   :type 'string)
170
171 (defcustom gnus-summary-goto-unread t
172   "*If t, many commands will go to the next unread article.
173 This applies to marking commands as well as other commands that
174 \"naturally\" select the next article, like, for instance, `SPC' at
175 the end of an article.
176
177 If nil, the marking commands do NOT go to the next unread article
178 (they go to the next article instead).  If `never', commands that
179 usually go to the next unread article, will go to the next article,
180 whether it is read or not."
181   :group 'gnus-summary-marks
182   :link '(custom-manual "(gnus)Setting Marks")
183   :type '(choice (const :tag "off" nil)
184                  (const never)
185                  (sexp :menu-tag "on" t)))
186
187 (defcustom gnus-summary-default-score 0
188   "*Default article score level.
189 All scores generated by the score files will be added to this score.
190 If this variable is nil, scoring will be disabled."
191   :group 'gnus-score-default
192   :type '(choice (const :tag "disable")
193                  integer))
194
195 (defcustom gnus-summary-zcore-fuzz 0
196   "*Fuzziness factor for the zcore in the summary buffer.
197 Articles with scores closer than this to `gnus-summary-default-score'
198 will not be marked."
199   :group 'gnus-summary-format
200   :type 'integer)
201
202 (defcustom gnus-simplify-subject-fuzzy-regexp nil
203   "*Strings to be removed when doing fuzzy matches.
204 This can either be a regular expression or list of regular expressions
205 that will be removed from subject strings if fuzzy subject
206 simplification is selected."
207   :group 'gnus-thread
208   :type '(repeat regexp))
209
210 (defcustom gnus-show-threads t
211   "*If non-nil, display threads in summary mode."
212   :group 'gnus-thread
213   :type 'boolean)
214
215 (defcustom gnus-thread-hide-subtree nil
216   "*If non-nil, hide all threads initially.
217 If threads are hidden, you have to run the command
218 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
219 to expose hidden threads."
220   :group 'gnus-thread
221   :type 'boolean)
222
223 (defcustom gnus-thread-hide-killed t
224   "*If non-nil, hide killed threads automatically."
225   :group 'gnus-thread
226   :type 'boolean)
227
228 (defcustom gnus-thread-ignore-subject t
229   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
230 If nil, articles that have different subjects from their parents will
231 start separate threads."
232   :group 'gnus-thread
233   :type 'boolean)
234
235 (defcustom gnus-thread-operation-ignore-subject t
236   "*If non-nil, subjects will be ignored when doing thread commands.
237 This affects commands like `gnus-summary-kill-thread' and
238 `gnus-summary-lower-thread'.
239
240 If this variable is nil, articles in the same thread with different
241 subjects will not be included in the operation in question.  If this
242 variable is `fuzzy', only articles that have subjects that are fuzzily
243 equal will be included."
244   :group 'gnus-thread
245   :type '(choice (const :tag "off" nil)
246                  (const fuzzy)
247                  (sexp :tag "on" t)))
248
249 (defcustom gnus-thread-indent-level 4
250   "*Number that says how much each sub-thread should be indented."
251   :group 'gnus-thread
252   :type 'integer)
253
254 (defcustom gnus-auto-extend-newsgroup t
255   "*If non-nil, extend newsgroup forward and backward when requested."
256   :group 'gnus-summary-choose
257   :type 'boolean)
258
259 (defcustom gnus-auto-select-first t
260   "*If nil, don't select the first unread article when entering a group.
261 If this variable is `best', select the highest-scored unread article
262 in the group.  If t, select the first unread article.
263
264 This variable can also be a function to place point on a likely
265 subject line.  Useful values include `gnus-summary-first-unread-subject',
266 `gnus-summary-first-unread-article' and
267 `gnus-summary-best-unread-article'.
268
269 If you want to prevent automatic selection of the first unread article
270 in some newsgroups, set the variable to nil in
271 `gnus-select-group-hook'."
272   :group 'gnus-group-select
273   :type '(choice (const :tag "none" nil)
274                  (const best)
275                  (sexp :menu-tag "first" t)
276                  (function-item gnus-summary-first-unread-subject)
277                  (function-item gnus-summary-first-unread-article)
278                  (function-item gnus-summary-best-unread-article)))
279
280 (defcustom gnus-auto-select-next t
281   "*If non-nil, offer to go to the next group from the end of the previous.
282 If the value is t and the next newsgroup is empty, Gnus will exit
283 summary mode and go back to group mode.  If the value is neither nil
284 nor t, Gnus will select the following unread newsgroup.  In
285 particular, if the value is the symbol `quietly', the next unread
286 newsgroup will be selected without any confirmation, and if it is
287 `almost-quietly', the next group will be selected without any
288 confirmation if you are located on the last article in the group.
289 Finally, if this variable is `slightly-quietly', the `Z n' command
290 will go to the next group without confirmation."
291   :group 'gnus-summary-maneuvering
292   :type '(choice (const :tag "off" nil)
293                  (const quietly)
294                  (const almost-quietly)
295                  (const slightly-quietly)
296                  (sexp :menu-tag "on" t)))
297
298 (defcustom gnus-auto-select-same nil
299   "*If non-nil, select the next article with the same subject.
300 If there are no more articles with the same subject, go to
301 the first unread article."
302   :group 'gnus-summary-maneuvering
303   :type 'boolean)
304
305 (defcustom gnus-summary-check-current nil
306   "*If non-nil, consider the current article when moving.
307 The \"unread\" movement commands will stay on the same line if the
308 current article is unread."
309   :group 'gnus-summary-maneuvering
310   :type 'boolean)
311
312 (defcustom gnus-auto-center-summary t
313   "*If non-nil, always center the current summary buffer.
314 In particular, if `vertical' do only vertical recentering.  If non-nil
315 and non-`vertical', do both horizontal and vertical recentering."
316   :group 'gnus-summary-maneuvering
317   :type '(choice (const :tag "none" nil)
318                  (const vertical)
319                  (integer :tag "height")
320                  (sexp :menu-tag "both" t)))
321
322 (defcustom gnus-show-all-headers nil
323   "*If non-nil, don't hide any headers."
324   :group 'gnus-article-hiding
325   :group 'gnus-article-headers
326   :type 'boolean)
327
328 (defcustom gnus-summary-ignore-duplicates nil
329   "*If non-nil, ignore articles with identical Message-ID headers."
330   :group 'gnus-summary
331   :type 'boolean)
332
333 (defcustom gnus-single-article-buffer t
334   "*If non-nil, display all articles in the same buffer.
335 If nil, each group will get its own article buffer."
336   :group 'gnus-article-various
337   :type 'boolean)
338
339 (defcustom gnus-break-pages t
340   "*If non-nil, do page breaking on articles.
341 The page delimiter is specified by the `gnus-page-delimiter'
342 variable."
343   :group 'gnus-article-various
344   :type 'boolean)
345
346 (defcustom gnus-move-split-methods nil
347   "*Variable used to suggest where articles are to be moved to.
348 It uses the same syntax as the `gnus-split-methods' variable."
349   :group 'gnus-summary-mail
350   :type '(repeat (choice (list :value (fun) function)
351                          (cons :value ("" "") regexp (repeat string))
352                          (sexp :value nil))))
353
354 (defcustom gnus-unread-mark ?  ;Whitespace
355   "*Mark used for unread articles."
356   :group 'gnus-summary-marks
357   :type 'character)
358
359 (defcustom gnus-ticked-mark ?!
360   "*Mark used for ticked articles."
361   :group 'gnus-summary-marks
362   :type 'character)
363
364 (defcustom gnus-dormant-mark ??
365   "*Mark used for dormant articles."
366   :group 'gnus-summary-marks
367   :type 'character)
368
369 (defcustom gnus-del-mark ?r
370   "*Mark used for del'd articles."
371   :group 'gnus-summary-marks
372   :type 'character)
373
374 (defcustom gnus-read-mark ?R
375   "*Mark used for read articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-expirable-mark ?E
380   "*Mark used for expirable articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-killed-mark ?K
385   "*Mark used for killed articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-souped-mark ?F
390   "*Mark used for killed articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-kill-file-mark ?X
395   "*Mark used for articles killed by kill files."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-low-score-mark ?Y
400   "*Mark used for articles with a low score."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-catchup-mark ?C
405   "*Mark used for articles that are caught up."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-replied-mark ?A
410   "*Mark used for articles that have been replied to."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-cached-mark ?*
415   "*Mark used for articles that are in the cache."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-saved-mark ?S
420   "*Mark used for articles that have been saved to."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-ancient-mark ?O
425   "*Mark used for ancient articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-sparse-mark ?Q
430   "*Mark used for sparsely reffed articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-canceled-mark ?G
435   "*Mark used for canceled articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-duplicate-mark ?M
440   "*Mark used for duplicate articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-undownloaded-mark ?@
445   "*Mark used for articles that weren't downloaded."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-downloadable-mark ?%
450   "*Mark used for articles that are to be downloaded."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-unsendable-mark ?=
455   "*Mark used for articles that won't be sent."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-score-over-mark ?+
460   "*Score mark used for articles with high scores."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-score-below-mark ?-
465   "*Score mark used for articles with low scores."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-empty-thread-mark ?  ;Whitespace
470   "*There is no thread under the article."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-not-empty-thread-mark ?=
475   "*There is a thread under the article."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-view-pseudo-asynchronously nil
480   "*If non-nil, Gnus will view pseudo-articles asynchronously."
481   :group 'gnus-extract-view
482   :type 'boolean)
483
484 (defcustom gnus-auto-expirable-marks
485   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
486         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
487         gnus-souped-mark gnus-duplicate-mark)
488   "*The list of marks converted into expiration if a group is auto-expirable."
489   :group 'gnus-summary
490   :type '(repeat character))
491
492 (defcustom gnus-inhibit-user-auto-expire t
493   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
494   :group 'gnus-summary
495   :type 'boolean)
496
497 (defcustom gnus-view-pseudos nil
498   "*If `automatic', pseudo-articles will be viewed automatically.
499 If `not-confirm', pseudos will be viewed automatically, and the user
500 will not be asked to confirm the command."
501   :group 'gnus-extract-view
502   :type '(choice (const :tag "off" nil)
503                  (const automatic)
504                  (const not-confirm)))
505
506 (defcustom gnus-view-pseudos-separately t
507   "*If non-nil, one pseudo-article will be created for each file to be viewed.
508 If nil, all files that use the same viewing command will be given as a
509 list of parameters to that command."
510   :group 'gnus-extract-view
511   :type 'boolean)
512
513 (defcustom gnus-insert-pseudo-articles t
514   "*If non-nil, insert pseudo-articles when decoding articles."
515   :group 'gnus-extract-view
516   :type 'boolean)
517
518 (defcustom gnus-summary-dummy-line-format
519   "  %(:                          :%) %S\n"
520   "*The format specification for the dummy roots in the summary buffer.
521 It works along the same lines as a normal formatting string,
522 with some simple extensions.
523
524 %S  The subject"
525   :group 'gnus-threading
526   :type 'string)
527
528 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
529   "*The format specification for the summary mode line.
530 It works along the same lines as a normal formatting string,
531 with some simple extensions:
532
533 %G  Group name
534 %p  Unprefixed group name
535 %A  Current article number
536 %z  Current article score
537 %V  Gnus version
538 %U  Number of unread articles in the group
539 %e  Number of unselected articles in the group
540 %Z  A string with unread/unselected article counts
541 %g  Shortish group name
542 %S  Subject of the current article
543 %u  User-defined spec
544 %s  Current score file name
545 %d  Number of dormant articles
546 %r  Number of articles that have been marked as read in this session
547 %E  Number of articles expunged by the score files"
548   :group 'gnus-summary-format
549   :type 'string)
550
551 (defcustom gnus-list-identifiers nil
552   "Regexp that matches list identifiers to be removed from subject.
553 This can also be a list of regexps."
554   :group 'gnus-summary-format
555   :group 'gnus-article-hiding
556   :type '(choice (const :tag "none" nil)
557                  (regexp :value ".*")
558                  (repeat :value (".*") regexp)))
559
560 (defcustom gnus-summary-mark-below 0
561   "*Mark all articles with a score below this variable as read.
562 This variable is local to each summary buffer and usually set by the
563 score file."
564   :group 'gnus-score-default
565   :type 'integer)
566
567 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
568   "*List of functions used for sorting articles in the summary buffer.
569 This variable is only used when not using a threaded display."
570   :group 'gnus-summary-sort
571   :type '(repeat (choice (function-item gnus-article-sort-by-number)
572                          (function-item gnus-article-sort-by-author)
573                          (function-item gnus-article-sort-by-subject)
574                          (function-item gnus-article-sort-by-date)
575                          (function-item gnus-article-sort-by-score)
576                          (function :tag "other"))))
577
578 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
579   "*List of functions used for sorting threads in the summary buffer.
580 By default, threads are sorted by article number.
581
582 Each function takes two threads and return non-nil if the first thread
583 should be sorted before the other.  If you use more than one function,
584 the primary sort function should be the last.  You should probably
585 always include `gnus-thread-sort-by-number' in the list of sorting
586 functions -- preferably first.
587
588 Ready-made functions include `gnus-thread-sort-by-number',
589 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
590 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
591 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
592   :group 'gnus-summary-sort
593   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
594                          (function-item gnus-thread-sort-by-author)
595                          (function-item gnus-thread-sort-by-subject)
596                          (function-item gnus-thread-sort-by-date)
597                          (function-item gnus-thread-sort-by-score)
598                          (function-item gnus-thread-sort-by-total-score)
599                          (function :tag "other"))))
600
601 (defcustom gnus-thread-score-function '+
602   "*Function used for calculating the total score of a thread.
603
604 The function is called with the scores of the article and each
605 subthread and should then return the score of the thread.
606
607 Some functions you can use are `+', `max', or `min'."
608   :group 'gnus-summary-sort
609   :type 'function)
610
611 (defcustom gnus-summary-expunge-below nil
612   "All articles that have a score less than this variable will be expunged.
613 This variable is local to the summary buffers."
614   :group 'gnus-score-default
615   :type '(choice (const :tag "off" nil)
616                  integer))
617
618 (defcustom gnus-thread-expunge-below nil
619   "All threads that have a total score less than this variable will be expunged.
620 See `gnus-thread-score-function' for en explanation of what a
621 \"thread score\" is.
622
623 This variable is local to the summary buffers."
624   :group 'gnus-threading
625   :group 'gnus-score-default
626   :type '(choice (const :tag "off" nil)
627                  integer))
628
629 (defcustom gnus-summary-mode-hook nil
630   "*A hook for Gnus summary mode.
631 This hook is run before any variables are set in the summary buffer."
632   :group 'gnus-summary-various
633   :type 'hook)
634
635 (defcustom gnus-summary-menu-hook nil
636   "*Hook run after the creation of the summary mode menu."
637   :group 'gnus-summary-visual
638   :type 'hook)
639
640 (defcustom gnus-summary-exit-hook nil
641   "*A hook called on exit from the summary buffer.
642 It will be called with point in the group buffer."
643   :group 'gnus-summary-exit
644   :type 'hook)
645
646 (defcustom gnus-summary-prepare-hook nil
647   "*A hook called after the summary buffer has been generated.
648 If you want to modify the summary buffer, you can use this hook."
649   :group 'gnus-summary-various
650   :type 'hook)
651
652 (defcustom gnus-summary-prepared-hook nil
653   "*A hook called as the last thing after the summary buffer has been generated."
654   :group 'gnus-summary-various
655   :type 'hook)
656
657 (defcustom gnus-summary-generate-hook nil
658   "*A hook run just before generating the summary buffer.
659 This hook is commonly used to customize threading variables and the
660 like."
661   :group 'gnus-summary-various
662   :type 'hook)
663
664 (defcustom gnus-select-group-hook nil
665   "*A hook called when a newsgroup is selected.
666
667 If you'd like to simplify subjects like the
668 `gnus-summary-next-same-subject' command does, you can use the
669 following hook:
670
671  (setq gnus-select-group-hook
672       (list
673         (lambda ()
674           (mapcar (lambda (header)
675                      (mail-header-set-subject
676                       header
677                       (gnus-simplify-subject
678                        (mail-header-subject header) 're-only)))
679                   gnus-newsgroup-headers))))"
680   :group 'gnus-group-select
681   :type 'hook)
682
683 (defcustom gnus-select-article-hook nil
684   "*A hook called when an article is selected."
685   :group 'gnus-summary-choose
686   :type 'hook)
687
688 (defcustom gnus-visual-mark-article-hook
689   (list 'gnus-highlight-selected-summary)
690   "*Hook run after selecting an article in the summary buffer.
691 It is meant to be used for highlighting the article in some way.  It
692 is not run if `gnus-visual' is nil."
693   :group 'gnus-summary-visual
694   :type 'hook)
695
696 (defcustom gnus-parse-headers-hook nil
697   "*A hook called before parsing the headers."
698   :group 'gnus-various
699   :type 'hook)
700
701 (defcustom gnus-exit-group-hook nil
702   "*A hook called when exiting summary mode.
703 This hook is not called from the non-updating exit commands like `Q'."
704   :group 'gnus-various
705   :type 'hook)
706
707 (defcustom gnus-summary-update-hook
708   (list 'gnus-summary-highlight-line)
709   "*A hook called when a summary line is changed.
710 The hook will not be called if `gnus-visual' is nil.
711
712 The default function `gnus-summary-highlight-line' will
713 highlight the line according to the `gnus-summary-highlight'
714 variable."
715   :group 'gnus-summary-visual
716   :type 'hook)
717
718 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
719   "*A hook called when an article is selected for the first time.
720 The hook is intended to mark an article as read (or unread)
721 automatically when it is selected."
722   :group 'gnus-summary-choose
723   :type 'hook)
724
725 (defcustom gnus-group-no-more-groups-hook nil
726   "*A hook run when returning to group mode having no more (unread) groups."
727   :group 'gnus-group-select
728   :type 'hook)
729
730 (defcustom gnus-ps-print-hook nil
731   "*A hook run before ps-printing something from Gnus."
732   :group 'gnus-summary
733   :type 'hook)
734
735 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
736   "Face used for highlighting the current article in the summary buffer."
737   :group 'gnus-summary-visual
738   :type 'face)
739
740 (defcustom gnus-summary-highlight
741   '(((= mark gnus-canceled-mark)
742      . gnus-summary-cancelled-face)
743     ((and (> score default)
744           (or (= mark gnus-dormant-mark)
745               (= mark gnus-ticked-mark)))
746      . gnus-summary-high-ticked-face)
747     ((and (< score default)
748           (or (= mark gnus-dormant-mark)
749               (= mark gnus-ticked-mark)))
750      . gnus-summary-low-ticked-face)
751     ((or (= mark gnus-dormant-mark)
752          (= mark gnus-ticked-mark))
753      . gnus-summary-normal-ticked-face)
754     ((and (> score default) (= mark gnus-ancient-mark))
755      . gnus-summary-high-ancient-face)
756     ((and (< score default) (= mark gnus-ancient-mark))
757      . gnus-summary-low-ancient-face)
758     ((= mark gnus-ancient-mark)
759      . gnus-summary-normal-ancient-face)
760     ((and (> score default) (= mark gnus-unread-mark))
761      . gnus-summary-high-unread-face)
762     ((and (< score default) (= mark gnus-unread-mark))
763      . gnus-summary-low-unread-face)
764     ((= mark gnus-unread-mark)
765      . gnus-summary-normal-unread-face)
766     ((and (> score default) (memq mark (list gnus-downloadable-mark
767                                              gnus-undownloaded-mark)))
768      . gnus-summary-high-unread-face)
769     ((and (< score default) (memq mark (list gnus-downloadable-mark
770                                              gnus-undownloaded-mark)))
771      . gnus-summary-low-unread-face)
772     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
773      . gnus-summary-normal-unread-face)
774     ((> score default)
775      . gnus-summary-high-read-face)
776     ((< score default)
777      . gnus-summary-low-read-face)
778     (t
779      . gnus-summary-normal-read-face))
780   "*Controls the highlighting of summary buffer lines.
781
782 A list of (FORM . FACE) pairs.  When deciding how a a particular
783 summary line should be displayed, each form is evaluated.  The content
784 of the face field after the first true form is used.  You can change
785 how those summary lines are displayed, by editing the face field.
786
787 You can use the following variables in the FORM field.
788
789 score:   The articles score
790 default: The default article score.
791 below:   The score below which articles are automatically marked as read.
792 mark:    The articles mark."
793   :group 'gnus-summary-visual
794   :type '(repeat (cons (sexp :tag "Form" nil)
795                        face)))
796
797 (defcustom gnus-alter-header-function nil
798   "Function called to allow alteration of article header structures.
799 The function is called with one parameter, the article header vector,
800 which it may alter in any way.")
801
802 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
803   "Variable that says which function should be used to decode a string with encoded words.")
804
805 (defcustom gnus-extra-headers nil
806   "*Extra headers to parse."
807   :group 'gnus-summary
808   :type '(repeat symbol))
809
810 (defcustom gnus-ignored-from-addresses
811   (and user-mail-address (regexp-quote user-mail-address))
812   "*Regexp of From headers that may be suppressed in favor of To headers."
813   :group 'gnus-summary
814   :type 'regexp)
815
816 (defcustom gnus-group-charset-alist
817   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
818     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
819     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
820     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
821     ("^relcom\\>" koi8-r)
822     ("^fido7\\>" koi8-r)
823     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
824     ("^israel\\>" iso-8859-1)
825     ("^han\\>" euc-kr)
826     ("^alt.chinese.text.big5\\>" chinese-big5)
827     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
828     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
829     (".*" iso-8859-1))
830   "Alist of regexps (to match group names) and default charsets to be used when reading."
831   :type '(repeat (list (regexp :tag "Group")
832                        (symbol :tag "Charset")))
833   :group 'gnus-charset)
834
835 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
836   "List of charsets that should be ignored.
837 When these charsets are used in the \"charset\" parameter, the
838 default charset will be used instead."
839   :type '(repeat symbol)
840   :group 'gnus-charset)
841
842 (defcustom gnus-group-ignored-charsets-alist 
843   '(("alt\\.chinese\\.text" iso-8859-1))
844   "Alist of regexps (to match group names) and charsets that should be ignored.
845 When these charsets are used in the \"charset\" parameter, the
846 default charset will be used instead."
847   :type '(repeat (cons (regexp :tag "Group")
848                        (repeat symbol)))
849   :group 'gnus-charset)
850
851 (defcustom gnus-group-highlight-words-alist nil
852   "Alist of group regexps and highlight regexps.
853 This variable uses the same syntax as `gnus-emphasis-alist'."
854   :type '(repeat (cons (regexp :tag "Group")
855                        (repeat (list (regexp :tag "Highlight regexp")
856                                      (number :tag "Group for entire word" 0)
857                                      (number :tag "Group for displayed part" 0)
858                                      (symbol :tag "Face" 
859                                              gnus-emphasis-highlight-words)))))
860   :group 'gnus-summary-visual)
861
862 (defcustom gnus-summary-show-article-charset-alist
863   nil
864   "Alist of number and charset.
865 The article will be shown with the charset corresponding to the
866 numbered argument.
867 For example: ((1 . cn-gb-2312) (2 . big5))."
868   :type '(repeat (cons (number :tag "Argument" 1)
869                        (symbol :tag "Charset")))
870   :group 'gnus-charset)
871
872 (defcustom gnus-preserve-marks t
873   "Whether marks are preserved when moving, copying and respooling messages."
874   :type 'boolean
875   :group 'gnus-summary-marks)
876
877 (defcustom gnus-alter-articles-to-read-function nil
878   "Function to be called to alter the list of articles to be selected."
879   :type 'function
880   :group 'gnus-summary)
881
882 ;;; Internal variables
883
884 (defvar gnus-article-mime-handles nil)
885 (defvar gnus-article-decoded-p nil)
886 (defvar gnus-scores-exclude-files nil)
887 (defvar gnus-page-broken nil)
888 (defvar gnus-inhibit-mime-unbuttonizing nil)
889
890 (defvar gnus-original-article nil)
891 (defvar gnus-article-internal-prepare-hook nil)
892 (defvar gnus-newsgroup-process-stack nil)
893
894 (defvar gnus-thread-indent-array nil)
895 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
896 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
897   "Function called to sort the articles within a thread after it has been gathered together.")
898
899 ;; Avoid highlighting in kill files.
900 (defvar gnus-summary-inhibit-highlight nil)
901 (defvar gnus-newsgroup-selected-overlay nil)
902 (defvar gnus-inhibit-limiting nil)
903 (defvar gnus-newsgroup-adaptive-score-file nil)
904 (defvar gnus-current-score-file nil)
905 (defvar gnus-current-move-group nil)
906 (defvar gnus-current-copy-group nil)
907 (defvar gnus-current-crosspost-group nil)
908
909 (defvar gnus-newsgroup-dependencies nil)
910 (defvar gnus-newsgroup-adaptive nil)
911 (defvar gnus-summary-display-article-function nil)
912 (defvar gnus-summary-highlight-line-function nil
913   "Function called after highlighting a summary line.")
914
915 (defvar gnus-summary-line-format-alist
916   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
917     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
918     (?s gnus-tmp-subject-or-nil ?s)
919     (?n gnus-tmp-name ?s)
920     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
921         ?s)
922     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
923             gnus-tmp-from) ?s)
924     (?F gnus-tmp-from ?s)
925     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
926     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
927     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
928     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
929     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
930     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
931     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
932     (?L gnus-tmp-lines ?d)
933     (?I gnus-tmp-indentation ?s)
934     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
935     (?R gnus-tmp-replied ?c)
936     (?\[ gnus-tmp-opening-bracket ?c)
937     (?\] gnus-tmp-closing-bracket ?c)
938     (?\> (make-string gnus-tmp-level ? ) ?s)
939     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
940     (?i gnus-tmp-score ?d)
941     (?z gnus-tmp-score-char ?c)
942     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
943     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
944     (?U gnus-tmp-unread ?c)
945     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
946     (?t (gnus-summary-number-of-articles-in-thread
947          (and (boundp 'thread) (car thread)) gnus-tmp-level)
948         ?d)
949     (?e (gnus-summary-number-of-articles-in-thread
950          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
951         ?c)
952     (?u gnus-tmp-user-defined ?s)
953     (?P (gnus-pick-line-number) ?d))
954   "An alist of format specifications that can appear in summary lines,
955 and what variables they correspond with, along with the type of the
956 variable (string, integer, character, etc).")
957
958 (defvar gnus-summary-dummy-line-format-alist
959   `((?S gnus-tmp-subject ?s)
960     (?N gnus-tmp-number ?d)
961     (?u gnus-tmp-user-defined ?s)))
962
963 (defvar gnus-summary-mode-line-format-alist
964   `((?G gnus-tmp-group-name ?s)
965     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
966     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
967     (?A gnus-tmp-article-number ?d)
968     (?Z gnus-tmp-unread-and-unselected ?s)
969     (?V gnus-version ?s)
970     (?U gnus-tmp-unread-and-unticked ?d)
971     (?S gnus-tmp-subject ?s)
972     (?e gnus-tmp-unselected ?d)
973     (?u gnus-tmp-user-defined ?s)
974     (?d (length gnus-newsgroup-dormant) ?d)
975     (?t (length gnus-newsgroup-marked) ?d)
976     (?r (length gnus-newsgroup-reads) ?d)
977     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
978     (?E gnus-newsgroup-expunged-tally ?d)
979     (?s (gnus-current-score-file-nondirectory) ?s)))
980
981 (defvar gnus-last-search-regexp nil
982   "Default regexp for article search command.")
983
984 (defvar gnus-last-shell-command nil
985   "Default shell command on article.")
986
987 (defvar gnus-newsgroup-begin nil)
988 (defvar gnus-newsgroup-end nil)
989 (defvar gnus-newsgroup-last-rmail nil)
990 (defvar gnus-newsgroup-last-mail nil)
991 (defvar gnus-newsgroup-last-folder nil)
992 (defvar gnus-newsgroup-last-file nil)
993 (defvar gnus-newsgroup-auto-expire nil)
994 (defvar gnus-newsgroup-active nil)
995
996 (defvar gnus-newsgroup-data nil)
997 (defvar gnus-newsgroup-data-reverse nil)
998 (defvar gnus-newsgroup-limit nil)
999 (defvar gnus-newsgroup-limits nil)
1000
1001 (defvar gnus-newsgroup-unreads nil
1002   "List of unread articles in the current newsgroup.")
1003
1004 (defvar gnus-newsgroup-unselected nil
1005   "List of unselected unread articles in the current newsgroup.")
1006
1007 (defvar gnus-newsgroup-reads nil
1008   "Alist of read articles and article marks in the current newsgroup.")
1009
1010 (defvar gnus-newsgroup-expunged-tally nil)
1011
1012 (defvar gnus-newsgroup-marked nil
1013   "List of ticked articles in the current newsgroup (a subset of unread art).")
1014
1015 (defvar gnus-newsgroup-killed nil
1016   "List of ranges of articles that have been through the scoring process.")
1017
1018 (defvar gnus-newsgroup-cached nil
1019   "List of articles that come from the article cache.")
1020
1021 (defvar gnus-newsgroup-saved nil
1022   "List of articles that have been saved.")
1023
1024 (defvar gnus-newsgroup-kill-headers nil)
1025
1026 (defvar gnus-newsgroup-replied nil
1027   "List of articles that have been replied to in the current newsgroup.")
1028
1029 (defvar gnus-newsgroup-expirable nil
1030   "List of articles in the current newsgroup that can be expired.")
1031
1032 (defvar gnus-newsgroup-processable nil
1033   "List of articles in the current newsgroup that can be processed.")
1034
1035 (defvar gnus-newsgroup-downloadable nil
1036   "List of articles in the current newsgroup that can be processed.")
1037
1038 (defvar gnus-newsgroup-undownloaded nil
1039   "List of articles in the current newsgroup that haven't been downloaded..")
1040
1041 (defvar gnus-newsgroup-unsendable nil
1042   "List of articles in the current newsgroup that won't be sent.")
1043
1044 (defvar gnus-newsgroup-bookmarks nil
1045   "List of articles in the current newsgroup that have bookmarks.")
1046
1047 (defvar gnus-newsgroup-dormant nil
1048   "List of dormant articles in the current newsgroup.")
1049
1050 (defvar gnus-newsgroup-scored nil
1051   "List of scored articles in the current newsgroup.")
1052
1053 (defvar gnus-newsgroup-headers nil
1054   "List of article headers in the current newsgroup.")
1055
1056 (defvar gnus-newsgroup-threads nil)
1057
1058 (defvar gnus-newsgroup-prepared nil
1059   "Whether the current group has been prepared properly.")
1060
1061 (defvar gnus-newsgroup-ancient nil
1062   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1063
1064 (defvar gnus-newsgroup-sparse nil)
1065
1066 (defvar gnus-current-article nil)
1067 (defvar gnus-article-current nil)
1068 (defvar gnus-current-headers nil)
1069 (defvar gnus-have-all-headers nil)
1070 (defvar gnus-last-article nil)
1071 (defvar gnus-newsgroup-history nil)
1072 (defvar gnus-newsgroup-charset nil)
1073 (defvar gnus-newsgroup-ephemeral-charset nil)
1074 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1075
1076 (defconst gnus-summary-local-variables
1077   '(gnus-newsgroup-name
1078     gnus-newsgroup-begin gnus-newsgroup-end
1079     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1080     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1081     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1082     gnus-newsgroup-unselected gnus-newsgroup-marked
1083     gnus-newsgroup-reads gnus-newsgroup-saved
1084     gnus-newsgroup-replied gnus-newsgroup-expirable
1085     gnus-newsgroup-processable gnus-newsgroup-killed
1086     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1087     gnus-newsgroup-unsendable
1088     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1089     gnus-newsgroup-headers gnus-newsgroup-threads
1090     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1091     gnus-current-article gnus-current-headers gnus-have-all-headers
1092     gnus-last-article gnus-article-internal-prepare-hook
1093     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1094     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1095     gnus-thread-expunge-below
1096     gnus-score-alist gnus-current-score-file
1097     (gnus-summary-expunge-below . global)
1098     (gnus-summary-mark-below . global)
1099     gnus-newsgroup-active gnus-scores-exclude-files
1100     gnus-newsgroup-history gnus-newsgroup-ancient
1101     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1102     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1103     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1104     (gnus-newsgroup-expunged-tally . 0)
1105     gnus-cache-removable-articles gnus-newsgroup-cached
1106     gnus-newsgroup-data gnus-newsgroup-data-reverse
1107     gnus-newsgroup-limit gnus-newsgroup-limits
1108     gnus-newsgroup-charset)
1109   "Variables that are buffer-local to the summary buffers.")
1110
1111 ;; Byte-compiler warning.
1112 (defvar gnus-article-mode-map)
1113
1114 ;; MIME stuff.
1115
1116 (defvar gnus-decode-encoded-word-methods
1117   '(mail-decode-encoded-word-string)
1118   "List of methods used to decode encoded words.
1119
1120 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1121 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1122 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1123 whose names match REGEXP.
1124
1125 For example:
1126 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1127  mail-decode-encoded-word-string
1128  (\"chinese\" . rfc1843-decode-string))
1129 ")
1130
1131 (defvar gnus-decode-encoded-word-methods-cache nil)
1132
1133 (defun gnus-multi-decode-encoded-word-string (string)
1134   "Apply the functions from `gnus-encoded-word-methods' that match."
1135   (unless (and gnus-decode-encoded-word-methods-cache
1136                (eq gnus-newsgroup-name
1137                    (car gnus-decode-encoded-word-methods-cache)))
1138     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1139     (mapcar (lambda (x)
1140               (if (symbolp x)
1141                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1142                 (if (and gnus-newsgroup-name
1143                          (string-match (car x) gnus-newsgroup-name))
1144                     (nconc gnus-decode-encoded-word-methods-cache
1145                            (list (cdr x))))))
1146           gnus-decode-encoded-word-methods))
1147   (let ((xlist gnus-decode-encoded-word-methods-cache))
1148     (pop xlist)
1149     (while xlist
1150       (setq string (funcall (pop xlist) string))))
1151   string)
1152
1153 ;; Subject simplification.
1154
1155 (defun gnus-simplify-whitespace (str)
1156   "Remove excessive whitespace."
1157   (let ((mystr str))
1158     ;; Multiple spaces.
1159     (while (string-match "[ \t][ \t]+" mystr)
1160       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1161                           " "
1162                           (substring mystr (match-end 0)))))
1163     ;; Leading spaces.
1164     (when (string-match "^[ \t]+" mystr)
1165       (setq mystr (substring mystr (match-end 0))))
1166     ;; Trailing spaces.
1167     (when (string-match "[ \t]+$" mystr)
1168       (setq mystr (substring mystr 0 (match-beginning 0))))
1169     mystr))
1170
1171 (defsubst gnus-simplify-subject-re (subject)
1172   "Remove \"Re:\" from subject lines."
1173   (if (string-match "^[Rr][Ee]: *" subject)
1174       (substring subject (match-end 0))
1175     subject))
1176
1177 (defun gnus-simplify-subject (subject &optional re-only)
1178   "Remove `Re:' and words in parentheses.
1179 If RE-ONLY is non-nil, strip leading `Re:'s only."
1180   (let ((case-fold-search t))           ;Ignore case.
1181     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1182     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1183       (setq subject (substring subject (match-end 0))))
1184     ;; Remove uninteresting prefixes.
1185     (when (and (not re-only)
1186                gnus-simplify-ignored-prefixes
1187                (string-match gnus-simplify-ignored-prefixes subject))
1188       (setq subject (substring subject (match-end 0))))
1189     ;; Remove words in parentheses from end.
1190     (unless re-only
1191       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1192         (setq subject (substring subject 0 (match-beginning 0)))))
1193     ;; Return subject string.
1194     subject))
1195
1196 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1197 ;; all whitespace.
1198 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1199   (goto-char (point-min))
1200   (while (re-search-forward regexp nil t)
1201     (replace-match (or newtext ""))))
1202
1203 (defun gnus-simplify-buffer-fuzzy ()
1204   "Simplify string in the buffer fuzzily.
1205 The string in the accessible portion of the current buffer is simplified.
1206 It is assumed to be a single-line subject.
1207 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1208 matter is removed.  Additional things can be deleted by setting
1209 gnus-simplify-subject-fuzzy-regexp."
1210   (let ((case-fold-search t)
1211         (modified-tick))
1212     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1213
1214     (while (not (eq modified-tick (buffer-modified-tick)))
1215       (setq modified-tick (buffer-modified-tick))
1216       (cond
1217        ((listp gnus-simplify-subject-fuzzy-regexp)
1218         (mapcar 'gnus-simplify-buffer-fuzzy-step
1219                 gnus-simplify-subject-fuzzy-regexp))
1220        (gnus-simplify-subject-fuzzy-regexp
1221         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1222       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1223       (gnus-simplify-buffer-fuzzy-step
1224        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1225       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1226
1227     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1228     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1229     (gnus-simplify-buffer-fuzzy-step " $")
1230     (gnus-simplify-buffer-fuzzy-step "^ +")))
1231
1232 (defun gnus-simplify-subject-fuzzy (subject)
1233   "Simplify a subject string fuzzily.
1234 See `gnus-simplify-buffer-fuzzy' for details."
1235   (save-excursion
1236     (gnus-set-work-buffer)
1237     (let ((case-fold-search t))
1238       ;; Remove uninteresting prefixes.
1239       (when (and gnus-simplify-ignored-prefixes
1240                  (string-match gnus-simplify-ignored-prefixes subject))
1241         (setq subject (substring subject (match-end 0))))
1242       (insert subject)
1243       (inline (gnus-simplify-buffer-fuzzy))
1244       (buffer-string))))
1245
1246 (defsubst gnus-simplify-subject-fully (subject)
1247   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1248   (cond
1249    (gnus-simplify-subject-functions
1250     (gnus-map-function gnus-simplify-subject-functions subject))
1251    ((null gnus-summary-gather-subject-limit)
1252     (gnus-simplify-subject-re subject))
1253    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1254     (gnus-simplify-subject-fuzzy subject))
1255    ((numberp gnus-summary-gather-subject-limit)
1256     (gnus-limit-string (gnus-simplify-subject-re subject)
1257                        gnus-summary-gather-subject-limit))
1258    (t
1259     subject)))
1260
1261 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1262   "Check whether two subjects are equal.
1263 If optional argument simple-first is t, first argument is already
1264 simplified."
1265   (cond
1266    ((null simple-first)
1267     (equal (gnus-simplify-subject-fully s1)
1268            (gnus-simplify-subject-fully s2)))
1269    (t
1270     (equal s1
1271            (gnus-simplify-subject-fully s2)))))
1272
1273 (defun gnus-summary-bubble-group ()
1274   "Increase the score of the current group.
1275 This is a handy function to add to `gnus-summary-exit-hook' to
1276 increase the score of each group you read."
1277   (gnus-group-add-score gnus-newsgroup-name))
1278
1279 \f
1280 ;;;
1281 ;;; Gnus summary mode
1282 ;;;
1283
1284 (put 'gnus-summary-mode 'mode-class 'special)
1285
1286 (when t
1287   ;; Non-orthogonal keys
1288
1289   (gnus-define-keys gnus-summary-mode-map
1290     " " gnus-summary-next-page
1291     "\177" gnus-summary-prev-page
1292     [delete] gnus-summary-prev-page
1293     [backspace] gnus-summary-prev-page
1294     "\r" gnus-summary-scroll-up
1295     "\M-\r" gnus-summary-scroll-down
1296     "n" gnus-summary-next-unread-article
1297     "p" gnus-summary-prev-unread-article
1298     "N" gnus-summary-next-article
1299     "P" gnus-summary-prev-article
1300     "\M-\C-n" gnus-summary-next-same-subject
1301     "\M-\C-p" gnus-summary-prev-same-subject
1302     "\M-n" gnus-summary-next-unread-subject
1303     "\M-p" gnus-summary-prev-unread-subject
1304     "." gnus-summary-first-unread-article
1305     "," gnus-summary-best-unread-article
1306     "\M-s" gnus-summary-search-article-forward
1307     "\M-r" gnus-summary-search-article-backward
1308     "<" gnus-summary-beginning-of-article
1309     ">" gnus-summary-end-of-article
1310     "j" gnus-summary-goto-article
1311     "^" gnus-summary-refer-parent-article
1312     "\M-^" gnus-summary-refer-article
1313     "u" gnus-summary-tick-article-forward
1314     "!" gnus-summary-tick-article-forward
1315     "U" gnus-summary-tick-article-backward
1316     "d" gnus-summary-mark-as-read-forward
1317     "D" gnus-summary-mark-as-read-backward
1318     "E" gnus-summary-mark-as-expirable
1319     "\M-u" gnus-summary-clear-mark-forward
1320     "\M-U" gnus-summary-clear-mark-backward
1321     "k" gnus-summary-kill-same-subject-and-select
1322     "\C-k" gnus-summary-kill-same-subject
1323     "\M-\C-k" gnus-summary-kill-thread
1324     "\M-\C-l" gnus-summary-lower-thread
1325     "e" gnus-summary-edit-article
1326     "#" gnus-summary-mark-as-processable
1327     "\M-#" gnus-summary-unmark-as-processable
1328     "\M-\C-t" gnus-summary-toggle-threads
1329     "\M-\C-s" gnus-summary-show-thread
1330     "\M-\C-h" gnus-summary-hide-thread
1331     "\M-\C-f" gnus-summary-next-thread
1332     "\M-\C-b" gnus-summary-prev-thread
1333     [(meta down)] gnus-summary-next-thread
1334     [(meta up)] gnus-summary-prev-thread
1335     "\M-\C-u" gnus-summary-up-thread
1336     "\M-\C-d" gnus-summary-down-thread
1337     "&" gnus-summary-execute-command
1338     "c" gnus-summary-catchup-and-exit
1339     "\C-w" gnus-summary-mark-region-as-read
1340     "\C-t" gnus-summary-toggle-truncation
1341     "?" gnus-summary-mark-as-dormant
1342     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1343     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1344     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1345     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1346     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1347     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1348     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1349     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1350     "=" gnus-summary-expand-window
1351     "\C-x\C-s" gnus-summary-reselect-current-group
1352     "\M-g" gnus-summary-rescan-group
1353     "w" gnus-summary-stop-page-breaking
1354     "\C-c\C-r" gnus-summary-caesar-message
1355     "f" gnus-summary-followup
1356     "F" gnus-summary-followup-with-original
1357     "C" gnus-summary-cancel-article
1358     "r" gnus-summary-reply
1359     "R" gnus-summary-reply-with-original
1360     "\C-c\C-f" gnus-summary-mail-forward
1361     "o" gnus-summary-save-article
1362     "\C-o" gnus-summary-save-article-mail
1363     "|" gnus-summary-pipe-output
1364     "\M-k" gnus-summary-edit-local-kill
1365     "\M-K" gnus-summary-edit-global-kill
1366     ;; "V" gnus-version
1367     "\C-c\C-d" gnus-summary-describe-group
1368     "q" gnus-summary-exit
1369     "Q" gnus-summary-exit-no-update
1370     "\C-c\C-i" gnus-info-find-node
1371     gnus-mouse-2 gnus-mouse-pick-article
1372     "m" gnus-summary-mail-other-window
1373     "a" gnus-summary-post-news
1374     "x" gnus-summary-limit-to-unread
1375     "s" gnus-summary-isearch-article
1376     "t" gnus-summary-toggle-header
1377     "g" gnus-summary-show-article
1378     "l" gnus-summary-goto-last-article
1379     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1380     "\C-d" gnus-summary-enter-digest-group
1381     "\M-\C-d" gnus-summary-read-document
1382     "\M-\C-e" gnus-summary-edit-parameters
1383     "\M-\C-a" gnus-summary-customize-parameters
1384     "\C-c\C-b" gnus-bug
1385     "*" gnus-cache-enter-article
1386     "\M-*" gnus-cache-remove-article
1387     "\M-&" gnus-summary-universal-argument
1388     "\C-l" gnus-recenter
1389     "I" gnus-summary-increase-score
1390     "L" gnus-summary-lower-score
1391     "\M-i" gnus-symbolic-argument
1392     "h" gnus-summary-select-article-buffer
1393
1394     "b" gnus-article-view-part
1395     "\M-t" gnus-summary-toggle-display-buttonized
1396
1397     "V" gnus-summary-score-map
1398     "X" gnus-uu-extract-map
1399     "S" gnus-summary-send-map)
1400
1401   ;; Sort of orthogonal keymap
1402   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1403     "t" gnus-summary-tick-article-forward
1404     "!" gnus-summary-tick-article-forward
1405     "d" gnus-summary-mark-as-read-forward
1406     "r" gnus-summary-mark-as-read-forward
1407     "c" gnus-summary-clear-mark-forward
1408     " " gnus-summary-clear-mark-forward
1409     "e" gnus-summary-mark-as-expirable
1410     "x" gnus-summary-mark-as-expirable
1411     "?" gnus-summary-mark-as-dormant
1412     "b" gnus-summary-set-bookmark
1413     "B" gnus-summary-remove-bookmark
1414     "#" gnus-summary-mark-as-processable
1415     "\M-#" gnus-summary-unmark-as-processable
1416     "S" gnus-summary-limit-include-expunged
1417     "C" gnus-summary-catchup
1418     "H" gnus-summary-catchup-to-here
1419     "\C-c" gnus-summary-catchup-all
1420     "k" gnus-summary-kill-same-subject-and-select
1421     "K" gnus-summary-kill-same-subject
1422     "P" gnus-uu-mark-map)
1423
1424   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1425     "c" gnus-summary-clear-above
1426     "u" gnus-summary-tick-above
1427     "m" gnus-summary-mark-above
1428     "k" gnus-summary-kill-below)
1429
1430   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1431     "/" gnus-summary-limit-to-subject
1432     "n" gnus-summary-limit-to-articles
1433     "w" gnus-summary-pop-limit
1434     "s" gnus-summary-limit-to-subject
1435     "a" gnus-summary-limit-to-author
1436     "u" gnus-summary-limit-to-unread
1437     "m" gnus-summary-limit-to-marks
1438     "M" gnus-summary-limit-exclude-marks
1439     "v" gnus-summary-limit-to-score
1440     "*" gnus-summary-limit-include-cached
1441     "D" gnus-summary-limit-include-dormant
1442     "T" gnus-summary-limit-include-thread
1443     "d" gnus-summary-limit-exclude-dormant
1444     "t" gnus-summary-limit-to-age
1445     "x" gnus-summary-limit-to-extra 
1446     "E" gnus-summary-limit-include-expunged
1447     "c" gnus-summary-limit-exclude-childless-dormant
1448     "C" gnus-summary-limit-mark-excluded-as-read)
1449
1450   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1451     "n" gnus-summary-next-unread-article
1452     "p" gnus-summary-prev-unread-article
1453     "N" gnus-summary-next-article
1454     "P" gnus-summary-prev-article
1455     "\C-n" gnus-summary-next-same-subject
1456     "\C-p" gnus-summary-prev-same-subject
1457     "\M-n" gnus-summary-next-unread-subject
1458     "\M-p" gnus-summary-prev-unread-subject
1459     "f" gnus-summary-first-unread-article
1460     "b" gnus-summary-best-unread-article
1461     "j" gnus-summary-goto-article
1462     "g" gnus-summary-goto-subject
1463     "l" gnus-summary-goto-last-article
1464     "o" gnus-summary-pop-article)
1465
1466   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1467     "k" gnus-summary-kill-thread
1468     "l" gnus-summary-lower-thread
1469     "i" gnus-summary-raise-thread
1470     "T" gnus-summary-toggle-threads
1471     "t" gnus-summary-rethread-current
1472     "^" gnus-summary-reparent-thread
1473     "s" gnus-summary-show-thread
1474     "S" gnus-summary-show-all-threads
1475     "h" gnus-summary-hide-thread
1476     "H" gnus-summary-hide-all-threads
1477     "n" gnus-summary-next-thread
1478     "p" gnus-summary-prev-thread
1479     "u" gnus-summary-up-thread
1480     "o" gnus-summary-top-thread
1481     "d" gnus-summary-down-thread
1482     "#" gnus-uu-mark-thread
1483     "\M-#" gnus-uu-unmark-thread)
1484
1485   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1486     "g" gnus-summary-prepare
1487     "c" gnus-summary-insert-cached-articles)
1488
1489   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1490     "c" gnus-summary-catchup-and-exit
1491     "C" gnus-summary-catchup-all-and-exit
1492     "E" gnus-summary-exit-no-update
1493     "Q" gnus-summary-exit
1494     "Z" gnus-summary-exit
1495     "n" gnus-summary-catchup-and-goto-next-group
1496     "R" gnus-summary-reselect-current-group
1497     "G" gnus-summary-rescan-group
1498     "N" gnus-summary-next-group
1499     "s" gnus-summary-save-newsrc
1500     "P" gnus-summary-prev-group)
1501
1502   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1503     " " gnus-summary-next-page
1504     "n" gnus-summary-next-page
1505     "\177" gnus-summary-prev-page
1506     [delete] gnus-summary-prev-page
1507     "p" gnus-summary-prev-page
1508     "\r" gnus-summary-scroll-up
1509     "\M-\r" gnus-summary-scroll-down
1510     "<" gnus-summary-beginning-of-article
1511     ">" gnus-summary-end-of-article
1512     "b" gnus-summary-beginning-of-article
1513     "e" gnus-summary-end-of-article
1514     "^" gnus-summary-refer-parent-article
1515     "r" gnus-summary-refer-parent-article
1516     "D" gnus-summary-enter-digest-group
1517     "R" gnus-summary-refer-references
1518     "T" gnus-summary-refer-thread
1519     "g" gnus-summary-show-article
1520     "s" gnus-summary-isearch-article
1521     "P" gnus-summary-print-article
1522     "t" gnus-article-babel)
1523
1524   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1525     "b" gnus-article-add-buttons
1526     "B" gnus-article-add-buttons-to-head
1527     "o" gnus-article-treat-overstrike
1528     "e" gnus-article-emphasize
1529     "w" gnus-article-fill-cited-article
1530     "Q" gnus-article-fill-long-lines
1531     "C" gnus-article-capitalize-sentences
1532     "c" gnus-article-remove-cr
1533     "q" gnus-article-de-quoted-unreadable
1534     "f" gnus-article-display-x-face
1535     "l" gnus-summary-stop-page-breaking
1536     "r" gnus-summary-caesar-message
1537     "t" gnus-article-hide-headers
1538     "v" gnus-summary-verbose-headers
1539     "H" gnus-article-strip-headers-in-body
1540     "d" gnus-article-treat-dumbquotes)
1541
1542   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1543     "a" gnus-article-hide
1544     "h" gnus-article-hide-headers
1545     "b" gnus-article-hide-boring-headers
1546     "s" gnus-article-hide-signature
1547     "c" gnus-article-hide-citation
1548     "C" gnus-article-hide-citation-in-followups
1549     "l" gnus-article-hide-list-identifiers
1550     "p" gnus-article-hide-pgp
1551     "B" gnus-article-strip-banner
1552     "P" gnus-article-hide-pem
1553     "\C-c" gnus-article-hide-citation-maybe)
1554
1555   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1556     "a" gnus-article-highlight
1557     "h" gnus-article-highlight-headers
1558     "c" gnus-article-highlight-citation
1559     "s" gnus-article-highlight-signature)
1560
1561   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1562     "w" gnus-article-decode-mime-words
1563     "c" gnus-article-decode-charset
1564     "v" gnus-mime-view-all-parts
1565     "b" gnus-article-view-part)
1566
1567   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1568     "z" gnus-article-date-ut
1569     "u" gnus-article-date-ut
1570     "l" gnus-article-date-local
1571     "e" gnus-article-date-lapsed
1572     "o" gnus-article-date-original
1573     "i" gnus-article-date-iso8601
1574     "s" gnus-article-date-user)
1575
1576   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1577     "t" gnus-article-remove-trailing-blank-lines
1578     "l" gnus-article-strip-leading-blank-lines
1579     "m" gnus-article-strip-multiple-blank-lines
1580     "a" gnus-article-strip-blank-lines
1581     "A" gnus-article-strip-all-blank-lines
1582     "s" gnus-article-strip-leading-space
1583     "e" gnus-article-strip-trailing-space)
1584
1585   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1586     "v" gnus-version
1587     "f" gnus-summary-fetch-faq
1588     "d" gnus-summary-describe-group
1589     "h" gnus-summary-describe-briefly
1590     "i" gnus-info-find-node)
1591
1592   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1593     "e" gnus-summary-expire-articles
1594     "\M-\C-e" gnus-summary-expire-articles-now
1595     "\177" gnus-summary-delete-article
1596     [delete] gnus-summary-delete-article
1597     [backspace] gnus-summary-delete-article
1598     "m" gnus-summary-move-article
1599     "r" gnus-summary-respool-article
1600     "w" gnus-summary-edit-article
1601     "c" gnus-summary-copy-article
1602     "B" gnus-summary-crosspost-article
1603     "q" gnus-summary-respool-query
1604     "t" gnus-summary-respool-trace
1605     "i" gnus-summary-import-article
1606     "p" gnus-summary-article-posted-p)
1607
1608   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1609     "o" gnus-summary-save-article
1610     "m" gnus-summary-save-article-mail
1611     "F" gnus-summary-write-article-file
1612     "r" gnus-summary-save-article-rmail
1613     "f" gnus-summary-save-article-file
1614     "b" gnus-summary-save-article-body-file
1615     "h" gnus-summary-save-article-folder
1616     "v" gnus-summary-save-article-vm
1617     "p" gnus-summary-pipe-output
1618     "s" gnus-soup-add-article)
1619
1620   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1621     "b" gnus-summary-display-buttonized
1622     "m" gnus-summary-repair-multipart
1623     "v" gnus-article-view-part
1624     "o" gnus-article-save-part
1625     "c" gnus-article-copy-part
1626     "e" gnus-article-externalize-part
1627     "i" gnus-article-inline-part
1628     "|" gnus-article-pipe-part))
1629
1630 (defun gnus-summary-make-menu-bar ()
1631   (gnus-turn-off-edit-menu 'summary)
1632
1633   (unless (boundp 'gnus-summary-misc-menu)
1634
1635     (easy-menu-define
1636      gnus-summary-kill-menu gnus-summary-mode-map ""
1637      (cons
1638       "Score"
1639       (nconc
1640        (list
1641         ["Enter score..." gnus-summary-score-entry t]
1642         ["Customize" gnus-score-customize t])
1643        (gnus-make-score-map 'increase)
1644        (gnus-make-score-map 'lower)
1645        '(("Mark"
1646           ["Kill below" gnus-summary-kill-below t]
1647           ["Mark above" gnus-summary-mark-above t]
1648           ["Tick above" gnus-summary-tick-above t]
1649           ["Clear above" gnus-summary-clear-above t])
1650          ["Current score" gnus-summary-current-score t]
1651          ["Set score" gnus-summary-set-score t]
1652          ["Switch current score file..." gnus-score-change-score-file t]
1653          ["Set mark below..." gnus-score-set-mark-below t]
1654          ["Set expunge below..." gnus-score-set-expunge-below t]
1655          ["Edit current score file" gnus-score-edit-current-scores t]
1656          ["Edit score file" gnus-score-edit-file t]
1657          ["Trace score" gnus-score-find-trace t]
1658          ["Find words" gnus-score-find-favourite-words t]
1659          ["Rescore buffer" gnus-summary-rescore t]
1660          ["Increase score..." gnus-summary-increase-score t]
1661          ["Lower score..." gnus-summary-lower-score t]))))
1662
1663     ;; Define both the Article menu in the summary buffer and the equivalent
1664     ;; Commands menu in the article buffer here for consistency.
1665     (let ((innards
1666            '(("Hide"
1667               ["All" gnus-article-hide t]
1668               ["Headers" gnus-article-hide-headers t]
1669               ["Signature" gnus-article-hide-signature t]
1670               ["Citation" gnus-article-hide-citation t]
1671               ["List identifiers" gnus-article-hide-list-identifiers t]
1672               ["PGP" gnus-article-hide-pgp t]
1673               ["Banner" gnus-article-strip-banner t]
1674               ["Boring headers" gnus-article-hide-boring-headers t])
1675              ("Highlight"
1676               ["All" gnus-article-highlight t]
1677               ["Headers" gnus-article-highlight-headers t]
1678               ["Signature" gnus-article-highlight-signature t]
1679               ["Citation" gnus-article-highlight-citation t])
1680              ("MIME"
1681               ["Words" gnus-article-decode-mime-words t]
1682               ["Charset" gnus-article-decode-charset t]
1683               ["QP" gnus-article-de-quoted-unreadable t]
1684               ["View all" gnus-mime-view-all-parts t])
1685              ("Date"
1686               ["Local" gnus-article-date-local t]
1687               ["ISO8601" gnus-article-date-iso8601 t]
1688               ["UT" gnus-article-date-ut t]
1689               ["Original" gnus-article-date-original t]
1690               ["Lapsed" gnus-article-date-lapsed t]
1691               ["User-defined" gnus-article-date-user t])
1692              ("Washing"
1693               ("Remove Blanks"
1694                ["Leading" gnus-article-strip-leading-blank-lines t]
1695                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1696                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1697                ["All of the above" gnus-article-strip-blank-lines t]
1698                ["All" gnus-article-strip-all-blank-lines t]
1699                ["Leading space" gnus-article-strip-leading-space t]
1700                ["Trailing space" gnus-article-strip-trailing-space t])
1701               ["Overstrike" gnus-article-treat-overstrike t]
1702               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1703               ["Emphasis" gnus-article-emphasize t]
1704               ["Word wrap" gnus-article-fill-cited-article t]
1705               ["Fill long lines" gnus-article-fill-long-lines t]
1706               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1707               ["CR" gnus-article-remove-cr t]
1708               ["Show X-Face" gnus-article-display-x-face t]
1709               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1710               ["Rot 13" gnus-summary-caesar-message t]
1711               ["Unix pipe" gnus-summary-pipe-message t]
1712               ["Add buttons" gnus-article-add-buttons t]
1713               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1714               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1715               ["Verbose header" gnus-summary-verbose-headers t]
1716               ["Toggle header" gnus-summary-toggle-header t]
1717               ["HZ" gnus-article-decode-HZ t])
1718              ("Output"
1719               ["Save in default format" gnus-summary-save-article t]
1720               ["Save in file" gnus-summary-save-article-file t]
1721               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1722               ["Save in MH folder" gnus-summary-save-article-folder t]
1723               ["Save in VM folder" gnus-summary-save-article-vm t]
1724               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1725               ["Save body in file" gnus-summary-save-article-body-file t]
1726               ["Pipe through a filter" gnus-summary-pipe-output t]
1727               ["Add to SOUP packet" gnus-soup-add-article t]
1728               ["Print" gnus-summary-print-article t])
1729              ("Backend"
1730               ["Respool article..." gnus-summary-respool-article t]
1731               ["Move article..." gnus-summary-move-article
1732                (gnus-check-backend-function
1733                 'request-move-article gnus-newsgroup-name)]
1734               ["Copy article..." gnus-summary-copy-article t]
1735               ["Crosspost article..." gnus-summary-crosspost-article
1736                (gnus-check-backend-function
1737                 'request-replace-article gnus-newsgroup-name)]
1738               ["Import file..." gnus-summary-import-article t]
1739               ["Check if posted" gnus-summary-article-posted-p t]
1740               ["Edit article" gnus-summary-edit-article
1741                (not (gnus-group-read-only-p))]
1742               ["Delete article" gnus-summary-delete-article
1743                (gnus-check-backend-function
1744                 'request-expire-articles gnus-newsgroup-name)]
1745               ["Query respool" gnus-summary-respool-query t]
1746               ["Trace respool" gnus-summary-respool-trace t]
1747               ["Delete expirable articles" gnus-summary-expire-articles-now
1748                (gnus-check-backend-function
1749                 'request-expire-articles gnus-newsgroup-name)])
1750              ("Extract"
1751               ["Uudecode" gnus-uu-decode-uu t]
1752               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1753               ["Unshar" gnus-uu-decode-unshar t]
1754               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1755               ["Save" gnus-uu-decode-save t]
1756               ["Binhex" gnus-uu-decode-binhex t]
1757               ["Postscript" gnus-uu-decode-postscript t])
1758              ("Cache"
1759               ["Enter article" gnus-cache-enter-article t]
1760               ["Remove article" gnus-cache-remove-article t])
1761              ["Translate" gnus-article-babel t]
1762              ["Select article buffer" gnus-summary-select-article-buffer t]
1763              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1764              ["Isearch article..." gnus-summary-isearch-article t]
1765              ["Beginning of the article" gnus-summary-beginning-of-article t]
1766              ["End of the article" gnus-summary-end-of-article t]
1767              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1768              ["Fetch referenced articles" gnus-summary-refer-references t]
1769              ["Fetch current thread" gnus-summary-refer-thread t]
1770              ["Fetch article with id..." gnus-summary-refer-article t]
1771              ["Redisplay" gnus-summary-show-article t])))
1772       (easy-menu-define
1773        gnus-summary-article-menu gnus-summary-mode-map ""
1774        (cons "Article" innards))
1775
1776       (easy-menu-define
1777        gnus-article-commands-menu gnus-article-mode-map ""
1778        (cons "Commands" innards)))
1779
1780     (easy-menu-define
1781      gnus-summary-thread-menu gnus-summary-mode-map ""
1782      '("Threads"
1783        ["Toggle threading" gnus-summary-toggle-threads t]
1784        ["Hide threads" gnus-summary-hide-all-threads t]
1785        ["Show threads" gnus-summary-show-all-threads t]
1786        ["Hide thread" gnus-summary-hide-thread t]
1787        ["Show thread" gnus-summary-show-thread t]
1788        ["Go to next thread" gnus-summary-next-thread t]
1789        ["Go to previous thread" gnus-summary-prev-thread t]
1790        ["Go down thread" gnus-summary-down-thread t]
1791        ["Go up thread" gnus-summary-up-thread t]
1792        ["Top of thread" gnus-summary-top-thread t]
1793        ["Mark thread as read" gnus-summary-kill-thread t]
1794        ["Lower thread score" gnus-summary-lower-thread t]
1795        ["Raise thread score" gnus-summary-raise-thread t]
1796        ["Rethread current" gnus-summary-rethread-current t]))
1797
1798     (easy-menu-define
1799      gnus-summary-post-menu gnus-summary-mode-map ""
1800      '("Post"
1801        ["Post an article" gnus-summary-post-news t]
1802        ["Followup" gnus-summary-followup t]
1803        ["Followup and yank" gnus-summary-followup-with-original t]
1804        ["Supersede article" gnus-summary-supersede-article t]
1805        ["Cancel article" gnus-summary-cancel-article t]
1806        ["Reply" gnus-summary-reply t]
1807        ["Reply and yank" gnus-summary-reply-with-original t]
1808        ["Wide reply" gnus-summary-wide-reply t]
1809        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1810        ["Mail forward" gnus-summary-mail-forward t]
1811        ["Post forward" gnus-summary-post-forward t]
1812        ["Digest and mail" gnus-uu-digest-mail-forward t]
1813        ["Digest and post" gnus-uu-digest-post-forward t]
1814        ["Resend message" gnus-summary-resend-message t]
1815        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1816        ["Send a mail" gnus-summary-mail-other-window t]
1817        ["Uuencode and post" gnus-uu-post-news t]
1818        ["Followup via news" gnus-summary-followup-to-mail t]
1819        ["Followup via news and yank"
1820         gnus-summary-followup-to-mail-with-original t]
1821        ;;("Draft"
1822        ;;["Send" gnus-summary-send-draft t]
1823        ;;["Send bounced" gnus-resend-bounced-mail t])
1824        ))
1825
1826     (easy-menu-define
1827      gnus-summary-misc-menu gnus-summary-mode-map ""
1828      '("Misc"
1829        ("Mark Read"
1830         ["Mark as read" gnus-summary-mark-as-read-forward t]
1831         ["Mark same subject and select"
1832          gnus-summary-kill-same-subject-and-select t]
1833         ["Mark same subject" gnus-summary-kill-same-subject t]
1834         ["Catchup" gnus-summary-catchup t]
1835         ["Catchup all" gnus-summary-catchup-all t]
1836         ["Catchup to here" gnus-summary-catchup-to-here t]
1837         ["Catchup region" gnus-summary-mark-region-as-read t]
1838         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1839        ("Mark Various"
1840         ["Tick" gnus-summary-tick-article-forward t]
1841         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1842         ["Remove marks" gnus-summary-clear-mark-forward t]
1843         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1844         ["Set bookmark" gnus-summary-set-bookmark t]
1845         ["Remove bookmark" gnus-summary-remove-bookmark t])
1846        ("Mark Limit"
1847         ["Marks..." gnus-summary-limit-to-marks t]
1848         ["Subject..." gnus-summary-limit-to-subject t]
1849         ["Author..." gnus-summary-limit-to-author t]
1850         ["Age..." gnus-summary-limit-to-age t]
1851         ["Extra..." gnus-summary-limit-to-extra t]
1852         ["Score" gnus-summary-limit-to-score t]
1853         ["Unread" gnus-summary-limit-to-unread t]
1854         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1855         ["Articles" gnus-summary-limit-to-articles t]
1856         ["Pop limit" gnus-summary-pop-limit t]
1857         ["Show dormant" gnus-summary-limit-include-dormant t]
1858         ["Hide childless dormant"
1859          gnus-summary-limit-exclude-childless-dormant t]
1860         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1861         ["Hide marked" gnus-summary-limit-exclude-marks t]
1862         ["Show expunged" gnus-summary-show-all-expunged t])
1863        ("Process Mark"
1864         ["Set mark" gnus-summary-mark-as-processable t]
1865         ["Remove mark" gnus-summary-unmark-as-processable t]
1866         ["Remove all marks" gnus-summary-unmark-all-processable t]
1867         ["Mark above" gnus-uu-mark-over t]
1868         ["Mark series" gnus-uu-mark-series t]
1869         ["Mark region" gnus-uu-mark-region t]
1870         ["Unmark region" gnus-uu-unmark-region t]
1871         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1872         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1873         ["Mark all" gnus-uu-mark-all t]
1874         ["Mark buffer" gnus-uu-mark-buffer t]
1875         ["Mark sparse" gnus-uu-mark-sparse t]
1876         ["Mark thread" gnus-uu-mark-thread t]
1877         ["Unmark thread" gnus-uu-unmark-thread t]
1878         ("Process Mark Sets"
1879          ["Kill" gnus-summary-kill-process-mark t]
1880          ["Yank" gnus-summary-yank-process-mark
1881           gnus-newsgroup-process-stack]
1882          ["Save" gnus-summary-save-process-mark t]))
1883        ("Scroll article"
1884         ["Page forward" gnus-summary-next-page t]
1885         ["Page backward" gnus-summary-prev-page t]
1886         ["Line forward" gnus-summary-scroll-up t])
1887        ("Move"
1888         ["Next unread article" gnus-summary-next-unread-article t]
1889         ["Previous unread article" gnus-summary-prev-unread-article t]
1890         ["Next article" gnus-summary-next-article t]
1891         ["Previous article" gnus-summary-prev-article t]
1892         ["Next unread subject" gnus-summary-next-unread-subject t]
1893         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1894         ["Next article same subject" gnus-summary-next-same-subject t]
1895         ["Previous article same subject" gnus-summary-prev-same-subject t]
1896         ["First unread article" gnus-summary-first-unread-article t]
1897         ["Best unread article" gnus-summary-best-unread-article t]
1898         ["Go to subject number..." gnus-summary-goto-subject t]
1899         ["Go to article number..." gnus-summary-goto-article t]
1900         ["Go to the last article" gnus-summary-goto-last-article t]
1901         ["Pop article off history" gnus-summary-pop-article t])
1902        ("Sort"
1903         ["Sort by number" gnus-summary-sort-by-number t]
1904         ["Sort by author" gnus-summary-sort-by-author t]
1905         ["Sort by subject" gnus-summary-sort-by-subject t]
1906         ["Sort by date" gnus-summary-sort-by-date t]
1907         ["Sort by score" gnus-summary-sort-by-score t]
1908         ["Sort by lines" gnus-summary-sort-by-lines t]
1909         ["Sort by characters" gnus-summary-sort-by-chars t])
1910        ("Help"
1911         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1912         ["Describe group" gnus-summary-describe-group t]
1913         ["Read manual" gnus-info-find-node t])
1914        ("Modes"
1915         ["Pick and read" gnus-pick-mode t]
1916         ["Binary" gnus-binary-mode t])
1917        ("Regeneration"
1918         ["Regenerate" gnus-summary-prepare t]
1919         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1920         ["Toggle threading" gnus-summary-toggle-threads t])
1921        ["Filter articles..." gnus-summary-execute-command t]
1922        ["Run command on subjects..." gnus-summary-universal-argument t]
1923        ["Search articles forward..." gnus-summary-search-article-forward t]
1924        ["Search articles backward..." gnus-summary-search-article-backward t]
1925        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1926        ["Expand window" gnus-summary-expand-window t]
1927        ["Expire expirable articles" gnus-summary-expire-articles
1928         (gnus-check-backend-function
1929          'request-expire-articles gnus-newsgroup-name)]
1930        ["Edit local kill file" gnus-summary-edit-local-kill t]
1931        ["Edit main kill file" gnus-summary-edit-global-kill t]
1932        ["Edit group parameters" gnus-summary-edit-parameters t]
1933        ["Customize group parameters" gnus-summary-customize-parameters t]
1934        ["Send a bug report" gnus-bug t]
1935        ("Exit"
1936         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1937         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1938         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1939         ["Exit group" gnus-summary-exit t]
1940         ["Exit group without updating" gnus-summary-exit-no-update t]
1941         ["Exit and goto next group" gnus-summary-next-group t]
1942         ["Exit and goto prev group" gnus-summary-prev-group t]
1943         ["Reselect group" gnus-summary-reselect-current-group t]
1944         ["Rescan group" gnus-summary-rescan-group t]
1945         ["Update dribble" gnus-summary-save-newsrc t])))
1946
1947     (gnus-run-hooks 'gnus-summary-menu-hook)))
1948
1949 (defun gnus-score-set-default (var value)
1950   "A version of set that updates the GNU Emacs menu-bar."
1951   (set var value)
1952   ;; It is the message that forces the active status to be updated.
1953   (message ""))
1954
1955 (defun gnus-make-score-map (type)
1956   "Make a summary score map of type TYPE."
1957   (if t
1958       nil
1959     (let ((headers '(("author" "from" string)
1960                      ("subject" "subject" string)
1961                      ("article body" "body" string)
1962                      ("article head" "head" string)
1963                      ("xref" "xref" string)
1964                      ("extra header" "extra" string)
1965                      ("lines" "lines" number)
1966                      ("followups to author" "followup" string)))
1967           (types '((number ("less than" <)
1968                            ("greater than" >)
1969                            ("equal" =))
1970                    (string ("substring" s)
1971                            ("exact string" e)
1972                            ("fuzzy string" f)
1973                            ("regexp" r))))
1974           (perms '(("temporary" (current-time-string))
1975                    ("permanent" nil)
1976                    ("immediate" now)))
1977           header)
1978       (list
1979        (apply
1980         'nconc
1981         (list
1982          (if (eq type 'lower)
1983              "Lower score"
1984            "Increase score"))
1985         (let (outh)
1986           (while headers
1987             (setq header (car headers))
1988             (setq outh
1989                   (cons
1990                    (apply
1991                     'nconc
1992                     (list (car header))
1993                     (let ((ts (cdr (assoc (nth 2 header) types)))
1994                           outt)
1995                       (while ts
1996                         (setq outt
1997                               (cons
1998                                (apply
1999                                 'nconc
2000                                 (list (caar ts))
2001                                 (let ((ps perms)
2002                                       outp)
2003                                   (while ps
2004                                     (setq outp
2005                                           (cons
2006                                            (vector
2007                                             (caar ps)
2008                                             (list
2009                                              'gnus-summary-score-entry
2010                                              (nth 1 header)
2011                                              (if (or (string= (nth 1 header)
2012                                                               "head")
2013                                                      (string= (nth 1 header)
2014                                                               "body"))
2015                                                  ""
2016                                                (list 'gnus-summary-header
2017                                                      (nth 1 header)))
2018                                              (list 'quote (nth 1 (car ts)))
2019                                              (list 'gnus-score-delta-default
2020                                                    nil)
2021                                              (nth 1 (car ps))
2022                                              t)
2023                                             t)
2024                                            outp))
2025                                     (setq ps (cdr ps)))
2026                                   (list (nreverse outp))))
2027                                outt))
2028                         (setq ts (cdr ts)))
2029                       (list (nreverse outt))))
2030                    outh))
2031             (setq headers (cdr headers)))
2032           (list (nreverse outh))))))))
2033
2034 \f
2035
2036 (defun gnus-summary-mode (&optional group)
2037   "Major mode for reading articles.
2038
2039 All normal editing commands are switched off.
2040 \\<gnus-summary-mode-map>
2041 Each line in this buffer represents one article.  To read an
2042 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2043 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2044 respectively.
2045
2046 You can also post articles and send mail from this buffer.  To
2047 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2048 of an article, type `\\[gnus-summary-reply]'.
2049
2050 There are approx. one gazillion commands you can execute in this
2051 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2052
2053 The following commands are available:
2054
2055 \\{gnus-summary-mode-map}"
2056   (interactive)
2057   (when (gnus-visual-p 'summary-menu 'menu)
2058     (gnus-summary-make-menu-bar))
2059   (kill-all-local-variables)
2060   (gnus-summary-make-local-variables)
2061   (gnus-make-thread-indent-array)
2062   (gnus-simplify-mode-line)
2063   (setq major-mode 'gnus-summary-mode)
2064   (setq mode-name "Summary")
2065   (make-local-variable 'minor-mode-alist)
2066   (use-local-map gnus-summary-mode-map)
2067   (buffer-disable-undo)
2068   (setq buffer-read-only t)             ;Disable modification
2069   (setq truncate-lines t)
2070   (setq selective-display t)
2071   (setq selective-display-ellipses t)   ;Display `...'
2072   (gnus-summary-set-display-table)
2073   (gnus-set-default-directory)
2074   (setq gnus-newsgroup-name group)
2075   (make-local-variable 'gnus-summary-line-format)
2076   (make-local-variable 'gnus-summary-line-format-spec)
2077   (make-local-variable 'gnus-summary-dummy-line-format)
2078   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2079   (make-local-variable 'gnus-summary-mark-positions)
2080   (make-local-hook 'pre-command-hook)
2081   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2082   (gnus-run-hooks 'gnus-summary-mode-hook)
2083   (mm-enable-multibyte)
2084   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2085   (gnus-update-summary-mark-positions))
2086
2087 (defun gnus-summary-make-local-variables ()
2088   "Make all the local summary buffer variables."
2089   (let (global)
2090     (dolist (local gnus-summary-local-variables)
2091       (if (consp local)
2092           (progn
2093             (if (eq (cdr local) 'global)
2094                 ;; Copy the global value of the variable.
2095                 (setq global (symbol-value (car local)))
2096               ;; Use the value from the list.
2097               (setq global (eval (cdr local))))
2098             (set (make-local-variable (car local)) global))
2099         ;; Simple nil-valued local variable.
2100         (set (make-local-variable local) nil)))))
2101
2102 (defun gnus-summary-clear-local-variables ()
2103   (let ((locals gnus-summary-local-variables))
2104     (while locals
2105       (if (consp (car locals))
2106           (and (vectorp (caar locals))
2107                (set (caar locals) nil))
2108         (and (vectorp (car locals))
2109              (set (car locals) nil)))
2110       (setq locals (cdr locals)))))
2111
2112 ;; Summary data functions.
2113
2114 (defmacro gnus-data-number (data)
2115   `(car ,data))
2116
2117 (defmacro gnus-data-set-number (data number)
2118   `(setcar ,data ,number))
2119
2120 (defmacro gnus-data-mark (data)
2121   `(nth 1 ,data))
2122
2123 (defmacro gnus-data-set-mark (data mark)
2124   `(setcar (nthcdr 1 ,data) ,mark))
2125
2126 (defmacro gnus-data-pos (data)
2127   `(nth 2 ,data))
2128
2129 (defmacro gnus-data-set-pos (data pos)
2130   `(setcar (nthcdr 2 ,data) ,pos))
2131
2132 (defmacro gnus-data-header (data)
2133   `(nth 3 ,data))
2134
2135 (defmacro gnus-data-set-header (data header)
2136   `(setf (nth 3 ,data) ,header))
2137
2138 (defmacro gnus-data-level (data)
2139   `(nth 4 ,data))
2140
2141 (defmacro gnus-data-unread-p (data)
2142   `(= (nth 1 ,data) gnus-unread-mark))
2143
2144 (defmacro gnus-data-read-p (data)
2145   `(/= (nth 1 ,data) gnus-unread-mark))
2146
2147 (defmacro gnus-data-pseudo-p (data)
2148   `(consp (nth 3 ,data)))
2149
2150 (defmacro gnus-data-find (number)
2151   `(assq ,number gnus-newsgroup-data))
2152
2153 (defmacro gnus-data-find-list (number &optional data)
2154   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2155      (memq (assq ,number bdata)
2156            bdata)))
2157
2158 (defmacro gnus-data-make (number mark pos header level)
2159   `(list ,number ,mark ,pos ,header ,level))
2160
2161 (defun gnus-data-enter (after-article number mark pos header level offset)
2162   (let ((data (gnus-data-find-list after-article)))
2163     (unless data
2164       (error "No such article: %d" after-article))
2165     (setcdr data (cons (gnus-data-make number mark pos header level)
2166                        (cdr data)))
2167     (setq gnus-newsgroup-data-reverse nil)
2168     (gnus-data-update-list (cddr data) offset)))
2169
2170 (defun gnus-data-enter-list (after-article list &optional offset)
2171   (when list
2172     (let ((data (and after-article (gnus-data-find-list after-article)))
2173           (ilist list))
2174       (if (not (or data
2175                    after-article))
2176           (let ((odata gnus-newsgroup-data))
2177             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2178             (when offset
2179               (gnus-data-update-list odata offset)))
2180         ;; Find the last element in the list to be spliced into the main
2181         ;; list.
2182         (while (cdr list)
2183           (setq list (cdr list)))
2184         (if (not data)
2185             (progn
2186               (setcdr list gnus-newsgroup-data)
2187               (setq gnus-newsgroup-data ilist)
2188               (when offset
2189                 (gnus-data-update-list (cdr list) offset)))
2190           (setcdr list (cdr data))
2191           (setcdr data ilist)
2192           (when offset
2193             (gnus-data-update-list (cdr list) offset))))
2194       (setq gnus-newsgroup-data-reverse nil))))
2195
2196 (defun gnus-data-remove (article &optional offset)
2197   (let ((data gnus-newsgroup-data))
2198     (if (= (gnus-data-number (car data)) article)
2199         (progn
2200           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2201                 gnus-newsgroup-data-reverse nil)
2202           (when offset
2203             (gnus-data-update-list gnus-newsgroup-data offset)))
2204       (while (cdr data)
2205         (when (= (gnus-data-number (cadr data)) article)
2206           (setcdr data (cddr data))
2207           (when offset
2208             (gnus-data-update-list (cdr data) offset))
2209           (setq data nil
2210                 gnus-newsgroup-data-reverse nil))
2211         (setq data (cdr data))))))
2212
2213 (defmacro gnus-data-list (backward)
2214   `(if ,backward
2215        (or gnus-newsgroup-data-reverse
2216            (setq gnus-newsgroup-data-reverse
2217                  (reverse gnus-newsgroup-data)))
2218      gnus-newsgroup-data))
2219
2220 (defun gnus-data-update-list (data offset)
2221   "Add OFFSET to the POS of all data entries in DATA."
2222   (setq gnus-newsgroup-data-reverse nil)
2223   (while data
2224     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2225     (setq data (cdr data))))
2226
2227 (defun gnus-summary-article-pseudo-p (article)
2228   "Say whether this article is a pseudo article or not."
2229   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2230
2231 (defmacro gnus-summary-article-sparse-p (article)
2232   "Say whether this article is a sparse article or not."
2233   `(memq ,article gnus-newsgroup-sparse))
2234
2235 (defmacro gnus-summary-article-ancient-p (article)
2236   "Say whether this article is a sparse article or not."
2237   `(memq ,article gnus-newsgroup-ancient))
2238
2239 (defun gnus-article-parent-p (number)
2240   "Say whether this article is a parent or not."
2241   (let ((data (gnus-data-find-list number)))
2242     (and (cdr data)                     ; There has to be an article after...
2243          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2244             (gnus-data-level (nth 1 data))))))
2245
2246 (defun gnus-article-children (number)
2247   "Return a list of all children to NUMBER."
2248   (let* ((data (gnus-data-find-list number))
2249          (level (gnus-data-level (car data)))
2250          children)
2251     (setq data (cdr data))
2252     (while (and data
2253                 (= (gnus-data-level (car data)) (1+ level)))
2254       (push (gnus-data-number (car data)) children)
2255       (setq data (cdr data)))
2256     children))
2257
2258 (defmacro gnus-summary-skip-intangible ()
2259   "If the current article is intangible, then jump to a different article."
2260   '(let ((to (get-text-property (point) 'gnus-intangible)))
2261      (and to (gnus-summary-goto-subject to))))
2262
2263 (defmacro gnus-summary-article-intangible-p ()
2264   "Say whether this article is intangible or not."
2265   '(get-text-property (point) 'gnus-intangible))
2266
2267 (defun gnus-article-read-p (article)
2268   "Say whether ARTICLE is read or not."
2269   (not (or (memq article gnus-newsgroup-marked)
2270            (memq article gnus-newsgroup-unreads)
2271            (memq article gnus-newsgroup-unselected)
2272            (memq article gnus-newsgroup-dormant))))
2273
2274 ;; Some summary mode macros.
2275
2276 (defmacro gnus-summary-article-number ()
2277   "The article number of the article on the current line.
2278 If there isn's an article number here, then we return the current
2279 article number."
2280   '(progn
2281      (gnus-summary-skip-intangible)
2282      (or (get-text-property (point) 'gnus-number)
2283          (gnus-summary-last-subject))))
2284
2285 (defmacro gnus-summary-article-header (&optional number)
2286   "Return the header of article NUMBER."
2287   `(gnus-data-header (gnus-data-find
2288                       ,(or number '(gnus-summary-article-number)))))
2289
2290 (defmacro gnus-summary-thread-level (&optional number)
2291   "Return the level of thread that starts with article NUMBER."
2292   `(if (and (eq gnus-summary-make-false-root 'dummy)
2293             (get-text-property (point) 'gnus-intangible))
2294        0
2295      (gnus-data-level (gnus-data-find
2296                        ,(or number '(gnus-summary-article-number))))))
2297
2298 (defmacro gnus-summary-article-mark (&optional number)
2299   "Return the mark of article NUMBER."
2300   `(gnus-data-mark (gnus-data-find
2301                     ,(or number '(gnus-summary-article-number)))))
2302
2303 (defmacro gnus-summary-article-pos (&optional number)
2304   "Return the position of the line of article NUMBER."
2305   `(gnus-data-pos (gnus-data-find
2306                    ,(or number '(gnus-summary-article-number)))))
2307
2308 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2309 (defmacro gnus-summary-article-subject (&optional number)
2310   "Return current subject string or nil if nothing."
2311   `(let ((headers
2312           ,(if number
2313                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2314              '(gnus-data-header (assq (gnus-summary-article-number)
2315                                       gnus-newsgroup-data)))))
2316      (and headers
2317           (vectorp headers)
2318           (mail-header-subject headers))))
2319
2320 (defmacro gnus-summary-article-score (&optional number)
2321   "Return current article score."
2322   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2323                   gnus-newsgroup-scored))
2324        gnus-summary-default-score 0))
2325
2326 (defun gnus-summary-article-children (&optional number)
2327   "Return a list of article numbers that are children of article NUMBER."
2328   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2329          (level (gnus-data-level (car data)))
2330          l children)
2331     (while (and (setq data (cdr data))
2332                 (> (setq l (gnus-data-level (car data))) level))
2333       (and (= (1+ level) l)
2334            (push (gnus-data-number (car data))
2335                  children)))
2336     (nreverse children)))
2337
2338 (defun gnus-summary-article-parent (&optional number)
2339   "Return the article number of the parent of article NUMBER."
2340   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2341                                     (gnus-data-list t)))
2342          (level (gnus-data-level (car data))))
2343     (if (zerop level)
2344         ()                              ; This is a root.
2345       ;; We search until we find an article with a level less than
2346       ;; this one.  That function has to be the parent.
2347       (while (and (setq data (cdr data))
2348                   (not (< (gnus-data-level (car data)) level))))
2349       (and data (gnus-data-number (car data))))))
2350
2351 (defun gnus-unread-mark-p (mark)
2352   "Say whether MARK is the unread mark."
2353   (= mark gnus-unread-mark))
2354
2355 (defun gnus-read-mark-p (mark)
2356   "Say whether MARK is one of the marks that mark as read.
2357 This is all marks except unread, ticked, dormant, and expirable."
2358   (not (or (= mark gnus-unread-mark)
2359            (= mark gnus-ticked-mark)
2360            (= mark gnus-dormant-mark)
2361            (= mark gnus-expirable-mark))))
2362
2363 (defmacro gnus-article-mark (number)
2364   "Return the MARK of article NUMBER.
2365 This macro should only be used when computing the mark the \"first\"
2366 time; i.e., when generating the summary lines.  After that,
2367 `gnus-summary-article-mark' should be used to examine the
2368 marks of articles."
2369   `(cond
2370     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2371     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2372     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2373     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2374     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2375     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2376     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2377     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2378            gnus-ancient-mark))))
2379
2380 ;; Saving hidden threads.
2381
2382 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2383 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2384
2385 (defmacro gnus-save-hidden-threads (&rest forms)
2386   "Save hidden threads, eval FORMS, and restore the hidden threads."
2387   (let ((config (make-symbol "config")))
2388     `(let ((,config (gnus-hidden-threads-configuration)))
2389        (unwind-protect
2390            (save-excursion
2391              ,@forms)
2392          (gnus-restore-hidden-threads-configuration ,config)))))
2393
2394 (defun gnus-data-compute-positions ()
2395   "Compute the positions of all articles."
2396   (setq gnus-newsgroup-data-reverse nil)
2397   (let ((data gnus-newsgroup-data))
2398     (save-excursion
2399       (gnus-save-hidden-threads
2400         (gnus-summary-show-all-threads)
2401         (goto-char (point-min))
2402         (while data
2403           (while (get-text-property (point) 'gnus-intangible)
2404             (forward-line 1))
2405           (gnus-data-set-pos (car data) (+ (point) 3))
2406           (setq data (cdr data))
2407           (forward-line 1))))))
2408
2409 (defun gnus-hidden-threads-configuration ()
2410   "Return the current hidden threads configuration."
2411   (save-excursion
2412     (let (config)
2413       (goto-char (point-min))
2414       (while (search-forward "\r" nil t)
2415         (push (1- (point)) config))
2416       config)))
2417
2418 (defun gnus-restore-hidden-threads-configuration (config)
2419   "Restore hidden threads configuration from CONFIG."
2420   (let (point buffer-read-only)
2421     (while (setq point (pop config))
2422       (when (and (< point (point-max))
2423                  (goto-char point)
2424                  (eq (char-after) ?\n))
2425         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2426
2427 ;; Various summary mode internalish functions.
2428
2429 (defun gnus-mouse-pick-article (e)
2430   (interactive "e")
2431   (mouse-set-point e)
2432   (gnus-summary-next-page nil t))
2433
2434 (defun gnus-summary-set-display-table ()
2435   ;; Change the display table.  Odd characters have a tendency to mess
2436   ;; up nicely formatted displays - we make all possible glyphs
2437   ;; display only a single character.
2438
2439   ;; We start from the standard display table, if any.
2440   (let ((table (or (copy-sequence standard-display-table)
2441                    (make-display-table)))
2442         (i 32))
2443     ;; Nix out all the control chars...
2444     (while (>= (setq i (1- i)) 0)
2445       (aset table i [??]))
2446     ;; ... but not newline and cr, of course.  (cr is necessary for the
2447     ;; selective display).
2448     (aset table ?\n nil)
2449     (aset table ?\r nil)
2450     ;; We keep TAB as well.
2451     (aset table ?\t nil)
2452     ;; We nix out any glyphs over 126 that are not set already.
2453     (let ((i 256))
2454       (while (>= (setq i (1- i)) 127)
2455         ;; Only modify if the entry is nil.
2456         (unless (aref table i)
2457           (aset table i [??]))))
2458     (setq buffer-display-table table)))
2459
2460 (defun gnus-summary-setup-buffer (group)
2461   "Initialize summary buffer."
2462   (let ((buffer (concat "*Summary " group "*")))
2463     (if (get-buffer buffer)
2464         (progn
2465           (set-buffer buffer)
2466           (setq gnus-summary-buffer (current-buffer))
2467           (not gnus-newsgroup-prepared))
2468       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2469       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2470       (gnus-summary-mode group)
2471       (when gnus-carpal
2472         (gnus-carpal-setup-buffer 'summary))
2473       (unless gnus-single-article-buffer
2474         (make-local-variable 'gnus-article-buffer)
2475         (make-local-variable 'gnus-article-current)
2476         (make-local-variable 'gnus-original-article-buffer))
2477       (setq gnus-newsgroup-name group)
2478       t)))
2479
2480 (defun gnus-set-global-variables ()
2481   ;; Set the global equivalents of the summary buffer-local variables
2482   ;; to the latest values they had.  These reflect the summary buffer
2483   ;; that was in action when the last article was fetched.
2484   (when (eq major-mode 'gnus-summary-mode)
2485     (setq gnus-summary-buffer (current-buffer))
2486     (let ((name gnus-newsgroup-name)
2487           (marked gnus-newsgroup-marked)
2488           (unread gnus-newsgroup-unreads)
2489           (headers gnus-current-headers)
2490           (data gnus-newsgroup-data)
2491           (summary gnus-summary-buffer)
2492           (article-buffer gnus-article-buffer)
2493           (original gnus-original-article-buffer)
2494           (gac gnus-article-current)
2495           (reffed gnus-reffed-article-number)
2496           (score-file gnus-current-score-file)
2497           (default-charset gnus-newsgroup-charset))
2498       (save-excursion
2499         (set-buffer gnus-group-buffer)
2500         (setq gnus-newsgroup-name name
2501               gnus-newsgroup-marked marked
2502               gnus-newsgroup-unreads unread
2503               gnus-current-headers headers
2504               gnus-newsgroup-data data
2505               gnus-article-current gac
2506               gnus-summary-buffer summary
2507               gnus-article-buffer article-buffer
2508               gnus-original-article-buffer original
2509               gnus-reffed-article-number reffed
2510               gnus-current-score-file score-file
2511               gnus-newsgroup-charset default-charset)
2512         ;; The article buffer also has local variables.
2513         (when (gnus-buffer-live-p gnus-article-buffer)
2514           (set-buffer gnus-article-buffer)
2515           (setq gnus-summary-buffer summary))))))
2516
2517 (defun gnus-summary-article-unread-p (article)
2518   "Say whether ARTICLE is unread or not."
2519   (memq article gnus-newsgroup-unreads))
2520
2521 (defun gnus-summary-first-article-p (&optional article)
2522   "Return whether ARTICLE is the first article in the buffer."
2523   (if (not (setq article (or article (gnus-summary-article-number))))
2524       nil
2525     (eq article (caar gnus-newsgroup-data))))
2526
2527 (defun gnus-summary-last-article-p (&optional article)
2528   "Return whether ARTICLE is the last article in the buffer."
2529   (if (not (setq article (or article (gnus-summary-article-number))))
2530       ;; All non-existent numbers are the last article.  :-)
2531       t
2532     (not (cdr (gnus-data-find-list article)))))
2533
2534 (defun gnus-make-thread-indent-array ()
2535   (let ((n 200))
2536     (unless (and gnus-thread-indent-array
2537                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2538       (setq gnus-thread-indent-array (make-vector 201 "")
2539             gnus-thread-indent-array-level gnus-thread-indent-level)
2540       (while (>= n 0)
2541         (aset gnus-thread-indent-array n
2542               (make-string (* n gnus-thread-indent-level) ? ))
2543         (setq n (1- n))))))
2544
2545 (defun gnus-update-summary-mark-positions ()
2546   "Compute where the summary marks are to go."
2547   (save-excursion
2548     (when (gnus-buffer-exists-p gnus-summary-buffer)
2549       (set-buffer gnus-summary-buffer))
2550     (let ((gnus-replied-mark 129)
2551           (gnus-score-below-mark 130)
2552           (gnus-score-over-mark 130)
2553           (gnus-download-mark 131)
2554           (spec gnus-summary-line-format-spec)
2555           gnus-visual pos)
2556       (save-excursion
2557         (gnus-set-work-buffer)
2558         (let ((gnus-summary-line-format-spec spec)
2559               (gnus-newsgroup-downloadable '((0 . t))))
2560           (gnus-summary-insert-line
2561            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2562           (goto-char (point-min))
2563           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2564                                              (- (point) 2)))))
2565           (goto-char (point-min))
2566           (push (cons 'replied (and (search-forward "\201" nil t)
2567                                     (- (point) 2)))
2568                 pos)
2569           (goto-char (point-min))
2570           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2571                 pos)
2572           (goto-char (point-min))
2573           (push (cons 'download
2574                       (and (search-forward "\203" nil t) (- (point) 2)))
2575                 pos)))
2576       (setq gnus-summary-mark-positions pos))))
2577
2578 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2579   "Insert a dummy root in the summary buffer."
2580   (beginning-of-line)
2581   (gnus-add-text-properties
2582    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2583    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2584
2585 (defun gnus-summary-from-or-to-or-newsgroups (header)
2586   (let ((to (cdr (assq 'To (mail-header-extra header))))
2587         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2588         (mail-parse-charset gnus-newsgroup-charset)
2589         (mail-parse-ignored-charsets 
2590          (save-excursion (set-buffer gnus-summary-buffer)
2591                          gnus-newsgroup-ignored-charsets)))
2592     (cond
2593      ((and to
2594            gnus-ignored-from-addresses
2595            (string-match gnus-ignored-from-addresses
2596                          (mail-header-from header)))
2597       (concat "-> "
2598               (or (car (funcall gnus-extract-address-components
2599                                 (funcall
2600                                  gnus-decode-encoded-word-function to)))
2601                   (funcall gnus-decode-encoded-word-function to))))
2602      ((and newsgroups
2603            gnus-ignored-from-addresses
2604            (string-match gnus-ignored-from-addresses
2605                          (mail-header-from header)))
2606       (concat "=> " newsgroups))
2607      (t
2608       (or (car (funcall gnus-extract-address-components
2609                         (mail-header-from header)))
2610           (mail-header-from header))))))
2611
2612 (defun gnus-summary-insert-line (gnus-tmp-header
2613                                  gnus-tmp-level gnus-tmp-current
2614                                  gnus-tmp-unread gnus-tmp-replied
2615                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2616                                  &optional gnus-tmp-dummy gnus-tmp-score
2617                                  gnus-tmp-process)
2618   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2619          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2620          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2621          (gnus-tmp-score-char
2622           (if (or (null gnus-summary-default-score)
2623                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2624                       gnus-summary-zcore-fuzz))
2625               ?  ;Whitespace
2626             (if (< gnus-tmp-score gnus-summary-default-score)
2627                 gnus-score-below-mark gnus-score-over-mark)))
2628          (gnus-tmp-replied
2629           (cond (gnus-tmp-process gnus-process-mark)
2630                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2631                  gnus-cached-mark)
2632                 (gnus-tmp-replied gnus-replied-mark)
2633                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2634                  gnus-saved-mark)
2635                 (t gnus-unread-mark)))
2636          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2637          (gnus-tmp-name
2638           (cond
2639            ((string-match "<[^>]+> *$" gnus-tmp-from)
2640             (let ((beg (match-beginning 0)))
2641               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2642                        (substring gnus-tmp-from (1+ (match-beginning 0))
2643                                   (1- (match-end 0))))
2644                   (substring gnus-tmp-from 0 beg))))
2645            ((string-match "(.+)" gnus-tmp-from)
2646             (substring gnus-tmp-from
2647                        (1+ (match-beginning 0)) (1- (match-end 0))))
2648            (t gnus-tmp-from)))
2649          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2650          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2651          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2652          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2653          (buffer-read-only nil))
2654     (when (string= gnus-tmp-name "")
2655       (setq gnus-tmp-name gnus-tmp-from))
2656     (unless (numberp gnus-tmp-lines)
2657       (setq gnus-tmp-lines 0))
2658     (gnus-put-text-property
2659      (point)
2660      (progn (eval gnus-summary-line-format-spec) (point))
2661      'gnus-number gnus-tmp-number)
2662     (when (gnus-visual-p 'summary-highlight 'highlight)
2663       (forward-line -1)
2664       (gnus-run-hooks 'gnus-summary-update-hook)
2665       (forward-line 1))))
2666
2667 (defun gnus-summary-update-line (&optional dont-update)
2668   ;; Update summary line after change.
2669   (when (and gnus-summary-default-score
2670              (not gnus-summary-inhibit-highlight))
2671     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2672            (article (gnus-summary-article-number))
2673            (score (gnus-summary-article-score article)))
2674       (unless dont-update
2675         (if (and gnus-summary-mark-below
2676                  (< (gnus-summary-article-score)
2677                     gnus-summary-mark-below))
2678             ;; This article has a low score, so we mark it as read.
2679             (when (memq article gnus-newsgroup-unreads)
2680               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2681           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2682             ;; This article was previously marked as read on account
2683             ;; of a low score, but now it has risen, so we mark it as
2684             ;; unread.
2685             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2686         (gnus-summary-update-mark
2687          (if (or (null gnus-summary-default-score)
2688                  (<= (abs (- score gnus-summary-default-score))
2689                      gnus-summary-zcore-fuzz))
2690              ?  ;Whitespace
2691            (if (< score gnus-summary-default-score)
2692                gnus-score-below-mark gnus-score-over-mark))
2693          'score))
2694       ;; Do visual highlighting.
2695       (when (gnus-visual-p 'summary-highlight 'highlight)
2696         (gnus-run-hooks 'gnus-summary-update-hook)))))
2697
2698 (defvar gnus-tmp-new-adopts nil)
2699
2700 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2701   "Return the number of articles in THREAD.
2702 This may be 0 in some cases -- if none of the articles in
2703 the thread are to be displayed."
2704   (let* ((number
2705           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2706           (cond
2707            ((not (listp thread))
2708             1)
2709            ((and (consp thread) (cdr thread))
2710             (apply
2711              '+ 1 (mapcar
2712                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2713            ((null thread)
2714             1)
2715            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2716             1)
2717            (t 0))))
2718     (when (and level (zerop level) gnus-tmp-new-adopts)
2719       (incf number
2720             (apply '+ (mapcar
2721                        'gnus-summary-number-of-articles-in-thread
2722                        gnus-tmp-new-adopts))))
2723     (if char
2724         (if (> number 1) gnus-not-empty-thread-mark
2725           gnus-empty-thread-mark)
2726       number)))
2727
2728 (defun gnus-summary-set-local-parameters (group)
2729   "Go through the local params of GROUP and set all variable specs in that list."
2730   (let ((params (gnus-group-find-parameter group))
2731         elem)
2732     (while params
2733       (setq elem (car params)
2734             params (cdr params))
2735       (and (consp elem)                 ; Has to be a cons.
2736            (consp (cdr elem))           ; The cdr has to be a list.
2737            (symbolp (car elem))         ; Has to be a symbol in there.
2738            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2739            (ignore-errors               ; So we set it.
2740              (make-local-variable (car elem))
2741              (set (car elem) (eval (nth 1 elem))))))))
2742
2743 (defun gnus-summary-read-group (group &optional show-all no-article
2744                                       kill-buffer no-display backward
2745                                       select-articles)
2746   "Start reading news in newsgroup GROUP.
2747 If SHOW-ALL is non-nil, already read articles are also listed.
2748 If NO-ARTICLE is non-nil, no article is selected initially.
2749 If NO-DISPLAY, don't generate a summary buffer."
2750   (let (result)
2751     (while (and group
2752                 (null (setq result
2753                             (let ((gnus-auto-select-next nil))
2754                               (or (gnus-summary-read-group-1
2755                                    group show-all no-article
2756                                    kill-buffer no-display
2757                                    select-articles)
2758                                   (setq show-all nil
2759                                         select-articles nil)))))
2760                 (eq gnus-auto-select-next 'quietly))
2761       (set-buffer gnus-group-buffer)
2762       ;; The entry function called above goes to the next
2763       ;; group automatically, so we go two groups back
2764       ;; if we are searching for the previous group.
2765       (when backward
2766         (gnus-group-prev-unread-group 2))
2767       (if (not (equal group (gnus-group-group-name)))
2768           (setq group (gnus-group-group-name))
2769         (setq group nil)))
2770     result))
2771
2772 (defun gnus-summary-read-group-1 (group show-all no-article
2773                                         kill-buffer no-display
2774                                         &optional select-articles)
2775   ;; Killed foreign groups can't be entered.
2776   (when (and (not (gnus-group-native-p group))
2777              (not (gnus-gethash group gnus-newsrc-hashtb)))
2778     (error "Dead non-native groups can't be entered"))
2779   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2780   (let* ((new-group (gnus-summary-setup-buffer group))
2781          (quit-config (gnus-group-quit-config group))
2782          (did-select (and new-group (gnus-select-newsgroup
2783                                      group show-all select-articles))))
2784     (cond
2785      ;; This summary buffer exists already, so we just select it.
2786      ((not new-group)
2787       (gnus-set-global-variables)
2788       (when kill-buffer
2789         (gnus-kill-or-deaden-summary kill-buffer))
2790       (gnus-configure-windows 'summary 'force)
2791       (gnus-set-mode-line 'summary)
2792       (gnus-summary-position-point)
2793       (message "")
2794       t)
2795      ;; We couldn't select this group.
2796      ((null did-select)
2797       (when (and (eq major-mode 'gnus-summary-mode)
2798                  (not (equal (current-buffer) kill-buffer)))
2799         (kill-buffer (current-buffer))
2800         (if (not quit-config)
2801             (progn
2802               ;; Update the info -- marks might need to be removed,
2803               ;; for instance.
2804               (gnus-summary-update-info)
2805               (set-buffer gnus-group-buffer)
2806               (gnus-group-jump-to-group group)
2807               (gnus-group-next-unread-group 1))
2808           (gnus-handle-ephemeral-exit quit-config)))
2809       (gnus-message 3 "Can't select group")
2810       nil)
2811      ;; The user did a `C-g' while prompting for number of articles,
2812      ;; so we exit this group.
2813      ((eq did-select 'quit)
2814       (and (eq major-mode 'gnus-summary-mode)
2815            (not (equal (current-buffer) kill-buffer))
2816            (kill-buffer (current-buffer)))
2817       (when kill-buffer
2818         (gnus-kill-or-deaden-summary kill-buffer))
2819       (if (not quit-config)
2820           (progn
2821             (set-buffer gnus-group-buffer)
2822             (gnus-group-jump-to-group group)
2823             (gnus-group-next-unread-group 1)
2824             (gnus-configure-windows 'group 'force))
2825         (gnus-handle-ephemeral-exit quit-config))
2826       ;; Finally signal the quit.
2827       (signal 'quit nil))
2828      ;; The group was successfully selected.
2829      (t
2830       (gnus-set-global-variables)
2831       ;; Save the active value in effect when the group was entered.
2832       (setq gnus-newsgroup-active
2833             (gnus-copy-sequence
2834              (gnus-active gnus-newsgroup-name)))
2835       ;; You can change the summary buffer in some way with this hook.
2836       (gnus-run-hooks 'gnus-select-group-hook)
2837       ;; Set any local variables in the group parameters.
2838       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2839       (gnus-update-format-specifications
2840        nil 'summary 'summary-mode 'summary-dummy)
2841       (gnus-update-summary-mark-positions)
2842       ;; Do score processing.
2843       (when gnus-use-scoring
2844         (gnus-possibly-score-headers))
2845       ;; Check whether to fill in the gaps in the threads.
2846       (when gnus-build-sparse-threads
2847         (gnus-build-sparse-threads))
2848       ;; Find the initial limit.
2849       (if gnus-show-threads
2850           (if show-all
2851               (let ((gnus-newsgroup-dormant nil))
2852                 (gnus-summary-initial-limit show-all))
2853             (gnus-summary-initial-limit show-all))
2854         ;; When untreaded, all articles are always shown.
2855         (setq gnus-newsgroup-limit
2856               (mapcar
2857                (lambda (header) (mail-header-number header))
2858                gnus-newsgroup-headers)))
2859       ;; Generate the summary buffer.
2860       (unless no-display
2861         (gnus-summary-prepare))
2862       (when gnus-use-trees
2863         (gnus-tree-open group)
2864         (setq gnus-summary-highlight-line-function
2865               'gnus-tree-highlight-article))
2866       ;; If the summary buffer is empty, but there are some low-scored
2867       ;; articles or some excluded dormants, we include these in the
2868       ;; buffer.
2869       (when (and (zerop (buffer-size))
2870                  (not no-display))
2871         (cond (gnus-newsgroup-dormant
2872                (gnus-summary-limit-include-dormant))
2873               ((and gnus-newsgroup-scored show-all)
2874                (gnus-summary-limit-include-expunged t))))
2875       ;; Function `gnus-apply-kill-file' must be called in this hook.
2876       (gnus-run-hooks 'gnus-apply-kill-hook)
2877       (if (and (zerop (buffer-size))
2878                (not no-display))
2879           (progn
2880             ;; This newsgroup is empty.
2881             (gnus-summary-catchup-and-exit nil t)
2882             (gnus-message 6 "No unread news")
2883             (when kill-buffer
2884               (gnus-kill-or-deaden-summary kill-buffer))
2885             ;; Return nil from this function.
2886             nil)
2887         ;; Hide conversation thread subtrees.  We cannot do this in
2888         ;; gnus-summary-prepare-hook since kill processing may not
2889         ;; work with hidden articles.
2890         (and gnus-show-threads
2891              gnus-thread-hide-subtree
2892              (gnus-summary-hide-all-threads))
2893         (when kill-buffer
2894           (gnus-kill-or-deaden-summary kill-buffer))
2895         ;; Show first unread article if requested.
2896         (if (and (not no-article)
2897                  (not no-display)
2898                  gnus-newsgroup-unreads
2899                  gnus-auto-select-first)
2900             (progn
2901               (gnus-configure-windows 'summary)
2902               (cond
2903                ((eq gnus-auto-select-first 'best)
2904                 (gnus-summary-best-unread-article))
2905                ((eq gnus-auto-select-first t)
2906                 (gnus-summary-first-unread-article))
2907                ((gnus-functionp gnus-auto-select-first)
2908                 (funcall gnus-auto-select-first))))
2909           ;; Don't select any articles, just move point to the first
2910           ;; article in the group.
2911           (goto-char (point-min))
2912           (gnus-summary-position-point)
2913           (gnus-configure-windows 'summary 'force)
2914           (gnus-set-mode-line 'summary))
2915         (when (get-buffer-window gnus-group-buffer t)
2916           ;; Gotta use windows, because recenter does weird stuff if
2917           ;; the current buffer ain't the displayed window.
2918           (let ((owin (selected-window)))
2919             (select-window (get-buffer-window gnus-group-buffer t))
2920             (when (gnus-group-goto-group group)
2921               (recenter))
2922             (select-window owin)))
2923         ;; Mark this buffer as "prepared".
2924         (setq gnus-newsgroup-prepared t)
2925         (gnus-run-hooks 'gnus-summary-prepared-hook)
2926         t)))))
2927
2928 (defun gnus-summary-prepare ()
2929   "Generate the summary buffer."
2930   (interactive)
2931   (let ((buffer-read-only nil))
2932     (erase-buffer)
2933     (setq gnus-newsgroup-data nil
2934           gnus-newsgroup-data-reverse nil)
2935     (gnus-run-hooks 'gnus-summary-generate-hook)
2936     ;; Generate the buffer, either with threads or without.
2937     (when gnus-newsgroup-headers
2938       (gnus-summary-prepare-threads
2939        (if gnus-show-threads
2940            (gnus-sort-gathered-threads
2941             (funcall gnus-summary-thread-gathering-function
2942                      (gnus-sort-threads
2943                       (gnus-cut-threads (gnus-make-threads)))))
2944          ;; Unthreaded display.
2945          (gnus-sort-articles gnus-newsgroup-headers))))
2946     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2947     ;; Call hooks for modifying summary buffer.
2948     (goto-char (point-min))
2949     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2950
2951 (defsubst gnus-general-simplify-subject (subject)
2952   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2953   (setq subject
2954         (cond
2955          ;; Truncate the subject.
2956          (gnus-simplify-subject-functions
2957           (gnus-map-function gnus-simplify-subject-functions subject))
2958          ((numberp gnus-summary-gather-subject-limit)
2959           (setq subject (gnus-simplify-subject-re subject))
2960           (if (> (length subject) gnus-summary-gather-subject-limit)
2961               (substring subject 0 gnus-summary-gather-subject-limit)
2962             subject))
2963          ;; Fuzzily simplify it.
2964          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2965           (gnus-simplify-subject-fuzzy subject))
2966          ;; Just remove the leading "Re:".
2967          (t
2968           (gnus-simplify-subject-re subject))))
2969
2970   (if (and gnus-summary-gather-exclude-subject
2971            (string-match gnus-summary-gather-exclude-subject subject))
2972       nil                               ; This article shouldn't be gathered
2973     subject))
2974
2975 (defun gnus-summary-simplify-subject-query ()
2976   "Query where the respool algorithm would put this article."
2977   (interactive)
2978   (gnus-summary-select-article)
2979   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2980
2981 (defun gnus-gather-threads-by-subject (threads)
2982   "Gather threads by looking at Subject headers."
2983   (if (not gnus-summary-make-false-root)
2984       threads
2985     (let ((hashtb (gnus-make-hashtable 1024))
2986           (prev threads)
2987           (result threads)
2988           subject hthread whole-subject)
2989       (while threads
2990         (setq subject (gnus-general-simplify-subject
2991                        (setq whole-subject (mail-header-subject
2992                                             (caar threads)))))
2993         (when subject
2994           (if (setq hthread (gnus-gethash subject hashtb))
2995               (progn
2996                 ;; We enter a dummy root into the thread, if we
2997                 ;; haven't done that already.
2998                 (unless (stringp (caar hthread))
2999                   (setcar hthread (list whole-subject (car hthread))))
3000                 ;; We add this new gathered thread to this gathered
3001                 ;; thread.
3002                 (setcdr (car hthread)
3003                         (nconc (cdar hthread) (list (car threads))))
3004                 ;; Remove it from the list of threads.
3005                 (setcdr prev (cdr threads))
3006                 (setq threads prev))
3007             ;; Enter this thread into the hash table.
3008             (gnus-sethash subject threads hashtb)))
3009         (setq prev threads)
3010         (setq threads (cdr threads)))
3011       result)))
3012
3013 (defun gnus-gather-threads-by-references (threads)
3014   "Gather threads by looking at References headers."
3015   (let ((idhashtb (gnus-make-hashtable 1024))
3016         (thhashtb (gnus-make-hashtable 1024))
3017         (prev threads)
3018         (result threads)
3019         ids references id gthread gid entered ref)
3020     (while threads
3021       (when (setq references (mail-header-references (caar threads)))
3022         (setq id (mail-header-id (caar threads))
3023               ids (gnus-split-references references)
3024               entered nil)
3025         (while (setq ref (pop ids))
3026           (setq ids (delete ref ids))
3027           (if (not (setq gid (gnus-gethash ref idhashtb)))
3028               (progn
3029                 (gnus-sethash ref id idhashtb)
3030                 (gnus-sethash id threads thhashtb))
3031             (setq gthread (gnus-gethash gid thhashtb))
3032             (unless entered
3033               ;; We enter a dummy root into the thread, if we
3034               ;; haven't done that already.
3035               (unless (stringp (caar gthread))
3036                 (setcar gthread (list (mail-header-subject (caar gthread))
3037                                       (car gthread))))
3038               ;; We add this new gathered thread to this gathered
3039               ;; thread.
3040               (setcdr (car gthread)
3041                       (nconc (cdar gthread) (list (car threads)))))
3042             ;; Add it into the thread hash table.
3043             (gnus-sethash id gthread thhashtb)
3044             (setq entered t)
3045             ;; Remove it from the list of threads.
3046             (setcdr prev (cdr threads))
3047             (setq threads prev))))
3048       (setq prev threads)
3049       (setq threads (cdr threads)))
3050     result))
3051
3052 (defun gnus-sort-gathered-threads (threads)
3053   "Sort subtreads inside each gathered thread by article number."
3054   (let ((result threads))
3055     (while threads
3056       (when (stringp (caar threads))
3057         (setcdr (car threads)
3058                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3059       (setq threads (cdr threads)))
3060     result))
3061
3062 (defun gnus-thread-loop-p (root thread)
3063   "Say whether ROOT is in THREAD."
3064   (let ((stack (list thread))
3065         (infloop 0)
3066         th)
3067     (while (setq thread (pop stack))
3068       (setq th (cdr thread))
3069       (while (and th
3070                   (not (eq (caar th) root)))
3071         (pop th))
3072       (if th
3073           ;; We have found a loop.
3074           (let (ref-dep)
3075             (setcdr thread (delq (car th) (cdr thread)))
3076             (if (boundp (setq ref-dep (intern "none"
3077                                               gnus-newsgroup-dependencies)))
3078                 (setcdr (symbol-value ref-dep)
3079                         (nconc (cdr (symbol-value ref-dep))
3080                                (list (car th))))
3081               (set ref-dep (list nil (car th))))
3082             (setq infloop 1
3083                   stack nil))
3084         ;; Push all the subthreads onto the stack.
3085         (push (cdr thread) stack)))
3086     infloop))
3087
3088 (defun gnus-make-threads ()
3089   "Go through the dependency hashtb and find the roots.  Return all threads."
3090   (let (threads)
3091     (while (catch 'infloop
3092              (mapatoms
3093               (lambda (refs)
3094                 ;; Deal with self-referencing References loops.
3095                 (when (and (car (symbol-value refs))
3096                            (not (zerop
3097                                  (apply
3098                                   '+
3099                                   (mapcar
3100                                    (lambda (thread)
3101                                      (gnus-thread-loop-p
3102                                       (car (symbol-value refs)) thread))
3103                                    (cdr (symbol-value refs)))))))
3104                   (setq threads nil)
3105                   (throw 'infloop t))
3106                 (unless (car (symbol-value refs))
3107                   ;; These threads do not refer back to any other articles,
3108                   ;; so they're roots.
3109                   (setq threads (append (cdr (symbol-value refs)) threads))))
3110               gnus-newsgroup-dependencies)))
3111     threads))
3112
3113 ;; Build the thread tree.
3114 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3115   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3116
3117 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3118 if it was already present.
3119
3120 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3121 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3122 Message-IDs will be renamed be renamed to a unique Message-ID before
3123 being entered.
3124
3125 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3126   (let* ((id (mail-header-id header))
3127          (id-dep (and id (intern id dependencies)))
3128          ref ref-dep ref-header)
3129     ;; Enter this `header' in the `dependencies' table.
3130     (cond
3131      ((not id-dep)
3132       (setq header nil))
3133      ;; The first two cases do the normal part: enter a new `header'
3134      ;; in the `dependencies' table.
3135      ((not (boundp id-dep))
3136       (set id-dep (list header)))
3137      ((null (car (symbol-value id-dep)))
3138       (setcar (symbol-value id-dep) header))
3139
3140      ;; From here the `header' was already present in the
3141      ;; `dependencies' table.
3142      (force-new
3143       ;; Overrides an existing entry;
3144       ;; just set the header part of the entry.
3145       (setcar (symbol-value id-dep) header))
3146
3147      ;; Renames the existing `header' to a unique Message-ID.
3148      ((not gnus-summary-ignore-duplicates)
3149       ;; An article with this Message-ID has already been seen.
3150       ;; We rename the Message-ID.
3151       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3152            (list header))
3153       (mail-header-set-id header id))
3154
3155      ;; The last case ignores an existing entry, except it adds any
3156      ;; additional Xrefs (in case the two articles came from different
3157      ;; servers.
3158      ;; Also sets `header' to `nil' meaning that the `dependencies'
3159      ;; table was *not* modified.
3160      (t
3161       (mail-header-set-xref
3162        (car (symbol-value id-dep))
3163        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3164                    "")
3165                (or (mail-header-xref header) "")))
3166       (setq header nil)))
3167
3168     (when header
3169       ;; First check if that we are not creating a References loop.
3170       (setq ref (gnus-parent-id (mail-header-references header)))
3171       (while (and ref
3172                   (setq ref-dep (intern-soft ref dependencies))
3173                   (boundp ref-dep)
3174                   (setq ref-header (car (symbol-value ref-dep))))
3175         (if (string= id ref)
3176             ;; Yuk!  This is a reference loop.  Make the article be a
3177             ;; root article.
3178             (progn
3179               (mail-header-set-references (car (symbol-value id-dep)) "none")
3180               (setq ref nil))
3181           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3182       (setq ref (gnus-parent-id (mail-header-references header)))
3183       (setq ref-dep (intern (or ref "none") dependencies))
3184       (if (boundp ref-dep)
3185           (setcdr (symbol-value ref-dep)
3186                   (nconc (cdr (symbol-value ref-dep))
3187                          (list (symbol-value id-dep))))
3188         (set ref-dep (list nil (symbol-value id-dep)))))
3189     header))
3190
3191 (defun gnus-build-sparse-threads ()
3192   (let ((headers gnus-newsgroup-headers)
3193         (mail-parse-charset gnus-newsgroup-charset)
3194         (gnus-summary-ignore-duplicates t)
3195         header references generation relations
3196         subject child end new-child date)
3197     ;; First we create an alist of generations/relations, where
3198     ;; generations is how much we trust the relation, and the relation
3199     ;; is parent/child.
3200     (gnus-message 7 "Making sparse threads...")
3201     (save-excursion
3202       (nnheader-set-temp-buffer " *gnus sparse threads*")
3203       (while (setq header (pop headers))
3204         (when (and (setq references (mail-header-references header))
3205                    (not (string= references "")))
3206           (insert references)
3207           (setq child (mail-header-id header)
3208                 subject (mail-header-subject header)
3209                 date (mail-header-date header)
3210                 generation 0)
3211           (while (search-backward ">" nil t)
3212             (setq end (1+ (point)))
3213             (when (search-backward "<" nil t)
3214               (setq new-child (buffer-substring (point) end))
3215               (push (list (incf generation)
3216                           child (setq child new-child)
3217                           subject date)
3218                     relations)))
3219           (when child
3220             (push (list (1+ generation) child nil subject) relations))
3221           (erase-buffer)))
3222       (kill-buffer (current-buffer)))
3223     ;; Sort over trustworthiness.
3224     (mapcar
3225      (lambda (relation)
3226        (when (gnus-dependencies-add-header
3227               (make-full-mail-header
3228                gnus-reffed-article-number
3229                (nth 3 relation) "" (or (nth 4 relation) "")
3230                (nth 1 relation)
3231                (or (nth 2 relation) "") 0 0 "")
3232               gnus-newsgroup-dependencies nil)
3233          (push gnus-reffed-article-number gnus-newsgroup-limit)
3234          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3235          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3236                gnus-newsgroup-reads)
3237          (decf gnus-reffed-article-number)))
3238      (sort relations 'car-less-than-car))
3239     (gnus-message 7 "Making sparse threads...done")))
3240
3241 (defun gnus-build-old-threads ()
3242   ;; Look at all the articles that refer back to old articles, and
3243   ;; fetch the headers for the articles that aren't there.  This will
3244   ;; build complete threads - if the roots haven't been expired by the
3245   ;; server, that is.
3246   (let ((mail-parse-charset gnus-newsgroup-charset)
3247         id heads)
3248     (mapatoms
3249      (lambda (refs)
3250        (when (not (car (symbol-value refs)))
3251          (setq heads (cdr (symbol-value refs)))
3252          (while heads
3253            (if (memq (mail-header-number (caar heads))
3254                      gnus-newsgroup-dormant)
3255                (setq heads (cdr heads))
3256              (setq id (symbol-name refs))
3257              (while (and (setq id (gnus-build-get-header id))
3258                          (not (car (gnus-id-to-thread id)))))
3259              (setq heads nil)))))
3260      gnus-newsgroup-dependencies)))
3261
3262 ;; This function has to be called with point after the article number
3263 ;; on the beginning of the line.
3264 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3265   (let ((eol (gnus-point-at-eol))
3266         (buffer (current-buffer))
3267         header)
3268
3269     ;; overview: [num subject from date id refs chars lines misc]
3270     (unwind-protect
3271         (progn
3272           (narrow-to-region (point) eol)
3273           (unless (eobp)
3274             (forward-char))
3275
3276           (setq header
3277                 (make-full-mail-header
3278                  number                 ; number
3279                  (funcall gnus-decode-encoded-word-function
3280                           (nnheader-nov-field)) ; subject
3281                  (funcall gnus-decode-encoded-word-function
3282                           (nnheader-nov-field)) ; from
3283                  (nnheader-nov-field)   ; date
3284                  (nnheader-nov-read-message-id) ; id
3285                  (nnheader-nov-field)   ; refs
3286                  (nnheader-nov-read-integer) ; chars
3287                  (nnheader-nov-read-integer) ; lines
3288                  (unless (eobp)
3289                    (nnheader-nov-field)) ; misc
3290                  (nnheader-nov-parse-extra)))) ; extra
3291
3292       (widen))
3293
3294     (when gnus-alter-header-function
3295       (funcall gnus-alter-header-function header))
3296     (gnus-dependencies-add-header header dependencies force-new)))
3297
3298 (defun gnus-build-get-header (id)
3299   ;; Look through the buffer of NOV lines and find the header to
3300   ;; ID.  Enter this line into the dependencies hash table, and return
3301   ;; the id of the parent article (if any).
3302   (let ((deps gnus-newsgroup-dependencies)
3303         found header)
3304     (prog1
3305         (save-excursion
3306           (set-buffer nntp-server-buffer)
3307           (let ((case-fold-search nil))
3308             (goto-char (point-min))
3309             (while (and (not found)
3310                         (search-forward id nil t))
3311               (beginning-of-line)
3312               (setq found (looking-at
3313                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3314                                    (regexp-quote id))))
3315               (or found (beginning-of-line 2)))
3316             (when found
3317               (beginning-of-line)
3318               (and
3319                (setq header (gnus-nov-parse-line
3320                              (read (current-buffer)) deps))
3321                (gnus-parent-id (mail-header-references header))))))
3322       (when header
3323         (let ((number (mail-header-number header)))
3324           (push number gnus-newsgroup-limit)
3325           (push header gnus-newsgroup-headers)
3326           (if (memq number gnus-newsgroup-unselected)
3327               (progn
3328                 (push number gnus-newsgroup-unreads)
3329                 (setq gnus-newsgroup-unselected
3330                       (delq number gnus-newsgroup-unselected)))
3331             (push number gnus-newsgroup-ancient)))))))
3332
3333 (defun gnus-build-all-threads ()
3334   "Read all the headers."
3335   (let ((gnus-summary-ignore-duplicates t)
3336         (mail-parse-charset gnus-newsgroup-charset)
3337         (dependencies gnus-newsgroup-dependencies)
3338         header article)
3339     (save-excursion
3340       (set-buffer nntp-server-buffer)
3341       (let ((case-fold-search nil))
3342         (goto-char (point-min))
3343         (while (not (eobp))
3344           (ignore-errors
3345             (setq article (read (current-buffer))
3346                   header (gnus-nov-parse-line article dependencies)))
3347           (when header
3348             (save-excursion
3349               (set-buffer gnus-summary-buffer)
3350               (push header gnus-newsgroup-headers)
3351               (if (memq (setq article (mail-header-number header))
3352                         gnus-newsgroup-unselected)
3353                   (progn
3354                     (push article gnus-newsgroup-unreads)
3355                     (setq gnus-newsgroup-unselected
3356                           (delq article gnus-newsgroup-unselected)))
3357                 (push article gnus-newsgroup-ancient)))
3358             (forward-line 1)))))))
3359
3360 (defun gnus-summary-update-article-line (article header)
3361   "Update the line for ARTICLE using HEADERS."
3362   (let* ((id (mail-header-id header))
3363          (thread (gnus-id-to-thread id)))
3364     (unless thread
3365       (error "Article in no thread"))
3366     ;; Update the thread.
3367     (setcar thread header)
3368     (gnus-summary-goto-subject article)
3369     (let* ((datal (gnus-data-find-list article))
3370            (data (car datal))
3371            (length (when (cdr datal)
3372                      (- (gnus-data-pos data)
3373                         (gnus-data-pos (cadr datal)))))
3374            (buffer-read-only nil)
3375            (level (gnus-summary-thread-level)))
3376       (gnus-delete-line)
3377       (gnus-summary-insert-line
3378        header level nil (gnus-article-mark article)
3379        (memq article gnus-newsgroup-replied)
3380        (memq article gnus-newsgroup-expirable)
3381        ;; Only insert the Subject string when it's different
3382        ;; from the previous Subject string.
3383        (if (and
3384             gnus-show-threads
3385             (gnus-subject-equal
3386              (condition-case ()
3387                  (mail-header-subject
3388                   (gnus-data-header
3389                    (cadr
3390                     (gnus-data-find-list
3391                      article
3392                      (gnus-data-list t)))))
3393                ;; Error on the side of excessive subjects.
3394                (error ""))
3395              (mail-header-subject header)))
3396            ""
3397          (mail-header-subject header))
3398        nil (cdr (assq article gnus-newsgroup-scored))
3399        (memq article gnus-newsgroup-processable))
3400       (when length
3401         (gnus-data-update-list
3402          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3403
3404 (defun gnus-summary-update-article (article &optional iheader)
3405   "Update ARTICLE in the summary buffer."
3406   (set-buffer gnus-summary-buffer)
3407   (let* ((header (gnus-summary-article-header article))
3408          (id (mail-header-id header))
3409          (data (gnus-data-find article))
3410          (thread (gnus-id-to-thread id))
3411          (references (mail-header-references header))
3412          (parent
3413           (gnus-id-to-thread
3414            (or (gnus-parent-id
3415                 (when (and references
3416                            (not (equal "" references)))
3417                   references))
3418                "none")))
3419          (buffer-read-only nil)
3420          (old (car thread)))
3421     (when thread
3422       (unless iheader
3423         (setcar thread nil)
3424         (when parent
3425           (delq thread parent)))
3426       (if (gnus-summary-insert-subject id header)
3427           ;; Set the (possibly) new article number in the data structure.
3428           (gnus-data-set-number data (gnus-id-to-article id))
3429         (setcar thread old)
3430         nil))))
3431
3432 (defun gnus-rebuild-thread (id &optional line)
3433   "Rebuild the thread containing ID.
3434 If LINE, insert the rebuilt thread starting on line LINE."
3435   (let ((buffer-read-only nil)
3436         old-pos current thread data)
3437     (if (not gnus-show-threads)
3438         (setq thread (list (car (gnus-id-to-thread id))))
3439       ;; Get the thread this article is part of.
3440       (setq thread (gnus-remove-thread id)))
3441     (setq old-pos (gnus-point-at-bol))
3442     (setq current (save-excursion
3443                     (and (zerop (forward-line -1))
3444                          (gnus-summary-article-number))))
3445     ;; If this is a gathered thread, we have to go some re-gathering.
3446     (when (stringp (car thread))
3447       (let ((subject (car thread))
3448             roots thr)
3449         (setq thread (cdr thread))
3450         (while thread
3451           (unless (memq (setq thr (gnus-id-to-thread
3452                                    (gnus-root-id
3453                                     (mail-header-id (caar thread)))))
3454                         roots)
3455             (push thr roots))
3456           (setq thread (cdr thread)))
3457         ;; We now have all (unique) roots.
3458         (if (= (length roots) 1)
3459             ;; All the loose roots are now one solid root.
3460             (setq thread (car roots))
3461           (setq thread (cons subject (gnus-sort-threads roots))))))
3462     (let (threads)
3463       ;; We then insert this thread into the summary buffer.
3464       (when line
3465         (goto-char (point-min))
3466         (forward-line (1- line)))
3467       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3468         (if gnus-show-threads
3469             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3470           (gnus-summary-prepare-unthreaded thread))
3471         (setq data (nreverse gnus-newsgroup-data))
3472         (setq threads gnus-newsgroup-threads))
3473       ;; We splice the new data into the data structure.
3474       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3475       ;;!!! then we want to insert at the beginning of the buffer.
3476       ;;!!! That happens to be true with Gnus now, but that may
3477       ;;!!! change in the future.  Perhaps.
3478       (gnus-data-enter-list
3479        (if line nil current) data (- (point) old-pos))
3480       (setq gnus-newsgroup-threads
3481             (nconc threads gnus-newsgroup-threads))
3482       (gnus-data-compute-positions))))
3483
3484 (defun gnus-number-to-header (number)
3485   "Return the header for article NUMBER."
3486   (let ((headers gnus-newsgroup-headers))
3487     (while (and headers
3488                 (not (= number (mail-header-number (car headers)))))
3489       (pop headers))
3490     (when headers
3491       (car headers))))
3492
3493 (defun gnus-parent-headers (in-headers &optional generation)
3494   "Return the headers of the GENERATIONeth parent of HEADERS."
3495   (unless generation
3496     (setq generation 1))
3497   (let ((parent t)
3498         (headers in-headers)
3499         references)
3500     (while (and parent
3501                 (not (zerop generation))
3502                 (setq references (mail-header-references headers)))
3503       (setq headers (if (and references
3504                              (setq parent (gnus-parent-id references)))
3505                         (car (gnus-id-to-thread parent))
3506                       nil))
3507       (decf generation))
3508     (and (not (eq headers in-headers))
3509          headers)))
3510
3511 (defun gnus-id-to-thread (id)
3512   "Return the (sub-)thread where ID appears."
3513   (gnus-gethash id gnus-newsgroup-dependencies))
3514
3515 (defun gnus-id-to-article (id)
3516   "Return the article number of ID."
3517   (let ((thread (gnus-id-to-thread id)))
3518     (when (and thread
3519                (car thread))
3520       (mail-header-number (car thread)))))
3521
3522 (defun gnus-id-to-header (id)
3523   "Return the article headers of ID."
3524   (car (gnus-id-to-thread id)))
3525
3526 (defun gnus-article-displayed-root-p (article)
3527   "Say whether ARTICLE is a root(ish) article."
3528   (let ((level (gnus-summary-thread-level article))
3529         (refs (mail-header-references  (gnus-summary-article-header article)))
3530         particle)
3531     (cond
3532      ((null level) nil)
3533      ((zerop level) t)
3534      ((null refs) t)
3535      ((null (gnus-parent-id refs)) t)
3536      ((and (= 1 level)
3537            (null (setq particle (gnus-id-to-article
3538                                  (gnus-parent-id refs))))
3539            (null (gnus-summary-thread-level particle)))))))
3540
3541 (defun gnus-root-id (id)
3542   "Return the id of the root of the thread where ID appears."
3543   (let (last-id prev)
3544     (while (and id (setq prev (car (gnus-id-to-thread id))))
3545       (setq last-id id
3546             id (gnus-parent-id (mail-header-references prev))))
3547     last-id))
3548
3549 (defun gnus-articles-in-thread (thread)
3550   "Return the list of articles in THREAD."
3551   (cons (mail-header-number (car thread))
3552         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3553
3554 (defun gnus-remove-thread (id &optional dont-remove)
3555   "Remove the thread that has ID in it."
3556   (let (headers thread last-id)
3557     ;; First go up in this thread until we find the root.
3558     (setq last-id (gnus-root-id id)
3559           headers (message-flatten-list (gnus-id-to-thread last-id)))
3560     ;; We have now found the real root of this thread.  It might have
3561     ;; been gathered into some loose thread, so we have to search
3562     ;; through the threads to find the thread we wanted.
3563     (let ((threads gnus-newsgroup-threads)
3564           sub)
3565       (while threads
3566         (setq sub (car threads))
3567         (if (stringp (car sub))
3568             ;; This is a gathered thread, so we look at the roots
3569             ;; below it to find whether this article is in this
3570             ;; gathered root.
3571             (progn
3572               (setq sub (cdr sub))
3573               (while sub
3574                 (when (member (caar sub) headers)
3575                   (setq thread (car threads)
3576                         threads nil
3577                         sub nil))
3578                 (setq sub (cdr sub))))
3579           ;; It's an ordinary thread, so we check it.
3580           (when (eq (car sub) (car headers))
3581             (setq thread sub
3582                   threads nil)))
3583         (setq threads (cdr threads)))
3584       ;; If this article is in no thread, then it's a root.
3585       (if thread
3586           (unless dont-remove
3587             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3588         (setq thread (gnus-id-to-thread last-id)))
3589       (when thread
3590         (prog1
3591             thread                      ; We return this thread.
3592           (unless dont-remove
3593             (if (stringp (car thread))
3594                 (progn
3595                   ;; If we use dummy roots, then we have to remove the
3596                   ;; dummy root as well.
3597                   (when (eq gnus-summary-make-false-root 'dummy)
3598                     ;; We go to the dummy root by going to
3599                     ;; the first sub-"thread", and then one line up.
3600                     (gnus-summary-goto-article
3601                      (mail-header-number (caadr thread)))
3602                     (forward-line -1)
3603                     (gnus-delete-line)
3604                     (gnus-data-compute-positions))
3605                   (setq thread (cdr thread))
3606                   (while thread
3607                     (gnus-remove-thread-1 (car thread))
3608                     (setq thread (cdr thread))))
3609               (gnus-remove-thread-1 thread))))))))
3610
3611 (defun gnus-remove-thread-1 (thread)
3612   "Remove the thread THREAD recursively."
3613   (let ((number (mail-header-number (pop thread)))
3614         d)
3615     (setq thread (reverse thread))
3616     (while thread
3617       (gnus-remove-thread-1 (pop thread)))
3618     (when (setq d (gnus-data-find number))
3619       (goto-char (gnus-data-pos d))
3620       (gnus-summary-show-thread)
3621       (gnus-data-remove
3622        number
3623        (- (gnus-point-at-bol)
3624           (prog1
3625               (1+ (gnus-point-at-eol))
3626             (gnus-delete-line)))))))
3627
3628 (defun gnus-sort-threads (threads)
3629   "Sort THREADS."
3630   (if (not gnus-thread-sort-functions)
3631       threads
3632     (gnus-message 8 "Sorting threads...")
3633     (prog1
3634         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3635       (gnus-message 8 "Sorting threads...done"))))
3636
3637 (defun gnus-sort-articles (articles)
3638   "Sort ARTICLES."
3639   (when gnus-article-sort-functions
3640     (gnus-message 7 "Sorting articles...")
3641     (prog1
3642         (setq gnus-newsgroup-headers
3643               (sort articles (gnus-make-sort-function
3644                               gnus-article-sort-functions)))
3645       (gnus-message 7 "Sorting articles...done"))))
3646
3647 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3648 (defmacro gnus-thread-header (thread)
3649   ;; Return header of first article in THREAD.
3650   ;; Note that THREAD must never, ever be anything else than a variable -
3651   ;; using some other form will lead to serious barfage.
3652   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3653   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3654   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" 
3655         (vector thread) 2))
3656
3657 (defsubst gnus-article-sort-by-number (h1 h2)
3658   "Sort articles by article number."
3659   (< (mail-header-number h1)
3660      (mail-header-number h2)))
3661
3662 (defun gnus-thread-sort-by-number (h1 h2)
3663   "Sort threads by root article number."
3664   (gnus-article-sort-by-number
3665    (gnus-thread-header h1) (gnus-thread-header h2)))
3666
3667 (defsubst gnus-article-sort-by-lines (h1 h2)
3668   "Sort articles by article Lines header."
3669   (< (mail-header-lines h1)
3670      (mail-header-lines h2)))
3671
3672 (defun gnus-thread-sort-by-lines (h1 h2)
3673   "Sort threads by root article Lines header."
3674   (gnus-article-sort-by-lines
3675    (gnus-thread-header h1) (gnus-thread-header h2)))
3676
3677 (defsubst gnus-article-sort-by-chars (h1 h2)
3678   "Sort articles by octet length."
3679   (< (mail-header-chars h1)
3680      (mail-header-chars h2)))
3681
3682 (defun gnus-thread-sort-by-chars (h1 h2)
3683   "Sort threads by root article octet length."
3684   (gnus-article-sort-by-chars
3685    (gnus-thread-header h1) (gnus-thread-header h2)))
3686
3687 (defsubst gnus-article-sort-by-author (h1 h2)
3688   "Sort articles by root author."
3689   (string-lessp
3690    (let ((extract (funcall
3691                    gnus-extract-address-components
3692                    (mail-header-from h1))))
3693      (or (car extract) (cadr extract) ""))
3694    (let ((extract (funcall
3695                    gnus-extract-address-components
3696                    (mail-header-from h2))))
3697      (or (car extract) (cadr extract) ""))))
3698
3699 (defun gnus-thread-sort-by-author (h1 h2)
3700   "Sort threads by root author."
3701   (gnus-article-sort-by-author
3702    (gnus-thread-header h1)  (gnus-thread-header h2)))
3703
3704 (defsubst gnus-article-sort-by-subject (h1 h2)
3705   "Sort articles by root subject."
3706   (string-lessp
3707    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3708    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3709
3710 (defun gnus-thread-sort-by-subject (h1 h2)
3711   "Sort threads by root subject."
3712   (gnus-article-sort-by-subject
3713    (gnus-thread-header h1) (gnus-thread-header h2)))
3714
3715 (defsubst gnus-article-sort-by-date (h1 h2)
3716   "Sort articles by root article date."
3717   (time-less-p
3718    (gnus-date-get-time (mail-header-date h1))
3719    (gnus-date-get-time (mail-header-date h2))))
3720
3721 (defun gnus-thread-sort-by-date (h1 h2)
3722   "Sort threads by root article date."
3723   (gnus-article-sort-by-date
3724    (gnus-thread-header h1) (gnus-thread-header h2)))
3725
3726 (defsubst gnus-article-sort-by-score (h1 h2)
3727   "Sort articles by root article score.
3728 Unscored articles will be counted as having a score of zero."
3729   (> (or (cdr (assq (mail-header-number h1)
3730                     gnus-newsgroup-scored))
3731          gnus-summary-default-score 0)
3732      (or (cdr (assq (mail-header-number h2)
3733                     gnus-newsgroup-scored))
3734          gnus-summary-default-score 0)))
3735
3736 (defun gnus-thread-sort-by-score (h1 h2)
3737   "Sort threads by root article score."
3738   (gnus-article-sort-by-score
3739    (gnus-thread-header h1) (gnus-thread-header h2)))
3740
3741 (defun gnus-thread-sort-by-total-score (h1 h2)
3742   "Sort threads by the sum of all scores in the thread.
3743 Unscored articles will be counted as having a score of zero."
3744   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3745
3746 (defun gnus-thread-total-score (thread)
3747   ;; This function find the total score of THREAD.
3748   (cond ((null thread)
3749          0)
3750         ((consp thread)
3751          (if (stringp (car thread))
3752              (apply gnus-thread-score-function 0
3753                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3754            (gnus-thread-total-score-1 thread)))
3755         (t
3756          (gnus-thread-total-score-1 (list thread)))))
3757
3758 (defun gnus-thread-total-score-1 (root)
3759   ;; This function find the total score of the thread below ROOT.
3760   (setq root (car root))
3761   (apply gnus-thread-score-function
3762          (or (append
3763               (mapcar 'gnus-thread-total-score
3764                       (cdr (gnus-id-to-thread (mail-header-id root))))
3765               (when (> (mail-header-number root) 0)
3766                 (list (or (cdr (assq (mail-header-number root)
3767                                      gnus-newsgroup-scored))
3768                           gnus-summary-default-score 0))))
3769              (list gnus-summary-default-score)
3770              '(0))))
3771
3772 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3773 (defvar gnus-tmp-prev-subject nil)
3774 (defvar gnus-tmp-false-parent nil)
3775 (defvar gnus-tmp-root-expunged nil)
3776 (defvar gnus-tmp-dummy-line nil)
3777
3778 (defvar gnus-tmp-header)
3779 (defun gnus-extra-header (type &optional header)
3780   "Return the extra header of TYPE."
3781   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3782       ""))
3783
3784 (defun gnus-summary-prepare-threads (threads)
3785   "Prepare summary buffer from THREADS and indentation LEVEL.
3786 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3787 or a straight list of headers."
3788   (gnus-message 7 "Generating summary...")
3789
3790   (setq gnus-newsgroup-threads threads)
3791   (beginning-of-line)
3792
3793   (let ((gnus-tmp-level 0)
3794         (default-score (or gnus-summary-default-score 0))
3795         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3796         thread number subject stack state gnus-tmp-gathered beg-match
3797         new-roots gnus-tmp-new-adopts thread-end
3798         gnus-tmp-header gnus-tmp-unread
3799         gnus-tmp-replied gnus-tmp-subject-or-nil
3800         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3801         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3802         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3803
3804     (setq gnus-tmp-prev-subject nil)
3805
3806     (if (vectorp (car threads))
3807         ;; If this is a straight (sic) list of headers, then a
3808         ;; threaded summary display isn't required, so we just create
3809         ;; an unthreaded one.
3810         (gnus-summary-prepare-unthreaded threads)
3811
3812       ;; Do the threaded display.
3813
3814       (while (or threads stack gnus-tmp-new-adopts new-roots)
3815
3816         (if (and (= gnus-tmp-level 0)
3817                  (or (not stack)
3818                      (= (caar stack) 0))
3819                  (not gnus-tmp-false-parent)
3820                  (or gnus-tmp-new-adopts new-roots))
3821             (if gnus-tmp-new-adopts
3822                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3823                       thread (list (car gnus-tmp-new-adopts))
3824                       gnus-tmp-header (caar thread)
3825                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3826               (when new-roots
3827                 (setq thread (list (car new-roots))
3828                       gnus-tmp-header (caar thread)
3829                       new-roots (cdr new-roots))))
3830
3831           (if threads
3832               ;; If there are some threads, we do them before the
3833               ;; threads on the stack.
3834               (setq thread threads
3835                     gnus-tmp-header (caar thread))
3836             ;; There were no current threads, so we pop something off
3837             ;; the stack.
3838             (setq state (car stack)
3839                   gnus-tmp-level (car state)
3840                   thread (cdr state)
3841                   stack (cdr stack)
3842                   gnus-tmp-header (caar thread))))
3843
3844         (setq gnus-tmp-false-parent nil)
3845         (setq gnus-tmp-root-expunged nil)
3846         (setq thread-end nil)
3847
3848         (if (stringp gnus-tmp-header)
3849             ;; The header is a dummy root.
3850             (cond
3851              ((eq gnus-summary-make-false-root 'adopt)
3852               ;; We let the first article adopt the rest.
3853               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3854                                                (cddar thread)))
3855               (setq gnus-tmp-gathered
3856                     (nconc (mapcar
3857                             (lambda (h) (mail-header-number (car h)))
3858                             (cddar thread))
3859                            gnus-tmp-gathered))
3860               (setq thread (cons (list (caar thread)
3861                                        (cadar thread))
3862                                  (cdr thread)))
3863               (setq gnus-tmp-level -1
3864                     gnus-tmp-false-parent t))
3865              ((eq gnus-summary-make-false-root 'empty)
3866               ;; We print adopted articles with empty subject fields.
3867               (setq gnus-tmp-gathered
3868                     (nconc (mapcar
3869                             (lambda (h) (mail-header-number (car h)))
3870                             (cddar thread))
3871                            gnus-tmp-gathered))
3872               (setq gnus-tmp-level -1))
3873              ((eq gnus-summary-make-false-root 'dummy)
3874               ;; We remember that we probably want to output a dummy
3875               ;; root.
3876               (setq gnus-tmp-dummy-line gnus-tmp-header)
3877               (setq gnus-tmp-prev-subject gnus-tmp-header))
3878              (t
3879               ;; We do not make a root for the gathered
3880               ;; sub-threads at all.
3881               (setq gnus-tmp-level -1)))
3882
3883           (setq number (mail-header-number gnus-tmp-header)
3884                 subject (mail-header-subject gnus-tmp-header))
3885
3886           (cond
3887            ;; If the thread has changed subject, we might want to make
3888            ;; this subthread into a root.
3889            ((and (null gnus-thread-ignore-subject)
3890                  (not (zerop gnus-tmp-level))
3891                  gnus-tmp-prev-subject
3892                  (not (inline
3893                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3894             (setq new-roots (nconc new-roots (list (car thread)))
3895                   thread-end t
3896                   gnus-tmp-header nil))
3897            ;; If the article lies outside the current limit,
3898            ;; then we do not display it.
3899            ((not (memq number gnus-newsgroup-limit))
3900             (setq gnus-tmp-gathered
3901                   (nconc (mapcar
3902                           (lambda (h) (mail-header-number (car h)))
3903                           (cdar thread))
3904                          gnus-tmp-gathered))
3905             (setq gnus-tmp-new-adopts (if (cdar thread)
3906                                           (append gnus-tmp-new-adopts
3907                                                   (cdar thread))
3908                                         gnus-tmp-new-adopts)
3909                   thread-end t
3910                   gnus-tmp-header nil)
3911             (when (zerop gnus-tmp-level)
3912               (setq gnus-tmp-root-expunged t)))
3913            ;; Perhaps this article is to be marked as read?
3914            ((and gnus-summary-mark-below
3915                  (< (or (cdr (assq number gnus-newsgroup-scored))
3916                         default-score)
3917                     gnus-summary-mark-below)
3918                  ;; Don't touch sparse articles.
3919                  (not (gnus-summary-article-sparse-p number))
3920                  (not (gnus-summary-article-ancient-p number)))
3921             (setq gnus-newsgroup-unreads
3922                   (delq number gnus-newsgroup-unreads))
3923             (if gnus-newsgroup-auto-expire
3924                 (push number gnus-newsgroup-expirable)
3925               (push (cons number gnus-low-score-mark)
3926                     gnus-newsgroup-reads))))
3927
3928           (when gnus-tmp-header
3929             ;; We may have an old dummy line to output before this
3930             ;; article.
3931             (when (and gnus-tmp-dummy-line
3932                        (gnus-subject-equal
3933                         gnus-tmp-dummy-line
3934                         (mail-header-subject gnus-tmp-header)))
3935               (gnus-summary-insert-dummy-line
3936                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3937               (setq gnus-tmp-dummy-line nil))
3938
3939             ;; Compute the mark.
3940             (setq gnus-tmp-unread (gnus-article-mark number))
3941
3942             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3943                                   gnus-tmp-header gnus-tmp-level)
3944                   gnus-newsgroup-data)
3945
3946             ;; Actually insert the line.
3947             (setq
3948              gnus-tmp-subject-or-nil
3949              (cond
3950               ((and gnus-thread-ignore-subject
3951                     gnus-tmp-prev-subject
3952                     (not (inline (gnus-subject-equal
3953                                   gnus-tmp-prev-subject subject))))
3954                subject)
3955               ((zerop gnus-tmp-level)
3956                (if (and (eq gnus-summary-make-false-root 'empty)
3957                         (memq number gnus-tmp-gathered)
3958                         gnus-tmp-prev-subject
3959                         (inline (gnus-subject-equal
3960                                  gnus-tmp-prev-subject subject)))
3961                    gnus-summary-same-subject
3962                  subject))
3963               (t gnus-summary-same-subject)))
3964             (if (and (eq gnus-summary-make-false-root 'adopt)
3965                      (= gnus-tmp-level 1)
3966                      (memq number gnus-tmp-gathered))
3967                 (setq gnus-tmp-opening-bracket ?\<
3968                       gnus-tmp-closing-bracket ?\>)
3969               (setq gnus-tmp-opening-bracket ?\[
3970                     gnus-tmp-closing-bracket ?\]))
3971             (setq
3972              gnus-tmp-indentation
3973              (aref gnus-thread-indent-array gnus-tmp-level)
3974              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3975              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3976                                 gnus-summary-default-score 0)
3977              gnus-tmp-score-char
3978              (if (or (null gnus-summary-default-score)
3979                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3980                          gnus-summary-zcore-fuzz))
3981                  ?  ;Whitespace
3982                (if (< gnus-tmp-score gnus-summary-default-score)
3983                    gnus-score-below-mark gnus-score-over-mark))
3984              gnus-tmp-replied
3985              (cond ((memq number gnus-newsgroup-processable)
3986                     gnus-process-mark)
3987                    ((memq number gnus-newsgroup-cached)
3988                     gnus-cached-mark)
3989                    ((memq number gnus-newsgroup-replied)
3990                     gnus-replied-mark)
3991                    ((memq number gnus-newsgroup-saved)
3992                     gnus-saved-mark)
3993                    (t gnus-unread-mark))
3994              gnus-tmp-from (mail-header-from gnus-tmp-header)
3995              gnus-tmp-name
3996              (cond
3997               ((string-match "<[^>]+> *$" gnus-tmp-from)
3998                (setq beg-match (match-beginning 0))
3999                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4000                         (substring gnus-tmp-from (1+ (match-beginning 0))
4001                                    (1- (match-end 0))))
4002                    (substring gnus-tmp-from 0 beg-match)))
4003               ((string-match "(.+)" gnus-tmp-from)
4004                (substring gnus-tmp-from
4005                           (1+ (match-beginning 0)) (1- (match-end 0))))
4006               (t gnus-tmp-from)))
4007             (when (string= gnus-tmp-name "")
4008               (setq gnus-tmp-name gnus-tmp-from))
4009             (unless (numberp gnus-tmp-lines)
4010               (setq gnus-tmp-lines 0))
4011             (gnus-put-text-property
4012              (point)
4013              (progn (eval gnus-summary-line-format-spec) (point))
4014              'gnus-number number)
4015             (when gnus-visual-p
4016               (forward-line -1)
4017               (gnus-run-hooks 'gnus-summary-update-hook)
4018               (forward-line 1))
4019
4020             (setq gnus-tmp-prev-subject subject)))
4021
4022         (when (nth 1 thread)
4023           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4024         (incf gnus-tmp-level)
4025         (setq threads (if thread-end nil (cdar thread)))
4026         (unless threads
4027           (setq gnus-tmp-level 0)))))
4028   (gnus-message 7 "Generating summary...done"))
4029
4030 (defun gnus-summary-prepare-unthreaded (headers)
4031   "Generate an unthreaded summary buffer based on HEADERS."
4032   (let (header number mark)
4033
4034     (beginning-of-line)
4035
4036     (while headers
4037       ;; We may have to root out some bad articles...
4038       (when (memq (setq number (mail-header-number
4039                                 (setq header (pop headers))))
4040                   gnus-newsgroup-limit)
4041         ;; Mark article as read when it has a low score.
4042         (when (and gnus-summary-mark-below
4043                    (< (or (cdr (assq number gnus-newsgroup-scored))
4044                           gnus-summary-default-score 0)
4045                       gnus-summary-mark-below)
4046                    (not (gnus-summary-article-ancient-p number)))
4047           (setq gnus-newsgroup-unreads
4048                 (delq number gnus-newsgroup-unreads))
4049           (if gnus-newsgroup-auto-expire
4050               (push number gnus-newsgroup-expirable)
4051             (push (cons number gnus-low-score-mark)
4052                   gnus-newsgroup-reads)))
4053
4054         (setq mark (gnus-article-mark number))
4055         (push (gnus-data-make number mark (1+ (point)) header 0)
4056               gnus-newsgroup-data)
4057         (gnus-summary-insert-line
4058          header 0 number
4059          mark (memq number gnus-newsgroup-replied)
4060          (memq number gnus-newsgroup-expirable)
4061          (mail-header-subject header) nil
4062          (cdr (assq number gnus-newsgroup-scored))
4063          (memq number gnus-newsgroup-processable))))))
4064
4065 (defun gnus-summary-remove-list-identifiers ()
4066   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4067   (let ((regexp (if (stringp gnus-list-identifiers)
4068                     gnus-list-identifiers
4069                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4070     (dolist (header gnus-newsgroup-headers)
4071       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4072                           (mail-header-subject header))
4073         (mail-header-set-subject
4074          header (concat (substring (mail-header-subject header)
4075                                    0 (match-beginning 2))
4076                         (substring (mail-header-subject header)
4077                                    (match-end 2))))))))
4078
4079 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4080   "Select newsgroup GROUP.
4081 If READ-ALL is non-nil, all articles in the group are selected.
4082 If SELECT-ARTICLES, only select those articles from GROUP."
4083   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4084          ;;!!! Dirty hack; should be removed.
4085          (gnus-summary-ignore-duplicates
4086           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4087               t
4088             gnus-summary-ignore-duplicates))
4089          (info (nth 2 entry))
4090          articles fetched-articles cached)
4091
4092     (unless (gnus-check-server
4093              (setq gnus-current-select-method
4094                    (gnus-find-method-for-group group)))
4095       (error "Couldn't open server"))
4096
4097     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4098         (gnus-activate-group group)     ; Or we can activate it...
4099         (progn                          ; Or we bug out.
4100           (when (equal major-mode 'gnus-summary-mode)
4101             (kill-buffer (current-buffer)))
4102           (error "Couldn't request group %s: %s"
4103                  group (gnus-status-message group))))
4104
4105     (unless (gnus-request-group group t)
4106       (when (equal major-mode 'gnus-summary-mode)
4107         (kill-buffer (current-buffer)))
4108       (error "Couldn't request group %s: %s"
4109              group (gnus-status-message group)))
4110
4111     (setq gnus-newsgroup-name group)
4112     (setq gnus-newsgroup-unselected nil)
4113     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4114     (gnus-summary-setup-default-charset)
4115
4116     ;; Adjust and set lists of article marks.
4117     (when info
4118       (gnus-adjust-marked-articles info))
4119
4120     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4121     (when (gnus-virtual-group-p group)
4122       (setq cached gnus-newsgroup-cached))
4123
4124     (setq gnus-newsgroup-unreads
4125           (gnus-set-difference
4126            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4127            gnus-newsgroup-dormant))
4128
4129     (setq gnus-newsgroup-processable nil)
4130
4131     (gnus-update-read-articles group gnus-newsgroup-unreads)
4132
4133     (if (setq articles select-articles)
4134         (setq gnus-newsgroup-unselected
4135               (gnus-sorted-intersection
4136                gnus-newsgroup-unreads
4137                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4138       (setq articles (gnus-articles-to-read group read-all)))
4139
4140     (cond
4141      ((null articles)
4142       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4143       'quit)
4144      ((eq articles 0) nil)
4145      (t
4146       ;; Init the dependencies hash table.
4147       (setq gnus-newsgroup-dependencies
4148             (gnus-make-hashtable (length articles)))
4149       (gnus-set-global-variables)
4150       ;; Retrieve the headers and read them in.
4151       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4152       (setq gnus-newsgroup-headers
4153             (if (eq 'nov
4154                     (setq gnus-headers-retrieved-by
4155                           (gnus-retrieve-headers
4156                            articles gnus-newsgroup-name
4157                            ;; We might want to fetch old headers, but
4158                            ;; not if there is only 1 article.
4159                            (and (or (and
4160                                      (not (eq gnus-fetch-old-headers 'some))
4161                                      (not (numberp gnus-fetch-old-headers)))
4162                                     (> (length articles) 1))
4163                                 gnus-fetch-old-headers))))
4164                 (gnus-get-newsgroup-headers-xover
4165                  articles nil nil gnus-newsgroup-name t)
4166               (gnus-get-newsgroup-headers)))
4167       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4168
4169       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4170       (when cached
4171         (setq gnus-newsgroup-cached cached))
4172
4173       ;; Suppress duplicates?
4174       (when gnus-suppress-duplicates
4175         (gnus-dup-suppress-articles))
4176
4177       ;; Set the initial limit.
4178       (setq gnus-newsgroup-limit (copy-sequence articles))
4179       ;; Remove canceled articles from the list of unread articles.
4180       (setq gnus-newsgroup-unreads
4181             (gnus-set-sorted-intersection
4182              gnus-newsgroup-unreads
4183              (setq fetched-articles
4184                    (mapcar (lambda (headers) (mail-header-number headers))
4185                            gnus-newsgroup-headers))))
4186       ;; Removed marked articles that do not exist.
4187       (gnus-update-missing-marks
4188        (gnus-sorted-complement fetched-articles articles))
4189       ;; We might want to build some more threads first.
4190       (when (and gnus-fetch-old-headers
4191                  (eq gnus-headers-retrieved-by 'nov))
4192         (if (eq gnus-fetch-old-headers 'invisible)
4193             (gnus-build-all-threads)
4194           (gnus-build-old-threads)))
4195       ;; Let the Gnus agent mark articles as read.
4196       (when gnus-agent
4197         (gnus-agent-get-undownloaded-list))
4198       ;; Remove list identifiers from subject
4199       (when gnus-list-identifiers
4200         (gnus-summary-remove-list-identifiers))
4201       ;; Check whether auto-expire is to be done in this group.
4202       (setq gnus-newsgroup-auto-expire
4203             (gnus-group-auto-expirable-p group))
4204       ;; Set up the article buffer now, if necessary.
4205       (unless gnus-single-article-buffer
4206         (gnus-article-setup-buffer))
4207       ;; First and last article in this newsgroup.
4208       (when gnus-newsgroup-headers
4209         (setq gnus-newsgroup-begin
4210               (mail-header-number (car gnus-newsgroup-headers))
4211               gnus-newsgroup-end
4212               (mail-header-number
4213                (gnus-last-element gnus-newsgroup-headers))))
4214       ;; GROUP is successfully selected.
4215       (or gnus-newsgroup-headers t)))))
4216
4217 (defun gnus-articles-to-read (group &optional read-all)
4218   ;; Find out what articles the user wants to read.
4219   (let* ((articles
4220           ;; Select all articles if `read-all' is non-nil, or if there
4221           ;; are no unread articles.
4222           (if (or read-all
4223                   (and (zerop (length gnus-newsgroup-marked))
4224                        (zerop (length gnus-newsgroup-unreads)))
4225                   (eq (gnus-group-find-parameter group 'display)
4226                       'all))
4227               (or
4228                (gnus-uncompress-range (gnus-active group))
4229                (gnus-cache-articles-in-group group))
4230             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4231                           (copy-sequence gnus-newsgroup-unreads))
4232                   '<)))
4233          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4234          (scored (length scored-list))
4235          (number (length articles))
4236          (marked (+ (length gnus-newsgroup-marked)
4237                     (length gnus-newsgroup-dormant)))
4238          (select
4239           (cond
4240            ((numberp read-all)
4241             read-all)
4242            (t
4243             (condition-case ()
4244                 (cond
4245                  ((and (or (<= scored marked) (= scored number))
4246                        (numberp gnus-large-newsgroup)
4247                        (> number gnus-large-newsgroup))
4248                   (let ((input
4249                          (read-string
4250                           (format
4251                            "How many articles from %s (default %d): "
4252                            (gnus-limit-string gnus-newsgroup-name 35)
4253                            number))))
4254                     (if (string-match "^[ \t]*$" input) number input)))
4255                  ((and (> scored marked) (< scored number)
4256                        (> (- scored number) 20))
4257                   (let ((input
4258                          (read-string
4259                           (format "%s %s (%d scored, %d total): "
4260                                   "How many articles from"
4261                                   group scored number))))
4262                     (if (string-match "^[ \t]*$" input)
4263                         number input)))
4264                  (t number))
4265               (quit nil))))))
4266     (setq select (if (stringp select) (string-to-number select) select))
4267     (if (or (null select) (zerop select))
4268         select
4269       (if (and (not (zerop scored)) (<= (abs select) scored))
4270           (progn
4271             (setq articles (sort scored-list '<))
4272             (setq number (length articles)))
4273         (setq articles (copy-sequence articles)))
4274
4275       (when (< (abs select) number)
4276         (if (< select 0)
4277             ;; Select the N oldest articles.
4278             (setcdr (nthcdr (1- (abs select)) articles) nil)
4279           ;; Select the N most recent articles.
4280           (setq articles (nthcdr (- number select) articles))))
4281       (setq gnus-newsgroup-unselected
4282             (gnus-sorted-intersection
4283              gnus-newsgroup-unreads
4284              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4285       (when gnus-alter-articles-to-read-function
4286         (setq gnus-newsgroup-unreads
4287               (sort 
4288                (funcall gnus-alter-articles-to-read-function
4289                         gnus-newsgroup-name gnus-newsgroup-unreads)
4290                '<)))
4291       articles)))
4292
4293 (defun gnus-killed-articles (killed articles)
4294   (let (out)
4295     (while articles
4296       (when (inline (gnus-member-of-range (car articles) killed))
4297         (push (car articles) out))
4298       (setq articles (cdr articles)))
4299     out))
4300
4301 (defun gnus-uncompress-marks (marks)
4302   "Uncompress the mark ranges in MARKS."
4303   (let ((uncompressed '(score bookmark))
4304         out)
4305     (while marks
4306       (if (memq (caar marks) uncompressed)
4307           (push (car marks) out)
4308         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4309       (setq marks (cdr marks)))
4310     out))
4311
4312 (defun gnus-adjust-marked-articles (info)
4313   "Set all article lists and remove all marks that are no longer valid."
4314   (let* ((marked-lists (gnus-info-marks info))
4315          (active (gnus-active (gnus-info-group info)))
4316          (min (car active))
4317          (max (cdr active))
4318          (types gnus-article-mark-lists)
4319          (uncompressed '(score bookmark killed))
4320          marks var articles article mark)
4321
4322     (while marked-lists
4323       (setq marks (pop marked-lists))
4324       (set (setq var (intern (format "gnus-newsgroup-%s"
4325                                      (car (rassq (setq mark (car marks))
4326                                                  types)))))
4327            (if (memq (car marks) uncompressed) (cdr marks)
4328              (gnus-uncompress-range (cdr marks))))
4329
4330       (setq articles (symbol-value var))
4331
4332       ;; All articles have to be subsets of the active articles.
4333       (cond
4334        ;; Adjust "simple" lists.
4335        ((memq mark '(tick dormant expire reply save))
4336         (while articles
4337           (when (or (< (setq article (pop articles)) min) (> article max))
4338             (set var (delq article (symbol-value var))))))
4339        ;; Adjust assocs.
4340        ((memq mark uncompressed)
4341         (when (not (listp (cdr (symbol-value var))))
4342           (set var (list (symbol-value var))))
4343         (when (not (listp (cdr articles)))
4344           (setq articles (list articles)))
4345         (while articles
4346           (when (or (not (consp (setq article (pop articles))))
4347                     (< (car article) min)
4348                     (> (car article) max))
4349             (set var (delq article (symbol-value var))))))))))
4350
4351 (defun gnus-update-missing-marks (missing)
4352   "Go through the list of MISSING articles and remove them from the mark lists."
4353   (when missing
4354     (let ((types gnus-article-mark-lists)
4355           var m)
4356       ;; Go through all types.
4357       (while types
4358         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4359         (when (symbol-value var)
4360           ;; This list has articles.  So we delete all missing articles
4361           ;; from it.
4362           (setq m missing)
4363           (while m
4364             (set var (delq (pop m) (symbol-value var)))))))))
4365
4366 (defun gnus-update-marks ()
4367   "Enter the various lists of marked articles into the newsgroup info list."
4368   (let ((types gnus-article-mark-lists)
4369         (info (gnus-get-info gnus-newsgroup-name))
4370         (uncompressed '(score bookmark killed))
4371         type list newmarked symbol delta-marks)
4372     (when info
4373       ;; Add all marks lists to the list of marks lists.
4374       (while (setq type (pop types))
4375         (setq list (symbol-value
4376                     (setq symbol
4377                           (intern (format "gnus-newsgroup-%s"
4378                                           (car type))))))
4379
4380         (when list
4381           ;; Get rid of the entries of the articles that have the
4382           ;; default score.
4383           (when (and (eq (cdr type) 'score)
4384                      gnus-save-score
4385                      list)
4386             (let* ((arts list)
4387                    (prev (cons nil list))
4388                    (all prev))
4389               (while arts
4390                 (if (or (not (consp (car arts)))
4391                         (= (cdar arts) gnus-summary-default-score))
4392                     (setcdr prev (cdr arts))
4393                   (setq prev arts))
4394                 (setq arts (cdr arts)))
4395               (setq list (cdr all)))))
4396
4397         (unless (memq (cdr type) uncompressed)
4398           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4399        
4400         (when (gnus-check-backend-function
4401                'request-set-mark gnus-newsgroup-name)
4402           ;; uncompressed:s are not proper flags (they are cons cells)
4403           ;; cache is a internal gnus flag
4404           (unless (memq (cdr type) (cons 'cache uncompressed))
4405             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4406                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4407                    (add (gnus-remove-from-range
4408                          (gnus-copy-sequence list) old)))
4409               (when add
4410                 (push (list add 'add (list (cdr type))) delta-marks))
4411               (when del
4412                 (push (list del 'del (list (cdr type))) delta-marks)))))
4413           
4414         (when list
4415           (push (cons (cdr type) list) newmarked)))
4416
4417       (when delta-marks
4418         (unless (gnus-check-group gnus-newsgroup-name)
4419           (error "Can't open server for %s" gnus-newsgroup-name))
4420         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4421           
4422       ;; Enter these new marks into the info of the group.
4423       (if (nthcdr 3 info)
4424           (setcar (nthcdr 3 info) newmarked)
4425         ;; Add the marks lists to the end of the info.
4426         (when newmarked
4427           (setcdr (nthcdr 2 info) (list newmarked))))
4428
4429       ;; Cut off the end of the info if there's nothing else there.
4430       (let ((i 5))
4431         (while (and (> i 2)
4432                     (not (nth i info)))
4433           (when (nthcdr (decf i) info)
4434             (setcdr (nthcdr i info) nil)))))))
4435
4436 (defun gnus-set-mode-line (where)
4437   "This function sets the mode line of the article or summary buffers.
4438 If WHERE is `summary', the summary mode line format will be used."
4439   ;; Is this mode line one we keep updated?
4440   (when (and (memq where gnus-updated-mode-lines)
4441              (symbol-value
4442               (intern (format "gnus-%s-mode-line-format-spec" where))))
4443     (let (mode-string)
4444       (save-excursion
4445         ;; We evaluate this in the summary buffer since these
4446         ;; variables are buffer-local to that buffer.
4447         (set-buffer gnus-summary-buffer)
4448         ;; We bind all these variables that are used in the `eval' form
4449         ;; below.
4450         (let* ((mformat (symbol-value
4451                          (intern
4452                           (format "gnus-%s-mode-line-format-spec" where))))
4453                (gnus-tmp-group-name gnus-newsgroup-name)
4454                (gnus-tmp-article-number (or gnus-current-article 0))
4455                (gnus-tmp-unread gnus-newsgroup-unreads)
4456                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4457                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4458                (gnus-tmp-unread-and-unselected
4459                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4460                             (zerop gnus-tmp-unselected))
4461                        "")
4462                       ((zerop gnus-tmp-unselected)
4463                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4464                       (t (format "{%d(+%d) more}"
4465                                  gnus-tmp-unread-and-unticked
4466                                  gnus-tmp-unselected))))
4467                (gnus-tmp-subject
4468                 (if (and gnus-current-headers
4469                          (vectorp gnus-current-headers))
4470                     (gnus-mode-string-quote
4471                      (mail-header-subject gnus-current-headers))
4472                   ""))
4473                bufname-length max-len
4474                gnus-tmp-header);; passed as argument to any user-format-funcs
4475           (setq mode-string (eval mformat))
4476           (setq bufname-length (if (string-match "%b" mode-string)
4477                                    (- (length
4478                                        (buffer-name
4479                                         (if (eq where 'summary)
4480                                             nil
4481                                           (get-buffer gnus-article-buffer))))
4482                                       2)
4483                                  0))
4484           (setq max-len (max 4 (if gnus-mode-non-string-length
4485                                    (- (window-width)
4486                                       gnus-mode-non-string-length
4487                                       bufname-length)
4488                                  (length mode-string))))
4489           ;; We might have to chop a bit of the string off...
4490           (when (> (length mode-string) max-len)
4491             (setq mode-string
4492                   (concat (truncate-string-to-width mode-string (- max-len 3))
4493                           "...")))
4494           ;; Pad the mode string a bit.
4495           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4496       ;; Update the mode line.
4497       (setq mode-line-buffer-identification
4498             (gnus-mode-line-buffer-identification (list mode-string)))
4499       (set-buffer-modified-p t))))
4500
4501 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4502   "Go through the HEADERS list and add all Xrefs to a hash table.
4503 The resulting hash table is returned, or nil if no Xrefs were found."
4504   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4505          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4506          (xref-hashtb (gnus-make-hashtable))
4507          start group entry number xrefs header)
4508     (while headers
4509       (setq header (pop headers))
4510       (when (and (setq xrefs (mail-header-xref header))
4511                  (not (memq (setq number (mail-header-number header))
4512                             unreads)))
4513         (setq start 0)
4514         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4515           (setq start (match-end 0))
4516           (setq group (if prefix
4517                           (concat prefix (substring xrefs (match-beginning 1)
4518                                                     (match-end 1)))
4519                         (substring xrefs (match-beginning 1) (match-end 1))))
4520           (setq number
4521                 (string-to-int (substring xrefs (match-beginning 2)
4522                                           (match-end 2))))
4523           (if (setq entry (gnus-gethash group xref-hashtb))
4524               (setcdr entry (cons number (cdr entry)))
4525             (gnus-sethash group (cons number nil) xref-hashtb)))))
4526     (and start xref-hashtb)))
4527
4528 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4529   "Look through all the headers and mark the Xrefs as read."
4530   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4531         name entry info xref-hashtb idlist method nth4)
4532     (save-excursion
4533       (set-buffer gnus-group-buffer)
4534       (when (setq xref-hashtb
4535                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4536         (mapatoms
4537          (lambda (group)
4538            (unless (string= from-newsgroup (setq name (symbol-name group)))
4539              (setq idlist (symbol-value group))
4540              ;; Dead groups are not updated.
4541              (and (prog1
4542                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4543                             info (nth 2 entry))
4544                     (when (stringp (setq nth4 (gnus-info-method info)))
4545                       (setq nth4 (gnus-server-to-method nth4))))
4546                   ;; Only do the xrefs if the group has the same
4547                   ;; select method as the group we have just read.
4548                   (or (gnus-methods-equal-p
4549                        nth4 (gnus-find-method-for-group from-newsgroup))
4550                       virtual
4551                       (equal nth4 (setq method (gnus-find-method-for-group
4552                                                 from-newsgroup)))
4553                       (and (equal (car nth4) (car method))
4554                            (equal (nth 1 nth4) (nth 1 method))))
4555                   gnus-use-cross-reference
4556                   (or (not (eq gnus-use-cross-reference t))
4557                       virtual
4558                       ;; Only do cross-references on subscribed
4559                       ;; groups, if that is what is wanted.
4560                       (<= (gnus-info-level info) gnus-level-subscribed))
4561                   (gnus-group-make-articles-read name idlist))))
4562          xref-hashtb)))))
4563
4564 (defun gnus-compute-read-articles (group articles)
4565   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4566          (info (nth 2 entry))
4567          (active (gnus-active group))
4568          ninfo)
4569     (when entry
4570       ;; First peel off all invalid article numbers.
4571       (when active
4572         (let ((ids articles)
4573               id first)
4574           (while (setq id (pop ids))
4575             (when (and first (> id (cdr active)))
4576               ;; We'll end up in this situation in one particular
4577               ;; obscure situation.  If you re-scan a group and get
4578               ;; a new article that is cross-posted to a different
4579               ;; group that has not been re-scanned, you might get
4580               ;; crossposted article that has a higher number than
4581               ;; Gnus believes possible.  So we re-activate this
4582               ;; group as well.  This might mean doing the
4583               ;; crossposting thingy will *increase* the number
4584               ;; of articles in some groups.  Tsk, tsk.
4585               (setq active (or (gnus-activate-group group) active)))
4586             (when (or (> id (cdr active))
4587                       (< id (car active)))
4588               (setq articles (delq id articles))))))
4589       ;; If the read list is nil, we init it.
4590       (if (and active
4591                (null (gnus-info-read info))
4592                (> (car active) 1))
4593           (setq ninfo (cons 1 (1- (car active))))
4594         (setq ninfo (gnus-info-read info)))
4595       ;; Then we add the read articles to the range.
4596       (gnus-add-to-range
4597        ninfo (setq articles (sort articles '<))))))
4598
4599 (defun gnus-group-make-articles-read (group articles)
4600   "Update the info of GROUP to say that ARTICLES are read."
4601   (let* ((num 0)
4602          (entry (gnus-gethash group gnus-newsrc-hashtb))
4603          (info (nth 2 entry))
4604          (active (gnus-active group))
4605          range)
4606     (when entry
4607       (setq range (gnus-compute-read-articles group articles))
4608       (save-excursion
4609         (set-buffer gnus-group-buffer)
4610         (gnus-undo-register
4611           `(progn
4612              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4613              (gnus-info-set-read ',info ',(gnus-info-read info))
4614              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4615              (gnus-group-update-group ,group t))))
4616       ;; Add the read articles to the range.
4617       (gnus-info-set-read info range)
4618       ;; Then we have to re-compute how many unread
4619       ;; articles there are in this group.
4620       (when active
4621         (cond
4622          ((not range)
4623           (setq num (- (1+ (cdr active)) (car active))))
4624          ((not (listp (cdr range)))
4625           (setq num (- (cdr active) (- (1+ (cdr range))
4626                                        (car range)))))
4627          (t
4628           (while range
4629             (if (numberp (car range))
4630                 (setq num (1+ num))
4631               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4632             (setq range (cdr range)))
4633           (setq num (- (cdr active) num))))
4634         ;; Update the number of unread articles.
4635         (setcar entry num)
4636         ;; Update the group buffer.
4637         (gnus-group-update-group group t)))))
4638
4639 (defvar gnus-newsgroup-none-id 0)
4640
4641 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4642   (let ((cur nntp-server-buffer)
4643         (dependencies
4644          (or dependencies
4645              (save-excursion (set-buffer gnus-summary-buffer)
4646                              gnus-newsgroup-dependencies)))
4647         headers id end ref
4648         (mail-parse-charset gnus-newsgroup-charset)
4649         (mail-parse-ignored-charsets 
4650          (save-excursion (condition-case nil
4651                              (set-buffer gnus-summary-buffer)
4652                            (error))
4653                          gnus-newsgroup-ignored-charsets)))
4654     (save-excursion
4655       (set-buffer nntp-server-buffer)
4656       ;; Translate all TAB characters into SPACE characters.
4657       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4658       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4659       (gnus-run-hooks 'gnus-parse-headers-hook)
4660       (let ((case-fold-search t)
4661             in-reply-to header p lines chars)
4662         (goto-char (point-min))
4663         ;; Search to the beginning of the next header.  Error messages
4664         ;; do not begin with 2 or 3.
4665         (while (re-search-forward "^[23][0-9]+ " nil t)
4666           (setq id nil
4667                 ref nil)
4668           ;; This implementation of this function, with nine
4669           ;; search-forwards instead of the one re-search-forward and
4670           ;; a case (which basically was the old function) is actually
4671           ;; about twice as fast, even though it looks messier.  You
4672           ;; can't have everything, I guess.  Speed and elegance
4673           ;; doesn't always go hand in hand.
4674           (setq
4675            header
4676            (vector
4677             ;; Number.
4678             (prog1
4679                 (read cur)
4680               (end-of-line)
4681               (setq p (point))
4682               (narrow-to-region (point)
4683                                 (or (and (search-forward "\n.\n" nil t)
4684                                          (- (point) 2))
4685                                     (point))))
4686             ;; Subject.
4687             (progn
4688               (goto-char p)
4689               (if (search-forward "\nsubject: " nil t)
4690                   (funcall gnus-decode-encoded-word-function
4691                            (nnheader-header-value))
4692                 "(none)"))
4693             ;; From.
4694             (progn
4695               (goto-char p)
4696               (if (search-forward "\nfrom: " nil t)
4697                   (funcall gnus-decode-encoded-word-function
4698                            (nnheader-header-value))
4699                 "(nobody)"))
4700             ;; Date.
4701             (progn
4702               (goto-char p)
4703               (if (search-forward "\ndate: " nil t)
4704                   (nnheader-header-value) ""))
4705             ;; Message-ID.
4706             (progn
4707               (goto-char p)
4708               (setq id (if (re-search-forward
4709                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4710                            ;; We do it this way to make sure the Message-ID
4711                            ;; is (somewhat) syntactically valid.
4712                            (buffer-substring (match-beginning 1)
4713                                              (match-end 1))
4714                          ;; If there was no message-id, we just fake one
4715                          ;; to make subsequent routines simpler.
4716                          (nnheader-generate-fake-message-id))))
4717             ;; References.
4718             (progn
4719               (goto-char p)
4720               (if (search-forward "\nreferences: " nil t)
4721                   (progn
4722                     (setq end (point))
4723                     (prog1
4724                         (nnheader-header-value)
4725                       (setq ref
4726                             (buffer-substring
4727                              (progn
4728                                (end-of-line)
4729                                (search-backward ">" end t)
4730                                (1+ (point)))
4731                              (progn
4732                                (search-backward "<" end t)
4733                                (point))))))
4734                 ;; Get the references from the in-reply-to header if there
4735                 ;; were no references and the in-reply-to header looks
4736                 ;; promising.
4737                 (if (and (search-forward "\nin-reply-to: " nil t)
4738                          (setq in-reply-to (nnheader-header-value))
4739                          (string-match "<[^>]+>" in-reply-to))
4740                     (let (ref2)
4741                       (setq ref (substring in-reply-to (match-beginning 0)
4742                                            (match-end 0)))
4743                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4744                         (setq ref2 (substring in-reply-to (match-beginning 0)
4745                                               (match-end 0)))
4746                         (when (> (length ref2) (length ref))
4747                           (setq ref ref2)))
4748                       ref)
4749                   (setq ref nil))))
4750             ;; Chars.
4751             (progn
4752               (goto-char p)
4753               (if (search-forward "\nchars: " nil t)
4754                   (if (numberp (setq chars (ignore-errors (read cur))))
4755                       chars 0)
4756                 0))
4757             ;; Lines.
4758             (progn
4759               (goto-char p)
4760               (if (search-forward "\nlines: " nil t)
4761                   (if (numberp (setq lines (ignore-errors (read cur))))
4762                       lines 0)
4763                 0))
4764             ;; Xref.
4765             (progn
4766               (goto-char p)
4767               (and (search-forward "\nxref: " nil t)
4768                    (nnheader-header-value)))
4769             ;; Extra.
4770             (when gnus-extra-headers
4771               (let ((extra gnus-extra-headers)
4772                     out)
4773                 (while extra
4774                   (goto-char p)
4775                   (when (search-forward
4776                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4777                     (push (cons (car extra) (nnheader-header-value))
4778                           out))
4779                   (pop extra))
4780                 out))))
4781           (when (equal id ref)
4782             (setq ref nil))
4783
4784           (when gnus-alter-header-function
4785             (funcall gnus-alter-header-function header)
4786             (setq id (mail-header-id header)
4787                   ref (gnus-parent-id (mail-header-references header))))
4788
4789           (when (setq header
4790                       (gnus-dependencies-add-header
4791                        header dependencies force-new))
4792             (push header headers))
4793           (goto-char (point-max))
4794           (widen))
4795         (nreverse headers)))))
4796
4797 ;; Goes through the xover lines and returns a list of vectors
4798 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4799                                                   force-new dependencies
4800                                                   group also-fetch-heads)
4801   "Parse the news overview data in the server buffer, and return a
4802 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4803   ;; Get the Xref when the users reads the articles since most/some
4804   ;; NNTP servers do not include Xrefs when using XOVER.
4805   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4806   (let ((mail-parse-charset gnus-newsgroup-charset)
4807         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4808         (cur nntp-server-buffer)
4809         (dependencies (or dependencies gnus-newsgroup-dependencies))
4810         number headers header)
4811     (save-excursion
4812       (set-buffer nntp-server-buffer)
4813       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4814       ;; Allow the user to mangle the headers before parsing them.
4815       (gnus-run-hooks 'gnus-parse-headers-hook)
4816       (goto-char (point-min))
4817       (while (not (eobp))
4818         (condition-case ()
4819             (while (and sequence (not (eobp)))
4820               (setq number (read cur))
4821               (while (and sequence
4822                           (< (car sequence) number))
4823                 (setq sequence (cdr sequence)))
4824               (and sequence
4825                    (eq number (car sequence))
4826                    (progn
4827                      (setq sequence (cdr sequence))
4828                      (setq header (inline
4829                                     (gnus-nov-parse-line
4830                                      number dependencies force-new))))
4831                    (push header headers))
4832               (forward-line 1))
4833           (error
4834            (gnus-error 4 "Strange nov line (%d)"
4835                        (count-lines (point-min) (point)))))
4836         (forward-line 1))
4837       ;; A common bug in inn is that if you have posted an article and
4838       ;; then retrieves the active file, it will answer correctly --
4839       ;; the new article is included.  However, a NOV entry for the
4840       ;; article may not have been generated yet, so this may fail.
4841       ;; We work around this problem by retrieving the last few
4842       ;; headers using HEAD.
4843       (if (or (not also-fetch-heads)
4844               (not sequence))
4845           ;; We (probably) got all the headers.
4846           (nreverse headers)
4847         (let ((gnus-nov-is-evil t))
4848           (nconc
4849            (nreverse headers)
4850            (when (gnus-retrieve-headers sequence group)
4851              (gnus-get-newsgroup-headers))))))))
4852
4853 (defun gnus-article-get-xrefs ()
4854   "Fill in the Xref value in `gnus-current-headers', if necessary.
4855 This is meant to be called in `gnus-article-internal-prepare-hook'."
4856   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4857                                  gnus-current-headers)))
4858     (or (not gnus-use-cross-reference)
4859         (not headers)
4860         (and (mail-header-xref headers)
4861              (not (string= (mail-header-xref headers) "")))
4862         (let ((case-fold-search t)
4863               xref)
4864           (save-restriction
4865             (nnheader-narrow-to-headers)
4866             (goto-char (point-min))
4867             (when (or (and (not (eobp))
4868                            (eq (downcase (char-after)) ?x)
4869                            (looking-at "Xref:"))
4870                       (search-forward "\nXref:" nil t))
4871               (goto-char (1+ (match-end 0)))
4872               (setq xref (buffer-substring (point)
4873                                            (progn (end-of-line) (point))))
4874               (mail-header-set-xref headers xref)))))))
4875
4876 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4877   "Find article ID and insert the summary line for that article.
4878 OLD-HEADER can either be a header or a line number to insert
4879 the subject line on."
4880   (let* ((line (and (numberp old-header) old-header))
4881          (old-header (and (vectorp old-header) old-header))
4882          (header (cond ((and old-header use-old-header)
4883                         old-header)
4884                        ((and (numberp id)
4885                              (gnus-number-to-header id))
4886                         (gnus-number-to-header id))
4887                        (t
4888                         (gnus-read-header id))))
4889          (number (and (numberp id) id))
4890          d)
4891     (when header
4892       ;; Rebuild the thread that this article is part of and go to the
4893       ;; article we have fetched.
4894       (when (and (not gnus-show-threads)
4895                  old-header)
4896         (when (and number
4897                    (setq d (gnus-data-find (mail-header-number old-header))))
4898           (goto-char (gnus-data-pos d))
4899           (gnus-data-remove
4900            number
4901            (- (gnus-point-at-bol)
4902               (prog1
4903                   (1+ (gnus-point-at-eol))
4904                 (gnus-delete-line))))))
4905       (when old-header
4906         (mail-header-set-number header (mail-header-number old-header)))
4907       (setq gnus-newsgroup-sparse
4908             (delq (setq number (mail-header-number header))
4909                   gnus-newsgroup-sparse))
4910       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4911       (push number gnus-newsgroup-limit)
4912       (gnus-rebuild-thread (mail-header-id header) line)
4913       (gnus-summary-goto-subject number nil t))
4914     (when (and (numberp number)
4915                (> number 0))
4916       ;; We have to update the boundaries even if we can't fetch the
4917       ;; article if ID is a number -- so that the next `P' or `N'
4918       ;; command will fetch the previous (or next) article even
4919       ;; if the one we tried to fetch this time has been canceled.
4920       (when (> number gnus-newsgroup-end)
4921         (setq gnus-newsgroup-end number))
4922       (when (< number gnus-newsgroup-begin)
4923         (setq gnus-newsgroup-begin number))
4924       (setq gnus-newsgroup-unselected
4925             (delq number gnus-newsgroup-unselected)))
4926     ;; Report back a success?
4927     (and header (mail-header-number header))))
4928
4929 ;;; Process/prefix in the summary buffer
4930
4931 (defun gnus-summary-work-articles (n)
4932   "Return a list of articles to be worked upon.
4933 The prefix argument, the list of process marked articles, and the
4934 current article will be taken into consideration."
4935   (save-excursion
4936     (set-buffer gnus-summary-buffer)
4937     (cond
4938      (n
4939       ;; A numerical prefix has been given.
4940       (setq n (prefix-numeric-value n))
4941       (let ((backward (< n 0))
4942             (n (abs (prefix-numeric-value n)))
4943             articles article)
4944         (save-excursion
4945           (while
4946               (and (> n 0)
4947                    (push (setq article (gnus-summary-article-number))
4948                          articles)
4949                    (if backward
4950                        (gnus-summary-find-prev nil article)
4951                      (gnus-summary-find-next nil article)))
4952             (decf n)))
4953         (nreverse articles)))
4954      ((and (gnus-region-active-p) (mark))
4955       (message "region active")
4956       ;; Work on the region between point and mark.
4957       (let ((max (max (point) (mark)))
4958             articles article)
4959         (save-excursion
4960           (goto-char (min (min (point) (mark))))
4961           (while
4962               (and
4963                (push (setq article (gnus-summary-article-number)) articles)
4964                (gnus-summary-find-next nil article)
4965                (< (point) max)))
4966           (nreverse articles))))
4967      (gnus-newsgroup-processable
4968       ;; There are process-marked articles present.
4969       ;; Save current state.
4970       (gnus-summary-save-process-mark)
4971       ;; Return the list.
4972       (reverse gnus-newsgroup-processable))
4973      (t
4974       ;; Just return the current article.
4975       (list (gnus-summary-article-number))))))
4976
4977 (defmacro gnus-summary-iterate (arg &rest forms)
4978   "Iterate over the process/prefixed articles and do FORMS.
4979 ARG is the interactive prefix given to the command.  FORMS will be
4980 executed with point over the summary line of the articles."
4981   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4982     `(let ((,articles (gnus-summary-work-articles ,arg)))
4983        (while ,articles
4984          (gnus-summary-goto-subject (car ,articles))
4985          ,@forms
4986          (pop ,articles)))))
4987
4988 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4989 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4990
4991 (defun gnus-summary-save-process-mark ()
4992   "Push the current set of process marked articles on the stack."
4993   (interactive)
4994   (push (copy-sequence gnus-newsgroup-processable)
4995         gnus-newsgroup-process-stack))
4996
4997 (defun gnus-summary-kill-process-mark ()
4998   "Push the current set of process marked articles on the stack and unmark."
4999   (interactive)
5000   (gnus-summary-save-process-mark)
5001   (gnus-summary-unmark-all-processable))
5002
5003 (defun gnus-summary-yank-process-mark ()
5004   "Pop the last process mark state off the stack and restore it."
5005   (interactive)
5006   (unless gnus-newsgroup-process-stack
5007     (error "Empty mark stack"))
5008   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5009
5010 (defun gnus-summary-process-mark-set (set)
5011   "Make SET into the current process marked articles."
5012   (gnus-summary-unmark-all-processable)
5013   (while set
5014     (gnus-summary-set-process-mark (pop set))))
5015
5016 ;;; Searching and stuff
5017
5018 (defun gnus-summary-search-group (&optional backward use-level)
5019   "Search for next unread newsgroup.
5020 If optional argument BACKWARD is non-nil, search backward instead."
5021   (save-excursion
5022     (set-buffer gnus-group-buffer)
5023     (when (gnus-group-search-forward
5024            backward nil (if use-level (gnus-group-group-level) nil))
5025       (gnus-group-group-name))))
5026
5027 (defun gnus-summary-best-group (&optional exclude-group)
5028   "Find the name of the best unread group.
5029 If EXCLUDE-GROUP, do not go to this group."
5030   (save-excursion
5031     (set-buffer gnus-group-buffer)
5032     (save-excursion
5033       (gnus-group-best-unread-group exclude-group))))
5034
5035 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5036   (if backward (gnus-summary-find-prev)
5037     (let* ((dummy (gnus-summary-article-intangible-p))
5038            (article (or article (gnus-summary-article-number)))
5039            (arts (gnus-data-find-list article))
5040            result)
5041       (when (and (not dummy)
5042                  (or (not gnus-summary-check-current)
5043                      (not unread)
5044                      (not (gnus-data-unread-p (car arts)))))
5045         (setq arts (cdr arts)))
5046       (when (setq result
5047                   (if unread
5048                       (progn
5049                         (while arts
5050                           (when (or (and undownloaded
5051                                          (eq gnus-undownloaded-mark
5052                                              (gnus-data-mark (car arts))))
5053                                     (gnus-data-unread-p (car arts)))
5054                             (setq result (car arts)
5055                                   arts nil))
5056                           (setq arts (cdr arts)))
5057                         result)
5058                     (car arts)))
5059         (goto-char (gnus-data-pos result))
5060         (gnus-data-number result)))))
5061
5062 (defun gnus-summary-find-prev (&optional unread article)
5063   (let* ((eobp (eobp))
5064          (article (or article (gnus-summary-article-number)))
5065          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5066          result)
5067     (when (and (not eobp)
5068                (or (not gnus-summary-check-current)
5069                    (not unread)
5070                    (not (gnus-data-unread-p (car arts)))))
5071       (setq arts (cdr arts)))
5072     (when (setq result
5073                 (if unread
5074                     (progn
5075                       (while arts
5076                         (when (gnus-data-unread-p (car arts))
5077                           (setq result (car arts)
5078                                 arts nil))
5079                         (setq arts (cdr arts)))
5080                       result)
5081                   (car arts)))
5082       (goto-char (gnus-data-pos result))
5083       (gnus-data-number result))))
5084
5085 (defun gnus-summary-find-subject (subject &optional unread backward article)
5086   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5087          (article (or article (gnus-summary-article-number)))
5088          (articles (gnus-data-list backward))
5089          (arts (gnus-data-find-list article articles))
5090          result)
5091     (when (or (not gnus-summary-check-current)
5092               (not unread)
5093               (not (gnus-data-unread-p (car arts))))
5094       (setq arts (cdr arts)))
5095     (while arts
5096       (and (or (not unread)
5097                (gnus-data-unread-p (car arts)))
5098            (vectorp (gnus-data-header (car arts)))
5099            (gnus-subject-equal
5100             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5101            (setq result (car arts)
5102                  arts nil))
5103       (setq arts (cdr arts)))
5104     (and result
5105          (goto-char (gnus-data-pos result))
5106          (gnus-data-number result))))
5107
5108 (defun gnus-summary-search-forward (&optional unread subject backward)
5109   "Search forward for an article.
5110 If UNREAD, look for unread articles.  If SUBJECT, look for
5111 articles with that subject.  If BACKWARD, search backward instead."
5112   (cond (subject (gnus-summary-find-subject subject unread backward))
5113         (backward (gnus-summary-find-prev unread))
5114         (t (gnus-summary-find-next unread))))
5115
5116 (defun gnus-recenter (&optional n)
5117   "Center point in window and redisplay frame.
5118 Also do horizontal recentering."
5119   (interactive "P")
5120   (when (and nil
5121              gnus-auto-center-summary
5122              (not (eq gnus-auto-center-summary 'vertical)))
5123     (gnus-horizontal-recenter))
5124   (recenter n))
5125
5126 (defun gnus-summary-recenter ()
5127   "Center point in the summary window.
5128 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5129 displayed, no centering will be performed."
5130   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5131   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5132   (interactive)
5133   (let* ((top (cond ((< (window-height) 4) 0)
5134                     ((< (window-height) 7) 1)
5135                     (t (if (numberp gnus-auto-center-summary)
5136                            gnus-auto-center-summary
5137                          2))))
5138          (height (1- (window-height)))
5139          (bottom (save-excursion (goto-char (point-max))
5140                                  (forward-line (- height))
5141                                  (point)))
5142          (window (get-buffer-window (current-buffer))))
5143     ;; The user has to want it.
5144     (when gnus-auto-center-summary
5145       (when (get-buffer-window gnus-article-buffer)
5146         ;; Only do recentering when the article buffer is displayed,
5147         ;; Set the window start to either `bottom', which is the biggest
5148         ;; possible valid number, or the second line from the top,
5149         ;; whichever is the least.
5150         (set-window-start
5151          window (min bottom (save-excursion
5152                               (forward-line (- top)) (point)))
5153          t))
5154       ;; Do horizontal recentering while we're at it.
5155       (when (and (get-buffer-window (current-buffer) t)
5156                  (not (eq gnus-auto-center-summary 'vertical)))
5157         (let ((selected (selected-window)))
5158           (select-window (get-buffer-window (current-buffer) t))
5159           (gnus-summary-position-point)
5160           (gnus-horizontal-recenter)
5161           (select-window selected))))))
5162
5163 (defun gnus-summary-jump-to-group (newsgroup)
5164   "Move point to NEWSGROUP in group mode buffer."
5165   ;; Keep update point of group mode buffer if visible.
5166   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5167       (save-window-excursion
5168         ;; Take care of tree window mode.
5169         (when (get-buffer-window gnus-group-buffer)
5170           (pop-to-buffer gnus-group-buffer))
5171         (gnus-group-jump-to-group newsgroup))
5172     (save-excursion
5173       ;; Take care of tree window mode.
5174       (if (get-buffer-window gnus-group-buffer)
5175           (pop-to-buffer gnus-group-buffer)
5176         (set-buffer gnus-group-buffer))
5177       (gnus-group-jump-to-group newsgroup))))
5178
5179 ;; This function returns a list of article numbers based on the
5180 ;; difference between the ranges of read articles in this group and
5181 ;; the range of active articles.
5182 (defun gnus-list-of-unread-articles (group)
5183   (let* ((read (gnus-info-read (gnus-get-info group)))
5184          (active (or (gnus-active group) (gnus-activate-group group)))
5185          (last (cdr active))
5186          first nlast unread)
5187     ;; If none are read, then all are unread.
5188     (if (not read)
5189         (setq first (car active))
5190       ;; If the range of read articles is a single range, then the
5191       ;; first unread article is the article after the last read
5192       ;; article.  Sounds logical, doesn't it?
5193       (if (and (not (listp (cdr read)))
5194                (or (< (car read) (car active))
5195                    (progn (setq read (list read))
5196                           nil)))
5197           (setq first (max (car active) (1+ (cdr read))))
5198         ;; `read' is a list of ranges.
5199         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5200                                   (caar read)))
5201                   1)
5202           (setq first (car active)))
5203         (while read
5204           (when first
5205             (while (< first nlast)
5206               (push first unread)
5207               (setq first (1+ first))))
5208           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5209           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5210           (setq read (cdr read)))))
5211     ;; And add the last unread articles.
5212     (while (<= first last)
5213       (push first unread)
5214       (setq first (1+ first)))
5215     ;; Return the list of unread articles.
5216     (delq 0 (nreverse unread))))
5217
5218 (defun gnus-list-of-read-articles (group)
5219   "Return a list of unread, unticked and non-dormant articles."
5220   (let* ((info (gnus-get-info group))
5221          (marked (gnus-info-marks info))
5222          (active (gnus-active group)))
5223     (and info active
5224          (gnus-set-difference
5225           (gnus-sorted-complement
5226            (gnus-uncompress-range active)
5227            (gnus-list-of-unread-articles group))
5228           (append
5229            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5230            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5231
5232 ;; Various summary commands
5233
5234 (defun gnus-summary-select-article-buffer ()
5235   "Reconfigure windows to show article buffer."
5236   (interactive)
5237   (if (not (gnus-buffer-live-p gnus-article-buffer))
5238       (error "There is no article buffer for this summary buffer")
5239     (gnus-configure-windows 'article)
5240     (select-window (get-buffer-window gnus-article-buffer))))
5241
5242 (defun gnus-summary-universal-argument (arg)
5243   "Perform any operation on all articles that are process/prefixed."
5244   (interactive "P")
5245   (let ((articles (gnus-summary-work-articles arg))
5246         func article)
5247     (if (eq
5248          (setq
5249           func
5250           (key-binding
5251            (read-key-sequence
5252             (substitute-command-keys
5253              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5254          'undefined)
5255         (gnus-error 1 "Undefined key")
5256       (save-excursion
5257         (while articles
5258           (gnus-summary-goto-subject (setq article (pop articles)))
5259           (let (gnus-newsgroup-processable)
5260             (command-execute func))
5261           (gnus-summary-remove-process-mark article)))))
5262   (gnus-summary-position-point))
5263
5264 (defun gnus-summary-toggle-truncation (&optional arg)
5265   "Toggle truncation of summary lines.
5266 With arg, turn line truncation on iff arg is positive."
5267   (interactive "P")
5268   (setq truncate-lines
5269         (if (null arg) (not truncate-lines)
5270           (> (prefix-numeric-value arg) 0)))
5271   (redraw-display))
5272
5273 (defun gnus-summary-reselect-current-group (&optional all rescan)
5274   "Exit and then reselect the current newsgroup.
5275 The prefix argument ALL means to select all articles."
5276   (interactive "P")
5277   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5278     (error "Ephemeral groups can't be reselected"))
5279   (let ((current-subject (gnus-summary-article-number))
5280         (group gnus-newsgroup-name))
5281     (setq gnus-newsgroup-begin nil)
5282     (gnus-summary-exit)
5283     ;; We have to adjust the point of group mode buffer because
5284     ;; point was moved to the next unread newsgroup by exiting.
5285     (gnus-summary-jump-to-group group)
5286     (when rescan
5287       (save-excursion
5288         (gnus-group-get-new-news-this-group 1)))
5289     (gnus-group-read-group all t)
5290     (gnus-summary-goto-subject current-subject nil t)))
5291
5292 (defun gnus-summary-rescan-group (&optional all)
5293   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5294   (interactive "P")
5295   (gnus-summary-reselect-current-group all t))
5296
5297 (defun gnus-summary-update-info (&optional non-destructive)
5298   (save-excursion
5299     (let ((group gnus-newsgroup-name))
5300       (when group
5301         (when gnus-newsgroup-kill-headers
5302           (setq gnus-newsgroup-killed
5303                 (gnus-compress-sequence
5304                  (nconc
5305                   (gnus-set-sorted-intersection
5306                    (gnus-uncompress-range gnus-newsgroup-killed)
5307                    (setq gnus-newsgroup-unselected
5308                          (sort gnus-newsgroup-unselected '<)))
5309                   (setq gnus-newsgroup-unreads
5310                         (sort gnus-newsgroup-unreads '<)))
5311                  t)))
5312         (unless (listp (cdr gnus-newsgroup-killed))
5313           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5314         (let ((headers gnus-newsgroup-headers))
5315           ;; Set the new ranges of read articles.
5316           (save-excursion
5317             (set-buffer gnus-group-buffer)
5318             (gnus-undo-force-boundary))
5319           (gnus-update-read-articles
5320            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5321           ;; Set the current article marks.
5322           (let ((gnus-newsgroup-scored
5323                  (if (and (not gnus-save-score)
5324                           (not non-destructive))
5325                      nil
5326                    gnus-newsgroup-scored)))
5327             (save-excursion
5328               (gnus-update-marks)))
5329           ;; Do the cross-ref thing.
5330           (when gnus-use-cross-reference
5331             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5332           ;; Do not switch windows but change the buffer to work.
5333           (set-buffer gnus-group-buffer)
5334           (unless (gnus-ephemeral-group-p group)
5335             (gnus-group-update-group group)))))))
5336
5337 (defun gnus-summary-save-newsrc (&optional force)
5338   "Save the current number of read/marked articles in the dribble buffer.
5339 The dribble buffer will then be saved.
5340 If FORCE (the prefix), also save the .newsrc file(s)."
5341   (interactive "P")
5342   (gnus-summary-update-info t)
5343   (if force
5344       (gnus-save-newsrc-file)
5345     (gnus-dribble-save)))
5346
5347 (defun gnus-summary-exit (&optional temporary)
5348   "Exit reading current newsgroup, and then return to group selection mode.
5349 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5350   (interactive)
5351   (gnus-set-global-variables)
5352   (when (gnus-buffer-live-p gnus-article-buffer)
5353     (save-excursion
5354       (set-buffer gnus-article-buffer)
5355       (mm-destroy-parts gnus-article-mime-handles)))
5356   (gnus-kill-save-kill-buffer)
5357   (gnus-async-halt-prefetch)
5358   (let* ((group gnus-newsgroup-name)
5359          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5360          (mode major-mode)
5361          (group-point nil)
5362          (buf (current-buffer)))
5363     (unless quit-config
5364       ;; Do adaptive scoring, and possibly save score files.
5365       (when gnus-newsgroup-adaptive
5366         (gnus-score-adaptive))
5367       (when gnus-use-scoring
5368         (gnus-score-save)))
5369     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5370     ;; If we have several article buffers, we kill them at exit.
5371     (unless gnus-single-article-buffer
5372       (gnus-kill-buffer gnus-original-article-buffer)
5373       (setq gnus-article-current nil))
5374     (when gnus-use-cache
5375       (gnus-cache-possibly-remove-articles)
5376       (gnus-cache-save-buffers))
5377     (gnus-async-prefetch-remove-group group)
5378     (when gnus-suppress-duplicates
5379       (gnus-dup-enter-articles))
5380     (when gnus-use-trees
5381       (gnus-tree-close group))
5382     (when gnus-use-cache
5383       (gnus-cache-write-active))
5384     ;; Remove entries for this group.
5385     (nnmail-purge-split-history (gnus-group-real-name group))
5386     ;; Make all changes in this group permanent.
5387     (unless quit-config
5388       (gnus-run-hooks 'gnus-exit-group-hook)
5389       (gnus-summary-update-info))
5390     (gnus-close-group group)
5391     ;; Make sure where we were, and go to next newsgroup.
5392     (set-buffer gnus-group-buffer)
5393     (unless quit-config
5394       (gnus-group-jump-to-group group))
5395     (gnus-run-hooks 'gnus-summary-exit-hook)
5396     (unless (or quit-config
5397                 ;; If this group has disappeared from the summary
5398                 ;; buffer, don't skip forwards.
5399                 (not (string= group (gnus-group-group-name))))
5400       (gnus-group-next-unread-group 1))
5401     (setq group-point (point))
5402     (if temporary
5403         nil                             ;Nothing to do.
5404       ;; If we have several article buffers, we kill them at exit.
5405       (unless gnus-single-article-buffer
5406         (gnus-kill-buffer gnus-article-buffer)
5407         (gnus-kill-buffer gnus-original-article-buffer)
5408         (setq gnus-article-current nil))
5409       (set-buffer buf)
5410       (if (not gnus-kill-summary-on-exit)
5411           (gnus-deaden-summary)
5412         ;; We set all buffer-local variables to nil.  It is unclear why
5413         ;; this is needed, but if we don't, buffer-local variables are
5414         ;; not garbage-collected, it seems.  This would the lead to en
5415         ;; ever-growing Emacs.
5416         (gnus-summary-clear-local-variables)
5417         (when (get-buffer gnus-article-buffer)
5418           (bury-buffer gnus-article-buffer))
5419         ;; We clear the global counterparts of the buffer-local
5420         ;; variables as well, just to be on the safe side.
5421         (set-buffer gnus-group-buffer)
5422         (gnus-summary-clear-local-variables)
5423         ;; Return to group mode buffer.
5424         (when (eq mode 'gnus-summary-mode)
5425           (gnus-kill-buffer buf)))
5426       (setq gnus-current-select-method gnus-select-method)
5427       (pop-to-buffer gnus-group-buffer)
5428       (if (not quit-config)
5429           (progn
5430             (goto-char group-point)
5431             (gnus-configure-windows 'group 'force))
5432         (gnus-handle-ephemeral-exit quit-config))
5433       ;; Clear the current group name.
5434       (unless quit-config
5435         (setq gnus-newsgroup-name nil)))))
5436
5437 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5438 (defun gnus-summary-exit-no-update (&optional no-questions)
5439   "Quit reading current newsgroup without updating read article info."
5440   (interactive)
5441   (let* ((group gnus-newsgroup-name)
5442          (quit-config (gnus-group-quit-config group)))
5443     (when (or no-questions
5444               gnus-expert-user
5445               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5446       (gnus-async-halt-prefetch)
5447       (mapcar 'funcall
5448               (delq 'gnus-summary-expire-articles
5449                     (copy-sequence gnus-summary-prepare-exit-hook)))
5450       (when (gnus-buffer-live-p gnus-article-buffer)
5451         (save-excursion
5452           (set-buffer gnus-article-buffer)
5453           (mm-destroy-parts gnus-article-mime-handles)))
5454       ;; If we have several article buffers, we kill them at exit.
5455       (unless gnus-single-article-buffer
5456         (gnus-kill-buffer gnus-article-buffer)
5457         (gnus-kill-buffer gnus-original-article-buffer)
5458         (setq gnus-article-current nil))
5459       (if (not gnus-kill-summary-on-exit)
5460           (gnus-deaden-summary)
5461         (gnus-close-group group)
5462         (gnus-summary-clear-local-variables)
5463         (set-buffer gnus-group-buffer)
5464         (gnus-summary-clear-local-variables)
5465         (when (get-buffer gnus-summary-buffer)
5466           (kill-buffer gnus-summary-buffer)))
5467       (unless gnus-single-article-buffer
5468         (setq gnus-article-current nil))
5469       (when gnus-use-trees
5470         (gnus-tree-close group))
5471       (gnus-async-prefetch-remove-group group)
5472       (when (get-buffer gnus-article-buffer)
5473         (bury-buffer gnus-article-buffer))
5474       ;; Return to the group buffer.
5475       (gnus-configure-windows 'group 'force)
5476       ;; Clear the current group name.
5477       (setq gnus-newsgroup-name nil)
5478       (when (equal (gnus-group-group-name) group)
5479         (gnus-group-next-unread-group 1))
5480       (when quit-config
5481         (gnus-handle-ephemeral-exit quit-config)))))
5482
5483 (defun gnus-handle-ephemeral-exit (quit-config)
5484   "Handle movement when leaving an ephemeral group.
5485 The state which existed when entering the ephemeral is reset."
5486   (if (not (buffer-name (car quit-config)))
5487       (gnus-configure-windows 'group 'force)
5488     (set-buffer (car quit-config))
5489     (cond ((eq major-mode 'gnus-summary-mode)
5490            (gnus-set-global-variables))
5491           ((eq major-mode 'gnus-article-mode)
5492            (save-excursion
5493              ;; The `gnus-summary-buffer' variable may point
5494              ;; to the old summary buffer when using a single
5495              ;; article buffer.
5496              (unless (gnus-buffer-live-p gnus-summary-buffer)
5497                (set-buffer gnus-group-buffer))
5498              (set-buffer gnus-summary-buffer)
5499              (gnus-set-global-variables))))
5500     (if (or (eq (cdr quit-config) 'article)
5501             (eq (cdr quit-config) 'pick))
5502         (progn
5503           ;; The current article may be from the ephemeral group
5504           ;; thus it is best that we reload this article
5505           (gnus-summary-show-article)
5506           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5507               (gnus-configure-windows 'pick 'force)
5508             (gnus-configure-windows (cdr quit-config) 'force)))
5509       (gnus-configure-windows (cdr quit-config) 'force))
5510     (when (eq major-mode 'gnus-summary-mode)
5511       (gnus-summary-next-subject 1 nil t)
5512       (gnus-summary-recenter)
5513       (gnus-summary-position-point))))
5514
5515 ;;; Dead summaries.
5516
5517 (defvar gnus-dead-summary-mode-map nil)
5518
5519 (unless gnus-dead-summary-mode-map
5520   (setq gnus-dead-summary-mode-map (make-keymap))
5521   (suppress-keymap gnus-dead-summary-mode-map)
5522   (substitute-key-definition
5523    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5524   (let ((keys '("\C-d" "\r" "\177" [delete])))
5525     (while keys
5526       (define-key gnus-dead-summary-mode-map
5527         (pop keys) 'gnus-summary-wake-up-the-dead))))
5528
5529 (defvar gnus-dead-summary-mode nil
5530   "Minor mode for Gnus summary buffers.")
5531
5532 (defun gnus-dead-summary-mode (&optional arg)
5533   "Minor mode for Gnus summary buffers."
5534   (interactive "P")
5535   (when (eq major-mode 'gnus-summary-mode)
5536     (make-local-variable 'gnus-dead-summary-mode)
5537     (setq gnus-dead-summary-mode
5538           (if (null arg) (not gnus-dead-summary-mode)
5539             (> (prefix-numeric-value arg) 0)))
5540     (when gnus-dead-summary-mode
5541       (gnus-add-minor-mode
5542        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5543
5544 (defun gnus-deaden-summary ()
5545   "Make the current summary buffer into a dead summary buffer."
5546   ;; Kill any previous dead summary buffer.
5547   (when (and gnus-dead-summary
5548              (buffer-name gnus-dead-summary))
5549     (save-excursion
5550       (set-buffer gnus-dead-summary)
5551       (when gnus-dead-summary-mode
5552         (kill-buffer (current-buffer)))))
5553   ;; Make this the current dead summary.
5554   (setq gnus-dead-summary (current-buffer))
5555   (gnus-dead-summary-mode 1)
5556   (let ((name (buffer-name)))
5557     (when (string-match "Summary" name)
5558       (rename-buffer
5559        (concat (substring name 0 (match-beginning 0)) "Dead "
5560                (substring name (match-beginning 0)))
5561        t)
5562       (bury-buffer))))
5563
5564 (defun gnus-kill-or-deaden-summary (buffer)
5565   "Kill or deaden the summary BUFFER."
5566   (save-excursion
5567     (when (and (buffer-name buffer)
5568                (not gnus-single-article-buffer))
5569       (save-excursion
5570         (set-buffer buffer)
5571         (gnus-kill-buffer gnus-article-buffer)
5572         (gnus-kill-buffer gnus-original-article-buffer)))
5573     (cond (gnus-kill-summary-on-exit
5574            (when (and gnus-use-trees
5575                       (gnus-buffer-exists-p buffer))
5576              (save-excursion
5577                (set-buffer buffer)
5578                (gnus-tree-close gnus-newsgroup-name)))
5579            (gnus-kill-buffer buffer))
5580           ((gnus-buffer-exists-p buffer)
5581            (save-excursion
5582              (set-buffer buffer)
5583              (gnus-deaden-summary))))))
5584
5585 (defun gnus-summary-wake-up-the-dead (&rest args)
5586   "Wake up the dead summary buffer."
5587   (interactive)
5588   (gnus-dead-summary-mode -1)
5589   (let ((name (buffer-name)))
5590     (when (string-match "Dead " name)
5591       (rename-buffer
5592        (concat (substring name 0 (match-beginning 0))
5593                (substring name (match-end 0)))
5594        t)))
5595   (gnus-message 3 "This dead summary is now alive again"))
5596
5597 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5598 (defun gnus-summary-fetch-faq (&optional faq-dir)
5599   "Fetch the FAQ for the current group.
5600 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5601 in."
5602   (interactive
5603    (list
5604     (when current-prefix-arg
5605       (completing-read
5606        "Faq dir: " (and (listp gnus-group-faq-directory)
5607                         (mapcar (lambda (file) (list file))
5608                                 gnus-group-faq-directory))))))
5609   (let (gnus-faq-buffer)
5610     (when (setq gnus-faq-buffer
5611                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5612       (gnus-configure-windows 'summary-faq))))
5613
5614 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5615 (defun gnus-summary-describe-group (&optional force)
5616   "Describe the current newsgroup."
5617   (interactive "P")
5618   (gnus-group-describe-group force gnus-newsgroup-name))
5619
5620 (defun gnus-summary-describe-briefly ()
5621   "Describe summary mode commands briefly."
5622   (interactive)
5623   (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")))
5624
5625 ;; Walking around group mode buffer from summary mode.
5626
5627 (defun gnus-summary-next-group (&optional no-article target-group backward)
5628   "Exit current newsgroup and then select next unread newsgroup.
5629 If prefix argument NO-ARTICLE is non-nil, no article is selected
5630 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5631 previous group instead."
5632   (interactive "P")
5633   ;; Stop pre-fetching.
5634   (gnus-async-halt-prefetch)
5635   (let ((current-group gnus-newsgroup-name)
5636         (current-buffer (current-buffer))
5637         entered)
5638     ;; First we semi-exit this group to update Xrefs and all variables.
5639     ;; We can't do a real exit, because the window conf must remain
5640     ;; the same in case the user is prompted for info, and we don't
5641     ;; want the window conf to change before that...
5642     (gnus-summary-exit t)
5643     (while (not entered)
5644       ;; Then we find what group we are supposed to enter.
5645       (set-buffer gnus-group-buffer)
5646       (gnus-group-jump-to-group current-group)
5647       (setq target-group
5648             (or target-group
5649                 (if (eq gnus-keep-same-level 'best)
5650                     (gnus-summary-best-group gnus-newsgroup-name)
5651                   (gnus-summary-search-group backward gnus-keep-same-level))))
5652       (if (not target-group)
5653           ;; There are no further groups, so we return to the group
5654           ;; buffer.
5655           (progn
5656             (gnus-message 5 "Returning to the group buffer")
5657             (setq entered t)
5658             (when (gnus-buffer-live-p current-buffer)
5659               (set-buffer current-buffer)
5660               (gnus-summary-exit))
5661             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5662         ;; We try to enter the target group.
5663         (gnus-group-jump-to-group target-group)
5664         (let ((unreads (gnus-group-group-unread)))
5665           (if (and (or (eq t unreads)
5666                        (and unreads (not (zerop unreads))))
5667                    (gnus-summary-read-group
5668                     target-group nil no-article
5669                     (and (buffer-name current-buffer) current-buffer)
5670                     nil backward))
5671               (setq entered t)
5672             (setq current-group target-group
5673                   target-group nil)))))))
5674
5675 (defun gnus-summary-prev-group (&optional no-article)
5676   "Exit current newsgroup and then select previous unread newsgroup.
5677 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5678   (interactive "P")
5679   (gnus-summary-next-group no-article nil t))
5680
5681 ;; Walking around summary lines.
5682
5683 (defun gnus-summary-first-subject (&optional unread undownloaded)
5684   "Go to the first unread subject.
5685 If UNREAD is non-nil, go to the first unread article.
5686 Returns the article selected or nil if there are no unread articles."
5687   (interactive "P")
5688   (prog1
5689       (cond
5690        ;; Empty summary.
5691        ((null gnus-newsgroup-data)
5692         (gnus-message 3 "No articles in the group")
5693         nil)
5694        ;; Pick the first article.
5695        ((not unread)
5696         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5697         (gnus-data-number (car gnus-newsgroup-data)))
5698        ;; No unread articles.
5699        ((null gnus-newsgroup-unreads)
5700         (gnus-message 3 "No more unread articles")
5701         nil)
5702        ;; Find the first unread article.
5703        (t
5704         (let ((data gnus-newsgroup-data))
5705           (while (and data
5706                       (and (not (and undownloaded
5707                                      (eq gnus-undownloaded-mark
5708                                          (gnus-data-mark (car data)))))
5709                            (not (gnus-data-unread-p (car data)))))
5710             (setq data (cdr data)))
5711           (when data
5712             (goto-char (gnus-data-pos (car data)))
5713             (gnus-data-number (car data))))))
5714     (gnus-summary-position-point)))
5715
5716 (defun gnus-summary-next-subject (n &optional unread dont-display)
5717   "Go to next N'th summary line.
5718 If N is negative, go to the previous N'th subject line.
5719 If UNREAD is non-nil, only unread articles are selected.
5720 The difference between N and the actual number of steps taken is
5721 returned."
5722   (interactive "p")
5723   (let ((backward (< n 0))
5724         (n (abs n)))
5725     (while (and (> n 0)
5726                 (if backward
5727                     (gnus-summary-find-prev unread)
5728                   (gnus-summary-find-next unread)))
5729       (unless (zerop (setq n (1- n)))
5730         (gnus-summary-show-thread)))
5731     (when (/= 0 n)
5732       (gnus-message 7 "No more%s articles"
5733                     (if unread " unread" "")))
5734     (unless dont-display
5735       (gnus-summary-recenter)
5736       (gnus-summary-position-point))
5737     n))
5738
5739 (defun gnus-summary-next-unread-subject (n)
5740   "Go to next N'th unread summary line."
5741   (interactive "p")
5742   (gnus-summary-next-subject n t))
5743
5744 (defun gnus-summary-prev-subject (n &optional unread)
5745   "Go to previous N'th summary line.
5746 If optional argument UNREAD is non-nil, only unread article is selected."
5747   (interactive "p")
5748   (gnus-summary-next-subject (- n) unread))
5749
5750 (defun gnus-summary-prev-unread-subject (n)
5751   "Go to previous N'th unread summary line."
5752   (interactive "p")
5753   (gnus-summary-next-subject (- n) t))
5754
5755 (defun gnus-summary-goto-subject (article &optional force silent)
5756   "Go the subject line of ARTICLE.
5757 If FORCE, also allow jumping to articles not currently shown."
5758   (interactive "nArticle number: ")
5759   (let ((b (point))
5760         (data (gnus-data-find article)))
5761     ;; We read in the article if we have to.
5762     (and (not data)
5763          force
5764          (gnus-summary-insert-subject
5765           article
5766           (if (or (numberp force) (vectorp force)) force)
5767           t)
5768          (setq data (gnus-data-find article)))
5769     (goto-char b)
5770     (if (not data)
5771         (progn
5772           (unless silent
5773             (gnus-message 3 "Can't find article %d" article))
5774           nil)
5775       (goto-char (gnus-data-pos data))
5776       (gnus-summary-position-point)
5777       article)))
5778
5779 ;; Walking around summary lines with displaying articles.
5780
5781 (defun gnus-summary-expand-window (&optional arg)
5782   "Make the summary buffer take up the entire Emacs frame.
5783 Given a prefix, will force an `article' buffer configuration."
5784   (interactive "P")
5785   (if arg
5786       (gnus-configure-windows 'article 'force)
5787     (gnus-configure-windows 'summary 'force)))
5788
5789 (defun gnus-summary-display-article (article &optional all-header)
5790   "Display ARTICLE in article buffer."
5791   (gnus-set-global-variables)
5792   (if (null article)
5793       nil
5794     (prog1
5795         (if gnus-summary-display-article-function
5796             (funcall gnus-summary-display-article-function article all-header)
5797           (gnus-article-prepare article all-header))
5798       (gnus-run-hooks 'gnus-select-article-hook)
5799       (when (and gnus-current-article
5800                  (not (zerop gnus-current-article)))
5801         (gnus-summary-goto-subject gnus-current-article))
5802       (gnus-summary-recenter)
5803       (when (and gnus-use-trees gnus-show-threads)
5804         (gnus-possibly-generate-tree article)
5805         (gnus-highlight-selected-tree article))
5806       ;; Successfully display article.
5807       (gnus-article-set-window-start
5808        (cdr (assq article gnus-newsgroup-bookmarks))))))
5809
5810 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5811   "Select the current article.
5812 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5813 non-nil, the article will be re-fetched even if it already present in
5814 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5815 be displayed."
5816   ;; Make sure we are in the summary buffer to work around bbdb bug.
5817   (unless (eq major-mode 'gnus-summary-mode)
5818     (set-buffer gnus-summary-buffer))
5819   (let ((article (or article (gnus-summary-article-number)))
5820         (all-headers (not (not all-headers))) ;Must be T or NIL.
5821         gnus-summary-display-article-function)
5822     (and (not pseudo)
5823          (gnus-summary-article-pseudo-p article)
5824          (error "This is a pseudo-article"))
5825     (save-excursion
5826       (set-buffer gnus-summary-buffer)
5827       (if (or (and gnus-single-article-buffer
5828                    (or (null gnus-current-article)
5829                        (null gnus-article-current)
5830                        (null (get-buffer gnus-article-buffer))
5831                        (not (eq article (cdr gnus-article-current)))
5832                        (not (equal (car gnus-article-current)
5833                                    gnus-newsgroup-name))))
5834               (and (not gnus-single-article-buffer)
5835                    (or (null gnus-current-article)
5836                        (not (eq gnus-current-article article))))
5837               force)
5838           ;; The requested article is different from the current article.
5839           (progn
5840             (gnus-summary-display-article article all-headers)
5841             (when (or all-headers gnus-show-all-headers)
5842               (gnus-article-show-all-headers))
5843             (gnus-article-set-window-start
5844              (cdr (assq article gnus-newsgroup-bookmarks)))
5845             article)
5846         (when (or all-headers gnus-show-all-headers)
5847           (gnus-article-show-all-headers))
5848         'old))))
5849
5850 (defun gnus-summary-set-current-mark (&optional current-mark)
5851   "Obsolete function."
5852   nil)
5853
5854 (defun gnus-summary-next-article (&optional unread subject backward push)
5855   "Select the next article.
5856 If UNREAD, only unread articles are selected.
5857 If SUBJECT, only articles with SUBJECT are selected.
5858 If BACKWARD, the previous article is selected instead of the next."
5859   (interactive "P")
5860   (cond
5861    ;; Is there such an article?
5862    ((and (gnus-summary-search-forward unread subject backward)
5863          (or (gnus-summary-display-article (gnus-summary-article-number))
5864              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5865     (gnus-summary-position-point))
5866    ;; If not, we try the first unread, if that is wanted.
5867    ((and subject
5868          gnus-auto-select-same
5869          (gnus-summary-first-unread-article))
5870     (gnus-summary-position-point)
5871     (gnus-message 6 "Wrapped"))
5872    ;; Try to get next/previous article not displayed in this group.
5873    ((and gnus-auto-extend-newsgroup
5874          (not unread) (not subject))
5875     (gnus-summary-goto-article
5876      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5877      nil (count-lines (point-min) (point))))
5878    ;; Go to next/previous group.
5879    (t
5880     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5881       (gnus-summary-jump-to-group gnus-newsgroup-name))
5882     (let ((cmd last-command-char)
5883           (point
5884            (save-excursion
5885              (set-buffer gnus-group-buffer)
5886              (point)))
5887           (group
5888            (if (eq gnus-keep-same-level 'best)
5889                (gnus-summary-best-group gnus-newsgroup-name)
5890              (gnus-summary-search-group backward gnus-keep-same-level))))
5891       ;; For some reason, the group window gets selected.  We change
5892       ;; it back.
5893       (select-window (get-buffer-window (current-buffer)))
5894       ;; Select next unread newsgroup automagically.
5895       (cond
5896        ((or (not gnus-auto-select-next)
5897             (not cmd))
5898         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5899        ((or (eq gnus-auto-select-next 'quietly)
5900             (and (eq gnus-auto-select-next 'slightly-quietly)
5901                  push)
5902             (and (eq gnus-auto-select-next 'almost-quietly)
5903                  (gnus-summary-last-article-p)))
5904         ;; Select quietly.
5905         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5906             (gnus-summary-exit)
5907           (gnus-message 7 "No more%s articles (%s)..."
5908                         (if unread " unread" "")
5909                         (if group (concat "selecting " group)
5910                           "exiting"))
5911           (gnus-summary-next-group nil group backward)))
5912        (t
5913         (when (gnus-key-press-event-p last-input-event)
5914           (gnus-summary-walk-group-buffer
5915            gnus-newsgroup-name cmd unread backward point))))))))
5916
5917 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5918   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5919                       (?\C-p (gnus-group-prev-unread-group 1))))
5920         (cursor-in-echo-area t)
5921         keve key group ended)
5922     (save-excursion
5923       (set-buffer gnus-group-buffer)
5924       (goto-char start)
5925       (setq group
5926             (if (eq gnus-keep-same-level 'best)
5927                 (gnus-summary-best-group gnus-newsgroup-name)
5928               (gnus-summary-search-group backward gnus-keep-same-level))))
5929     (while (not ended)
5930       (gnus-message
5931        5 "No more%s articles%s" (if unread " unread" "")
5932        (if (and group
5933                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5934            (format " (Type %s for %s [%s])"
5935                    (single-key-description cmd) group
5936                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5937          (format " (Type %s to exit %s)"
5938                  (single-key-description cmd)
5939                  gnus-newsgroup-name)))
5940       ;; Confirm auto selection.
5941       (setq key (car (setq keve (gnus-read-event-char))))
5942       (setq ended t)
5943       (cond
5944        ((assq key keystrokes)
5945         (let ((obuf (current-buffer)))
5946           (switch-to-buffer gnus-group-buffer)
5947           (when group
5948             (gnus-group-jump-to-group group))
5949           (eval (cadr (assq key keystrokes)))
5950           (setq group (gnus-group-group-name))
5951           (switch-to-buffer obuf))
5952         (setq ended nil))
5953        ((equal key cmd)
5954         (if (or (not group)
5955                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5956             (gnus-summary-exit)
5957           (gnus-summary-next-group nil group backward)))
5958        (t
5959         (push (cdr keve) unread-command-events))))))
5960
5961 (defun gnus-summary-next-unread-article ()
5962   "Select unread article after current one."
5963   (interactive)
5964   (gnus-summary-next-article
5965    (or (not (eq gnus-summary-goto-unread 'never))
5966        (gnus-summary-last-article-p (gnus-summary-article-number)))
5967    (and gnus-auto-select-same
5968         (gnus-summary-article-subject))))
5969
5970 (defun gnus-summary-prev-article (&optional unread subject)
5971   "Select the article after the current one.
5972 If UNREAD is non-nil, only unread articles are selected."
5973   (interactive "P")
5974   (gnus-summary-next-article unread subject t))
5975
5976 (defun gnus-summary-prev-unread-article ()
5977   "Select unread article before current one."
5978   (interactive)
5979   (gnus-summary-prev-article
5980    (or (not (eq gnus-summary-goto-unread 'never))
5981        (gnus-summary-first-article-p (gnus-summary-article-number)))
5982    (and gnus-auto-select-same
5983         (gnus-summary-article-subject))))
5984
5985 (defun gnus-summary-next-page (&optional lines circular)
5986   "Show next page of the selected article.
5987 If at the end of the current article, select the next article.
5988 LINES says how many lines should be scrolled up.
5989
5990 If CIRCULAR is non-nil, go to the start of the article instead of
5991 selecting the next article when reaching the end of the current
5992 article."
5993   (interactive "P")
5994   (setq gnus-summary-buffer (current-buffer))
5995   (gnus-set-global-variables)
5996   (let ((article (gnus-summary-article-number))
5997         (article-window (get-buffer-window gnus-article-buffer t))
5998         endp)
5999     ;; If the buffer is empty, we have no article.
6000     (unless article
6001       (error "No article to select"))
6002     (gnus-configure-windows 'article)
6003     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6004         (if (and (eq gnus-summary-goto-unread 'never)
6005                  (not (gnus-summary-last-article-p article)))
6006             (gnus-summary-next-article)
6007           (gnus-summary-next-unread-article))
6008       (if (or (null gnus-current-article)
6009               (null gnus-article-current)
6010               (/= article (cdr gnus-article-current))
6011               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6012           ;; Selected subject is different from current article's.
6013           (gnus-summary-display-article article)
6014         (when article-window
6015           (gnus-eval-in-buffer-window gnus-article-buffer
6016             (setq endp (gnus-article-next-page lines)))
6017           (when endp
6018             (cond (circular
6019                    (gnus-summary-beginning-of-article))
6020                   (lines
6021                    (gnus-message 3 "End of message"))
6022                   ((null lines)
6023                    (if (and (eq gnus-summary-goto-unread 'never)
6024                             (not (gnus-summary-last-article-p article)))
6025                        (gnus-summary-next-article)
6026                      (gnus-summary-next-unread-article))))))))
6027     (gnus-summary-recenter)
6028     (gnus-summary-position-point)))
6029
6030 (defun gnus-summary-prev-page (&optional lines move)
6031   "Show previous page of selected article.
6032 Argument LINES specifies lines to be scrolled down.
6033 If MOVE, move to the previous unread article if point is at
6034 the beginning of the buffer."
6035   (interactive "P")
6036   (let ((article (gnus-summary-article-number))
6037         (article-window (get-buffer-window gnus-article-buffer t))
6038         endp)
6039     (gnus-configure-windows 'article)
6040     (if (or (null gnus-current-article)
6041             (null gnus-article-current)
6042             (/= article (cdr gnus-article-current))
6043             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6044         ;; Selected subject is different from current article's.
6045         (gnus-summary-display-article article)
6046       (gnus-summary-recenter)
6047       (when article-window
6048         (gnus-eval-in-buffer-window gnus-article-buffer
6049           (setq endp (gnus-article-prev-page lines)))
6050         (when (and move endp)
6051           (cond (lines
6052                  (gnus-message 3 "Beginning of message"))
6053                 ((null lines)
6054                  (if (and (eq gnus-summary-goto-unread 'never)
6055                           (not (gnus-summary-first-article-p article)))
6056                      (gnus-summary-prev-article)
6057                    (gnus-summary-prev-unread-article))))))))
6058   (gnus-summary-position-point))
6059
6060 (defun gnus-summary-prev-page-or-article (&optional lines)
6061   "Show previous page of selected article.
6062 Argument LINES specifies lines to be scrolled down.
6063 If at the beginning of the article, go to the next article."
6064   (interactive "P")
6065   (gnus-summary-prev-page lines t))
6066
6067 (defun gnus-summary-scroll-up (lines)
6068   "Scroll up (or down) one line current article.
6069 Argument LINES specifies lines to be scrolled up (or down if negative)."
6070   (interactive "p")
6071   (gnus-configure-windows 'article)
6072   (gnus-summary-show-thread)
6073   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6074     (gnus-eval-in-buffer-window gnus-article-buffer
6075       (cond ((> lines 0)
6076              (when (gnus-article-next-page lines)
6077                (gnus-message 3 "End of message")))
6078             ((< lines 0)
6079              (gnus-article-prev-page (- lines))))))
6080   (gnus-summary-recenter)
6081   (gnus-summary-position-point))
6082
6083 (defun gnus-summary-scroll-down (lines)
6084   "Scroll down (or up) one line current article.
6085 Argument LINES specifies lines to be scrolled down (or up if negative)."
6086   (interactive "p")
6087   (gnus-summary-scroll-up (- lines)))
6088
6089 (defun gnus-summary-next-same-subject ()
6090   "Select next article which has the same subject as current one."
6091   (interactive)
6092   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6093
6094 (defun gnus-summary-prev-same-subject ()
6095   "Select previous article which has the same subject as current one."
6096   (interactive)
6097   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6098
6099 (defun gnus-summary-next-unread-same-subject ()
6100   "Select next unread article which has the same subject as current one."
6101   (interactive)
6102   (gnus-summary-next-article t (gnus-summary-article-subject)))
6103
6104 (defun gnus-summary-prev-unread-same-subject ()
6105   "Select previous unread article which has the same subject as current one."
6106   (interactive)
6107   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6108
6109 (defun gnus-summary-first-unread-article ()
6110   "Select the first unread article.
6111 Return nil if there are no unread articles."
6112   (interactive)
6113   (prog1
6114       (when (gnus-summary-first-subject t)
6115         (gnus-summary-show-thread)
6116         (gnus-summary-first-subject t)
6117         (gnus-summary-display-article (gnus-summary-article-number)))
6118     (gnus-summary-position-point)))
6119
6120 (defun gnus-summary-first-unread-subject ()
6121   "Place the point on the subject line of the first unread article.
6122 Return nil if there are no unread articles."
6123   (interactive)
6124   (prog1
6125       (when (gnus-summary-first-subject t)
6126         (gnus-summary-show-thread)
6127         (gnus-summary-first-subject t))
6128     (gnus-summary-position-point)))
6129
6130 (defun gnus-summary-first-article ()
6131   "Select the first article.
6132 Return nil if there are no articles."
6133   (interactive)
6134   (prog1
6135       (when (gnus-summary-first-subject)
6136         (gnus-summary-show-thread)
6137         (gnus-summary-first-subject)
6138         (gnus-summary-display-article (gnus-summary-article-number)))
6139     (gnus-summary-position-point)))
6140
6141 (defun gnus-summary-best-unread-article ()
6142   "Select the unread article with the highest score."
6143   (interactive)
6144   (let ((best -1000000)
6145         (data gnus-newsgroup-data)
6146         article score)
6147     (while data
6148       (and (gnus-data-unread-p (car data))
6149            (> (setq score
6150                     (gnus-summary-article-score (gnus-data-number (car data))))
6151               best)
6152            (setq best score
6153                  article (gnus-data-number (car data))))
6154       (setq data (cdr data)))
6155     (prog1
6156         (if article
6157             (gnus-summary-goto-article article)
6158           (error "No unread articles"))
6159       (gnus-summary-position-point))))
6160
6161 (defun gnus-summary-last-subject ()
6162   "Go to the last displayed subject line in the group."
6163   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6164     (when article
6165       (gnus-summary-goto-subject article))))
6166
6167 (defun gnus-summary-goto-article (article &optional all-headers force)
6168   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6169 If ALL-HEADERS is non-nil, no header lines are hidden.
6170 If FORCE, go to the article even if it isn't displayed.  If FORCE
6171 is a number, it is the line the article is to be displayed on."
6172   (interactive
6173    (list
6174     (completing-read
6175      "Article number or Message-ID: "
6176      (mapcar (lambda (number) (list (int-to-string number)))
6177              gnus-newsgroup-limit))
6178     current-prefix-arg
6179     t))
6180   (prog1
6181       (if (and (stringp article)
6182                (string-match "@" article))
6183           (gnus-summary-refer-article article)
6184         (when (stringp article)
6185           (setq article (string-to-number article)))
6186         (if (gnus-summary-goto-subject article force)
6187             (gnus-summary-display-article article all-headers)
6188           (gnus-message 4 "Couldn't go to article %s" article) nil))
6189     (gnus-summary-position-point)))
6190
6191 (defun gnus-summary-goto-last-article ()
6192   "Go to the previously read article."
6193   (interactive)
6194   (prog1
6195       (when gnus-last-article
6196         (gnus-summary-goto-article gnus-last-article nil t))
6197     (gnus-summary-position-point)))
6198
6199 (defun gnus-summary-pop-article (number)
6200   "Pop one article off the history and go to the previous.
6201 NUMBER articles will be popped off."
6202   (interactive "p")
6203   (let (to)
6204     (setq gnus-newsgroup-history
6205           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6206     (if to
6207         (gnus-summary-goto-article (car to) nil t)
6208       (error "Article history empty")))
6209   (gnus-summary-position-point))
6210
6211 ;; Summary commands and functions for limiting the summary buffer.
6212
6213 (defun gnus-summary-limit-to-articles (n)
6214   "Limit the summary buffer to the next N articles.
6215 If not given a prefix, use the process marked articles instead."
6216   (interactive "P")
6217   (prog1
6218       (let ((articles (gnus-summary-work-articles n)))
6219         (setq gnus-newsgroup-processable nil)
6220         (gnus-summary-limit articles))
6221     (gnus-summary-position-point)))
6222
6223 (defun gnus-summary-pop-limit (&optional total)
6224   "Restore the previous limit.
6225 If given a prefix, remove all limits."
6226   (interactive "P")
6227   (when total
6228     (setq gnus-newsgroup-limits
6229           (list (mapcar (lambda (h) (mail-header-number h))
6230                         gnus-newsgroup-headers))))
6231   (unless gnus-newsgroup-limits
6232     (error "No limit to pop"))
6233   (prog1
6234       (gnus-summary-limit nil 'pop)
6235     (gnus-summary-position-point)))
6236
6237 (defun gnus-summary-limit-to-subject (subject &optional header)
6238   "Limit the summary buffer to articles that have subjects that match a regexp."
6239   (interactive "sLimit to subject (regexp): ")
6240   (unless header
6241     (setq header "subject"))
6242   (when (not (equal "" subject))
6243     (prog1
6244         (let ((articles (gnus-summary-find-matching
6245                          (or header "subject") subject 'all)))
6246           (unless articles
6247             (error "Found no matches for \"%s\"" subject))
6248           (gnus-summary-limit articles))
6249       (gnus-summary-position-point))))
6250
6251 (defun gnus-summary-limit-to-author (from)
6252   "Limit the summary buffer to articles that have authors that match a regexp."
6253   (interactive "sLimit to author (regexp): ")
6254   (gnus-summary-limit-to-subject from "from"))
6255
6256 (defun gnus-summary-limit-to-age (age &optional younger-p)
6257   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6258 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6259 articles that are younger than AGE days."
6260   (interactive
6261    (let ((younger current-prefix-arg)
6262          (days-got nil)
6263          days)
6264      (while (not days-got)
6265        (setq days (if younger
6266                       (read-string "Limit to articles within (in days): ")
6267                     (read-string "Limit to articles old than (in days): ")))
6268        (when (> (length days) 0)
6269          (setq days (read days)))
6270        (if (numberp days)
6271            (setq days-got t)
6272          (message "Please enter a number.")
6273          (sleep-for 1)))
6274      (list days younger)))
6275   (prog1
6276       (let ((data gnus-newsgroup-data)
6277             (cutoff (days-to-time age))
6278             articles d date is-younger)
6279         (while (setq d (pop data))
6280           (when (and (vectorp (gnus-data-header d))
6281                      (setq date (mail-header-date (gnus-data-header d))))
6282             (setq is-younger (time-less-p
6283                               (time-since (condition-case ()
6284                                               (date-to-time date)
6285                                             (error '(0 0))))
6286                               cutoff))
6287             (when (if younger-p
6288                       is-younger
6289                     (not is-younger))
6290               (push (gnus-data-number d) articles))))
6291         (gnus-summary-limit (nreverse articles)))
6292     (gnus-summary-position-point)))
6293
6294 (defun gnus-summary-limit-to-extra (header regexp)
6295   "Limit the summary buffer to articles that match an 'extra' header."
6296   (interactive
6297    (let ((header
6298           (intern
6299            (gnus-completing-read
6300             (symbol-name (car gnus-extra-headers))      
6301             "Limit extra header:"       
6302             (mapcar (lambda (x) 
6303                       (cons (symbol-name x) x))
6304                     gnus-extra-headers)
6305             nil                 
6306             t))))
6307      (list header
6308            (read-string (format "Limit to header %s (regexp): " header)))))
6309   (when (not (equal "" regexp))
6310     (prog1
6311         (let ((articles (gnus-summary-find-matching
6312                          (cons 'extra header) regexp 'all)))
6313           (unless articles
6314             (error "Found no matches for \"%s\"" regexp))
6315           (gnus-summary-limit articles))
6316       (gnus-summary-position-point))))
6317
6318 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6319 (make-obsolete
6320  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6321
6322 (defun gnus-summary-limit-to-unread (&optional all)
6323   "Limit the summary buffer to articles that are not marked as read.
6324 If ALL is non-nil, limit strictly to unread articles."
6325   (interactive "P")
6326   (if all
6327       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6328     (gnus-summary-limit-to-marks
6329      ;; Concat all the marks that say that an article is read and have
6330      ;; those removed.
6331      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6332            gnus-killed-mark gnus-kill-file-mark
6333            gnus-low-score-mark gnus-expirable-mark
6334            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6335            gnus-duplicate-mark gnus-souped-mark)
6336      'reverse)))
6337
6338 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6339 (make-obsolete 'gnus-summary-delete-marked-with
6340                'gnus-summary-limit-exlude-marks)
6341
6342 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6343   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6344 If REVERSE, limit the summary buffer to articles that are marked
6345 with MARKS.  MARKS can either be a string of marks or a list of marks.
6346 Returns how many articles were removed."
6347   (interactive "sMarks: ")
6348   (gnus-summary-limit-to-marks marks t))
6349
6350 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6351   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6352 If REVERSE (the prefix), limit the summary buffer to articles that are
6353 not marked with MARKS.  MARKS can either be a string of marks or a
6354 list of marks.
6355 Returns how many articles were removed."
6356   (interactive "sMarks: \nP")
6357   (prog1
6358       (let ((data gnus-newsgroup-data)
6359             (marks (if (listp marks) marks
6360                      (append marks nil))) ; Transform to list.
6361             articles)
6362         (while data
6363           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6364                   (memq (gnus-data-mark (car data)) marks))
6365             (push (gnus-data-number (car data)) articles))
6366           (setq data (cdr data)))
6367         (gnus-summary-limit articles))
6368     (gnus-summary-position-point)))
6369
6370 (defun gnus-summary-limit-to-score (&optional score)
6371   "Limit to articles with score at or above SCORE."
6372   (interactive "P")
6373   (setq score (if score
6374                   (prefix-numeric-value score)
6375                 (or gnus-summary-default-score 0)))
6376   (let ((data gnus-newsgroup-data)
6377         articles)
6378     (while data
6379       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6380                 score)
6381         (push (gnus-data-number (car data)) articles))
6382       (setq data (cdr data)))
6383     (prog1
6384         (gnus-summary-limit articles)
6385       (gnus-summary-position-point))))
6386
6387 (defun gnus-summary-limit-include-thread (id)
6388   "Display all the hidden articles that in the current thread."
6389   (interactive (list (mail-header-id (gnus-summary-article-header))))
6390   (let ((articles (gnus-articles-in-thread
6391                    (gnus-id-to-thread (gnus-root-id id)))))
6392     (prog1
6393         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6394       (gnus-summary-position-point))))
6395
6396 (defun gnus-summary-limit-include-dormant ()
6397   "Display all the hidden articles that are marked as dormant.
6398 Note that this command only works on a subset of the articles currently
6399 fetched for this group."
6400   (interactive)
6401   (unless gnus-newsgroup-dormant
6402     (error "There are no dormant articles in this group"))
6403   (prog1
6404       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6405     (gnus-summary-position-point)))
6406
6407 (defun gnus-summary-limit-exclude-dormant ()
6408   "Hide all dormant articles."
6409   (interactive)
6410   (prog1
6411       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6412     (gnus-summary-position-point)))
6413
6414 (defun gnus-summary-limit-exclude-childless-dormant ()
6415   "Hide all dormant articles that have no children."
6416   (interactive)
6417   (let ((data (gnus-data-list t))
6418         articles d children)
6419     ;; Find all articles that are either not dormant or have
6420     ;; children.
6421     (while (setq d (pop data))
6422       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6423                 (and (setq children
6424                            (gnus-article-children (gnus-data-number d)))
6425                      (let (found)
6426                        (while children
6427                          (when (memq (car children) articles)
6428                            (setq children nil
6429                                  found t))
6430                          (pop children))
6431                        found)))
6432         (push (gnus-data-number d) articles)))
6433     ;; Do the limiting.
6434     (prog1
6435         (gnus-summary-limit articles)
6436       (gnus-summary-position-point))))
6437
6438 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6439   "Mark all unread excluded articles as read.
6440 If ALL, mark even excluded ticked and dormants as read."
6441   (interactive "P")
6442   (let ((articles (gnus-sorted-complement
6443                    (sort
6444                     (mapcar (lambda (h) (mail-header-number h))
6445                             gnus-newsgroup-headers)
6446                     '<)
6447                    (sort gnus-newsgroup-limit '<)))
6448         article)
6449     (setq gnus-newsgroup-unreads
6450           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6451     (if all
6452         (setq gnus-newsgroup-dormant nil
6453               gnus-newsgroup-marked nil
6454               gnus-newsgroup-reads
6455               (nconc
6456                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6457                gnus-newsgroup-reads))
6458       (while (setq article (pop articles))
6459         (unless (or (memq article gnus-newsgroup-dormant)
6460                     (memq article gnus-newsgroup-marked))
6461           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6462
6463 (defun gnus-summary-limit (articles &optional pop)
6464   (if pop
6465       ;; We pop the previous limit off the stack and use that.
6466       (setq articles (car gnus-newsgroup-limits)
6467             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6468     ;; We use the new limit, so we push the old limit on the stack.
6469     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6470   ;; Set the limit.
6471   (setq gnus-newsgroup-limit articles)
6472   (let ((total (length gnus-newsgroup-data))
6473         (data (gnus-data-find-list (gnus-summary-article-number)))
6474         (gnus-summary-mark-below nil)   ; Inhibit this.
6475         found)
6476     ;; This will do all the work of generating the new summary buffer
6477     ;; according to the new limit.
6478     (gnus-summary-prepare)
6479     ;; Hide any threads, possibly.
6480     (and gnus-show-threads
6481          gnus-thread-hide-subtree
6482          (gnus-summary-hide-all-threads))
6483     ;; Try to return to the article you were at, or one in the
6484     ;; neighborhood.
6485     (when data
6486       ;; We try to find some article after the current one.
6487       (while data
6488         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6489           (setq data nil
6490                 found t))
6491         (setq data (cdr data))))
6492     (unless found
6493       ;; If there is no data, that means that we were after the last
6494       ;; article.  The same goes when we can't find any articles
6495       ;; after the current one.
6496       (goto-char (point-max))
6497       (gnus-summary-find-prev))
6498     (gnus-set-mode-line 'summary)
6499     ;; We return how many articles were removed from the summary
6500     ;; buffer as a result of the new limit.
6501     (- total (length gnus-newsgroup-data))))
6502
6503 (defsubst gnus-invisible-cut-children (threads)
6504   (let ((num 0))
6505     (while threads
6506       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6507         (incf num))
6508       (pop threads))
6509     (< num 2)))
6510
6511 (defsubst gnus-cut-thread (thread)
6512   "Go forwards in the thread until we find an article that we want to display."
6513   (when (or (eq gnus-fetch-old-headers 'some)
6514             (eq gnus-fetch-old-headers 'invisible)
6515             (numberp gnus-fetch-old-headers)
6516             (eq gnus-build-sparse-threads 'some)
6517             (eq gnus-build-sparse-threads 'more))
6518     ;; Deal with old-fetched headers and sparse threads.
6519     (while (and
6520             thread
6521             (or
6522              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6523              (gnus-summary-article-ancient-p
6524               (mail-header-number (car thread))))
6525             (if (or (<= (length (cdr thread)) 1)
6526                     (eq gnus-fetch-old-headers 'invisible))
6527                 (setq gnus-newsgroup-limit
6528                       (delq (mail-header-number (car thread))
6529                             gnus-newsgroup-limit)
6530                       thread (cadr thread))
6531               (when (gnus-invisible-cut-children (cdr thread))
6532                 (let ((th (cdr thread)))
6533                   (while th
6534                     (if (memq (mail-header-number (caar th))
6535                               gnus-newsgroup-limit)
6536                         (setq thread (car th)
6537                               th nil)
6538                       (setq th (cdr th))))))))))
6539   thread)
6540
6541 (defun gnus-cut-threads (threads)
6542   "Cut off all uninteresting articles from the beginning of threads."
6543   (when (or (eq gnus-fetch-old-headers 'some)
6544             (eq gnus-fetch-old-headers 'invisible)
6545             (numberp gnus-fetch-old-headers)
6546             (eq gnus-build-sparse-threads 'some)
6547             (eq gnus-build-sparse-threads 'more))
6548     (let ((th threads))
6549       (while th
6550         (setcar th (gnus-cut-thread (car th)))
6551         (setq th (cdr th)))))
6552   ;; Remove nixed out threads.
6553   (delq nil threads))
6554
6555 (defun gnus-summary-initial-limit (&optional show-if-empty)
6556   "Figure out what the initial limit is supposed to be on group entry.
6557 This entails weeding out unwanted dormants, low-scored articles,
6558 fetch-old-headers verbiage, and so on."
6559   ;; Most groups have nothing to remove.
6560   (if (or gnus-inhibit-limiting
6561           (and (null gnus-newsgroup-dormant)
6562                (not (eq gnus-fetch-old-headers 'some))
6563                (not (numberp gnus-fetch-old-headers))
6564                (not (eq gnus-fetch-old-headers 'invisible))
6565                (null gnus-summary-expunge-below)
6566                (not (eq gnus-build-sparse-threads 'some))
6567                (not (eq gnus-build-sparse-threads 'more))
6568                (null gnus-thread-expunge-below)
6569                (not gnus-use-nocem)))
6570       ()                                ; Do nothing.
6571     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6572     (setq gnus-newsgroup-limit nil)
6573     (mapatoms
6574      (lambda (node)
6575        (unless (car (symbol-value node))
6576          ;; These threads have no parents -- they are roots.
6577          (let ((nodes (cdr (symbol-value node)))
6578                thread)
6579            (while nodes
6580              (if (and gnus-thread-expunge-below
6581                       (< (gnus-thread-total-score (car nodes))
6582                          gnus-thread-expunge-below))
6583                  (gnus-expunge-thread (pop nodes))
6584                (setq thread (pop nodes))
6585                (gnus-summary-limit-children thread))))))
6586      gnus-newsgroup-dependencies)
6587     ;; If this limitation resulted in an empty group, we might
6588     ;; pop the previous limit and use it instead.
6589     (when (and (not gnus-newsgroup-limit)
6590                show-if-empty)
6591       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6592     gnus-newsgroup-limit))
6593
6594 (defun gnus-summary-limit-children (thread)
6595   "Return 1 if this subthread is visible and 0 if it is not."
6596   ;; First we get the number of visible children to this thread.  This
6597   ;; is done by recursing down the thread using this function, so this
6598   ;; will really go down to a leaf article first, before slowly
6599   ;; working its way up towards the root.
6600   (when thread
6601     (let ((children
6602            (if (cdr thread)
6603                (apply '+ (mapcar 'gnus-summary-limit-children
6604                                  (cdr thread)))
6605              0))
6606           (number (mail-header-number (car thread)))
6607           score)
6608       (if (and
6609            (not (memq number gnus-newsgroup-marked))
6610            (or
6611             ;; If this article is dormant and has absolutely no visible
6612             ;; children, then this article isn't visible.
6613             (and (memq number gnus-newsgroup-dormant)
6614                  (zerop children))
6615             ;; If this is "fetch-old-headered" and there is no
6616             ;; visible children, then we don't want this article.
6617             (and (or (eq gnus-fetch-old-headers 'some)
6618                      (numberp gnus-fetch-old-headers))
6619                  (gnus-summary-article-ancient-p number)
6620                  (zerop children))
6621             ;; If this is "fetch-old-headered" and `invisible', then
6622             ;; we don't want this article.
6623             (and (eq gnus-fetch-old-headers 'invisible)
6624                  (gnus-summary-article-ancient-p number))
6625             ;; If this is a sparsely inserted article with no children,
6626             ;; we don't want it.
6627             (and (eq gnus-build-sparse-threads 'some)
6628                  (gnus-summary-article-sparse-p number)
6629                  (zerop children))
6630             ;; If we use expunging, and this article is really
6631             ;; low-scored, then we don't want this article.
6632             (when (and gnus-summary-expunge-below
6633                        (< (setq score
6634                                 (or (cdr (assq number gnus-newsgroup-scored))
6635                                     gnus-summary-default-score))
6636                           gnus-summary-expunge-below))
6637               ;; We increase the expunge-tally here, but that has
6638               ;; nothing to do with the limits, really.
6639               (incf gnus-newsgroup-expunged-tally)
6640               ;; We also mark as read here, if that's wanted.
6641               (when (and gnus-summary-mark-below
6642                          (< score gnus-summary-mark-below))
6643                 (setq gnus-newsgroup-unreads
6644                       (delq number gnus-newsgroup-unreads))
6645                 (if gnus-newsgroup-auto-expire
6646                     (push number gnus-newsgroup-expirable)
6647                   (push (cons number gnus-low-score-mark)
6648                         gnus-newsgroup-reads)))
6649               t)
6650             ;; Check NoCeM things.
6651             (if (and gnus-use-nocem
6652                      (gnus-nocem-unwanted-article-p
6653                       (mail-header-id (car thread))))
6654                 (progn
6655                   (setq gnus-newsgroup-unreads
6656                         (delq number gnus-newsgroup-unreads))
6657                   t))))
6658           ;; Nope, invisible article.
6659           0
6660         ;; Ok, this article is to be visible, so we add it to the limit
6661         ;; and return 1.
6662         (push number gnus-newsgroup-limit)
6663         1))))
6664
6665 (defun gnus-expunge-thread (thread)
6666   "Mark all articles in THREAD as read."
6667   (let* ((number (mail-header-number (car thread))))
6668     (incf gnus-newsgroup-expunged-tally)
6669     ;; We also mark as read here, if that's wanted.
6670     (setq gnus-newsgroup-unreads
6671           (delq number gnus-newsgroup-unreads))
6672     (if gnus-newsgroup-auto-expire
6673         (push number gnus-newsgroup-expirable)
6674       (push (cons number gnus-low-score-mark)
6675             gnus-newsgroup-reads)))
6676   ;; Go recursively through all subthreads.
6677   (mapcar 'gnus-expunge-thread (cdr thread)))
6678
6679 ;; Summary article oriented commands
6680
6681 (defun gnus-summary-refer-parent-article (n)
6682   "Refer parent article N times.
6683 If N is negative, go to ancestor -N instead.
6684 The difference between N and the number of articles fetched is returned."
6685   (interactive "p")
6686   (let ((skip 1)
6687         error header ref)
6688     (when (not (natnump n))
6689       (setq skip (abs n)
6690             n 1))
6691     (while (and (> n 0)
6692                 (not error))
6693       (setq header (gnus-summary-article-header))
6694       (if (and (eq (mail-header-number header)
6695                    (cdr gnus-article-current))
6696                (equal gnus-newsgroup-name
6697                       (car gnus-article-current)))
6698           ;; If we try to find the parent of the currently
6699           ;; displayed article, then we take a look at the actual
6700           ;; References header, since this is slightly more
6701           ;; reliable than the References field we got from the
6702           ;; server.
6703           (save-excursion
6704             (set-buffer gnus-original-article-buffer)
6705             (nnheader-narrow-to-headers)
6706             (unless (setq ref (message-fetch-field "references"))
6707               (setq ref (message-fetch-field "in-reply-to")))
6708             (widen))
6709         (setq ref
6710               ;; It's not the current article, so we take a bet on
6711               ;; the value we got from the server.
6712               (mail-header-references header)))
6713       (if (and ref
6714                (not (equal ref "")))
6715           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6716             (gnus-message 1 "Couldn't find parent"))
6717         (gnus-message 1 "No references in article %d"
6718                       (gnus-summary-article-number))
6719         (setq error t))
6720       (decf n))
6721     (gnus-summary-position-point)
6722     n))
6723
6724 (defun gnus-summary-refer-references ()
6725   "Fetch all articles mentioned in the References header.
6726 Return the number of articles fetched."
6727   (interactive)
6728   (let ((ref (mail-header-references (gnus-summary-article-header)))
6729         (current (gnus-summary-article-number))
6730         (n 0))
6731     (if (or (not ref)
6732             (equal ref ""))
6733         (error "No References in the current article")
6734       ;; For each Message-ID in the References header...
6735       (while (string-match "<[^>]*>" ref)
6736         (incf n)
6737         ;; ... fetch that article.
6738         (gnus-summary-refer-article
6739          (prog1 (match-string 0 ref)
6740            (setq ref (substring ref (match-end 0))))))
6741       (gnus-summary-goto-subject current)
6742       (gnus-summary-position-point)
6743       n)))
6744
6745 (defun gnus-summary-refer-thread (&optional limit)
6746   "Fetch all articles in the current thread.
6747 If LIMIT (the numerical prefix), fetch that many old headers instead
6748 of what's specified by the `gnus-refer-thread-limit' variable."
6749   (interactive "P")
6750   (let ((id (mail-header-id (gnus-summary-article-header)))
6751         (limit (if limit (prefix-numeric-value limit)
6752                  gnus-refer-thread-limit)))
6753     ;; We want to fetch LIMIT *old* headers, but we also have to
6754     ;; re-fetch all the headers in the current buffer, because many of
6755     ;; them may be undisplayed.  So we adjust LIMIT.
6756     (when (numberp limit)
6757       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6758     (unless (eq gnus-fetch-old-headers 'invisible)
6759       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6760       ;; Retrieve the headers and read them in.
6761       (if (eq (gnus-retrieve-headers
6762                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6763               'nov)
6764           (gnus-build-all-threads)
6765         (error "Can't fetch thread from backends that don't support NOV"))
6766       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6767     (gnus-summary-limit-include-thread id)))
6768
6769 (defun gnus-summary-refer-article (message-id)
6770   "Fetch an article specified by MESSAGE-ID."
6771   (interactive "sMessage-ID: ")
6772   (when (and (stringp message-id)
6773              (not (zerop (length message-id))))
6774     ;; Construct the correct Message-ID if necessary.
6775     ;; Suggested by tale@pawl.rpi.edu.
6776     (unless (string-match "^<" message-id)
6777       (setq message-id (concat "<" message-id)))
6778     (unless (string-match ">$" message-id)
6779       (setq message-id (concat message-id ">")))
6780     (let* ((header (gnus-id-to-header message-id))
6781            (sparse (and header
6782                         (gnus-summary-article-sparse-p
6783                          (mail-header-number header))
6784                         (memq (mail-header-number header)
6785                               gnus-newsgroup-limit)))
6786            number)
6787       (cond
6788        ;; If the article is present in the buffer we just go to it.
6789        ((and header
6790              (or (not (gnus-summary-article-sparse-p
6791                        (mail-header-number header)))
6792                  sparse))
6793         (prog1
6794             (gnus-summary-goto-article
6795              (mail-header-number header) nil t)
6796           (when sparse
6797             (gnus-summary-update-article (mail-header-number header)))))
6798        (t
6799         ;; We fetch the article.
6800         (catch 'found
6801           (dolist (gnus-override-method (gnus-refer-article-methods))
6802             (gnus-check-server gnus-override-method)
6803             ;; Fetch the header, and display the article.
6804             (when (setq number (gnus-summary-insert-subject message-id))
6805               (gnus-summary-select-article nil nil nil number)
6806               (throw 'found t)))
6807           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6808
6809 (defun gnus-refer-article-methods ()
6810   "Return a list of referrable methods."
6811   (cond
6812    ;; No method, so we default to current and native.
6813    ((null gnus-refer-article-method)
6814     (list gnus-current-select-method gnus-select-method))
6815    ;; Current.
6816    ((eq 'current gnus-refer-article-method)
6817     (list gnus-current-select-method))
6818    ;; List of select methods.
6819    ((not (stringp (cadr gnus-refer-article-method)))
6820     (let (out)
6821       (dolist (method gnus-refer-article-method)
6822         (push (if (eq 'current method)
6823                   gnus-current-select-method
6824                 method)
6825               out))
6826       (nreverse out)))
6827    ;; One single select method.
6828    (t
6829     (list gnus-refer-article-method))))
6830
6831 (defun gnus-summary-edit-parameters ()
6832   "Edit the group parameters of the current group."
6833   (interactive)
6834   (gnus-group-edit-group gnus-newsgroup-name 'params))
6835
6836 (defun gnus-summary-customize-parameters ()
6837   "Customize the group parameters of the current group."
6838   (interactive)
6839   (gnus-group-customize gnus-newsgroup-name))
6840
6841 (defun gnus-summary-enter-digest-group (&optional force)
6842   "Enter an nndoc group based on the current article.
6843 If FORCE, force a digest interpretation.  If not, try
6844 to guess what the document format is."
6845   (interactive "P")
6846   (let ((conf gnus-current-window-configuration))
6847     (save-excursion
6848       (gnus-summary-select-article))
6849     (setq gnus-current-window-configuration conf)
6850     (let* ((name (format "%s-%d"
6851                          (gnus-group-prefixed-name
6852                           gnus-newsgroup-name (list 'nndoc ""))
6853                          (save-excursion
6854                            (set-buffer gnus-summary-buffer)
6855                            gnus-current-article)))
6856            (ogroup gnus-newsgroup-name)
6857            (params (append (gnus-info-params (gnus-get-info ogroup))
6858                            (list (cons 'to-group ogroup))
6859                            (list (cons 'save-article-group ogroup))))
6860            (case-fold-search t)
6861            (buf (current-buffer))
6862            dig to-address)
6863       (save-excursion
6864         (set-buffer gnus-original-article-buffer)
6865         ;; Have the digest group inherit the main mail address of
6866         ;; the parent article.
6867         (when (setq to-address (or (message-fetch-field "reply-to")
6868                                    (message-fetch-field "from")))
6869           (setq params (append (list (cons 'to-address to-address)))))
6870         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6871         (insert-buffer-substring gnus-original-article-buffer)
6872         ;; Remove lines that may lead nndoc to misinterpret the
6873         ;; document type.
6874         (narrow-to-region
6875          (goto-char (point-min))
6876          (or (search-forward "\n\n" nil t) (point)))
6877         (goto-char (point-min))
6878         (delete-matching-lines "^Path:\\|^From ")
6879         (widen))
6880       (unwind-protect
6881           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6882                     (gnus-newsgroup-ephemeral-ignored-charsets
6883                      gnus-newsgroup-ignored-charsets))
6884                 (gnus-group-read-ephemeral-group
6885                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6886                               (nndoc-article-type
6887                                ,(if force 'mbox 'guess))) t))
6888               ;; Make all postings to this group go to the parent group.
6889               (nconc (gnus-info-params (gnus-get-info name))
6890                      params)
6891             ;; Couldn't select this doc group.
6892             (switch-to-buffer buf)
6893             (gnus-set-global-variables)
6894             (gnus-configure-windows 'summary)
6895             (gnus-message 3 "Article couldn't be entered?"))
6896         (kill-buffer dig)))))
6897
6898 (defun gnus-summary-read-document (n)
6899   "Open a new group based on the current article(s).
6900 This will allow you to read digests and other similar
6901 documents as newsgroups.
6902 Obeys the standard process/prefix convention."
6903   (interactive "P")
6904   (let* ((articles (gnus-summary-work-articles n))
6905          (ogroup gnus-newsgroup-name)
6906          (params (append (gnus-info-params (gnus-get-info ogroup))
6907                          (list (cons 'to-group ogroup))))
6908          article group egroup groups vgroup)
6909     (while (setq article (pop articles))
6910       (setq group (format "%s-%d" gnus-newsgroup-name article))
6911       (gnus-summary-remove-process-mark article)
6912       (when (gnus-summary-display-article article)
6913         (save-excursion
6914           (with-temp-buffer
6915             (insert-buffer-substring gnus-original-article-buffer)
6916             ;; Remove some headers that may lead nndoc to make
6917             ;; the wrong guess.
6918             (message-narrow-to-head)
6919             (goto-char (point-min))
6920             (delete-matching-lines "^\\(Path\\):\\|^From ")
6921             (widen)
6922             (if (setq egroup
6923                       (gnus-group-read-ephemeral-group
6924                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6925                                      (nndoc-article-type guess))
6926                        t nil t))
6927                 (progn
6928                   ;; Make all postings to this group go to the parent group.
6929                   (nconc (gnus-info-params (gnus-get-info egroup))
6930                          params)
6931                   (push egroup groups))
6932               ;; Couldn't select this doc group.
6933               (gnus-error 3 "Article couldn't be entered"))))))
6934     ;; Now we have selected all the documents.
6935     (cond
6936      ((not groups)
6937       (error "None of the articles could be interpreted as documents"))
6938      ((gnus-group-read-ephemeral-group
6939        (setq vgroup (format
6940                      "nnvirtual:%s-%s" gnus-newsgroup-name
6941                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6942        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6943        t
6944        (cons (current-buffer) 'summary)))
6945      (t
6946       (error "Couldn't select virtual nndoc group")))))
6947
6948 (defun gnus-summary-isearch-article (&optional regexp-p)
6949   "Do incremental search forward on the current article.
6950 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6951   (interactive "P")
6952   (gnus-summary-select-article)
6953   (gnus-configure-windows 'article)
6954   (gnus-eval-in-buffer-window gnus-article-buffer
6955     (save-restriction
6956       (widen)
6957       (isearch-forward regexp-p))))
6958
6959 (defun gnus-summary-search-article-forward (regexp &optional backward)
6960   "Search for an article containing REGEXP forward.
6961 If BACKWARD, search backward instead."
6962   (interactive
6963    (list (read-string
6964           (format "Search article %s (regexp%s): "
6965                   (if current-prefix-arg "backward" "forward")
6966                   (if gnus-last-search-regexp
6967                       (concat ", default " gnus-last-search-regexp)
6968                     "")))
6969          current-prefix-arg))
6970   (if (string-equal regexp "")
6971       (setq regexp (or gnus-last-search-regexp ""))
6972     (setq gnus-last-search-regexp regexp))
6973   (if (gnus-summary-search-article regexp backward)
6974       (gnus-summary-show-thread)
6975     (error "Search failed: \"%s\"" regexp)))
6976
6977 (defun gnus-summary-search-article-backward (regexp)
6978   "Search for an article containing REGEXP backward."
6979   (interactive
6980    (list (read-string
6981           (format "Search article backward (regexp%s): "
6982                   (if gnus-last-search-regexp
6983                       (concat ", default " gnus-last-search-regexp)
6984                     "")))))
6985   (gnus-summary-search-article-forward regexp 'backward))
6986
6987 (defun gnus-summary-search-article (regexp &optional backward)
6988   "Search for an article containing REGEXP.
6989 Optional argument BACKWARD means do search for backward.
6990 `gnus-select-article-hook' is not called during the search."
6991   ;; We have to require this here to make sure that the following
6992   ;; dynamic binding isn't shadowed by autoloading.
6993   (require 'gnus-async)
6994   (require 'gnus-art)
6995   (let ((gnus-select-article-hook nil)  ;Disable hook.
6996         (gnus-article-prepare-hook nil)
6997         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6998         (gnus-use-article-prefetch nil)
6999         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7000         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7001         (sum (current-buffer))
7002         (gnus-display-mime-function nil)
7003         (found nil)
7004         point)
7005     (gnus-save-hidden-threads
7006       (gnus-summary-select-article)
7007       (set-buffer gnus-article-buffer)
7008       (goto-char (window-point (get-buffer-window (current-buffer))))
7009       (when backward
7010         (forward-line -1))
7011       (while (not found)
7012         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7013         (if (if backward
7014                 (re-search-backward regexp nil t)
7015               (re-search-forward regexp nil t))
7016             ;; We found the regexp.
7017             (progn
7018               (setq found 'found)
7019               (beginning-of-line)
7020               (set-window-start
7021                (get-buffer-window (current-buffer))
7022                (point))
7023               (forward-line 1)
7024               (set-window-point
7025                (get-buffer-window (current-buffer))
7026                (point))
7027               (set-buffer sum)
7028               (setq point (point)))
7029           ;; We didn't find it, so we go to the next article.
7030           (set-buffer sum)
7031           (setq found 'not)
7032           (while (eq found 'not)
7033             (if (not (if backward (gnus-summary-find-prev)
7034                        (gnus-summary-find-next)))
7035                 ;; No more articles.
7036                 (setq found t)
7037               ;; Select the next article and adjust point.
7038               (unless (gnus-summary-article-sparse-p
7039                        (gnus-summary-article-number))
7040                 (setq found nil)
7041                 (gnus-summary-select-article)
7042                 (set-buffer gnus-article-buffer)
7043                 (widen)
7044                 (goto-char (if backward (point-max) (point-min))))))))
7045       (gnus-message 7 ""))
7046     ;; Return whether we found the regexp.
7047     (when (eq found 'found)
7048       (goto-char point)
7049       (gnus-summary-show-thread)
7050       (gnus-summary-goto-subject gnus-current-article)
7051       (gnus-summary-position-point)
7052       t)))
7053
7054 (defun gnus-summary-find-matching (header regexp &optional backward unread
7055                                           not-case-fold)
7056   "Return a list of all articles that match REGEXP on HEADER.
7057 The search stars on the current article and goes forwards unless
7058 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7059 If UNREAD is non-nil, only unread articles will
7060 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7061 in the comparisons."
7062   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7063                 (gnus-data-find-list
7064                  (gnus-summary-article-number) (gnus-data-list backward))))
7065         (case-fold-search (not not-case-fold))
7066         articles d func)
7067     (if (consp header)
7068         (if (eq (car header) 'extra)
7069             (setq func
7070                   `(lambda (h)
7071                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7072                          "")))
7073           (error "%s is an invalid header" header))
7074       (unless (fboundp (intern (concat "mail-header-" header)))
7075         (error "%s is not a valid header" header))
7076       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7077     (while data
7078       (setq d (car data))
7079       (and (or (not unread)             ; We want all articles...
7080                (gnus-data-unread-p d))  ; Or just unreads.
7081            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7082            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7083            (push (gnus-data-number d) articles)) ; Success!
7084       (setq data (cdr data)))
7085     (nreverse articles)))
7086
7087 (defun gnus-summary-execute-command (header regexp command &optional backward)
7088   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7089 If HEADER is an empty string (or nil), the match is done on the entire
7090 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7091   (interactive
7092    (list (let ((completion-ignore-case t))
7093            (completing-read
7094             "Header name: "
7095             (mapcar (lambda (string) (list string))
7096                     '("Number" "Subject" "From" "Lines" "Date"
7097                       "Message-ID" "Xref" "References" "Body"))
7098             nil 'require-match))
7099          (read-string "Regexp: ")
7100          (read-key-sequence "Command: ")
7101          current-prefix-arg))
7102   (when (equal header "Body")
7103     (setq header ""))
7104   ;; Hidden thread subtrees must be searched as well.
7105   (gnus-summary-show-all-threads)
7106   ;; We don't want to change current point nor window configuration.
7107   (save-excursion
7108     (save-window-excursion
7109       (gnus-message 6 "Executing %s..." (key-description command))
7110       ;; We'd like to execute COMMAND interactively so as to give arguments.
7111       (gnus-execute header regexp
7112                     `(call-interactively ',(key-binding command))
7113                     backward)
7114       (gnus-message 6 "Executing %s...done" (key-description command)))))
7115
7116 (defun gnus-summary-beginning-of-article ()
7117   "Scroll the article back to the beginning."
7118   (interactive)
7119   (gnus-summary-select-article)
7120   (gnus-configure-windows 'article)
7121   (gnus-eval-in-buffer-window gnus-article-buffer
7122     (widen)
7123     (goto-char (point-min))
7124     (when gnus-page-broken
7125       (gnus-narrow-to-page))))
7126
7127 (defun gnus-summary-end-of-article ()
7128   "Scroll to the end of the article."
7129   (interactive)
7130   (gnus-summary-select-article)
7131   (gnus-configure-windows 'article)
7132   (gnus-eval-in-buffer-window gnus-article-buffer
7133     (widen)
7134     (goto-char (point-max))
7135     (recenter -3)
7136     (when gnus-page-broken
7137       (gnus-narrow-to-page))))
7138
7139 (defun gnus-summary-print-article (&optional filename n)
7140   "Generate and print a PostScript image of the N next (mail) articles.
7141
7142 If N is negative, print the N previous articles.  If N is nil and articles
7143 have been marked with the process mark, print these instead.
7144
7145 If the optional first argument FILENAME is nil, send the image to the
7146 printer.  If FILENAME is a string, save the PostScript image in a file with
7147 that name.  If FILENAME is a number, prompt the user for the name of the file
7148 to save in."
7149   (interactive (list (ps-print-preprint current-prefix-arg)
7150                      current-prefix-arg))
7151   (dolist (article (gnus-summary-work-articles n))
7152     (gnus-summary-select-article nil nil 'pseudo article)
7153     (gnus-eval-in-buffer-window gnus-article-buffer
7154       (let ((buffer (generate-new-buffer " *print*")))
7155         (unwind-protect
7156             (progn
7157               (copy-to-buffer buffer (point-min) (point-max))
7158               (set-buffer buffer)
7159               (gnus-article-delete-invisible-text)
7160               (let ((ps-left-header
7161                      (list
7162                       (concat "("
7163                               (mail-header-subject gnus-current-headers) ")")
7164                       (concat "("
7165                               (mail-header-from gnus-current-headers) ")")))
7166                     (ps-right-header
7167                      (list
7168                       "/pagenumberstring load"
7169                       (concat "("
7170                               (mail-header-date gnus-current-headers) ")"))))
7171                 (gnus-run-hooks 'gnus-ps-print-hook)
7172                 (save-excursion
7173                   (ps-print-buffer-with-faces filename))))
7174           (kill-buffer buffer))))))
7175
7176 (defun gnus-summary-show-article (&optional arg)
7177   "Force re-fetching of the current article.
7178 If ARG (the prefix) is a number, show the article with the charset 
7179 defined in `gnus-summary-show-article-charset-alist', or the charset
7180 inputed.
7181 If ARG (the prefix) is non-nil and not a number, show the raw article 
7182 without any article massaging functions being run."
7183   (interactive "P")
7184   (cond 
7185    ((numberp arg)
7186     (let ((gnus-newsgroup-charset 
7187            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7188                (read-coding-system "Charset: ")))
7189           (gnus-newsgroup-ignored-charsets 'gnus-all))
7190       (gnus-summary-select-article nil 'force)))
7191    ((not arg)
7192     ;; Select the article the normal way.
7193     (gnus-summary-select-article nil 'force))
7194    (t
7195     ;; We have to require this here to make sure that the following
7196     ;; dynamic binding isn't shadowed by autoloading.
7197     (require 'gnus-async)
7198     (require 'gnus-art)
7199     ;; Bind the article treatment functions to nil.
7200     (let ((gnus-have-all-headers t)
7201           gnus-article-prepare-hook
7202           gnus-article-decode-hook
7203           gnus-display-mime-function
7204           gnus-break-pages)
7205       ;; Destroy any MIME parts.
7206       (when (gnus-buffer-live-p gnus-article-buffer)
7207         (save-excursion
7208           (set-buffer gnus-article-buffer)
7209           (mm-destroy-parts gnus-article-mime-handles)))
7210       (gnus-summary-select-article nil 'force))))
7211   (gnus-summary-goto-subject gnus-current-article)
7212   (gnus-summary-position-point))
7213
7214 (defun gnus-summary-verbose-headers (&optional arg)
7215   "Toggle permanent full header display.
7216 If ARG is a positive number, turn header display on.
7217 If ARG is a negative number, turn header display off."
7218   (interactive "P")
7219   (setq gnus-show-all-headers
7220         (cond ((or (not (numberp arg))
7221                    (zerop arg))
7222                (not gnus-show-all-headers))
7223               ((natnump arg)
7224                t)))
7225   (gnus-summary-show-article))
7226
7227 (defun gnus-summary-toggle-header (&optional arg)
7228   "Show the headers if they are hidden, or hide them if they are shown.
7229 If ARG is a positive number, show the entire header.
7230 If ARG is a negative number, hide the unwanted header lines."
7231   (interactive "P")
7232   (save-excursion
7233     (set-buffer gnus-article-buffer)
7234     (save-restriction
7235       (let* ((buffer-read-only nil)
7236              (inhibit-point-motion-hooks t)
7237              hidden e)
7238         (setq hidden
7239               (if (numberp arg)
7240                   (>= arg 0)
7241                 (save-restriction 
7242                   (article-narrow-to-head)
7243                   (gnus-article-hidden-text-p 'headers))))
7244         (goto-char (point-min))
7245         (when (search-forward "\n\n" nil t)
7246           (delete-region (point-min) (1- (point))))
7247         (goto-char (point-min))
7248         (save-excursion
7249           (set-buffer gnus-original-article-buffer)
7250           (goto-char (point-min))
7251           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7252         (insert-buffer-substring gnus-original-article-buffer 1 e)
7253         (save-restriction
7254           (narrow-to-region (point-min) (point))
7255           (article-decode-encoded-words)
7256           (if  hidden
7257               (let ((gnus-treat-hide-headers nil)
7258                     (gnus-treat-hide-boring-headers nil))
7259                 (setq gnus-article-wash-types
7260                       (delq 'headers gnus-article-wash-types))
7261                 (gnus-treat-article 'head))
7262             (gnus-treat-article 'head)))
7263         (gnus-set-mode-line 'article)))))
7264
7265 (defun gnus-summary-show-all-headers ()
7266   "Make all header lines visible."
7267   (interactive)
7268   (gnus-article-show-all-headers))
7269
7270 (defun gnus-summary-caesar-message (&optional arg)
7271   "Caesar rotate the current article by 13.
7272 The numerical prefix specifies how many places to rotate each letter
7273 forward."
7274   (interactive "P")
7275   (gnus-summary-select-article)
7276   (let ((mail-header-separator ""))
7277     (gnus-eval-in-buffer-window gnus-article-buffer
7278       (save-restriction
7279         (widen)
7280         (let ((start (window-start))
7281               buffer-read-only)
7282           (message-caesar-buffer-body arg)
7283           (set-window-start (get-buffer-window (current-buffer)) start))))))
7284
7285 (defun gnus-summary-stop-page-breaking ()
7286   "Stop page breaking in the current article."
7287   (interactive)
7288   (gnus-summary-select-article)
7289   (gnus-eval-in-buffer-window gnus-article-buffer
7290     (widen)
7291     (when (gnus-visual-p 'page-marker)
7292       (let ((buffer-read-only nil))
7293         (gnus-remove-text-with-property 'gnus-prev)
7294         (gnus-remove-text-with-property 'gnus-next))
7295       (setq gnus-page-broken nil))))
7296
7297 (defun gnus-summary-move-article (&optional n to-newsgroup
7298                                             select-method action)
7299   "Move the current article to a different newsgroup.
7300 If N is a positive number, move the N next articles.
7301 If N is a negative number, move the N previous articles.
7302 If N is nil and any articles have been marked with the process mark,
7303 move those articles instead.
7304 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7305 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7306 re-spool using this method.
7307
7308 For this function to work, both the current newsgroup and the
7309 newsgroup that you want to move to have to support the `request-move'
7310 and `request-accept' functions.
7311
7312 ACTION can be either `move' (the default), `crosspost' or `copy'."
7313   (interactive "P")
7314   (unless action
7315     (setq action 'move))
7316   ;; Disable marking as read.
7317   (let (gnus-mark-article-hook)
7318     (save-window-excursion
7319       (gnus-summary-select-article)))
7320   ;; Check whether the source group supports the required functions.
7321   (cond ((and (eq action 'move)
7322               (not (gnus-check-backend-function
7323                     'request-move-article gnus-newsgroup-name)))
7324          (error "The current group does not support article moving"))
7325         ((and (eq action 'crosspost)
7326               (not (gnus-check-backend-function
7327                     'request-replace-article gnus-newsgroup-name)))
7328          (error "The current group does not support article editing")))
7329   (let ((articles (gnus-summary-work-articles n))
7330         (prefix (if (gnus-check-backend-function
7331                     'request-move-article gnus-newsgroup-name)
7332                     (gnus-group-real-prefix gnus-newsgroup-name)
7333                   ""))
7334         (names '((move "Move" "Moving")
7335                  (copy "Copy" "Copying")
7336                  (crosspost "Crosspost" "Crossposting")))
7337         (copy-buf (save-excursion
7338                     (nnheader-set-temp-buffer " *copy article*")))
7339         art-group to-method new-xref article to-groups)
7340     (unless (assq action names)
7341       (error "Unknown action %s" action))
7342     ;; Read the newsgroup name.
7343     (when (and (not to-newsgroup)
7344                (not select-method))
7345       (setq to-newsgroup
7346             (gnus-read-move-group-name
7347              (cadr (assq action names))
7348              (symbol-value (intern (format "gnus-current-%s-group" action)))
7349              articles prefix))
7350       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7351     (setq to-method (or select-method
7352                         (gnus-server-to-method
7353                          (gnus-group-method to-newsgroup))))
7354     ;; Check the method we are to move this article to...
7355     (unless (gnus-check-backend-function
7356              'request-accept-article (car to-method))
7357       (error "%s does not support article copying" (car to-method)))
7358     (unless (gnus-check-server to-method)
7359       (error "Can't open server %s" (car to-method)))
7360     (gnus-message 6 "%s to %s: %s..."
7361                   (caddr (assq action names))
7362                   (or (car select-method) to-newsgroup) articles)
7363     (while articles
7364       (setq article (pop articles))
7365       (setq
7366        art-group
7367        (cond
7368         ;; Move the article.
7369         ((eq action 'move)
7370          ;; Remove this article from future suppression.
7371          (gnus-dup-unsuppress-article article)
7372          (gnus-request-move-article
7373           article                       ; Article to move
7374           gnus-newsgroup-name           ; From newsgroup
7375           (nth 1 (gnus-find-method-for-group
7376                   gnus-newsgroup-name)) ; Server
7377           (list 'gnus-request-accept-article
7378                 to-newsgroup (list 'quote select-method)
7379                 (not articles) t)       ; Accept form
7380           (not articles)))              ; Only save nov last time
7381         ;; Copy the article.
7382         ((eq action 'copy)
7383          (save-excursion
7384            (set-buffer copy-buf)
7385            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7386              (gnus-request-accept-article
7387               to-newsgroup select-method (not articles) t))))
7388         ;; Crosspost the article.
7389         ((eq action 'crosspost)
7390          (let ((xref (message-tokenize-header
7391                       (mail-header-xref (gnus-summary-article-header article))
7392                       " ")))
7393            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7394                                   ":" article))
7395            (unless xref
7396              (setq xref (list (system-name))))
7397            (setq new-xref
7398                  (concat
7399                   (mapconcat 'identity
7400                              (delete "Xref:" (delete new-xref xref))
7401                              " ")
7402                   " " new-xref))
7403            (save-excursion
7404              (set-buffer copy-buf)
7405              ;; First put the article in the destination group.
7406              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7407              (when (consp (setq art-group
7408                                 (gnus-request-accept-article
7409                                  to-newsgroup select-method (not articles))))
7410                (setq new-xref (concat new-xref " " (car art-group)
7411                                       ":" (cdr art-group)))
7412                ;; Now we have the new Xrefs header, so we insert
7413                ;; it and replace the new article.
7414                (nnheader-replace-header "Xref" new-xref)
7415                (gnus-request-replace-article
7416                 (cdr art-group) to-newsgroup (current-buffer))
7417                art-group))))))
7418       (cond
7419        ((not art-group)
7420         (gnus-message 1 "Couldn't %s article %s: %s"
7421                       (cadr (assq action names)) article
7422                       (nnheader-get-report (car to-method))))
7423        ((eq art-group 'junk)
7424         (when (eq action 'move)
7425           (gnus-summary-mark-article article gnus-canceled-mark)
7426           (gnus-message 4 "Deleted article %s" article)))
7427        (t
7428         (let* ((pto-group (gnus-group-prefixed-name
7429                            (car art-group) to-method))
7430                (entry
7431                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7432                (info (nth 2 entry))
7433                (to-group (gnus-info-group info))
7434                to-marks)
7435           ;; Update the group that has been moved to.
7436           (when (and info
7437                      (memq action '(move copy)))
7438             (unless (member to-group to-groups)
7439               (push to-group to-groups))
7440
7441             (unless (memq article gnus-newsgroup-unreads)
7442               (push 'read to-marks)
7443               (gnus-info-set-read
7444                info (gnus-add-to-range (gnus-info-read info)
7445                                        (list (cdr art-group)))))
7446
7447             ;; See whether the article is to be put in the cache.
7448             (let ((marks gnus-article-mark-lists)
7449                   (to-article (cdr art-group)))
7450
7451               ;; Enter the article into the cache in the new group,
7452               ;; if that is required.
7453               (when gnus-use-cache
7454                 (gnus-cache-possibly-enter-article
7455                  to-group to-article
7456                  (memq article gnus-newsgroup-marked)
7457                  (memq article gnus-newsgroup-dormant)
7458                  (memq article gnus-newsgroup-unreads)))
7459
7460               (when gnus-preserve-marks 
7461                 ;; Copy any marks over to the new group.
7462                 (when (and (equal to-group gnus-newsgroup-name)
7463                            (not (memq article gnus-newsgroup-unreads)))
7464                   ;; Mark this article as read in this group.
7465                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7466                   (setcdr (gnus-active to-group) to-article)
7467                   (setcdr gnus-newsgroup-active to-article))
7468
7469                 (while marks
7470                   (when (memq article (symbol-value
7471                                        (intern (format "gnus-newsgroup-%s"
7472                                                        (caar marks)))))
7473                     (push (cdar marks) to-marks)
7474                     ;; If the other group is the same as this group,
7475                     ;; then we have to add the mark to the list.
7476                     (when (equal to-group gnus-newsgroup-name)
7477                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7478                            (cons to-article
7479                                  (symbol-value
7480                                   (intern (format "gnus-newsgroup-%s"
7481                                                   (caar marks)))))))
7482                     ;; Copy the marks to other group.
7483                     (gnus-add-marked-articles
7484                      to-group (cdar marks) (list to-article) info))
7485                   (setq marks (cdr marks)))
7486
7487                 (gnus-request-set-mark to-group (list (list (list to-article)
7488                                                             'set
7489                                                             to-marks))))
7490
7491               (gnus-dribble-enter
7492                (concat "(gnus-group-set-info '"
7493                        (gnus-prin1-to-string (gnus-get-info to-group))
7494                        ")"))))
7495
7496           ;; Update the Xref header in this article to point to
7497           ;; the new crossposted article we have just created.
7498           (when (eq action 'crosspost)
7499             (save-excursion
7500               (set-buffer copy-buf)
7501               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7502               (nnheader-replace-header "Xref" new-xref)
7503               (gnus-request-replace-article
7504                article gnus-newsgroup-name (current-buffer)))))
7505
7506         ;;;!!!Why is this necessary?
7507         (set-buffer gnus-summary-buffer)
7508
7509         (gnus-summary-goto-subject article)
7510         (when (eq action 'move)
7511           (gnus-summary-mark-article article gnus-canceled-mark))))
7512       (gnus-summary-remove-process-mark article))
7513     ;; Re-activate all groups that have been moved to.
7514     (while to-groups
7515       (save-excursion
7516         (set-buffer gnus-group-buffer)
7517         (when (gnus-group-goto-group (car to-groups) t)
7518           (gnus-group-get-new-news-this-group 1 t))
7519         (pop to-groups)))
7520
7521     (gnus-kill-buffer copy-buf)
7522     (gnus-summary-position-point)
7523     (gnus-set-mode-line 'summary)))
7524
7525 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7526   "Move the current article to a different newsgroup.
7527 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7528 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7529 re-spool using this method."
7530   (interactive "P")
7531   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7532
7533 (defun gnus-summary-crosspost-article (&optional n)
7534   "Crosspost the current article to some other group."
7535   (interactive "P")
7536   (gnus-summary-move-article n nil nil 'crosspost))
7537
7538 (defcustom gnus-summary-respool-default-method nil
7539   "Default method for respooling an article.
7540 If nil, use to the current newsgroup method."
7541   :type '(choice (gnus-select-method :value (nnml ""))
7542                  (const nil))
7543   :group 'gnus-summary-mail)
7544
7545 (defun gnus-summary-respool-article (&optional n method)
7546   "Respool the current article.
7547 The article will be squeezed through the mail spooling process again,
7548 which means that it will be put in some mail newsgroup or other
7549 depending on `nnmail-split-methods'.
7550 If N is a positive number, respool the N next articles.
7551 If N is a negative number, respool the N previous articles.
7552 If N is nil and any articles have been marked with the process mark,
7553 respool those articles instead.
7554
7555 Respooling can be done both from mail groups and \"real\" newsgroups.
7556 In the former case, the articles in question will be moved from the
7557 current group into whatever groups they are destined to.  In the
7558 latter case, they will be copied into the relevant groups."
7559   (interactive
7560    (list current-prefix-arg
7561          (let* ((methods (gnus-methods-using 'respool))
7562                 (methname
7563                  (symbol-name (or gnus-summary-respool-default-method
7564                                   (car (gnus-find-method-for-group
7565                                         gnus-newsgroup-name)))))
7566                 (method
7567                  (gnus-completing-read
7568                   methname "What backend do you want to use when respooling?"
7569                   methods nil t nil 'gnus-mail-method-history))
7570                 ms)
7571            (cond
7572             ((zerop (length (setq ms (gnus-servers-using-backend
7573                                       (intern method)))))
7574              (list (intern method) ""))
7575             ((= 1 (length ms))
7576              (car ms))
7577             (t
7578              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7579                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7580                            ms-alist))))))))
7581   (unless method
7582     (error "No method given for respooling"))
7583   (if (assoc (symbol-name
7584               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7585              (gnus-methods-using 'respool))
7586       (gnus-summary-move-article n nil method)
7587     (gnus-summary-copy-article n nil method)))
7588
7589 (defun gnus-summary-import-article (file)
7590   "Import an arbitrary file into a mail newsgroup."
7591   (interactive "fImport file: ")
7592   (let ((group gnus-newsgroup-name)
7593         (now (current-time))
7594         atts lines)
7595     (unless (gnus-check-backend-function 'request-accept-article group)
7596       (error "%s does not support article importing" group))
7597     (or (file-readable-p file)
7598         (not (file-regular-p file))
7599         (error "Can't read %s" file))
7600     (save-excursion
7601       (set-buffer (gnus-get-buffer-create " *import file*"))
7602       (erase-buffer)
7603       (insert-file-contents file)
7604       (goto-char (point-min))
7605       (unless (nnheader-article-p)
7606         ;; This doesn't look like an article, so we fudge some headers.
7607         (setq atts (file-attributes file)
7608               lines (count-lines (point-min) (point-max)))
7609         (insert "From: " (read-string "From: ") "\n"
7610                 "Subject: " (read-string "Subject: ") "\n"
7611                 "Date: " (message-make-date (nth 5 atts))
7612                 "\n"
7613                 "Message-ID: " (message-make-message-id) "\n"
7614                 "Lines: " (int-to-string lines) "\n"
7615                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7616       (gnus-request-accept-article group nil t)
7617       (kill-buffer (current-buffer)))))
7618
7619 (defun gnus-summary-article-posted-p ()
7620   "Say whether the current (mail) article is available from news as well.
7621 This will be the case if the article has both been mailed and posted."
7622   (interactive)
7623   (let ((id (mail-header-references (gnus-summary-article-header)))
7624         (gnus-override-method (car (gnus-refer-article-methods))))
7625     (if (gnus-request-head id "")
7626         (gnus-message 2 "The current message was found on %s"
7627                       gnus-override-method)
7628       (gnus-message 2 "The current message couldn't be found on %s"
7629                     gnus-override-method)
7630       nil)))
7631
7632 (defun gnus-summary-expire-articles (&optional now)
7633   "Expire all articles that are marked as expirable in the current group."
7634   (interactive)
7635   (when (gnus-check-backend-function
7636          'request-expire-articles gnus-newsgroup-name)
7637     ;; This backend supports expiry.
7638     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7639            (expirable (if total
7640                           (progn
7641                             ;; We need to update the info for
7642                             ;; this group for `gnus-list-of-read-articles'
7643                             ;; to give us the right answer.
7644                             (gnus-run-hooks 'gnus-exit-group-hook)
7645                             (gnus-summary-update-info)
7646                             (gnus-list-of-read-articles gnus-newsgroup-name))
7647                         (setq gnus-newsgroup-expirable
7648                               (sort gnus-newsgroup-expirable '<))))
7649            (expiry-wait (if now 'immediate
7650                           (gnus-group-find-parameter
7651                            gnus-newsgroup-name 'expiry-wait)))
7652            es)
7653       (when expirable
7654         ;; There are expirable articles in this group, so we run them
7655         ;; through the expiry process.
7656         (gnus-message 6 "Expiring articles...")
7657         (unless (gnus-check-group gnus-newsgroup-name)
7658           (error "Can't open server for %s" gnus-newsgroup-name))
7659         ;; The list of articles that weren't expired is returned.
7660         (save-excursion
7661           (if expiry-wait
7662               (let ((nnmail-expiry-wait-function nil)
7663                     (nnmail-expiry-wait expiry-wait))
7664                 (setq es (gnus-request-expire-articles
7665                           expirable gnus-newsgroup-name)))
7666             (setq es (gnus-request-expire-articles
7667                       expirable gnus-newsgroup-name))))
7668         (unless total
7669           (setq gnus-newsgroup-expirable es))
7670         ;; We go through the old list of expirable, and mark all
7671         ;; really expired articles as nonexistent.
7672         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7673           (let ((gnus-use-cache nil))
7674             (while expirable
7675               (unless (memq (car expirable) es)
7676                 (when (gnus-data-find (car expirable))
7677                   (gnus-summary-mark-article
7678                    (car expirable) gnus-canceled-mark)))
7679               (setq expirable (cdr expirable)))))
7680         (gnus-message 6 "Expiring articles...done")))))
7681
7682 (defun gnus-summary-expire-articles-now ()
7683   "Expunge all expirable articles in the current group.
7684 This means that *all* articles that are marked as expirable will be
7685 deleted forever, right now."
7686   (interactive)
7687   (or gnus-expert-user
7688       (gnus-yes-or-no-p
7689        "Are you really, really, really sure you want to delete all these messages? ")
7690       (error "Phew!"))
7691   (gnus-summary-expire-articles t))
7692
7693 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7694 (defun gnus-summary-delete-article (&optional n)
7695   "Delete the N next (mail) articles.
7696 This command actually deletes articles.  This is not a marking
7697 command.  The article will disappear forever from your life, never to
7698 return.
7699 If N is negative, delete backwards.
7700 If N is nil and articles have been marked with the process mark,
7701 delete these instead."
7702   (interactive "P")
7703   (unless (gnus-check-backend-function 'request-expire-articles
7704                                        gnus-newsgroup-name)
7705     (error "The current newsgroup does not support article deletion"))
7706   ;; Compute the list of articles to delete.
7707   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7708         not-deleted)
7709     (if (and gnus-novice-user
7710              (not (gnus-yes-or-no-p
7711                    (format "Do you really want to delete %s forever? "
7712                            (if (> (length articles) 1)
7713                                (format "these %s articles" (length articles))
7714                              "this article")))))
7715         ()
7716       ;; Delete the articles.
7717       (setq not-deleted (gnus-request-expire-articles
7718                          articles gnus-newsgroup-name 'force))
7719       (while articles
7720         (gnus-summary-remove-process-mark (car articles))
7721         ;; The backend might not have been able to delete the article
7722         ;; after all.
7723         (unless (memq (car articles) not-deleted)
7724           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7725         (setq articles (cdr articles)))
7726       (when not-deleted
7727         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7728     (gnus-summary-position-point)
7729     (gnus-set-mode-line 'summary)
7730     not-deleted))
7731
7732 (defun gnus-summary-edit-article (&optional force)
7733   "Edit the current article.
7734 This will have permanent effect only in mail groups.
7735 If FORCE is non-nil, allow editing of articles even in read-only
7736 groups."
7737   (interactive "P")
7738   (save-excursion
7739     (set-buffer gnus-summary-buffer)
7740     (let ((mail-parse-charset gnus-newsgroup-charset)
7741           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7742       (gnus-set-global-variables)
7743       (when (and (not force)
7744                  (gnus-group-read-only-p))
7745         (error "The current newsgroup does not support article editing"))
7746       (gnus-summary-show-article t)
7747       (gnus-article-edit-article
7748        'mime-to-mml
7749        `(lambda (no-highlight)
7750           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7751                 (mail-parse-ignored-charsets 
7752                  ',gnus-newsgroup-ignored-charsets))
7753             (mml-to-mime)
7754             (gnus-summary-edit-article-done
7755              ,(or (mail-header-references gnus-current-headers) "")
7756              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7757
7758 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7759
7760 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7761                                                  no-highlight)
7762   "Make edits to the current article permanent."
7763   (interactive)
7764   ;; Replace the article.
7765   (let ((buf (current-buffer)))
7766     (with-temp-buffer
7767       (insert-buffer-substring buf)
7768       (if (and (not read-only)
7769                (not (gnus-request-replace-article
7770                      (cdr gnus-article-current) (car gnus-article-current)
7771                      (current-buffer) t)))
7772           (error "Couldn't replace article")
7773         ;; Update the summary buffer.
7774         (if (and references
7775                  (equal (message-tokenize-header references " ")
7776                         (message-tokenize-header
7777                          (or (message-fetch-field "references") "") " ")))
7778             ;; We only have to update this line.
7779             (save-excursion
7780               (save-restriction
7781                 (message-narrow-to-head)
7782                 (let ((head (buffer-string))
7783                       header)
7784                   (with-temp-buffer
7785                     (insert (format "211 %d Article retrieved.\n"
7786                                     (cdr gnus-article-current)))
7787                     (insert head)
7788                     (insert ".\n")
7789                     (let ((nntp-server-buffer (current-buffer)))
7790                       (setq header (car (gnus-get-newsgroup-headers
7791                                          (save-excursion
7792                                            (set-buffer gnus-summary-buffer)
7793                                            gnus-newsgroup-dependencies)
7794                                          t))))
7795                     (save-excursion
7796                       (set-buffer gnus-summary-buffer)
7797                       (gnus-data-set-header
7798                        (gnus-data-find (cdr gnus-article-current))
7799                        header)
7800                       (gnus-summary-update-article-line
7801                        (cdr gnus-article-current) header))))))
7802           ;; Update threads.
7803           (set-buffer (or buffer gnus-summary-buffer))
7804           (gnus-summary-update-article (cdr gnus-article-current)))
7805         ;; Prettify the article buffer again.
7806         (unless no-highlight
7807           (save-excursion
7808             (set-buffer gnus-article-buffer)
7809             ;;;!!! Fix this -- article should be rehighlighted.
7810             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7811             (set-buffer gnus-original-article-buffer)
7812             (gnus-request-article
7813              (cdr gnus-article-current)
7814              (car gnus-article-current) (current-buffer))))
7815         ;; Prettify the summary buffer line.
7816         (when (gnus-visual-p 'summary-highlight 'highlight)
7817           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7818
7819 (defun gnus-summary-edit-wash (key)
7820   "Perform editing command KEY in the article buffer."
7821   (interactive
7822    (list
7823     (progn
7824       (message "%s" (concat (this-command-keys) "- "))
7825       (read-char))))
7826   (message "")
7827   (gnus-summary-edit-article)
7828   (execute-kbd-macro (concat (this-command-keys) key))
7829   (gnus-article-edit-done))
7830
7831 ;;; Respooling
7832
7833 (defun gnus-summary-respool-query (&optional silent trace)
7834   "Query where the respool algorithm would put this article."
7835   (interactive)
7836   (let (gnus-mark-article-hook)
7837     (gnus-summary-select-article)
7838     (save-excursion
7839       (set-buffer gnus-original-article-buffer)
7840       (save-restriction
7841         (message-narrow-to-head)
7842         (let ((groups (nnmail-article-group 'identity trace)))
7843           (unless silent
7844             (if groups
7845                 (message "This message would go to %s"
7846                          (mapconcat 'car groups ", "))
7847               (message "This message would go to no groups"))
7848             groups))))))
7849
7850 (defun gnus-summary-respool-trace ()
7851   "Trace where the respool algorithm would put this article.
7852 Display a buffer showing all fancy splitting patterns which matched."
7853   (interactive)
7854   (gnus-summary-respool-query nil t))
7855
7856 ;; Summary marking commands.
7857
7858 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7859   "Mark articles which has the same subject as read, and then select the next.
7860 If UNMARK is positive, remove any kind of mark.
7861 If UNMARK is negative, tick articles."
7862   (interactive "P")
7863   (when unmark
7864     (setq unmark (prefix-numeric-value unmark)))
7865   (let ((count
7866          (gnus-summary-mark-same-subject
7867           (gnus-summary-article-subject) unmark)))
7868     ;; Select next unread article.  If auto-select-same mode, should
7869     ;; select the first unread article.
7870     (gnus-summary-next-article t (and gnus-auto-select-same
7871                                       (gnus-summary-article-subject)))
7872     (gnus-message 7 "%d article%s marked as %s"
7873                   count (if (= count 1) " is" "s are")
7874                   (if unmark "unread" "read"))))
7875
7876 (defun gnus-summary-kill-same-subject (&optional unmark)
7877   "Mark articles which has the same subject as read.
7878 If UNMARK is positive, remove any kind of mark.
7879 If UNMARK is negative, tick articles."
7880   (interactive "P")
7881   (when unmark
7882     (setq unmark (prefix-numeric-value unmark)))
7883   (let ((count
7884          (gnus-summary-mark-same-subject
7885           (gnus-summary-article-subject) unmark)))
7886     ;; If marked as read, go to next unread subject.
7887     (when (null unmark)
7888       ;; Go to next unread subject.
7889       (gnus-summary-next-subject 1 t))
7890     (gnus-message 7 "%d articles are marked as %s"
7891                   count (if unmark "unread" "read"))))
7892
7893 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7894   "Mark articles with same SUBJECT as read, and return marked number.
7895 If optional argument UNMARK is positive, remove any kinds of marks.
7896 If optional argument UNMARK is negative, mark articles as unread instead."
7897   (let ((count 1))
7898     (save-excursion
7899       (cond
7900        ((null unmark)                   ; Mark as read.
7901         (while (and
7902                 (progn
7903                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7904                   (gnus-summary-show-thread) t)
7905                 (gnus-summary-find-subject subject))
7906           (setq count (1+ count))))
7907        ((> unmark 0)                    ; Tick.
7908         (while (and
7909                 (progn
7910                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7911                   (gnus-summary-show-thread) t)
7912                 (gnus-summary-find-subject subject))
7913           (setq count (1+ count))))
7914        (t                               ; Mark as unread.
7915         (while (and
7916                 (progn
7917                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7918                   (gnus-summary-show-thread) t)
7919                 (gnus-summary-find-subject subject))
7920           (setq count (1+ count)))))
7921       (gnus-set-mode-line 'summary)
7922       ;; Return the number of marked articles.
7923       count)))
7924
7925 (defun gnus-summary-mark-as-processable (n &optional unmark)
7926   "Set the process mark on the next N articles.
7927 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7928 the process mark instead.  The difference between N and the actual
7929 number of articles marked is returned."
7930   (interactive "p")
7931   (let ((backward (< n 0))
7932         (n (abs n)))
7933     (while (and
7934             (> n 0)
7935             (if unmark
7936                 (gnus-summary-remove-process-mark
7937                  (gnus-summary-article-number))
7938               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7939             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7940       (setq n (1- n)))
7941     (when (/= 0 n)
7942       (gnus-message 7 "No more articles"))
7943     (gnus-summary-recenter)
7944     (gnus-summary-position-point)
7945     n))
7946
7947 (defun gnus-summary-unmark-as-processable (n)
7948   "Remove the process mark from the next N articles.
7949 If N is negative, unmark backward instead.  The difference between N and
7950 the actual number of articles unmarked is returned."
7951   (interactive "p")
7952   (gnus-summary-mark-as-processable n t))
7953
7954 (defun gnus-summary-unmark-all-processable ()
7955   "Remove the process mark from all articles."
7956   (interactive)
7957   (save-excursion
7958     (while gnus-newsgroup-processable
7959       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7960   (gnus-summary-position-point))
7961
7962 (defun gnus-summary-mark-as-expirable (n)
7963   "Mark N articles forward as expirable.
7964 If N is negative, mark backward instead.  The difference between N and
7965 the actual number of articles marked is returned."
7966   (interactive "p")
7967   (gnus-summary-mark-forward n gnus-expirable-mark))
7968
7969 (defun gnus-summary-mark-article-as-replied (article)
7970   "Mark ARTICLE replied and update the summary line."
7971   (push article gnus-newsgroup-replied)
7972   (let ((buffer-read-only nil))
7973     (when (gnus-summary-goto-subject article nil t)
7974       (gnus-summary-update-secondary-mark article))))
7975
7976 (defun gnus-summary-set-bookmark (article)
7977   "Set a bookmark in current article."
7978   (interactive (list (gnus-summary-article-number)))
7979   (when (or (not (get-buffer gnus-article-buffer))
7980             (not gnus-current-article)
7981             (not gnus-article-current)
7982             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7983     (error "No current article selected"))
7984   ;; Remove old bookmark, if one exists.
7985   (let ((old (assq article gnus-newsgroup-bookmarks)))
7986     (when old
7987       (setq gnus-newsgroup-bookmarks
7988             (delq old gnus-newsgroup-bookmarks))))
7989   ;; Set the new bookmark, which is on the form
7990   ;; (article-number . line-number-in-body).
7991   (push
7992    (cons article
7993          (save-excursion
7994            (set-buffer gnus-article-buffer)
7995            (count-lines
7996             (min (point)
7997                  (save-excursion
7998                    (goto-char (point-min))
7999                    (search-forward "\n\n" nil t)
8000                    (point)))
8001             (point))))
8002    gnus-newsgroup-bookmarks)
8003   (gnus-message 6 "A bookmark has been added to the current article."))
8004
8005 (defun gnus-summary-remove-bookmark (article)
8006   "Remove the bookmark from the current article."
8007   (interactive (list (gnus-summary-article-number)))
8008   ;; Remove old bookmark, if one exists.
8009   (let ((old (assq article gnus-newsgroup-bookmarks)))
8010     (if old
8011         (progn
8012           (setq gnus-newsgroup-bookmarks
8013                 (delq old gnus-newsgroup-bookmarks))
8014           (gnus-message 6 "Removed bookmark."))
8015       (gnus-message 6 "No bookmark in current article."))))
8016
8017 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8018 (defun gnus-summary-mark-as-dormant (n)
8019   "Mark N articles forward as dormant.
8020 If N is negative, mark backward instead.  The difference between N and
8021 the actual number of articles marked is returned."
8022   (interactive "p")
8023   (gnus-summary-mark-forward n gnus-dormant-mark))
8024
8025 (defun gnus-summary-set-process-mark (article)
8026   "Set the process mark on ARTICLE and update the summary line."
8027   (setq gnus-newsgroup-processable
8028         (cons article
8029               (delq article gnus-newsgroup-processable)))
8030   (when (gnus-summary-goto-subject article)
8031     (gnus-summary-show-thread)
8032     (gnus-summary-goto-subject article)
8033     (gnus-summary-update-secondary-mark article)))
8034
8035 (defun gnus-summary-remove-process-mark (article)
8036   "Remove the process mark from ARTICLE and update the summary line."
8037   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8038   (when (gnus-summary-goto-subject article)
8039     (gnus-summary-show-thread)
8040     (gnus-summary-goto-subject article)
8041     (gnus-summary-update-secondary-mark article)))
8042
8043 (defun gnus-summary-set-saved-mark (article)
8044   "Set the process mark on ARTICLE and update the summary line."
8045   (push article gnus-newsgroup-saved)
8046   (when (gnus-summary-goto-subject article)
8047     (gnus-summary-update-secondary-mark article)))
8048
8049 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8050   "Mark N articles as read forwards.
8051 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8052 The difference between N and the actual number of articles marked is
8053 returned.
8054 Iff NO-EXPIRE, auto-expiry will be inhibited."
8055   (interactive "p")
8056   (gnus-summary-show-thread)
8057   (let ((backward (< n 0))
8058         (gnus-summary-goto-unread
8059          (and gnus-summary-goto-unread
8060               (not (eq gnus-summary-goto-unread 'never))
8061               (not (memq mark (list gnus-unread-mark
8062                                     gnus-ticked-mark gnus-dormant-mark)))))
8063         (n (abs n))
8064         (mark (or mark gnus-del-mark)))
8065     (while (and (> n 0)
8066                 (gnus-summary-mark-article nil mark no-expire)
8067                 (zerop (gnus-summary-next-subject
8068                         (if backward -1 1)
8069                         (and gnus-summary-goto-unread
8070                              (not (eq gnus-summary-goto-unread 'never)))
8071                         t)))
8072       (setq n (1- n)))
8073     (when (/= 0 n)
8074       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8075     (gnus-summary-recenter)
8076     (gnus-summary-position-point)
8077     (gnus-set-mode-line 'summary)
8078     n))
8079
8080 (defun gnus-summary-mark-article-as-read (mark)
8081   "Mark the current article quickly as read with MARK."
8082   (let ((article (gnus-summary-article-number)))
8083     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8084     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8085     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8086     (push (cons article mark) gnus-newsgroup-reads)
8087     ;; Possibly remove from cache, if that is used.
8088     (when gnus-use-cache
8089       (gnus-cache-enter-remove-article article))
8090     ;; Allow the backend to change the mark.
8091     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8092     ;; Check for auto-expiry.
8093     (when (and gnus-newsgroup-auto-expire
8094                (memq mark gnus-auto-expirable-marks))
8095       (setq mark gnus-expirable-mark)
8096       ;; Let the backend know about the mark change.
8097       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8098       (push article gnus-newsgroup-expirable))
8099     ;; Set the mark in the buffer.
8100     (gnus-summary-update-mark mark 'unread)
8101     t))
8102
8103 (defun gnus-summary-mark-article-as-unread (mark)
8104   "Mark the current article quickly as unread with MARK."
8105   (let* ((article (gnus-summary-article-number))
8106          (old-mark (gnus-summary-article-mark article)))
8107     ;; Allow the backend to change the mark.
8108     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8109     (if (eq mark old-mark)
8110         t
8111       (if (<= article 0)
8112           (progn
8113             (gnus-error 1 "Can't mark negative article numbers")
8114             nil)
8115         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8116         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8117         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8118         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8119         (cond ((= mark gnus-ticked-mark)
8120                (push article gnus-newsgroup-marked))
8121               ((= mark gnus-dormant-mark)
8122                (push article gnus-newsgroup-dormant))
8123               (t
8124                (push article gnus-newsgroup-unreads)))
8125         (gnus-pull article gnus-newsgroup-reads)
8126
8127         ;; See whether the article is to be put in the cache.
8128         (and gnus-use-cache
8129              (vectorp (gnus-summary-article-header article))
8130              (save-excursion
8131                (gnus-cache-possibly-enter-article
8132                 gnus-newsgroup-name article
8133                 (= mark gnus-ticked-mark)
8134                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8135
8136         ;; Fix the mark.
8137         (gnus-summary-update-mark mark 'unread)
8138         t))))
8139
8140 (defun gnus-summary-mark-article (&optional article mark no-expire)
8141   "Mark ARTICLE with MARK.  MARK can be any character.
8142 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8143 `??' (dormant) and `?E' (expirable).
8144 If MARK is nil, then the default character `?r' is used.
8145 If ARTICLE is nil, then the article on the current line will be
8146 marked.
8147 Iff NO-EXPIRE, auto-expiry will be inhibited."
8148   ;; The mark might be a string.
8149   (when (stringp mark)
8150     (setq mark (aref mark 0)))
8151   ;; If no mark is given, then we check auto-expiring.
8152   (when (null mark)
8153     (setq mark gnus-del-mark))
8154   (when (and (not no-expire)
8155              gnus-newsgroup-auto-expire
8156              (memq mark gnus-auto-expirable-marks))
8157     (setq mark gnus-expirable-mark))
8158   (let ((article (or article (gnus-summary-article-number)))
8159         (old-mark (gnus-summary-article-mark article)))
8160     ;; Allow the backend to change the mark.
8161     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8162     (if (eq mark old-mark)
8163         t
8164       (unless article
8165         (error "No article on current line"))
8166       (if (not (if (or (= mark gnus-unread-mark)
8167                        (= mark gnus-ticked-mark)
8168                        (= mark gnus-dormant-mark))
8169                    (gnus-mark-article-as-unread article mark)
8170                  (gnus-mark-article-as-read article mark)))
8171           t
8172         ;; See whether the article is to be put in the cache.
8173         (and gnus-use-cache
8174              (not (= mark gnus-canceled-mark))
8175              (vectorp (gnus-summary-article-header article))
8176              (save-excursion
8177                (gnus-cache-possibly-enter-article
8178                 gnus-newsgroup-name article
8179                 (= mark gnus-ticked-mark)
8180                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8181
8182         (when (gnus-summary-goto-subject article nil t)
8183           (let ((buffer-read-only nil))
8184             (gnus-summary-show-thread)
8185             ;; Fix the mark.
8186             (gnus-summary-update-mark mark 'unread)
8187             t))))))
8188
8189 (defun gnus-summary-update-secondary-mark (article)
8190   "Update the secondary (read, process, cache) mark."
8191   (gnus-summary-update-mark
8192    (cond ((memq article gnus-newsgroup-processable)
8193           gnus-process-mark)
8194          ((memq article gnus-newsgroup-cached)
8195           gnus-cached-mark)
8196          ((memq article gnus-newsgroup-replied)
8197           gnus-replied-mark)
8198          ((memq article gnus-newsgroup-saved)
8199           gnus-saved-mark)
8200          (t gnus-unread-mark))
8201    'replied)
8202   (when (gnus-visual-p 'summary-highlight 'highlight)
8203     (gnus-run-hooks 'gnus-summary-update-hook))
8204   t)
8205
8206 (defun gnus-summary-update-mark (mark type)
8207   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8208         (buffer-read-only nil))
8209     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8210     (when forward
8211       (when (looking-at "\r")
8212         (incf forward))
8213       (when (<= (+ forward (point)) (point-max))
8214         ;; Go to the right position on the line.
8215         (goto-char (+ forward (point)))
8216         ;; Replace the old mark with the new mark.
8217         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8218         ;; Optionally update the marks by some user rule.
8219         (when (eq type 'unread)
8220           (gnus-data-set-mark
8221            (gnus-data-find (gnus-summary-article-number)) mark)
8222           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8223
8224 (defun gnus-mark-article-as-read (article &optional mark)
8225   "Enter ARTICLE in the pertinent lists and remove it from others."
8226   ;; Make the article expirable.
8227   (let ((mark (or mark gnus-del-mark)))
8228     (if (= mark gnus-expirable-mark)
8229         (push article gnus-newsgroup-expirable)
8230       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8231     ;; Remove from unread and marked lists.
8232     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8233     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8234     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8235     (push (cons article mark) gnus-newsgroup-reads)
8236     ;; Possibly remove from cache, if that is used.
8237     (when gnus-use-cache
8238       (gnus-cache-enter-remove-article article))
8239     t))
8240
8241 (defun gnus-mark-article-as-unread (article &optional mark)
8242   "Enter ARTICLE in the pertinent lists and remove it from others."
8243   (let ((mark (or mark gnus-ticked-mark)))
8244     (if (<= article 0)
8245         (progn
8246           (gnus-error 1 "Can't mark negative article numbers")
8247           nil)
8248       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8249             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8250             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8251             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8252
8253       ;; Unsuppress duplicates?
8254       (when gnus-suppress-duplicates
8255         (gnus-dup-unsuppress-article article))
8256
8257       (cond ((= mark gnus-ticked-mark)
8258              (push article gnus-newsgroup-marked))
8259             ((= mark gnus-dormant-mark)
8260              (push article gnus-newsgroup-dormant))
8261             (t
8262              (push article gnus-newsgroup-unreads)))
8263       (gnus-pull article gnus-newsgroup-reads)
8264       t)))
8265
8266 (defalias 'gnus-summary-mark-as-unread-forward
8267   'gnus-summary-tick-article-forward)
8268 (make-obsolete 'gnus-summary-mark-as-unread-forward
8269                'gnus-summary-tick-article-forward)
8270 (defun gnus-summary-tick-article-forward (n)
8271   "Tick N articles forwards.
8272 If N is negative, tick backwards instead.
8273 The difference between N and the number of articles ticked is returned."
8274   (interactive "p")
8275   (gnus-summary-mark-forward n gnus-ticked-mark))
8276
8277 (defalias 'gnus-summary-mark-as-unread-backward
8278   'gnus-summary-tick-article-backward)
8279 (make-obsolete 'gnus-summary-mark-as-unread-backward
8280                'gnus-summary-tick-article-backward)
8281 (defun gnus-summary-tick-article-backward (n)
8282   "Tick N articles backwards.
8283 The difference between N and the number of articles ticked is returned."
8284   (interactive "p")
8285   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8286
8287 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8288 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8289 (defun gnus-summary-tick-article (&optional article clear-mark)
8290   "Mark current article as unread.
8291 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8292 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8293   (interactive)
8294   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8295                                        gnus-ticked-mark)))
8296
8297 (defun gnus-summary-mark-as-read-forward (n)
8298   "Mark N articles as read forwards.
8299 If N is negative, mark backwards instead.
8300 The difference between N and the actual number of articles marked is
8301 returned."
8302   (interactive "p")
8303   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8304
8305 (defun gnus-summary-mark-as-read-backward (n)
8306   "Mark the N articles as read backwards.
8307 The difference between N and the actual number of articles marked is
8308 returned."
8309   (interactive "p")
8310   (gnus-summary-mark-forward
8311    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8312
8313 (defun gnus-summary-mark-as-read (&optional article mark)
8314   "Mark current article as read.
8315 ARTICLE specifies the article to be marked as read.
8316 MARK specifies a string to be inserted at the beginning of the line."
8317   (gnus-summary-mark-article article mark))
8318
8319 (defun gnus-summary-clear-mark-forward (n)
8320   "Clear marks from N articles forward.
8321 If N is negative, clear backward instead.
8322 The difference between N and the number of marks cleared is returned."
8323   (interactive "p")
8324   (gnus-summary-mark-forward n gnus-unread-mark))
8325
8326 (defun gnus-summary-clear-mark-backward (n)
8327   "Clear marks from N articles backward.
8328 The difference between N and the number of marks cleared is returned."
8329   (interactive "p")
8330   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8331
8332 (defun gnus-summary-mark-unread-as-read ()
8333   "Intended to be used by `gnus-summary-mark-article-hook'."
8334   (when (memq gnus-current-article gnus-newsgroup-unreads)
8335     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8336
8337 (defun gnus-summary-mark-read-and-unread-as-read ()
8338   "Intended to be used by `gnus-summary-mark-article-hook'."
8339   (let ((mark (gnus-summary-article-mark)))
8340     (when (or (gnus-unread-mark-p mark)
8341               (gnus-read-mark-p mark))
8342       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8343
8344 (defun gnus-summary-mark-region-as-read (point mark all)
8345   "Mark all unread articles between point and mark as read.
8346 If given a prefix, mark all articles between point and mark as read,
8347 even ticked and dormant ones."
8348   (interactive "r\nP")
8349   (save-excursion
8350     (let (article)
8351       (goto-char point)
8352       (beginning-of-line)
8353       (while (and
8354               (< (point) mark)
8355               (progn
8356                 (when (or all
8357                           (memq (setq article (gnus-summary-article-number))
8358                                 gnus-newsgroup-unreads))
8359                   (gnus-summary-mark-article article gnus-del-mark))
8360                 t)
8361               (gnus-summary-find-next))))))
8362
8363 (defun gnus-summary-mark-below (score mark)
8364   "Mark articles with score less than SCORE with MARK."
8365   (interactive "P\ncMark: ")
8366   (setq score (if score
8367                   (prefix-numeric-value score)
8368                 (or gnus-summary-default-score 0)))
8369   (save-excursion
8370     (set-buffer gnus-summary-buffer)
8371     (goto-char (point-min))
8372     (while
8373         (progn
8374           (and (< (gnus-summary-article-score) score)
8375                (gnus-summary-mark-article nil mark))
8376           (gnus-summary-find-next)))))
8377
8378 (defun gnus-summary-kill-below (&optional score)
8379   "Mark articles with score below SCORE as read."
8380   (interactive "P")
8381   (gnus-summary-mark-below score gnus-killed-mark))
8382
8383 (defun gnus-summary-clear-above (&optional score)
8384   "Clear all marks from articles with score above SCORE."
8385   (interactive "P")
8386   (gnus-summary-mark-above score gnus-unread-mark))
8387
8388 (defun gnus-summary-tick-above (&optional score)
8389   "Tick all articles with score above SCORE."
8390   (interactive "P")
8391   (gnus-summary-mark-above score gnus-ticked-mark))
8392
8393 (defun gnus-summary-mark-above (score mark)
8394   "Mark articles with score over SCORE with MARK."
8395   (interactive "P\ncMark: ")
8396   (setq score (if score
8397                   (prefix-numeric-value score)
8398                 (or gnus-summary-default-score 0)))
8399   (save-excursion
8400     (set-buffer gnus-summary-buffer)
8401     (goto-char (point-min))
8402     (while (and (progn
8403                   (when (> (gnus-summary-article-score) score)
8404                     (gnus-summary-mark-article nil mark))
8405                   t)
8406                 (gnus-summary-find-next)))))
8407
8408 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8409 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8410 (defun gnus-summary-limit-include-expunged (&optional no-error)
8411   "Display all the hidden articles that were expunged for low scores."
8412   (interactive)
8413   (let ((buffer-read-only nil))
8414     (let ((scored gnus-newsgroup-scored)
8415           headers h)
8416       (while scored
8417         (unless (gnus-summary-goto-subject (caar scored))
8418           (and (setq h (gnus-summary-article-header (caar scored)))
8419                (< (cdar scored) gnus-summary-expunge-below)
8420                (push h headers)))
8421         (setq scored (cdr scored)))
8422       (if (not headers)
8423           (when (not no-error)
8424             (error "No expunged articles hidden"))
8425         (goto-char (point-min))
8426         (gnus-summary-prepare-unthreaded (nreverse headers))
8427         (goto-char (point-min))
8428         (gnus-summary-position-point)
8429         t))))
8430
8431 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8432   "Mark all unread articles in this newsgroup as read.
8433 If prefix argument ALL is non-nil, ticked and dormant articles will
8434 also be marked as read.
8435 If QUIETLY is non-nil, no questions will be asked.
8436 If TO-HERE is non-nil, it should be a point in the buffer.  All
8437 articles before this point will be marked as read.
8438 Note that this function will only catch up the unread article
8439 in the current summary buffer limitation.
8440 The number of articles marked as read is returned."
8441   (interactive "P")
8442   (prog1
8443       (save-excursion
8444         (when (or quietly
8445                   (not gnus-interactive-catchup) ;Without confirmation?
8446                   gnus-expert-user
8447                   (gnus-y-or-n-p
8448                    (if all
8449                        "Mark absolutely all articles as read? "
8450                      "Mark all unread articles as read? ")))
8451           (if (and not-mark
8452                    (not gnus-newsgroup-adaptive)
8453                    (not gnus-newsgroup-auto-expire)
8454                    (not gnus-suppress-duplicates)
8455                    (or (not gnus-use-cache)
8456                        (eq gnus-use-cache 'passive)))
8457               (progn
8458                 (when all
8459                   (setq gnus-newsgroup-marked nil
8460                         gnus-newsgroup-dormant nil))
8461                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8462             ;; We actually mark all articles as canceled, which we
8463             ;; have to do when using auto-expiry or adaptive scoring.
8464             (gnus-summary-show-all-threads)
8465             (when (gnus-summary-first-subject (not all) t)
8466               (while (and
8467                       (if to-here (< (point) to-here) t)
8468                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8469                       (gnus-summary-find-next (not all) nil nil t))))
8470             (gnus-set-mode-line 'summary))
8471           t))
8472     (gnus-summary-position-point)))
8473
8474 (defun gnus-summary-catchup-to-here (&optional all)
8475   "Mark all unticked articles before the current one as read.
8476 If ALL is non-nil, also mark ticked and dormant articles as read."
8477   (interactive "P")
8478   (save-excursion
8479     (gnus-save-hidden-threads
8480       (let ((beg (point)))
8481         ;; We check that there are unread articles.
8482         (when (or all (gnus-summary-find-prev))
8483           (gnus-summary-catchup all t beg)))))
8484   (gnus-summary-position-point))
8485
8486 (defun gnus-summary-catchup-all (&optional quietly)
8487   "Mark all articles in this newsgroup as read."
8488   (interactive "P")
8489   (gnus-summary-catchup t quietly))
8490
8491 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8492   "Mark all unread articles in this group as read, then exit.
8493 If prefix argument ALL is non-nil, all articles are marked as read."
8494   (interactive "P")
8495   (when (gnus-summary-catchup all quietly nil 'fast)
8496     ;; Select next newsgroup or exit.
8497     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8498              (eq gnus-auto-select-next 'quietly))
8499         (gnus-summary-next-group nil)
8500       (gnus-summary-exit))))
8501
8502 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8503   "Mark all articles in this newsgroup as read, and then exit."
8504   (interactive "P")
8505   (gnus-summary-catchup-and-exit t quietly))
8506
8507 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8508   "Mark all articles in this group as read and select the next group.
8509 If given a prefix, mark all articles, unread as well as ticked, as
8510 read."
8511   (interactive "P")
8512   (save-excursion
8513     (gnus-summary-catchup all))
8514   (gnus-summary-next-group))
8515
8516 ;; Thread-based commands.
8517
8518 (defun gnus-summary-articles-in-thread (&optional article)
8519   "Return a list of all articles in the current thread.
8520 If ARTICLE is non-nil, return all articles in the thread that starts
8521 with that article."
8522   (let* ((article (or article (gnus-summary-article-number)))
8523          (data (gnus-data-find-list article))
8524          (top-level (gnus-data-level (car data)))
8525          (top-subject
8526           (cond ((null gnus-thread-operation-ignore-subject)
8527                  (gnus-simplify-subject-re
8528                   (mail-header-subject (gnus-data-header (car data)))))
8529                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8530                  (gnus-simplify-subject-fuzzy
8531                   (mail-header-subject (gnus-data-header (car data)))))
8532                 (t nil)))
8533          (end-point (save-excursion
8534                       (if (gnus-summary-go-to-next-thread)
8535                           (point) (point-max))))
8536          articles)
8537     (while (and data
8538                 (< (gnus-data-pos (car data)) end-point))
8539       (when (or (not top-subject)
8540                 (string= top-subject
8541                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8542                              (gnus-simplify-subject-fuzzy
8543                               (mail-header-subject
8544                                (gnus-data-header (car data))))
8545                            (gnus-simplify-subject-re
8546                             (mail-header-subject
8547                              (gnus-data-header (car data)))))))
8548         (push (gnus-data-number (car data)) articles))
8549       (unless (and (setq data (cdr data))
8550                    (> (gnus-data-level (car data)) top-level))
8551         (setq data nil)))
8552     ;; Return the list of articles.
8553     (nreverse articles)))
8554
8555 (defun gnus-summary-rethread-current ()
8556   "Rethread the thread the current article is part of."
8557   (interactive)
8558   (let* ((gnus-show-threads t)
8559          (article (gnus-summary-article-number))
8560          (id (mail-header-id (gnus-summary-article-header)))
8561          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8562     (unless id
8563       (error "No article on the current line"))
8564     (gnus-rebuild-thread id)
8565     (gnus-summary-goto-subject article)))
8566
8567 (defun gnus-summary-reparent-thread ()
8568   "Make the current article child of the marked (or previous) article.
8569
8570 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8571 is non-nil or the Subject: of both articles are the same."
8572   (interactive)
8573   (unless (not (gnus-group-read-only-p))
8574     (error "The current newsgroup does not support article editing"))
8575   (unless (<= (length gnus-newsgroup-processable) 1)
8576     (error "No more than one article may be marked"))
8577   (save-window-excursion
8578     (let ((gnus-article-buffer " *reparent*")
8579           (current-article (gnus-summary-article-number))
8580           ;; First grab the marked article, otherwise one line up.
8581           (parent-article (if (not (null gnus-newsgroup-processable))
8582                               (car gnus-newsgroup-processable)
8583                             (save-excursion
8584                               (if (eq (forward-line -1) 0)
8585                                   (gnus-summary-article-number)
8586                                 (error "Beginning of summary buffer"))))))
8587       (unless (not (eq current-article parent-article))
8588         (error "An article may not be self-referential"))
8589       (let ((message-id (mail-header-id
8590                          (gnus-summary-article-header parent-article))))
8591         (unless (and message-id (not (equal message-id "")))
8592           (error "No message-id in desired parent"))
8593         (gnus-with-article current-article
8594           (goto-char (point-min))
8595           (if (re-search-forward "^References: " nil t)
8596               (progn
8597                 (re-search-forward "^[^ \t]" nil t)
8598                 (forward-line -1)
8599                 (end-of-line)
8600                 (insert " " message-id))
8601             (insert "References: " message-id "\n")))
8602         (set-buffer gnus-summary-buffer)
8603         (gnus-summary-unmark-all-processable)
8604         (gnus-summary-update-article current-article)
8605         (gnus-summary-rethread-current)
8606         (gnus-message 3 "Article %d is now the child of article %d"
8607                       current-article parent-article)))))
8608
8609 (defun gnus-summary-toggle-threads (&optional arg)
8610   "Toggle showing conversation threads.
8611 If ARG is positive number, turn showing conversation threads on."
8612   (interactive "P")
8613   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8614     (setq gnus-show-threads
8615           (if (null arg) (not gnus-show-threads)
8616             (> (prefix-numeric-value arg) 0)))
8617     (gnus-summary-prepare)
8618     (gnus-summary-goto-subject current)
8619     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8620     (gnus-summary-position-point)))
8621
8622 (defun gnus-summary-show-all-threads ()
8623   "Show all threads."
8624   (interactive)
8625   (save-excursion
8626     (let ((buffer-read-only nil))
8627       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8628   (gnus-summary-position-point))
8629
8630 (defun gnus-summary-show-thread ()
8631   "Show thread subtrees.
8632 Returns nil if no thread was there to be shown."
8633   (interactive)
8634   (let ((buffer-read-only nil)
8635         (orig (point))
8636         ;; first goto end then to beg, to have point at beg after let
8637         (end (progn (end-of-line) (point)))
8638         (beg (progn (beginning-of-line) (point))))
8639     (prog1
8640         ;; Any hidden lines here?
8641         (search-forward "\r" end t)
8642       (subst-char-in-region beg end ?\^M ?\n t)
8643       (goto-char orig)
8644       (gnus-summary-position-point))))
8645
8646 (defun gnus-summary-hide-all-threads ()
8647   "Hide all thread subtrees."
8648   (interactive)
8649   (save-excursion
8650     (goto-char (point-min))
8651     (gnus-summary-hide-thread)
8652     (while (zerop (gnus-summary-next-thread 1 t))
8653       (gnus-summary-hide-thread)))
8654   (gnus-summary-position-point))
8655
8656 (defun gnus-summary-hide-thread ()
8657   "Hide thread subtrees.
8658 Returns nil if no threads were there to be hidden."
8659   (interactive)
8660   (let ((buffer-read-only nil)
8661         (start (point))
8662         (article (gnus-summary-article-number)))
8663     (goto-char start)
8664     ;; Go forward until either the buffer ends or the subthread
8665     ;; ends.
8666     (when (and (not (eobp))
8667                (or (zerop (gnus-summary-next-thread 1 t))
8668                    (goto-char (point-max))))
8669       (prog1
8670           (if (and (> (point) start)
8671                    (search-backward "\n" start t))
8672               (progn
8673                 (subst-char-in-region start (point) ?\n ?\^M)
8674                 (gnus-summary-goto-subject article))
8675             (goto-char start)
8676             nil)))))
8677
8678 (defun gnus-summary-go-to-next-thread (&optional previous)
8679   "Go to the same level (or less) next thread.
8680 If PREVIOUS is non-nil, go to previous thread instead.
8681 Return the article number moved to, or nil if moving was impossible."
8682   (let ((level (gnus-summary-thread-level))
8683         (way (if previous -1 1))
8684         (beg (point)))
8685     (forward-line way)
8686     (while (and (not (eobp))
8687                 (< level (gnus-summary-thread-level)))
8688       (forward-line way))
8689     (if (eobp)
8690         (progn
8691           (goto-char beg)
8692           nil)
8693       (setq beg (point))
8694       (prog1
8695           (gnus-summary-article-number)
8696         (goto-char beg)))))
8697
8698 (defun gnus-summary-next-thread (n &optional silent)
8699   "Go to the same level next N'th thread.
8700 If N is negative, search backward instead.
8701 Returns the difference between N and the number of skips actually
8702 done.
8703
8704 If SILENT, don't output messages."
8705   (interactive "p")
8706   (let ((backward (< n 0))
8707         (n (abs n)))
8708     (while (and (> n 0)
8709                 (gnus-summary-go-to-next-thread backward))
8710       (decf n))
8711     (unless silent
8712       (gnus-summary-position-point))
8713     (when (and (not silent) (/= 0 n))
8714       (gnus-message 7 "No more threads"))
8715     n))
8716
8717 (defun gnus-summary-prev-thread (n)
8718   "Go to the same level previous N'th thread.
8719 Returns the difference between N and the number of skips actually
8720 done."
8721   (interactive "p")
8722   (gnus-summary-next-thread (- n)))
8723
8724 (defun gnus-summary-go-down-thread ()
8725   "Go down one level in the current thread."
8726   (let ((children (gnus-summary-article-children)))
8727     (when children
8728       (gnus-summary-goto-subject (car children)))))
8729
8730 (defun gnus-summary-go-up-thread ()
8731   "Go up one level in the current thread."
8732   (let ((parent (gnus-summary-article-parent)))
8733     (when parent
8734       (gnus-summary-goto-subject parent))))
8735
8736 (defun gnus-summary-down-thread (n)
8737   "Go down thread N steps.
8738 If N is negative, go up instead.
8739 Returns the difference between N and how many steps down that were
8740 taken."
8741   (interactive "p")
8742   (let ((up (< n 0))
8743         (n (abs n)))
8744     (while (and (> n 0)
8745                 (if up (gnus-summary-go-up-thread)
8746                   (gnus-summary-go-down-thread)))
8747       (setq n (1- n)))
8748     (gnus-summary-position-point)
8749     (when (/= 0 n)
8750       (gnus-message 7 "Can't go further"))
8751     n))
8752
8753 (defun gnus-summary-up-thread (n)
8754   "Go up thread N steps.
8755 If N is negative, go up instead.
8756 Returns the difference between N and how many steps down that were
8757 taken."
8758   (interactive "p")
8759   (gnus-summary-down-thread (- n)))
8760
8761 (defun gnus-summary-top-thread ()
8762   "Go to the top of the thread."
8763   (interactive)
8764   (while (gnus-summary-go-up-thread))
8765   (gnus-summary-article-number))
8766
8767 (defun gnus-summary-kill-thread (&optional unmark)
8768   "Mark articles under current thread as read.
8769 If the prefix argument is positive, remove any kinds of marks.
8770 If the prefix argument is negative, tick articles instead."
8771   (interactive "P")
8772   (when unmark
8773     (setq unmark (prefix-numeric-value unmark)))
8774   (let ((articles (gnus-summary-articles-in-thread)))
8775     (save-excursion
8776       ;; Expand the thread.
8777       (gnus-summary-show-thread)
8778       ;; Mark all the articles.
8779       (while articles
8780         (gnus-summary-goto-subject (car articles))
8781         (cond ((null unmark)
8782                (gnus-summary-mark-article-as-read gnus-killed-mark))
8783               ((> unmark 0)
8784                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8785               (t
8786                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8787         (setq articles (cdr articles))))
8788     ;; Hide killed subtrees.
8789     (and (null unmark)
8790          gnus-thread-hide-killed
8791          (gnus-summary-hide-thread))
8792     ;; If marked as read, go to next unread subject.
8793     (when (null unmark)
8794       ;; Go to next unread subject.
8795       (gnus-summary-next-subject 1 t)))
8796   (gnus-set-mode-line 'summary))
8797
8798 ;; Summary sorting commands
8799
8800 (defun gnus-summary-sort-by-number (&optional reverse)
8801   "Sort the summary buffer by article number.
8802 Argument REVERSE means reverse order."
8803   (interactive "P")
8804   (gnus-summary-sort 'number reverse))
8805
8806 (defun gnus-summary-sort-by-author (&optional reverse)
8807   "Sort the summary buffer by author name alphabetically.
8808 If case-fold-search is non-nil, case of letters is ignored.
8809 Argument REVERSE means reverse order."
8810   (interactive "P")
8811   (gnus-summary-sort 'author reverse))
8812
8813 (defun gnus-summary-sort-by-subject (&optional reverse)
8814   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8815 If case-fold-search is non-nil, case of letters is ignored.
8816 Argument REVERSE means reverse order."
8817   (interactive "P")
8818   (gnus-summary-sort 'subject reverse))
8819
8820 (defun gnus-summary-sort-by-date (&optional reverse)
8821   "Sort the summary buffer by date.
8822 Argument REVERSE means reverse order."
8823   (interactive "P")
8824   (gnus-summary-sort 'date reverse))
8825
8826 (defun gnus-summary-sort-by-score (&optional reverse)
8827   "Sort the summary buffer by score.
8828 Argument REVERSE means reverse order."
8829   (interactive "P")
8830   (gnus-summary-sort 'score reverse))
8831
8832 (defun gnus-summary-sort-by-lines (&optional reverse)
8833   "Sort the summary buffer by the number of lines.
8834 Argument REVERSE means reverse order."
8835   (interactive "P")
8836   (gnus-summary-sort 'lines reverse))
8837
8838 (defun gnus-summary-sort-by-chars (&optional reverse)
8839   "Sort the summary buffer by article length.
8840 Argument REVERSE means reverse order."
8841   (interactive "P")
8842   (gnus-summary-sort 'chars reverse))   
8843
8844 (defun gnus-summary-sort (predicate reverse)
8845   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8846   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8847          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8848          (gnus-thread-sort-functions
8849           (if (not reverse)
8850               thread
8851             `(lambda (t1 t2)
8852                (,thread t2 t1))))
8853          (gnus-article-sort-functions
8854           (if (not reverse)
8855               article
8856             `(lambda (t1 t2)
8857                (,article t2 t1))))
8858          (buffer-read-only)
8859          (gnus-summary-prepare-hook nil))
8860     ;; We do the sorting by regenerating the threads.
8861     (gnus-summary-prepare)
8862     ;; Hide subthreads if needed.
8863     (when (and gnus-show-threads gnus-thread-hide-subtree)
8864       (gnus-summary-hide-all-threads))))
8865
8866 ;; Summary saving commands.
8867
8868 (defun gnus-summary-save-article (&optional n not-saved)
8869   "Save the current article using the default saver function.
8870 If N is a positive number, save the N next articles.
8871 If N is a negative number, save the N previous articles.
8872 If N is nil and any articles have been marked with the process mark,
8873 save those articles instead.
8874 The variable `gnus-default-article-saver' specifies the saver function."
8875   (interactive "P")
8876   (let* ((articles (gnus-summary-work-articles n))
8877          (save-buffer (save-excursion
8878                         (nnheader-set-temp-buffer " *Gnus Save*")))
8879          (num (length articles))
8880          header file)
8881     (dolist (article articles)
8882       (setq header (gnus-summary-article-header article))
8883       (if (not (vectorp header))
8884           ;; This is a pseudo-article.
8885           (if (assq 'name header)
8886               (gnus-copy-file (cdr (assq 'name header)))
8887             (gnus-message 1 "Article %d is unsaveable" article))
8888         ;; This is a real article.
8889         (save-window-excursion
8890           (gnus-summary-select-article t nil nil article))
8891         (save-excursion
8892           (set-buffer save-buffer)
8893           (erase-buffer)
8894           (insert-buffer-substring gnus-original-article-buffer))
8895         (setq file (gnus-article-save save-buffer file num))
8896         (gnus-summary-remove-process-mark article)
8897         (unless not-saved
8898           (gnus-summary-set-saved-mark article))))
8899     (gnus-kill-buffer save-buffer)
8900     (gnus-summary-position-point)
8901     (gnus-set-mode-line 'summary)
8902     n))
8903
8904 (defun gnus-summary-pipe-output (&optional arg)
8905   "Pipe the current article to a subprocess.
8906 If N is a positive number, pipe the N next articles.
8907 If N is a negative number, pipe the N previous articles.
8908 If N is nil and any articles have been marked with the process mark,
8909 pipe those articles instead."
8910   (interactive "P")
8911   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8912     (gnus-summary-save-article arg t))
8913   (gnus-configure-windows 'pipe))
8914
8915 (defun gnus-summary-save-article-mail (&optional arg)
8916   "Append the current article to an mail file.
8917 If N is a positive number, save the N next articles.
8918 If N is a negative number, save the N previous articles.
8919 If N is nil and any articles have been marked with the process mark,
8920 save those articles instead."
8921   (interactive "P")
8922   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8923     (gnus-summary-save-article arg)))
8924
8925 (defun gnus-summary-save-article-rmail (&optional arg)
8926   "Append the current article to an rmail file.
8927 If N is a positive number, save the N next articles.
8928 If N is a negative number, save the N previous articles.
8929 If N is nil and any articles have been marked with the process mark,
8930 save those articles instead."
8931   (interactive "P")
8932   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8933     (gnus-summary-save-article arg)))
8934
8935 (defun gnus-summary-save-article-file (&optional arg)
8936   "Append the current article to a file.
8937 If N is a positive number, save the N next articles.
8938 If N is a negative number, save the N previous articles.
8939 If N is nil and any articles have been marked with the process mark,
8940 save those articles instead."
8941   (interactive "P")
8942   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8943     (gnus-summary-save-article arg)))
8944
8945 (defun gnus-summary-write-article-file (&optional arg)
8946   "Write the current article to a file, deleting the previous file.
8947 If N is a positive number, save the N next articles.
8948 If N is a negative number, save the N previous articles.
8949 If N is nil and any articles have been marked with the process mark,
8950 save those articles instead."
8951   (interactive "P")
8952   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8953     (gnus-summary-save-article arg)))
8954
8955 (defun gnus-summary-save-article-body-file (&optional arg)
8956   "Append the current article body to a file.
8957 If N is a positive number, save the N next articles.
8958 If N is a negative number, save the N previous articles.
8959 If N is nil and any articles have been marked with the process mark,
8960 save those articles instead."
8961   (interactive "P")
8962   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8963     (gnus-summary-save-article arg)))
8964
8965 (defun gnus-summary-pipe-message (program)
8966   "Pipe the current article through PROGRAM."
8967   (interactive "sProgram: ")
8968   (gnus-summary-select-article)
8969   (let ((mail-header-separator ""))
8970     (gnus-eval-in-buffer-window gnus-article-buffer
8971       (save-restriction
8972         (widen)
8973         (let ((start (window-start))
8974               buffer-read-only)
8975           (message-pipe-buffer-body program)
8976           (set-window-start (get-buffer-window (current-buffer)) start))))))
8977
8978 (defun gnus-get-split-value (methods)
8979   "Return a value based on the split METHODS."
8980   (let (split-name method result match)
8981     (when methods
8982       (save-excursion
8983         (set-buffer gnus-original-article-buffer)
8984         (save-restriction
8985           (nnheader-narrow-to-headers)
8986           (while methods
8987             (goto-char (point-min))
8988             (setq method (pop methods))
8989             (setq match (car method))
8990             (when (cond
8991                    ((stringp match)
8992                     ;; Regular expression.
8993                     (ignore-errors
8994                       (re-search-forward match nil t)))
8995                    ((gnus-functionp match)
8996                     ;; Function.
8997                     (save-restriction
8998                       (widen)
8999                       (setq result (funcall match gnus-newsgroup-name))))
9000                    ((consp match)
9001                     ;; Form.
9002                     (save-restriction
9003                       (widen)
9004                       (setq result (eval match)))))
9005               (setq split-name (append (cdr method) split-name))
9006               (cond ((stringp result)
9007                      (push (expand-file-name
9008                             result gnus-article-save-directory)
9009                            split-name))
9010                     ((consp result)
9011                      (setq split-name (append result split-name)))))))))
9012     (nreverse split-name)))
9013
9014 (defun gnus-valid-move-group-p (group)
9015   (and (boundp group)
9016        (symbol-name group)
9017        (symbol-value group)
9018        (gnus-get-function (gnus-find-method-for-group
9019                            (symbol-name group)) 'request-accept-article t)))
9020
9021 (defun gnus-read-move-group-name (prompt default articles prefix)
9022   "Read a group name."
9023   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9024          (minibuffer-confirm-incomplete nil) ; XEmacs
9025          (prom
9026           (format "%s %s to:"
9027                   prompt
9028                   (if (> (length articles) 1)
9029                       (format "these %d articles" (length articles))
9030                     "this article")))
9031          (to-newsgroup
9032           (cond
9033            ((null split-name)
9034             (gnus-completing-read default prom
9035                                   gnus-active-hashtb
9036                                   'gnus-valid-move-group-p
9037                                   nil prefix
9038                                   'gnus-group-history))
9039            ((= 1 (length split-name))
9040             (gnus-completing-read (car split-name) prom
9041                                   gnus-active-hashtb
9042                                   'gnus-valid-move-group-p
9043                                   nil nil
9044                                   'gnus-group-history))
9045            (t
9046             (gnus-completing-read nil prom
9047                                   (mapcar (lambda (el) (list el))
9048                                           (nreverse split-name))
9049                                   nil nil nil
9050                                   'gnus-group-history))))
9051          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9052     (when to-newsgroup
9053       (if (or (string= to-newsgroup "")
9054               (string= to-newsgroup prefix))
9055           (setq to-newsgroup default))
9056       (unless to-newsgroup
9057         (error "No group name entered"))
9058       (or (gnus-active to-newsgroup)
9059           (gnus-activate-group to-newsgroup nil nil to-method)
9060           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9061                                      to-newsgroup))
9062               (or (and (gnus-request-create-group to-newsgroup to-method)
9063                        (gnus-activate-group
9064                         to-newsgroup nil nil to-method)
9065                        (gnus-subscribe-group to-newsgroup))
9066                   (error "Couldn't create group %s" to-newsgroup)))
9067           (error "No such group: %s" to-newsgroup)))
9068     to-newsgroup))
9069
9070 (defun gnus-summary-save-parts (type dir n &optional reverse)
9071   "Save parts matching TYPE to DIR.
9072 If REVERSE, save parts that do not match TYPE."
9073   (interactive
9074    (list (read-string "Save parts of type: " "image/.*")
9075          (read-file-name "Save to directory: " nil nil t)
9076          current-prefix-arg))
9077   (gnus-summary-iterate n
9078     (let ((gnus-display-mime-function nil)
9079           (gnus-inhibit-treatment t))
9080       (gnus-summary-select-article))
9081     (save-excursion
9082       (set-buffer gnus-article-buffer)
9083       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9084         (when handles
9085           (gnus-summary-save-parts-1 type dir handles reverse)
9086           (mm-destroy-parts handles))))))
9087
9088 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9089   (if (stringp (car handle))
9090       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9091               (cdr handle))
9092     (when (if reverse
9093               (not (string-match type (mm-handle-media-type handle)))
9094             (string-match type (mm-handle-media-type handle)))
9095       (let ((file (expand-file-name
9096                    (file-name-nondirectory
9097                     (or
9098                      (mail-content-type-get
9099                       (mm-handle-disposition handle) 'filename)
9100                      (concat gnus-newsgroup-name "." gnus-current-article)))
9101                    dir)))
9102         (unless (file-exists-p file)
9103           (mm-save-part-to-file handle file))))))
9104
9105 ;; Summary extract commands
9106
9107 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9108   (let ((buffer-read-only nil)
9109         (article (gnus-summary-article-number))
9110         after-article b e)
9111     (unless (gnus-summary-goto-subject article)
9112       (error "No such article: %d" article))
9113     (gnus-summary-position-point)
9114     ;; If all commands are to be bunched up on one line, we collect
9115     ;; them here.
9116     (unless gnus-view-pseudos-separately
9117       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9118             files action)
9119         (while ps
9120           (setq action (cdr (assq 'action (car ps))))
9121           (setq files (list (cdr (assq 'name (car ps)))))
9122           (while (and ps (cdr ps)
9123                       (string= (or action "1")
9124                                (or (cdr (assq 'action (cadr ps))) "2")))
9125             (push (cdr (assq 'name (cadr ps))) files)
9126             (setcdr ps (cddr ps)))
9127           (when files
9128             (when (not (string-match "%s" action))
9129               (push " " files))
9130             (push " " files)
9131             (when (assq 'execute (car ps))
9132               (setcdr (assq 'execute (car ps))
9133                       (funcall (if (string-match "%s" action)
9134                                    'format 'concat)
9135                                action
9136                                (mapconcat
9137                                 (lambda (f)
9138                                   (if (equal f " ")
9139                                       f
9140                                     (mm-quote-arg f)))
9141                                 files " ")))))
9142           (setq ps (cdr ps)))))
9143     (if (and gnus-view-pseudos (not not-view))
9144         (while pslist
9145           (when (assq 'execute (car pslist))
9146             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9147                                   (eq gnus-view-pseudos 'not-confirm)))
9148           (setq pslist (cdr pslist)))
9149       (save-excursion
9150         (while pslist
9151           (setq after-article (or (cdr (assq 'article (car pslist)))
9152                                   (gnus-summary-article-number)))
9153           (gnus-summary-goto-subject after-article)
9154           (forward-line 1)
9155           (setq b (point))
9156           (insert "    " (file-name-nondirectory
9157                           (cdr (assq 'name (car pslist))))
9158                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9159           (setq e (point))
9160           (forward-line -1)             ; back to `b'
9161           (gnus-add-text-properties
9162            b (1- e) (list 'gnus-number gnus-reffed-article-number
9163                           gnus-mouse-face-prop gnus-mouse-face))
9164           (gnus-data-enter
9165            after-article gnus-reffed-article-number
9166            gnus-unread-mark b (car pslist) 0 (- e b))
9167           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9168           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9169           (setq pslist (cdr pslist)))))))
9170
9171 (defun gnus-pseudos< (p1 p2)
9172   (let ((c1 (cdr (assq 'action p1)))
9173         (c2 (cdr (assq 'action p2))))
9174     (and c1 c2 (string< c1 c2))))
9175
9176 (defun gnus-request-pseudo-article (props)
9177   (cond ((assq 'execute props)
9178          (gnus-execute-command (cdr (assq 'execute props)))))
9179   (let ((gnus-current-article (gnus-summary-article-number)))
9180     (gnus-run-hooks 'gnus-mark-article-hook)))
9181
9182 (defun gnus-execute-command (command &optional automatic)
9183   (save-excursion
9184     (gnus-article-setup-buffer)
9185     (set-buffer gnus-article-buffer)
9186     (setq buffer-read-only nil)
9187     (let ((command (if automatic command
9188                      (read-string "Command: " (cons command 0)))))
9189       (erase-buffer)
9190       (insert "$ " command "\n\n")
9191       (if gnus-view-pseudo-asynchronously
9192           (start-process "gnus-execute" (current-buffer) shell-file-name
9193                          shell-command-switch command)
9194         (call-process shell-file-name nil t nil
9195                       shell-command-switch command)))))
9196
9197 ;; Summary kill commands.
9198
9199 (defun gnus-summary-edit-global-kill (article)
9200   "Edit the \"global\" kill file."
9201   (interactive (list (gnus-summary-article-number)))
9202   (gnus-group-edit-global-kill article))
9203
9204 (defun gnus-summary-edit-local-kill ()
9205   "Edit a local kill file applied to the current newsgroup."
9206   (interactive)
9207   (setq gnus-current-headers (gnus-summary-article-header))
9208   (gnus-group-edit-local-kill
9209    (gnus-summary-article-number) gnus-newsgroup-name))
9210
9211 ;;; Header reading.
9212
9213 (defun gnus-read-header (id &optional header)
9214   "Read the headers of article ID and enter them into the Gnus system."
9215   (let ((group gnus-newsgroup-name)
9216         (gnus-override-method
9217          (or
9218           gnus-override-method
9219           (and (gnus-news-group-p gnus-newsgroup-name)
9220                (car (gnus-refer-article-methods)))))
9221         where)
9222     ;; First we check to see whether the header in question is already
9223     ;; fetched.
9224     (if (stringp id)
9225         ;; This is a Message-ID.
9226         (setq header (or header (gnus-id-to-header id)))
9227       ;; This is an article number.
9228       (setq header (or header (gnus-summary-article-header id))))
9229     (if (and header
9230              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9231         ;; We have found the header.
9232         header
9233       ;; If this is a sparse article, we have to nix out its
9234       ;; previous entry in the thread hashtb.
9235       (when (and header
9236                  (gnus-summary-article-sparse-p (mail-header-number header)))
9237         (let* ((parent (gnus-parent-id (mail-header-references header)))
9238                (thread (and parent (gnus-id-to-thread parent))))
9239           (when thread
9240             (delq (assq header thread) thread))))
9241       ;; We have to really fetch the header to this article.
9242       (save-excursion
9243         (set-buffer nntp-server-buffer)
9244         (when (setq where (gnus-request-head id group))
9245           (nnheader-fold-continuation-lines)
9246           (goto-char (point-max))
9247           (insert ".\n")
9248           (goto-char (point-min))
9249           (insert "211 ")
9250           (princ (cond
9251                   ((numberp id) id)
9252                   ((cdr where) (cdr where))
9253                   (header (mail-header-number header))
9254                   (t gnus-reffed-article-number))
9255                  (current-buffer))
9256           (insert " Article retrieved.\n"))
9257         (if (or (not where)
9258                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9259             ()                          ; Malformed head.
9260           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9261             (when (and (stringp id)
9262                        (not (string= (gnus-group-real-name group)
9263                                      (car where))))
9264               ;; If we fetched by Message-ID and the article came
9265               ;; from a different group, we fudge some bogus article
9266               ;; numbers for this article.
9267               (mail-header-set-number header gnus-reffed-article-number))
9268             (save-excursion
9269               (set-buffer gnus-summary-buffer)
9270               (decf gnus-reffed-article-number)
9271               (gnus-remove-header (mail-header-number header))
9272               (push header gnus-newsgroup-headers)
9273               (setq gnus-current-headers header)
9274               (push (mail-header-number header) gnus-newsgroup-limit)))
9275           header)))))
9276
9277 (defun gnus-remove-header (number)
9278   "Remove header NUMBER from `gnus-newsgroup-headers'."
9279   (if (and gnus-newsgroup-headers
9280            (= number (mail-header-number (car gnus-newsgroup-headers))))
9281       (pop gnus-newsgroup-headers)
9282     (let ((headers gnus-newsgroup-headers))
9283       (while (and (cdr headers)
9284                   (not (= number (mail-header-number (cadr headers)))))
9285         (pop headers))
9286       (when (cdr headers)
9287         (setcdr headers (cddr headers))))))
9288
9289 ;;;
9290 ;;; summary highlights
9291 ;;;
9292
9293 (defun gnus-highlight-selected-summary ()
9294   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9295   ;; Highlight selected article in summary buffer
9296   (when gnus-summary-selected-face
9297     (save-excursion
9298       (let* ((beg (progn (beginning-of-line) (point)))
9299              (end (progn (end-of-line) (point)))
9300              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9301              (from (if (get-text-property beg gnus-mouse-face-prop)
9302                        beg
9303                      (or (next-single-property-change
9304                           beg gnus-mouse-face-prop nil end)
9305                          beg)))
9306              (to
9307               (if (= from end)
9308                   (- from 2)
9309                 (or (next-single-property-change
9310                      from gnus-mouse-face-prop nil end)
9311                     end))))
9312         ;; If no mouse-face prop on line we will have to = from = end,
9313         ;; so we highlight the entire line instead.
9314         (when (= (+ to 2) from)
9315           (setq from beg)
9316           (setq to end))
9317         (if gnus-newsgroup-selected-overlay
9318             ;; Move old overlay.
9319             (gnus-move-overlay
9320              gnus-newsgroup-selected-overlay from to (current-buffer))
9321           ;; Create new overlay.
9322           (gnus-overlay-put
9323            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9324            'face gnus-summary-selected-face))))))
9325
9326 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9327 (defun gnus-summary-highlight-line ()
9328   "Highlight current line according to `gnus-summary-highlight'."
9329   (let* ((list gnus-summary-highlight)
9330          (p (point))
9331          (end (progn (end-of-line) (point)))
9332          ;; now find out where the line starts and leave point there.
9333          (beg (progn (beginning-of-line) (point)))
9334          (article (gnus-summary-article-number))
9335          (score (or (cdr (assq (or article gnus-current-article)
9336                                gnus-newsgroup-scored))
9337                     gnus-summary-default-score 0))
9338          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9339          (inhibit-read-only t))
9340     ;; Eval the cars of the lists until we find a match.
9341     (let ((default gnus-summary-default-score))
9342       (while (and list
9343                   (not (eval (caar list))))
9344         (setq list (cdr list))))
9345     (let ((face (cdar list)))
9346       (unless (eq face (get-text-property beg 'face))
9347         (gnus-put-text-property-excluding-characters-with-faces
9348          beg end 'face
9349          (setq face (if (boundp face) (symbol-value face) face)))
9350         (when gnus-summary-highlight-line-function
9351           (funcall gnus-summary-highlight-line-function article face))))
9352     (goto-char p)))
9353
9354 (defun gnus-update-read-articles (group unread &optional compute)
9355   "Update the list of read articles in GROUP."
9356   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9357          (entry (gnus-gethash group gnus-newsrc-hashtb))
9358          (info (nth 2 entry))
9359          (prev 1)
9360          (unread (sort (copy-sequence unread) '<))
9361          read)
9362     (if (or (not info) (not active))
9363         ;; There is no info on this group if it was, in fact,
9364         ;; killed.  Gnus stores no information on killed groups, so
9365         ;; there's nothing to be done.
9366         ;; One could store the information somewhere temporarily,
9367         ;; perhaps...  Hmmm...
9368         ()
9369       ;; Remove any negative articles numbers.
9370       (while (and unread (< (car unread) 0))
9371         (setq unread (cdr unread)))
9372       ;; Remove any expired article numbers
9373       (while (and unread (< (car unread) (car active)))
9374         (setq unread (cdr unread)))
9375       ;; Compute the ranges of read articles by looking at the list of
9376       ;; unread articles.
9377       (while unread
9378         (when (/= (car unread) prev)
9379           (push (if (= prev (1- (car unread))) prev
9380                   (cons prev (1- (car unread))))
9381                 read))
9382         (setq prev (1+ (car unread)))
9383         (setq unread (cdr unread)))
9384       (when (<= prev (cdr active))
9385         (push (cons prev (cdr active)) read))
9386       (setq read (if (> (length read) 1) (nreverse read) read))
9387       (if compute
9388           read
9389         (save-excursion
9390           (set-buffer gnus-group-buffer)
9391           (gnus-undo-register
9392             `(progn
9393                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9394                (gnus-info-set-read ',info ',(gnus-info-read info))
9395                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9396                (gnus-group-update-group ,group t))))
9397         ;; Propagate the read marks to the backend.
9398         (if (gnus-check-backend-function 'request-set-mark group)
9399             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9400                   (add (gnus-remove-from-range read (gnus-info-read info))))
9401               (when (or add del)
9402                 (unless (gnus-check-group group)
9403                   (error "Can't open server for %s" group))
9404                 (gnus-request-set-mark
9405                  group (delq nil (list (if add (list add 'add '(read)))
9406                                        (if del (list del 'del '(read)))))))))
9407         ;; Enter this list into the group info.
9408         (gnus-info-set-read info read)
9409         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9410         (gnus-get-unread-articles-in-group info (gnus-active group))
9411         t))))
9412
9413 (defun gnus-offer-save-summaries ()
9414   "Offer to save all active summary buffers."
9415   (save-excursion
9416     (let ((buflist (buffer-list))
9417           buffers bufname)
9418       ;; Go through all buffers and find all summaries.
9419       (while buflist
9420         (and (setq bufname (buffer-name (car buflist)))
9421              (string-match "Summary" bufname)
9422              (save-excursion
9423                (set-buffer bufname)
9424                ;; We check that this is, indeed, a summary buffer.
9425                (and (eq major-mode 'gnus-summary-mode)
9426                     ;; Also make sure this isn't bogus.
9427                     gnus-newsgroup-prepared
9428                     ;; Also make sure that this isn't a dead summary buffer.
9429                     (not gnus-dead-summary-mode)))
9430              (push bufname buffers))
9431         (setq buflist (cdr buflist)))
9432       ;; Go through all these summary buffers and offer to save them.
9433       (when buffers
9434         (map-y-or-n-p
9435          "Update summary buffer %s? "
9436          (lambda (buf)
9437            (switch-to-buffer buf)
9438            (gnus-summary-exit))
9439          buffers)))))
9440
9441 (defun gnus-summary-setup-default-charset ()
9442   "Setup newsgroup default charset."
9443   (if (equal gnus-newsgroup-name "nndraft:drafts")
9444       (setq gnus-newsgroup-charset nil)
9445     (let* ((name (and gnus-newsgroup-name
9446                       (gnus-group-real-name gnus-newsgroup-name)))
9447            (ignored-charsets 
9448             (or gnus-newsgroup-ephemeral-ignored-charsets
9449                 (append
9450                  (and gnus-newsgroup-name
9451                       (or (gnus-group-find-parameter gnus-newsgroup-name
9452                                                      'ignored-charsets t)
9453                           (let ((alist gnus-group-ignored-charsets-alist)
9454                                 elem (charsets nil))
9455                             (while (setq elem (pop alist))
9456                               (when (and name
9457                                          (string-match (car elem) name))
9458                                 (setq alist nil
9459                                       charsets (cdr elem))))
9460                             charsets)))
9461                  gnus-newsgroup-ignored-charsets))))
9462       (setq gnus-newsgroup-charset
9463             (or gnus-newsgroup-ephemeral-charset
9464                 (and gnus-newsgroup-name
9465                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9466                          (let ((alist gnus-group-charset-alist)
9467                                elem charset)
9468                            (while (setq elem (pop alist))
9469                              (when (and name
9470                                         (string-match (car elem) name))
9471                                (setq alist nil
9472                                      charset (cadr elem))))
9473                            charset)))
9474                 gnus-default-charset))
9475       (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9476            ignored-charsets))))
9477
9478 ;;;
9479 ;;; Mime Commands
9480 ;;;
9481
9482 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9483   "Display the current article buffer fully MIME-buttonized.
9484 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9485 treated as multipart/mixed."
9486   (interactive "P")
9487   (require 'gnus-art)
9488   (let ((gnus-unbuttonized-mime-types nil)
9489         (gnus-mime-display-multipart-as-mixed show-all-parts))
9490     (gnus-summary-show-article)))
9491
9492 (defun gnus-summary-repair-multipart (article)
9493   "Add a Content-Type header to a multipart article without one."
9494   (interactive (list (gnus-summary-article-number)))
9495   (gnus-with-article article
9496     (message-narrow-to-head)
9497     (goto-char (point-max))
9498     (widen)
9499     (when (search-forward "\n--" nil t)
9500       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9501         (message-narrow-to-head)
9502         (message-remove-header "Mime-Version")
9503         (message-remove-header "Content-Type")
9504         (goto-char (point-max))
9505         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9506                         separator))
9507         (insert "Mime-Version: 1.0\n")
9508         (widen))))
9509   (let (gnus-mark-article-hook)
9510     (gnus-summary-select-article t t nil article)))
9511
9512 (defun gnus-summary-toggle-display-buttonized ()
9513   "Toggle the buttonizing of the article buffer."
9514   (interactive)
9515   (require 'gnus-art)
9516   (if (setq gnus-inhibit-mime-unbuttonizing
9517             (not gnus-inhibit-mime-unbuttonizing))
9518       (let ((gnus-unbuttonized-mime-types nil))
9519         (gnus-summary-show-article))
9520     (gnus-summary-show-article)))
9521
9522 ;;;
9523 ;;; with article
9524 ;;;
9525
9526 (defmacro gnus-with-article (article &rest forms)
9527   "Select ARTICLE and perform FORMS in the original article buffer.
9528 Then replace the article with the result."
9529   `(progn
9530      ;; We don't want the article to be marked as read.
9531      (let (gnus-mark-article-hook)
9532        (gnus-summary-select-article t t nil ,article))
9533      (set-buffer gnus-original-article-buffer)
9534      ,@forms
9535      (if (not (gnus-check-backend-function
9536                'request-replace-article (car gnus-article-current)))
9537          (gnus-message 5 "Read-only group; not replacing")
9538        (unless (gnus-request-replace-article
9539                 ,article (car gnus-article-current)
9540                 (current-buffer) t)
9541          (error "Couldn't replace article")))
9542      ;; The cache and backlog have to be flushed somewhat.
9543      (when gnus-keep-backlog
9544        (gnus-backlog-remove-article
9545         (car gnus-article-current) (cdr gnus-article-current)))
9546      (when gnus-use-cache
9547        (gnus-cache-update-article
9548         (car gnus-article-current) (cdr gnus-article-current)))))
9549
9550 (put 'gnus-with-article 'lisp-indent-function 1)
9551 (put 'gnus-with-article 'edebug-form-spec '(form body))
9552
9553 ;;;
9554 ;;; Generic summary marking commands
9555 ;;;
9556
9557 (defvar gnus-summary-marking-alist
9558   '((read gnus-del-mark "d")
9559     (unread gnus-unread-mark "u")
9560     (ticked gnus-ticked-mark "!")
9561     (dormant gnus-dormant-mark "?")
9562     (expirable gnus-expirable-mark "e"))
9563   "An alist of names/marks/keystrokes.")
9564
9565 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9566 (defvar gnus-summary-mark-map)
9567
9568 (defun gnus-summary-make-all-marking-commands ()
9569   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9570   (dolist (elem gnus-summary-marking-alist)
9571     (apply 'gnus-summary-make-marking-command elem)))
9572
9573 (defun gnus-summary-make-marking-command (name mark keystroke)
9574   (let ((map (make-sparse-keymap)))
9575     (define-key gnus-summary-generic-mark-map keystroke map)
9576     (dolist (lway `((next "next" next nil "n")
9577                     (next-unread "next unread" next t "N")
9578                     (prev "previous" prev nil "p")
9579                     (prev-unread "previous unread" prev t "P")
9580                     (nomove "" nil nil ,keystroke)))
9581       (let ((func (gnus-summary-make-marking-command-1
9582                    mark (car lway) lway name)))
9583         (setq func (eval func))
9584         (define-key map (nth 4 lway) func)))))
9585       
9586 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9587   `(defun ,(intern
9588             (format "gnus-summary-put-mark-as-%s%s"
9589                     name (if (eq way 'nomove)
9590                              ""
9591                            (concat "-" (symbol-name way)))))
9592      (n)
9593      ,(format
9594        "Mark the current article as %s%s.
9595 If N, the prefix, then repeat N times.
9596 If N is negative, move in reverse order.
9597 The difference between N and the actual number of articles marked is
9598 returned."
9599        name (cadr lway))
9600      (interactive "p")
9601      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9602     
9603 (defun gnus-summary-generic-mark (n mark move unread)
9604   "Mark N articles with MARK."
9605   (unless (eq major-mode 'gnus-summary-mode)
9606     (error "This command can only be used in the summary buffer"))
9607   (gnus-summary-show-thread)
9608   (let ((nummove
9609          (cond
9610           ((eq move 'next) 1)
9611           ((eq move 'prev) -1)
9612           (t 0))))
9613     (if (zerop nummove)
9614         (setq n 1)
9615       (when (< n 0)
9616         (setq n (abs n)
9617               nummove (* -1 nummove))))
9618     (while (and (> n 0)
9619                 (gnus-summary-mark-article nil mark)
9620                 (zerop (gnus-summary-next-subject nummove unread t)))
9621       (setq n (1- n)))
9622     (when (/= 0 n)
9623       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9624     (gnus-summary-recenter)
9625     (gnus-summary-position-point)
9626     (gnus-set-mode-line 'summary)
9627     n))
9628
9629 (gnus-summary-make-all-marking-commands)
9630
9631 (gnus-ems-redefine)
9632
9633 (provide 'gnus-sum)
9634
9635 (run-hooks 'gnus-sum-load-hook)
9636
9637 ;;; gnus-sum.el ends here