Sync up with Pterodactyl Gnus v0.92.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
7 ;; Keywords: mail, news, MIME
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-group)
34 (require 'gnus-spec)
35 (require 'gnus-range)
36 (require 'gnus-int)
37 (require 'gnus-undo)
38 (require 'gnus-util)
39 (require 'mime-view)
40
41 (eval-when-compile
42   (require 'mime-play)
43   (require 'static))
44
45 ;; Avoid byte-compile warnings.
46 (defvar gnus-decode-encoded-word-function)
47
48 (eval-and-compile
49   (autoload 'gnus-cache-articles-in-group "gnus-cache"))
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
53
54 (defcustom gnus-kill-summary-on-exit t
55   "*If non-nil, kill the summary buffer when you exit from it.
56 If nil, the summary will become a \"*Dead Summary*\" buffer, and
57 it will be killed sometime later."
58   :group 'gnus-summary-exit
59   :type 'boolean)
60
61 (defcustom gnus-fetch-old-headers nil
62   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
63 If an unread article in the group refers to an older, already read (or
64 just marked as read) article, the old article will not normally be
65 displayed in the Summary buffer.  If this variable is non-nil, Gnus
66 will attempt to grab the headers to the old articles, and thereby
67 build complete threads.  If it has the value `some', only enough
68 headers to connect otherwise loose threads will be displayed.  This
69 variable can also be a number.  In that case, no more than that number
70 of old headers will be fetched.  If it has the value `invisible', all
71 old headers will be fetched, but none will be displayed.
72
73 The server has to support NOV for any of this to work."
74   :group 'gnus-thread
75   :type '(choice (const :tag "off" nil)
76                  (const some)
77                  number
78                  (sexp :menu-tag "other" t)))
79
80 (defcustom gnus-refer-thread-limit 200
81   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
82 If t, fetch all the available old headers."
83   :group 'gnus-thread
84   :type '(choice number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-summary-make-false-root 'adopt
88   "*nil means that Gnus won't gather loose threads.
89 If the root of a thread has expired or been read in a previous
90 session, the information necessary to build a complete thread has been
91 lost.  Instead of having many small sub-threads from this original thread
92 scattered all over the summary buffer, Gnus can gather them.
93
94 If non-nil, Gnus will try to gather all loose sub-threads from an
95 original thread into one large thread.
96
97 If this variable is non-nil, it should be one of `none', `adopt',
98 `dummy' or `empty'.
99
100 If this variable is `none', Gnus will not make a false root, but just
101 present the sub-threads after another.
102 If this variable is `dummy', Gnus will create a dummy root that will
103 have all the sub-threads as children.
104 If this variable is `adopt', Gnus will make one of the \"children\"
105 the parent and mark all the step-children as such.
106 If this variable is `empty', the \"children\" are printed with empty
107 subject fields.  (Or rather, they will be printed with a string
108 given by the `gnus-summary-same-subject' variable.)"
109   :group 'gnus-thread
110   :type '(choice (const :tag "off" nil)
111                  (const none)
112                  (const dummy)
113                  (const adopt)
114                  (const empty)))
115
116 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
117   "*A regexp to match subjects to be excluded from loose thread gathering.
118 As loose thread gathering is done on subjects only, that means that
119 there can be many false gatherings performed.  By rooting out certain
120 common subjects, gathering might become saner."
121   :group 'gnus-thread
122   :type 'regexp)
123
124 (defcustom gnus-summary-gather-subject-limit nil
125   "*Maximum length of subject comparisons when gathering loose threads.
126 Use nil to compare full subjects.  Setting this variable to a low
127 number will help gather threads that have been corrupted by
128 newsreaders chopping off subject lines, but it might also mean that
129 unrelated articles that have subject that happen to begin with the
130 same few characters will be incorrectly gathered.
131
132 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
133 comparing subjects."
134   :group 'gnus-thread
135   :type '(choice (const :tag "off" nil)
136                  (const fuzzy)
137                  (sexp :menu-tag "on" t)))
138
139 (defcustom gnus-simplify-subject-functions nil
140   "List of functions taking a string argument that simplify subjects.
141 The functions are applied recursively.
142
143 Useful functions to put in this list include: `gnus-simplify-subject-re',
144 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
145   :group 'gnus-thread
146   :type '(repeat function))
147
148 (defcustom gnus-simplify-ignored-prefixes nil
149   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  regexp))
153
154 (defcustom gnus-build-sparse-threads nil
155   "*If non-nil, fill in the gaps in threads.
156 If `some', only fill in the gaps that are needed to tie loose threads
157 together.  If `more', fill in all leaf nodes that Gnus can find.  If
158 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
159   :group 'gnus-thread
160   :type '(choice (const :tag "off" nil)
161                  (const some)
162                  (const more)
163                  (sexp :menu-tag "all" t)))
164
165 (defcustom gnus-summary-thread-gathering-function
166   'gnus-gather-threads-by-subject
167   "*Function used for gathering loose threads.
168 There are two pre-defined functions: `gnus-gather-threads-by-subject',
169 which only takes Subjects into consideration; and
170 `gnus-gather-threads-by-references', which compared the References
171 headers of the articles to find matches."
172   :group 'gnus-thread
173   :type '(radio (function-item gnus-gather-threads-by-subject)
174                 (function-item gnus-gather-threads-by-references)
175                 (function :tag "other")))
176
177 (defcustom gnus-summary-same-subject ""
178   "*String indicating that the current article has the same subject as the previous.
179 This variable will only be used if the value of
180 `gnus-summary-make-false-root' is `empty'."
181   :group 'gnus-summary-format
182   :type 'string)
183
184 (defcustom gnus-summary-goto-unread t
185   "*If t, marking commands will go to the next unread article.
186 If `never', commands that usually go to the next unread article, will
187 go to the next article, whether it is read or not.
188 If nil, only the marking commands will go to the next (un)read article."
189   :group 'gnus-summary-marks
190   :link '(custom-manual "(gnus)Setting Marks")
191   :type '(choice (const :tag "off" nil)
192                  (const never)
193                  (sexp :menu-tag "on" t)))
194
195 (defcustom gnus-summary-default-score 0
196   "*Default article score level.
197 All scores generated by the score files will be added to this score.
198 If this variable is nil, scoring will be disabled."
199   :group 'gnus-score-default
200   :type '(choice (const :tag "disable")
201                  integer))
202
203 (defcustom gnus-summary-zcore-fuzz 0
204   "*Fuzziness factor for the zcore in the summary buffer.
205 Articles with scores closer than this to `gnus-summary-default-score'
206 will not be marked."
207   :group 'gnus-summary-format
208   :type 'integer)
209
210 (defcustom gnus-simplify-subject-fuzzy-regexp nil
211   "*Strings to be removed when doing fuzzy matches.
212 This can either be a regular expression or list of regular expressions
213 that will be removed from subject strings if fuzzy subject
214 simplification is selected."
215   :group 'gnus-thread
216   :type '(repeat regexp))
217
218 (defcustom gnus-show-threads t
219   "*If non-nil, display threads in summary mode."
220   :group 'gnus-thread
221   :type 'boolean)
222
223 (defcustom gnus-thread-hide-subtree nil
224   "*If non-nil, hide all threads initially.
225 If threads are hidden, you have to run the command
226 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
227 to expose hidden threads."
228   :group 'gnus-thread
229   :type 'boolean)
230
231 (defcustom gnus-thread-hide-killed t
232   "*If non-nil, hide killed threads automatically."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-ignore-subject t
237   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
238 If nil, articles that have different subjects from their parents will
239 start separate threads."
240   :group 'gnus-thread
241   :type 'boolean)
242
243 (defcustom gnus-thread-operation-ignore-subject t
244   "*If non-nil, subjects will be ignored when doing thread commands.
245 This affects commands like `gnus-summary-kill-thread' and
246 `gnus-summary-lower-thread'.
247
248 If this variable is nil, articles in the same thread with different
249 subjects will not be included in the operation in question.  If this
250 variable is `fuzzy', only articles that have subjects that are fuzzily
251 equal will be included."
252   :group 'gnus-thread
253   :type '(choice (const :tag "off" nil)
254                  (const fuzzy)
255                  (sexp :tag "on" t)))
256
257 (defcustom gnus-thread-indent-level 4
258   "*Number that says how much each sub-thread should be indented."
259   :group 'gnus-thread
260   :type 'integer)
261
262 (defcustom gnus-auto-extend-newsgroup t
263   "*If non-nil, extend newsgroup forward and backward when requested."
264   :group 'gnus-summary-choose
265   :type 'boolean)
266
267 (defcustom gnus-auto-select-first t
268   "*If nil, don't select the first unread article when entering a group.
269 If this variable is `best', select the highest-scored unread article
270 in the group.  If t, select the first unread article.
271
272 This variable can also be a function to place point on a likely
273 subject line.  Useful values include `gnus-summary-first-unread-subject',
274 `gnus-summary-first-unread-article' and
275 `gnus-summary-best-unread-article'.
276
277 If you want to prevent automatic selection of the first unread article
278 in some newsgroups, set the variable to nil in
279 `gnus-select-group-hook'."
280   :group 'gnus-group-select
281   :type '(choice (const :tag "none" nil)
282                  (const best)
283                  (sexp :menu-tag "first" t)
284                  (function-item gnus-summary-first-unread-subject)
285                  (function-item gnus-summary-first-unread-article)
286                  (function-item gnus-summary-best-unread-article)))
287
288 (defcustom gnus-dont-select-after-jump-to-other-group nil
289   "If non-nil, don't select the first unread article after entering the
290 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
291 it is depend on the value of `gnus-auto-select-first' whether to select
292 or not."
293   :group 'gnus-group-select
294   :type 'boolean)
295
296 (defcustom gnus-auto-select-next t
297   "*If non-nil, offer to go to the next group from the end of the previous.
298 If the value is t and the next newsgroup is empty, Gnus will exit
299 summary mode and go back to group mode.  If the value is neither nil
300 nor t, Gnus will select the following unread newsgroup.  In
301 particular, if the value is the symbol `quietly', the next unread
302 newsgroup will be selected without any confirmation, and if it is
303 `almost-quietly', the next group will be selected without any
304 confirmation if you are located on the last article in the group.
305 Finally, if this variable is `slightly-quietly', the `Z n' command
306 will go to the next group without confirmation."
307   :group 'gnus-summary-maneuvering
308   :type '(choice (const :tag "off" nil)
309                  (const quietly)
310                  (const almost-quietly)
311                  (const slightly-quietly)
312                  (sexp :menu-tag "on" t)))
313
314 (defcustom gnus-auto-select-same nil
315   "*If non-nil, select the next article with the same subject.
316 If there are no more articles with the same subject, go to
317 the first unread article."
318   :group 'gnus-summary-maneuvering
319   :type 'boolean)
320
321 (defcustom gnus-summary-check-current nil
322   "*If non-nil, consider the current article when moving.
323 The \"unread\" movement commands will stay on the same line if the
324 current article is unread."
325   :group 'gnus-summary-maneuvering
326   :type 'boolean)
327
328 (defcustom gnus-auto-center-summary t
329   "*If non-nil, always center the current summary buffer.
330 In particular, if `vertical' do only vertical recentering.  If non-nil
331 and non-`vertical', do both horizontal and vertical recentering."
332   :group 'gnus-summary-maneuvering
333   :type '(choice (const :tag "none" nil)
334                  (const vertical)
335                  (integer :tag "height")
336                  (sexp :menu-tag "both" t)))
337
338 (defcustom gnus-show-all-headers nil
339   "*If non-nil, don't hide any headers."
340   :group 'gnus-article-hiding
341   :group 'gnus-article-headers
342   :type 'boolean)
343
344 (defcustom gnus-summary-ignore-duplicates nil
345   "*If non-nil, ignore articles with identical Message-ID headers."
346   :group 'gnus-summary
347   :type 'boolean)
348
349 (defcustom gnus-single-article-buffer t
350   "*If non-nil, display all articles in the same buffer.
351 If nil, each group will get its own article buffer."
352   :group 'gnus-article-various
353   :type 'boolean)
354
355 (defcustom gnus-break-pages t
356   "*If non-nil, do page breaking on articles.
357 The page delimiter is specified by the `gnus-page-delimiter'
358 variable."
359   :group 'gnus-article-various
360   :type 'boolean)
361
362 (defcustom gnus-show-mime t
363   "*If non-nil, do mime processing of articles.
364 The articles will simply be fed to the function given by
365 `gnus-article-display-method-for-mime'."
366   :group 'gnus-article-mime
367   :type 'boolean)
368
369 (defcustom gnus-move-split-methods nil
370   "*Variable used to suggest where articles are to be moved to.
371 It uses the same syntax as the `gnus-split-methods' variable."
372   :group 'gnus-summary-mail
373   :type '(repeat (choice (list :value (fun) function)
374                          (cons :value ("" "") regexp (repeat string))
375                          (sexp :value nil))))
376
377 (defcustom gnus-unread-mark ? ;Whitespace
378   "*Mark used for unread articles."
379   :group 'gnus-summary-marks
380   :type 'character)
381
382 (defcustom gnus-ticked-mark ?!
383   "*Mark used for ticked articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-dormant-mark ??
388   "*Mark used for dormant articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-del-mark ?r
393   "*Mark used for del'd articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-read-mark ?R
398   "*Mark used for read articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-expirable-mark ?E
403   "*Mark used for expirable articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-killed-mark ?K
408   "*Mark used for killed articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-souped-mark ?F
413   "*Mark used for killed articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-kill-file-mark ?X
418   "*Mark used for articles killed by kill files."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-low-score-mark ?Y
423   "*Mark used for articles with a low score."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-catchup-mark ?C
428   "*Mark used for articles that are caught up."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-replied-mark ?A
433   "*Mark used for articles that have been replied to."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-cached-mark ?*
438   "*Mark used for articles that are in the cache."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-saved-mark ?S
443   "*Mark used for articles that have been saved to."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-ancient-mark ?O
448   "*Mark used for ancient articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-sparse-mark ?Q
453   "*Mark used for sparsely reffed articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-canceled-mark ?G
458   "*Mark used for canceled articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-duplicate-mark ?M
463   "*Mark used for duplicate articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-undownloaded-mark ?@
468   "*Mark used for articles that weren't downloaded."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-downloadable-mark ?%
473   "*Mark used for articles that are to be downloaded."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-unsendable-mark ?=
478   "*Mark used for articles that won't be sent."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-score-over-mark ?+
483   "*Score mark used for articles with high scores."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-score-below-mark ?-
488   "*Score mark used for articles with low scores."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-empty-thread-mark ? ;Whitespace
493   "*There is no thread under the article."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-not-empty-thread-mark ?=
498   "*There is a thread under the article."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-view-pseudo-asynchronously nil
503   "*If non-nil, Gnus will view pseudo-articles asynchronously."
504   :group 'gnus-extract-view
505   :type 'boolean)
506
507 (defcustom gnus-auto-expirable-marks
508   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
509         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
510         gnus-souped-mark gnus-duplicate-mark)
511   "*The list of marks converted into expiration if a group is auto-expirable."
512   :group 'gnus-summary
513   :type '(repeat character))
514
515 (defcustom gnus-inhibit-user-auto-expire t
516   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
517   :group 'gnus-summary
518   :type 'boolean)
519
520 (defcustom gnus-view-pseudos nil
521   "*If `automatic', pseudo-articles will be viewed automatically.
522 If `not-confirm', pseudos will be viewed automatically, and the user
523 will not be asked to confirm the command."
524   :group 'gnus-extract-view
525   :type '(choice (const :tag "off" nil)
526                  (const automatic)
527                  (const not-confirm)))
528
529 (defcustom gnus-view-pseudos-separately t
530   "*If non-nil, one pseudo-article will be created for each file to be viewed.
531 If nil, all files that use the same viewing command will be given as a
532 list of parameters to that command."
533   :group 'gnus-extract-view
534   :type 'boolean)
535
536 (defcustom gnus-insert-pseudo-articles t
537   "*If non-nil, insert pseudo-articles when decoding articles."
538   :group 'gnus-extract-view
539   :type 'boolean)
540
541 (defcustom gnus-summary-dummy-line-format
542   "  %(:                          :%) %S\n"
543   "*The format specification for the dummy roots in the summary buffer.
544 It works along the same lines as a normal formatting string,
545 with some simple extensions.
546
547 %S  The subject"
548   :group 'gnus-threading
549   :type 'string)
550
551 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
552   "*The format specification for the summary mode line.
553 It works along the same lines as a normal formatting string,
554 with some simple extensions:
555
556 %G  Group name
557 %p  Unprefixed group name
558 %A  Current article number
559 %z  Current article score
560 %V  Gnus version
561 %U  Number of unread articles in the group
562 %e  Number of unselected articles in the group
563 %Z  A string with unread/unselected article counts
564 %g  Shortish group name
565 %S  Subject of the current article
566 %u  User-defined spec
567 %s  Current score file name
568 %d  Number of dormant articles
569 %r  Number of articles that have been marked as read in this session
570 %E  Number of articles expunged by the score files"
571   :group 'gnus-summary-format
572   :type 'string)
573
574 (defcustom gnus-summary-mark-below 0
575   "*Mark all articles with a score below this variable as read.
576 This variable is local to each summary buffer and usually set by the
577 score file."
578   :group 'gnus-score-default
579   :type 'integer)
580
581 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
582   "*List of functions used for sorting articles in the summary buffer.
583 This variable is only used when not using a threaded display."
584   :group 'gnus-summary-sort
585   :type '(repeat (choice (function-item gnus-article-sort-by-number)
586                          (function-item gnus-article-sort-by-author)
587                          (function-item gnus-article-sort-by-subject)
588                          (function-item gnus-article-sort-by-date)
589                          (function-item gnus-article-sort-by-score)
590                          (function :tag "other"))))
591
592 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
593   "*List of functions used for sorting threads in the summary buffer.
594 By default, threads are sorted by article number.
595
596 Each function takes two threads and return non-nil if the first thread
597 should be sorted before the other.  If you use more than one function,
598 the primary sort function should be the last.  You should probably
599 always include `gnus-thread-sort-by-number' in the list of sorting
600 functions -- preferably first.
601
602 Ready-made functions include `gnus-thread-sort-by-number',
603 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
604 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
605 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
606   :group 'gnus-summary-sort
607   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
608                          (function-item gnus-thread-sort-by-author)
609                          (function-item gnus-thread-sort-by-subject)
610                          (function-item gnus-thread-sort-by-date)
611                          (function-item gnus-thread-sort-by-score)
612                          (function-item gnus-thread-sort-by-total-score)
613                          (function :tag "other"))))
614
615 (defcustom gnus-thread-score-function '+
616   "*Function used for calculating the total score of a thread.
617
618 The function is called with the scores of the article and each
619 subthread and should then return the score of the thread.
620
621 Some functions you can use are `+', `max', or `min'."
622   :group 'gnus-summary-sort
623   :type 'function)
624
625 (defcustom gnus-summary-expunge-below nil
626   "All articles that have a score less than this variable will be expunged.
627 This variable is local to the summary buffers."
628   :group 'gnus-score-default
629   :type '(choice (const :tag "off" nil)
630                  integer))
631
632 (defcustom gnus-thread-expunge-below nil
633   "All threads that have a total score less than this variable will be expunged.
634 See `gnus-thread-score-function' for en explanation of what a
635 \"thread score\" is.
636
637 This variable is local to the summary buffers."
638   :group 'gnus-threading
639   :group 'gnus-score-default
640   :type '(choice (const :tag "off" nil)
641                  integer))
642
643 (defcustom gnus-summary-mode-hook nil
644   "*A hook for Gnus summary mode.
645 This hook is run before any variables are set in the summary buffer."
646   :group 'gnus-summary-various
647   :type 'hook)
648
649 (defcustom gnus-summary-menu-hook nil
650   "*Hook run after the creation of the summary mode menu."
651   :group 'gnus-summary-visual
652   :type 'hook)
653
654 (defcustom gnus-summary-exit-hook nil
655   "*A hook called on exit from the summary buffer.
656 It will be called with point in the group buffer."
657   :group 'gnus-summary-exit
658   :type 'hook)
659
660 (defcustom gnus-summary-prepare-hook nil
661   "*A hook called after the summary buffer has been generated.
662 If you want to modify the summary buffer, you can use this hook."
663   :group 'gnus-summary-various
664   :type 'hook)
665
666 (defcustom gnus-summary-prepared-hook nil
667   "*A hook called as the last thing after the summary buffer has been generated."
668   :group 'gnus-summary-various
669   :type 'hook)
670
671 (defcustom gnus-summary-generate-hook nil
672   "*A hook run just before generating the summary buffer.
673 This hook is commonly used to customize threading variables and the
674 like."
675   :group 'gnus-summary-various
676   :type 'hook)
677
678 (defcustom gnus-select-group-hook nil
679   "*A hook called when a newsgroup is selected.
680
681 If you'd like to simplify subjects like the
682 `gnus-summary-next-same-subject' command does, you can use the
683 following hook:
684
685  (setq gnus-select-group-hook
686       (list
687         (lambda ()
688           (mapcar (lambda (header)
689                      (mail-header-set-subject
690                       header
691                       (gnus-simplify-subject
692                        (mail-header-subject header) 're-only)))
693                   gnus-newsgroup-headers))))"
694   :group 'gnus-group-select
695   :type 'hook)
696
697 (defcustom gnus-select-article-hook nil
698   "*A hook called when an article is selected."
699   :group 'gnus-summary-choose
700   :type 'hook)
701
702 (defcustom gnus-visual-mark-article-hook
703   (list 'gnus-highlight-selected-summary)
704   "*Hook run after selecting an article in the summary buffer.
705 It is meant to be used for highlighting the article in some way.  It
706 is not run if `gnus-visual' is nil."
707   :group 'gnus-summary-visual
708   :type 'hook)
709
710 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
711   "*A hook called before parsing the headers."
712   :group 'gnus-various
713   :type 'hook)
714
715 (defcustom gnus-exit-group-hook nil
716   "*A hook called when exiting (not quitting) summary mode."
717   :group 'gnus-various
718   :type 'hook)
719
720 (defcustom gnus-summary-update-hook
721   (list 'gnus-summary-highlight-line)
722   "*A hook called when a summary line is changed.
723 The hook will not be called if `gnus-visual' is nil.
724
725 The default function `gnus-summary-highlight-line' will
726 highlight the line according to the `gnus-summary-highlight'
727 variable."
728   :group 'gnus-summary-visual
729   :type 'hook)
730
731 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
732   "*A hook called when an article is selected for the first time.
733 The hook is intended to mark an article as read (or unread)
734 automatically when it is selected."
735   :group 'gnus-summary-choose
736   :type 'hook)
737
738 (defcustom gnus-group-no-more-groups-hook nil
739   "*A hook run when returning to group mode having no more (unread) groups."
740   :group 'gnus-group-select
741   :type 'hook)
742
743 (defcustom gnus-ps-print-hook nil
744   "*A hook run before ps-printing something from Gnus."
745   :group 'gnus-summary
746   :type 'hook)
747
748 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
749   "Face used for highlighting the current article in the summary buffer."
750   :group 'gnus-summary-visual
751   :type 'face)
752
753 (defcustom gnus-summary-highlight
754   '(((= mark gnus-canceled-mark)
755      . gnus-summary-cancelled-face)
756     ((and (> score default)
757           (or (= mark gnus-dormant-mark)
758               (= mark gnus-ticked-mark)))
759      . gnus-summary-high-ticked-face)
760     ((and (< score default)
761           (or (= mark gnus-dormant-mark)
762               (= mark gnus-ticked-mark)))
763      . gnus-summary-low-ticked-face)
764     ((or (= mark gnus-dormant-mark)
765          (= mark gnus-ticked-mark))
766      . gnus-summary-normal-ticked-face)
767     ((and (> score default) (= mark gnus-ancient-mark))
768      . gnus-summary-high-ancient-face)
769     ((and (< score default) (= mark gnus-ancient-mark))
770      . gnus-summary-low-ancient-face)
771     ((= mark gnus-ancient-mark)
772      . gnus-summary-normal-ancient-face)
773     ((and (> score default) (= mark gnus-unread-mark))
774      . gnus-summary-high-unread-face)
775     ((and (< score default) (= mark gnus-unread-mark))
776      . gnus-summary-low-unread-face)
777     ((= mark gnus-unread-mark)
778      . gnus-summary-normal-unread-face)
779     ((and (> score default) (memq mark (list gnus-downloadable-mark
780                                              gnus-undownloaded-mark)))
781      . gnus-summary-high-unread-face)
782     ((and (< score default) (memq mark (list gnus-downloadable-mark
783                                              gnus-undownloaded-mark)))
784      . gnus-summary-low-unread-face)
785     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
786      . gnus-summary-normal-unread-face)
787     ((> score default)
788      . gnus-summary-high-read-face)
789     ((< score default)
790      . gnus-summary-low-read-face)
791     (t
792      . gnus-summary-normal-read-face))
793   "*Controls the highlighting of summary buffer lines.
794
795 A list of (FORM . FACE) pairs.  When deciding how a a particular
796 summary line should be displayed, each form is evaluated.  The content
797 of the face field after the first true form is used.  You can change
798 how those summary lines are displayed, by editing the face field.
799
800 You can use the following variables in the FORM field.
801
802 score:   The articles score
803 default: The default article score.
804 below:   The score below which articles are automatically marked as read.
805 mark:    The articles mark."
806   :group 'gnus-summary-visual
807   :type '(repeat (cons (sexp :tag "Form" nil)
808                        face)))
809
810 (defcustom gnus-alter-header-function nil
811   "Function called to allow alteration of article header structures.
812 The function is called with one parameter, the article header vector,
813 which it may alter in any way.")
814
815 (defcustom gnus-extra-headers nil
816   "*Extra headers to parse."
817   :group 'gnus-summary
818   :type '(repeat symbol))
819
820 (defcustom gnus-ignored-from-addresses
821   (and user-mail-address (regexp-quote user-mail-address))
822   "*Regexp of From headers that may be suppressed in favor of To headers."
823   :group 'gnus-summary
824   :type 'regexp)
825
826 (defcustom gnus-group-charset-alist
827   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
828     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
829     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
830     ("^relcom\\>" koi8-r)
831     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
832     ("^israel\\>" iso-8859-1)
833     ("^han\\>" euc-kr)
834     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
835     (".*" iso-8859-1))
836   "Alist of regexps (to match group names) and default charsets to be used when reading."
837   :type '(repeat (list (regexp :tag "Group")
838                        (symbol :tag "Charset")))
839   :group 'gnus-charset)
840
841 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
842   "List of charsets that should be ignored.
843 When these charsets are used in the \"charset\" parameter, the
844 default charset will be used instead."
845   :type '(repeat symbol)
846   :group 'gnus-charset)
847
848 (defcustom gnus-group-ignored-charsets-alist 
849   '(("alt\\.chinese\\.text" iso-8859-1))
850   "Alist of regexps (to match group names) and charsets that should be ignored.
851 When these charsets are used in the \"charset\" parameter, the
852 default charset will be used instead."
853   :type '(repeat (cons (regexp :tag "Group")
854                        (repeat symbol)))
855   :group 'gnus-charset)
856
857 (defcustom gnus-group-highlight-words-alist nil
858   "Alist of group regexps and highlight regexps.
859 This variable uses the same syntax as `gnus-emphasis-alist'."
860   :type '(repeat (cons (regexp :tag "Group")
861                        (repeat (list (regexp :tag "Highlight regexp")
862                                      (number :tag "Group for entire word" 0)
863                                      (number :tag "Group for displayed part" 0)
864                                      (symbol :tag "Face" 
865                                              gnus-emphasis-highlight-words)))))
866   :group 'gnus-summary-visual)
867
868 ;;; Internal variables
869
870 (defvar gnus-scores-exclude-files nil)
871 (defvar gnus-page-broken nil)
872 (defvar gnus-inhibit-mime-unbuttonizing nil)
873
874 (defvar gnus-original-article nil)
875 (defvar gnus-article-internal-prepare-hook nil)
876 (defvar gnus-newsgroup-process-stack nil)
877
878 (defvar gnus-thread-indent-array nil)
879 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
880
881 ;; Avoid highlighting in kill files.
882 (defvar gnus-summary-inhibit-highlight nil)
883 (defvar gnus-newsgroup-selected-overlay nil)
884 (defvar gnus-inhibit-limiting nil)
885 (defvar gnus-newsgroup-adaptive-score-file nil)
886 (defvar gnus-current-score-file nil)
887 (defvar gnus-current-move-group nil)
888 (defvar gnus-current-copy-group nil)
889 (defvar gnus-current-crosspost-group nil)
890
891 (defvar gnus-newsgroup-dependencies nil)
892 (defvar gnus-newsgroup-adaptive nil)
893 (defvar gnus-summary-display-article-function nil)
894 (defvar gnus-summary-highlight-line-function nil
895   "Function called after highlighting a summary line.")
896
897 (defvar gnus-summary-line-format-alist
898   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
899     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
900     (?s gnus-tmp-subject-or-nil ?s)
901     (?n gnus-tmp-name ?s)
902     (?A (std11-address-string
903          (car (mime-read-field 'From gnus-tmp-header))) ?s)
904     (?a (or (std11-full-name-string
905              (car (mime-read-field 'From gnus-tmp-header)))
906             gnus-tmp-from) ?s)
907     (?F gnus-tmp-from ?s)
908     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
909     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
910     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
911     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
912     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
913     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
914     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
915     (?L gnus-tmp-lines ?d)
916     (?I gnus-tmp-indentation ?s)
917     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
918     (?R gnus-tmp-replied ?c)
919     (?\[ gnus-tmp-opening-bracket ?c)
920     (?\] gnus-tmp-closing-bracket ?c)
921     (?\> (make-string gnus-tmp-level ? ) ?s)
922     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
923     (?i gnus-tmp-score ?d)
924     (?z gnus-tmp-score-char ?c)
925     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
926     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
927     (?U gnus-tmp-unread ?c)
928     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
929     (?t (gnus-summary-number-of-articles-in-thread
930          (and (boundp 'thread) (car thread)) gnus-tmp-level)
931         ?d)
932     (?e (gnus-summary-number-of-articles-in-thread
933          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
934         ?c)
935     (?u gnus-tmp-user-defined ?s)
936     (?P (gnus-pick-line-number) ?d))
937   "An alist of format specifications that can appear in summary lines,
938 and what variables they correspond with, along with the type of the
939 variable (string, integer, character, etc).")
940
941 (defvar gnus-summary-dummy-line-format-alist
942   `((?S gnus-tmp-subject ?s)
943     (?N gnus-tmp-number ?d)
944     (?u gnus-tmp-user-defined ?s)))
945
946 (defvar gnus-summary-mode-line-format-alist
947   `((?G gnus-tmp-group-name ?s)
948     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
949     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
950     (?A gnus-tmp-article-number ?d)
951     (?Z gnus-tmp-unread-and-unselected ?s)
952     (?V gnus-version ?s)
953     (?U gnus-tmp-unread-and-unticked ?d)
954     (?S gnus-tmp-subject ?s)
955     (?e gnus-tmp-unselected ?d)
956     (?u gnus-tmp-user-defined ?s)
957     (?d (length gnus-newsgroup-dormant) ?d)
958     (?t (length gnus-newsgroup-marked) ?d)
959     (?r (length gnus-newsgroup-reads) ?d)
960     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
961     (?E gnus-newsgroup-expunged-tally ?d)
962     (?s (gnus-current-score-file-nondirectory) ?s)))
963
964 (defvar gnus-last-search-regexp nil
965   "Default regexp for article search command.")
966
967 (defvar gnus-last-shell-command nil
968   "Default shell command on article.")
969
970 (defvar gnus-newsgroup-begin nil)
971 (defvar gnus-newsgroup-end nil)
972 (defvar gnus-newsgroup-last-rmail nil)
973 (defvar gnus-newsgroup-last-mail nil)
974 (defvar gnus-newsgroup-last-folder nil)
975 (defvar gnus-newsgroup-last-file nil)
976 (defvar gnus-newsgroup-auto-expire nil)
977 (defvar gnus-newsgroup-active nil)
978
979 (defvar gnus-newsgroup-data nil)
980 (defvar gnus-newsgroup-data-reverse nil)
981 (defvar gnus-newsgroup-limit nil)
982 (defvar gnus-newsgroup-limits nil)
983
984 (defvar gnus-newsgroup-unreads nil
985   "List of unread articles in the current newsgroup.")
986
987 (defvar gnus-newsgroup-unselected nil
988   "List of unselected unread articles in the current newsgroup.")
989
990 (defvar gnus-newsgroup-reads nil
991   "Alist of read articles and article marks in the current newsgroup.")
992
993 (defvar gnus-newsgroup-expunged-tally nil)
994
995 (defvar gnus-newsgroup-marked nil
996   "List of ticked articles in the current newsgroup (a subset of unread art).")
997
998 (defvar gnus-newsgroup-killed nil
999   "List of ranges of articles that have been through the scoring process.")
1000
1001 (defvar gnus-newsgroup-cached nil
1002   "List of articles that come from the article cache.")
1003
1004 (defvar gnus-newsgroup-saved nil
1005   "List of articles that have been saved.")
1006
1007 (defvar gnus-newsgroup-kill-headers nil)
1008
1009 (defvar gnus-newsgroup-replied nil
1010   "List of articles that have been replied to in the current newsgroup.")
1011
1012 (defvar gnus-newsgroup-expirable nil
1013   "List of articles in the current newsgroup that can be expired.")
1014
1015 (defvar gnus-newsgroup-processable nil
1016   "List of articles in the current newsgroup that can be processed.")
1017
1018 (defvar gnus-newsgroup-downloadable nil
1019   "List of articles in the current newsgroup that can be processed.")
1020
1021 (defvar gnus-newsgroup-undownloaded nil
1022   "List of articles in the current newsgroup that haven't been downloaded..")
1023
1024 (defvar gnus-newsgroup-unsendable nil
1025   "List of articles in the current newsgroup that won't be sent.")
1026
1027 (defvar gnus-newsgroup-bookmarks nil
1028   "List of articles in the current newsgroup that have bookmarks.")
1029
1030 (defvar gnus-newsgroup-dormant nil
1031   "List of dormant articles in the current newsgroup.")
1032
1033 (defvar gnus-newsgroup-scored nil
1034   "List of scored articles in the current newsgroup.")
1035
1036 (defvar gnus-newsgroup-headers nil
1037   "List of article headers in the current newsgroup.")
1038
1039 (defvar gnus-newsgroup-threads nil)
1040
1041 (defvar gnus-newsgroup-prepared nil
1042   "Whether the current group has been prepared properly.")
1043
1044 (defvar gnus-newsgroup-ancient nil
1045   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1046
1047 (defvar gnus-newsgroup-sparse nil)
1048
1049 (defvar gnus-current-article nil)
1050 (defvar gnus-article-current nil)
1051 (defvar gnus-current-headers nil)
1052 (defvar gnus-have-all-headers nil)
1053 (defvar gnus-last-article nil)
1054 (defvar gnus-newsgroup-history nil)
1055 (defvar gnus-newsgroup-charset nil)
1056 (defvar gnus-newsgroup-ephemeral-charset nil)
1057 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1058
1059 (defconst gnus-summary-local-variables
1060   '(gnus-newsgroup-name
1061     gnus-newsgroup-begin gnus-newsgroup-end
1062     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1063     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1064     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1065     gnus-newsgroup-unselected gnus-newsgroup-marked
1066     gnus-newsgroup-reads gnus-newsgroup-saved
1067     gnus-newsgroup-replied gnus-newsgroup-expirable
1068     gnus-newsgroup-processable gnus-newsgroup-killed
1069     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1070     gnus-newsgroup-unsendable
1071     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1072     gnus-newsgroup-headers gnus-newsgroup-threads
1073     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1074     gnus-current-article gnus-current-headers gnus-have-all-headers
1075     gnus-last-article gnus-article-internal-prepare-hook
1076     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1077     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1078     gnus-thread-expunge-below
1079     gnus-score-alist gnus-current-score-file
1080     (gnus-summary-expunge-below . global)
1081     (gnus-summary-mark-below . global)
1082     gnus-newsgroup-active gnus-scores-exclude-files
1083     gnus-newsgroup-history gnus-newsgroup-ancient
1084     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1085     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1086     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1087     (gnus-newsgroup-expunged-tally . 0)
1088     gnus-cache-removable-articles gnus-newsgroup-cached
1089     gnus-newsgroup-data gnus-newsgroup-data-reverse
1090     gnus-newsgroup-limit gnus-newsgroup-limits
1091     gnus-newsgroup-charset)
1092   "Variables that are buffer-local to the summary buffers.")
1093
1094 ;; Byte-compiler warning.
1095 (defvar gnus-article-mode-map)
1096
1097 ;; Subject simplification.
1098
1099 (defun gnus-simplify-whitespace (str)
1100   "Remove excessive whitespace."
1101   (let ((mystr str))
1102     ;; Multiple spaces.
1103     (while (string-match "[ \t][ \t]+" mystr)
1104       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1105                           " "
1106                           (substring mystr (match-end 0)))))
1107     ;; Leading spaces.
1108     (when (string-match "^[ \t]+" mystr)
1109       (setq mystr (substring mystr (match-end 0))))
1110     ;; Trailing spaces.
1111     (when (string-match "[ \t]+$" mystr)
1112       (setq mystr (substring mystr 0 (match-beginning 0))))
1113     mystr))
1114
1115 (defsubst gnus-simplify-subject-re (subject)
1116   "Remove \"Re:\" from subject lines."
1117   (if (string-match "^[Rr][Ee]: *" subject)
1118       (substring subject (match-end 0))
1119     subject))
1120
1121 (defun gnus-simplify-subject (subject &optional re-only)
1122   "Remove `Re:' and words in parentheses.
1123 If RE-ONLY is non-nil, strip leading `Re:'s only."
1124   (let ((case-fold-search t))           ;Ignore case.
1125     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1126     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1127       (setq subject (substring subject (match-end 0))))
1128     ;; Remove uninteresting prefixes.
1129     (when (and (not re-only)
1130                gnus-simplify-ignored-prefixes
1131                (string-match gnus-simplify-ignored-prefixes subject))
1132       (setq subject (substring subject (match-end 0))))
1133     ;; Remove words in parentheses from end.
1134     (unless re-only
1135       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1136         (setq subject (substring subject 0 (match-beginning 0)))))
1137     ;; Return subject string.
1138     subject))
1139
1140 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1141 ;; all whitespace.
1142 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1143   (goto-char (point-min))
1144   (while (re-search-forward regexp nil t)
1145       (replace-match (or newtext ""))))
1146
1147 (defun gnus-simplify-buffer-fuzzy ()
1148   "Simplify string in the buffer fuzzily.
1149 The string in the accessible portion of the current buffer is simplified.
1150 It is assumed to be a single-line subject.
1151 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1152 matter is removed.  Additional things can be deleted by setting
1153 gnus-simplify-subject-fuzzy-regexp."
1154   (let ((case-fold-search t)
1155         (modified-tick))
1156     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1157
1158     (while (not (eq modified-tick (buffer-modified-tick)))
1159       (setq modified-tick (buffer-modified-tick))
1160       (cond
1161        ((listp gnus-simplify-subject-fuzzy-regexp)
1162         (mapcar 'gnus-simplify-buffer-fuzzy-step
1163                 gnus-simplify-subject-fuzzy-regexp))
1164        (gnus-simplify-subject-fuzzy-regexp
1165         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1166       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1167       (gnus-simplify-buffer-fuzzy-step
1168        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1169       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1170
1171     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1172     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1173     (gnus-simplify-buffer-fuzzy-step " $")
1174     (gnus-simplify-buffer-fuzzy-step "^ +")))
1175
1176 (defun gnus-simplify-subject-fuzzy (subject)
1177   "Simplify a subject string fuzzily.
1178 See `gnus-simplify-buffer-fuzzy' for details."
1179   (save-excursion
1180     (gnus-set-work-buffer)
1181     (let ((case-fold-search t))
1182       ;; Remove uninteresting prefixes.
1183       (when (and gnus-simplify-ignored-prefixes
1184                  (string-match gnus-simplify-ignored-prefixes subject))
1185         (setq subject (substring subject (match-end 0))))
1186       (insert subject)
1187       (inline (gnus-simplify-buffer-fuzzy))
1188       (buffer-string))))
1189
1190 (defsubst gnus-simplify-subject-fully (subject)
1191   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1192   (cond
1193    (gnus-simplify-subject-functions
1194     (gnus-map-function gnus-simplify-subject-functions subject))
1195    ((null gnus-summary-gather-subject-limit)
1196     (gnus-simplify-subject-re subject))
1197    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1198     (gnus-simplify-subject-fuzzy subject))
1199    ((numberp gnus-summary-gather-subject-limit)
1200     (gnus-limit-string (gnus-simplify-subject-re subject)
1201                        gnus-summary-gather-subject-limit))
1202    (t
1203     subject)))
1204
1205 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1206   "Check whether two subjects are equal.
1207 If optional argument simple-first is t, first argument is already
1208 simplified."
1209   (cond
1210    ((null simple-first)
1211     (equal (gnus-simplify-subject-fully s1)
1212            (gnus-simplify-subject-fully s2)))
1213    (t
1214     (equal s1
1215            (gnus-simplify-subject-fully s2)))))
1216
1217 (defun gnus-summary-bubble-group ()
1218   "Increase the score of the current group.
1219 This is a handy function to add to `gnus-summary-exit-hook' to
1220 increase the score of each group you read."
1221   (gnus-group-add-score gnus-newsgroup-name))
1222
1223 \f
1224 ;;;
1225 ;;; Gnus summary mode
1226 ;;;
1227
1228 (put 'gnus-summary-mode 'mode-class 'special)
1229
1230 (when t
1231   ;; Non-orthogonal keys
1232
1233   (gnus-define-keys gnus-summary-mode-map
1234     " " gnus-summary-next-page
1235     "\177" gnus-summary-prev-page
1236     [delete] gnus-summary-prev-page
1237     [backspace] gnus-summary-prev-page
1238     "\r" gnus-summary-scroll-up
1239     "\M-\r" gnus-summary-scroll-down
1240     "n" gnus-summary-next-unread-article
1241     "p" gnus-summary-prev-unread-article
1242     "N" gnus-summary-next-article
1243     "P" gnus-summary-prev-article
1244     "\M-\C-n" gnus-summary-next-same-subject
1245     "\M-\C-p" gnus-summary-prev-same-subject
1246     "\M-n" gnus-summary-next-unread-subject
1247     "\M-p" gnus-summary-prev-unread-subject
1248     "." gnus-summary-first-unread-article
1249     "," gnus-summary-best-unread-article
1250     "\M-s" gnus-summary-search-article-forward
1251     "\M-r" gnus-summary-search-article-backward
1252     "<" gnus-summary-beginning-of-article
1253     ">" gnus-summary-end-of-article
1254     "j" gnus-summary-goto-article
1255     "^" gnus-summary-refer-parent-article
1256     "\M-^" gnus-summary-refer-article
1257     "u" gnus-summary-tick-article-forward
1258     "!" gnus-summary-tick-article-forward
1259     "U" gnus-summary-tick-article-backward
1260     "d" gnus-summary-mark-as-read-forward
1261     "D" gnus-summary-mark-as-read-backward
1262     "E" gnus-summary-mark-as-expirable
1263     "\M-u" gnus-summary-clear-mark-forward
1264     "\M-U" gnus-summary-clear-mark-backward
1265     "k" gnus-summary-kill-same-subject-and-select
1266     "\C-k" gnus-summary-kill-same-subject
1267     "\M-\C-k" gnus-summary-kill-thread
1268     "\M-\C-l" gnus-summary-lower-thread
1269     "e" gnus-summary-edit-article
1270     "#" gnus-summary-mark-as-processable
1271     "\M-#" gnus-summary-unmark-as-processable
1272     "\M-\C-t" gnus-summary-toggle-threads
1273     "\M-\C-s" gnus-summary-show-thread
1274     "\M-\C-h" gnus-summary-hide-thread
1275     "\M-\C-f" gnus-summary-next-thread
1276     "\M-\C-b" gnus-summary-prev-thread
1277     "\M-\C-u" gnus-summary-up-thread
1278     "\M-\C-d" gnus-summary-down-thread
1279     "&" gnus-summary-execute-command
1280     "c" gnus-summary-catchup-and-exit
1281     "\C-w" gnus-summary-mark-region-as-read
1282     "\C-t" gnus-summary-toggle-truncation
1283     "?" gnus-summary-mark-as-dormant
1284     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1285     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1286     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1287     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1288     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1289     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1290     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1291     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1292     "=" gnus-summary-expand-window
1293     "\C-x\C-s" gnus-summary-reselect-current-group
1294     "\M-g" gnus-summary-rescan-group
1295     "w" gnus-summary-stop-page-breaking
1296     "\C-c\C-r" gnus-summary-caesar-message
1297     "\M-t" gnus-summary-toggle-mime
1298     "f" gnus-summary-followup
1299     "F" gnus-summary-followup-with-original
1300     "C" gnus-summary-cancel-article
1301     "r" gnus-summary-reply
1302     "R" gnus-summary-reply-with-original
1303     "\C-c\C-f" gnus-summary-mail-forward
1304     "o" gnus-summary-save-article
1305     "\C-o" gnus-summary-save-article-mail
1306     "|" gnus-summary-pipe-output
1307     "\M-k" gnus-summary-edit-local-kill
1308     "\M-K" gnus-summary-edit-global-kill
1309     ;; "V" gnus-version
1310     "\C-c\C-d" gnus-summary-describe-group
1311     "q" gnus-summary-exit
1312     "Q" gnus-summary-exit-no-update
1313     "\C-c\C-i" gnus-info-find-node
1314     gnus-mouse-2 gnus-mouse-pick-article
1315     "m" gnus-summary-mail-other-window
1316     "a" gnus-summary-post-news
1317     "x" gnus-summary-limit-to-unread
1318     "s" gnus-summary-isearch-article
1319     "t" gnus-article-toggle-headers
1320     "g" gnus-summary-show-article
1321     "l" gnus-summary-goto-last-article
1322     "v" gnus-summary-preview-mime-message
1323     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1324     "\C-d" gnus-summary-enter-digest-group
1325     "\M-\C-d" gnus-summary-read-document
1326     "\M-\C-e" gnus-summary-edit-parameters
1327     "\M-\C-g" gnus-summary-customize-parameters
1328     "\C-c\C-b" gnus-bug
1329     "*" gnus-cache-enter-article
1330     "\M-*" gnus-cache-remove-article
1331     "\M-&" gnus-summary-universal-argument
1332     "\C-l" gnus-recenter
1333     "I" gnus-summary-increase-score
1334     "L" gnus-summary-lower-score
1335     "\M-i" gnus-symbolic-argument
1336     "h" gnus-summary-select-article-buffer
1337
1338     "V" gnus-summary-score-map
1339     "X" gnus-uu-extract-map
1340     "S" gnus-summary-send-map)
1341
1342   ;; Sort of orthogonal keymap
1343   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1344     "t" gnus-summary-tick-article-forward
1345     "!" gnus-summary-tick-article-forward
1346     "d" gnus-summary-mark-as-read-forward
1347     "r" gnus-summary-mark-as-read-forward
1348     "c" gnus-summary-clear-mark-forward
1349     " " gnus-summary-clear-mark-forward
1350     "e" gnus-summary-mark-as-expirable
1351     "x" gnus-summary-mark-as-expirable
1352     "?" gnus-summary-mark-as-dormant
1353     "b" gnus-summary-set-bookmark
1354     "B" gnus-summary-remove-bookmark
1355     "#" gnus-summary-mark-as-processable
1356     "\M-#" gnus-summary-unmark-as-processable
1357     "S" gnus-summary-limit-include-expunged
1358     "C" gnus-summary-catchup
1359     "H" gnus-summary-catchup-to-here
1360     "\C-c" gnus-summary-catchup-all
1361     "k" gnus-summary-kill-same-subject-and-select
1362     "K" gnus-summary-kill-same-subject
1363     "P" gnus-uu-mark-map)
1364
1365   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1366     "c" gnus-summary-clear-above
1367     "u" gnus-summary-tick-above
1368     "m" gnus-summary-mark-above
1369     "k" gnus-summary-kill-below)
1370
1371   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1372     "/" gnus-summary-limit-to-subject
1373     "n" gnus-summary-limit-to-articles
1374     "w" gnus-summary-pop-limit
1375     "s" gnus-summary-limit-to-subject
1376     "a" gnus-summary-limit-to-author
1377     "u" gnus-summary-limit-to-unread
1378     "m" gnus-summary-limit-to-marks
1379     "M" gnus-summary-limit-exclude-marks
1380     "v" gnus-summary-limit-to-score
1381     "*" gnus-summary-limit-include-cached
1382     "D" gnus-summary-limit-include-dormant
1383     "T" gnus-summary-limit-include-thread
1384     "d" gnus-summary-limit-exclude-dormant
1385     "t" gnus-summary-limit-to-age
1386     "x" gnus-summary-limit-to-extra 
1387     "E" gnus-summary-limit-include-expunged
1388     "c" gnus-summary-limit-exclude-childless-dormant
1389     "C" gnus-summary-limit-mark-excluded-as-read)
1390
1391   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1392     "n" gnus-summary-next-unread-article
1393     "p" gnus-summary-prev-unread-article
1394     "N" gnus-summary-next-article
1395     "P" gnus-summary-prev-article
1396     "\C-n" gnus-summary-next-same-subject
1397     "\C-p" gnus-summary-prev-same-subject
1398     "\M-n" gnus-summary-next-unread-subject
1399     "\M-p" gnus-summary-prev-unread-subject
1400     "f" gnus-summary-first-unread-article
1401     "b" gnus-summary-best-unread-article
1402     "j" gnus-summary-goto-article
1403     "g" gnus-summary-goto-subject
1404     "l" gnus-summary-goto-last-article
1405     "o" gnus-summary-pop-article)
1406
1407   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1408     "k" gnus-summary-kill-thread
1409     "l" gnus-summary-lower-thread
1410     "i" gnus-summary-raise-thread
1411     "T" gnus-summary-toggle-threads
1412     "t" gnus-summary-rethread-current
1413     "^" gnus-summary-reparent-thread
1414     "s" gnus-summary-show-thread
1415     "S" gnus-summary-show-all-threads
1416     "h" gnus-summary-hide-thread
1417     "H" gnus-summary-hide-all-threads
1418     "n" gnus-summary-next-thread
1419     "p" gnus-summary-prev-thread
1420     "u" gnus-summary-up-thread
1421     "o" gnus-summary-top-thread
1422     "d" gnus-summary-down-thread
1423     "#" gnus-uu-mark-thread
1424     "\M-#" gnus-uu-unmark-thread)
1425
1426   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1427     "g" gnus-summary-prepare
1428     "c" gnus-summary-insert-cached-articles)
1429
1430   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1431     "c" gnus-summary-catchup-and-exit
1432     "C" gnus-summary-catchup-all-and-exit
1433     "E" gnus-summary-exit-no-update
1434     "J" gnus-summary-jump-to-other-group
1435     "Q" gnus-summary-exit
1436     "Z" gnus-summary-exit
1437     "n" gnus-summary-catchup-and-goto-next-group
1438     "R" gnus-summary-reselect-current-group
1439     "G" gnus-summary-rescan-group
1440     "N" gnus-summary-next-group
1441     "s" gnus-summary-save-newsrc
1442     "P" gnus-summary-prev-group)
1443
1444   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1445     " " gnus-summary-next-page
1446     "n" gnus-summary-next-page
1447     "\177" gnus-summary-prev-page
1448     [delete] gnus-summary-prev-page
1449     "p" gnus-summary-prev-page
1450     "\r" gnus-summary-scroll-up
1451     "\M-\r" gnus-summary-scroll-down
1452     "<" gnus-summary-beginning-of-article
1453     ">" gnus-summary-end-of-article
1454     "b" gnus-summary-beginning-of-article
1455     "e" gnus-summary-end-of-article
1456     "^" gnus-summary-refer-parent-article
1457     "r" gnus-summary-refer-parent-article
1458     "D" gnus-summary-enter-digest-group
1459     "R" gnus-summary-refer-references
1460     "T" gnus-summary-refer-thread
1461     "g" gnus-summary-show-article
1462     "s" gnus-summary-isearch-article
1463     "P" gnus-summary-print-article
1464     "t" gnus-article-babel)
1465
1466   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1467     "b" gnus-article-add-buttons
1468     "B" gnus-article-add-buttons-to-head
1469     "o" gnus-article-treat-overstrike
1470     "e" gnus-article-emphasize
1471     "w" gnus-article-fill-cited-article
1472     "Q" gnus-article-fill-long-lines
1473     "C" gnus-article-capitalize-sentences
1474     "c" gnus-article-remove-cr
1475     "f" gnus-article-display-x-face
1476     "l" gnus-summary-stop-page-breaking
1477     "r" gnus-summary-caesar-message
1478     "t" gnus-article-toggle-headers
1479     "v" gnus-summary-verbose-headers
1480     "m" gnus-summary-toggle-mime
1481     "H" gnus-article-strip-headers-in-body
1482     "d" gnus-article-treat-dumbquotes)
1483
1484   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1485     "a" gnus-article-hide
1486     "h" gnus-article-toggle-headers
1487     "b" gnus-article-hide-boring-headers
1488     "s" gnus-article-hide-signature
1489     "c" gnus-article-hide-citation
1490     "C" gnus-article-hide-citation-in-followups
1491     "p" gnus-article-hide-pgp
1492     "B" gnus-article-strip-banner
1493     "P" gnus-article-hide-pem
1494     "\C-c" gnus-article-hide-citation-maybe)
1495
1496   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1497     "a" gnus-article-highlight
1498     "h" gnus-article-highlight-headers
1499     "c" gnus-article-highlight-citation
1500     "s" gnus-article-highlight-signature)
1501
1502   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1503     "z" gnus-article-date-ut
1504     "u" gnus-article-date-ut
1505     "l" gnus-article-date-local
1506     "e" gnus-article-date-lapsed
1507     "o" gnus-article-date-original
1508     "i" gnus-article-date-iso8601
1509     "s" gnus-article-date-user)
1510
1511   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1512     "t" gnus-article-remove-trailing-blank-lines
1513     "l" gnus-article-strip-leading-blank-lines
1514     "m" gnus-article-strip-multiple-blank-lines
1515     "a" gnus-article-strip-blank-lines
1516     "A" gnus-article-strip-all-blank-lines
1517     "s" gnus-article-strip-leading-space
1518     "e" gnus-article-strip-trailing-space)
1519
1520   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1521     "v" gnus-version
1522     "f" gnus-summary-fetch-faq
1523     "d" gnus-summary-describe-group
1524     "h" gnus-summary-describe-briefly
1525     "i" gnus-info-find-node)
1526
1527   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1528     "e" gnus-summary-expire-articles
1529     "\M-\C-e" gnus-summary-expire-articles-now
1530     "\177" gnus-summary-delete-article
1531     [delete] gnus-summary-delete-article
1532     [backspace] gnus-summary-delete-article
1533     "m" gnus-summary-move-article
1534     "r" gnus-summary-respool-article
1535     "w" gnus-summary-edit-article
1536     "c" gnus-summary-copy-article
1537     "B" gnus-summary-crosspost-article
1538     "q" gnus-summary-respool-query
1539     "t" gnus-summary-respool-trace
1540     "i" gnus-summary-import-article
1541     "p" gnus-summary-article-posted-p)
1542
1543   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1544     "o" gnus-summary-save-article
1545     "m" gnus-summary-save-article-mail
1546     "F" gnus-summary-write-article-file
1547     "r" gnus-summary-save-article-rmail
1548     "f" gnus-summary-save-article-file
1549     "b" gnus-summary-save-article-body-file
1550     "h" gnus-summary-save-article-folder
1551     "v" gnus-summary-save-article-vm
1552     "p" gnus-summary-pipe-output
1553     "s" gnus-soup-add-article)
1554
1555   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1556     "b" gnus-summary-display-buttonized
1557     "m" gnus-summary-repair-multipart
1558     "v" gnus-article-view-part
1559     "o" gnus-article-save-part
1560     "c" gnus-article-copy-part
1561     "e" gnus-article-externalize-part
1562     "i" gnus-article-inline-part
1563     "|" gnus-article-pipe-part)
1564   )
1565
1566 (defun gnus-summary-make-menu-bar ()
1567   (gnus-turn-off-edit-menu 'summary)
1568
1569   (unless (boundp 'gnus-summary-misc-menu)
1570
1571     (easy-menu-define
1572      gnus-summary-kill-menu gnus-summary-mode-map ""
1573      (cons
1574       "Score"
1575       (nconc
1576        (list
1577         ["Enter score..." gnus-summary-score-entry t]
1578         ["Customize" gnus-score-customize t])
1579        (gnus-make-score-map 'increase)
1580        (gnus-make-score-map 'lower)
1581        '(("Mark"
1582           ["Kill below" gnus-summary-kill-below t]
1583           ["Mark above" gnus-summary-mark-above t]
1584           ["Tick above" gnus-summary-tick-above t]
1585           ["Clear above" gnus-summary-clear-above t])
1586          ["Current score" gnus-summary-current-score t]
1587          ["Set score" gnus-summary-set-score t]
1588          ["Switch current score file..." gnus-score-change-score-file t]
1589          ["Set mark below..." gnus-score-set-mark-below t]
1590          ["Set expunge below..." gnus-score-set-expunge-below t]
1591          ["Edit current score file" gnus-score-edit-current-scores t]
1592          ["Edit score file" gnus-score-edit-file t]
1593          ["Trace score" gnus-score-find-trace t]
1594          ["Find words" gnus-score-find-favourite-words t]
1595          ["Rescore buffer" gnus-summary-rescore t]
1596          ["Increase score..." gnus-summary-increase-score t]
1597          ["Lower score..." gnus-summary-lower-score t]))))
1598
1599     ;; Define both the Article menu in the summary buffer and the equivalent
1600     ;; Commands menu in the article buffer here for consistency.
1601     (let ((innards
1602            '(("Hide"
1603               ["All" gnus-article-hide t]
1604               ["Headers" gnus-article-toggle-headers t]
1605               ["Signature" gnus-article-hide-signature t]
1606               ["Citation" gnus-article-hide-citation t]
1607               ["PGP" gnus-article-hide-pgp t]
1608               ["Banner" gnus-article-strip-banner t]
1609               ["Boring headers" gnus-article-hide-boring-headers t])
1610              ("Highlight"
1611               ["All" gnus-article-highlight t]
1612               ["Headers" gnus-article-highlight-headers t]
1613               ["Signature" gnus-article-highlight-signature t]
1614               ["Citation" gnus-article-highlight-citation t])
1615              ("Date"
1616               ["Local" gnus-article-date-local t]
1617               ["ISO8601" gnus-article-date-iso8601 t]
1618               ["UT" gnus-article-date-ut t]
1619               ["Original" gnus-article-date-original t]
1620               ["Lapsed" gnus-article-date-lapsed t]
1621               ["User-defined" gnus-article-date-user t])
1622              ("Washing"
1623               ("Remove Blanks"
1624                ["Leading" gnus-article-strip-leading-blank-lines t]
1625                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1626                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1627                ["All of the above" gnus-article-strip-blank-lines t]
1628                ["All" gnus-article-strip-all-blank-lines t]
1629                ["Leading space" gnus-article-strip-leading-space t]
1630                ["Trailing space" gnus-article-strip-trailing-space t])
1631               ["Overstrike" gnus-article-treat-overstrike t]
1632               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1633               ["Emphasis" gnus-article-emphasize t]
1634               ["Word wrap" gnus-article-fill-cited-article t]
1635               ["Fill long lines" gnus-article-fill-long-lines t]
1636               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1637               ["CR" gnus-article-remove-cr t]
1638               ["Show X-Face" gnus-article-display-x-face t]
1639               ["Rot 13" gnus-summary-caesar-message t]
1640               ["Unix pipe" gnus-summary-pipe-message t]
1641               ["Add buttons" gnus-article-add-buttons t]
1642               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1643               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1644               ["Toggle MIME" gnus-summary-toggle-mime t]
1645               ["Verbose header" gnus-summary-verbose-headers t]
1646               ["Toggle header" gnus-summary-toggle-header t])
1647              ("Output"
1648               ["Save in default format" gnus-summary-save-article t]
1649               ["Save in file" gnus-summary-save-article-file t]
1650               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1651               ["Save in MH folder" gnus-summary-save-article-folder t]
1652               ["Save in VM folder" gnus-summary-save-article-vm t]
1653               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1654               ["Save body in file" gnus-summary-save-article-body-file t]
1655               ["Pipe through a filter" gnus-summary-pipe-output t]
1656               ["Add to SOUP packet" gnus-soup-add-article t]
1657               ["Print" gnus-summary-print-article t])
1658              ("Backend"
1659               ["Respool article..." gnus-summary-respool-article t]
1660               ["Move article..." gnus-summary-move-article
1661                (gnus-check-backend-function
1662                 'request-move-article gnus-newsgroup-name)]
1663               ["Copy article..." gnus-summary-copy-article t]
1664               ["Crosspost article..." gnus-summary-crosspost-article
1665                (gnus-check-backend-function
1666                 'request-replace-article gnus-newsgroup-name)]
1667               ["Import file..." gnus-summary-import-article t]
1668               ["Check if posted" gnus-summary-article-posted-p t]
1669               ["Edit article" gnus-summary-edit-article
1670                (not (gnus-group-read-only-p))]
1671               ["Delete article" gnus-summary-delete-article
1672                (gnus-check-backend-function
1673                 'request-expire-articles gnus-newsgroup-name)]
1674               ["Query respool" gnus-summary-respool-query t]
1675               ["Trace respool" gnus-summary-respool-trace t]
1676               ["Delete expirable articles" gnus-summary-expire-articles-now
1677                (gnus-check-backend-function
1678                 'request-expire-articles gnus-newsgroup-name)])
1679              ("Extract"
1680               ["Uudecode" gnus-uu-decode-uu t]
1681               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1682               ["Unshar" gnus-uu-decode-unshar t]
1683               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1684               ["Save" gnus-uu-decode-save t]
1685               ["Binhex" gnus-uu-decode-binhex t]
1686               ["Postscript" gnus-uu-decode-postscript t])
1687              ("Cache"
1688               ["Enter article" gnus-cache-enter-article t]
1689               ["Remove article" gnus-cache-remove-article t])
1690              ["Translate" gnus-article-babel t]
1691              ["Select article buffer" gnus-summary-select-article-buffer t]
1692              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1693              ["Isearch article..." gnus-summary-isearch-article t]
1694              ["Beginning of the article" gnus-summary-beginning-of-article t]
1695              ["End of the article" gnus-summary-end-of-article t]
1696              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1697              ["Fetch referenced articles" gnus-summary-refer-references t]
1698              ["Fetch current thread" gnus-summary-refer-thread t]
1699              ["Fetch article with id..." gnus-summary-refer-article t]
1700              ["Redisplay" gnus-summary-show-article t])))
1701       (easy-menu-define
1702        gnus-summary-article-menu gnus-summary-mode-map ""
1703        (cons "Article" innards))
1704
1705       (easy-menu-define
1706        gnus-article-commands-menu gnus-article-mode-map ""
1707        (cons "Commands" innards)))
1708
1709     (easy-menu-define
1710      gnus-summary-thread-menu gnus-summary-mode-map ""
1711      '("Threads"
1712        ["Toggle threading" gnus-summary-toggle-threads t]
1713        ["Hide threads" gnus-summary-hide-all-threads t]
1714        ["Show threads" gnus-summary-show-all-threads t]
1715        ["Hide thread" gnus-summary-hide-thread t]
1716        ["Show thread" gnus-summary-show-thread t]
1717        ["Go to next thread" gnus-summary-next-thread t]
1718        ["Go to previous thread" gnus-summary-prev-thread t]
1719        ["Go down thread" gnus-summary-down-thread t]
1720        ["Go up thread" gnus-summary-up-thread t]
1721        ["Top of thread" gnus-summary-top-thread t]
1722        ["Mark thread as read" gnus-summary-kill-thread t]
1723        ["Lower thread score" gnus-summary-lower-thread t]
1724        ["Raise thread score" gnus-summary-raise-thread t]
1725        ["Rethread current" gnus-summary-rethread-current t]
1726        ))
1727
1728     (easy-menu-define
1729      gnus-summary-post-menu gnus-summary-mode-map ""
1730      '("Post"
1731        ["Post an article" gnus-summary-post-news t]
1732        ["Followup" gnus-summary-followup t]
1733        ["Followup and yank" gnus-summary-followup-with-original t]
1734        ["Supersede article" gnus-summary-supersede-article t]
1735        ["Cancel article" gnus-summary-cancel-article t]
1736        ["Reply" gnus-summary-reply t]
1737        ["Reply and yank" gnus-summary-reply-with-original t]
1738        ["Wide reply" gnus-summary-wide-reply t]
1739        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1740        ["Mail forward" gnus-summary-mail-forward t]
1741        ["Post forward" gnus-summary-post-forward t]
1742        ["Digest and mail" gnus-summary-mail-digest t]
1743        ["Digest and post" gnus-summary-post-digest t]
1744        ["Resend message" gnus-summary-resend-message t]
1745        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1746        ["Send a mail" gnus-summary-mail-other-window t]
1747        ["Uuencode and post" gnus-uu-post-news t]
1748        ["Followup via news" gnus-summary-followup-to-mail t]
1749        ["Followup via news and yank"
1750         gnus-summary-followup-to-mail-with-original t]
1751        ;;("Draft"
1752        ;;["Send" gnus-summary-send-draft t]
1753        ;;["Send bounced" gnus-resend-bounced-mail t])
1754        ))
1755
1756     (easy-menu-define
1757      gnus-summary-misc-menu gnus-summary-mode-map ""
1758      '("Misc"
1759        ("Mark Read"
1760         ["Mark as read" gnus-summary-mark-as-read-forward t]
1761         ["Mark same subject and select"
1762          gnus-summary-kill-same-subject-and-select t]
1763         ["Mark same subject" gnus-summary-kill-same-subject t]
1764         ["Catchup" gnus-summary-catchup t]
1765         ["Catchup all" gnus-summary-catchup-all t]
1766         ["Catchup to here" gnus-summary-catchup-to-here t]
1767         ["Catchup region" gnus-summary-mark-region-as-read t]
1768         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1769        ("Mark Various"
1770         ["Tick" gnus-summary-tick-article-forward t]
1771         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1772         ["Remove marks" gnus-summary-clear-mark-forward t]
1773         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1774         ["Set bookmark" gnus-summary-set-bookmark t]
1775         ["Remove bookmark" gnus-summary-remove-bookmark t])
1776        ("Mark Limit"
1777         ["Marks..." gnus-summary-limit-to-marks t]
1778         ["Subject..." gnus-summary-limit-to-subject t]
1779         ["Author..." gnus-summary-limit-to-author t]
1780         ["Age..." gnus-summary-limit-to-age t]
1781         ["Extra..." gnus-summary-limit-to-extra t]
1782         ["Score" gnus-summary-limit-to-score t]
1783         ["Unread" gnus-summary-limit-to-unread t]
1784         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1785         ["Articles" gnus-summary-limit-to-articles t]
1786         ["Pop limit" gnus-summary-pop-limit t]
1787         ["Show dormant" gnus-summary-limit-include-dormant t]
1788         ["Hide childless dormant"
1789          gnus-summary-limit-exclude-childless-dormant t]
1790         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1791         ["Hide marked" gnus-summary-limit-exclude-marks t]
1792         ["Show expunged" gnus-summary-show-all-expunged t])
1793        ("Process Mark"
1794         ["Set mark" gnus-summary-mark-as-processable t]
1795         ["Remove mark" gnus-summary-unmark-as-processable t]
1796         ["Remove all marks" gnus-summary-unmark-all-processable t]
1797         ["Mark above" gnus-uu-mark-over t]
1798         ["Mark series" gnus-uu-mark-series t]
1799         ["Mark region" gnus-uu-mark-region t]
1800         ["Unmark region" gnus-uu-unmark-region t]
1801         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1802         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1803         ["Mark all" gnus-uu-mark-all t]
1804         ["Mark buffer" gnus-uu-mark-buffer t]
1805         ["Mark sparse" gnus-uu-mark-sparse t]
1806         ["Mark thread" gnus-uu-mark-thread t]
1807         ["Unmark thread" gnus-uu-unmark-thread t]
1808         ("Process Mark Sets"
1809          ["Kill" gnus-summary-kill-process-mark t]
1810          ["Yank" gnus-summary-yank-process-mark
1811           gnus-newsgroup-process-stack]
1812          ["Save" gnus-summary-save-process-mark t]))
1813        ("Scroll article"
1814         ["Page forward" gnus-summary-next-page t]
1815         ["Page backward" gnus-summary-prev-page t]
1816         ["Line forward" gnus-summary-scroll-up t])
1817        ("Move"
1818         ["Next unread article" gnus-summary-next-unread-article t]
1819         ["Previous unread article" gnus-summary-prev-unread-article t]
1820         ["Next article" gnus-summary-next-article t]
1821         ["Previous article" gnus-summary-prev-article t]
1822         ["Next unread subject" gnus-summary-next-unread-subject t]
1823         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1824         ["Next article same subject" gnus-summary-next-same-subject t]
1825         ["Previous article same subject" gnus-summary-prev-same-subject t]
1826         ["First unread article" gnus-summary-first-unread-article t]
1827         ["Best unread article" gnus-summary-best-unread-article t]
1828         ["Go to subject number..." gnus-summary-goto-subject t]
1829         ["Go to article number..." gnus-summary-goto-article t]
1830         ["Go to the last article" gnus-summary-goto-last-article t]
1831         ["Pop article off history" gnus-summary-pop-article t])
1832        ("Sort"
1833         ["Sort by number" gnus-summary-sort-by-number t]
1834         ["Sort by author" gnus-summary-sort-by-author t]
1835         ["Sort by subject" gnus-summary-sort-by-subject t]
1836         ["Sort by date" gnus-summary-sort-by-date t]
1837         ["Sort by score" gnus-summary-sort-by-score t]
1838         ["Sort by lines" gnus-summary-sort-by-lines t]
1839         ["Sort by characters" gnus-summary-sort-by-chars t])
1840        ("Help"
1841         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1842         ["Describe group" gnus-summary-describe-group t]
1843         ["Read manual" gnus-info-find-node t])
1844        ("Modes"
1845         ["Pick and read" gnus-pick-mode t]
1846         ["Binary" gnus-binary-mode t])
1847        ("Regeneration"
1848         ["Regenerate" gnus-summary-prepare t]
1849         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1850         ["Toggle threading" gnus-summary-toggle-threads t])
1851        ["Filter articles..." gnus-summary-execute-command t]
1852        ["Run command on subjects..." gnus-summary-universal-argument t]
1853        ["Search articles forward..." gnus-summary-search-article-forward t]
1854        ["Search articles backward..." gnus-summary-search-article-backward t]
1855        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1856        ["Expand window" gnus-summary-expand-window t]
1857        ["Expire expirable articles" gnus-summary-expire-articles
1858         (gnus-check-backend-function
1859          'request-expire-articles gnus-newsgroup-name)]
1860        ["Edit local kill file" gnus-summary-edit-local-kill t]
1861        ["Edit main kill file" gnus-summary-edit-global-kill t]
1862        ["Edit group parameters" gnus-summary-edit-parameters t]
1863        ["Customize group parameters" gnus-summary-customize-parameters t]
1864        ["Send a bug report" gnus-bug t]
1865        ("Exit"
1866         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1867         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1868         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1869         ["Exit group" gnus-summary-exit t]
1870         ["Exit group without updating" gnus-summary-exit-no-update t]
1871         ["Exit and goto next group" gnus-summary-next-group t]
1872         ["Exit and goto prev group" gnus-summary-prev-group t]
1873         ["Reselect group" gnus-summary-reselect-current-group t]
1874         ["Rescan group" gnus-summary-rescan-group t]
1875         ["Update dribble" gnus-summary-save-newsrc t])))
1876
1877     (gnus-run-hooks 'gnus-summary-menu-hook)))
1878
1879 (defun gnus-score-set-default (var value)
1880   "A version of set that updates the GNU Emacs menu-bar."
1881   (set var value)
1882   ;; It is the message that forces the active status to be updated.
1883   (message ""))
1884
1885 (defun gnus-make-score-map (type)
1886   "Make a summary score map of type TYPE."
1887   (if t
1888       nil
1889     (let ((headers '(("author" "from" string)
1890                      ("subject" "subject" string)
1891                      ("article body" "body" string)
1892                      ("article head" "head" string)
1893                      ("xref" "xref" string)
1894                      ("extra header" "extra" string)
1895                      ("lines" "lines" number)
1896                      ("followups to author" "followup" string)))
1897           (types '((number ("less than" <)
1898                            ("greater than" >)
1899                            ("equal" =))
1900                    (string ("substring" s)
1901                            ("exact string" e)
1902                            ("fuzzy string" f)
1903                            ("regexp" r))))
1904           (perms '(("temporary" (current-time-string))
1905                    ("permanent" nil)
1906                    ("immediate" now)))
1907           header)
1908       (list
1909        (apply
1910         'nconc
1911         (list
1912          (if (eq type 'lower)
1913              "Lower score"
1914            "Increase score"))
1915         (let (outh)
1916           (while headers
1917             (setq header (car headers))
1918             (setq outh
1919                   (cons
1920                    (apply
1921                     'nconc
1922                     (list (car header))
1923                     (let ((ts (cdr (assoc (nth 2 header) types)))
1924                           outt)
1925                       (while ts
1926                         (setq outt
1927                               (cons
1928                                (apply
1929                                 'nconc
1930                                 (list (caar ts))
1931                                 (let ((ps perms)
1932                                       outp)
1933                                   (while ps
1934                                     (setq outp
1935                                           (cons
1936                                            (vector
1937                                             (caar ps)
1938                                             (list
1939                                              'gnus-summary-score-entry
1940                                              (nth 1 header)
1941                                              (if (or (string= (nth 1 header)
1942                                                               "head")
1943                                                      (string= (nth 1 header)
1944                                                               "body"))
1945                                                  ""
1946                                                (list 'gnus-summary-header
1947                                                      (nth 1 header)))
1948                                              (list 'quote (nth 1 (car ts)))
1949                                              (list 'gnus-score-default nil)
1950                                              (nth 1 (car ps))
1951                                              t)
1952                                             t)
1953                                            outp))
1954                                     (setq ps (cdr ps)))
1955                                   (list (nreverse outp))))
1956                                outt))
1957                         (setq ts (cdr ts)))
1958                       (list (nreverse outt))))
1959                    outh))
1960             (setq headers (cdr headers)))
1961           (list (nreverse outh))))))))
1962
1963 \f
1964
1965 (defun gnus-summary-mode (&optional group)
1966   "Major mode for reading articles.
1967
1968 All normal editing commands are switched off.
1969 \\<gnus-summary-mode-map>
1970 Each line in this buffer represents one article.  To read an
1971 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1972 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1973 respectively.
1974
1975 You can also post articles and send mail from this buffer.  To
1976 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1977 of an article, type `\\[gnus-summary-reply]'.
1978
1979 There are approx. one gazillion commands you can execute in this
1980 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1981
1982 The following commands are available:
1983
1984 \\{gnus-summary-mode-map}"
1985   (interactive)
1986   (when (gnus-visual-p 'summary-menu 'menu)
1987     (gnus-summary-make-menu-bar))
1988   (kill-all-local-variables)
1989   (gnus-summary-make-local-variables)
1990   (gnus-make-thread-indent-array)
1991   (gnus-simplify-mode-line)
1992   (setq major-mode 'gnus-summary-mode)
1993   (setq mode-name "Summary")
1994   (make-local-variable 'minor-mode-alist)
1995   (use-local-map gnus-summary-mode-map)
1996   (buffer-disable-undo)
1997   (setq buffer-read-only t)             ;Disable modification
1998   (setq truncate-lines t)
1999   (setq selective-display t)
2000   (setq selective-display-ellipses t)   ;Display `...'
2001   (gnus-summary-set-display-table)
2002   (gnus-set-default-directory)
2003   (setq gnus-newsgroup-name group)
2004   (make-local-variable 'gnus-summary-line-format)
2005   (make-local-variable 'gnus-summary-line-format-spec)
2006   (make-local-variable 'gnus-summary-dummy-line-format)
2007   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2008   (make-local-variable 'gnus-summary-mark-positions)
2009   (make-local-hook 'pre-command-hook)
2010   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2011   (gnus-run-hooks 'gnus-summary-mode-hook)
2012   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2013   (gnus-update-summary-mark-positions))
2014
2015 (defun gnus-summary-make-local-variables ()
2016   "Make all the local summary buffer variables."
2017   (let (global)
2018     (dolist (local gnus-summary-local-variables)
2019       (if (consp local)
2020           (progn
2021             (if (eq (cdr local) 'global)
2022                 ;; Copy the global value of the variable.
2023                 (setq global (symbol-value (car local)))
2024               ;; Use the value from the list.
2025               (setq global (eval (cdr local))))
2026             (set (make-local-variable (car local)) global))
2027         ;; Simple nil-valued local variable.
2028         (set (make-local-variable local) nil)))))
2029
2030 (defun gnus-summary-clear-local-variables ()
2031   (let ((locals gnus-summary-local-variables))
2032     (while locals
2033       (if (consp (car locals))
2034           (and (vectorp (caar locals))
2035                (set (caar locals) nil))
2036         (and (vectorp (car locals))
2037              (set (car locals) nil)))
2038       (setq locals (cdr locals)))))
2039
2040 ;; Summary data functions.
2041
2042 (defmacro gnus-data-number (data)
2043   `(car ,data))
2044
2045 (defmacro gnus-data-set-number (data number)
2046   `(setcar ,data ,number))
2047
2048 (defmacro gnus-data-mark (data)
2049   `(nth 1 ,data))
2050
2051 (defmacro gnus-data-set-mark (data mark)
2052   `(setcar (nthcdr 1 ,data) ,mark))
2053
2054 (defmacro gnus-data-pos (data)
2055   `(nth 2 ,data))
2056
2057 (defmacro gnus-data-set-pos (data pos)
2058   `(setcar (nthcdr 2 ,data) ,pos))
2059
2060 (defmacro gnus-data-header (data)
2061   `(nth 3 ,data))
2062
2063 (defmacro gnus-data-set-header (data header)
2064   `(setf (nth 3 ,data) ,header))
2065
2066 (defmacro gnus-data-level (data)
2067   `(nth 4 ,data))
2068
2069 (defmacro gnus-data-unread-p (data)
2070   `(= (nth 1 ,data) gnus-unread-mark))
2071
2072 (defmacro gnus-data-read-p (data)
2073   `(/= (nth 1 ,data) gnus-unread-mark))
2074
2075 (defmacro gnus-data-pseudo-p (data)
2076   `(consp (nth 3 ,data)))
2077
2078 (defmacro gnus-data-find (number)
2079   `(assq ,number gnus-newsgroup-data))
2080
2081 (defmacro gnus-data-find-list (number &optional data)
2082   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2083      (memq (assq ,number bdata)
2084            bdata)))
2085
2086 (defmacro gnus-data-make (number mark pos header level)
2087   `(list ,number ,mark ,pos ,header ,level))
2088
2089 (defun gnus-data-enter (after-article number mark pos header level offset)
2090   (let ((data (gnus-data-find-list after-article)))
2091     (unless data
2092       (error "No such article: %d" after-article))
2093     (setcdr data (cons (gnus-data-make number mark pos header level)
2094                        (cdr data)))
2095     (setq gnus-newsgroup-data-reverse nil)
2096     (gnus-data-update-list (cddr data) offset)))
2097
2098 (defun gnus-data-enter-list (after-article list &optional offset)
2099   (when list
2100     (let ((data (and after-article (gnus-data-find-list after-article)))
2101           (ilist list))
2102       (if (not (or data
2103                    after-article))
2104           (let ((odata gnus-newsgroup-data))
2105             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2106             (when offset
2107               (gnus-data-update-list odata offset)))
2108         ;; Find the last element in the list to be spliced into the main
2109         ;; list.
2110         (while (cdr list)
2111           (setq list (cdr list)))
2112         (if (not data)
2113             (progn
2114               (setcdr list gnus-newsgroup-data)
2115               (setq gnus-newsgroup-data ilist)
2116               (when offset
2117                 (gnus-data-update-list (cdr list) offset)))
2118           (setcdr list (cdr data))
2119           (setcdr data ilist)
2120           (when offset
2121             (gnus-data-update-list (cdr list) offset))))
2122       (setq gnus-newsgroup-data-reverse nil))))
2123
2124 (defun gnus-data-remove (article &optional offset)
2125   (let ((data gnus-newsgroup-data))
2126     (if (= (gnus-data-number (car data)) article)
2127         (progn
2128           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2129                 gnus-newsgroup-data-reverse nil)
2130           (when offset
2131             (gnus-data-update-list gnus-newsgroup-data offset)))
2132       (while (cdr data)
2133         (when (= (gnus-data-number (cadr data)) article)
2134           (setcdr data (cddr data))
2135           (when offset
2136             (gnus-data-update-list (cdr data) offset))
2137           (setq data nil
2138                 gnus-newsgroup-data-reverse nil))
2139         (setq data (cdr data))))))
2140
2141 (defmacro gnus-data-list (backward)
2142   `(if ,backward
2143        (or gnus-newsgroup-data-reverse
2144            (setq gnus-newsgroup-data-reverse
2145                  (reverse gnus-newsgroup-data)))
2146      gnus-newsgroup-data))
2147
2148 (defun gnus-data-update-list (data offset)
2149   "Add OFFSET to the POS of all data entries in DATA."
2150   (setq gnus-newsgroup-data-reverse nil)
2151   (while data
2152     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2153     (setq data (cdr data))))
2154
2155 (defun gnus-summary-article-pseudo-p (article)
2156   "Say whether this article is a pseudo article or not."
2157   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2158
2159 (defmacro gnus-summary-article-sparse-p (article)
2160   "Say whether this article is a sparse article or not."
2161   `(memq ,article gnus-newsgroup-sparse))
2162
2163 (defmacro gnus-summary-article-ancient-p (article)
2164   "Say whether this article is a sparse article or not."
2165   `(memq ,article gnus-newsgroup-ancient))
2166
2167 (defun gnus-article-parent-p (number)
2168   "Say whether this article is a parent or not."
2169   (let ((data (gnus-data-find-list number)))
2170     (and (cdr data)                     ; There has to be an article after...
2171          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2172             (gnus-data-level (nth 1 data))))))
2173
2174 (defun gnus-article-children (number)
2175   "Return a list of all children to NUMBER."
2176   (let* ((data (gnus-data-find-list number))
2177          (level (gnus-data-level (car data)))
2178          children)
2179     (setq data (cdr data))
2180     (while (and data
2181                 (= (gnus-data-level (car data)) (1+ level)))
2182       (push (gnus-data-number (car data)) children)
2183       (setq data (cdr data)))
2184     children))
2185
2186 (defmacro gnus-summary-skip-intangible ()
2187   "If the current article is intangible, then jump to a different article."
2188   '(let ((to (get-text-property (point) 'gnus-intangible)))
2189      (and to (gnus-summary-goto-subject to))))
2190
2191 (defmacro gnus-summary-article-intangible-p ()
2192   "Say whether this article is intangible or not."
2193   '(get-text-property (point) 'gnus-intangible))
2194
2195 (defun gnus-article-read-p (article)
2196   "Say whether ARTICLE is read or not."
2197   (not (or (memq article gnus-newsgroup-marked)
2198            (memq article gnus-newsgroup-unreads)
2199            (memq article gnus-newsgroup-unselected)
2200            (memq article gnus-newsgroup-dormant))))
2201
2202 ;; Some summary mode macros.
2203
2204 (defmacro gnus-summary-article-number ()
2205   "The article number of the article on the current line.
2206 If there isn's an article number here, then we return the current
2207 article number."
2208   '(progn
2209      (gnus-summary-skip-intangible)
2210      (or (get-text-property (point) 'gnus-number)
2211          (gnus-summary-last-subject))))
2212
2213 (defmacro gnus-summary-article-header (&optional number)
2214   "Return the header of article NUMBER."
2215   `(gnus-data-header (gnus-data-find
2216                       ,(or number '(gnus-summary-article-number)))))
2217
2218 (defmacro gnus-summary-thread-level (&optional number)
2219   "Return the level of thread that starts with article NUMBER."
2220   `(if (and (eq gnus-summary-make-false-root 'dummy)
2221             (get-text-property (point) 'gnus-intangible))
2222        0
2223      (gnus-data-level (gnus-data-find
2224                        ,(or number '(gnus-summary-article-number))))))
2225
2226 (defmacro gnus-summary-article-mark (&optional number)
2227   "Return the mark of article NUMBER."
2228   `(gnus-data-mark (gnus-data-find
2229                     ,(or number '(gnus-summary-article-number)))))
2230
2231 (defmacro gnus-summary-article-pos (&optional number)
2232   "Return the position of the line of article NUMBER."
2233   `(gnus-data-pos (gnus-data-find
2234                    ,(or number '(gnus-summary-article-number)))))
2235
2236 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2237 (defmacro gnus-summary-article-subject (&optional number)
2238   "Return current subject string or nil if nothing."
2239   `(let ((headers
2240           ,(if number
2241                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2242              '(gnus-data-header (assq (gnus-summary-article-number)
2243                                       gnus-newsgroup-data)))))
2244      (and headers
2245           (vectorp headers)
2246           (mail-header-subject headers))))
2247
2248 (defmacro gnus-summary-article-score (&optional number)
2249   "Return current article score."
2250   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2251                   gnus-newsgroup-scored))
2252        gnus-summary-default-score 0))
2253
2254 (defun gnus-summary-article-children (&optional number)
2255   "Return a list of article numbers that are children of article NUMBER."
2256   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2257          (level (gnus-data-level (car data)))
2258          l children)
2259     (while (and (setq data (cdr data))
2260                 (> (setq l (gnus-data-level (car data))) level))
2261       (and (= (1+ level) l)
2262            (push (gnus-data-number (car data))
2263                  children)))
2264     (nreverse children)))
2265
2266 (defun gnus-summary-article-parent (&optional number)
2267   "Return the article number of the parent of article NUMBER."
2268   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2269                                     (gnus-data-list t)))
2270          (level (gnus-data-level (car data))))
2271     (if (zerop level)
2272         ()                              ; This is a root.
2273       ;; We search until we find an article with a level less than
2274       ;; this one.  That function has to be the parent.
2275       (while (and (setq data (cdr data))
2276                   (not (< (gnus-data-level (car data)) level))))
2277       (and data (gnus-data-number (car data))))))
2278
2279 (defun gnus-unread-mark-p (mark)
2280   "Say whether MARK is the unread mark."
2281   (= mark gnus-unread-mark))
2282
2283 (defun gnus-read-mark-p (mark)
2284   "Say whether MARK is one of the marks that mark as read.
2285 This is all marks except unread, ticked, dormant, and expirable."
2286   (not (or (= mark gnus-unread-mark)
2287            (= mark gnus-ticked-mark)
2288            (= mark gnus-dormant-mark)
2289            (= mark gnus-expirable-mark))))
2290
2291 (defmacro gnus-article-mark (number)
2292   "Return the MARK of article NUMBER.
2293 This macro should only be used when computing the mark the \"first\"
2294 time; i.e., when generating the summary lines.  After that,
2295 `gnus-summary-article-mark' should be used to examine the
2296 marks of articles."
2297   `(cond
2298     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2299     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2300     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2301     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2302     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2303     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2304     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2305     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2306            gnus-ancient-mark))))
2307
2308 ;; Saving hidden threads.
2309
2310 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2311 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2312
2313 (defmacro gnus-save-hidden-threads (&rest forms)
2314   "Save hidden threads, eval FORMS, and restore the hidden threads."
2315   (let ((config (make-symbol "config")))
2316     `(let ((,config (gnus-hidden-threads-configuration)))
2317        (unwind-protect
2318            (save-excursion
2319              ,@forms)
2320          (gnus-restore-hidden-threads-configuration ,config)))))
2321
2322 (defun gnus-data-compute-positions ()
2323   "Compute the positions of all articles."
2324   (setq gnus-newsgroup-data-reverse nil)
2325   (let ((data gnus-newsgroup-data))
2326     (save-excursion
2327       (gnus-save-hidden-threads
2328         (gnus-summary-show-all-threads)
2329         (goto-char (point-min))
2330         (while data
2331           (while (get-text-property (point) 'gnus-intangible)
2332             (forward-line 1))
2333           (gnus-data-set-pos (car data) (+ (point) 3))
2334           (setq data (cdr data))
2335           (forward-line 1))))))
2336
2337 (defun gnus-hidden-threads-configuration ()
2338   "Return the current hidden threads configuration."
2339   (save-excursion
2340     (let (config)
2341       (goto-char (point-min))
2342       (while (search-forward "\r" nil t)
2343         (push (1- (point)) config))
2344       config)))
2345
2346 (defun gnus-restore-hidden-threads-configuration (config)
2347   "Restore hidden threads configuration from CONFIG."
2348   (let (point buffer-read-only)
2349     (while (setq point (pop config))
2350       (when (and (< point (point-max))
2351                  (goto-char point)
2352                  (eq (char-after) ?\n))
2353         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2354
2355 ;; Various summary mode internalish functions.
2356
2357 (defun gnus-mouse-pick-article (e)
2358   (interactive "e")
2359   (mouse-set-point e)
2360   (gnus-summary-next-page nil t))
2361
2362 (defun gnus-summary-set-display-table ()
2363   ;; Change the display table.  Odd characters have a tendency to mess
2364   ;; up nicely formatted displays - we make all possible glyphs
2365   ;; display only a single character.
2366
2367   ;; We start from the standard display table, if any.
2368   (let ((table (or (copy-sequence standard-display-table)
2369                    (make-display-table)))
2370         (i 32))
2371     ;; Nix out all the control chars...
2372     (while (>= (setq i (1- i)) 0)
2373       (aset table i [??]))
2374     ;; ... but not newline and cr, of course.  (cr is necessary for the
2375     ;; selective display).
2376     (aset table ?\n nil)
2377     (aset table ?\r nil)
2378     ;; We keep TAB as well.
2379     (aset table ?\t nil)
2380     ;; We nix out any glyphs over 126 that are not set already.
2381     (let ((i 256))
2382       (while (>= (setq i (1- i)) 127)
2383         ;; Only modify if the entry is nil.
2384         (unless (aref table i)
2385           (aset table i [??]))))
2386     (setq buffer-display-table table)))
2387
2388 (defun gnus-summary-setup-buffer (group)
2389   "Initialize summary buffer."
2390   (let ((buffer (concat "*Summary " group "*")))
2391     (if (get-buffer buffer)
2392         (progn
2393           (set-buffer buffer)
2394           (setq gnus-summary-buffer (current-buffer))
2395           (not gnus-newsgroup-prepared))
2396       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2397       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2398       (gnus-summary-mode group)
2399       (when gnus-carpal
2400         (gnus-carpal-setup-buffer 'summary))
2401       (unless gnus-single-article-buffer
2402         (make-local-variable 'gnus-article-buffer)
2403         (make-local-variable 'gnus-article-current)
2404         (make-local-variable 'gnus-original-article-buffer))
2405       (setq gnus-newsgroup-name group)
2406       t)))
2407
2408 (defun gnus-set-global-variables ()
2409   ;; Set the global equivalents of the summary buffer-local variables
2410   ;; to the latest values they had.  These reflect the summary buffer
2411   ;; that was in action when the last article was fetched.
2412   (when (eq major-mode 'gnus-summary-mode)
2413     (setq gnus-summary-buffer (current-buffer))
2414     (let ((name gnus-newsgroup-name)
2415           (marked gnus-newsgroup-marked)
2416           (unread gnus-newsgroup-unreads)
2417           (headers gnus-current-headers)
2418           (data gnus-newsgroup-data)
2419           (summary gnus-summary-buffer)
2420           (article-buffer gnus-article-buffer)
2421           (original gnus-original-article-buffer)
2422           (gac gnus-article-current)
2423           (reffed gnus-reffed-article-number)
2424           (score-file gnus-current-score-file)
2425           (default-charset gnus-newsgroup-charset))
2426       (save-excursion
2427         (set-buffer gnus-group-buffer)
2428         (setq gnus-newsgroup-name name
2429               gnus-newsgroup-marked marked
2430               gnus-newsgroup-unreads unread
2431               gnus-current-headers headers
2432               gnus-newsgroup-data data
2433               gnus-article-current gac
2434               gnus-summary-buffer summary
2435               gnus-article-buffer article-buffer
2436               gnus-original-article-buffer original
2437               gnus-reffed-article-number reffed
2438               gnus-current-score-file score-file
2439               gnus-newsgroup-charset default-charset)
2440         ;; The article buffer also has local variables.
2441         (when (gnus-buffer-live-p gnus-article-buffer)
2442           (set-buffer gnus-article-buffer)
2443           (setq gnus-summary-buffer summary))))))
2444
2445 (defun gnus-summary-article-unread-p (article)
2446   "Say whether ARTICLE is unread or not."
2447   (memq article gnus-newsgroup-unreads))
2448
2449 (defun gnus-summary-first-article-p (&optional article)
2450   "Return whether ARTICLE is the first article in the buffer."
2451   (if (not (setq article (or article (gnus-summary-article-number))))
2452       nil
2453     (eq article (caar gnus-newsgroup-data))))
2454
2455 (defun gnus-summary-last-article-p (&optional article)
2456   "Return whether ARTICLE is the last article in the buffer."
2457   (if (not (setq article (or article (gnus-summary-article-number))))
2458       ;; All non-existent numbers are the last article.  :-)
2459       t
2460     (not (cdr (gnus-data-find-list article)))))
2461
2462 (defun gnus-make-thread-indent-array ()
2463   (let ((n 200))
2464     (unless (and gnus-thread-indent-array
2465                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2466       (setq gnus-thread-indent-array (make-vector 201 "")
2467             gnus-thread-indent-array-level gnus-thread-indent-level)
2468       (while (>= n 0)
2469         (aset gnus-thread-indent-array n
2470               (make-string (* n gnus-thread-indent-level) ? ))
2471         (setq n (1- n))))))
2472
2473 (defun gnus-update-summary-mark-positions ()
2474   "Compute where the summary marks are to go."
2475   (save-excursion
2476     (when (gnus-buffer-exists-p gnus-summary-buffer)
2477       (set-buffer gnus-summary-buffer))
2478     (let ((gnus-replied-mark 129)
2479           (gnus-score-below-mark 130)
2480           (gnus-score-over-mark 130)
2481           (gnus-download-mark 131)
2482           (spec gnus-summary-line-format-spec)
2483           gnus-visual pos)
2484       (save-excursion
2485         (gnus-set-work-buffer)
2486         (let ((gnus-summary-line-format-spec spec)
2487               (gnus-newsgroup-downloadable '((0 . t))))
2488           (gnus-summary-insert-line
2489            (make-full-mail-header 0 "" "" "" "" "" 0 0 "" nil)
2490            0 nil 128 t nil "" nil 1)
2491           (goto-char (point-min))
2492           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2493                                              (- (point) 2)))))
2494           (goto-char (point-min))
2495           (push (cons 'replied (and (search-forward "\201" nil t)
2496                                     (- (point) 2)))
2497                 pos)
2498           (goto-char (point-min))
2499           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2500                 pos)
2501           (goto-char (point-min))
2502           (push (cons 'download
2503                       (and (search-forward "\203" nil t) (- (point) 2)))
2504                 pos)))
2505       (setq gnus-summary-mark-positions pos))))
2506
2507 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2508   "Insert a dummy root in the summary buffer."
2509   (beginning-of-line)
2510   (gnus-add-text-properties
2511    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2512    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2513
2514 (defun gnus-summary-from-or-to-or-newsgroups (header)
2515   (let ((to (cdr (assq 'To (mail-header-extra header))))
2516         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2517         (mail-parse-charset gnus-newsgroup-charset)
2518         (mail-parse-ignored-charsets 
2519          (save-excursion (set-buffer gnus-summary-buffer)
2520                          gnus-newsgroup-ignored-charsets)))
2521     (cond
2522      ((and to
2523            gnus-ignored-from-addresses
2524            (string-match gnus-ignored-from-addresses
2525                          (mail-header-from header)))
2526       (concat "-> "
2527               (or (car (funcall gnus-extract-address-components
2528                                 (funcall
2529                                  gnus-decode-encoded-word-function to)))
2530                   (funcall gnus-decode-encoded-word-function to))))
2531      ((and newsgroups
2532            gnus-ignored-from-addresses
2533            (string-match gnus-ignored-from-addresses
2534                          (mail-header-from header)))
2535       (concat "=> " newsgroups))
2536      (t
2537       (or (car (funcall gnus-extract-address-components
2538                         (mail-header-from header)))
2539           (mail-header-from header))))))
2540
2541 (defun gnus-summary-insert-line (gnus-tmp-header
2542                                  gnus-tmp-level gnus-tmp-current
2543                                  gnus-tmp-unread gnus-tmp-replied
2544                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2545                                  &optional gnus-tmp-dummy gnus-tmp-score
2546                                  gnus-tmp-process)
2547   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2548          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2549          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2550          (gnus-tmp-score-char
2551           (if (or (null gnus-summary-default-score)
2552                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2553                       gnus-summary-zcore-fuzz))
2554               ? ;Whitespace
2555             (if (< gnus-tmp-score gnus-summary-default-score)
2556                 gnus-score-below-mark gnus-score-over-mark)))
2557          (gnus-tmp-replied
2558           (cond (gnus-tmp-process gnus-process-mark)
2559                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2560                  gnus-cached-mark)
2561                 (gnus-tmp-replied gnus-replied-mark)
2562                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2563                  gnus-saved-mark)
2564                 (t gnus-unread-mark)))
2565          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2566          (gnus-tmp-name
2567           (cond
2568            ((string-match "<[^>]+> *$" gnus-tmp-from)
2569             (let ((beg (match-beginning 0)))
2570               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2571                        (substring gnus-tmp-from (1+ (match-beginning 0))
2572                                   (1- (match-end 0))))
2573                   (substring gnus-tmp-from 0 beg))))
2574            ((string-match "(.+)" gnus-tmp-from)
2575             (substring gnus-tmp-from
2576                        (1+ (match-beginning 0)) (1- (match-end 0))))
2577            (t gnus-tmp-from)))
2578          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2579          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2580          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2581          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2582          (buffer-read-only nil))
2583     (when (string= gnus-tmp-name "")
2584       (setq gnus-tmp-name gnus-tmp-from))
2585     (unless (numberp gnus-tmp-lines)
2586       (setq gnus-tmp-lines 0))
2587     (gnus-put-text-property-excluding-characters-with-faces
2588      (point)
2589      (progn (eval gnus-summary-line-format-spec) (point))
2590      'gnus-number gnus-tmp-number)
2591     (when (gnus-visual-p 'summary-highlight 'highlight)
2592       (forward-line -1)
2593       (gnus-run-hooks 'gnus-summary-update-hook)
2594       (forward-line 1))))
2595
2596 (defun gnus-summary-update-line (&optional dont-update)
2597   ;; Update summary line after change.
2598   (when (and gnus-summary-default-score
2599              (not gnus-summary-inhibit-highlight))
2600     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2601            (article (gnus-summary-article-number))
2602            (score (gnus-summary-article-score article)))
2603       (unless dont-update
2604         (if (and gnus-summary-mark-below
2605                  (< (gnus-summary-article-score)
2606                     gnus-summary-mark-below))
2607             ;; This article has a low score, so we mark it as read.
2608             (when (memq article gnus-newsgroup-unreads)
2609               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2610           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2611             ;; This article was previously marked as read on account
2612             ;; of a low score, but now it has risen, so we mark it as
2613             ;; unread.
2614             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2615         (gnus-summary-update-mark
2616          (if (or (null gnus-summary-default-score)
2617                  (<= (abs (- score gnus-summary-default-score))
2618                      gnus-summary-zcore-fuzz))
2619              ? ;Whitespace
2620            (if (< score gnus-summary-default-score)
2621                gnus-score-below-mark gnus-score-over-mark))
2622          'score))
2623       ;; Do visual highlighting.
2624       (when (gnus-visual-p 'summary-highlight 'highlight)
2625         (gnus-run-hooks 'gnus-summary-update-hook)))))
2626
2627 (defvar gnus-tmp-new-adopts nil)
2628
2629 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2630   "Return the number of articles in THREAD.
2631 This may be 0 in some cases -- if none of the articles in
2632 the thread are to be displayed."
2633   (let* ((number
2634           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2635           (cond
2636            ((not (listp thread))
2637             1)
2638            ((and (consp thread) (cdr thread))
2639             (apply
2640              '+ 1 (mapcar
2641                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2642            ((null thread)
2643             1)
2644            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2645             1)
2646            (t 0))))
2647     (when (and level (zerop level) gnus-tmp-new-adopts)
2648       (incf number
2649             (apply '+ (mapcar
2650                        'gnus-summary-number-of-articles-in-thread
2651                        gnus-tmp-new-adopts))))
2652     (if char
2653         (if (> number 1) gnus-not-empty-thread-mark
2654           gnus-empty-thread-mark)
2655       number)))
2656
2657 (defun gnus-summary-set-local-parameters (group)
2658   "Go through the local params of GROUP and set all variable specs in that list."
2659   (let ((params (gnus-group-find-parameter group))
2660         elem)
2661     (while params
2662       (setq elem (car params)
2663             params (cdr params))
2664       (and (consp elem)                 ; Has to be a cons.
2665            (consp (cdr elem))           ; The cdr has to be a list.
2666            (symbolp (car elem))         ; Has to be a symbol in there.
2667            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2668            (ignore-errors               ; So we set it.
2669              (make-local-variable (car elem))
2670              (set (car elem) (eval (nth 1 elem))))))))
2671
2672 (defun gnus-summary-read-group (group &optional show-all no-article
2673                                       kill-buffer no-display backward
2674                                       select-articles)
2675   "Start reading news in newsgroup GROUP.
2676 If SHOW-ALL is non-nil, already read articles are also listed.
2677 If NO-ARTICLE is non-nil, no article is selected initially.
2678 If NO-DISPLAY, don't generate a summary buffer."
2679   (let (result)
2680     (while (and group
2681                 (null (setq result
2682                             (let ((gnus-auto-select-next nil))
2683                               (or (gnus-summary-read-group-1
2684                                    group show-all no-article
2685                                    kill-buffer no-display
2686                                    select-articles)
2687                                   (setq show-all nil
2688                                         select-articles nil)))))
2689                 (eq gnus-auto-select-next 'quietly))
2690       (set-buffer gnus-group-buffer)
2691       ;; The entry function called above goes to the next
2692       ;; group automatically, so we go two groups back
2693       ;; if we are searching for the previous group.
2694       (when backward
2695         (gnus-group-prev-unread-group 2))
2696       (if (not (equal group (gnus-group-group-name)))
2697           (setq group (gnus-group-group-name))
2698         (setq group nil)))
2699     result))
2700
2701 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2702   "Directly jump to the other GROUP from summary buffer.
2703 If SHOW-ALL is non-nil, already read articles are also listed."
2704   (interactive
2705    (if (eq gnus-summary-buffer (current-buffer))
2706        (list (completing-read
2707               "Group: " gnus-active-hashtb nil t
2708               (when (and gnus-newsgroup-name
2709                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2710                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2711               'gnus-group-history)
2712              current-prefix-arg)
2713      (error "%s must be invoked from a gnus summary buffer." this-command)))
2714   (unless (or (zerop (length group))
2715               (and gnus-newsgroup-name
2716                    (string-equal gnus-newsgroup-name group)))
2717     (gnus-summary-exit)
2718     (gnus-summary-read-group group show-all
2719                              gnus-dont-select-after-jump-to-other-group)))
2720
2721 (defun gnus-summary-read-group-1 (group show-all no-article
2722                                         kill-buffer no-display
2723                                         &optional select-articles)
2724   ;; Killed foreign groups can't be entered.
2725   (when (and (not (gnus-group-native-p group))
2726              (not (gnus-gethash group gnus-newsrc-hashtb)))
2727     (error "Dead non-native groups can't be entered"))
2728   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2729   (let* ((new-group (gnus-summary-setup-buffer group))
2730          (quit-config (gnus-group-quit-config group))
2731          (did-select (and new-group (gnus-select-newsgroup
2732                                      group show-all select-articles))))
2733     (cond
2734      ;; This summary buffer exists already, so we just select it.
2735      ((not new-group)
2736       (gnus-set-global-variables)
2737       (when kill-buffer
2738         (gnus-kill-or-deaden-summary kill-buffer))
2739       (gnus-configure-windows 'summary 'force)
2740       (gnus-set-mode-line 'summary)
2741       (gnus-summary-position-point)
2742       (message "")
2743       t)
2744      ;; We couldn't select this group.
2745      ((null did-select)
2746       (when (and (eq major-mode 'gnus-summary-mode)
2747                  (not (equal (current-buffer) kill-buffer)))
2748         (kill-buffer (current-buffer))
2749         (if (not quit-config)
2750             (progn
2751               ;; Update the info -- marks might need to be removed,
2752               ;; for instance.
2753               (gnus-summary-update-info)
2754               (set-buffer gnus-group-buffer)
2755               (gnus-group-jump-to-group group)
2756               (gnus-group-next-unread-group 1))
2757           (gnus-handle-ephemeral-exit quit-config)))
2758       (gnus-message 3 "Can't select group")
2759       nil)
2760      ;; The user did a `C-g' while prompting for number of articles,
2761      ;; so we exit this group.
2762      ((eq did-select 'quit)
2763       (and (eq major-mode 'gnus-summary-mode)
2764            (not (equal (current-buffer) kill-buffer))
2765            (kill-buffer (current-buffer)))
2766       (when kill-buffer
2767         (gnus-kill-or-deaden-summary kill-buffer))
2768       (if (not quit-config)
2769           (progn
2770             (set-buffer gnus-group-buffer)
2771             (gnus-group-jump-to-group group)
2772             (gnus-group-next-unread-group 1)
2773             (gnus-configure-windows 'group 'force))
2774         (gnus-handle-ephemeral-exit quit-config))
2775       ;; Finally signal the quit.
2776       (signal 'quit nil))
2777      ;; The group was successfully selected.
2778      (t
2779       (gnus-set-global-variables)
2780       ;; Save the active value in effect when the group was entered.
2781       (setq gnus-newsgroup-active
2782             (gnus-copy-sequence
2783              (gnus-active gnus-newsgroup-name)))
2784       ;; You can change the summary buffer in some way with this hook.
2785       (gnus-run-hooks 'gnus-select-group-hook)
2786       ;; Set any local variables in the group parameters.
2787       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2788       (gnus-update-format-specifications
2789        nil 'summary 'summary-mode 'summary-dummy)
2790       ;; Do score processing.
2791       (when gnus-use-scoring
2792         (gnus-possibly-score-headers))
2793       ;; Check whether to fill in the gaps in the threads.
2794       (when gnus-build-sparse-threads
2795         (gnus-build-sparse-threads))
2796       ;; Find the initial limit.
2797       (if gnus-show-threads
2798           (if show-all
2799               (let ((gnus-newsgroup-dormant nil))
2800                 (gnus-summary-initial-limit show-all))
2801             (gnus-summary-initial-limit show-all))
2802         (setq gnus-newsgroup-limit
2803               (mapcar
2804                (lambda (header) (mail-header-number header))
2805                gnus-newsgroup-headers)))
2806       ;; Generate the summary buffer.
2807       (unless no-display
2808         (gnus-summary-prepare))
2809       (when gnus-use-trees
2810         (gnus-tree-open group)
2811         (setq gnus-summary-highlight-line-function
2812               'gnus-tree-highlight-article))
2813       ;; If the summary buffer is empty, but there are some low-scored
2814       ;; articles or some excluded dormants, we include these in the
2815       ;; buffer.
2816       (when (and (zerop (buffer-size))
2817                  (not no-display))
2818         (cond (gnus-newsgroup-dormant
2819                (gnus-summary-limit-include-dormant))
2820               ((and gnus-newsgroup-scored show-all)
2821                (gnus-summary-limit-include-expunged t))))
2822       ;; Function `gnus-apply-kill-file' must be called in this hook.
2823       (gnus-run-hooks 'gnus-apply-kill-hook)
2824       (if (and (zerop (buffer-size))
2825                (not no-display))
2826           (progn
2827             ;; This newsgroup is empty.
2828             (gnus-summary-catchup-and-exit nil t)
2829             (gnus-message 6 "No unread news")
2830             (when kill-buffer
2831               (gnus-kill-or-deaden-summary kill-buffer))
2832             ;; Return nil from this function.
2833             nil)
2834         ;; Hide conversation thread subtrees.  We cannot do this in
2835         ;; gnus-summary-prepare-hook since kill processing may not
2836         ;; work with hidden articles.
2837         (and gnus-show-threads
2838              gnus-thread-hide-subtree
2839              (gnus-summary-hide-all-threads))
2840         (when kill-buffer
2841           (gnus-kill-or-deaden-summary kill-buffer))
2842         ;; Show first unread article if requested.
2843         (if (and (not no-article)
2844                  (not no-display)
2845                  gnus-newsgroup-unreads
2846                  gnus-auto-select-first)
2847             (progn
2848               (gnus-configure-windows 'summary)
2849               (cond
2850                ((eq gnus-auto-select-first 'best)
2851                 (gnus-summary-best-unread-article))
2852                ((eq gnus-auto-select-first t)
2853                 (gnus-summary-first-unread-article))
2854                ((gnus-functionp gnus-auto-select-first)
2855                 (funcall gnus-auto-select-first))))
2856           ;; Don't select any articles, just move point to the first
2857           ;; article in the group.
2858           (goto-char (point-min))
2859           (gnus-summary-position-point)
2860           (gnus-configure-windows 'summary 'force)
2861           (gnus-set-mode-line 'summary))
2862         (when (get-buffer-window gnus-group-buffer t)
2863           ;; Gotta use windows, because recenter does weird stuff if
2864           ;; the current buffer ain't the displayed window.
2865           (let ((owin (selected-window)))
2866             (select-window (get-buffer-window gnus-group-buffer t))
2867             (when (gnus-group-goto-group group)
2868               (recenter))
2869             (select-window owin)))
2870         ;; Mark this buffer as "prepared".
2871         (setq gnus-newsgroup-prepared t)
2872         (gnus-run-hooks 'gnus-summary-prepared-hook)
2873         t)))))
2874
2875 (defun gnus-summary-prepare ()
2876   "Generate the summary buffer."
2877   (interactive)
2878   (let ((buffer-read-only nil))
2879     (erase-buffer)
2880     (setq gnus-newsgroup-data nil
2881           gnus-newsgroup-data-reverse nil)
2882     (gnus-run-hooks 'gnus-summary-generate-hook)
2883     ;; Generate the buffer, either with threads or without.
2884     (when gnus-newsgroup-headers
2885       (gnus-summary-prepare-threads
2886        (if gnus-show-threads
2887            (gnus-sort-gathered-threads
2888             (funcall gnus-summary-thread-gathering-function
2889                      (gnus-sort-threads
2890                       (gnus-cut-threads (gnus-make-threads)))))
2891          ;; Unthreaded display.
2892          (gnus-sort-articles gnus-newsgroup-headers))))
2893     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2894     ;; Call hooks for modifying summary buffer.
2895     (goto-char (point-min))
2896     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2897
2898 (defsubst gnus-general-simplify-subject (subject)
2899   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2900   (setq subject
2901         (cond
2902          ;; Truncate the subject.
2903          (gnus-simplify-subject-functions
2904           (gnus-map-function gnus-simplify-subject-functions subject))
2905          ((numberp gnus-summary-gather-subject-limit)
2906           (setq subject (gnus-simplify-subject-re subject))
2907           (if (> (length subject) gnus-summary-gather-subject-limit)
2908               (substring subject 0 gnus-summary-gather-subject-limit)
2909             subject))
2910          ;; Fuzzily simplify it.
2911          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2912           (gnus-simplify-subject-fuzzy subject))
2913          ;; Just remove the leading "Re:".
2914          (t
2915           (gnus-simplify-subject-re subject))))
2916
2917   (if (and gnus-summary-gather-exclude-subject
2918            (string-match gnus-summary-gather-exclude-subject subject))
2919       nil                               ; This article shouldn't be gathered
2920     subject))
2921
2922 (defun gnus-summary-simplify-subject-query ()
2923   "Query where the respool algorithm would put this article."
2924   (interactive)
2925   (gnus-summary-select-article)
2926   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2927
2928 (defun gnus-gather-threads-by-subject (threads)
2929   "Gather threads by looking at Subject headers."
2930   (if (not gnus-summary-make-false-root)
2931       threads
2932     (let ((hashtb (gnus-make-hashtable 1024))
2933           (prev threads)
2934           (result threads)
2935           subject hthread whole-subject)
2936       (while threads
2937         (setq subject (gnus-general-simplify-subject
2938                        (setq whole-subject (mail-header-subject
2939                                             (caar threads)))))
2940         (when subject
2941           (if (setq hthread (gnus-gethash subject hashtb))
2942               (progn
2943                 ;; We enter a dummy root into the thread, if we
2944                 ;; haven't done that already.
2945                 (unless (stringp (caar hthread))
2946                   (setcar hthread (list whole-subject (car hthread))))
2947                 ;; We add this new gathered thread to this gathered
2948                 ;; thread.
2949                 (setcdr (car hthread)
2950                         (nconc (cdar hthread) (list (car threads))))
2951                 ;; Remove it from the list of threads.
2952                 (setcdr prev (cdr threads))
2953                 (setq threads prev))
2954             ;; Enter this thread into the hash table.
2955             (gnus-sethash subject threads hashtb)))
2956         (setq prev threads)
2957         (setq threads (cdr threads)))
2958       result)))
2959
2960 (defun gnus-gather-threads-by-references (threads)
2961   "Gather threads by looking at References headers."
2962   (let ((idhashtb (gnus-make-hashtable 1024))
2963         (thhashtb (gnus-make-hashtable 1024))
2964         (prev threads)
2965         (result threads)
2966         ids references id gthread gid entered ref)
2967     (while threads
2968       (when (setq references (mail-header-references (caar threads)))
2969         (setq id (mail-header-id (caar threads))
2970               ids (gnus-split-references references)
2971               entered nil)
2972         (while (setq ref (pop ids))
2973           (setq ids (delete ref ids))
2974           (if (not (setq gid (gnus-gethash ref idhashtb)))
2975               (progn
2976                 (gnus-sethash ref id idhashtb)
2977                 (gnus-sethash id threads thhashtb))
2978             (setq gthread (gnus-gethash gid thhashtb))
2979             (unless entered
2980               ;; We enter a dummy root into the thread, if we
2981               ;; haven't done that already.
2982               (unless (stringp (caar gthread))
2983                 (setcar gthread (list (mail-header-subject (caar gthread))
2984                                       (car gthread))))
2985               ;; We add this new gathered thread to this gathered
2986               ;; thread.
2987               (setcdr (car gthread)
2988                       (nconc (cdar gthread) (list (car threads)))))
2989             ;; Add it into the thread hash table.
2990             (gnus-sethash id gthread thhashtb)
2991             (setq entered t)
2992             ;; Remove it from the list of threads.
2993             (setcdr prev (cdr threads))
2994             (setq threads prev))))
2995       (setq prev threads)
2996       (setq threads (cdr threads)))
2997     result))
2998
2999 (defun gnus-sort-gathered-threads (threads)
3000   "Sort subtreads inside each gathered thread by article number."
3001   (let ((result threads))
3002     (while threads
3003       (when (stringp (caar threads))
3004         (setcdr (car threads)
3005                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
3006       (setq threads (cdr threads)))
3007     result))
3008
3009 (defun gnus-thread-loop-p (root thread)
3010   "Say whether ROOT is in THREAD."
3011   (let ((stack (list thread))
3012         (infloop 0)
3013         th)
3014     (while (setq thread (pop stack))
3015       (setq th (cdr thread))
3016       (while (and th
3017                   (not (eq (caar th) root)))
3018         (pop th))
3019       (if th
3020           ;; We have found a loop.
3021           (let (ref-dep)
3022             (setcdr thread (delq (car th) (cdr thread)))
3023             (if (boundp (setq ref-dep (intern "none"
3024                                               gnus-newsgroup-dependencies)))
3025                 (setcdr (symbol-value ref-dep)
3026                         (nconc (cdr (symbol-value ref-dep))
3027                                (list (car th))))
3028               (set ref-dep (list nil (car th))))
3029             (setq infloop 1
3030                   stack nil))
3031         ;; Push all the subthreads onto the stack.
3032         (push (cdr thread) stack)))
3033     infloop))
3034
3035 (defun gnus-make-threads ()
3036   "Go through the dependency hashtb and find the roots.  Return all threads."
3037   (let (threads)
3038     (while (catch 'infloop
3039              (mapatoms
3040               (lambda (refs)
3041                 ;; Deal with self-referencing References loops.
3042                 (when (and (car (symbol-value refs))
3043                            (not (zerop
3044                                  (apply
3045                                   '+
3046                                   (mapcar
3047                                    (lambda (thread)
3048                                      (gnus-thread-loop-p
3049                                       (car (symbol-value refs)) thread))
3050                                    (cdr (symbol-value refs)))))))
3051                   (setq threads nil)
3052                   (throw 'infloop t))
3053                 (unless (car (symbol-value refs))
3054                   ;; These threads do not refer back to any other articles,
3055                   ;; so they're roots.
3056                   (setq threads (append (cdr (symbol-value refs)) threads))))
3057               gnus-newsgroup-dependencies)))
3058     threads))
3059
3060 ;; Build the thread tree.
3061 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3062   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3063
3064 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3065 if it was already present.
3066
3067 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3068 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3069 Message-IDs will be renamed be renamed to a unique Message-ID before
3070 being entered.
3071
3072 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3073   (let* ((id (mail-header-id header))
3074          (id-dep (and id (intern id dependencies)))
3075          ref ref-dep ref-header)
3076     ;; Enter this `header' in the `dependencies' table.
3077     (cond
3078      ((not id-dep)
3079       (setq header nil))
3080      ;; The first two cases do the normal part: enter a new `header'
3081      ;; in the `dependencies' table.
3082      ((not (boundp id-dep))
3083       (set id-dep (list header)))
3084      ((null (car (symbol-value id-dep)))
3085       (setcar (symbol-value id-dep) header))
3086
3087      ;; From here the `header' was already present in the
3088      ;; `dependencies' table.
3089      (force-new
3090       ;; Overrides an existing entry;
3091       ;; just set the header part of the entry.
3092       (setcar (symbol-value id-dep) header))
3093
3094      ;; Renames the existing `header' to a unique Message-ID.
3095      ((not gnus-summary-ignore-duplicates)
3096       ;; An article with this Message-ID has already been seen.
3097       ;; We rename the Message-ID.
3098       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3099            (list header))
3100       (mail-header-set-id header id))
3101
3102      ;; The last case ignores an existing entry, except it adds any
3103      ;; additional Xrefs (in case the two articles came from different
3104      ;; servers.
3105      ;; Also sets `header' to `nil' meaning that the `dependencies'
3106      ;; table was *not* modified.
3107      (t
3108       (mail-header-set-xref
3109        (car (symbol-value id-dep))
3110        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3111                    "")
3112                (or (mail-header-xref header) "")))
3113       (setq header nil)))
3114
3115     (when header
3116       ;; First check if that we are not creating a References loop.
3117       (setq ref (gnus-parent-id (mail-header-references header)))
3118       (while (and ref
3119                   (setq ref-dep (intern-soft ref dependencies))
3120                   (boundp ref-dep)
3121                   (setq ref-header (car (symbol-value ref-dep))))
3122         (if (string= id ref)
3123             ;; Yuk!  This is a reference loop.  Make the article be a
3124             ;; root article.
3125             (progn
3126               (mail-header-set-references (car (symbol-value id-dep)) "none")
3127               (setq ref nil))
3128           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3129       (setq ref (gnus-parent-id (mail-header-references header)))
3130       (setq ref-dep (intern (or ref "none") dependencies))
3131       (if (boundp ref-dep)
3132           (setcdr (symbol-value ref-dep)
3133                   (nconc (cdr (symbol-value ref-dep))
3134                          (list (symbol-value id-dep))))
3135         (set ref-dep (list nil (symbol-value id-dep)))))
3136     header))
3137
3138 (defun gnus-build-sparse-threads ()
3139   (let ((headers gnus-newsgroup-headers)
3140         (gnus-summary-ignore-duplicates t)
3141         header references generation relations
3142         subject child end new-child date)
3143     ;; First we create an alist of generations/relations, where
3144     ;; generations is how much we trust the relation, and the relation
3145     ;; is parent/child.
3146     (gnus-message 7 "Making sparse threads...")
3147     (save-excursion
3148       (nnheader-set-temp-buffer " *gnus sparse threads*")
3149       (while (setq header (pop headers))
3150         (when (and (setq references (mail-header-references header))
3151                    (not (string= references "")))
3152           (insert references)
3153           (setq child (mail-header-id header)
3154                 subject (mail-header-subject header)
3155                 date (mail-header-date header)
3156                 generation 0)
3157           (while (search-backward ">" nil t)
3158             (setq end (1+ (point)))
3159             (when (search-backward "<" nil t)
3160               (setq new-child (buffer-substring (point) end))
3161               (push (list (incf generation)
3162                           child (setq child new-child)
3163                           subject date)
3164                     relations)))
3165           (when child
3166             (push (list (1+ generation) child nil subject) relations))
3167           (erase-buffer)))
3168       (kill-buffer (current-buffer)))
3169     ;; Sort over trustworthiness.
3170     (mapcar
3171      (lambda (relation)
3172        (when (gnus-dependencies-add-header
3173               (make-full-mail-header
3174                gnus-reffed-article-number
3175                (nth 3 relation) "" (or (nth 4 relation) "")
3176                (nth 1 relation)
3177                (or (nth 2 relation) "") 0 0 "")
3178               gnus-newsgroup-dependencies nil)
3179          (push gnus-reffed-article-number gnus-newsgroup-limit)
3180          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3181          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3182                gnus-newsgroup-reads)
3183          (decf gnus-reffed-article-number)))
3184      (sort relations 'car-less-than-car))
3185     (gnus-message 7 "Making sparse threads...done")))
3186
3187 (defun gnus-build-old-threads ()
3188   ;; Look at all the articles that refer back to old articles, and
3189   ;; fetch the headers for the articles that aren't there.  This will
3190   ;; build complete threads - if the roots haven't been expired by the
3191   ;; server, that is.
3192   (let (id heads)
3193     (mapatoms
3194      (lambda (refs)
3195        (when (not (car (symbol-value refs)))
3196          (setq heads (cdr (symbol-value refs)))
3197          (while heads
3198            (if (memq (mail-header-number (caar heads))
3199                      gnus-newsgroup-dormant)
3200                (setq heads (cdr heads))
3201              (setq id (symbol-name refs))
3202              (while (and (setq id (gnus-build-get-header id))
3203                          (not (car (gnus-id-to-thread id)))))
3204              (setq heads nil)))))
3205      gnus-newsgroup-dependencies)))
3206
3207 ;; This function has to be called with point after the article number
3208 ;; on the beginning of the line.
3209 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3210   (let ((eol (gnus-point-at-eol))
3211         (buffer (current-buffer))
3212         header)
3213
3214     ;; overview: [num subject from date id refs chars lines misc]
3215     (unless (eobp)
3216       (forward-char))
3217
3218     (setq header
3219           (make-full-mail-header
3220            number                               ; number
3221            (nnheader-nov-field)                 ; subject
3222            (nnheader-nov-field)                 ; from
3223            (nnheader-nov-field)                 ; date
3224            (nnheader-nov-read-message-id)       ; id
3225            (nnheader-nov-field)                 ; refs
3226            (nnheader-nov-read-integer)          ; chars
3227            (nnheader-nov-read-integer)          ; lines
3228            (unless (eobp)
3229              (nnheader-nov-field))              ; misc
3230            (nnheader-nov-parse-extra)))         ; extra
3231
3232     (when gnus-alter-header-function
3233       (funcall gnus-alter-header-function header))
3234     (gnus-dependencies-add-header header dependencies force-new)))
3235
3236 (defun gnus-build-get-header (id)
3237   ;; Look through the buffer of NOV lines and find the header to
3238   ;; ID.  Enter this line into the dependencies hash table, and return
3239   ;; the id of the parent article (if any).
3240   (let ((deps gnus-newsgroup-dependencies)
3241         found header)
3242     (prog1
3243         (save-excursion
3244           (set-buffer nntp-server-buffer)
3245           (let ((case-fold-search nil))
3246             (goto-char (point-min))
3247             (while (and (not found)
3248                         (search-forward id nil t))
3249               (beginning-of-line)
3250               (setq found (looking-at
3251                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3252                                    (regexp-quote id))))
3253               (or found (beginning-of-line 2)))
3254             (when found
3255               (beginning-of-line)
3256               (and
3257                (setq header (gnus-nov-parse-line
3258                              (read (current-buffer)) deps))
3259                (gnus-parent-id (mail-header-references header))))))
3260       (when header
3261         (let ((number (mail-header-number header)))
3262           (push number gnus-newsgroup-limit)
3263           (push header gnus-newsgroup-headers)
3264           (if (memq number gnus-newsgroup-unselected)
3265               (progn
3266                 (push number gnus-newsgroup-unreads)
3267                 (setq gnus-newsgroup-unselected
3268                       (delq number gnus-newsgroup-unselected)))
3269             (push number gnus-newsgroup-ancient)))))))
3270
3271 (defun gnus-build-all-threads ()
3272   "Read all the headers."
3273   (let ((gnus-summary-ignore-duplicates t)
3274         (dependencies gnus-newsgroup-dependencies)
3275         header article)
3276     (save-excursion
3277       (set-buffer nntp-server-buffer)
3278       (let ((case-fold-search nil))
3279         (goto-char (point-min))
3280         (while (not (eobp))
3281           (ignore-errors
3282             (setq article (read (current-buffer))
3283                   header (gnus-nov-parse-line
3284                           article dependencies)))
3285           (when header
3286             (save-excursion
3287               (set-buffer gnus-summary-buffer)
3288               (push header gnus-newsgroup-headers)
3289               (if (memq (setq article (mail-header-number header))
3290                         gnus-newsgroup-unselected)
3291                   (progn
3292                     (push article gnus-newsgroup-unreads)
3293                     (setq gnus-newsgroup-unselected
3294                           (delq article gnus-newsgroup-unselected)))
3295                 (push article gnus-newsgroup-ancient)))
3296             (forward-line 1)))))))
3297
3298 (defun gnus-summary-update-article-line (article header)
3299   "Update the line for ARTICLE using HEADERS."
3300   (let* ((id (mail-header-id header))
3301          (thread (gnus-id-to-thread id)))
3302     (unless thread
3303       (error "Article in no thread"))
3304     ;; Update the thread.
3305     (setcar thread header)
3306     (gnus-summary-goto-subject article)
3307     (let* ((datal (gnus-data-find-list article))
3308            (data (car datal))
3309            (length (when (cdr datal)
3310                      (- (gnus-data-pos data)
3311                         (gnus-data-pos (cadr datal)))))
3312            (buffer-read-only nil)
3313            (level (gnus-summary-thread-level)))
3314       (gnus-delete-line)
3315       (gnus-summary-insert-line
3316        header level nil (gnus-article-mark article)
3317        (memq article gnus-newsgroup-replied)
3318        (memq article gnus-newsgroup-expirable)
3319        ;; Only insert the Subject string when it's different
3320        ;; from the previous Subject string.
3321        (if (gnus-subject-equal
3322             (condition-case ()
3323                 (mail-header-subject
3324                  (gnus-data-header
3325                   (cadr
3326                    (gnus-data-find-list
3327                     article
3328                     (gnus-data-list t)))))
3329               ;; Error on the side of excessive subjects.
3330               (error ""))
3331             (mail-header-subject header))
3332            ""
3333          (mail-header-subject header))
3334        nil (cdr (assq article gnus-newsgroup-scored))
3335        (memq article gnus-newsgroup-processable))
3336       (when length
3337         (gnus-data-update-list
3338          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3339
3340 (defun gnus-summary-update-article (article &optional iheader)
3341   "Update ARTICLE in the summary buffer."
3342   (set-buffer gnus-summary-buffer)
3343   (let* ((header (gnus-summary-article-header article))
3344          (id (mail-header-id header))
3345          (data (gnus-data-find article))
3346          (thread (gnus-id-to-thread id))
3347          (references (mail-header-references header))
3348          (parent
3349           (gnus-id-to-thread
3350            (or (gnus-parent-id
3351                 (when (and references
3352                            (not (equal "" references)))
3353                   references))
3354                "none")))
3355          (buffer-read-only nil)
3356          (old (car thread)))
3357     (when thread
3358       (unless iheader
3359         (setcar thread nil)
3360         (when parent
3361           (delq thread parent)))
3362       (if (gnus-summary-insert-subject id header)
3363           ;; Set the (possibly) new article number in the data structure.
3364           (gnus-data-set-number data (gnus-id-to-article id))
3365         (setcar thread old)
3366         nil))))
3367
3368 (defun gnus-rebuild-thread (id &optional line)
3369   "Rebuild the thread containing ID.
3370 If LINE, insert the rebuilt thread starting on line LINE."
3371   (let ((buffer-read-only nil)
3372         old-pos current thread data)
3373     (if (not gnus-show-threads)
3374         (setq thread (list (car (gnus-id-to-thread id))))
3375       ;; Get the thread this article is part of.
3376       (setq thread (gnus-remove-thread id)))
3377     (setq old-pos (gnus-point-at-bol))
3378     (setq current (save-excursion
3379                     (and (zerop (forward-line -1))
3380                          (gnus-summary-article-number))))
3381     ;; If this is a gathered thread, we have to go some re-gathering.
3382     (when (stringp (car thread))
3383       (let ((subject (car thread))
3384             roots thr)
3385         (setq thread (cdr thread))
3386         (while thread
3387           (unless (memq (setq thr (gnus-id-to-thread
3388                                    (gnus-root-id
3389                                     (mail-header-id (caar thread)))))
3390                         roots)
3391             (push thr roots))
3392           (setq thread (cdr thread)))
3393         ;; We now have all (unique) roots.
3394         (if (= (length roots) 1)
3395             ;; All the loose roots are now one solid root.
3396             (setq thread (car roots))
3397           (setq thread (cons subject (gnus-sort-threads roots))))))
3398     (let (threads)
3399       ;; We then insert this thread into the summary buffer.
3400       (when line
3401         (goto-char (point-min))
3402         (forward-line (1- line)))
3403       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3404         (if gnus-show-threads
3405             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3406           (gnus-summary-prepare-unthreaded thread))
3407         (setq data (nreverse gnus-newsgroup-data))
3408         (setq threads gnus-newsgroup-threads))
3409       ;; We splice the new data into the data structure.
3410       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3411       ;;!!! then we want to insert at the beginning of the buffer.
3412       ;;!!! That happens to be true with Gnus now, but that may
3413       ;;!!! change in the future.  Perhaps.
3414       (gnus-data-enter-list
3415        (if line nil current) data (- (point) old-pos))
3416       (setq gnus-newsgroup-threads
3417             (nconc threads gnus-newsgroup-threads))
3418       (gnus-data-compute-positions))))
3419
3420 (defun gnus-number-to-header (number)
3421   "Return the header for article NUMBER."
3422   (let ((headers gnus-newsgroup-headers))
3423     (while (and headers
3424                 (not (= number (mail-header-number (car headers)))))
3425       (pop headers))
3426     (when headers
3427       (car headers))))
3428
3429 (defun gnus-parent-headers (in-headers &optional generation)
3430   "Return the headers of the GENERATIONeth parent of HEADERS."
3431   (unless generation
3432     (setq generation 1))
3433   (let ((parent t)
3434         (headers in-headers)
3435         references)
3436     (while (and parent
3437                 (not (zerop generation))
3438                 (setq references (mail-header-references headers)))
3439       (setq headers (if (and references
3440                              (setq parent (gnus-parent-id references)))
3441                         (car (gnus-id-to-thread parent))
3442                       nil))
3443       (decf generation))
3444     (and (not (eq headers in-headers))
3445          headers)))
3446
3447 (defun gnus-id-to-thread (id)
3448   "Return the (sub-)thread where ID appears."
3449   (gnus-gethash id gnus-newsgroup-dependencies))
3450
3451 (defun gnus-id-to-article (id)
3452   "Return the article number of ID."
3453   (let ((thread (gnus-id-to-thread id)))
3454     (when (and thread
3455                (car thread))
3456       (mail-header-number (car thread)))))
3457
3458 (defun gnus-id-to-header (id)
3459   "Return the article headers of ID."
3460   (car (gnus-id-to-thread id)))
3461
3462 (defun gnus-article-displayed-root-p (article)
3463   "Say whether ARTICLE is a root(ish) article."
3464   (let ((level (gnus-summary-thread-level article))
3465         (refs (mail-header-references  (gnus-summary-article-header article)))
3466         particle)
3467     (cond
3468      ((null level) nil)
3469      ((zerop level) t)
3470      ((null refs) t)
3471      ((null (gnus-parent-id refs)) t)
3472      ((and (= 1 level)
3473            (null (setq particle (gnus-id-to-article
3474                                  (gnus-parent-id refs))))
3475            (null (gnus-summary-thread-level particle)))))))
3476
3477 (defun gnus-root-id (id)
3478   "Return the id of the root of the thread where ID appears."
3479   (let (last-id prev)
3480     (while (and id (setq prev (car (gnus-id-to-thread id))))
3481       (setq last-id id
3482             id (gnus-parent-id (mail-header-references prev))))
3483     last-id))
3484
3485 (defun gnus-articles-in-thread (thread)
3486   "Return the list of articles in THREAD."
3487   (cons (mail-header-number (car thread))
3488         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3489
3490 (defun gnus-remove-thread (id &optional dont-remove)
3491   "Remove the thread that has ID in it."
3492   (let (headers thread last-id)
3493     ;; First go up in this thread until we find the root.
3494     (setq last-id (gnus-root-id id)
3495           headers (message-flatten-list (gnus-id-to-thread last-id)))
3496     ;; We have now found the real root of this thread.  It might have
3497     ;; been gathered into some loose thread, so we have to search
3498     ;; through the threads to find the thread we wanted.
3499     (let ((threads gnus-newsgroup-threads)
3500           sub)
3501       (while threads
3502         (setq sub (car threads))
3503         (if (stringp (car sub))
3504             ;; This is a gathered thread, so we look at the roots
3505             ;; below it to find whether this article is in this
3506             ;; gathered root.
3507             (progn
3508               (setq sub (cdr sub))
3509               (while sub
3510                 (when (member (caar sub) headers)
3511                   (setq thread (car threads)
3512                         threads nil
3513                         sub nil))
3514                 (setq sub (cdr sub))))
3515           ;; It's an ordinary thread, so we check it.
3516           (when (eq (car sub) (car headers))
3517             (setq thread sub
3518                   threads nil)))
3519         (setq threads (cdr threads)))
3520       ;; If this article is in no thread, then it's a root.
3521       (if thread
3522           (unless dont-remove
3523             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3524         (setq thread (gnus-id-to-thread last-id)))
3525       (when thread
3526         (prog1
3527             thread                      ; We return this thread.
3528           (unless dont-remove
3529             (if (stringp (car thread))
3530                 (progn
3531                   ;; If we use dummy roots, then we have to remove the
3532                   ;; dummy root as well.
3533                   (when (eq gnus-summary-make-false-root 'dummy)
3534                     ;; We go to the dummy root by going to
3535                     ;; the first sub-"thread", and then one line up.
3536                     (gnus-summary-goto-article
3537                      (mail-header-number (caadr thread)))
3538                     (forward-line -1)
3539                     (gnus-delete-line)
3540                     (gnus-data-compute-positions))
3541                   (setq thread (cdr thread))
3542                   (while thread
3543                     (gnus-remove-thread-1 (car thread))
3544                     (setq thread (cdr thread))))
3545               (gnus-summary-show-all-threads)
3546               (gnus-remove-thread-1 thread))))))))
3547
3548 (defun gnus-remove-thread-1 (thread)
3549   "Remove the thread THREAD recursively."
3550   (let ((number (mail-header-number (pop thread)))
3551         d)
3552     (setq thread (reverse thread))
3553     (while thread
3554       (gnus-remove-thread-1 (pop thread)))
3555     (when (setq d (gnus-data-find number))
3556       (goto-char (gnus-data-pos d))
3557       (gnus-data-remove
3558        number
3559        (- (gnus-point-at-bol)
3560           (prog1
3561               (1+ (gnus-point-at-eol))
3562             (gnus-delete-line)))))))
3563
3564 (defun gnus-sort-threads (threads)
3565   "Sort THREADS."
3566   (if (not gnus-thread-sort-functions)
3567       threads
3568     (gnus-message 8 "Sorting threads...")
3569     (prog1
3570         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3571       (gnus-message 8 "Sorting threads...done"))))
3572
3573 (defun gnus-sort-articles (articles)
3574   "Sort ARTICLES."
3575   (when gnus-article-sort-functions
3576     (gnus-message 7 "Sorting articles...")
3577     (prog1
3578         (setq gnus-newsgroup-headers
3579               (sort articles (gnus-make-sort-function
3580                               gnus-article-sort-functions)))
3581       (gnus-message 7 "Sorting articles...done"))))
3582
3583 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3584 (defmacro gnus-thread-header (thread)
3585   ;; Return header of first article in THREAD.
3586   ;; Note that THREAD must never, ever be anything else than a variable -
3587   ;; using some other form will lead to serious barfage.
3588   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3589   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3590   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3591         (vector thread) 2))
3592
3593 (defsubst gnus-article-sort-by-number (h1 h2)
3594   "Sort articles by article number."
3595   (< (mail-header-number h1)
3596      (mail-header-number h2)))
3597
3598 (defun gnus-thread-sort-by-number (h1 h2)
3599   "Sort threads by root article number."
3600   (gnus-article-sort-by-number
3601    (gnus-thread-header h1) (gnus-thread-header h2)))
3602
3603 (defsubst gnus-article-sort-by-lines (h1 h2)
3604   "Sort articles by article Lines header."
3605   (< (mail-header-lines h1)
3606      (mail-header-lines h2)))
3607
3608 (defun gnus-thread-sort-by-lines (h1 h2)
3609   "Sort threads by root article Lines header."
3610   (gnus-article-sort-by-lines
3611    (gnus-thread-header h1) (gnus-thread-header h2)))
3612
3613 (defsubst gnus-article-sort-by-chars (h1 h2)
3614   "Sort articles by octet length."
3615   (< (mail-header-chars h1)
3616      (mail-header-chars h2)))
3617
3618 (defun gnus-thread-sort-by-chars (h1 h2)
3619   "Sort threads by root article octet length."
3620   (gnus-article-sort-by-chars
3621    (gnus-thread-header h1) (gnus-thread-header h2)))
3622
3623 (defsubst gnus-article-sort-by-author (h1 h2)
3624   "Sort articles by root author."
3625   (string-lessp
3626    (let ((addr (car (mime-read-field 'From h1))))
3627      (or (std11-full-name-string addr)
3628          (std11-address-string addr)
3629          ""))
3630    (let ((addr (car (mime-read-field 'From h2))))
3631      (or (std11-full-name-string addr)
3632          (std11-address-string addr)
3633          ""))
3634    ))
3635
3636 (defun gnus-thread-sort-by-author (h1 h2)
3637   "Sort threads by root author."
3638   (gnus-article-sort-by-author
3639    (gnus-thread-header h1)  (gnus-thread-header h2)))
3640
3641 (defsubst gnus-article-sort-by-subject (h1 h2)
3642   "Sort articles by root subject."
3643   (string-lessp
3644    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3645    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3646
3647 (defun gnus-thread-sort-by-subject (h1 h2)
3648   "Sort threads by root subject."
3649   (gnus-article-sort-by-subject
3650    (gnus-thread-header h1) (gnus-thread-header h2)))
3651
3652 (defsubst gnus-article-sort-by-date (h1 h2)
3653   "Sort articles by root article date."
3654   (time-less-p
3655    (gnus-date-get-time (mail-header-date h1))
3656    (gnus-date-get-time (mail-header-date h2))))
3657
3658 (defun gnus-thread-sort-by-date (h1 h2)
3659   "Sort threads by root article date."
3660   (gnus-article-sort-by-date
3661    (gnus-thread-header h1) (gnus-thread-header h2)))
3662
3663 (defsubst gnus-article-sort-by-score (h1 h2)
3664   "Sort articles by root article score.
3665 Unscored articles will be counted as having a score of zero."
3666   (> (or (cdr (assq (mail-header-number h1)
3667                     gnus-newsgroup-scored))
3668          gnus-summary-default-score 0)
3669      (or (cdr (assq (mail-header-number h2)
3670                     gnus-newsgroup-scored))
3671          gnus-summary-default-score 0)))
3672
3673 (defun gnus-thread-sort-by-score (h1 h2)
3674   "Sort threads by root article score."
3675   (gnus-article-sort-by-score
3676    (gnus-thread-header h1) (gnus-thread-header h2)))
3677
3678 (defun gnus-thread-sort-by-total-score (h1 h2)
3679   "Sort threads by the sum of all scores in the thread.
3680 Unscored articles will be counted as having a score of zero."
3681   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3682
3683 (defun gnus-thread-total-score (thread)
3684   ;; This function find the total score of THREAD.
3685   (cond ((null thread)
3686          0)
3687         ((consp thread)
3688          (if (stringp (car thread))
3689              (apply gnus-thread-score-function 0
3690                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3691            (gnus-thread-total-score-1 thread)))
3692         (t
3693          (gnus-thread-total-score-1 (list thread)))))
3694
3695 (defun gnus-thread-total-score-1 (root)
3696   ;; This function find the total score of the thread below ROOT.
3697   (setq root (car root))
3698   (apply gnus-thread-score-function
3699          (or (append
3700               (mapcar 'gnus-thread-total-score
3701                       (cdr (gnus-id-to-thread (mail-header-id root))))
3702               (when (> (mail-header-number root) 0)
3703                 (list (or (cdr (assq (mail-header-number root)
3704                                      gnus-newsgroup-scored))
3705                           gnus-summary-default-score 0))))
3706              (list gnus-summary-default-score)
3707              '(0))))
3708
3709 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3710 (defvar gnus-tmp-prev-subject nil)
3711 (defvar gnus-tmp-false-parent nil)
3712 (defvar gnus-tmp-root-expunged nil)
3713 (defvar gnus-tmp-dummy-line nil)
3714
3715 (defvar gnus-tmp-header)
3716 (defun gnus-extra-header (type &optional header)
3717   "Return the extra header of TYPE."
3718   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3719       ""))
3720
3721 (defun gnus-summary-prepare-threads (threads)
3722   "Prepare summary buffer from THREADS and indentation LEVEL.
3723 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3724 or a straight list of headers."
3725   (gnus-message 7 "Generating summary...")
3726
3727   (setq gnus-newsgroup-threads threads)
3728   (beginning-of-line)
3729
3730   (let ((gnus-tmp-level 0)
3731         (default-score (or gnus-summary-default-score 0))
3732         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3733         thread number subject stack state gnus-tmp-gathered beg-match
3734         new-roots gnus-tmp-new-adopts thread-end
3735         gnus-tmp-header gnus-tmp-unread
3736         gnus-tmp-replied gnus-tmp-subject-or-nil
3737         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3738         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3739         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3740
3741     (setq gnus-tmp-prev-subject nil)
3742
3743     (if (vectorp (car threads))
3744         ;; If this is a straight (sic) list of headers, then a
3745         ;; threaded summary display isn't required, so we just create
3746         ;; an unthreaded one.
3747         (gnus-summary-prepare-unthreaded threads)
3748
3749       ;; Do the threaded display.
3750
3751       (while (or threads stack gnus-tmp-new-adopts new-roots)
3752
3753         (if (and (= gnus-tmp-level 0)
3754                  (or (not stack)
3755                      (= (caar stack) 0))
3756                  (not gnus-tmp-false-parent)
3757                  (or gnus-tmp-new-adopts new-roots))
3758             (if gnus-tmp-new-adopts
3759                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3760                       thread (list (car gnus-tmp-new-adopts))
3761                       gnus-tmp-header (caar thread)
3762                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3763               (when new-roots
3764                 (setq thread (list (car new-roots))
3765                       gnus-tmp-header (caar thread)
3766                       new-roots (cdr new-roots))))
3767
3768           (if threads
3769               ;; If there are some threads, we do them before the
3770               ;; threads on the stack.
3771               (setq thread threads
3772                     gnus-tmp-header (caar thread))
3773             ;; There were no current threads, so we pop something off
3774             ;; the stack.
3775             (setq state (car stack)
3776                   gnus-tmp-level (car state)
3777                   thread (cdr state)
3778                   stack (cdr stack)
3779                   gnus-tmp-header (caar thread))))
3780
3781         (setq gnus-tmp-false-parent nil)
3782         (setq gnus-tmp-root-expunged nil)
3783         (setq thread-end nil)
3784
3785         (if (stringp gnus-tmp-header)
3786             ;; The header is a dummy root.
3787             (cond
3788              ((eq gnus-summary-make-false-root 'adopt)
3789               ;; We let the first article adopt the rest.
3790               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3791                                                (cddar thread)))
3792               (setq gnus-tmp-gathered
3793                     (nconc (mapcar
3794                             (lambda (h) (mail-header-number (car h)))
3795                             (cddar thread))
3796                            gnus-tmp-gathered))
3797               (setq thread (cons (list (caar thread)
3798                                        (cadar thread))
3799                                  (cdr thread)))
3800               (setq gnus-tmp-level -1
3801                     gnus-tmp-false-parent t))
3802              ((eq gnus-summary-make-false-root 'empty)
3803               ;; We print adopted articles with empty subject fields.
3804               (setq gnus-tmp-gathered
3805                     (nconc (mapcar
3806                             (lambda (h) (mail-header-number (car h)))
3807                             (cddar thread))
3808                            gnus-tmp-gathered))
3809               (setq gnus-tmp-level -1))
3810              ((eq gnus-summary-make-false-root 'dummy)
3811               ;; We remember that we probably want to output a dummy
3812               ;; root.
3813               (setq gnus-tmp-dummy-line gnus-tmp-header)
3814               (setq gnus-tmp-prev-subject gnus-tmp-header))
3815              (t
3816               ;; We do not make a root for the gathered
3817               ;; sub-threads at all.
3818               (setq gnus-tmp-level -1)))
3819
3820           (setq number (mail-header-number gnus-tmp-header)
3821                 subject (mail-header-subject gnus-tmp-header))
3822
3823           (cond
3824            ;; If the thread has changed subject, we might want to make
3825            ;; this subthread into a root.
3826            ((and (null gnus-thread-ignore-subject)
3827                  (not (zerop gnus-tmp-level))
3828                  gnus-tmp-prev-subject
3829                  (not (inline
3830                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3831             (setq new-roots (nconc new-roots (list (car thread)))
3832                   thread-end t
3833                   gnus-tmp-header nil))
3834            ;; If the article lies outside the current limit,
3835            ;; then we do not display it.
3836            ((not (memq number gnus-newsgroup-limit))
3837             (setq gnus-tmp-gathered
3838                   (nconc (mapcar
3839                           (lambda (h) (mail-header-number (car h)))
3840                           (cdar thread))
3841                          gnus-tmp-gathered))
3842             (setq gnus-tmp-new-adopts (if (cdar thread)
3843                                           (append gnus-tmp-new-adopts
3844                                                   (cdar thread))
3845                                         gnus-tmp-new-adopts)
3846                   thread-end t
3847                   gnus-tmp-header nil)
3848             (when (zerop gnus-tmp-level)
3849               (setq gnus-tmp-root-expunged t)))
3850            ;; Perhaps this article is to be marked as read?
3851            ((and gnus-summary-mark-below
3852                  (< (or (cdr (assq number gnus-newsgroup-scored))
3853                         default-score)
3854                     gnus-summary-mark-below)
3855                  ;; Don't touch sparse articles.
3856                  (not (gnus-summary-article-sparse-p number))
3857                  (not (gnus-summary-article-ancient-p number)))
3858             (setq gnus-newsgroup-unreads
3859                   (delq number gnus-newsgroup-unreads))
3860             (if gnus-newsgroup-auto-expire
3861                 (push number gnus-newsgroup-expirable)
3862               (push (cons number gnus-low-score-mark)
3863                     gnus-newsgroup-reads))))
3864
3865           (when gnus-tmp-header
3866             ;; We may have an old dummy line to output before this
3867             ;; article.
3868             (when (and gnus-tmp-dummy-line
3869                        (gnus-subject-equal
3870                         gnus-tmp-dummy-line
3871                         (mail-header-subject gnus-tmp-header)))
3872               (gnus-summary-insert-dummy-line
3873                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3874               (setq gnus-tmp-dummy-line nil))
3875
3876             ;; Compute the mark.
3877             (setq gnus-tmp-unread (gnus-article-mark number))
3878
3879             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3880                                   gnus-tmp-header gnus-tmp-level)
3881                   gnus-newsgroup-data)
3882
3883             ;; Actually insert the line.
3884             (setq
3885              gnus-tmp-subject-or-nil
3886              (cond
3887               ((and gnus-thread-ignore-subject
3888                     gnus-tmp-prev-subject
3889                     (not (inline (gnus-subject-equal
3890                                   gnus-tmp-prev-subject subject))))
3891                subject)
3892               ((zerop gnus-tmp-level)
3893                (if (and (eq gnus-summary-make-false-root 'empty)
3894                         (memq number gnus-tmp-gathered)
3895                         gnus-tmp-prev-subject
3896                         (inline (gnus-subject-equal
3897                                  gnus-tmp-prev-subject subject)))
3898                    gnus-summary-same-subject
3899                  subject))
3900               (t gnus-summary-same-subject)))
3901             (if (and (eq gnus-summary-make-false-root 'adopt)
3902                      (= gnus-tmp-level 1)
3903                      (memq number gnus-tmp-gathered))
3904                 (setq gnus-tmp-opening-bracket ?\<
3905                       gnus-tmp-closing-bracket ?\>)
3906               (setq gnus-tmp-opening-bracket ?\[
3907                     gnus-tmp-closing-bracket ?\]))
3908             (setq
3909              gnus-tmp-indentation
3910              (aref gnus-thread-indent-array gnus-tmp-level)
3911              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3912              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3913                                 gnus-summary-default-score 0)
3914              gnus-tmp-score-char
3915              (if (or (null gnus-summary-default-score)
3916                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3917                          gnus-summary-zcore-fuzz))
3918                  ? ;Whitespace
3919                (if (< gnus-tmp-score gnus-summary-default-score)
3920                    gnus-score-below-mark gnus-score-over-mark))
3921              gnus-tmp-replied
3922              (cond ((memq number gnus-newsgroup-processable)
3923                     gnus-process-mark)
3924                    ((memq number gnus-newsgroup-cached)
3925                     gnus-cached-mark)
3926                    ((memq number gnus-newsgroup-replied)
3927                     gnus-replied-mark)
3928                    ((memq number gnus-newsgroup-saved)
3929                     gnus-saved-mark)
3930                    (t gnus-unread-mark))
3931              gnus-tmp-from (mail-header-from gnus-tmp-header)
3932              gnus-tmp-name
3933              (cond
3934               ((string-match "<[^>]+> *$" gnus-tmp-from)
3935                (setq beg-match (match-beginning 0))
3936                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3937                         (substring gnus-tmp-from (1+ (match-beginning 0))
3938                                    (1- (match-end 0))))
3939                    (substring gnus-tmp-from 0 beg-match)))
3940               ((string-match "(.+)" gnus-tmp-from)
3941                (substring gnus-tmp-from
3942                           (1+ (match-beginning 0)) (1- (match-end 0))))
3943               (t gnus-tmp-from)))
3944             (when (string= gnus-tmp-name "")
3945               (setq gnus-tmp-name gnus-tmp-from))
3946             (unless (numberp gnus-tmp-lines)
3947               (setq gnus-tmp-lines 0))
3948             (gnus-put-text-property
3949              (point)
3950              (progn (eval gnus-summary-line-format-spec) (point))
3951              'gnus-number number)
3952             (when gnus-visual-p
3953               (forward-line -1)
3954               (gnus-run-hooks 'gnus-summary-update-hook)
3955               (forward-line 1))
3956
3957             (setq gnus-tmp-prev-subject subject)))
3958
3959         (when (nth 1 thread)
3960           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3961         (incf gnus-tmp-level)
3962         (setq threads (if thread-end nil (cdar thread)))
3963         (unless threads
3964           (setq gnus-tmp-level 0)))))
3965   (gnus-message 7 "Generating summary...done"))
3966
3967 (defun gnus-summary-prepare-unthreaded (headers)
3968   "Generate an unthreaded summary buffer based on HEADERS."
3969   (let (header number mark)
3970
3971     (beginning-of-line)
3972
3973     (while headers
3974       ;; We may have to root out some bad articles...
3975       (when (memq (setq number (mail-header-number
3976                                 (setq header (pop headers))))
3977                   gnus-newsgroup-limit)
3978         ;; Mark article as read when it has a low score.
3979         (when (and gnus-summary-mark-below
3980                    (< (or (cdr (assq number gnus-newsgroup-scored))
3981                           gnus-summary-default-score 0)
3982                       gnus-summary-mark-below)
3983                    (not (gnus-summary-article-ancient-p number)))
3984           (setq gnus-newsgroup-unreads
3985                 (delq number gnus-newsgroup-unreads))
3986           (if gnus-newsgroup-auto-expire
3987               (push number gnus-newsgroup-expirable)
3988             (push (cons number gnus-low-score-mark)
3989                   gnus-newsgroup-reads)))
3990
3991         (setq mark (gnus-article-mark number))
3992         (push (gnus-data-make number mark (1+ (point)) header 0)
3993               gnus-newsgroup-data)
3994         (gnus-summary-insert-line
3995          header 0 number
3996          mark (memq number gnus-newsgroup-replied)
3997          (memq number gnus-newsgroup-expirable)
3998          (mail-header-subject header) nil
3999          (cdr (assq number gnus-newsgroup-scored))
4000          (memq number gnus-newsgroup-processable))))))
4001
4002 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4003   "Select newsgroup GROUP.
4004 If READ-ALL is non-nil, all articles in the group are selected.
4005 If SELECT-ARTICLES, only select those articles from GROUP."
4006   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4007          ;;!!! Dirty hack; should be removed.
4008          (gnus-summary-ignore-duplicates
4009           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4010               t
4011             gnus-summary-ignore-duplicates))
4012          (info (nth 2 entry))
4013          articles fetched-articles cached)
4014
4015     (unless (gnus-check-server
4016              (setq gnus-current-select-method
4017                    (gnus-find-method-for-group group)))
4018       (error "Couldn't open server"))
4019
4020     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4021         (gnus-activate-group group)     ; Or we can activate it...
4022         (progn                          ; Or we bug out.
4023           (when (equal major-mode 'gnus-summary-mode)
4024             (kill-buffer (current-buffer)))
4025           (error "Couldn't request group %s: %s"
4026                  group (gnus-status-message group))))
4027
4028     (unless (gnus-request-group group t)
4029       (when (equal major-mode 'gnus-summary-mode)
4030         (kill-buffer (current-buffer)))
4031       (error "Couldn't request group %s: %s"
4032              group (gnus-status-message group)))
4033
4034     (setq gnus-newsgroup-name group)
4035     (setq gnus-newsgroup-unselected nil)
4036     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4037     (gnus-summary-setup-default-charset)
4038
4039     ;; Adjust and set lists of article marks.
4040     (when info
4041       (gnus-adjust-marked-articles info))
4042
4043     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4044     (setq cached
4045           (if (gnus-virtual-group-p group)
4046               gnus-newsgroup-cached
4047             (gnus-cache-articles-in-group group)))
4048
4049     (setq gnus-newsgroup-unreads
4050           (gnus-set-difference
4051            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4052            gnus-newsgroup-dormant))
4053
4054     (setq gnus-newsgroup-processable nil)
4055
4056     (gnus-update-read-articles group gnus-newsgroup-unreads)
4057
4058     (if (setq articles select-articles)
4059         (setq gnus-newsgroup-unselected
4060               (gnus-sorted-intersection
4061                gnus-newsgroup-unreads
4062                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4063       (setq articles (gnus-articles-to-read group read-all)))
4064
4065     (cond
4066      ((null articles)
4067       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4068       'quit)
4069      ((eq articles 0) nil)
4070      (t
4071       ;; Init the dependencies hash table.
4072       (setq gnus-newsgroup-dependencies
4073             (gnus-make-hashtable (length articles)))
4074       (gnus-set-global-variables)
4075       ;; Retrieve the headers and read them in.
4076       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4077       (setq gnus-newsgroup-headers
4078             (gnus-retrieve-parsed-headers
4079              articles gnus-newsgroup-name
4080              ;; We might want to fetch old headers, but
4081              ;; not if there is only 1 article.
4082              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4083                            (not (numberp gnus-fetch-old-headers)))
4084                       (> (length articles) 1))
4085                   gnus-fetch-old-headers)))
4086       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4087
4088       ;; Suppress duplicates?
4089       (when gnus-suppress-duplicates
4090         (gnus-dup-suppress-articles))
4091
4092       ;; Set the initial limit.
4093       (setq gnus-newsgroup-limit (copy-sequence articles))
4094       ;; Remove canceled articles from the list of unread articles.
4095       (setq gnus-newsgroup-unreads
4096             (gnus-set-sorted-intersection
4097              gnus-newsgroup-unreads
4098              (setq fetched-articles
4099                    (mapcar (lambda (headers) (mail-header-number headers))
4100                            gnus-newsgroup-headers))))
4101       ;; Removed marked articles that do not exist.
4102       (gnus-update-missing-marks
4103        (gnus-sorted-complement fetched-articles articles))
4104
4105       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4106       (when cached
4107         (setq gnus-newsgroup-cached cached))
4108
4109       ;; We might want to build some more threads first.
4110       (when (and gnus-fetch-old-headers
4111                  (eq gnus-headers-retrieved-by 'nov))
4112         (if (eq gnus-fetch-old-headers 'invisible)
4113             (gnus-build-all-threads)
4114           (gnus-build-old-threads)))
4115       ;; Let the Gnus agent mark articles as read.
4116       (when gnus-agent
4117         (gnus-agent-get-undownloaded-list))
4118       ;; Check whether auto-expire is to be done in this group.
4119       (setq gnus-newsgroup-auto-expire
4120             (gnus-group-auto-expirable-p group))
4121       ;; Set up the article buffer now, if necessary.
4122       (unless gnus-single-article-buffer
4123         (gnus-article-setup-buffer))
4124       ;; First and last article in this newsgroup.
4125       (when gnus-newsgroup-headers
4126         (setq gnus-newsgroup-begin
4127               (mail-header-number (car gnus-newsgroup-headers))
4128               gnus-newsgroup-end
4129               (mail-header-number
4130                (gnus-last-element gnus-newsgroup-headers))))
4131       ;; GROUP is successfully selected.
4132       (or gnus-newsgroup-headers t)))))
4133
4134 (defun gnus-articles-to-read (group &optional read-all)
4135   ;; Find out what articles the user wants to read.
4136   (let* ((articles
4137           ;; Select all articles if `read-all' is non-nil, or if there
4138           ;; are no unread articles.
4139           (if (or read-all
4140                   (and (zerop (length gnus-newsgroup-marked))
4141                        (zerop (length gnus-newsgroup-unreads)))
4142                   (eq (gnus-group-find-parameter group 'display)
4143                       'all))
4144               (gnus-uncompress-range (gnus-active group))
4145             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4146                           (copy-sequence gnus-newsgroup-unreads))
4147                   '<)))
4148          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4149          (scored (length scored-list))
4150          (number (length articles))
4151          (marked (+ (length gnus-newsgroup-marked)
4152                     (length gnus-newsgroup-dormant)))
4153          (select
4154           (cond
4155            ((numberp read-all)
4156             read-all)
4157            (t
4158             (condition-case ()
4159                 (cond
4160                  ((and (or (<= scored marked) (= scored number))
4161                        (natnump gnus-large-newsgroup)
4162                        (> number gnus-large-newsgroup))
4163                   (let ((input (read-from-minibuffer
4164                                 (format
4165                                  "How many articles from %s (max %d): "
4166                                  (gnus-limit-string gnus-newsgroup-name 35)
4167                                  number)
4168                                 (static-if (< emacs-major-version 20)
4169                                     (number-to-string gnus-large-newsgroup)
4170                                   (cons
4171                                    (number-to-string gnus-large-newsgroup)
4172                                    0)))))
4173                     (if (string-match "^[ \t]*$" input)
4174                         number
4175                       input)))
4176                  ((and (> scored marked) (< scored number)
4177                        (> (- scored number) 20))
4178                   (let ((input
4179                          (read-string
4180                           (format "%s %s (%d scored, %d total): "
4181                                   "How many articles from"
4182                                   group scored number))))
4183                     (if (string-match "^[ \t]*$" input)
4184                         number input)))
4185                  (t number))
4186               (quit nil))))))
4187     (setq select (if (stringp select) (string-to-number select) select))
4188     (if (or (null select) (zerop select))
4189         select
4190       (if (and (not (zerop scored)) (<= (abs select) scored))
4191           (progn
4192             (setq articles (sort scored-list '<))
4193             (setq number (length articles)))
4194         (setq articles (copy-sequence articles)))
4195
4196       (when (< (abs select) number)
4197         (if (< select 0)
4198             ;; Select the N oldest articles.
4199             (setcdr (nthcdr (1- (abs select)) articles) nil)
4200           ;; Select the N most recent articles.
4201           (setq articles (nthcdr (- number select) articles))))
4202       (setq gnus-newsgroup-unselected
4203             (gnus-sorted-intersection
4204              gnus-newsgroup-unreads
4205              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4206       articles)))
4207
4208 (defun gnus-killed-articles (killed articles)
4209   (let (out)
4210     (while articles
4211       (when (inline (gnus-member-of-range (car articles) killed))
4212         (push (car articles) out))
4213       (setq articles (cdr articles)))
4214     out))
4215
4216 (defun gnus-uncompress-marks (marks)
4217   "Uncompress the mark ranges in MARKS."
4218   (let ((uncompressed '(score bookmark))
4219         out)
4220     (while marks
4221       (if (memq (caar marks) uncompressed)
4222           (push (car marks) out)
4223         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4224       (setq marks (cdr marks)))
4225     out))
4226
4227 (defun gnus-adjust-marked-articles (info)
4228   "Set all article lists and remove all marks that are no longer valid."
4229   (let* ((marked-lists (gnus-info-marks info))
4230          (active (gnus-active (gnus-info-group info)))
4231          (min (car active))
4232          (max (cdr active))
4233          (types gnus-article-mark-lists)
4234          (uncompressed '(score bookmark killed))
4235          marks var articles article mark)
4236
4237     (while marked-lists
4238       (setq marks (pop marked-lists))
4239       (set (setq var (intern (format "gnus-newsgroup-%s"
4240                                      (car (rassq (setq mark (car marks))
4241                                                  types)))))
4242            (if (memq (car marks) uncompressed) (cdr marks)
4243              (gnus-uncompress-range (cdr marks))))
4244
4245       (setq articles (symbol-value var))
4246
4247       ;; All articles have to be subsets of the active articles.
4248       (cond
4249        ;; Adjust "simple" lists.
4250        ((memq mark '(tick dormant expire reply save))
4251         (while articles
4252           (when (or (< (setq article (pop articles)) min) (> article max))
4253             (set var (delq article (symbol-value var))))))
4254        ;; Adjust assocs.
4255        ((memq mark uncompressed)
4256         (when (not (listp (cdr (symbol-value var))))
4257           (set var (list (symbol-value var))))
4258         (when (not (listp (cdr articles)))
4259           (setq articles (list articles)))
4260         (while articles
4261           (when (or (not (consp (setq article (pop articles))))
4262                     (< (car article) min)
4263                     (> (car article) max))
4264             (set var (delq article (symbol-value var))))))))))
4265
4266 (defun gnus-update-missing-marks (missing)
4267   "Go through the list of MISSING articles and remove them from the mark lists."
4268   (when missing
4269     (let ((types gnus-article-mark-lists)
4270           var m)
4271       ;; Go through all types.
4272       (while types
4273         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4274         (when (symbol-value var)
4275           ;; This list has articles.  So we delete all missing articles
4276           ;; from it.
4277           (setq m missing)
4278           (while m
4279             (set var (delq (pop m) (symbol-value var)))))))))
4280
4281 (defun gnus-update-marks ()
4282   "Enter the various lists of marked articles into the newsgroup info list."
4283   (let ((types gnus-article-mark-lists)
4284         (info (gnus-get-info gnus-newsgroup-name))
4285         (uncompressed '(score bookmark killed))
4286         type list newmarked symbol delta-marks)
4287     (when info
4288       ;; Add all marks lists to the list of marks lists.
4289       (while (setq type (pop types))
4290         (setq list (symbol-value
4291                           (setq symbol
4292                                 (intern (format "gnus-newsgroup-%s"
4293                                                 (car type))))))
4294
4295         (when list
4296           ;; Get rid of the entries of the articles that have the
4297           ;; default score.
4298           (when (and (eq (cdr type) 'score)
4299                      gnus-save-score
4300                      list)
4301             (let* ((arts list)
4302                    (prev (cons nil list))
4303                    (all prev))
4304               (while arts
4305                 (if (or (not (consp (car arts)))
4306                         (= (cdar arts) gnus-summary-default-score))
4307                     (setcdr prev (cdr arts))
4308                   (setq prev arts))
4309                 (setq arts (cdr arts)))
4310               (setq list (cdr all)))))
4311
4312        (or (memq (cdr type) uncompressed)
4313            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4314        
4315        (when (gnus-check-backend-function 'request-set-mark
4316                                           gnus-newsgroup-name)
4317          ;; uncompressed:s are not proper flags (they are cons cells)
4318          ;; cache is a internal gnus flag
4319          (unless (memq (cdr type) (cons 'cache uncompressed))
4320            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4321                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4322                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4323              (if add
4324                  (push (list add 'add (list (cdr type))) delta-marks))
4325              (if del
4326                  (push (list del 'del (list (cdr type))) delta-marks)))))
4327           
4328         (when list
4329          (push (cons (cdr type) list) newmarked)))
4330
4331       (when delta-marks
4332         (unless (gnus-check-group gnus-newsgroup-name)
4333           (error "Can't open server for %s" gnus-newsgroup-name))
4334         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4335           
4336       ;; Enter these new marks into the info of the group.
4337       (if (nthcdr 3 info)
4338           (setcar (nthcdr 3 info) newmarked)
4339         ;; Add the marks lists to the end of the info.
4340         (when newmarked
4341           (setcdr (nthcdr 2 info) (list newmarked))))
4342
4343       ;; Cut off the end of the info if there's nothing else there.
4344       (let ((i 5))
4345         (while (and (> i 2)
4346                     (not (nth i info)))
4347           (when (nthcdr (decf i) info)
4348             (setcdr (nthcdr i info) nil)))))))
4349
4350 (defun gnus-set-mode-line (where)
4351   "This function sets the mode line of the article or summary buffers.
4352 If WHERE is `summary', the summary mode line format will be used."
4353   ;; Is this mode line one we keep updated?
4354   (when (and (memq where gnus-updated-mode-lines)
4355              (symbol-value
4356               (intern (format "gnus-%s-mode-line-format-spec" where))))
4357     (let (mode-string)
4358       (save-excursion
4359         ;; We evaluate this in the summary buffer since these
4360         ;; variables are buffer-local to that buffer.
4361         (set-buffer gnus-summary-buffer)
4362         ;; We bind all these variables that are used in the `eval' form
4363         ;; below.
4364         (let* ((mformat (symbol-value
4365                          (intern
4366                           (format "gnus-%s-mode-line-format-spec" where))))
4367                (gnus-tmp-group-name gnus-newsgroup-name)
4368                (gnus-tmp-article-number (or gnus-current-article 0))
4369                (gnus-tmp-unread gnus-newsgroup-unreads)
4370                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4371                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4372                (gnus-tmp-unread-and-unselected
4373                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4374                             (zerop gnus-tmp-unselected))
4375                        "")
4376                       ((zerop gnus-tmp-unselected)
4377                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4378                       (t (format "{%d(+%d) more}"
4379                                  gnus-tmp-unread-and-unticked
4380                                  gnus-tmp-unselected))))
4381                (gnus-tmp-subject
4382                 (if (and gnus-current-headers
4383                          (vectorp gnus-current-headers))
4384                     (gnus-mode-string-quote
4385                      (mail-header-subject gnus-current-headers))
4386                   ""))
4387                bufname-length max-len
4388                gnus-tmp-header);; passed as argument to any user-format-funcs
4389           (setq mode-string (eval mformat))
4390           (setq bufname-length (if (string-match "%b" mode-string)
4391                                    (- (length
4392                                        (buffer-name
4393                                         (if (eq where 'summary)
4394                                             nil
4395                                           (get-buffer gnus-article-buffer))))
4396                                       2)
4397                                  0))
4398           (setq max-len (max 4 (if gnus-mode-non-string-length
4399                                    (- (window-width)
4400                                       gnus-mode-non-string-length
4401                                       bufname-length)
4402                                  (length mode-string))))
4403           ;; We might have to chop a bit of the string off...
4404           (when (> (length mode-string) max-len)
4405             (setq mode-string
4406                   (concat (gnus-truncate-string mode-string (- max-len 3))
4407                           "...")))
4408           ;; Pad the mode string a bit.
4409           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4410       ;; Update the mode line.
4411       (setq mode-line-buffer-identification
4412             (gnus-mode-line-buffer-identification (list mode-string)))
4413       (set-buffer-modified-p t))))
4414
4415 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4416   "Go through the HEADERS list and add all Xrefs to a hash table.
4417 The resulting hash table is returned, or nil if no Xrefs were found."
4418   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4419          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4420          (xref-hashtb (gnus-make-hashtable))
4421          start group entry number xrefs header)
4422     (while headers
4423       (setq header (pop headers))
4424       (when (and (setq xrefs (mail-header-xref header))
4425                  (not (memq (setq number (mail-header-number header))
4426                             unreads)))
4427         (setq start 0)
4428         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4429           (setq start (match-end 0))
4430           (setq group (if prefix
4431                           (concat prefix (substring xrefs (match-beginning 1)
4432                                                     (match-end 1)))
4433                         (substring xrefs (match-beginning 1) (match-end 1))))
4434           (setq number
4435                 (string-to-int (substring xrefs (match-beginning 2)
4436                                           (match-end 2))))
4437           (if (setq entry (gnus-gethash group xref-hashtb))
4438               (setcdr entry (cons number (cdr entry)))
4439             (gnus-sethash group (cons number nil) xref-hashtb)))))
4440     (and start xref-hashtb)))
4441
4442 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4443   "Look through all the headers and mark the Xrefs as read."
4444   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4445         name entry info xref-hashtb idlist method nth4)
4446     (save-excursion
4447       (set-buffer gnus-group-buffer)
4448       (when (setq xref-hashtb
4449                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4450         (mapatoms
4451          (lambda (group)
4452            (unless (string= from-newsgroup (setq name (symbol-name group)))
4453              (setq idlist (symbol-value group))
4454              ;; Dead groups are not updated.
4455              (and (prog1
4456                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4457                             info (nth 2 entry))
4458                     (when (stringp (setq nth4 (gnus-info-method info)))
4459                       (setq nth4 (gnus-server-to-method nth4))))
4460                   ;; Only do the xrefs if the group has the same
4461                   ;; select method as the group we have just read.
4462                   (or (gnus-methods-equal-p
4463                        nth4 (gnus-find-method-for-group from-newsgroup))
4464                       virtual
4465                       (equal nth4 (setq method (gnus-find-method-for-group
4466                                                 from-newsgroup)))
4467                       (and (equal (car nth4) (car method))
4468                            (equal (nth 1 nth4) (nth 1 method))))
4469                   gnus-use-cross-reference
4470                   (or (not (eq gnus-use-cross-reference t))
4471                       virtual
4472                       ;; Only do cross-references on subscribed
4473                       ;; groups, if that is what is wanted.
4474                       (<= (gnus-info-level info) gnus-level-subscribed))
4475                   (gnus-group-make-articles-read name idlist))))
4476          xref-hashtb)))))
4477
4478 (defun gnus-compute-read-articles (group articles)
4479   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4480          (info (nth 2 entry))
4481          (active (gnus-active group))
4482          ninfo)
4483     (when entry
4484       ;; First peel off all invalid article numbers.
4485       (when active
4486         (let ((ids articles)
4487               id first)
4488           (while (setq id (pop ids))
4489             (when (and first (> id (cdr active)))
4490               ;; We'll end up in this situation in one particular
4491               ;; obscure situation.  If you re-scan a group and get
4492               ;; a new article that is cross-posted to a different
4493               ;; group that has not been re-scanned, you might get
4494               ;; crossposted article that has a higher number than
4495               ;; Gnus believes possible.  So we re-activate this
4496               ;; group as well.  This might mean doing the
4497               ;; crossposting thingy will *increase* the number
4498               ;; of articles in some groups.  Tsk, tsk.
4499               (setq active (or (gnus-activate-group group) active)))
4500             (when (or (> id (cdr active))
4501                       (< id (car active)))
4502               (setq articles (delq id articles))))))
4503       ;; If the read list is nil, we init it.
4504       (if (and active
4505                (null (gnus-info-read info))
4506                (> (car active) 1))
4507           (setq ninfo (cons 1 (1- (car active))))
4508         (setq ninfo (gnus-info-read info)))
4509       ;; Then we add the read articles to the range.
4510       (gnus-add-to-range
4511        ninfo (setq articles (sort articles '<))))))
4512
4513 (defun gnus-group-make-articles-read (group articles)
4514   "Update the info of GROUP to say that ARTICLES are read."
4515   (let* ((num 0)
4516          (entry (gnus-gethash group gnus-newsrc-hashtb))
4517          (info (nth 2 entry))
4518          (active (gnus-active group))
4519          range)
4520     (when entry
4521       (setq range (gnus-compute-read-articles group articles))
4522       (save-excursion
4523         (set-buffer gnus-group-buffer)
4524         (gnus-undo-register
4525           `(progn
4526              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4527              (gnus-info-set-read ',info ',(gnus-info-read info))
4528              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4529              (gnus-group-update-group ,group t))))
4530       ;; Add the read articles to the range.
4531       (gnus-info-set-read info range)
4532       ;; Then we have to re-compute how many unread
4533       ;; articles there are in this group.
4534       (when active
4535         (cond
4536          ((not range)
4537           (setq num (- (1+ (cdr active)) (car active))))
4538          ((not (listp (cdr range)))
4539           (setq num (- (cdr active) (- (1+ (cdr range))
4540                                        (car range)))))
4541          (t
4542           (while range
4543             (if (numberp (car range))
4544                 (setq num (1+ num))
4545               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4546             (setq range (cdr range)))
4547           (setq num (- (cdr active) num))))
4548         ;; Update the number of unread articles.
4549         (setcar entry num)
4550         ;; Update the group buffer.
4551         (gnus-group-update-group group t)))))
4552
4553 (defvar gnus-newsgroup-none-id 0)
4554
4555 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4556   (let ((cur nntp-server-buffer)
4557         (dependencies
4558          (or dependencies
4559              (save-excursion (set-buffer gnus-summary-buffer)
4560                              gnus-newsgroup-dependencies)))
4561         headers id end ref
4562         (mail-parse-charset gnus-newsgroup-charset)
4563         (mail-parse-ignored-charsets 
4564          (or (and (gnus-buffer-live-p gnus-summary-buffer)
4565                   (save-excursion (set-buffer gnus-summary-buffer)
4566                                   gnus-newsgroup-ignored-charsets))
4567              gnus-newsgroup-ignored-charsets)))
4568     (save-excursion
4569       (set-buffer nntp-server-buffer)
4570       ;; Translate all TAB characters into SPACE characters.
4571       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4572       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4573       (gnus-run-hooks 'gnus-parse-headers-hook)
4574       (let ((case-fold-search t)
4575             in-reply-to header p lines chars ctype)
4576         (goto-char (point-min))
4577         ;; Search to the beginning of the next header.  Error messages
4578         ;; do not begin with 2 or 3.
4579         (while (re-search-forward "^[23][0-9]+ " nil t)
4580           (setq id nil
4581                 ref nil)
4582           ;; This implementation of this function, with nine
4583           ;; search-forwards instead of the one re-search-forward and
4584           ;; a case (which basically was the old function) is actually
4585           ;; about twice as fast, even though it looks messier.  You
4586           ;; can't have everything, I guess.  Speed and elegance
4587           ;; doesn't always go hand in hand.
4588           (setq
4589            header
4590            (make-full-mail-header
4591             ;; Number.
4592             (prog1
4593                 (read cur)
4594               (end-of-line)
4595               (setq p (point))
4596               (narrow-to-region (point)
4597                                 (or (and (search-forward "\n.\n" nil t)
4598                                          (- (point) 2))
4599                                     (point))))
4600             ;; Subject.
4601             (progn
4602               (goto-char p)
4603               (if (search-forward "\nsubject: " nil t)
4604                   (buffer-substring (match-end 0) (std11-field-end))
4605                 "(none)"))
4606             ;; From.
4607             (progn
4608               (goto-char p)
4609               (if (search-forward "\nfrom: " nil t)
4610                   (buffer-substring (match-end 0) (std11-field-end))
4611                 "(nobody)"))
4612             ;; Date.
4613             (progn
4614               (goto-char p)
4615               (if (search-forward "\ndate: " nil t)
4616                   (buffer-substring (match-end 0) (std11-field-end))
4617                 ""))
4618             ;; Message-ID.
4619             (progn
4620               (goto-char p)
4621               (setq id (if (re-search-forward
4622                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4623                            ;; We do it this way to make sure the Message-ID
4624                            ;; is (somewhat) syntactically valid.
4625                            (buffer-substring (match-beginning 1)
4626                                              (match-end 1))
4627                          ;; If there was no message-id, we just fake one
4628                          ;; to make subsequent routines simpler.
4629                          (nnheader-generate-fake-message-id))))
4630             ;; References.
4631             (progn
4632               (goto-char p)
4633               (if (search-forward "\nreferences: " nil t)
4634                   (progn
4635                     (setq end (point))
4636                     (prog1
4637                         (buffer-substring (match-end 0) (std11-field-end))
4638                       (setq ref
4639                             (buffer-substring
4640                              (progn
4641                                ;; (end-of-line)
4642                                (search-backward ">" end t)
4643                                (1+ (point)))
4644                              (progn
4645                                (search-backward "<" end t)
4646                                (point))))))
4647                 ;; Get the references from the in-reply-to header if there
4648                 ;; were no references and the in-reply-to header looks
4649                 ;; promising.
4650                 (if (and (search-forward "\nin-reply-to: " nil t)
4651                          (setq in-reply-to
4652                                (buffer-substring (match-end 0)
4653                                                  (std11-field-end)))
4654                          (string-match "<[^>]+>" in-reply-to))
4655                     (let (ref2)
4656                       (setq ref (substring in-reply-to (match-beginning 0)
4657                                            (match-end 0)))
4658                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4659                         (setq ref2 (substring in-reply-to (match-beginning 0)
4660                                               (match-end 0)))
4661                         (when (> (length ref2) (length ref))
4662                           (setq ref ref2)))
4663                       ref)
4664                   (setq ref nil))))
4665             ;; Chars.
4666             (progn
4667               (goto-char p)
4668               (if (search-forward "\nchars: " nil t)
4669                   (if (numberp (setq chars (ignore-errors (read cur))))
4670                       chars 0)
4671                 0))
4672             ;; Lines.
4673             (progn
4674               (goto-char p)
4675               (if (search-forward "\nlines: " nil t)
4676                   (if (numberp (setq lines (ignore-errors (read cur))))
4677                       lines 0)
4678                 0))
4679             ;; Xref.
4680             (progn
4681               (goto-char p)
4682               (and (search-forward "\nxref: " nil t)
4683                    (buffer-substring (match-end 0) (std11-field-end))))
4684             ;; Extra.
4685             (when gnus-extra-headers
4686               (let ((extra gnus-extra-headers)
4687                     out)
4688                 (while extra
4689                   (goto-char p)
4690                   (when (search-forward
4691                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4692                     (push (cons (car extra)
4693                                 (buffer-substring (match-end 0)
4694                                                   (std11-field-end)))
4695                           out))
4696                   (pop extra))
4697                 out))))
4698           (goto-char p)
4699           (if (and (search-forward "\ncontent-type: " nil t)
4700                    (setq ctype
4701                          (buffer-substring (match-end 0) (std11-field-end))))
4702               (mime-entity-set-content-type-internal
4703                header (mime-parse-Content-Type ctype)))
4704           (when (equal id ref)
4705             (setq ref nil))
4706
4707           (when gnus-alter-header-function
4708             (funcall gnus-alter-header-function header)
4709             (setq id (mail-header-id header)
4710                   ref (gnus-parent-id (mail-header-references header))))
4711
4712           (when (setq header
4713                       (gnus-dependencies-add-header
4714                        header dependencies force-new))
4715             (push header headers))
4716           (goto-char (point-max))
4717           (widen))
4718         (nreverse headers)))))
4719
4720 ;; Goes through the xover lines and returns a list of vectors
4721 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4722                                                   force-new dependencies
4723                                                   group also-fetch-heads)
4724   "Parse the news overview data in the server buffer, and return a
4725 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4726   ;; Get the Xref when the users reads the articles since most/some
4727   ;; NNTP servers do not include Xrefs when using XOVER.
4728   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4729   (let ((mail-parse-charset gnus-newsgroup-charset)
4730         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4731         (cur nntp-server-buffer)
4732         (dependencies (or dependencies gnus-newsgroup-dependencies))
4733         number headers header)
4734     (save-excursion
4735       (set-buffer nntp-server-buffer)
4736       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4737       ;; Allow the user to mangle the headers before parsing them.
4738       (gnus-run-hooks 'gnus-parse-headers-hook)
4739       (goto-char (point-min))
4740       (while (not (eobp))
4741         (condition-case ()
4742             (while (and sequence (not (eobp)))
4743               (setq number (read cur))
4744               (while (and sequence
4745                           (< (car sequence) number))
4746                 (setq sequence (cdr sequence)))
4747               (and sequence
4748                    (eq number (car sequence))
4749                    (progn
4750                      (setq sequence (cdr sequence))
4751                      (setq header (inline
4752                                     (gnus-nov-parse-line
4753                                      number dependencies force-new))))
4754                    (push header headers))
4755               (forward-line 1))
4756           (error
4757            (gnus-error 4 "Strange nov line (%d)"
4758                        (count-lines (point-min) (point)))))
4759         (forward-line 1))
4760       ;; A common bug in inn is that if you have posted an article and
4761       ;; then retrieves the active file, it will answer correctly --
4762       ;; the new article is included.  However, a NOV entry for the
4763       ;; article may not have been generated yet, so this may fail.
4764       ;; We work around this problem by retrieving the last few
4765       ;; headers using HEAD.
4766       (if (or (not also-fetch-heads)
4767               (not sequence))
4768           ;; We (probably) got all the headers.
4769           (nreverse headers)
4770         (let ((gnus-nov-is-evil t))
4771           (nconc
4772            (nreverse headers)
4773            (gnus-retrieve-parsed-headers sequence group)
4774            ))))))
4775
4776 (defun gnus-article-get-xrefs ()
4777   "Fill in the Xref value in `gnus-current-headers', if necessary.
4778 This is meant to be called in `gnus-article-internal-prepare-hook'."
4779   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4780                                  gnus-current-headers)))
4781     (or (not gnus-use-cross-reference)
4782         (not headers)
4783         (and (mail-header-xref headers)
4784              (not (string= (mail-header-xref headers) "")))
4785         (let ((case-fold-search t)
4786               xref)
4787           (save-restriction
4788             (nnheader-narrow-to-headers)
4789             (goto-char (point-min))
4790             (when (or (and (eq (downcase (char-after)) ?x)
4791                            (looking-at "Xref:"))
4792                       (search-forward "\nXref:" nil t))
4793               (goto-char (1+ (match-end 0)))
4794               (setq xref (buffer-substring (point)
4795                                            (progn (end-of-line) (point))))
4796               (mail-header-set-xref headers xref)))))))
4797
4798 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4799   "Find article ID and insert the summary line for that article.
4800 OLD-HEADER can either be a header or a line number to insert
4801 the subject line on."
4802   (let* ((line (and (numberp old-header) old-header))
4803          (old-header (and (vectorp old-header) old-header))
4804          (header (cond ((and old-header use-old-header)
4805                         old-header)
4806                        ((and (numberp id)
4807                              (gnus-number-to-header id))
4808                         (gnus-number-to-header id))
4809                        (t
4810                         (gnus-read-header id))))
4811          (number (and (numberp id) id))
4812          d)
4813     (when header
4814       ;; Rebuild the thread that this article is part of and go to the
4815       ;; article we have fetched.
4816       (when (and (not gnus-show-threads)
4817                  old-header)
4818         (when (and number
4819                    (setq d (gnus-data-find (mail-header-number old-header))))
4820           (goto-char (gnus-data-pos d))
4821           (gnus-data-remove
4822            number
4823            (- (gnus-point-at-bol)
4824               (prog1
4825                   (1+ (gnus-point-at-eol))
4826                 (gnus-delete-line))))))
4827       (when old-header
4828         (mail-header-set-number header (mail-header-number old-header)))
4829       (setq gnus-newsgroup-sparse
4830             (delq (setq number (mail-header-number header))
4831                   gnus-newsgroup-sparse))
4832       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4833       (push number gnus-newsgroup-limit)
4834       (gnus-rebuild-thread (mail-header-id header) line)
4835       (gnus-summary-goto-subject number nil t))
4836     (when (and (numberp number)
4837                (> number 0))
4838       ;; We have to update the boundaries even if we can't fetch the
4839       ;; article if ID is a number -- so that the next `P' or `N'
4840       ;; command will fetch the previous (or next) article even
4841       ;; if the one we tried to fetch this time has been canceled.
4842       (when (> number gnus-newsgroup-end)
4843         (setq gnus-newsgroup-end number))
4844       (when (< number gnus-newsgroup-begin)
4845         (setq gnus-newsgroup-begin number))
4846       (setq gnus-newsgroup-unselected
4847             (delq number gnus-newsgroup-unselected)))
4848     ;; Report back a success?
4849     (and header (mail-header-number header))))
4850
4851 ;;; Process/prefix in the summary buffer
4852
4853 (defun gnus-summary-work-articles (n)
4854   "Return a list of articles to be worked upon.
4855 The prefix argument, the list of process marked articles, and the
4856 current article will be taken into consideration."
4857   (save-excursion
4858     (set-buffer gnus-summary-buffer)
4859     (cond
4860      (n
4861       ;; A numerical prefix has been given.
4862       (setq n (prefix-numeric-value n))
4863       (let ((backward (< n 0))
4864             (n (abs (prefix-numeric-value n)))
4865             articles article)
4866         (save-excursion
4867           (while
4868               (and (> n 0)
4869                    (push (setq article (gnus-summary-article-number))
4870                          articles)
4871                    (if backward
4872                        (gnus-summary-find-prev nil article)
4873                      (gnus-summary-find-next nil article)))
4874             (decf n)))
4875         (nreverse articles)))
4876      ((and (gnus-region-active-p) (mark))
4877       (message "region active")
4878       ;; Work on the region between point and mark.
4879       (let ((max (max (point) (mark)))
4880             articles article)
4881         (save-excursion
4882           (goto-char (min (point) (mark)))
4883           (while
4884               (and
4885                (push (setq article (gnus-summary-article-number)) articles)
4886                (gnus-summary-find-next nil article)
4887                (< (point) max)))
4888           (nreverse articles))))
4889      (gnus-newsgroup-processable
4890       ;; There are process-marked articles present.
4891       ;; Save current state.
4892       (gnus-summary-save-process-mark)
4893       ;; Return the list.
4894       (reverse gnus-newsgroup-processable))
4895      (t
4896       ;; Just return the current article.
4897       (list (gnus-summary-article-number))))))
4898
4899 (defmacro gnus-summary-iterate (arg &rest forms)
4900   "Iterate over the process/prefixed articles and do FORMS.
4901 ARG is the interactive prefix given to the command.  FORMS will be
4902 executed with point over the summary line of the articles."
4903   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4904     `(let ((,articles (gnus-summary-work-articles ,arg)))
4905        (while ,articles
4906          (gnus-summary-goto-subject (car ,articles))
4907          ,@forms
4908          (pop ,articles)))))
4909
4910 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4911 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4912
4913 (defun gnus-summary-save-process-mark ()
4914   "Push the current set of process marked articles on the stack."
4915   (interactive)
4916   (push (copy-sequence gnus-newsgroup-processable)
4917         gnus-newsgroup-process-stack))
4918
4919 (defun gnus-summary-kill-process-mark ()
4920   "Push the current set of process marked articles on the stack and unmark."
4921   (interactive)
4922   (gnus-summary-save-process-mark)
4923   (gnus-summary-unmark-all-processable))
4924
4925 (defun gnus-summary-yank-process-mark ()
4926   "Pop the last process mark state off the stack and restore it."
4927   (interactive)
4928   (unless gnus-newsgroup-process-stack
4929     (error "Empty mark stack"))
4930   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4931
4932 (defun gnus-summary-process-mark-set (set)
4933   "Make SET into the current process marked articles."
4934   (gnus-summary-unmark-all-processable)
4935   (while set
4936     (gnus-summary-set-process-mark (pop set))))
4937
4938 ;;; Searching and stuff
4939
4940 (defun gnus-summary-search-group (&optional backward use-level)
4941   "Search for next unread newsgroup.
4942 If optional argument BACKWARD is non-nil, search backward instead."
4943   (save-excursion
4944     (set-buffer gnus-group-buffer)
4945     (when (gnus-group-search-forward
4946            backward nil (if use-level (gnus-group-group-level) nil))
4947       (gnus-group-group-name))))
4948
4949 (defun gnus-summary-best-group (&optional exclude-group)
4950   "Find the name of the best unread group.
4951 If EXCLUDE-GROUP, do not go to this group."
4952   (save-excursion
4953     (set-buffer gnus-group-buffer)
4954     (save-excursion
4955       (gnus-group-best-unread-group exclude-group))))
4956
4957 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4958   (if backward (gnus-summary-find-prev)
4959     (let* ((dummy (gnus-summary-article-intangible-p))
4960            (article (or article (gnus-summary-article-number)))
4961            (arts (gnus-data-find-list article))
4962            result)
4963       (when (and (not dummy)
4964                  (or (not gnus-summary-check-current)
4965                      (not unread)
4966                      (not (gnus-data-unread-p (car arts)))))
4967         (setq arts (cdr arts)))
4968       (when (setq result
4969                   (if unread
4970                       (progn
4971                         (while arts
4972                           (when (or (and undownloaded
4973                                          (eq gnus-undownloaded-mark
4974                                              (gnus-data-mark (car arts))))
4975                                     (gnus-data-unread-p (car arts)))
4976                             (setq result (car arts)
4977                                   arts nil))
4978                           (setq arts (cdr arts)))
4979                         result)
4980                     (car arts)))
4981         (goto-char (gnus-data-pos result))
4982         (gnus-data-number result)))))
4983
4984 (defun gnus-summary-find-prev (&optional unread article)
4985   (let* ((eobp (eobp))
4986          (article (or article (gnus-summary-article-number)))
4987          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4988          result)
4989     (when (and (not eobp)
4990                (or (not gnus-summary-check-current)
4991                    (not unread)
4992                    (not (gnus-data-unread-p (car arts)))))
4993       (setq arts (cdr arts)))
4994     (when (setq result
4995                 (if unread
4996                     (progn
4997                       (while arts
4998                         (when (gnus-data-unread-p (car arts))
4999                           (setq result (car arts)
5000                                 arts nil))
5001                         (setq arts (cdr arts)))
5002                       result)
5003                   (car arts)))
5004       (goto-char (gnus-data-pos result))
5005       (gnus-data-number result))))
5006
5007 (defun gnus-summary-find-subject (subject &optional unread backward article)
5008   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5009          (article (or article (gnus-summary-article-number)))
5010          (articles (gnus-data-list backward))
5011          (arts (gnus-data-find-list article articles))
5012          result)
5013     (when (or (not gnus-summary-check-current)
5014               (not unread)
5015               (not (gnus-data-unread-p (car arts))))
5016       (setq arts (cdr arts)))
5017     (while arts
5018       (and (or (not unread)
5019                (gnus-data-unread-p (car arts)))
5020            (vectorp (gnus-data-header (car arts)))
5021            (gnus-subject-equal
5022             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5023            (setq result (car arts)
5024                  arts nil))
5025       (setq arts (cdr arts)))
5026     (and result
5027          (goto-char (gnus-data-pos result))
5028          (gnus-data-number result))))
5029
5030 (defun gnus-summary-search-forward (&optional unread subject backward)
5031   "Search forward for an article.
5032 If UNREAD, look for unread articles.  If SUBJECT, look for
5033 articles with that subject.  If BACKWARD, search backward instead."
5034   (cond (subject (gnus-summary-find-subject subject unread backward))
5035         (backward (gnus-summary-find-prev unread))
5036         (t (gnus-summary-find-next unread))))
5037
5038 (defun gnus-recenter (&optional n)
5039   "Center point in window and redisplay frame.
5040 Also do horizontal recentering."
5041   (interactive "P")
5042   (when (and gnus-auto-center-summary
5043              (not (eq gnus-auto-center-summary 'vertical)))
5044     (gnus-horizontal-recenter))
5045   (recenter n))
5046
5047 (defun gnus-summary-recenter ()
5048   "Center point in the summary window.
5049 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5050 displayed, no centering will be performed."
5051   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5052   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5053   (let* ((top (cond ((< (window-height) 4) 0)
5054                     ((< (window-height) 7) 1)
5055                     (t (if (numberp gnus-auto-center-summary)
5056                            gnus-auto-center-summary
5057                          2))))
5058          (height (1- (window-height)))
5059          (bottom (save-excursion (goto-char (point-max))
5060                                  (forward-line (- height))
5061                                  (point)))
5062          (window (get-buffer-window (current-buffer))))
5063     ;; The user has to want it.
5064     (when gnus-auto-center-summary
5065       (when (get-buffer-window gnus-article-buffer)
5066         ;; Only do recentering when the article buffer is displayed,
5067         ;; Set the window start to either `bottom', which is the biggest
5068         ;; possible valid number, or the second line from the top,
5069         ;; whichever is the least.
5070         (set-window-start
5071          window (min bottom (save-excursion
5072                               (forward-line (- top)) (point)))))
5073       ;; Do horizontal recentering while we're at it.
5074       (when (and (get-buffer-window (current-buffer) t)
5075                  (not (eq gnus-auto-center-summary 'vertical)))
5076         (let ((selected (selected-window)))
5077           (select-window (get-buffer-window (current-buffer) t))
5078           (gnus-summary-position-point)
5079           (gnus-horizontal-recenter)
5080           (select-window selected))))))
5081
5082 (defun gnus-summary-jump-to-group (newsgroup)
5083   "Move point to NEWSGROUP in group mode buffer."
5084   ;; Keep update point of group mode buffer if visible.
5085   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5086       (save-window-excursion
5087         ;; Take care of tree window mode.
5088         (when (get-buffer-window gnus-group-buffer)
5089           (pop-to-buffer gnus-group-buffer))
5090         (gnus-group-jump-to-group newsgroup))
5091     (save-excursion
5092       ;; Take care of tree window mode.
5093       (if (get-buffer-window gnus-group-buffer)
5094           (pop-to-buffer gnus-group-buffer)
5095         (set-buffer gnus-group-buffer))
5096       (gnus-group-jump-to-group newsgroup))))
5097
5098 ;; This function returns a list of article numbers based on the
5099 ;; difference between the ranges of read articles in this group and
5100 ;; the range of active articles.
5101 (defun gnus-list-of-unread-articles (group)
5102   (let* ((read (gnus-info-read (gnus-get-info group)))
5103          (active (or (gnus-active group) (gnus-activate-group group)))
5104          (last (cdr active))
5105          first nlast unread)
5106     ;; If none are read, then all are unread.
5107     (if (not read)
5108         (setq first (car active))
5109       ;; If the range of read articles is a single range, then the
5110       ;; first unread article is the article after the last read
5111       ;; article.  Sounds logical, doesn't it?
5112       (if (not (listp (cdr read)))
5113           (setq first (max (car active) (1+ (cdr read))))
5114         ;; `read' is a list of ranges.
5115         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5116                                   (caar read)))
5117                   1)
5118           (setq first (car active)))
5119         (while read
5120           (when first
5121             (while (< first nlast)
5122               (push first unread)
5123               (setq first (1+ first))))
5124           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5125           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5126           (setq read (cdr read)))))
5127     ;; And add the last unread articles.
5128     (while (<= first last)
5129       (push first unread)
5130       (setq first (1+ first)))
5131     ;; Return the list of unread articles.
5132     (delq 0 (nreverse unread))))
5133
5134 (defun gnus-list-of-read-articles (group)
5135   "Return a list of unread, unticked and non-dormant articles."
5136   (let* ((info (gnus-get-info group))
5137          (marked (gnus-info-marks info))
5138          (active (gnus-active group)))
5139     (and info active
5140          (gnus-set-difference
5141           (gnus-sorted-complement
5142            (gnus-uncompress-range active)
5143            (gnus-list-of-unread-articles group))
5144           (append
5145            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5146            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5147
5148 ;; Various summary commands
5149
5150 (defun gnus-summary-select-article-buffer ()
5151   "Reconfigure windows to show article buffer."
5152   (interactive)
5153   (if (not (gnus-buffer-live-p gnus-article-buffer))
5154       (error "There is no article buffer for this summary buffer")
5155     (gnus-configure-windows 'article)
5156     (select-window (get-buffer-window gnus-article-buffer))))
5157
5158 (defun gnus-summary-universal-argument (arg)
5159   "Perform any operation on all articles that are process/prefixed."
5160   (interactive "P")
5161   (let ((articles (gnus-summary-work-articles arg))
5162         func article)
5163     (if (eq
5164          (setq
5165           func
5166           (key-binding
5167            (read-key-sequence
5168             (substitute-command-keys
5169              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5170              ))))
5171          'undefined)
5172         (gnus-error 1 "Undefined key")
5173       (save-excursion
5174         (while articles
5175           (gnus-summary-goto-subject (setq article (pop articles)))
5176           (let (gnus-newsgroup-processable)
5177             (command-execute func))
5178           (gnus-summary-remove-process-mark article)))))
5179   (gnus-summary-position-point))
5180
5181 (defun gnus-summary-toggle-truncation (&optional arg)
5182   "Toggle truncation of summary lines.
5183 With arg, turn line truncation on iff arg is positive."
5184   (interactive "P")
5185   (setq truncate-lines
5186         (if (null arg) (not truncate-lines)
5187           (> (prefix-numeric-value arg) 0)))
5188   (redraw-display))
5189
5190 (defun gnus-summary-reselect-current-group (&optional all rescan)
5191   "Rescan the current newsgroup, exit and then reselect it.
5192 The prefix argument ALL means to select all articles."
5193   (interactive "P")
5194   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5195     (error "Ephemeral groups can't be reselected"))
5196   (let ((current-subject (gnus-summary-article-number))
5197         (group gnus-newsgroup-name))
5198     (save-excursion
5199       (set-buffer gnus-group-buffer)
5200       ;; We have to adjust the point of group mode buffer because
5201       ;; point was moved to the next unread newsgroup by exiting.
5202       (gnus-summary-jump-to-group group)
5203       (when rescan
5204         (save-excursion
5205           (gnus-group-get-new-news-this-group 1))))
5206     (setq gnus-newsgroup-begin nil)
5207     (gnus-summary-exit)
5208     (gnus-group-read-group all t group)
5209     (gnus-summary-goto-subject current-subject nil t)))
5210
5211 (defun gnus-summary-rescan-group (&optional all)
5212   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5213   (interactive "P")
5214   (gnus-summary-reselect-current-group all t))
5215
5216 (defun gnus-summary-update-info (&optional non-destructive)
5217   (save-excursion
5218     (let ((group gnus-newsgroup-name))
5219       (when group
5220         (when gnus-newsgroup-kill-headers
5221           (setq gnus-newsgroup-killed
5222                 (gnus-compress-sequence
5223                  (nconc
5224                   (gnus-set-sorted-intersection
5225                    (gnus-uncompress-range gnus-newsgroup-killed)
5226                    (setq gnus-newsgroup-unselected
5227                          (sort gnus-newsgroup-unselected '<)))
5228                   (setq gnus-newsgroup-unreads
5229                         (sort gnus-newsgroup-unreads '<)))
5230                  t)))
5231         (unless (listp (cdr gnus-newsgroup-killed))
5232           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5233         (let ((headers gnus-newsgroup-headers))
5234           ;; Set the new ranges of read articles.
5235           (save-excursion
5236             (set-buffer gnus-group-buffer)
5237             (gnus-undo-force-boundary))
5238           (gnus-update-read-articles
5239            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5240           ;; Set the current article marks.
5241           (let ((gnus-newsgroup-scored
5242                  (if (and (not gnus-save-score)
5243                           (not non-destructive))
5244                      nil
5245                    gnus-newsgroup-scored)))
5246             (save-excursion
5247               (gnus-update-marks)))
5248           ;; Do the cross-ref thing.
5249           (when gnus-use-cross-reference
5250             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5251           ;; Do not switch windows but change the buffer to work.
5252           (set-buffer gnus-group-buffer)
5253           (unless (gnus-ephemeral-group-p group)
5254             (gnus-group-update-group group)))))))
5255
5256 (defun gnus-summary-save-newsrc (&optional force)
5257   "Save the current number of read/marked articles in the dribble buffer.
5258 The dribble buffer will then be saved.
5259 If FORCE (the prefix), also save the .newsrc file(s)."
5260   (interactive "P")
5261   (gnus-summary-update-info t)
5262   (if force
5263       (gnus-save-newsrc-file)
5264     (gnus-dribble-save)))
5265
5266 (defun gnus-summary-exit (&optional temporary)
5267   "Exit reading current newsgroup, and then return to group selection mode.
5268 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5269   (interactive)
5270   (gnus-set-global-variables)
5271   (gnus-kill-save-kill-buffer)
5272   (gnus-async-halt-prefetch)
5273   (let* ((group gnus-newsgroup-name)
5274          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5275          (mode major-mode)
5276          (group-point nil)
5277          (buf (current-buffer)))
5278     (unless quit-config
5279       ;; Do adaptive scoring, and possibly save score files.
5280       (when gnus-newsgroup-adaptive
5281         (gnus-score-adaptive))
5282       (when gnus-use-scoring
5283         (gnus-score-save)))
5284     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5285     ;; If we have several article buffers, we kill them at exit.
5286     (unless gnus-single-article-buffer
5287       (gnus-kill-buffer gnus-original-article-buffer)
5288       (setq gnus-article-current nil))
5289     (when gnus-use-cache
5290       (gnus-cache-possibly-remove-articles)
5291       (gnus-cache-save-buffers))
5292     (gnus-async-prefetch-remove-group group)
5293     (when gnus-suppress-duplicates
5294       (gnus-dup-enter-articles))
5295     (when gnus-use-trees
5296       (gnus-tree-close group))
5297     ;; Remove entries for this group.
5298     (nnmail-purge-split-history (gnus-group-real-name group))
5299     ;; Make all changes in this group permanent.
5300     (unless quit-config
5301       (gnus-run-hooks 'gnus-exit-group-hook)
5302       (gnus-summary-update-info))
5303     (gnus-close-group group)
5304     ;; Make sure where we were, and go to next newsgroup.
5305     (set-buffer gnus-group-buffer)
5306     (unless quit-config
5307       (gnus-group-jump-to-group group))
5308     (gnus-run-hooks 'gnus-summary-exit-hook)
5309     (unless (or quit-config
5310                 ;; If this group has disappeared from the summary
5311                 ;; buffer, don't skip forwards.
5312                 (not (string= group (gnus-group-group-name))))
5313       (gnus-group-next-unread-group 1))
5314     (setq group-point (point))
5315     (if temporary
5316         nil                             ;Nothing to do.
5317       ;; If we have several article buffers, we kill them at exit.
5318       (unless gnus-single-article-buffer
5319         (gnus-kill-buffer gnus-article-buffer)
5320         (gnus-kill-buffer gnus-original-article-buffer)
5321         (setq gnus-article-current nil))
5322       (set-buffer buf)
5323       (if (not gnus-kill-summary-on-exit)
5324           (gnus-deaden-summary)
5325         ;; We set all buffer-local variables to nil.  It is unclear why
5326         ;; this is needed, but if we don't, buffer-local variables are
5327         ;; not garbage-collected, it seems.  This would the lead to en
5328         ;; ever-growing Emacs.
5329         (gnus-summary-clear-local-variables)
5330         (when (get-buffer gnus-article-buffer)
5331           (bury-buffer gnus-article-buffer))
5332         ;; We clear the global counterparts of the buffer-local
5333         ;; variables as well, just to be on the safe side.
5334         (set-buffer gnus-group-buffer)
5335         (gnus-summary-clear-local-variables)
5336         ;; Return to group mode buffer.
5337         (when (eq mode 'gnus-summary-mode)
5338           (gnus-kill-buffer buf)))
5339       (setq gnus-current-select-method gnus-select-method)
5340       (pop-to-buffer gnus-group-buffer)
5341       (if (not quit-config)
5342           (progn
5343             (goto-char group-point)
5344             (gnus-configure-windows 'group 'force))
5345         (gnus-handle-ephemeral-exit quit-config))
5346       ;; Clear the current group name.
5347       (unless quit-config
5348         (setq gnus-newsgroup-name nil)))))
5349
5350 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5351 (defun gnus-summary-exit-no-update (&optional no-questions)
5352   "Quit reading current newsgroup without updating read article info."
5353   (interactive)
5354   (let* ((group gnus-newsgroup-name)
5355          (quit-config (gnus-group-quit-config group)))
5356     (when (or no-questions
5357               gnus-expert-user
5358               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5359       (gnus-async-halt-prefetch)
5360       (mapcar 'funcall
5361               (delq 'gnus-summary-expire-articles
5362                     (copy-list gnus-summary-prepare-exit-hook)))
5363       ;; If we have several article buffers, we kill them at exit.
5364       (unless gnus-single-article-buffer
5365         (gnus-kill-buffer gnus-article-buffer)
5366         (gnus-kill-buffer gnus-original-article-buffer)
5367         (setq gnus-article-current nil))
5368       (if (not gnus-kill-summary-on-exit)
5369           (gnus-deaden-summary)
5370         (gnus-close-group group)
5371         (gnus-summary-clear-local-variables)
5372         (set-buffer gnus-group-buffer)
5373         (gnus-summary-clear-local-variables)
5374         (when (get-buffer gnus-summary-buffer)
5375           (kill-buffer gnus-summary-buffer)))
5376       (unless gnus-single-article-buffer
5377         (setq gnus-article-current nil))
5378       (when gnus-use-trees
5379         (gnus-tree-close group))
5380       (gnus-async-prefetch-remove-group group)
5381       (when (get-buffer gnus-article-buffer)
5382         (bury-buffer gnus-article-buffer))
5383       ;; Return to the group buffer.
5384       (gnus-configure-windows 'group 'force)
5385       ;; Clear the current group name.
5386       (setq gnus-newsgroup-name nil)
5387       (when (equal (gnus-group-group-name) group)
5388         (gnus-group-next-unread-group 1))
5389       (when quit-config
5390         (gnus-handle-ephemeral-exit quit-config)))))
5391
5392 (defun gnus-handle-ephemeral-exit (quit-config)
5393   "Handle movement when leaving an ephemeral group.
5394 The state which existed when entering the ephemeral is reset."
5395   (if (not (buffer-name (car quit-config)))
5396       (gnus-configure-windows 'group 'force)
5397     (set-buffer (car quit-config))
5398     (cond ((eq major-mode 'gnus-summary-mode)
5399            (gnus-set-global-variables))
5400           ((eq major-mode 'gnus-article-mode)
5401            (save-excursion
5402              ;; The `gnus-summary-buffer' variable may point
5403              ;; to the old summary buffer when using a single
5404              ;; article buffer.
5405              (unless (gnus-buffer-live-p gnus-summary-buffer)
5406                (set-buffer gnus-group-buffer))
5407              (set-buffer gnus-summary-buffer)
5408              (gnus-set-global-variables))))
5409     (if (or (eq (cdr quit-config) 'article)
5410             (eq (cdr quit-config) 'pick))
5411         (progn
5412           ;; The current article may be from the ephemeral group
5413           ;; thus it is best that we reload this article
5414           (gnus-summary-show-article)
5415           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5416               (gnus-configure-windows 'pick 'force)
5417             (gnus-configure-windows (cdr quit-config) 'force)))
5418       (gnus-configure-windows (cdr quit-config) 'force))
5419     (when (eq major-mode 'gnus-summary-mode)
5420       (gnus-summary-next-subject 1 nil t)
5421       (gnus-summary-recenter)
5422       (gnus-summary-position-point))))
5423
5424 (defun gnus-summary-preview-mime-message ()
5425   "MIME decode and play this message."
5426   (interactive)
5427   (let ((gnus-break-pages nil)
5428         (gnus-show-mime t))
5429     (gnus-summary-select-article gnus-show-all-headers t))
5430   (select-window (get-buffer-window gnus-article-buffer)))
5431
5432 ;;; Dead summaries.
5433
5434 (defvar gnus-dead-summary-mode-map nil)
5435
5436 (unless gnus-dead-summary-mode-map
5437   (setq gnus-dead-summary-mode-map (make-keymap))
5438   (suppress-keymap gnus-dead-summary-mode-map)
5439   (substitute-key-definition
5440    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5441   (let ((keys '("\C-d" "\r" "\177" [delete])))
5442     (while keys
5443       (define-key gnus-dead-summary-mode-map
5444         (pop keys) 'gnus-summary-wake-up-the-dead))))
5445
5446 (defvar gnus-dead-summary-mode nil
5447   "Minor mode for Gnus summary buffers.")
5448
5449 (defun gnus-dead-summary-mode (&optional arg)
5450   "Minor mode for Gnus summary buffers."
5451   (interactive "P")
5452   (when (eq major-mode 'gnus-summary-mode)
5453     (make-local-variable 'gnus-dead-summary-mode)
5454     (setq gnus-dead-summary-mode
5455           (if (null arg) (not gnus-dead-summary-mode)
5456             (> (prefix-numeric-value arg) 0)))
5457     (when gnus-dead-summary-mode
5458       (gnus-add-minor-mode
5459        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5460
5461 (defun gnus-deaden-summary ()
5462   "Make the current summary buffer into a dead summary buffer."
5463   ;; Kill any previous dead summary buffer.
5464   (when (and gnus-dead-summary
5465              (buffer-name gnus-dead-summary))
5466     (save-excursion
5467       (set-buffer gnus-dead-summary)
5468       (when gnus-dead-summary-mode
5469         (kill-buffer (current-buffer)))))
5470   ;; Make this the current dead summary.
5471   (setq gnus-dead-summary (current-buffer))
5472   (gnus-dead-summary-mode 1)
5473   (let ((name (buffer-name)))
5474     (when (string-match "Summary" name)
5475       (rename-buffer
5476        (concat (substring name 0 (match-beginning 0)) "Dead "
5477                (substring name (match-beginning 0)))
5478        t))))
5479
5480 (defun gnus-kill-or-deaden-summary (buffer)
5481   "Kill or deaden the summary BUFFER."
5482   (save-excursion
5483     (when (and (buffer-name buffer)
5484                (not gnus-single-article-buffer))
5485       (save-excursion
5486         (set-buffer buffer)
5487         (gnus-kill-buffer gnus-article-buffer)
5488         (gnus-kill-buffer gnus-original-article-buffer)))
5489     (cond (gnus-kill-summary-on-exit
5490            (when (and gnus-use-trees
5491                       (gnus-buffer-exists-p buffer))
5492              (save-excursion
5493                (set-buffer buffer)
5494                (gnus-tree-close gnus-newsgroup-name)))
5495            (gnus-kill-buffer buffer))
5496           ((gnus-buffer-exists-p buffer)
5497            (save-excursion
5498              (set-buffer buffer)
5499              (gnus-deaden-summary))))))
5500
5501 (defun gnus-summary-wake-up-the-dead (&rest args)
5502   "Wake up the dead summary buffer."
5503   (interactive)
5504   (gnus-dead-summary-mode -1)
5505   (let ((name (buffer-name)))
5506     (when (string-match "Dead " name)
5507       (rename-buffer
5508        (concat (substring name 0 (match-beginning 0))
5509                (substring name (match-end 0)))
5510        t)))
5511   (gnus-message 3 "This dead summary is now alive again"))
5512
5513 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5514 (defun gnus-summary-fetch-faq (&optional faq-dir)
5515   "Fetch the FAQ for the current group.
5516 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5517 in."
5518   (interactive
5519    (list
5520     (when current-prefix-arg
5521       (completing-read
5522        "Faq dir: " (and (listp gnus-group-faq-directory)
5523                         (mapcar (lambda (file) (list file))
5524                                 gnus-group-faq-directory))))))
5525   (let (gnus-faq-buffer)
5526     (when (setq gnus-faq-buffer
5527                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5528       (gnus-configure-windows 'summary-faq))))
5529
5530 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5531 (defun gnus-summary-describe-group (&optional force)
5532   "Describe the current newsgroup."
5533   (interactive "P")
5534   (gnus-group-describe-group force gnus-newsgroup-name))
5535
5536 (defun gnus-summary-describe-briefly ()
5537   "Describe summary mode commands briefly."
5538   (interactive)
5539   (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")))
5540
5541 ;; Walking around group mode buffer from summary mode.
5542
5543 (defun gnus-summary-next-group (&optional no-article target-group backward)
5544   "Exit current newsgroup and then select next unread newsgroup.
5545 If prefix argument NO-ARTICLE is non-nil, no article is selected
5546 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5547 previous group instead."
5548   (interactive "P")
5549   ;; Stop pre-fetching.
5550   (gnus-async-halt-prefetch)
5551   (let ((current-group gnus-newsgroup-name)
5552         (current-buffer (current-buffer))
5553         entered)
5554     ;; First we semi-exit this group to update Xrefs and all variables.
5555     ;; We can't do a real exit, because the window conf must remain
5556     ;; the same in case the user is prompted for info, and we don't
5557     ;; want the window conf to change before that...
5558     (gnus-summary-exit t)
5559     (while (not entered)
5560       ;; Then we find what group we are supposed to enter.
5561       (set-buffer gnus-group-buffer)
5562       (gnus-group-jump-to-group current-group)
5563       (setq target-group
5564             (or target-group
5565                 (if (eq gnus-keep-same-level 'best)
5566                     (gnus-summary-best-group gnus-newsgroup-name)
5567                   (gnus-summary-search-group backward gnus-keep-same-level))))
5568       (if (not target-group)
5569           ;; There are no further groups, so we return to the group
5570           ;; buffer.
5571           (progn
5572             (gnus-message 5 "Returning to the group buffer")
5573             (setq entered t)
5574             (when (gnus-buffer-live-p current-buffer)
5575               (set-buffer current-buffer)
5576               (gnus-summary-exit))
5577             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5578         ;; We try to enter the target group.
5579         (gnus-group-jump-to-group target-group)
5580         (let ((unreads (gnus-group-group-unread)))
5581           (if (and (or (eq t unreads)
5582                        (and unreads (not (zerop unreads))))
5583                    (gnus-summary-read-group
5584                     target-group nil no-article
5585                     (and (buffer-name current-buffer) current-buffer)
5586                     nil backward))
5587               (setq entered t)
5588             (setq current-group target-group
5589                   target-group nil)))))))
5590
5591 (defun gnus-summary-prev-group (&optional no-article)
5592   "Exit current newsgroup and then select previous unread newsgroup.
5593 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5594   (interactive "P")
5595   (gnus-summary-next-group no-article nil t))
5596
5597 ;; Walking around summary lines.
5598
5599 (defun gnus-summary-first-subject (&optional unread undownloaded)
5600   "Go to the first unread subject.
5601 If UNREAD is non-nil, go to the first unread article.
5602 Returns the article selected or nil if there are no unread articles."
5603   (interactive "P")
5604   (prog1
5605       (cond
5606        ;; Empty summary.
5607        ((null gnus-newsgroup-data)
5608         (gnus-message 3 "No articles in the group")
5609         nil)
5610        ;; Pick the first article.
5611        ((not unread)
5612         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5613         (gnus-data-number (car gnus-newsgroup-data)))
5614        ;; No unread articles.
5615        ((null gnus-newsgroup-unreads)
5616         (gnus-message 3 "No more unread articles")
5617         nil)
5618        ;; Find the first unread article.
5619        (t
5620         (let ((data gnus-newsgroup-data))
5621           (while (and data
5622                       (and (not (and undownloaded
5623                                      (eq gnus-undownloaded-mark
5624                                          (gnus-data-mark (car data)))))
5625                            (not (gnus-data-unread-p (car data)))))
5626             (setq data (cdr data)))
5627           (when data
5628             (goto-char (gnus-data-pos (car data)))
5629             (gnus-data-number (car data))))))
5630     (gnus-summary-position-point)))
5631
5632 (defun gnus-summary-next-subject (n &optional unread dont-display)
5633   "Go to next N'th summary line.
5634 If N is negative, go to the previous N'th subject line.
5635 If UNREAD is non-nil, only unread articles are selected.
5636 The difference between N and the actual number of steps taken is
5637 returned."
5638   (interactive "p")
5639   (let ((backward (< n 0))
5640         (n (abs n)))
5641     (while (and (> n 0)
5642                 (if backward
5643                     (gnus-summary-find-prev unread)
5644                   (gnus-summary-find-next unread)))
5645       (gnus-summary-show-thread)
5646       (setq n (1- n)))
5647     (when (/= 0 n)
5648       (gnus-message 7 "No more%s articles"
5649                     (if unread " unread" "")))
5650     (unless dont-display
5651       (gnus-summary-recenter)
5652       (gnus-summary-position-point))
5653     n))
5654
5655 (defun gnus-summary-next-unread-subject (n)
5656   "Go to next N'th unread summary line."
5657   (interactive "p")
5658   (gnus-summary-next-subject n t))
5659
5660 (defun gnus-summary-prev-subject (n &optional unread)
5661   "Go to previous N'th summary line.
5662 If optional argument UNREAD is non-nil, only unread article is selected."
5663   (interactive "p")
5664   (gnus-summary-next-subject (- n) unread))
5665
5666 (defun gnus-summary-prev-unread-subject (n)
5667   "Go to previous N'th unread summary line."
5668   (interactive "p")
5669   (gnus-summary-next-subject (- n) t))
5670
5671 (defun gnus-summary-goto-subject (article &optional force silent)
5672   "Go the subject line of ARTICLE.
5673 If FORCE, also allow jumping to articles not currently shown."
5674   (interactive "nArticle number: ")
5675   (let ((b (point))
5676         (data (gnus-data-find article)))
5677     ;; We read in the article if we have to.
5678     (and (not data)
5679          force
5680          (gnus-summary-insert-subject
5681           article
5682           (if (or (numberp force) (vectorp force)) force)
5683           t)
5684          (setq data (gnus-data-find article)))
5685     (goto-char b)
5686     (if (not data)
5687         (progn
5688           (unless silent
5689             (gnus-message 3 "Can't find article %d" article))
5690           nil)
5691       (goto-char (gnus-data-pos data))
5692       (gnus-summary-position-point)
5693       article)))
5694
5695 ;; Walking around summary lines with displaying articles.
5696
5697 (defun gnus-summary-expand-window (&optional arg)
5698   "Make the summary buffer take up the entire Emacs frame.
5699 Given a prefix, will force an `article' buffer configuration."
5700   (interactive "P")
5701   (if arg
5702       (gnus-configure-windows 'article 'force)
5703     (gnus-configure-windows 'summary 'force)))
5704
5705 (defun gnus-summary-display-article (article &optional all-header)
5706   "Display ARTICLE in article buffer."
5707   (gnus-set-global-variables)
5708   (if (null article)
5709       nil
5710     (prog1
5711         (if gnus-summary-display-article-function
5712             (funcall gnus-summary-display-article-function article all-header)
5713           (gnus-article-prepare article all-header))
5714       (gnus-run-hooks 'gnus-select-article-hook)
5715       (when (and gnus-current-article
5716                  (not (zerop gnus-current-article)))
5717         (gnus-summary-goto-subject gnus-current-article))
5718       (gnus-summary-recenter)
5719       (when (and gnus-use-trees gnus-show-threads)
5720         (gnus-possibly-generate-tree article)
5721         (gnus-highlight-selected-tree article))
5722       ;; Successfully display article.
5723       (gnus-article-set-window-start
5724        (cdr (assq article gnus-newsgroup-bookmarks))))))
5725
5726 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5727   "Select the current article.
5728 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5729 non-nil, the article will be re-fetched even if it already present in
5730 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5731 be displayed."
5732   ;; Make sure we are in the summary buffer to work around bbdb bug.
5733   (unless (eq major-mode 'gnus-summary-mode)
5734     (set-buffer gnus-summary-buffer))
5735   (let ((article (or article (gnus-summary-article-number)))
5736         (all-headers (not (not all-headers))) ;Must be T or NIL.
5737         gnus-summary-display-article-function
5738         did)
5739     (and (not pseudo)
5740          (gnus-summary-article-pseudo-p article)
5741          (error "This is a pseudo-article"))
5742     (prog1
5743         (save-excursion
5744           (set-buffer gnus-summary-buffer)
5745           (if (or (and gnus-single-article-buffer
5746                        (or (null gnus-current-article)
5747                            (null gnus-article-current)
5748                            (null (get-buffer gnus-article-buffer))
5749                            (not (eq article (cdr gnus-article-current)))
5750                            (not (equal (car gnus-article-current)
5751                                        gnus-newsgroup-name))))
5752                   (and (not gnus-single-article-buffer)
5753                        (or (null gnus-current-article)
5754                            (not (eq gnus-current-article article))))
5755                   force)
5756               ;; The requested article is different from the current article.
5757               (prog1
5758                   (gnus-summary-display-article article all-headers)
5759                 (setq did article)
5760                 (when (or all-headers gnus-show-all-headers)
5761                   (gnus-article-show-all-headers)))
5762             (when (or all-headers gnus-show-all-headers)
5763               (gnus-article-show-all-headers))
5764             'old))
5765       (when did
5766         (gnus-article-set-window-start
5767          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5768
5769 (defun gnus-summary-set-current-mark (&optional current-mark)
5770   "Obsolete function."
5771   nil)
5772
5773 (defun gnus-summary-next-article (&optional unread subject backward push)
5774   "Select the next article.
5775 If UNREAD, only unread articles are selected.
5776 If SUBJECT, only articles with SUBJECT are selected.
5777 If BACKWARD, the previous article is selected instead of the next."
5778   (interactive "P")
5779   (cond
5780    ;; Is there such an article?
5781    ((and (gnus-summary-search-forward unread subject backward)
5782          (or (gnus-summary-display-article (gnus-summary-article-number))
5783              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5784     (gnus-summary-position-point))
5785    ;; If not, we try the first unread, if that is wanted.
5786    ((and subject
5787          gnus-auto-select-same
5788          (gnus-summary-first-unread-article))
5789     (gnus-summary-position-point)
5790     (gnus-message 6 "Wrapped"))
5791    ;; Try to get next/previous article not displayed in this group.
5792    ((and gnus-auto-extend-newsgroup
5793          (not unread) (not subject))
5794     (gnus-summary-goto-article
5795      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5796      nil (count-lines (point-min) (point))))
5797    ;; Go to next/previous group.
5798    (t
5799     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5800       (gnus-summary-jump-to-group gnus-newsgroup-name))
5801     (let ((cmd last-command-char)
5802           (point
5803            (save-excursion
5804              (set-buffer gnus-group-buffer)
5805              (point)))
5806           (group
5807            (if (eq gnus-keep-same-level 'best)
5808                (gnus-summary-best-group gnus-newsgroup-name)
5809              (gnus-summary-search-group backward gnus-keep-same-level))))
5810       ;; For some reason, the group window gets selected.  We change
5811       ;; it back.
5812       (select-window (get-buffer-window (current-buffer)))
5813       ;; Select next unread newsgroup automagically.
5814       (cond
5815        ((or (not gnus-auto-select-next)
5816             (not cmd))
5817         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5818        ((or (eq gnus-auto-select-next 'quietly)
5819             (and (eq gnus-auto-select-next 'slightly-quietly)
5820                  push)
5821             (and (eq gnus-auto-select-next 'almost-quietly)
5822                  (gnus-summary-last-article-p)))
5823         ;; Select quietly.
5824         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5825             (gnus-summary-exit)
5826           (gnus-message 7 "No more%s articles (%s)..."
5827                         (if unread " unread" "")
5828                         (if group (concat "selecting " group)
5829                           "exiting"))
5830           (gnus-summary-next-group nil group backward)))
5831        (t
5832         (when (gnus-key-press-event-p last-input-event)
5833           (gnus-summary-walk-group-buffer
5834            gnus-newsgroup-name cmd unread backward point))))))))
5835
5836 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5837   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5838                       (?\C-p (gnus-group-prev-unread-group 1))))
5839         (cursor-in-echo-area t)
5840         keve key group ended)
5841     (save-excursion
5842       (set-buffer gnus-group-buffer)
5843       (goto-char start)
5844       (setq group
5845             (if (eq gnus-keep-same-level 'best)
5846                 (gnus-summary-best-group gnus-newsgroup-name)
5847               (gnus-summary-search-group backward gnus-keep-same-level))))
5848     (while (not ended)
5849       (gnus-message
5850        5 "No more%s articles%s" (if unread " unread" "")
5851        (if (and group
5852                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5853            (format " (Type %s for %s [%s])"
5854                    (single-key-description cmd) group
5855                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5856          (format " (Type %s to exit %s)"
5857                  (single-key-description cmd)
5858                  gnus-newsgroup-name)))
5859       ;; Confirm auto selection.
5860       (setq key (car (setq keve (gnus-read-event-char))))
5861       (setq ended t)
5862       (cond
5863        ((assq key keystrokes)
5864         (let ((obuf (current-buffer)))
5865           (switch-to-buffer gnus-group-buffer)
5866           (when group
5867             (gnus-group-jump-to-group group))
5868           (eval (cadr (assq key keystrokes)))
5869           (setq group (gnus-group-group-name))
5870           (switch-to-buffer obuf))
5871         (setq ended nil))
5872        ((equal key cmd)
5873         (if (or (not group)
5874                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5875             (gnus-summary-exit)
5876           (gnus-summary-next-group nil group backward)))
5877        (t
5878         (push (cdr keve) unread-command-events))))))
5879
5880 (defun gnus-summary-next-unread-article ()
5881   "Select unread article after current one."
5882   (interactive)
5883   (gnus-summary-next-article
5884    (or (not (eq gnus-summary-goto-unread 'never))
5885        (gnus-summary-last-article-p (gnus-summary-article-number)))
5886    (and gnus-auto-select-same
5887         (gnus-summary-article-subject))))
5888
5889 (defun gnus-summary-prev-article (&optional unread subject)
5890   "Select the article after the current one.
5891 If UNREAD is non-nil, only unread articles are selected."
5892   (interactive "P")
5893   (gnus-summary-next-article unread subject t))
5894
5895 (defun gnus-summary-prev-unread-article ()
5896   "Select unread article before current one."
5897   (interactive)
5898   (gnus-summary-prev-article
5899    (or (not (eq gnus-summary-goto-unread 'never))
5900        (gnus-summary-first-article-p (gnus-summary-article-number)))
5901    (and gnus-auto-select-same
5902         (gnus-summary-article-subject))))
5903
5904 (defun gnus-summary-next-page (&optional lines circular)
5905   "Show next page of the selected article.
5906 If at the end of the current article, select the next article.
5907 LINES says how many lines should be scrolled up.
5908
5909 If CIRCULAR is non-nil, go to the start of the article instead of
5910 selecting the next article when reaching the end of the current
5911 article."
5912   (interactive "P")
5913   (setq gnus-summary-buffer (current-buffer))
5914   (gnus-set-global-variables)
5915   (let ((article (gnus-summary-article-number))
5916         (article-window (get-buffer-window gnus-article-buffer t))
5917         endp)
5918     ;; If the buffer is empty, we have no article.
5919     (unless article
5920       (error "No article to select"))
5921     (gnus-configure-windows 'article)
5922     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5923         (if (and (eq gnus-summary-goto-unread 'never)
5924                  (not (gnus-summary-last-article-p article)))
5925             (gnus-summary-next-article)
5926           (gnus-summary-next-unread-article))
5927       (if (or (null gnus-current-article)
5928               (null gnus-article-current)
5929               (/= article (cdr gnus-article-current))
5930               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5931           ;; Selected subject is different from current article's.
5932           (gnus-summary-display-article article)
5933         (when article-window
5934           (gnus-eval-in-buffer-window gnus-article-buffer
5935             (setq endp (gnus-article-next-page lines)))
5936           (when endp
5937             (cond (circular
5938                    (gnus-summary-beginning-of-article))
5939                   (lines
5940                    (gnus-message 3 "End of message"))
5941                   ((null lines)
5942                    (if (and (eq gnus-summary-goto-unread 'never)
5943                             (not (gnus-summary-last-article-p article)))
5944                        (gnus-summary-next-article)
5945                      (gnus-summary-next-unread-article))))))))
5946     (gnus-summary-recenter)
5947     (gnus-summary-position-point)))
5948
5949 (defun gnus-summary-prev-page (&optional lines move)
5950   "Show previous page of selected article.
5951 Argument LINES specifies lines to be scrolled down.
5952 If MOVE, move to the previous unread article if point is at
5953 the beginning of the buffer."
5954   (interactive "P")
5955   (let ((article (gnus-summary-article-number))
5956         (article-window (get-buffer-window gnus-article-buffer t))
5957         endp)
5958     (gnus-configure-windows 'article)
5959     (if (or (null gnus-current-article)
5960             (null gnus-article-current)
5961             (/= article (cdr gnus-article-current))
5962             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5963         ;; Selected subject is different from current article's.
5964         (gnus-summary-display-article article)
5965       (gnus-summary-recenter)
5966       (when article-window
5967         (gnus-eval-in-buffer-window gnus-article-buffer
5968           (setq endp (gnus-article-prev-page lines)))
5969         (when (and move endp)
5970           (cond (lines
5971                  (gnus-message 3 "Beginning of message"))
5972                 ((null lines)
5973                  (if (and (eq gnus-summary-goto-unread 'never)
5974                           (not (gnus-summary-first-article-p article)))
5975                      (gnus-summary-prev-article)
5976                    (gnus-summary-prev-unread-article))))))))
5977   (gnus-summary-position-point))
5978
5979 (defun gnus-summary-prev-page-or-article (&optional lines)
5980   "Show previous page of selected article.
5981 Argument LINES specifies lines to be scrolled down.
5982 If at the beginning of the article, go to the next article."
5983   (interactive "P")
5984   (gnus-summary-prev-page lines t))
5985
5986 (defun gnus-summary-scroll-up (lines)
5987   "Scroll up (or down) one line current article.
5988 Argument LINES specifies lines to be scrolled up (or down if negative)."
5989   (interactive "p")
5990   (gnus-configure-windows 'article)
5991   (gnus-summary-show-thread)
5992   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5993     (gnus-eval-in-buffer-window gnus-article-buffer
5994       (cond ((> lines 0)
5995              (when (gnus-article-next-page lines)
5996                (gnus-message 3 "End of message")))
5997             ((< lines 0)
5998              (gnus-article-prev-page (- lines))))))
5999   (gnus-summary-recenter)
6000   (gnus-summary-position-point))
6001
6002 (defun gnus-summary-scroll-down (lines)
6003   "Scroll down (or up) one line current article.
6004 Argument LINES specifies lines to be scrolled down (or up if negative)."
6005   (interactive "p")
6006   (gnus-summary-scroll-up (- lines)))
6007
6008 (defun gnus-summary-next-same-subject ()
6009   "Select next article which has the same subject as current one."
6010   (interactive)
6011   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6012
6013 (defun gnus-summary-prev-same-subject ()
6014   "Select previous article which has the same subject as current one."
6015   (interactive)
6016   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6017
6018 (defun gnus-summary-next-unread-same-subject ()
6019   "Select next unread article which has the same subject as current one."
6020   (interactive)
6021   (gnus-summary-next-article t (gnus-summary-article-subject)))
6022
6023 (defun gnus-summary-prev-unread-same-subject ()
6024   "Select previous unread article which has the same subject as current one."
6025   (interactive)
6026   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6027
6028 (defun gnus-summary-first-unread-article ()
6029   "Select the first unread article.
6030 Return nil if there are no unread articles."
6031   (interactive)
6032   (prog1
6033       (when (gnus-summary-first-subject t)
6034         (gnus-summary-show-thread)
6035         (gnus-summary-first-subject t)
6036         (gnus-summary-display-article (gnus-summary-article-number)))
6037     (gnus-summary-position-point)))
6038
6039 (defun gnus-summary-first-unread-subject ()
6040   "Place the point on the subject line of the first unread article.
6041 Return nil if there are no unread articles."
6042   (interactive)
6043   (prog1
6044       (when (gnus-summary-first-subject t)
6045         (gnus-summary-show-thread)
6046         (gnus-summary-first-subject t))
6047     (gnus-summary-position-point)))
6048
6049 (defun gnus-summary-first-article ()
6050   "Select the first article.
6051 Return nil if there are no articles."
6052   (interactive)
6053   (prog1
6054       (when (gnus-summary-first-subject)
6055         (gnus-summary-show-thread)
6056         (gnus-summary-first-subject)
6057         (gnus-summary-display-article (gnus-summary-article-number)))
6058     (gnus-summary-position-point)))
6059
6060 (defun gnus-summary-best-unread-article ()
6061   "Select the unread article with the highest score."
6062   (interactive)
6063   (let ((best -1000000)
6064         (data gnus-newsgroup-data)
6065         article score)
6066     (while data
6067       (and (gnus-data-unread-p (car data))
6068            (> (setq score
6069                     (gnus-summary-article-score (gnus-data-number (car data))))
6070               best)
6071            (setq best score
6072                  article (gnus-data-number (car data))))
6073       (setq data (cdr data)))
6074     (prog1
6075         (if article
6076             (gnus-summary-goto-article article)
6077           (error "No unread articles"))
6078       (gnus-summary-position-point))))
6079
6080 (defun gnus-summary-last-subject ()
6081   "Go to the last displayed subject line in the group."
6082   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6083     (when article
6084       (gnus-summary-goto-subject article))))
6085
6086 (defun gnus-summary-goto-article (article &optional all-headers force)
6087   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6088 If ALL-HEADERS is non-nil, no header lines are hidden.
6089 If FORCE, go to the article even if it isn't displayed.  If FORCE
6090 is a number, it is the line the article is to be displayed on."
6091   (interactive
6092    (list
6093     (completing-read
6094      "Article number or Message-ID: "
6095      (mapcar (lambda (number) (list (int-to-string number)))
6096              gnus-newsgroup-limit))
6097     current-prefix-arg
6098     t))
6099   (prog1
6100       (if (and (stringp article)
6101                (string-match "@" article))
6102           (gnus-summary-refer-article article)
6103         (when (stringp article)
6104           (setq article (string-to-number article)))
6105         (if (gnus-summary-goto-subject article force)
6106             (gnus-summary-display-article article all-headers)
6107           (gnus-message 4 "Couldn't go to article %s" article) nil))
6108     (gnus-summary-position-point)))
6109
6110 (defun gnus-summary-goto-last-article ()
6111   "Go to the previously read article."
6112   (interactive)
6113   (prog1
6114       (when gnus-last-article
6115         (gnus-summary-goto-article gnus-last-article nil t))
6116     (gnus-summary-position-point)))
6117
6118 (defun gnus-summary-pop-article (number)
6119   "Pop one article off the history and go to the previous.
6120 NUMBER articles will be popped off."
6121   (interactive "p")
6122   (let (to)
6123     (setq gnus-newsgroup-history
6124           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6125     (if to
6126         (gnus-summary-goto-article (car to) nil t)
6127       (error "Article history empty")))
6128   (gnus-summary-position-point))
6129
6130 ;; Summary commands and functions for limiting the summary buffer.
6131
6132 (defun gnus-summary-limit-to-articles (n)
6133   "Limit the summary buffer to the next N articles.
6134 If not given a prefix, use the process marked articles instead."
6135   (interactive "P")
6136   (prog1
6137       (let ((articles (gnus-summary-work-articles n)))
6138         (setq gnus-newsgroup-processable nil)
6139         (gnus-summary-limit articles))
6140     (gnus-summary-position-point)))
6141
6142 (defun gnus-summary-pop-limit (&optional total)
6143   "Restore the previous limit.
6144 If given a prefix, remove all limits."
6145   (interactive "P")
6146   (when total
6147     (setq gnus-newsgroup-limits
6148           (list (mapcar (lambda (h) (mail-header-number h))
6149                         gnus-newsgroup-headers))))
6150   (unless gnus-newsgroup-limits
6151     (error "No limit to pop"))
6152   (prog1
6153       (gnus-summary-limit nil 'pop)
6154     (gnus-summary-position-point)))
6155
6156 (defun gnus-summary-limit-to-subject (subject &optional header)
6157   "Limit the summary buffer to articles that have subjects that match a regexp."
6158   (interactive "sLimit to subject (regexp): ")
6159   (unless header
6160     (setq header "subject"))
6161   (when (not (equal "" subject))
6162     (prog1
6163         (let ((articles (gnus-summary-find-matching
6164                          (or header "subject") subject 'all)))
6165           (unless articles
6166             (error "Found no matches for \"%s\"" subject))
6167           (gnus-summary-limit articles))
6168       (gnus-summary-position-point))))
6169
6170 (defun gnus-summary-limit-to-author (from)
6171   "Limit the summary buffer to articles that have authors that match a regexp."
6172   (interactive "sLimit to author (regexp): ")
6173   (gnus-summary-limit-to-subject from "from"))
6174
6175 (defun gnus-summary-limit-to-age (age &optional younger-p)
6176   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6177 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6178 articles that are younger than AGE days."
6179   (interactive "nLimit to articles older than (in days): \nP")
6180   (prog1
6181       (let ((data gnus-newsgroup-data)
6182             (cutoff (days-to-time age))
6183             articles d date is-younger)
6184         (while (setq d (pop data))
6185           (when (and (vectorp (gnus-data-header d))
6186                      (setq date (mail-header-date (gnus-data-header d))))
6187             (setq is-younger (time-less-p
6188                               (time-since (date-to-time date))
6189                               cutoff))
6190             (when (if younger-p
6191                       is-younger
6192                     (not is-younger))
6193               (push (gnus-data-number d) articles))))
6194         (gnus-summary-limit (nreverse articles)))
6195     (gnus-summary-position-point)))
6196
6197 (defun gnus-summary-limit-to-extra (header regexp)
6198   "Limit the summary buffer to articles that match an 'extra' header."
6199   (interactive
6200    (let ((header
6201           (intern
6202            (gnus-completing-read
6203             (symbol-name (car gnus-extra-headers))      
6204             "Limit extra header:"       
6205             (mapcar (lambda (x) 
6206                       (cons (symbol-name x) x))
6207                     gnus-extra-headers)
6208             nil                 
6209             t))))
6210      (list header
6211            (read-string (format "Limit to header %s (regexp): " header)))))
6212   (when (not (equal "" regexp))
6213     (prog1
6214         (let ((articles (gnus-summary-find-matching
6215                          (cons 'extra header) regexp 'all)))
6216           (unless articles
6217             (error "Found no matches for \"%s\"" regexp))
6218           (gnus-summary-limit articles))
6219       (gnus-summary-position-point))))
6220
6221 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6222 (make-obsolete
6223  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6224
6225 (defun gnus-summary-limit-to-unread (&optional all)
6226   "Limit the summary buffer to articles that are not marked as read.
6227 If ALL is non-nil, limit strictly to unread articles."
6228   (interactive "P")
6229   (if all
6230       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6231     (gnus-summary-limit-to-marks
6232      ;; Concat all the marks that say that an article is read and have
6233      ;; those removed.
6234      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6235            gnus-killed-mark gnus-kill-file-mark
6236            gnus-low-score-mark gnus-expirable-mark
6237            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6238            gnus-duplicate-mark gnus-souped-mark)
6239      'reverse)))
6240
6241 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6242 (make-obsolete 'gnus-summary-delete-marked-with
6243                'gnus-summary-limit-exlude-marks)
6244
6245 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6246   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6247 If REVERSE, limit the summary buffer to articles that are marked
6248 with MARKS.  MARKS can either be a string of marks or a list of marks.
6249 Returns how many articles were removed."
6250   (interactive "sMarks: ")
6251   (gnus-summary-limit-to-marks marks t))
6252
6253 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6254   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6255 If REVERSE (the prefix), limit the summary buffer to articles that are
6256 not marked with MARKS.  MARKS can either be a string of marks or a
6257 list of marks.
6258 Returns how many articles were removed."
6259   (interactive "sMarks: \nP")
6260   (prog1
6261       (let ((data gnus-newsgroup-data)
6262             (marks (if (listp marks) marks
6263                      (append marks nil))) ; Transform to list.
6264             articles)
6265         (while data
6266           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6267                   (memq (gnus-data-mark (car data)) marks))
6268             (push (gnus-data-number (car data)) articles))
6269           (setq data (cdr data)))
6270         (gnus-summary-limit articles))
6271     (gnus-summary-position-point)))
6272
6273 (defun gnus-summary-limit-to-score (&optional score)
6274   "Limit to articles with score at or above SCORE."
6275   (interactive "P")
6276   (setq score (if score
6277                   (prefix-numeric-value score)
6278                 (or gnus-summary-default-score 0)))
6279   (let ((data gnus-newsgroup-data)
6280         articles)
6281     (while data
6282       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6283                 score)
6284         (push (gnus-data-number (car data)) articles))
6285       (setq data (cdr data)))
6286     (prog1
6287         (gnus-summary-limit articles)
6288       (gnus-summary-position-point))))
6289
6290 (defun gnus-summary-limit-include-thread (id)
6291   "Display all the hidden articles that in the current thread."
6292   (interactive (list (mail-header-id (gnus-summary-article-header))))
6293   (let ((articles (gnus-articles-in-thread
6294                    (gnus-id-to-thread (gnus-root-id id)))))
6295     (prog1
6296         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6297       (gnus-summary-position-point))))
6298
6299 (defun gnus-summary-limit-include-dormant ()
6300   "Display all the hidden articles that are marked as dormant.
6301 Note that this command only works on a subset of the articles currently
6302 fetched for this group."
6303   (interactive)
6304   (unless gnus-newsgroup-dormant
6305     (error "There are no dormant articles in this group"))
6306   (prog1
6307       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6308     (gnus-summary-position-point)))
6309
6310 (defun gnus-summary-limit-exclude-dormant ()
6311   "Hide all dormant articles."
6312   (interactive)
6313   (prog1
6314       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6315     (gnus-summary-position-point)))
6316
6317 (defun gnus-summary-limit-exclude-childless-dormant ()
6318   "Hide all dormant articles that have no children."
6319   (interactive)
6320   (let ((data (gnus-data-list t))
6321         articles d children)
6322     ;; Find all articles that are either not dormant or have
6323     ;; children.
6324     (while (setq d (pop data))
6325       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6326                 (and (setq children
6327                            (gnus-article-children (gnus-data-number d)))
6328                      (let (found)
6329                        (while children
6330                          (when (memq (car children) articles)
6331                            (setq children nil
6332                                  found t))
6333                          (pop children))
6334                        found)))
6335         (push (gnus-data-number d) articles)))
6336     ;; Do the limiting.
6337     (prog1
6338         (gnus-summary-limit articles)
6339       (gnus-summary-position-point))))
6340
6341 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6342   "Mark all unread excluded articles as read.
6343 If ALL, mark even excluded ticked and dormants as read."
6344   (interactive "P")
6345   (let ((articles (gnus-sorted-complement
6346                    (sort
6347                     (mapcar (lambda (h) (mail-header-number h))
6348                             gnus-newsgroup-headers)
6349                     '<)
6350                    (sort gnus-newsgroup-limit '<)))
6351         article)
6352     (setq gnus-newsgroup-unreads
6353           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6354     (if all
6355         (setq gnus-newsgroup-dormant nil
6356               gnus-newsgroup-marked nil
6357               gnus-newsgroup-reads
6358               (nconc
6359                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6360                gnus-newsgroup-reads))
6361       (while (setq article (pop articles))
6362         (unless (or (memq article gnus-newsgroup-dormant)
6363                     (memq article gnus-newsgroup-marked))
6364           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6365
6366 (defun gnus-summary-limit (articles &optional pop)
6367   (if pop
6368       ;; We pop the previous limit off the stack and use that.
6369       (setq articles (car gnus-newsgroup-limits)
6370             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6371     ;; We use the new limit, so we push the old limit on the stack.
6372     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6373   ;; Set the limit.
6374   (setq gnus-newsgroup-limit articles)
6375   (let ((total (length gnus-newsgroup-data))
6376         (data (gnus-data-find-list (gnus-summary-article-number)))
6377         (gnus-summary-mark-below nil)   ; Inhibit this.
6378         found)
6379     ;; This will do all the work of generating the new summary buffer
6380     ;; according to the new limit.
6381     (gnus-summary-prepare)
6382     ;; Hide any threads, possibly.
6383     (and gnus-show-threads
6384          gnus-thread-hide-subtree
6385          (gnus-summary-hide-all-threads))
6386     ;; Try to return to the article you were at, or one in the
6387     ;; neighborhood.
6388     (when data
6389       ;; We try to find some article after the current one.
6390       (while data
6391         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6392           (setq data nil
6393                 found t))
6394         (setq data (cdr data))))
6395     (unless found
6396       ;; If there is no data, that means that we were after the last
6397       ;; article.  The same goes when we can't find any articles
6398       ;; after the current one.
6399       (goto-char (point-max))
6400       (gnus-summary-find-prev))
6401     (gnus-set-mode-line 'summary)
6402     ;; We return how many articles were removed from the summary
6403     ;; buffer as a result of the new limit.
6404     (- total (length gnus-newsgroup-data))))
6405
6406 (defsubst gnus-invisible-cut-children (threads)
6407   (let ((num 0))
6408     (while threads
6409       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6410         (incf num))
6411       (pop threads))
6412     (< num 2)))
6413
6414 (defsubst gnus-cut-thread (thread)
6415   "Go forwards in the thread until we find an article that we want to display."
6416   (when (or (eq gnus-fetch-old-headers 'some)
6417             (eq gnus-fetch-old-headers 'invisible)
6418             (eq gnus-build-sparse-threads 'some)
6419             (eq gnus-build-sparse-threads 'more))
6420     ;; Deal with old-fetched headers and sparse threads.
6421     (while (and
6422             thread
6423             (or
6424              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6425              (gnus-summary-article-ancient-p
6426               (mail-header-number (car thread))))
6427             (if (or (<= (length (cdr thread)) 1)
6428                     (eq gnus-fetch-old-headers 'invisible))
6429                 (setq gnus-newsgroup-limit
6430                       (delq (mail-header-number (car thread))
6431                             gnus-newsgroup-limit)
6432                       thread (cadr thread))
6433               (when (gnus-invisible-cut-children (cdr thread))
6434                 (let ((th (cdr thread)))
6435                   (while th
6436                     (if (memq (mail-header-number (caar th))
6437                               gnus-newsgroup-limit)
6438                         (setq thread (car th)
6439                               th nil)
6440                       (setq th (cdr th))))))))))
6441   thread)
6442
6443 (defun gnus-cut-threads (threads)
6444   "Cut off all uninteresting articles from the beginning of threads."
6445   (when (or (eq gnus-fetch-old-headers 'some)
6446             (eq gnus-fetch-old-headers 'invisible)
6447             (eq gnus-build-sparse-threads 'some)
6448             (eq gnus-build-sparse-threads 'more))
6449     (let ((th threads))
6450       (while th
6451         (setcar th (gnus-cut-thread (car th)))
6452         (setq th (cdr th)))))
6453   ;; Remove nixed out threads.
6454   (delq nil threads))
6455
6456 (defun gnus-summary-initial-limit (&optional show-if-empty)
6457   "Figure out what the initial limit is supposed to be on group entry.
6458 This entails weeding out unwanted dormants, low-scored articles,
6459 fetch-old-headers verbiage, and so on."
6460   ;; Most groups have nothing to remove.
6461   (if (or gnus-inhibit-limiting
6462           (and (null gnus-newsgroup-dormant)
6463                (not (eq gnus-fetch-old-headers 'some))
6464                (not (eq gnus-fetch-old-headers 'invisible))
6465                (null gnus-summary-expunge-below)
6466                (not (eq gnus-build-sparse-threads 'some))
6467                (not (eq gnus-build-sparse-threads 'more))
6468                (null gnus-thread-expunge-below)
6469                (not gnus-use-nocem)))
6470       ()                                ; Do nothing.
6471     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6472     (setq gnus-newsgroup-limit nil)
6473     (mapatoms
6474      (lambda (node)
6475        (unless (car (symbol-value node))
6476          ;; These threads have no parents -- they are roots.
6477          (let ((nodes (cdr (symbol-value node)))
6478                thread)
6479            (while nodes
6480              (if (and gnus-thread-expunge-below
6481                       (< (gnus-thread-total-score (car nodes))
6482                          gnus-thread-expunge-below))
6483                  (gnus-expunge-thread (pop nodes))
6484                (setq thread (pop nodes))
6485                (gnus-summary-limit-children thread))))))
6486      gnus-newsgroup-dependencies)
6487     ;; If this limitation resulted in an empty group, we might
6488     ;; pop the previous limit and use it instead.
6489     (when (and (not gnus-newsgroup-limit)
6490                show-if-empty)
6491       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6492     gnus-newsgroup-limit))
6493
6494 (defun gnus-summary-limit-children (thread)
6495   "Return 1 if this subthread is visible and 0 if it is not."
6496   ;; First we get the number of visible children to this thread.  This
6497   ;; is done by recursing down the thread using this function, so this
6498   ;; will really go down to a leaf article first, before slowly
6499   ;; working its way up towards the root.
6500   (when thread
6501     (let ((children
6502            (if (cdr thread)
6503                (apply '+ (mapcar 'gnus-summary-limit-children
6504                                  (cdr thread)))
6505              0))
6506           (number (mail-header-number (car thread)))
6507           score)
6508       (if (and
6509            (not (memq number gnus-newsgroup-marked))
6510            (or
6511             ;; If this article is dormant and has absolutely no visible
6512             ;; children, then this article isn't visible.
6513             (and (memq number gnus-newsgroup-dormant)
6514                  (zerop children))
6515             ;; If this is "fetch-old-headered" and there is no
6516             ;; visible children, then we don't want this article.
6517             (and (eq gnus-fetch-old-headers 'some)
6518                  (gnus-summary-article-ancient-p number)
6519                  (zerop children))
6520             ;; If this is "fetch-old-headered" and `invisible', then
6521             ;; we don't want this article.
6522             (and (eq gnus-fetch-old-headers 'invisible)
6523                  (gnus-summary-article-ancient-p number))
6524             ;; If this is a sparsely inserted article with no children,
6525             ;; we don't want it.
6526             (and (eq gnus-build-sparse-threads 'some)
6527                  (gnus-summary-article-sparse-p number)
6528                  (zerop children))
6529             ;; If we use expunging, and this article is really
6530             ;; low-scored, then we don't want this article.
6531             (when (and gnus-summary-expunge-below
6532                        (< (setq score
6533                                 (or (cdr (assq number gnus-newsgroup-scored))
6534                                     gnus-summary-default-score))
6535                           gnus-summary-expunge-below))
6536               ;; We increase the expunge-tally here, but that has
6537               ;; nothing to do with the limits, really.
6538               (incf gnus-newsgroup-expunged-tally)
6539               ;; We also mark as read here, if that's wanted.
6540               (when (and gnus-summary-mark-below
6541                          (< score gnus-summary-mark-below))
6542                 (setq gnus-newsgroup-unreads
6543                       (delq number gnus-newsgroup-unreads))
6544                 (if gnus-newsgroup-auto-expire
6545                     (push number gnus-newsgroup-expirable)
6546                   (push (cons number gnus-low-score-mark)
6547                         gnus-newsgroup-reads)))
6548               t)
6549             ;; Check NoCeM things.
6550             (if (and gnus-use-nocem
6551                      (gnus-nocem-unwanted-article-p
6552                       (mail-header-id (car thread))))
6553                 (progn
6554                   (setq gnus-newsgroup-unreads
6555                         (delq number gnus-newsgroup-unreads))
6556                   t))))
6557           ;; Nope, invisible article.
6558           0
6559         ;; Ok, this article is to be visible, so we add it to the limit
6560         ;; and return 1.
6561         (push number gnus-newsgroup-limit)
6562         1))))
6563
6564 (defun gnus-expunge-thread (thread)
6565   "Mark all articles in THREAD as read."
6566   (let* ((number (mail-header-number (car thread))))
6567     (incf gnus-newsgroup-expunged-tally)
6568     ;; We also mark as read here, if that's wanted.
6569     (setq gnus-newsgroup-unreads
6570           (delq number gnus-newsgroup-unreads))
6571     (if gnus-newsgroup-auto-expire
6572         (push number gnus-newsgroup-expirable)
6573       (push (cons number gnus-low-score-mark)
6574             gnus-newsgroup-reads)))
6575   ;; Go recursively through all subthreads.
6576   (mapcar 'gnus-expunge-thread (cdr thread)))
6577
6578 ;; Summary article oriented commands
6579
6580 (defun gnus-summary-refer-parent-article (n)
6581   "Refer parent article N times.
6582 If N is negative, go to ancestor -N instead.
6583 The difference between N and the number of articles fetched is returned."
6584   (interactive "p")
6585   (let ((skip 1)
6586         error header ref)
6587     (when (not (natnump n))
6588       (setq skip (abs n)
6589             n 1))
6590     (while (and (> n 0)
6591                 (not error))
6592       (setq header (gnus-summary-article-header))
6593       (if (and (eq (mail-header-number header)
6594                    (cdr gnus-article-current))
6595                (equal gnus-newsgroup-name
6596                       (car gnus-article-current)))
6597           ;; If we try to find the parent of the currently
6598           ;; displayed article, then we take a look at the actual
6599           ;; References header, since this is slightly more
6600           ;; reliable than the References field we got from the
6601           ;; server.
6602           (save-excursion
6603             (set-buffer gnus-original-article-buffer)
6604             (nnheader-narrow-to-headers)
6605             (unless (setq ref (message-fetch-field "references"))
6606               (setq ref (message-fetch-field "in-reply-to")))
6607             (widen))
6608         (setq ref
6609               ;; It's not the current article, so we take a bet on
6610               ;; the value we got from the server.
6611               (mail-header-references header)))
6612       (if (and ref
6613                (not (equal ref "")))
6614           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6615             (gnus-message 1 "Couldn't find parent"))
6616         (gnus-message 1 "No references in article %d"
6617                       (gnus-summary-article-number))
6618         (setq error t))
6619       (decf n))
6620     (gnus-summary-position-point)
6621     n))
6622
6623 (defun gnus-summary-refer-references ()
6624   "Fetch all articles mentioned in the References header.
6625 Return the number of articles fetched."
6626   (interactive)
6627   (let ((ref (mail-header-references (gnus-summary-article-header)))
6628         (current (gnus-summary-article-number))
6629         (n 0))
6630     (if (or (not ref)
6631             (equal ref ""))
6632         (error "No References in the current article")
6633       ;; For each Message-ID in the References header...
6634       (while (string-match "<[^>]*>" ref)
6635         (incf n)
6636         ;; ... fetch that article.
6637         (gnus-summary-refer-article
6638          (prog1 (match-string 0 ref)
6639            (setq ref (substring ref (match-end 0))))))
6640       (gnus-summary-goto-subject current)
6641       (gnus-summary-position-point)
6642       n)))
6643
6644 (defun gnus-summary-refer-thread (&optional limit)
6645   "Fetch all articles in the current thread.
6646 If LIMIT (the numerical prefix), fetch that many old headers instead
6647 of what's specified by the `gnus-refer-thread-limit' variable."
6648   (interactive "P")
6649   (let ((id (mail-header-id (gnus-summary-article-header)))
6650         (limit (if limit (prefix-numeric-value limit)
6651                  gnus-refer-thread-limit)))
6652     ;; We want to fetch LIMIT *old* headers, but we also have to
6653     ;; re-fetch all the headers in the current buffer, because many of
6654     ;; them may be undisplayed.  So we adjust LIMIT.
6655     (when (numberp limit)
6656       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6657     (unless (eq gnus-fetch-old-headers 'invisible)
6658       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6659       ;; Retrieve the headers and read them in.
6660       (if (eq (gnus-retrieve-headers
6661                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6662               'nov)
6663           (gnus-build-all-threads)
6664         (error "Can't fetch thread from backends that don't support NOV"))
6665       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6666     (gnus-summary-limit-include-thread id)))
6667
6668 (defun gnus-summary-refer-article (message-id &optional arg)
6669   "Fetch an article specified by MESSAGE-ID.
6670 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6671 or `gnus-select-method', no matter what backend the article comes from."
6672   (interactive "sMessage-ID: \nP")
6673   (when (and (stringp message-id)
6674              (not (zerop (length message-id))))
6675     ;; Construct the correct Message-ID if necessary.
6676     ;; Suggested by tale@pawl.rpi.edu.
6677     (unless (string-match "^<" message-id)
6678       (setq message-id (concat "<" message-id)))
6679     (unless (string-match ">$" message-id)
6680       (setq message-id (concat message-id ">")))
6681     (let* ((header (gnus-id-to-header message-id))
6682            (sparse (and header
6683                         (gnus-summary-article-sparse-p
6684                          (mail-header-number header))
6685                         (memq (mail-header-number header)
6686                               gnus-newsgroup-limit))))
6687       (cond
6688        ;; If the article is present in the buffer we just go to it.
6689        ((and header
6690              (or (not (gnus-summary-article-sparse-p
6691                        (mail-header-number header)))
6692                  sparse))
6693         (prog1
6694             (gnus-summary-goto-article
6695              (mail-header-number header) nil t)
6696           (when sparse
6697             (gnus-summary-update-article (mail-header-number header)))))
6698        (t
6699         ;; We fetch the article
6700         (let ((gnus-override-method
6701                (cond ((gnus-news-group-p gnus-newsgroup-name)
6702                       gnus-refer-article-method)
6703                      (arg
6704                       (or gnus-refer-article-method gnus-select-method))
6705                      (t nil)))
6706               number)
6707           ;; Start the special refer-article method, if necessary.
6708           (when (and gnus-refer-article-method
6709                      (gnus-news-group-p gnus-newsgroup-name))
6710             (gnus-check-server gnus-refer-article-method))
6711           ;; Fetch the header, and display the article.
6712           (if (setq number (gnus-summary-insert-subject message-id))
6713               (gnus-summary-select-article nil nil nil number)
6714             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6715
6716 (defun gnus-summary-edit-parameters ()
6717   "Edit the group parameters of the current group."
6718   (interactive)
6719   (gnus-group-edit-group gnus-newsgroup-name 'params))
6720
6721 (defun gnus-summary-customize-parameters ()
6722   "Customize the group parameters of the current group."
6723   (interactive)
6724   (gnus-group-customize gnus-newsgroup-name))
6725
6726 (defun gnus-summary-enter-digest-group (&optional force)
6727   "Enter an nndoc group based on the current article.
6728 If FORCE, force a digest interpretation.  If not, try
6729 to guess what the document format is."
6730   (interactive "P")
6731   (let ((conf gnus-current-window-configuration))
6732     (save-excursion
6733       (gnus-summary-select-article))
6734     (setq gnus-current-window-configuration conf)
6735     (let* ((name (format "%s-%d"
6736                          (gnus-group-prefixed-name
6737                           gnus-newsgroup-name (list 'nndoc ""))
6738                          (save-excursion
6739                            (set-buffer gnus-summary-buffer)
6740                            gnus-current-article)))
6741            (ogroup gnus-newsgroup-name)
6742            (params (append (gnus-info-params (gnus-get-info ogroup))
6743                            (list (cons 'to-group ogroup))
6744                            (list (cons 'save-article-group ogroup))))
6745            (case-fold-search t)
6746            (buf (current-buffer))
6747            dig)
6748       (save-excursion
6749         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6750         (insert-buffer-substring gnus-original-article-buffer)
6751         ;; Remove lines that may lead nndoc to misinterpret the
6752         ;; document type.
6753         (narrow-to-region
6754          (goto-char (point-min))
6755          (or (search-forward "\n\n" nil t) (point)))
6756         (goto-char (point-min))
6757         (delete-matching-lines "^Path:\\|^From ")
6758         (widen))
6759       (unwind-protect
6760           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6761                     (gnus-newsgroup-ephemeral-ignored-charsets
6762                      gnus-newsgroup-ignored-charsets))
6763                 (gnus-group-read-ephemeral-group
6764                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6765                               (nndoc-article-type
6766                                ,(if force 'digest 'guess))) t))
6767               ;; Make all postings to this group go to the parent group.
6768               (nconc (gnus-info-params (gnus-get-info name))
6769                      params)
6770             ;; Couldn't select this doc group.
6771             (switch-to-buffer buf)
6772             (gnus-set-global-variables)
6773             (gnus-configure-windows 'summary)
6774             (gnus-message 3 "Article couldn't be entered?"))
6775         (kill-buffer dig)))))
6776
6777 (defun gnus-summary-read-document (n)
6778   "Open a new group based on the current article(s).
6779 This will allow you to read digests and other similar
6780 documents as newsgroups.
6781 Obeys the standard process/prefix convention."
6782   (interactive "P")
6783   (let* ((articles (gnus-summary-work-articles n))
6784          (ogroup gnus-newsgroup-name)
6785          (params (append (gnus-info-params (gnus-get-info ogroup))
6786                          (list (cons 'to-group ogroup))))
6787          article group egroup groups vgroup)
6788     (while (setq article (pop articles))
6789       (setq group (format "%s-%d" gnus-newsgroup-name article))
6790       (gnus-summary-remove-process-mark article)
6791       (when (gnus-summary-display-article article)
6792         (save-excursion
6793           (with-temp-buffer
6794             (insert-buffer-substring gnus-original-article-buffer)
6795             ;; Remove some headers that may lead nndoc to make
6796             ;; the wrong guess.
6797             (message-narrow-to-head)
6798             (goto-char (point-min))
6799             (delete-matching-lines "^\\(Path\\):\\|^From ")
6800             (widen)
6801             (if (setq egroup
6802                       (gnus-group-read-ephemeral-group
6803                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6804                                      (nndoc-article-type guess))
6805                        t nil t))
6806                 (progn
6807                   ;; Make all postings to this group go to the parent group.
6808                   (nconc (gnus-info-params (gnus-get-info egroup))
6809                          params)
6810                   (push egroup groups))
6811               ;; Couldn't select this doc group.
6812               (gnus-error 3 "Article couldn't be entered"))))))
6813     ;; Now we have selected all the documents.
6814     (cond
6815      ((not groups)
6816       (error "None of the articles could be interpreted as documents"))
6817      ((gnus-group-read-ephemeral-group
6818        (setq vgroup (format
6819                      "nnvirtual:%s-%s" gnus-newsgroup-name
6820                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6821        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6822        t
6823        (cons (current-buffer) 'summary)))
6824      (t
6825       (error "Couldn't select virtual nndoc group")))))
6826
6827 (defun gnus-summary-isearch-article (&optional regexp-p)
6828   "Do incremental search forward on the current article.
6829 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6830   (interactive "P")
6831   (gnus-summary-select-article)
6832   (gnus-configure-windows 'article)
6833   (gnus-eval-in-buffer-window gnus-article-buffer
6834     (save-restriction
6835       (widen)
6836       (isearch-forward regexp-p))))
6837
6838 (defun gnus-summary-search-article-forward (regexp &optional backward)
6839   "Search for an article containing REGEXP forward.
6840 If BACKWARD, search backward instead."
6841   (interactive
6842    (list (read-string
6843           (format "Search article %s (regexp%s): "
6844                   (if current-prefix-arg "backward" "forward")
6845                   (if gnus-last-search-regexp
6846                       (concat ", default " gnus-last-search-regexp)
6847                     "")))
6848          current-prefix-arg))
6849   (if (string-equal regexp "")
6850       (setq regexp (or gnus-last-search-regexp ""))
6851     (setq gnus-last-search-regexp regexp))
6852   (if (gnus-summary-search-article regexp backward)
6853       (gnus-summary-show-thread)
6854     (error "Search failed: \"%s\"" regexp)))
6855
6856 (defun gnus-summary-search-article-backward (regexp)
6857   "Search for an article containing REGEXP backward."
6858   (interactive
6859    (list (read-string
6860           (format "Search article backward (regexp%s): "
6861                   (if gnus-last-search-regexp
6862                       (concat ", default " gnus-last-search-regexp)
6863                     "")))))
6864   (gnus-summary-search-article-forward regexp 'backward))
6865
6866 (defun gnus-summary-search-article (regexp &optional backward)
6867   "Search for an article containing REGEXP.
6868 Optional argument BACKWARD means do search for backward.
6869 `gnus-select-article-hook' is not called during the search."
6870   ;; We have to require this here to make sure that the following
6871   ;; dynamic binding isn't shadowed by autoloading.
6872   (require 'gnus-async)
6873   (require 'gnus-art)
6874   (let ((gnus-select-article-hook nil)  ;Disable hook.
6875         (gnus-article-display-hook nil)
6876         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6877         (gnus-use-article-prefetch nil)
6878         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6879         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6880         (sum (current-buffer))
6881         (found nil)
6882         point)
6883     (gnus-save-hidden-threads
6884       (gnus-summary-select-article)
6885       (set-buffer gnus-article-buffer)
6886       (when backward
6887         (forward-line -1))
6888       (while (not found)
6889         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6890         (if (if backward
6891                 (re-search-backward regexp nil t)
6892               (re-search-forward regexp nil t))
6893             ;; We found the regexp.
6894             (progn
6895               (setq found 'found)
6896               (beginning-of-line)
6897               (set-window-start
6898                (get-buffer-window (current-buffer))
6899                (point))
6900               (forward-line 1)
6901               (set-buffer sum)
6902               (setq point (point)))
6903           ;; We didn't find it, so we go to the next article.
6904           (set-buffer sum)
6905           (setq found 'not)
6906           (while (eq found 'not)
6907             (if (not (if backward (gnus-summary-find-prev)
6908                        (gnus-summary-find-next)))
6909                 ;; No more articles.
6910                 (setq found t)
6911               ;; Select the next article and adjust point.
6912               (unless (gnus-summary-article-sparse-p
6913                        (gnus-summary-article-number))
6914                 (setq found nil)
6915                 (gnus-summary-select-article)
6916                 (set-buffer gnus-article-buffer)
6917                 (widen)
6918                 (goto-char (if backward (point-max) (point-min))))))))
6919       (gnus-message 7 ""))
6920     ;; Return whether we found the regexp.
6921     (when (eq found 'found)
6922       (goto-char point)
6923       (gnus-summary-show-thread)
6924       (gnus-summary-goto-subject gnus-current-article)
6925       (gnus-summary-position-point)
6926       t)))
6927
6928 (defun gnus-summary-find-matching (header regexp &optional backward unread
6929                                           not-case-fold)
6930   "Return a list of all articles that match REGEXP on HEADER.
6931 The search stars on the current article and goes forwards unless
6932 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6933 If UNREAD is non-nil, only unread articles will
6934 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6935 in the comparisons."
6936   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6937                 (gnus-data-find-list
6938                  (gnus-summary-article-number) (gnus-data-list backward))))
6939         (case-fold-search (not not-case-fold))
6940         articles d func)
6941     (if (consp header)
6942         (if (eq (car header) 'extra)
6943             (setq func
6944                   `(lambda (h)
6945                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
6946                          "")))
6947           (error "%s is an invalid header" header))
6948       (unless (fboundp (intern (concat "mail-header-" header)))
6949         (error "%s is not a valid header" header))
6950       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
6951     (while data
6952       (setq d (car data))
6953       (and (or (not unread)             ; We want all articles...
6954                (gnus-data-unread-p d))  ; Or just unreads.
6955            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6956            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6957            (push (gnus-data-number d) articles)) ; Success!
6958       (setq data (cdr data)))
6959     (nreverse articles)))
6960
6961 (defun gnus-summary-execute-command (header regexp command &optional backward)
6962   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6963 If HEADER is an empty string (or nil), the match is done on the entire
6964 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6965   (interactive
6966    (list (let ((completion-ignore-case t))
6967            (completing-read
6968             "Header name: "
6969             (mapcar (lambda (string) (list string))
6970                     '("Number" "Subject" "From" "Lines" "Date"
6971                       "Message-ID" "Xref" "References" "Body"))
6972             nil 'require-match))
6973          (read-string "Regexp: ")
6974          (read-key-sequence "Command: ")
6975          current-prefix-arg))
6976   (when (equal header "Body")
6977     (setq header ""))
6978   ;; Hidden thread subtrees must be searched as well.
6979   (gnus-summary-show-all-threads)
6980   ;; We don't want to change current point nor window configuration.
6981   (save-excursion
6982     (save-window-excursion
6983       (gnus-message 6 "Executing %s..." (key-description command))
6984       ;; We'd like to execute COMMAND interactively so as to give arguments.
6985       (gnus-execute header regexp
6986                     `(call-interactively ',(key-binding command))
6987                     backward)
6988       (gnus-message 6 "Executing %s...done" (key-description command)))))
6989
6990 (defun gnus-summary-beginning-of-article ()
6991   "Scroll the article back to the beginning."
6992   (interactive)
6993   (gnus-summary-select-article)
6994   (gnus-configure-windows 'article)
6995   (gnus-eval-in-buffer-window gnus-article-buffer
6996     (widen)
6997     (goto-char (point-min))
6998     (when gnus-page-broken
6999       (gnus-narrow-to-page))))
7000
7001 (defun gnus-summary-end-of-article ()
7002   "Scroll to the end of the article."
7003   (interactive)
7004   (gnus-summary-select-article)
7005   (gnus-configure-windows 'article)
7006   (gnus-eval-in-buffer-window gnus-article-buffer
7007     (widen)
7008     (goto-char (point-max))
7009     (recenter -3)
7010     (when gnus-page-broken
7011       (gnus-narrow-to-page))))
7012
7013 (defun gnus-summary-print-article (&optional filename n)
7014   "Generate and print a PostScript image of the N next (mail) articles.
7015
7016 If N is negative, print the N previous articles.  If N is nil and articles
7017 have been marked with the process mark, print these instead.
7018
7019 If the optional first argument FILENAME is nil, send the image to the
7020 printer.  If FILENAME is a string, save the PostScript image in a file with
7021 that name.  If FILENAME is a number, prompt the user for the name of the file
7022 to save in."
7023   (interactive (list (ps-print-preprint current-prefix-arg)
7024                      current-prefix-arg))
7025   (dolist (article (gnus-summary-work-articles n))
7026     (gnus-summary-select-article nil nil 'pseudo article)
7027     (gnus-eval-in-buffer-window gnus-article-buffer
7028       (let ((buffer (generate-new-buffer " *print*")))
7029         (unwind-protect
7030             (progn
7031               (copy-to-buffer buffer (point-min) (point-max))
7032               (set-buffer buffer)
7033               (gnus-article-delete-invisible-text)
7034               (let ((ps-left-header
7035                      (list
7036                       (concat "("
7037                               (mail-header-subject gnus-current-headers) ")")
7038                       (concat "("
7039                               (mail-header-from gnus-current-headers) ")")))
7040                     (ps-right-header
7041                      (list
7042                       "/pagenumberstring load"
7043                       (concat "("
7044                               (mail-header-date gnus-current-headers) ")"))))
7045                 (gnus-run-hooks 'gnus-ps-print-hook)
7046                 (save-excursion
7047                   (ps-print-buffer-with-faces filename))))
7048           (kill-buffer buffer))))))
7049
7050 (defun gnus-summary-show-article (&optional arg)
7051   "Force re-fetching of the current article.
7052 If ARG (the prefix) is non-nil, show the raw article without any
7053 article massaging functions being run."
7054   (interactive "P")
7055   (if (not arg)
7056       ;; Select the article the normal way.
7057       (gnus-summary-select-article nil 'force)
7058     ;; We have to require this here to make sure that the following
7059     ;; dynamic binding isn't shadowed by autoloading.
7060     (require 'gnus-async)
7061     (require 'gnus-art)
7062     ;; Bind the article treatment functions to nil.
7063     (let ((gnus-have-all-headers t)
7064           gnus-article-display-hook
7065           gnus-article-prepare-hook
7066           gnus-article-decode-hook
7067           gnus-break-pages
7068           gnus-show-mime
7069           gnus-visual)
7070       (gnus-summary-select-article nil 'force)))
7071   (gnus-summary-goto-subject gnus-current-article)
7072   (gnus-summary-position-point))
7073
7074 (defun gnus-summary-verbose-headers (&optional arg)
7075   "Toggle permanent full header display.
7076 If ARG is a positive number, turn header display on.
7077 If ARG is a negative number, turn header display off."
7078   (interactive "P")
7079   (setq gnus-show-all-headers
7080         (cond ((or (not (numberp arg))
7081                    (zerop arg))
7082                (not gnus-show-all-headers))
7083               ((natnump arg)
7084                t)))
7085   (gnus-summary-show-article))
7086
7087 (defun gnus-summary-toggle-header (&optional arg)
7088   "Show the headers if they are hidden, or hide them if they are shown.
7089 If ARG is a positive number, show the entire header.
7090 If ARG is a negative number, hide the unwanted header lines."
7091   (interactive "P")
7092   (save-excursion
7093     (set-buffer gnus-article-buffer)
7094     (save-restriction
7095       (let* ((buffer-read-only nil)
7096              (inhibit-point-motion-hooks t)
7097              hidden e)
7098         (save-restriction 
7099           (article-narrow-to-head)
7100           (setq hidden (gnus-article-hidden-text-p 'headers)))
7101         (goto-char (point-min))
7102         (when (search-forward "\n\n" nil t)
7103           (delete-region (point-min) (1- (point))))
7104         (goto-char (point-min))
7105         (save-excursion
7106           (set-buffer gnus-original-article-buffer)
7107           (goto-char (point-min))
7108           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7109         (insert-buffer-substring gnus-original-article-buffer 1 e)
7110         (save-restriction
7111           (narrow-to-region (point-min) (point))
7112           (article-decode-encoded-words)
7113           (if (or hidden
7114                   (and (numberp arg) (< arg 0)))
7115               (let ((gnus-treat-hide-headers nil)
7116                     (gnus-treat-hide-boring-headers nil))
7117                 (gnus-treat-article 'head))
7118             (gnus-treat-article 'head)))))))
7119
7120 (defun gnus-summary-show-all-headers ()
7121   "Make all header lines visible."
7122   (interactive)
7123   (gnus-article-show-all-headers))
7124
7125 (defun gnus-summary-toggle-mime (&optional arg)
7126   "Toggle MIME processing.
7127 If ARG is a positive number, turn MIME processing on."
7128   (interactive "P")
7129   (setq gnus-show-mime
7130         (if (null arg)
7131             (not gnus-show-mime)
7132           (> (prefix-numeric-value arg) 0)))
7133   (gnus-summary-select-article t 'force))
7134
7135 (defun gnus-summary-caesar-message (&optional arg)
7136   "Caesar rotate the current article by 13.
7137 The numerical prefix specifies how many places to rotate each letter
7138 forward."
7139   (interactive "P")
7140   (gnus-summary-select-article)
7141   (let ((mail-header-separator ""))
7142     (gnus-eval-in-buffer-window gnus-article-buffer
7143       (save-restriction
7144         (widen)
7145         (let ((start (window-start))
7146               buffer-read-only)
7147           (message-caesar-buffer-body arg)
7148           (set-window-start (get-buffer-window (current-buffer)) start))))))
7149
7150 (defun gnus-summary-stop-page-breaking ()
7151   "Stop page breaking in the current article."
7152   (interactive)
7153   (gnus-summary-select-article)
7154   (gnus-eval-in-buffer-window gnus-article-buffer
7155     (widen)
7156     (when (gnus-visual-p 'page-marker)
7157       (let ((buffer-read-only nil))
7158         (gnus-remove-text-with-property 'gnus-prev)
7159         (gnus-remove-text-with-property 'gnus-next))
7160       (setq gnus-page-broken nil))))
7161
7162 (defun gnus-summary-move-article (&optional n to-newsgroup
7163                                             select-method action)
7164   "Move the current article to a different newsgroup.
7165 If N is a positive number, move the N next articles.
7166 If N is a negative number, move the N previous articles.
7167 If N is nil and any articles have been marked with the process mark,
7168 move those articles instead.
7169 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7170 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7171 re-spool using this method.
7172
7173 For this function to work, both the current newsgroup and the
7174 newsgroup that you want to move to have to support the `request-move'
7175 and `request-accept' functions."
7176   (interactive "P")
7177   (unless action
7178     (setq action 'move))
7179   ;; Disable marking as read.
7180   (let (gnus-mark-article-hook)
7181     (save-window-excursion
7182       (gnus-summary-select-article)))
7183   ;; Check whether the source group supports the required functions.
7184   (cond ((and (eq action 'move)
7185               (not (gnus-check-backend-function
7186                     'request-move-article gnus-newsgroup-name)))
7187          (error "The current group does not support article moving"))
7188         ((and (eq action 'crosspost)
7189               (not (gnus-check-backend-function
7190                     'request-replace-article gnus-newsgroup-name)))
7191          (error "The current group does not support article editing")))
7192   (let ((articles (gnus-summary-work-articles n))
7193         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7194         (names '((move "Move" "Moving")
7195                  (copy "Copy" "Copying")
7196                  (crosspost "Crosspost" "Crossposting")))
7197         (copy-buf (save-excursion
7198                     (nnheader-set-temp-buffer " *copy article*")))
7199         (default-marks gnus-article-mark-lists)
7200         (no-expire-marks (delete '(expirable . expire)
7201                                  (copy-sequence gnus-article-mark-lists)))
7202         art-group to-method new-xref article to-groups)
7203     (unless (assq action names)
7204       (error "Unknown action %s" action))
7205     ;; Read the newsgroup name.
7206     (when (and (not to-newsgroup)
7207                (not select-method))
7208       (setq to-newsgroup
7209             (gnus-read-move-group-name
7210              (cadr (assq action names))
7211              (symbol-value (intern (format "gnus-current-%s-group" action)))
7212              articles prefix))
7213       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7214     (setq to-method (or select-method
7215                         (gnus-group-name-to-method to-newsgroup)))
7216     ;; Check the method we are to move this article to...
7217     (unless (gnus-check-backend-function
7218              'request-accept-article (car to-method))
7219       (error "%s does not support article copying" (car to-method)))
7220     (unless (gnus-check-server to-method)
7221       (error "Can't open server %s" (car to-method)))
7222     (gnus-message 6 "%s to %s: %s..."
7223                   (caddr (assq action names))
7224                   (or (car select-method) to-newsgroup) articles)
7225     (while articles
7226       (setq article (pop articles))
7227       (setq
7228        art-group
7229        (cond
7230         ;; Move the article.
7231         ((eq action 'move)
7232          ;; Remove this article from future suppression.
7233          (gnus-dup-unsuppress-article article)
7234          (gnus-request-move-article
7235           article                       ; Article to move
7236           gnus-newsgroup-name           ; From newsgroup
7237           (nth 1 (gnus-find-method-for-group
7238                   gnus-newsgroup-name)) ; Server
7239           (list 'gnus-request-accept-article
7240                 to-newsgroup (list 'quote select-method)
7241                 (not articles) t)               ; Accept form
7242           (not articles)))              ; Only save nov last time
7243         ;; Copy the article.
7244         ((eq action 'copy)
7245          (save-excursion
7246            (set-buffer copy-buf)
7247            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7248              (gnus-request-accept-article
7249               to-newsgroup select-method (not articles) t))))
7250         ;; Crosspost the article.
7251         ((eq action 'crosspost)
7252          (let ((xref (message-tokenize-header
7253                       (mail-header-xref (gnus-summary-article-header article))
7254                       " ")))
7255            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7256                                   ":" article))
7257            (unless xref
7258              (setq xref (list (system-name))))
7259            (setq new-xref
7260                  (concat
7261                   (mapconcat 'identity
7262                              (delete "Xref:" (delete new-xref xref))
7263                              " ")
7264                   " " new-xref))
7265            (save-excursion
7266              (set-buffer copy-buf)
7267              ;; First put the article in the destination group.
7268              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7269              (when (consp (setq art-group
7270                                 (gnus-request-accept-article
7271                                  to-newsgroup select-method (not articles))))
7272                (setq new-xref (concat new-xref " " (car art-group)
7273                                       ":" (cdr art-group)))
7274                ;; Now we have the new Xrefs header, so we insert
7275                ;; it and replace the new article.
7276                (nnheader-replace-header "Xref" new-xref)
7277                (gnus-request-replace-article
7278                 (cdr art-group) to-newsgroup (current-buffer))
7279                art-group))))))
7280       (cond
7281        ((not art-group)
7282         (gnus-message 1 "Couldn't %s article %s"
7283                       (cadr (assq action names)) article))
7284        ((and (eq art-group 'junk)
7285              (eq action 'move))
7286         (gnus-summary-mark-article article gnus-canceled-mark)
7287         (gnus-message 4 "Deleted article %s" article))
7288        (t
7289         (let* ((pto-group (gnus-group-prefixed-name
7290                            (car art-group) to-method))
7291                (entry
7292                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7293                (info (nth 2 entry))
7294                (to-group (gnus-info-group info))
7295                to-marks)
7296           ;; Update the group that has been moved to.
7297           (when (and info
7298                      (memq action '(move copy)))
7299             (unless (member to-group to-groups)
7300               (push to-group to-groups))
7301
7302             (unless (memq article gnus-newsgroup-unreads)
7303               (push 'read to-marks)
7304               (gnus-info-set-read
7305                info (gnus-add-to-range (gnus-info-read info)
7306                                        (list (cdr art-group)))))
7307
7308             ;; Copy any marks over to the new group.
7309             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7310                              default-marks
7311                            no-expire-marks))
7312                   (to-article (cdr art-group)))
7313
7314               ;; See whether the article is to be put in the cache.
7315               (when gnus-use-cache
7316                 (gnus-cache-possibly-enter-article
7317                  to-group to-article
7318                  (let ((header (copy-sequence
7319                                 (gnus-summary-article-header article))))
7320                    (mail-header-set-number header to-article)
7321                    header)
7322                  (memq article gnus-newsgroup-marked)
7323                  (memq article gnus-newsgroup-dormant)
7324                  (memq article gnus-newsgroup-unreads)))
7325
7326               (when (and (equal to-group gnus-newsgroup-name)
7327                          (not (memq article gnus-newsgroup-unreads)))
7328                 ;; Mark this article as read in this group.
7329                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7330                 (setcdr (gnus-active to-group) to-article)
7331                 (setcdr gnus-newsgroup-active to-article))
7332
7333               (while marks
7334                 (when (memq article (symbol-value
7335                                      (intern (format "gnus-newsgroup-%s"
7336                                                      (caar marks)))))
7337                   (push (cdar marks) to-marks)
7338                   ;; If the other group is the same as this group,
7339                   ;; then we have to add the mark to the list.
7340                   (when (equal to-group gnus-newsgroup-name)
7341                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7342                          (cons to-article
7343                                (symbol-value
7344                                 (intern (format "gnus-newsgroup-%s"
7345                                                 (caar marks)))))))
7346                   ;; Copy the marks to other group.
7347                   (gnus-add-marked-articles
7348                    to-group (cdar marks) (list to-article) info))
7349                 (setq marks (cdr marks)))
7350
7351               (gnus-request-set-mark to-group (list (list (list to-article)
7352                                                           'set
7353                                                           to-marks)))
7354
7355               (gnus-dribble-enter
7356                (concat "(gnus-group-set-info '"
7357                        (gnus-prin1-to-string (gnus-get-info to-group))
7358                        ")"))))
7359
7360           ;; Update the Xref header in this article to point to
7361           ;; the new crossposted article we have just created.
7362           (when (eq action 'crosspost)
7363             (save-excursion
7364               (set-buffer copy-buf)
7365               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7366               (nnheader-replace-header "Xref" new-xref)
7367               (gnus-request-replace-article
7368                article gnus-newsgroup-name (current-buffer)))))
7369
7370         ;;;!!!Why is this necessary?
7371         (set-buffer gnus-summary-buffer)
7372
7373         (gnus-summary-goto-subject article)
7374         (when (eq action 'move)
7375           (gnus-summary-mark-article article gnus-canceled-mark))))
7376       (gnus-summary-remove-process-mark article))
7377     ;; Re-activate all groups that have been moved to.
7378     (while to-groups
7379       (save-excursion
7380         (set-buffer gnus-group-buffer)
7381         (when (gnus-group-goto-group (car to-groups) t)
7382           (gnus-group-get-new-news-this-group 1 t))
7383         (pop to-groups)))
7384
7385     (gnus-kill-buffer copy-buf)
7386     (gnus-summary-position-point)
7387     (gnus-set-mode-line 'summary)))
7388
7389 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7390   "Move the current article to a different newsgroup.
7391 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7392 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7393 re-spool using this method."
7394   (interactive "P")
7395   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7396
7397 (defun gnus-summary-crosspost-article (&optional n)
7398   "Crosspost the current article to some other group."
7399   (interactive "P")
7400   (gnus-summary-move-article n nil nil 'crosspost))
7401
7402 (defcustom gnus-summary-respool-default-method nil
7403   "Default method for respooling an article.
7404 If nil, use to the current newsgroup method."
7405   :type '(choice (gnus-select-method :value (nnml ""))
7406                  (const nil))
7407   :group 'gnus-summary-mail)
7408
7409 (defun gnus-summary-respool-article (&optional n method)
7410   "Respool the current article.
7411 The article will be squeezed through the mail spooling process again,
7412 which means that it will be put in some mail newsgroup or other
7413 depending on `nnmail-split-methods'.
7414 If N is a positive number, respool the N next articles.
7415 If N is a negative number, respool the N previous articles.
7416 If N is nil and any articles have been marked with the process mark,
7417 respool those articles instead.
7418
7419 Respooling can be done both from mail groups and \"real\" newsgroups.
7420 In the former case, the articles in question will be moved from the
7421 current group into whatever groups they are destined to.  In the
7422 latter case, they will be copied into the relevant groups."
7423   (interactive
7424    (list current-prefix-arg
7425          (let* ((methods (gnus-methods-using 'respool))
7426                 (methname
7427                  (symbol-name (or gnus-summary-respool-default-method
7428                                   (car (gnus-find-method-for-group
7429                                         gnus-newsgroup-name)))))
7430                 (method
7431                  (gnus-completing-read
7432                   methname "What backend do you want to use when respooling?"
7433                   methods nil t nil 'gnus-mail-method-history))
7434                 ms)
7435            (cond
7436             ((zerop (length (setq ms (gnus-servers-using-backend
7437                                       (intern method)))))
7438              (list (intern method) ""))
7439             ((= 1 (length ms))
7440              (car ms))
7441             (t
7442              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7443                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7444                            ms-alist))))))))
7445   (unless method
7446     (error "No method given for respooling"))
7447   (if (assoc (symbol-name
7448               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7449              (gnus-methods-using 'respool))
7450       (gnus-summary-move-article n nil method)
7451     (gnus-summary-copy-article n nil method)))
7452
7453 (defun gnus-summary-import-article (file)
7454   "Import an arbitrary file into a mail newsgroup."
7455   (interactive "fImport file: ")
7456   (let ((group gnus-newsgroup-name)
7457         (now (current-time))
7458         atts lines)
7459     (unless (gnus-check-backend-function 'request-accept-article group)
7460       (error "%s does not support article importing" group))
7461     (or (file-readable-p file)
7462         (not (file-regular-p file))
7463         (error "Can't read %s" file))
7464     (save-excursion
7465       (set-buffer (gnus-get-buffer-create " *import file*"))
7466       (erase-buffer)
7467       (nnheader-insert-file-contents file)
7468       (goto-char (point-min))
7469       (unless (nnheader-article-p)
7470         ;; This doesn't look like an article, so we fudge some headers.
7471         (setq atts (file-attributes file)
7472               lines (count-lines (point-min) (point-max)))
7473         (insert "From: " (read-string "From: ") "\n"
7474                 "Subject: " (read-string "Subject: ") "\n"
7475                 "Date: " (message-make-date (nth 5 atts))
7476                 "\n"
7477                 "Message-ID: " (message-make-message-id) "\n"
7478                 "Lines: " (int-to-string lines) "\n"
7479                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7480       (gnus-request-accept-article group nil t)
7481       (kill-buffer (current-buffer)))))
7482
7483 (defun gnus-summary-article-posted-p ()
7484   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7485 This will be the case if the article has both been mailed and posted."
7486   (interactive)
7487   (let ((id (mail-header-references (gnus-summary-article-header)))
7488         (gnus-override-method
7489          (or gnus-refer-article-method gnus-select-method)))
7490     (if (gnus-request-head id "")
7491         (gnus-message 2 "The current message was found on %s"
7492                       gnus-override-method)
7493       (gnus-message 2 "The current message couldn't be found on %s"
7494                     gnus-override-method)
7495       nil)))
7496
7497 (defun gnus-summary-expire-articles (&optional now)
7498   "Expire all articles that are marked as expirable in the current group."
7499   (interactive)
7500   (when (gnus-check-backend-function
7501          'request-expire-articles gnus-newsgroup-name)
7502     ;; This backend supports expiry.
7503     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7504            (expirable (if total
7505                           (progn
7506                             ;; We need to update the info for
7507                             ;; this group for `gnus-list-of-read-articles'
7508                             ;; to give us the right answer.
7509                             (gnus-run-hooks 'gnus-exit-group-hook)
7510                             (gnus-summary-update-info)
7511                             (gnus-list-of-read-articles gnus-newsgroup-name))
7512                         (setq gnus-newsgroup-expirable
7513                               (sort gnus-newsgroup-expirable '<))))
7514            (expiry-wait (if now 'immediate
7515                           (gnus-group-find-parameter
7516                            gnus-newsgroup-name 'expiry-wait)))
7517            es)
7518       (when expirable
7519         ;; There are expirable articles in this group, so we run them
7520         ;; through the expiry process.
7521         (gnus-message 6 "Expiring articles...")
7522         (unless (gnus-check-group gnus-newsgroup-name)
7523           (error "Can't open server for %s" gnus-newsgroup-name))
7524         ;; The list of articles that weren't expired is returned.
7525         (save-excursion
7526           (if expiry-wait
7527               (let ((nnmail-expiry-wait-function nil)
7528                     (nnmail-expiry-wait expiry-wait))
7529                 (setq es (gnus-request-expire-articles
7530                           expirable gnus-newsgroup-name)))
7531             (setq es (gnus-request-expire-articles
7532                       expirable gnus-newsgroup-name))))
7533         (unless total
7534           (setq gnus-newsgroup-expirable es))
7535         ;; We go through the old list of expirable, and mark all
7536         ;; really expired articles as nonexistent.
7537         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7538           (let ((gnus-use-cache nil))
7539             (while expirable
7540               (unless (memq (car expirable) es)
7541                 (when (gnus-data-find (car expirable))
7542                   (gnus-summary-mark-article
7543                    (car expirable) gnus-canceled-mark)))
7544               (setq expirable (cdr expirable)))))
7545         (gnus-message 6 "Expiring articles...done")))))
7546
7547 (defun gnus-summary-expire-articles-now ()
7548   "Expunge all expirable articles in the current group.
7549 This means that *all* articles that are marked as expirable will be
7550 deleted forever, right now."
7551   (interactive)
7552   (or gnus-expert-user
7553       (gnus-yes-or-no-p
7554        "Are you really, really, really sure you want to delete all these messages? ")
7555       (error "Phew!"))
7556   (gnus-summary-expire-articles t))
7557
7558 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7559 (defun gnus-summary-delete-article (&optional n)
7560   "Delete the N next (mail) articles.
7561 This command actually deletes articles.  This is not a marking
7562 command.  The article will disappear forever from your life, never to
7563 return.
7564 If N is negative, delete backwards.
7565 If N is nil and articles have been marked with the process mark,
7566 delete these instead."
7567   (interactive "P")
7568   (unless (gnus-check-backend-function 'request-expire-articles
7569                                        gnus-newsgroup-name)
7570     (error "The current newsgroup does not support article deletion"))
7571   ;; Compute the list of articles to delete.
7572   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7573         not-deleted)
7574     (if (and gnus-novice-user
7575              (not (gnus-yes-or-no-p
7576                    (format "Do you really want to delete %s forever? "
7577                            (if (> (length articles) 1)
7578                                (format "these %s articles" (length articles))
7579                              "this article")))))
7580         ()
7581       ;; Delete the articles.
7582       (setq not-deleted (gnus-request-expire-articles
7583                          articles gnus-newsgroup-name 'force))
7584       (while articles
7585         (gnus-summary-remove-process-mark (car articles))
7586         ;; The backend might not have been able to delete the article
7587         ;; after all.
7588         (unless (memq (car articles) not-deleted)
7589           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7590         (setq articles (cdr articles)))
7591       (when not-deleted
7592         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7593     (gnus-summary-position-point)
7594     (gnus-set-mode-line 'summary)
7595     not-deleted))
7596
7597 (defun gnus-summary-edit-article (&optional force)
7598   "Edit the current article.
7599 This will have permanent effect only in mail groups.
7600 If FORCE is non-nil, allow editing of articles even in read-only
7601 groups."
7602   (interactive "P")
7603   (save-excursion
7604     (set-buffer gnus-summary-buffer)
7605     (let ((mail-parse-charset gnus-newsgroup-charset)
7606           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7607       (gnus-set-global-variables)
7608       (when (and (not force)
7609                  (gnus-group-read-only-p))
7610         (error "The current newsgroup does not support article editing"))
7611       (gnus-summary-show-article t)
7612       (gnus-article-edit-article
7613        'ignore
7614        `(lambda (no-highlight)
7615           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7616                 (mail-parse-ignored-charsets
7617                  ',gnus-newsgroup-ignored-charsets))
7618             (gnus-summary-edit-article-done
7619              ,(or (mail-header-references gnus-current-headers) "")
7620              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7621
7622 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7623
7624 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7625                                                  no-highlight)
7626   "Make edits to the current article permanent."
7627   (interactive)
7628   ;; Replace the article.
7629   (let ((buf (current-buffer)))
7630     (with-temp-buffer
7631       (insert-buffer-substring buf)
7632       (if (and (not read-only)
7633                (not (gnus-request-replace-article
7634                      (cdr gnus-article-current) (car gnus-article-current)
7635                      (current-buffer) t)))
7636           (error "Couldn't replace article")
7637         ;; Update the summary buffer.
7638         (if (and references
7639                  (equal (message-tokenize-header references " ")
7640                         (message-tokenize-header
7641                          (or (message-fetch-field "references") "") " ")))
7642             ;; We only have to update this line.
7643             (save-excursion
7644               (save-restriction
7645                 (message-narrow-to-head)
7646                 (let ((head (buffer-string))
7647                       header)
7648                   (with-temp-buffer
7649                     (insert (format "211 %d Article retrieved.\n"
7650                                     (cdr gnus-article-current)))
7651                     (insert head)
7652                     (insert ".\n")
7653                     (let ((nntp-server-buffer (current-buffer)))
7654                       (setq header (car (gnus-get-newsgroup-headers
7655                                          (save-excursion
7656                                            (set-buffer gnus-summary-buffer)
7657                                            gnus-newsgroup-dependencies)
7658                                          t))))
7659                     (save-excursion
7660                       (set-buffer gnus-summary-buffer)
7661                       (gnus-data-set-header
7662                        (gnus-data-find (cdr gnus-article-current))
7663                        header)
7664                       (gnus-summary-update-article-line
7665                        (cdr gnus-article-current) header))))))
7666           ;; Update threads.
7667           (set-buffer (or buffer gnus-summary-buffer))
7668           (gnus-summary-update-article (cdr gnus-article-current)))
7669         ;; Prettify the article buffer again.
7670         (unless no-highlight
7671           (save-excursion
7672             (set-buffer gnus-article-buffer)
7673             ;;;!!! Fix this -- article should be rehighlighted.
7674             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7675             (set-buffer gnus-original-article-buffer)
7676             (gnus-request-article
7677              (cdr gnus-article-current)
7678              (car gnus-article-current) (current-buffer))))
7679         ;; Prettify the summary buffer line.
7680         (when (gnus-visual-p 'summary-highlight 'highlight)
7681           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7682
7683 (defun gnus-summary-edit-wash (key)
7684   "Perform editing command KEY in the article buffer."
7685   (interactive
7686    (list
7687     (progn
7688       (message "%s" (concat (this-command-keys) "- "))
7689       (read-char))))
7690   (message "")
7691   (gnus-summary-edit-article)
7692   (execute-kbd-macro (concat (this-command-keys) key))
7693   (gnus-article-edit-done))
7694
7695 ;;; Respooling
7696
7697 (defun gnus-summary-respool-query (&optional silent trace)
7698   "Query where the respool algorithm would put this article."
7699   (interactive)
7700   (let (gnus-mark-article-hook)
7701     (gnus-summary-select-article)
7702     (save-excursion
7703       (set-buffer gnus-original-article-buffer)
7704       (save-restriction
7705         (message-narrow-to-head)
7706         (let ((groups (nnmail-article-group 'identity trace)))
7707           (unless silent
7708             (if groups
7709                 (message "This message would go to %s"
7710                          (mapconcat 'car groups ", "))
7711               (message "This message would go to no groups"))
7712             groups))))))
7713
7714 (defun gnus-summary-respool-trace ()
7715   "Trace where the respool algorithm would put this article.
7716 Display a buffer showing all fancy splitting patterns which matched."
7717   (interactive)
7718   (gnus-summary-respool-query nil t))
7719
7720 ;; Summary marking commands.
7721
7722 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7723   "Mark articles which has the same subject as read, and then select the next.
7724 If UNMARK is positive, remove any kind of mark.
7725 If UNMARK is negative, tick articles."
7726   (interactive "P")
7727   (when unmark
7728     (setq unmark (prefix-numeric-value unmark)))
7729   (let ((count
7730          (gnus-summary-mark-same-subject
7731           (gnus-summary-article-subject) unmark)))
7732     ;; Select next unread article.  If auto-select-same mode, should
7733     ;; select the first unread article.
7734     (gnus-summary-next-article t (and gnus-auto-select-same
7735                                       (gnus-summary-article-subject)))
7736     (gnus-message 7 "%d article%s marked as %s"
7737                   count (if (= count 1) " is" "s are")
7738                   (if unmark "unread" "read"))))
7739
7740 (defun gnus-summary-kill-same-subject (&optional unmark)
7741   "Mark articles which has the same subject as read.
7742 If UNMARK is positive, remove any kind of mark.
7743 If UNMARK is negative, tick articles."
7744   (interactive "P")
7745   (when unmark
7746     (setq unmark (prefix-numeric-value unmark)))
7747   (let ((count
7748          (gnus-summary-mark-same-subject
7749           (gnus-summary-article-subject) unmark)))
7750     ;; If marked as read, go to next unread subject.
7751     (when (null unmark)
7752       ;; Go to next unread subject.
7753       (gnus-summary-next-subject 1 t))
7754     (gnus-message 7 "%d articles are marked as %s"
7755                   count (if unmark "unread" "read"))))
7756
7757 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7758   "Mark articles with same SUBJECT as read, and return marked number.
7759 If optional argument UNMARK is positive, remove any kinds of marks.
7760 If optional argument UNMARK is negative, mark articles as unread instead."
7761   (let ((count 1))
7762     (save-excursion
7763       (cond
7764        ((null unmark)                   ; Mark as read.
7765         (while (and
7766                 (progn
7767                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7768                   (gnus-summary-show-thread) t)
7769                 (gnus-summary-find-subject subject))
7770           (setq count (1+ count))))
7771        ((> unmark 0)                    ; Tick.
7772         (while (and
7773                 (progn
7774                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7775                   (gnus-summary-show-thread) t)
7776                 (gnus-summary-find-subject subject))
7777           (setq count (1+ count))))
7778        (t                               ; Mark as unread.
7779         (while (and
7780                 (progn
7781                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7782                   (gnus-summary-show-thread) t)
7783                 (gnus-summary-find-subject subject))
7784           (setq count (1+ count)))))
7785       (gnus-set-mode-line 'summary)
7786       ;; Return the number of marked articles.
7787       count)))
7788
7789 (defun gnus-summary-mark-as-processable (n &optional unmark)
7790   "Set the process mark on the next N articles.
7791 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7792 the process mark instead.  The difference between N and the actual
7793 number of articles marked is returned."
7794   (interactive "p")
7795   (let ((backward (< n 0))
7796         (n (abs n)))
7797     (while (and
7798             (> n 0)
7799             (if unmark
7800                 (gnus-summary-remove-process-mark
7801                  (gnus-summary-article-number))
7802               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7803             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7804       (setq n (1- n)))
7805     (when (/= 0 n)
7806       (gnus-message 7 "No more articles"))
7807     (gnus-summary-recenter)
7808     (gnus-summary-position-point)
7809     n))
7810
7811 (defun gnus-summary-unmark-as-processable (n)
7812   "Remove the process mark from the next N articles.
7813 If N is negative, unmark backward instead.  The difference between N and
7814 the actual number of articles unmarked is returned."
7815   (interactive "p")
7816   (gnus-summary-mark-as-processable n t))
7817
7818 (defun gnus-summary-unmark-all-processable ()
7819   "Remove the process mark from all articles."
7820   (interactive)
7821   (save-excursion
7822     (while gnus-newsgroup-processable
7823       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7824   (gnus-summary-position-point))
7825
7826 (defun gnus-summary-mark-as-expirable (n)
7827   "Mark N articles forward as expirable.
7828 If N is negative, mark backward instead.  The difference between N and
7829 the actual number of articles marked is returned."
7830   (interactive "p")
7831   (gnus-summary-mark-forward n gnus-expirable-mark))
7832
7833 (defun gnus-summary-mark-article-as-replied (article)
7834   "Mark ARTICLE replied and update the summary line."
7835   (push article gnus-newsgroup-replied)
7836   (let ((buffer-read-only nil))
7837     (when (gnus-summary-goto-subject article nil t)
7838       (gnus-summary-update-secondary-mark article))))
7839
7840 (defun gnus-summary-set-bookmark (article)
7841   "Set a bookmark in current article."
7842   (interactive (list (gnus-summary-article-number)))
7843   (when (or (not (get-buffer gnus-article-buffer))
7844             (not gnus-current-article)
7845             (not gnus-article-current)
7846             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7847     (error "No current article selected"))
7848   ;; Remove old bookmark, if one exists.
7849   (let ((old (assq article gnus-newsgroup-bookmarks)))
7850     (when old
7851       (setq gnus-newsgroup-bookmarks
7852             (delq old gnus-newsgroup-bookmarks))))
7853   ;; Set the new bookmark, which is on the form
7854   ;; (article-number . line-number-in-body).
7855   (push
7856    (cons article
7857          (save-excursion
7858            (set-buffer gnus-article-buffer)
7859            (count-lines
7860             (min (point)
7861                  (save-excursion
7862                    (goto-char (point-min))
7863                    (search-forward "\n\n" nil t)
7864                    (point)))
7865             (point))))
7866    gnus-newsgroup-bookmarks)
7867   (gnus-message 6 "A bookmark has been added to the current article."))
7868
7869 (defun gnus-summary-remove-bookmark (article)
7870   "Remove the bookmark from the current article."
7871   (interactive (list (gnus-summary-article-number)))
7872   ;; Remove old bookmark, if one exists.
7873   (let ((old (assq article gnus-newsgroup-bookmarks)))
7874     (if old
7875         (progn
7876           (setq gnus-newsgroup-bookmarks
7877                 (delq old gnus-newsgroup-bookmarks))
7878           (gnus-message 6 "Removed bookmark."))
7879       (gnus-message 6 "No bookmark in current article."))))
7880
7881 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7882 (defun gnus-summary-mark-as-dormant (n)
7883   "Mark N articles forward as dormant.
7884 If N is negative, mark backward instead.  The difference between N and
7885 the actual number of articles marked is returned."
7886   (interactive "p")
7887   (gnus-summary-mark-forward n gnus-dormant-mark))
7888
7889 (defun gnus-summary-set-process-mark (article)
7890   "Set the process mark on ARTICLE and update the summary line."
7891   (setq gnus-newsgroup-processable
7892         (cons article
7893               (delq article gnus-newsgroup-processable)))
7894   (when (gnus-summary-goto-subject article)
7895     (gnus-summary-show-thread)
7896     (gnus-summary-goto-subject article)
7897     (gnus-summary-update-secondary-mark article)))
7898
7899 (defun gnus-summary-remove-process-mark (article)
7900   "Remove the process mark from ARTICLE and update the summary line."
7901   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7902   (when (gnus-summary-goto-subject article)
7903     (gnus-summary-show-thread)
7904     (gnus-summary-goto-subject article)
7905     (gnus-summary-update-secondary-mark article)))
7906
7907 (defun gnus-summary-set-saved-mark (article)
7908   "Set the process mark on ARTICLE and update the summary line."
7909   (push article gnus-newsgroup-saved)
7910   (when (gnus-summary-goto-subject article)
7911     (gnus-summary-update-secondary-mark article)))
7912
7913 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7914   "Mark N articles as read forwards.
7915 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7916 The difference between N and the actual number of articles marked is
7917 returned."
7918   (interactive "p")
7919   (gnus-summary-show-thread)
7920   (let ((backward (< n 0))
7921         (gnus-summary-goto-unread
7922          (and gnus-summary-goto-unread
7923               (not (eq gnus-summary-goto-unread 'never))
7924               (not (memq mark (list gnus-unread-mark
7925                                     gnus-ticked-mark gnus-dormant-mark)))))
7926         (n (abs n))
7927         (mark (or mark gnus-del-mark)))
7928     (while (and (> n 0)
7929                 (gnus-summary-mark-article nil mark no-expire)
7930                 (zerop (gnus-summary-next-subject
7931                         (if backward -1 1)
7932                         (and gnus-summary-goto-unread
7933                              (not (eq gnus-summary-goto-unread 'never)))
7934                         t)))
7935       (setq n (1- n)))
7936     (when (/= 0 n)
7937       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7938     (gnus-summary-recenter)
7939     (gnus-summary-position-point)
7940     (gnus-set-mode-line 'summary)
7941     n))
7942
7943 (defun gnus-summary-mark-article-as-read (mark)
7944   "Mark the current article quickly as read with MARK."
7945   (let ((article (gnus-summary-article-number)))
7946     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7947     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7948     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7949     (push (cons article mark) gnus-newsgroup-reads)
7950     ;; Possibly remove from cache, if that is used.
7951     (when gnus-use-cache
7952       (gnus-cache-enter-remove-article article))
7953     ;; Allow the backend to change the mark.
7954     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7955     ;; Check for auto-expiry.
7956     (when (and gnus-newsgroup-auto-expire
7957                (memq mark gnus-auto-expirable-marks))
7958       (setq mark gnus-expirable-mark)
7959       ;; Let the backend know about the mark change.
7960       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7961       (push article gnus-newsgroup-expirable))
7962     ;; Set the mark in the buffer.
7963     (gnus-summary-update-mark mark 'unread)
7964     t))
7965
7966 (defun gnus-summary-mark-article-as-unread (mark)
7967   "Mark the current article quickly as unread with MARK."
7968   (let* ((article (gnus-summary-article-number))
7969          (old-mark (gnus-summary-article-mark article)))
7970     ;; Allow the backend to change the mark.
7971     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7972     (if (eq mark old-mark)
7973         t
7974       (if (<= article 0)
7975           (progn
7976             (gnus-error 1 "Can't mark negative article numbers")
7977             nil)
7978         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7979         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7980         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7981         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7982         (cond ((= mark gnus-ticked-mark)
7983                (push article gnus-newsgroup-marked))
7984               ((= mark gnus-dormant-mark)
7985                (push article gnus-newsgroup-dormant))
7986               (t
7987                (push article gnus-newsgroup-unreads)))
7988         (gnus-pull article gnus-newsgroup-reads)
7989
7990         ;; See whether the article is to be put in the cache.
7991         (and gnus-use-cache
7992              (vectorp (gnus-summary-article-header article))
7993              (save-excursion
7994                (gnus-cache-possibly-enter-article
7995                 gnus-newsgroup-name article
7996                 (gnus-summary-article-header article)
7997                 (= mark gnus-ticked-mark)
7998                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7999
8000         ;; Fix the mark.
8001         (gnus-summary-update-mark mark 'unread)
8002         t))))
8003
8004 (defun gnus-summary-mark-article (&optional article mark no-expire)
8005   "Mark ARTICLE with MARK.  MARK can be any character.
8006 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8007 `??' (dormant) and `?E' (expirable).
8008 If MARK is nil, then the default character `?r' is used.
8009 If ARTICLE is nil, then the article on the current line will be
8010 marked."
8011   ;; The mark might be a string.
8012   (when (stringp mark)
8013     (setq mark (aref mark 0)))
8014   ;; If no mark is given, then we check auto-expiring.
8015   (when (null mark)
8016     (setq mark gnus-del-mark))
8017   (when (and (not no-expire)
8018              gnus-newsgroup-auto-expire
8019              (memq mark gnus-auto-expirable-marks))
8020     (setq mark gnus-expirable-mark))
8021   (let ((article (or article (gnus-summary-article-number)))
8022         (old-mark (gnus-summary-article-mark article)))
8023     ;; Allow the backend to change the mark.
8024     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8025     (if (eq mark old-mark)
8026         t
8027       (unless article
8028         (error "No article on current line"))
8029       (if (not (if (or (= mark gnus-unread-mark)
8030                        (= mark gnus-ticked-mark)
8031                        (= mark gnus-dormant-mark))
8032                    (gnus-mark-article-as-unread article mark)
8033                  (gnus-mark-article-as-read article mark)))
8034           t
8035         ;; See whether the article is to be put in the cache.
8036         (and gnus-use-cache
8037              (not (= mark gnus-canceled-mark))
8038              (vectorp (gnus-summary-article-header article))
8039              (save-excursion
8040                (gnus-cache-possibly-enter-article
8041                 gnus-newsgroup-name article
8042                 (gnus-summary-article-header article)
8043                 (= mark gnus-ticked-mark)
8044                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8045
8046         (when (gnus-summary-goto-subject article nil t)
8047           (let ((buffer-read-only nil))
8048             (gnus-summary-show-thread)
8049             ;; Fix the mark.
8050             (gnus-summary-update-mark mark 'unread)
8051             t))))))
8052
8053 (defun gnus-summary-update-secondary-mark (article)
8054   "Update the secondary (read, process, cache) mark."
8055   (gnus-summary-update-mark
8056    (cond ((memq article gnus-newsgroup-processable)
8057           gnus-process-mark)
8058          ((memq article gnus-newsgroup-cached)
8059           gnus-cached-mark)
8060          ((memq article gnus-newsgroup-replied)
8061           gnus-replied-mark)
8062          ((memq article gnus-newsgroup-saved)
8063           gnus-saved-mark)
8064          (t gnus-unread-mark))
8065    'replied)
8066   (when (gnus-visual-p 'summary-highlight 'highlight)
8067     (gnus-run-hooks 'gnus-summary-update-hook))
8068   t)
8069
8070 (defun gnus-summary-update-mark (mark type)
8071   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8072         (buffer-read-only nil))
8073     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8074     (when forward
8075       (when (looking-at "\r")
8076         (incf forward))
8077       (when (<= (+ forward (point)) (point-max))
8078         ;; Go to the right position on the line.
8079         (goto-char (+ forward (point)))
8080         ;; Replace the old mark with the new mark.
8081         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8082         ;; Optionally update the marks by some user rule.
8083         (when (eq type 'unread)
8084           (gnus-data-set-mark
8085            (gnus-data-find (gnus-summary-article-number)) mark)
8086           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8087
8088 (defun gnus-mark-article-as-read (article &optional mark)
8089   "Enter ARTICLE in the pertinent lists and remove it from others."
8090   ;; Make the article expirable.
8091   (let ((mark (or mark gnus-del-mark)))
8092     (if (= mark gnus-expirable-mark)
8093         (push article gnus-newsgroup-expirable)
8094       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8095     ;; Remove from unread and marked lists.
8096     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8097     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8098     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8099     (push (cons article mark) gnus-newsgroup-reads)
8100     ;; Possibly remove from cache, if that is used.
8101     (when gnus-use-cache
8102       (gnus-cache-enter-remove-article article))
8103     t))
8104
8105 (defun gnus-mark-article-as-unread (article &optional mark)
8106   "Enter ARTICLE in the pertinent lists and remove it from others."
8107   (let ((mark (or mark gnus-ticked-mark)))
8108     (if (<= article 0)
8109         (progn
8110           (gnus-error 1 "Can't mark negative article numbers")
8111           nil)
8112       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8113             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8114             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8115             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8116
8117       ;; Unsuppress duplicates?
8118       (when gnus-suppress-duplicates
8119         (gnus-dup-unsuppress-article article))
8120
8121       (cond ((= mark gnus-ticked-mark)
8122              (push article gnus-newsgroup-marked))
8123             ((= mark gnus-dormant-mark)
8124              (push article gnus-newsgroup-dormant))
8125             (t
8126              (push article gnus-newsgroup-unreads)))
8127       (gnus-pull article gnus-newsgroup-reads)
8128       t)))
8129
8130 (defalias 'gnus-summary-mark-as-unread-forward
8131   'gnus-summary-tick-article-forward)
8132 (make-obsolete 'gnus-summary-mark-as-unread-forward
8133                'gnus-summary-tick-article-forward)
8134 (defun gnus-summary-tick-article-forward (n)
8135   "Tick N articles forwards.
8136 If N is negative, tick backwards instead.
8137 The difference between N and the number of articles ticked is returned."
8138   (interactive "p")
8139   (gnus-summary-mark-forward n gnus-ticked-mark))
8140
8141 (defalias 'gnus-summary-mark-as-unread-backward
8142   'gnus-summary-tick-article-backward)
8143 (make-obsolete 'gnus-summary-mark-as-unread-backward
8144                'gnus-summary-tick-article-backward)
8145 (defun gnus-summary-tick-article-backward (n)
8146   "Tick N articles backwards.
8147 The difference between N and the number of articles ticked is returned."
8148   (interactive "p")
8149   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8150
8151 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8152 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8153 (defun gnus-summary-tick-article (&optional article clear-mark)
8154   "Mark current article as unread.
8155 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8156 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8157   (interactive)
8158   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8159                                        gnus-ticked-mark)))
8160
8161 (defun gnus-summary-mark-as-read-forward (n)
8162   "Mark N articles as read forwards.
8163 If N is negative, mark backwards instead.
8164 The difference between N and the actual number of articles marked is
8165 returned."
8166   (interactive "p")
8167   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8168
8169 (defun gnus-summary-mark-as-read-backward (n)
8170   "Mark the N articles as read backwards.
8171 The difference between N and the actual number of articles marked is
8172 returned."
8173   (interactive "p")
8174   (gnus-summary-mark-forward
8175    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8176
8177 (defun gnus-summary-mark-as-read (&optional article mark)
8178   "Mark current article as read.
8179 ARTICLE specifies the article to be marked as read.
8180 MARK specifies a string to be inserted at the beginning of the line."
8181   (gnus-summary-mark-article article mark))
8182
8183 (defun gnus-summary-clear-mark-forward (n)
8184   "Clear marks from N articles forward.
8185 If N is negative, clear backward instead.
8186 The difference between N and the number of marks cleared is returned."
8187   (interactive "p")
8188   (gnus-summary-mark-forward n gnus-unread-mark))
8189
8190 (defun gnus-summary-clear-mark-backward (n)
8191   "Clear marks from N articles backward.
8192 The difference between N and the number of marks cleared is returned."
8193   (interactive "p")
8194   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8195
8196 (defun gnus-summary-mark-unread-as-read ()
8197   "Intended to be used by `gnus-summary-mark-article-hook'."
8198   (when (memq gnus-current-article gnus-newsgroup-unreads)
8199     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8200
8201 (defun gnus-summary-mark-read-and-unread-as-read ()
8202   "Intended to be used by `gnus-summary-mark-article-hook'."
8203   (let ((mark (gnus-summary-article-mark)))
8204     (when (or (gnus-unread-mark-p mark)
8205               (gnus-read-mark-p mark))
8206       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8207
8208 (defun gnus-summary-mark-region-as-read (point mark all)
8209   "Mark all unread articles between point and mark as read.
8210 If given a prefix, mark all articles between point and mark as read,
8211 even ticked and dormant ones."
8212   (interactive "r\nP")
8213   (save-excursion
8214     (let (article)
8215       (goto-char point)
8216       (beginning-of-line)
8217       (while (and
8218               (< (point) mark)
8219               (progn
8220                 (when (or all
8221                           (memq (setq article (gnus-summary-article-number))
8222                                 gnus-newsgroup-unreads))
8223                   (gnus-summary-mark-article article gnus-del-mark))
8224                 t)
8225               (gnus-summary-find-next))))))
8226
8227 (defun gnus-summary-mark-below (score mark)
8228   "Mark articles with score less than SCORE with MARK."
8229   (interactive "P\ncMark: ")
8230   (setq score (if score
8231                   (prefix-numeric-value score)
8232                 (or gnus-summary-default-score 0)))
8233   (save-excursion
8234     (set-buffer gnus-summary-buffer)
8235     (goto-char (point-min))
8236     (while
8237         (progn
8238           (and (< (gnus-summary-article-score) score)
8239                (gnus-summary-mark-article nil mark))
8240           (gnus-summary-find-next)))))
8241
8242 (defun gnus-summary-kill-below (&optional score)
8243   "Mark articles with score below SCORE as read."
8244   (interactive "P")
8245   (gnus-summary-mark-below score gnus-killed-mark))
8246
8247 (defun gnus-summary-clear-above (&optional score)
8248   "Clear all marks from articles with score above SCORE."
8249   (interactive "P")
8250   (gnus-summary-mark-above score gnus-unread-mark))
8251
8252 (defun gnus-summary-tick-above (&optional score)
8253   "Tick all articles with score above SCORE."
8254   (interactive "P")
8255   (gnus-summary-mark-above score gnus-ticked-mark))
8256
8257 (defun gnus-summary-mark-above (score mark)
8258   "Mark articles with score over SCORE with MARK."
8259   (interactive "P\ncMark: ")
8260   (setq score (if score
8261                   (prefix-numeric-value score)
8262                 (or gnus-summary-default-score 0)))
8263   (save-excursion
8264     (set-buffer gnus-summary-buffer)
8265     (goto-char (point-min))
8266     (while (and (progn
8267                   (when (> (gnus-summary-article-score) score)
8268                     (gnus-summary-mark-article nil mark))
8269                   t)
8270                 (gnus-summary-find-next)))))
8271
8272 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8273 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8274 (defun gnus-summary-limit-include-expunged (&optional no-error)
8275   "Display all the hidden articles that were expunged for low scores."
8276   (interactive)
8277   (let ((buffer-read-only nil))
8278     (let ((scored gnus-newsgroup-scored)
8279           headers h)
8280       (while scored
8281         (unless (gnus-summary-goto-subject (caar scored))
8282           (and (setq h (gnus-summary-article-header (caar scored)))
8283                (< (cdar scored) gnus-summary-expunge-below)
8284                (push h headers)))
8285         (setq scored (cdr scored)))
8286       (if (not headers)
8287           (when (not no-error)
8288             (error "No expunged articles hidden"))
8289         (goto-char (point-min))
8290         (gnus-summary-prepare-unthreaded (nreverse headers))
8291         (goto-char (point-min))
8292         (gnus-summary-position-point)
8293         t))))
8294
8295 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8296   "Mark all unread articles in this newsgroup as read.
8297 If prefix argument ALL is non-nil, ticked and dormant articles will
8298 also be marked as read.
8299 If QUIETLY is non-nil, no questions will be asked.
8300 If TO-HERE is non-nil, it should be a point in the buffer.  All
8301 articles before this point will be marked as read.
8302 Note that this function will only catch up the unread article
8303 in the current summary buffer limitation.
8304 The number of articles marked as read is returned."
8305   (interactive "P")
8306   (prog1
8307       (save-excursion
8308         (when (or quietly
8309                   (not gnus-interactive-catchup) ;Without confirmation?
8310                   gnus-expert-user
8311                   (gnus-y-or-n-p
8312                    (if all
8313                        "Mark absolutely all articles as read? "
8314                      "Mark all unread articles as read? ")))
8315           (if (and not-mark
8316                    (not gnus-newsgroup-adaptive)
8317                    (not gnus-newsgroup-auto-expire)
8318                    (not gnus-suppress-duplicates)
8319                    (or (not gnus-use-cache)
8320                        (eq gnus-use-cache 'passive)))
8321               (progn
8322                 (when all
8323                   (setq gnus-newsgroup-marked nil
8324                         gnus-newsgroup-dormant nil))
8325                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8326             ;; We actually mark all articles as canceled, which we
8327             ;; have to do when using auto-expiry or adaptive scoring.
8328             (gnus-summary-show-all-threads)
8329             (when (gnus-summary-first-subject (not all) t)
8330               (while (and
8331                       (if to-here (< (point) to-here) t)
8332                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8333                       (gnus-summary-find-next (not all) nil nil t))))
8334             (gnus-set-mode-line 'summary))
8335           t))
8336     (gnus-summary-position-point)))
8337
8338 (defun gnus-summary-catchup-to-here (&optional all)
8339   "Mark all unticked articles before the current one as read.
8340 If ALL is non-nil, also mark ticked and dormant articles as read."
8341   (interactive "P")
8342   (save-excursion
8343     (gnus-save-hidden-threads
8344       (let ((beg (point)))
8345         ;; We check that there are unread articles.
8346         (when (or all (gnus-summary-find-prev))
8347           (gnus-summary-catchup all t beg)))))
8348   (gnus-summary-position-point))
8349
8350 (defun gnus-summary-catchup-all (&optional quietly)
8351   "Mark all articles in this newsgroup as read."
8352   (interactive "P")
8353   (gnus-summary-catchup t quietly))
8354
8355 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8356   "Mark all unread articles in this group as read, then exit.
8357 If prefix argument ALL is non-nil, all articles are marked as read."
8358   (interactive "P")
8359   (when (gnus-summary-catchup all quietly nil 'fast)
8360     ;; Select next newsgroup or exit.
8361     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8362              (eq gnus-auto-select-next 'quietly))
8363         (gnus-summary-next-group nil)
8364       (gnus-summary-exit))))
8365
8366 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8367   "Mark all articles in this newsgroup as read, and then exit."
8368   (interactive "P")
8369   (gnus-summary-catchup-and-exit t quietly))
8370
8371 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8372 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8373   "Mark all articles in this group as read and select the next group.
8374 If given a prefix, mark all articles, unread as well as ticked, as
8375 read."
8376   (interactive "P")
8377   (save-excursion
8378     (gnus-summary-catchup all))
8379   (gnus-summary-next-article t nil nil t))
8380
8381 ;; Thread-based commands.
8382
8383 (defun gnus-summary-articles-in-thread (&optional article)
8384   "Return a list of all articles in the current thread.
8385 If ARTICLE is non-nil, return all articles in the thread that starts
8386 with that article."
8387   (let* ((article (or article (gnus-summary-article-number)))
8388          (data (gnus-data-find-list article))
8389          (top-level (gnus-data-level (car data)))
8390          (top-subject
8391           (cond ((null gnus-thread-operation-ignore-subject)
8392                  (gnus-simplify-subject-re
8393                   (mail-header-subject (gnus-data-header (car data)))))
8394                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8395                  (gnus-simplify-subject-fuzzy
8396                   (mail-header-subject (gnus-data-header (car data)))))
8397                 (t nil)))
8398          (end-point (save-excursion
8399                       (if (gnus-summary-go-to-next-thread)
8400                           (point) (point-max))))
8401          articles)
8402     (while (and data
8403                 (< (gnus-data-pos (car data)) end-point))
8404       (when (or (not top-subject)
8405                 (string= top-subject
8406                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8407                              (gnus-simplify-subject-fuzzy
8408                               (mail-header-subject
8409                                (gnus-data-header (car data))))
8410                            (gnus-simplify-subject-re
8411                             (mail-header-subject
8412                              (gnus-data-header (car data)))))))
8413         (push (gnus-data-number (car data)) articles))
8414       (unless (and (setq data (cdr data))
8415                    (> (gnus-data-level (car data)) top-level))
8416         (setq data nil)))
8417     ;; Return the list of articles.
8418     (nreverse articles)))
8419
8420 (defun gnus-summary-rethread-current ()
8421   "Rethread the thread the current article is part of."
8422   (interactive)
8423   (let* ((gnus-show-threads t)
8424          (article (gnus-summary-article-number))
8425          (id (mail-header-id (gnus-summary-article-header)))
8426          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8427     (unless id
8428       (error "No article on the current line"))
8429     (gnus-rebuild-thread id)
8430     (gnus-summary-goto-subject article)))
8431
8432 (defun gnus-summary-reparent-thread ()
8433   "Make the current article child of the marked (or previous) article.
8434
8435 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8436 is non-nil or the Subject: of both articles are the same."
8437   (interactive)
8438   (unless (not (gnus-group-read-only-p))
8439     (error "The current newsgroup does not support article editing"))
8440   (unless (<= (length gnus-newsgroup-processable) 1)
8441     (error "No more than one article may be marked"))
8442   (save-window-excursion
8443     (let ((gnus-article-buffer " *reparent*")
8444           (current-article (gnus-summary-article-number))
8445           ;; First grab the marked article, otherwise one line up.
8446           (parent-article (if (not (null gnus-newsgroup-processable))
8447                               (car gnus-newsgroup-processable)
8448                             (save-excursion
8449                               (if (eq (forward-line -1) 0)
8450                                   (gnus-summary-article-number)
8451                                 (error "Beginning of summary buffer"))))))
8452       (unless (not (eq current-article parent-article))
8453         (error "An article may not be self-referential"))
8454       (let ((message-id (mail-header-id
8455                          (gnus-summary-article-header parent-article))))
8456         (unless (and message-id (not (equal message-id "")))
8457           (error "No message-id in desired parent"))
8458         (gnus-with-article current-article
8459           (goto-char (point-min))
8460           (if (re-search-forward "^References: " nil t)
8461               (progn
8462                 (re-search-forward "^[^ \t]" nil t)
8463                 (forward-line -1)
8464                 (end-of-line)
8465                 (insert " " message-id))
8466             (insert "References: " message-id "\n")))
8467         (set-buffer gnus-summary-buffer)
8468         (gnus-summary-unmark-all-processable)
8469         (gnus-summary-update-article current-article)
8470         (gnus-summary-rethread-current)
8471         (gnus-message 3 "Article %d is now the child of article %d"
8472                       current-article parent-article)))))
8473
8474 (defun gnus-summary-toggle-threads (&optional arg)
8475   "Toggle showing conversation threads.
8476 If ARG is positive number, turn showing conversation threads on."
8477   (interactive "P")
8478   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8479     (setq gnus-show-threads
8480           (if (null arg) (not gnus-show-threads)
8481             (> (prefix-numeric-value arg) 0)))
8482     (gnus-summary-prepare)
8483     (gnus-summary-goto-subject current)
8484     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8485     (gnus-summary-position-point)))
8486
8487 (defun gnus-summary-show-all-threads ()
8488   "Show all threads."
8489   (interactive)
8490   (save-excursion
8491     (let ((buffer-read-only nil))
8492       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8493   (gnus-summary-position-point))
8494
8495 (defun gnus-summary-show-thread ()
8496   "Show thread subtrees.
8497 Returns nil if no thread was there to be shown."
8498   (interactive)
8499   (let ((buffer-read-only nil)
8500         (orig (point))
8501         ;; first goto end then to beg, to have point at beg after let
8502         (end (progn (end-of-line) (point)))
8503         (beg (progn (beginning-of-line) (point))))
8504     (prog1
8505         ;; Any hidden lines here?
8506         (search-forward "\r" end t)
8507       (subst-char-in-region beg end ?\^M ?\n t)
8508       (goto-char orig)
8509       (gnus-summary-position-point))))
8510
8511 (defun gnus-summary-hide-all-threads ()
8512   "Hide all thread subtrees."
8513   (interactive)
8514   (save-excursion
8515     (goto-char (point-min))
8516     (gnus-summary-hide-thread)
8517     (while (zerop (gnus-summary-next-thread 1 t))
8518       (gnus-summary-hide-thread)))
8519   (gnus-summary-position-point))
8520
8521 (defun gnus-summary-hide-thread ()
8522   "Hide thread subtrees.
8523 Returns nil if no threads were there to be hidden."
8524   (interactive)
8525   (let ((buffer-read-only nil)
8526         (start (point))
8527         (article (gnus-summary-article-number)))
8528     (goto-char start)
8529     ;; Go forward until either the buffer ends or the subthread
8530     ;; ends.
8531     (when (and (not (eobp))
8532                (or (zerop (gnus-summary-next-thread 1 t))
8533                    (goto-char (point-max))))
8534       (prog1
8535           (if (and (> (point) start)
8536                    (search-backward "\n" start t))
8537               (progn
8538                 (subst-char-in-region start (point) ?\n ?\^M)
8539                 (gnus-summary-goto-subject article))
8540             (goto-char start)
8541             nil)
8542         ;;(gnus-summary-position-point)
8543         ))))
8544
8545 (defun gnus-summary-go-to-next-thread (&optional previous)
8546   "Go to the same level (or less) next thread.
8547 If PREVIOUS is non-nil, go to previous thread instead.
8548 Return the article number moved to, or nil if moving was impossible."
8549   (let ((level (gnus-summary-thread-level))
8550         (way (if previous -1 1))
8551         (beg (point)))
8552     (forward-line way)
8553     (while (and (not (eobp))
8554                 (< level (gnus-summary-thread-level)))
8555       (forward-line way))
8556     (if (eobp)
8557         (progn
8558           (goto-char beg)
8559           nil)
8560       (setq beg (point))
8561       (prog1
8562           (gnus-summary-article-number)
8563         (goto-char beg)))))
8564
8565 (defun gnus-summary-next-thread (n &optional silent)
8566   "Go to the same level next N'th thread.
8567 If N is negative, search backward instead.
8568 Returns the difference between N and the number of skips actually
8569 done.
8570
8571 If SILENT, don't output messages."
8572   (interactive "p")
8573   (let ((backward (< n 0))
8574         (n (abs n)))
8575     (while (and (> n 0)
8576                 (gnus-summary-go-to-next-thread backward))
8577       (decf n))
8578     (unless silent
8579       (gnus-summary-position-point))
8580     (when (and (not silent) (/= 0 n))
8581       (gnus-message 7 "No more threads"))
8582     n))
8583
8584 (defun gnus-summary-prev-thread (n)
8585   "Go to the same level previous N'th thread.
8586 Returns the difference between N and the number of skips actually
8587 done."
8588   (interactive "p")
8589   (gnus-summary-next-thread (- n)))
8590
8591 (defun gnus-summary-go-down-thread ()
8592   "Go down one level in the current thread."
8593   (let ((children (gnus-summary-article-children)))
8594     (when children
8595       (gnus-summary-goto-subject (car children)))))
8596
8597 (defun gnus-summary-go-up-thread ()
8598   "Go up one level in the current thread."
8599   (let ((parent (gnus-summary-article-parent)))
8600     (when parent
8601       (gnus-summary-goto-subject parent))))
8602
8603 (defun gnus-summary-down-thread (n)
8604   "Go down thread N steps.
8605 If N is negative, go up instead.
8606 Returns the difference between N and how many steps down that were
8607 taken."
8608   (interactive "p")
8609   (let ((up (< n 0))
8610         (n (abs n)))
8611     (while (and (> n 0)
8612                 (if up (gnus-summary-go-up-thread)
8613                   (gnus-summary-go-down-thread)))
8614       (setq n (1- n)))
8615     (gnus-summary-position-point)
8616     (when (/= 0 n)
8617       (gnus-message 7 "Can't go further"))
8618     n))
8619
8620 (defun gnus-summary-up-thread (n)
8621   "Go up thread N steps.
8622 If N is negative, go up instead.
8623 Returns the difference between N and how many steps down that were
8624 taken."
8625   (interactive "p")
8626   (gnus-summary-down-thread (- n)))
8627
8628 (defun gnus-summary-top-thread ()
8629   "Go to the top of the thread."
8630   (interactive)
8631   (while (gnus-summary-go-up-thread))
8632   (gnus-summary-article-number))
8633
8634 (defun gnus-summary-kill-thread (&optional unmark)
8635   "Mark articles under current thread as read.
8636 If the prefix argument is positive, remove any kinds of marks.
8637 If the prefix argument is negative, tick articles instead."
8638   (interactive "P")
8639   (when unmark
8640     (setq unmark (prefix-numeric-value unmark)))
8641   (let ((articles (gnus-summary-articles-in-thread)))
8642     (save-excursion
8643       ;; Expand the thread.
8644       (gnus-summary-show-thread)
8645       ;; Mark all the articles.
8646       (while articles
8647         (gnus-summary-goto-subject (car articles))
8648         (cond ((null unmark)
8649                (gnus-summary-mark-article-as-read gnus-killed-mark))
8650               ((> unmark 0)
8651                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8652               (t
8653                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8654         (setq articles (cdr articles))))
8655     ;; Hide killed subtrees.
8656     (and (null unmark)
8657          gnus-thread-hide-killed
8658          (gnus-summary-hide-thread))
8659     ;; If marked as read, go to next unread subject.
8660     (when (null unmark)
8661       ;; Go to next unread subject.
8662       (gnus-summary-next-subject 1 t)))
8663   (gnus-set-mode-line 'summary))
8664
8665 ;; Summary sorting commands
8666
8667 (defun gnus-summary-sort-by-number (&optional reverse)
8668   "Sort the summary buffer by article number.
8669 Argument REVERSE means reverse order."
8670   (interactive "P")
8671   (gnus-summary-sort 'number reverse))
8672
8673 (defun gnus-summary-sort-by-author (&optional reverse)
8674   "Sort the summary buffer by author name alphabetically.
8675 If case-fold-search is non-nil, case of letters is ignored.
8676 Argument REVERSE means reverse order."
8677   (interactive "P")
8678   (gnus-summary-sort 'author reverse))
8679
8680 (defun gnus-summary-sort-by-subject (&optional reverse)
8681   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8682 If case-fold-search is non-nil, case of letters is ignored.
8683 Argument REVERSE means reverse order."
8684   (interactive "P")
8685   (gnus-summary-sort 'subject reverse))
8686
8687 (defun gnus-summary-sort-by-date (&optional reverse)
8688   "Sort the summary buffer by date.
8689 Argument REVERSE means reverse order."
8690   (interactive "P")
8691   (gnus-summary-sort 'date reverse))
8692
8693 (defun gnus-summary-sort-by-score (&optional reverse)
8694   "Sort the summary buffer by score.
8695 Argument REVERSE means reverse order."
8696   (interactive "P")
8697   (gnus-summary-sort 'score reverse))
8698
8699 (defun gnus-summary-sort-by-lines (&optional reverse)
8700   "Sort the summary buffer by the number of lines.
8701 Argument REVERSE means reverse order."
8702   (interactive "P")
8703   (gnus-summary-sort 'lines reverse))
8704
8705 (defun gnus-summary-sort-by-chars (&optional reverse)
8706   "Sort the summary buffer by article length.
8707 Argument REVERSE means reverse order."
8708   (interactive "P")
8709   (gnus-summary-sort 'chars reverse))   
8710
8711 (defun gnus-summary-sort (predicate reverse)
8712   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8713   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8714          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8715          (gnus-thread-sort-functions
8716           (if (not reverse)
8717               thread
8718             `(lambda (t1 t2)
8719                (,thread t2 t1))))
8720          (gnus-article-sort-functions
8721           (if (not reverse)
8722               article
8723             `(lambda (t1 t2)
8724                (,article t2 t1))))
8725          (buffer-read-only)
8726          (gnus-summary-prepare-hook nil))
8727     ;; We do the sorting by regenerating the threads.
8728     (gnus-summary-prepare)
8729     ;; Hide subthreads if needed.
8730     (when (and gnus-show-threads gnus-thread-hide-subtree)
8731       (gnus-summary-hide-all-threads))))
8732
8733 ;; Summary saving commands.
8734
8735 (defun gnus-summary-save-article (&optional n not-saved)
8736   "Save the current article using the default saver function.
8737 If N is a positive number, save the N next articles.
8738 If N is a negative number, save the N previous articles.
8739 If N is nil and any articles have been marked with the process mark,
8740 save those articles instead.
8741 The variable `gnus-default-article-saver' specifies the saver function."
8742   (interactive "P")
8743   (let* ((articles (gnus-summary-work-articles n))
8744          (save-buffer (save-excursion
8745                         (nnheader-set-temp-buffer " *Gnus Save*")))
8746          (num (length articles))
8747          header file)
8748     (dolist (article articles)
8749       (setq header (gnus-summary-article-header article))
8750       (if (not (vectorp header))
8751           ;; This is a pseudo-article.
8752           (if (assq 'name header)
8753               (gnus-copy-file (cdr (assq 'name header)))
8754             (gnus-message 1 "Article %d is unsaveable" article))
8755         ;; This is a real article.
8756         (save-window-excursion
8757           (gnus-summary-select-article t nil nil article))
8758         (save-excursion
8759           (set-buffer save-buffer)
8760           (erase-buffer)
8761           (insert-buffer-substring gnus-original-article-buffer))
8762         (setq file (gnus-article-save save-buffer file num))
8763         (gnus-summary-remove-process-mark article)
8764         (unless not-saved
8765           (gnus-summary-set-saved-mark article))))
8766     (gnus-kill-buffer save-buffer)
8767     (gnus-summary-position-point)
8768     (gnus-set-mode-line 'summary)
8769     n))
8770
8771 (defun gnus-summary-pipe-output (&optional arg)
8772   "Pipe the current article to a subprocess.
8773 If N is a positive number, pipe the N next articles.
8774 If N is a negative number, pipe the N previous articles.
8775 If N is nil and any articles have been marked with the process mark,
8776 pipe those articles instead."
8777   (interactive "P")
8778   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8779     (gnus-summary-save-article arg t))
8780   (gnus-configure-windows 'pipe))
8781
8782 (defun gnus-summary-save-article-mail (&optional arg)
8783   "Append the current article to an mail file.
8784 If N is a positive number, save the N next articles.
8785 If N is a negative number, save the N previous articles.
8786 If N is nil and any articles have been marked with the process mark,
8787 save those articles instead."
8788   (interactive "P")
8789   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8790     (gnus-summary-save-article arg)))
8791
8792 (defun gnus-summary-save-article-rmail (&optional arg)
8793   "Append the current article to an rmail file.
8794 If N is a positive number, save the N next articles.
8795 If N is a negative number, save the N previous articles.
8796 If N is nil and any articles have been marked with the process mark,
8797 save those articles instead."
8798   (interactive "P")
8799   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8800     (gnus-summary-save-article arg)))
8801
8802 (defun gnus-summary-save-article-file (&optional arg)
8803   "Append the current article to a file.
8804 If N is a positive number, save the N next articles.
8805 If N is a negative number, save the N previous articles.
8806 If N is nil and any articles have been marked with the process mark,
8807 save those articles instead."
8808   (interactive "P")
8809   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8810     (gnus-summary-save-article arg)))
8811
8812 (defun gnus-summary-write-article-file (&optional arg)
8813   "Write the current article to a file, deleting the previous file.
8814 If N is a positive number, save the N next articles.
8815 If N is a negative number, save the N previous articles.
8816 If N is nil and any articles have been marked with the process mark,
8817 save those articles instead."
8818   (interactive "P")
8819   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8820     (gnus-summary-save-article arg)))
8821
8822 (defun gnus-summary-save-article-body-file (&optional arg)
8823   "Append the current article body to a file.
8824 If N is a positive number, save the N next articles.
8825 If N is a negative number, save the N previous articles.
8826 If N is nil and any articles have been marked with the process mark,
8827 save those articles instead."
8828   (interactive "P")
8829   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8830     (gnus-summary-save-article arg)))
8831
8832 (defun gnus-summary-pipe-message (program)
8833   "Pipe the current article through PROGRAM."
8834   (interactive "sProgram: ")
8835   (gnus-summary-select-article)
8836   (let ((mail-header-separator ""))
8837     (gnus-eval-in-buffer-window gnus-article-buffer
8838       (save-restriction
8839         (widen)
8840         (let ((start (window-start))
8841               buffer-read-only)
8842           (message-pipe-buffer-body program)
8843           (set-window-start (get-buffer-window (current-buffer)) start))))))
8844
8845 (defun gnus-get-split-value (methods)
8846   "Return a value based on the split METHODS."
8847   (let (split-name method result match)
8848     (when methods
8849       (save-excursion
8850         (set-buffer gnus-original-article-buffer)
8851         (save-restriction
8852           (nnheader-narrow-to-headers)
8853           (while methods
8854             (goto-char (point-min))
8855             (setq method (pop methods))
8856             (setq match (car method))
8857             (when (cond
8858                    ((stringp match)
8859                     ;; Regular expression.
8860                     (ignore-errors
8861                       (re-search-forward match nil t)))
8862                    ((gnus-functionp match)
8863                     ;; Function.
8864                     (save-restriction
8865                       (widen)
8866                       (setq result (funcall match gnus-newsgroup-name))))
8867                    ((consp match)
8868                     ;; Form.
8869                     (save-restriction
8870                       (widen)
8871                       (setq result (eval match)))))
8872               (setq split-name (append (cdr method) split-name))
8873               (cond ((stringp result)
8874                      (push (expand-file-name
8875                             result gnus-article-save-directory)
8876                            split-name))
8877                     ((consp result)
8878                      (setq split-name (append result split-name)))))))))
8879     (nreverse split-name)))
8880
8881 (defun gnus-valid-move-group-p (group)
8882   (and (boundp group)
8883        (symbol-name group)
8884        (symbol-value group)
8885        (gnus-get-function (gnus-find-method-for-group
8886                            (symbol-name group)) 'request-accept-article t)))
8887
8888 (defun gnus-read-move-group-name (prompt default articles prefix)
8889   "Read a group name."
8890   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8891          (minibuffer-confirm-incomplete nil) ; XEmacs
8892          (prom
8893           (format "%s %s to:"
8894                   prompt
8895                   (if (> (length articles) 1)
8896                       (format "these %d articles" (length articles))
8897                     "this article")))
8898          (to-newsgroup
8899           (cond
8900            ((null split-name)
8901             (gnus-completing-read default prom
8902                                   gnus-active-hashtb
8903                                   'gnus-valid-move-group-p
8904                                   nil prefix
8905                                   'gnus-group-history))
8906            ((= 1 (length split-name))
8907             (gnus-completing-read (car split-name) prom
8908                                   gnus-active-hashtb
8909                                   'gnus-valid-move-group-p
8910                                   nil nil
8911                                   'gnus-group-history))
8912            (t
8913             (gnus-completing-read nil prom
8914                                   (mapcar (lambda (el) (list el))
8915                                           (nreverse split-name))
8916                                   nil nil nil
8917                                   'gnus-group-history)))))
8918     (when to-newsgroup
8919       (if (or (string= to-newsgroup "")
8920               (string= to-newsgroup prefix))
8921           (setq to-newsgroup default))
8922       (unless to-newsgroup
8923         (error "No group name entered"))
8924       (or (gnus-active to-newsgroup)
8925           (gnus-activate-group to-newsgroup)
8926           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8927                                      to-newsgroup))
8928               (or (and (gnus-request-create-group
8929                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8930                        (gnus-activate-group to-newsgroup nil nil
8931                                             (gnus-group-name-to-method
8932                                              to-newsgroup)))
8933                   (error "Couldn't create group %s" to-newsgroup)))
8934           (error "No such group: %s" to-newsgroup)))
8935     to-newsgroup))
8936
8937 (defun gnus-summary-save-parts (type dir n reverse)
8938   "Save parts matching TYPE to DIR.
8939 If REVERSE, save parts that do not match TYPE."
8940   (interactive
8941    (list (read-string "Save parts of type: " "image/.*")
8942          (read-file-name "Save to directory: " t nil t)
8943          current-prefix-arg))
8944   (gnus-summary-iterate n
8945     (let ((gnus-display-mime-function nil)
8946           (gnus-inhibit-treatment t))
8947       (gnus-summary-select-article))
8948     (save-excursion
8949       (set-buffer gnus-article-buffer)
8950       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
8951         (when handles
8952           (gnus-summary-save-parts-1 type dir handles reverse)
8953           (mm-destroy-parts handles))))))
8954
8955 (defun gnus-summary-save-parts-1 (type dir handle reverse)
8956   (if (stringp (car handle))
8957       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
8958               (cdr handle))
8959     (when (if reverse
8960               (not (string-match type (car (mm-handle-type handle))))
8961             (string-match type (car (mm-handle-type handle))))
8962       (let ((file (expand-file-name
8963                    (file-name-nondirectory
8964                     (or
8965                      (mail-content-type-get
8966                       (mm-handle-disposition handle) 'filename)
8967                      (concat gnus-newsgroup-name "." gnus-current-article)))
8968                    dir)))
8969         (unless (file-exists-p file)
8970           (mm-save-part-to-file handle file))))))
8971
8972 ;; Summary extract commands
8973
8974 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8975   (let ((buffer-read-only nil)
8976         (article (gnus-summary-article-number))
8977         after-article b e)
8978     (unless (gnus-summary-goto-subject article)
8979       (error "No such article: %d" article))
8980     (gnus-summary-position-point)
8981     ;; If all commands are to be bunched up on one line, we collect
8982     ;; them here.
8983     (unless gnus-view-pseudos-separately
8984       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8985             files action)
8986         (while ps
8987           (setq action (cdr (assq 'action (car ps))))
8988           (setq files (list (cdr (assq 'name (car ps)))))
8989           (while (and ps (cdr ps)
8990                       (string= (or action "1")
8991                                (or (cdr (assq 'action (cadr ps))) "2")))
8992             (push (cdr (assq 'name (cadr ps))) files)
8993             (setcdr ps (cddr ps)))
8994           (when files
8995             (when (not (string-match "%s" action))
8996               (push " " files))
8997             (push " " files)
8998             (when (assq 'execute (car ps))
8999               (setcdr (assq 'execute (car ps))
9000                       (funcall (if (string-match "%s" action)
9001                                    'format 'concat)
9002                                action
9003                                (mapconcat
9004                                 (lambda (f)
9005                                   (if (equal f " ")
9006                                       f
9007                                     (gnus-quote-arg-for-sh-or-csh f)))
9008                                 files " ")))))
9009           (setq ps (cdr ps)))))
9010     (if (and gnus-view-pseudos (not not-view))
9011         (while pslist
9012           (when (assq 'execute (car pslist))
9013             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9014                                   (eq gnus-view-pseudos 'not-confirm)))
9015           (setq pslist (cdr pslist)))
9016       (save-excursion
9017         (while pslist
9018           (setq after-article (or (cdr (assq 'article (car pslist)))
9019                                   (gnus-summary-article-number)))
9020           (gnus-summary-goto-subject after-article)
9021           (forward-line 1)
9022           (setq b (point))
9023           (insert "    " (file-name-nondirectory
9024                           (cdr (assq 'name (car pslist))))
9025                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9026           (setq e (point))
9027           (forward-line -1)             ; back to `b'
9028           (gnus-add-text-properties
9029            b (1- e) (list 'gnus-number gnus-reffed-article-number
9030                           gnus-mouse-face-prop gnus-mouse-face))
9031           (gnus-data-enter
9032            after-article gnus-reffed-article-number
9033            gnus-unread-mark b (car pslist) 0 (- e b))
9034           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9035           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9036           (setq pslist (cdr pslist)))))))
9037
9038 (defun gnus-pseudos< (p1 p2)
9039   (let ((c1 (cdr (assq 'action p1)))
9040         (c2 (cdr (assq 'action p2))))
9041     (and c1 c2 (string< c1 c2))))
9042
9043 (defun gnus-request-pseudo-article (props)
9044   (cond ((assq 'execute props)
9045          (gnus-execute-command (cdr (assq 'execute props)))))
9046   (let ((gnus-current-article (gnus-summary-article-number)))
9047     (gnus-run-hooks 'gnus-mark-article-hook)))
9048
9049 (defun gnus-execute-command (command &optional automatic)
9050   (save-excursion
9051     (gnus-article-setup-buffer)
9052     (set-buffer gnus-article-buffer)
9053     (setq buffer-read-only nil)
9054     (let ((command (if automatic command
9055                      (read-string "Command: " (cons command 0)))))
9056       (erase-buffer)
9057       (insert "$ " command "\n\n")
9058       (if gnus-view-pseudo-asynchronously
9059           (start-process "gnus-execute" (current-buffer) shell-file-name
9060                          shell-command-switch command)
9061         (call-process shell-file-name nil t nil
9062                       shell-command-switch command)))))
9063
9064 ;; Summary kill commands.
9065
9066 (defun gnus-summary-edit-global-kill (article)
9067   "Edit the \"global\" kill file."
9068   (interactive (list (gnus-summary-article-number)))
9069   (gnus-group-edit-global-kill article))
9070
9071 (defun gnus-summary-edit-local-kill ()
9072   "Edit a local kill file applied to the current newsgroup."
9073   (interactive)
9074   (setq gnus-current-headers (gnus-summary-article-header))
9075   (gnus-group-edit-local-kill
9076    (gnus-summary-article-number) gnus-newsgroup-name))
9077
9078 ;;; Header reading.
9079
9080 (defun gnus-read-header (id &optional header)
9081   "Read the headers of article ID and enter them into the Gnus system."
9082   (let ((group gnus-newsgroup-name)
9083         (gnus-override-method
9084          (and (gnus-news-group-p gnus-newsgroup-name)
9085               gnus-refer-article-method))
9086         where)
9087     ;; First we check to see whether the header in question is already
9088     ;; fetched.
9089     (if (stringp id)
9090         ;; This is a Message-ID.
9091         (setq header (or header (gnus-id-to-header id)))
9092       ;; This is an article number.
9093       (setq header (or header (gnus-summary-article-header id))))
9094     (if (and header
9095              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9096         ;; We have found the header.
9097         header
9098       ;; If this is a sparse article, we have to nix out its
9099       ;; previous entry in the thread hashtb.
9100       (when (and header
9101                  (gnus-summary-article-sparse-p (mail-header-number header)))
9102         (let* ((parent (gnus-parent-id (mail-header-references header)))
9103                (thread (and parent (gnus-id-to-thread parent))))
9104           (when thread
9105             (delq (assq header thread) thread))))
9106       ;; We have to really fetch the header to this article.
9107       (save-excursion
9108         (set-buffer nntp-server-buffer)
9109         (when (setq where (gnus-request-head id group))
9110           (nnheader-fold-continuation-lines)
9111           (goto-char (point-max))
9112           (insert ".\n")
9113           (goto-char (point-min))
9114           (insert "211 ")
9115           (princ (cond
9116                   ((numberp id) id)
9117                   ((cdr where) (cdr where))
9118                   (header (mail-header-number header))
9119                   (t gnus-reffed-article-number))
9120                  (current-buffer))
9121           (insert " Article retrieved.\n"))
9122         (if (or (not where)
9123                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9124             ()                          ; Malformed head.
9125           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9126             (when (and (stringp id)
9127                        (not (string= (gnus-group-real-name group)
9128                                      (car where))))
9129               ;; If we fetched by Message-ID and the article came
9130               ;; from a different group, we fudge some bogus article
9131               ;; numbers for this article.
9132               (mail-header-set-number header gnus-reffed-article-number))
9133             (save-excursion
9134               (set-buffer gnus-summary-buffer)
9135               (decf gnus-reffed-article-number)
9136               (gnus-remove-header (mail-header-number header))
9137               (push header gnus-newsgroup-headers)
9138               (setq gnus-current-headers header)
9139               (push (mail-header-number header) gnus-newsgroup-limit)))
9140           header)))))
9141
9142 (defun gnus-remove-header (number)
9143   "Remove header NUMBER from `gnus-newsgroup-headers'."
9144   (if (and gnus-newsgroup-headers
9145            (= number (mail-header-number (car gnus-newsgroup-headers))))
9146       (pop gnus-newsgroup-headers)
9147     (let ((headers gnus-newsgroup-headers))
9148       (while (and (cdr headers)
9149                   (not (= number (mail-header-number (cadr headers)))))
9150         (pop headers))
9151       (when (cdr headers)
9152         (setcdr headers (cddr headers))))))
9153
9154 ;;;
9155 ;;; summary highlights
9156 ;;;
9157
9158 (defun gnus-highlight-selected-summary ()
9159   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9160   ;; Highlight selected article in summary buffer
9161   (when gnus-summary-selected-face
9162     (save-excursion
9163       (let* ((beg (progn (beginning-of-line) (point)))
9164              (end (progn (end-of-line) (point)))
9165              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9166              (from (if (get-text-property beg gnus-mouse-face-prop)
9167                        beg
9168                      (or (next-single-property-change
9169                           beg gnus-mouse-face-prop nil end)
9170                          beg)))
9171              (to
9172               (if (= from end)
9173                   (- from 2)
9174                 (or (next-single-property-change
9175                      from gnus-mouse-face-prop nil end)
9176                     end))))
9177         ;; If no mouse-face prop on line we will have to = from = end,
9178         ;; so we highlight the entire line instead.
9179         (when (= (+ to 2) from)
9180           (setq from beg)
9181           (setq to end))
9182         (if gnus-newsgroup-selected-overlay
9183             ;; Move old overlay.
9184             (gnus-move-overlay
9185              gnus-newsgroup-selected-overlay from to (current-buffer))
9186           ;; Create new overlay.
9187           (gnus-overlay-put
9188            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9189            'face gnus-summary-selected-face))))))
9190
9191 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9192 (defun gnus-summary-highlight-line ()
9193   "Highlight current line according to `gnus-summary-highlight'."
9194   (let* ((list gnus-summary-highlight)
9195          (p (point))
9196          (end (progn (end-of-line) (point)))
9197          ;; now find out where the line starts and leave point there.
9198          (beg (progn (beginning-of-line) (point)))
9199          (article (gnus-summary-article-number))
9200          (score (or (cdr (assq (or article gnus-current-article)
9201                                gnus-newsgroup-scored))
9202                     gnus-summary-default-score 0))
9203          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9204          (inhibit-read-only t))
9205     ;; Eval the cars of the lists until we find a match.
9206     (let ((default gnus-summary-default-score))
9207       (while (and list
9208                   (not (eval (caar list))))
9209         (setq list (cdr list))))
9210     (let ((face (cdar list)))
9211       (unless (eq face (get-text-property beg 'face))
9212         (gnus-put-text-property-excluding-characters-with-faces
9213          beg end 'face
9214          (setq face (if (boundp face) (symbol-value face) face)))
9215         (when gnus-summary-highlight-line-function
9216           (funcall gnus-summary-highlight-line-function article face))))
9217     (goto-char p)))
9218
9219 (defun gnus-update-read-articles (group unread &optional compute)
9220   "Update the list of read articles in GROUP."
9221   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9222          (entry (gnus-gethash group gnus-newsrc-hashtb))
9223          (info (nth 2 entry))
9224          (prev 1)
9225          (unread (sort (copy-sequence unread) '<))
9226          read)
9227     (if (or (not info) (not active))
9228         ;; There is no info on this group if it was, in fact,
9229         ;; killed.  Gnus stores no information on killed groups, so
9230         ;; there's nothing to be done.
9231         ;; One could store the information somewhere temporarily,
9232         ;; perhaps...  Hmmm...
9233         ()
9234       ;; Remove any negative articles numbers.
9235       (while (and unread (< (car unread) 0))
9236         (setq unread (cdr unread)))
9237       ;; Remove any expired article numbers
9238       (while (and unread (< (car unread) (car active)))
9239         (setq unread (cdr unread)))
9240       ;; Compute the ranges of read articles by looking at the list of
9241       ;; unread articles.
9242       (while unread
9243         (when (/= (car unread) prev)
9244           (push (if (= prev (1- (car unread))) prev
9245                   (cons prev (1- (car unread))))
9246                 read))
9247         (setq prev (1+ (car unread)))
9248         (setq unread (cdr unread)))
9249       (when (<= prev (cdr active))
9250         (push (cons prev (cdr active)) read))
9251       (setq read (if (> (length read) 1) (nreverse read) read))
9252       (if compute
9253           read
9254         (save-excursion
9255           (set-buffer gnus-group-buffer)
9256           (gnus-undo-register
9257             `(progn
9258                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9259                (gnus-info-set-read ',info ',(gnus-info-read info))
9260                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9261                (gnus-group-update-group ,group t))))
9262        ;; Propagate the read marks to the backend.
9263        (if (gnus-check-backend-function 'request-set-mark group)
9264            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9265                  (add (gnus-remove-from-range read (gnus-info-read info))))
9266              (when (or add del)
9267                (unless (gnus-check-group group)
9268                  (error "Can't open server for %s" group))
9269                (gnus-request-set-mark
9270                 group (delq nil (list (if add (list add 'add '(read)))
9271                                       (if del (list del 'del '(read)))))))))
9272         ;; Enter this list into the group info.
9273         (gnus-info-set-read info read)
9274         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9275         (gnus-get-unread-articles-in-group info (gnus-active group))
9276         t))))
9277
9278 (defun gnus-offer-save-summaries ()
9279   "Offer to save all active summary buffers."
9280   (save-excursion
9281     (let ((buflist (buffer-list))
9282           buffers bufname)
9283       ;; Go through all buffers and find all summaries.
9284       (while buflist
9285         (and (setq bufname (buffer-name (car buflist)))
9286              (string-match "Summary" bufname)
9287              (save-excursion
9288                (set-buffer bufname)
9289                ;; We check that this is, indeed, a summary buffer.
9290                (and (eq major-mode 'gnus-summary-mode)
9291                     ;; Also make sure this isn't bogus.
9292                     gnus-newsgroup-prepared
9293                     ;; Also make sure that this isn't a dead summary buffer.
9294                     (not gnus-dead-summary-mode)))
9295              (push bufname buffers))
9296         (setq buflist (cdr buflist)))
9297       ;; Go through all these summary buffers and offer to save them.
9298       (when buffers
9299         (map-y-or-n-p
9300          "Update summary buffer %s? "
9301          (lambda (buf)
9302            (switch-to-buffer buf)
9303            (gnus-summary-exit))
9304          buffers)))))
9305
9306
9307 ;;; @ for mime-partial
9308 ;;;
9309
9310 (defun gnus-request-partial-message ()
9311   (save-excursion
9312     (let ((number (gnus-summary-article-number))
9313           (group gnus-newsgroup-name)
9314           (mother gnus-article-buffer))
9315       (set-buffer (get-buffer-create " *Partial Article*"))
9316       (erase-buffer)
9317       (setq mime-preview-buffer mother)
9318       (gnus-request-article-this-buffer number group)
9319       (mime-parse-buffer)
9320       )))
9321
9322 (autoload 'mime-combine-message/partial-pieces-automatically
9323   "mime-partial"
9324   "Internal method to combine message/partial messages automatically.")
9325
9326 (mime-add-condition
9327  'action '((type . message)(subtype . partial)
9328            (major-mode . gnus-original-article-mode)
9329            (method . mime-combine-message/partial-pieces-automatically)
9330            (summary-buffer-exp . gnus-summary-buffer)
9331            (request-partial-message-method . gnus-request-partial-message)
9332            ))
9333
9334
9335 ;;; @ for message/rfc822
9336 ;;;
9337
9338 (defun gnus-mime-extract-message/rfc822 (entity situation)
9339   (let (group article num cwin swin cur)
9340     (with-current-buffer (mime-entity-buffer entity)
9341       (save-restriction
9342         (narrow-to-region (mime-entity-body-start entity)
9343                           (mime-entity-body-end entity))
9344         (setq group (or (cdr (assq 'group situation))
9345                         (completing-read "Group: "
9346                                          gnus-active-hashtb
9347                                          nil
9348                                          (gnus-read-active-file-p)
9349                                          gnus-newsgroup-name))
9350               article (gnus-request-accept-article group)
9351               )
9352         ))
9353     (when (and (consp article)
9354                (numberp (setq article (cdr article))))
9355       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9356             cwin (get-buffer-window (current-buffer) t)
9357             )
9358       (save-window-excursion
9359         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9360             (select-window swin)
9361           (set-buffer gnus-summary-buffer)
9362           )
9363         (setq cur gnus-current-article)
9364         (forward-line num)
9365         (let (gnus-show-threads)
9366           (gnus-summary-goto-subject article t)
9367           )
9368         (gnus-summary-clear-mark-forward 1)
9369         (gnus-summary-goto-subject cur)
9370         )
9371       (when (and cwin (window-frame cwin))
9372         (select-frame (window-frame cwin))
9373         )
9374       (when (boundp 'mime-acting-situation-to-override)
9375         (set-alist 'mime-acting-situation-to-override
9376                    'group
9377                    group)
9378         (set-alist 'mime-acting-situation-to-override
9379                    'after-method
9380                    `(progn
9381                       (save-current-buffer
9382                         (set-buffer gnus-group-buffer)
9383                         (gnus-activate-group ,group)
9384                         )
9385                       (gnus-summary-goto-article ,cur
9386                                                  gnus-show-all-headers)
9387                       ))
9388         (set-alist 'mime-acting-situation-to-override
9389                    'number num)
9390         )
9391       )))
9392
9393 (mime-add-condition
9394  'action '((type . message)(subtype . rfc822)
9395            (major-mode . gnus-original-article-mode)
9396            (method . gnus-mime-extract-message/rfc822)
9397            (mode . "extract")
9398            ))
9399
9400 (mime-add-condition
9401  'action '((type . message)(subtype . news)
9402            (major-mode . gnus-original-article-mode)
9403            (method . gnus-mime-extract-message/rfc822)
9404            (mode . "extract")
9405            ))
9406
9407 (defun gnus-mime-extract-multipart (entity situation)
9408   (let ((children (mime-entity-children entity))
9409         mime-acting-situation-to-override
9410         f)
9411     (while children
9412       (mime-play-entity (car children)
9413                         (cons (assq 'mode situation)
9414                               mime-acting-situation-to-override))
9415       (setq children (cdr children)))
9416     (if (setq f (cdr (assq 'after-method
9417                            mime-acting-situation-to-override)))
9418         (eval f)
9419       )))  
9420
9421 (mime-add-condition
9422  'action '((type . multipart)
9423            (method . gnus-mime-extract-multipart)
9424            (mode . "extract")
9425            )
9426  'with-default)
9427
9428
9429 ;;; @ end
9430 ;;;
9431
9432 (defun gnus-summary-setup-default-charset ()
9433   "Setup newsgroup default charset."
9434   (let* ((name (and gnus-newsgroup-name
9435                    (gnus-group-real-name gnus-newsgroup-name)))
9436          (ignored-charsets 
9437           (or gnus-newsgroup-ephemeral-ignored-charsets
9438               (append
9439                (and gnus-newsgroup-name
9440                     (or (gnus-group-find-parameter gnus-newsgroup-name
9441                                                    'ignored-charsets t)
9442                         (let ((alist gnus-group-ignored-charsets-alist)
9443                            elem (charsets nil))
9444                           (while (setq elem (pop alist))
9445                             (when (and name
9446                                        (string-match (car elem) name))
9447                               (setq alist nil
9448                                     charsets (cdr elem))))
9449                           charsets))))
9450               gnus-newsgroup-ignored-charsets)))
9451     (setq gnus-newsgroup-charset
9452           (or gnus-newsgroup-ephemeral-charset
9453               (and gnus-newsgroup-name
9454                    (or (gnus-group-find-parameter gnus-newsgroup-name
9455                                                   'charset)
9456                        (let ((alist gnus-group-charset-alist)
9457                              elem (charset nil))
9458                          (while (setq elem (pop alist))
9459                            (when (and name
9460                                       (string-match (car elem) name))
9461                              (setq alist nil
9462                                    charset (cadr elem))))
9463                          charset)))
9464               gnus-default-charset))
9465     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9466          ignored-charsets)))
9467
9468 ;;;
9469 ;;; Mime Commands
9470 ;;;
9471
9472 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9473   "Display the current article buffer fully MIME-buttonized.
9474 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9475 treated as multipart/mixed."
9476   (interactive "P")
9477   (require 'gnus-art)
9478   (let ((gnus-unbuttonized-mime-types nil)
9479         (gnus-mime-display-multipart-as-mixed show-all-parts))
9480     (gnus-summary-show-article)))
9481
9482 (defun gnus-summary-repair-multipart (article)
9483   "Add a Content-Type header to a multipart article without one."
9484   (interactive (list (gnus-summary-article-number)))
9485   (gnus-with-article article
9486     (message-narrow-to-head)
9487     (goto-char (point-max))
9488     (widen)
9489     (when (search-forward "\n--" nil t)
9490       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9491         (message-narrow-to-head)
9492         (message-remove-header "Mime-Version")
9493         (message-remove-header "Content-Type")
9494         (goto-char (point-max))
9495         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9496                         separator))
9497         (insert "Mime-Version: 1.0\n")
9498         (widen))))
9499   (let (gnus-mark-article-hook)
9500     (gnus-summary-select-article t t nil article)))
9501
9502 (defun gnus-summary-toggle-display-buttonized ()
9503   "Toggle the buttonizing of the article buffer."
9504   (interactive)
9505   (require 'gnus-art)
9506   (if (setq gnus-inhibit-mime-unbuttonizing
9507             (not gnus-inhibit-mime-unbuttonizing))
9508       (let ((gnus-unbuttonized-mime-types nil))
9509         (gnus-summary-show-article))
9510     (gnus-summary-show-article)))
9511
9512 ;;;
9513 ;;; with article
9514 ;;;
9515
9516 (defmacro gnus-with-article (article &rest forms)
9517   "Select ARTICLE and perform FORMS in the original article buffer.
9518 Then replace the article with the result."
9519   `(progn
9520      ;; We don't want the article to be marked as read.
9521      (let (gnus-mark-article-hook)
9522        (gnus-summary-select-article t t nil ,article))
9523      (set-buffer gnus-original-article-buffer)
9524      ,@forms
9525      (if (not (gnus-check-backend-function
9526                'request-replace-article (car gnus-article-current)))
9527          (gnus-message 5 "Read-only group; not replacing")
9528        (unless (gnus-request-replace-article
9529                 ,article (car gnus-article-current)
9530                 (current-buffer) t)
9531          (error "Couldn't replace article")))
9532      ;; The cache and backlog have to be flushed somewhat.
9533      (when gnus-keep-backlog
9534        (gnus-backlog-remove-article
9535         (car gnus-article-current) (cdr gnus-article-current)))
9536      (when gnus-use-cache
9537        (gnus-cache-update-article
9538         (car gnus-article-current) (cdr gnus-article-current)))))
9539
9540 (put 'gnus-with-article 'lisp-indent-function 1)
9541 (put 'gnus-with-article 'edebug-form-spec '(form body))
9542
9543 ;;;
9544 ;;; Generic summary marking commands
9545 ;;;
9546
9547 (defvar gnus-summary-marking-alist
9548   '((read gnus-del-mark "d")
9549     (unread gnus-unread-mark "u")
9550     (ticked gnus-ticked-mark "!")
9551     (dormant gnus-dormant-mark "?")
9552     (expirable gnus-expirable-mark "e"))
9553   "An alist of names/marks/keystrokes.")
9554
9555 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9556 (defvar gnus-summary-mark-map)
9557
9558 (defun gnus-summary-make-all-marking-commands ()
9559   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9560   (dolist (elem gnus-summary-marking-alist)
9561     (apply 'gnus-summary-make-marking-command elem)))
9562
9563 (defun gnus-summary-make-marking-command (name mark keystroke)
9564   (let ((map (make-sparse-keymap)))
9565     (define-key gnus-summary-generic-mark-map keystroke map)
9566     (dolist (lway `((next "next" next nil "n")
9567                     (next-unread "next unread" next t "N")
9568                     (prev "previous" prev nil "p")
9569                     (prev-unread "previous unread" prev t "P")
9570                     (nomove "" nil nil ,keystroke)))
9571       (let ((func (gnus-summary-make-marking-command-1
9572                    mark (car lway) lway name)))
9573         (setq func (eval func))
9574         (define-key map (nth 4 lway) func)))))
9575       
9576 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9577   `(defun ,(intern
9578             (format "gnus-summary-put-mark-as-%s%s"
9579                     name (if (eq way 'nomove)
9580                              ""
9581                            (concat "-" (symbol-name way)))))
9582      (n)
9583      ,(format
9584        "Mark the current article as %s%s.
9585 If N, the prefix, then repeat N times.
9586 If N is negative, move in reverse order.
9587 The difference between N and the actual number of articles marked is
9588 returned."
9589        name (cadr lway))
9590      (interactive "p")
9591      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9592     
9593 (defun gnus-summary-generic-mark (n mark move unread)
9594   "Mark N articles with MARK."
9595   (unless (eq major-mode 'gnus-summary-mode)
9596     (error "This command can only be used in the summary buffer"))
9597   (gnus-summary-show-thread)
9598   (let ((nummove
9599          (cond
9600           ((eq move 'next) 1)
9601           ((eq move 'prev) -1)
9602           (t 0))))
9603     (if (zerop nummove)
9604         (setq n 1)
9605       (when (< n 0)
9606         (setq n (abs n)
9607               nummove (* -1 nummove))))
9608     (while (and (> n 0)
9609                 (gnus-summary-mark-article nil mark)
9610                 (zerop (gnus-summary-next-subject nummove unread t)))
9611       (setq n (1- n)))
9612     (when (/= 0 n)
9613       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9614     (gnus-summary-recenter)
9615     (gnus-summary-position-point)
9616     (gnus-set-mode-line 'summary)
9617     n))
9618
9619 (gnus-summary-make-all-marking-commands)
9620
9621 (gnus-ems-redefine)
9622
9623 (provide 'gnus-sum)
9624
9625 (run-hooks 'gnus-sum-load-hook)
9626
9627 ;;; gnus-sum.el ends here