Synch.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mime-view)
41
42 (eval-when-compile
43   (require 'mime-play)
44   (require 'static))
45
46 (eval-and-compile
47   (autoload 'gnus-cache-articles-in-group "gnus-cache")
48   (autoload 'pgg-decrypt-region "pgg" nil t)
49   (autoload 'pgg-verify-region "pgg" nil t))
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, many commands will go to the next unread article.
186 This applies to marking commands as well as other commands that
187 \"naturally\" select the next article, like, for instance, `SPC' at
188 the end of an article.
189
190 If nil, the marking commands do NOT go to the next unread article
191 (they go to the next article instead).  If `never', commands that
192 usually go to the next unread article, will go to the next article,
193 whether it is read or not."
194   :group 'gnus-summary-marks
195   :link '(custom-manual "(gnus)Setting Marks")
196   :type '(choice (const :tag "off" nil)
197                  (const never)
198                  (sexp :menu-tag "on" t)))
199
200 (defcustom gnus-summary-default-score 0
201   "*Default article score level.
202 All scores generated by the score files will be added to this score.
203 If this variable is nil, scoring will be disabled."
204   :group 'gnus-score-default
205   :type '(choice (const :tag "disable")
206                  integer))
207
208 (defcustom gnus-summary-zcore-fuzz 0
209   "*Fuzziness factor for the zcore in the summary buffer.
210 Articles with scores closer than this to `gnus-summary-default-score'
211 will not be marked."
212   :group 'gnus-summary-format
213   :type 'integer)
214
215 (defcustom gnus-simplify-subject-fuzzy-regexp nil
216   "*Strings to be removed when doing fuzzy matches.
217 This can either be a regular expression or list of regular expressions
218 that will be removed from subject strings if fuzzy subject
219 simplification is selected."
220   :group 'gnus-thread
221   :type '(repeat regexp))
222
223 (defcustom gnus-show-threads t
224   "*If non-nil, display threads in summary mode."
225   :group 'gnus-thread
226   :type 'boolean)
227
228 (defcustom gnus-thread-hide-subtree nil
229   "*If non-nil, hide all threads initially.
230 If threads are hidden, you have to run the command
231 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
232 to expose hidden threads."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-hide-killed t
237   "*If non-nil, hide killed threads automatically."
238   :group 'gnus-thread
239   :type 'boolean)
240
241 (defcustom gnus-thread-ignore-subject t
242   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
243 If nil, articles that have different subjects from their parents will
244 start separate threads."
245   :group 'gnus-thread
246   :type 'boolean)
247
248 (defcustom gnus-thread-operation-ignore-subject t
249   "*If non-nil, subjects will be ignored when doing thread commands.
250 This affects commands like `gnus-summary-kill-thread' and
251 `gnus-summary-lower-thread'.
252
253 If this variable is nil, articles in the same thread with different
254 subjects will not be included in the operation in question.  If this
255 variable is `fuzzy', only articles that have subjects that are fuzzily
256 equal will be included."
257   :group 'gnus-thread
258   :type '(choice (const :tag "off" nil)
259                  (const fuzzy)
260                  (sexp :tag "on" t)))
261
262 (defcustom gnus-thread-indent-level 4
263   "*Number that says how much each sub-thread should be indented."
264   :group 'gnus-thread
265   :type 'integer)
266
267 (defcustom gnus-auto-extend-newsgroup t
268   "*If non-nil, extend newsgroup forward and backward when requested."
269   :group 'gnus-summary-choose
270   :type 'boolean)
271
272 (defcustom gnus-auto-select-first t
273   "*If nil, don't select the first unread article when entering a group.
274 If this variable is `best', select the highest-scored unread article
275 in the group.  If t, select the first unread article.
276
277 This variable can also be a function to place point on a likely
278 subject line.  Useful values include `gnus-summary-first-unread-subject',
279 `gnus-summary-first-unread-article' and
280 `gnus-summary-best-unread-article'.
281
282 If you want to prevent automatic selection of the first unread article
283 in some newsgroups, set the variable to nil in
284 `gnus-select-group-hook'."
285   :group 'gnus-group-select
286   :type '(choice (const :tag "none" nil)
287                  (const best)
288                  (sexp :menu-tag "first" t)
289                  (function-item gnus-summary-first-unread-subject)
290                  (function-item gnus-summary-first-unread-article)
291                  (function-item gnus-summary-best-unread-article)))
292
293 (defcustom gnus-dont-select-after-jump-to-other-group nil
294   "If non-nil, don't select the first unread article after entering the
295 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
296 it is depend on the value of `gnus-auto-select-first' whether to select
297 or not."
298   :group 'gnus-group-select
299   :type 'boolean)
300
301 (defcustom gnus-auto-select-next t
302   "*If non-nil, offer to go to the next group from the end of the previous.
303 If the value is t and the next newsgroup is empty, Gnus will exit
304 summary mode and go back to group mode.  If the value is neither nil
305 nor t, Gnus will select the following unread newsgroup.  In
306 particular, if the value is the symbol `quietly', the next unread
307 newsgroup will be selected without any confirmation, and if it is
308 `almost-quietly', the next group will be selected without any
309 confirmation if you are located on the last article in the group.
310 Finally, if this variable is `slightly-quietly', the `Z n' command
311 will go to the next group without confirmation."
312   :group 'gnus-summary-maneuvering
313   :type '(choice (const :tag "off" nil)
314                  (const quietly)
315                  (const almost-quietly)
316                  (const slightly-quietly)
317                  (sexp :menu-tag "on" t)))
318
319 (defcustom gnus-auto-select-same nil
320   "*If non-nil, select the next article with the same subject.
321 If there are no more articles with the same subject, go to
322 the first unread article."
323   :group 'gnus-summary-maneuvering
324   :type 'boolean)
325
326 (defcustom gnus-summary-check-current nil
327   "*If non-nil, consider the current article when moving.
328 The \"unread\" movement commands will stay on the same line if the
329 current article is unread."
330   :group 'gnus-summary-maneuvering
331   :type 'boolean)
332
333 (defcustom gnus-auto-center-summary t
334   "*If non-nil, always center the current summary buffer.
335 In particular, if `vertical' do only vertical recentering.  If non-nil
336 and non-`vertical', do both horizontal and vertical recentering."
337   :group 'gnus-summary-maneuvering
338   :type '(choice (const :tag "none" nil)
339                  (const vertical)
340                  (integer :tag "height")
341                  (sexp :menu-tag "both" t)))
342
343 (defcustom gnus-show-all-headers nil
344   "*If non-nil, don't hide any headers."
345   :group 'gnus-article-hiding
346   :group 'gnus-article-headers
347   :type 'boolean)
348
349 (defcustom gnus-summary-ignore-duplicates nil
350   "*If non-nil, ignore articles with identical Message-ID headers."
351   :group 'gnus-summary
352   :type 'boolean)
353
354 (defcustom gnus-single-article-buffer t
355   "*If non-nil, display all articles in the same buffer.
356 If nil, each group will get its own article buffer."
357   :group 'gnus-article-various
358   :type 'boolean)
359
360 (defcustom gnus-break-pages t
361   "*If non-nil, do page breaking on articles.
362 The page delimiter is specified by the `gnus-page-delimiter'
363 variable."
364   :group 'gnus-article-various
365   :type 'boolean)
366
367 (defcustom gnus-show-mime t
368   "*If non-nil, do mime processing of articles.
369 The articles will simply be fed to the function given by
370 `gnus-article-display-method-for-mime'."
371   :group 'gnus-article-mime
372   :type 'boolean)
373
374 (defcustom gnus-move-split-methods nil
375   "*Variable used to suggest where articles are to be moved to.
376 It uses the same syntax as the `gnus-split-methods' variable."
377   :group 'gnus-summary-mail
378   :type '(repeat (choice (list :value (fun) function)
379                          (cons :value ("" "") regexp (repeat string))
380                          (sexp :value nil))))
381
382 (defcustom gnus-unread-mark ?  ;Whitespace
383   "*Mark used for unread articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-ticked-mark ?!
388   "*Mark used for ticked articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-dormant-mark ??
393   "*Mark used for dormant articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-del-mark ?r
398   "*Mark used for del'd articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-read-mark ?R
403   "*Mark used for read articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-expirable-mark ?E
408   "*Mark used for expirable articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-killed-mark ?K
413   "*Mark used for killed articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-souped-mark ?F
418   "*Mark used for killed articles."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-kill-file-mark ?X
423   "*Mark used for articles killed by kill files."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-low-score-mark ?Y
428   "*Mark used for articles with a low score."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-catchup-mark ?C
433   "*Mark used for articles that are caught up."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-replied-mark ?A
438   "*Mark used for articles that have been replied to."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-cached-mark ?*
443   "*Mark used for articles that are in the cache."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-saved-mark ?S
448   "*Mark used for articles that have been saved to."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-ancient-mark ?O
453   "*Mark used for ancient articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-sparse-mark ?Q
458   "*Mark used for sparsely reffed articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-canceled-mark ?G
463   "*Mark used for canceled articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-duplicate-mark ?M
468   "*Mark used for duplicate articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-undownloaded-mark ?@
473   "*Mark used for articles that weren't downloaded."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-downloadable-mark ?%
478   "*Mark used for articles that are to be downloaded."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-unsendable-mark ?=
483   "*Mark used for articles that won't be sent."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-score-over-mark ?+
488   "*Score mark used for articles with high scores."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-score-below-mark ?-
493   "*Score mark used for articles with low scores."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-empty-thread-mark ?  ;Whitespace
498   "*There is no thread under the article."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-not-empty-thread-mark ?=
503   "*There is a thread under the article."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-view-pseudo-asynchronously nil
508   "*If non-nil, Gnus will view pseudo-articles asynchronously."
509   :group 'gnus-extract-view
510   :type 'boolean)
511
512 (defcustom gnus-auto-expirable-marks
513   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
514         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
515         gnus-souped-mark gnus-duplicate-mark)
516   "*The list of marks converted into expiration if a group is auto-expirable."
517   :group 'gnus-summary
518   :type '(repeat character))
519
520 (defcustom gnus-inhibit-user-auto-expire t
521   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
522   :group 'gnus-summary
523   :type 'boolean)
524
525 (defcustom gnus-view-pseudos nil
526   "*If `automatic', pseudo-articles will be viewed automatically.
527 If `not-confirm', pseudos will be viewed automatically, and the user
528 will not be asked to confirm the command."
529   :group 'gnus-extract-view
530   :type '(choice (const :tag "off" nil)
531                  (const automatic)
532                  (const not-confirm)))
533
534 (defcustom gnus-view-pseudos-separately t
535   "*If non-nil, one pseudo-article will be created for each file to be viewed.
536 If nil, all files that use the same viewing command will be given as a
537 list of parameters to that command."
538   :group 'gnus-extract-view
539   :type 'boolean)
540
541 (defcustom gnus-insert-pseudo-articles t
542   "*If non-nil, insert pseudo-articles when decoding articles."
543   :group 'gnus-extract-view
544   :type 'boolean)
545
546 (defcustom gnus-summary-dummy-line-format
547   "  %(:                          :%) %S\n"
548   "*The format specification for the dummy roots in the summary buffer.
549 It works along the same lines as a normal formatting string,
550 with some simple extensions.
551
552 %S  The subject"
553   :group 'gnus-threading
554   :type 'string)
555
556 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
557   "*The format specification for the summary mode line.
558 It works along the same lines as a normal formatting string,
559 with some simple extensions:
560
561 %G  Group name
562 %p  Unprefixed group name
563 %A  Current article number
564 %z  Current article score
565 %V  Gnus version
566 %U  Number of unread articles in the group
567 %e  Number of unselected articles in the group
568 %Z  A string with unread/unselected article counts
569 %g  Shortish group name
570 %S  Subject of the current article
571 %u  User-defined spec
572 %s  Current score file name
573 %d  Number of dormant articles
574 %r  Number of articles that have been marked as read in this session
575 %E  Number of articles expunged by the score files"
576   :group 'gnus-summary-format
577   :type 'string)
578
579 (defcustom gnus-list-identifiers nil
580   "Regexp that matches list identifiers to be removed from subject.
581 This can also be a list of regexps."
582   :group 'gnus-summary-format
583   :group 'gnus-article-hiding
584   :type '(choice (const :tag "none" nil)
585                  (regexp :value ".*")
586                  (repeat :value (".*") regexp)))
587
588 (defcustom gnus-summary-mark-below 0
589   "*Mark all articles with a score below this variable as read.
590 This variable is local to each summary buffer and usually set by the
591 score file."
592   :group 'gnus-score-default
593   :type 'integer)
594
595 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
596   "*List of functions used for sorting articles in the summary buffer.
597 This variable is only used when not using a threaded display."
598   :group 'gnus-summary-sort
599   :type '(repeat (choice (function-item gnus-article-sort-by-number)
600                          (function-item gnus-article-sort-by-author)
601                          (function-item gnus-article-sort-by-subject)
602                          (function-item gnus-article-sort-by-date)
603                          (function-item gnus-article-sort-by-score)
604                          (function :tag "other"))))
605
606 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
607   "*List of functions used for sorting threads in the summary buffer.
608 By default, threads are sorted by article number.
609
610 Each function takes two threads and return non-nil if the first thread
611 should be sorted before the other.  If you use more than one function,
612 the primary sort function should be the last.  You should probably
613 always include `gnus-thread-sort-by-number' in the list of sorting
614 functions -- preferably first.
615
616 Ready-made functions include `gnus-thread-sort-by-number',
617 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
618 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
619 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
620   :group 'gnus-summary-sort
621   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
622                          (function-item gnus-thread-sort-by-author)
623                          (function-item gnus-thread-sort-by-subject)
624                          (function-item gnus-thread-sort-by-date)
625                          (function-item gnus-thread-sort-by-score)
626                          (function-item gnus-thread-sort-by-total-score)
627                          (function :tag "other"))))
628
629 (defcustom gnus-thread-score-function '+
630   "*Function used for calculating the total score of a thread.
631
632 The function is called with the scores of the article and each
633 subthread and should then return the score of the thread.
634
635 Some functions you can use are `+', `max', or `min'."
636   :group 'gnus-summary-sort
637   :type 'function)
638
639 (defcustom gnus-summary-expunge-below nil
640   "All articles that have a score less than this variable will be expunged.
641 This variable is local to the summary buffers."
642   :group 'gnus-score-default
643   :type '(choice (const :tag "off" nil)
644                  integer))
645
646 (defcustom gnus-thread-expunge-below nil
647   "All threads that have a total score less than this variable will be expunged.
648 See `gnus-thread-score-function' for en explanation of what a
649 \"thread score\" is.
650
651 This variable is local to the summary buffers."
652   :group 'gnus-threading
653   :group 'gnus-score-default
654   :type '(choice (const :tag "off" nil)
655                  integer))
656
657 (defcustom gnus-summary-mode-hook nil
658   "*A hook for Gnus summary mode.
659 This hook is run before any variables are set in the summary buffer."
660   :group 'gnus-summary-various
661   :type 'hook)
662
663 (defcustom gnus-summary-menu-hook nil
664   "*Hook run after the creation of the summary mode menu."
665   :group 'gnus-summary-visual
666   :type 'hook)
667
668 (defcustom gnus-summary-exit-hook nil
669   "*A hook called on exit from the summary buffer.
670 It will be called with point in the group buffer."
671   :group 'gnus-summary-exit
672   :type 'hook)
673
674 (defcustom gnus-summary-prepare-hook nil
675   "*A hook called after the summary buffer has been generated.
676 If you want to modify the summary buffer, you can use this hook."
677   :group 'gnus-summary-various
678   :type 'hook)
679
680 (defcustom gnus-summary-prepared-hook nil
681   "*A hook called as the last thing after the summary buffer has been generated."
682   :group 'gnus-summary-various
683   :type 'hook)
684
685 (defcustom gnus-summary-generate-hook nil
686   "*A hook run just before generating the summary buffer.
687 This hook is commonly used to customize threading variables and the
688 like."
689   :group 'gnus-summary-various
690   :type 'hook)
691
692 (defcustom gnus-select-group-hook nil
693   "*A hook called when a newsgroup is selected.
694
695 If you'd like to simplify subjects like the
696 `gnus-summary-next-same-subject' command does, you can use the
697 following hook:
698
699  (setq gnus-select-group-hook
700       (list
701         (lambda ()
702           (mapcar (lambda (header)
703                      (mail-header-set-subject
704                       header
705                       (gnus-simplify-subject
706                        (mail-header-subject header) 're-only)))
707                   gnus-newsgroup-headers))))"
708   :group 'gnus-group-select
709   :type 'hook)
710
711 (defcustom gnus-select-article-hook nil
712   "*A hook called when an article is selected."
713   :group 'gnus-summary-choose
714   :type 'hook)
715
716 (defcustom gnus-visual-mark-article-hook
717   (list 'gnus-highlight-selected-summary)
718   "*Hook run after selecting an article in the summary buffer.
719 It is meant to be used for highlighting the article in some way.  It
720 is not run if `gnus-visual' is nil."
721   :group 'gnus-summary-visual
722   :type 'hook)
723
724 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
725   "*A hook called before parsing the headers."
726   :group 'gnus-various
727   :type 'hook)
728
729 (defcustom gnus-exit-group-hook nil
730   "*A hook called when exiting summary mode.
731 This hook is not called from the non-updating exit commands like `Q'."
732   :group 'gnus-various
733   :type 'hook)
734
735 (defcustom gnus-summary-update-hook
736   (list 'gnus-summary-highlight-line)
737   "*A hook called when a summary line is changed.
738 The hook will not be called if `gnus-visual' is nil.
739
740 The default function `gnus-summary-highlight-line' will
741 highlight the line according to the `gnus-summary-highlight'
742 variable."
743   :group 'gnus-summary-visual
744   :type 'hook)
745
746 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
747   "*A hook called when an article is selected for the first time.
748 The hook is intended to mark an article as read (or unread)
749 automatically when it is selected."
750   :group 'gnus-summary-choose
751   :type 'hook)
752
753 (defcustom gnus-group-no-more-groups-hook nil
754   "*A hook run when returning to group mode having no more (unread) groups."
755   :group 'gnus-group-select
756   :type 'hook)
757
758 (defcustom gnus-ps-print-hook nil
759   "*A hook run before ps-printing something from Gnus."
760   :group 'gnus-summary
761   :type 'hook)
762
763 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
764   "Face used for highlighting the current article in the summary buffer."
765   :group 'gnus-summary-visual
766   :type 'face)
767
768 (defcustom gnus-summary-highlight
769   '(((= mark gnus-canceled-mark)
770      . gnus-summary-cancelled-face)
771     ((and (> score default)
772           (or (= mark gnus-dormant-mark)
773               (= mark gnus-ticked-mark)))
774      . gnus-summary-high-ticked-face)
775     ((and (< score default)
776           (or (= mark gnus-dormant-mark)
777               (= mark gnus-ticked-mark)))
778      . gnus-summary-low-ticked-face)
779     ((or (= mark gnus-dormant-mark)
780          (= mark gnus-ticked-mark))
781      . gnus-summary-normal-ticked-face)
782     ((and (> score default) (= mark gnus-ancient-mark))
783      . gnus-summary-high-ancient-face)
784     ((and (< score default) (= mark gnus-ancient-mark))
785      . gnus-summary-low-ancient-face)
786     ((= mark gnus-ancient-mark)
787      . gnus-summary-normal-ancient-face)
788     ((and (> score default) (= mark gnus-unread-mark))
789      . gnus-summary-high-unread-face)
790     ((and (< score default) (= mark gnus-unread-mark))
791      . gnus-summary-low-unread-face)
792     ((and (memq article gnus-newsgroup-incorporated) 
793           (= mark gnus-unread-mark))
794      . gnus-summary-incorporated-face)
795     ((= mark gnus-unread-mark)
796      . gnus-summary-normal-unread-face)
797     ((and (> score default) (memq mark (list gnus-downloadable-mark
798                                              gnus-undownloaded-mark)))
799      . gnus-summary-high-unread-face)
800     ((and (< score default) (memq mark (list gnus-downloadable-mark
801                                              gnus-undownloaded-mark)))
802      . gnus-summary-low-unread-face)
803     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
804      . gnus-summary-normal-unread-face)
805     ((> score default)
806      . gnus-summary-high-read-face)
807     ((< score default)
808      . gnus-summary-low-read-face)
809     (t
810      . gnus-summary-normal-read-face))
811   "*Controls the highlighting of summary buffer lines.
812
813 A list of (FORM . FACE) pairs.  When deciding how a a particular
814 summary line should be displayed, each form is evaluated.  The content
815 of the face field after the first true form is used.  You can change
816 how those summary lines are displayed, by editing the face field.
817
818 You can use the following variables in the FORM field.
819
820 score:   The articles score
821 default: The default article score.
822 below:   The score below which articles are automatically marked as read.
823 mark:    The articles mark."
824   :group 'gnus-summary-visual
825   :type '(repeat (cons (sexp :tag "Form" nil)
826                        face)))
827
828 (defcustom gnus-alter-header-function nil
829   "Function called to allow alteration of article header structures.
830 The function is called with one parameter, the article header vector,
831 which it may alter in any way.")
832
833 (defvar gnus-decode-encoded-word-function
834   (mime-find-field-decoder 'From 'nov)
835   "Variable that says which function should be used to decode a string with encoded words.")
836
837 (defcustom gnus-extra-headers nil
838   "*Extra headers to parse."
839   :group 'gnus-summary
840   :type '(repeat symbol))
841
842 (defcustom gnus-ignored-from-addresses
843   (and user-mail-address (regexp-quote user-mail-address))
844   "*Regexp of From headers that may be suppressed in favor of To headers."
845   :group 'gnus-summary
846   :type 'regexp)
847
848 (defcustom gnus-group-charset-alist
849   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
850     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
851     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
852     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
853     ("^relcom\\>" koi8-r)
854     ("^fido7\\>" koi8-r)
855     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
856     ("^israel\\>" iso-8859-1)
857     ("^han\\>" euc-kr)
858     ("^alt.chinese.text.big5\\>" chinese-big5)
859     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
860     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
861     (".*" iso-8859-1))
862   "Alist of regexps (to match group names) and default charsets to be used when reading."
863   :type '(repeat (list (regexp :tag "Group")
864                        (symbol :tag "Charset")))
865   :group 'gnus-charset)
866
867 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
868   "List of charsets that should be ignored.
869 When these charsets are used in the \"charset\" parameter, the
870 default charset will be used instead."
871   :type '(repeat symbol)
872   :group 'gnus-charset)
873
874 (defcustom gnus-group-ignored-charsets-alist 
875   '(("alt\\.chinese\\.text" iso-8859-1))
876   "Alist of regexps (to match group names) and charsets that should be ignored.
877 When these charsets are used in the \"charset\" parameter, the
878 default charset will be used instead."
879   :type '(repeat (cons (regexp :tag "Group")
880                        (repeat symbol)))
881   :group 'gnus-charset)
882
883 (defcustom gnus-group-highlight-words-alist nil
884   "Alist of group regexps and highlight regexps.
885 This variable uses the same syntax as `gnus-emphasis-alist'."
886   :type '(repeat (cons (regexp :tag "Group")
887                        (repeat (list (regexp :tag "Highlight regexp")
888                                      (number :tag "Group for entire word" 0)
889                                      (number :tag "Group for displayed part" 0)
890                                      (symbol :tag "Face" 
891                                              gnus-emphasis-highlight-words)))))
892   :group 'gnus-summary-visual)
893
894 (defcustom gnus-use-wheel nil
895   "Use Intelli-mouse on summary movement"
896   :type 'boolean
897   :group 'gnus-summary-maneuvering)
898
899 (defcustom gnus-wheel-scroll-amount '(5 . 1)
900   "Amount to scroll messages by spinning the mouse wheel.
901 This is actually a cons cell, where the first item is the amount to scroll
902 on a normal wheel event, and the second is the amount to scroll when the
903 wheel is moved with the shift key depressed."
904   :type '(cons (integer :tag "Shift") integer)
905   :group 'gnus-summary-maneuvering)
906
907 (defcustom gnus-wheel-edge-resistance 2
908   "How hard it should be to change the current article
909 by moving the mouse over the edge of the article window."
910   :type 'integer
911   :group 'gnus-summary-maneuvering)
912
913 (defcustom gnus-summary-show-article-charset-alist
914   nil
915   "Alist of number and charset.
916 The article will be shown with the charset corresponding to the
917 numbered argument.
918 For example: ((1 . cn-gb-2312) (2 . big5))."
919   :type '(repeat (cons (number :tag "Argument" 1)
920                        (symbol :tag "Charset")))
921   :group 'gnus-charset)
922
923 (defcustom gnus-preserve-marks t
924   "Whether marks are preserved when moving, copying and respooling messages."
925   :type 'boolean
926   :group 'gnus-summary-marks)
927
928 (defcustom gnus-alter-articles-to-read-function nil
929   "Function to be called to alter the list of articles to be selected."
930   :type 'function
931   :group 'gnus-summary)
932
933 (defcustom gnus-orphan-score nil
934   "*All orphans get this score added.  Set in the score file."
935   :group 'gnus-score-default
936   :type '(choice (const nil)
937                  integer))
938
939 ;;; Internal variables
940
941 (defvar gnus-article-mime-handles nil)
942 (defvar gnus-article-decoded-p nil)
943 (defvar gnus-scores-exclude-files nil)
944 (defvar gnus-page-broken nil)
945 (defvar gnus-inhibit-mime-unbuttonizing nil)
946
947 (defvar gnus-original-article nil)
948 (defvar gnus-article-internal-prepare-hook nil)
949 (defvar gnus-newsgroup-process-stack nil)
950
951 (defvar gnus-thread-indent-array nil)
952 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
953 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
954   "Function called to sort the articles within a thread after it has been gathered together.")
955
956 ;; Avoid highlighting in kill files.
957 (defvar gnus-summary-inhibit-highlight nil)
958 (defvar gnus-newsgroup-selected-overlay nil)
959 (defvar gnus-inhibit-limiting nil)
960 (defvar gnus-newsgroup-adaptive-score-file nil)
961 (defvar gnus-current-score-file nil)
962 (defvar gnus-current-move-group nil)
963 (defvar gnus-current-copy-group nil)
964 (defvar gnus-current-crosspost-group nil)
965
966 (defvar gnus-newsgroup-dependencies nil)
967 (defvar gnus-newsgroup-adaptive nil)
968 (defvar gnus-summary-display-article-function nil)
969 (defvar gnus-summary-highlight-line-function nil
970   "Function called after highlighting a summary line.")
971
972 (defvar gnus-summary-line-format-alist
973   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
974     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
975     (?s gnus-tmp-subject-or-nil ?s)
976     (?n gnus-tmp-name ?s)
977     (?A (std11-address-string
978          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
979     (?a (or (std11-full-name-string
980              (car (mime-entity-read-field gnus-tmp-header 'From)))
981             gnus-tmp-from) ?s)
982     (?F gnus-tmp-from ?s)
983     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
984     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
985     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
986     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
987     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
988     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
989     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
990     (?L gnus-tmp-lines ?d)
991     (?I gnus-tmp-indentation ?s)
992     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
993     (?R gnus-tmp-replied ?c)
994     (?\[ gnus-tmp-opening-bracket ?c)
995     (?\] gnus-tmp-closing-bracket ?c)
996     (?\> (make-string gnus-tmp-level ? ) ?s)
997     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
998     (?i gnus-tmp-score ?d)
999     (?z gnus-tmp-score-char ?c)
1000     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1001     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1002     (?U gnus-tmp-unread ?c)
1003     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1004     (?t (gnus-summary-number-of-articles-in-thread
1005          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1006         ?d)
1007     (?e (gnus-summary-number-of-articles-in-thread
1008          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1009         ?c)
1010     (?u gnus-tmp-user-defined ?s)
1011     (?P (gnus-pick-line-number) ?d))
1012   "An alist of format specifications that can appear in summary lines,
1013 and what variables they correspond with, along with the type of the
1014 variable (string, integer, character, etc).")
1015
1016 (defvar gnus-summary-dummy-line-format-alist
1017   `((?S gnus-tmp-subject ?s)
1018     (?N gnus-tmp-number ?d)
1019     (?u gnus-tmp-user-defined ?s)))
1020
1021 (defvar gnus-summary-mode-line-format-alist
1022   `((?G gnus-tmp-group-name ?s)
1023     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1024     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1025     (?A gnus-tmp-article-number ?d)
1026     (?Z gnus-tmp-unread-and-unselected ?s)
1027     (?V gnus-version ?s)
1028     (?U gnus-tmp-unread-and-unticked ?d)
1029     (?S gnus-tmp-subject ?s)
1030     (?e gnus-tmp-unselected ?d)
1031     (?u gnus-tmp-user-defined ?s)
1032     (?d (length gnus-newsgroup-dormant) ?d)
1033     (?t (length gnus-newsgroup-marked) ?d)
1034     (?r (length gnus-newsgroup-reads) ?d)
1035     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1036     (?E gnus-newsgroup-expunged-tally ?d)
1037     (?s (gnus-current-score-file-nondirectory) ?s)))
1038
1039 (defvar gnus-last-search-regexp nil
1040   "Default regexp for article search command.")
1041
1042 (defvar gnus-summary-search-article-matched-data nil
1043   "Last matched data of article search command.  It is the local variable
1044 in `gnus-article-buffer' which consists of the list of start position,
1045 end position and text.")
1046
1047 (defvar gnus-last-shell-command nil
1048   "Default shell command on article.")
1049
1050 (defvar gnus-newsgroup-begin nil)
1051 (defvar gnus-newsgroup-end nil)
1052 (defvar gnus-newsgroup-last-rmail nil)
1053 (defvar gnus-newsgroup-last-mail nil)
1054 (defvar gnus-newsgroup-last-folder nil)
1055 (defvar gnus-newsgroup-last-file nil)
1056 (defvar gnus-newsgroup-auto-expire nil)
1057 (defvar gnus-newsgroup-active nil)
1058
1059 (defvar gnus-newsgroup-data nil)
1060 (defvar gnus-newsgroup-data-reverse nil)
1061 (defvar gnus-newsgroup-limit nil)
1062 (defvar gnus-newsgroup-limits nil)
1063
1064 (defvar gnus-newsgroup-unreads nil
1065   "List of unread articles in the current newsgroup.")
1066
1067 (defvar gnus-newsgroup-unselected nil
1068   "List of unselected unread articles in the current newsgroup.")
1069
1070 (defvar gnus-newsgroup-reads nil
1071   "Alist of read articles and article marks in the current newsgroup.")
1072
1073 (defvar gnus-newsgroup-expunged-tally nil)
1074
1075 (defvar gnus-newsgroup-marked nil
1076   "List of ticked articles in the current newsgroup (a subset of unread art).")
1077
1078 (defvar gnus-newsgroup-killed nil
1079   "List of ranges of articles that have been through the scoring process.")
1080
1081 (defvar gnus-newsgroup-cached nil
1082   "List of articles that come from the article cache.")
1083
1084 (defvar gnus-newsgroup-saved nil
1085   "List of articles that have been saved.")
1086
1087 (defvar gnus-newsgroup-kill-headers nil)
1088
1089 (defvar gnus-newsgroup-replied nil
1090   "List of articles that have been replied to in the current newsgroup.")
1091
1092 (defvar gnus-newsgroup-expirable nil
1093   "List of articles in the current newsgroup that can be expired.")
1094
1095 (defvar gnus-newsgroup-processable nil
1096   "List of articles in the current newsgroup that can be processed.")
1097
1098 (defvar gnus-newsgroup-downloadable nil
1099   "List of articles in the current newsgroup that can be processed.")
1100
1101 (defvar gnus-newsgroup-undownloaded nil
1102   "List of articles in the current newsgroup that haven't been downloaded..")
1103
1104 (defvar gnus-newsgroup-unsendable nil
1105   "List of articles in the current newsgroup that won't be sent.")
1106
1107 (defvar gnus-newsgroup-bookmarks nil
1108   "List of articles in the current newsgroup that have bookmarks.")
1109
1110 (defvar gnus-newsgroup-dormant nil
1111   "List of dormant articles in the current newsgroup.")
1112
1113 (defvar gnus-newsgroup-scored nil
1114   "List of scored articles in the current newsgroup.")
1115
1116 (defvar gnus-newsgroup-incorporated nil
1117   "List of incorporated articles in the current newsgroup.")
1118
1119 (defvar gnus-newsgroup-headers nil
1120   "List of article headers in the current newsgroup.")
1121
1122 (defvar gnus-newsgroup-threads nil)
1123
1124 (defvar gnus-newsgroup-prepared nil
1125   "Whether the current group has been prepared properly.")
1126
1127 (defvar gnus-newsgroup-ancient nil
1128   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1129
1130 (defvar gnus-newsgroup-sparse nil)
1131
1132 (defvar gnus-current-article nil)
1133 (defvar gnus-article-current nil)
1134 (defvar gnus-current-headers nil)
1135 (defvar gnus-have-all-headers nil)
1136 (defvar gnus-last-article nil)
1137 (defvar gnus-newsgroup-history nil)
1138 (defvar gnus-newsgroup-charset nil)
1139 (defvar gnus-newsgroup-ephemeral-charset nil)
1140 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1141
1142 (defconst gnus-summary-local-variables
1143   '(gnus-newsgroup-name
1144     gnus-newsgroup-begin gnus-newsgroup-end
1145     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1146     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1147     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1148     gnus-newsgroup-unselected gnus-newsgroup-marked
1149     gnus-newsgroup-reads gnus-newsgroup-saved
1150     gnus-newsgroup-replied gnus-newsgroup-expirable
1151     gnus-newsgroup-processable gnus-newsgroup-killed
1152     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1153     gnus-newsgroup-unsendable
1154     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1155     gnus-newsgroup-headers gnus-newsgroup-threads
1156     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1157     gnus-current-article gnus-current-headers gnus-have-all-headers
1158     gnus-last-article gnus-article-internal-prepare-hook
1159     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1160     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1161     gnus-thread-expunge-below
1162     gnus-score-alist gnus-current-score-file
1163     (gnus-summary-expunge-below . global)
1164     (gnus-summary-mark-below . global)
1165     (gnus-orphan-score . global)
1166     gnus-newsgroup-active gnus-scores-exclude-files
1167     gnus-newsgroup-history gnus-newsgroup-ancient
1168     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1169     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1170     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1171     (gnus-newsgroup-expunged-tally . 0)
1172     gnus-cache-removable-articles gnus-newsgroup-cached
1173     gnus-newsgroup-data gnus-newsgroup-data-reverse
1174     gnus-newsgroup-limit gnus-newsgroup-limits
1175     gnus-newsgroup-charset
1176     gnus-newsgroup-incorporated)
1177   "Variables that are buffer-local to the summary buffers.")
1178
1179 ;; Byte-compiler warning.
1180 (defvar gnus-article-mode-map)
1181
1182 ;; Subject simplification.
1183
1184 (defun gnus-simplify-whitespace (str)
1185   "Remove excessive whitespace."
1186   (let ((mystr str))
1187     ;; Multiple spaces.
1188     (while (string-match "[ \t][ \t]+" mystr)
1189       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1190                           " "
1191                           (substring mystr (match-end 0)))))
1192     ;; Leading spaces.
1193     (when (string-match "^[ \t]+" mystr)
1194       (setq mystr (substring mystr (match-end 0))))
1195     ;; Trailing spaces.
1196     (when (string-match "[ \t]+$" mystr)
1197       (setq mystr (substring mystr 0 (match-beginning 0))))
1198     mystr))
1199
1200 (defsubst gnus-simplify-subject-re (subject)
1201   "Remove \"Re:\" from subject lines."
1202   (if (string-match "^[Rr][Ee]: *" subject)
1203       (substring subject (match-end 0))
1204     subject))
1205
1206 (defun gnus-simplify-subject (subject &optional re-only)
1207   "Remove `Re:' and words in parentheses.
1208 If RE-ONLY is non-nil, strip leading `Re:'s only."
1209   (let ((case-fold-search t))           ;Ignore case.
1210     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1211     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1212       (setq subject (substring subject (match-end 0))))
1213     ;; Remove uninteresting prefixes.
1214     (when (and (not re-only)
1215                gnus-simplify-ignored-prefixes
1216                (string-match gnus-simplify-ignored-prefixes subject))
1217       (setq subject (substring subject (match-end 0))))
1218     ;; Remove words in parentheses from end.
1219     (unless re-only
1220       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1221         (setq subject (substring subject 0 (match-beginning 0)))))
1222     ;; Return subject string.
1223     subject))
1224
1225 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1226 ;; all whitespace.
1227 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1228   (goto-char (point-min))
1229   (while (re-search-forward regexp nil t)
1230     (replace-match (or newtext ""))))
1231
1232 (defun gnus-simplify-buffer-fuzzy ()
1233   "Simplify string in the buffer fuzzily.
1234 The string in the accessible portion of the current buffer is simplified.
1235 It is assumed to be a single-line subject.
1236 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1237 matter is removed.  Additional things can be deleted by setting
1238 gnus-simplify-subject-fuzzy-regexp."
1239   (let ((case-fold-search t)
1240         (modified-tick))
1241     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1242
1243     (while (not (eq modified-tick (buffer-modified-tick)))
1244       (setq modified-tick (buffer-modified-tick))
1245       (cond
1246        ((listp gnus-simplify-subject-fuzzy-regexp)
1247         (mapcar 'gnus-simplify-buffer-fuzzy-step
1248                 gnus-simplify-subject-fuzzy-regexp))
1249        (gnus-simplify-subject-fuzzy-regexp
1250         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1251       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1252       (gnus-simplify-buffer-fuzzy-step
1253        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1254       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1255
1256     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1257     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1258     (gnus-simplify-buffer-fuzzy-step " $")
1259     (gnus-simplify-buffer-fuzzy-step "^ +")))
1260
1261 (defun gnus-simplify-subject-fuzzy (subject)
1262   "Simplify a subject string fuzzily.
1263 See `gnus-simplify-buffer-fuzzy' for details."
1264   (save-excursion
1265     (gnus-set-work-buffer)
1266     (let ((case-fold-search t))
1267       ;; Remove uninteresting prefixes.
1268       (when (and gnus-simplify-ignored-prefixes
1269                  (string-match gnus-simplify-ignored-prefixes subject))
1270         (setq subject (substring subject (match-end 0))))
1271       (insert subject)
1272       (inline (gnus-simplify-buffer-fuzzy))
1273       (buffer-string))))
1274
1275 (defsubst gnus-simplify-subject-fully (subject)
1276   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1277   (cond
1278    (gnus-simplify-subject-functions
1279     (gnus-map-function gnus-simplify-subject-functions subject))
1280    ((null gnus-summary-gather-subject-limit)
1281     (gnus-simplify-subject-re subject))
1282    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1283     (gnus-simplify-subject-fuzzy subject))
1284    ((numberp gnus-summary-gather-subject-limit)
1285     (gnus-limit-string (gnus-simplify-subject-re subject)
1286                        gnus-summary-gather-subject-limit))
1287    (t
1288     subject)))
1289
1290 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1291   "Check whether two subjects are equal.
1292 If optional argument simple-first is t, first argument is already
1293 simplified."
1294   (cond
1295    ((null simple-first)
1296     (equal (gnus-simplify-subject-fully s1)
1297            (gnus-simplify-subject-fully s2)))
1298    (t
1299     (equal s1
1300            (gnus-simplify-subject-fully s2)))))
1301
1302 (defun gnus-summary-bubble-group ()
1303   "Increase the score of the current group.
1304 This is a handy function to add to `gnus-summary-exit-hook' to
1305 increase the score of each group you read."
1306   (gnus-group-add-score gnus-newsgroup-name))
1307
1308 \f
1309 ;;;
1310 ;;; Gnus summary mode
1311 ;;;
1312
1313 (put 'gnus-summary-mode 'mode-class 'special)
1314
1315 (when t
1316   ;; Non-orthogonal keys
1317
1318   (gnus-define-keys gnus-summary-mode-map
1319     " " gnus-summary-next-page
1320     "\177" gnus-summary-prev-page
1321     [delete] gnus-summary-prev-page
1322     [backspace] gnus-summary-prev-page
1323     "\r" gnus-summary-scroll-up
1324     "\M-\r" gnus-summary-scroll-down
1325     "n" gnus-summary-next-unread-article
1326     "p" gnus-summary-prev-unread-article
1327     "N" gnus-summary-next-article
1328     "P" gnus-summary-prev-article
1329     "\M-\C-n" gnus-summary-next-same-subject
1330     "\M-\C-p" gnus-summary-prev-same-subject
1331     "\M-n" gnus-summary-next-unread-subject
1332     "\M-p" gnus-summary-prev-unread-subject
1333     "." gnus-summary-first-unread-article
1334     "," gnus-summary-best-unread-article
1335     "\M-s" gnus-summary-search-article-forward
1336     "\M-r" gnus-summary-search-article-backward
1337     "<" gnus-summary-beginning-of-article
1338     ">" gnus-summary-end-of-article
1339     "j" gnus-summary-goto-article
1340     "^" gnus-summary-refer-parent-article
1341     "\M-^" gnus-summary-refer-article
1342     "u" gnus-summary-tick-article-forward
1343     "!" gnus-summary-tick-article-forward
1344     "U" gnus-summary-tick-article-backward
1345     "d" gnus-summary-mark-as-read-forward
1346     "D" gnus-summary-mark-as-read-backward
1347     "E" gnus-summary-mark-as-expirable
1348     "\M-u" gnus-summary-clear-mark-forward
1349     "\M-U" gnus-summary-clear-mark-backward
1350     "k" gnus-summary-kill-same-subject-and-select
1351     "\C-k" gnus-summary-kill-same-subject
1352     "\M-\C-k" gnus-summary-kill-thread
1353     "\M-\C-l" gnus-summary-lower-thread
1354     "e" gnus-summary-edit-article
1355     "#" gnus-summary-mark-as-processable
1356     "\M-#" gnus-summary-unmark-as-processable
1357     "\M-\C-t" gnus-summary-toggle-threads
1358     "\M-\C-s" gnus-summary-show-thread
1359     "\M-\C-h" gnus-summary-hide-thread
1360     "\M-\C-f" gnus-summary-next-thread
1361     "\M-\C-b" gnus-summary-prev-thread
1362     [(meta down)] gnus-summary-next-thread
1363     [(meta up)] gnus-summary-prev-thread
1364     "\M-\C-u" gnus-summary-up-thread
1365     "\M-\C-d" gnus-summary-down-thread
1366     "&" gnus-summary-execute-command
1367     "c" gnus-summary-catchup-and-exit
1368     "\C-w" gnus-summary-mark-region-as-read
1369     "\C-t" gnus-summary-toggle-truncation
1370     "?" gnus-summary-mark-as-dormant
1371     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1372     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1373     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1374     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1375     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1376     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1377     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1378     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1379     "=" gnus-summary-expand-window
1380     "\C-x\C-s" gnus-summary-reselect-current-group
1381     "\M-g" gnus-summary-rescan-group
1382     "w" gnus-summary-stop-page-breaking
1383     "\C-c\C-r" gnus-summary-caesar-message
1384     "\M-t" gnus-summary-toggle-mime
1385     "f" gnus-summary-followup
1386     "F" gnus-summary-followup-with-original
1387     "C" gnus-summary-cancel-article
1388     "r" gnus-summary-reply
1389     "R" gnus-summary-reply-with-original
1390     "\C-c\C-f" gnus-summary-mail-forward
1391     "o" gnus-summary-save-article
1392     "\C-o" gnus-summary-save-article-mail
1393     "|" gnus-summary-pipe-output
1394     "\M-k" gnus-summary-edit-local-kill
1395     "\M-K" gnus-summary-edit-global-kill
1396     ;; "V" gnus-version
1397     "\C-c\C-d" gnus-summary-describe-group
1398     "q" gnus-summary-exit
1399     "Q" gnus-summary-exit-no-update
1400     "\C-c\C-i" gnus-info-find-node
1401     gnus-mouse-2 gnus-mouse-pick-article
1402     "m" gnus-summary-mail-other-window
1403     "a" gnus-summary-post-news
1404     "x" gnus-summary-limit-to-unread
1405     "s" gnus-summary-isearch-article
1406     "t" gnus-article-toggle-headers
1407     "g" gnus-summary-show-article
1408     "l" gnus-summary-goto-last-article
1409     "v" gnus-summary-preview-mime-message
1410     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1411     "\C-d" gnus-summary-enter-digest-group
1412     "\M-\C-d" gnus-summary-read-document
1413     "\M-\C-e" gnus-summary-edit-parameters
1414     "\M-\C-a" gnus-summary-customize-parameters
1415     "\C-c\C-b" gnus-bug
1416     "*" gnus-cache-enter-article
1417     "\M-*" gnus-cache-remove-article
1418     "\M-&" gnus-summary-universal-argument
1419     "\C-l" gnus-recenter
1420     "I" gnus-summary-increase-score
1421     "L" gnus-summary-lower-score
1422     "\M-i" gnus-symbolic-argument
1423     "h" gnus-summary-select-article-buffer
1424
1425     "V" gnus-summary-score-map
1426     "X" gnus-uu-extract-map
1427     "S" gnus-summary-send-map)
1428
1429   ;; Sort of orthogonal keymap
1430   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1431     "t" gnus-summary-tick-article-forward
1432     "!" gnus-summary-tick-article-forward
1433     "d" gnus-summary-mark-as-read-forward
1434     "r" gnus-summary-mark-as-read-forward
1435     "c" gnus-summary-clear-mark-forward
1436     " " gnus-summary-clear-mark-forward
1437     "e" gnus-summary-mark-as-expirable
1438     "x" gnus-summary-mark-as-expirable
1439     "?" gnus-summary-mark-as-dormant
1440     "b" gnus-summary-set-bookmark
1441     "B" gnus-summary-remove-bookmark
1442     "#" gnus-summary-mark-as-processable
1443     "\M-#" gnus-summary-unmark-as-processable
1444     "S" gnus-summary-limit-include-expunged
1445     "C" gnus-summary-catchup
1446     "H" gnus-summary-catchup-to-here
1447     "\C-c" gnus-summary-catchup-all
1448     "k" gnus-summary-kill-same-subject-and-select
1449     "K" gnus-summary-kill-same-subject
1450     "P" gnus-uu-mark-map)
1451
1452   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1453     "c" gnus-summary-clear-above
1454     "u" gnus-summary-tick-above
1455     "m" gnus-summary-mark-above
1456     "k" gnus-summary-kill-below)
1457
1458   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1459     "/" gnus-summary-limit-to-subject
1460     "n" gnus-summary-limit-to-articles
1461     "w" gnus-summary-pop-limit
1462     "s" gnus-summary-limit-to-subject
1463     "a" gnus-summary-limit-to-author
1464     "u" gnus-summary-limit-to-unread
1465     "m" gnus-summary-limit-to-marks
1466     "M" gnus-summary-limit-exclude-marks
1467     "v" gnus-summary-limit-to-score
1468     "*" gnus-summary-limit-include-cached
1469     "D" gnus-summary-limit-include-dormant
1470     "T" gnus-summary-limit-include-thread
1471     "d" gnus-summary-limit-exclude-dormant
1472     "t" gnus-summary-limit-to-age
1473     "x" gnus-summary-limit-to-extra 
1474     "E" gnus-summary-limit-include-expunged
1475     "c" gnus-summary-limit-exclude-childless-dormant
1476     "C" gnus-summary-limit-mark-excluded-as-read)
1477
1478   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1479     "n" gnus-summary-next-unread-article
1480     "p" gnus-summary-prev-unread-article
1481     "N" gnus-summary-next-article
1482     "P" gnus-summary-prev-article
1483     "\C-n" gnus-summary-next-same-subject
1484     "\C-p" gnus-summary-prev-same-subject
1485     "\M-n" gnus-summary-next-unread-subject
1486     "\M-p" gnus-summary-prev-unread-subject
1487     "f" gnus-summary-first-unread-article
1488     "b" gnus-summary-best-unread-article
1489     "j" gnus-summary-goto-article
1490     "g" gnus-summary-goto-subject
1491     "l" gnus-summary-goto-last-article
1492     "o" gnus-summary-pop-article)
1493
1494   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1495     "k" gnus-summary-kill-thread
1496     "l" gnus-summary-lower-thread
1497     "i" gnus-summary-raise-thread
1498     "T" gnus-summary-toggle-threads
1499     "t" gnus-summary-rethread-current
1500     "^" gnus-summary-reparent-thread
1501     "s" gnus-summary-show-thread
1502     "S" gnus-summary-show-all-threads
1503     "h" gnus-summary-hide-thread
1504     "H" gnus-summary-hide-all-threads
1505     "n" gnus-summary-next-thread
1506     "p" gnus-summary-prev-thread
1507     "u" gnus-summary-up-thread
1508     "o" gnus-summary-top-thread
1509     "d" gnus-summary-down-thread
1510     "#" gnus-uu-mark-thread
1511     "\M-#" gnus-uu-unmark-thread)
1512
1513   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1514     "g" gnus-summary-prepare
1515     "c" gnus-summary-insert-cached-articles)
1516
1517   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1518     "c" gnus-summary-catchup-and-exit
1519     "C" gnus-summary-catchup-all-and-exit
1520     "E" gnus-summary-exit-no-update
1521     "J" gnus-summary-jump-to-other-group
1522     "Q" gnus-summary-exit
1523     "Z" gnus-summary-exit
1524     "n" gnus-summary-catchup-and-goto-next-group
1525     "R" gnus-summary-reselect-current-group
1526     "G" gnus-summary-rescan-group
1527     "N" gnus-summary-next-group
1528     "s" gnus-summary-save-newsrc
1529     "P" gnus-summary-prev-group)
1530
1531   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1532     " " gnus-summary-next-page
1533     "n" gnus-summary-next-page
1534     "\177" gnus-summary-prev-page
1535     [delete] gnus-summary-prev-page
1536     "p" gnus-summary-prev-page
1537     "\r" gnus-summary-scroll-up
1538     "\M-\r" gnus-summary-scroll-down
1539     "<" gnus-summary-beginning-of-article
1540     ">" gnus-summary-end-of-article
1541     "b" gnus-summary-beginning-of-article
1542     "e" gnus-summary-end-of-article
1543     "^" gnus-summary-refer-parent-article
1544     "r" gnus-summary-refer-parent-article
1545     "D" gnus-summary-enter-digest-group
1546     "R" gnus-summary-refer-references
1547     "T" gnus-summary-refer-thread
1548     "g" gnus-summary-show-article
1549     "s" gnus-summary-isearch-article
1550     "P" gnus-summary-print-article
1551     "t" gnus-article-babel
1552     "d" gnus-summary-decrypt-article
1553     "v" gnus-summary-verify-article)
1554
1555   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1556     "b" gnus-article-add-buttons
1557     "B" gnus-article-add-buttons-to-head
1558     "o" gnus-article-treat-overstrike
1559     "e" gnus-article-emphasize
1560     "w" gnus-article-fill-cited-article
1561     "Q" gnus-article-fill-long-lines
1562     "C" gnus-article-capitalize-sentences
1563     "c" gnus-article-remove-cr
1564     "Z" gnus-article-decode-HZ
1565     "f" gnus-article-display-x-face
1566     "l" gnus-summary-stop-page-breaking
1567     "r" gnus-summary-caesar-message
1568     "t" gnus-article-toggle-headers
1569     "v" gnus-summary-verbose-headers
1570     "m" gnus-summary-toggle-mime
1571     "H" gnus-article-strip-headers-in-body
1572     "d" gnus-article-treat-dumbquotes
1573     "s" gnus-smiley-display)
1574
1575   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1576     "a" gnus-article-hide
1577     "h" gnus-article-toggle-headers
1578     "b" gnus-article-hide-boring-headers
1579     "s" gnus-article-hide-signature
1580     "c" gnus-article-hide-citation
1581     "C" gnus-article-hide-citation-in-followups
1582     "l" gnus-article-hide-list-identifiers
1583     "p" gnus-article-hide-pgp
1584     "B" gnus-article-strip-banner
1585     "P" gnus-article-hide-pem
1586     "\C-c" gnus-article-hide-citation-maybe)
1587
1588   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1589     "a" gnus-article-highlight
1590     "h" gnus-article-highlight-headers
1591     "c" gnus-article-highlight-citation
1592     "s" gnus-article-highlight-signature)
1593
1594   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1595     "z" gnus-article-date-ut
1596     "u" gnus-article-date-ut
1597     "l" gnus-article-date-local
1598     "e" gnus-article-date-lapsed
1599     "o" gnus-article-date-original
1600     "i" gnus-article-date-iso8601
1601     "s" gnus-article-date-user)
1602
1603   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1604     "t" gnus-article-remove-trailing-blank-lines
1605     "l" gnus-article-strip-leading-blank-lines
1606     "m" gnus-article-strip-multiple-blank-lines
1607     "a" gnus-article-strip-blank-lines
1608     "A" gnus-article-strip-all-blank-lines
1609     "s" gnus-article-strip-leading-space
1610     "e" gnus-article-strip-trailing-space)
1611
1612   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1613     "v" gnus-version
1614     "f" gnus-summary-fetch-faq
1615     "d" gnus-summary-describe-group
1616     "h" gnus-summary-describe-briefly
1617     "i" gnus-info-find-node)
1618
1619   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1620     "e" gnus-summary-expire-articles
1621     "\M-\C-e" gnus-summary-expire-articles-now
1622     "\177" gnus-summary-delete-article
1623     [delete] gnus-summary-delete-article
1624     [backspace] gnus-summary-delete-article
1625     "m" gnus-summary-move-article
1626     "r" gnus-summary-respool-article
1627     "w" gnus-summary-edit-article
1628     "c" gnus-summary-copy-article
1629     "B" gnus-summary-crosspost-article
1630     "q" gnus-summary-respool-query
1631     "t" gnus-summary-respool-trace
1632     "i" gnus-summary-import-article
1633     "p" gnus-summary-article-posted-p)
1634
1635   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1636     "o" gnus-summary-save-article
1637     "m" gnus-summary-save-article-mail
1638     "F" gnus-summary-write-article-file
1639     "r" gnus-summary-save-article-rmail
1640     "f" gnus-summary-save-article-file
1641     "b" gnus-summary-save-article-body-file
1642     "h" gnus-summary-save-article-folder
1643     "v" gnus-summary-save-article-vm
1644     "p" gnus-summary-pipe-output
1645     "s" gnus-soup-add-article)
1646
1647   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1648     "b" gnus-summary-display-buttonized
1649     "m" gnus-summary-repair-multipart
1650     "v" gnus-article-view-part
1651     "o" gnus-article-save-part
1652     "c" gnus-article-copy-part
1653     "e" gnus-article-externalize-part
1654     "i" gnus-article-inline-part
1655     "|" gnus-article-pipe-part))
1656
1657 (defun gnus-summary-make-menu-bar ()
1658   (gnus-turn-off-edit-menu 'summary)
1659
1660   (unless (boundp 'gnus-summary-misc-menu)
1661
1662     (easy-menu-define
1663      gnus-summary-kill-menu gnus-summary-mode-map ""
1664      (cons
1665       "Score"
1666       (nconc
1667        (list
1668         ["Enter score..." gnus-summary-score-entry t]
1669         ["Customize" gnus-score-customize t])
1670        (gnus-make-score-map 'increase)
1671        (gnus-make-score-map 'lower)
1672        '(("Mark"
1673           ["Kill below" gnus-summary-kill-below t]
1674           ["Mark above" gnus-summary-mark-above t]
1675           ["Tick above" gnus-summary-tick-above t]
1676           ["Clear above" gnus-summary-clear-above t])
1677          ["Current score" gnus-summary-current-score t]
1678          ["Set score" gnus-summary-set-score t]
1679          ["Switch current score file..." gnus-score-change-score-file t]
1680          ["Set mark below..." gnus-score-set-mark-below t]
1681          ["Set expunge below..." gnus-score-set-expunge-below t]
1682          ["Edit current score file" gnus-score-edit-current-scores t]
1683          ["Edit score file" gnus-score-edit-file t]
1684          ["Trace score" gnus-score-find-trace t]
1685          ["Find words" gnus-score-find-favourite-words t]
1686          ["Rescore buffer" gnus-summary-rescore t]
1687          ["Increase score..." gnus-summary-increase-score t]
1688          ["Lower score..." gnus-summary-lower-score t]))))
1689
1690     ;; Define both the Article menu in the summary buffer and the equivalent
1691     ;; Commands menu in the article buffer here for consistency.
1692     (let ((innards
1693            '(("Hide"
1694               ["All" gnus-article-hide t]
1695               ["Headers" gnus-article-toggle-headers t]
1696               ["Signature" gnus-article-hide-signature t]
1697               ["Citation" gnus-article-hide-citation t]
1698               ["List identifiers" gnus-article-hide-list-identifiers t]
1699               ["PGP" gnus-article-hide-pgp t]
1700               ["Banner" gnus-article-strip-banner t]
1701               ["Boring headers" gnus-article-hide-boring-headers t])
1702              ("Highlight"
1703               ["All" gnus-article-highlight t]
1704               ["Headers" gnus-article-highlight-headers t]
1705               ["Signature" gnus-article-highlight-signature t]
1706               ["Citation" gnus-article-highlight-citation t])
1707              ("Date"
1708               ["Local" gnus-article-date-local t]
1709               ["ISO8601" gnus-article-date-iso8601 t]
1710               ["UT" gnus-article-date-ut t]
1711               ["Original" gnus-article-date-original t]
1712               ["Lapsed" gnus-article-date-lapsed t]
1713               ["User-defined" gnus-article-date-user t])
1714              ("Washing"
1715               ("Remove Blanks"
1716                ["Leading" gnus-article-strip-leading-blank-lines t]
1717                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1718                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1719                ["All of the above" gnus-article-strip-blank-lines t]
1720                ["All" gnus-article-strip-all-blank-lines t]
1721                ["Leading space" gnus-article-strip-leading-space t]
1722                ["Trailing space" gnus-article-strip-trailing-space t])
1723               ["Overstrike" gnus-article-treat-overstrike t]
1724               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1725               ["Emphasis" gnus-article-emphasize t]
1726               ["Word wrap" gnus-article-fill-cited-article t]
1727               ["Fill long lines" gnus-article-fill-long-lines t]
1728               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1729               ["CR" gnus-article-remove-cr t]
1730               ["Show X-Face" gnus-article-display-x-face t]
1731               ["Rot 13" gnus-summary-caesar-message t]
1732               ["Unix pipe" gnus-summary-pipe-message t]
1733               ["Add buttons" gnus-article-add-buttons t]
1734               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1735               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1736               ["Toggle MIME" gnus-summary-toggle-mime t]
1737               ["Verbose header" gnus-summary-verbose-headers t]
1738               ["Toggle header" gnus-summary-toggle-header t]
1739               ["Toggle smileys" gnus-smiley-display t]
1740               ["HZ" gnus-article-decode-HZ t])
1741              ("Output"
1742               ["Save in default format" gnus-summary-save-article t]
1743               ["Save in file" gnus-summary-save-article-file t]
1744               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1745               ["Save in MH folder" gnus-summary-save-article-folder t]
1746               ["Save in VM folder" gnus-summary-save-article-vm t]
1747               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1748               ["Save body in file" gnus-summary-save-article-body-file t]
1749               ["Pipe through a filter" gnus-summary-pipe-output t]
1750               ["Add to SOUP packet" gnus-soup-add-article t]
1751               ["Print" gnus-summary-print-article t])
1752              ("Backend"
1753               ["Respool article..." gnus-summary-respool-article t]
1754               ["Move article..." gnus-summary-move-article
1755                (gnus-check-backend-function
1756                 'request-move-article gnus-newsgroup-name)]
1757               ["Copy article..." gnus-summary-copy-article t]
1758               ["Crosspost article..." gnus-summary-crosspost-article
1759                (gnus-check-backend-function
1760                 'request-replace-article gnus-newsgroup-name)]
1761               ["Import file..." gnus-summary-import-article t]
1762               ["Check if posted" gnus-summary-article-posted-p t]
1763               ["Edit article" gnus-summary-edit-article
1764                (not (gnus-group-read-only-p))]
1765               ["Delete article" gnus-summary-delete-article
1766                (gnus-check-backend-function
1767                 'request-expire-articles gnus-newsgroup-name)]
1768               ["Query respool" gnus-summary-respool-query t]
1769               ["Trace respool" gnus-summary-respool-trace t]
1770               ["Delete expirable articles" gnus-summary-expire-articles-now
1771                (gnus-check-backend-function
1772                 'request-expire-articles gnus-newsgroup-name)])
1773              ("Extract"
1774               ["Uudecode" gnus-uu-decode-uu t]
1775               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1776               ["Unshar" gnus-uu-decode-unshar t]
1777               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1778               ["Save" gnus-uu-decode-save t]
1779               ["Binhex" gnus-uu-decode-binhex t]
1780               ["Postscript" gnus-uu-decode-postscript t])
1781              ("Cache"
1782               ["Enter article" gnus-cache-enter-article t]
1783               ["Remove article" gnus-cache-remove-article t])
1784              ["Translate" gnus-article-babel t]
1785              ["Select article buffer" gnus-summary-select-article-buffer t]
1786              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1787              ["Isearch article..." gnus-summary-isearch-article t]
1788              ["Beginning of the article" gnus-summary-beginning-of-article t]
1789              ["End of the article" gnus-summary-end-of-article t]
1790              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1791              ["Fetch referenced articles" gnus-summary-refer-references t]
1792              ["Fetch current thread" gnus-summary-refer-thread t]
1793              ["Fetch article with id..." gnus-summary-refer-article t]
1794              ["Redisplay" gnus-summary-show-article t])))
1795       (easy-menu-define
1796        gnus-summary-article-menu gnus-summary-mode-map ""
1797        (cons "Article" innards))
1798
1799       (easy-menu-define
1800        gnus-article-commands-menu gnus-article-mode-map ""
1801        (cons "Commands" innards)))
1802
1803     (easy-menu-define
1804      gnus-summary-thread-menu gnus-summary-mode-map ""
1805      '("Threads"
1806        ["Toggle threading" gnus-summary-toggle-threads t]
1807        ["Hide threads" gnus-summary-hide-all-threads t]
1808        ["Show threads" gnus-summary-show-all-threads t]
1809        ["Hide thread" gnus-summary-hide-thread t]
1810        ["Show thread" gnus-summary-show-thread t]
1811        ["Go to next thread" gnus-summary-next-thread t]
1812        ["Go to previous thread" gnus-summary-prev-thread t]
1813        ["Go down thread" gnus-summary-down-thread t]
1814        ["Go up thread" gnus-summary-up-thread t]
1815        ["Top of thread" gnus-summary-top-thread t]
1816        ["Mark thread as read" gnus-summary-kill-thread t]
1817        ["Lower thread score" gnus-summary-lower-thread t]
1818        ["Raise thread score" gnus-summary-raise-thread t]
1819        ["Rethread current" gnus-summary-rethread-current t]))
1820
1821     (easy-menu-define
1822      gnus-summary-post-menu gnus-summary-mode-map ""
1823      '("Post"
1824        ["Post an article" gnus-summary-post-news t]
1825        ["Followup" gnus-summary-followup t]
1826        ["Followup and yank" gnus-summary-followup-with-original t]
1827        ["Supersede article" gnus-summary-supersede-article t]
1828        ["Cancel article" gnus-summary-cancel-article t]
1829        ["Reply" gnus-summary-reply t]
1830        ["Reply and yank" gnus-summary-reply-with-original t]
1831        ["Wide reply" gnus-summary-wide-reply t]
1832        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1833        ["Mail forward" gnus-summary-mail-forward t]
1834        ["Post forward" gnus-summary-post-forward t]
1835        ["Digest and mail" gnus-uu-digest-mail-forward t]
1836        ["Digest and post" gnus-uu-digest-post-forward t]
1837        ["Resend message" gnus-summary-resend-message t]
1838        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1839        ["Send a mail" gnus-summary-mail-other-window t]
1840        ["Uuencode and post" gnus-uu-post-news t]
1841        ["Followup via news" gnus-summary-followup-to-mail t]
1842        ["Followup via news and yank"
1843         gnus-summary-followup-to-mail-with-original t]
1844        ;;("Draft"
1845        ;;["Send" gnus-summary-send-draft t]
1846        ;;["Send bounced" gnus-resend-bounced-mail t])
1847        ))
1848
1849     (easy-menu-define
1850      gnus-summary-misc-menu gnus-summary-mode-map ""
1851      '("Misc"
1852        ("Mark Read"
1853         ["Mark as read" gnus-summary-mark-as-read-forward t]
1854         ["Mark same subject and select"
1855          gnus-summary-kill-same-subject-and-select t]
1856         ["Mark same subject" gnus-summary-kill-same-subject t]
1857         ["Catchup" gnus-summary-catchup t]
1858         ["Catchup all" gnus-summary-catchup-all t]
1859         ["Catchup to here" gnus-summary-catchup-to-here t]
1860         ["Catchup region" gnus-summary-mark-region-as-read t]
1861         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1862        ("Mark Various"
1863         ["Tick" gnus-summary-tick-article-forward t]
1864         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1865         ["Remove marks" gnus-summary-clear-mark-forward t]
1866         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1867         ["Set bookmark" gnus-summary-set-bookmark t]
1868         ["Remove bookmark" gnus-summary-remove-bookmark t])
1869        ("Mark Limit"
1870         ["Marks..." gnus-summary-limit-to-marks t]
1871         ["Subject..." gnus-summary-limit-to-subject t]
1872         ["Author..." gnus-summary-limit-to-author t]
1873         ["Age..." gnus-summary-limit-to-age t]
1874         ["Extra..." gnus-summary-limit-to-extra t]
1875         ["Score" gnus-summary-limit-to-score t]
1876         ["Unread" gnus-summary-limit-to-unread t]
1877         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1878         ["Articles" gnus-summary-limit-to-articles t]
1879         ["Pop limit" gnus-summary-pop-limit t]
1880         ["Show dormant" gnus-summary-limit-include-dormant t]
1881         ["Hide childless dormant"
1882          gnus-summary-limit-exclude-childless-dormant t]
1883         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1884         ["Hide marked" gnus-summary-limit-exclude-marks t]
1885         ["Show expunged" gnus-summary-show-all-expunged t])
1886        ("Process Mark"
1887         ["Set mark" gnus-summary-mark-as-processable t]
1888         ["Remove mark" gnus-summary-unmark-as-processable t]
1889         ["Remove all marks" gnus-summary-unmark-all-processable t]
1890         ["Mark above" gnus-uu-mark-over t]
1891         ["Mark series" gnus-uu-mark-series t]
1892         ["Mark region" gnus-uu-mark-region t]
1893         ["Unmark region" gnus-uu-unmark-region t]
1894         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1895         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1896         ["Mark all" gnus-uu-mark-all t]
1897         ["Mark buffer" gnus-uu-mark-buffer t]
1898         ["Mark sparse" gnus-uu-mark-sparse t]
1899         ["Mark thread" gnus-uu-mark-thread t]
1900         ["Unmark thread" gnus-uu-unmark-thread t]
1901         ("Process Mark Sets"
1902          ["Kill" gnus-summary-kill-process-mark t]
1903          ["Yank" gnus-summary-yank-process-mark
1904           gnus-newsgroup-process-stack]
1905          ["Save" gnus-summary-save-process-mark t]))
1906        ("Scroll article"
1907         ["Page forward" gnus-summary-next-page t]
1908         ["Page backward" gnus-summary-prev-page t]
1909         ["Line forward" gnus-summary-scroll-up t])
1910        ("Move"
1911         ["Next unread article" gnus-summary-next-unread-article t]
1912         ["Previous unread article" gnus-summary-prev-unread-article t]
1913         ["Next article" gnus-summary-next-article t]
1914         ["Previous article" gnus-summary-prev-article t]
1915         ["Next unread subject" gnus-summary-next-unread-subject t]
1916         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1917         ["Next article same subject" gnus-summary-next-same-subject t]
1918         ["Previous article same subject" gnus-summary-prev-same-subject t]
1919         ["First unread article" gnus-summary-first-unread-article t]
1920         ["Best unread article" gnus-summary-best-unread-article t]
1921         ["Go to subject number..." gnus-summary-goto-subject t]
1922         ["Go to article number..." gnus-summary-goto-article t]
1923         ["Go to the last article" gnus-summary-goto-last-article t]
1924         ["Pop article off history" gnus-summary-pop-article t])
1925        ("Sort"
1926         ["Sort by number" gnus-summary-sort-by-number t]
1927         ["Sort by author" gnus-summary-sort-by-author t]
1928         ["Sort by subject" gnus-summary-sort-by-subject t]
1929         ["Sort by date" gnus-summary-sort-by-date t]
1930         ["Sort by score" gnus-summary-sort-by-score t]
1931         ["Sort by lines" gnus-summary-sort-by-lines t]
1932         ["Sort by characters" gnus-summary-sort-by-chars t])
1933        ("Help"
1934         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1935         ["Describe group" gnus-summary-describe-group t]
1936         ["Read manual" gnus-info-find-node t])
1937        ("Modes"
1938         ["Pick and read" gnus-pick-mode t]
1939         ["Binary" gnus-binary-mode t])
1940        ("Regeneration"
1941         ["Regenerate" gnus-summary-prepare t]
1942         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1943         ["Toggle threading" gnus-summary-toggle-threads t])
1944        ["Filter articles..." gnus-summary-execute-command t]
1945        ["Run command on subjects..." gnus-summary-universal-argument t]
1946        ["Search articles forward..." gnus-summary-search-article-forward t]
1947        ["Search articles backward..." gnus-summary-search-article-backward t]
1948        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1949        ["Expand window" gnus-summary-expand-window t]
1950        ["Expire expirable articles" gnus-summary-expire-articles
1951         (gnus-check-backend-function
1952          'request-expire-articles gnus-newsgroup-name)]
1953        ["Edit local kill file" gnus-summary-edit-local-kill t]
1954        ["Edit main kill file" gnus-summary-edit-global-kill t]
1955        ["Edit group parameters" gnus-summary-edit-parameters t]
1956        ["Customize group parameters" gnus-summary-customize-parameters t]
1957        ["Send a bug report" gnus-bug t]
1958        ("Exit"
1959         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1960         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1961         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1962         ["Exit group" gnus-summary-exit t]
1963         ["Exit group without updating" gnus-summary-exit-no-update t]
1964         ["Exit and goto next group" gnus-summary-next-group t]
1965         ["Exit and goto prev group" gnus-summary-prev-group t]
1966         ["Reselect group" gnus-summary-reselect-current-group t]
1967         ["Rescan group" gnus-summary-rescan-group t]
1968         ["Update dribble" gnus-summary-save-newsrc t])))
1969
1970     (gnus-run-hooks 'gnus-summary-menu-hook)))
1971
1972 (defun gnus-score-set-default (var value)
1973   "A version of set that updates the GNU Emacs menu-bar."
1974   (set var value)
1975   ;; It is the message that forces the active status to be updated.
1976   (message ""))
1977
1978 (defun gnus-make-score-map (type)
1979   "Make a summary score map of type TYPE."
1980   (if t
1981       nil
1982     (let ((headers '(("author" "from" string)
1983                      ("subject" "subject" string)
1984                      ("article body" "body" string)
1985                      ("article head" "head" string)
1986                      ("xref" "xref" string)
1987                      ("extra header" "extra" string)
1988                      ("lines" "lines" number)
1989                      ("followups to author" "followup" string)))
1990           (types '((number ("less than" <)
1991                            ("greater than" >)
1992                            ("equal" =))
1993                    (string ("substring" s)
1994                            ("exact string" e)
1995                            ("fuzzy string" f)
1996                            ("regexp" r))))
1997           (perms '(("temporary" (current-time-string))
1998                    ("permanent" nil)
1999                    ("immediate" now)))
2000           header)
2001       (list
2002        (apply
2003         'nconc
2004         (list
2005          (if (eq type 'lower)
2006              "Lower score"
2007            "Increase score"))
2008         (let (outh)
2009           (while headers
2010             (setq header (car headers))
2011             (setq outh
2012                   (cons
2013                    (apply
2014                     'nconc
2015                     (list (car header))
2016                     (let ((ts (cdr (assoc (nth 2 header) types)))
2017                           outt)
2018                       (while ts
2019                         (setq outt
2020                               (cons
2021                                (apply
2022                                 'nconc
2023                                 (list (caar ts))
2024                                 (let ((ps perms)
2025                                       outp)
2026                                   (while ps
2027                                     (setq outp
2028                                           (cons
2029                                            (vector
2030                                             (caar ps)
2031                                             (list
2032                                              'gnus-summary-score-entry
2033                                              (nth 1 header)
2034                                              (if (or (string= (nth 1 header)
2035                                                               "head")
2036                                                      (string= (nth 1 header)
2037                                                               "body"))
2038                                                  ""
2039                                                (list 'gnus-summary-header
2040                                                      (nth 1 header)))
2041                                              (list 'quote (nth 1 (car ts)))
2042                                              (list 'gnus-score-delta-default
2043                                                    nil)
2044                                              (nth 1 (car ps))
2045                                              t)
2046                                             t)
2047                                            outp))
2048                                     (setq ps (cdr ps)))
2049                                   (list (nreverse outp))))
2050                                outt))
2051                         (setq ts (cdr ts)))
2052                       (list (nreverse outt))))
2053                    outh))
2054             (setq headers (cdr headers)))
2055           (list (nreverse outh))))))))
2056
2057 \f
2058
2059 (defun gnus-summary-mode (&optional group)
2060   "Major mode for reading articles.
2061
2062 All normal editing commands are switched off.
2063 \\<gnus-summary-mode-map>
2064 Each line in this buffer represents one article.  To read an
2065 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2066 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2067 respectively.
2068
2069 You can also post articles and send mail from this buffer.  To
2070 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2071 of an article, type `\\[gnus-summary-reply]'.
2072
2073 There are approx. one gazillion commands you can execute in this
2074 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2075
2076 The following commands are available:
2077
2078 \\{gnus-summary-mode-map}"
2079   (interactive)
2080   (when (gnus-visual-p 'summary-menu 'menu)
2081     (gnus-summary-make-menu-bar))
2082   (kill-all-local-variables)
2083   (gnus-summary-make-local-variables)
2084   (gnus-make-thread-indent-array)
2085   (gnus-simplify-mode-line)
2086   (setq major-mode 'gnus-summary-mode)
2087   (setq mode-name "Summary")
2088   (make-local-variable 'minor-mode-alist)
2089   (use-local-map gnus-summary-mode-map)
2090   (buffer-disable-undo)
2091   (setq buffer-read-only t)             ;Disable modification
2092   (setq truncate-lines t)
2093   (setq selective-display t)
2094   (setq selective-display-ellipses t)   ;Display `...'
2095   (gnus-summary-set-display-table)
2096   (gnus-set-default-directory)
2097   (setq gnus-newsgroup-name group)
2098   (unless (gnus-news-group-p group)
2099     (setq gnus-newsgroup-incorporated
2100           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2101   (make-local-variable 'gnus-summary-line-format)
2102   (make-local-variable 'gnus-summary-line-format-spec)
2103   (make-local-variable 'gnus-summary-dummy-line-format)
2104   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2105   (make-local-variable 'gnus-summary-mark-positions)
2106   (make-local-hook 'pre-command-hook)
2107   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2108   (gnus-run-hooks 'gnus-summary-mode-hook)
2109   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2110   (gnus-update-summary-mark-positions))
2111
2112 (defun gnus-summary-make-local-variables ()
2113   "Make all the local summary buffer variables."
2114   (let (global)
2115     (dolist (local gnus-summary-local-variables)
2116       (if (consp local)
2117           (progn
2118             (if (eq (cdr local) 'global)
2119                 ;; Copy the global value of the variable.
2120                 (setq global (symbol-value (car local)))
2121               ;; Use the value from the list.
2122               (setq global (eval (cdr local))))
2123             (set (make-local-variable (car local)) global))
2124         ;; Simple nil-valued local variable.
2125         (set (make-local-variable local) nil)))))
2126
2127 (defun gnus-summary-clear-local-variables ()
2128   (let ((locals gnus-summary-local-variables))
2129     (while locals
2130       (if (consp (car locals))
2131           (and (vectorp (caar locals))
2132                (set (caar locals) nil))
2133         (and (vectorp (car locals))
2134              (set (car locals) nil)))
2135       (setq locals (cdr locals)))))
2136
2137 ;; Summary data functions.
2138
2139 (defmacro gnus-data-number (data)
2140   `(car ,data))
2141
2142 (defmacro gnus-data-set-number (data number)
2143   `(setcar ,data ,number))
2144
2145 (defmacro gnus-data-mark (data)
2146   `(nth 1 ,data))
2147
2148 (defmacro gnus-data-set-mark (data mark)
2149   `(setcar (nthcdr 1 ,data) ,mark))
2150
2151 (defmacro gnus-data-pos (data)
2152   `(nth 2 ,data))
2153
2154 (defmacro gnus-data-set-pos (data pos)
2155   `(setcar (nthcdr 2 ,data) ,pos))
2156
2157 (defmacro gnus-data-header (data)
2158   `(nth 3 ,data))
2159
2160 (defmacro gnus-data-set-header (data header)
2161   `(setcar (nthcdr 3 ,data) ,header))
2162
2163 (defmacro gnus-data-level (data)
2164   `(nth 4 ,data))
2165
2166 (defmacro gnus-data-unread-p (data)
2167   `(= (nth 1 ,data) gnus-unread-mark))
2168
2169 (defmacro gnus-data-read-p (data)
2170   `(/= (nth 1 ,data) gnus-unread-mark))
2171
2172 (defmacro gnus-data-pseudo-p (data)
2173   `(consp (nth 3 ,data)))
2174
2175 (defmacro gnus-data-find (number)
2176   `(assq ,number gnus-newsgroup-data))
2177
2178 (defmacro gnus-data-find-list (number &optional data)
2179   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2180      (memq (assq ,number bdata)
2181            bdata)))
2182
2183 (defmacro gnus-data-make (number mark pos header level)
2184   `(list ,number ,mark ,pos ,header ,level))
2185
2186 (defun gnus-data-enter (after-article number mark pos header level offset)
2187   (let ((data (gnus-data-find-list after-article)))
2188     (unless data
2189       (error "No such article: %d" after-article))
2190     (setcdr data (cons (gnus-data-make number mark pos header level)
2191                        (cdr data)))
2192     (setq gnus-newsgroup-data-reverse nil)
2193     (gnus-data-update-list (cddr data) offset)))
2194
2195 (defun gnus-data-enter-list (after-article list &optional offset)
2196   (when list
2197     (let ((data (and after-article (gnus-data-find-list after-article)))
2198           (ilist list))
2199       (if (not (or data
2200                    after-article))
2201           (let ((odata gnus-newsgroup-data))
2202             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2203             (when offset
2204               (gnus-data-update-list odata offset)))
2205         ;; Find the last element in the list to be spliced into the main
2206         ;; list.
2207         (while (cdr list)
2208           (setq list (cdr list)))
2209         (if (not data)
2210             (progn
2211               (setcdr list gnus-newsgroup-data)
2212               (setq gnus-newsgroup-data ilist)
2213               (when offset
2214                 (gnus-data-update-list (cdr list) offset)))
2215           (setcdr list (cdr data))
2216           (setcdr data ilist)
2217           (when offset
2218             (gnus-data-update-list (cdr list) offset))))
2219       (setq gnus-newsgroup-data-reverse nil))))
2220
2221 (defun gnus-data-remove (article &optional offset)
2222   (let ((data gnus-newsgroup-data))
2223     (if (= (gnus-data-number (car data)) article)
2224         (progn
2225           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2226                 gnus-newsgroup-data-reverse nil)
2227           (when offset
2228             (gnus-data-update-list gnus-newsgroup-data offset)))
2229       (while (cdr data)
2230         (when (= (gnus-data-number (cadr data)) article)
2231           (setcdr data (cddr data))
2232           (when offset
2233             (gnus-data-update-list (cdr data) offset))
2234           (setq data nil
2235                 gnus-newsgroup-data-reverse nil))
2236         (setq data (cdr data))))))
2237
2238 (defmacro gnus-data-list (backward)
2239   `(if ,backward
2240        (or gnus-newsgroup-data-reverse
2241            (setq gnus-newsgroup-data-reverse
2242                  (reverse gnus-newsgroup-data)))
2243      gnus-newsgroup-data))
2244
2245 (defun gnus-data-update-list (data offset)
2246   "Add OFFSET to the POS of all data entries in DATA."
2247   (setq gnus-newsgroup-data-reverse nil)
2248   (while data
2249     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2250     (setq data (cdr data))))
2251
2252 (defun gnus-summary-article-pseudo-p (article)
2253   "Say whether this article is a pseudo article or not."
2254   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2255
2256 (defmacro gnus-summary-article-sparse-p (article)
2257   "Say whether this article is a sparse article or not."
2258   `(memq ,article gnus-newsgroup-sparse))
2259
2260 (defmacro gnus-summary-article-ancient-p (article)
2261   "Say whether this article is a sparse article or not."
2262   `(memq ,article gnus-newsgroup-ancient))
2263
2264 (defun gnus-article-parent-p (number)
2265   "Say whether this article is a parent or not."
2266   (let ((data (gnus-data-find-list number)))
2267     (and (cdr data)                     ; There has to be an article after...
2268          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2269             (gnus-data-level (nth 1 data))))))
2270
2271 (defun gnus-article-children (number)
2272   "Return a list of all children to NUMBER."
2273   (let* ((data (gnus-data-find-list number))
2274          (level (gnus-data-level (car data)))
2275          children)
2276     (setq data (cdr data))
2277     (while (and data
2278                 (= (gnus-data-level (car data)) (1+ level)))
2279       (push (gnus-data-number (car data)) children)
2280       (setq data (cdr data)))
2281     children))
2282
2283 (defmacro gnus-summary-skip-intangible ()
2284   "If the current article is intangible, then jump to a different article."
2285   '(let ((to (get-text-property (point) 'gnus-intangible)))
2286      (and to (gnus-summary-goto-subject to))))
2287
2288 (defmacro gnus-summary-article-intangible-p ()
2289   "Say whether this article is intangible or not."
2290   '(get-text-property (point) 'gnus-intangible))
2291
2292 (defun gnus-article-read-p (article)
2293   "Say whether ARTICLE is read or not."
2294   (not (or (memq article gnus-newsgroup-marked)
2295            (memq article gnus-newsgroup-unreads)
2296            (memq article gnus-newsgroup-unselected)
2297            (memq article gnus-newsgroup-dormant))))
2298
2299 ;; Some summary mode macros.
2300
2301 (defmacro gnus-summary-article-number ()
2302   "The article number of the article on the current line.
2303 If there isn's an article number here, then we return the current
2304 article number."
2305   '(progn
2306      (gnus-summary-skip-intangible)
2307      (or (get-text-property (point) 'gnus-number)
2308          (gnus-summary-last-subject))))
2309
2310 (defmacro gnus-summary-article-header (&optional number)
2311   "Return the header of article NUMBER."
2312   `(gnus-data-header (gnus-data-find
2313                       ,(or number '(gnus-summary-article-number)))))
2314
2315 (defmacro gnus-summary-thread-level (&optional number)
2316   "Return the level of thread that starts with article NUMBER."
2317   `(if (and (eq gnus-summary-make-false-root 'dummy)
2318             (get-text-property (point) 'gnus-intangible))
2319        0
2320      (gnus-data-level (gnus-data-find
2321                        ,(or number '(gnus-summary-article-number))))))
2322
2323 (defmacro gnus-summary-article-mark (&optional number)
2324   "Return the mark of article NUMBER."
2325   `(gnus-data-mark (gnus-data-find
2326                     ,(or number '(gnus-summary-article-number)))))
2327
2328 (defmacro gnus-summary-article-pos (&optional number)
2329   "Return the position of the line of article NUMBER."
2330   `(gnus-data-pos (gnus-data-find
2331                    ,(or number '(gnus-summary-article-number)))))
2332
2333 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2334 (defmacro gnus-summary-article-subject (&optional number)
2335   "Return current subject string or nil if nothing."
2336   `(let ((headers
2337           ,(if number
2338                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2339              '(gnus-data-header (assq (gnus-summary-article-number)
2340                                       gnus-newsgroup-data)))))
2341      (and headers
2342           (vectorp headers)
2343           (mail-header-subject headers))))
2344
2345 (defmacro gnus-summary-article-score (&optional number)
2346   "Return current article score."
2347   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2348                   gnus-newsgroup-scored))
2349        gnus-summary-default-score 0))
2350
2351 (defun gnus-summary-article-children (&optional number)
2352   "Return a list of article numbers that are children of article NUMBER."
2353   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2354          (level (gnus-data-level (car data)))
2355          l children)
2356     (while (and (setq data (cdr data))
2357                 (> (setq l (gnus-data-level (car data))) level))
2358       (and (= (1+ level) l)
2359            (push (gnus-data-number (car data))
2360                  children)))
2361     (nreverse children)))
2362
2363 (defun gnus-summary-article-parent (&optional number)
2364   "Return the article number of the parent of article NUMBER."
2365   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2366                                     (gnus-data-list t)))
2367          (level (gnus-data-level (car data))))
2368     (if (zerop level)
2369         ()                              ; This is a root.
2370       ;; We search until we find an article with a level less than
2371       ;; this one.  That function has to be the parent.
2372       (while (and (setq data (cdr data))
2373                   (not (< (gnus-data-level (car data)) level))))
2374       (and data (gnus-data-number (car data))))))
2375
2376 (defun gnus-unread-mark-p (mark)
2377   "Say whether MARK is the unread mark."
2378   (= mark gnus-unread-mark))
2379
2380 (defun gnus-read-mark-p (mark)
2381   "Say whether MARK is one of the marks that mark as read.
2382 This is all marks except unread, ticked, dormant, and expirable."
2383   (not (or (= mark gnus-unread-mark)
2384            (= mark gnus-ticked-mark)
2385            (= mark gnus-dormant-mark)
2386            (= mark gnus-expirable-mark))))
2387
2388 (defmacro gnus-article-mark (number)
2389   "Return the MARK of article NUMBER.
2390 This macro should only be used when computing the mark the \"first\"
2391 time; i.e., when generating the summary lines.  After that,
2392 `gnus-summary-article-mark' should be used to examine the
2393 marks of articles."
2394   `(cond
2395     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2396     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2397     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2398     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2399     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2400     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2401     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2402     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2403            gnus-ancient-mark))))
2404
2405 ;; Saving hidden threads.
2406
2407 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2408 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2409
2410 (defmacro gnus-save-hidden-threads (&rest forms)
2411   "Save hidden threads, eval FORMS, and restore the hidden threads."
2412   (let ((config (make-symbol "config")))
2413     `(let ((,config (gnus-hidden-threads-configuration)))
2414        (unwind-protect
2415            (save-excursion
2416              ,@forms)
2417          (gnus-restore-hidden-threads-configuration ,config)))))
2418
2419 (defun gnus-data-compute-positions ()
2420   "Compute the positions of all articles."
2421   (setq gnus-newsgroup-data-reverse nil)
2422   (let ((data gnus-newsgroup-data))
2423     (save-excursion
2424       (gnus-save-hidden-threads
2425         (gnus-summary-show-all-threads)
2426         (goto-char (point-min))
2427         (while data
2428           (while (get-text-property (point) 'gnus-intangible)
2429             (forward-line 1))
2430           (gnus-data-set-pos (car data) (+ (point) 3))
2431           (setq data (cdr data))
2432           (forward-line 1))))))
2433
2434 (defun gnus-hidden-threads-configuration ()
2435   "Return the current hidden threads configuration."
2436   (save-excursion
2437     (let (config)
2438       (goto-char (point-min))
2439       (while (search-forward "\r" nil t)
2440         (push (1- (point)) config))
2441       config)))
2442
2443 (defun gnus-restore-hidden-threads-configuration (config)
2444   "Restore hidden threads configuration from CONFIG."
2445   (let (point buffer-read-only)
2446     (while (setq point (pop config))
2447       (when (and (< point (point-max))
2448                  (goto-char point)
2449                  (eq (char-after) ?\n))
2450         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2451
2452 ;; Various summary mode internalish functions.
2453
2454 (defun gnus-mouse-pick-article (e)
2455   (interactive "e")
2456   (mouse-set-point e)
2457   (gnus-summary-next-page nil t))
2458
2459 (defun gnus-summary-set-display-table ()
2460   ;; Change the display table.  Odd characters have a tendency to mess
2461   ;; up nicely formatted displays - we make all possible glyphs
2462   ;; display only a single character.
2463
2464   ;; We start from the standard display table, if any.
2465   (let ((table (or (copy-sequence standard-display-table)
2466                    (make-display-table)))
2467         (i 32))
2468     ;; Nix out all the control chars...
2469     (while (>= (setq i (1- i)) 0)
2470       (aset table i [??]))
2471     ;; ... but not newline and cr, of course.  (cr is necessary for the
2472     ;; selective display).
2473     (aset table ?\n nil)
2474     (aset table ?\r nil)
2475     ;; We keep TAB as well.
2476     (aset table ?\t nil)
2477     ;; We nix out any glyphs over 126 that are not set already.
2478     (let ((i 256))
2479       (while (>= (setq i (1- i)) 127)
2480         ;; Only modify if the entry is nil.
2481         (unless (aref table i)
2482           (aset table i [??]))))
2483     (setq buffer-display-table table)))
2484
2485 (defun gnus-summary-setup-buffer (group)
2486   "Initialize summary buffer."
2487   (let ((buffer (concat "*Summary " group "*")))
2488     (if (get-buffer buffer)
2489         (progn
2490           (set-buffer buffer)
2491           (setq gnus-summary-buffer (current-buffer))
2492           (not gnus-newsgroup-prepared))
2493       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2494       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2495       (gnus-summary-mode group)
2496       (when gnus-carpal
2497         (gnus-carpal-setup-buffer 'summary))
2498       (unless gnus-single-article-buffer
2499         (make-local-variable 'gnus-article-buffer)
2500         (make-local-variable 'gnus-article-current)
2501         (make-local-variable 'gnus-original-article-buffer))
2502       (setq gnus-newsgroup-name group)
2503       t)))
2504
2505 (defun gnus-set-global-variables ()
2506   ;; Set the global equivalents of the summary buffer-local variables
2507   ;; to the latest values they had.  These reflect the summary buffer
2508   ;; that was in action when the last article was fetched.
2509   (when (eq major-mode 'gnus-summary-mode)
2510     (setq gnus-summary-buffer (current-buffer))
2511     (let ((name gnus-newsgroup-name)
2512           (marked gnus-newsgroup-marked)
2513           (unread gnus-newsgroup-unreads)
2514           (headers gnus-current-headers)
2515           (data gnus-newsgroup-data)
2516           (summary gnus-summary-buffer)
2517           (article-buffer gnus-article-buffer)
2518           (original gnus-original-article-buffer)
2519           (gac gnus-article-current)
2520           (reffed gnus-reffed-article-number)
2521           (score-file gnus-current-score-file)
2522           (default-charset gnus-newsgroup-charset))
2523       (save-excursion
2524         (set-buffer gnus-group-buffer)
2525         (setq gnus-newsgroup-name name
2526               gnus-newsgroup-marked marked
2527               gnus-newsgroup-unreads unread
2528               gnus-current-headers headers
2529               gnus-newsgroup-data data
2530               gnus-article-current gac
2531               gnus-summary-buffer summary
2532               gnus-article-buffer article-buffer
2533               gnus-original-article-buffer original
2534               gnus-reffed-article-number reffed
2535               gnus-current-score-file score-file
2536               gnus-newsgroup-charset default-charset)
2537         ;; The article buffer also has local variables.
2538         (when (gnus-buffer-live-p gnus-article-buffer)
2539           (set-buffer gnus-article-buffer)
2540           (setq gnus-summary-buffer summary))))))
2541
2542 (defun gnus-summary-article-unread-p (article)
2543   "Say whether ARTICLE is unread or not."
2544   (memq article gnus-newsgroup-unreads))
2545
2546 (defun gnus-summary-first-article-p (&optional article)
2547   "Return whether ARTICLE is the first article in the buffer."
2548   (if (not (setq article (or article (gnus-summary-article-number))))
2549       nil
2550     (eq article (caar gnus-newsgroup-data))))
2551
2552 (defun gnus-summary-last-article-p (&optional article)
2553   "Return whether ARTICLE is the last article in the buffer."
2554   (if (not (setq article (or article (gnus-summary-article-number))))
2555       ;; All non-existent numbers are the last article.  :-)
2556       t
2557     (not (cdr (gnus-data-find-list article)))))
2558
2559 (defun gnus-make-thread-indent-array ()
2560   (let ((n 200))
2561     (unless (and gnus-thread-indent-array
2562                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2563       (setq gnus-thread-indent-array (make-vector 201 "")
2564             gnus-thread-indent-array-level gnus-thread-indent-level)
2565       (while (>= n 0)
2566         (aset gnus-thread-indent-array n
2567               (make-string (* n gnus-thread-indent-level) ? ))
2568         (setq n (1- n))))))
2569
2570 (defun gnus-update-summary-mark-positions ()
2571   "Compute where the summary marks are to go."
2572   (save-excursion
2573     (when (gnus-buffer-exists-p gnus-summary-buffer)
2574       (set-buffer gnus-summary-buffer))
2575     (let ((gnus-replied-mark 129)
2576           (gnus-score-below-mark 130)
2577           (gnus-score-over-mark 130)
2578           (gnus-download-mark 131)
2579           (spec gnus-summary-line-format-spec)
2580           gnus-visual pos)
2581       (save-excursion
2582         (gnus-set-work-buffer)
2583         (let ((gnus-summary-line-format-spec spec)
2584               (gnus-newsgroup-downloadable '((0 . t))))
2585           (gnus-summary-insert-line
2586            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2587            0 nil 128 t nil "" nil 1)
2588           (goto-char (point-min))
2589           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2590                                              (- (point) 2)))))
2591           (goto-char (point-min))
2592           (push (cons 'replied (and (search-forward "\201" nil t)
2593                                     (- (point) 2)))
2594                 pos)
2595           (goto-char (point-min))
2596           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2597                 pos)
2598           (goto-char (point-min))
2599           (push (cons 'download
2600                       (and (search-forward "\203" nil t) (- (point) 2)))
2601                 pos)))
2602       (setq gnus-summary-mark-positions pos))))
2603
2604 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2605   "Insert a dummy root in the summary buffer."
2606   (beginning-of-line)
2607   (gnus-add-text-properties
2608    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2609    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2610
2611 (defun gnus-summary-from-or-to-or-newsgroups (header)
2612   (let ((to (cdr (assq 'To (mail-header-extra header))))
2613         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2614         (default-mime-charset (with-current-buffer gnus-summary-buffer
2615                                 default-mime-charset)))
2616     (cond
2617      ((and to
2618            gnus-ignored-from-addresses
2619            (string-match gnus-ignored-from-addresses
2620                          (mail-header-from header)))
2621       (concat "-> "
2622               (or (car (funcall gnus-extract-address-components
2623                                 (funcall
2624                                  gnus-decode-encoded-word-function to)))
2625                   (funcall gnus-decode-encoded-word-function to))))
2626      ((and newsgroups
2627            gnus-ignored-from-addresses
2628            (string-match gnus-ignored-from-addresses
2629                          (mail-header-from header)))
2630       (concat "=> " newsgroups))
2631      (t
2632       (or (car (funcall gnus-extract-address-components
2633                         (mail-header-from header)))
2634           (mail-header-from header))))))
2635
2636 (defun gnus-summary-insert-line (gnus-tmp-header
2637                                  gnus-tmp-level gnus-tmp-current
2638                                  gnus-tmp-unread gnus-tmp-replied
2639                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2640                                  &optional gnus-tmp-dummy gnus-tmp-score
2641                                  gnus-tmp-process)
2642   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2643          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2644          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2645          (gnus-tmp-score-char
2646           (if (or (null gnus-summary-default-score)
2647                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2648                       gnus-summary-zcore-fuzz))
2649               ?  ;Whitespace
2650             (if (< gnus-tmp-score gnus-summary-default-score)
2651                 gnus-score-below-mark gnus-score-over-mark)))
2652          (gnus-tmp-replied
2653           (cond (gnus-tmp-process gnus-process-mark)
2654                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2655                  gnus-cached-mark)
2656                 (gnus-tmp-replied gnus-replied-mark)
2657                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2658                  gnus-saved-mark)
2659                 (t gnus-unread-mark)))
2660          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2661          (gnus-tmp-name
2662           (cond
2663            ((string-match "<[^>]+> *$" gnus-tmp-from)
2664             (let ((beg (match-beginning 0)))
2665               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2666                        (substring gnus-tmp-from (1+ (match-beginning 0))
2667                                   (1- (match-end 0))))
2668                   (substring gnus-tmp-from 0 beg))))
2669            ((string-match "(.+)" gnus-tmp-from)
2670             (substring gnus-tmp-from
2671                        (1+ (match-beginning 0)) (1- (match-end 0))))
2672            (t gnus-tmp-from)))
2673          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2674          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2675          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2676          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2677          (buffer-read-only nil))
2678     (when (string= gnus-tmp-name "")
2679       (setq gnus-tmp-name gnus-tmp-from))
2680     (unless (numberp gnus-tmp-lines)
2681       (setq gnus-tmp-lines 0))
2682     (gnus-put-text-property-excluding-characters-with-faces
2683      (point)
2684      (progn (eval gnus-summary-line-format-spec) (point))
2685      'gnus-number gnus-tmp-number)
2686     (when (gnus-visual-p 'summary-highlight 'highlight)
2687       (forward-line -1)
2688       (gnus-run-hooks 'gnus-summary-update-hook)
2689       (forward-line 1))))
2690
2691 (defun gnus-summary-update-line (&optional dont-update)
2692   ;; Update summary line after change.
2693   (when (and gnus-summary-default-score
2694              (not gnus-summary-inhibit-highlight))
2695     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2696            (article (gnus-summary-article-number))
2697            (score (gnus-summary-article-score article)))
2698       (unless dont-update
2699         (if (and gnus-summary-mark-below
2700                  (< (gnus-summary-article-score)
2701                     gnus-summary-mark-below))
2702             ;; This article has a low score, so we mark it as read.
2703             (when (memq article gnus-newsgroup-unreads)
2704               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2705           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2706             ;; This article was previously marked as read on account
2707             ;; of a low score, but now it has risen, so we mark it as
2708             ;; unread.
2709             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2710         (gnus-summary-update-mark
2711          (if (or (null gnus-summary-default-score)
2712                  (<= (abs (- score gnus-summary-default-score))
2713                      gnus-summary-zcore-fuzz))
2714              ?  ;Whitespace
2715            (if (< score gnus-summary-default-score)
2716                gnus-score-below-mark gnus-score-over-mark))
2717          'score))
2718       ;; Do visual highlighting.
2719       (when (gnus-visual-p 'summary-highlight 'highlight)
2720         (gnus-run-hooks 'gnus-summary-update-hook)))))
2721
2722 (defvar gnus-tmp-new-adopts nil)
2723
2724 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2725   "Return the number of articles in THREAD.
2726 This may be 0 in some cases -- if none of the articles in
2727 the thread are to be displayed."
2728   (let* ((number
2729           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2730           (cond
2731            ((not (listp thread))
2732             1)
2733            ((and (consp thread) (cdr thread))
2734             (apply
2735              '+ 1 (mapcar
2736                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2737            ((null thread)
2738             1)
2739            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2740             1)
2741            (t 0))))
2742     (when (and level (zerop level) gnus-tmp-new-adopts)
2743       (incf number
2744             (apply '+ (mapcar
2745                        'gnus-summary-number-of-articles-in-thread
2746                        gnus-tmp-new-adopts))))
2747     (if char
2748         (if (> number 1) gnus-not-empty-thread-mark
2749           gnus-empty-thread-mark)
2750       number)))
2751
2752 (defun gnus-summary-set-local-parameters (group)
2753   "Go through the local params of GROUP and set all variable specs in that list."
2754   (let ((params (gnus-group-find-parameter group))
2755         elem)
2756     (while params
2757       (setq elem (car params)
2758             params (cdr params))
2759       (and (consp elem)                 ; Has to be a cons.
2760            (consp (cdr elem))           ; The cdr has to be a list.
2761            (symbolp (car elem))         ; Has to be a symbol in there.
2762            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2763            (ignore-errors               ; So we set it.
2764              (make-local-variable (car elem))
2765              (set (car elem) (eval (nth 1 elem))))))))
2766
2767 (defun gnus-summary-read-group (group &optional show-all no-article
2768                                       kill-buffer no-display backward
2769                                       select-articles)
2770   "Start reading news in newsgroup GROUP.
2771 If SHOW-ALL is non-nil, already read articles are also listed.
2772 If NO-ARTICLE is non-nil, no article is selected initially.
2773 If NO-DISPLAY, don't generate a summary buffer."
2774   (let (result)
2775     (while (and group
2776                 (null (setq result
2777                             (let ((gnus-auto-select-next nil))
2778                               (or (gnus-summary-read-group-1
2779                                    group show-all no-article
2780                                    kill-buffer no-display
2781                                    select-articles)
2782                                   (setq show-all nil
2783                                         select-articles nil)))))
2784                 (eq gnus-auto-select-next 'quietly))
2785       (set-buffer gnus-group-buffer)
2786       ;; The entry function called above goes to the next
2787       ;; group automatically, so we go two groups back
2788       ;; if we are searching for the previous group.
2789       (when backward
2790         (gnus-group-prev-unread-group 2))
2791       (if (not (equal group (gnus-group-group-name)))
2792           (setq group (gnus-group-group-name))
2793         (setq group nil)))
2794     result))
2795
2796 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2797   "Directly jump to the other GROUP from summary buffer.
2798 If SHOW-ALL is non-nil, already read articles are also listed."
2799   (interactive
2800    (if (eq gnus-summary-buffer (current-buffer))
2801        (list (completing-read
2802               "Group: " gnus-active-hashtb nil t
2803               (when (and gnus-newsgroup-name
2804                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2805                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2806               'gnus-group-history)
2807              current-prefix-arg)
2808      (error "%s must be invoked from a gnus summary buffer." this-command)))
2809   (unless (or (zerop (length group))
2810               (and gnus-newsgroup-name
2811                    (string-equal gnus-newsgroup-name group)))
2812     (gnus-summary-exit)
2813     (gnus-summary-read-group group show-all
2814                              gnus-dont-select-after-jump-to-other-group)))
2815
2816 (defun gnus-summary-read-group-1 (group show-all no-article
2817                                         kill-buffer no-display
2818                                         &optional select-articles)
2819   ;; Killed foreign groups can't be entered.
2820   (when (and (not (gnus-group-native-p group))
2821              (not (gnus-gethash group gnus-newsrc-hashtb)))
2822     (error "Dead non-native groups can't be entered"))
2823   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2824   (let* ((new-group (gnus-summary-setup-buffer group))
2825          (quit-config (gnus-group-quit-config group))
2826          (did-select (and new-group (gnus-select-newsgroup
2827                                      group show-all select-articles))))
2828     (cond
2829      ;; This summary buffer exists already, so we just select it.
2830      ((not new-group)
2831       (gnus-set-global-variables)
2832       (when kill-buffer
2833         (gnus-kill-or-deaden-summary kill-buffer))
2834       (gnus-configure-windows 'summary 'force)
2835       (gnus-set-mode-line 'summary)
2836       (gnus-summary-position-point)
2837       (message "")
2838       t)
2839      ;; We couldn't select this group.
2840      ((null did-select)
2841       (when (and (eq major-mode 'gnus-summary-mode)
2842                  (not (equal (current-buffer) kill-buffer)))
2843         (kill-buffer (current-buffer))
2844         (if (not quit-config)
2845             (progn
2846               ;; Update the info -- marks might need to be removed,
2847               ;; for instance.
2848               (gnus-summary-update-info)
2849               (set-buffer gnus-group-buffer)
2850               (gnus-group-jump-to-group group)
2851               (gnus-group-next-unread-group 1))
2852           (gnus-handle-ephemeral-exit quit-config)))
2853       (gnus-message 3 "Can't select group")
2854       nil)
2855      ;; The user did a `C-g' while prompting for number of articles,
2856      ;; so we exit this group.
2857      ((eq did-select 'quit)
2858       (and (eq major-mode 'gnus-summary-mode)
2859            (not (equal (current-buffer) kill-buffer))
2860            (kill-buffer (current-buffer)))
2861       (when kill-buffer
2862         (gnus-kill-or-deaden-summary kill-buffer))
2863       (if (not quit-config)
2864           (progn
2865             (set-buffer gnus-group-buffer)
2866             (gnus-group-jump-to-group group)
2867             (gnus-group-next-unread-group 1)
2868             (gnus-configure-windows 'group 'force))
2869         (gnus-handle-ephemeral-exit quit-config))
2870       ;; Finally signal the quit.
2871       (signal 'quit nil))
2872      ;; The group was successfully selected.
2873      (t
2874       (gnus-set-global-variables)
2875       ;; Save the active value in effect when the group was entered.
2876       (setq gnus-newsgroup-active
2877             (gnus-copy-sequence
2878              (gnus-active gnus-newsgroup-name)))
2879       ;; You can change the summary buffer in some way with this hook.
2880       (gnus-run-hooks 'gnus-select-group-hook)
2881       ;; Set any local variables in the group parameters.
2882       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2883       (gnus-update-format-specifications
2884        nil 'summary 'summary-mode 'summary-dummy)
2885       (gnus-update-summary-mark-positions)
2886       ;; Do score processing.
2887       (when gnus-use-scoring
2888         (gnus-possibly-score-headers))
2889       ;; Check whether to fill in the gaps in the threads.
2890       (when gnus-build-sparse-threads
2891         (gnus-build-sparse-threads))
2892       ;; Find the initial limit.
2893       (if gnus-show-threads
2894           (if show-all
2895               (let ((gnus-newsgroup-dormant nil))
2896                 (gnus-summary-initial-limit show-all))
2897             (gnus-summary-initial-limit show-all))
2898         ;; When untreaded, all articles are always shown.
2899         (setq gnus-newsgroup-limit
2900               (mapcar
2901                (lambda (header) (mail-header-number header))
2902                gnus-newsgroup-headers)))
2903       ;; Generate the summary buffer.
2904       (unless no-display
2905         (gnus-summary-prepare))
2906       (when gnus-use-trees
2907         (gnus-tree-open group)
2908         (setq gnus-summary-highlight-line-function
2909               'gnus-tree-highlight-article))
2910       ;; If the summary buffer is empty, but there are some low-scored
2911       ;; articles or some excluded dormants, we include these in the
2912       ;; buffer.
2913       (when (and (zerop (buffer-size))
2914                  (not no-display))
2915         (cond (gnus-newsgroup-dormant
2916                (gnus-summary-limit-include-dormant))
2917               ((and gnus-newsgroup-scored show-all)
2918                (gnus-summary-limit-include-expunged t))))
2919       ;; Function `gnus-apply-kill-file' must be called in this hook.
2920       (gnus-run-hooks 'gnus-apply-kill-hook)
2921       (if (and (zerop (buffer-size))
2922                (not no-display))
2923           (progn
2924             ;; This newsgroup is empty.
2925             (gnus-summary-catchup-and-exit nil t)
2926             (gnus-message 6 "No unread news")
2927             (when kill-buffer
2928               (gnus-kill-or-deaden-summary kill-buffer))
2929             ;; Return nil from this function.
2930             nil)
2931         ;; Hide conversation thread subtrees.  We cannot do this in
2932         ;; gnus-summary-prepare-hook since kill processing may not
2933         ;; work with hidden articles.
2934         (and gnus-show-threads
2935              gnus-thread-hide-subtree
2936              (gnus-summary-hide-all-threads))
2937         (when kill-buffer
2938           (gnus-kill-or-deaden-summary kill-buffer))
2939         ;; Show first unread article if requested.
2940         (if (and (not no-article)
2941                  (not no-display)
2942                  gnus-newsgroup-unreads
2943                  gnus-auto-select-first)
2944             (progn
2945               (gnus-configure-windows 'summary)
2946               (cond
2947                ((eq gnus-auto-select-first 'best)
2948                 (gnus-summary-best-unread-article))
2949                ((eq gnus-auto-select-first t)
2950                 (gnus-summary-first-unread-article))
2951                ((gnus-functionp gnus-auto-select-first)
2952                 (funcall gnus-auto-select-first))))
2953           ;; Don't select any articles, just move point to the first
2954           ;; article in the group.
2955           (goto-char (point-min))
2956           (gnus-summary-position-point)
2957           (gnus-configure-windows 'summary 'force)
2958           (gnus-set-mode-line 'summary))
2959         (when (get-buffer-window gnus-group-buffer t)
2960           ;; Gotta use windows, because recenter does weird stuff if
2961           ;; the current buffer ain't the displayed window.
2962           (let ((owin (selected-window)))
2963             (select-window (get-buffer-window gnus-group-buffer t))
2964             (when (gnus-group-goto-group group)
2965               (recenter))
2966             (select-window owin)))
2967         ;; Mark this buffer as "prepared".
2968         (setq gnus-newsgroup-prepared t)
2969         (gnus-run-hooks 'gnus-summary-prepared-hook)
2970         t)))))
2971
2972 (defun gnus-summary-prepare ()
2973   "Generate the summary buffer."
2974   (interactive)
2975   (let ((buffer-read-only nil))
2976     (erase-buffer)
2977     (setq gnus-newsgroup-data nil
2978           gnus-newsgroup-data-reverse nil)
2979     (gnus-run-hooks 'gnus-summary-generate-hook)
2980     ;; Generate the buffer, either with threads or without.
2981     (when gnus-newsgroup-headers
2982       (gnus-summary-prepare-threads
2983        (if gnus-show-threads
2984            (gnus-sort-gathered-threads
2985             (funcall gnus-summary-thread-gathering-function
2986                      (gnus-sort-threads
2987                       (gnus-cut-threads (gnus-make-threads)))))
2988          ;; Unthreaded display.
2989          (gnus-sort-articles gnus-newsgroup-headers))))
2990     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2991     ;; Call hooks for modifying summary buffer.
2992     (goto-char (point-min))
2993     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2994
2995 (defsubst gnus-general-simplify-subject (subject)
2996   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2997   (setq subject
2998         (cond
2999          ;; Truncate the subject.
3000          (gnus-simplify-subject-functions
3001           (gnus-map-function gnus-simplify-subject-functions subject))
3002          ((numberp gnus-summary-gather-subject-limit)
3003           (setq subject (gnus-simplify-subject-re subject))
3004           (if (> (length subject) gnus-summary-gather-subject-limit)
3005               (substring subject 0 gnus-summary-gather-subject-limit)
3006             subject))
3007          ;; Fuzzily simplify it.
3008          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3009           (gnus-simplify-subject-fuzzy subject))
3010          ;; Just remove the leading "Re:".
3011          (t
3012           (gnus-simplify-subject-re subject))))
3013
3014   (if (and gnus-summary-gather-exclude-subject
3015            (string-match gnus-summary-gather-exclude-subject subject))
3016       nil                               ; This article shouldn't be gathered
3017     subject))
3018
3019 (defun gnus-summary-simplify-subject-query ()
3020   "Query where the respool algorithm would put this article."
3021   (interactive)
3022   (gnus-summary-select-article)
3023   (message "%s"
3024            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3025
3026 (defun gnus-gather-threads-by-subject (threads)
3027   "Gather threads by looking at Subject headers."
3028   (if (not gnus-summary-make-false-root)
3029       threads
3030     (let ((hashtb (gnus-make-hashtable 1024))
3031           (prev threads)
3032           (result threads)
3033           subject hthread whole-subject)
3034       (while threads
3035         (setq subject (gnus-general-simplify-subject
3036                        (setq whole-subject (mail-header-subject
3037                                             (caar threads)))))
3038         (when subject
3039           (if (setq hthread (gnus-gethash subject hashtb))
3040               (progn
3041                 ;; We enter a dummy root into the thread, if we
3042                 ;; haven't done that already.
3043                 (unless (stringp (caar hthread))
3044                   (setcar hthread (list whole-subject (car hthread))))
3045                 ;; We add this new gathered thread to this gathered
3046                 ;; thread.
3047                 (setcdr (car hthread)
3048                         (nconc (cdar hthread) (list (car threads))))
3049                 ;; Remove it from the list of threads.
3050                 (setcdr prev (cdr threads))
3051                 (setq threads prev))
3052             ;; Enter this thread into the hash table.
3053             (gnus-sethash subject threads hashtb)))
3054         (setq prev threads)
3055         (setq threads (cdr threads)))
3056       result)))
3057
3058 (defun gnus-gather-threads-by-references (threads)
3059   "Gather threads by looking at References headers."
3060   (let ((idhashtb (gnus-make-hashtable 1024))
3061         (thhashtb (gnus-make-hashtable 1024))
3062         (prev threads)
3063         (result threads)
3064         ids references id gthread gid entered ref)
3065     (while threads
3066       (when (setq references (mail-header-references (caar threads)))
3067         (setq id (mail-header-id (caar threads))
3068               ids (gnus-split-references references)
3069               entered nil)
3070         (while (setq ref (pop ids))
3071           (setq ids (delete ref ids))
3072           (if (not (setq gid (gnus-gethash ref idhashtb)))
3073               (progn
3074                 (gnus-sethash ref id idhashtb)
3075                 (gnus-sethash id threads thhashtb))
3076             (setq gthread (gnus-gethash gid thhashtb))
3077             (unless entered
3078               ;; We enter a dummy root into the thread, if we
3079               ;; haven't done that already.
3080               (unless (stringp (caar gthread))
3081                 (setcar gthread (list (mail-header-subject (caar gthread))
3082                                       (car gthread))))
3083               ;; We add this new gathered thread to this gathered
3084               ;; thread.
3085               (setcdr (car gthread)
3086                       (nconc (cdar gthread) (list (car threads)))))
3087             ;; Add it into the thread hash table.
3088             (gnus-sethash id gthread thhashtb)
3089             (setq entered t)
3090             ;; Remove it from the list of threads.
3091             (setcdr prev (cdr threads))
3092             (setq threads prev))))
3093       (setq prev threads)
3094       (setq threads (cdr threads)))
3095     result))
3096
3097 (defun gnus-sort-gathered-threads (threads)
3098   "Sort subtreads inside each gathered thread by article number."
3099   (let ((result threads))
3100     (while threads
3101       (when (stringp (caar threads))
3102         (setcdr (car threads)
3103                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3104       (setq threads (cdr threads)))
3105     result))
3106
3107 (defun gnus-thread-loop-p (root thread)
3108   "Say whether ROOT is in THREAD."
3109   (let ((stack (list thread))
3110         (infloop 0)
3111         th)
3112     (while (setq thread (pop stack))
3113       (setq th (cdr thread))
3114       (while (and th
3115                   (not (eq (caar th) root)))
3116         (pop th))
3117       (if th
3118           ;; We have found a loop.
3119           (let (ref-dep)
3120             (setcdr thread (delq (car th) (cdr thread)))
3121             (if (boundp (setq ref-dep (intern "none"
3122                                               gnus-newsgroup-dependencies)))
3123                 (setcdr (symbol-value ref-dep)
3124                         (nconc (cdr (symbol-value ref-dep))
3125                                (list (car th))))
3126               (set ref-dep (list nil (car th))))
3127             (setq infloop 1
3128                   stack nil))
3129         ;; Push all the subthreads onto the stack.
3130         (push (cdr thread) stack)))
3131     infloop))
3132
3133 (defun gnus-make-threads ()
3134   "Go through the dependency hashtb and find the roots.  Return all threads."
3135   (let (threads)
3136     (while (catch 'infloop
3137              (mapatoms
3138               (lambda (refs)
3139                 ;; Deal with self-referencing References loops.
3140                 (when (and (car (symbol-value refs))
3141                            (not (zerop
3142                                  (apply
3143                                   '+
3144                                   (mapcar
3145                                    (lambda (thread)
3146                                      (gnus-thread-loop-p
3147                                       (car (symbol-value refs)) thread))
3148                                    (cdr (symbol-value refs)))))))
3149                   (setq threads nil)
3150                   (throw 'infloop t))
3151                 (unless (car (symbol-value refs))
3152                   ;; These threads do not refer back to any other articles,
3153                   ;; so they're roots.
3154                   (setq threads (append (cdr (symbol-value refs)) threads))))
3155               gnus-newsgroup-dependencies)))
3156     threads))
3157
3158 ;; Build the thread tree.
3159 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3160   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3161
3162 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3163 if it was already present.
3164
3165 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3166 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3167 Message-IDs will be renamed be renamed to a unique Message-ID before
3168 being entered.
3169
3170 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3171   (let* ((id (mail-header-id header))
3172          (id-dep (and id (intern id dependencies)))
3173          ref ref-dep ref-header)
3174     ;; Enter this `header' in the `dependencies' table.
3175     (cond
3176      ((not id-dep)
3177       (setq header nil))
3178      ;; The first two cases do the normal part: enter a new `header'
3179      ;; in the `dependencies' table.
3180      ((not (boundp id-dep))
3181       (set id-dep (list header)))
3182      ((null (car (symbol-value id-dep)))
3183       (setcar (symbol-value id-dep) header))
3184
3185      ;; From here the `header' was already present in the
3186      ;; `dependencies' table.
3187      (force-new
3188       ;; Overrides an existing entry;
3189       ;; just set the header part of the entry.
3190       (setcar (symbol-value id-dep) header))
3191
3192      ;; Renames the existing `header' to a unique Message-ID.
3193      ((not gnus-summary-ignore-duplicates)
3194       ;; An article with this Message-ID has already been seen.
3195       ;; We rename the Message-ID.
3196       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3197            (list header))
3198       (mail-header-set-id header id))
3199
3200      ;; The last case ignores an existing entry, except it adds any
3201      ;; additional Xrefs (in case the two articles came from different
3202      ;; servers.
3203      ;; Also sets `header' to `nil' meaning that the `dependencies'
3204      ;; table was *not* modified.
3205      (t
3206       (mail-header-set-xref
3207        (car (symbol-value id-dep))
3208        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3209                    "")
3210                (or (mail-header-xref header) "")))
3211       (setq header nil)))
3212
3213     (when header
3214       ;; First check if that we are not creating a References loop.
3215       (setq ref (gnus-parent-id (mail-header-references header)))
3216       (while (and ref
3217                   (setq ref-dep (intern-soft ref dependencies))
3218                   (boundp ref-dep)
3219                   (setq ref-header (car (symbol-value ref-dep))))
3220         (if (string= id ref)
3221             ;; Yuk!  This is a reference loop.  Make the article be a
3222             ;; root article.
3223             (progn
3224               (mail-header-set-references (car (symbol-value id-dep)) "none")
3225               (setq ref nil))
3226           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3227       (setq ref (gnus-parent-id (mail-header-references header)))
3228       (setq ref-dep (intern (or ref "none") dependencies))
3229       (if (boundp ref-dep)
3230           (setcdr (symbol-value ref-dep)
3231                   (nconc (cdr (symbol-value ref-dep))
3232                          (list (symbol-value id-dep))))
3233         (set ref-dep (list nil (symbol-value id-dep)))))
3234     header))
3235
3236 (defun gnus-build-sparse-threads ()
3237   (let ((headers gnus-newsgroup-headers)
3238         (mail-parse-charset gnus-newsgroup-charset)
3239         (gnus-summary-ignore-duplicates t)
3240         header references generation relations
3241         subject child end new-child date)
3242     ;; First we create an alist of generations/relations, where
3243     ;; generations is how much we trust the relation, and the relation
3244     ;; is parent/child.
3245     (gnus-message 7 "Making sparse threads...")
3246     (save-excursion
3247       (nnheader-set-temp-buffer " *gnus sparse threads*")
3248       (while (setq header (pop headers))
3249         (when (and (setq references (mail-header-references header))
3250                    (not (string= references "")))
3251           (insert references)
3252           (setq child (mail-header-id header)
3253                 subject (mail-header-subject header)
3254                 date (mail-header-date header)
3255                 generation 0)
3256           (while (search-backward ">" nil t)
3257             (setq end (1+ (point)))
3258             (when (search-backward "<" nil t)
3259               (setq new-child (buffer-substring (point) end))
3260               (push (list (incf generation)
3261                           child (setq child new-child)
3262                           subject date)
3263                     relations)))
3264           (when child
3265             (push (list (1+ generation) child nil subject) relations))
3266           (erase-buffer)))
3267       (kill-buffer (current-buffer)))
3268     ;; Sort over trustworthiness.
3269     (mapcar
3270      (lambda (relation)
3271        (when (gnus-dependencies-add-header
3272               (make-full-mail-header
3273                gnus-reffed-article-number
3274                (nth 3 relation) "" (or (nth 4 relation) "")
3275                (nth 1 relation)
3276                (or (nth 2 relation) "") 0 0 "")
3277               gnus-newsgroup-dependencies nil)
3278          (push gnus-reffed-article-number gnus-newsgroup-limit)
3279          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3280          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3281                gnus-newsgroup-reads)
3282          (decf gnus-reffed-article-number)))
3283      (sort relations 'car-less-than-car))
3284     (gnus-message 7 "Making sparse threads...done")))
3285
3286 (defun gnus-build-old-threads ()
3287   ;; Look at all the articles that refer back to old articles, and
3288   ;; fetch the headers for the articles that aren't there.  This will
3289   ;; build complete threads - if the roots haven't been expired by the
3290   ;; server, that is.
3291   (let ((mail-parse-charset gnus-newsgroup-charset)
3292         id heads)
3293     (mapatoms
3294      (lambda (refs)
3295        (when (not (car (symbol-value refs)))
3296          (setq heads (cdr (symbol-value refs)))
3297          (while heads
3298            (if (memq (mail-header-number (caar heads))
3299                      gnus-newsgroup-dormant)
3300                (setq heads (cdr heads))
3301              (setq id (symbol-name refs))
3302              (while (and (setq id (gnus-build-get-header id))
3303                          (not (car (gnus-id-to-thread id)))))
3304              (setq heads nil)))))
3305      gnus-newsgroup-dependencies)))
3306
3307 ;; This function has to be called with point after the article number
3308 ;; on the beginning of the line.
3309 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3310   (let ((eol (gnus-point-at-eol))
3311         (buffer (current-buffer))
3312         header)
3313
3314     ;; overview: [num subject from date id refs chars lines misc]
3315     (unless (eobp)
3316       (forward-char))
3317
3318     (setq header
3319           (make-full-mail-header
3320            number                               ; number
3321            (nnheader-nov-field)                 ; subject
3322            (nnheader-nov-field)                 ; from
3323            (nnheader-nov-field)                 ; date
3324            (nnheader-nov-read-message-id)       ; id
3325            (nnheader-nov-field)                 ; refs
3326            (nnheader-nov-read-integer)          ; chars
3327            (nnheader-nov-read-integer)          ; lines
3328            (unless (eobp)
3329              (nnheader-nov-field))              ; misc
3330            (nnheader-nov-parse-extra)))         ; extra
3331
3332     (when gnus-alter-header-function
3333       (funcall gnus-alter-header-function header))
3334     (gnus-dependencies-add-header header dependencies force-new)))
3335
3336 (defun gnus-build-get-header (id)
3337   ;; Look through the buffer of NOV lines and find the header to
3338   ;; ID.  Enter this line into the dependencies hash table, and return
3339   ;; the id of the parent article (if any).
3340   (let ((deps gnus-newsgroup-dependencies)
3341         found header)
3342     (prog1
3343         (save-excursion
3344           (set-buffer nntp-server-buffer)
3345           (let ((case-fold-search nil))
3346             (goto-char (point-min))
3347             (while (and (not found)
3348                         (search-forward id nil t))
3349               (beginning-of-line)
3350               (setq found (looking-at
3351                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3352                                    (regexp-quote id))))
3353               (or found (beginning-of-line 2)))
3354             (when found
3355               (beginning-of-line)
3356               (and
3357                (setq header (gnus-nov-parse-line
3358                              (read (current-buffer)) deps))
3359                (gnus-parent-id (mail-header-references header))))))
3360       (when header
3361         (let ((number (mail-header-number header)))
3362           (push number gnus-newsgroup-limit)
3363           (push header gnus-newsgroup-headers)
3364           (if (memq number gnus-newsgroup-unselected)
3365               (progn
3366                 (push number gnus-newsgroup-unreads)
3367                 (setq gnus-newsgroup-unselected
3368                       (delq number gnus-newsgroup-unselected)))
3369             (push number gnus-newsgroup-ancient)))))))
3370
3371 (defun gnus-build-all-threads ()
3372   "Read all the headers."
3373   (let ((gnus-summary-ignore-duplicates t)
3374         (mail-parse-charset gnus-newsgroup-charset)
3375         (dependencies gnus-newsgroup-dependencies)
3376         header article)
3377     (save-excursion
3378       (set-buffer nntp-server-buffer)
3379       (let ((case-fold-search nil))
3380         (goto-char (point-min))
3381         (while (not (eobp))
3382           (ignore-errors
3383             (setq article (read (current-buffer))
3384                   header (gnus-nov-parse-line article dependencies)))
3385           (when header
3386             (save-excursion
3387               (set-buffer gnus-summary-buffer)
3388               (push header gnus-newsgroup-headers)
3389               (if (memq (setq article (mail-header-number header))
3390                         gnus-newsgroup-unselected)
3391                   (progn
3392                     (push article gnus-newsgroup-unreads)
3393                     (setq gnus-newsgroup-unselected
3394                           (delq article gnus-newsgroup-unselected)))
3395                 (push article gnus-newsgroup-ancient)))
3396             (forward-line 1)))))))
3397
3398 (defun gnus-summary-update-article-line (article header)
3399   "Update the line for ARTICLE using HEADERS."
3400   (let* ((id (mail-header-id header))
3401          (thread (gnus-id-to-thread id)))
3402     (unless thread
3403       (error "Article in no thread"))
3404     ;; Update the thread.
3405     (setcar thread header)
3406     (gnus-summary-goto-subject article)
3407     (let* ((datal (gnus-data-find-list article))
3408            (data (car datal))
3409            (length (when (cdr datal)
3410                      (- (gnus-data-pos data)
3411                         (gnus-data-pos (cadr datal)))))
3412            (buffer-read-only nil)
3413            (level (gnus-summary-thread-level)))
3414       (gnus-delete-line)
3415       (gnus-summary-insert-line
3416        header level nil (gnus-article-mark article)
3417        (memq article gnus-newsgroup-replied)
3418        (memq article gnus-newsgroup-expirable)
3419        ;; Only insert the Subject string when it's different
3420        ;; from the previous Subject string.
3421        (if (and
3422             gnus-show-threads
3423             (gnus-subject-equal
3424              (condition-case ()
3425                  (mail-header-subject
3426                   (gnus-data-header
3427                    (cadr
3428                     (gnus-data-find-list
3429                      article
3430                      (gnus-data-list t)))))
3431                ;; Error on the side of excessive subjects.
3432                (error ""))
3433              (mail-header-subject header)))
3434            ""
3435          (mail-header-subject header))
3436        nil (cdr (assq article gnus-newsgroup-scored))
3437        (memq article gnus-newsgroup-processable))
3438       (when length
3439         (gnus-data-update-list
3440          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3441
3442 (defun gnus-summary-update-article (article &optional iheader)
3443   "Update ARTICLE in the summary buffer."
3444   (set-buffer gnus-summary-buffer)
3445   (let* ((header (gnus-summary-article-header article))
3446          (id (mail-header-id header))
3447          (data (gnus-data-find article))
3448          (thread (gnus-id-to-thread id))
3449          (references (mail-header-references header))
3450          (parent
3451           (gnus-id-to-thread
3452            (or (gnus-parent-id
3453                 (when (and references
3454                            (not (equal "" references)))
3455                   references))
3456                "none")))
3457          (buffer-read-only nil)
3458          (old (car thread)))
3459     (when thread
3460       (unless iheader
3461         (setcar thread nil)
3462         (when parent
3463           (delq thread parent)))
3464       (if (gnus-summary-insert-subject id header)
3465           ;; Set the (possibly) new article number in the data structure.
3466           (gnus-data-set-number data (gnus-id-to-article id))
3467         (setcar thread old)
3468         nil))))
3469
3470 (defun gnus-rebuild-thread (id &optional line)
3471   "Rebuild the thread containing ID.
3472 If LINE, insert the rebuilt thread starting on line LINE."
3473   (let ((buffer-read-only nil)
3474         old-pos current thread data)
3475     (if (not gnus-show-threads)
3476         (setq thread (list (car (gnus-id-to-thread id))))
3477       ;; Get the thread this article is part of.
3478       (setq thread (gnus-remove-thread id)))
3479     (setq old-pos (gnus-point-at-bol))
3480     (setq current (save-excursion
3481                     (and (zerop (forward-line -1))
3482                          (gnus-summary-article-number))))
3483     ;; If this is a gathered thread, we have to go some re-gathering.
3484     (when (stringp (car thread))
3485       (let ((subject (car thread))
3486             roots thr)
3487         (setq thread (cdr thread))
3488         (while thread
3489           (unless (memq (setq thr (gnus-id-to-thread
3490                                    (gnus-root-id
3491                                     (mail-header-id (caar thread)))))
3492                         roots)
3493             (push thr roots))
3494           (setq thread (cdr thread)))
3495         ;; We now have all (unique) roots.
3496         (if (= (length roots) 1)
3497             ;; All the loose roots are now one solid root.
3498             (setq thread (car roots))
3499           (setq thread (cons subject (gnus-sort-threads roots))))))
3500     (let (threads)
3501       ;; We then insert this thread into the summary buffer.
3502       (when line
3503         (goto-char (point-min))
3504         (forward-line (1- line)))
3505       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3506         (if gnus-show-threads
3507             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3508           (gnus-summary-prepare-unthreaded thread))
3509         (setq data (nreverse gnus-newsgroup-data))
3510         (setq threads gnus-newsgroup-threads))
3511       ;; We splice the new data into the data structure.
3512       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3513       ;;!!! then we want to insert at the beginning of the buffer.
3514       ;;!!! That happens to be true with Gnus now, but that may
3515       ;;!!! change in the future.  Perhaps.
3516       (gnus-data-enter-list
3517        (if line nil current) data (- (point) old-pos))
3518       (setq gnus-newsgroup-threads
3519             (nconc threads gnus-newsgroup-threads))
3520       (gnus-data-compute-positions))))
3521
3522 (defun gnus-number-to-header (number)
3523   "Return the header for article NUMBER."
3524   (let ((headers gnus-newsgroup-headers))
3525     (while (and headers
3526                 (not (= number (mail-header-number (car headers)))))
3527       (pop headers))
3528     (when headers
3529       (car headers))))
3530
3531 (defun gnus-parent-headers (in-headers &optional generation)
3532   "Return the headers of the GENERATIONeth parent of HEADERS."
3533   (unless generation
3534     (setq generation 1))
3535   (let ((parent t)
3536         (headers in-headers)
3537         references)
3538     (while (and parent
3539                 (not (zerop generation))
3540                 (setq references (mail-header-references headers)))
3541       (setq headers (if (and references
3542                              (setq parent (gnus-parent-id references)))
3543                         (car (gnus-id-to-thread parent))
3544                       nil))
3545       (decf generation))
3546     (and (not (eq headers in-headers))
3547          headers)))
3548
3549 (defun gnus-id-to-thread (id)
3550   "Return the (sub-)thread where ID appears."
3551   (gnus-gethash id gnus-newsgroup-dependencies))
3552
3553 (defun gnus-id-to-article (id)
3554   "Return the article number of ID."
3555   (let ((thread (gnus-id-to-thread id)))
3556     (when (and thread
3557                (car thread))
3558       (mail-header-number (car thread)))))
3559
3560 (defun gnus-id-to-header (id)
3561   "Return the article headers of ID."
3562   (car (gnus-id-to-thread id)))
3563
3564 (defun gnus-article-displayed-root-p (article)
3565   "Say whether ARTICLE is a root(ish) article."
3566   (let ((level (gnus-summary-thread-level article))
3567         (refs (mail-header-references  (gnus-summary-article-header article)))
3568         particle)
3569     (cond
3570      ((null level) nil)
3571      ((zerop level) t)
3572      ((null refs) t)
3573      ((null (gnus-parent-id refs)) t)
3574      ((and (= 1 level)
3575            (null (setq particle (gnus-id-to-article
3576                                  (gnus-parent-id refs))))
3577            (null (gnus-summary-thread-level particle)))))))
3578
3579 (defun gnus-root-id (id)
3580   "Return the id of the root of the thread where ID appears."
3581   (let (last-id prev)
3582     (while (and id (setq prev (car (gnus-id-to-thread id))))
3583       (setq last-id id
3584             id (gnus-parent-id (mail-header-references prev))))
3585     last-id))
3586
3587 (defun gnus-articles-in-thread (thread)
3588   "Return the list of articles in THREAD."
3589   (cons (mail-header-number (car thread))
3590         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3591
3592 (defun gnus-remove-thread (id &optional dont-remove)
3593   "Remove the thread that has ID in it."
3594   (let (headers thread last-id)
3595     ;; First go up in this thread until we find the root.
3596     (setq last-id (gnus-root-id id)
3597           headers (message-flatten-list (gnus-id-to-thread last-id)))
3598     ;; We have now found the real root of this thread.  It might have
3599     ;; been gathered into some loose thread, so we have to search
3600     ;; through the threads to find the thread we wanted.
3601     (let ((threads gnus-newsgroup-threads)
3602           sub)
3603       (while threads
3604         (setq sub (car threads))
3605         (if (stringp (car sub))
3606             ;; This is a gathered thread, so we look at the roots
3607             ;; below it to find whether this article is in this
3608             ;; gathered root.
3609             (progn
3610               (setq sub (cdr sub))
3611               (while sub
3612                 (when (member (caar sub) headers)
3613                   (setq thread (car threads)
3614                         threads nil
3615                         sub nil))
3616                 (setq sub (cdr sub))))
3617           ;; It's an ordinary thread, so we check it.
3618           (when (eq (car sub) (car headers))
3619             (setq thread sub
3620                   threads nil)))
3621         (setq threads (cdr threads)))
3622       ;; If this article is in no thread, then it's a root.
3623       (if thread
3624           (unless dont-remove
3625             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3626         (setq thread (gnus-id-to-thread last-id)))
3627       (when thread
3628         (prog1
3629             thread                      ; We return this thread.
3630           (unless dont-remove
3631             (if (stringp (car thread))
3632                 (progn
3633                   ;; If we use dummy roots, then we have to remove the
3634                   ;; dummy root as well.
3635                   (when (eq gnus-summary-make-false-root 'dummy)
3636                     ;; We go to the dummy root by going to
3637                     ;; the first sub-"thread", and then one line up.
3638                     (gnus-summary-goto-article
3639                      (mail-header-number (caadr thread)))
3640                     (forward-line -1)
3641                     (gnus-delete-line)
3642                     (gnus-data-compute-positions))
3643                   (setq thread (cdr thread))
3644                   (while thread
3645                     (gnus-remove-thread-1 (car thread))
3646                     (setq thread (cdr thread))))
3647               (gnus-remove-thread-1 thread))))))))
3648
3649 (defun gnus-remove-thread-1 (thread)
3650   "Remove the thread THREAD recursively."
3651   (let ((number (mail-header-number (pop thread)))
3652         d)
3653     (setq thread (reverse thread))
3654     (while thread
3655       (gnus-remove-thread-1 (pop thread)))
3656     (when (setq d (gnus-data-find number))
3657       (goto-char (gnus-data-pos d))
3658       (gnus-summary-show-thread)
3659       (gnus-data-remove
3660        number
3661        (- (gnus-point-at-bol)
3662           (prog1
3663               (1+ (gnus-point-at-eol))
3664             (gnus-delete-line)))))))
3665
3666 (defun gnus-sort-threads (threads)
3667   "Sort THREADS."
3668   (if (not gnus-thread-sort-functions)
3669       threads
3670     (gnus-message 8 "Sorting threads...")
3671     (prog1
3672         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3673       (gnus-message 8 "Sorting threads...done"))))
3674
3675 (defun gnus-sort-articles (articles)
3676   "Sort ARTICLES."
3677   (when gnus-article-sort-functions
3678     (gnus-message 7 "Sorting articles...")
3679     (prog1
3680         (setq gnus-newsgroup-headers
3681               (sort articles (gnus-make-sort-function
3682                               gnus-article-sort-functions)))
3683       (gnus-message 7 "Sorting articles...done"))))
3684
3685 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3686 (defmacro gnus-thread-header (thread)
3687   ;; Return header of first article in THREAD.
3688   ;; Note that THREAD must never, ever be anything else than a variable -
3689   ;; using some other form will lead to serious barfage.
3690   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3691   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3692   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" 
3693         (vector thread) 2))
3694
3695 (defsubst gnus-article-sort-by-number (h1 h2)
3696   "Sort articles by article number."
3697   (< (mail-header-number h1)
3698      (mail-header-number h2)))
3699
3700 (defun gnus-thread-sort-by-number (h1 h2)
3701   "Sort threads by root article number."
3702   (gnus-article-sort-by-number
3703    (gnus-thread-header h1) (gnus-thread-header h2)))
3704
3705 (defsubst gnus-article-sort-by-lines (h1 h2)
3706   "Sort articles by article Lines header."
3707   (< (mail-header-lines h1)
3708      (mail-header-lines h2)))
3709
3710 (defun gnus-thread-sort-by-lines (h1 h2)
3711   "Sort threads by root article Lines header."
3712   (gnus-article-sort-by-lines
3713    (gnus-thread-header h1) (gnus-thread-header h2)))
3714
3715 (defsubst gnus-article-sort-by-chars (h1 h2)
3716   "Sort articles by octet length."
3717   (< (mail-header-chars h1)
3718      (mail-header-chars h2)))
3719
3720 (defun gnus-thread-sort-by-chars (h1 h2)
3721   "Sort threads by root article octet length."
3722   (gnus-article-sort-by-chars
3723    (gnus-thread-header h1) (gnus-thread-header h2)))
3724
3725 (defsubst gnus-article-sort-by-author (h1 h2)
3726   "Sort articles by root author."
3727   (string-lessp
3728    (let ((addr (car (mime-entity-read-field h1 'From))))
3729      (or (std11-full-name-string addr)
3730          (std11-address-string addr)
3731          ""))
3732    (let ((addr (car (mime-entity-read-field h2 'From))))
3733      (or (std11-full-name-string addr)
3734          (std11-address-string addr)
3735          ""))
3736    ))
3737
3738 (defun gnus-thread-sort-by-author (h1 h2)
3739   "Sort threads by root author."
3740   (gnus-article-sort-by-author
3741    (gnus-thread-header h1)  (gnus-thread-header h2)))
3742
3743 (defsubst gnus-article-sort-by-subject (h1 h2)
3744   "Sort articles by root subject."
3745   (string-lessp
3746    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3747    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3748
3749 (defun gnus-thread-sort-by-subject (h1 h2)
3750   "Sort threads by root subject."
3751   (gnus-article-sort-by-subject
3752    (gnus-thread-header h1) (gnus-thread-header h2)))
3753
3754 (defsubst gnus-article-sort-by-date (h1 h2)
3755   "Sort articles by root article date."
3756   (time-less-p
3757    (gnus-date-get-time (mail-header-date h1))
3758    (gnus-date-get-time (mail-header-date h2))))
3759
3760 (defun gnus-thread-sort-by-date (h1 h2)
3761   "Sort threads by root article date."
3762   (gnus-article-sort-by-date
3763    (gnus-thread-header h1) (gnus-thread-header h2)))
3764
3765 (defsubst gnus-article-sort-by-score (h1 h2)
3766   "Sort articles by root article score.
3767 Unscored articles will be counted as having a score of zero."
3768   (> (or (cdr (assq (mail-header-number h1)
3769                     gnus-newsgroup-scored))
3770          gnus-summary-default-score 0)
3771      (or (cdr (assq (mail-header-number h2)
3772                     gnus-newsgroup-scored))
3773          gnus-summary-default-score 0)))
3774
3775 (defun gnus-thread-sort-by-score (h1 h2)
3776   "Sort threads by root article score."
3777   (gnus-article-sort-by-score
3778    (gnus-thread-header h1) (gnus-thread-header h2)))
3779
3780 (defun gnus-thread-sort-by-total-score (h1 h2)
3781   "Sort threads by the sum of all scores in the thread.
3782 Unscored articles will be counted as having a score of zero."
3783   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3784
3785 (defun gnus-thread-total-score (thread)
3786   ;; This function find the total score of THREAD.
3787   (cond ((null thread)
3788          0)
3789         ((consp thread)
3790          (if (stringp (car thread))
3791              (apply gnus-thread-score-function 0
3792                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3793            (gnus-thread-total-score-1 thread)))
3794         (t
3795          (gnus-thread-total-score-1 (list thread)))))
3796
3797 (defun gnus-thread-total-score-1 (root)
3798   ;; This function find the total score of the thread below ROOT.
3799   (setq root (car root))
3800   (apply gnus-thread-score-function
3801          (or (append
3802               (mapcar 'gnus-thread-total-score
3803                       (cdr (gnus-id-to-thread (mail-header-id root))))
3804               (when (> (mail-header-number root) 0)
3805                 (list (or (cdr (assq (mail-header-number root)
3806                                      gnus-newsgroup-scored))
3807                           gnus-summary-default-score 0))))
3808              (list gnus-summary-default-score)
3809              '(0))))
3810
3811 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3812 (defvar gnus-tmp-prev-subject nil)
3813 (defvar gnus-tmp-false-parent nil)
3814 (defvar gnus-tmp-root-expunged nil)
3815 (defvar gnus-tmp-dummy-line nil)
3816
3817 (defvar gnus-tmp-header)
3818 (defun gnus-extra-header (type &optional header)
3819   "Return the extra header of TYPE."
3820   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3821       ""))
3822
3823 (defun gnus-summary-prepare-threads (threads)
3824   "Prepare summary buffer from THREADS and indentation LEVEL.
3825 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3826 or a straight list of headers."
3827   (gnus-message 7 "Generating summary...")
3828
3829   (setq gnus-newsgroup-threads threads)
3830   (beginning-of-line)
3831
3832   (let ((gnus-tmp-level 0)
3833         (default-score (or gnus-summary-default-score 0))
3834         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3835         thread number subject stack state gnus-tmp-gathered beg-match
3836         new-roots gnus-tmp-new-adopts thread-end
3837         gnus-tmp-header gnus-tmp-unread
3838         gnus-tmp-replied gnus-tmp-subject-or-nil
3839         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3840         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3841         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3842
3843     (setq gnus-tmp-prev-subject nil)
3844
3845     (if (vectorp (car threads))
3846         ;; If this is a straight (sic) list of headers, then a
3847         ;; threaded summary display isn't required, so we just create
3848         ;; an unthreaded one.
3849         (gnus-summary-prepare-unthreaded threads)
3850
3851       ;; Do the threaded display.
3852
3853       (while (or threads stack gnus-tmp-new-adopts new-roots)
3854
3855         (if (and (= gnus-tmp-level 0)
3856                  (or (not stack)
3857                      (= (caar stack) 0))
3858                  (not gnus-tmp-false-parent)
3859                  (or gnus-tmp-new-adopts new-roots))
3860             (if gnus-tmp-new-adopts
3861                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3862                       thread (list (car gnus-tmp-new-adopts))
3863                       gnus-tmp-header (caar thread)
3864                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3865               (when new-roots
3866                 (setq thread (list (car new-roots))
3867                       gnus-tmp-header (caar thread)
3868                       new-roots (cdr new-roots))))
3869
3870           (if threads
3871               ;; If there are some threads, we do them before the
3872               ;; threads on the stack.
3873               (setq thread threads
3874                     gnus-tmp-header (caar thread))
3875             ;; There were no current threads, so we pop something off
3876             ;; the stack.
3877             (setq state (car stack)
3878                   gnus-tmp-level (car state)
3879                   thread (cdr state)
3880                   stack (cdr stack)
3881                   gnus-tmp-header (caar thread))))
3882
3883         (setq gnus-tmp-false-parent nil)
3884         (setq gnus-tmp-root-expunged nil)
3885         (setq thread-end nil)
3886
3887         (if (stringp gnus-tmp-header)
3888             ;; The header is a dummy root.
3889             (cond
3890              ((eq gnus-summary-make-false-root 'adopt)
3891               ;; We let the first article adopt the rest.
3892               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3893                                                (cddar thread)))
3894               (setq gnus-tmp-gathered
3895                     (nconc (mapcar
3896                             (lambda (h) (mail-header-number (car h)))
3897                             (cddar thread))
3898                            gnus-tmp-gathered))
3899               (setq thread (cons (list (caar thread)
3900                                        (cadar thread))
3901                                  (cdr thread)))
3902               (setq gnus-tmp-level -1
3903                     gnus-tmp-false-parent t))
3904              ((eq gnus-summary-make-false-root 'empty)
3905               ;; We print adopted articles with empty subject fields.
3906               (setq gnus-tmp-gathered
3907                     (nconc (mapcar
3908                             (lambda (h) (mail-header-number (car h)))
3909                             (cddar thread))
3910                            gnus-tmp-gathered))
3911               (setq gnus-tmp-level -1))
3912              ((eq gnus-summary-make-false-root 'dummy)
3913               ;; We remember that we probably want to output a dummy
3914               ;; root.
3915               (setq gnus-tmp-dummy-line gnus-tmp-header)
3916               (setq gnus-tmp-prev-subject gnus-tmp-header))
3917              (t
3918               ;; We do not make a root for the gathered
3919               ;; sub-threads at all.
3920               (setq gnus-tmp-level -1)))
3921
3922           (setq number (mail-header-number gnus-tmp-header)
3923                 subject (mail-header-subject gnus-tmp-header))
3924
3925           (cond
3926            ;; If the thread has changed subject, we might want to make
3927            ;; this subthread into a root.
3928            ((and (null gnus-thread-ignore-subject)
3929                  (not (zerop gnus-tmp-level))
3930                  gnus-tmp-prev-subject
3931                  (not (inline
3932                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3933             (setq new-roots (nconc new-roots (list (car thread)))
3934                   thread-end t
3935                   gnus-tmp-header nil))
3936            ;; If the article lies outside the current limit,
3937            ;; then we do not display it.
3938            ((not (memq number gnus-newsgroup-limit))
3939             (setq gnus-tmp-gathered
3940                   (nconc (mapcar
3941                           (lambda (h) (mail-header-number (car h)))
3942                           (cdar thread))
3943                          gnus-tmp-gathered))
3944             (setq gnus-tmp-new-adopts (if (cdar thread)
3945                                           (append gnus-tmp-new-adopts
3946                                                   (cdar thread))
3947                                         gnus-tmp-new-adopts)
3948                   thread-end t
3949                   gnus-tmp-header nil)
3950             (when (zerop gnus-tmp-level)
3951               (setq gnus-tmp-root-expunged t)))
3952            ;; Perhaps this article is to be marked as read?
3953            ((and gnus-summary-mark-below
3954                  (< (or (cdr (assq number gnus-newsgroup-scored))
3955                         default-score)
3956                     gnus-summary-mark-below)
3957                  ;; Don't touch sparse articles.
3958                  (not (gnus-summary-article-sparse-p number))
3959                  (not (gnus-summary-article-ancient-p number)))
3960             (setq gnus-newsgroup-unreads
3961                   (delq number gnus-newsgroup-unreads))
3962             (if gnus-newsgroup-auto-expire
3963                 (push number gnus-newsgroup-expirable)
3964               (push (cons number gnus-low-score-mark)
3965                     gnus-newsgroup-reads))))
3966
3967           (when gnus-tmp-header
3968             ;; We may have an old dummy line to output before this
3969             ;; article.
3970             (when (and gnus-tmp-dummy-line
3971                        (gnus-subject-equal
3972                         gnus-tmp-dummy-line
3973                         (mail-header-subject gnus-tmp-header)))
3974               (gnus-summary-insert-dummy-line
3975                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3976               (setq gnus-tmp-dummy-line nil))
3977
3978             ;; Compute the mark.
3979             (setq gnus-tmp-unread (gnus-article-mark number))
3980
3981             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3982                                   gnus-tmp-header gnus-tmp-level)
3983                   gnus-newsgroup-data)
3984
3985             ;; Actually insert the line.
3986             (setq
3987              gnus-tmp-subject-or-nil
3988              (cond
3989               ((and gnus-thread-ignore-subject
3990                     gnus-tmp-prev-subject
3991                     (not (inline (gnus-subject-equal
3992                                   gnus-tmp-prev-subject subject))))
3993                subject)
3994               ((zerop gnus-tmp-level)
3995                (if (and (eq gnus-summary-make-false-root 'empty)
3996                         (memq number gnus-tmp-gathered)
3997                         gnus-tmp-prev-subject
3998                         (inline (gnus-subject-equal
3999                                  gnus-tmp-prev-subject subject)))
4000                    gnus-summary-same-subject
4001                  subject))
4002               (t gnus-summary-same-subject)))
4003             (if (and (eq gnus-summary-make-false-root 'adopt)
4004                      (= gnus-tmp-level 1)
4005                      (memq number gnus-tmp-gathered))
4006                 (setq gnus-tmp-opening-bracket ?\<
4007                       gnus-tmp-closing-bracket ?\>)
4008               (setq gnus-tmp-opening-bracket ?\[
4009                     gnus-tmp-closing-bracket ?\]))
4010             (setq
4011              gnus-tmp-indentation
4012              (aref gnus-thread-indent-array gnus-tmp-level)
4013              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4014              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4015                                 gnus-summary-default-score 0)
4016              gnus-tmp-score-char
4017              (if (or (null gnus-summary-default-score)
4018                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4019                          gnus-summary-zcore-fuzz))
4020                  ?  ;Whitespace
4021                (if (< gnus-tmp-score gnus-summary-default-score)
4022                    gnus-score-below-mark gnus-score-over-mark))
4023              gnus-tmp-replied
4024              (cond ((memq number gnus-newsgroup-processable)
4025                     gnus-process-mark)
4026                    ((memq number gnus-newsgroup-cached)
4027                     gnus-cached-mark)
4028                    ((memq number gnus-newsgroup-replied)
4029                     gnus-replied-mark)
4030                    ((memq number gnus-newsgroup-saved)
4031                     gnus-saved-mark)
4032                    (t gnus-unread-mark))
4033              gnus-tmp-from (mail-header-from gnus-tmp-header)
4034              gnus-tmp-name
4035              (cond
4036               ((string-match "<[^>]+> *$" gnus-tmp-from)
4037                (setq beg-match (match-beginning 0))
4038                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4039                         (substring gnus-tmp-from (1+ (match-beginning 0))
4040                                    (1- (match-end 0))))
4041                    (substring gnus-tmp-from 0 beg-match)))
4042               ((string-match "(.+)" gnus-tmp-from)
4043                (substring gnus-tmp-from
4044                           (1+ (match-beginning 0)) (1- (match-end 0))))
4045               (t gnus-tmp-from)))
4046             (when (string= gnus-tmp-name "")
4047               (setq gnus-tmp-name gnus-tmp-from))
4048             (unless (numberp gnus-tmp-lines)
4049               (setq gnus-tmp-lines 0))
4050             (gnus-put-text-property
4051              (point)
4052              (progn (eval gnus-summary-line-format-spec) (point))
4053              'gnus-number number)
4054             (when gnus-visual-p
4055               (forward-line -1)
4056               (gnus-run-hooks 'gnus-summary-update-hook)
4057               (forward-line 1))
4058
4059             (setq gnus-tmp-prev-subject subject)))
4060
4061         (when (nth 1 thread)
4062           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4063         (incf gnus-tmp-level)
4064         (setq threads (if thread-end nil (cdar thread)))
4065         (unless threads
4066           (setq gnus-tmp-level 0)))))
4067   (gnus-message 7 "Generating summary...done"))
4068
4069 (defun gnus-summary-prepare-unthreaded (headers)
4070   "Generate an unthreaded summary buffer based on HEADERS."
4071   (let (header number mark)
4072
4073     (beginning-of-line)
4074
4075     (while headers
4076       ;; We may have to root out some bad articles...
4077       (when (memq (setq number (mail-header-number
4078                                 (setq header (pop headers))))
4079                   gnus-newsgroup-limit)
4080         ;; Mark article as read when it has a low score.
4081         (when (and gnus-summary-mark-below
4082                    (< (or (cdr (assq number gnus-newsgroup-scored))
4083                           gnus-summary-default-score 0)
4084                       gnus-summary-mark-below)
4085                    (not (gnus-summary-article-ancient-p number)))
4086           (setq gnus-newsgroup-unreads
4087                 (delq number gnus-newsgroup-unreads))
4088           (if gnus-newsgroup-auto-expire
4089               (push number gnus-newsgroup-expirable)
4090             (push (cons number gnus-low-score-mark)
4091                   gnus-newsgroup-reads)))
4092
4093         (setq mark (gnus-article-mark number))
4094         (push (gnus-data-make number mark (1+ (point)) header 0)
4095               gnus-newsgroup-data)
4096         (gnus-summary-insert-line
4097          header 0 number
4098          mark (memq number gnus-newsgroup-replied)
4099          (memq number gnus-newsgroup-expirable)
4100          (mail-header-subject header) nil
4101          (cdr (assq number gnus-newsgroup-scored))
4102          (memq number gnus-newsgroup-processable))))))
4103
4104 (defun gnus-summary-remove-list-identifiers ()
4105   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4106   (let ((regexp (if (stringp gnus-list-identifiers)
4107                     gnus-list-identifiers
4108                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4109     (dolist (header gnus-newsgroup-headers)
4110       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4111                                   " *\\)\\)+\\(Re: +\\)?\\)")
4112                           (mail-header-subject header))
4113         (mail-header-set-subject
4114          header (concat (substring (mail-header-subject header)
4115                                    0 (match-beginning 1))
4116                         (or
4117                          (match-string 3 (mail-header-subject header))
4118                          (match-string 5 (mail-header-subject header)))
4119                         (substring (mail-header-subject header)
4120                                    (match-end 1))))))))
4121
4122 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4123   "Select newsgroup GROUP.
4124 If READ-ALL is non-nil, all articles in the group are selected.
4125 If SELECT-ARTICLES, only select those articles from GROUP."
4126   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4127          ;;!!! Dirty hack; should be removed.
4128          (gnus-summary-ignore-duplicates
4129           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4130               t
4131             gnus-summary-ignore-duplicates))
4132          (info (nth 2 entry))
4133          articles fetched-articles cached)
4134
4135     (unless (gnus-check-server
4136              (setq gnus-current-select-method
4137                    (gnus-find-method-for-group group)))
4138       (error "Couldn't open server"))
4139
4140     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4141         (gnus-activate-group group)     ; Or we can activate it...
4142         (progn                          ; Or we bug out.
4143           (when (equal major-mode 'gnus-summary-mode)
4144             (kill-buffer (current-buffer)))
4145           (error "Couldn't request group %s: %s"
4146                  group (gnus-status-message group))))
4147
4148     (unless (gnus-request-group group t)
4149       (when (equal major-mode 'gnus-summary-mode)
4150         (kill-buffer (current-buffer)))
4151       (error "Couldn't request group %s: %s"
4152              group (gnus-status-message group)))
4153
4154     (setq gnus-newsgroup-name group)
4155     (setq gnus-newsgroup-unselected nil)
4156     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4157     (gnus-summary-setup-default-charset)
4158
4159     ;; Adjust and set lists of article marks.
4160     (when info
4161       (gnus-adjust-marked-articles info))
4162
4163     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4164     (setq cached
4165           (if (gnus-virtual-group-p group)
4166               gnus-newsgroup-cached
4167             (gnus-cache-articles-in-group group)))
4168
4169     (setq gnus-newsgroup-unreads
4170           (gnus-set-difference
4171            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4172            gnus-newsgroup-dormant))
4173
4174     (setq gnus-newsgroup-processable nil)
4175
4176     (gnus-update-read-articles group gnus-newsgroup-unreads)
4177
4178     (if (setq articles select-articles)
4179         (setq gnus-newsgroup-unselected
4180               (gnus-sorted-intersection
4181                gnus-newsgroup-unreads
4182                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4183       (setq articles (gnus-articles-to-read group read-all)))
4184
4185     (cond
4186      ((null articles)
4187       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4188       'quit)
4189      ((eq articles 0) nil)
4190      (t
4191       ;; Init the dependencies hash table.
4192       (setq gnus-newsgroup-dependencies
4193             (gnus-make-hashtable (length articles)))
4194       (gnus-set-global-variables)
4195       ;; Retrieve the headers and read them in.
4196       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4197       (setq gnus-newsgroup-headers
4198             (gnus-retrieve-parsed-headers
4199              articles gnus-newsgroup-name
4200              ;; We might want to fetch old headers, but
4201              ;; not if there is only 1 article.
4202              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4203                            (not (numberp gnus-fetch-old-headers)))
4204                       (> (length articles) 1))
4205                   gnus-fetch-old-headers)))
4206       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4207
4208       ;; Suppress duplicates?
4209       (when gnus-suppress-duplicates
4210         (gnus-dup-suppress-articles))
4211
4212       ;; Set the initial limit.
4213       (setq gnus-newsgroup-limit (copy-sequence articles))
4214       ;; Remove canceled articles from the list of unread articles.
4215       (setq gnus-newsgroup-unreads
4216             (gnus-set-sorted-intersection
4217              gnus-newsgroup-unreads
4218              (setq fetched-articles
4219                    (mapcar (lambda (headers) (mail-header-number headers))
4220                            gnus-newsgroup-headers))))
4221       ;; Removed marked articles that do not exist.
4222       (gnus-update-missing-marks
4223        (gnus-sorted-complement fetched-articles articles))
4224
4225       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4226       (when cached
4227         (setq gnus-newsgroup-cached cached))
4228
4229       ;; We might want to build some more threads first.
4230       (when (and gnus-fetch-old-headers
4231                  (eq gnus-headers-retrieved-by 'nov))
4232         (if (eq gnus-fetch-old-headers 'invisible)
4233             (gnus-build-all-threads)
4234           (gnus-build-old-threads)))
4235       ;; Let the Gnus agent mark articles as read.
4236       (when gnus-agent
4237         (gnus-agent-get-undownloaded-list))
4238       ;; Remove list identifiers from subject
4239       (when gnus-list-identifiers
4240         (gnus-summary-remove-list-identifiers))
4241       ;; Check whether auto-expire is to be done in this group.
4242       (setq gnus-newsgroup-auto-expire
4243             (gnus-group-auto-expirable-p group))
4244       ;; Set up the article buffer now, if necessary.
4245       (unless gnus-single-article-buffer
4246         (gnus-article-setup-buffer))
4247       ;; First and last article in this newsgroup.
4248       (when gnus-newsgroup-headers
4249         (setq gnus-newsgroup-begin
4250               (mail-header-number (car gnus-newsgroup-headers))
4251               gnus-newsgroup-end
4252               (mail-header-number
4253                (gnus-last-element gnus-newsgroup-headers))))
4254       ;; GROUP is successfully selected.
4255       (or gnus-newsgroup-headers t)))))
4256
4257 (defun gnus-articles-to-read (group &optional read-all)
4258   ;; Find out what articles the user wants to read.
4259   (let* ((articles
4260           ;; Select all articles if `read-all' is non-nil, or if there
4261           ;; are no unread articles.
4262           (if (or read-all
4263                   (and (zerop (length gnus-newsgroup-marked))
4264                        (zerop (length gnus-newsgroup-unreads)))
4265                   (eq (gnus-group-find-parameter group 'display)
4266                       'all))
4267               (or
4268                (gnus-uncompress-range (gnus-active group))
4269                (gnus-cache-articles-in-group group))
4270             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4271                           (copy-sequence gnus-newsgroup-unreads))
4272                   '<)))
4273          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4274          (scored (length scored-list))
4275          (number (length articles))
4276          (marked (+ (length gnus-newsgroup-marked)
4277                     (length gnus-newsgroup-dormant)))
4278          (select
4279           (cond
4280            ((numberp read-all)
4281             read-all)
4282            (t
4283             (condition-case ()
4284                 (cond
4285                  ((and (or (<= scored marked) (= scored number))
4286                        (natnump gnus-large-newsgroup)
4287                        (> number gnus-large-newsgroup))
4288                   (let* ((cursor-in-echo-area nil)
4289                          (input (read-from-minibuffer
4290                                  (format
4291                                   "How many articles from %s (max %d): "
4292                                   (gnus-limit-string gnus-newsgroup-name 35)
4293                                   number)
4294                                  (cons (number-to-string gnus-large-newsgroup)
4295                                        0))))
4296                     (if (string-match "^[ \t]*$" input)
4297                         number
4298                       input)))
4299                  ((and (> scored marked) (< scored number)
4300                        (> (- scored number) 20))
4301                   (let ((input
4302                          (read-string
4303                           (format "%s %s (%d scored, %d total): "
4304                                   "How many articles from"
4305                                   group scored number))))
4306                     (if (string-match "^[ \t]*$" input)
4307                         number input)))
4308                  (t number))
4309               (quit nil))))))
4310     (setq select (if (stringp select) (string-to-number select) select))
4311     (if (or (null select) (zerop select))
4312         select
4313       (if (and (not (zerop scored)) (<= (abs select) scored))
4314           (progn
4315             (setq articles (sort scored-list '<))
4316             (setq number (length articles)))
4317         (setq articles (copy-sequence articles)))
4318
4319       (when (< (abs select) number)
4320         (if (< select 0)
4321             ;; Select the N oldest articles.
4322             (setcdr (nthcdr (1- (abs select)) articles) nil)
4323           ;; Select the N most recent articles.
4324           (setq articles (nthcdr (- number select) articles))))
4325       (setq gnus-newsgroup-unselected
4326             (gnus-sorted-intersection
4327              gnus-newsgroup-unreads
4328              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4329       (when gnus-alter-articles-to-read-function
4330         (setq gnus-newsgroup-unreads
4331               (sort 
4332                (funcall gnus-alter-articles-to-read-function
4333                         gnus-newsgroup-name gnus-newsgroup-unreads)
4334                '<)))
4335       articles)))
4336
4337 (defun gnus-killed-articles (killed articles)
4338   (let (out)
4339     (while articles
4340       (when (inline (gnus-member-of-range (car articles) killed))
4341         (push (car articles) out))
4342       (setq articles (cdr articles)))
4343     out))
4344
4345 (defun gnus-uncompress-marks (marks)
4346   "Uncompress the mark ranges in MARKS."
4347   (let ((uncompressed '(score bookmark))
4348         out)
4349     (while marks
4350       (if (memq (caar marks) uncompressed)
4351           (push (car marks) out)
4352         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4353       (setq marks (cdr marks)))
4354     out))
4355
4356 (defun gnus-adjust-marked-articles (info)
4357   "Set all article lists and remove all marks that are no longer valid."
4358   (let* ((marked-lists (gnus-info-marks info))
4359          (active (gnus-active (gnus-info-group info)))
4360          (min (car active))
4361          (max (cdr active))
4362          (types gnus-article-mark-lists)
4363          (uncompressed '(score bookmark killed))
4364          marks var articles article mark)
4365
4366     (while marked-lists
4367       (setq marks (pop marked-lists))
4368       (set (setq var (intern (format "gnus-newsgroup-%s"
4369                                      (car (rassq (setq mark (car marks))
4370                                                  types)))))
4371            (if (memq (car marks) uncompressed) (cdr marks)
4372              (gnus-uncompress-range (cdr marks))))
4373
4374       (setq articles (symbol-value var))
4375
4376       ;; All articles have to be subsets of the active articles.
4377       (cond
4378        ;; Adjust "simple" lists.
4379        ((memq mark '(tick dormant expire reply save))
4380         (while articles
4381           (when (or (< (setq article (pop articles)) min) (> article max))
4382             (set var (delq article (symbol-value var))))))
4383        ;; Adjust assocs.
4384        ((memq mark uncompressed)
4385         (when (not (listp (cdr (symbol-value var))))
4386           (set var (list (symbol-value var))))
4387         (when (not (listp (cdr articles)))
4388           (setq articles (list articles)))
4389         (while articles
4390           (when (or (not (consp (setq article (pop articles))))
4391                     (< (car article) min)
4392                     (> (car article) max))
4393             (set var (delq article (symbol-value var))))))))))
4394
4395 (defun gnus-update-missing-marks (missing)
4396   "Go through the list of MISSING articles and remove them from the mark lists."
4397   (when missing
4398     (let ((types gnus-article-mark-lists)
4399           var m)
4400       ;; Go through all types.
4401       (while types
4402         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4403         (when (symbol-value var)
4404           ;; This list has articles.  So we delete all missing articles
4405           ;; from it.
4406           (setq m missing)
4407           (while m
4408             (set var (delq (pop m) (symbol-value var)))))))))
4409
4410 (defun gnus-update-marks ()
4411   "Enter the various lists of marked articles into the newsgroup info list."
4412   (let ((types gnus-article-mark-lists)
4413         (info (gnus-get-info gnus-newsgroup-name))
4414         (uncompressed '(score bookmark killed))
4415         type list newmarked symbol delta-marks)
4416     (when info
4417       ;; Add all marks lists to the list of marks lists.
4418       (while (setq type (pop types))
4419         (setq list (symbol-value
4420                     (setq symbol
4421                           (intern (format "gnus-newsgroup-%s"
4422                                           (car type))))))
4423
4424         (when list
4425           ;; Get rid of the entries of the articles that have the
4426           ;; default score.
4427           (when (and (eq (cdr type) 'score)
4428                      gnus-save-score
4429                      list)
4430             (let* ((arts list)
4431                    (prev (cons nil list))
4432                    (all prev))
4433               (while arts
4434                 (if (or (not (consp (car arts)))
4435                         (= (cdar arts) gnus-summary-default-score))
4436                     (setcdr prev (cdr arts))
4437                   (setq prev arts))
4438                 (setq arts (cdr arts)))
4439               (setq list (cdr all)))))
4440
4441         (unless (memq (cdr type) uncompressed)
4442           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4443        
4444         (when (gnus-check-backend-function
4445                'request-set-mark gnus-newsgroup-name)
4446           ;; propagate flags to server, with the following exceptions:
4447           ;; uncompressed:s are not proper flags (they are cons cells)
4448           ;; cache is a internal gnus flag
4449           ;; download are local to one gnus installation (well)
4450           ;; unsend are for nndraft groups only
4451           ;; xxx: generality of this?  this suits nnimap anyway
4452           (unless (memq (cdr type) (append '(cache download unsend)
4453                                            uncompressed))
4454             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4455                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4456                    (add (gnus-remove-from-range
4457                          (gnus-copy-sequence list) old)))
4458               (when add
4459                 (push (list add 'add (list (cdr type))) delta-marks))
4460               (when del
4461                 (push (list del 'del (list (cdr type))) delta-marks)))))
4462           
4463         (when list
4464           (push (cons (cdr type) list) newmarked)))
4465
4466       (when delta-marks
4467         (unless (gnus-check-group gnus-newsgroup-name)
4468           (error "Can't open server for %s" gnus-newsgroup-name))
4469         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4470           
4471       ;; Enter these new marks into the info of the group.
4472       (if (nthcdr 3 info)
4473           (setcar (nthcdr 3 info) newmarked)
4474         ;; Add the marks lists to the end of the info.
4475         (when newmarked
4476           (setcdr (nthcdr 2 info) (list newmarked))))
4477
4478       ;; Cut off the end of the info if there's nothing else there.
4479       (let ((i 5))
4480         (while (and (> i 2)
4481                     (not (nth i info)))
4482           (when (nthcdr (decf i) info)
4483             (setcdr (nthcdr i info) nil)))))))
4484
4485 (defun gnus-set-mode-line (where)
4486   "This function sets the mode line of the article or summary buffers.
4487 If WHERE is `summary', the summary mode line format will be used."
4488   ;; Is this mode line one we keep updated?
4489   (when (and (memq where gnus-updated-mode-lines)
4490              (symbol-value
4491               (intern (format "gnus-%s-mode-line-format-spec" where))))
4492     (let (mode-string)
4493       (save-excursion
4494         ;; We evaluate this in the summary buffer since these
4495         ;; variables are buffer-local to that buffer.
4496         (set-buffer gnus-summary-buffer)
4497         ;; We bind all these variables that are used in the `eval' form
4498         ;; below.
4499         (let* ((mformat (symbol-value
4500                          (intern
4501                           (format "gnus-%s-mode-line-format-spec" where))))
4502                (gnus-tmp-group-name (gnus-group-name-decode 
4503                                      gnus-newsgroup-name
4504                                      (gnus-group-name-charset 
4505                                       nil
4506                                       gnus-newsgroup-name)))
4507                (gnus-tmp-article-number (or gnus-current-article 0))
4508                (gnus-tmp-unread gnus-newsgroup-unreads)
4509                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4510                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4511                (gnus-tmp-unread-and-unselected
4512                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4513                             (zerop gnus-tmp-unselected))
4514                        "")
4515                       ((zerop gnus-tmp-unselected)
4516                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4517                       (t (format "{%d(+%d) more}"
4518                                  gnus-tmp-unread-and-unticked
4519                                  gnus-tmp-unselected))))
4520                (gnus-tmp-subject
4521                 (if (and gnus-current-headers
4522                          (vectorp gnus-current-headers))
4523                     (gnus-mode-string-quote
4524                      (mail-header-subject gnus-current-headers))
4525                   ""))
4526                bufname-length max-len
4527                gnus-tmp-header);; passed as argument to any user-format-funcs
4528           (setq mode-string (eval mformat))
4529           (setq bufname-length (if (string-match "%b" mode-string)
4530                                    (- (length
4531                                        (buffer-name
4532                                         (if (eq where 'summary)
4533                                             nil
4534                                           (get-buffer gnus-article-buffer))))
4535                                       2)
4536                                  0))
4537           (setq max-len (max 4 (if gnus-mode-non-string-length
4538                                    (- (window-width)
4539                                       gnus-mode-non-string-length
4540                                       bufname-length)
4541                                  (length mode-string))))
4542           ;; We might have to chop a bit of the string off...
4543           (when (> (length mode-string) max-len)
4544             (setq mode-string
4545                   (concat (gnus-truncate-string mode-string (- max-len 3))
4546                           "...")))
4547           ;; Pad the mode string a bit.
4548           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4549       ;; Update the mode line.
4550       (setq mode-line-buffer-identification
4551             (gnus-mode-line-buffer-identification (list mode-string)))
4552       (set-buffer-modified-p t))))
4553
4554 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4555   "Go through the HEADERS list and add all Xrefs to a hash table.
4556 The resulting hash table is returned, or nil if no Xrefs were found."
4557   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4558          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4559          (xref-hashtb (gnus-make-hashtable))
4560          start group entry number xrefs header)
4561     (while headers
4562       (setq header (pop headers))
4563       (when (and (setq xrefs (mail-header-xref header))
4564                  (not (memq (setq number (mail-header-number header))
4565                             unreads)))
4566         (setq start 0)
4567         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4568           (setq start (match-end 0))
4569           (setq group (if prefix
4570                           (concat prefix (substring xrefs (match-beginning 1)
4571                                                     (match-end 1)))
4572                         (substring xrefs (match-beginning 1) (match-end 1))))
4573           (setq number
4574                 (string-to-int (substring xrefs (match-beginning 2)
4575                                           (match-end 2))))
4576           (if (setq entry (gnus-gethash group xref-hashtb))
4577               (setcdr entry (cons number (cdr entry)))
4578             (gnus-sethash group (cons number nil) xref-hashtb)))))
4579     (and start xref-hashtb)))
4580
4581 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4582   "Look through all the headers and mark the Xrefs as read."
4583   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4584         name entry info xref-hashtb idlist method nth4)
4585     (save-excursion
4586       (set-buffer gnus-group-buffer)
4587       (when (setq xref-hashtb
4588                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4589         (mapatoms
4590          (lambda (group)
4591            (unless (string= from-newsgroup (setq name (symbol-name group)))
4592              (setq idlist (symbol-value group))
4593              ;; Dead groups are not updated.
4594              (and (prog1
4595                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4596                             info (nth 2 entry))
4597                     (when (stringp (setq nth4 (gnus-info-method info)))
4598                       (setq nth4 (gnus-server-to-method nth4))))
4599                   ;; Only do the xrefs if the group has the same
4600                   ;; select method as the group we have just read.
4601                   (or (gnus-methods-equal-p
4602                        nth4 (gnus-find-method-for-group from-newsgroup))
4603                       virtual
4604                       (equal nth4 (setq method (gnus-find-method-for-group
4605                                                 from-newsgroup)))
4606                       (and (equal (car nth4) (car method))
4607                            (equal (nth 1 nth4) (nth 1 method))))
4608                   gnus-use-cross-reference
4609                   (or (not (eq gnus-use-cross-reference t))
4610                       virtual
4611                       ;; Only do cross-references on subscribed
4612                       ;; groups, if that is what is wanted.
4613                       (<= (gnus-info-level info) gnus-level-subscribed))
4614                   (gnus-group-make-articles-read name idlist))))
4615          xref-hashtb)))))
4616
4617 (defun gnus-compute-read-articles (group articles)
4618   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4619          (info (nth 2 entry))
4620          (active (gnus-active group))
4621          ninfo)
4622     (when entry
4623       ;; First peel off all invalid article numbers.
4624       (when active
4625         (let ((ids articles)
4626               id first)
4627           (while (setq id (pop ids))
4628             (when (and first (> id (cdr active)))
4629               ;; We'll end up in this situation in one particular
4630               ;; obscure situation.  If you re-scan a group and get
4631               ;; a new article that is cross-posted to a different
4632               ;; group that has not been re-scanned, you might get
4633               ;; crossposted article that has a higher number than
4634               ;; Gnus believes possible.  So we re-activate this
4635               ;; group as well.  This might mean doing the
4636               ;; crossposting thingy will *increase* the number
4637               ;; of articles in some groups.  Tsk, tsk.
4638               (setq active (or (gnus-activate-group group) active)))
4639             (when (or (> id (cdr active))
4640                       (< id (car active)))
4641               (setq articles (delq id articles))))))
4642       ;; If the read list is nil, we init it.
4643       (if (and active
4644                (null (gnus-info-read info))
4645                (> (car active) 1))
4646           (setq ninfo (cons 1 (1- (car active))))
4647         (setq ninfo (gnus-info-read info)))
4648       ;; Then we add the read articles to the range.
4649       (gnus-add-to-range
4650        ninfo (setq articles (sort articles '<))))))
4651
4652 (defun gnus-group-make-articles-read (group articles)
4653   "Update the info of GROUP to say that ARTICLES are read."
4654   (let* ((num 0)
4655          (entry (gnus-gethash group gnus-newsrc-hashtb))
4656          (info (nth 2 entry))
4657          (active (gnus-active group))
4658          range)
4659     (when entry
4660       (setq range (gnus-compute-read-articles group articles))
4661       (save-excursion
4662         (set-buffer gnus-group-buffer)
4663         (gnus-undo-register
4664           `(progn
4665              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4666              (gnus-info-set-read ',info ',(gnus-info-read info))
4667              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4668              (gnus-group-update-group ,group t))))
4669       ;; Add the read articles to the range.
4670       (gnus-info-set-read info range)
4671       ;; Then we have to re-compute how many unread
4672       ;; articles there are in this group.
4673       (when active
4674         (cond
4675          ((not range)
4676           (setq num (- (1+ (cdr active)) (car active))))
4677          ((not (listp (cdr range)))
4678           (setq num (- (cdr active) (- (1+ (cdr range))
4679                                        (car range)))))
4680          (t
4681           (while range
4682             (if (numberp (car range))
4683                 (setq num (1+ num))
4684               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4685             (setq range (cdr range)))
4686           (setq num (- (cdr active) num))))
4687         ;; Update the number of unread articles.
4688         (setcar entry num)
4689         ;; Update the group buffer.
4690         (gnus-group-update-group group t)))))
4691
4692 (defvar gnus-newsgroup-none-id 0)
4693
4694 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4695   (let ((cur nntp-server-buffer)
4696         (dependencies
4697          (or dependencies
4698              (save-excursion (set-buffer gnus-summary-buffer)
4699                              gnus-newsgroup-dependencies)))
4700         headers id end ref
4701         (mail-parse-charset gnus-newsgroup-charset)
4702         (mail-parse-ignored-charsets
4703          (save-excursion (condition-case nil
4704                              (set-buffer gnus-summary-buffer)
4705                            (error))
4706                          gnus-newsgroup-ignored-charsets)))
4707     (save-excursion
4708       (set-buffer nntp-server-buffer)
4709       ;; Translate all TAB characters into SPACE characters.
4710       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4711       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4712       (gnus-run-hooks 'gnus-parse-headers-hook)
4713       (let ((case-fold-search t)
4714             in-reply-to header p lines chars ctype)
4715         (goto-char (point-min))
4716         ;; Search to the beginning of the next header.  Error messages
4717         ;; do not begin with 2 or 3.
4718         (while (re-search-forward "^[23][0-9]+ " nil t)
4719           (setq id nil
4720                 ref nil)
4721           ;; This implementation of this function, with nine
4722           ;; search-forwards instead of the one re-search-forward and
4723           ;; a case (which basically was the old function) is actually
4724           ;; about twice as fast, even though it looks messier.  You
4725           ;; can't have everything, I guess.  Speed and elegance
4726           ;; doesn't always go hand in hand.
4727           (setq
4728            header
4729            (make-full-mail-header
4730             ;; Number.
4731             (prog1
4732                 (read cur)
4733               (end-of-line)
4734               (setq p (point))
4735               (narrow-to-region (point)
4736                                 (or (and (search-forward "\n.\n" nil t)
4737                                          (- (point) 2))
4738                                     (point))))
4739             ;; Subject.
4740             (progn
4741               (goto-char p)
4742               (if (search-forward "\nsubject: " nil t)
4743                   (buffer-substring (match-end 0) (std11-field-end))
4744                 "(none)"))
4745             ;; From.
4746             (progn
4747               (goto-char p)
4748               (if (search-forward "\nfrom: " nil t)
4749                   (buffer-substring (match-end 0) (std11-field-end))
4750                 "(nobody)"))
4751             ;; Date.
4752             (progn
4753               (goto-char p)
4754               (if (search-forward "\ndate: " nil t)
4755                   (buffer-substring (match-end 0) (std11-field-end))
4756                 ""))
4757             ;; Message-ID.
4758             (progn
4759               (goto-char p)
4760               (setq id (if (re-search-forward
4761                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4762                            ;; We do it this way to make sure the Message-ID
4763                            ;; is (somewhat) syntactically valid.
4764                            (buffer-substring (match-beginning 1)
4765                                              (match-end 1))
4766                          ;; If there was no message-id, we just fake one
4767                          ;; to make subsequent routines simpler.
4768                          (nnheader-generate-fake-message-id))))
4769             ;; References.
4770             (progn
4771               (goto-char p)
4772               (if (search-forward "\nreferences: " nil t)
4773                   (progn
4774                     (setq end (point))
4775                     (prog1
4776                         (buffer-substring (match-end 0) (std11-field-end))
4777                       (setq ref
4778                             (buffer-substring
4779                              (progn
4780                                ;; (end-of-line)
4781                                (search-backward ">" end t)
4782                                (1+ (point)))
4783                              (progn
4784                                (search-backward "<" end t)
4785                                (point))))))
4786                 ;; Get the references from the in-reply-to header if there
4787                 ;; were no references and the in-reply-to header looks
4788                 ;; promising.
4789                 (if (and (search-forward "\nin-reply-to: " nil t)
4790                          (setq in-reply-to
4791                                (buffer-substring (match-end 0)
4792                                                  (std11-field-end)))
4793                          (string-match "<[^>]+>" in-reply-to))
4794                     (let (ref2)
4795                       (setq ref (substring in-reply-to (match-beginning 0)
4796                                            (match-end 0)))
4797                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4798                         (setq ref2 (substring in-reply-to (match-beginning 0)
4799                                               (match-end 0)))
4800                         (when (> (length ref2) (length ref))
4801                           (setq ref ref2)))
4802                       ref)
4803                   (setq ref nil))))
4804             ;; Chars.
4805             (progn
4806               (goto-char p)
4807               (if (search-forward "\nchars: " nil t)
4808                   (if (numberp (setq chars (ignore-errors (read cur))))
4809                       chars 0)
4810                 0))
4811             ;; Lines.
4812             (progn
4813               (goto-char p)
4814               (if (search-forward "\nlines: " nil t)
4815                   (if (numberp (setq lines (ignore-errors (read cur))))
4816                       lines 0)
4817                 0))
4818             ;; Xref.
4819             (progn
4820               (goto-char p)
4821               (and (search-forward "\nxref: " nil t)
4822                    (buffer-substring (match-end 0) (std11-field-end))))
4823             ;; Extra.
4824             (when gnus-extra-headers
4825               (let ((extra gnus-extra-headers)
4826                     out)
4827                 (while extra
4828                   (goto-char p)
4829                   (when (search-forward
4830                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4831                     (push (cons (car extra)
4832                                 (buffer-substring (match-end 0)
4833                                                   (std11-field-end)))
4834                           out))
4835                   (pop extra))
4836                 out))))
4837           (goto-char p)
4838           (if (and (search-forward "\ncontent-type: " nil t)
4839                    (setq ctype
4840                          (buffer-substring (match-end 0) (std11-field-end))))
4841               (mime-entity-set-content-type-internal
4842                header (mime-parse-Content-Type ctype)))
4843           (when (equal id ref)
4844             (setq ref nil))
4845
4846           (when gnus-alter-header-function
4847             (funcall gnus-alter-header-function header)
4848             (setq id (mail-header-id header)
4849                   ref (gnus-parent-id (mail-header-references header))))
4850
4851           (when (setq header
4852                       (gnus-dependencies-add-header
4853                        header dependencies force-new))
4854             (push header headers))
4855           (goto-char (point-max))
4856           (widen))
4857         (nreverse headers)))))
4858
4859 ;; Goes through the xover lines and returns a list of vectors
4860 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4861                                                   force-new dependencies
4862                                                   group also-fetch-heads)
4863   "Parse the news overview data in the server buffer, and return a
4864 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4865   ;; Get the Xref when the users reads the articles since most/some
4866   ;; NNTP servers do not include Xrefs when using XOVER.
4867   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4868   (let ((mail-parse-charset gnus-newsgroup-charset)
4869         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4870         (cur nntp-server-buffer)
4871         (dependencies (or dependencies gnus-newsgroup-dependencies))
4872         number headers header)
4873     (save-excursion
4874       (set-buffer nntp-server-buffer)
4875       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4876       ;; Allow the user to mangle the headers before parsing them.
4877       (gnus-run-hooks 'gnus-parse-headers-hook)
4878       (goto-char (point-min))
4879       (while (not (eobp))
4880         (condition-case ()
4881             (while (and sequence (not (eobp)))
4882               (setq number (read cur))
4883               (while (and sequence
4884                           (< (car sequence) number))
4885                 (setq sequence (cdr sequence)))
4886               (and sequence
4887                    (eq number (car sequence))
4888                    (progn
4889                      (setq sequence (cdr sequence))
4890                      (setq header (inline
4891                                     (gnus-nov-parse-line
4892                                      number dependencies force-new))))
4893                    (push header headers))
4894               (forward-line 1))
4895           (error
4896            (gnus-error 4 "Strange nov line (%d)"
4897                        (count-lines (point-min) (point)))))
4898         (forward-line 1))
4899       ;; A common bug in inn is that if you have posted an article and
4900       ;; then retrieves the active file, it will answer correctly --
4901       ;; the new article is included.  However, a NOV entry for the
4902       ;; article may not have been generated yet, so this may fail.
4903       ;; We work around this problem by retrieving the last few
4904       ;; headers using HEAD.
4905       (if (or (not also-fetch-heads)
4906               (not sequence))
4907           ;; We (probably) got all the headers.
4908           (nreverse headers)
4909         (let ((gnus-nov-is-evil t))
4910           (nconc
4911            (nreverse headers)
4912            (gnus-retrieve-parsed-headers sequence group)
4913            ))))))
4914
4915 (defun gnus-article-get-xrefs ()
4916   "Fill in the Xref value in `gnus-current-headers', if necessary.
4917 This is meant to be called in `gnus-article-internal-prepare-hook'."
4918   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4919                                  gnus-current-headers)))
4920     (or (not gnus-use-cross-reference)
4921         (not headers)
4922         (and (mail-header-xref headers)
4923              (not (string= (mail-header-xref headers) "")))
4924         (let ((case-fold-search t)
4925               xref)
4926           (save-restriction
4927             (nnheader-narrow-to-headers)
4928             (goto-char (point-min))
4929             (when (or (and (not (eobp))
4930                            (eq (downcase (char-after)) ?x)
4931                            (looking-at "Xref:"))
4932                       (search-forward "\nXref:" nil t))
4933               (goto-char (1+ (match-end 0)))
4934               (setq xref (buffer-substring (point)
4935                                            (progn (end-of-line) (point))))
4936               (mail-header-set-xref headers xref)))))))
4937
4938 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4939   "Find article ID and insert the summary line for that article.
4940 OLD-HEADER can either be a header or a line number to insert
4941 the subject line on."
4942   (let* ((line (and (numberp old-header) old-header))
4943          (old-header (and (vectorp old-header) old-header))
4944          (header (cond ((and old-header use-old-header)
4945                         old-header)
4946                        ((and (numberp id)
4947                              (gnus-number-to-header id))
4948                         (gnus-number-to-header id))
4949                        (t
4950                         (gnus-read-header id))))
4951          (number (and (numberp id) id))
4952          d)
4953     (when header
4954       ;; Rebuild the thread that this article is part of and go to the
4955       ;; article we have fetched.
4956       (when (and (not gnus-show-threads)
4957                  old-header)
4958         (when (and number
4959                    (setq d (gnus-data-find (mail-header-number old-header))))
4960           (goto-char (gnus-data-pos d))
4961           (gnus-data-remove
4962            number
4963            (- (gnus-point-at-bol)
4964               (prog1
4965                   (1+ (gnus-point-at-eol))
4966                 (gnus-delete-line))))))
4967       (when old-header
4968         (mail-header-set-number header (mail-header-number old-header)))
4969       (setq gnus-newsgroup-sparse
4970             (delq (setq number (mail-header-number header))
4971                   gnus-newsgroup-sparse))
4972       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4973       (push number gnus-newsgroup-limit)
4974       (gnus-rebuild-thread (mail-header-id header) line)
4975       (gnus-summary-goto-subject number nil t))
4976     (when (and (numberp number)
4977                (> number 0))
4978       ;; We have to update the boundaries even if we can't fetch the
4979       ;; article if ID is a number -- so that the next `P' or `N'
4980       ;; command will fetch the previous (or next) article even
4981       ;; if the one we tried to fetch this time has been canceled.
4982       (when (> number gnus-newsgroup-end)
4983         (setq gnus-newsgroup-end number))
4984       (when (< number gnus-newsgroup-begin)
4985         (setq gnus-newsgroup-begin number))
4986       (setq gnus-newsgroup-unselected
4987             (delq number gnus-newsgroup-unselected)))
4988     ;; Report back a success?
4989     (and header (mail-header-number header))))
4990
4991 ;;; Process/prefix in the summary buffer
4992
4993 (defun gnus-summary-work-articles (n)
4994   "Return a list of articles to be worked upon.
4995 The prefix argument, the list of process marked articles, and the
4996 current article will be taken into consideration."
4997   (save-excursion
4998     (set-buffer gnus-summary-buffer)
4999     (cond
5000      (n
5001       ;; A numerical prefix has been given.
5002       (setq n (prefix-numeric-value n))
5003       (let ((backward (< n 0))
5004             (n (abs (prefix-numeric-value n)))
5005             articles article)
5006         (save-excursion
5007           (while
5008               (and (> n 0)
5009                    (push (setq article (gnus-summary-article-number))
5010                          articles)
5011                    (if backward
5012                        (gnus-summary-find-prev nil article)
5013                      (gnus-summary-find-next nil article)))
5014             (decf n)))
5015         (nreverse articles)))
5016      ((and (gnus-region-active-p) (mark))
5017       (message "region active")
5018       ;; Work on the region between point and mark.
5019       (let ((max (max (point) (mark)))
5020             articles article)
5021         (save-excursion
5022           (goto-char (min (point) (mark)))
5023           (while
5024               (and
5025                (push (setq article (gnus-summary-article-number)) articles)
5026                (gnus-summary-find-next nil article)
5027                (< (point) max)))
5028           (nreverse articles))))
5029      (gnus-newsgroup-processable
5030       ;; There are process-marked articles present.
5031       ;; Save current state.
5032       (gnus-summary-save-process-mark)
5033       ;; Return the list.
5034       (reverse gnus-newsgroup-processable))
5035      (t
5036       ;; Just return the current article.
5037       (list (gnus-summary-article-number))))))
5038
5039 (defmacro gnus-summary-iterate (arg &rest forms)
5040   "Iterate over the process/prefixed articles and do FORMS.
5041 ARG is the interactive prefix given to the command.  FORMS will be
5042 executed with point over the summary line of the articles."
5043   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5044     `(let ((,articles (gnus-summary-work-articles ,arg)))
5045        (while ,articles
5046          (gnus-summary-goto-subject (car ,articles))
5047          ,@forms
5048          (pop ,articles)))))
5049
5050 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5051 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5052
5053 (defun gnus-summary-save-process-mark ()
5054   "Push the current set of process marked articles on the stack."
5055   (interactive)
5056   (push (copy-sequence gnus-newsgroup-processable)
5057         gnus-newsgroup-process-stack))
5058
5059 (defun gnus-summary-kill-process-mark ()
5060   "Push the current set of process marked articles on the stack and unmark."
5061   (interactive)
5062   (gnus-summary-save-process-mark)
5063   (gnus-summary-unmark-all-processable))
5064
5065 (defun gnus-summary-yank-process-mark ()
5066   "Pop the last process mark state off the stack and restore it."
5067   (interactive)
5068   (unless gnus-newsgroup-process-stack
5069     (error "Empty mark stack"))
5070   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5071
5072 (defun gnus-summary-process-mark-set (set)
5073   "Make SET into the current process marked articles."
5074   (gnus-summary-unmark-all-processable)
5075   (while set
5076     (gnus-summary-set-process-mark (pop set))))
5077
5078 ;;; Searching and stuff
5079
5080 (defun gnus-summary-search-group (&optional backward use-level)
5081   "Search for next unread newsgroup.
5082 If optional argument BACKWARD is non-nil, search backward instead."
5083   (save-excursion
5084     (set-buffer gnus-group-buffer)
5085     (when (gnus-group-search-forward
5086            backward nil (if use-level (gnus-group-group-level) nil))
5087       (gnus-group-group-name))))
5088
5089 (defun gnus-summary-best-group (&optional exclude-group)
5090   "Find the name of the best unread group.
5091 If EXCLUDE-GROUP, do not go to this group."
5092   (save-excursion
5093     (set-buffer gnus-group-buffer)
5094     (save-excursion
5095       (gnus-group-best-unread-group exclude-group))))
5096
5097 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5098   (if backward (gnus-summary-find-prev)
5099     (let* ((dummy (gnus-summary-article-intangible-p))
5100            (article (or article (gnus-summary-article-number)))
5101            (arts (gnus-data-find-list article))
5102            result)
5103       (when (and (not dummy)
5104                  (or (not gnus-summary-check-current)
5105                      (not unread)
5106                      (not (gnus-data-unread-p (car arts)))))
5107         (setq arts (cdr arts)))
5108       (when (setq result
5109                   (if unread
5110                       (progn
5111                         (while arts
5112                           (when (or (and undownloaded
5113                                          (eq gnus-undownloaded-mark
5114                                              (gnus-data-mark (car arts))))
5115                                     (gnus-data-unread-p (car arts)))
5116                             (setq result (car arts)
5117                                   arts nil))
5118                           (setq arts (cdr arts)))
5119                         result)
5120                     (car arts)))
5121         (goto-char (gnus-data-pos result))
5122         (gnus-data-number result)))))
5123
5124 (defun gnus-summary-find-prev (&optional unread article)
5125   (let* ((eobp (eobp))
5126          (article (or article (gnus-summary-article-number)))
5127          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5128          result)
5129     (when (and (not eobp)
5130                (or (not gnus-summary-check-current)
5131                    (not unread)
5132                    (not (gnus-data-unread-p (car arts)))))
5133       (setq arts (cdr arts)))
5134     (when (setq result
5135                 (if unread
5136                     (progn
5137                       (while arts
5138                         (when (gnus-data-unread-p (car arts))
5139                           (setq result (car arts)
5140                                 arts nil))
5141                         (setq arts (cdr arts)))
5142                       result)
5143                   (car arts)))
5144       (goto-char (gnus-data-pos result))
5145       (gnus-data-number result))))
5146
5147 (defun gnus-summary-find-subject (subject &optional unread backward article)
5148   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5149          (article (or article (gnus-summary-article-number)))
5150          (articles (gnus-data-list backward))
5151          (arts (gnus-data-find-list article articles))
5152          result)
5153     (when (or (not gnus-summary-check-current)
5154               (not unread)
5155               (not (gnus-data-unread-p (car arts))))
5156       (setq arts (cdr arts)))
5157     (while arts
5158       (and (or (not unread)
5159                (gnus-data-unread-p (car arts)))
5160            (vectorp (gnus-data-header (car arts)))
5161            (gnus-subject-equal
5162             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5163            (setq result (car arts)
5164                  arts nil))
5165       (setq arts (cdr arts)))
5166     (and result
5167          (goto-char (gnus-data-pos result))
5168          (gnus-data-number result))))
5169
5170 (defun gnus-summary-search-forward (&optional unread subject backward)
5171   "Search forward for an article.
5172 If UNREAD, look for unread articles.  If SUBJECT, look for
5173 articles with that subject.  If BACKWARD, search backward instead."
5174   (cond (subject (gnus-summary-find-subject subject unread backward))
5175         (backward (gnus-summary-find-prev unread))
5176         (t (gnus-summary-find-next unread))))
5177
5178 (defun gnus-recenter (&optional n)
5179   "Center point in window and redisplay frame.
5180 Also do horizontal recentering."
5181   (interactive "P")
5182   (when (and gnus-auto-center-summary
5183              (not (eq gnus-auto-center-summary 'vertical)))
5184     (gnus-horizontal-recenter))
5185   (recenter n))
5186
5187 (defun gnus-summary-recenter ()
5188   "Center point in the summary window.
5189 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5190 displayed, no centering will be performed."
5191   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5192   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5193   (interactive)
5194   (let* ((top (cond ((< (window-height) 4) 0)
5195                     ((< (window-height) 7) 1)
5196                     (t (if (numberp gnus-auto-center-summary)
5197                            gnus-auto-center-summary
5198                          2))))
5199          (height (1- (window-height)))
5200          (bottom (save-excursion (goto-char (point-max))
5201                                  (forward-line (- height))
5202                                  (point)))
5203          (window (get-buffer-window (current-buffer))))
5204     ;; The user has to want it.
5205     (when gnus-auto-center-summary
5206       (when (get-buffer-window gnus-article-buffer)
5207         ;; Only do recentering when the article buffer is displayed,
5208         ;; Set the window start to either `bottom', which is the biggest
5209         ;; possible valid number, or the second line from the top,
5210         ;; whichever is the least.
5211         (set-window-start
5212          window (min bottom (save-excursion
5213                               (forward-line (- top)) (point)))
5214          t))
5215       ;; Do horizontal recentering while we're at it.
5216       (when (and (get-buffer-window (current-buffer) t)
5217                  (not (eq gnus-auto-center-summary 'vertical)))
5218         (let ((selected (selected-window)))
5219           (select-window (get-buffer-window (current-buffer) t))
5220           (gnus-summary-position-point)
5221           (gnus-horizontal-recenter)
5222           (select-window selected))))))
5223
5224 (defun gnus-summary-jump-to-group (newsgroup)
5225   "Move point to NEWSGROUP in group mode buffer."
5226   ;; Keep update point of group mode buffer if visible.
5227   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5228       (save-window-excursion
5229         ;; Take care of tree window mode.
5230         (when (get-buffer-window gnus-group-buffer)
5231           (pop-to-buffer gnus-group-buffer))
5232         (gnus-group-jump-to-group newsgroup))
5233     (save-excursion
5234       ;; Take care of tree window mode.
5235       (if (get-buffer-window gnus-group-buffer)
5236           (pop-to-buffer gnus-group-buffer)
5237         (set-buffer gnus-group-buffer))
5238       (gnus-group-jump-to-group newsgroup))))
5239
5240 ;; This function returns a list of article numbers based on the
5241 ;; difference between the ranges of read articles in this group and
5242 ;; the range of active articles.
5243 (defun gnus-list-of-unread-articles (group)
5244   (let* ((read (gnus-info-read (gnus-get-info group)))
5245          (active (or (gnus-active group) (gnus-activate-group group)))
5246          (last (cdr active))
5247          first nlast unread)
5248     ;; If none are read, then all are unread.
5249     (if (not read)
5250         (setq first (car active))
5251       ;; If the range of read articles is a single range, then the
5252       ;; first unread article is the article after the last read
5253       ;; article.  Sounds logical, doesn't it?
5254       (if (and (not (listp (cdr read)))
5255                (or (< (car read) (car active))
5256                    (progn (setq read (list read))
5257                           nil)))
5258           (setq first (max (car active) (1+ (cdr read))))
5259         ;; `read' is a list of ranges.
5260         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5261                                   (caar read)))
5262                   1)
5263           (setq first (car active)))
5264         (while read
5265           (when first
5266             (while (< first nlast)
5267               (push first unread)
5268               (setq first (1+ first))))
5269           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5270           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5271           (setq read (cdr read)))))
5272     ;; And add the last unread articles.
5273     (while (<= first last)
5274       (push first unread)
5275       (setq first (1+ first)))
5276     ;; Return the list of unread articles.
5277     (delq 0 (nreverse unread))))
5278
5279 (defun gnus-list-of-read-articles (group)
5280   "Return a list of unread, unticked and non-dormant articles."
5281   (let* ((info (gnus-get-info group))
5282          (marked (gnus-info-marks info))
5283          (active (gnus-active group)))
5284     (and info active
5285          (gnus-set-difference
5286           (gnus-sorted-complement
5287            (gnus-uncompress-range active)
5288            (gnus-list-of-unread-articles group))
5289           (append
5290            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5291            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5292
5293 ;; Various summary commands
5294
5295 (defun gnus-summary-select-article-buffer ()
5296   "Reconfigure windows to show article buffer."
5297   (interactive)
5298   (if (not (gnus-buffer-live-p gnus-article-buffer))
5299       (error "There is no article buffer for this summary buffer")
5300     (gnus-configure-windows 'article)
5301     (select-window (get-buffer-window gnus-article-buffer))))
5302
5303 (defun gnus-summary-universal-argument (arg)
5304   "Perform any operation on all articles that are process/prefixed."
5305   (interactive "P")
5306   (let ((articles (gnus-summary-work-articles arg))
5307         func article)
5308     (if (eq
5309          (setq
5310           func
5311           (key-binding
5312            (read-key-sequence
5313             (substitute-command-keys
5314              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5315          'undefined)
5316         (gnus-error 1 "Undefined key")
5317       (save-excursion
5318         (while articles
5319           (gnus-summary-goto-subject (setq article (pop articles)))
5320           (let (gnus-newsgroup-processable)
5321             (command-execute func))
5322           (gnus-summary-remove-process-mark article)))))
5323   (gnus-summary-position-point))
5324
5325 (defun gnus-summary-toggle-truncation (&optional arg)
5326   "Toggle truncation of summary lines.
5327 With arg, turn line truncation on iff arg is positive."
5328   (interactive "P")
5329   (setq truncate-lines
5330         (if (null arg) (not truncate-lines)
5331           (> (prefix-numeric-value arg) 0)))
5332   (redraw-display))
5333
5334 (defun gnus-summary-reselect-current-group (&optional all rescan)
5335   "Exit and then reselect the current newsgroup.
5336 The prefix argument ALL means to select all articles."
5337   (interactive "P")
5338   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5339     (error "Ephemeral groups can't be reselected"))
5340   (let ((current-subject (gnus-summary-article-number))
5341         (group gnus-newsgroup-name))
5342     (setq gnus-newsgroup-begin nil)
5343     (gnus-summary-exit)
5344     ;; We have to adjust the point of group mode buffer because
5345     ;; point was moved to the next unread newsgroup by exiting.
5346     (gnus-summary-jump-to-group group)
5347     (when rescan
5348       (save-excursion
5349         (save-window-excursion
5350           ;; Don't show group contents.
5351           (set-window-start (selected-window) (point-max))
5352           (gnus-group-get-new-news-this-group 1))))
5353     (gnus-group-read-group all t)
5354     (gnus-summary-goto-subject current-subject nil t)))
5355
5356 (defun gnus-summary-rescan-group (&optional all)
5357   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5358   (interactive "P")
5359   (gnus-summary-reselect-current-group all t))
5360
5361 (defun gnus-summary-update-info (&optional non-destructive)
5362   (save-excursion
5363     (let ((group gnus-newsgroup-name))
5364       (when group
5365         (when gnus-newsgroup-kill-headers
5366           (setq gnus-newsgroup-killed
5367                 (gnus-compress-sequence
5368                  (nconc
5369                   (gnus-set-sorted-intersection
5370                    (gnus-uncompress-range gnus-newsgroup-killed)
5371                    (setq gnus-newsgroup-unselected
5372                          (sort gnus-newsgroup-unselected '<)))
5373                   (setq gnus-newsgroup-unreads
5374                         (sort gnus-newsgroup-unreads '<)))
5375                  t)))
5376         (unless (listp (cdr gnus-newsgroup-killed))
5377           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5378         (let ((headers gnus-newsgroup-headers))
5379           ;; Set the new ranges of read articles.
5380           (save-excursion
5381             (set-buffer gnus-group-buffer)
5382             (gnus-undo-force-boundary))
5383           (gnus-update-read-articles
5384            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5385           ;; Set the current article marks.
5386           (let ((gnus-newsgroup-scored
5387                  (if (and (not gnus-save-score)
5388                           (not non-destructive))
5389                      nil
5390                    gnus-newsgroup-scored)))
5391             (save-excursion
5392               (gnus-update-marks)))
5393           ;; Do the cross-ref thing.
5394           (when gnus-use-cross-reference
5395             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5396           ;; Do not switch windows but change the buffer to work.
5397           (set-buffer gnus-group-buffer)
5398           (unless (gnus-ephemeral-group-p group)
5399             (gnus-group-update-group group)))))))
5400
5401 (defun gnus-summary-save-newsrc (&optional force)
5402   "Save the current number of read/marked articles in the dribble buffer.
5403 The dribble buffer will then be saved.
5404 If FORCE (the prefix), also save the .newsrc file(s)."
5405   (interactive "P")
5406   (gnus-summary-update-info t)
5407   (if force
5408       (gnus-save-newsrc-file)
5409     (gnus-dribble-save)))
5410
5411 (defun gnus-summary-exit (&optional temporary)
5412   "Exit reading current newsgroup, and then return to group selection mode.
5413 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5414   (interactive)
5415   (gnus-set-global-variables)
5416   (gnus-kill-save-kill-buffer)
5417   (gnus-async-halt-prefetch)
5418   (let* ((group gnus-newsgroup-name)
5419          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5420          (mode major-mode)
5421          (group-point nil)
5422          (buf (current-buffer)))
5423     (unless quit-config
5424       ;; Do adaptive scoring, and possibly save score files.
5425       (when gnus-newsgroup-adaptive
5426         (gnus-score-adaptive))
5427       (when gnus-use-scoring
5428         (gnus-score-save)))
5429     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5430     ;; If we have several article buffers, we kill them at exit.
5431     (unless gnus-single-article-buffer
5432       (gnus-kill-buffer gnus-original-article-buffer)
5433       (setq gnus-article-current nil))
5434     (when gnus-use-cache
5435       (gnus-cache-possibly-remove-articles)
5436       (gnus-cache-save-buffers))
5437     (gnus-async-prefetch-remove-group group)
5438     (when gnus-suppress-duplicates
5439       (gnus-dup-enter-articles))
5440     (when gnus-use-trees
5441       (gnus-tree-close group))
5442     (when gnus-use-cache
5443       (gnus-cache-write-active))
5444     ;; Remove entries for this group.
5445     (nnmail-purge-split-history (gnus-group-real-name group))
5446     ;; Make all changes in this group permanent.
5447     (unless quit-config
5448       (gnus-run-hooks 'gnus-exit-group-hook)
5449       (gnus-summary-update-info))
5450     (gnus-close-group group)
5451     ;; Make sure where we were, and go to next newsgroup.
5452     (set-buffer gnus-group-buffer)
5453     (unless quit-config
5454       (gnus-group-jump-to-group group))
5455     (gnus-run-hooks 'gnus-summary-exit-hook)
5456     (unless (or quit-config
5457                 ;; If this group has disappeared from the summary
5458                 ;; buffer, don't skip forwards.
5459                 (not (string= group (gnus-group-group-name))))
5460       (gnus-group-next-unread-group 1))
5461     (setq group-point (point))
5462     (if temporary
5463         nil                             ;Nothing to do.
5464       ;; If we have several article buffers, we kill them at exit.
5465       (unless gnus-single-article-buffer
5466         (gnus-kill-buffer gnus-article-buffer)
5467         (gnus-kill-buffer gnus-original-article-buffer)
5468         (setq gnus-article-current nil))
5469       (set-buffer buf)
5470       (if (not gnus-kill-summary-on-exit)
5471           (gnus-deaden-summary)
5472         ;; We set all buffer-local variables to nil.  It is unclear why
5473         ;; this is needed, but if we don't, buffer-local variables are
5474         ;; not garbage-collected, it seems.  This would the lead to en
5475         ;; ever-growing Emacs.
5476         (gnus-summary-clear-local-variables)
5477         (when (get-buffer gnus-article-buffer)
5478           (bury-buffer gnus-article-buffer))
5479         ;; We clear the global counterparts of the buffer-local
5480         ;; variables as well, just to be on the safe side.
5481         (set-buffer gnus-group-buffer)
5482         (gnus-summary-clear-local-variables)
5483         ;; Return to group mode buffer.
5484         (when (eq mode 'gnus-summary-mode)
5485           (gnus-kill-buffer buf)))
5486       (setq gnus-current-select-method gnus-select-method)
5487       (pop-to-buffer gnus-group-buffer)
5488       (if (not quit-config)
5489           (progn
5490             (goto-char group-point)
5491             (gnus-configure-windows 'group 'force)
5492             (unless (pos-visible-in-window-p)
5493               (forward-line (/ (static-if (featurep 'xemacs)
5494                                    (window-displayed-height)
5495                                  (1- (window-height)))
5496                                -2))
5497               (set-window-start (selected-window) (point))
5498               (goto-char group-point)))
5499         (gnus-handle-ephemeral-exit quit-config))
5500       ;; Clear the current group name.
5501       (unless quit-config
5502         (setq gnus-newsgroup-name nil)))))
5503
5504 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5505 (defun gnus-summary-exit-no-update (&optional no-questions)
5506   "Quit reading current newsgroup without updating read article info."
5507   (interactive)
5508   (let* ((group gnus-newsgroup-name)
5509          (quit-config (gnus-group-quit-config group)))
5510     (when (or no-questions
5511               gnus-expert-user
5512               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5513       (gnus-async-halt-prefetch)
5514       (mapcar 'funcall
5515               (delq 'gnus-summary-expire-articles
5516                     (copy-sequence gnus-summary-prepare-exit-hook)))
5517       ;; If we have several article buffers, we kill them at exit.
5518       (unless gnus-single-article-buffer
5519         (gnus-kill-buffer gnus-article-buffer)
5520         (gnus-kill-buffer gnus-original-article-buffer)
5521         (setq gnus-article-current nil))
5522       (if (not gnus-kill-summary-on-exit)
5523           (gnus-deaden-summary)
5524         (gnus-close-group group)
5525         (gnus-summary-clear-local-variables)
5526         (set-buffer gnus-group-buffer)
5527         (gnus-summary-clear-local-variables)
5528         (when (get-buffer gnus-summary-buffer)
5529           (kill-buffer gnus-summary-buffer)))
5530       (unless gnus-single-article-buffer
5531         (setq gnus-article-current nil))
5532       (when gnus-use-trees
5533         (gnus-tree-close group))
5534       (gnus-async-prefetch-remove-group group)
5535       (when (get-buffer gnus-article-buffer)
5536         (bury-buffer gnus-article-buffer))
5537       ;; Return to the group buffer.
5538       (gnus-configure-windows 'group 'force)
5539       ;; Clear the current group name.
5540       (setq gnus-newsgroup-name nil)
5541       (when (equal (gnus-group-group-name) group)
5542         (gnus-group-next-unread-group 1))
5543       (when quit-config
5544         (gnus-handle-ephemeral-exit quit-config)))))
5545
5546 (defun gnus-handle-ephemeral-exit (quit-config)
5547   "Handle movement when leaving an ephemeral group.
5548 The state which existed when entering the ephemeral is reset."
5549   (if (not (buffer-name (car quit-config)))
5550       (gnus-configure-windows 'group 'force)
5551     (set-buffer (car quit-config))
5552     (cond ((eq major-mode 'gnus-summary-mode)
5553            (gnus-set-global-variables))
5554           ((eq major-mode 'gnus-article-mode)
5555            (save-excursion
5556              ;; The `gnus-summary-buffer' variable may point
5557              ;; to the old summary buffer when using a single
5558              ;; article buffer.
5559              (unless (gnus-buffer-live-p gnus-summary-buffer)
5560                (set-buffer gnus-group-buffer))
5561              (set-buffer gnus-summary-buffer)
5562              (gnus-set-global-variables))))
5563     (if (or (eq (cdr quit-config) 'article)
5564             (eq (cdr quit-config) 'pick))
5565         (progn
5566           ;; The current article may be from the ephemeral group
5567           ;; thus it is best that we reload this article
5568           (gnus-summary-show-article)
5569           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5570               (gnus-configure-windows 'pick 'force)
5571             (gnus-configure-windows (cdr quit-config) 'force)))
5572       (gnus-configure-windows (cdr quit-config) 'force))
5573     (when (eq major-mode 'gnus-summary-mode)
5574       (gnus-summary-next-subject 1 nil t)
5575       (gnus-summary-recenter)
5576       (gnus-summary-position-point))))
5577
5578 (defun gnus-summary-preview-mime-message ()
5579   "MIME decode and play this message."
5580   (interactive)
5581   (let ((gnus-break-pages nil)
5582         (gnus-show-mime t))
5583     (gnus-summary-select-article gnus-show-all-headers t))
5584   (select-window (get-buffer-window gnus-article-buffer)))
5585
5586 ;;; Dead summaries.
5587
5588 (defvar gnus-dead-summary-mode-map nil)
5589
5590 (unless gnus-dead-summary-mode-map
5591   (setq gnus-dead-summary-mode-map (make-keymap))
5592   (suppress-keymap gnus-dead-summary-mode-map)
5593   (substitute-key-definition
5594    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5595   (let ((keys '("\C-d" "\r" "\177" [delete])))
5596     (while keys
5597       (define-key gnus-dead-summary-mode-map
5598         (pop keys) 'gnus-summary-wake-up-the-dead))))
5599
5600 (defvar gnus-dead-summary-mode nil
5601   "Minor mode for Gnus summary buffers.")
5602
5603 (defun gnus-dead-summary-mode (&optional arg)
5604   "Minor mode for Gnus summary buffers."
5605   (interactive "P")
5606   (when (eq major-mode 'gnus-summary-mode)
5607     (make-local-variable 'gnus-dead-summary-mode)
5608     (setq gnus-dead-summary-mode
5609           (if (null arg) (not gnus-dead-summary-mode)
5610             (> (prefix-numeric-value arg) 0)))
5611     (when gnus-dead-summary-mode
5612       (gnus-add-minor-mode
5613        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5614
5615 (defun gnus-deaden-summary ()
5616   "Make the current summary buffer into a dead summary buffer."
5617   ;; Kill any previous dead summary buffer.
5618   (when (and gnus-dead-summary
5619              (buffer-name gnus-dead-summary))
5620     (save-excursion
5621       (set-buffer gnus-dead-summary)
5622       (when gnus-dead-summary-mode
5623         (kill-buffer (current-buffer)))))
5624   ;; Make this the current dead summary.
5625   (setq gnus-dead-summary (current-buffer))
5626   (gnus-dead-summary-mode 1)
5627   (let ((name (buffer-name)))
5628     (when (string-match "Summary" name)
5629       (rename-buffer
5630        (concat (substring name 0 (match-beginning 0)) "Dead "
5631                (substring name (match-beginning 0)))
5632        t)
5633       (bury-buffer))))
5634
5635 (defun gnus-kill-or-deaden-summary (buffer)
5636   "Kill or deaden the summary BUFFER."
5637   (save-excursion
5638     (when (and (buffer-name buffer)
5639                (not gnus-single-article-buffer))
5640       (save-excursion
5641         (set-buffer buffer)
5642         (gnus-kill-buffer gnus-article-buffer)
5643         (gnus-kill-buffer gnus-original-article-buffer)))
5644     (cond (gnus-kill-summary-on-exit
5645            (when (and gnus-use-trees
5646                       (gnus-buffer-exists-p buffer))
5647              (save-excursion
5648                (set-buffer buffer)
5649                (gnus-tree-close gnus-newsgroup-name)))
5650            (gnus-kill-buffer buffer))
5651           ((gnus-buffer-exists-p buffer)
5652            (save-excursion
5653              (set-buffer buffer)
5654              (gnus-deaden-summary))))))
5655
5656 (defun gnus-summary-wake-up-the-dead (&rest args)
5657   "Wake up the dead summary buffer."
5658   (interactive)
5659   (gnus-dead-summary-mode -1)
5660   (let ((name (buffer-name)))
5661     (when (string-match "Dead " name)
5662       (rename-buffer
5663        (concat (substring name 0 (match-beginning 0))
5664                (substring name (match-end 0)))
5665        t)))
5666   (gnus-message 3 "This dead summary is now alive again"))
5667
5668 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5669 (defun gnus-summary-fetch-faq (&optional faq-dir)
5670   "Fetch the FAQ for the current group.
5671 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5672 in."
5673   (interactive
5674    (list
5675     (when current-prefix-arg
5676       (completing-read
5677        "Faq dir: " (and (listp gnus-group-faq-directory)
5678                         (mapcar (lambda (file) (list file))
5679                                 gnus-group-faq-directory))))))
5680   (let (gnus-faq-buffer)
5681     (when (setq gnus-faq-buffer
5682                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5683       (gnus-configure-windows 'summary-faq))))
5684
5685 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5686 (defun gnus-summary-describe-group (&optional force)
5687   "Describe the current newsgroup."
5688   (interactive "P")
5689   (gnus-group-describe-group force gnus-newsgroup-name))
5690
5691 (defun gnus-summary-describe-briefly ()
5692   "Describe summary mode commands briefly."
5693   (interactive)
5694   (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")))
5695
5696 ;; Walking around group mode buffer from summary mode.
5697
5698 (defun gnus-summary-next-group (&optional no-article target-group backward)
5699   "Exit current newsgroup and then select next unread newsgroup.
5700 If prefix argument NO-ARTICLE is non-nil, no article is selected
5701 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5702 previous group instead."
5703   (interactive "P")
5704   ;; Stop pre-fetching.
5705   (gnus-async-halt-prefetch)
5706   (let ((current-group gnus-newsgroup-name)
5707         (current-buffer (current-buffer))
5708         entered)
5709     ;; First we semi-exit this group to update Xrefs and all variables.
5710     ;; We can't do a real exit, because the window conf must remain
5711     ;; the same in case the user is prompted for info, and we don't
5712     ;; want the window conf to change before that...
5713     (gnus-summary-exit t)
5714     (while (not entered)
5715       ;; Then we find what group we are supposed to enter.
5716       (set-buffer gnus-group-buffer)
5717       (gnus-group-jump-to-group current-group)
5718       (setq target-group
5719             (or target-group
5720                 (if (eq gnus-keep-same-level 'best)
5721                     (gnus-summary-best-group gnus-newsgroup-name)
5722                   (gnus-summary-search-group backward gnus-keep-same-level))))
5723       (if (not target-group)
5724           ;; There are no further groups, so we return to the group
5725           ;; buffer.
5726           (progn
5727             (gnus-message 5 "Returning to the group buffer")
5728             (setq entered t)
5729             (when (gnus-buffer-live-p current-buffer)
5730               (set-buffer current-buffer)
5731               (gnus-summary-exit))
5732             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5733         ;; We try to enter the target group.
5734         (gnus-group-jump-to-group target-group)
5735         (let ((unreads (gnus-group-group-unread)))
5736           (if (and (or (eq t unreads)
5737                        (and unreads (not (zerop unreads))))
5738                    (gnus-summary-read-group
5739                     target-group nil no-article
5740                     (and (buffer-name current-buffer) current-buffer)
5741                     nil backward))
5742               (setq entered t)
5743             (setq current-group target-group
5744                   target-group nil)))))))
5745
5746 (defun gnus-summary-prev-group (&optional no-article)
5747   "Exit current newsgroup and then select previous unread newsgroup.
5748 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5749   (interactive "P")
5750   (gnus-summary-next-group no-article nil t))
5751
5752 ;; Walking around summary lines.
5753
5754 (defun gnus-summary-first-subject (&optional unread undownloaded)
5755   "Go to the first unread subject.
5756 If UNREAD is non-nil, go to the first unread article.
5757 Returns the article selected or nil if there are no unread articles."
5758   (interactive "P")
5759   (prog1
5760       (cond
5761        ;; Empty summary.
5762        ((null gnus-newsgroup-data)
5763         (gnus-message 3 "No articles in the group")
5764         nil)
5765        ;; Pick the first article.
5766        ((not unread)
5767         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5768         (gnus-data-number (car gnus-newsgroup-data)))
5769        ;; No unread articles.
5770        ((null gnus-newsgroup-unreads)
5771         (gnus-message 3 "No more unread articles")
5772         nil)
5773        ;; Find the first unread article.
5774        (t
5775         (let ((data gnus-newsgroup-data))
5776           (while (and data
5777                       (and (not (and undownloaded
5778                                      (eq gnus-undownloaded-mark
5779                                          (gnus-data-mark (car data)))))
5780                            (not (gnus-data-unread-p (car data)))))
5781             (setq data (cdr data)))
5782           (when data
5783             (goto-char (gnus-data-pos (car data)))
5784             (gnus-data-number (car data))))))
5785     (gnus-summary-position-point)))
5786
5787 (defun gnus-summary-next-subject (n &optional unread dont-display)
5788   "Go to next N'th summary line.
5789 If N is negative, go to the previous N'th subject line.
5790 If UNREAD is non-nil, only unread articles are selected.
5791 The difference between N and the actual number of steps taken is
5792 returned."
5793   (interactive "p")
5794   (let ((backward (< n 0))
5795         (n (abs n)))
5796     (while (and (> n 0)
5797                 (if backward
5798                     (gnus-summary-find-prev unread)
5799                   (gnus-summary-find-next unread)))
5800       (unless (zerop (setq n (1- n)))
5801         (gnus-summary-show-thread)))
5802     (when (/= 0 n)
5803       (gnus-message 7 "No more%s articles"
5804                     (if unread " unread" "")))
5805     (unless dont-display
5806       (gnus-summary-recenter)
5807       (gnus-summary-position-point))
5808     n))
5809
5810 (defun gnus-summary-next-unread-subject (n)
5811   "Go to next N'th unread summary line."
5812   (interactive "p")
5813   (gnus-summary-next-subject n t))
5814
5815 (defun gnus-summary-prev-subject (n &optional unread)
5816   "Go to previous N'th summary line.
5817 If optional argument UNREAD is non-nil, only unread article is selected."
5818   (interactive "p")
5819   (gnus-summary-next-subject (- n) unread))
5820
5821 (defun gnus-summary-prev-unread-subject (n)
5822   "Go to previous N'th unread summary line."
5823   (interactive "p")
5824   (gnus-summary-next-subject (- n) t))
5825
5826 (defun gnus-summary-goto-subject (article &optional force silent)
5827   "Go the subject line of ARTICLE.
5828 If FORCE, also allow jumping to articles not currently shown."
5829   (interactive "nArticle number: ")
5830   (let ((b (point))
5831         (data (gnus-data-find article)))
5832     ;; We read in the article if we have to.
5833     (and (not data)
5834          force
5835          (gnus-summary-insert-subject
5836           article
5837           (if (or (numberp force) (vectorp force)) force)
5838           t)
5839          (setq data (gnus-data-find article)))
5840     (goto-char b)
5841     (if (not data)
5842         (progn
5843           (unless silent
5844             (gnus-message 3 "Can't find article %d" article))
5845           nil)
5846       (goto-char (gnus-data-pos data))
5847       (gnus-summary-position-point)
5848       article)))
5849
5850 ;; Walking around summary lines with displaying articles.
5851
5852 (defun gnus-summary-expand-window (&optional arg)
5853   "Make the summary buffer take up the entire Emacs frame.
5854 Given a prefix, will force an `article' buffer configuration."
5855   (interactive "P")
5856   (if arg
5857       (gnus-configure-windows 'article 'force)
5858     (gnus-configure-windows 'summary 'force)))
5859
5860 (defun gnus-summary-display-article (article &optional all-header)
5861   "Display ARTICLE in article buffer."
5862   (gnus-set-global-variables)
5863   (if (null article)
5864       nil
5865     (prog1
5866         (if gnus-summary-display-article-function
5867             (funcall gnus-summary-display-article-function article all-header)
5868           (gnus-article-prepare article all-header))
5869       (with-current-buffer gnus-article-buffer
5870         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5871              nil))
5872       (gnus-run-hooks 'gnus-select-article-hook)
5873       (when (and gnus-current-article
5874                  (not (zerop gnus-current-article)))
5875         (gnus-summary-goto-subject gnus-current-article))
5876       (gnus-summary-recenter)
5877       (when (and gnus-use-trees gnus-show-threads)
5878         (gnus-possibly-generate-tree article)
5879         (gnus-highlight-selected-tree article))
5880       ;; Successfully display article.
5881       (gnus-article-set-window-start
5882        (cdr (assq article gnus-newsgroup-bookmarks))))))
5883
5884 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5885   "Select the current article.
5886 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5887 non-nil, the article will be re-fetched even if it already present in
5888 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5889 be displayed."
5890   ;; Make sure we are in the summary buffer to work around bbdb bug.
5891   (unless (eq major-mode 'gnus-summary-mode)
5892     (set-buffer gnus-summary-buffer))
5893   (let ((article (or article (gnus-summary-article-number)))
5894         (all-headers (not (not all-headers))) ;Must be T or NIL.
5895         gnus-summary-display-article-function)
5896     (and (not pseudo)
5897          (gnus-summary-article-pseudo-p article)
5898          (error "This is a pseudo-article"))
5899     (save-excursion
5900       (set-buffer gnus-summary-buffer)
5901       (if (or (and gnus-single-article-buffer
5902                    (or (null gnus-current-article)
5903                        (null gnus-article-current)
5904                        (null (get-buffer gnus-article-buffer))
5905                        (not (eq article (cdr gnus-article-current)))
5906                        (not (equal (car gnus-article-current)
5907                                    gnus-newsgroup-name))))
5908               (and (not gnus-single-article-buffer)
5909                    (or (null gnus-current-article)
5910                        (not (eq gnus-current-article article))))
5911               force)
5912           ;; The requested article is different from the current article.
5913           (progn
5914             (gnus-summary-display-article article all-headers)
5915             (when (or all-headers gnus-show-all-headers)
5916               (gnus-article-show-all-headers))
5917             (gnus-article-set-window-start
5918              (cdr (assq article gnus-newsgroup-bookmarks)))
5919             article)
5920         (when (or all-headers gnus-show-all-headers)
5921           (gnus-article-show-all-headers))
5922         'old))))
5923
5924 (defun gnus-summary-set-current-mark (&optional current-mark)
5925   "Obsolete function."
5926   nil)
5927
5928 (defun gnus-summary-next-article (&optional unread subject backward push)
5929   "Select the next article.
5930 If UNREAD, only unread articles are selected.
5931 If SUBJECT, only articles with SUBJECT are selected.
5932 If BACKWARD, the previous article is selected instead of the next."
5933   (interactive "P")
5934   (cond
5935    ;; Is there such an article?
5936    ((and (gnus-summary-search-forward unread subject backward)
5937          (or (gnus-summary-display-article (gnus-summary-article-number))
5938              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5939     (gnus-summary-position-point))
5940    ;; If not, we try the first unread, if that is wanted.
5941    ((and subject
5942          gnus-auto-select-same
5943          (gnus-summary-first-unread-article))
5944     (gnus-summary-position-point)
5945     (gnus-message 6 "Wrapped"))
5946    ;; Try to get next/previous article not displayed in this group.
5947    ((and gnus-auto-extend-newsgroup
5948          (not unread) (not subject))
5949     (gnus-summary-goto-article
5950      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5951      nil (count-lines (point-min) (point))))
5952    ;; Go to next/previous group.
5953    (t
5954     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5955       (gnus-summary-jump-to-group gnus-newsgroup-name))
5956     (let ((cmd last-command-char)
5957           (point
5958            (save-excursion
5959              (set-buffer gnus-group-buffer)
5960              (point)))
5961           (group
5962            (if (eq gnus-keep-same-level 'best)
5963                (gnus-summary-best-group gnus-newsgroup-name)
5964              (gnus-summary-search-group backward gnus-keep-same-level))))
5965       ;; For some reason, the group window gets selected.  We change
5966       ;; it back.
5967       (select-window (get-buffer-window (current-buffer)))
5968       ;; Select next unread newsgroup automagically.
5969       (cond
5970        ((or (not gnus-auto-select-next)
5971             (not cmd))
5972         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5973        ((or (eq gnus-auto-select-next 'quietly)
5974             (and (eq gnus-auto-select-next 'slightly-quietly)
5975                  push)
5976             (and (eq gnus-auto-select-next 'almost-quietly)
5977                  (gnus-summary-last-article-p)))
5978         ;; Select quietly.
5979         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5980             (gnus-summary-exit)
5981           (gnus-message 7 "No more%s articles (%s)..."
5982                         (if unread " unread" "")
5983                         (if group (concat "selecting " group)
5984                           "exiting"))
5985           (gnus-summary-next-group nil group backward)))
5986        (t
5987         (when (gnus-key-press-event-p last-input-event)
5988           (gnus-summary-walk-group-buffer
5989            gnus-newsgroup-name cmd unread backward point))))))))
5990
5991 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5992   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5993                       (?\C-p (gnus-group-prev-unread-group 1))))
5994         (cursor-in-echo-area t)
5995         keve key group ended)
5996     (save-excursion
5997       (set-buffer gnus-group-buffer)
5998       (goto-char start)
5999       (setq group
6000             (if (eq gnus-keep-same-level 'best)
6001                 (gnus-summary-best-group gnus-newsgroup-name)
6002               (gnus-summary-search-group backward gnus-keep-same-level))))
6003     (while (not ended)
6004       (gnus-message
6005        5 "No more%s articles%s" (if unread " unread" "")
6006        (if (and group
6007                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6008            (format " (Type %s for %s [%s])"
6009                    (single-key-description cmd) group
6010                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6011          (format " (Type %s to exit %s)"
6012                  (single-key-description cmd)
6013                  gnus-newsgroup-name)))
6014       ;; Confirm auto selection.
6015       (setq key (car (setq keve (gnus-read-event-char))))
6016       (setq ended t)
6017       (cond
6018        ((assq key keystrokes)
6019         (let ((obuf (current-buffer)))
6020           (switch-to-buffer gnus-group-buffer)
6021           (when group
6022             (gnus-group-jump-to-group group))
6023           (eval (cadr (assq key keystrokes)))
6024           (setq group (gnus-group-group-name))
6025           (switch-to-buffer obuf))
6026         (setq ended nil))
6027        ((equal key cmd)
6028         (if (or (not group)
6029                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6030             (gnus-summary-exit)
6031           (gnus-summary-next-group nil group backward)))
6032        (t
6033         (push (cdr keve) unread-command-events))))))
6034
6035 (defun gnus-summary-next-unread-article ()
6036   "Select unread article after current one."
6037   (interactive)
6038   (gnus-summary-next-article
6039    (or (not (eq gnus-summary-goto-unread 'never))
6040        (gnus-summary-last-article-p (gnus-summary-article-number)))
6041    (and gnus-auto-select-same
6042         (gnus-summary-article-subject))))
6043
6044 (defun gnus-summary-prev-article (&optional unread subject)
6045   "Select the article after the current one.
6046 If UNREAD is non-nil, only unread articles are selected."
6047   (interactive "P")
6048   (gnus-summary-next-article unread subject t))
6049
6050 (defun gnus-summary-prev-unread-article ()
6051   "Select unread article before current one."
6052   (interactive)
6053   (gnus-summary-prev-article
6054    (or (not (eq gnus-summary-goto-unread 'never))
6055        (gnus-summary-first-article-p (gnus-summary-article-number)))
6056    (and gnus-auto-select-same
6057         (gnus-summary-article-subject))))
6058
6059 (defun gnus-summary-next-page (&optional lines circular)
6060   "Show next page of the selected article.
6061 If at the end of the current article, select the next article.
6062 LINES says how many lines should be scrolled up.
6063
6064 If CIRCULAR is non-nil, go to the start of the article instead of
6065 selecting the next article when reaching the end of the current
6066 article."
6067   (interactive "P")
6068   (setq gnus-summary-buffer (current-buffer))
6069   (gnus-set-global-variables)
6070   (let ((article (gnus-summary-article-number))
6071         (article-window (get-buffer-window gnus-article-buffer t))
6072         endp)
6073     ;; If the buffer is empty, we have no article.
6074     (unless article
6075       (error "No article to select"))
6076     (gnus-configure-windows 'article)
6077     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6078         (if (and (eq gnus-summary-goto-unread 'never)
6079                  (not (gnus-summary-last-article-p article)))
6080             (gnus-summary-next-article)
6081           (gnus-summary-next-unread-article))
6082       (if (or (null gnus-current-article)
6083               (null gnus-article-current)
6084               (/= article (cdr gnus-article-current))
6085               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6086           ;; Selected subject is different from current article's.
6087           (gnus-summary-display-article article)
6088         (when article-window
6089           (gnus-eval-in-buffer-window gnus-article-buffer
6090             (setq endp (gnus-article-next-page lines)))
6091           (when endp
6092             (cond (circular
6093                    (gnus-summary-beginning-of-article))
6094                   (lines
6095                    (gnus-message 3 "End of message"))
6096                   ((null lines)
6097                    (if (and (eq gnus-summary-goto-unread 'never)
6098                             (not (gnus-summary-last-article-p article)))
6099                        (gnus-summary-next-article)
6100                      (gnus-summary-next-unread-article))))))))
6101     (gnus-summary-recenter)
6102     (gnus-summary-position-point)))
6103
6104 (defun gnus-summary-prev-page (&optional lines move)
6105   "Show previous page of selected article.
6106 Argument LINES specifies lines to be scrolled down.
6107 If MOVE, move to the previous unread article if point is at
6108 the beginning of the buffer."
6109   (interactive "P")
6110   (let ((article (gnus-summary-article-number))
6111         (article-window (get-buffer-window gnus-article-buffer t))
6112         endp)
6113     (gnus-configure-windows 'article)
6114     (if (or (null gnus-current-article)
6115             (null gnus-article-current)
6116             (/= article (cdr gnus-article-current))
6117             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6118         ;; Selected subject is different from current article's.
6119         (gnus-summary-display-article article)
6120       (gnus-summary-recenter)
6121       (when article-window
6122         (gnus-eval-in-buffer-window gnus-article-buffer
6123           (setq endp (gnus-article-prev-page lines)))
6124         (when (and move endp)
6125           (cond (lines
6126                  (gnus-message 3 "Beginning of message"))
6127                 ((null lines)
6128                  (if (and (eq gnus-summary-goto-unread 'never)
6129                           (not (gnus-summary-first-article-p article)))
6130                      (gnus-summary-prev-article)
6131                    (gnus-summary-prev-unread-article))))))))
6132   (gnus-summary-position-point))
6133
6134 (defun gnus-summary-prev-page-or-article (&optional lines)
6135   "Show previous page of selected article.
6136 Argument LINES specifies lines to be scrolled down.
6137 If at the beginning of the article, go to the next article."
6138   (interactive "P")
6139   (gnus-summary-prev-page lines t))
6140
6141 (defun gnus-summary-scroll-up (lines)
6142   "Scroll up (or down) one line current article.
6143 Argument LINES specifies lines to be scrolled up (or down if negative)."
6144   (interactive "p")
6145   (gnus-configure-windows 'article)
6146   (gnus-summary-show-thread)
6147   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6148     (gnus-eval-in-buffer-window gnus-article-buffer
6149       (cond ((> lines 0)
6150              (when (gnus-article-next-page lines)
6151                (gnus-message 3 "End of message")))
6152             ((< lines 0)
6153              (gnus-article-prev-page (- lines))))))
6154   (gnus-summary-recenter)
6155   (gnus-summary-position-point))
6156
6157 (defun gnus-summary-scroll-down (lines)
6158   "Scroll down (or up) one line current article.
6159 Argument LINES specifies lines to be scrolled down (or up if negative)."
6160   (interactive "p")
6161   (gnus-summary-scroll-up (- lines)))
6162
6163 (defun gnus-summary-next-same-subject ()
6164   "Select next article which has the same subject as current one."
6165   (interactive)
6166   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6167
6168 (defun gnus-summary-prev-same-subject ()
6169   "Select previous article which has the same subject as current one."
6170   (interactive)
6171   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6172
6173 (defun gnus-summary-next-unread-same-subject ()
6174   "Select next unread article which has the same subject as current one."
6175   (interactive)
6176   (gnus-summary-next-article t (gnus-summary-article-subject)))
6177
6178 (defun gnus-summary-prev-unread-same-subject ()
6179   "Select previous unread article which has the same subject as current one."
6180   (interactive)
6181   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6182
6183 (defun gnus-summary-first-unread-article ()
6184   "Select the first unread article.
6185 Return nil if there are no unread articles."
6186   (interactive)
6187   (prog1
6188       (when (gnus-summary-first-subject t)
6189         (gnus-summary-show-thread)
6190         (gnus-summary-first-subject t)
6191         (gnus-summary-display-article (gnus-summary-article-number)))
6192     (gnus-summary-position-point)))
6193
6194 (defun gnus-summary-first-unread-subject ()
6195   "Place the point on the subject line of the first unread article.
6196 Return nil if there are no unread articles."
6197   (interactive)
6198   (prog1
6199       (when (gnus-summary-first-subject t)
6200         (gnus-summary-show-thread)
6201         (gnus-summary-first-subject t))
6202     (gnus-summary-position-point)))
6203
6204 (defun gnus-summary-first-article ()
6205   "Select the first article.
6206 Return nil if there are no articles."
6207   (interactive)
6208   (prog1
6209       (when (gnus-summary-first-subject)
6210         (gnus-summary-show-thread)
6211         (gnus-summary-first-subject)
6212         (gnus-summary-display-article (gnus-summary-article-number)))
6213     (gnus-summary-position-point)))
6214
6215 (defun gnus-summary-best-unread-article ()
6216   "Select the unread article with the highest score."
6217   (interactive)
6218   (let ((best -1000000)
6219         (data gnus-newsgroup-data)
6220         article score)
6221     (while data
6222       (and (gnus-data-unread-p (car data))
6223            (> (setq score
6224                     (gnus-summary-article-score (gnus-data-number (car data))))
6225               best)
6226            (setq best score
6227                  article (gnus-data-number (car data))))
6228       (setq data (cdr data)))
6229     (prog1
6230         (if article
6231             (gnus-summary-goto-article article)
6232           (error "No unread articles"))
6233       (gnus-summary-position-point))))
6234
6235 (defun gnus-summary-last-subject ()
6236   "Go to the last displayed subject line in the group."
6237   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6238     (when article
6239       (gnus-summary-goto-subject article))))
6240
6241 (defun gnus-summary-goto-article (article &optional all-headers force)
6242   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6243 If ALL-HEADERS is non-nil, no header lines are hidden.
6244 If FORCE, go to the article even if it isn't displayed.  If FORCE
6245 is a number, it is the line the article is to be displayed on."
6246   (interactive
6247    (list
6248     (completing-read
6249      "Article number or Message-ID: "
6250      (mapcar (lambda (number) (list (int-to-string number)))
6251              gnus-newsgroup-limit))
6252     current-prefix-arg
6253     t))
6254   (prog1
6255       (if (and (stringp article)
6256                (string-match "@" article))
6257           (gnus-summary-refer-article article)
6258         (when (stringp article)
6259           (setq article (string-to-number article)))
6260         (if (gnus-summary-goto-subject article force)
6261             (gnus-summary-display-article article all-headers)
6262           (gnus-message 4 "Couldn't go to article %s" article) nil))
6263     (gnus-summary-position-point)))
6264
6265 (defun gnus-summary-goto-last-article ()
6266   "Go to the previously read article."
6267   (interactive)
6268   (prog1
6269       (when gnus-last-article
6270         (gnus-summary-goto-article gnus-last-article nil t))
6271     (gnus-summary-position-point)))
6272
6273 (defun gnus-summary-pop-article (number)
6274   "Pop one article off the history and go to the previous.
6275 NUMBER articles will be popped off."
6276   (interactive "p")
6277   (let (to)
6278     (setq gnus-newsgroup-history
6279           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6280     (if to
6281         (gnus-summary-goto-article (car to) nil t)
6282       (error "Article history empty")))
6283   (gnus-summary-position-point))
6284
6285 ;; Summary commands and functions for limiting the summary buffer.
6286
6287 (defun gnus-summary-limit-to-articles (n)
6288   "Limit the summary buffer to the next N articles.
6289 If not given a prefix, use the process marked articles instead."
6290   (interactive "P")
6291   (prog1
6292       (let ((articles (gnus-summary-work-articles n)))
6293         (setq gnus-newsgroup-processable nil)
6294         (gnus-summary-limit articles))
6295     (gnus-summary-position-point)))
6296
6297 (defun gnus-summary-pop-limit (&optional total)
6298   "Restore the previous limit.
6299 If given a prefix, remove all limits."
6300   (interactive "P")
6301   (when total
6302     (setq gnus-newsgroup-limits
6303           (list (mapcar (lambda (h) (mail-header-number h))
6304                         gnus-newsgroup-headers))))
6305   (unless gnus-newsgroup-limits
6306     (error "No limit to pop"))
6307   (prog1
6308       (gnus-summary-limit nil 'pop)
6309     (gnus-summary-position-point)))
6310
6311 (defun gnus-summary-limit-to-subject (subject &optional header)
6312   "Limit the summary buffer to articles that have subjects that match a regexp."
6313   (interactive "sLimit to subject (regexp): ")
6314   (unless header
6315     (setq header "subject"))
6316   (when (not (equal "" subject))
6317     (prog1
6318         (let ((articles (gnus-summary-find-matching
6319                          (or header "subject") subject 'all)))
6320           (unless articles
6321             (error "Found no matches for \"%s\"" subject))
6322           (gnus-summary-limit articles))
6323       (gnus-summary-position-point))))
6324
6325 (defun gnus-summary-limit-to-author (from)
6326   "Limit the summary buffer to articles that have authors that match a regexp."
6327   (interactive "sLimit to author (regexp): ")
6328   (gnus-summary-limit-to-subject from "from"))
6329
6330 (defun gnus-summary-limit-to-age (age &optional younger-p)
6331   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6332 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6333 articles that are younger than AGE days."
6334   (interactive
6335    (let ((younger current-prefix-arg)
6336          (days-got nil)
6337          days)
6338      (while (not days-got)
6339        (setq days (if younger
6340                       (read-string "Limit to articles within (in days): ")
6341                     (read-string "Limit to articles old than (in days): ")))
6342        (when (> (length days) 0)
6343          (setq days (read days)))
6344        (if (numberp days)
6345            (setq days-got t)
6346          (message "Please enter a number.")
6347          (sleep-for 1)))
6348      (list days younger)))
6349   (prog1
6350       (let ((data gnus-newsgroup-data)
6351             (cutoff (days-to-time age))
6352             articles d date is-younger)
6353         (while (setq d (pop data))
6354           (when (and (vectorp (gnus-data-header d))
6355                      (setq date (mail-header-date (gnus-data-header d))))
6356             (setq is-younger (time-less-p
6357                               (time-since (condition-case ()
6358                                               (date-to-time date)
6359                                             (error '(0 0))))
6360                               cutoff))
6361             (when (if younger-p
6362                       is-younger
6363                     (not is-younger))
6364               (push (gnus-data-number d) articles))))
6365         (gnus-summary-limit (nreverse articles)))
6366     (gnus-summary-position-point)))
6367
6368 (defun gnus-summary-limit-to-extra (header regexp)
6369   "Limit the summary buffer to articles that match an 'extra' header."
6370   (interactive
6371    (let ((header
6372           (intern
6373            (gnus-completing-read
6374             (symbol-name (car gnus-extra-headers))      
6375             "Limit extra header:"       
6376             (mapcar (lambda (x) 
6377                       (cons (symbol-name x) x))
6378                     gnus-extra-headers)
6379             nil                 
6380             t))))
6381      (list header
6382            (read-string (format "Limit to header %s (regexp): " header)))))
6383   (when (not (equal "" regexp))
6384     (prog1
6385         (let ((articles (gnus-summary-find-matching
6386                          (cons 'extra header) regexp 'all)))
6387           (unless articles
6388             (error "Found no matches for \"%s\"" regexp))
6389           (gnus-summary-limit articles))
6390       (gnus-summary-position-point))))
6391
6392 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6393 (make-obsolete
6394  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6395
6396 (defun gnus-summary-limit-to-unread (&optional all)
6397   "Limit the summary buffer to articles that are not marked as read.
6398 If ALL is non-nil, limit strictly to unread articles."
6399   (interactive "P")
6400   (if all
6401       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6402     (gnus-summary-limit-to-marks
6403      ;; Concat all the marks that say that an article is read and have
6404      ;; those removed.
6405      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6406            gnus-killed-mark gnus-kill-file-mark
6407            gnus-low-score-mark gnus-expirable-mark
6408            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6409            gnus-duplicate-mark gnus-souped-mark)
6410      'reverse)))
6411
6412 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6413 (make-obsolete 'gnus-summary-delete-marked-with
6414                'gnus-summary-limit-exlude-marks)
6415
6416 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6417   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6418 If REVERSE, limit the summary buffer to articles that are marked
6419 with MARKS.  MARKS can either be a string of marks or a list of marks.
6420 Returns how many articles were removed."
6421   (interactive "sMarks: ")
6422   (gnus-summary-limit-to-marks marks t))
6423
6424 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6425   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6426 If REVERSE (the prefix), limit the summary buffer to articles that are
6427 not marked with MARKS.  MARKS can either be a string of marks or a
6428 list of marks.
6429 Returns how many articles were removed."
6430   (interactive "sMarks: \nP")
6431   (prog1
6432       (let ((data gnus-newsgroup-data)
6433             (marks (if (listp marks) marks
6434                      (append marks nil))) ; Transform to list.
6435             articles)
6436         (while data
6437           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6438                   (memq (gnus-data-mark (car data)) marks))
6439             (push (gnus-data-number (car data)) articles))
6440           (setq data (cdr data)))
6441         (gnus-summary-limit articles))
6442     (gnus-summary-position-point)))
6443
6444 (defun gnus-summary-limit-to-score (&optional score)
6445   "Limit to articles with score at or above SCORE."
6446   (interactive "P")
6447   (setq score (if score
6448                   (prefix-numeric-value score)
6449                 (or gnus-summary-default-score 0)))
6450   (let ((data gnus-newsgroup-data)
6451         articles)
6452     (while data
6453       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6454                 score)
6455         (push (gnus-data-number (car data)) articles))
6456       (setq data (cdr data)))
6457     (prog1
6458         (gnus-summary-limit articles)
6459       (gnus-summary-position-point))))
6460
6461 (defun gnus-summary-limit-include-thread (id)
6462   "Display all the hidden articles that in the current thread."
6463   (interactive (list (mail-header-id (gnus-summary-article-header))))
6464   (let ((articles (gnus-articles-in-thread
6465                    (gnus-id-to-thread (gnus-root-id id)))))
6466     (prog1
6467         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6468       (gnus-summary-position-point))))
6469
6470 (defun gnus-summary-limit-include-dormant ()
6471   "Display all the hidden articles that are marked as dormant.
6472 Note that this command only works on a subset of the articles currently
6473 fetched for this group."
6474   (interactive)
6475   (unless gnus-newsgroup-dormant
6476     (error "There are no dormant articles in this group"))
6477   (prog1
6478       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6479     (gnus-summary-position-point)))
6480
6481 (defun gnus-summary-limit-exclude-dormant ()
6482   "Hide all dormant articles."
6483   (interactive)
6484   (prog1
6485       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6486     (gnus-summary-position-point)))
6487
6488 (defun gnus-summary-limit-exclude-childless-dormant ()
6489   "Hide all dormant articles that have no children."
6490   (interactive)
6491   (let ((data (gnus-data-list t))
6492         articles d children)
6493     ;; Find all articles that are either not dormant or have
6494     ;; children.
6495     (while (setq d (pop data))
6496       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6497                 (and (setq children
6498                            (gnus-article-children (gnus-data-number d)))
6499                      (let (found)
6500                        (while children
6501                          (when (memq (car children) articles)
6502                            (setq children nil
6503                                  found t))
6504                          (pop children))
6505                        found)))
6506         (push (gnus-data-number d) articles)))
6507     ;; Do the limiting.
6508     (prog1
6509         (gnus-summary-limit articles)
6510       (gnus-summary-position-point))))
6511
6512 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6513   "Mark all unread excluded articles as read.
6514 If ALL, mark even excluded ticked and dormants as read."
6515   (interactive "P")
6516   (let ((articles (gnus-sorted-complement
6517                    (sort
6518                     (mapcar (lambda (h) (mail-header-number h))
6519                             gnus-newsgroup-headers)
6520                     '<)
6521                    (sort gnus-newsgroup-limit '<)))
6522         article)
6523     (setq gnus-newsgroup-unreads
6524           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6525     (if all
6526         (setq gnus-newsgroup-dormant nil
6527               gnus-newsgroup-marked nil
6528               gnus-newsgroup-reads
6529               (nconc
6530                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6531                gnus-newsgroup-reads))
6532       (while (setq article (pop articles))
6533         (unless (or (memq article gnus-newsgroup-dormant)
6534                     (memq article gnus-newsgroup-marked))
6535           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6536
6537 (defun gnus-summary-limit (articles &optional pop)
6538   (if pop
6539       ;; We pop the previous limit off the stack and use that.
6540       (setq articles (car gnus-newsgroup-limits)
6541             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6542     ;; We use the new limit, so we push the old limit on the stack.
6543     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6544   ;; Set the limit.
6545   (setq gnus-newsgroup-limit articles)
6546   (let ((total (length gnus-newsgroup-data))
6547         (data (gnus-data-find-list (gnus-summary-article-number)))
6548         (gnus-summary-mark-below nil)   ; Inhibit this.
6549         found)
6550     ;; This will do all the work of generating the new summary buffer
6551     ;; according to the new limit.
6552     (gnus-summary-prepare)
6553     ;; Hide any threads, possibly.
6554     (and gnus-show-threads
6555          gnus-thread-hide-subtree
6556          (gnus-summary-hide-all-threads))
6557     ;; Try to return to the article you were at, or one in the
6558     ;; neighborhood.
6559     (when data
6560       ;; We try to find some article after the current one.
6561       (while data
6562         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6563           (setq data nil
6564                 found t))
6565         (setq data (cdr data))))
6566     (unless found
6567       ;; If there is no data, that means that we were after the last
6568       ;; article.  The same goes when we can't find any articles
6569       ;; after the current one.
6570       (goto-char (point-max))
6571       (gnus-summary-find-prev))
6572     (gnus-set-mode-line 'summary)
6573     ;; We return how many articles were removed from the summary
6574     ;; buffer as a result of the new limit.
6575     (- total (length gnus-newsgroup-data))))
6576
6577 (defsubst gnus-invisible-cut-children (threads)
6578   (let ((num 0))
6579     (while threads
6580       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6581         (incf num))
6582       (pop threads))
6583     (< num 2)))
6584
6585 (defsubst gnus-cut-thread (thread)
6586   "Go forwards in the thread until we find an article that we want to display."
6587   (when (or (eq gnus-fetch-old-headers 'some)
6588             (eq gnus-fetch-old-headers 'invisible)
6589             (numberp gnus-fetch-old-headers)
6590             (eq gnus-build-sparse-threads 'some)
6591             (eq gnus-build-sparse-threads 'more))
6592     ;; Deal with old-fetched headers and sparse threads.
6593     (while (and
6594             thread
6595             (or
6596              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6597              (gnus-summary-article-ancient-p
6598               (mail-header-number (car thread))))
6599             (if (or (<= (length (cdr thread)) 1)
6600                     (eq gnus-fetch-old-headers 'invisible))
6601                 (setq gnus-newsgroup-limit
6602                       (delq (mail-header-number (car thread))
6603                             gnus-newsgroup-limit)
6604                       thread (cadr thread))
6605               (when (gnus-invisible-cut-children (cdr thread))
6606                 (let ((th (cdr thread)))
6607                   (while th
6608                     (if (memq (mail-header-number (caar th))
6609                               gnus-newsgroup-limit)
6610                         (setq thread (car th)
6611                               th nil)
6612                       (setq th (cdr th))))))))))
6613   thread)
6614
6615 (defun gnus-cut-threads (threads)
6616   "Cut off all uninteresting articles from the beginning of threads."
6617   (when (or (eq gnus-fetch-old-headers 'some)
6618             (eq gnus-fetch-old-headers 'invisible)
6619             (numberp gnus-fetch-old-headers)
6620             (eq gnus-build-sparse-threads 'some)
6621             (eq gnus-build-sparse-threads 'more))
6622     (let ((th threads))
6623       (while th
6624         (setcar th (gnus-cut-thread (car th)))
6625         (setq th (cdr th)))))
6626   ;; Remove nixed out threads.
6627   (delq nil threads))
6628
6629 (defun gnus-summary-initial-limit (&optional show-if-empty)
6630   "Figure out what the initial limit is supposed to be on group entry.
6631 This entails weeding out unwanted dormants, low-scored articles,
6632 fetch-old-headers verbiage, and so on."
6633   ;; Most groups have nothing to remove.
6634   (if (or gnus-inhibit-limiting
6635           (and (null gnus-newsgroup-dormant)
6636                (not (eq gnus-fetch-old-headers 'some))
6637                (not (numberp gnus-fetch-old-headers))
6638                (not (eq gnus-fetch-old-headers 'invisible))
6639                (null gnus-summary-expunge-below)
6640                (not (eq gnus-build-sparse-threads 'some))
6641                (not (eq gnus-build-sparse-threads 'more))
6642                (null gnus-thread-expunge-below)
6643                (not gnus-use-nocem)))
6644       ()                                ; Do nothing.
6645     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6646     (setq gnus-newsgroup-limit nil)
6647     (mapatoms
6648      (lambda (node)
6649        (unless (car (symbol-value node))
6650          ;; These threads have no parents -- they are roots.
6651          (let ((nodes (cdr (symbol-value node)))
6652                thread)
6653            (while nodes
6654              (if (and gnus-thread-expunge-below
6655                       (< (gnus-thread-total-score (car nodes))
6656                          gnus-thread-expunge-below))
6657                  (gnus-expunge-thread (pop nodes))
6658                (setq thread (pop nodes))
6659                (gnus-summary-limit-children thread))))))
6660      gnus-newsgroup-dependencies)
6661     ;; If this limitation resulted in an empty group, we might
6662     ;; pop the previous limit and use it instead.
6663     (when (and (not gnus-newsgroup-limit)
6664                show-if-empty)
6665       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6666     gnus-newsgroup-limit))
6667
6668 (defun gnus-summary-limit-children (thread)
6669   "Return 1 if this subthread is visible and 0 if it is not."
6670   ;; First we get the number of visible children to this thread.  This
6671   ;; is done by recursing down the thread using this function, so this
6672   ;; will really go down to a leaf article first, before slowly
6673   ;; working its way up towards the root.
6674   (when thread
6675     (let ((children
6676            (if (cdr thread)
6677                (apply '+ (mapcar 'gnus-summary-limit-children
6678                                  (cdr thread)))
6679              0))
6680           (number (mail-header-number (car thread)))
6681           score)
6682       (if (and
6683            (not (memq number gnus-newsgroup-marked))
6684            (or
6685             ;; If this article is dormant and has absolutely no visible
6686             ;; children, then this article isn't visible.
6687             (and (memq number gnus-newsgroup-dormant)
6688                  (zerop children))
6689             ;; If this is "fetch-old-headered" and there is no
6690             ;; visible children, then we don't want this article.
6691             (and (or (eq gnus-fetch-old-headers 'some)
6692                      (numberp gnus-fetch-old-headers))
6693                  (gnus-summary-article-ancient-p number)
6694                  (zerop children))
6695             ;; If this is "fetch-old-headered" and `invisible', then
6696             ;; we don't want this article.
6697             (and (eq gnus-fetch-old-headers 'invisible)
6698                  (gnus-summary-article-ancient-p number))
6699             ;; If this is a sparsely inserted article with no children,
6700             ;; we don't want it.
6701             (and (eq gnus-build-sparse-threads 'some)
6702                  (gnus-summary-article-sparse-p number)
6703                  (zerop children))
6704             ;; If we use expunging, and this article is really
6705             ;; low-scored, then we don't want this article.
6706             (when (and gnus-summary-expunge-below
6707                        (< (setq score
6708                                 (or (cdr (assq number gnus-newsgroup-scored))
6709                                     gnus-summary-default-score))
6710                           gnus-summary-expunge-below))
6711               ;; We increase the expunge-tally here, but that has
6712               ;; nothing to do with the limits, really.
6713               (incf gnus-newsgroup-expunged-tally)
6714               ;; We also mark as read here, if that's wanted.
6715               (when (and gnus-summary-mark-below
6716                          (< score gnus-summary-mark-below))
6717                 (setq gnus-newsgroup-unreads
6718                       (delq number gnus-newsgroup-unreads))
6719                 (if gnus-newsgroup-auto-expire
6720                     (push number gnus-newsgroup-expirable)
6721                   (push (cons number gnus-low-score-mark)
6722                         gnus-newsgroup-reads)))
6723               t)
6724             ;; Check NoCeM things.
6725             (if (and gnus-use-nocem
6726                      (gnus-nocem-unwanted-article-p
6727                       (mail-header-id (car thread))))
6728                 (progn
6729                   (setq gnus-newsgroup-unreads
6730                         (delq number gnus-newsgroup-unreads))
6731                   t))))
6732           ;; Nope, invisible article.
6733           0
6734         ;; Ok, this article is to be visible, so we add it to the limit
6735         ;; and return 1.
6736         (push number gnus-newsgroup-limit)
6737         1))))
6738
6739 (defun gnus-expunge-thread (thread)
6740   "Mark all articles in THREAD as read."
6741   (let* ((number (mail-header-number (car thread))))
6742     (incf gnus-newsgroup-expunged-tally)
6743     ;; We also mark as read here, if that's wanted.
6744     (setq gnus-newsgroup-unreads
6745           (delq number gnus-newsgroup-unreads))
6746     (if gnus-newsgroup-auto-expire
6747         (push number gnus-newsgroup-expirable)
6748       (push (cons number gnus-low-score-mark)
6749             gnus-newsgroup-reads)))
6750   ;; Go recursively through all subthreads.
6751   (mapcar 'gnus-expunge-thread (cdr thread)))
6752
6753 ;; Summary article oriented commands
6754
6755 (defun gnus-summary-refer-parent-article (n)
6756   "Refer parent article N times.
6757 If N is negative, go to ancestor -N instead.
6758 The difference between N and the number of articles fetched is returned."
6759   (interactive "p")
6760   (let ((skip 1)
6761         error header ref)
6762     (when (not (natnump n))
6763       (setq skip (abs n)
6764             n 1))
6765     (while (and (> n 0)
6766                 (not error))
6767       (setq header (gnus-summary-article-header))
6768       (if (and (eq (mail-header-number header)
6769                    (cdr gnus-article-current))
6770                (equal gnus-newsgroup-name
6771                       (car gnus-article-current)))
6772           ;; If we try to find the parent of the currently
6773           ;; displayed article, then we take a look at the actual
6774           ;; References header, since this is slightly more
6775           ;; reliable than the References field we got from the
6776           ;; server.
6777           (save-excursion
6778             (set-buffer gnus-original-article-buffer)
6779             (nnheader-narrow-to-headers)
6780             (unless (setq ref (message-fetch-field "references"))
6781               (setq ref (message-fetch-field "in-reply-to")))
6782             (widen))
6783         (setq ref
6784               ;; It's not the current article, so we take a bet on
6785               ;; the value we got from the server.
6786               (mail-header-references header)))
6787       (if (and ref
6788                (not (equal ref "")))
6789           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6790             (gnus-message 1 "Couldn't find parent"))
6791         (gnus-message 1 "No references in article %d"
6792                       (gnus-summary-article-number))
6793         (setq error t))
6794       (decf n))
6795     (gnus-summary-position-point)
6796     n))
6797
6798 (defun gnus-summary-refer-references ()
6799   "Fetch all articles mentioned in the References header.
6800 Return the number of articles fetched."
6801   (interactive)
6802   (let ((ref (mail-header-references (gnus-summary-article-header)))
6803         (current (gnus-summary-article-number))
6804         (n 0))
6805     (if (or (not ref)
6806             (equal ref ""))
6807         (error "No References in the current article")
6808       ;; For each Message-ID in the References header...
6809       (while (string-match "<[^>]*>" ref)
6810         (incf n)
6811         ;; ... fetch that article.
6812         (gnus-summary-refer-article
6813          (prog1 (match-string 0 ref)
6814            (setq ref (substring ref (match-end 0))))))
6815       (gnus-summary-goto-subject current)
6816       (gnus-summary-position-point)
6817       n)))
6818
6819 (defun gnus-summary-refer-thread (&optional limit)
6820   "Fetch all articles in the current thread.
6821 If LIMIT (the numerical prefix), fetch that many old headers instead
6822 of what's specified by the `gnus-refer-thread-limit' variable."
6823   (interactive "P")
6824   (let ((id (mail-header-id (gnus-summary-article-header)))
6825         (limit (if limit (prefix-numeric-value limit)
6826                  gnus-refer-thread-limit)))
6827     ;; We want to fetch LIMIT *old* headers, but we also have to
6828     ;; re-fetch all the headers in the current buffer, because many of
6829     ;; them may be undisplayed.  So we adjust LIMIT.
6830     (when (numberp limit)
6831       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6832     (unless (eq gnus-fetch-old-headers 'invisible)
6833       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6834       ;; Retrieve the headers and read them in.
6835       (if (eq (gnus-retrieve-headers
6836                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6837               'nov)
6838           (gnus-build-all-threads)
6839         (error "Can't fetch thread from backends that don't support NOV"))
6840       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6841     (gnus-summary-limit-include-thread id)))
6842
6843 (defun gnus-summary-refer-article (message-id)
6844   "Fetch an article specified by MESSAGE-ID."
6845   (interactive "sMessage-ID: ")
6846   (when (and (stringp message-id)
6847              (not (zerop (length message-id))))
6848     ;; Construct the correct Message-ID if necessary.
6849     ;; Suggested by tale@pawl.rpi.edu.
6850     (unless (string-match "^<" message-id)
6851       (setq message-id (concat "<" message-id)))
6852     (unless (string-match ">$" message-id)
6853       (setq message-id (concat message-id ">")))
6854     (let* ((header (gnus-id-to-header message-id))
6855            (sparse (and header
6856                         (gnus-summary-article-sparse-p
6857                          (mail-header-number header))
6858                         (memq (mail-header-number header)
6859                               gnus-newsgroup-limit)))
6860            number)
6861       (cond
6862        ;; If the article is present in the buffer we just go to it.
6863        ((and header
6864              (or (not (gnus-summary-article-sparse-p
6865                        (mail-header-number header)))
6866                  sparse))
6867         (prog1
6868             (gnus-summary-goto-article
6869              (mail-header-number header) nil t)
6870           (when sparse
6871             (gnus-summary-update-article (mail-header-number header)))))
6872        (t
6873         ;; We fetch the article.
6874         (catch 'found
6875           (dolist (gnus-override-method (gnus-refer-article-methods))
6876             (gnus-check-server gnus-override-method)
6877             ;; Fetch the header, and display the article.
6878             (when (setq number (gnus-summary-insert-subject message-id))
6879               (gnus-summary-select-article nil nil nil number)
6880               (throw 'found t)))
6881           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6882
6883 (defun gnus-refer-article-methods ()
6884   "Return a list of referrable methods."
6885   (cond
6886    ;; No method, so we default to current and native.
6887    ((null gnus-refer-article-method)
6888     (list gnus-current-select-method gnus-select-method))
6889    ;; Current.
6890    ((eq 'current gnus-refer-article-method)
6891     (list gnus-current-select-method))
6892    ;; List of select methods.
6893    ((not (stringp (cadr gnus-refer-article-method)))
6894     (let (out)
6895       (dolist (method gnus-refer-article-method)
6896         (push (if (eq 'current method)
6897                   gnus-current-select-method
6898                 method)
6899               out))
6900       (nreverse out)))
6901    ;; One single select method.
6902    (t
6903     (list gnus-refer-article-method))))
6904
6905 (defun gnus-summary-edit-parameters ()
6906   "Edit the group parameters of the current group."
6907   (interactive)
6908   (gnus-group-edit-group gnus-newsgroup-name 'params))
6909
6910 (defun gnus-summary-customize-parameters ()
6911   "Customize the group parameters of the current group."
6912   (interactive)
6913   (gnus-group-customize gnus-newsgroup-name))
6914
6915 (defun gnus-summary-enter-digest-group (&optional force)
6916   "Enter an nndoc group based on the current article.
6917 If FORCE, force a digest interpretation.  If not, try
6918 to guess what the document format is."
6919   (interactive "P")
6920   (let ((conf gnus-current-window-configuration))
6921     (save-excursion
6922       (gnus-summary-select-article))
6923     (setq gnus-current-window-configuration conf)
6924     (let* ((name (format "%s-%d"
6925                          (gnus-group-prefixed-name
6926                           gnus-newsgroup-name (list 'nndoc ""))
6927                          (save-excursion
6928                            (set-buffer gnus-summary-buffer)
6929                            gnus-current-article)))
6930            (ogroup gnus-newsgroup-name)
6931            (params (append (gnus-info-params (gnus-get-info ogroup))
6932                            (list (cons 'to-group ogroup))
6933                            (list (cons 'save-article-group ogroup))))
6934            (case-fold-search t)
6935            (buf (current-buffer))
6936            dig to-address)
6937       (save-excursion
6938         (set-buffer gnus-original-article-buffer)
6939         ;; Have the digest group inherit the main mail address of
6940         ;; the parent article.
6941         (when (setq to-address (or (message-fetch-field "reply-to")
6942                                    (message-fetch-field "from")))
6943           (setq params (append (list (cons 'to-address to-address)))))
6944         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6945         (insert-buffer-substring gnus-original-article-buffer)
6946         ;; Remove lines that may lead nndoc to misinterpret the
6947         ;; document type.
6948         (narrow-to-region
6949          (goto-char (point-min))
6950          (or (search-forward "\n\n" nil t) (point)))
6951         (goto-char (point-min))
6952         (delete-matching-lines "^Path:\\|^From ")
6953         (widen))
6954       (unwind-protect
6955           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6956                     (gnus-newsgroup-ephemeral-ignored-charsets
6957                      gnus-newsgroup-ignored-charsets))
6958                 (gnus-group-read-ephemeral-group
6959                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6960                               (nndoc-article-type
6961                                ,(if force 'mbox 'guess))) t))
6962               ;; Make all postings to this group go to the parent group.
6963               (nconc (gnus-info-params (gnus-get-info name))
6964                      params)
6965             ;; Couldn't select this doc group.
6966             (switch-to-buffer buf)
6967             (gnus-set-global-variables)
6968             (gnus-configure-windows 'summary)
6969             (gnus-message 3 "Article couldn't be entered?"))
6970         (kill-buffer dig)))))
6971
6972 (defun gnus-summary-read-document (n)
6973   "Open a new group based on the current article(s).
6974 This will allow you to read digests and other similar
6975 documents as newsgroups.
6976 Obeys the standard process/prefix convention."
6977   (interactive "P")
6978   (let* ((articles (gnus-summary-work-articles n))
6979          (ogroup gnus-newsgroup-name)
6980          (params (append (gnus-info-params (gnus-get-info ogroup))
6981                          (list (cons 'to-group ogroup))))
6982          article group egroup groups vgroup)
6983     (while (setq article (pop articles))
6984       (setq group (format "%s-%d" gnus-newsgroup-name article))
6985       (gnus-summary-remove-process-mark article)
6986       (when (gnus-summary-display-article article)
6987         (save-excursion
6988           (with-temp-buffer
6989             (insert-buffer-substring gnus-original-article-buffer)
6990             ;; Remove some headers that may lead nndoc to make
6991             ;; the wrong guess.
6992             (message-narrow-to-head)
6993             (goto-char (point-min))
6994             (delete-matching-lines "^\\(Path\\):\\|^From ")
6995             (widen)
6996             (if (setq egroup
6997                       (gnus-group-read-ephemeral-group
6998                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6999                                      (nndoc-article-type guess))
7000                        t nil t))
7001                 (progn
7002                   ;; Make all postings to this group go to the parent group.
7003                   (nconc (gnus-info-params (gnus-get-info egroup))
7004                          params)
7005                   (push egroup groups))
7006               ;; Couldn't select this doc group.
7007               (gnus-error 3 "Article couldn't be entered"))))))
7008     ;; Now we have selected all the documents.
7009     (cond
7010      ((not groups)
7011       (error "None of the articles could be interpreted as documents"))
7012      ((gnus-group-read-ephemeral-group
7013        (setq vgroup (format
7014                      "nnvirtual:%s-%s" gnus-newsgroup-name
7015                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7016        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7017        t
7018        (cons (current-buffer) 'summary)))
7019      (t
7020       (error "Couldn't select virtual nndoc group")))))
7021
7022 (defun gnus-summary-isearch-article (&optional regexp-p)
7023   "Do incremental search forward on the current article.
7024 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7025   (interactive "P")
7026   (let* ((gnus-inhibit-treatment t)
7027          (old (gnus-summary-select-article)))
7028     (gnus-configure-windows 'article)
7029     (gnus-eval-in-buffer-window gnus-article-buffer
7030       (save-restriction
7031         (widen)
7032         (when (eq 'old old)
7033           (gnus-article-show-all-headers))
7034         (goto-char (point-min))
7035         (isearch-forward regexp-p)))))
7036
7037 (defun gnus-summary-search-article-forward (regexp &optional backward)
7038   "Search for an article containing REGEXP forward.
7039 If BACKWARD, search backward instead."
7040   (interactive
7041    (list (read-string
7042           (format "Search article %s (regexp%s): "
7043                   (if current-prefix-arg "backward" "forward")
7044                   (if gnus-last-search-regexp
7045                       (concat ", default " gnus-last-search-regexp)
7046                     "")))
7047          current-prefix-arg))
7048   (if (string-equal regexp "")
7049       (setq regexp (or gnus-last-search-regexp ""))
7050     (setq gnus-last-search-regexp regexp))
7051   (if (gnus-summary-search-article regexp backward)
7052       (gnus-summary-show-thread)
7053     (error "Search failed: \"%s\"" regexp)))
7054
7055 (defun gnus-summary-search-article-backward (regexp)
7056   "Search for an article containing REGEXP backward."
7057   (interactive
7058    (list (read-string
7059           (format "Search article backward (regexp%s): "
7060                   (if gnus-last-search-regexp
7061                       (concat ", default " gnus-last-search-regexp)
7062                     "")))))
7063   (gnus-summary-search-article-forward regexp 'backward))
7064
7065 (eval-when-compile
7066   (defmacro gnus-summary-search-article-position-point (regexp backward)
7067     "Dehighlight the last matched text and goto the beginning position."
7068     (` (if (and gnus-summary-search-article-matched-data
7069                 (let ((text (caddr gnus-summary-search-article-matched-data))
7070                       (inhibit-read-only t)
7071                       buffer-read-only)
7072                   (delete-region
7073                    (goto-char (car gnus-summary-search-article-matched-data))
7074                    (cadr gnus-summary-search-article-matched-data))
7075                   (insert text)
7076                   (string-match (, regexp) text)))
7077            (if (, backward) (beginning-of-line) (end-of-line))
7078          (goto-char (if (, backward) (point-max) (point-min))))))
7079
7080   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7081     "Place point where X-Face image is displayed."
7082     (if (featurep 'xemacs)
7083         (` (let ((end (if (search-forward "\n\n" nil t)
7084                           (goto-char (1- (point)))
7085                         (point-min)))
7086                  extent)
7087              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7088              (unless (and (re-search-forward "^From:" end t)
7089                           (setq extent (extent-at (point)))
7090                           (extent-begin-glyph extent))
7091                (goto-char (, opoint)))))
7092       (` (let ((end (if (search-forward "\n\n" nil t)
7093                         (goto-char (1- (point)))
7094                       (point-min))))
7095            (goto-char
7096             (or (text-property-any (or (search-backward "\n\n" nil t)
7097                                        (point-min))
7098                                    end 'x-face-mule-bitmap-image t)
7099                 (, opoint)))))))
7100
7101   (defmacro gnus-summary-search-article-highlight-matched-text
7102     (backward treated x-face)
7103     "Highlight matched text in the function `gnus-summary-search-article'."
7104     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7105              (end (set-marker (make-marker) (match-end 0)))
7106              (inhibit-read-only t)
7107              buffer-read-only)
7108          (unless treated
7109            (let ((,@
7110                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7111                     (mapcar
7112                      (lambda (item) (setq items (delq item items)))
7113                      '(gnus-treat-buttonize
7114                        gnus-treat-fill-article
7115                        gnus-treat-fill-long-lines
7116                        gnus-treat-emphasize
7117                        gnus-treat-highlight-headers
7118                        gnus-treat-highlight-citation
7119                        gnus-treat-highlight-signature
7120                        gnus-treat-overstrike
7121                        gnus-treat-display-xface
7122                        gnus-treat-buttonize-head
7123                        gnus-treat-decode-article-as-default-mime-charset))
7124                     (static-if (featurep 'xemacs)
7125                         items
7126                       (cons '(x-face-mule-delete-x-face-field
7127                               (quote never))
7128                             items))))
7129                  (gnus-treat-display-xface
7130                   (when (, x-face) gnus-treat-display-xface)))
7131              (gnus-article-prepare-mime-display)))
7132          (goto-char (if (, backward) start end))
7133          (when (, x-face)
7134            (gnus-summary-search-article-highlight-goto-x-face (point)))
7135          (setq gnus-summary-search-article-matched-data
7136                (list start end (buffer-substring start end)))
7137          (unless (eq start end);; matched text has been deleted. :-<
7138            (put-text-property start end 'face
7139                               (or (find-face 'isearch)
7140                                   'secondary-selection))))))
7141   )
7142
7143 (defun gnus-summary-search-article (regexp &optional backward)
7144   "Search for an article containing REGEXP.
7145 Optional argument BACKWARD means do search for backward.
7146 `gnus-select-article-hook' is not called during the search."
7147   ;; We have to require this here to make sure that the following
7148   ;; dynamic binding isn't shadowed by autoloading.
7149   (require 'gnus-async)
7150   (require 'gnus-art)
7151   (let ((gnus-select-article-hook nil)  ;Disable hook.
7152         (gnus-article-display-hook nil)
7153         (gnus-article-prepare-hook nil)
7154         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7155         (gnus-use-article-prefetch nil)
7156         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7157         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7158         (sum (current-buffer))
7159         (found nil)
7160         point treated)
7161     (gnus-save-hidden-threads
7162       (static-if (featurep 'xemacs)
7163           (let ((gnus-inhibit-treatment t))
7164             (setq treated (eq 'old (gnus-summary-select-article)))
7165             (when (and treated
7166                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7167                                  (window-live-p (get-buffer-window
7168                                                  gnus-article-buffer t)))))
7169               (gnus-summary-select-article nil t)
7170               (setq treated nil)))
7171         (let ((gnus-inhibit-treatment t)
7172               (x-face-mule-delete-x-face-field 'never))
7173           (setq treated (eq 'old (gnus-summary-select-article)))
7174           (when (and treated
7175                      (not
7176                       (and (gnus-buffer-live-p gnus-article-buffer)
7177                            (window-live-p (get-buffer-window
7178                                            gnus-article-buffer t))
7179                            (or (not (string-match "^\\^X-Face:" regexp))
7180                                (with-current-buffer gnus-article-buffer
7181                                  gnus-summary-search-article-matched-data)))))
7182             (gnus-summary-select-article nil t)
7183             (setq treated nil))))
7184       (set-buffer gnus-article-buffer)
7185       (widen)
7186       (if treated
7187           (progn
7188             (gnus-article-show-all-headers)
7189             (gnus-summary-search-article-position-point regexp backward))
7190         (goto-char (if backward (point-max) (point-min))))
7191       (while (not found)
7192         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7193         (if (if backward
7194                 (re-search-backward regexp nil t)
7195               (re-search-forward regexp nil t))
7196             ;; We found the regexp.
7197             (progn
7198               (gnus-summary-search-article-highlight-matched-text
7199                backward treated (string-match "^\\^X-Face:" regexp))
7200               (setq found 'found)
7201               (forward-line
7202                (/ (- 2 (window-height
7203                         (get-buffer-window gnus-article-buffer t)))
7204                   2))
7205               (set-window-start
7206                (get-buffer-window (current-buffer))
7207                (point))
7208               (set-buffer sum)
7209               (setq point (point)))
7210           ;; We didn't find it, so we go to the next article.
7211           (set-buffer sum)
7212           (setq found 'not)
7213           (while (eq found 'not)
7214             (if (not (if backward (gnus-summary-find-prev)
7215                        (gnus-summary-find-next)))
7216                 ;; No more articles.
7217                 (setq found t)
7218               ;; Select the next article and adjust point.
7219               (unless (gnus-summary-article-sparse-p
7220                        (gnus-summary-article-number))
7221                 (setq found nil)
7222                 (let ((gnus-inhibit-treatment t))
7223                   (gnus-summary-select-article))
7224                 (setq treated nil)
7225                 (set-buffer gnus-article-buffer)
7226                 (widen)
7227                 (goto-char (if backward (point-max) (point-min))))))))
7228       (gnus-message 7 ""))
7229     ;; Return whether we found the regexp.
7230     (when (eq found 'found)
7231       (goto-char point)
7232       (gnus-summary-show-thread)
7233       (gnus-summary-goto-subject gnus-current-article)
7234       (gnus-summary-position-point)
7235       t)))
7236
7237 (defun gnus-summary-find-matching (header regexp &optional backward unread
7238                                           not-case-fold)
7239   "Return a list of all articles that match REGEXP on HEADER.
7240 The search stars on the current article and goes forwards unless
7241 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7242 If UNREAD is non-nil, only unread articles will
7243 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7244 in the comparisons."
7245   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7246                 (gnus-data-find-list
7247                  (gnus-summary-article-number) (gnus-data-list backward))))
7248         (case-fold-search (not not-case-fold))
7249         articles d func)
7250     (if (consp header)
7251         (if (eq (car header) 'extra)
7252             (setq func
7253                   `(lambda (h)
7254                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7255                          "")))
7256           (error "%s is an invalid header" header))
7257       (unless (fboundp (intern (concat "mail-header-" header)))
7258         (error "%s is not a valid header" header))
7259       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7260     (while data
7261       (setq d (car data))
7262       (and (or (not unread)             ; We want all articles...
7263                (gnus-data-unread-p d))  ; Or just unreads.
7264            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7265            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7266            (push (gnus-data-number d) articles)) ; Success!
7267       (setq data (cdr data)))
7268     (nreverse articles)))
7269
7270 (defun gnus-summary-execute-command (header regexp command &optional backward)
7271   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7272 If HEADER is an empty string (or nil), the match is done on the entire
7273 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7274   (interactive
7275    (list (let ((completion-ignore-case t))
7276            (completing-read
7277             "Header name: "
7278             (mapcar (lambda (string) (list string))
7279                     '("Number" "Subject" "From" "Lines" "Date"
7280                       "Message-ID" "Xref" "References" "Body"))
7281             nil 'require-match))
7282          (read-string "Regexp: ")
7283          (read-key-sequence "Command: ")
7284          current-prefix-arg))
7285   (when (equal header "Body")
7286     (setq header ""))
7287   ;; Hidden thread subtrees must be searched as well.
7288   (gnus-summary-show-all-threads)
7289   ;; We don't want to change current point nor window configuration.
7290   (save-excursion
7291     (save-window-excursion
7292       (gnus-message 6 "Executing %s..." (key-description command))
7293       ;; We'd like to execute COMMAND interactively so as to give arguments.
7294       (gnus-execute header regexp
7295                     `(call-interactively ',(key-binding command))
7296                     backward)
7297       (gnus-message 6 "Executing %s...done" (key-description command)))))
7298
7299 (defun gnus-summary-beginning-of-article ()
7300   "Scroll the article back to the beginning."
7301   (interactive)
7302   (gnus-summary-select-article)
7303   (gnus-configure-windows 'article)
7304   (gnus-eval-in-buffer-window gnus-article-buffer
7305     (widen)
7306     (goto-char (point-min))
7307     (when gnus-page-broken
7308       (gnus-narrow-to-page))))
7309
7310 (defun gnus-summary-end-of-article ()
7311   "Scroll to the end of the article."
7312   (interactive)
7313   (gnus-summary-select-article)
7314   (gnus-configure-windows 'article)
7315   (gnus-eval-in-buffer-window gnus-article-buffer
7316     (widen)
7317     (goto-char (point-max))
7318     (recenter -3)
7319     (when gnus-page-broken
7320       (gnus-narrow-to-page))))
7321
7322 (defun gnus-summary-print-article (&optional filename n)
7323   "Generate and print a PostScript image of the N next (mail) articles.
7324
7325 If N is negative, print the N previous articles.  If N is nil and articles
7326 have been marked with the process mark, print these instead.
7327
7328 If the optional first argument FILENAME is nil, send the image to the
7329 printer.  If FILENAME is a string, save the PostScript image in a file with
7330 that name.  If FILENAME is a number, prompt the user for the name of the file
7331 to save in."
7332   (interactive (list (ps-print-preprint current-prefix-arg)
7333                      current-prefix-arg))
7334   (dolist (article (gnus-summary-work-articles n))
7335     (gnus-summary-select-article nil nil 'pseudo article)
7336     (gnus-eval-in-buffer-window gnus-article-buffer
7337       (let ((buffer (generate-new-buffer " *print*")))
7338         (unwind-protect
7339             (progn
7340               (copy-to-buffer buffer (point-min) (point-max))
7341               (set-buffer buffer)
7342               (gnus-article-delete-invisible-text)
7343               (let ((ps-left-header
7344                      (list
7345                       (concat "("
7346                               (mail-header-subject gnus-current-headers) ")")
7347                       (concat "("
7348                               (mail-header-from gnus-current-headers) ")")))
7349                     (ps-right-header
7350                      (list
7351                       "/pagenumberstring load"
7352                       (concat "("
7353                               (mail-header-date gnus-current-headers) ")"))))
7354                 (gnus-run-hooks 'gnus-ps-print-hook)
7355                 (save-excursion
7356                   (ps-print-buffer-with-faces filename))))
7357           (kill-buffer buffer))))))
7358
7359 (defun gnus-summary-show-article (&optional arg)
7360   "Force re-fetching of the current article.
7361 If ARG (the prefix) is a number, show the article with the charset 
7362 defined in `gnus-summary-show-article-charset-alist', or the charset
7363 inputed.
7364 If ARG (the prefix) is non-nil and not a number, show the raw article 
7365 without any article massaging functions being run."
7366   (interactive "P")
7367   (cond 
7368    ((numberp arg)
7369     (let ((gnus-newsgroup-charset 
7370            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7371                (read-coding-system "Charset: ")))
7372           (gnus-newsgroup-ignored-charsets 'gnus-all))
7373       (gnus-summary-select-article nil 'force)))
7374    ((not arg)
7375     ;; Select the article the normal way.
7376     (gnus-summary-select-article nil 'force))
7377    (t
7378     ;; We have to require this here to make sure that the following
7379     ;; dynamic binding isn't shadowed by autoloading.
7380     (require 'gnus-async)
7381     (require 'gnus-art)
7382     ;; Bind the article treatment functions to nil.
7383     (let ((gnus-have-all-headers t)
7384           gnus-article-display-hook
7385           gnus-article-prepare-hook
7386           gnus-article-decode-hook
7387           gnus-break-pages
7388           gnus-show-mime)
7389       (gnus-summary-select-article nil 'force))))
7390   (gnus-summary-goto-subject gnus-current-article)
7391   (gnus-summary-position-point))
7392
7393 (defun gnus-summary-verbose-headers (&optional arg)
7394   "Toggle permanent full header display.
7395 If ARG is a positive number, turn header display on.
7396 If ARG is a negative number, turn header display off."
7397   (interactive "P")
7398   (setq gnus-show-all-headers
7399         (cond ((or (not (numberp arg))
7400                    (zerop arg))
7401                (not gnus-show-all-headers))
7402               ((natnump arg)
7403                t)))
7404   (gnus-summary-show-article))
7405
7406 (defun gnus-summary-toggle-header (&optional arg)
7407   "Show the headers if they are hidden, or hide them if they are shown.
7408 If ARG is a positive number, show the entire header.
7409 If ARG is a negative number, hide the unwanted header lines."
7410   (interactive "P")
7411   (save-excursion
7412     (set-buffer gnus-article-buffer)
7413     (save-restriction
7414       (let* ((buffer-read-only nil)
7415              (inhibit-point-motion-hooks t)
7416              hidden e)
7417         (setq hidden
7418               (if (numberp arg)
7419                   (>= arg 0)
7420                 (save-restriction 
7421                   (article-narrow-to-head)
7422                   (gnus-article-hidden-text-p 'headers))))
7423         (goto-char (point-min))
7424         (when (search-forward "\n\n" nil t)
7425           (delete-region (point-min) (1- (point))))
7426         (goto-char (point-min))
7427         (save-excursion
7428           (set-buffer gnus-original-article-buffer)
7429           (goto-char (point-min))
7430           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7431         (insert-buffer-substring gnus-original-article-buffer 1 e)
7432         (save-restriction
7433           (narrow-to-region (point-min) (point))
7434           (article-decode-encoded-words)
7435           (if  hidden
7436               (let ((gnus-treat-hide-headers nil)
7437                     (gnus-treat-hide-boring-headers nil))
7438                 (setq gnus-article-wash-types
7439                       (delq 'headers gnus-article-wash-types))
7440                 (gnus-treat-article 'head))
7441             (gnus-treat-article 'head)))
7442         (gnus-set-mode-line 'article)))))
7443
7444 (defun gnus-summary-show-all-headers ()
7445   "Make all header lines visible."
7446   (interactive)
7447   (gnus-article-show-all-headers))
7448
7449 (defun gnus-summary-toggle-mime (&optional arg)
7450   "Toggle MIME processing.
7451 If ARG is a positive number, turn MIME processing on."
7452   (interactive "P")
7453   (setq gnus-show-mime
7454         (if (null arg)
7455             (not gnus-show-mime)
7456           (> (prefix-numeric-value arg) 0)))
7457   (gnus-summary-select-article t 'force))
7458
7459 (defun gnus-summary-caesar-message (&optional arg)
7460   "Caesar rotate the current article by 13.
7461 The numerical prefix specifies how many places to rotate each letter
7462 forward."
7463   (interactive "P")
7464   (gnus-summary-select-article)
7465   (let ((mail-header-separator ""))
7466     (gnus-eval-in-buffer-window gnus-article-buffer
7467       (save-restriction
7468         (widen)
7469         (let ((start (window-start))
7470               buffer-read-only)
7471           (message-caesar-buffer-body arg)
7472           (set-window-start (get-buffer-window (current-buffer)) start))))))
7473
7474 (defun gnus-summary-stop-page-breaking ()
7475   "Stop page breaking in the current article."
7476   (interactive)
7477   (gnus-summary-select-article)
7478   (gnus-eval-in-buffer-window gnus-article-buffer
7479     (widen)
7480     (when (gnus-visual-p 'page-marker)
7481       (let ((buffer-read-only nil))
7482         (gnus-remove-text-with-property 'gnus-prev)
7483         (gnus-remove-text-with-property 'gnus-next))
7484       (setq gnus-page-broken nil))))
7485
7486 (defun gnus-summary-move-article (&optional n to-newsgroup
7487                                             select-method action)
7488   "Move the current article to a different newsgroup.
7489 If N is a positive number, move the N next articles.
7490 If N is a negative number, move the N previous articles.
7491 If N is nil and any articles have been marked with the process mark,
7492 move those articles instead.
7493 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7494 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7495 re-spool using this method.
7496
7497 For this function to work, both the current newsgroup and the
7498 newsgroup that you want to move to have to support the `request-move'
7499 and `request-accept' functions.
7500
7501 ACTION can be either `move' (the default), `crosspost' or `copy'."
7502   (interactive "P")
7503   (unless action
7504     (setq action 'move))
7505   ;; Disable marking as read.
7506   (let (gnus-mark-article-hook)
7507     (save-window-excursion
7508       (gnus-summary-select-article)))
7509   ;; Check whether the source group supports the required functions.
7510   (cond ((and (eq action 'move)
7511               (not (gnus-check-backend-function
7512                     'request-move-article gnus-newsgroup-name)))
7513          (error "The current group does not support article moving"))
7514         ((and (eq action 'crosspost)
7515               (not (gnus-check-backend-function
7516                     'request-replace-article gnus-newsgroup-name)))
7517          (error "The current group does not support article editing")))
7518   (let ((articles (gnus-summary-work-articles n))
7519         (prefix (if (gnus-check-backend-function
7520                     'request-move-article gnus-newsgroup-name)
7521                     (gnus-group-real-prefix gnus-newsgroup-name)
7522                   ""))
7523         (names '((move "Move" "Moving")
7524                  (copy "Copy" "Copying")
7525                  (crosspost "Crosspost" "Crossposting")))
7526         (copy-buf (save-excursion
7527                     (nnheader-set-temp-buffer " *copy article*")))
7528         (default-marks gnus-article-mark-lists)
7529         (no-expire-marks (delete '(expirable . expire)
7530                                  (copy-sequence gnus-article-mark-lists)))
7531         art-group to-method new-xref article to-groups)
7532     (unless (assq action names)
7533       (error "Unknown action %s" action))
7534     ;; Read the newsgroup name.
7535     (when (and (not to-newsgroup)
7536                (not select-method))
7537       (setq to-newsgroup
7538             (gnus-read-move-group-name
7539              (cadr (assq action names))
7540              (symbol-value (intern (format "gnus-current-%s-group" action)))
7541              articles prefix))
7542       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7543     (setq to-method (or select-method
7544                         (gnus-server-to-method
7545                          (gnus-group-method to-newsgroup))))
7546     ;; Check the method we are to move this article to...
7547     (unless (gnus-check-backend-function
7548              'request-accept-article (car to-method))
7549       (error "%s does not support article copying" (car to-method)))
7550     (unless (gnus-check-server to-method)
7551       (error "Can't open server %s" (car to-method)))
7552     (gnus-message 6 "%s to %s: %s..."
7553                   (caddr (assq action names))
7554                   (or (car select-method) to-newsgroup) articles)
7555     (while articles
7556       (setq article (pop articles))
7557       (setq
7558        art-group
7559        (cond
7560         ;; Move the article.
7561         ((eq action 'move)
7562          ;; Remove this article from future suppression.
7563          (gnus-dup-unsuppress-article article)
7564          (gnus-request-move-article
7565           article                       ; Article to move
7566           gnus-newsgroup-name           ; From newsgroup
7567           (nth 1 (gnus-find-method-for-group
7568                   gnus-newsgroup-name)) ; Server
7569           (list 'gnus-request-accept-article
7570                 to-newsgroup (list 'quote select-method)
7571                 (not articles) t)       ; Accept form
7572           (not articles)))              ; Only save nov last time
7573         ;; Copy the article.
7574         ((eq action 'copy)
7575          (save-excursion
7576            (set-buffer copy-buf)
7577            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7578              (gnus-request-accept-article
7579               to-newsgroup select-method (not articles) t))))
7580         ;; Crosspost the article.
7581         ((eq action 'crosspost)
7582          (let ((xref (message-tokenize-header
7583                       (mail-header-xref (gnus-summary-article-header article))
7584                       " ")))
7585            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7586                                   ":" article))
7587            (unless xref
7588              (setq xref (list (system-name))))
7589            (setq new-xref
7590                  (concat
7591                   (mapconcat 'identity
7592                              (delete "Xref:" (delete new-xref xref))
7593                              " ")
7594                   " " new-xref))
7595            (save-excursion
7596              (set-buffer copy-buf)
7597              ;; First put the article in the destination group.
7598              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7599              (when (consp (setq art-group
7600                                 (gnus-request-accept-article
7601                                  to-newsgroup select-method (not articles))))
7602                (setq new-xref (concat new-xref " " (car art-group)
7603                                       ":" (cdr art-group)))
7604                ;; Now we have the new Xrefs header, so we insert
7605                ;; it and replace the new article.
7606                (nnheader-replace-header "Xref" new-xref)
7607                (gnus-request-replace-article
7608                 (cdr art-group) to-newsgroup (current-buffer))
7609                art-group))))))
7610       (cond
7611        ((not art-group)
7612         (gnus-message 1 "Couldn't %s article %s: %s"
7613                       (cadr (assq action names)) article
7614                       (nnheader-get-report (car to-method))))
7615        ((eq art-group 'junk)
7616         (when (eq action 'move)
7617           (gnus-summary-mark-article article gnus-canceled-mark)
7618           (gnus-message 4 "Deleted article %s" article)))
7619        (t
7620         (let* ((pto-group (gnus-group-prefixed-name
7621                            (car art-group) to-method))
7622                (entry
7623                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7624                (info (nth 2 entry))
7625                (to-group (gnus-info-group info))
7626                to-marks)
7627           ;; Update the group that has been moved to.
7628           (when (and info
7629                      (memq action '(move copy)))
7630             (unless (member to-group to-groups)
7631               (push to-group to-groups))
7632
7633             (unless (memq article gnus-newsgroup-unreads)
7634               (push 'read to-marks)
7635               (gnus-info-set-read
7636                info (gnus-add-to-range (gnus-info-read info)
7637                                        (list (cdr art-group)))))
7638
7639             ;; See whether the article is to be put in the cache.
7640             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7641                              default-marks
7642                            no-expire-marks))
7643                   (to-article (cdr art-group)))
7644
7645               ;; Enter the article into the cache in the new group,
7646               ;; if that is required.
7647               (when gnus-use-cache
7648                 (gnus-cache-possibly-enter-article
7649                  to-group to-article
7650                  (let ((header (copy-sequence
7651                                 (gnus-summary-article-header article))))
7652                    (mail-header-set-number header to-article)
7653                    header)
7654                  (memq article gnus-newsgroup-marked)
7655                  (memq article gnus-newsgroup-dormant)
7656                  (memq article gnus-newsgroup-unreads)))
7657
7658               (when gnus-preserve-marks 
7659                 ;; Copy any marks over to the new group.
7660                 (when (and (equal to-group gnus-newsgroup-name)
7661                            (not (memq article gnus-newsgroup-unreads)))
7662                   ;; Mark this article as read in this group.
7663                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7664                   (setcdr (gnus-active to-group) to-article)
7665                   (setcdr gnus-newsgroup-active to-article))
7666
7667                 (while marks
7668                   (when (memq article (symbol-value
7669                                        (intern (format "gnus-newsgroup-%s"
7670                                                        (caar marks)))))
7671                     (push (cdar marks) to-marks)
7672                     ;; If the other group is the same as this group,
7673                     ;; then we have to add the mark to the list.
7674                     (when (equal to-group gnus-newsgroup-name)
7675                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7676                            (cons to-article
7677                                  (symbol-value
7678                                   (intern (format "gnus-newsgroup-%s"
7679                                                   (caar marks)))))))
7680                     ;; Copy the marks to other group.
7681                     (gnus-add-marked-articles
7682                      to-group (cdar marks) (list to-article) info))
7683                   (setq marks (cdr marks)))
7684
7685                 (gnus-request-set-mark to-group (list (list (list to-article)
7686                                                             'set
7687                                                             to-marks))))
7688
7689               (gnus-dribble-enter
7690                (concat "(gnus-group-set-info '"
7691                        (gnus-prin1-to-string (gnus-get-info to-group))
7692                        ")"))))
7693
7694           ;; Update the Xref header in this article to point to
7695           ;; the new crossposted article we have just created.
7696           (when (eq action 'crosspost)
7697             (save-excursion
7698               (set-buffer copy-buf)
7699               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7700               (nnheader-replace-header "Xref" new-xref)
7701               (gnus-request-replace-article
7702                article gnus-newsgroup-name (current-buffer)))))
7703
7704         ;;;!!!Why is this necessary?
7705         (set-buffer gnus-summary-buffer)
7706
7707         (gnus-summary-goto-subject article)
7708         (when (eq action 'move)
7709           (gnus-summary-mark-article article gnus-canceled-mark))))
7710       (gnus-summary-remove-process-mark article))
7711     ;; Re-activate all groups that have been moved to.
7712     (while to-groups
7713       (save-excursion
7714         (set-buffer gnus-group-buffer)
7715         (when (gnus-group-goto-group (car to-groups) t)
7716           (gnus-group-get-new-news-this-group 1 t))
7717         (pop to-groups)))
7718
7719     (gnus-kill-buffer copy-buf)
7720     (gnus-summary-position-point)
7721     (gnus-set-mode-line 'summary)))
7722
7723 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7724   "Move the current article to a different newsgroup.
7725 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7726 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7727 re-spool using this method."
7728   (interactive "P")
7729   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7730
7731 (defun gnus-summary-crosspost-article (&optional n)
7732   "Crosspost the current article to some other group."
7733   (interactive "P")
7734   (gnus-summary-move-article n nil nil 'crosspost))
7735
7736 (defcustom gnus-summary-respool-default-method nil
7737   "Default method for respooling an article.
7738 If nil, use to the current newsgroup method."
7739   :type '(choice (gnus-select-method :value (nnml ""))
7740                  (const nil))
7741   :group 'gnus-summary-mail)
7742
7743 (defun gnus-summary-respool-article (&optional n method)
7744   "Respool the current article.
7745 The article will be squeezed through the mail spooling process again,
7746 which means that it will be put in some mail newsgroup or other
7747 depending on `nnmail-split-methods'.
7748 If N is a positive number, respool the N next articles.
7749 If N is a negative number, respool the N previous articles.
7750 If N is nil and any articles have been marked with the process mark,
7751 respool those articles instead.
7752
7753 Respooling can be done both from mail groups and \"real\" newsgroups.
7754 In the former case, the articles in question will be moved from the
7755 current group into whatever groups they are destined to.  In the
7756 latter case, they will be copied into the relevant groups."
7757   (interactive
7758    (list current-prefix-arg
7759          (let* ((methods (gnus-methods-using 'respool))
7760                 (methname
7761                  (symbol-name (or gnus-summary-respool-default-method
7762                                   (car (gnus-find-method-for-group
7763                                         gnus-newsgroup-name)))))
7764                 (method
7765                  (gnus-completing-read
7766                   methname "What backend do you want to use when respooling?"
7767                   methods nil t nil 'gnus-mail-method-history))
7768                 ms)
7769            (cond
7770             ((zerop (length (setq ms (gnus-servers-using-backend
7771                                       (intern method)))))
7772              (list (intern method) ""))
7773             ((= 1 (length ms))
7774              (car ms))
7775             (t
7776              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7777                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7778                            ms-alist))))))))
7779   (unless method
7780     (error "No method given for respooling"))
7781   (if (assoc (symbol-name
7782               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7783              (gnus-methods-using 'respool))
7784       (gnus-summary-move-article n nil method)
7785     (gnus-summary-copy-article n nil method)))
7786
7787 (defun gnus-summary-import-article (file)
7788   "Import an arbitrary file into a mail newsgroup."
7789   (interactive "fImport file: ")
7790   (let ((group gnus-newsgroup-name)
7791         (now (current-time))
7792         atts lines)
7793     (unless (gnus-check-backend-function 'request-accept-article group)
7794       (error "%s does not support article importing" group))
7795     (or (file-readable-p file)
7796         (not (file-regular-p file))
7797         (error "Can't read %s" file))
7798     (save-excursion
7799       (set-buffer (gnus-get-buffer-create " *import file*"))
7800       (erase-buffer)
7801       (nnheader-insert-file-contents file)
7802       (goto-char (point-min))
7803       (unless (nnheader-article-p)
7804         ;; This doesn't look like an article, so we fudge some headers.
7805         (setq atts (file-attributes file)
7806               lines (count-lines (point-min) (point-max)))
7807         (insert "From: " (read-string "From: ") "\n"
7808                 "Subject: " (read-string "Subject: ") "\n"
7809                 "Date: " (message-make-date (nth 5 atts))
7810                 "\n"
7811                 "Message-ID: " (message-make-message-id) "\n"
7812                 "Lines: " (int-to-string lines) "\n"
7813                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7814       (gnus-request-accept-article group nil t)
7815       (kill-buffer (current-buffer)))))
7816
7817 (defun gnus-summary-article-posted-p ()
7818   "Say whether the current (mail) article is available from news as well.
7819 This will be the case if the article has both been mailed and posted."
7820   (interactive)
7821   (let ((id (mail-header-references (gnus-summary-article-header)))
7822         (gnus-override-method (car (gnus-refer-article-methods))))
7823     (if (gnus-request-head id "")
7824         (gnus-message 2 "The current message was found on %s"
7825                       gnus-override-method)
7826       (gnus-message 2 "The current message couldn't be found on %s"
7827                     gnus-override-method)
7828       nil)))
7829
7830 (defun gnus-summary-expire-articles (&optional now)
7831   "Expire all articles that are marked as expirable in the current group."
7832   (interactive)
7833   (when (gnus-check-backend-function
7834          'request-expire-articles gnus-newsgroup-name)
7835     ;; This backend supports expiry.
7836     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7837            (expirable (if total
7838                           (progn
7839                             ;; We need to update the info for
7840                             ;; this group for `gnus-list-of-read-articles'
7841                             ;; to give us the right answer.
7842                             (gnus-run-hooks 'gnus-exit-group-hook)
7843                             (gnus-summary-update-info)
7844                             (gnus-list-of-read-articles gnus-newsgroup-name))
7845                         (setq gnus-newsgroup-expirable
7846                               (sort gnus-newsgroup-expirable '<))))
7847            (expiry-wait (if now 'immediate
7848                           (gnus-group-find-parameter
7849                            gnus-newsgroup-name 'expiry-wait)))
7850            es)
7851       (when expirable
7852         ;; There are expirable articles in this group, so we run them
7853         ;; through the expiry process.
7854         (gnus-message 6 "Expiring articles...")
7855         (unless (gnus-check-group gnus-newsgroup-name)
7856           (error "Can't open server for %s" gnus-newsgroup-name))
7857         ;; The list of articles that weren't expired is returned.
7858         (save-excursion
7859           (if expiry-wait
7860               (let ((nnmail-expiry-wait-function nil)
7861                     (nnmail-expiry-wait expiry-wait))
7862                 (setq es (gnus-request-expire-articles
7863                           expirable gnus-newsgroup-name)))
7864             (setq es (gnus-request-expire-articles
7865                       expirable gnus-newsgroup-name))))
7866         (unless total
7867           (setq gnus-newsgroup-expirable es))
7868         ;; We go through the old list of expirable, and mark all
7869         ;; really expired articles as nonexistent.
7870         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7871           (let ((gnus-use-cache nil))
7872             (while expirable
7873               (unless (memq (car expirable) es)
7874                 (when (gnus-data-find (car expirable))
7875                   (gnus-summary-mark-article
7876                    (car expirable) gnus-canceled-mark)))
7877               (setq expirable (cdr expirable)))))
7878         (gnus-message 6 "Expiring articles...done")))))
7879
7880 (defun gnus-summary-expire-articles-now ()
7881   "Expunge all expirable articles in the current group.
7882 This means that *all* articles that are marked as expirable will be
7883 deleted forever, right now."
7884   (interactive)
7885   (or gnus-expert-user
7886       (gnus-yes-or-no-p
7887        "Are you really, really, really sure you want to delete all these messages? ")
7888       (error "Phew!"))
7889   (gnus-summary-expire-articles t))
7890
7891 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7892 (defun gnus-summary-delete-article (&optional n)
7893   "Delete the N next (mail) articles.
7894 This command actually deletes articles.  This is not a marking
7895 command.  The article will disappear forever from your life, never to
7896 return.
7897 If N is negative, delete backwards.
7898 If N is nil and articles have been marked with the process mark,
7899 delete these instead."
7900   (interactive "P")
7901   (unless (gnus-check-backend-function 'request-expire-articles
7902                                        gnus-newsgroup-name)
7903     (error "The current newsgroup does not support article deletion"))
7904   ;; Compute the list of articles to delete.
7905   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7906         not-deleted)
7907     (if (and gnus-novice-user
7908              (not (gnus-yes-or-no-p
7909                    (format "Do you really want to delete %s forever? "
7910                            (if (> (length articles) 1)
7911                                (format "these %s articles" (length articles))
7912                              "this article")))))
7913         ()
7914       ;; Delete the articles.
7915       (setq not-deleted (gnus-request-expire-articles
7916                          articles gnus-newsgroup-name 'force))
7917       (while articles
7918         (gnus-summary-remove-process-mark (car articles))
7919         ;; The backend might not have been able to delete the article
7920         ;; after all.
7921         (unless (memq (car articles) not-deleted)
7922           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7923         (setq articles (cdr articles)))
7924       (when not-deleted
7925         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7926     (gnus-summary-position-point)
7927     (gnus-set-mode-line 'summary)
7928     not-deleted))
7929
7930 (defun gnus-summary-edit-article (&optional force)
7931   "Edit the current article.
7932 This will have permanent effect only in mail groups.
7933 If FORCE is non-nil, allow editing of articles even in read-only
7934 groups."
7935   (interactive "P")
7936   (save-excursion
7937     (set-buffer gnus-summary-buffer)
7938     (let ((mail-parse-charset gnus-newsgroup-charset)
7939           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7940       (gnus-set-global-variables)
7941       (when (and (not force)
7942                  (gnus-group-read-only-p))
7943         (error "The current newsgroup does not support article editing"))
7944       (gnus-summary-show-article t)
7945       (gnus-article-edit-article
7946        'ignore
7947        `(lambda (no-highlight)
7948           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7949                 (mail-parse-ignored-charsets
7950                  ',gnus-newsgroup-ignored-charsets))
7951             (gnus-summary-edit-article-done
7952              ,(or (mail-header-references gnus-current-headers) "")
7953              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7954
7955 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7956
7957 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7958                                                  no-highlight)
7959   "Make edits to the current article permanent."
7960   (interactive)
7961   ;; Replace the article.
7962   (let ((buf (current-buffer)))
7963     (with-temp-buffer
7964       (insert-buffer-substring buf)
7965       (if (and (not read-only)
7966                (not (gnus-request-replace-article
7967                      (cdr gnus-article-current) (car gnus-article-current)
7968                      (current-buffer) t)))
7969           (error "Couldn't replace article")
7970         ;; Update the summary buffer.
7971         (if (and references
7972                  (equal (message-tokenize-header references " ")
7973                         (message-tokenize-header
7974                          (or (message-fetch-field "references") "") " ")))
7975             ;; We only have to update this line.
7976             (save-excursion
7977               (save-restriction
7978                 (message-narrow-to-head)
7979                 (let ((head (buffer-string))
7980                       header)
7981                   (with-temp-buffer
7982                     (insert (format "211 %d Article retrieved.\n"
7983                                     (cdr gnus-article-current)))
7984                     (insert head)
7985                     (insert ".\n")
7986                     (let ((nntp-server-buffer (current-buffer)))
7987                       (setq header (car (gnus-get-newsgroup-headers
7988                                          (save-excursion
7989                                            (set-buffer gnus-summary-buffer)
7990                                            gnus-newsgroup-dependencies)
7991                                          t))))
7992                     (save-excursion
7993                       (set-buffer gnus-summary-buffer)
7994                       (gnus-data-set-header
7995                        (gnus-data-find (cdr gnus-article-current))
7996                        header)
7997                       (gnus-summary-update-article-line
7998                        (cdr gnus-article-current) header))))))
7999           ;; Update threads.
8000           (set-buffer (or buffer gnus-summary-buffer))
8001           (gnus-summary-update-article (cdr gnus-article-current)))
8002         ;; Prettify the article buffer again.
8003         (unless no-highlight
8004           (save-excursion
8005             (set-buffer gnus-article-buffer)
8006             ;;;!!! Fix this -- article should be rehighlighted.
8007             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8008             (set-buffer gnus-original-article-buffer)
8009             (gnus-request-article
8010              (cdr gnus-article-current)
8011              (car gnus-article-current) (current-buffer))))
8012         ;; Prettify the summary buffer line.
8013         (when (gnus-visual-p 'summary-highlight 'highlight)
8014           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8015
8016 (defun gnus-summary-edit-wash (key)
8017   "Perform editing command KEY in the article buffer."
8018   (interactive
8019    (list
8020     (progn
8021       (message "%s" (concat (this-command-keys) "- "))
8022       (read-char))))
8023   (message "")
8024   (gnus-summary-edit-article)
8025   (execute-kbd-macro (concat (this-command-keys) key))
8026   (gnus-article-edit-done))
8027
8028 ;;; Respooling
8029
8030 (defun gnus-summary-respool-query (&optional silent trace)
8031   "Query where the respool algorithm would put this article."
8032   (interactive)
8033   (let (gnus-mark-article-hook)
8034     (gnus-summary-select-article)
8035     (save-excursion
8036       (set-buffer gnus-original-article-buffer)
8037       (save-restriction
8038         (message-narrow-to-head)
8039         (let ((groups (nnmail-article-group 'identity trace)))
8040           (unless silent
8041             (if groups
8042                 (message "This message would go to %s"
8043                          (mapconcat 'car groups ", "))
8044               (message "This message would go to no groups"))
8045             groups))))))
8046
8047 (defun gnus-summary-respool-trace ()
8048   "Trace where the respool algorithm would put this article.
8049 Display a buffer showing all fancy splitting patterns which matched."
8050   (interactive)
8051   (gnus-summary-respool-query nil t))
8052
8053 ;; Summary marking commands.
8054
8055 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8056   "Mark articles which has the same subject as read, and then select the next.
8057 If UNMARK is positive, remove any kind of mark.
8058 If UNMARK is negative, tick articles."
8059   (interactive "P")
8060   (when unmark
8061     (setq unmark (prefix-numeric-value unmark)))
8062   (let ((count
8063          (gnus-summary-mark-same-subject
8064           (gnus-summary-article-subject) unmark)))
8065     ;; Select next unread article.  If auto-select-same mode, should
8066     ;; select the first unread article.
8067     (gnus-summary-next-article t (and gnus-auto-select-same
8068                                       (gnus-summary-article-subject)))
8069     (gnus-message 7 "%d article%s marked as %s"
8070                   count (if (= count 1) " is" "s are")
8071                   (if unmark "unread" "read"))))
8072
8073 (defun gnus-summary-kill-same-subject (&optional unmark)
8074   "Mark articles which has the same subject as read.
8075 If UNMARK is positive, remove any kind of mark.
8076 If UNMARK is negative, tick articles."
8077   (interactive "P")
8078   (when unmark
8079     (setq unmark (prefix-numeric-value unmark)))
8080   (let ((count
8081          (gnus-summary-mark-same-subject
8082           (gnus-summary-article-subject) unmark)))
8083     ;; If marked as read, go to next unread subject.
8084     (when (null unmark)
8085       ;; Go to next unread subject.
8086       (gnus-summary-next-subject 1 t))
8087     (gnus-message 7 "%d articles are marked as %s"
8088                   count (if unmark "unread" "read"))))
8089
8090 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8091   "Mark articles with same SUBJECT as read, and return marked number.
8092 If optional argument UNMARK is positive, remove any kinds of marks.
8093 If optional argument UNMARK is negative, mark articles as unread instead."
8094   (let ((count 1))
8095     (save-excursion
8096       (cond
8097        ((null unmark)                   ; Mark as read.
8098         (while (and
8099                 (progn
8100                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8101                   (gnus-summary-show-thread) t)
8102                 (gnus-summary-find-subject subject))
8103           (setq count (1+ count))))
8104        ((> unmark 0)                    ; Tick.
8105         (while (and
8106                 (progn
8107                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8108                   (gnus-summary-show-thread) t)
8109                 (gnus-summary-find-subject subject))
8110           (setq count (1+ count))))
8111        (t                               ; Mark as unread.
8112         (while (and
8113                 (progn
8114                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8115                   (gnus-summary-show-thread) t)
8116                 (gnus-summary-find-subject subject))
8117           (setq count (1+ count)))))
8118       (gnus-set-mode-line 'summary)
8119       ;; Return the number of marked articles.
8120       count)))
8121
8122 (defun gnus-summary-mark-as-processable (n &optional unmark)
8123   "Set the process mark on the next N articles.
8124 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8125 the process mark instead.  The difference between N and the actual
8126 number of articles marked is returned."
8127   (interactive "p")
8128   (let ((backward (< n 0))
8129         (n (abs n)))
8130     (while (and
8131             (> n 0)
8132             (if unmark
8133                 (gnus-summary-remove-process-mark
8134                  (gnus-summary-article-number))
8135               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8136             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8137       (setq n (1- n)))
8138     (when (/= 0 n)
8139       (gnus-message 7 "No more articles"))
8140     (gnus-summary-recenter)
8141     (gnus-summary-position-point)
8142     n))
8143
8144 (defun gnus-summary-unmark-as-processable (n)
8145   "Remove the process mark from the next N articles.
8146 If N is negative, unmark backward instead.  The difference between N and
8147 the actual number of articles unmarked is returned."
8148   (interactive "p")
8149   (gnus-summary-mark-as-processable n t))
8150
8151 (defun gnus-summary-unmark-all-processable ()
8152   "Remove the process mark from all articles."
8153   (interactive)
8154   (save-excursion
8155     (while gnus-newsgroup-processable
8156       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8157   (gnus-summary-position-point))
8158
8159 (defun gnus-summary-mark-as-expirable (n)
8160   "Mark N articles forward as expirable.
8161 If N is negative, mark backward instead.  The difference between N and
8162 the actual number of articles marked is returned."
8163   (interactive "p")
8164   (gnus-summary-mark-forward n gnus-expirable-mark))
8165
8166 (defun gnus-summary-mark-article-as-replied (article)
8167   "Mark ARTICLE replied and update the summary line."
8168   (push article gnus-newsgroup-replied)
8169   (let ((buffer-read-only nil))
8170     (when (gnus-summary-goto-subject article nil t)
8171       (gnus-summary-update-secondary-mark article))))
8172
8173 (defun gnus-summary-set-bookmark (article)
8174   "Set a bookmark in current article."
8175   (interactive (list (gnus-summary-article-number)))
8176   (when (or (not (get-buffer gnus-article-buffer))
8177             (not gnus-current-article)
8178             (not gnus-article-current)
8179             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8180     (error "No current article selected"))
8181   ;; Remove old bookmark, if one exists.
8182   (let ((old (assq article gnus-newsgroup-bookmarks)))
8183     (when old
8184       (setq gnus-newsgroup-bookmarks
8185             (delq old gnus-newsgroup-bookmarks))))
8186   ;; Set the new bookmark, which is on the form
8187   ;; (article-number . line-number-in-body).
8188   (push
8189    (cons article
8190          (save-excursion
8191            (set-buffer gnus-article-buffer)
8192            (count-lines
8193             (min (point)
8194                  (save-excursion
8195                    (goto-char (point-min))
8196                    (search-forward "\n\n" nil t)
8197                    (point)))
8198             (point))))
8199    gnus-newsgroup-bookmarks)
8200   (gnus-message 6 "A bookmark has been added to the current article."))
8201
8202 (defun gnus-summary-remove-bookmark (article)
8203   "Remove the bookmark from the current article."
8204   (interactive (list (gnus-summary-article-number)))
8205   ;; Remove old bookmark, if one exists.
8206   (let ((old (assq article gnus-newsgroup-bookmarks)))
8207     (if old
8208         (progn
8209           (setq gnus-newsgroup-bookmarks
8210                 (delq old gnus-newsgroup-bookmarks))
8211           (gnus-message 6 "Removed bookmark."))
8212       (gnus-message 6 "No bookmark in current article."))))
8213
8214 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8215 (defun gnus-summary-mark-as-dormant (n)
8216   "Mark N articles forward as dormant.
8217 If N is negative, mark backward instead.  The difference between N and
8218 the actual number of articles marked is returned."
8219   (interactive "p")
8220   (gnus-summary-mark-forward n gnus-dormant-mark))
8221
8222 (defun gnus-summary-set-process-mark (article)
8223   "Set the process mark on ARTICLE and update the summary line."
8224   (setq gnus-newsgroup-processable
8225         (cons article
8226               (delq article gnus-newsgroup-processable)))
8227   (when (gnus-summary-goto-subject article)
8228     (gnus-summary-show-thread)
8229     (gnus-summary-goto-subject article)
8230     (gnus-summary-update-secondary-mark article)))
8231
8232 (defun gnus-summary-remove-process-mark (article)
8233   "Remove the process mark from ARTICLE and update the summary line."
8234   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8235   (when (gnus-summary-goto-subject article)
8236     (gnus-summary-show-thread)
8237     (gnus-summary-goto-subject article)
8238     (gnus-summary-update-secondary-mark article)))
8239
8240 (defun gnus-summary-set-saved-mark (article)
8241   "Set the process mark on ARTICLE and update the summary line."
8242   (push article gnus-newsgroup-saved)
8243   (when (gnus-summary-goto-subject article)
8244     (gnus-summary-update-secondary-mark article)))
8245
8246 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8247   "Mark N articles as read forwards.
8248 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8249 The difference between N and the actual number of articles marked is
8250 returned.
8251 Iff NO-EXPIRE, auto-expiry will be inhibited."
8252   (interactive "p")
8253   (gnus-summary-show-thread)
8254   (let ((backward (< n 0))
8255         (gnus-summary-goto-unread
8256          (and gnus-summary-goto-unread
8257               (not (eq gnus-summary-goto-unread 'never))
8258               (not (memq mark (list gnus-unread-mark
8259                                     gnus-ticked-mark gnus-dormant-mark)))))
8260         (n (abs n))
8261         (mark (or mark gnus-del-mark)))
8262     (while (and (> n 0)
8263                 (gnus-summary-mark-article nil mark no-expire)
8264                 (zerop (gnus-summary-next-subject
8265                         (if backward -1 1)
8266                         (and gnus-summary-goto-unread
8267                              (not (eq gnus-summary-goto-unread 'never)))
8268                         t)))
8269       (setq n (1- n)))
8270     (when (/= 0 n)
8271       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8272     (gnus-summary-recenter)
8273     (gnus-summary-position-point)
8274     (gnus-set-mode-line 'summary)
8275     n))
8276
8277 (defun gnus-summary-mark-article-as-read (mark)
8278   "Mark the current article quickly as read with MARK."
8279   (let ((article (gnus-summary-article-number)))
8280     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8281     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8282     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8283     (push (cons article mark) gnus-newsgroup-reads)
8284     ;; Possibly remove from cache, if that is used.
8285     (when gnus-use-cache
8286       (gnus-cache-enter-remove-article article))
8287     ;; Allow the backend to change the mark.
8288     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8289     ;; Check for auto-expiry.
8290     (when (and gnus-newsgroup-auto-expire
8291                (memq mark gnus-auto-expirable-marks))
8292       (setq mark gnus-expirable-mark)
8293       ;; Let the backend know about the mark change.
8294       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8295       (push article gnus-newsgroup-expirable))
8296     ;; Set the mark in the buffer.
8297     (gnus-summary-update-mark mark 'unread)
8298     t))
8299
8300 (defun gnus-summary-mark-article-as-unread (mark)
8301   "Mark the current article quickly as unread with MARK."
8302   (let* ((article (gnus-summary-article-number))
8303          (old-mark (gnus-summary-article-mark article)))
8304     ;; Allow the backend to change the mark.
8305     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8306     (if (eq mark old-mark)
8307         t
8308       (if (<= article 0)
8309           (progn
8310             (gnus-error 1 "Can't mark negative article numbers")
8311             nil)
8312         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8313         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8314         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8315         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8316         (cond ((= mark gnus-ticked-mark)
8317                (push article gnus-newsgroup-marked))
8318               ((= mark gnus-dormant-mark)
8319                (push article gnus-newsgroup-dormant))
8320               (t
8321                (push article gnus-newsgroup-unreads)))
8322         (gnus-pull article gnus-newsgroup-reads)
8323
8324         ;; See whether the article is to be put in the cache.
8325         (and gnus-use-cache
8326              (vectorp (gnus-summary-article-header article))
8327              (save-excursion
8328                (gnus-cache-possibly-enter-article
8329                 gnus-newsgroup-name article
8330                 (gnus-summary-article-header article)
8331                 (= mark gnus-ticked-mark)
8332                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8333
8334         ;; Fix the mark.
8335         (gnus-summary-update-mark mark 'unread)
8336         t))))
8337
8338 (defun gnus-summary-mark-article (&optional article mark no-expire)
8339   "Mark ARTICLE with MARK.  MARK can be any character.
8340 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8341 `??' (dormant) and `?E' (expirable).
8342 If MARK is nil, then the default character `?r' is used.
8343 If ARTICLE is nil, then the article on the current line will be
8344 marked.
8345 Iff NO-EXPIRE, auto-expiry will be inhibited."
8346   ;; The mark might be a string.
8347   (when (stringp mark)
8348     (setq mark (aref mark 0)))
8349   ;; If no mark is given, then we check auto-expiring.
8350   (when (null mark)
8351     (setq mark gnus-del-mark))
8352   (when (and (not no-expire)
8353              gnus-newsgroup-auto-expire
8354              (memq mark gnus-auto-expirable-marks))
8355     (setq mark gnus-expirable-mark))
8356   (let ((article (or article (gnus-summary-article-number)))
8357         (old-mark (gnus-summary-article-mark article)))
8358     ;; Allow the backend to change the mark.
8359     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8360     (if (eq mark old-mark)
8361         t
8362       (unless article
8363         (error "No article on current line"))
8364       (if (not (if (or (= mark gnus-unread-mark)
8365                        (= mark gnus-ticked-mark)
8366                        (= mark gnus-dormant-mark))
8367                    (gnus-mark-article-as-unread article mark)
8368                  (gnus-mark-article-as-read article mark)))
8369           t
8370         ;; See whether the article is to be put in the cache.
8371         (and gnus-use-cache
8372              (not (= mark gnus-canceled-mark))
8373              (vectorp (gnus-summary-article-header article))
8374              (save-excursion
8375                (gnus-cache-possibly-enter-article
8376                 gnus-newsgroup-name article
8377                 (gnus-summary-article-header article)
8378                 (= mark gnus-ticked-mark)
8379                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8380
8381         (when (gnus-summary-goto-subject article nil t)
8382           (let ((buffer-read-only nil))
8383             (gnus-summary-show-thread)
8384             ;; Fix the mark.
8385             (gnus-summary-update-mark mark 'unread)
8386             t))))))
8387
8388 (defun gnus-summary-update-secondary-mark (article)
8389   "Update the secondary (read, process, cache) mark."
8390   (gnus-summary-update-mark
8391    (cond ((memq article gnus-newsgroup-processable)
8392           gnus-process-mark)
8393          ((memq article gnus-newsgroup-cached)
8394           gnus-cached-mark)
8395          ((memq article gnus-newsgroup-replied)
8396           gnus-replied-mark)
8397          ((memq article gnus-newsgroup-saved)
8398           gnus-saved-mark)
8399          (t gnus-unread-mark))
8400    'replied)
8401   (when (gnus-visual-p 'summary-highlight 'highlight)
8402     (gnus-run-hooks 'gnus-summary-update-hook))
8403   t)
8404
8405 (defun gnus-summary-update-mark (mark type)
8406   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8407         (buffer-read-only nil))
8408     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8409     (when forward
8410       (when (looking-at "\r")
8411         (incf forward))
8412       (when (<= (+ forward (point)) (point-max))
8413         ;; Go to the right position on the line.
8414         (goto-char (+ forward (point)))
8415         ;; Replace the old mark with the new mark.
8416         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8417         ;; Optionally update the marks by some user rule.
8418         (when (eq type 'unread)
8419           (gnus-data-set-mark
8420            (gnus-data-find (gnus-summary-article-number)) mark)
8421           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8422
8423 (defun gnus-mark-article-as-read (article &optional mark)
8424   "Enter ARTICLE in the pertinent lists and remove it from others."
8425   ;; Make the article expirable.
8426   (let ((mark (or mark gnus-del-mark)))
8427     (if (= mark gnus-expirable-mark)
8428         (push article gnus-newsgroup-expirable)
8429       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8430     ;; Remove from unread and marked lists.
8431     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8432     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8433     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8434     (push (cons article mark) gnus-newsgroup-reads)
8435     ;; Possibly remove from cache, if that is used.
8436     (when gnus-use-cache
8437       (gnus-cache-enter-remove-article article))
8438     t))
8439
8440 (defun gnus-mark-article-as-unread (article &optional mark)
8441   "Enter ARTICLE in the pertinent lists and remove it from others."
8442   (let ((mark (or mark gnus-ticked-mark)))
8443     (if (<= article 0)
8444         (progn
8445           (gnus-error 1 "Can't mark negative article numbers")
8446           nil)
8447       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8448             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8449             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8450             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8451
8452       ;; Unsuppress duplicates?
8453       (when gnus-suppress-duplicates
8454         (gnus-dup-unsuppress-article article))
8455
8456       (cond ((= mark gnus-ticked-mark)
8457              (push article gnus-newsgroup-marked))
8458             ((= mark gnus-dormant-mark)
8459              (push article gnus-newsgroup-dormant))
8460             (t
8461              (push article gnus-newsgroup-unreads)))
8462       (gnus-pull article gnus-newsgroup-reads)
8463       t)))
8464
8465 (defalias 'gnus-summary-mark-as-unread-forward
8466   'gnus-summary-tick-article-forward)
8467 (make-obsolete 'gnus-summary-mark-as-unread-forward
8468                'gnus-summary-tick-article-forward)
8469 (defun gnus-summary-tick-article-forward (n)
8470   "Tick N articles forwards.
8471 If N is negative, tick backwards instead.
8472 The difference between N and the number of articles ticked is returned."
8473   (interactive "p")
8474   (gnus-summary-mark-forward n gnus-ticked-mark))
8475
8476 (defalias 'gnus-summary-mark-as-unread-backward
8477   'gnus-summary-tick-article-backward)
8478 (make-obsolete 'gnus-summary-mark-as-unread-backward
8479                'gnus-summary-tick-article-backward)
8480 (defun gnus-summary-tick-article-backward (n)
8481   "Tick N articles backwards.
8482 The difference between N and the number of articles ticked is returned."
8483   (interactive "p")
8484   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8485
8486 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8487 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8488 (defun gnus-summary-tick-article (&optional article clear-mark)
8489   "Mark current article as unread.
8490 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8491 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8492   (interactive)
8493   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8494                                        gnus-ticked-mark)))
8495
8496 (defun gnus-summary-mark-as-read-forward (n)
8497   "Mark N articles as read forwards.
8498 If N is negative, mark backwards instead.
8499 The difference between N and the actual number of articles marked is
8500 returned."
8501   (interactive "p")
8502   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8503
8504 (defun gnus-summary-mark-as-read-backward (n)
8505   "Mark the N articles as read backwards.
8506 The difference between N and the actual number of articles marked is
8507 returned."
8508   (interactive "p")
8509   (gnus-summary-mark-forward
8510    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8511
8512 (defun gnus-summary-mark-as-read (&optional article mark)
8513   "Mark current article as read.
8514 ARTICLE specifies the article to be marked as read.
8515 MARK specifies a string to be inserted at the beginning of the line."
8516   (gnus-summary-mark-article article mark))
8517
8518 (defun gnus-summary-clear-mark-forward (n)
8519   "Clear marks from N articles forward.
8520 If N is negative, clear backward instead.
8521 The difference between N and the number of marks cleared is returned."
8522   (interactive "p")
8523   (gnus-summary-mark-forward n gnus-unread-mark))
8524
8525 (defun gnus-summary-clear-mark-backward (n)
8526   "Clear marks from N articles backward.
8527 The difference between N and the number of marks cleared is returned."
8528   (interactive "p")
8529   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8530
8531 (defun gnus-summary-mark-unread-as-read ()
8532   "Intended to be used by `gnus-summary-mark-article-hook'."
8533   (when (memq gnus-current-article gnus-newsgroup-unreads)
8534     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8535
8536 (defun gnus-summary-mark-read-and-unread-as-read ()
8537   "Intended to be used by `gnus-summary-mark-article-hook'."
8538   (let ((mark (gnus-summary-article-mark)))
8539     (when (or (gnus-unread-mark-p mark)
8540               (gnus-read-mark-p mark))
8541       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8542
8543 (defun gnus-summary-mark-region-as-read (point mark all)
8544   "Mark all unread articles between point and mark as read.
8545 If given a prefix, mark all articles between point and mark as read,
8546 even ticked and dormant ones."
8547   (interactive "r\nP")
8548   (save-excursion
8549     (let (article)
8550       (goto-char point)
8551       (beginning-of-line)
8552       (while (and
8553               (< (point) mark)
8554               (progn
8555                 (when (or all
8556                           (memq (setq article (gnus-summary-article-number))
8557                                 gnus-newsgroup-unreads))
8558                   (gnus-summary-mark-article article gnus-del-mark))
8559                 t)
8560               (gnus-summary-find-next))))))
8561
8562 (defun gnus-summary-mark-below (score mark)
8563   "Mark articles with score less than SCORE with MARK."
8564   (interactive "P\ncMark: ")
8565   (setq score (if score
8566                   (prefix-numeric-value score)
8567                 (or gnus-summary-default-score 0)))
8568   (save-excursion
8569     (set-buffer gnus-summary-buffer)
8570     (goto-char (point-min))
8571     (while
8572         (progn
8573           (and (< (gnus-summary-article-score) score)
8574                (gnus-summary-mark-article nil mark))
8575           (gnus-summary-find-next)))))
8576
8577 (defun gnus-summary-kill-below (&optional score)
8578   "Mark articles with score below SCORE as read."
8579   (interactive "P")
8580   (gnus-summary-mark-below score gnus-killed-mark))
8581
8582 (defun gnus-summary-clear-above (&optional score)
8583   "Clear all marks from articles with score above SCORE."
8584   (interactive "P")
8585   (gnus-summary-mark-above score gnus-unread-mark))
8586
8587 (defun gnus-summary-tick-above (&optional score)
8588   "Tick all articles with score above SCORE."
8589   (interactive "P")
8590   (gnus-summary-mark-above score gnus-ticked-mark))
8591
8592 (defun gnus-summary-mark-above (score mark)
8593   "Mark articles with score over SCORE with MARK."
8594   (interactive "P\ncMark: ")
8595   (setq score (if score
8596                   (prefix-numeric-value score)
8597                 (or gnus-summary-default-score 0)))
8598   (save-excursion
8599     (set-buffer gnus-summary-buffer)
8600     (goto-char (point-min))
8601     (while (and (progn
8602                   (when (> (gnus-summary-article-score) score)
8603                     (gnus-summary-mark-article nil mark))
8604                   t)
8605                 (gnus-summary-find-next)))))
8606
8607 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8608 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8609 (defun gnus-summary-limit-include-expunged (&optional no-error)
8610   "Display all the hidden articles that were expunged for low scores."
8611   (interactive)
8612   (let ((buffer-read-only nil))
8613     (let ((scored gnus-newsgroup-scored)
8614           headers h)
8615       (while scored
8616         (unless (gnus-summary-goto-subject (caar scored))
8617           (and (setq h (gnus-summary-article-header (caar scored)))
8618                (< (cdar scored) gnus-summary-expunge-below)
8619                (push h headers)))
8620         (setq scored (cdr scored)))
8621       (if (not headers)
8622           (when (not no-error)
8623             (error "No expunged articles hidden"))
8624         (goto-char (point-min))
8625         (gnus-summary-prepare-unthreaded (nreverse headers))
8626         (goto-char (point-min))
8627         (gnus-summary-position-point)
8628         t))))
8629
8630 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8631   "Mark all unread articles in this newsgroup as read.
8632 If prefix argument ALL is non-nil, ticked and dormant articles will
8633 also be marked as read.
8634 If QUIETLY is non-nil, no questions will be asked.
8635 If TO-HERE is non-nil, it should be a point in the buffer.  All
8636 articles before this point will be marked as read.
8637 Note that this function will only catch up the unread article
8638 in the current summary buffer limitation.
8639 The number of articles marked as read is returned."
8640   (interactive "P")
8641   (prog1
8642       (save-excursion
8643         (when (or quietly
8644                   (not gnus-interactive-catchup) ;Without confirmation?
8645                   gnus-expert-user
8646                   (gnus-y-or-n-p
8647                    (if all
8648                        "Mark absolutely all articles as read? "
8649                      "Mark all unread articles as read? ")))
8650           (if (and not-mark
8651                    (not gnus-newsgroup-adaptive)
8652                    (not gnus-newsgroup-auto-expire)
8653                    (not gnus-suppress-duplicates)
8654                    (or (not gnus-use-cache)
8655                        (eq gnus-use-cache 'passive)))
8656               (progn
8657                 (when all
8658                   (setq gnus-newsgroup-marked nil
8659                         gnus-newsgroup-dormant nil))
8660                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8661             ;; We actually mark all articles as canceled, which we
8662             ;; have to do when using auto-expiry or adaptive scoring.
8663             (gnus-summary-show-all-threads)
8664             (when (gnus-summary-first-subject (not all) t)
8665               (while (and
8666                       (if to-here (< (point) to-here) t)
8667                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8668                       (gnus-summary-find-next (not all) nil nil t))))
8669             (gnus-set-mode-line 'summary))
8670           t))
8671     (gnus-summary-position-point)))
8672
8673 (defun gnus-summary-catchup-to-here (&optional all)
8674   "Mark all unticked articles before the current one as read.
8675 If ALL is non-nil, also mark ticked and dormant articles as read."
8676   (interactive "P")
8677   (save-excursion
8678     (gnus-save-hidden-threads
8679       (let ((beg (point)))
8680         ;; We check that there are unread articles.
8681         (when (or all (gnus-summary-find-prev))
8682           (gnus-summary-catchup all t beg)))))
8683   (gnus-summary-position-point))
8684
8685 (defun gnus-summary-catchup-all (&optional quietly)
8686   "Mark all articles in this newsgroup as read."
8687   (interactive "P")
8688   (gnus-summary-catchup t quietly))
8689
8690 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8691   "Mark all unread articles in this group as read, then exit.
8692 If prefix argument ALL is non-nil, all articles are marked as read."
8693   (interactive "P")
8694   (when (gnus-summary-catchup all quietly nil 'fast)
8695     ;; Select next newsgroup or exit.
8696     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8697              (eq gnus-auto-select-next 'quietly))
8698         (gnus-summary-next-group nil)
8699       (gnus-summary-exit))))
8700
8701 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8702   "Mark all articles in this newsgroup as read, and then exit."
8703   (interactive "P")
8704   (gnus-summary-catchup-and-exit t quietly))
8705
8706 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8707   "Mark all articles in this group as read and select the next group.
8708 If given a prefix, mark all articles, unread as well as ticked, as
8709 read."
8710   (interactive "P")
8711   (save-excursion
8712     (gnus-summary-catchup all))
8713   (gnus-summary-next-group))
8714
8715 ;;;
8716 ;;; with article
8717 ;;;
8718
8719 (defmacro gnus-with-article (article &rest forms)
8720   "Select ARTICLE and perform FORMS in the original article buffer.
8721 Then replace the article with the result."
8722   `(progn
8723      ;; We don't want the article to be marked as read.
8724      (let (gnus-mark-article-hook)
8725        (gnus-summary-select-article t t nil ,article))
8726      (set-buffer gnus-original-article-buffer)
8727      ,@forms
8728      (if (not (gnus-check-backend-function
8729                'request-replace-article (car gnus-article-current)))
8730          (gnus-message 5 "Read-only group; not replacing")
8731        (unless (gnus-request-replace-article
8732                 ,article (car gnus-article-current)
8733                 (current-buffer) t)
8734          (error "Couldn't replace article")))
8735      ;; The cache and backlog have to be flushed somewhat.
8736      (when gnus-keep-backlog
8737        (gnus-backlog-remove-article
8738         (car gnus-article-current) (cdr gnus-article-current)))
8739      (when gnus-use-cache
8740        (gnus-cache-update-article
8741         (car gnus-article-current) (cdr gnus-article-current)))))
8742
8743 (put 'gnus-with-article 'lisp-indent-function 1)
8744 (put 'gnus-with-article 'edebug-form-spec '(form body))
8745
8746 ;; Thread-based commands.
8747
8748 (defun gnus-summary-articles-in-thread (&optional article)
8749   "Return a list of all articles in the current thread.
8750 If ARTICLE is non-nil, return all articles in the thread that starts
8751 with that article."
8752   (let* ((article (or article (gnus-summary-article-number)))
8753          (data (gnus-data-find-list article))
8754          (top-level (gnus-data-level (car data)))
8755          (top-subject
8756           (cond ((null gnus-thread-operation-ignore-subject)
8757                  (gnus-simplify-subject-re
8758                   (mail-header-subject (gnus-data-header (car data)))))
8759                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8760                  (gnus-simplify-subject-fuzzy
8761                   (mail-header-subject (gnus-data-header (car data)))))
8762                 (t nil)))
8763          (end-point (save-excursion
8764                       (if (gnus-summary-go-to-next-thread)
8765                           (point) (point-max))))
8766          articles)
8767     (while (and data
8768                 (< (gnus-data-pos (car data)) end-point))
8769       (when (or (not top-subject)
8770                 (string= top-subject
8771                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8772                              (gnus-simplify-subject-fuzzy
8773                               (mail-header-subject
8774                                (gnus-data-header (car data))))
8775                            (gnus-simplify-subject-re
8776                             (mail-header-subject
8777                              (gnus-data-header (car data)))))))
8778         (push (gnus-data-number (car data)) articles))
8779       (unless (and (setq data (cdr data))
8780                    (> (gnus-data-level (car data)) top-level))
8781         (setq data nil)))
8782     ;; Return the list of articles.
8783     (nreverse articles)))
8784
8785 (defun gnus-summary-rethread-current ()
8786   "Rethread the thread the current article is part of."
8787   (interactive)
8788   (let* ((gnus-show-threads t)
8789          (article (gnus-summary-article-number))
8790          (id (mail-header-id (gnus-summary-article-header)))
8791          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8792     (unless id
8793       (error "No article on the current line"))
8794     (gnus-rebuild-thread id)
8795     (gnus-summary-goto-subject article)))
8796
8797 (defun gnus-summary-reparent-thread ()
8798   "Make the current article child of the marked (or previous) article.
8799
8800 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8801 is non-nil or the Subject: of both articles are the same."
8802   (interactive)
8803   (unless (not (gnus-group-read-only-p))
8804     (error "The current newsgroup does not support article editing"))
8805   (unless (<= (length gnus-newsgroup-processable) 1)
8806     (error "No more than one article may be marked"))
8807   (save-window-excursion
8808     (let ((gnus-article-buffer " *reparent*")
8809           (current-article (gnus-summary-article-number))
8810           ;; First grab the marked article, otherwise one line up.
8811           (parent-article (if (not (null gnus-newsgroup-processable))
8812                               (car gnus-newsgroup-processable)
8813                             (save-excursion
8814                               (if (eq (forward-line -1) 0)
8815                                   (gnus-summary-article-number)
8816                                 (error "Beginning of summary buffer"))))))
8817       (unless (not (eq current-article parent-article))
8818         (error "An article may not be self-referential"))
8819       (let ((message-id (mail-header-id
8820                          (gnus-summary-article-header parent-article))))
8821         (unless (and message-id (not (equal message-id "")))
8822           (error "No message-id in desired parent"))
8823         (gnus-with-article current-article
8824           (goto-char (point-min))
8825           (if (re-search-forward "^References: " nil t)
8826               (progn
8827                 (re-search-forward "^[^ \t]" nil t)
8828                 (forward-line -1)
8829                 (end-of-line)
8830                 (insert " " message-id))
8831             (insert "References: " message-id "\n")))
8832         (set-buffer gnus-summary-buffer)
8833         (gnus-summary-unmark-all-processable)
8834         (gnus-summary-update-article current-article)
8835         (gnus-summary-rethread-current)
8836         (gnus-message 3 "Article %d is now the child of article %d"
8837                       current-article parent-article)))))
8838
8839 (defun gnus-summary-toggle-threads (&optional arg)
8840   "Toggle showing conversation threads.
8841 If ARG is positive number, turn showing conversation threads on."
8842   (interactive "P")
8843   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8844     (setq gnus-show-threads
8845           (if (null arg) (not gnus-show-threads)
8846             (> (prefix-numeric-value arg) 0)))
8847     (gnus-summary-prepare)
8848     (gnus-summary-goto-subject current)
8849     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8850     (gnus-summary-position-point)))
8851
8852 (defun gnus-summary-show-all-threads ()
8853   "Show all threads."
8854   (interactive)
8855   (save-excursion
8856     (let ((buffer-read-only nil))
8857       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8858   (gnus-summary-position-point))
8859
8860 (defun gnus-summary-show-thread ()
8861   "Show thread subtrees.
8862 Returns nil if no thread was there to be shown."
8863   (interactive)
8864   (let ((buffer-read-only nil)
8865         (orig (point))
8866         ;; first goto end then to beg, to have point at beg after let
8867         (end (progn (end-of-line) (point)))
8868         (beg (progn (beginning-of-line) (point))))
8869     (prog1
8870         ;; Any hidden lines here?
8871         (search-forward "\r" end t)
8872       (subst-char-in-region beg end ?\^M ?\n t)
8873       (goto-char orig)
8874       (gnus-summary-position-point))))
8875
8876 (defun gnus-summary-hide-all-threads ()
8877   "Hide all thread subtrees."
8878   (interactive)
8879   (save-excursion
8880     (goto-char (point-min))
8881     (gnus-summary-hide-thread)
8882     (while (zerop (gnus-summary-next-thread 1 t))
8883       (gnus-summary-hide-thread)))
8884   (gnus-summary-position-point))
8885
8886 (defun gnus-summary-hide-thread ()
8887   "Hide thread subtrees.
8888 Returns nil if no threads were there to be hidden."
8889   (interactive)
8890   (let ((buffer-read-only nil)
8891         (start (point))
8892         (article (gnus-summary-article-number)))
8893     (goto-char start)
8894     ;; Go forward until either the buffer ends or the subthread
8895     ;; ends.
8896     (when (and (not (eobp))
8897                (or (zerop (gnus-summary-next-thread 1 t))
8898                    (goto-char (point-max))))
8899       (prog1
8900           (if (and (> (point) start)
8901                    (search-backward "\n" start t))
8902               (progn
8903                 (subst-char-in-region start (point) ?\n ?\^M)
8904                 (gnus-summary-goto-subject article))
8905             (goto-char start)
8906             nil)))))
8907
8908 (defun gnus-summary-go-to-next-thread (&optional previous)
8909   "Go to the same level (or less) next thread.
8910 If PREVIOUS is non-nil, go to previous thread instead.
8911 Return the article number moved to, or nil if moving was impossible."
8912   (let ((level (gnus-summary-thread-level))
8913         (way (if previous -1 1))
8914         (beg (point)))
8915     (forward-line way)
8916     (while (and (not (eobp))
8917                 (< level (gnus-summary-thread-level)))
8918       (forward-line way))
8919     (if (eobp)
8920         (progn
8921           (goto-char beg)
8922           nil)
8923       (setq beg (point))
8924       (prog1
8925           (gnus-summary-article-number)
8926         (goto-char beg)))))
8927
8928 (defun gnus-summary-next-thread (n &optional silent)
8929   "Go to the same level next N'th thread.
8930 If N is negative, search backward instead.
8931 Returns the difference between N and the number of skips actually
8932 done.
8933
8934 If SILENT, don't output messages."
8935   (interactive "p")
8936   (let ((backward (< n 0))
8937         (n (abs n)))
8938     (while (and (> n 0)
8939                 (gnus-summary-go-to-next-thread backward))
8940       (decf n))
8941     (unless silent
8942       (gnus-summary-position-point))
8943     (when (and (not silent) (/= 0 n))
8944       (gnus-message 7 "No more threads"))
8945     n))
8946
8947 (defun gnus-summary-prev-thread (n)
8948   "Go to the same level previous N'th thread.
8949 Returns the difference between N and the number of skips actually
8950 done."
8951   (interactive "p")
8952   (gnus-summary-next-thread (- n)))
8953
8954 (defun gnus-summary-go-down-thread ()
8955   "Go down one level in the current thread."
8956   (let ((children (gnus-summary-article-children)))
8957     (when children
8958       (gnus-summary-goto-subject (car children)))))
8959
8960 (defun gnus-summary-go-up-thread ()
8961   "Go up one level in the current thread."
8962   (let ((parent (gnus-summary-article-parent)))
8963     (when parent
8964       (gnus-summary-goto-subject parent))))
8965
8966 (defun gnus-summary-down-thread (n)
8967   "Go down thread N steps.
8968 If N is negative, go up instead.
8969 Returns the difference between N and how many steps down that were
8970 taken."
8971   (interactive "p")
8972   (let ((up (< n 0))
8973         (n (abs n)))
8974     (while (and (> n 0)
8975                 (if up (gnus-summary-go-up-thread)
8976                   (gnus-summary-go-down-thread)))
8977       (setq n (1- n)))
8978     (gnus-summary-position-point)
8979     (when (/= 0 n)
8980       (gnus-message 7 "Can't go further"))
8981     n))
8982
8983 (defun gnus-summary-up-thread (n)
8984   "Go up thread N steps.
8985 If N is negative, go up instead.
8986 Returns the difference between N and how many steps down that were
8987 taken."
8988   (interactive "p")
8989   (gnus-summary-down-thread (- n)))
8990
8991 (defun gnus-summary-top-thread ()
8992   "Go to the top of the thread."
8993   (interactive)
8994   (while (gnus-summary-go-up-thread))
8995   (gnus-summary-article-number))
8996
8997 (defun gnus-summary-kill-thread (&optional unmark)
8998   "Mark articles under current thread as read.
8999 If the prefix argument is positive, remove any kinds of marks.
9000 If the prefix argument is negative, tick articles instead."
9001   (interactive "P")
9002   (when unmark
9003     (setq unmark (prefix-numeric-value unmark)))
9004   (let ((articles (gnus-summary-articles-in-thread)))
9005     (save-excursion
9006       ;; Expand the thread.
9007       (gnus-summary-show-thread)
9008       ;; Mark all the articles.
9009       (while articles
9010         (gnus-summary-goto-subject (car articles))
9011         (cond ((null unmark)
9012                (gnus-summary-mark-article-as-read gnus-killed-mark))
9013               ((> unmark 0)
9014                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9015               (t
9016                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9017         (setq articles (cdr articles))))
9018     ;; Hide killed subtrees.
9019     (and (null unmark)
9020          gnus-thread-hide-killed
9021          (gnus-summary-hide-thread))
9022     ;; If marked as read, go to next unread subject.
9023     (when (null unmark)
9024       ;; Go to next unread subject.
9025       (gnus-summary-next-subject 1 t)))
9026   (gnus-set-mode-line 'summary))
9027
9028 ;; Summary sorting commands
9029
9030 (defun gnus-summary-sort-by-number (&optional reverse)
9031   "Sort the summary buffer by article number.
9032 Argument REVERSE means reverse order."
9033   (interactive "P")
9034   (gnus-summary-sort 'number reverse))
9035
9036 (defun gnus-summary-sort-by-author (&optional reverse)
9037   "Sort the summary buffer by author name alphabetically.
9038 If case-fold-search is non-nil, case of letters is ignored.
9039 Argument REVERSE means reverse order."
9040   (interactive "P")
9041   (gnus-summary-sort 'author reverse))
9042
9043 (defun gnus-summary-sort-by-subject (&optional reverse)
9044   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9045 If case-fold-search is non-nil, case of letters is ignored.
9046 Argument REVERSE means reverse order."
9047   (interactive "P")
9048   (gnus-summary-sort 'subject reverse))
9049
9050 (defun gnus-summary-sort-by-date (&optional reverse)
9051   "Sort the summary buffer by date.
9052 Argument REVERSE means reverse order."
9053   (interactive "P")
9054   (gnus-summary-sort 'date reverse))
9055
9056 (defun gnus-summary-sort-by-score (&optional reverse)
9057   "Sort the summary buffer by score.
9058 Argument REVERSE means reverse order."
9059   (interactive "P")
9060   (gnus-summary-sort 'score reverse))
9061
9062 (defun gnus-summary-sort-by-lines (&optional reverse)
9063   "Sort the summary buffer by the number of lines.
9064 Argument REVERSE means reverse order."
9065   (interactive "P")
9066   (gnus-summary-sort 'lines reverse))
9067
9068 (defun gnus-summary-sort-by-chars (&optional reverse)
9069   "Sort the summary buffer by article length.
9070 Argument REVERSE means reverse order."
9071   (interactive "P")
9072   (gnus-summary-sort 'chars reverse))   
9073
9074 (defun gnus-summary-sort (predicate reverse)
9075   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9076   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9077          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9078          (gnus-thread-sort-functions
9079           (if (not reverse)
9080               thread
9081             `(lambda (t1 t2)
9082                (,thread t2 t1))))
9083          (gnus-article-sort-functions
9084           (if (not reverse)
9085               article
9086             `(lambda (t1 t2)
9087                (,article t2 t1))))
9088          (buffer-read-only)
9089          (gnus-summary-prepare-hook nil))
9090     ;; We do the sorting by regenerating the threads.
9091     (gnus-summary-prepare)
9092     ;; Hide subthreads if needed.
9093     (when (and gnus-show-threads gnus-thread-hide-subtree)
9094       (gnus-summary-hide-all-threads))))
9095
9096 ;; Summary saving commands.
9097
9098 (defun gnus-summary-save-article (&optional n not-saved)
9099   "Save the current article using the default saver function.
9100 If N is a positive number, save the N next articles.
9101 If N is a negative number, save the N previous articles.
9102 If N is nil and any articles have been marked with the process mark,
9103 save those articles instead.
9104 The variable `gnus-default-article-saver' specifies the saver function."
9105   (interactive "P")
9106   (let* ((articles (gnus-summary-work-articles n))
9107          (save-buffer (save-excursion
9108                         (nnheader-set-temp-buffer " *Gnus Save*")))
9109          (num (length articles))
9110          header file)
9111     (dolist (article articles)
9112       (setq header (gnus-summary-article-header article))
9113       (if (not (vectorp header))
9114           ;; This is a pseudo-article.
9115           (if (assq 'name header)
9116               (gnus-copy-file (cdr (assq 'name header)))
9117             (gnus-message 1 "Article %d is unsaveable" article))
9118         ;; This is a real article.
9119         (save-window-excursion
9120           (gnus-summary-select-article t nil nil article))
9121         (save-excursion
9122           (set-buffer save-buffer)
9123           (erase-buffer)
9124           (insert-buffer-substring gnus-original-article-buffer))
9125         (setq file (gnus-article-save save-buffer file num))
9126         (gnus-summary-remove-process-mark article)
9127         (unless not-saved
9128           (gnus-summary-set-saved-mark article))))
9129     (gnus-kill-buffer save-buffer)
9130     (gnus-summary-position-point)
9131     (gnus-set-mode-line 'summary)
9132     n))
9133
9134 (defun gnus-summary-pipe-output (&optional arg)
9135   "Pipe the current article to a subprocess.
9136 If N is a positive number, pipe the N next articles.
9137 If N is a negative number, pipe the N previous articles.
9138 If N is nil and any articles have been marked with the process mark,
9139 pipe those articles instead."
9140   (interactive "P")
9141   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9142     (gnus-summary-save-article arg t))
9143   (gnus-configure-windows 'pipe))
9144
9145 (defun gnus-summary-save-article-mail (&optional arg)
9146   "Append the current article to an mail file.
9147 If N is a positive number, save the N next articles.
9148 If N is a negative number, save the N previous articles.
9149 If N is nil and any articles have been marked with the process mark,
9150 save those articles instead."
9151   (interactive "P")
9152   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9153     (gnus-summary-save-article arg)))
9154
9155 (defun gnus-summary-save-article-rmail (&optional arg)
9156   "Append the current article to an rmail file.
9157 If N is a positive number, save the N next articles.
9158 If N is a negative number, save the N previous articles.
9159 If N is nil and any articles have been marked with the process mark,
9160 save those articles instead."
9161   (interactive "P")
9162   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9163     (gnus-summary-save-article arg)))
9164
9165 (defun gnus-summary-save-article-file (&optional arg)
9166   "Append the current article to a file.
9167 If N is a positive number, save the N next articles.
9168 If N is a negative number, save the N previous articles.
9169 If N is nil and any articles have been marked with the process mark,
9170 save those articles instead."
9171   (interactive "P")
9172   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9173     (gnus-summary-save-article arg)))
9174
9175 (defun gnus-summary-write-article-file (&optional arg)
9176   "Write the current article to a file, deleting the previous file.
9177 If N is a positive number, save the N next articles.
9178 If N is a negative number, save the N previous articles.
9179 If N is nil and any articles have been marked with the process mark,
9180 save those articles instead."
9181   (interactive "P")
9182   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9183     (gnus-summary-save-article arg)))
9184
9185 (defun gnus-summary-save-article-body-file (&optional arg)
9186   "Append the current article body to a file.
9187 If N is a positive number, save the N next articles.
9188 If N is a negative number, save the N previous articles.
9189 If N is nil and any articles have been marked with the process mark,
9190 save those articles instead."
9191   (interactive "P")
9192   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9193     (gnus-summary-save-article arg)))
9194
9195 (defun gnus-summary-pipe-message (program)
9196   "Pipe the current article through PROGRAM."
9197   (interactive "sProgram: ")
9198   (gnus-summary-select-article)
9199   (let ((mail-header-separator ""))
9200     (gnus-eval-in-buffer-window gnus-article-buffer
9201       (save-restriction
9202         (widen)
9203         (let ((start (window-start))
9204               buffer-read-only)
9205           (message-pipe-buffer-body program)
9206           (set-window-start (get-buffer-window (current-buffer)) start))))))
9207
9208 (defun gnus-get-split-value (methods)
9209   "Return a value based on the split METHODS."
9210   (let (split-name method result match)
9211     (when methods
9212       (save-excursion
9213         (set-buffer gnus-original-article-buffer)
9214         (save-restriction
9215           (nnheader-narrow-to-headers)
9216           (while methods
9217             (goto-char (point-min))
9218             (setq method (pop methods))
9219             (setq match (car method))
9220             (when (cond
9221                    ((stringp match)
9222                     ;; Regular expression.
9223                     (ignore-errors
9224                       (re-search-forward match nil t)))
9225                    ((gnus-functionp match)
9226                     ;; Function.
9227                     (save-restriction
9228                       (widen)
9229                       (setq result (funcall match gnus-newsgroup-name))))
9230                    ((consp match)
9231                     ;; Form.
9232                     (save-restriction
9233                       (widen)
9234                       (setq result (eval match)))))
9235               (setq split-name (append (cdr method) split-name))
9236               (cond ((stringp result)
9237                      (push (expand-file-name
9238                             result gnus-article-save-directory)
9239                            split-name))
9240                     ((consp result)
9241                      (setq split-name (append result split-name)))))))))
9242     (nreverse split-name)))
9243
9244 (defun gnus-valid-move-group-p (group)
9245   (and (boundp group)
9246        (symbol-name group)
9247        (symbol-value group)
9248        (gnus-get-function (gnus-find-method-for-group
9249                            (symbol-name group)) 'request-accept-article t)))
9250
9251 (defun gnus-read-move-group-name (prompt default articles prefix)
9252   "Read a group name."
9253   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9254          (minibuffer-confirm-incomplete nil) ; XEmacs
9255          (prom
9256           (format "%s %s to:"
9257                   prompt
9258                   (if (> (length articles) 1)
9259                       (format "these %d articles" (length articles))
9260                     "this article")))
9261          (to-newsgroup
9262           (cond
9263            ((null split-name)
9264             (gnus-completing-read default prom
9265                                   gnus-active-hashtb
9266                                   'gnus-valid-move-group-p
9267                                   nil prefix
9268                                   'gnus-group-history))
9269            ((= 1 (length split-name))
9270             (gnus-completing-read (car split-name) prom
9271                                   gnus-active-hashtb
9272                                   'gnus-valid-move-group-p
9273                                   nil nil
9274                                   'gnus-group-history))
9275            (t
9276             (gnus-completing-read nil prom
9277                                   (mapcar (lambda (el) (list el))
9278                                           (nreverse split-name))
9279                                   nil nil nil
9280                                   'gnus-group-history))))
9281          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9282     (when to-newsgroup
9283       (if (or (string= to-newsgroup "")
9284               (string= to-newsgroup prefix))
9285           (setq to-newsgroup default))
9286       (unless to-newsgroup
9287         (error "No group name entered"))
9288       (or (gnus-active to-newsgroup)
9289           (gnus-activate-group to-newsgroup nil nil to-method)
9290           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9291                                      to-newsgroup))
9292               (or (and (gnus-request-create-group to-newsgroup to-method)
9293                        (gnus-activate-group
9294                         to-newsgroup nil nil to-method)
9295                        (gnus-subscribe-group to-newsgroup))
9296                   (error "Couldn't create group %s" to-newsgroup)))
9297           (error "No such group: %s" to-newsgroup)))
9298     to-newsgroup))
9299
9300 (defun gnus-summary-save-parts (type dir n &optional reverse)
9301   "Save parts matching TYPE to DIR.
9302 If REVERSE, save parts that do not match TYPE."
9303   (interactive
9304    (list (read-string "Save parts of type: " "image/.*")
9305          (read-file-name "Save to directory: " nil nil t)
9306          current-prefix-arg))
9307   (gnus-summary-iterate n
9308     (let ((gnus-display-mime-function nil)
9309           (gnus-inhibit-treatment t))
9310       (gnus-summary-select-article))
9311     (save-excursion
9312       (set-buffer gnus-article-buffer)
9313       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9314         (when handles
9315           (gnus-summary-save-parts-1 type dir handles reverse)
9316           (mm-destroy-parts handles))))))
9317
9318 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9319   (if (stringp (car handle))
9320       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9321               (cdr handle))
9322     (when (if reverse
9323               (not (string-match type (mm-handle-media-type handle)))
9324             (string-match type (mm-handle-media-type handle)))
9325       (let ((file (expand-file-name
9326                    (file-name-nondirectory
9327                     (or
9328                      (mail-content-type-get
9329                       (mm-handle-disposition handle) 'filename)
9330                      (concat gnus-newsgroup-name
9331                              "." (number-to-string
9332                                   (cdr gnus-article-current)))))
9333                    dir)))
9334         (unless (file-exists-p file)
9335           (mm-save-part-to-file handle file))))))
9336
9337 ;; Summary extract commands
9338
9339 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9340   (let ((buffer-read-only nil)
9341         (article (gnus-summary-article-number))
9342         after-article b e)
9343     (unless (gnus-summary-goto-subject article)
9344       (error "No such article: %d" article))
9345     (gnus-summary-position-point)
9346     ;; If all commands are to be bunched up on one line, we collect
9347     ;; them here.
9348     (unless gnus-view-pseudos-separately
9349       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9350             files action)
9351         (while ps
9352           (setq action (cdr (assq 'action (car ps))))
9353           (setq files (list (cdr (assq 'name (car ps)))))
9354           (while (and ps (cdr ps)
9355                       (string= (or action "1")
9356                                (or (cdr (assq 'action (cadr ps))) "2")))
9357             (push (cdr (assq 'name (cadr ps))) files)
9358             (setcdr ps (cddr ps)))
9359           (when files
9360             (when (not (string-match "%s" action))
9361               (push " " files))
9362             (push " " files)
9363             (when (assq 'execute (car ps))
9364               (setcdr (assq 'execute (car ps))
9365                       (funcall (if (string-match "%s" action)
9366                                    'format 'concat)
9367                                action
9368                                (mapconcat
9369                                 (lambda (f)
9370                                   (if (equal f " ")
9371                                       f
9372                                     (gnus-quote-arg-for-sh-or-csh f)))
9373                                 files " ")))))
9374           (setq ps (cdr ps)))))
9375     (if (and gnus-view-pseudos (not not-view))
9376         (while pslist
9377           (when (assq 'execute (car pslist))
9378             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9379                                   (eq gnus-view-pseudos 'not-confirm)))
9380           (setq pslist (cdr pslist)))
9381       (save-excursion
9382         (while pslist
9383           (setq after-article (or (cdr (assq 'article (car pslist)))
9384                                   (gnus-summary-article-number)))
9385           (gnus-summary-goto-subject after-article)
9386           (forward-line 1)
9387           (setq b (point))
9388           (insert "    " (file-name-nondirectory
9389                           (cdr (assq 'name (car pslist))))
9390                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9391           (setq e (point))
9392           (forward-line -1)             ; back to `b'
9393           (gnus-add-text-properties
9394            b (1- e) (list 'gnus-number gnus-reffed-article-number
9395                           gnus-mouse-face-prop gnus-mouse-face))
9396           (gnus-data-enter
9397            after-article gnus-reffed-article-number
9398            gnus-unread-mark b (car pslist) 0 (- e b))
9399           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9400           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9401           (setq pslist (cdr pslist)))))))
9402
9403 (defun gnus-pseudos< (p1 p2)
9404   (let ((c1 (cdr (assq 'action p1)))
9405         (c2 (cdr (assq 'action p2))))
9406     (and c1 c2 (string< c1 c2))))
9407
9408 (defun gnus-request-pseudo-article (props)
9409   (cond ((assq 'execute props)
9410          (gnus-execute-command (cdr (assq 'execute props)))))
9411   (let ((gnus-current-article (gnus-summary-article-number)))
9412     (gnus-run-hooks 'gnus-mark-article-hook)))
9413
9414 (defun gnus-execute-command (command &optional automatic)
9415   (save-excursion
9416     (gnus-article-setup-buffer)
9417     (set-buffer gnus-article-buffer)
9418     (setq buffer-read-only nil)
9419     (let ((command (if automatic command
9420                      (read-string "Command: " (cons command 0)))))
9421       (erase-buffer)
9422       (insert "$ " command "\n\n")
9423       (if gnus-view-pseudo-asynchronously
9424           (start-process "gnus-execute" (current-buffer) shell-file-name
9425                          shell-command-switch command)
9426         (call-process shell-file-name nil t nil
9427                       shell-command-switch command)))))
9428
9429 ;; Summary kill commands.
9430
9431 (defun gnus-summary-edit-global-kill (article)
9432   "Edit the \"global\" kill file."
9433   (interactive (list (gnus-summary-article-number)))
9434   (gnus-group-edit-global-kill article))
9435
9436 (defun gnus-summary-edit-local-kill ()
9437   "Edit a local kill file applied to the current newsgroup."
9438   (interactive)
9439   (setq gnus-current-headers (gnus-summary-article-header))
9440   (gnus-group-edit-local-kill
9441    (gnus-summary-article-number) gnus-newsgroup-name))
9442
9443 ;;; Header reading.
9444
9445 (defun gnus-read-header (id &optional header)
9446   "Read the headers of article ID and enter them into the Gnus system."
9447   (let ((group gnus-newsgroup-name)
9448         (gnus-override-method
9449          (or
9450           gnus-override-method
9451           (and (gnus-news-group-p gnus-newsgroup-name)
9452                (car (gnus-refer-article-methods)))))
9453         where)
9454     ;; First we check to see whether the header in question is already
9455     ;; fetched.
9456     (if (stringp id)
9457         ;; This is a Message-ID.
9458         (setq header (or header (gnus-id-to-header id)))
9459       ;; This is an article number.
9460       (setq header (or header (gnus-summary-article-header id))))
9461     (if (and header
9462              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9463         ;; We have found the header.
9464         header
9465       ;; If this is a sparse article, we have to nix out its
9466       ;; previous entry in the thread hashtb.
9467       (when (and header
9468                  (gnus-summary-article-sparse-p (mail-header-number header)))
9469         (let* ((parent (gnus-parent-id (mail-header-references header)))
9470                (thread (and parent (gnus-id-to-thread parent))))
9471           (when thread
9472             (delq (assq header thread) thread))))
9473       ;; We have to really fetch the header to this article.
9474       (save-excursion
9475         (set-buffer nntp-server-buffer)
9476         (when (setq where (gnus-request-head id group))
9477           (nnheader-fold-continuation-lines)
9478           (goto-char (point-max))
9479           (insert ".\n")
9480           (goto-char (point-min))
9481           (insert "211 ")
9482           (princ (cond
9483                   ((numberp id) id)
9484                   ((cdr where) (cdr where))
9485                   (header (mail-header-number header))
9486                   (t gnus-reffed-article-number))
9487                  (current-buffer))
9488           (insert " Article retrieved.\n"))
9489         (if (or (not where)
9490                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9491             ()                          ; Malformed head.
9492           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9493             (when (and (stringp id)
9494                        (not (string= (gnus-group-real-name group)
9495                                      (car where))))
9496               ;; If we fetched by Message-ID and the article came
9497               ;; from a different group, we fudge some bogus article
9498               ;; numbers for this article.
9499               (mail-header-set-number header gnus-reffed-article-number))
9500             (save-excursion
9501               (set-buffer gnus-summary-buffer)
9502               (decf gnus-reffed-article-number)
9503               (gnus-remove-header (mail-header-number header))
9504               (push header gnus-newsgroup-headers)
9505               (setq gnus-current-headers header)
9506               (push (mail-header-number header) gnus-newsgroup-limit)))
9507           header)))))
9508
9509 (defun gnus-remove-header (number)
9510   "Remove header NUMBER from `gnus-newsgroup-headers'."
9511   (if (and gnus-newsgroup-headers
9512            (= number (mail-header-number (car gnus-newsgroup-headers))))
9513       (pop gnus-newsgroup-headers)
9514     (let ((headers gnus-newsgroup-headers))
9515       (while (and (cdr headers)
9516                   (not (= number (mail-header-number (cadr headers)))))
9517         (pop headers))
9518       (when (cdr headers)
9519         (setcdr headers (cddr headers))))))
9520
9521 ;;;
9522 ;;; summary highlights
9523 ;;;
9524
9525 (defun gnus-highlight-selected-summary ()
9526   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9527   ;; Highlight selected article in summary buffer
9528   (when gnus-summary-selected-face
9529     (save-excursion
9530       (let* ((beg (progn (beginning-of-line) (point)))
9531              (end (progn (end-of-line) (point)))
9532              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9533              (from (if (get-text-property beg gnus-mouse-face-prop)
9534                        beg
9535                      (or (next-single-property-change
9536                           beg gnus-mouse-face-prop nil end)
9537                          beg)))
9538              (to
9539               (if (= from end)
9540                   (- from 2)
9541                 (or (next-single-property-change
9542                      from gnus-mouse-face-prop nil end)
9543                     end))))
9544         ;; If no mouse-face prop on line we will have to = from = end,
9545         ;; so we highlight the entire line instead.
9546         (when (= (+ to 2) from)
9547           (setq from beg)
9548           (setq to end))
9549         (if gnus-newsgroup-selected-overlay
9550             ;; Move old overlay.
9551             (gnus-move-overlay
9552              gnus-newsgroup-selected-overlay from to (current-buffer))
9553           ;; Create new overlay.
9554           (gnus-overlay-put
9555            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9556            'face gnus-summary-selected-face))))))
9557
9558 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9559 (defun gnus-summary-highlight-line ()
9560   "Highlight current line according to `gnus-summary-highlight'."
9561   (let* ((list gnus-summary-highlight)
9562          (p (point))
9563          (end (progn (end-of-line) (point)))
9564          ;; now find out where the line starts and leave point there.
9565          (beg (progn (beginning-of-line) (point)))
9566          (article (gnus-summary-article-number))
9567          (score (or (cdr (assq (or article gnus-current-article)
9568                                gnus-newsgroup-scored))
9569                     gnus-summary-default-score 0))
9570          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9571          (inhibit-read-only t))
9572     ;; Eval the cars of the lists until we find a match.
9573     (let ((default gnus-summary-default-score))
9574       (while (and list
9575                   (not (eval (caar list))))
9576         (setq list (cdr list))))
9577     (let ((face (cdar list)))
9578       (unless (eq face (get-text-property beg 'face))
9579         (gnus-put-text-property-excluding-characters-with-faces
9580          beg end 'face
9581          (setq face (if (boundp face) (symbol-value face) face)))
9582         (when gnus-summary-highlight-line-function
9583           (funcall gnus-summary-highlight-line-function article face))))
9584     (goto-char p)))
9585
9586 (defun gnus-update-read-articles (group unread &optional compute)
9587   "Update the list of read articles in GROUP."
9588   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9589          (entry (gnus-gethash group gnus-newsrc-hashtb))
9590          (info (nth 2 entry))
9591          (prev 1)
9592          (unread (sort (copy-sequence unread) '<))
9593          read)
9594     (if (or (not info) (not active))
9595         ;; There is no info on this group if it was, in fact,
9596         ;; killed.  Gnus stores no information on killed groups, so
9597         ;; there's nothing to be done.
9598         ;; One could store the information somewhere temporarily,
9599         ;; perhaps...  Hmmm...
9600         ()
9601       ;; Remove any negative articles numbers.
9602       (while (and unread (< (car unread) 0))
9603         (setq unread (cdr unread)))
9604       ;; Remove any expired article numbers
9605       (while (and unread (< (car unread) (car active)))
9606         (setq unread (cdr unread)))
9607       ;; Compute the ranges of read articles by looking at the list of
9608       ;; unread articles.
9609       (while unread
9610         (when (/= (car unread) prev)
9611           (push (if (= prev (1- (car unread))) prev
9612                   (cons prev (1- (car unread))))
9613                 read))
9614         (setq prev (1+ (car unread)))
9615         (setq unread (cdr unread)))
9616       (when (<= prev (cdr active))
9617         (push (cons prev (cdr active)) read))
9618       (setq read (if (> (length read) 1) (nreverse read) read))
9619       (if compute
9620           read
9621         (save-excursion
9622           (set-buffer gnus-group-buffer)
9623           (gnus-undo-register
9624             `(progn
9625                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9626                (gnus-info-set-read ',info ',(gnus-info-read info))
9627                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9628                (gnus-group-update-group ,group t))))
9629         ;; Propagate the read marks to the backend.
9630         (if (gnus-check-backend-function 'request-set-mark group)
9631             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9632                   (add (gnus-remove-from-range read (gnus-info-read info))))
9633               (when (or add del)
9634                 (unless (gnus-check-group group)
9635                   (error "Can't open server for %s" group))
9636                 (gnus-request-set-mark
9637                  group (delq nil (list (if add (list add 'add '(read)))
9638                                        (if del (list del 'del '(read)))))))))
9639         ;; Enter this list into the group info.
9640         (gnus-info-set-read info read)
9641         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9642         (gnus-get-unread-articles-in-group info (gnus-active group))
9643         t))))
9644
9645 (defun gnus-offer-save-summaries ()
9646   "Offer to save all active summary buffers."
9647   (save-excursion
9648     (let ((buflist (buffer-list))
9649           buffers bufname)
9650       ;; Go through all buffers and find all summaries.
9651       (while buflist
9652         (and (setq bufname (buffer-name (car buflist)))
9653              (string-match "Summary" bufname)
9654              (save-excursion
9655                (set-buffer bufname)
9656                ;; We check that this is, indeed, a summary buffer.
9657                (and (eq major-mode 'gnus-summary-mode)
9658                     ;; Also make sure this isn't bogus.
9659                     gnus-newsgroup-prepared
9660                     ;; Also make sure that this isn't a dead summary buffer.
9661                     (not gnus-dead-summary-mode)))
9662              (push bufname buffers))
9663         (setq buflist (cdr buflist)))
9664       ;; Go through all these summary buffers and offer to save them.
9665       (when buffers
9666         (map-y-or-n-p
9667          "Update summary buffer %s? "
9668          (lambda (buf)
9669            (switch-to-buffer buf)
9670            (gnus-summary-exit))
9671          buffers)))))
9672
9673
9674 ;;; @ for mime-partial
9675 ;;;
9676
9677 (defun gnus-request-partial-message ()
9678   (save-excursion
9679     (let ((number (gnus-summary-article-number))
9680           (group gnus-newsgroup-name)
9681           (mother gnus-article-buffer))
9682       (set-buffer (get-buffer-create " *Partial Article*"))
9683       (erase-buffer)
9684       (setq mime-preview-buffer mother)
9685       (gnus-request-article-this-buffer number group)
9686       (mime-parse-buffer)
9687       )))
9688
9689 (autoload 'mime-combine-message/partial-pieces-automatically
9690   "mime-partial"
9691   "Internal method to combine message/partial messages automatically.")
9692
9693 (mime-add-condition
9694  'action '((type . message)(subtype . partial)
9695            (major-mode . gnus-original-article-mode)
9696            (method . mime-combine-message/partial-pieces-automatically)
9697            (summary-buffer-exp . gnus-summary-buffer)
9698            (request-partial-message-method . gnus-request-partial-message)
9699            ))
9700
9701
9702 ;;; @ for message/rfc822
9703 ;;;
9704
9705 (defun gnus-mime-extract-message/rfc822 (entity situation)
9706   (let (group article num cwin swin cur)
9707     (with-temp-buffer
9708       (mime-insert-entity-content entity)
9709       (setq group (or (cdr (assq 'group situation))
9710                       (completing-read "Group: "
9711                                        gnus-active-hashtb
9712                                        nil
9713                                        (gnus-read-active-file-p)
9714                                        gnus-newsgroup-name))
9715             article (gnus-request-accept-article group)))
9716     (when (and (consp article)
9717                (numberp (setq article (cdr article))))
9718       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9719             cwin (get-buffer-window (current-buffer) t))
9720       (save-window-excursion
9721         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9722             (select-window swin)
9723           (set-buffer gnus-summary-buffer))
9724         (setq cur gnus-current-article)
9725         (forward-line num)
9726         (let (gnus-show-threads)
9727           (gnus-summary-goto-subject article t))
9728         (gnus-summary-clear-mark-forward 1)
9729         (gnus-summary-goto-subject cur))
9730       (when (and cwin (window-frame cwin))
9731         (select-frame (window-frame cwin)))
9732       (when (boundp 'mime-acting-situation-to-override)
9733         (set-alist 'mime-acting-situation-to-override
9734                    'group
9735                    group)
9736         (set-alist 'mime-acting-situation-to-override
9737                    'after-method
9738                    `(progn
9739                       (save-current-buffer
9740                         (set-buffer gnus-group-buffer)
9741                         (gnus-activate-group ,group))
9742                       (gnus-summary-goto-article ,cur
9743                                                  gnus-show-all-headers)))
9744         (set-alist 'mime-acting-situation-to-override
9745                    'number num)))))
9746
9747 (mime-add-condition
9748  'action '((type . message)(subtype . rfc822)
9749            (major-mode . gnus-original-article-mode)
9750            (method . gnus-mime-extract-message/rfc822)
9751            (mode . "extract")
9752            ))
9753
9754 (mime-add-condition
9755  'action '((type . message)(subtype . news)
9756            (major-mode . gnus-original-article-mode)
9757            (method . gnus-mime-extract-message/rfc822)
9758            (mode . "extract")
9759            ))
9760
9761 (defun gnus-mime-extract-multipart (entity situation)
9762   (let ((children (mime-entity-children entity))
9763         mime-acting-situation-to-override
9764         f)
9765     (while children
9766       (mime-play-entity (car children)
9767                         (cons (assq 'mode situation)
9768                               mime-acting-situation-to-override))
9769       (setq children (cdr children)))
9770     (if (setq f (cdr (assq 'after-method
9771                            mime-acting-situation-to-override)))
9772         (eval f)
9773       )))  
9774
9775 (mime-add-condition
9776  'action '((type . multipart)
9777            (method . gnus-mime-extract-multipart)
9778            (mode . "extract")
9779            )
9780  'with-default)
9781
9782
9783 ;;; @ end
9784 ;;;
9785
9786 (defun gnus-summary-setup-default-charset ()
9787   "Setup newsgroup default charset."
9788   (if (equal gnus-newsgroup-name "nndraft:drafts")
9789       (setq gnus-newsgroup-charset nil)
9790     (let* ((name (and gnus-newsgroup-name
9791                       (gnus-group-real-name gnus-newsgroup-name)))
9792            (ignored-charsets 
9793             (or gnus-newsgroup-ephemeral-ignored-charsets
9794                 (append
9795                  (and gnus-newsgroup-name
9796                       (or (gnus-group-find-parameter gnus-newsgroup-name
9797                                                      'ignored-charsets t)
9798                           (let ((alist gnus-group-ignored-charsets-alist)
9799                                 elem (charsets nil))
9800                             (while (setq elem (pop alist))
9801                               (when (and name
9802                                          (string-match (car elem) name))
9803                                 (setq alist nil
9804                                       charsets (cdr elem))))
9805                             charsets)))
9806                  gnus-newsgroup-ignored-charsets))))
9807       (setq gnus-newsgroup-charset
9808             (or gnus-newsgroup-ephemeral-charset
9809                 (and gnus-newsgroup-name
9810                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9811                          (let ((alist gnus-group-charset-alist)
9812                                elem charset)
9813                            (while (setq elem (pop alist))
9814                              (when (and name
9815                                         (string-match (car elem) name))
9816                                (setq alist nil
9817                                      charset (cadr elem))))
9818                            charset)))
9819                 gnus-default-charset))
9820       (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9821            ignored-charsets))))
9822
9823 ;;;
9824 ;;; Mime Commands
9825 ;;;
9826
9827 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9828   "Display the current article buffer fully MIME-buttonized.
9829 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9830 treated as multipart/mixed."
9831   (interactive "P")
9832   (require 'gnus-art)
9833   (let ((gnus-unbuttonized-mime-types nil)
9834         (gnus-mime-display-multipart-as-mixed show-all-parts))
9835     (gnus-summary-show-article)))
9836
9837 (defun gnus-summary-repair-multipart (article)
9838   "Add a Content-Type header to a multipart article without one."
9839   (interactive (list (gnus-summary-article-number)))
9840   (gnus-with-article article
9841     (message-narrow-to-head)
9842     (goto-char (point-max))
9843     (widen)
9844     (when (search-forward "\n--" nil t)
9845       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9846         (message-narrow-to-head)
9847         (message-remove-header "Mime-Version")
9848         (message-remove-header "Content-Type")
9849         (goto-char (point-max))
9850         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9851                         separator))
9852         (insert "Mime-Version: 1.0\n")
9853         (widen))))
9854   (let (gnus-mark-article-hook)
9855     (gnus-summary-select-article t t nil article)))
9856
9857 (defun gnus-summary-toggle-display-buttonized ()
9858   "Toggle the buttonizing of the article buffer."
9859   (interactive)
9860   (require 'gnus-art)
9861   (if (setq gnus-inhibit-mime-unbuttonizing
9862             (not gnus-inhibit-mime-unbuttonizing))
9863       (let ((gnus-unbuttonized-mime-types nil))
9864         (gnus-summary-show-article))
9865     (gnus-summary-show-article)))
9866
9867 ;;;
9868 ;;; Intelli-mouse commmands
9869 ;;;
9870
9871 (defun gnus-wheel-summary-scroll (event)
9872   (interactive "e")
9873   (let ((amount (if (memq 'shift (event-modifiers event))
9874                     (car gnus-wheel-scroll-amount)
9875                   (cdr gnus-wheel-scroll-amount)))
9876         (direction (- (* (static-if (featurep 'xemacs)
9877                              (event-button event)
9878                            (cond ((eq 'mouse-4 (event-basic-type event))
9879                                   4)
9880                                  ((eq 'mouse-5 (event-basic-type event))
9881                                   5)))
9882                          2) 9))
9883         edge)
9884     (gnus-summary-scroll-up (* amount direction))
9885     (when (gnus-eval-in-buffer-window gnus-article-buffer
9886             (save-restriction
9887               (widen)
9888               (and (if (< 0 direction)
9889                        (gnus-article-next-page 0)
9890                      (gnus-article-prev-page 0)
9891                      (bobp))
9892                    (if (setq edge (get-text-property
9893                                    (point-min) 'gnus-wheel-edge))
9894                        (setq edge (* edge direction))
9895                      (setq edge -1))
9896                    (or (plusp edge)
9897                        (let ((buffer-read-only nil)
9898                              (inhibit-read-only t))
9899                          (put-text-property (point-min) (point-max)
9900                                             'gnus-wheel-edge direction)
9901                          nil))
9902                    (or (> edge gnus-wheel-edge-resistance)
9903                        (let ((buffer-read-only nil)
9904                              (inhibit-read-only t))
9905                          (put-text-property (point-min) (point-max)
9906                                             'gnus-wheel-edge
9907                                             (* (1+ edge) direction))
9908                          nil))
9909                    (eq last-command 'gnus-wheel-summary-scroll))))
9910       (gnus-summary-next-article nil nil (minusp direction)))))
9911
9912 (defun gnus-wheel-install ()
9913   "Enable mouse wheel support on summary window."
9914   (when gnus-use-wheel
9915     (let ((keys 
9916            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9917       (dolist (key keys)
9918         (define-key gnus-summary-mode-map key
9919           'gnus-wheel-summary-scroll)))))
9920
9921 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9922
9923 ;;;
9924 ;;; Traditional PGP commmands
9925 ;;;
9926
9927 (defun gnus-summary-decrypt-article (&optional force)
9928   "Decrypt the current article in traditional PGP way.
9929 This will have permanent effect only in mail groups.
9930 If FORCE is non-nil, allow editing of articles even in read-only
9931 groups."
9932   (interactive "P")
9933   (gnus-summary-select-article t)
9934   (gnus-eval-in-buffer-window gnus-article-buffer
9935     (save-excursion
9936       (save-restriction
9937         (widen)
9938         (goto-char (point-min))
9939         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
9940           (error "Not a traditional PGP message!"))
9941         (let ((armor-start (match-beginning 0)))
9942           (if (and (pgg-decrypt-region armor-start (point-max))
9943                    (or force (not (gnus-group-read-only-p))))
9944               (let ((inhibit-read-only t) 
9945                     buffer-read-only)
9946                 (delete-region armor-start
9947                                (progn 
9948                                  (re-search-forward "^-+END PGP" nil t)
9949                                  (beginning-of-line 2)
9950                                  (point)))
9951                 (insert-buffer-substring pgg-output-buffer))))))))
9952
9953 (defun gnus-summary-verify-article ()
9954   "Verify the current article in traditional PGP way."
9955   (interactive)
9956   (save-excursion
9957     (set-buffer gnus-original-article-buffer)
9958     (goto-char (point-min))
9959     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
9960       (error "Not a traditional PGP message!"))
9961     (re-search-forward "^-+END PGP" nil t)
9962     (beginning-of-line 2)
9963     (call-interactively (function pgg-verify-region))))
9964
9965 ;;;
9966 ;;; Generic summary marking commands
9967 ;;;
9968
9969 (defvar gnus-summary-marking-alist
9970   '((read gnus-del-mark "d")
9971     (unread gnus-unread-mark "u")
9972     (ticked gnus-ticked-mark "!")
9973     (dormant gnus-dormant-mark "?")
9974     (expirable gnus-expirable-mark "e"))
9975   "An alist of names/marks/keystrokes.")
9976
9977 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9978 (defvar gnus-summary-mark-map)
9979
9980 (defun gnus-summary-make-all-marking-commands ()
9981   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9982   (dolist (elem gnus-summary-marking-alist)
9983     (apply 'gnus-summary-make-marking-command elem)))
9984
9985 (defun gnus-summary-make-marking-command (name mark keystroke)
9986   (let ((map (make-sparse-keymap)))
9987     (define-key gnus-summary-generic-mark-map keystroke map)
9988     (dolist (lway `((next "next" next nil "n")
9989                     (next-unread "next unread" next t "N")
9990                     (prev "previous" prev nil "p")
9991                     (prev-unread "previous unread" prev t "P")
9992                     (nomove "" nil nil ,keystroke)))
9993       (let ((func (gnus-summary-make-marking-command-1
9994                    mark (car lway) lway name)))
9995         (setq func (eval func))
9996         (define-key map (nth 4 lway) func)))))
9997       
9998 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9999   `(defun ,(intern
10000             (format "gnus-summary-put-mark-as-%s%s"
10001                     name (if (eq way 'nomove)
10002                              ""
10003                            (concat "-" (symbol-name way)))))
10004      (n)
10005      ,(format
10006        "Mark the current article as %s%s.
10007 If N, the prefix, then repeat N times.
10008 If N is negative, move in reverse order.
10009 The difference between N and the actual number of articles marked is
10010 returned."
10011        name (car (cdr lway)))
10012      (interactive "p")
10013      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10014     
10015 (defun gnus-summary-generic-mark (n mark move unread)
10016   "Mark N articles with MARK."
10017   (unless (eq major-mode 'gnus-summary-mode)
10018     (error "This command can only be used in the summary buffer"))
10019   (gnus-summary-show-thread)
10020   (let ((nummove
10021          (cond
10022           ((eq move 'next) 1)
10023           ((eq move 'prev) -1)
10024           (t 0))))
10025     (if (zerop nummove)
10026         (setq n 1)
10027       (when (< n 0)
10028         (setq n (abs n)
10029               nummove (* -1 nummove))))
10030     (while (and (> n 0)
10031                 (gnus-summary-mark-article nil mark)
10032                 (zerop (gnus-summary-next-subject nummove unread t)))
10033       (setq n (1- n)))
10034     (when (/= 0 n)
10035       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10036     (gnus-summary-recenter)
10037     (gnus-summary-position-point)
10038     (gnus-set-mode-line 'summary)
10039     n))
10040
10041 (gnus-summary-make-all-marking-commands)
10042
10043 (gnus-ems-redefine)
10044
10045 (provide 'gnus-sum)
10046
10047 (run-hooks 'gnus-sum-load-hook)
10048
10049 ;;; gnus-sum.el ends here