Async with Gnus.
[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 ;;; Internal variables
934
935 (defvar gnus-article-mime-handles nil)
936 (defvar gnus-article-decoded-p nil)
937 (defvar gnus-scores-exclude-files nil)
938 (defvar gnus-page-broken nil)
939 (defvar gnus-inhibit-mime-unbuttonizing nil)
940
941 (defvar gnus-original-article nil)
942 (defvar gnus-article-internal-prepare-hook nil)
943 (defvar gnus-newsgroup-process-stack nil)
944
945 (defvar gnus-thread-indent-array nil)
946 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
947 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
948   "Function called to sort the articles within a thread after it has been gathered together.")
949
950 ;; Avoid highlighting in kill files.
951 (defvar gnus-summary-inhibit-highlight nil)
952 (defvar gnus-newsgroup-selected-overlay nil)
953 (defvar gnus-inhibit-limiting nil)
954 (defvar gnus-newsgroup-adaptive-score-file nil)
955 (defvar gnus-current-score-file nil)
956 (defvar gnus-current-move-group nil)
957 (defvar gnus-current-copy-group nil)
958 (defvar gnus-current-crosspost-group nil)
959
960 (defvar gnus-newsgroup-dependencies nil)
961 (defvar gnus-newsgroup-adaptive nil)
962 (defvar gnus-summary-display-article-function nil)
963 (defvar gnus-summary-highlight-line-function nil
964   "Function called after highlighting a summary line.")
965
966 (defvar gnus-summary-line-format-alist
967   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
968     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
969     (?s gnus-tmp-subject-or-nil ?s)
970     (?n gnus-tmp-name ?s)
971     (?A (std11-address-string
972          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
973     (?a (or (std11-full-name-string
974              (car (mime-entity-read-field gnus-tmp-header 'From)))
975             gnus-tmp-from) ?s)
976     (?F gnus-tmp-from ?s)
977     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
978     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
979     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
980     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
981     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
982     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
983     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
984     (?L gnus-tmp-lines ?d)
985     (?I gnus-tmp-indentation ?s)
986     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
987     (?R gnus-tmp-replied ?c)
988     (?\[ gnus-tmp-opening-bracket ?c)
989     (?\] gnus-tmp-closing-bracket ?c)
990     (?\> (make-string gnus-tmp-level ? ) ?s)
991     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
992     (?i gnus-tmp-score ?d)
993     (?z gnus-tmp-score-char ?c)
994     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
995     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
996     (?U gnus-tmp-unread ?c)
997     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
998     (?t (gnus-summary-number-of-articles-in-thread
999          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1000         ?d)
1001     (?e (gnus-summary-number-of-articles-in-thread
1002          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1003         ?c)
1004     (?u gnus-tmp-user-defined ?s)
1005     (?P (gnus-pick-line-number) ?d))
1006   "An alist of format specifications that can appear in summary lines,
1007 and what variables they correspond with, along with the type of the
1008 variable (string, integer, character, etc).")
1009
1010 (defvar gnus-summary-dummy-line-format-alist
1011   `((?S gnus-tmp-subject ?s)
1012     (?N gnus-tmp-number ?d)
1013     (?u gnus-tmp-user-defined ?s)))
1014
1015 (defvar gnus-summary-mode-line-format-alist
1016   `((?G gnus-tmp-group-name ?s)
1017     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1018     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1019     (?A gnus-tmp-article-number ?d)
1020     (?Z gnus-tmp-unread-and-unselected ?s)
1021     (?V gnus-version ?s)
1022     (?U gnus-tmp-unread-and-unticked ?d)
1023     (?S gnus-tmp-subject ?s)
1024     (?e gnus-tmp-unselected ?d)
1025     (?u gnus-tmp-user-defined ?s)
1026     (?d (length gnus-newsgroup-dormant) ?d)
1027     (?t (length gnus-newsgroup-marked) ?d)
1028     (?r (length gnus-newsgroup-reads) ?d)
1029     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1030     (?E gnus-newsgroup-expunged-tally ?d)
1031     (?s (gnus-current-score-file-nondirectory) ?s)))
1032
1033 (defvar gnus-last-search-regexp nil
1034   "Default regexp for article search command.")
1035
1036 (defvar gnus-summary-search-article-matched-data nil
1037   "Last matched data of article search command.  It is the local variable
1038 in `gnus-article-buffer' which consists of the list of start position,
1039 end position and text.")
1040
1041 (defvar gnus-last-shell-command nil
1042   "Default shell command on article.")
1043
1044 (defvar gnus-newsgroup-begin nil)
1045 (defvar gnus-newsgroup-end nil)
1046 (defvar gnus-newsgroup-last-rmail nil)
1047 (defvar gnus-newsgroup-last-mail nil)
1048 (defvar gnus-newsgroup-last-folder nil)
1049 (defvar gnus-newsgroup-last-file nil)
1050 (defvar gnus-newsgroup-auto-expire nil)
1051 (defvar gnus-newsgroup-active nil)
1052
1053 (defvar gnus-newsgroup-data nil)
1054 (defvar gnus-newsgroup-data-reverse nil)
1055 (defvar gnus-newsgroup-limit nil)
1056 (defvar gnus-newsgroup-limits nil)
1057
1058 (defvar gnus-newsgroup-unreads nil
1059   "List of unread articles in the current newsgroup.")
1060
1061 (defvar gnus-newsgroup-unselected nil
1062   "List of unselected unread articles in the current newsgroup.")
1063
1064 (defvar gnus-newsgroup-reads nil
1065   "Alist of read articles and article marks in the current newsgroup.")
1066
1067 (defvar gnus-newsgroup-expunged-tally nil)
1068
1069 (defvar gnus-newsgroup-marked nil
1070   "List of ticked articles in the current newsgroup (a subset of unread art).")
1071
1072 (defvar gnus-newsgroup-killed nil
1073   "List of ranges of articles that have been through the scoring process.")
1074
1075 (defvar gnus-newsgroup-cached nil
1076   "List of articles that come from the article cache.")
1077
1078 (defvar gnus-newsgroup-saved nil
1079   "List of articles that have been saved.")
1080
1081 (defvar gnus-newsgroup-kill-headers nil)
1082
1083 (defvar gnus-newsgroup-replied nil
1084   "List of articles that have been replied to in the current newsgroup.")
1085
1086 (defvar gnus-newsgroup-expirable nil
1087   "List of articles in the current newsgroup that can be expired.")
1088
1089 (defvar gnus-newsgroup-processable nil
1090   "List of articles in the current newsgroup that can be processed.")
1091
1092 (defvar gnus-newsgroup-downloadable nil
1093   "List of articles in the current newsgroup that can be processed.")
1094
1095 (defvar gnus-newsgroup-undownloaded nil
1096   "List of articles in the current newsgroup that haven't been downloaded..")
1097
1098 (defvar gnus-newsgroup-unsendable nil
1099   "List of articles in the current newsgroup that won't be sent.")
1100
1101 (defvar gnus-newsgroup-bookmarks nil
1102   "List of articles in the current newsgroup that have bookmarks.")
1103
1104 (defvar gnus-newsgroup-dormant nil
1105   "List of dormant articles in the current newsgroup.")
1106
1107 (defvar gnus-newsgroup-scored nil
1108   "List of scored articles in the current newsgroup.")
1109
1110 (defvar gnus-newsgroup-incorporated nil
1111   "List of incorporated articles in the current newsgroup.")
1112
1113 (defvar gnus-newsgroup-headers nil
1114   "List of article headers in the current newsgroup.")
1115
1116 (defvar gnus-newsgroup-threads nil)
1117
1118 (defvar gnus-newsgroup-prepared nil
1119   "Whether the current group has been prepared properly.")
1120
1121 (defvar gnus-newsgroup-ancient nil
1122   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1123
1124 (defvar gnus-newsgroup-sparse nil)
1125
1126 (defvar gnus-current-article nil)
1127 (defvar gnus-article-current nil)
1128 (defvar gnus-current-headers nil)
1129 (defvar gnus-have-all-headers nil)
1130 (defvar gnus-last-article nil)
1131 (defvar gnus-newsgroup-history nil)
1132 (defvar gnus-newsgroup-charset nil)
1133 (defvar gnus-newsgroup-ephemeral-charset nil)
1134 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1135
1136 (defconst gnus-summary-local-variables
1137   '(gnus-newsgroup-name
1138     gnus-newsgroup-begin gnus-newsgroup-end
1139     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1140     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1141     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1142     gnus-newsgroup-unselected gnus-newsgroup-marked
1143     gnus-newsgroup-reads gnus-newsgroup-saved
1144     gnus-newsgroup-replied gnus-newsgroup-expirable
1145     gnus-newsgroup-processable gnus-newsgroup-killed
1146     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1147     gnus-newsgroup-unsendable
1148     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1149     gnus-newsgroup-headers gnus-newsgroup-threads
1150     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1151     gnus-current-article gnus-current-headers gnus-have-all-headers
1152     gnus-last-article gnus-article-internal-prepare-hook
1153     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1154     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1155     gnus-thread-expunge-below
1156     gnus-score-alist gnus-current-score-file
1157     (gnus-summary-expunge-below . global)
1158     (gnus-summary-mark-below . global)
1159     gnus-newsgroup-active gnus-scores-exclude-files
1160     gnus-newsgroup-history gnus-newsgroup-ancient
1161     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1162     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1163     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1164     (gnus-newsgroup-expunged-tally . 0)
1165     gnus-cache-removable-articles gnus-newsgroup-cached
1166     gnus-newsgroup-data gnus-newsgroup-data-reverse
1167     gnus-newsgroup-limit gnus-newsgroup-limits
1168     gnus-newsgroup-charset
1169     gnus-newsgroup-incorporated)
1170   "Variables that are buffer-local to the summary buffers.")
1171
1172 ;; Byte-compiler warning.
1173 (defvar gnus-article-mode-map)
1174
1175 ;; Subject simplification.
1176
1177 (defun gnus-simplify-whitespace (str)
1178   "Remove excessive whitespace."
1179   (let ((mystr str))
1180     ;; Multiple spaces.
1181     (while (string-match "[ \t][ \t]+" mystr)
1182       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1183                           " "
1184                           (substring mystr (match-end 0)))))
1185     ;; Leading spaces.
1186     (when (string-match "^[ \t]+" mystr)
1187       (setq mystr (substring mystr (match-end 0))))
1188     ;; Trailing spaces.
1189     (when (string-match "[ \t]+$" mystr)
1190       (setq mystr (substring mystr 0 (match-beginning 0))))
1191     mystr))
1192
1193 (defsubst gnus-simplify-subject-re (subject)
1194   "Remove \"Re:\" from subject lines."
1195   (if (string-match "^[Rr][Ee]: *" subject)
1196       (substring subject (match-end 0))
1197     subject))
1198
1199 (defun gnus-simplify-subject (subject &optional re-only)
1200   "Remove `Re:' and words in parentheses.
1201 If RE-ONLY is non-nil, strip leading `Re:'s only."
1202   (let ((case-fold-search t))           ;Ignore case.
1203     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1204     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1205       (setq subject (substring subject (match-end 0))))
1206     ;; Remove uninteresting prefixes.
1207     (when (and (not re-only)
1208                gnus-simplify-ignored-prefixes
1209                (string-match gnus-simplify-ignored-prefixes subject))
1210       (setq subject (substring subject (match-end 0))))
1211     ;; Remove words in parentheses from end.
1212     (unless re-only
1213       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1214         (setq subject (substring subject 0 (match-beginning 0)))))
1215     ;; Return subject string.
1216     subject))
1217
1218 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1219 ;; all whitespace.
1220 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1221   (goto-char (point-min))
1222   (while (re-search-forward regexp nil t)
1223     (replace-match (or newtext ""))))
1224
1225 (defun gnus-simplify-buffer-fuzzy ()
1226   "Simplify string in the buffer fuzzily.
1227 The string in the accessible portion of the current buffer is simplified.
1228 It is assumed to be a single-line subject.
1229 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1230 matter is removed.  Additional things can be deleted by setting
1231 gnus-simplify-subject-fuzzy-regexp."
1232   (let ((case-fold-search t)
1233         (modified-tick))
1234     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1235
1236     (while (not (eq modified-tick (buffer-modified-tick)))
1237       (setq modified-tick (buffer-modified-tick))
1238       (cond
1239        ((listp gnus-simplify-subject-fuzzy-regexp)
1240         (mapcar 'gnus-simplify-buffer-fuzzy-step
1241                 gnus-simplify-subject-fuzzy-regexp))
1242        (gnus-simplify-subject-fuzzy-regexp
1243         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1244       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1245       (gnus-simplify-buffer-fuzzy-step
1246        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1247       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1248
1249     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1250     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1251     (gnus-simplify-buffer-fuzzy-step " $")
1252     (gnus-simplify-buffer-fuzzy-step "^ +")))
1253
1254 (defun gnus-simplify-subject-fuzzy (subject)
1255   "Simplify a subject string fuzzily.
1256 See `gnus-simplify-buffer-fuzzy' for details."
1257   (save-excursion
1258     (gnus-set-work-buffer)
1259     (let ((case-fold-search t))
1260       ;; Remove uninteresting prefixes.
1261       (when (and gnus-simplify-ignored-prefixes
1262                  (string-match gnus-simplify-ignored-prefixes subject))
1263         (setq subject (substring subject (match-end 0))))
1264       (insert subject)
1265       (inline (gnus-simplify-buffer-fuzzy))
1266       (buffer-string))))
1267
1268 (defsubst gnus-simplify-subject-fully (subject)
1269   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1270   (cond
1271    (gnus-simplify-subject-functions
1272     (gnus-map-function gnus-simplify-subject-functions subject))
1273    ((null gnus-summary-gather-subject-limit)
1274     (gnus-simplify-subject-re subject))
1275    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1276     (gnus-simplify-subject-fuzzy subject))
1277    ((numberp gnus-summary-gather-subject-limit)
1278     (gnus-limit-string (gnus-simplify-subject-re subject)
1279                        gnus-summary-gather-subject-limit))
1280    (t
1281     subject)))
1282
1283 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1284   "Check whether two subjects are equal.
1285 If optional argument simple-first is t, first argument is already
1286 simplified."
1287   (cond
1288    ((null simple-first)
1289     (equal (gnus-simplify-subject-fully s1)
1290            (gnus-simplify-subject-fully s2)))
1291    (t
1292     (equal s1
1293            (gnus-simplify-subject-fully s2)))))
1294
1295 (defun gnus-summary-bubble-group ()
1296   "Increase the score of the current group.
1297 This is a handy function to add to `gnus-summary-exit-hook' to
1298 increase the score of each group you read."
1299   (gnus-group-add-score gnus-newsgroup-name))
1300
1301 \f
1302 ;;;
1303 ;;; Gnus summary mode
1304 ;;;
1305
1306 (put 'gnus-summary-mode 'mode-class 'special)
1307
1308 (when t
1309   ;; Non-orthogonal keys
1310
1311   (gnus-define-keys gnus-summary-mode-map
1312     " " gnus-summary-next-page
1313     "\177" gnus-summary-prev-page
1314     [delete] gnus-summary-prev-page
1315     [backspace] gnus-summary-prev-page
1316     "\r" gnus-summary-scroll-up
1317     "\M-\r" gnus-summary-scroll-down
1318     "n" gnus-summary-next-unread-article
1319     "p" gnus-summary-prev-unread-article
1320     "N" gnus-summary-next-article
1321     "P" gnus-summary-prev-article
1322     "\M-\C-n" gnus-summary-next-same-subject
1323     "\M-\C-p" gnus-summary-prev-same-subject
1324     "\M-n" gnus-summary-next-unread-subject
1325     "\M-p" gnus-summary-prev-unread-subject
1326     "." gnus-summary-first-unread-article
1327     "," gnus-summary-best-unread-article
1328     "\M-s" gnus-summary-search-article-forward
1329     "\M-r" gnus-summary-search-article-backward
1330     "<" gnus-summary-beginning-of-article
1331     ">" gnus-summary-end-of-article
1332     "j" gnus-summary-goto-article
1333     "^" gnus-summary-refer-parent-article
1334     "\M-^" gnus-summary-refer-article
1335     "u" gnus-summary-tick-article-forward
1336     "!" gnus-summary-tick-article-forward
1337     "U" gnus-summary-tick-article-backward
1338     "d" gnus-summary-mark-as-read-forward
1339     "D" gnus-summary-mark-as-read-backward
1340     "E" gnus-summary-mark-as-expirable
1341     "\M-u" gnus-summary-clear-mark-forward
1342     "\M-U" gnus-summary-clear-mark-backward
1343     "k" gnus-summary-kill-same-subject-and-select
1344     "\C-k" gnus-summary-kill-same-subject
1345     "\M-\C-k" gnus-summary-kill-thread
1346     "\M-\C-l" gnus-summary-lower-thread
1347     "e" gnus-summary-edit-article
1348     "#" gnus-summary-mark-as-processable
1349     "\M-#" gnus-summary-unmark-as-processable
1350     "\M-\C-t" gnus-summary-toggle-threads
1351     "\M-\C-s" gnus-summary-show-thread
1352     "\M-\C-h" gnus-summary-hide-thread
1353     "\M-\C-f" gnus-summary-next-thread
1354     "\M-\C-b" gnus-summary-prev-thread
1355     [(meta down)] gnus-summary-next-thread
1356     [(meta up)] gnus-summary-prev-thread
1357     "\M-\C-u" gnus-summary-up-thread
1358     "\M-\C-d" gnus-summary-down-thread
1359     "&" gnus-summary-execute-command
1360     "c" gnus-summary-catchup-and-exit
1361     "\C-w" gnus-summary-mark-region-as-read
1362     "\C-t" gnus-summary-toggle-truncation
1363     "?" gnus-summary-mark-as-dormant
1364     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1365     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1366     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1367     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1368     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1369     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1370     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1371     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1372     "=" gnus-summary-expand-window
1373     "\C-x\C-s" gnus-summary-reselect-current-group
1374     "\M-g" gnus-summary-rescan-group
1375     "w" gnus-summary-stop-page-breaking
1376     "\C-c\C-r" gnus-summary-caesar-message
1377     "\M-t" gnus-summary-toggle-mime
1378     "f" gnus-summary-followup
1379     "F" gnus-summary-followup-with-original
1380     "C" gnus-summary-cancel-article
1381     "r" gnus-summary-reply
1382     "R" gnus-summary-reply-with-original
1383     "\C-c\C-f" gnus-summary-mail-forward
1384     "o" gnus-summary-save-article
1385     "\C-o" gnus-summary-save-article-mail
1386     "|" gnus-summary-pipe-output
1387     "\M-k" gnus-summary-edit-local-kill
1388     "\M-K" gnus-summary-edit-global-kill
1389     ;; "V" gnus-version
1390     "\C-c\C-d" gnus-summary-describe-group
1391     "q" gnus-summary-exit
1392     "Q" gnus-summary-exit-no-update
1393     "\C-c\C-i" gnus-info-find-node
1394     gnus-mouse-2 gnus-mouse-pick-article
1395     "m" gnus-summary-mail-other-window
1396     "a" gnus-summary-post-news
1397     "x" gnus-summary-limit-to-unread
1398     "s" gnus-summary-isearch-article
1399     "t" gnus-article-toggle-headers
1400     "g" gnus-summary-show-article
1401     "l" gnus-summary-goto-last-article
1402     "v" gnus-summary-preview-mime-message
1403     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1404     "\C-d" gnus-summary-enter-digest-group
1405     "\M-\C-d" gnus-summary-read-document
1406     "\M-\C-e" gnus-summary-edit-parameters
1407     "\M-\C-a" gnus-summary-customize-parameters
1408     "\C-c\C-b" gnus-bug
1409     "*" gnus-cache-enter-article
1410     "\M-*" gnus-cache-remove-article
1411     "\M-&" gnus-summary-universal-argument
1412     "\C-l" gnus-recenter
1413     "I" gnus-summary-increase-score
1414     "L" gnus-summary-lower-score
1415     "\M-i" gnus-symbolic-argument
1416     "h" gnus-summary-select-article-buffer
1417
1418     "V" gnus-summary-score-map
1419     "X" gnus-uu-extract-map
1420     "S" gnus-summary-send-map)
1421
1422   ;; Sort of orthogonal keymap
1423   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1424     "t" gnus-summary-tick-article-forward
1425     "!" gnus-summary-tick-article-forward
1426     "d" gnus-summary-mark-as-read-forward
1427     "r" gnus-summary-mark-as-read-forward
1428     "c" gnus-summary-clear-mark-forward
1429     " " gnus-summary-clear-mark-forward
1430     "e" gnus-summary-mark-as-expirable
1431     "x" gnus-summary-mark-as-expirable
1432     "?" gnus-summary-mark-as-dormant
1433     "b" gnus-summary-set-bookmark
1434     "B" gnus-summary-remove-bookmark
1435     "#" gnus-summary-mark-as-processable
1436     "\M-#" gnus-summary-unmark-as-processable
1437     "S" gnus-summary-limit-include-expunged
1438     "C" gnus-summary-catchup
1439     "H" gnus-summary-catchup-to-here
1440     "\C-c" gnus-summary-catchup-all
1441     "k" gnus-summary-kill-same-subject-and-select
1442     "K" gnus-summary-kill-same-subject
1443     "P" gnus-uu-mark-map)
1444
1445   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1446     "c" gnus-summary-clear-above
1447     "u" gnus-summary-tick-above
1448     "m" gnus-summary-mark-above
1449     "k" gnus-summary-kill-below)
1450
1451   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1452     "/" gnus-summary-limit-to-subject
1453     "n" gnus-summary-limit-to-articles
1454     "w" gnus-summary-pop-limit
1455     "s" gnus-summary-limit-to-subject
1456     "a" gnus-summary-limit-to-author
1457     "u" gnus-summary-limit-to-unread
1458     "m" gnus-summary-limit-to-marks
1459     "M" gnus-summary-limit-exclude-marks
1460     "v" gnus-summary-limit-to-score
1461     "*" gnus-summary-limit-include-cached
1462     "D" gnus-summary-limit-include-dormant
1463     "T" gnus-summary-limit-include-thread
1464     "d" gnus-summary-limit-exclude-dormant
1465     "t" gnus-summary-limit-to-age
1466     "x" gnus-summary-limit-to-extra 
1467     "E" gnus-summary-limit-include-expunged
1468     "c" gnus-summary-limit-exclude-childless-dormant
1469     "C" gnus-summary-limit-mark-excluded-as-read)
1470
1471   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1472     "n" gnus-summary-next-unread-article
1473     "p" gnus-summary-prev-unread-article
1474     "N" gnus-summary-next-article
1475     "P" gnus-summary-prev-article
1476     "\C-n" gnus-summary-next-same-subject
1477     "\C-p" gnus-summary-prev-same-subject
1478     "\M-n" gnus-summary-next-unread-subject
1479     "\M-p" gnus-summary-prev-unread-subject
1480     "f" gnus-summary-first-unread-article
1481     "b" gnus-summary-best-unread-article
1482     "j" gnus-summary-goto-article
1483     "g" gnus-summary-goto-subject
1484     "l" gnus-summary-goto-last-article
1485     "o" gnus-summary-pop-article)
1486
1487   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1488     "k" gnus-summary-kill-thread
1489     "l" gnus-summary-lower-thread
1490     "i" gnus-summary-raise-thread
1491     "T" gnus-summary-toggle-threads
1492     "t" gnus-summary-rethread-current
1493     "^" gnus-summary-reparent-thread
1494     "s" gnus-summary-show-thread
1495     "S" gnus-summary-show-all-threads
1496     "h" gnus-summary-hide-thread
1497     "H" gnus-summary-hide-all-threads
1498     "n" gnus-summary-next-thread
1499     "p" gnus-summary-prev-thread
1500     "u" gnus-summary-up-thread
1501     "o" gnus-summary-top-thread
1502     "d" gnus-summary-down-thread
1503     "#" gnus-uu-mark-thread
1504     "\M-#" gnus-uu-unmark-thread)
1505
1506   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1507     "g" gnus-summary-prepare
1508     "c" gnus-summary-insert-cached-articles)
1509
1510   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1511     "c" gnus-summary-catchup-and-exit
1512     "C" gnus-summary-catchup-all-and-exit
1513     "E" gnus-summary-exit-no-update
1514     "J" gnus-summary-jump-to-other-group
1515     "Q" gnus-summary-exit
1516     "Z" gnus-summary-exit
1517     "n" gnus-summary-catchup-and-goto-next-group
1518     "R" gnus-summary-reselect-current-group
1519     "G" gnus-summary-rescan-group
1520     "N" gnus-summary-next-group
1521     "s" gnus-summary-save-newsrc
1522     "P" gnus-summary-prev-group)
1523
1524   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1525     " " gnus-summary-next-page
1526     "n" gnus-summary-next-page
1527     "\177" gnus-summary-prev-page
1528     [delete] gnus-summary-prev-page
1529     "p" gnus-summary-prev-page
1530     "\r" gnus-summary-scroll-up
1531     "\M-\r" gnus-summary-scroll-down
1532     "<" gnus-summary-beginning-of-article
1533     ">" gnus-summary-end-of-article
1534     "b" gnus-summary-beginning-of-article
1535     "e" gnus-summary-end-of-article
1536     "^" gnus-summary-refer-parent-article
1537     "r" gnus-summary-refer-parent-article
1538     "D" gnus-summary-enter-digest-group
1539     "R" gnus-summary-refer-references
1540     "T" gnus-summary-refer-thread
1541     "g" gnus-summary-show-article
1542     "s" gnus-summary-isearch-article
1543     "P" gnus-summary-print-article
1544     "t" gnus-article-babel
1545     "d" gnus-summary-decrypt-article
1546     "v" gnus-summary-verify-article)
1547
1548   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1549     "b" gnus-article-add-buttons
1550     "B" gnus-article-add-buttons-to-head
1551     "o" gnus-article-treat-overstrike
1552     "e" gnus-article-emphasize
1553     "w" gnus-article-fill-cited-article
1554     "Q" gnus-article-fill-long-lines
1555     "C" gnus-article-capitalize-sentences
1556     "c" gnus-article-remove-cr
1557     "Z" gnus-article-decode-HZ
1558     "f" gnus-article-display-x-face
1559     "l" gnus-summary-stop-page-breaking
1560     "r" gnus-summary-caesar-message
1561     "t" gnus-article-toggle-headers
1562     "v" gnus-summary-verbose-headers
1563     "m" gnus-summary-toggle-mime
1564     "H" gnus-article-strip-headers-in-body
1565     "d" gnus-article-treat-dumbquotes
1566     "s" gnus-smiley-display)
1567
1568   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1569     "a" gnus-article-hide
1570     "h" gnus-article-toggle-headers
1571     "b" gnus-article-hide-boring-headers
1572     "s" gnus-article-hide-signature
1573     "c" gnus-article-hide-citation
1574     "C" gnus-article-hide-citation-in-followups
1575     "l" gnus-article-hide-list-identifiers
1576     "p" gnus-article-hide-pgp
1577     "B" gnus-article-strip-banner
1578     "P" gnus-article-hide-pem
1579     "\C-c" gnus-article-hide-citation-maybe)
1580
1581   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1582     "a" gnus-article-highlight
1583     "h" gnus-article-highlight-headers
1584     "c" gnus-article-highlight-citation
1585     "s" gnus-article-highlight-signature)
1586
1587   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1588     "z" gnus-article-date-ut
1589     "u" gnus-article-date-ut
1590     "l" gnus-article-date-local
1591     "e" gnus-article-date-lapsed
1592     "o" gnus-article-date-original
1593     "i" gnus-article-date-iso8601
1594     "s" gnus-article-date-user)
1595
1596   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1597     "t" gnus-article-remove-trailing-blank-lines
1598     "l" gnus-article-strip-leading-blank-lines
1599     "m" gnus-article-strip-multiple-blank-lines
1600     "a" gnus-article-strip-blank-lines
1601     "A" gnus-article-strip-all-blank-lines
1602     "s" gnus-article-strip-leading-space
1603     "e" gnus-article-strip-trailing-space)
1604
1605   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1606     "v" gnus-version
1607     "f" gnus-summary-fetch-faq
1608     "d" gnus-summary-describe-group
1609     "h" gnus-summary-describe-briefly
1610     "i" gnus-info-find-node)
1611
1612   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1613     "e" gnus-summary-expire-articles
1614     "\M-\C-e" gnus-summary-expire-articles-now
1615     "\177" gnus-summary-delete-article
1616     [delete] gnus-summary-delete-article
1617     [backspace] gnus-summary-delete-article
1618     "m" gnus-summary-move-article
1619     "r" gnus-summary-respool-article
1620     "w" gnus-summary-edit-article
1621     "c" gnus-summary-copy-article
1622     "B" gnus-summary-crosspost-article
1623     "q" gnus-summary-respool-query
1624     "t" gnus-summary-respool-trace
1625     "i" gnus-summary-import-article
1626     "p" gnus-summary-article-posted-p)
1627
1628   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1629     "o" gnus-summary-save-article
1630     "m" gnus-summary-save-article-mail
1631     "F" gnus-summary-write-article-file
1632     "r" gnus-summary-save-article-rmail
1633     "f" gnus-summary-save-article-file
1634     "b" gnus-summary-save-article-body-file
1635     "h" gnus-summary-save-article-folder
1636     "v" gnus-summary-save-article-vm
1637     "p" gnus-summary-pipe-output
1638     "s" gnus-soup-add-article)
1639
1640   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1641     "b" gnus-summary-display-buttonized
1642     "m" gnus-summary-repair-multipart
1643     "v" gnus-article-view-part
1644     "o" gnus-article-save-part
1645     "c" gnus-article-copy-part
1646     "e" gnus-article-externalize-part
1647     "i" gnus-article-inline-part
1648     "|" gnus-article-pipe-part))
1649
1650 (defun gnus-summary-make-menu-bar ()
1651   (gnus-turn-off-edit-menu 'summary)
1652
1653   (unless (boundp 'gnus-summary-misc-menu)
1654
1655     (easy-menu-define
1656      gnus-summary-kill-menu gnus-summary-mode-map ""
1657      (cons
1658       "Score"
1659       (nconc
1660        (list
1661         ["Enter score..." gnus-summary-score-entry t]
1662         ["Customize" gnus-score-customize t])
1663        (gnus-make-score-map 'increase)
1664        (gnus-make-score-map 'lower)
1665        '(("Mark"
1666           ["Kill below" gnus-summary-kill-below t]
1667           ["Mark above" gnus-summary-mark-above t]
1668           ["Tick above" gnus-summary-tick-above t]
1669           ["Clear above" gnus-summary-clear-above t])
1670          ["Current score" gnus-summary-current-score t]
1671          ["Set score" gnus-summary-set-score t]
1672          ["Switch current score file..." gnus-score-change-score-file t]
1673          ["Set mark below..." gnus-score-set-mark-below t]
1674          ["Set expunge below..." gnus-score-set-expunge-below t]
1675          ["Edit current score file" gnus-score-edit-current-scores t]
1676          ["Edit score file" gnus-score-edit-file t]
1677          ["Trace score" gnus-score-find-trace t]
1678          ["Find words" gnus-score-find-favourite-words t]
1679          ["Rescore buffer" gnus-summary-rescore t]
1680          ["Increase score..." gnus-summary-increase-score t]
1681          ["Lower score..." gnus-summary-lower-score t]))))
1682
1683     ;; Define both the Article menu in the summary buffer and the equivalent
1684     ;; Commands menu in the article buffer here for consistency.
1685     (let ((innards
1686            '(("Hide"
1687               ["All" gnus-article-hide t]
1688               ["Headers" gnus-article-toggle-headers t]
1689               ["Signature" gnus-article-hide-signature t]
1690               ["Citation" gnus-article-hide-citation t]
1691               ["List identifiers" gnus-article-hide-list-identifiers t]
1692               ["PGP" gnus-article-hide-pgp t]
1693               ["Banner" gnus-article-strip-banner t]
1694               ["Boring headers" gnus-article-hide-boring-headers t])
1695              ("Highlight"
1696               ["All" gnus-article-highlight t]
1697               ["Headers" gnus-article-highlight-headers t]
1698               ["Signature" gnus-article-highlight-signature t]
1699               ["Citation" gnus-article-highlight-citation t])
1700              ("Date"
1701               ["Local" gnus-article-date-local t]
1702               ["ISO8601" gnus-article-date-iso8601 t]
1703               ["UT" gnus-article-date-ut t]
1704               ["Original" gnus-article-date-original t]
1705               ["Lapsed" gnus-article-date-lapsed t]
1706               ["User-defined" gnus-article-date-user t])
1707              ("Washing"
1708               ("Remove Blanks"
1709                ["Leading" gnus-article-strip-leading-blank-lines t]
1710                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1711                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1712                ["All of the above" gnus-article-strip-blank-lines t]
1713                ["All" gnus-article-strip-all-blank-lines t]
1714                ["Leading space" gnus-article-strip-leading-space t]
1715                ["Trailing space" gnus-article-strip-trailing-space t])
1716               ["Overstrike" gnus-article-treat-overstrike t]
1717               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1718               ["Emphasis" gnus-article-emphasize t]
1719               ["Word wrap" gnus-article-fill-cited-article t]
1720               ["Fill long lines" gnus-article-fill-long-lines t]
1721               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1722               ["CR" gnus-article-remove-cr t]
1723               ["Show X-Face" gnus-article-display-x-face t]
1724               ["Rot 13" gnus-summary-caesar-message t]
1725               ["Unix pipe" gnus-summary-pipe-message t]
1726               ["Add buttons" gnus-article-add-buttons t]
1727               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1728               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1729               ["Toggle MIME" gnus-summary-toggle-mime t]
1730               ["Verbose header" gnus-summary-verbose-headers t]
1731               ["Toggle header" gnus-summary-toggle-header t]
1732               ["Toggle smileys" gnus-smiley-display t]
1733               ["HZ" gnus-article-decode-HZ t])
1734              ("Output"
1735               ["Save in default format" gnus-summary-save-article t]
1736               ["Save in file" gnus-summary-save-article-file t]
1737               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1738               ["Save in MH folder" gnus-summary-save-article-folder t]
1739               ["Save in VM folder" gnus-summary-save-article-vm t]
1740               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1741               ["Save body in file" gnus-summary-save-article-body-file t]
1742               ["Pipe through a filter" gnus-summary-pipe-output t]
1743               ["Add to SOUP packet" gnus-soup-add-article t]
1744               ["Print" gnus-summary-print-article t])
1745              ("Backend"
1746               ["Respool article..." gnus-summary-respool-article t]
1747               ["Move article..." gnus-summary-move-article
1748                (gnus-check-backend-function
1749                 'request-move-article gnus-newsgroup-name)]
1750               ["Copy article..." gnus-summary-copy-article t]
1751               ["Crosspost article..." gnus-summary-crosspost-article
1752                (gnus-check-backend-function
1753                 'request-replace-article gnus-newsgroup-name)]
1754               ["Import file..." gnus-summary-import-article t]
1755               ["Check if posted" gnus-summary-article-posted-p t]
1756               ["Edit article" gnus-summary-edit-article
1757                (not (gnus-group-read-only-p))]
1758               ["Delete article" gnus-summary-delete-article
1759                (gnus-check-backend-function
1760                 'request-expire-articles gnus-newsgroup-name)]
1761               ["Query respool" gnus-summary-respool-query t]
1762               ["Trace respool" gnus-summary-respool-trace t]
1763               ["Delete expirable articles" gnus-summary-expire-articles-now
1764                (gnus-check-backend-function
1765                 'request-expire-articles gnus-newsgroup-name)])
1766              ("Extract"
1767               ["Uudecode" gnus-uu-decode-uu t]
1768               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1769               ["Unshar" gnus-uu-decode-unshar t]
1770               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1771               ["Save" gnus-uu-decode-save t]
1772               ["Binhex" gnus-uu-decode-binhex t]
1773               ["Postscript" gnus-uu-decode-postscript t])
1774              ("Cache"
1775               ["Enter article" gnus-cache-enter-article t]
1776               ["Remove article" gnus-cache-remove-article t])
1777              ["Translate" gnus-article-babel t]
1778              ["Select article buffer" gnus-summary-select-article-buffer t]
1779              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1780              ["Isearch article..." gnus-summary-isearch-article t]
1781              ["Beginning of the article" gnus-summary-beginning-of-article t]
1782              ["End of the article" gnus-summary-end-of-article t]
1783              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1784              ["Fetch referenced articles" gnus-summary-refer-references t]
1785              ["Fetch current thread" gnus-summary-refer-thread t]
1786              ["Fetch article with id..." gnus-summary-refer-article t]
1787              ["Redisplay" gnus-summary-show-article t])))
1788       (easy-menu-define
1789        gnus-summary-article-menu gnus-summary-mode-map ""
1790        (cons "Article" innards))
1791
1792       (easy-menu-define
1793        gnus-article-commands-menu gnus-article-mode-map ""
1794        (cons "Commands" innards)))
1795
1796     (easy-menu-define
1797      gnus-summary-thread-menu gnus-summary-mode-map ""
1798      '("Threads"
1799        ["Toggle threading" gnus-summary-toggle-threads t]
1800        ["Hide threads" gnus-summary-hide-all-threads t]
1801        ["Show threads" gnus-summary-show-all-threads t]
1802        ["Hide thread" gnus-summary-hide-thread t]
1803        ["Show thread" gnus-summary-show-thread t]
1804        ["Go to next thread" gnus-summary-next-thread t]
1805        ["Go to previous thread" gnus-summary-prev-thread t]
1806        ["Go down thread" gnus-summary-down-thread t]
1807        ["Go up thread" gnus-summary-up-thread t]
1808        ["Top of thread" gnus-summary-top-thread t]
1809        ["Mark thread as read" gnus-summary-kill-thread t]
1810        ["Lower thread score" gnus-summary-lower-thread t]
1811        ["Raise thread score" gnus-summary-raise-thread t]
1812        ["Rethread current" gnus-summary-rethread-current t]))
1813
1814     (easy-menu-define
1815      gnus-summary-post-menu gnus-summary-mode-map ""
1816      '("Post"
1817        ["Post an article" gnus-summary-post-news t]
1818        ["Followup" gnus-summary-followup t]
1819        ["Followup and yank" gnus-summary-followup-with-original t]
1820        ["Supersede article" gnus-summary-supersede-article t]
1821        ["Cancel article" gnus-summary-cancel-article t]
1822        ["Reply" gnus-summary-reply t]
1823        ["Reply and yank" gnus-summary-reply-with-original t]
1824        ["Wide reply" gnus-summary-wide-reply t]
1825        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1826        ["Mail forward" gnus-summary-mail-forward t]
1827        ["Post forward" gnus-summary-post-forward t]
1828        ["Digest and mail" gnus-summary-mail-digest t]
1829        ["Digest and post" gnus-summary-post-digest t]
1830        ["Resend message" gnus-summary-resend-message t]
1831        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1832        ["Send a mail" gnus-summary-mail-other-window t]
1833        ["Uuencode and post" gnus-uu-post-news t]
1834        ["Followup via news" gnus-summary-followup-to-mail t]
1835        ["Followup via news and yank"
1836         gnus-summary-followup-to-mail-with-original t]
1837        ;;("Draft"
1838        ;;["Send" gnus-summary-send-draft t]
1839        ;;["Send bounced" gnus-resend-bounced-mail t])
1840        ))
1841
1842     (easy-menu-define
1843      gnus-summary-misc-menu gnus-summary-mode-map ""
1844      '("Misc"
1845        ("Mark Read"
1846         ["Mark as read" gnus-summary-mark-as-read-forward t]
1847         ["Mark same subject and select"
1848          gnus-summary-kill-same-subject-and-select t]
1849         ["Mark same subject" gnus-summary-kill-same-subject t]
1850         ["Catchup" gnus-summary-catchup t]
1851         ["Catchup all" gnus-summary-catchup-all t]
1852         ["Catchup to here" gnus-summary-catchup-to-here t]
1853         ["Catchup region" gnus-summary-mark-region-as-read t]
1854         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1855        ("Mark Various"
1856         ["Tick" gnus-summary-tick-article-forward t]
1857         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1858         ["Remove marks" gnus-summary-clear-mark-forward t]
1859         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1860         ["Set bookmark" gnus-summary-set-bookmark t]
1861         ["Remove bookmark" gnus-summary-remove-bookmark t])
1862        ("Mark Limit"
1863         ["Marks..." gnus-summary-limit-to-marks t]
1864         ["Subject..." gnus-summary-limit-to-subject t]
1865         ["Author..." gnus-summary-limit-to-author t]
1866         ["Age..." gnus-summary-limit-to-age t]
1867         ["Extra..." gnus-summary-limit-to-extra t]
1868         ["Score" gnus-summary-limit-to-score t]
1869         ["Unread" gnus-summary-limit-to-unread t]
1870         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1871         ["Articles" gnus-summary-limit-to-articles t]
1872         ["Pop limit" gnus-summary-pop-limit t]
1873         ["Show dormant" gnus-summary-limit-include-dormant t]
1874         ["Hide childless dormant"
1875          gnus-summary-limit-exclude-childless-dormant t]
1876         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1877         ["Hide marked" gnus-summary-limit-exclude-marks t]
1878         ["Show expunged" gnus-summary-show-all-expunged t])
1879        ("Process Mark"
1880         ["Set mark" gnus-summary-mark-as-processable t]
1881         ["Remove mark" gnus-summary-unmark-as-processable t]
1882         ["Remove all marks" gnus-summary-unmark-all-processable t]
1883         ["Mark above" gnus-uu-mark-over t]
1884         ["Mark series" gnus-uu-mark-series t]
1885         ["Mark region" gnus-uu-mark-region t]
1886         ["Unmark region" gnus-uu-unmark-region t]
1887         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1888         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1889         ["Mark all" gnus-uu-mark-all t]
1890         ["Mark buffer" gnus-uu-mark-buffer t]
1891         ["Mark sparse" gnus-uu-mark-sparse t]
1892         ["Mark thread" gnus-uu-mark-thread t]
1893         ["Unmark thread" gnus-uu-unmark-thread t]
1894         ("Process Mark Sets"
1895          ["Kill" gnus-summary-kill-process-mark t]
1896          ["Yank" gnus-summary-yank-process-mark
1897           gnus-newsgroup-process-stack]
1898          ["Save" gnus-summary-save-process-mark t]))
1899        ("Scroll article"
1900         ["Page forward" gnus-summary-next-page t]
1901         ["Page backward" gnus-summary-prev-page t]
1902         ["Line forward" gnus-summary-scroll-up t])
1903        ("Move"
1904         ["Next unread article" gnus-summary-next-unread-article t]
1905         ["Previous unread article" gnus-summary-prev-unread-article t]
1906         ["Next article" gnus-summary-next-article t]
1907         ["Previous article" gnus-summary-prev-article t]
1908         ["Next unread subject" gnus-summary-next-unread-subject t]
1909         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1910         ["Next article same subject" gnus-summary-next-same-subject t]
1911         ["Previous article same subject" gnus-summary-prev-same-subject t]
1912         ["First unread article" gnus-summary-first-unread-article t]
1913         ["Best unread article" gnus-summary-best-unread-article t]
1914         ["Go to subject number..." gnus-summary-goto-subject t]
1915         ["Go to article number..." gnus-summary-goto-article t]
1916         ["Go to the last article" gnus-summary-goto-last-article t]
1917         ["Pop article off history" gnus-summary-pop-article t])
1918        ("Sort"
1919         ["Sort by number" gnus-summary-sort-by-number t]
1920         ["Sort by author" gnus-summary-sort-by-author t]
1921         ["Sort by subject" gnus-summary-sort-by-subject t]
1922         ["Sort by date" gnus-summary-sort-by-date t]
1923         ["Sort by score" gnus-summary-sort-by-score t]
1924         ["Sort by lines" gnus-summary-sort-by-lines t]
1925         ["Sort by characters" gnus-summary-sort-by-chars t])
1926        ("Help"
1927         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1928         ["Describe group" gnus-summary-describe-group t]
1929         ["Read manual" gnus-info-find-node t])
1930        ("Modes"
1931         ["Pick and read" gnus-pick-mode t]
1932         ["Binary" gnus-binary-mode t])
1933        ("Regeneration"
1934         ["Regenerate" gnus-summary-prepare t]
1935         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1936         ["Toggle threading" gnus-summary-toggle-threads t])
1937        ["Filter articles..." gnus-summary-execute-command t]
1938        ["Run command on subjects..." gnus-summary-universal-argument t]
1939        ["Search articles forward..." gnus-summary-search-article-forward t]
1940        ["Search articles backward..." gnus-summary-search-article-backward t]
1941        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1942        ["Expand window" gnus-summary-expand-window t]
1943        ["Expire expirable articles" gnus-summary-expire-articles
1944         (gnus-check-backend-function
1945          'request-expire-articles gnus-newsgroup-name)]
1946        ["Edit local kill file" gnus-summary-edit-local-kill t]
1947        ["Edit main kill file" gnus-summary-edit-global-kill t]
1948        ["Edit group parameters" gnus-summary-edit-parameters t]
1949        ["Customize group parameters" gnus-summary-customize-parameters t]
1950        ["Send a bug report" gnus-bug t]
1951        ("Exit"
1952         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1953         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1954         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1955         ["Exit group" gnus-summary-exit t]
1956         ["Exit group without updating" gnus-summary-exit-no-update t]
1957         ["Exit and goto next group" gnus-summary-next-group t]
1958         ["Exit and goto prev group" gnus-summary-prev-group t]
1959         ["Reselect group" gnus-summary-reselect-current-group t]
1960         ["Rescan group" gnus-summary-rescan-group t]
1961         ["Update dribble" gnus-summary-save-newsrc t])))
1962
1963     (gnus-run-hooks 'gnus-summary-menu-hook)))
1964
1965 (defun gnus-score-set-default (var value)
1966   "A version of set that updates the GNU Emacs menu-bar."
1967   (set var value)
1968   ;; It is the message that forces the active status to be updated.
1969   (message ""))
1970
1971 (defun gnus-make-score-map (type)
1972   "Make a summary score map of type TYPE."
1973   (if t
1974       nil
1975     (let ((headers '(("author" "from" string)
1976                      ("subject" "subject" string)
1977                      ("article body" "body" string)
1978                      ("article head" "head" string)
1979                      ("xref" "xref" string)
1980                      ("extra header" "extra" string)
1981                      ("lines" "lines" number)
1982                      ("followups to author" "followup" string)))
1983           (types '((number ("less than" <)
1984                            ("greater than" >)
1985                            ("equal" =))
1986                    (string ("substring" s)
1987                            ("exact string" e)
1988                            ("fuzzy string" f)
1989                            ("regexp" r))))
1990           (perms '(("temporary" (current-time-string))
1991                    ("permanent" nil)
1992                    ("immediate" now)))
1993           header)
1994       (list
1995        (apply
1996         'nconc
1997         (list
1998          (if (eq type 'lower)
1999              "Lower score"
2000            "Increase score"))
2001         (let (outh)
2002           (while headers
2003             (setq header (car headers))
2004             (setq outh
2005                   (cons
2006                    (apply
2007                     'nconc
2008                     (list (car header))
2009                     (let ((ts (cdr (assoc (nth 2 header) types)))
2010                           outt)
2011                       (while ts
2012                         (setq outt
2013                               (cons
2014                                (apply
2015                                 'nconc
2016                                 (list (caar ts))
2017                                 (let ((ps perms)
2018                                       outp)
2019                                   (while ps
2020                                     (setq outp
2021                                           (cons
2022                                            (vector
2023                                             (caar ps)
2024                                             (list
2025                                              'gnus-summary-score-entry
2026                                              (nth 1 header)
2027                                              (if (or (string= (nth 1 header)
2028                                                               "head")
2029                                                      (string= (nth 1 header)
2030                                                               "body"))
2031                                                  ""
2032                                                (list 'gnus-summary-header
2033                                                      (nth 1 header)))
2034                                              (list 'quote (nth 1 (car ts)))
2035                                              (list 'gnus-score-delta-default
2036                                                    nil)
2037                                              (nth 1 (car ps))
2038                                              t)
2039                                             t)
2040                                            outp))
2041                                     (setq ps (cdr ps)))
2042                                   (list (nreverse outp))))
2043                                outt))
2044                         (setq ts (cdr ts)))
2045                       (list (nreverse outt))))
2046                    outh))
2047             (setq headers (cdr headers)))
2048           (list (nreverse outh))))))))
2049
2050 \f
2051
2052 (defun gnus-summary-mode (&optional group)
2053   "Major mode for reading articles.
2054
2055 All normal editing commands are switched off.
2056 \\<gnus-summary-mode-map>
2057 Each line in this buffer represents one article.  To read an
2058 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2059 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2060 respectively.
2061
2062 You can also post articles and send mail from this buffer.  To
2063 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2064 of an article, type `\\[gnus-summary-reply]'.
2065
2066 There are approx. one gazillion commands you can execute in this
2067 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2068
2069 The following commands are available:
2070
2071 \\{gnus-summary-mode-map}"
2072   (interactive)
2073   (when (gnus-visual-p 'summary-menu 'menu)
2074     (gnus-summary-make-menu-bar))
2075   (kill-all-local-variables)
2076   (gnus-summary-make-local-variables)
2077   (gnus-make-thread-indent-array)
2078   (gnus-simplify-mode-line)
2079   (setq major-mode 'gnus-summary-mode)
2080   (setq mode-name "Summary")
2081   (make-local-variable 'minor-mode-alist)
2082   (use-local-map gnus-summary-mode-map)
2083   (buffer-disable-undo)
2084   (setq buffer-read-only t)             ;Disable modification
2085   (setq truncate-lines t)
2086   (setq selective-display t)
2087   (setq selective-display-ellipses t)   ;Display `...'
2088   (gnus-summary-set-display-table)
2089   (gnus-set-default-directory)
2090   (setq gnus-newsgroup-name group)
2091   (unless (gnus-news-group-p group)
2092     (setq gnus-newsgroup-incorporated
2093           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2094   (make-local-variable 'gnus-summary-line-format)
2095   (make-local-variable 'gnus-summary-line-format-spec)
2096   (make-local-variable 'gnus-summary-dummy-line-format)
2097   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2098   (make-local-variable 'gnus-summary-mark-positions)
2099   (make-local-hook 'pre-command-hook)
2100   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2101   (gnus-run-hooks 'gnus-summary-mode-hook)
2102   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2103   (gnus-update-summary-mark-positions))
2104
2105 (defun gnus-summary-make-local-variables ()
2106   "Make all the local summary buffer variables."
2107   (let (global)
2108     (dolist (local gnus-summary-local-variables)
2109       (if (consp local)
2110           (progn
2111             (if (eq (cdr local) 'global)
2112                 ;; Copy the global value of the variable.
2113                 (setq global (symbol-value (car local)))
2114               ;; Use the value from the list.
2115               (setq global (eval (cdr local))))
2116             (set (make-local-variable (car local)) global))
2117         ;; Simple nil-valued local variable.
2118         (set (make-local-variable local) nil)))))
2119
2120 (defun gnus-summary-clear-local-variables ()
2121   (let ((locals gnus-summary-local-variables))
2122     (while locals
2123       (if (consp (car locals))
2124           (and (vectorp (caar locals))
2125                (set (caar locals) nil))
2126         (and (vectorp (car locals))
2127              (set (car locals) nil)))
2128       (setq locals (cdr locals)))))
2129
2130 ;; Summary data functions.
2131
2132 (defmacro gnus-data-number (data)
2133   `(car ,data))
2134
2135 (defmacro gnus-data-set-number (data number)
2136   `(setcar ,data ,number))
2137
2138 (defmacro gnus-data-mark (data)
2139   `(nth 1 ,data))
2140
2141 (defmacro gnus-data-set-mark (data mark)
2142   `(setcar (nthcdr 1 ,data) ,mark))
2143
2144 (defmacro gnus-data-pos (data)
2145   `(nth 2 ,data))
2146
2147 (defmacro gnus-data-set-pos (data pos)
2148   `(setcar (nthcdr 2 ,data) ,pos))
2149
2150 (defmacro gnus-data-header (data)
2151   `(nth 3 ,data))
2152
2153 (defmacro gnus-data-set-header (data header)
2154   `(setcar (nthcdr 3 ,data) ,header))
2155
2156 (defmacro gnus-data-level (data)
2157   `(nth 4 ,data))
2158
2159 (defmacro gnus-data-unread-p (data)
2160   `(= (nth 1 ,data) gnus-unread-mark))
2161
2162 (defmacro gnus-data-read-p (data)
2163   `(/= (nth 1 ,data) gnus-unread-mark))
2164
2165 (defmacro gnus-data-pseudo-p (data)
2166   `(consp (nth 3 ,data)))
2167
2168 (defmacro gnus-data-find (number)
2169   `(assq ,number gnus-newsgroup-data))
2170
2171 (defmacro gnus-data-find-list (number &optional data)
2172   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2173      (memq (assq ,number bdata)
2174            bdata)))
2175
2176 (defmacro gnus-data-make (number mark pos header level)
2177   `(list ,number ,mark ,pos ,header ,level))
2178
2179 (defun gnus-data-enter (after-article number mark pos header level offset)
2180   (let ((data (gnus-data-find-list after-article)))
2181     (unless data
2182       (error "No such article: %d" after-article))
2183     (setcdr data (cons (gnus-data-make number mark pos header level)
2184                        (cdr data)))
2185     (setq gnus-newsgroup-data-reverse nil)
2186     (gnus-data-update-list (cddr data) offset)))
2187
2188 (defun gnus-data-enter-list (after-article list &optional offset)
2189   (when list
2190     (let ((data (and after-article (gnus-data-find-list after-article)))
2191           (ilist list))
2192       (if (not (or data
2193                    after-article))
2194           (let ((odata gnus-newsgroup-data))
2195             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2196             (when offset
2197               (gnus-data-update-list odata offset)))
2198         ;; Find the last element in the list to be spliced into the main
2199         ;; list.
2200         (while (cdr list)
2201           (setq list (cdr list)))
2202         (if (not data)
2203             (progn
2204               (setcdr list gnus-newsgroup-data)
2205               (setq gnus-newsgroup-data ilist)
2206               (when offset
2207                 (gnus-data-update-list (cdr list) offset)))
2208           (setcdr list (cdr data))
2209           (setcdr data ilist)
2210           (when offset
2211             (gnus-data-update-list (cdr list) offset))))
2212       (setq gnus-newsgroup-data-reverse nil))))
2213
2214 (defun gnus-data-remove (article &optional offset)
2215   (let ((data gnus-newsgroup-data))
2216     (if (= (gnus-data-number (car data)) article)
2217         (progn
2218           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2219                 gnus-newsgroup-data-reverse nil)
2220           (when offset
2221             (gnus-data-update-list gnus-newsgroup-data offset)))
2222       (while (cdr data)
2223         (when (= (gnus-data-number (cadr data)) article)
2224           (setcdr data (cddr data))
2225           (when offset
2226             (gnus-data-update-list (cdr data) offset))
2227           (setq data nil
2228                 gnus-newsgroup-data-reverse nil))
2229         (setq data (cdr data))))))
2230
2231 (defmacro gnus-data-list (backward)
2232   `(if ,backward
2233        (or gnus-newsgroup-data-reverse
2234            (setq gnus-newsgroup-data-reverse
2235                  (reverse gnus-newsgroup-data)))
2236      gnus-newsgroup-data))
2237
2238 (defun gnus-data-update-list (data offset)
2239   "Add OFFSET to the POS of all data entries in DATA."
2240   (setq gnus-newsgroup-data-reverse nil)
2241   (while data
2242     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2243     (setq data (cdr data))))
2244
2245 (defun gnus-summary-article-pseudo-p (article)
2246   "Say whether this article is a pseudo article or not."
2247   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2248
2249 (defmacro gnus-summary-article-sparse-p (article)
2250   "Say whether this article is a sparse article or not."
2251   `(memq ,article gnus-newsgroup-sparse))
2252
2253 (defmacro gnus-summary-article-ancient-p (article)
2254   "Say whether this article is a sparse article or not."
2255   `(memq ,article gnus-newsgroup-ancient))
2256
2257 (defun gnus-article-parent-p (number)
2258   "Say whether this article is a parent or not."
2259   (let ((data (gnus-data-find-list number)))
2260     (and (cdr data)                     ; There has to be an article after...
2261          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2262             (gnus-data-level (nth 1 data))))))
2263
2264 (defun gnus-article-children (number)
2265   "Return a list of all children to NUMBER."
2266   (let* ((data (gnus-data-find-list number))
2267          (level (gnus-data-level (car data)))
2268          children)
2269     (setq data (cdr data))
2270     (while (and data
2271                 (= (gnus-data-level (car data)) (1+ level)))
2272       (push (gnus-data-number (car data)) children)
2273       (setq data (cdr data)))
2274     children))
2275
2276 (defmacro gnus-summary-skip-intangible ()
2277   "If the current article is intangible, then jump to a different article."
2278   '(let ((to (get-text-property (point) 'gnus-intangible)))
2279      (and to (gnus-summary-goto-subject to))))
2280
2281 (defmacro gnus-summary-article-intangible-p ()
2282   "Say whether this article is intangible or not."
2283   '(get-text-property (point) 'gnus-intangible))
2284
2285 (defun gnus-article-read-p (article)
2286   "Say whether ARTICLE is read or not."
2287   (not (or (memq article gnus-newsgroup-marked)
2288            (memq article gnus-newsgroup-unreads)
2289            (memq article gnus-newsgroup-unselected)
2290            (memq article gnus-newsgroup-dormant))))
2291
2292 ;; Some summary mode macros.
2293
2294 (defmacro gnus-summary-article-number ()
2295   "The article number of the article on the current line.
2296 If there isn's an article number here, then we return the current
2297 article number."
2298   '(progn
2299      (gnus-summary-skip-intangible)
2300      (or (get-text-property (point) 'gnus-number)
2301          (gnus-summary-last-subject))))
2302
2303 (defmacro gnus-summary-article-header (&optional number)
2304   "Return the header of article NUMBER."
2305   `(gnus-data-header (gnus-data-find
2306                       ,(or number '(gnus-summary-article-number)))))
2307
2308 (defmacro gnus-summary-thread-level (&optional number)
2309   "Return the level of thread that starts with article NUMBER."
2310   `(if (and (eq gnus-summary-make-false-root 'dummy)
2311             (get-text-property (point) 'gnus-intangible))
2312        0
2313      (gnus-data-level (gnus-data-find
2314                        ,(or number '(gnus-summary-article-number))))))
2315
2316 (defmacro gnus-summary-article-mark (&optional number)
2317   "Return the mark of article NUMBER."
2318   `(gnus-data-mark (gnus-data-find
2319                     ,(or number '(gnus-summary-article-number)))))
2320
2321 (defmacro gnus-summary-article-pos (&optional number)
2322   "Return the position of the line of article NUMBER."
2323   `(gnus-data-pos (gnus-data-find
2324                    ,(or number '(gnus-summary-article-number)))))
2325
2326 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2327 (defmacro gnus-summary-article-subject (&optional number)
2328   "Return current subject string or nil if nothing."
2329   `(let ((headers
2330           ,(if number
2331                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2332              '(gnus-data-header (assq (gnus-summary-article-number)
2333                                       gnus-newsgroup-data)))))
2334      (and headers
2335           (vectorp headers)
2336           (mail-header-subject headers))))
2337
2338 (defmacro gnus-summary-article-score (&optional number)
2339   "Return current article score."
2340   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2341                   gnus-newsgroup-scored))
2342        gnus-summary-default-score 0))
2343
2344 (defun gnus-summary-article-children (&optional number)
2345   "Return a list of article numbers that are children of article NUMBER."
2346   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2347          (level (gnus-data-level (car data)))
2348          l children)
2349     (while (and (setq data (cdr data))
2350                 (> (setq l (gnus-data-level (car data))) level))
2351       (and (= (1+ level) l)
2352            (push (gnus-data-number (car data))
2353                  children)))
2354     (nreverse children)))
2355
2356 (defun gnus-summary-article-parent (&optional number)
2357   "Return the article number of the parent of article NUMBER."
2358   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2359                                     (gnus-data-list t)))
2360          (level (gnus-data-level (car data))))
2361     (if (zerop level)
2362         ()                              ; This is a root.
2363       ;; We search until we find an article with a level less than
2364       ;; this one.  That function has to be the parent.
2365       (while (and (setq data (cdr data))
2366                   (not (< (gnus-data-level (car data)) level))))
2367       (and data (gnus-data-number (car data))))))
2368
2369 (defun gnus-unread-mark-p (mark)
2370   "Say whether MARK is the unread mark."
2371   (= mark gnus-unread-mark))
2372
2373 (defun gnus-read-mark-p (mark)
2374   "Say whether MARK is one of the marks that mark as read.
2375 This is all marks except unread, ticked, dormant, and expirable."
2376   (not (or (= mark gnus-unread-mark)
2377            (= mark gnus-ticked-mark)
2378            (= mark gnus-dormant-mark)
2379            (= mark gnus-expirable-mark))))
2380
2381 (defmacro gnus-article-mark (number)
2382   "Return the MARK of article NUMBER.
2383 This macro should only be used when computing the mark the \"first\"
2384 time; i.e., when generating the summary lines.  After that,
2385 `gnus-summary-article-mark' should be used to examine the
2386 marks of articles."
2387   `(cond
2388     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2389     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2390     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2391     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2392     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2393     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2394     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2395     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2396            gnus-ancient-mark))))
2397
2398 ;; Saving hidden threads.
2399
2400 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2401 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2402
2403 (defmacro gnus-save-hidden-threads (&rest forms)
2404   "Save hidden threads, eval FORMS, and restore the hidden threads."
2405   (let ((config (make-symbol "config")))
2406     `(let ((,config (gnus-hidden-threads-configuration)))
2407        (unwind-protect
2408            (save-excursion
2409              ,@forms)
2410          (gnus-restore-hidden-threads-configuration ,config)))))
2411
2412 (defun gnus-data-compute-positions ()
2413   "Compute the positions of all articles."
2414   (setq gnus-newsgroup-data-reverse nil)
2415   (let ((data gnus-newsgroup-data))
2416     (save-excursion
2417       (gnus-save-hidden-threads
2418         (gnus-summary-show-all-threads)
2419         (goto-char (point-min))
2420         (while data
2421           (while (get-text-property (point) 'gnus-intangible)
2422             (forward-line 1))
2423           (gnus-data-set-pos (car data) (+ (point) 3))
2424           (setq data (cdr data))
2425           (forward-line 1))))))
2426
2427 (defun gnus-hidden-threads-configuration ()
2428   "Return the current hidden threads configuration."
2429   (save-excursion
2430     (let (config)
2431       (goto-char (point-min))
2432       (while (search-forward "\r" nil t)
2433         (push (1- (point)) config))
2434       config)))
2435
2436 (defun gnus-restore-hidden-threads-configuration (config)
2437   "Restore hidden threads configuration from CONFIG."
2438   (let (point buffer-read-only)
2439     (while (setq point (pop config))
2440       (when (and (< point (point-max))
2441                  (goto-char point)
2442                  (eq (char-after) ?\n))
2443         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2444
2445 ;; Various summary mode internalish functions.
2446
2447 (defun gnus-mouse-pick-article (e)
2448   (interactive "e")
2449   (mouse-set-point e)
2450   (gnus-summary-next-page nil t))
2451
2452 (defun gnus-summary-set-display-table ()
2453   ;; Change the display table.  Odd characters have a tendency to mess
2454   ;; up nicely formatted displays - we make all possible glyphs
2455   ;; display only a single character.
2456
2457   ;; We start from the standard display table, if any.
2458   (let ((table (or (copy-sequence standard-display-table)
2459                    (make-display-table)))
2460         (i 32))
2461     ;; Nix out all the control chars...
2462     (while (>= (setq i (1- i)) 0)
2463       (aset table i [??]))
2464     ;; ... but not newline and cr, of course.  (cr is necessary for the
2465     ;; selective display).
2466     (aset table ?\n nil)
2467     (aset table ?\r nil)
2468     ;; We keep TAB as well.
2469     (aset table ?\t nil)
2470     ;; We nix out any glyphs over 126 that are not set already.
2471     (let ((i 256))
2472       (while (>= (setq i (1- i)) 127)
2473         ;; Only modify if the entry is nil.
2474         (unless (aref table i)
2475           (aset table i [??]))))
2476     (setq buffer-display-table table)))
2477
2478 (defun gnus-summary-setup-buffer (group)
2479   "Initialize summary buffer."
2480   (let ((buffer (concat "*Summary " group "*")))
2481     (if (get-buffer buffer)
2482         (progn
2483           (set-buffer buffer)
2484           (setq gnus-summary-buffer (current-buffer))
2485           (not gnus-newsgroup-prepared))
2486       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2487       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2488       (gnus-summary-mode group)
2489       (when gnus-carpal
2490         (gnus-carpal-setup-buffer 'summary))
2491       (unless gnus-single-article-buffer
2492         (make-local-variable 'gnus-article-buffer)
2493         (make-local-variable 'gnus-article-current)
2494         (make-local-variable 'gnus-original-article-buffer))
2495       (setq gnus-newsgroup-name group)
2496       t)))
2497
2498 (defun gnus-set-global-variables ()
2499   ;; Set the global equivalents of the summary buffer-local variables
2500   ;; to the latest values they had.  These reflect the summary buffer
2501   ;; that was in action when the last article was fetched.
2502   (when (eq major-mode 'gnus-summary-mode)
2503     (setq gnus-summary-buffer (current-buffer))
2504     (let ((name gnus-newsgroup-name)
2505           (marked gnus-newsgroup-marked)
2506           (unread gnus-newsgroup-unreads)
2507           (headers gnus-current-headers)
2508           (data gnus-newsgroup-data)
2509           (summary gnus-summary-buffer)
2510           (article-buffer gnus-article-buffer)
2511           (original gnus-original-article-buffer)
2512           (gac gnus-article-current)
2513           (reffed gnus-reffed-article-number)
2514           (score-file gnus-current-score-file)
2515           (default-charset gnus-newsgroup-charset))
2516       (save-excursion
2517         (set-buffer gnus-group-buffer)
2518         (setq gnus-newsgroup-name name
2519               gnus-newsgroup-marked marked
2520               gnus-newsgroup-unreads unread
2521               gnus-current-headers headers
2522               gnus-newsgroup-data data
2523               gnus-article-current gac
2524               gnus-summary-buffer summary
2525               gnus-article-buffer article-buffer
2526               gnus-original-article-buffer original
2527               gnus-reffed-article-number reffed
2528               gnus-current-score-file score-file
2529               gnus-newsgroup-charset default-charset)
2530         ;; The article buffer also has local variables.
2531         (when (gnus-buffer-live-p gnus-article-buffer)
2532           (set-buffer gnus-article-buffer)
2533           (setq gnus-summary-buffer summary))))))
2534
2535 (defun gnus-summary-article-unread-p (article)
2536   "Say whether ARTICLE is unread or not."
2537   (memq article gnus-newsgroup-unreads))
2538
2539 (defun gnus-summary-first-article-p (&optional article)
2540   "Return whether ARTICLE is the first article in the buffer."
2541   (if (not (setq article (or article (gnus-summary-article-number))))
2542       nil
2543     (eq article (caar gnus-newsgroup-data))))
2544
2545 (defun gnus-summary-last-article-p (&optional article)
2546   "Return whether ARTICLE is the last article in the buffer."
2547   (if (not (setq article (or article (gnus-summary-article-number))))
2548       ;; All non-existent numbers are the last article.  :-)
2549       t
2550     (not (cdr (gnus-data-find-list article)))))
2551
2552 (defun gnus-make-thread-indent-array ()
2553   (let ((n 200))
2554     (unless (and gnus-thread-indent-array
2555                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2556       (setq gnus-thread-indent-array (make-vector 201 "")
2557             gnus-thread-indent-array-level gnus-thread-indent-level)
2558       (while (>= n 0)
2559         (aset gnus-thread-indent-array n
2560               (make-string (* n gnus-thread-indent-level) ? ))
2561         (setq n (1- n))))))
2562
2563 (defun gnus-update-summary-mark-positions ()
2564   "Compute where the summary marks are to go."
2565   (save-excursion
2566     (when (gnus-buffer-exists-p gnus-summary-buffer)
2567       (set-buffer gnus-summary-buffer))
2568     (let ((gnus-replied-mark 129)
2569           (gnus-score-below-mark 130)
2570           (gnus-score-over-mark 130)
2571           (gnus-download-mark 131)
2572           (spec gnus-summary-line-format-spec)
2573           gnus-visual pos)
2574       (save-excursion
2575         (gnus-set-work-buffer)
2576         (let ((gnus-summary-line-format-spec spec)
2577               (gnus-newsgroup-downloadable '((0 . t))))
2578           (gnus-summary-insert-line
2579            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2580            0 nil 128 t nil "" nil 1)
2581           (goto-char (point-min))
2582           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2583                                              (- (point) 2)))))
2584           (goto-char (point-min))
2585           (push (cons 'replied (and (search-forward "\201" nil t)
2586                                     (- (point) 2)))
2587                 pos)
2588           (goto-char (point-min))
2589           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2590                 pos)
2591           (goto-char (point-min))
2592           (push (cons 'download
2593                       (and (search-forward "\203" nil t) (- (point) 2)))
2594                 pos)))
2595       (setq gnus-summary-mark-positions pos))))
2596
2597 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2598   "Insert a dummy root in the summary buffer."
2599   (beginning-of-line)
2600   (gnus-add-text-properties
2601    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2602    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2603
2604 (defun gnus-summary-from-or-to-or-newsgroups (header)
2605   (let ((to (cdr (assq 'To (mail-header-extra header))))
2606         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2607         (default-mime-charset (with-current-buffer gnus-summary-buffer
2608                                 default-mime-charset)))
2609     (cond
2610      ((and to
2611            gnus-ignored-from-addresses
2612            (string-match gnus-ignored-from-addresses
2613                          (mail-header-from header)))
2614       (concat "-> "
2615               (or (car (funcall gnus-extract-address-components
2616                                 (funcall
2617                                  gnus-decode-encoded-word-function to)))
2618                   (funcall gnus-decode-encoded-word-function to))))
2619      ((and newsgroups
2620            gnus-ignored-from-addresses
2621            (string-match gnus-ignored-from-addresses
2622                          (mail-header-from header)))
2623       (concat "=> " newsgroups))
2624      (t
2625       (or (car (funcall gnus-extract-address-components
2626                         (mail-header-from header)))
2627           (mail-header-from header))))))
2628
2629 (defun gnus-summary-insert-line (gnus-tmp-header
2630                                  gnus-tmp-level gnus-tmp-current
2631                                  gnus-tmp-unread gnus-tmp-replied
2632                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2633                                  &optional gnus-tmp-dummy gnus-tmp-score
2634                                  gnus-tmp-process)
2635   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2636          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2637          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2638          (gnus-tmp-score-char
2639           (if (or (null gnus-summary-default-score)
2640                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2641                       gnus-summary-zcore-fuzz))
2642               ?  ;Whitespace
2643             (if (< gnus-tmp-score gnus-summary-default-score)
2644                 gnus-score-below-mark gnus-score-over-mark)))
2645          (gnus-tmp-replied
2646           (cond (gnus-tmp-process gnus-process-mark)
2647                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2648                  gnus-cached-mark)
2649                 (gnus-tmp-replied gnus-replied-mark)
2650                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2651                  gnus-saved-mark)
2652                 (t gnus-unread-mark)))
2653          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2654          (gnus-tmp-name
2655           (cond
2656            ((string-match "<[^>]+> *$" gnus-tmp-from)
2657             (let ((beg (match-beginning 0)))
2658               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2659                        (substring gnus-tmp-from (1+ (match-beginning 0))
2660                                   (1- (match-end 0))))
2661                   (substring gnus-tmp-from 0 beg))))
2662            ((string-match "(.+)" gnus-tmp-from)
2663             (substring gnus-tmp-from
2664                        (1+ (match-beginning 0)) (1- (match-end 0))))
2665            (t gnus-tmp-from)))
2666          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2667          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2668          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2669          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2670          (buffer-read-only nil))
2671     (when (string= gnus-tmp-name "")
2672       (setq gnus-tmp-name gnus-tmp-from))
2673     (unless (numberp gnus-tmp-lines)
2674       (setq gnus-tmp-lines 0))
2675     (gnus-put-text-property-excluding-characters-with-faces
2676      (point)
2677      (progn (eval gnus-summary-line-format-spec) (point))
2678      'gnus-number gnus-tmp-number)
2679     (when (gnus-visual-p 'summary-highlight 'highlight)
2680       (forward-line -1)
2681       (gnus-run-hooks 'gnus-summary-update-hook)
2682       (forward-line 1))))
2683
2684 (defun gnus-summary-update-line (&optional dont-update)
2685   ;; Update summary line after change.
2686   (when (and gnus-summary-default-score
2687              (not gnus-summary-inhibit-highlight))
2688     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2689            (article (gnus-summary-article-number))
2690            (score (gnus-summary-article-score article)))
2691       (unless dont-update
2692         (if (and gnus-summary-mark-below
2693                  (< (gnus-summary-article-score)
2694                     gnus-summary-mark-below))
2695             ;; This article has a low score, so we mark it as read.
2696             (when (memq article gnus-newsgroup-unreads)
2697               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2698           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2699             ;; This article was previously marked as read on account
2700             ;; of a low score, but now it has risen, so we mark it as
2701             ;; unread.
2702             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2703         (gnus-summary-update-mark
2704          (if (or (null gnus-summary-default-score)
2705                  (<= (abs (- score gnus-summary-default-score))
2706                      gnus-summary-zcore-fuzz))
2707              ?  ;Whitespace
2708            (if (< score gnus-summary-default-score)
2709                gnus-score-below-mark gnus-score-over-mark))
2710          'score))
2711       ;; Do visual highlighting.
2712       (when (gnus-visual-p 'summary-highlight 'highlight)
2713         (gnus-run-hooks 'gnus-summary-update-hook)))))
2714
2715 (defvar gnus-tmp-new-adopts nil)
2716
2717 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2718   "Return the number of articles in THREAD.
2719 This may be 0 in some cases -- if none of the articles in
2720 the thread are to be displayed."
2721   (let* ((number
2722           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2723           (cond
2724            ((not (listp thread))
2725             1)
2726            ((and (consp thread) (cdr thread))
2727             (apply
2728              '+ 1 (mapcar
2729                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2730            ((null thread)
2731             1)
2732            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2733             1)
2734            (t 0))))
2735     (when (and level (zerop level) gnus-tmp-new-adopts)
2736       (incf number
2737             (apply '+ (mapcar
2738                        'gnus-summary-number-of-articles-in-thread
2739                        gnus-tmp-new-adopts))))
2740     (if char
2741         (if (> number 1) gnus-not-empty-thread-mark
2742           gnus-empty-thread-mark)
2743       number)))
2744
2745 (defun gnus-summary-set-local-parameters (group)
2746   "Go through the local params of GROUP and set all variable specs in that list."
2747   (let ((params (gnus-group-find-parameter group))
2748         elem)
2749     (while params
2750       (setq elem (car params)
2751             params (cdr params))
2752       (and (consp elem)                 ; Has to be a cons.
2753            (consp (cdr elem))           ; The cdr has to be a list.
2754            (symbolp (car elem))         ; Has to be a symbol in there.
2755            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2756            (ignore-errors               ; So we set it.
2757              (make-local-variable (car elem))
2758              (set (car elem) (eval (nth 1 elem))))))))
2759
2760 (defun gnus-summary-read-group (group &optional show-all no-article
2761                                       kill-buffer no-display backward
2762                                       select-articles)
2763   "Start reading news in newsgroup GROUP.
2764 If SHOW-ALL is non-nil, already read articles are also listed.
2765 If NO-ARTICLE is non-nil, no article is selected initially.
2766 If NO-DISPLAY, don't generate a summary buffer."
2767   (let (result)
2768     (while (and group
2769                 (null (setq result
2770                             (let ((gnus-auto-select-next nil))
2771                               (or (gnus-summary-read-group-1
2772                                    group show-all no-article
2773                                    kill-buffer no-display
2774                                    select-articles)
2775                                   (setq show-all nil
2776                                         select-articles nil)))))
2777                 (eq gnus-auto-select-next 'quietly))
2778       (set-buffer gnus-group-buffer)
2779       ;; The entry function called above goes to the next
2780       ;; group automatically, so we go two groups back
2781       ;; if we are searching for the previous group.
2782       (when backward
2783         (gnus-group-prev-unread-group 2))
2784       (if (not (equal group (gnus-group-group-name)))
2785           (setq group (gnus-group-group-name))
2786         (setq group nil)))
2787     result))
2788
2789 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2790   "Directly jump to the other GROUP from summary buffer.
2791 If SHOW-ALL is non-nil, already read articles are also listed."
2792   (interactive
2793    (if (eq gnus-summary-buffer (current-buffer))
2794        (list (completing-read
2795               "Group: " gnus-active-hashtb nil t
2796               (when (and gnus-newsgroup-name
2797                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2798                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2799               'gnus-group-history)
2800              current-prefix-arg)
2801      (error "%s must be invoked from a gnus summary buffer." this-command)))
2802   (unless (or (zerop (length group))
2803               (and gnus-newsgroup-name
2804                    (string-equal gnus-newsgroup-name group)))
2805     (gnus-summary-exit)
2806     (gnus-summary-read-group group show-all
2807                              gnus-dont-select-after-jump-to-other-group)))
2808
2809 (defun gnus-summary-read-group-1 (group show-all no-article
2810                                         kill-buffer no-display
2811                                         &optional select-articles)
2812   ;; Killed foreign groups can't be entered.
2813   (when (and (not (gnus-group-native-p group))
2814              (not (gnus-gethash group gnus-newsrc-hashtb)))
2815     (error "Dead non-native groups can't be entered"))
2816   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2817   (let* ((new-group (gnus-summary-setup-buffer group))
2818          (quit-config (gnus-group-quit-config group))
2819          (did-select (and new-group (gnus-select-newsgroup
2820                                      group show-all select-articles))))
2821     (cond
2822      ;; This summary buffer exists already, so we just select it.
2823      ((not new-group)
2824       (gnus-set-global-variables)
2825       (when kill-buffer
2826         (gnus-kill-or-deaden-summary kill-buffer))
2827       (gnus-configure-windows 'summary 'force)
2828       (gnus-set-mode-line 'summary)
2829       (gnus-summary-position-point)
2830       (message "")
2831       t)
2832      ;; We couldn't select this group.
2833      ((null did-select)
2834       (when (and (eq major-mode 'gnus-summary-mode)
2835                  (not (equal (current-buffer) kill-buffer)))
2836         (kill-buffer (current-buffer))
2837         (if (not quit-config)
2838             (progn
2839               ;; Update the info -- marks might need to be removed,
2840               ;; for instance.
2841               (gnus-summary-update-info)
2842               (set-buffer gnus-group-buffer)
2843               (gnus-group-jump-to-group group)
2844               (gnus-group-next-unread-group 1))
2845           (gnus-handle-ephemeral-exit quit-config)))
2846       (gnus-message 3 "Can't select group")
2847       nil)
2848      ;; The user did a `C-g' while prompting for number of articles,
2849      ;; so we exit this group.
2850      ((eq did-select 'quit)
2851       (and (eq major-mode 'gnus-summary-mode)
2852            (not (equal (current-buffer) kill-buffer))
2853            (kill-buffer (current-buffer)))
2854       (when kill-buffer
2855         (gnus-kill-or-deaden-summary kill-buffer))
2856       (if (not quit-config)
2857           (progn
2858             (set-buffer gnus-group-buffer)
2859             (gnus-group-jump-to-group group)
2860             (gnus-group-next-unread-group 1)
2861             (gnus-configure-windows 'group 'force))
2862         (gnus-handle-ephemeral-exit quit-config))
2863       ;; Finally signal the quit.
2864       (signal 'quit nil))
2865      ;; The group was successfully selected.
2866      (t
2867       (gnus-set-global-variables)
2868       ;; Save the active value in effect when the group was entered.
2869       (setq gnus-newsgroup-active
2870             (gnus-copy-sequence
2871              (gnus-active gnus-newsgroup-name)))
2872       ;; You can change the summary buffer in some way with this hook.
2873       (gnus-run-hooks 'gnus-select-group-hook)
2874       ;; Set any local variables in the group parameters.
2875       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2876       (gnus-update-format-specifications
2877        nil 'summary 'summary-mode 'summary-dummy)
2878       (gnus-update-summary-mark-positions)
2879       ;; Do score processing.
2880       (when gnus-use-scoring
2881         (gnus-possibly-score-headers))
2882       ;; Check whether to fill in the gaps in the threads.
2883       (when gnus-build-sparse-threads
2884         (gnus-build-sparse-threads))
2885       ;; Find the initial limit.
2886       (if gnus-show-threads
2887           (if show-all
2888               (let ((gnus-newsgroup-dormant nil))
2889                 (gnus-summary-initial-limit show-all))
2890             (gnus-summary-initial-limit show-all))
2891         ;; When untreaded, all articles are always shown.
2892         (setq gnus-newsgroup-limit
2893               (mapcar
2894                (lambda (header) (mail-header-number header))
2895                gnus-newsgroup-headers)))
2896       ;; Generate the summary buffer.
2897       (unless no-display
2898         (gnus-summary-prepare))
2899       (when gnus-use-trees
2900         (gnus-tree-open group)
2901         (setq gnus-summary-highlight-line-function
2902               'gnus-tree-highlight-article))
2903       ;; If the summary buffer is empty, but there are some low-scored
2904       ;; articles or some excluded dormants, we include these in the
2905       ;; buffer.
2906       (when (and (zerop (buffer-size))
2907                  (not no-display))
2908         (cond (gnus-newsgroup-dormant
2909                (gnus-summary-limit-include-dormant))
2910               ((and gnus-newsgroup-scored show-all)
2911                (gnus-summary-limit-include-expunged t))))
2912       ;; Function `gnus-apply-kill-file' must be called in this hook.
2913       (gnus-run-hooks 'gnus-apply-kill-hook)
2914       (if (and (zerop (buffer-size))
2915                (not no-display))
2916           (progn
2917             ;; This newsgroup is empty.
2918             (gnus-summary-catchup-and-exit nil t)
2919             (gnus-message 6 "No unread news")
2920             (when kill-buffer
2921               (gnus-kill-or-deaden-summary kill-buffer))
2922             ;; Return nil from this function.
2923             nil)
2924         ;; Hide conversation thread subtrees.  We cannot do this in
2925         ;; gnus-summary-prepare-hook since kill processing may not
2926         ;; work with hidden articles.
2927         (and gnus-show-threads
2928              gnus-thread-hide-subtree
2929              (gnus-summary-hide-all-threads))
2930         (when kill-buffer
2931           (gnus-kill-or-deaden-summary kill-buffer))
2932         ;; Show first unread article if requested.
2933         (if (and (not no-article)
2934                  (not no-display)
2935                  gnus-newsgroup-unreads
2936                  gnus-auto-select-first)
2937             (progn
2938               (gnus-configure-windows 'summary)
2939               (cond
2940                ((eq gnus-auto-select-first 'best)
2941                 (gnus-summary-best-unread-article))
2942                ((eq gnus-auto-select-first t)
2943                 (gnus-summary-first-unread-article))
2944                ((gnus-functionp gnus-auto-select-first)
2945                 (funcall gnus-auto-select-first))))
2946           ;; Don't select any articles, just move point to the first
2947           ;; article in the group.
2948           (goto-char (point-min))
2949           (gnus-summary-position-point)
2950           (gnus-configure-windows 'summary 'force)
2951           (gnus-set-mode-line 'summary))
2952         (when (get-buffer-window gnus-group-buffer t)
2953           ;; Gotta use windows, because recenter does weird stuff if
2954           ;; the current buffer ain't the displayed window.
2955           (let ((owin (selected-window)))
2956             (select-window (get-buffer-window gnus-group-buffer t))
2957             (when (gnus-group-goto-group group)
2958               (recenter))
2959             (select-window owin)))
2960         ;; Mark this buffer as "prepared".
2961         (setq gnus-newsgroup-prepared t)
2962         (gnus-run-hooks 'gnus-summary-prepared-hook)
2963         t)))))
2964
2965 (defun gnus-summary-prepare ()
2966   "Generate the summary buffer."
2967   (interactive)
2968   (let ((buffer-read-only nil))
2969     (erase-buffer)
2970     (setq gnus-newsgroup-data nil
2971           gnus-newsgroup-data-reverse nil)
2972     (gnus-run-hooks 'gnus-summary-generate-hook)
2973     ;; Generate the buffer, either with threads or without.
2974     (when gnus-newsgroup-headers
2975       (gnus-summary-prepare-threads
2976        (if gnus-show-threads
2977            (gnus-sort-gathered-threads
2978             (funcall gnus-summary-thread-gathering-function
2979                      (gnus-sort-threads
2980                       (gnus-cut-threads (gnus-make-threads)))))
2981          ;; Unthreaded display.
2982          (gnus-sort-articles gnus-newsgroup-headers))))
2983     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2984     ;; Call hooks for modifying summary buffer.
2985     (goto-char (point-min))
2986     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2987
2988 (defsubst gnus-general-simplify-subject (subject)
2989   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2990   (setq subject
2991         (cond
2992          ;; Truncate the subject.
2993          (gnus-simplify-subject-functions
2994           (gnus-map-function gnus-simplify-subject-functions subject))
2995          ((numberp gnus-summary-gather-subject-limit)
2996           (setq subject (gnus-simplify-subject-re subject))
2997           (if (> (length subject) gnus-summary-gather-subject-limit)
2998               (substring subject 0 gnus-summary-gather-subject-limit)
2999             subject))
3000          ;; Fuzzily simplify it.
3001          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3002           (gnus-simplify-subject-fuzzy subject))
3003          ;; Just remove the leading "Re:".
3004          (t
3005           (gnus-simplify-subject-re subject))))
3006
3007   (if (and gnus-summary-gather-exclude-subject
3008            (string-match gnus-summary-gather-exclude-subject subject))
3009       nil                               ; This article shouldn't be gathered
3010     subject))
3011
3012 (defun gnus-summary-simplify-subject-query ()
3013   "Query where the respool algorithm would put this article."
3014   (interactive)
3015   (gnus-summary-select-article)
3016   (message "%s"
3017            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3018
3019 (defun gnus-gather-threads-by-subject (threads)
3020   "Gather threads by looking at Subject headers."
3021   (if (not gnus-summary-make-false-root)
3022       threads
3023     (let ((hashtb (gnus-make-hashtable 1024))
3024           (prev threads)
3025           (result threads)
3026           subject hthread whole-subject)
3027       (while threads
3028         (setq subject (gnus-general-simplify-subject
3029                        (setq whole-subject (mail-header-subject
3030                                             (caar threads)))))
3031         (when subject
3032           (if (setq hthread (gnus-gethash subject hashtb))
3033               (progn
3034                 ;; We enter a dummy root into the thread, if we
3035                 ;; haven't done that already.
3036                 (unless (stringp (caar hthread))
3037                   (setcar hthread (list whole-subject (car hthread))))
3038                 ;; We add this new gathered thread to this gathered
3039                 ;; thread.
3040                 (setcdr (car hthread)
3041                         (nconc (cdar hthread) (list (car threads))))
3042                 ;; Remove it from the list of threads.
3043                 (setcdr prev (cdr threads))
3044                 (setq threads prev))
3045             ;; Enter this thread into the hash table.
3046             (gnus-sethash subject threads hashtb)))
3047         (setq prev threads)
3048         (setq threads (cdr threads)))
3049       result)))
3050
3051 (defun gnus-gather-threads-by-references (threads)
3052   "Gather threads by looking at References headers."
3053   (let ((idhashtb (gnus-make-hashtable 1024))
3054         (thhashtb (gnus-make-hashtable 1024))
3055         (prev threads)
3056         (result threads)
3057         ids references id gthread gid entered ref)
3058     (while threads
3059       (when (setq references (mail-header-references (caar threads)))
3060         (setq id (mail-header-id (caar threads))
3061               ids (gnus-split-references references)
3062               entered nil)
3063         (while (setq ref (pop ids))
3064           (setq ids (delete ref ids))
3065           (if (not (setq gid (gnus-gethash ref idhashtb)))
3066               (progn
3067                 (gnus-sethash ref id idhashtb)
3068                 (gnus-sethash id threads thhashtb))
3069             (setq gthread (gnus-gethash gid thhashtb))
3070             (unless entered
3071               ;; We enter a dummy root into the thread, if we
3072               ;; haven't done that already.
3073               (unless (stringp (caar gthread))
3074                 (setcar gthread (list (mail-header-subject (caar gthread))
3075                                       (car gthread))))
3076               ;; We add this new gathered thread to this gathered
3077               ;; thread.
3078               (setcdr (car gthread)
3079                       (nconc (cdar gthread) (list (car threads)))))
3080             ;; Add it into the thread hash table.
3081             (gnus-sethash id gthread thhashtb)
3082             (setq entered t)
3083             ;; Remove it from the list of threads.
3084             (setcdr prev (cdr threads))
3085             (setq threads prev))))
3086       (setq prev threads)
3087       (setq threads (cdr threads)))
3088     result))
3089
3090 (defun gnus-sort-gathered-threads (threads)
3091   "Sort subtreads inside each gathered thread by article number."
3092   (let ((result threads))
3093     (while threads
3094       (when (stringp (caar threads))
3095         (setcdr (car threads)
3096                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3097       (setq threads (cdr threads)))
3098     result))
3099
3100 (defun gnus-thread-loop-p (root thread)
3101   "Say whether ROOT is in THREAD."
3102   (let ((stack (list thread))
3103         (infloop 0)
3104         th)
3105     (while (setq thread (pop stack))
3106       (setq th (cdr thread))
3107       (while (and th
3108                   (not (eq (caar th) root)))
3109         (pop th))
3110       (if th
3111           ;; We have found a loop.
3112           (let (ref-dep)
3113             (setcdr thread (delq (car th) (cdr thread)))
3114             (if (boundp (setq ref-dep (intern "none"
3115                                               gnus-newsgroup-dependencies)))
3116                 (setcdr (symbol-value ref-dep)
3117                         (nconc (cdr (symbol-value ref-dep))
3118                                (list (car th))))
3119               (set ref-dep (list nil (car th))))
3120             (setq infloop 1
3121                   stack nil))
3122         ;; Push all the subthreads onto the stack.
3123         (push (cdr thread) stack)))
3124     infloop))
3125
3126 (defun gnus-make-threads ()
3127   "Go through the dependency hashtb and find the roots.  Return all threads."
3128   (let (threads)
3129     (while (catch 'infloop
3130              (mapatoms
3131               (lambda (refs)
3132                 ;; Deal with self-referencing References loops.
3133                 (when (and (car (symbol-value refs))
3134                            (not (zerop
3135                                  (apply
3136                                   '+
3137                                   (mapcar
3138                                    (lambda (thread)
3139                                      (gnus-thread-loop-p
3140                                       (car (symbol-value refs)) thread))
3141                                    (cdr (symbol-value refs)))))))
3142                   (setq threads nil)
3143                   (throw 'infloop t))
3144                 (unless (car (symbol-value refs))
3145                   ;; These threads do not refer back to any other articles,
3146                   ;; so they're roots.
3147                   (setq threads (append (cdr (symbol-value refs)) threads))))
3148               gnus-newsgroup-dependencies)))
3149     threads))
3150
3151 ;; Build the thread tree.
3152 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3153   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3154
3155 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3156 if it was already present.
3157
3158 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3159 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3160 Message-IDs will be renamed be renamed to a unique Message-ID before
3161 being entered.
3162
3163 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3164   (let* ((id (mail-header-id header))
3165          (id-dep (and id (intern id dependencies)))
3166          ref ref-dep ref-header)
3167     ;; Enter this `header' in the `dependencies' table.
3168     (cond
3169      ((not id-dep)
3170       (setq header nil))
3171      ;; The first two cases do the normal part: enter a new `header'
3172      ;; in the `dependencies' table.
3173      ((not (boundp id-dep))
3174       (set id-dep (list header)))
3175      ((null (car (symbol-value id-dep)))
3176       (setcar (symbol-value id-dep) header))
3177
3178      ;; From here the `header' was already present in the
3179      ;; `dependencies' table.
3180      (force-new
3181       ;; Overrides an existing entry;
3182       ;; just set the header part of the entry.
3183       (setcar (symbol-value id-dep) header))
3184
3185      ;; Renames the existing `header' to a unique Message-ID.
3186      ((not gnus-summary-ignore-duplicates)
3187       ;; An article with this Message-ID has already been seen.
3188       ;; We rename the Message-ID.
3189       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3190            (list header))
3191       (mail-header-set-id header id))
3192
3193      ;; The last case ignores an existing entry, except it adds any
3194      ;; additional Xrefs (in case the two articles came from different
3195      ;; servers.
3196      ;; Also sets `header' to `nil' meaning that the `dependencies'
3197      ;; table was *not* modified.
3198      (t
3199       (mail-header-set-xref
3200        (car (symbol-value id-dep))
3201        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3202                    "")
3203                (or (mail-header-xref header) "")))
3204       (setq header nil)))
3205
3206     (when header
3207       ;; First check if that we are not creating a References loop.
3208       (setq ref (gnus-parent-id (mail-header-references header)))
3209       (while (and ref
3210                   (setq ref-dep (intern-soft ref dependencies))
3211                   (boundp ref-dep)
3212                   (setq ref-header (car (symbol-value ref-dep))))
3213         (if (string= id ref)
3214             ;; Yuk!  This is a reference loop.  Make the article be a
3215             ;; root article.
3216             (progn
3217               (mail-header-set-references (car (symbol-value id-dep)) "none")
3218               (setq ref nil))
3219           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3220       (setq ref (gnus-parent-id (mail-header-references header)))
3221       (setq ref-dep (intern (or ref "none") dependencies))
3222       (if (boundp ref-dep)
3223           (setcdr (symbol-value ref-dep)
3224                   (nconc (cdr (symbol-value ref-dep))
3225                          (list (symbol-value id-dep))))
3226         (set ref-dep (list nil (symbol-value id-dep)))))
3227     header))
3228
3229 (defun gnus-build-sparse-threads ()
3230   (let ((headers gnus-newsgroup-headers)
3231         (mail-parse-charset gnus-newsgroup-charset)
3232         (gnus-summary-ignore-duplicates t)
3233         header references generation relations
3234         subject child end new-child date)
3235     ;; First we create an alist of generations/relations, where
3236     ;; generations is how much we trust the relation, and the relation
3237     ;; is parent/child.
3238     (gnus-message 7 "Making sparse threads...")
3239     (save-excursion
3240       (nnheader-set-temp-buffer " *gnus sparse threads*")
3241       (while (setq header (pop headers))
3242         (when (and (setq references (mail-header-references header))
3243                    (not (string= references "")))
3244           (insert references)
3245           (setq child (mail-header-id header)
3246                 subject (mail-header-subject header)
3247                 date (mail-header-date header)
3248                 generation 0)
3249           (while (search-backward ">" nil t)
3250             (setq end (1+ (point)))
3251             (when (search-backward "<" nil t)
3252               (setq new-child (buffer-substring (point) end))
3253               (push (list (incf generation)
3254                           child (setq child new-child)
3255                           subject date)
3256                     relations)))
3257           (when child
3258             (push (list (1+ generation) child nil subject) relations))
3259           (erase-buffer)))
3260       (kill-buffer (current-buffer)))
3261     ;; Sort over trustworthiness.
3262     (mapcar
3263      (lambda (relation)
3264        (when (gnus-dependencies-add-header
3265               (make-full-mail-header
3266                gnus-reffed-article-number
3267                (nth 3 relation) "" (or (nth 4 relation) "")
3268                (nth 1 relation)
3269                (or (nth 2 relation) "") 0 0 "")
3270               gnus-newsgroup-dependencies nil)
3271          (push gnus-reffed-article-number gnus-newsgroup-limit)
3272          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3273          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3274                gnus-newsgroup-reads)
3275          (decf gnus-reffed-article-number)))
3276      (sort relations 'car-less-than-car))
3277     (gnus-message 7 "Making sparse threads...done")))
3278
3279 (defun gnus-build-old-threads ()
3280   ;; Look at all the articles that refer back to old articles, and
3281   ;; fetch the headers for the articles that aren't there.  This will
3282   ;; build complete threads - if the roots haven't been expired by the
3283   ;; server, that is.
3284   (let ((mail-parse-charset gnus-newsgroup-charset)
3285         id heads)
3286     (mapatoms
3287      (lambda (refs)
3288        (when (not (car (symbol-value refs)))
3289          (setq heads (cdr (symbol-value refs)))
3290          (while heads
3291            (if (memq (mail-header-number (caar heads))
3292                      gnus-newsgroup-dormant)
3293                (setq heads (cdr heads))
3294              (setq id (symbol-name refs))
3295              (while (and (setq id (gnus-build-get-header id))
3296                          (not (car (gnus-id-to-thread id)))))
3297              (setq heads nil)))))
3298      gnus-newsgroup-dependencies)))
3299
3300 ;; This function has to be called with point after the article number
3301 ;; on the beginning of the line.
3302 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3303   (let ((eol (gnus-point-at-eol))
3304         (buffer (current-buffer))
3305         header)
3306
3307     ;; overview: [num subject from date id refs chars lines misc]
3308     (unless (eobp)
3309       (forward-char))
3310
3311     (setq header
3312           (make-full-mail-header
3313            number                               ; number
3314            (nnheader-nov-field)                 ; subject
3315            (nnheader-nov-field)                 ; from
3316            (nnheader-nov-field)                 ; date
3317            (nnheader-nov-read-message-id)       ; id
3318            (nnheader-nov-field)                 ; refs
3319            (nnheader-nov-read-integer)          ; chars
3320            (nnheader-nov-read-integer)          ; lines
3321            (unless (eobp)
3322              (nnheader-nov-field))              ; misc
3323            (nnheader-nov-parse-extra)))         ; extra
3324
3325     (when gnus-alter-header-function
3326       (funcall gnus-alter-header-function header))
3327     (gnus-dependencies-add-header header dependencies force-new)))
3328
3329 (defun gnus-build-get-header (id)
3330   ;; Look through the buffer of NOV lines and find the header to
3331   ;; ID.  Enter this line into the dependencies hash table, and return
3332   ;; the id of the parent article (if any).
3333   (let ((deps gnus-newsgroup-dependencies)
3334         found header)
3335     (prog1
3336         (save-excursion
3337           (set-buffer nntp-server-buffer)
3338           (let ((case-fold-search nil))
3339             (goto-char (point-min))
3340             (while (and (not found)
3341                         (search-forward id nil t))
3342               (beginning-of-line)
3343               (setq found (looking-at
3344                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3345                                    (regexp-quote id))))
3346               (or found (beginning-of-line 2)))
3347             (when found
3348               (beginning-of-line)
3349               (and
3350                (setq header (gnus-nov-parse-line
3351                              (read (current-buffer)) deps))
3352                (gnus-parent-id (mail-header-references header))))))
3353       (when header
3354         (let ((number (mail-header-number header)))
3355           (push number gnus-newsgroup-limit)
3356           (push header gnus-newsgroup-headers)
3357           (if (memq number gnus-newsgroup-unselected)
3358               (progn
3359                 (push number gnus-newsgroup-unreads)
3360                 (setq gnus-newsgroup-unselected
3361                       (delq number gnus-newsgroup-unselected)))
3362             (push number gnus-newsgroup-ancient)))))))
3363
3364 (defun gnus-build-all-threads ()
3365   "Read all the headers."
3366   (let ((gnus-summary-ignore-duplicates t)
3367         (mail-parse-charset gnus-newsgroup-charset)
3368         (dependencies gnus-newsgroup-dependencies)
3369         header article)
3370     (save-excursion
3371       (set-buffer nntp-server-buffer)
3372       (let ((case-fold-search nil))
3373         (goto-char (point-min))
3374         (while (not (eobp))
3375           (ignore-errors
3376             (setq article (read (current-buffer))
3377                   header (gnus-nov-parse-line article dependencies)))
3378           (when header
3379             (save-excursion
3380               (set-buffer gnus-summary-buffer)
3381               (push header gnus-newsgroup-headers)
3382               (if (memq (setq article (mail-header-number header))
3383                         gnus-newsgroup-unselected)
3384                   (progn
3385                     (push article gnus-newsgroup-unreads)
3386                     (setq gnus-newsgroup-unselected
3387                           (delq article gnus-newsgroup-unselected)))
3388                 (push article gnus-newsgroup-ancient)))
3389             (forward-line 1)))))))
3390
3391 (defun gnus-summary-update-article-line (article header)
3392   "Update the line for ARTICLE using HEADERS."
3393   (let* ((id (mail-header-id header))
3394          (thread (gnus-id-to-thread id)))
3395     (unless thread
3396       (error "Article in no thread"))
3397     ;; Update the thread.
3398     (setcar thread header)
3399     (gnus-summary-goto-subject article)
3400     (let* ((datal (gnus-data-find-list article))
3401            (data (car datal))
3402            (length (when (cdr datal)
3403                      (- (gnus-data-pos data)
3404                         (gnus-data-pos (cadr datal)))))
3405            (buffer-read-only nil)
3406            (level (gnus-summary-thread-level)))
3407       (gnus-delete-line)
3408       (gnus-summary-insert-line
3409        header level nil (gnus-article-mark article)
3410        (memq article gnus-newsgroup-replied)
3411        (memq article gnus-newsgroup-expirable)
3412        ;; Only insert the Subject string when it's different
3413        ;; from the previous Subject string.
3414        (if (and
3415             gnus-show-threads
3416             (gnus-subject-equal
3417              (condition-case ()
3418                  (mail-header-subject
3419                   (gnus-data-header
3420                    (cadr
3421                     (gnus-data-find-list
3422                      article
3423                      (gnus-data-list t)))))
3424                ;; Error on the side of excessive subjects.
3425                (error ""))
3426              (mail-header-subject header)))
3427            ""
3428          (mail-header-subject header))
3429        nil (cdr (assq article gnus-newsgroup-scored))
3430        (memq article gnus-newsgroup-processable))
3431       (when length
3432         (gnus-data-update-list
3433          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3434
3435 (defun gnus-summary-update-article (article &optional iheader)
3436   "Update ARTICLE in the summary buffer."
3437   (set-buffer gnus-summary-buffer)
3438   (let* ((header (gnus-summary-article-header article))
3439          (id (mail-header-id header))
3440          (data (gnus-data-find article))
3441          (thread (gnus-id-to-thread id))
3442          (references (mail-header-references header))
3443          (parent
3444           (gnus-id-to-thread
3445            (or (gnus-parent-id
3446                 (when (and references
3447                            (not (equal "" references)))
3448                   references))
3449                "none")))
3450          (buffer-read-only nil)
3451          (old (car thread)))
3452     (when thread
3453       (unless iheader
3454         (setcar thread nil)
3455         (when parent
3456           (delq thread parent)))
3457       (if (gnus-summary-insert-subject id header)
3458           ;; Set the (possibly) new article number in the data structure.
3459           (gnus-data-set-number data (gnus-id-to-article id))
3460         (setcar thread old)
3461         nil))))
3462
3463 (defun gnus-rebuild-thread (id &optional line)
3464   "Rebuild the thread containing ID.
3465 If LINE, insert the rebuilt thread starting on line LINE."
3466   (let ((buffer-read-only nil)
3467         old-pos current thread data)
3468     (if (not gnus-show-threads)
3469         (setq thread (list (car (gnus-id-to-thread id))))
3470       ;; Get the thread this article is part of.
3471       (setq thread (gnus-remove-thread id)))
3472     (setq old-pos (gnus-point-at-bol))
3473     (setq current (save-excursion
3474                     (and (zerop (forward-line -1))
3475                          (gnus-summary-article-number))))
3476     ;; If this is a gathered thread, we have to go some re-gathering.
3477     (when (stringp (car thread))
3478       (let ((subject (car thread))
3479             roots thr)
3480         (setq thread (cdr thread))
3481         (while thread
3482           (unless (memq (setq thr (gnus-id-to-thread
3483                                    (gnus-root-id
3484                                     (mail-header-id (caar thread)))))
3485                         roots)
3486             (push thr roots))
3487           (setq thread (cdr thread)))
3488         ;; We now have all (unique) roots.
3489         (if (= (length roots) 1)
3490             ;; All the loose roots are now one solid root.
3491             (setq thread (car roots))
3492           (setq thread (cons subject (gnus-sort-threads roots))))))
3493     (let (threads)
3494       ;; We then insert this thread into the summary buffer.
3495       (when line
3496         (goto-char (point-min))
3497         (forward-line (1- line)))
3498       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3499         (if gnus-show-threads
3500             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3501           (gnus-summary-prepare-unthreaded thread))
3502         (setq data (nreverse gnus-newsgroup-data))
3503         (setq threads gnus-newsgroup-threads))
3504       ;; We splice the new data into the data structure.
3505       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3506       ;;!!! then we want to insert at the beginning of the buffer.
3507       ;;!!! That happens to be true with Gnus now, but that may
3508       ;;!!! change in the future.  Perhaps.
3509       (gnus-data-enter-list
3510        (if line nil current) data (- (point) old-pos))
3511       (setq gnus-newsgroup-threads
3512             (nconc threads gnus-newsgroup-threads))
3513       (gnus-data-compute-positions))))
3514
3515 (defun gnus-number-to-header (number)
3516   "Return the header for article NUMBER."
3517   (let ((headers gnus-newsgroup-headers))
3518     (while (and headers
3519                 (not (= number (mail-header-number (car headers)))))
3520       (pop headers))
3521     (when headers
3522       (car headers))))
3523
3524 (defun gnus-parent-headers (in-headers &optional generation)
3525   "Return the headers of the GENERATIONeth parent of HEADERS."
3526   (unless generation
3527     (setq generation 1))
3528   (let ((parent t)
3529         (headers in-headers)
3530         references)
3531     (while (and parent
3532                 (not (zerop generation))
3533                 (setq references (mail-header-references headers)))
3534       (setq headers (if (and references
3535                              (setq parent (gnus-parent-id references)))
3536                         (car (gnus-id-to-thread parent))
3537                       nil))
3538       (decf generation))
3539     (and (not (eq headers in-headers))
3540          headers)))
3541
3542 (defun gnus-id-to-thread (id)
3543   "Return the (sub-)thread where ID appears."
3544   (gnus-gethash id gnus-newsgroup-dependencies))
3545
3546 (defun gnus-id-to-article (id)
3547   "Return the article number of ID."
3548   (let ((thread (gnus-id-to-thread id)))
3549     (when (and thread
3550                (car thread))
3551       (mail-header-number (car thread)))))
3552
3553 (defun gnus-id-to-header (id)
3554   "Return the article headers of ID."
3555   (car (gnus-id-to-thread id)))
3556
3557 (defun gnus-article-displayed-root-p (article)
3558   "Say whether ARTICLE is a root(ish) article."
3559   (let ((level (gnus-summary-thread-level article))
3560         (refs (mail-header-references  (gnus-summary-article-header article)))
3561         particle)
3562     (cond
3563      ((null level) nil)
3564      ((zerop level) t)
3565      ((null refs) t)
3566      ((null (gnus-parent-id refs)) t)
3567      ((and (= 1 level)
3568            (null (setq particle (gnus-id-to-article
3569                                  (gnus-parent-id refs))))
3570            (null (gnus-summary-thread-level particle)))))))
3571
3572 (defun gnus-root-id (id)
3573   "Return the id of the root of the thread where ID appears."
3574   (let (last-id prev)
3575     (while (and id (setq prev (car (gnus-id-to-thread id))))
3576       (setq last-id id
3577             id (gnus-parent-id (mail-header-references prev))))
3578     last-id))
3579
3580 (defun gnus-articles-in-thread (thread)
3581   "Return the list of articles in THREAD."
3582   (cons (mail-header-number (car thread))
3583         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3584
3585 (defun gnus-remove-thread (id &optional dont-remove)
3586   "Remove the thread that has ID in it."
3587   (let (headers thread last-id)
3588     ;; First go up in this thread until we find the root.
3589     (setq last-id (gnus-root-id id)
3590           headers (message-flatten-list (gnus-id-to-thread last-id)))
3591     ;; We have now found the real root of this thread.  It might have
3592     ;; been gathered into some loose thread, so we have to search
3593     ;; through the threads to find the thread we wanted.
3594     (let ((threads gnus-newsgroup-threads)
3595           sub)
3596       (while threads
3597         (setq sub (car threads))
3598         (if (stringp (car sub))
3599             ;; This is a gathered thread, so we look at the roots
3600             ;; below it to find whether this article is in this
3601             ;; gathered root.
3602             (progn
3603               (setq sub (cdr sub))
3604               (while sub
3605                 (when (member (caar sub) headers)
3606                   (setq thread (car threads)
3607                         threads nil
3608                         sub nil))
3609                 (setq sub (cdr sub))))
3610           ;; It's an ordinary thread, so we check it.
3611           (when (eq (car sub) (car headers))
3612             (setq thread sub
3613                   threads nil)))
3614         (setq threads (cdr threads)))
3615       ;; If this article is in no thread, then it's a root.
3616       (if thread
3617           (unless dont-remove
3618             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3619         (setq thread (gnus-id-to-thread last-id)))
3620       (when thread
3621         (prog1
3622             thread                      ; We return this thread.
3623           (unless dont-remove
3624             (if (stringp (car thread))
3625                 (progn
3626                   ;; If we use dummy roots, then we have to remove the
3627                   ;; dummy root as well.
3628                   (when (eq gnus-summary-make-false-root 'dummy)
3629                     ;; We go to the dummy root by going to
3630                     ;; the first sub-"thread", and then one line up.
3631                     (gnus-summary-goto-article
3632                      (mail-header-number (caadr thread)))
3633                     (forward-line -1)
3634                     (gnus-delete-line)
3635                     (gnus-data-compute-positions))
3636                   (setq thread (cdr thread))
3637                   (while thread
3638                     (gnus-remove-thread-1 (car thread))
3639                     (setq thread (cdr thread))))
3640               (gnus-remove-thread-1 thread))))))))
3641
3642 (defun gnus-remove-thread-1 (thread)
3643   "Remove the thread THREAD recursively."
3644   (let ((number (mail-header-number (pop thread)))
3645         d)
3646     (setq thread (reverse thread))
3647     (while thread
3648       (gnus-remove-thread-1 (pop thread)))
3649     (when (setq d (gnus-data-find number))
3650       (goto-char (gnus-data-pos d))
3651       (gnus-summary-show-thread)
3652       (gnus-data-remove
3653        number
3654        (- (gnus-point-at-bol)
3655           (prog1
3656               (1+ (gnus-point-at-eol))
3657             (gnus-delete-line)))))))
3658
3659 (defun gnus-sort-threads (threads)
3660   "Sort THREADS."
3661   (if (not gnus-thread-sort-functions)
3662       threads
3663     (gnus-message 8 "Sorting threads...")
3664     (prog1
3665         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3666       (gnus-message 8 "Sorting threads...done"))))
3667
3668 (defun gnus-sort-articles (articles)
3669   "Sort ARTICLES."
3670   (when gnus-article-sort-functions
3671     (gnus-message 7 "Sorting articles...")
3672     (prog1
3673         (setq gnus-newsgroup-headers
3674               (sort articles (gnus-make-sort-function
3675                               gnus-article-sort-functions)))
3676       (gnus-message 7 "Sorting articles...done"))))
3677
3678 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3679 (defmacro gnus-thread-header (thread)
3680   ;; Return header of first article in THREAD.
3681   ;; Note that THREAD must never, ever be anything else than a variable -
3682   ;; using some other form will lead to serious barfage.
3683   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3684   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3685   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" 
3686         (vector thread) 2))
3687
3688 (defsubst gnus-article-sort-by-number (h1 h2)
3689   "Sort articles by article number."
3690   (< (mail-header-number h1)
3691      (mail-header-number h2)))
3692
3693 (defun gnus-thread-sort-by-number (h1 h2)
3694   "Sort threads by root article number."
3695   (gnus-article-sort-by-number
3696    (gnus-thread-header h1) (gnus-thread-header h2)))
3697
3698 (defsubst gnus-article-sort-by-lines (h1 h2)
3699   "Sort articles by article Lines header."
3700   (< (mail-header-lines h1)
3701      (mail-header-lines h2)))
3702
3703 (defun gnus-thread-sort-by-lines (h1 h2)
3704   "Sort threads by root article Lines header."
3705   (gnus-article-sort-by-lines
3706    (gnus-thread-header h1) (gnus-thread-header h2)))
3707
3708 (defsubst gnus-article-sort-by-chars (h1 h2)
3709   "Sort articles by octet length."
3710   (< (mail-header-chars h1)
3711      (mail-header-chars h2)))
3712
3713 (defun gnus-thread-sort-by-chars (h1 h2)
3714   "Sort threads by root article octet length."
3715   (gnus-article-sort-by-chars
3716    (gnus-thread-header h1) (gnus-thread-header h2)))
3717
3718 (defsubst gnus-article-sort-by-author (h1 h2)
3719   "Sort articles by root author."
3720   (string-lessp
3721    (let ((addr (car (mime-entity-read-field h1 'From))))
3722      (or (std11-full-name-string addr)
3723          (std11-address-string addr)
3724          ""))
3725    (let ((addr (car (mime-entity-read-field h2 'From))))
3726      (or (std11-full-name-string addr)
3727          (std11-address-string addr)
3728          ""))
3729    ))
3730
3731 (defun gnus-thread-sort-by-author (h1 h2)
3732   "Sort threads by root author."
3733   (gnus-article-sort-by-author
3734    (gnus-thread-header h1)  (gnus-thread-header h2)))
3735
3736 (defsubst gnus-article-sort-by-subject (h1 h2)
3737   "Sort articles by root subject."
3738   (string-lessp
3739    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3740    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3741
3742 (defun gnus-thread-sort-by-subject (h1 h2)
3743   "Sort threads by root subject."
3744   (gnus-article-sort-by-subject
3745    (gnus-thread-header h1) (gnus-thread-header h2)))
3746
3747 (defsubst gnus-article-sort-by-date (h1 h2)
3748   "Sort articles by root article date."
3749   (time-less-p
3750    (gnus-date-get-time (mail-header-date h1))
3751    (gnus-date-get-time (mail-header-date h2))))
3752
3753 (defun gnus-thread-sort-by-date (h1 h2)
3754   "Sort threads by root article date."
3755   (gnus-article-sort-by-date
3756    (gnus-thread-header h1) (gnus-thread-header h2)))
3757
3758 (defsubst gnus-article-sort-by-score (h1 h2)
3759   "Sort articles by root article score.
3760 Unscored articles will be counted as having a score of zero."
3761   (> (or (cdr (assq (mail-header-number h1)
3762                     gnus-newsgroup-scored))
3763          gnus-summary-default-score 0)
3764      (or (cdr (assq (mail-header-number h2)
3765                     gnus-newsgroup-scored))
3766          gnus-summary-default-score 0)))
3767
3768 (defun gnus-thread-sort-by-score (h1 h2)
3769   "Sort threads by root article score."
3770   (gnus-article-sort-by-score
3771    (gnus-thread-header h1) (gnus-thread-header h2)))
3772
3773 (defun gnus-thread-sort-by-total-score (h1 h2)
3774   "Sort threads by the sum of all scores in the thread.
3775 Unscored articles will be counted as having a score of zero."
3776   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3777
3778 (defun gnus-thread-total-score (thread)
3779   ;; This function find the total score of THREAD.
3780   (cond ((null thread)
3781          0)
3782         ((consp thread)
3783          (if (stringp (car thread))
3784              (apply gnus-thread-score-function 0
3785                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3786            (gnus-thread-total-score-1 thread)))
3787         (t
3788          (gnus-thread-total-score-1 (list thread)))))
3789
3790 (defun gnus-thread-total-score-1 (root)
3791   ;; This function find the total score of the thread below ROOT.
3792   (setq root (car root))
3793   (apply gnus-thread-score-function
3794          (or (append
3795               (mapcar 'gnus-thread-total-score
3796                       (cdr (gnus-id-to-thread (mail-header-id root))))
3797               (when (> (mail-header-number root) 0)
3798                 (list (or (cdr (assq (mail-header-number root)
3799                                      gnus-newsgroup-scored))
3800                           gnus-summary-default-score 0))))
3801              (list gnus-summary-default-score)
3802              '(0))))
3803
3804 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3805 (defvar gnus-tmp-prev-subject nil)
3806 (defvar gnus-tmp-false-parent nil)
3807 (defvar gnus-tmp-root-expunged nil)
3808 (defvar gnus-tmp-dummy-line nil)
3809
3810 (defvar gnus-tmp-header)
3811 (defun gnus-extra-header (type &optional header)
3812   "Return the extra header of TYPE."
3813   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3814       ""))
3815
3816 (defun gnus-summary-prepare-threads (threads)
3817   "Prepare summary buffer from THREADS and indentation LEVEL.
3818 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3819 or a straight list of headers."
3820   (gnus-message 7 "Generating summary...")
3821
3822   (setq gnus-newsgroup-threads threads)
3823   (beginning-of-line)
3824
3825   (let ((gnus-tmp-level 0)
3826         (default-score (or gnus-summary-default-score 0))
3827         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3828         thread number subject stack state gnus-tmp-gathered beg-match
3829         new-roots gnus-tmp-new-adopts thread-end
3830         gnus-tmp-header gnus-tmp-unread
3831         gnus-tmp-replied gnus-tmp-subject-or-nil
3832         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3833         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3834         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3835
3836     (setq gnus-tmp-prev-subject nil)
3837
3838     (if (vectorp (car threads))
3839         ;; If this is a straight (sic) list of headers, then a
3840         ;; threaded summary display isn't required, so we just create
3841         ;; an unthreaded one.
3842         (gnus-summary-prepare-unthreaded threads)
3843
3844       ;; Do the threaded display.
3845
3846       (while (or threads stack gnus-tmp-new-adopts new-roots)
3847
3848         (if (and (= gnus-tmp-level 0)
3849                  (or (not stack)
3850                      (= (caar stack) 0))
3851                  (not gnus-tmp-false-parent)
3852                  (or gnus-tmp-new-adopts new-roots))
3853             (if gnus-tmp-new-adopts
3854                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3855                       thread (list (car gnus-tmp-new-adopts))
3856                       gnus-tmp-header (caar thread)
3857                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3858               (when new-roots
3859                 (setq thread (list (car new-roots))
3860                       gnus-tmp-header (caar thread)
3861                       new-roots (cdr new-roots))))
3862
3863           (if threads
3864               ;; If there are some threads, we do them before the
3865               ;; threads on the stack.
3866               (setq thread threads
3867                     gnus-tmp-header (caar thread))
3868             ;; There were no current threads, so we pop something off
3869             ;; the stack.
3870             (setq state (car stack)
3871                   gnus-tmp-level (car state)
3872                   thread (cdr state)
3873                   stack (cdr stack)
3874                   gnus-tmp-header (caar thread))))
3875
3876         (setq gnus-tmp-false-parent nil)
3877         (setq gnus-tmp-root-expunged nil)
3878         (setq thread-end nil)
3879
3880         (if (stringp gnus-tmp-header)
3881             ;; The header is a dummy root.
3882             (cond
3883              ((eq gnus-summary-make-false-root 'adopt)
3884               ;; We let the first article adopt the rest.
3885               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3886                                                (cddar thread)))
3887               (setq gnus-tmp-gathered
3888                     (nconc (mapcar
3889                             (lambda (h) (mail-header-number (car h)))
3890                             (cddar thread))
3891                            gnus-tmp-gathered))
3892               (setq thread (cons (list (caar thread)
3893                                        (cadar thread))
3894                                  (cdr thread)))
3895               (setq gnus-tmp-level -1
3896                     gnus-tmp-false-parent t))
3897              ((eq gnus-summary-make-false-root 'empty)
3898               ;; We print adopted articles with empty subject fields.
3899               (setq gnus-tmp-gathered
3900                     (nconc (mapcar
3901                             (lambda (h) (mail-header-number (car h)))
3902                             (cddar thread))
3903                            gnus-tmp-gathered))
3904               (setq gnus-tmp-level -1))
3905              ((eq gnus-summary-make-false-root 'dummy)
3906               ;; We remember that we probably want to output a dummy
3907               ;; root.
3908               (setq gnus-tmp-dummy-line gnus-tmp-header)
3909               (setq gnus-tmp-prev-subject gnus-tmp-header))
3910              (t
3911               ;; We do not make a root for the gathered
3912               ;; sub-threads at all.
3913               (setq gnus-tmp-level -1)))
3914
3915           (setq number (mail-header-number gnus-tmp-header)
3916                 subject (mail-header-subject gnus-tmp-header))
3917
3918           (cond
3919            ;; If the thread has changed subject, we might want to make
3920            ;; this subthread into a root.
3921            ((and (null gnus-thread-ignore-subject)
3922                  (not (zerop gnus-tmp-level))
3923                  gnus-tmp-prev-subject
3924                  (not (inline
3925                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3926             (setq new-roots (nconc new-roots (list (car thread)))
3927                   thread-end t
3928                   gnus-tmp-header nil))
3929            ;; If the article lies outside the current limit,
3930            ;; then we do not display it.
3931            ((not (memq number gnus-newsgroup-limit))
3932             (setq gnus-tmp-gathered
3933                   (nconc (mapcar
3934                           (lambda (h) (mail-header-number (car h)))
3935                           (cdar thread))
3936                          gnus-tmp-gathered))
3937             (setq gnus-tmp-new-adopts (if (cdar thread)
3938                                           (append gnus-tmp-new-adopts
3939                                                   (cdar thread))
3940                                         gnus-tmp-new-adopts)
3941                   thread-end t
3942                   gnus-tmp-header nil)
3943             (when (zerop gnus-tmp-level)
3944               (setq gnus-tmp-root-expunged t)))
3945            ;; Perhaps this article is to be marked as read?
3946            ((and gnus-summary-mark-below
3947                  (< (or (cdr (assq number gnus-newsgroup-scored))
3948                         default-score)
3949                     gnus-summary-mark-below)
3950                  ;; Don't touch sparse articles.
3951                  (not (gnus-summary-article-sparse-p number))
3952                  (not (gnus-summary-article-ancient-p number)))
3953             (setq gnus-newsgroup-unreads
3954                   (delq number gnus-newsgroup-unreads))
3955             (if gnus-newsgroup-auto-expire
3956                 (push number gnus-newsgroup-expirable)
3957               (push (cons number gnus-low-score-mark)
3958                     gnus-newsgroup-reads))))
3959
3960           (when gnus-tmp-header
3961             ;; We may have an old dummy line to output before this
3962             ;; article.
3963             (when (and gnus-tmp-dummy-line
3964                        (gnus-subject-equal
3965                         gnus-tmp-dummy-line
3966                         (mail-header-subject gnus-tmp-header)))
3967               (gnus-summary-insert-dummy-line
3968                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3969               (setq gnus-tmp-dummy-line nil))
3970
3971             ;; Compute the mark.
3972             (setq gnus-tmp-unread (gnus-article-mark number))
3973
3974             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3975                                   gnus-tmp-header gnus-tmp-level)
3976                   gnus-newsgroup-data)
3977
3978             ;; Actually insert the line.
3979             (setq
3980              gnus-tmp-subject-or-nil
3981              (cond
3982               ((and gnus-thread-ignore-subject
3983                     gnus-tmp-prev-subject
3984                     (not (inline (gnus-subject-equal
3985                                   gnus-tmp-prev-subject subject))))
3986                subject)
3987               ((zerop gnus-tmp-level)
3988                (if (and (eq gnus-summary-make-false-root 'empty)
3989                         (memq number gnus-tmp-gathered)
3990                         gnus-tmp-prev-subject
3991                         (inline (gnus-subject-equal
3992                                  gnus-tmp-prev-subject subject)))
3993                    gnus-summary-same-subject
3994                  subject))
3995               (t gnus-summary-same-subject)))
3996             (if (and (eq gnus-summary-make-false-root 'adopt)
3997                      (= gnus-tmp-level 1)
3998                      (memq number gnus-tmp-gathered))
3999                 (setq gnus-tmp-opening-bracket ?\<
4000                       gnus-tmp-closing-bracket ?\>)
4001               (setq gnus-tmp-opening-bracket ?\[
4002                     gnus-tmp-closing-bracket ?\]))
4003             (setq
4004              gnus-tmp-indentation
4005              (aref gnus-thread-indent-array gnus-tmp-level)
4006              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4007              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4008                                 gnus-summary-default-score 0)
4009              gnus-tmp-score-char
4010              (if (or (null gnus-summary-default-score)
4011                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4012                          gnus-summary-zcore-fuzz))
4013                  ?  ;Whitespace
4014                (if (< gnus-tmp-score gnus-summary-default-score)
4015                    gnus-score-below-mark gnus-score-over-mark))
4016              gnus-tmp-replied
4017              (cond ((memq number gnus-newsgroup-processable)
4018                     gnus-process-mark)
4019                    ((memq number gnus-newsgroup-cached)
4020                     gnus-cached-mark)
4021                    ((memq number gnus-newsgroup-replied)
4022                     gnus-replied-mark)
4023                    ((memq number gnus-newsgroup-saved)
4024                     gnus-saved-mark)
4025                    (t gnus-unread-mark))
4026              gnus-tmp-from (mail-header-from gnus-tmp-header)
4027              gnus-tmp-name
4028              (cond
4029               ((string-match "<[^>]+> *$" gnus-tmp-from)
4030                (setq beg-match (match-beginning 0))
4031                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4032                         (substring gnus-tmp-from (1+ (match-beginning 0))
4033                                    (1- (match-end 0))))
4034                    (substring gnus-tmp-from 0 beg-match)))
4035               ((string-match "(.+)" gnus-tmp-from)
4036                (substring gnus-tmp-from
4037                           (1+ (match-beginning 0)) (1- (match-end 0))))
4038               (t gnus-tmp-from)))
4039             (when (string= gnus-tmp-name "")
4040               (setq gnus-tmp-name gnus-tmp-from))
4041             (unless (numberp gnus-tmp-lines)
4042               (setq gnus-tmp-lines 0))
4043             (gnus-put-text-property
4044              (point)
4045              (progn (eval gnus-summary-line-format-spec) (point))
4046              'gnus-number number)
4047             (when gnus-visual-p
4048               (forward-line -1)
4049               (gnus-run-hooks 'gnus-summary-update-hook)
4050               (forward-line 1))
4051
4052             (setq gnus-tmp-prev-subject subject)))
4053
4054         (when (nth 1 thread)
4055           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4056         (incf gnus-tmp-level)
4057         (setq threads (if thread-end nil (cdar thread)))
4058         (unless threads
4059           (setq gnus-tmp-level 0)))))
4060   (gnus-message 7 "Generating summary...done"))
4061
4062 (defun gnus-summary-prepare-unthreaded (headers)
4063   "Generate an unthreaded summary buffer based on HEADERS."
4064   (let (header number mark)
4065
4066     (beginning-of-line)
4067
4068     (while headers
4069       ;; We may have to root out some bad articles...
4070       (when (memq (setq number (mail-header-number
4071                                 (setq header (pop headers))))
4072                   gnus-newsgroup-limit)
4073         ;; Mark article as read when it has a low score.
4074         (when (and gnus-summary-mark-below
4075                    (< (or (cdr (assq number gnus-newsgroup-scored))
4076                           gnus-summary-default-score 0)
4077                       gnus-summary-mark-below)
4078                    (not (gnus-summary-article-ancient-p number)))
4079           (setq gnus-newsgroup-unreads
4080                 (delq number gnus-newsgroup-unreads))
4081           (if gnus-newsgroup-auto-expire
4082               (push number gnus-newsgroup-expirable)
4083             (push (cons number gnus-low-score-mark)
4084                   gnus-newsgroup-reads)))
4085
4086         (setq mark (gnus-article-mark number))
4087         (push (gnus-data-make number mark (1+ (point)) header 0)
4088               gnus-newsgroup-data)
4089         (gnus-summary-insert-line
4090          header 0 number
4091          mark (memq number gnus-newsgroup-replied)
4092          (memq number gnus-newsgroup-expirable)
4093          (mail-header-subject header) nil
4094          (cdr (assq number gnus-newsgroup-scored))
4095          (memq number gnus-newsgroup-processable))))))
4096
4097 (defun gnus-summary-remove-list-identifiers ()
4098   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4099   (let ((regexp (if (stringp gnus-list-identifiers)
4100                     gnus-list-identifiers
4101                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4102     (dolist (header gnus-newsgroup-headers)
4103       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4104                           (mail-header-subject header))
4105         (mail-header-set-subject
4106          header (concat (substring (mail-header-subject header)
4107                                    0 (match-beginning 2))
4108                         (substring (mail-header-subject header)
4109                                    (match-end 2))))))))
4110
4111 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4112   "Select newsgroup GROUP.
4113 If READ-ALL is non-nil, all articles in the group are selected.
4114 If SELECT-ARTICLES, only select those articles from GROUP."
4115   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4116          ;;!!! Dirty hack; should be removed.
4117          (gnus-summary-ignore-duplicates
4118           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4119               t
4120             gnus-summary-ignore-duplicates))
4121          (info (nth 2 entry))
4122          articles fetched-articles cached)
4123
4124     (unless (gnus-check-server
4125              (setq gnus-current-select-method
4126                    (gnus-find-method-for-group group)))
4127       (error "Couldn't open server"))
4128
4129     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4130         (gnus-activate-group group)     ; Or we can activate it...
4131         (progn                          ; Or we bug out.
4132           (when (equal major-mode 'gnus-summary-mode)
4133             (kill-buffer (current-buffer)))
4134           (error "Couldn't request group %s: %s"
4135                  group (gnus-status-message group))))
4136
4137     (unless (gnus-request-group group t)
4138       (when (equal major-mode 'gnus-summary-mode)
4139         (kill-buffer (current-buffer)))
4140       (error "Couldn't request group %s: %s"
4141              group (gnus-status-message group)))
4142
4143     (setq gnus-newsgroup-name group)
4144     (setq gnus-newsgroup-unselected nil)
4145     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4146     (gnus-summary-setup-default-charset)
4147
4148     ;; Adjust and set lists of article marks.
4149     (when info
4150       (gnus-adjust-marked-articles info))
4151
4152     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4153     (setq cached
4154           (if (gnus-virtual-group-p group)
4155               gnus-newsgroup-cached
4156             (gnus-cache-articles-in-group group)))
4157
4158     (setq gnus-newsgroup-unreads
4159           (gnus-set-difference
4160            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4161            gnus-newsgroup-dormant))
4162
4163     (setq gnus-newsgroup-processable nil)
4164
4165     (gnus-update-read-articles group gnus-newsgroup-unreads)
4166
4167     (if (setq articles select-articles)
4168         (setq gnus-newsgroup-unselected
4169               (gnus-sorted-intersection
4170                gnus-newsgroup-unreads
4171                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4172       (setq articles (gnus-articles-to-read group read-all)))
4173
4174     (cond
4175      ((null articles)
4176       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4177       'quit)
4178      ((eq articles 0) nil)
4179      (t
4180       ;; Init the dependencies hash table.
4181       (setq gnus-newsgroup-dependencies
4182             (gnus-make-hashtable (length articles)))
4183       (gnus-set-global-variables)
4184       ;; Retrieve the headers and read them in.
4185       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4186       (setq gnus-newsgroup-headers
4187             (gnus-retrieve-parsed-headers
4188              articles gnus-newsgroup-name
4189              ;; We might want to fetch old headers, but
4190              ;; not if there is only 1 article.
4191              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4192                            (not (numberp gnus-fetch-old-headers)))
4193                       (> (length articles) 1))
4194                   gnus-fetch-old-headers)))
4195       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4196
4197       ;; Suppress duplicates?
4198       (when gnus-suppress-duplicates
4199         (gnus-dup-suppress-articles))
4200
4201       ;; Set the initial limit.
4202       (setq gnus-newsgroup-limit (copy-sequence articles))
4203       ;; Remove canceled articles from the list of unread articles.
4204       (setq gnus-newsgroup-unreads
4205             (gnus-set-sorted-intersection
4206              gnus-newsgroup-unreads
4207              (setq fetched-articles
4208                    (mapcar (lambda (headers) (mail-header-number headers))
4209                            gnus-newsgroup-headers))))
4210       ;; Removed marked articles that do not exist.
4211       (gnus-update-missing-marks
4212        (gnus-sorted-complement fetched-articles articles))
4213
4214       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4215       (when cached
4216         (setq gnus-newsgroup-cached cached))
4217
4218       ;; We might want to build some more threads first.
4219       (when (and gnus-fetch-old-headers
4220                  (eq gnus-headers-retrieved-by 'nov))
4221         (if (eq gnus-fetch-old-headers 'invisible)
4222             (gnus-build-all-threads)
4223           (gnus-build-old-threads)))
4224       ;; Let the Gnus agent mark articles as read.
4225       (when gnus-agent
4226         (gnus-agent-get-undownloaded-list))
4227       ;; Remove list identifiers from subject
4228       (when gnus-list-identifiers
4229         (gnus-summary-remove-list-identifiers))
4230       ;; Check whether auto-expire is to be done in this group.
4231       (setq gnus-newsgroup-auto-expire
4232             (gnus-group-auto-expirable-p group))
4233       ;; Set up the article buffer now, if necessary.
4234       (unless gnus-single-article-buffer
4235         (gnus-article-setup-buffer))
4236       ;; First and last article in this newsgroup.
4237       (when gnus-newsgroup-headers
4238         (setq gnus-newsgroup-begin
4239               (mail-header-number (car gnus-newsgroup-headers))
4240               gnus-newsgroup-end
4241               (mail-header-number
4242                (gnus-last-element gnus-newsgroup-headers))))
4243       ;; GROUP is successfully selected.
4244       (or gnus-newsgroup-headers t)))))
4245
4246 (defun gnus-articles-to-read (group &optional read-all)
4247   ;; Find out what articles the user wants to read.
4248   (let* ((articles
4249           ;; Select all articles if `read-all' is non-nil, or if there
4250           ;; are no unread articles.
4251           (if (or read-all
4252                   (and (zerop (length gnus-newsgroup-marked))
4253                        (zerop (length gnus-newsgroup-unreads)))
4254                   (eq (gnus-group-find-parameter group 'display)
4255                       'all))
4256               (or
4257                (gnus-uncompress-range (gnus-active group))
4258                (gnus-cache-articles-in-group group))
4259             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4260                           (copy-sequence gnus-newsgroup-unreads))
4261                   '<)))
4262          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4263          (scored (length scored-list))
4264          (number (length articles))
4265          (marked (+ (length gnus-newsgroup-marked)
4266                     (length gnus-newsgroup-dormant)))
4267          (select
4268           (cond
4269            ((numberp read-all)
4270             read-all)
4271            (t
4272             (condition-case ()
4273                 (cond
4274                  ((and (or (<= scored marked) (= scored number))
4275                        (natnump gnus-large-newsgroup)
4276                        (> number gnus-large-newsgroup))
4277                   (let* ((cursor-in-echo-area nil)
4278                          (input (read-from-minibuffer
4279                                  (format
4280                                   "How many articles from %s (max %d): "
4281                                   (gnus-limit-string gnus-newsgroup-name 35)
4282                                   number)
4283                                  (cons (number-to-string gnus-large-newsgroup)
4284                                        0))))
4285                     (if (string-match "^[ \t]*$" input)
4286                         number
4287                       input)))
4288                  ((and (> scored marked) (< scored number)
4289                        (> (- scored number) 20))
4290                   (let ((input
4291                          (read-string
4292                           (format "%s %s (%d scored, %d total): "
4293                                   "How many articles from"
4294                                   group scored number))))
4295                     (if (string-match "^[ \t]*$" input)
4296                         number input)))
4297                  (t number))
4298               (quit nil))))))
4299     (setq select (if (stringp select) (string-to-number select) select))
4300     (if (or (null select) (zerop select))
4301         select
4302       (if (and (not (zerop scored)) (<= (abs select) scored))
4303           (progn
4304             (setq articles (sort scored-list '<))
4305             (setq number (length articles)))
4306         (setq articles (copy-sequence articles)))
4307
4308       (when (< (abs select) number)
4309         (if (< select 0)
4310             ;; Select the N oldest articles.
4311             (setcdr (nthcdr (1- (abs select)) articles) nil)
4312           ;; Select the N most recent articles.
4313           (setq articles (nthcdr (- number select) articles))))
4314       (setq gnus-newsgroup-unselected
4315             (gnus-sorted-intersection
4316              gnus-newsgroup-unreads
4317              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4318       (when gnus-alter-articles-to-read-function
4319         (setq gnus-newsgroup-unreads
4320               (sort 
4321                (funcall gnus-alter-articles-to-read-function
4322                         gnus-newsgroup-name gnus-newsgroup-unreads)
4323                '<)))
4324       articles)))
4325
4326 (defun gnus-killed-articles (killed articles)
4327   (let (out)
4328     (while articles
4329       (when (inline (gnus-member-of-range (car articles) killed))
4330         (push (car articles) out))
4331       (setq articles (cdr articles)))
4332     out))
4333
4334 (defun gnus-uncompress-marks (marks)
4335   "Uncompress the mark ranges in MARKS."
4336   (let ((uncompressed '(score bookmark))
4337         out)
4338     (while marks
4339       (if (memq (caar marks) uncompressed)
4340           (push (car marks) out)
4341         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4342       (setq marks (cdr marks)))
4343     out))
4344
4345 (defun gnus-adjust-marked-articles (info)
4346   "Set all article lists and remove all marks that are no longer valid."
4347   (let* ((marked-lists (gnus-info-marks info))
4348          (active (gnus-active (gnus-info-group info)))
4349          (min (car active))
4350          (max (cdr active))
4351          (types gnus-article-mark-lists)
4352          (uncompressed '(score bookmark killed))
4353          marks var articles article mark)
4354
4355     (while marked-lists
4356       (setq marks (pop marked-lists))
4357       (set (setq var (intern (format "gnus-newsgroup-%s"
4358                                      (car (rassq (setq mark (car marks))
4359                                                  types)))))
4360            (if (memq (car marks) uncompressed) (cdr marks)
4361              (gnus-uncompress-range (cdr marks))))
4362
4363       (setq articles (symbol-value var))
4364
4365       ;; All articles have to be subsets of the active articles.
4366       (cond
4367        ;; Adjust "simple" lists.
4368        ((memq mark '(tick dormant expire reply save))
4369         (while articles
4370           (when (or (< (setq article (pop articles)) min) (> article max))
4371             (set var (delq article (symbol-value var))))))
4372        ;; Adjust assocs.
4373        ((memq mark uncompressed)
4374         (when (not (listp (cdr (symbol-value var))))
4375           (set var (list (symbol-value var))))
4376         (when (not (listp (cdr articles)))
4377           (setq articles (list articles)))
4378         (while articles
4379           (when (or (not (consp (setq article (pop articles))))
4380                     (< (car article) min)
4381                     (> (car article) max))
4382             (set var (delq article (symbol-value var))))))))))
4383
4384 (defun gnus-update-missing-marks (missing)
4385   "Go through the list of MISSING articles and remove them from the mark lists."
4386   (when missing
4387     (let ((types gnus-article-mark-lists)
4388           var m)
4389       ;; Go through all types.
4390       (while types
4391         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4392         (when (symbol-value var)
4393           ;; This list has articles.  So we delete all missing articles
4394           ;; from it.
4395           (setq m missing)
4396           (while m
4397             (set var (delq (pop m) (symbol-value var)))))))))
4398
4399 (defun gnus-update-marks ()
4400   "Enter the various lists of marked articles into the newsgroup info list."
4401   (let ((types gnus-article-mark-lists)
4402         (info (gnus-get-info gnus-newsgroup-name))
4403         (uncompressed '(score bookmark killed))
4404         type list newmarked symbol delta-marks)
4405     (when info
4406       ;; Add all marks lists to the list of marks lists.
4407       (while (setq type (pop types))
4408         (setq list (symbol-value
4409                     (setq symbol
4410                           (intern (format "gnus-newsgroup-%s"
4411                                           (car type))))))
4412
4413         (when list
4414           ;; Get rid of the entries of the articles that have the
4415           ;; default score.
4416           (when (and (eq (cdr type) 'score)
4417                      gnus-save-score
4418                      list)
4419             (let* ((arts list)
4420                    (prev (cons nil list))
4421                    (all prev))
4422               (while arts
4423                 (if (or (not (consp (car arts)))
4424                         (= (cdar arts) gnus-summary-default-score))
4425                     (setcdr prev (cdr arts))
4426                   (setq prev arts))
4427                 (setq arts (cdr arts)))
4428               (setq list (cdr all)))))
4429
4430         (unless (memq (cdr type) uncompressed)
4431           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4432        
4433         (when (gnus-check-backend-function
4434                'request-set-mark gnus-newsgroup-name)
4435           ;; uncompressed:s are not proper flags (they are cons cells)
4436           ;; cache is a internal gnus flag
4437           (unless (memq (cdr type) (cons 'cache uncompressed))
4438             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4439                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4440                    (add (gnus-remove-from-range
4441                          (gnus-copy-sequence list) old)))
4442               (when add
4443                 (push (list add 'add (list (cdr type))) delta-marks))
4444               (when del
4445                 (push (list del 'del (list (cdr type))) delta-marks)))))
4446           
4447         (when list
4448           (push (cons (cdr type) list) newmarked)))
4449
4450       (when delta-marks
4451         (unless (gnus-check-group gnus-newsgroup-name)
4452           (error "Can't open server for %s" gnus-newsgroup-name))
4453         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4454           
4455       ;; Enter these new marks into the info of the group.
4456       (if (nthcdr 3 info)
4457           (setcar (nthcdr 3 info) newmarked)
4458         ;; Add the marks lists to the end of the info.
4459         (when newmarked
4460           (setcdr (nthcdr 2 info) (list newmarked))))
4461
4462       ;; Cut off the end of the info if there's nothing else there.
4463       (let ((i 5))
4464         (while (and (> i 2)
4465                     (not (nth i info)))
4466           (when (nthcdr (decf i) info)
4467             (setcdr (nthcdr i info) nil)))))))
4468
4469 (defun gnus-set-mode-line (where)
4470   "This function sets the mode line of the article or summary buffers.
4471 If WHERE is `summary', the summary mode line format will be used."
4472   ;; Is this mode line one we keep updated?
4473   (when (and (memq where gnus-updated-mode-lines)
4474              (symbol-value
4475               (intern (format "gnus-%s-mode-line-format-spec" where))))
4476     (let (mode-string)
4477       (save-excursion
4478         ;; We evaluate this in the summary buffer since these
4479         ;; variables are buffer-local to that buffer.
4480         (set-buffer gnus-summary-buffer)
4481         ;; We bind all these variables that are used in the `eval' form
4482         ;; below.
4483         (let* ((mformat (symbol-value
4484                          (intern
4485                           (format "gnus-%s-mode-line-format-spec" where))))
4486                (gnus-tmp-group-name gnus-newsgroup-name)
4487                (gnus-tmp-article-number (or gnus-current-article 0))
4488                (gnus-tmp-unread gnus-newsgroup-unreads)
4489                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4490                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4491                (gnus-tmp-unread-and-unselected
4492                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4493                             (zerop gnus-tmp-unselected))
4494                        "")
4495                       ((zerop gnus-tmp-unselected)
4496                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4497                       (t (format "{%d(+%d) more}"
4498                                  gnus-tmp-unread-and-unticked
4499                                  gnus-tmp-unselected))))
4500                (gnus-tmp-subject
4501                 (if (and gnus-current-headers
4502                          (vectorp gnus-current-headers))
4503                     (gnus-mode-string-quote
4504                      (mail-header-subject gnus-current-headers))
4505                   ""))
4506                bufname-length max-len
4507                gnus-tmp-header);; passed as argument to any user-format-funcs
4508           (setq mode-string (eval mformat))
4509           (setq bufname-length (if (string-match "%b" mode-string)
4510                                    (- (length
4511                                        (buffer-name
4512                                         (if (eq where 'summary)
4513                                             nil
4514                                           (get-buffer gnus-article-buffer))))
4515                                       2)
4516                                  0))
4517           (setq max-len (max 4 (if gnus-mode-non-string-length
4518                                    (- (window-width)
4519                                       gnus-mode-non-string-length
4520                                       bufname-length)
4521                                  (length mode-string))))
4522           ;; We might have to chop a bit of the string off...
4523           (when (> (length mode-string) max-len)
4524             (setq mode-string
4525                   (concat (gnus-truncate-string mode-string (- max-len 3))
4526                           "...")))
4527           ;; Pad the mode string a bit.
4528           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4529       ;; Update the mode line.
4530       (setq mode-line-buffer-identification
4531             (gnus-mode-line-buffer-identification (list mode-string)))
4532       (set-buffer-modified-p t))))
4533
4534 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4535   "Go through the HEADERS list and add all Xrefs to a hash table.
4536 The resulting hash table is returned, or nil if no Xrefs were found."
4537   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4538          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4539          (xref-hashtb (gnus-make-hashtable))
4540          start group entry number xrefs header)
4541     (while headers
4542       (setq header (pop headers))
4543       (when (and (setq xrefs (mail-header-xref header))
4544                  (not (memq (setq number (mail-header-number header))
4545                             unreads)))
4546         (setq start 0)
4547         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4548           (setq start (match-end 0))
4549           (setq group (if prefix
4550                           (concat prefix (substring xrefs (match-beginning 1)
4551                                                     (match-end 1)))
4552                         (substring xrefs (match-beginning 1) (match-end 1))))
4553           (setq number
4554                 (string-to-int (substring xrefs (match-beginning 2)
4555                                           (match-end 2))))
4556           (if (setq entry (gnus-gethash group xref-hashtb))
4557               (setcdr entry (cons number (cdr entry)))
4558             (gnus-sethash group (cons number nil) xref-hashtb)))))
4559     (and start xref-hashtb)))
4560
4561 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4562   "Look through all the headers and mark the Xrefs as read."
4563   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4564         name entry info xref-hashtb idlist method nth4)
4565     (save-excursion
4566       (set-buffer gnus-group-buffer)
4567       (when (setq xref-hashtb
4568                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4569         (mapatoms
4570          (lambda (group)
4571            (unless (string= from-newsgroup (setq name (symbol-name group)))
4572              (setq idlist (symbol-value group))
4573              ;; Dead groups are not updated.
4574              (and (prog1
4575                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4576                             info (nth 2 entry))
4577                     (when (stringp (setq nth4 (gnus-info-method info)))
4578                       (setq nth4 (gnus-server-to-method nth4))))
4579                   ;; Only do the xrefs if the group has the same
4580                   ;; select method as the group we have just read.
4581                   (or (gnus-methods-equal-p
4582                        nth4 (gnus-find-method-for-group from-newsgroup))
4583                       virtual
4584                       (equal nth4 (setq method (gnus-find-method-for-group
4585                                                 from-newsgroup)))
4586                       (and (equal (car nth4) (car method))
4587                            (equal (nth 1 nth4) (nth 1 method))))
4588                   gnus-use-cross-reference
4589                   (or (not (eq gnus-use-cross-reference t))
4590                       virtual
4591                       ;; Only do cross-references on subscribed
4592                       ;; groups, if that is what is wanted.
4593                       (<= (gnus-info-level info) gnus-level-subscribed))
4594                   (gnus-group-make-articles-read name idlist))))
4595          xref-hashtb)))))
4596
4597 (defun gnus-compute-read-articles (group articles)
4598   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4599          (info (nth 2 entry))
4600          (active (gnus-active group))
4601          ninfo)
4602     (when entry
4603       ;; First peel off all invalid article numbers.
4604       (when active
4605         (let ((ids articles)
4606               id first)
4607           (while (setq id (pop ids))
4608             (when (and first (> id (cdr active)))
4609               ;; We'll end up in this situation in one particular
4610               ;; obscure situation.  If you re-scan a group and get
4611               ;; a new article that is cross-posted to a different
4612               ;; group that has not been re-scanned, you might get
4613               ;; crossposted article that has a higher number than
4614               ;; Gnus believes possible.  So we re-activate this
4615               ;; group as well.  This might mean doing the
4616               ;; crossposting thingy will *increase* the number
4617               ;; of articles in some groups.  Tsk, tsk.
4618               (setq active (or (gnus-activate-group group) active)))
4619             (when (or (> id (cdr active))
4620                       (< id (car active)))
4621               (setq articles (delq id articles))))))
4622       ;; If the read list is nil, we init it.
4623       (if (and active
4624                (null (gnus-info-read info))
4625                (> (car active) 1))
4626           (setq ninfo (cons 1 (1- (car active))))
4627         (setq ninfo (gnus-info-read info)))
4628       ;; Then we add the read articles to the range.
4629       (gnus-add-to-range
4630        ninfo (setq articles (sort articles '<))))))
4631
4632 (defun gnus-group-make-articles-read (group articles)
4633   "Update the info of GROUP to say that ARTICLES are read."
4634   (let* ((num 0)
4635          (entry (gnus-gethash group gnus-newsrc-hashtb))
4636          (info (nth 2 entry))
4637          (active (gnus-active group))
4638          range)
4639     (when entry
4640       (setq range (gnus-compute-read-articles group articles))
4641       (save-excursion
4642         (set-buffer gnus-group-buffer)
4643         (gnus-undo-register
4644           `(progn
4645              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4646              (gnus-info-set-read ',info ',(gnus-info-read info))
4647              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4648              (gnus-group-update-group ,group t))))
4649       ;; Add the read articles to the range.
4650       (gnus-info-set-read info range)
4651       ;; Then we have to re-compute how many unread
4652       ;; articles there are in this group.
4653       (when active
4654         (cond
4655          ((not range)
4656           (setq num (- (1+ (cdr active)) (car active))))
4657          ((not (listp (cdr range)))
4658           (setq num (- (cdr active) (- (1+ (cdr range))
4659                                        (car range)))))
4660          (t
4661           (while range
4662             (if (numberp (car range))
4663                 (setq num (1+ num))
4664               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4665             (setq range (cdr range)))
4666           (setq num (- (cdr active) num))))
4667         ;; Update the number of unread articles.
4668         (setcar entry num)
4669         ;; Update the group buffer.
4670         (gnus-group-update-group group t)))))
4671
4672 (defvar gnus-newsgroup-none-id 0)
4673
4674 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4675   (let ((cur nntp-server-buffer)
4676         (dependencies
4677          (or dependencies
4678              (save-excursion (set-buffer gnus-summary-buffer)
4679                              gnus-newsgroup-dependencies)))
4680         headers id end ref
4681         (mail-parse-charset gnus-newsgroup-charset)
4682         (mail-parse-ignored-charsets
4683          (save-excursion (condition-case nil
4684                              (set-buffer gnus-summary-buffer)
4685                            (error))
4686                          gnus-newsgroup-ignored-charsets)))
4687     (save-excursion
4688       (set-buffer nntp-server-buffer)
4689       ;; Translate all TAB characters into SPACE characters.
4690       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4691       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4692       (gnus-run-hooks 'gnus-parse-headers-hook)
4693       (let ((case-fold-search t)
4694             in-reply-to header p lines chars ctype)
4695         (goto-char (point-min))
4696         ;; Search to the beginning of the next header.  Error messages
4697         ;; do not begin with 2 or 3.
4698         (while (re-search-forward "^[23][0-9]+ " nil t)
4699           (setq id nil
4700                 ref nil)
4701           ;; This implementation of this function, with nine
4702           ;; search-forwards instead of the one re-search-forward and
4703           ;; a case (which basically was the old function) is actually
4704           ;; about twice as fast, even though it looks messier.  You
4705           ;; can't have everything, I guess.  Speed and elegance
4706           ;; doesn't always go hand in hand.
4707           (setq
4708            header
4709            (make-full-mail-header
4710             ;; Number.
4711             (prog1
4712                 (read cur)
4713               (end-of-line)
4714               (setq p (point))
4715               (narrow-to-region (point)
4716                                 (or (and (search-forward "\n.\n" nil t)
4717                                          (- (point) 2))
4718                                     (point))))
4719             ;; Subject.
4720             (progn
4721               (goto-char p)
4722               (if (search-forward "\nsubject: " nil t)
4723                   (buffer-substring (match-end 0) (std11-field-end))
4724                 "(none)"))
4725             ;; From.
4726             (progn
4727               (goto-char p)
4728               (if (search-forward "\nfrom: " nil t)
4729                   (buffer-substring (match-end 0) (std11-field-end))
4730                 "(nobody)"))
4731             ;; Date.
4732             (progn
4733               (goto-char p)
4734               (if (search-forward "\ndate: " nil t)
4735                   (buffer-substring (match-end 0) (std11-field-end))
4736                 ""))
4737             ;; Message-ID.
4738             (progn
4739               (goto-char p)
4740               (setq id (if (re-search-forward
4741                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4742                            ;; We do it this way to make sure the Message-ID
4743                            ;; is (somewhat) syntactically valid.
4744                            (buffer-substring (match-beginning 1)
4745                                              (match-end 1))
4746                          ;; If there was no message-id, we just fake one
4747                          ;; to make subsequent routines simpler.
4748                          (nnheader-generate-fake-message-id))))
4749             ;; References.
4750             (progn
4751               (goto-char p)
4752               (if (search-forward "\nreferences: " nil t)
4753                   (progn
4754                     (setq end (point))
4755                     (prog1
4756                         (buffer-substring (match-end 0) (std11-field-end))
4757                       (setq ref
4758                             (buffer-substring
4759                              (progn
4760                                ;; (end-of-line)
4761                                (search-backward ">" end t)
4762                                (1+ (point)))
4763                              (progn
4764                                (search-backward "<" end t)
4765                                (point))))))
4766                 ;; Get the references from the in-reply-to header if there
4767                 ;; were no references and the in-reply-to header looks
4768                 ;; promising.
4769                 (if (and (search-forward "\nin-reply-to: " nil t)
4770                          (setq in-reply-to
4771                                (buffer-substring (match-end 0)
4772                                                  (std11-field-end)))
4773                          (string-match "<[^>]+>" in-reply-to))
4774                     (let (ref2)
4775                       (setq ref (substring in-reply-to (match-beginning 0)
4776                                            (match-end 0)))
4777                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4778                         (setq ref2 (substring in-reply-to (match-beginning 0)
4779                                               (match-end 0)))
4780                         (when (> (length ref2) (length ref))
4781                           (setq ref ref2)))
4782                       ref)
4783                   (setq ref nil))))
4784             ;; Chars.
4785             (progn
4786               (goto-char p)
4787               (if (search-forward "\nchars: " nil t)
4788                   (if (numberp (setq chars (ignore-errors (read cur))))
4789                       chars 0)
4790                 0))
4791             ;; Lines.
4792             (progn
4793               (goto-char p)
4794               (if (search-forward "\nlines: " nil t)
4795                   (if (numberp (setq lines (ignore-errors (read cur))))
4796                       lines 0)
4797                 0))
4798             ;; Xref.
4799             (progn
4800               (goto-char p)
4801               (and (search-forward "\nxref: " nil t)
4802                    (buffer-substring (match-end 0) (std11-field-end))))
4803             ;; Extra.
4804             (when gnus-extra-headers
4805               (let ((extra gnus-extra-headers)
4806                     out)
4807                 (while extra
4808                   (goto-char p)
4809                   (when (search-forward
4810                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4811                     (push (cons (car extra)
4812                                 (buffer-substring (match-end 0)
4813                                                   (std11-field-end)))
4814                           out))
4815                   (pop extra))
4816                 out))))
4817           (goto-char p)
4818           (if (and (search-forward "\ncontent-type: " nil t)
4819                    (setq ctype
4820                          (buffer-substring (match-end 0) (std11-field-end))))
4821               (mime-entity-set-content-type-internal
4822                header (mime-parse-Content-Type ctype)))
4823           (when (equal id ref)
4824             (setq ref nil))
4825
4826           (when gnus-alter-header-function
4827             (funcall gnus-alter-header-function header)
4828             (setq id (mail-header-id header)
4829                   ref (gnus-parent-id (mail-header-references header))))
4830
4831           (when (setq header
4832                       (gnus-dependencies-add-header
4833                        header dependencies force-new))
4834             (push header headers))
4835           (goto-char (point-max))
4836           (widen))
4837         (nreverse headers)))))
4838
4839 ;; Goes through the xover lines and returns a list of vectors
4840 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4841                                                   force-new dependencies
4842                                                   group also-fetch-heads)
4843   "Parse the news overview data in the server buffer, and return a
4844 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4845   ;; Get the Xref when the users reads the articles since most/some
4846   ;; NNTP servers do not include Xrefs when using XOVER.
4847   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4848   (let ((mail-parse-charset gnus-newsgroup-charset)
4849         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4850         (cur nntp-server-buffer)
4851         (dependencies (or dependencies gnus-newsgroup-dependencies))
4852         number headers header)
4853     (save-excursion
4854       (set-buffer nntp-server-buffer)
4855       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4856       ;; Allow the user to mangle the headers before parsing them.
4857       (gnus-run-hooks 'gnus-parse-headers-hook)
4858       (goto-char (point-min))
4859       (while (not (eobp))
4860         (condition-case ()
4861             (while (and sequence (not (eobp)))
4862               (setq number (read cur))
4863               (while (and sequence
4864                           (< (car sequence) number))
4865                 (setq sequence (cdr sequence)))
4866               (and sequence
4867                    (eq number (car sequence))
4868                    (progn
4869                      (setq sequence (cdr sequence))
4870                      (setq header (inline
4871                                     (gnus-nov-parse-line
4872                                      number dependencies force-new))))
4873                    (push header headers))
4874               (forward-line 1))
4875           (error
4876            (gnus-error 4 "Strange nov line (%d)"
4877                        (count-lines (point-min) (point)))))
4878         (forward-line 1))
4879       ;; A common bug in inn is that if you have posted an article and
4880       ;; then retrieves the active file, it will answer correctly --
4881       ;; the new article is included.  However, a NOV entry for the
4882       ;; article may not have been generated yet, so this may fail.
4883       ;; We work around this problem by retrieving the last few
4884       ;; headers using HEAD.
4885       (if (or (not also-fetch-heads)
4886               (not sequence))
4887           ;; We (probably) got all the headers.
4888           (nreverse headers)
4889         (let ((gnus-nov-is-evil t))
4890           (nconc
4891            (nreverse headers)
4892            (gnus-retrieve-parsed-headers sequence group)
4893            ))))))
4894
4895 (defun gnus-article-get-xrefs ()
4896   "Fill in the Xref value in `gnus-current-headers', if necessary.
4897 This is meant to be called in `gnus-article-internal-prepare-hook'."
4898   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4899                                  gnus-current-headers)))
4900     (or (not gnus-use-cross-reference)
4901         (not headers)
4902         (and (mail-header-xref headers)
4903              (not (string= (mail-header-xref headers) "")))
4904         (let ((case-fold-search t)
4905               xref)
4906           (save-restriction
4907             (nnheader-narrow-to-headers)
4908             (goto-char (point-min))
4909             (when (or (and (not (eobp))
4910                            (eq (downcase (char-after)) ?x)
4911                            (looking-at "Xref:"))
4912                       (search-forward "\nXref:" nil t))
4913               (goto-char (1+ (match-end 0)))
4914               (setq xref (buffer-substring (point)
4915                                            (progn (end-of-line) (point))))
4916               (mail-header-set-xref headers xref)))))))
4917
4918 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4919   "Find article ID and insert the summary line for that article.
4920 OLD-HEADER can either be a header or a line number to insert
4921 the subject line on."
4922   (let* ((line (and (numberp old-header) old-header))
4923          (old-header (and (vectorp old-header) old-header))
4924          (header (cond ((and old-header use-old-header)
4925                         old-header)
4926                        ((and (numberp id)
4927                              (gnus-number-to-header id))
4928                         (gnus-number-to-header id))
4929                        (t
4930                         (gnus-read-header id))))
4931          (number (and (numberp id) id))
4932          d)
4933     (when header
4934       ;; Rebuild the thread that this article is part of and go to the
4935       ;; article we have fetched.
4936       (when (and (not gnus-show-threads)
4937                  old-header)
4938         (when (and number
4939                    (setq d (gnus-data-find (mail-header-number old-header))))
4940           (goto-char (gnus-data-pos d))
4941           (gnus-data-remove
4942            number
4943            (- (gnus-point-at-bol)
4944               (prog1
4945                   (1+ (gnus-point-at-eol))
4946                 (gnus-delete-line))))))
4947       (when old-header
4948         (mail-header-set-number header (mail-header-number old-header)))
4949       (setq gnus-newsgroup-sparse
4950             (delq (setq number (mail-header-number header))
4951                   gnus-newsgroup-sparse))
4952       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4953       (push number gnus-newsgroup-limit)
4954       (gnus-rebuild-thread (mail-header-id header) line)
4955       (gnus-summary-goto-subject number nil t))
4956     (when (and (numberp number)
4957                (> number 0))
4958       ;; We have to update the boundaries even if we can't fetch the
4959       ;; article if ID is a number -- so that the next `P' or `N'
4960       ;; command will fetch the previous (or next) article even
4961       ;; if the one we tried to fetch this time has been canceled.
4962       (when (> number gnus-newsgroup-end)
4963         (setq gnus-newsgroup-end number))
4964       (when (< number gnus-newsgroup-begin)
4965         (setq gnus-newsgroup-begin number))
4966       (setq gnus-newsgroup-unselected
4967             (delq number gnus-newsgroup-unselected)))
4968     ;; Report back a success?
4969     (and header (mail-header-number header))))
4970
4971 ;;; Process/prefix in the summary buffer
4972
4973 (defun gnus-summary-work-articles (n)
4974   "Return a list of articles to be worked upon.
4975 The prefix argument, the list of process marked articles, and the
4976 current article will be taken into consideration."
4977   (save-excursion
4978     (set-buffer gnus-summary-buffer)
4979     (cond
4980      (n
4981       ;; A numerical prefix has been given.
4982       (setq n (prefix-numeric-value n))
4983       (let ((backward (< n 0))
4984             (n (abs (prefix-numeric-value n)))
4985             articles article)
4986         (save-excursion
4987           (while
4988               (and (> n 0)
4989                    (push (setq article (gnus-summary-article-number))
4990                          articles)
4991                    (if backward
4992                        (gnus-summary-find-prev nil article)
4993                      (gnus-summary-find-next nil article)))
4994             (decf n)))
4995         (nreverse articles)))
4996      ((and (gnus-region-active-p) (mark))
4997       (message "region active")
4998       ;; Work on the region between point and mark.
4999       (let ((max (max (point) (mark)))
5000             articles article)
5001         (save-excursion
5002           (goto-char (min (point) (mark)))
5003           (while
5004               (and
5005                (push (setq article (gnus-summary-article-number)) articles)
5006                (gnus-summary-find-next nil article)
5007                (< (point) max)))
5008           (nreverse articles))))
5009      (gnus-newsgroup-processable
5010       ;; There are process-marked articles present.
5011       ;; Save current state.
5012       (gnus-summary-save-process-mark)
5013       ;; Return the list.
5014       (reverse gnus-newsgroup-processable))
5015      (t
5016       ;; Just return the current article.
5017       (list (gnus-summary-article-number))))))
5018
5019 (defmacro gnus-summary-iterate (arg &rest forms)
5020   "Iterate over the process/prefixed articles and do FORMS.
5021 ARG is the interactive prefix given to the command.  FORMS will be
5022 executed with point over the summary line of the articles."
5023   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5024     `(let ((,articles (gnus-summary-work-articles ,arg)))
5025        (while ,articles
5026          (gnus-summary-goto-subject (car ,articles))
5027          ,@forms
5028          (pop ,articles)))))
5029
5030 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5031 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5032
5033 (defun gnus-summary-save-process-mark ()
5034   "Push the current set of process marked articles on the stack."
5035   (interactive)
5036   (push (copy-sequence gnus-newsgroup-processable)
5037         gnus-newsgroup-process-stack))
5038
5039 (defun gnus-summary-kill-process-mark ()
5040   "Push the current set of process marked articles on the stack and unmark."
5041   (interactive)
5042   (gnus-summary-save-process-mark)
5043   (gnus-summary-unmark-all-processable))
5044
5045 (defun gnus-summary-yank-process-mark ()
5046   "Pop the last process mark state off the stack and restore it."
5047   (interactive)
5048   (unless gnus-newsgroup-process-stack
5049     (error "Empty mark stack"))
5050   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5051
5052 (defun gnus-summary-process-mark-set (set)
5053   "Make SET into the current process marked articles."
5054   (gnus-summary-unmark-all-processable)
5055   (while set
5056     (gnus-summary-set-process-mark (pop set))))
5057
5058 ;;; Searching and stuff
5059
5060 (defun gnus-summary-search-group (&optional backward use-level)
5061   "Search for next unread newsgroup.
5062 If optional argument BACKWARD is non-nil, search backward instead."
5063   (save-excursion
5064     (set-buffer gnus-group-buffer)
5065     (when (gnus-group-search-forward
5066            backward nil (if use-level (gnus-group-group-level) nil))
5067       (gnus-group-group-name))))
5068
5069 (defun gnus-summary-best-group (&optional exclude-group)
5070   "Find the name of the best unread group.
5071 If EXCLUDE-GROUP, do not go to this group."
5072   (save-excursion
5073     (set-buffer gnus-group-buffer)
5074     (save-excursion
5075       (gnus-group-best-unread-group exclude-group))))
5076
5077 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5078   (if backward (gnus-summary-find-prev)
5079     (let* ((dummy (gnus-summary-article-intangible-p))
5080            (article (or article (gnus-summary-article-number)))
5081            (arts (gnus-data-find-list article))
5082            result)
5083       (when (and (not dummy)
5084                  (or (not gnus-summary-check-current)
5085                      (not unread)
5086                      (not (gnus-data-unread-p (car arts)))))
5087         (setq arts (cdr arts)))
5088       (when (setq result
5089                   (if unread
5090                       (progn
5091                         (while arts
5092                           (when (or (and undownloaded
5093                                          (eq gnus-undownloaded-mark
5094                                              (gnus-data-mark (car arts))))
5095                                     (gnus-data-unread-p (car arts)))
5096                             (setq result (car arts)
5097                                   arts nil))
5098                           (setq arts (cdr arts)))
5099                         result)
5100                     (car arts)))
5101         (goto-char (gnus-data-pos result))
5102         (gnus-data-number result)))))
5103
5104 (defun gnus-summary-find-prev (&optional unread article)
5105   (let* ((eobp (eobp))
5106          (article (or article (gnus-summary-article-number)))
5107          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5108          result)
5109     (when (and (not eobp)
5110                (or (not gnus-summary-check-current)
5111                    (not unread)
5112                    (not (gnus-data-unread-p (car arts)))))
5113       (setq arts (cdr arts)))
5114     (when (setq result
5115                 (if unread
5116                     (progn
5117                       (while arts
5118                         (when (gnus-data-unread-p (car arts))
5119                           (setq result (car arts)
5120                                 arts nil))
5121                         (setq arts (cdr arts)))
5122                       result)
5123                   (car arts)))
5124       (goto-char (gnus-data-pos result))
5125       (gnus-data-number result))))
5126
5127 (defun gnus-summary-find-subject (subject &optional unread backward article)
5128   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5129          (article (or article (gnus-summary-article-number)))
5130          (articles (gnus-data-list backward))
5131          (arts (gnus-data-find-list article articles))
5132          result)
5133     (when (or (not gnus-summary-check-current)
5134               (not unread)
5135               (not (gnus-data-unread-p (car arts))))
5136       (setq arts (cdr arts)))
5137     (while arts
5138       (and (or (not unread)
5139                (gnus-data-unread-p (car arts)))
5140            (vectorp (gnus-data-header (car arts)))
5141            (gnus-subject-equal
5142             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5143            (setq result (car arts)
5144                  arts nil))
5145       (setq arts (cdr arts)))
5146     (and result
5147          (goto-char (gnus-data-pos result))
5148          (gnus-data-number result))))
5149
5150 (defun gnus-summary-search-forward (&optional unread subject backward)
5151   "Search forward for an article.
5152 If UNREAD, look for unread articles.  If SUBJECT, look for
5153 articles with that subject.  If BACKWARD, search backward instead."
5154   (cond (subject (gnus-summary-find-subject subject unread backward))
5155         (backward (gnus-summary-find-prev unread))
5156         (t (gnus-summary-find-next unread))))
5157
5158 (defun gnus-recenter (&optional n)
5159   "Center point in window and redisplay frame.
5160 Also do horizontal recentering."
5161   (interactive "P")
5162   (when (and nil
5163              gnus-auto-center-summary
5164              (not (eq gnus-auto-center-summary 'vertical)))
5165     (gnus-horizontal-recenter))
5166   (recenter n))
5167
5168 (defun gnus-summary-recenter ()
5169   "Center point in the summary window.
5170 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5171 displayed, no centering will be performed."
5172   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5173   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5174   (interactive)
5175   (let* ((top (cond ((< (window-height) 4) 0)
5176                     ((< (window-height) 7) 1)
5177                     (t (if (numberp gnus-auto-center-summary)
5178                            gnus-auto-center-summary
5179                          2))))
5180          (height (1- (window-height)))
5181          (bottom (save-excursion (goto-char (point-max))
5182                                  (forward-line (- height))
5183                                  (point)))
5184          (window (get-buffer-window (current-buffer))))
5185     ;; The user has to want it.
5186     (when gnus-auto-center-summary
5187       (when (get-buffer-window gnus-article-buffer)
5188         ;; Only do recentering when the article buffer is displayed,
5189         ;; Set the window start to either `bottom', which is the biggest
5190         ;; possible valid number, or the second line from the top,
5191         ;; whichever is the least.
5192         (set-window-start
5193          window (min bottom (save-excursion
5194                               (forward-line (- top)) (point)))
5195          t))
5196       ;; Do horizontal recentering while we're at it.
5197       (when (and (get-buffer-window (current-buffer) t)
5198                  (not (eq gnus-auto-center-summary 'vertical)))
5199         (let ((selected (selected-window)))
5200           (select-window (get-buffer-window (current-buffer) t))
5201           (gnus-summary-position-point)
5202           (gnus-horizontal-recenter)
5203           (select-window selected))))))
5204
5205 (defun gnus-summary-jump-to-group (newsgroup)
5206   "Move point to NEWSGROUP in group mode buffer."
5207   ;; Keep update point of group mode buffer if visible.
5208   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5209       (save-window-excursion
5210         ;; Take care of tree window mode.
5211         (when (get-buffer-window gnus-group-buffer)
5212           (pop-to-buffer gnus-group-buffer))
5213         (gnus-group-jump-to-group newsgroup))
5214     (save-excursion
5215       ;; Take care of tree window mode.
5216       (if (get-buffer-window gnus-group-buffer)
5217           (pop-to-buffer gnus-group-buffer)
5218         (set-buffer gnus-group-buffer))
5219       (gnus-group-jump-to-group newsgroup))))
5220
5221 ;; This function returns a list of article numbers based on the
5222 ;; difference between the ranges of read articles in this group and
5223 ;; the range of active articles.
5224 (defun gnus-list-of-unread-articles (group)
5225   (let* ((read (gnus-info-read (gnus-get-info group)))
5226          (active (or (gnus-active group) (gnus-activate-group group)))
5227          (last (cdr active))
5228          first nlast unread)
5229     ;; If none are read, then all are unread.
5230     (if (not read)
5231         (setq first (car active))
5232       ;; If the range of read articles is a single range, then the
5233       ;; first unread article is the article after the last read
5234       ;; article.  Sounds logical, doesn't it?
5235       (if (and (not (listp (cdr read)))
5236                (or (< (car read) (car active))
5237                    (progn (setq read (list read))
5238                           nil)))
5239           (setq first (max (car active) (1+ (cdr read))))
5240         ;; `read' is a list of ranges.
5241         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5242                                   (caar read)))
5243                   1)
5244           (setq first (car active)))
5245         (while read
5246           (when first
5247             (while (< first nlast)
5248               (push first unread)
5249               (setq first (1+ first))))
5250           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5251           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5252           (setq read (cdr read)))))
5253     ;; And add the last unread articles.
5254     (while (<= first last)
5255       (push first unread)
5256       (setq first (1+ first)))
5257     ;; Return the list of unread articles.
5258     (delq 0 (nreverse unread))))
5259
5260 (defun gnus-list-of-read-articles (group)
5261   "Return a list of unread, unticked and non-dormant articles."
5262   (let* ((info (gnus-get-info group))
5263          (marked (gnus-info-marks info))
5264          (active (gnus-active group)))
5265     (and info active
5266          (gnus-set-difference
5267           (gnus-sorted-complement
5268            (gnus-uncompress-range active)
5269            (gnus-list-of-unread-articles group))
5270           (append
5271            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5272            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5273
5274 ;; Various summary commands
5275
5276 (defun gnus-summary-select-article-buffer ()
5277   "Reconfigure windows to show article buffer."
5278   (interactive)
5279   (if (not (gnus-buffer-live-p gnus-article-buffer))
5280       (error "There is no article buffer for this summary buffer")
5281     (gnus-configure-windows 'article)
5282     (select-window (get-buffer-window gnus-article-buffer))))
5283
5284 (defun gnus-summary-universal-argument (arg)
5285   "Perform any operation on all articles that are process/prefixed."
5286   (interactive "P")
5287   (let ((articles (gnus-summary-work-articles arg))
5288         func article)
5289     (if (eq
5290          (setq
5291           func
5292           (key-binding
5293            (read-key-sequence
5294             (substitute-command-keys
5295              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5296          'undefined)
5297         (gnus-error 1 "Undefined key")
5298       (save-excursion
5299         (while articles
5300           (gnus-summary-goto-subject (setq article (pop articles)))
5301           (let (gnus-newsgroup-processable)
5302             (command-execute func))
5303           (gnus-summary-remove-process-mark article)))))
5304   (gnus-summary-position-point))
5305
5306 (defun gnus-summary-toggle-truncation (&optional arg)
5307   "Toggle truncation of summary lines.
5308 With arg, turn line truncation on iff arg is positive."
5309   (interactive "P")
5310   (setq truncate-lines
5311         (if (null arg) (not truncate-lines)
5312           (> (prefix-numeric-value arg) 0)))
5313   (redraw-display))
5314
5315 (defun gnus-summary-reselect-current-group (&optional all rescan)
5316   "Exit and then reselect the current newsgroup.
5317 The prefix argument ALL means to select all articles."
5318   (interactive "P")
5319   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5320     (error "Ephemeral groups can't be reselected"))
5321   (let ((current-subject (gnus-summary-article-number))
5322         (group gnus-newsgroup-name))
5323     (setq gnus-newsgroup-begin nil)
5324     (gnus-summary-exit)
5325     ;; We have to adjust the point of group mode buffer because
5326     ;; point was moved to the next unread newsgroup by exiting.
5327     (gnus-summary-jump-to-group group)
5328     (when rescan
5329       (save-excursion
5330         (save-window-excursion
5331           ;; Don't show group contents.
5332           (set-window-start (selected-window) (point-max))
5333           (gnus-group-get-new-news-this-group 1))))
5334     (gnus-group-read-group all t)
5335     (gnus-summary-goto-subject current-subject nil t)))
5336
5337 (defun gnus-summary-rescan-group (&optional all)
5338   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5339   (interactive "P")
5340   (gnus-summary-reselect-current-group all t))
5341
5342 (defun gnus-summary-update-info (&optional non-destructive)
5343   (save-excursion
5344     (let ((group gnus-newsgroup-name))
5345       (when group
5346         (when gnus-newsgroup-kill-headers
5347           (setq gnus-newsgroup-killed
5348                 (gnus-compress-sequence
5349                  (nconc
5350                   (gnus-set-sorted-intersection
5351                    (gnus-uncompress-range gnus-newsgroup-killed)
5352                    (setq gnus-newsgroup-unselected
5353                          (sort gnus-newsgroup-unselected '<)))
5354                   (setq gnus-newsgroup-unreads
5355                         (sort gnus-newsgroup-unreads '<)))
5356                  t)))
5357         (unless (listp (cdr gnus-newsgroup-killed))
5358           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5359         (let ((headers gnus-newsgroup-headers))
5360           ;; Set the new ranges of read articles.
5361           (save-excursion
5362             (set-buffer gnus-group-buffer)
5363             (gnus-undo-force-boundary))
5364           (gnus-update-read-articles
5365            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5366           ;; Set the current article marks.
5367           (let ((gnus-newsgroup-scored
5368                  (if (and (not gnus-save-score)
5369                           (not non-destructive))
5370                      nil
5371                    gnus-newsgroup-scored)))
5372             (save-excursion
5373               (gnus-update-marks)))
5374           ;; Do the cross-ref thing.
5375           (when gnus-use-cross-reference
5376             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5377           ;; Do not switch windows but change the buffer to work.
5378           (set-buffer gnus-group-buffer)
5379           (unless (gnus-ephemeral-group-p group)
5380             (gnus-group-update-group group)))))))
5381
5382 (defun gnus-summary-save-newsrc (&optional force)
5383   "Save the current number of read/marked articles in the dribble buffer.
5384 The dribble buffer will then be saved.
5385 If FORCE (the prefix), also save the .newsrc file(s)."
5386   (interactive "P")
5387   (gnus-summary-update-info t)
5388   (if force
5389       (gnus-save-newsrc-file)
5390     (gnus-dribble-save)))
5391
5392 (defun gnus-summary-exit (&optional temporary)
5393   "Exit reading current newsgroup, and then return to group selection mode.
5394 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5395   (interactive)
5396   (gnus-set-global-variables)
5397   (gnus-kill-save-kill-buffer)
5398   (gnus-async-halt-prefetch)
5399   (let* ((group gnus-newsgroup-name)
5400          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5401          (mode major-mode)
5402          (group-point nil)
5403          (buf (current-buffer)))
5404     (unless quit-config
5405       ;; Do adaptive scoring, and possibly save score files.
5406       (when gnus-newsgroup-adaptive
5407         (gnus-score-adaptive))
5408       (when gnus-use-scoring
5409         (gnus-score-save)))
5410     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5411     ;; If we have several article buffers, we kill them at exit.
5412     (unless gnus-single-article-buffer
5413       (gnus-kill-buffer gnus-original-article-buffer)
5414       (setq gnus-article-current nil))
5415     (when gnus-use-cache
5416       (gnus-cache-possibly-remove-articles)
5417       (gnus-cache-save-buffers))
5418     (gnus-async-prefetch-remove-group group)
5419     (when gnus-suppress-duplicates
5420       (gnus-dup-enter-articles))
5421     (when gnus-use-trees
5422       (gnus-tree-close group))
5423     (when gnus-use-cache
5424       (gnus-cache-write-active))
5425     ;; Remove entries for this group.
5426     (nnmail-purge-split-history (gnus-group-real-name group))
5427     ;; Make all changes in this group permanent.
5428     (unless quit-config
5429       (gnus-run-hooks 'gnus-exit-group-hook)
5430       (gnus-summary-update-info))
5431     (gnus-close-group group)
5432     ;; Make sure where we were, and go to next newsgroup.
5433     (set-buffer gnus-group-buffer)
5434     (unless quit-config
5435       (gnus-group-jump-to-group group))
5436     (gnus-run-hooks 'gnus-summary-exit-hook)
5437     (unless (or quit-config
5438                 ;; If this group has disappeared from the summary
5439                 ;; buffer, don't skip forwards.
5440                 (not (string= group (gnus-group-group-name))))
5441       (gnus-group-next-unread-group 1))
5442     (setq group-point (point))
5443     (if temporary
5444         nil                             ;Nothing to do.
5445       ;; If we have several article buffers, we kill them at exit.
5446       (unless gnus-single-article-buffer
5447         (gnus-kill-buffer gnus-article-buffer)
5448         (gnus-kill-buffer gnus-original-article-buffer)
5449         (setq gnus-article-current nil))
5450       (set-buffer buf)
5451       (if (not gnus-kill-summary-on-exit)
5452           (gnus-deaden-summary)
5453         ;; We set all buffer-local variables to nil.  It is unclear why
5454         ;; this is needed, but if we don't, buffer-local variables are
5455         ;; not garbage-collected, it seems.  This would the lead to en
5456         ;; ever-growing Emacs.
5457         (gnus-summary-clear-local-variables)
5458         (when (get-buffer gnus-article-buffer)
5459           (bury-buffer gnus-article-buffer))
5460         ;; We clear the global counterparts of the buffer-local
5461         ;; variables as well, just to be on the safe side.
5462         (set-buffer gnus-group-buffer)
5463         (gnus-summary-clear-local-variables)
5464         ;; Return to group mode buffer.
5465         (when (eq mode 'gnus-summary-mode)
5466           (gnus-kill-buffer buf)))
5467       (setq gnus-current-select-method gnus-select-method)
5468       (pop-to-buffer gnus-group-buffer)
5469       (if (not quit-config)
5470           (progn
5471             (goto-char group-point)
5472             (gnus-configure-windows 'group 'force)
5473             (unless (pos-visible-in-window-p)
5474               (forward-line (/ (static-if (featurep 'xemacs)
5475                                    (window-displayed-height)
5476                                  (1- (window-height)))
5477                                -2))
5478               (set-window-start (selected-window) (point))
5479               (goto-char group-point)))
5480         (gnus-handle-ephemeral-exit quit-config))
5481       ;; Clear the current group name.
5482       (unless quit-config
5483         (setq gnus-newsgroup-name nil)))))
5484
5485 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5486 (defun gnus-summary-exit-no-update (&optional no-questions)
5487   "Quit reading current newsgroup without updating read article info."
5488   (interactive)
5489   (let* ((group gnus-newsgroup-name)
5490          (quit-config (gnus-group-quit-config group)))
5491     (when (or no-questions
5492               gnus-expert-user
5493               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5494       (gnus-async-halt-prefetch)
5495       (mapcar 'funcall
5496               (delq 'gnus-summary-expire-articles
5497                     (copy-sequence gnus-summary-prepare-exit-hook)))
5498       ;; If we have several article buffers, we kill them at exit.
5499       (unless gnus-single-article-buffer
5500         (gnus-kill-buffer gnus-article-buffer)
5501         (gnus-kill-buffer gnus-original-article-buffer)
5502         (setq gnus-article-current nil))
5503       (if (not gnus-kill-summary-on-exit)
5504           (gnus-deaden-summary)
5505         (gnus-close-group group)
5506         (gnus-summary-clear-local-variables)
5507         (set-buffer gnus-group-buffer)
5508         (gnus-summary-clear-local-variables)
5509         (when (get-buffer gnus-summary-buffer)
5510           (kill-buffer gnus-summary-buffer)))
5511       (unless gnus-single-article-buffer
5512         (setq gnus-article-current nil))
5513       (when gnus-use-trees
5514         (gnus-tree-close group))
5515       (gnus-async-prefetch-remove-group group)
5516       (when (get-buffer gnus-article-buffer)
5517         (bury-buffer gnus-article-buffer))
5518       ;; Return to the group buffer.
5519       (gnus-configure-windows 'group 'force)
5520       ;; Clear the current group name.
5521       (setq gnus-newsgroup-name nil)
5522       (when (equal (gnus-group-group-name) group)
5523         (gnus-group-next-unread-group 1))
5524       (when quit-config
5525         (gnus-handle-ephemeral-exit quit-config)))))
5526
5527 (defun gnus-handle-ephemeral-exit (quit-config)
5528   "Handle movement when leaving an ephemeral group.
5529 The state which existed when entering the ephemeral is reset."
5530   (if (not (buffer-name (car quit-config)))
5531       (gnus-configure-windows 'group 'force)
5532     (set-buffer (car quit-config))
5533     (cond ((eq major-mode 'gnus-summary-mode)
5534            (gnus-set-global-variables))
5535           ((eq major-mode 'gnus-article-mode)
5536            (save-excursion
5537              ;; The `gnus-summary-buffer' variable may point
5538              ;; to the old summary buffer when using a single
5539              ;; article buffer.
5540              (unless (gnus-buffer-live-p gnus-summary-buffer)
5541                (set-buffer gnus-group-buffer))
5542              (set-buffer gnus-summary-buffer)
5543              (gnus-set-global-variables))))
5544     (if (or (eq (cdr quit-config) 'article)
5545             (eq (cdr quit-config) 'pick))
5546         (progn
5547           ;; The current article may be from the ephemeral group
5548           ;; thus it is best that we reload this article
5549           (gnus-summary-show-article)
5550           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5551               (gnus-configure-windows 'pick 'force)
5552             (gnus-configure-windows (cdr quit-config) 'force)))
5553       (gnus-configure-windows (cdr quit-config) 'force))
5554     (when (eq major-mode 'gnus-summary-mode)
5555       (gnus-summary-next-subject 1 nil t)
5556       (gnus-summary-recenter)
5557       (gnus-summary-position-point))))
5558
5559 (defun gnus-summary-preview-mime-message ()
5560   "MIME decode and play this message."
5561   (interactive)
5562   (let ((gnus-break-pages nil)
5563         (gnus-show-mime t))
5564     (gnus-summary-select-article gnus-show-all-headers t))
5565   (select-window (get-buffer-window gnus-article-buffer)))
5566
5567 ;;; Dead summaries.
5568
5569 (defvar gnus-dead-summary-mode-map nil)
5570
5571 (unless gnus-dead-summary-mode-map
5572   (setq gnus-dead-summary-mode-map (make-keymap))
5573   (suppress-keymap gnus-dead-summary-mode-map)
5574   (substitute-key-definition
5575    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5576   (let ((keys '("\C-d" "\r" "\177" [delete])))
5577     (while keys
5578       (define-key gnus-dead-summary-mode-map
5579         (pop keys) 'gnus-summary-wake-up-the-dead))))
5580
5581 (defvar gnus-dead-summary-mode nil
5582   "Minor mode for Gnus summary buffers.")
5583
5584 (defun gnus-dead-summary-mode (&optional arg)
5585   "Minor mode for Gnus summary buffers."
5586   (interactive "P")
5587   (when (eq major-mode 'gnus-summary-mode)
5588     (make-local-variable 'gnus-dead-summary-mode)
5589     (setq gnus-dead-summary-mode
5590           (if (null arg) (not gnus-dead-summary-mode)
5591             (> (prefix-numeric-value arg) 0)))
5592     (when gnus-dead-summary-mode
5593       (gnus-add-minor-mode
5594        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5595
5596 (defun gnus-deaden-summary ()
5597   "Make the current summary buffer into a dead summary buffer."
5598   ;; Kill any previous dead summary buffer.
5599   (when (and gnus-dead-summary
5600              (buffer-name gnus-dead-summary))
5601     (save-excursion
5602       (set-buffer gnus-dead-summary)
5603       (when gnus-dead-summary-mode
5604         (kill-buffer (current-buffer)))))
5605   ;; Make this the current dead summary.
5606   (setq gnus-dead-summary (current-buffer))
5607   (gnus-dead-summary-mode 1)
5608   (let ((name (buffer-name)))
5609     (when (string-match "Summary" name)
5610       (rename-buffer
5611        (concat (substring name 0 (match-beginning 0)) "Dead "
5612                (substring name (match-beginning 0)))
5613        t)
5614       (bury-buffer))))
5615
5616 (defun gnus-kill-or-deaden-summary (buffer)
5617   "Kill or deaden the summary BUFFER."
5618   (save-excursion
5619     (when (and (buffer-name buffer)
5620                (not gnus-single-article-buffer))
5621       (save-excursion
5622         (set-buffer buffer)
5623         (gnus-kill-buffer gnus-article-buffer)
5624         (gnus-kill-buffer gnus-original-article-buffer)))
5625     (cond (gnus-kill-summary-on-exit
5626            (when (and gnus-use-trees
5627                       (gnus-buffer-exists-p buffer))
5628              (save-excursion
5629                (set-buffer buffer)
5630                (gnus-tree-close gnus-newsgroup-name)))
5631            (gnus-kill-buffer buffer))
5632           ((gnus-buffer-exists-p buffer)
5633            (save-excursion
5634              (set-buffer buffer)
5635              (gnus-deaden-summary))))))
5636
5637 (defun gnus-summary-wake-up-the-dead (&rest args)
5638   "Wake up the dead summary buffer."
5639   (interactive)
5640   (gnus-dead-summary-mode -1)
5641   (let ((name (buffer-name)))
5642     (when (string-match "Dead " name)
5643       (rename-buffer
5644        (concat (substring name 0 (match-beginning 0))
5645                (substring name (match-end 0)))
5646        t)))
5647   (gnus-message 3 "This dead summary is now alive again"))
5648
5649 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5650 (defun gnus-summary-fetch-faq (&optional faq-dir)
5651   "Fetch the FAQ for the current group.
5652 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5653 in."
5654   (interactive
5655    (list
5656     (when current-prefix-arg
5657       (completing-read
5658        "Faq dir: " (and (listp gnus-group-faq-directory)
5659                         (mapcar (lambda (file) (list file))
5660                                 gnus-group-faq-directory))))))
5661   (let (gnus-faq-buffer)
5662     (when (setq gnus-faq-buffer
5663                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5664       (gnus-configure-windows 'summary-faq))))
5665
5666 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5667 (defun gnus-summary-describe-group (&optional force)
5668   "Describe the current newsgroup."
5669   (interactive "P")
5670   (gnus-group-describe-group force gnus-newsgroup-name))
5671
5672 (defun gnus-summary-describe-briefly ()
5673   "Describe summary mode commands briefly."
5674   (interactive)
5675   (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")))
5676
5677 ;; Walking around group mode buffer from summary mode.
5678
5679 (defun gnus-summary-next-group (&optional no-article target-group backward)
5680   "Exit current newsgroup and then select next unread newsgroup.
5681 If prefix argument NO-ARTICLE is non-nil, no article is selected
5682 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5683 previous group instead."
5684   (interactive "P")
5685   ;; Stop pre-fetching.
5686   (gnus-async-halt-prefetch)
5687   (let ((current-group gnus-newsgroup-name)
5688         (current-buffer (current-buffer))
5689         entered)
5690     ;; First we semi-exit this group to update Xrefs and all variables.
5691     ;; We can't do a real exit, because the window conf must remain
5692     ;; the same in case the user is prompted for info, and we don't
5693     ;; want the window conf to change before that...
5694     (gnus-summary-exit t)
5695     (while (not entered)
5696       ;; Then we find what group we are supposed to enter.
5697       (set-buffer gnus-group-buffer)
5698       (gnus-group-jump-to-group current-group)
5699       (setq target-group
5700             (or target-group
5701                 (if (eq gnus-keep-same-level 'best)
5702                     (gnus-summary-best-group gnus-newsgroup-name)
5703                   (gnus-summary-search-group backward gnus-keep-same-level))))
5704       (if (not target-group)
5705           ;; There are no further groups, so we return to the group
5706           ;; buffer.
5707           (progn
5708             (gnus-message 5 "Returning to the group buffer")
5709             (setq entered t)
5710             (when (gnus-buffer-live-p current-buffer)
5711               (set-buffer current-buffer)
5712               (gnus-summary-exit))
5713             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5714         ;; We try to enter the target group.
5715         (gnus-group-jump-to-group target-group)
5716         (let ((unreads (gnus-group-group-unread)))
5717           (if (and (or (eq t unreads)
5718                        (and unreads (not (zerop unreads))))
5719                    (gnus-summary-read-group
5720                     target-group nil no-article
5721                     (and (buffer-name current-buffer) current-buffer)
5722                     nil backward))
5723               (setq entered t)
5724             (setq current-group target-group
5725                   target-group nil)))))))
5726
5727 (defun gnus-summary-prev-group (&optional no-article)
5728   "Exit current newsgroup and then select previous unread newsgroup.
5729 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5730   (interactive "P")
5731   (gnus-summary-next-group no-article nil t))
5732
5733 ;; Walking around summary lines.
5734
5735 (defun gnus-summary-first-subject (&optional unread undownloaded)
5736   "Go to the first unread subject.
5737 If UNREAD is non-nil, go to the first unread article.
5738 Returns the article selected or nil if there are no unread articles."
5739   (interactive "P")
5740   (prog1
5741       (cond
5742        ;; Empty summary.
5743        ((null gnus-newsgroup-data)
5744         (gnus-message 3 "No articles in the group")
5745         nil)
5746        ;; Pick the first article.
5747        ((not unread)
5748         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5749         (gnus-data-number (car gnus-newsgroup-data)))
5750        ;; No unread articles.
5751        ((null gnus-newsgroup-unreads)
5752         (gnus-message 3 "No more unread articles")
5753         nil)
5754        ;; Find the first unread article.
5755        (t
5756         (let ((data gnus-newsgroup-data))
5757           (while (and data
5758                       (and (not (and undownloaded
5759                                      (eq gnus-undownloaded-mark
5760                                          (gnus-data-mark (car data)))))
5761                            (not (gnus-data-unread-p (car data)))))
5762             (setq data (cdr data)))
5763           (when data
5764             (goto-char (gnus-data-pos (car data)))
5765             (gnus-data-number (car data))))))
5766     (gnus-summary-position-point)))
5767
5768 (defun gnus-summary-next-subject (n &optional unread dont-display)
5769   "Go to next N'th summary line.
5770 If N is negative, go to the previous N'th subject line.
5771 If UNREAD is non-nil, only unread articles are selected.
5772 The difference between N and the actual number of steps taken is
5773 returned."
5774   (interactive "p")
5775   (let ((backward (< n 0))
5776         (n (abs n)))
5777     (while (and (> n 0)
5778                 (if backward
5779                     (gnus-summary-find-prev unread)
5780                   (gnus-summary-find-next unread)))
5781       (unless (zerop (setq n (1- n)))
5782         (gnus-summary-show-thread)))
5783     (when (/= 0 n)
5784       (gnus-message 7 "No more%s articles"
5785                     (if unread " unread" "")))
5786     (unless dont-display
5787       (gnus-summary-recenter)
5788       (gnus-summary-position-point))
5789     n))
5790
5791 (defun gnus-summary-next-unread-subject (n)
5792   "Go to next N'th unread summary line."
5793   (interactive "p")
5794   (gnus-summary-next-subject n t))
5795
5796 (defun gnus-summary-prev-subject (n &optional unread)
5797   "Go to previous N'th summary line.
5798 If optional argument UNREAD is non-nil, only unread article is selected."
5799   (interactive "p")
5800   (gnus-summary-next-subject (- n) unread))
5801
5802 (defun gnus-summary-prev-unread-subject (n)
5803   "Go to previous N'th unread summary line."
5804   (interactive "p")
5805   (gnus-summary-next-subject (- n) t))
5806
5807 (defun gnus-summary-goto-subject (article &optional force silent)
5808   "Go the subject line of ARTICLE.
5809 If FORCE, also allow jumping to articles not currently shown."
5810   (interactive "nArticle number: ")
5811   (let ((b (point))
5812         (data (gnus-data-find article)))
5813     ;; We read in the article if we have to.
5814     (and (not data)
5815          force
5816          (gnus-summary-insert-subject
5817           article
5818           (if (or (numberp force) (vectorp force)) force)
5819           t)
5820          (setq data (gnus-data-find article)))
5821     (goto-char b)
5822     (if (not data)
5823         (progn
5824           (unless silent
5825             (gnus-message 3 "Can't find article %d" article))
5826           nil)
5827       (goto-char (gnus-data-pos data))
5828       (gnus-summary-position-point)
5829       article)))
5830
5831 ;; Walking around summary lines with displaying articles.
5832
5833 (defun gnus-summary-expand-window (&optional arg)
5834   "Make the summary buffer take up the entire Emacs frame.
5835 Given a prefix, will force an `article' buffer configuration."
5836   (interactive "P")
5837   (if arg
5838       (gnus-configure-windows 'article 'force)
5839     (gnus-configure-windows 'summary 'force)))
5840
5841 (defun gnus-summary-display-article (article &optional all-header)
5842   "Display ARTICLE in article buffer."
5843   (gnus-set-global-variables)
5844   (if (null article)
5845       nil
5846     (prog1
5847         (if gnus-summary-display-article-function
5848             (funcall gnus-summary-display-article-function article all-header)
5849           (gnus-article-prepare article all-header))
5850       (with-current-buffer gnus-article-buffer
5851         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5852              nil))
5853       (gnus-run-hooks 'gnus-select-article-hook)
5854       (when (and gnus-current-article
5855                  (not (zerop gnus-current-article)))
5856         (gnus-summary-goto-subject gnus-current-article))
5857       (gnus-summary-recenter)
5858       (when (and gnus-use-trees gnus-show-threads)
5859         (gnus-possibly-generate-tree article)
5860         (gnus-highlight-selected-tree article))
5861       ;; Successfully display article.
5862       (gnus-article-set-window-start
5863        (cdr (assq article gnus-newsgroup-bookmarks))))))
5864
5865 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5866   "Select the current article.
5867 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5868 non-nil, the article will be re-fetched even if it already present in
5869 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5870 be displayed."
5871   ;; Make sure we are in the summary buffer to work around bbdb bug.
5872   (unless (eq major-mode 'gnus-summary-mode)
5873     (set-buffer gnus-summary-buffer))
5874   (let ((article (or article (gnus-summary-article-number)))
5875         (all-headers (not (not all-headers))) ;Must be T or NIL.
5876         gnus-summary-display-article-function)
5877     (and (not pseudo)
5878          (gnus-summary-article-pseudo-p article)
5879          (error "This is a pseudo-article"))
5880     (save-excursion
5881       (set-buffer gnus-summary-buffer)
5882       (if (or (and gnus-single-article-buffer
5883                    (or (null gnus-current-article)
5884                        (null gnus-article-current)
5885                        (null (get-buffer gnus-article-buffer))
5886                        (not (eq article (cdr gnus-article-current)))
5887                        (not (equal (car gnus-article-current)
5888                                    gnus-newsgroup-name))))
5889               (and (not gnus-single-article-buffer)
5890                    (or (null gnus-current-article)
5891                        (not (eq gnus-current-article article))))
5892               force)
5893           ;; The requested article is different from the current article.
5894           (progn
5895             (gnus-summary-display-article article all-headers)
5896             (when (or all-headers gnus-show-all-headers)
5897               (gnus-article-show-all-headers))
5898             (gnus-article-set-window-start
5899              (cdr (assq article gnus-newsgroup-bookmarks)))
5900             article)
5901         (when (or all-headers gnus-show-all-headers)
5902           (gnus-article-show-all-headers))
5903         'old))))
5904
5905 (defun gnus-summary-set-current-mark (&optional current-mark)
5906   "Obsolete function."
5907   nil)
5908
5909 (defun gnus-summary-next-article (&optional unread subject backward push)
5910   "Select the next article.
5911 If UNREAD, only unread articles are selected.
5912 If SUBJECT, only articles with SUBJECT are selected.
5913 If BACKWARD, the previous article is selected instead of the next."
5914   (interactive "P")
5915   (cond
5916    ;; Is there such an article?
5917    ((and (gnus-summary-search-forward unread subject backward)
5918          (or (gnus-summary-display-article (gnus-summary-article-number))
5919              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5920     (gnus-summary-position-point))
5921    ;; If not, we try the first unread, if that is wanted.
5922    ((and subject
5923          gnus-auto-select-same
5924          (gnus-summary-first-unread-article))
5925     (gnus-summary-position-point)
5926     (gnus-message 6 "Wrapped"))
5927    ;; Try to get next/previous article not displayed in this group.
5928    ((and gnus-auto-extend-newsgroup
5929          (not unread) (not subject))
5930     (gnus-summary-goto-article
5931      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5932      nil (count-lines (point-min) (point))))
5933    ;; Go to next/previous group.
5934    (t
5935     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5936       (gnus-summary-jump-to-group gnus-newsgroup-name))
5937     (let ((cmd last-command-char)
5938           (point
5939            (save-excursion
5940              (set-buffer gnus-group-buffer)
5941              (point)))
5942           (group
5943            (if (eq gnus-keep-same-level 'best)
5944                (gnus-summary-best-group gnus-newsgroup-name)
5945              (gnus-summary-search-group backward gnus-keep-same-level))))
5946       ;; For some reason, the group window gets selected.  We change
5947       ;; it back.
5948       (select-window (get-buffer-window (current-buffer)))
5949       ;; Select next unread newsgroup automagically.
5950       (cond
5951        ((or (not gnus-auto-select-next)
5952             (not cmd))
5953         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5954        ((or (eq gnus-auto-select-next 'quietly)
5955             (and (eq gnus-auto-select-next 'slightly-quietly)
5956                  push)
5957             (and (eq gnus-auto-select-next 'almost-quietly)
5958                  (gnus-summary-last-article-p)))
5959         ;; Select quietly.
5960         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5961             (gnus-summary-exit)
5962           (gnus-message 7 "No more%s articles (%s)..."
5963                         (if unread " unread" "")
5964                         (if group (concat "selecting " group)
5965                           "exiting"))
5966           (gnus-summary-next-group nil group backward)))
5967        (t
5968         (when (gnus-key-press-event-p last-input-event)
5969           (gnus-summary-walk-group-buffer
5970            gnus-newsgroup-name cmd unread backward point))))))))
5971
5972 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5973   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5974                       (?\C-p (gnus-group-prev-unread-group 1))))
5975         (cursor-in-echo-area t)
5976         keve key group ended)
5977     (save-excursion
5978       (set-buffer gnus-group-buffer)
5979       (goto-char start)
5980       (setq group
5981             (if (eq gnus-keep-same-level 'best)
5982                 (gnus-summary-best-group gnus-newsgroup-name)
5983               (gnus-summary-search-group backward gnus-keep-same-level))))
5984     (while (not ended)
5985       (gnus-message
5986        5 "No more%s articles%s" (if unread " unread" "")
5987        (if (and group
5988                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5989            (format " (Type %s for %s [%s])"
5990                    (single-key-description cmd) group
5991                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5992          (format " (Type %s to exit %s)"
5993                  (single-key-description cmd)
5994                  gnus-newsgroup-name)))
5995       ;; Confirm auto selection.
5996       (setq key (car (setq keve (gnus-read-event-char))))
5997       (setq ended t)
5998       (cond
5999        ((assq key keystrokes)
6000         (let ((obuf (current-buffer)))
6001           (switch-to-buffer gnus-group-buffer)
6002           (when group
6003             (gnus-group-jump-to-group group))
6004           (eval (cadr (assq key keystrokes)))
6005           (setq group (gnus-group-group-name))
6006           (switch-to-buffer obuf))
6007         (setq ended nil))
6008        ((equal key cmd)
6009         (if (or (not group)
6010                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6011             (gnus-summary-exit)
6012           (gnus-summary-next-group nil group backward)))
6013        (t
6014         (push (cdr keve) unread-command-events))))))
6015
6016 (defun gnus-summary-next-unread-article ()
6017   "Select unread article after current one."
6018   (interactive)
6019   (gnus-summary-next-article
6020    (or (not (eq gnus-summary-goto-unread 'never))
6021        (gnus-summary-last-article-p (gnus-summary-article-number)))
6022    (and gnus-auto-select-same
6023         (gnus-summary-article-subject))))
6024
6025 (defun gnus-summary-prev-article (&optional unread subject)
6026   "Select the article after the current one.
6027 If UNREAD is non-nil, only unread articles are selected."
6028   (interactive "P")
6029   (gnus-summary-next-article unread subject t))
6030
6031 (defun gnus-summary-prev-unread-article ()
6032   "Select unread article before current one."
6033   (interactive)
6034   (gnus-summary-prev-article
6035    (or (not (eq gnus-summary-goto-unread 'never))
6036        (gnus-summary-first-article-p (gnus-summary-article-number)))
6037    (and gnus-auto-select-same
6038         (gnus-summary-article-subject))))
6039
6040 (defun gnus-summary-next-page (&optional lines circular)
6041   "Show next page of the selected article.
6042 If at the end of the current article, select the next article.
6043 LINES says how many lines should be scrolled up.
6044
6045 If CIRCULAR is non-nil, go to the start of the article instead of
6046 selecting the next article when reaching the end of the current
6047 article."
6048   (interactive "P")
6049   (setq gnus-summary-buffer (current-buffer))
6050   (gnus-set-global-variables)
6051   (let ((article (gnus-summary-article-number))
6052         (article-window (get-buffer-window gnus-article-buffer t))
6053         endp)
6054     ;; If the buffer is empty, we have no article.
6055     (unless article
6056       (error "No article to select"))
6057     (gnus-configure-windows 'article)
6058     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6059         (if (and (eq gnus-summary-goto-unread 'never)
6060                  (not (gnus-summary-last-article-p article)))
6061             (gnus-summary-next-article)
6062           (gnus-summary-next-unread-article))
6063       (if (or (null gnus-current-article)
6064               (null gnus-article-current)
6065               (/= article (cdr gnus-article-current))
6066               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6067           ;; Selected subject is different from current article's.
6068           (gnus-summary-display-article article)
6069         (when article-window
6070           (gnus-eval-in-buffer-window gnus-article-buffer
6071             (setq endp (gnus-article-next-page lines)))
6072           (when endp
6073             (cond (circular
6074                    (gnus-summary-beginning-of-article))
6075                   (lines
6076                    (gnus-message 3 "End of message"))
6077                   ((null lines)
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     (gnus-summary-recenter)
6083     (gnus-summary-position-point)))
6084
6085 (defun gnus-summary-prev-page (&optional lines move)
6086   "Show previous page of selected article.
6087 Argument LINES specifies lines to be scrolled down.
6088 If MOVE, move to the previous unread article if point is at
6089 the beginning of the buffer."
6090   (interactive "P")
6091   (let ((article (gnus-summary-article-number))
6092         (article-window (get-buffer-window gnus-article-buffer t))
6093         endp)
6094     (gnus-configure-windows 'article)
6095     (if (or (null gnus-current-article)
6096             (null gnus-article-current)
6097             (/= article (cdr gnus-article-current))
6098             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6099         ;; Selected subject is different from current article's.
6100         (gnus-summary-display-article article)
6101       (gnus-summary-recenter)
6102       (when article-window
6103         (gnus-eval-in-buffer-window gnus-article-buffer
6104           (setq endp (gnus-article-prev-page lines)))
6105         (when (and move endp)
6106           (cond (lines
6107                  (gnus-message 3 "Beginning of message"))
6108                 ((null lines)
6109                  (if (and (eq gnus-summary-goto-unread 'never)
6110                           (not (gnus-summary-first-article-p article)))
6111                      (gnus-summary-prev-article)
6112                    (gnus-summary-prev-unread-article))))))))
6113   (gnus-summary-position-point))
6114
6115 (defun gnus-summary-prev-page-or-article (&optional lines)
6116   "Show previous page of selected article.
6117 Argument LINES specifies lines to be scrolled down.
6118 If at the beginning of the article, go to the next article."
6119   (interactive "P")
6120   (gnus-summary-prev-page lines t))
6121
6122 (defun gnus-summary-scroll-up (lines)
6123   "Scroll up (or down) one line current article.
6124 Argument LINES specifies lines to be scrolled up (or down if negative)."
6125   (interactive "p")
6126   (gnus-configure-windows 'article)
6127   (gnus-summary-show-thread)
6128   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6129     (gnus-eval-in-buffer-window gnus-article-buffer
6130       (cond ((> lines 0)
6131              (when (gnus-article-next-page lines)
6132                (gnus-message 3 "End of message")))
6133             ((< lines 0)
6134              (gnus-article-prev-page (- lines))))))
6135   (gnus-summary-recenter)
6136   (gnus-summary-position-point))
6137
6138 (defun gnus-summary-scroll-down (lines)
6139   "Scroll down (or up) one line current article.
6140 Argument LINES specifies lines to be scrolled down (or up if negative)."
6141   (interactive "p")
6142   (gnus-summary-scroll-up (- lines)))
6143
6144 (defun gnus-summary-next-same-subject ()
6145   "Select next article which has the same subject as current one."
6146   (interactive)
6147   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6148
6149 (defun gnus-summary-prev-same-subject ()
6150   "Select previous article which has the same subject as current one."
6151   (interactive)
6152   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6153
6154 (defun gnus-summary-next-unread-same-subject ()
6155   "Select next unread article which has the same subject as current one."
6156   (interactive)
6157   (gnus-summary-next-article t (gnus-summary-article-subject)))
6158
6159 (defun gnus-summary-prev-unread-same-subject ()
6160   "Select previous unread article which has the same subject as current one."
6161   (interactive)
6162   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6163
6164 (defun gnus-summary-first-unread-article ()
6165   "Select the first unread article.
6166 Return nil if there are no unread articles."
6167   (interactive)
6168   (prog1
6169       (when (gnus-summary-first-subject t)
6170         (gnus-summary-show-thread)
6171         (gnus-summary-first-subject t)
6172         (gnus-summary-display-article (gnus-summary-article-number)))
6173     (gnus-summary-position-point)))
6174
6175 (defun gnus-summary-first-unread-subject ()
6176   "Place the point on the subject line of the first unread article.
6177 Return nil if there are no unread articles."
6178   (interactive)
6179   (prog1
6180       (when (gnus-summary-first-subject t)
6181         (gnus-summary-show-thread)
6182         (gnus-summary-first-subject t))
6183     (gnus-summary-position-point)))
6184
6185 (defun gnus-summary-first-article ()
6186   "Select the first article.
6187 Return nil if there are no articles."
6188   (interactive)
6189   (prog1
6190       (when (gnus-summary-first-subject)
6191         (gnus-summary-show-thread)
6192         (gnus-summary-first-subject)
6193         (gnus-summary-display-article (gnus-summary-article-number)))
6194     (gnus-summary-position-point)))
6195
6196 (defun gnus-summary-best-unread-article ()
6197   "Select the unread article with the highest score."
6198   (interactive)
6199   (let ((best -1000000)
6200         (data gnus-newsgroup-data)
6201         article score)
6202     (while data
6203       (and (gnus-data-unread-p (car data))
6204            (> (setq score
6205                     (gnus-summary-article-score (gnus-data-number (car data))))
6206               best)
6207            (setq best score
6208                  article (gnus-data-number (car data))))
6209       (setq data (cdr data)))
6210     (prog1
6211         (if article
6212             (gnus-summary-goto-article article)
6213           (error "No unread articles"))
6214       (gnus-summary-position-point))))
6215
6216 (defun gnus-summary-last-subject ()
6217   "Go to the last displayed subject line in the group."
6218   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6219     (when article
6220       (gnus-summary-goto-subject article))))
6221
6222 (defun gnus-summary-goto-article (article &optional all-headers force)
6223   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6224 If ALL-HEADERS is non-nil, no header lines are hidden.
6225 If FORCE, go to the article even if it isn't displayed.  If FORCE
6226 is a number, it is the line the article is to be displayed on."
6227   (interactive
6228    (list
6229     (completing-read
6230      "Article number or Message-ID: "
6231      (mapcar (lambda (number) (list (int-to-string number)))
6232              gnus-newsgroup-limit))
6233     current-prefix-arg
6234     t))
6235   (prog1
6236       (if (and (stringp article)
6237                (string-match "@" article))
6238           (gnus-summary-refer-article article)
6239         (when (stringp article)
6240           (setq article (string-to-number article)))
6241         (if (gnus-summary-goto-subject article force)
6242             (gnus-summary-display-article article all-headers)
6243           (gnus-message 4 "Couldn't go to article %s" article) nil))
6244     (gnus-summary-position-point)))
6245
6246 (defun gnus-summary-goto-last-article ()
6247   "Go to the previously read article."
6248   (interactive)
6249   (prog1
6250       (when gnus-last-article
6251         (gnus-summary-goto-article gnus-last-article nil t))
6252     (gnus-summary-position-point)))
6253
6254 (defun gnus-summary-pop-article (number)
6255   "Pop one article off the history and go to the previous.
6256 NUMBER articles will be popped off."
6257   (interactive "p")
6258   (let (to)
6259     (setq gnus-newsgroup-history
6260           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6261     (if to
6262         (gnus-summary-goto-article (car to) nil t)
6263       (error "Article history empty")))
6264   (gnus-summary-position-point))
6265
6266 ;; Summary commands and functions for limiting the summary buffer.
6267
6268 (defun gnus-summary-limit-to-articles (n)
6269   "Limit the summary buffer to the next N articles.
6270 If not given a prefix, use the process marked articles instead."
6271   (interactive "P")
6272   (prog1
6273       (let ((articles (gnus-summary-work-articles n)))
6274         (setq gnus-newsgroup-processable nil)
6275         (gnus-summary-limit articles))
6276     (gnus-summary-position-point)))
6277
6278 (defun gnus-summary-pop-limit (&optional total)
6279   "Restore the previous limit.
6280 If given a prefix, remove all limits."
6281   (interactive "P")
6282   (when total
6283     (setq gnus-newsgroup-limits
6284           (list (mapcar (lambda (h) (mail-header-number h))
6285                         gnus-newsgroup-headers))))
6286   (unless gnus-newsgroup-limits
6287     (error "No limit to pop"))
6288   (prog1
6289       (gnus-summary-limit nil 'pop)
6290     (gnus-summary-position-point)))
6291
6292 (defun gnus-summary-limit-to-subject (subject &optional header)
6293   "Limit the summary buffer to articles that have subjects that match a regexp."
6294   (interactive "sLimit to subject (regexp): ")
6295   (unless header
6296     (setq header "subject"))
6297   (when (not (equal "" subject))
6298     (prog1
6299         (let ((articles (gnus-summary-find-matching
6300                          (or header "subject") subject 'all)))
6301           (unless articles
6302             (error "Found no matches for \"%s\"" subject))
6303           (gnus-summary-limit articles))
6304       (gnus-summary-position-point))))
6305
6306 (defun gnus-summary-limit-to-author (from)
6307   "Limit the summary buffer to articles that have authors that match a regexp."
6308   (interactive "sLimit to author (regexp): ")
6309   (gnus-summary-limit-to-subject from "from"))
6310
6311 (defun gnus-summary-limit-to-age (age &optional younger-p)
6312   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6313 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6314 articles that are younger than AGE days."
6315   (interactive
6316    (let ((younger current-prefix-arg)
6317          (days-got nil)
6318          days)
6319      (while (not days-got)
6320        (setq days (if younger
6321                       (read-string "Limit to articles within (in days): ")
6322                     (read-string "Limit to articles old than (in days): ")))
6323        (when (> (length days) 0)
6324          (setq days (read days)))
6325        (if (numberp days)
6326            (setq days-got t)
6327          (message "Please enter a number.")
6328          (sleep-for 1)))
6329      (list days younger)))
6330   (prog1
6331       (let ((data gnus-newsgroup-data)
6332             (cutoff (days-to-time age))
6333             articles d date is-younger)
6334         (while (setq d (pop data))
6335           (when (and (vectorp (gnus-data-header d))
6336                      (setq date (mail-header-date (gnus-data-header d))))
6337             (setq is-younger (time-less-p
6338                               (time-since (condition-case ()
6339                                               (date-to-time date)
6340                                             (error '(0 0))))
6341                               cutoff))
6342             (when (if younger-p
6343                       is-younger
6344                     (not is-younger))
6345               (push (gnus-data-number d) articles))))
6346         (gnus-summary-limit (nreverse articles)))
6347     (gnus-summary-position-point)))
6348
6349 (defun gnus-summary-limit-to-extra (header regexp)
6350   "Limit the summary buffer to articles that match an 'extra' header."
6351   (interactive
6352    (let ((header
6353           (intern
6354            (gnus-completing-read
6355             (symbol-name (car gnus-extra-headers))      
6356             "Limit extra header:"       
6357             (mapcar (lambda (x) 
6358                       (cons (symbol-name x) x))
6359                     gnus-extra-headers)
6360             nil                 
6361             t))))
6362      (list header
6363            (read-string (format "Limit to header %s (regexp): " header)))))
6364   (when (not (equal "" regexp))
6365     (prog1
6366         (let ((articles (gnus-summary-find-matching
6367                          (cons 'extra header) regexp 'all)))
6368           (unless articles
6369             (error "Found no matches for \"%s\"" regexp))
6370           (gnus-summary-limit articles))
6371       (gnus-summary-position-point))))
6372
6373 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6374 (make-obsolete
6375  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6376
6377 (defun gnus-summary-limit-to-unread (&optional all)
6378   "Limit the summary buffer to articles that are not marked as read.
6379 If ALL is non-nil, limit strictly to unread articles."
6380   (interactive "P")
6381   (if all
6382       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6383     (gnus-summary-limit-to-marks
6384      ;; Concat all the marks that say that an article is read and have
6385      ;; those removed.
6386      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6387            gnus-killed-mark gnus-kill-file-mark
6388            gnus-low-score-mark gnus-expirable-mark
6389            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6390            gnus-duplicate-mark gnus-souped-mark)
6391      'reverse)))
6392
6393 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6394 (make-obsolete 'gnus-summary-delete-marked-with
6395                'gnus-summary-limit-exlude-marks)
6396
6397 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6398   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6399 If REVERSE, limit the summary buffer to articles that are marked
6400 with MARKS.  MARKS can either be a string of marks or a list of marks.
6401 Returns how many articles were removed."
6402   (interactive "sMarks: ")
6403   (gnus-summary-limit-to-marks marks t))
6404
6405 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6406   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6407 If REVERSE (the prefix), limit the summary buffer to articles that are
6408 not marked with MARKS.  MARKS can either be a string of marks or a
6409 list of marks.
6410 Returns how many articles were removed."
6411   (interactive "sMarks: \nP")
6412   (prog1
6413       (let ((data gnus-newsgroup-data)
6414             (marks (if (listp marks) marks
6415                      (append marks nil))) ; Transform to list.
6416             articles)
6417         (while data
6418           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6419                   (memq (gnus-data-mark (car data)) marks))
6420             (push (gnus-data-number (car data)) articles))
6421           (setq data (cdr data)))
6422         (gnus-summary-limit articles))
6423     (gnus-summary-position-point)))
6424
6425 (defun gnus-summary-limit-to-score (&optional score)
6426   "Limit to articles with score at or above SCORE."
6427   (interactive "P")
6428   (setq score (if score
6429                   (prefix-numeric-value score)
6430                 (or gnus-summary-default-score 0)))
6431   (let ((data gnus-newsgroup-data)
6432         articles)
6433     (while data
6434       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6435                 score)
6436         (push (gnus-data-number (car data)) articles))
6437       (setq data (cdr data)))
6438     (prog1
6439         (gnus-summary-limit articles)
6440       (gnus-summary-position-point))))
6441
6442 (defun gnus-summary-limit-include-thread (id)
6443   "Display all the hidden articles that in the current thread."
6444   (interactive (list (mail-header-id (gnus-summary-article-header))))
6445   (let ((articles (gnus-articles-in-thread
6446                    (gnus-id-to-thread (gnus-root-id id)))))
6447     (prog1
6448         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6449       (gnus-summary-position-point))))
6450
6451 (defun gnus-summary-limit-include-dormant ()
6452   "Display all the hidden articles that are marked as dormant.
6453 Note that this command only works on a subset of the articles currently
6454 fetched for this group."
6455   (interactive)
6456   (unless gnus-newsgroup-dormant
6457     (error "There are no dormant articles in this group"))
6458   (prog1
6459       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6460     (gnus-summary-position-point)))
6461
6462 (defun gnus-summary-limit-exclude-dormant ()
6463   "Hide all dormant articles."
6464   (interactive)
6465   (prog1
6466       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6467     (gnus-summary-position-point)))
6468
6469 (defun gnus-summary-limit-exclude-childless-dormant ()
6470   "Hide all dormant articles that have no children."
6471   (interactive)
6472   (let ((data (gnus-data-list t))
6473         articles d children)
6474     ;; Find all articles that are either not dormant or have
6475     ;; children.
6476     (while (setq d (pop data))
6477       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6478                 (and (setq children
6479                            (gnus-article-children (gnus-data-number d)))
6480                      (let (found)
6481                        (while children
6482                          (when (memq (car children) articles)
6483                            (setq children nil
6484                                  found t))
6485                          (pop children))
6486                        found)))
6487         (push (gnus-data-number d) articles)))
6488     ;; Do the limiting.
6489     (prog1
6490         (gnus-summary-limit articles)
6491       (gnus-summary-position-point))))
6492
6493 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6494   "Mark all unread excluded articles as read.
6495 If ALL, mark even excluded ticked and dormants as read."
6496   (interactive "P")
6497   (let ((articles (gnus-sorted-complement
6498                    (sort
6499                     (mapcar (lambda (h) (mail-header-number h))
6500                             gnus-newsgroup-headers)
6501                     '<)
6502                    (sort gnus-newsgroup-limit '<)))
6503         article)
6504     (setq gnus-newsgroup-unreads
6505           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6506     (if all
6507         (setq gnus-newsgroup-dormant nil
6508               gnus-newsgroup-marked nil
6509               gnus-newsgroup-reads
6510               (nconc
6511                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6512                gnus-newsgroup-reads))
6513       (while (setq article (pop articles))
6514         (unless (or (memq article gnus-newsgroup-dormant)
6515                     (memq article gnus-newsgroup-marked))
6516           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6517
6518 (defun gnus-summary-limit (articles &optional pop)
6519   (if pop
6520       ;; We pop the previous limit off the stack and use that.
6521       (setq articles (car gnus-newsgroup-limits)
6522             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6523     ;; We use the new limit, so we push the old limit on the stack.
6524     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6525   ;; Set the limit.
6526   (setq gnus-newsgroup-limit articles)
6527   (let ((total (length gnus-newsgroup-data))
6528         (data (gnus-data-find-list (gnus-summary-article-number)))
6529         (gnus-summary-mark-below nil)   ; Inhibit this.
6530         found)
6531     ;; This will do all the work of generating the new summary buffer
6532     ;; according to the new limit.
6533     (gnus-summary-prepare)
6534     ;; Hide any threads, possibly.
6535     (and gnus-show-threads
6536          gnus-thread-hide-subtree
6537          (gnus-summary-hide-all-threads))
6538     ;; Try to return to the article you were at, or one in the
6539     ;; neighborhood.
6540     (when data
6541       ;; We try to find some article after the current one.
6542       (while data
6543         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6544           (setq data nil
6545                 found t))
6546         (setq data (cdr data))))
6547     (unless found
6548       ;; If there is no data, that means that we were after the last
6549       ;; article.  The same goes when we can't find any articles
6550       ;; after the current one.
6551       (goto-char (point-max))
6552       (gnus-summary-find-prev))
6553     (gnus-set-mode-line 'summary)
6554     ;; We return how many articles were removed from the summary
6555     ;; buffer as a result of the new limit.
6556     (- total (length gnus-newsgroup-data))))
6557
6558 (defsubst gnus-invisible-cut-children (threads)
6559   (let ((num 0))
6560     (while threads
6561       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6562         (incf num))
6563       (pop threads))
6564     (< num 2)))
6565
6566 (defsubst gnus-cut-thread (thread)
6567   "Go forwards in the thread until we find an article that we want to display."
6568   (when (or (eq gnus-fetch-old-headers 'some)
6569             (eq gnus-fetch-old-headers 'invisible)
6570             (numberp gnus-fetch-old-headers)
6571             (eq gnus-build-sparse-threads 'some)
6572             (eq gnus-build-sparse-threads 'more))
6573     ;; Deal with old-fetched headers and sparse threads.
6574     (while (and
6575             thread
6576             (or
6577              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6578              (gnus-summary-article-ancient-p
6579               (mail-header-number (car thread))))
6580             (if (or (<= (length (cdr thread)) 1)
6581                     (eq gnus-fetch-old-headers 'invisible))
6582                 (setq gnus-newsgroup-limit
6583                       (delq (mail-header-number (car thread))
6584                             gnus-newsgroup-limit)
6585                       thread (cadr thread))
6586               (when (gnus-invisible-cut-children (cdr thread))
6587                 (let ((th (cdr thread)))
6588                   (while th
6589                     (if (memq (mail-header-number (caar th))
6590                               gnus-newsgroup-limit)
6591                         (setq thread (car th)
6592                               th nil)
6593                       (setq th (cdr th))))))))))
6594   thread)
6595
6596 (defun gnus-cut-threads (threads)
6597   "Cut off all uninteresting articles from the beginning of threads."
6598   (when (or (eq gnus-fetch-old-headers 'some)
6599             (eq gnus-fetch-old-headers 'invisible)
6600             (numberp gnus-fetch-old-headers)
6601             (eq gnus-build-sparse-threads 'some)
6602             (eq gnus-build-sparse-threads 'more))
6603     (let ((th threads))
6604       (while th
6605         (setcar th (gnus-cut-thread (car th)))
6606         (setq th (cdr th)))))
6607   ;; Remove nixed out threads.
6608   (delq nil threads))
6609
6610 (defun gnus-summary-initial-limit (&optional show-if-empty)
6611   "Figure out what the initial limit is supposed to be on group entry.
6612 This entails weeding out unwanted dormants, low-scored articles,
6613 fetch-old-headers verbiage, and so on."
6614   ;; Most groups have nothing to remove.
6615   (if (or gnus-inhibit-limiting
6616           (and (null gnus-newsgroup-dormant)
6617                (not (eq gnus-fetch-old-headers 'some))
6618                (not (numberp gnus-fetch-old-headers))
6619                (not (eq gnus-fetch-old-headers 'invisible))
6620                (null gnus-summary-expunge-below)
6621                (not (eq gnus-build-sparse-threads 'some))
6622                (not (eq gnus-build-sparse-threads 'more))
6623                (null gnus-thread-expunge-below)
6624                (not gnus-use-nocem)))
6625       ()                                ; Do nothing.
6626     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6627     (setq gnus-newsgroup-limit nil)
6628     (mapatoms
6629      (lambda (node)
6630        (unless (car (symbol-value node))
6631          ;; These threads have no parents -- they are roots.
6632          (let ((nodes (cdr (symbol-value node)))
6633                thread)
6634            (while nodes
6635              (if (and gnus-thread-expunge-below
6636                       (< (gnus-thread-total-score (car nodes))
6637                          gnus-thread-expunge-below))
6638                  (gnus-expunge-thread (pop nodes))
6639                (setq thread (pop nodes))
6640                (gnus-summary-limit-children thread))))))
6641      gnus-newsgroup-dependencies)
6642     ;; If this limitation resulted in an empty group, we might
6643     ;; pop the previous limit and use it instead.
6644     (when (and (not gnus-newsgroup-limit)
6645                show-if-empty)
6646       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6647     gnus-newsgroup-limit))
6648
6649 (defun gnus-summary-limit-children (thread)
6650   "Return 1 if this subthread is visible and 0 if it is not."
6651   ;; First we get the number of visible children to this thread.  This
6652   ;; is done by recursing down the thread using this function, so this
6653   ;; will really go down to a leaf article first, before slowly
6654   ;; working its way up towards the root.
6655   (when thread
6656     (let ((children
6657            (if (cdr thread)
6658                (apply '+ (mapcar 'gnus-summary-limit-children
6659                                  (cdr thread)))
6660              0))
6661           (number (mail-header-number (car thread)))
6662           score)
6663       (if (and
6664            (not (memq number gnus-newsgroup-marked))
6665            (or
6666             ;; If this article is dormant and has absolutely no visible
6667             ;; children, then this article isn't visible.
6668             (and (memq number gnus-newsgroup-dormant)
6669                  (zerop children))
6670             ;; If this is "fetch-old-headered" and there is no
6671             ;; visible children, then we don't want this article.
6672             (and (or (eq gnus-fetch-old-headers 'some)
6673                      (numberp gnus-fetch-old-headers))
6674                  (gnus-summary-article-ancient-p number)
6675                  (zerop children))
6676             ;; If this is "fetch-old-headered" and `invisible', then
6677             ;; we don't want this article.
6678             (and (eq gnus-fetch-old-headers 'invisible)
6679                  (gnus-summary-article-ancient-p number))
6680             ;; If this is a sparsely inserted article with no children,
6681             ;; we don't want it.
6682             (and (eq gnus-build-sparse-threads 'some)
6683                  (gnus-summary-article-sparse-p number)
6684                  (zerop children))
6685             ;; If we use expunging, and this article is really
6686             ;; low-scored, then we don't want this article.
6687             (when (and gnus-summary-expunge-below
6688                        (< (setq score
6689                                 (or (cdr (assq number gnus-newsgroup-scored))
6690                                     gnus-summary-default-score))
6691                           gnus-summary-expunge-below))
6692               ;; We increase the expunge-tally here, but that has
6693               ;; nothing to do with the limits, really.
6694               (incf gnus-newsgroup-expunged-tally)
6695               ;; We also mark as read here, if that's wanted.
6696               (when (and gnus-summary-mark-below
6697                          (< score gnus-summary-mark-below))
6698                 (setq gnus-newsgroup-unreads
6699                       (delq number gnus-newsgroup-unreads))
6700                 (if gnus-newsgroup-auto-expire
6701                     (push number gnus-newsgroup-expirable)
6702                   (push (cons number gnus-low-score-mark)
6703                         gnus-newsgroup-reads)))
6704               t)
6705             ;; Check NoCeM things.
6706             (if (and gnus-use-nocem
6707                      (gnus-nocem-unwanted-article-p
6708                       (mail-header-id (car thread))))
6709                 (progn
6710                   (setq gnus-newsgroup-unreads
6711                         (delq number gnus-newsgroup-unreads))
6712                   t))))
6713           ;; Nope, invisible article.
6714           0
6715         ;; Ok, this article is to be visible, so we add it to the limit
6716         ;; and return 1.
6717         (push number gnus-newsgroup-limit)
6718         1))))
6719
6720 (defun gnus-expunge-thread (thread)
6721   "Mark all articles in THREAD as read."
6722   (let* ((number (mail-header-number (car thread))))
6723     (incf gnus-newsgroup-expunged-tally)
6724     ;; We also mark as read here, if that's wanted.
6725     (setq gnus-newsgroup-unreads
6726           (delq number gnus-newsgroup-unreads))
6727     (if gnus-newsgroup-auto-expire
6728         (push number gnus-newsgroup-expirable)
6729       (push (cons number gnus-low-score-mark)
6730             gnus-newsgroup-reads)))
6731   ;; Go recursively through all subthreads.
6732   (mapcar 'gnus-expunge-thread (cdr thread)))
6733
6734 ;; Summary article oriented commands
6735
6736 (defun gnus-summary-refer-parent-article (n)
6737   "Refer parent article N times.
6738 If N is negative, go to ancestor -N instead.
6739 The difference between N and the number of articles fetched is returned."
6740   (interactive "p")
6741   (let ((skip 1)
6742         error header ref)
6743     (when (not (natnump n))
6744       (setq skip (abs n)
6745             n 1))
6746     (while (and (> n 0)
6747                 (not error))
6748       (setq header (gnus-summary-article-header))
6749       (if (and (eq (mail-header-number header)
6750                    (cdr gnus-article-current))
6751                (equal gnus-newsgroup-name
6752                       (car gnus-article-current)))
6753           ;; If we try to find the parent of the currently
6754           ;; displayed article, then we take a look at the actual
6755           ;; References header, since this is slightly more
6756           ;; reliable than the References field we got from the
6757           ;; server.
6758           (save-excursion
6759             (set-buffer gnus-original-article-buffer)
6760             (nnheader-narrow-to-headers)
6761             (unless (setq ref (message-fetch-field "references"))
6762               (setq ref (message-fetch-field "in-reply-to")))
6763             (widen))
6764         (setq ref
6765               ;; It's not the current article, so we take a bet on
6766               ;; the value we got from the server.
6767               (mail-header-references header)))
6768       (if (and ref
6769                (not (equal ref "")))
6770           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6771             (gnus-message 1 "Couldn't find parent"))
6772         (gnus-message 1 "No references in article %d"
6773                       (gnus-summary-article-number))
6774         (setq error t))
6775       (decf n))
6776     (gnus-summary-position-point)
6777     n))
6778
6779 (defun gnus-summary-refer-references ()
6780   "Fetch all articles mentioned in the References header.
6781 Return the number of articles fetched."
6782   (interactive)
6783   (let ((ref (mail-header-references (gnus-summary-article-header)))
6784         (current (gnus-summary-article-number))
6785         (n 0))
6786     (if (or (not ref)
6787             (equal ref ""))
6788         (error "No References in the current article")
6789       ;; For each Message-ID in the References header...
6790       (while (string-match "<[^>]*>" ref)
6791         (incf n)
6792         ;; ... fetch that article.
6793         (gnus-summary-refer-article
6794          (prog1 (match-string 0 ref)
6795            (setq ref (substring ref (match-end 0))))))
6796       (gnus-summary-goto-subject current)
6797       (gnus-summary-position-point)
6798       n)))
6799
6800 (defun gnus-summary-refer-thread (&optional limit)
6801   "Fetch all articles in the current thread.
6802 If LIMIT (the numerical prefix), fetch that many old headers instead
6803 of what's specified by the `gnus-refer-thread-limit' variable."
6804   (interactive "P")
6805   (let ((id (mail-header-id (gnus-summary-article-header)))
6806         (limit (if limit (prefix-numeric-value limit)
6807                  gnus-refer-thread-limit)))
6808     ;; We want to fetch LIMIT *old* headers, but we also have to
6809     ;; re-fetch all the headers in the current buffer, because many of
6810     ;; them may be undisplayed.  So we adjust LIMIT.
6811     (when (numberp limit)
6812       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6813     (unless (eq gnus-fetch-old-headers 'invisible)
6814       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6815       ;; Retrieve the headers and read them in.
6816       (if (eq (gnus-retrieve-headers
6817                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6818               'nov)
6819           (gnus-build-all-threads)
6820         (error "Can't fetch thread from backends that don't support NOV"))
6821       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6822     (gnus-summary-limit-include-thread id)))
6823
6824 (defun gnus-summary-refer-article (message-id)
6825   "Fetch an article specified by MESSAGE-ID."
6826   (interactive "sMessage-ID: ")
6827   (when (and (stringp message-id)
6828              (not (zerop (length message-id))))
6829     ;; Construct the correct Message-ID if necessary.
6830     ;; Suggested by tale@pawl.rpi.edu.
6831     (unless (string-match "^<" message-id)
6832       (setq message-id (concat "<" message-id)))
6833     (unless (string-match ">$" message-id)
6834       (setq message-id (concat message-id ">")))
6835     (let* ((header (gnus-id-to-header message-id))
6836            (sparse (and header
6837                         (gnus-summary-article-sparse-p
6838                          (mail-header-number header))
6839                         (memq (mail-header-number header)
6840                               gnus-newsgroup-limit)))
6841            number)
6842       (cond
6843        ;; If the article is present in the buffer we just go to it.
6844        ((and header
6845              (or (not (gnus-summary-article-sparse-p
6846                        (mail-header-number header)))
6847                  sparse))
6848         (prog1
6849             (gnus-summary-goto-article
6850              (mail-header-number header) nil t)
6851           (when sparse
6852             (gnus-summary-update-article (mail-header-number header)))))
6853        (t
6854         ;; We fetch the article.
6855         (catch 'found
6856           (dolist (gnus-override-method (gnus-refer-article-methods))
6857             (gnus-check-server gnus-override-method)
6858             ;; Fetch the header, and display the article.
6859             (when (setq number (gnus-summary-insert-subject message-id))
6860               (gnus-summary-select-article nil nil nil number)
6861               (throw 'found t)))
6862           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6863
6864 (defun gnus-refer-article-methods ()
6865   "Return a list of referrable methods."
6866   (cond
6867    ;; No method, so we default to current and native.
6868    ((null gnus-refer-article-method)
6869     (list gnus-current-select-method gnus-select-method))
6870    ;; Current.
6871    ((eq 'current gnus-refer-article-method)
6872     (list gnus-current-select-method))
6873    ;; List of select methods.
6874    ((not (stringp (cadr gnus-refer-article-method)))
6875     (let (out)
6876       (dolist (method gnus-refer-article-method)
6877         (push (if (eq 'current method)
6878                   gnus-current-select-method
6879                 method)
6880               out))
6881       (nreverse out)))
6882    ;; One single select method.
6883    (t
6884     (list gnus-refer-article-method))))
6885
6886 (defun gnus-summary-edit-parameters ()
6887   "Edit the group parameters of the current group."
6888   (interactive)
6889   (gnus-group-edit-group gnus-newsgroup-name 'params))
6890
6891 (defun gnus-summary-customize-parameters ()
6892   "Customize the group parameters of the current group."
6893   (interactive)
6894   (gnus-group-customize gnus-newsgroup-name))
6895
6896 (defun gnus-summary-enter-digest-group (&optional force)
6897   "Enter an nndoc group based on the current article.
6898 If FORCE, force a digest interpretation.  If not, try
6899 to guess what the document format is."
6900   (interactive "P")
6901   (let ((conf gnus-current-window-configuration))
6902     (save-excursion
6903       (gnus-summary-select-article))
6904     (setq gnus-current-window-configuration conf)
6905     (let* ((name (format "%s-%d"
6906                          (gnus-group-prefixed-name
6907                           gnus-newsgroup-name (list 'nndoc ""))
6908                          (save-excursion
6909                            (set-buffer gnus-summary-buffer)
6910                            gnus-current-article)))
6911            (ogroup gnus-newsgroup-name)
6912            (params (append (gnus-info-params (gnus-get-info ogroup))
6913                            (list (cons 'to-group ogroup))
6914                            (list (cons 'save-article-group ogroup))))
6915            (case-fold-search t)
6916            (buf (current-buffer))
6917            dig to-address)
6918       (save-excursion
6919         (set-buffer gnus-original-article-buffer)
6920         ;; Have the digest group inherit the main mail address of
6921         ;; the parent article.
6922         (when (setq to-address (or (message-fetch-field "reply-to")
6923                                    (message-fetch-field "from")))
6924           (setq params (append (list (cons 'to-address to-address)))))
6925         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6926         (insert-buffer-substring gnus-original-article-buffer)
6927         ;; Remove lines that may lead nndoc to misinterpret the
6928         ;; document type.
6929         (narrow-to-region
6930          (goto-char (point-min))
6931          (or (search-forward "\n\n" nil t) (point)))
6932         (goto-char (point-min))
6933         (delete-matching-lines "^Path:\\|^From ")
6934         (widen))
6935       (unwind-protect
6936           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6937                     (gnus-newsgroup-ephemeral-ignored-charsets
6938                      gnus-newsgroup-ignored-charsets))
6939                 (gnus-group-read-ephemeral-group
6940                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6941                               (nndoc-article-type
6942                                ,(if force 'mbox 'guess))) t))
6943               ;; Make all postings to this group go to the parent group.
6944               (nconc (gnus-info-params (gnus-get-info name))
6945                      params)
6946             ;; Couldn't select this doc group.
6947             (switch-to-buffer buf)
6948             (gnus-set-global-variables)
6949             (gnus-configure-windows 'summary)
6950             (gnus-message 3 "Article couldn't be entered?"))
6951         (kill-buffer dig)))))
6952
6953 (defun gnus-summary-read-document (n)
6954   "Open a new group based on the current article(s).
6955 This will allow you to read digests and other similar
6956 documents as newsgroups.
6957 Obeys the standard process/prefix convention."
6958   (interactive "P")
6959   (let* ((articles (gnus-summary-work-articles n))
6960          (ogroup gnus-newsgroup-name)
6961          (params (append (gnus-info-params (gnus-get-info ogroup))
6962                          (list (cons 'to-group ogroup))))
6963          article group egroup groups vgroup)
6964     (while (setq article (pop articles))
6965       (setq group (format "%s-%d" gnus-newsgroup-name article))
6966       (gnus-summary-remove-process-mark article)
6967       (when (gnus-summary-display-article article)
6968         (save-excursion
6969           (with-temp-buffer
6970             (insert-buffer-substring gnus-original-article-buffer)
6971             ;; Remove some headers that may lead nndoc to make
6972             ;; the wrong guess.
6973             (message-narrow-to-head)
6974             (goto-char (point-min))
6975             (delete-matching-lines "^\\(Path\\):\\|^From ")
6976             (widen)
6977             (if (setq egroup
6978                       (gnus-group-read-ephemeral-group
6979                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6980                                      (nndoc-article-type guess))
6981                        t nil t))
6982                 (progn
6983                   ;; Make all postings to this group go to the parent group.
6984                   (nconc (gnus-info-params (gnus-get-info egroup))
6985                          params)
6986                   (push egroup groups))
6987               ;; Couldn't select this doc group.
6988               (gnus-error 3 "Article couldn't be entered"))))))
6989     ;; Now we have selected all the documents.
6990     (cond
6991      ((not groups)
6992       (error "None of the articles could be interpreted as documents"))
6993      ((gnus-group-read-ephemeral-group
6994        (setq vgroup (format
6995                      "nnvirtual:%s-%s" gnus-newsgroup-name
6996                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6997        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6998        t
6999        (cons (current-buffer) 'summary)))
7000      (t
7001       (error "Couldn't select virtual nndoc group")))))
7002
7003 (defun gnus-summary-isearch-article (&optional regexp-p)
7004   "Do incremental search forward on the current article.
7005 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7006   (interactive "P")
7007   (let* ((gnus-inhibit-treatment t)
7008          (old (gnus-summary-select-article)))
7009     (gnus-configure-windows 'article)
7010     (gnus-eval-in-buffer-window gnus-article-buffer
7011       (save-restriction
7012         (widen)
7013         (when (eq 'old old)
7014           (gnus-article-show-all-headers))
7015         (goto-char (point-min))
7016         (isearch-forward regexp-p)))))
7017
7018 (defun gnus-summary-search-article-forward (regexp &optional backward)
7019   "Search for an article containing REGEXP forward.
7020 If BACKWARD, search backward instead."
7021   (interactive
7022    (list (read-string
7023           (format "Search article %s (regexp%s): "
7024                   (if current-prefix-arg "backward" "forward")
7025                   (if gnus-last-search-regexp
7026                       (concat ", default " gnus-last-search-regexp)
7027                     "")))
7028          current-prefix-arg))
7029   (if (string-equal regexp "")
7030       (setq regexp (or gnus-last-search-regexp ""))
7031     (setq gnus-last-search-regexp regexp))
7032   (if (gnus-summary-search-article regexp backward)
7033       (gnus-summary-show-thread)
7034     (error "Search failed: \"%s\"" regexp)))
7035
7036 (defun gnus-summary-search-article-backward (regexp)
7037   "Search for an article containing REGEXP backward."
7038   (interactive
7039    (list (read-string
7040           (format "Search article backward (regexp%s): "
7041                   (if gnus-last-search-regexp
7042                       (concat ", default " gnus-last-search-regexp)
7043                     "")))))
7044   (gnus-summary-search-article-forward regexp 'backward))
7045
7046 (eval-when-compile
7047   (defmacro gnus-summary-search-article-position-point (regexp backward)
7048     "Dehighlight the last matched text and goto the beginning position."
7049     (` (if (and gnus-summary-search-article-matched-data
7050                 (let ((text (caddr gnus-summary-search-article-matched-data))
7051                       (inhibit-read-only t)
7052                       buffer-read-only)
7053                   (delete-region
7054                    (goto-char (car gnus-summary-search-article-matched-data))
7055                    (cadr gnus-summary-search-article-matched-data))
7056                   (insert text)
7057                   (string-match (, regexp) text)))
7058            (if (, backward) (beginning-of-line) (end-of-line))
7059          (goto-char (if (, backward) (point-max) (point-min))))))
7060
7061   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7062     "Place point where X-Face image is displayed."
7063     (if (featurep 'xemacs)
7064         (` (let ((end (if (search-forward "\n\n" nil t)
7065                           (goto-char (1- (point)))
7066                         (point-min)))
7067                  extent)
7068              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7069              (unless (and (re-search-forward "^From:" end t)
7070                           (setq extent (extent-at (point)))
7071                           (extent-begin-glyph extent))
7072                (goto-char (, opoint)))))
7073       (` (let ((end (if (search-forward "\n\n" nil t)
7074                         (goto-char (1- (point)))
7075                       (point-min))))
7076            (goto-char
7077             (or (text-property-any (or (search-backward "\n\n" nil t)
7078                                        (point-min))
7079                                    end 'x-face-mule-bitmap-image t)
7080                 (, opoint)))))))
7081
7082   (defmacro gnus-summary-search-article-highlight-matched-text
7083     (backward treated x-face)
7084     "Highlight matched text in the function `gnus-summary-search-article'."
7085     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7086              (end (set-marker (make-marker) (match-end 0)))
7087              (inhibit-read-only t)
7088              buffer-read-only)
7089          (unless treated
7090            (let ((,@
7091                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7092                     (mapcar
7093                      (lambda (item) (setq items (delq item items)))
7094                      '(gnus-treat-buttonize
7095                        gnus-treat-fill-article
7096                        gnus-treat-fill-long-lines
7097                        gnus-treat-emphasize
7098                        gnus-treat-highlight-headers
7099                        gnus-treat-highlight-citation
7100                        gnus-treat-highlight-signature
7101                        gnus-treat-overstrike
7102                        gnus-treat-display-xface
7103                        gnus-treat-buttonize-head
7104                        gnus-treat-decode-article-as-default-mime-charset))
7105                     (static-if (featurep 'xemacs)
7106                         items
7107                       (cons '(x-face-mule-delete-x-face-field
7108                               (quote never))
7109                             items))))
7110                  (gnus-treat-display-xface
7111                   (when (, x-face) gnus-treat-display-xface)))
7112              (gnus-article-prepare-mime-display)))
7113          (goto-char (if (, backward) start end))
7114          (when (, x-face)
7115            (gnus-summary-search-article-highlight-goto-x-face (point)))
7116          (setq gnus-summary-search-article-matched-data
7117                (list start end (buffer-substring start end)))
7118          (unless (eq start end);; matched text has been deleted. :-<
7119            (put-text-property start end 'face
7120                               (or (find-face 'isearch)
7121                                   'secondary-selection))))))
7122   )
7123
7124 (defun gnus-summary-search-article (regexp &optional backward)
7125   "Search for an article containing REGEXP.
7126 Optional argument BACKWARD means do search for backward.
7127 `gnus-select-article-hook' is not called during the search."
7128   ;; We have to require this here to make sure that the following
7129   ;; dynamic binding isn't shadowed by autoloading.
7130   (require 'gnus-async)
7131   (require 'gnus-art)
7132   (let ((gnus-select-article-hook nil)  ;Disable hook.
7133         (gnus-article-display-hook nil)
7134         (gnus-article-prepare-hook nil)
7135         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7136         (gnus-use-article-prefetch nil)
7137         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7138         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7139         (sum (current-buffer))
7140         (found nil)
7141         point treated)
7142     (gnus-save-hidden-threads
7143       (static-if (featurep 'xemacs)
7144           (let ((gnus-inhibit-treatment t))
7145             (setq treated (eq 'old (gnus-summary-select-article)))
7146             (when (and treated
7147                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7148                                  (window-live-p (get-buffer-window
7149                                                  gnus-article-buffer t)))))
7150               (gnus-summary-select-article nil t)
7151               (setq treated nil)))
7152         (let ((gnus-inhibit-treatment t)
7153               (x-face-mule-delete-x-face-field 'never))
7154           (setq treated (eq 'old (gnus-summary-select-article)))
7155           (when (and treated
7156                      (not
7157                       (and (gnus-buffer-live-p gnus-article-buffer)
7158                            (window-live-p (get-buffer-window
7159                                            gnus-article-buffer t))
7160                            (or (not (string-match "^\\^X-Face:" regexp))
7161                                (with-current-buffer gnus-article-buffer
7162                                  gnus-summary-search-article-matched-data)))))
7163             (gnus-summary-select-article nil t)
7164             (setq treated nil))))
7165       (set-buffer gnus-article-buffer)
7166       (widen)
7167       (if treated
7168           (progn
7169             (gnus-article-show-all-headers)
7170             (gnus-summary-search-article-position-point regexp backward))
7171         (goto-char (if backward (point-max) (point-min))))
7172       (while (not found)
7173         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7174         (if (if backward
7175                 (re-search-backward regexp nil t)
7176               (re-search-forward regexp nil t))
7177             ;; We found the regexp.
7178             (progn
7179               (gnus-summary-search-article-highlight-matched-text
7180                backward treated (string-match "^\\^X-Face:" regexp))
7181               (setq found 'found)
7182               (forward-line
7183                (/ (- 2 (window-height
7184                         (get-buffer-window gnus-article-buffer t)))
7185                   2))
7186               (set-window-start
7187                (get-buffer-window (current-buffer))
7188                (point))
7189               (set-buffer sum)
7190               (setq point (point)))
7191           ;; We didn't find it, so we go to the next article.
7192           (set-buffer sum)
7193           (setq found 'not)
7194           (while (eq found 'not)
7195             (if (not (if backward (gnus-summary-find-prev)
7196                        (gnus-summary-find-next)))
7197                 ;; No more articles.
7198                 (setq found t)
7199               ;; Select the next article and adjust point.
7200               (unless (gnus-summary-article-sparse-p
7201                        (gnus-summary-article-number))
7202                 (setq found nil)
7203                 (let ((gnus-inhibit-treatment t))
7204                   (gnus-summary-select-article))
7205                 (setq treated nil)
7206                 (set-buffer gnus-article-buffer)
7207                 (widen)
7208                 (goto-char (if backward (point-max) (point-min))))))))
7209       (gnus-message 7 ""))
7210     ;; Return whether we found the regexp.
7211     (when (eq found 'found)
7212       (goto-char point)
7213       (gnus-summary-show-thread)
7214       (gnus-summary-goto-subject gnus-current-article)
7215       (gnus-summary-position-point)
7216       t)))
7217
7218 (defun gnus-summary-find-matching (header regexp &optional backward unread
7219                                           not-case-fold)
7220   "Return a list of all articles that match REGEXP on HEADER.
7221 The search stars on the current article and goes forwards unless
7222 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7223 If UNREAD is non-nil, only unread articles will
7224 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7225 in the comparisons."
7226   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7227                 (gnus-data-find-list
7228                  (gnus-summary-article-number) (gnus-data-list backward))))
7229         (case-fold-search (not not-case-fold))
7230         articles d func)
7231     (if (consp header)
7232         (if (eq (car header) 'extra)
7233             (setq func
7234                   `(lambda (h)
7235                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7236                          "")))
7237           (error "%s is an invalid header" header))
7238       (unless (fboundp (intern (concat "mail-header-" header)))
7239         (error "%s is not a valid header" header))
7240       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7241     (while data
7242       (setq d (car data))
7243       (and (or (not unread)             ; We want all articles...
7244                (gnus-data-unread-p d))  ; Or just unreads.
7245            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7246            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7247            (push (gnus-data-number d) articles)) ; Success!
7248       (setq data (cdr data)))
7249     (nreverse articles)))
7250
7251 (defun gnus-summary-execute-command (header regexp command &optional backward)
7252   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7253 If HEADER is an empty string (or nil), the match is done on the entire
7254 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7255   (interactive
7256    (list (let ((completion-ignore-case t))
7257            (completing-read
7258             "Header name: "
7259             (mapcar (lambda (string) (list string))
7260                     '("Number" "Subject" "From" "Lines" "Date"
7261                       "Message-ID" "Xref" "References" "Body"))
7262             nil 'require-match))
7263          (read-string "Regexp: ")
7264          (read-key-sequence "Command: ")
7265          current-prefix-arg))
7266   (when (equal header "Body")
7267     (setq header ""))
7268   ;; Hidden thread subtrees must be searched as well.
7269   (gnus-summary-show-all-threads)
7270   ;; We don't want to change current point nor window configuration.
7271   (save-excursion
7272     (save-window-excursion
7273       (gnus-message 6 "Executing %s..." (key-description command))
7274       ;; We'd like to execute COMMAND interactively so as to give arguments.
7275       (gnus-execute header regexp
7276                     `(call-interactively ',(key-binding command))
7277                     backward)
7278       (gnus-message 6 "Executing %s...done" (key-description command)))))
7279
7280 (defun gnus-summary-beginning-of-article ()
7281   "Scroll the article back to the beginning."
7282   (interactive)
7283   (gnus-summary-select-article)
7284   (gnus-configure-windows 'article)
7285   (gnus-eval-in-buffer-window gnus-article-buffer
7286     (widen)
7287     (goto-char (point-min))
7288     (when gnus-page-broken
7289       (gnus-narrow-to-page))))
7290
7291 (defun gnus-summary-end-of-article ()
7292   "Scroll to the end of the article."
7293   (interactive)
7294   (gnus-summary-select-article)
7295   (gnus-configure-windows 'article)
7296   (gnus-eval-in-buffer-window gnus-article-buffer
7297     (widen)
7298     (goto-char (point-max))
7299     (recenter -3)
7300     (when gnus-page-broken
7301       (gnus-narrow-to-page))))
7302
7303 (defun gnus-summary-print-article (&optional filename n)
7304   "Generate and print a PostScript image of the N next (mail) articles.
7305
7306 If N is negative, print the N previous articles.  If N is nil and articles
7307 have been marked with the process mark, print these instead.
7308
7309 If the optional first argument FILENAME is nil, send the image to the
7310 printer.  If FILENAME is a string, save the PostScript image in a file with
7311 that name.  If FILENAME is a number, prompt the user for the name of the file
7312 to save in."
7313   (interactive (list (ps-print-preprint current-prefix-arg)
7314                      current-prefix-arg))
7315   (dolist (article (gnus-summary-work-articles n))
7316     (gnus-summary-select-article nil nil 'pseudo article)
7317     (gnus-eval-in-buffer-window gnus-article-buffer
7318       (let ((buffer (generate-new-buffer " *print*")))
7319         (unwind-protect
7320             (progn
7321               (copy-to-buffer buffer (point-min) (point-max))
7322               (set-buffer buffer)
7323               (gnus-article-delete-invisible-text)
7324               (let ((ps-left-header
7325                      (list
7326                       (concat "("
7327                               (mail-header-subject gnus-current-headers) ")")
7328                       (concat "("
7329                               (mail-header-from gnus-current-headers) ")")))
7330                     (ps-right-header
7331                      (list
7332                       "/pagenumberstring load"
7333                       (concat "("
7334                               (mail-header-date gnus-current-headers) ")"))))
7335                 (gnus-run-hooks 'gnus-ps-print-hook)
7336                 (save-excursion
7337                   (ps-print-buffer-with-faces filename))))
7338           (kill-buffer buffer))))))
7339
7340 (defun gnus-summary-show-article (&optional arg)
7341   "Force re-fetching of the current article.
7342 If ARG (the prefix) is a number, show the article with the charset 
7343 defined in `gnus-summary-show-article-charset-alist', or the charset
7344 inputed.
7345 If ARG (the prefix) is non-nil and not a number, show the raw article 
7346 without any article massaging functions being run."
7347   (interactive "P")
7348   (cond 
7349    ((numberp arg)
7350     (let ((gnus-newsgroup-charset 
7351            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7352                (read-coding-system "Charset: ")))
7353           (gnus-newsgroup-ignored-charsets 'gnus-all))
7354       (gnus-summary-select-article nil 'force)))
7355    ((not arg)
7356     ;; Select the article the normal way.
7357     (gnus-summary-select-article nil 'force))
7358    (t
7359     ;; We have to require this here to make sure that the following
7360     ;; dynamic binding isn't shadowed by autoloading.
7361     (require 'gnus-async)
7362     (require 'gnus-art)
7363     ;; Bind the article treatment functions to nil.
7364     (let ((gnus-have-all-headers t)
7365           gnus-article-display-hook
7366           gnus-article-prepare-hook
7367           gnus-article-decode-hook
7368           gnus-break-pages
7369           gnus-show-mime)
7370       (gnus-summary-select-article nil 'force))))
7371   (gnus-summary-goto-subject gnus-current-article)
7372   (gnus-summary-position-point))
7373
7374 (defun gnus-summary-verbose-headers (&optional arg)
7375   "Toggle permanent full header display.
7376 If ARG is a positive number, turn header display on.
7377 If ARG is a negative number, turn header display off."
7378   (interactive "P")
7379   (setq gnus-show-all-headers
7380         (cond ((or (not (numberp arg))
7381                    (zerop arg))
7382                (not gnus-show-all-headers))
7383               ((natnump arg)
7384                t)))
7385   (gnus-summary-show-article))
7386
7387 (defun gnus-summary-toggle-header (&optional arg)
7388   "Show the headers if they are hidden, or hide them if they are shown.
7389 If ARG is a positive number, show the entire header.
7390 If ARG is a negative number, hide the unwanted header lines."
7391   (interactive "P")
7392   (save-excursion
7393     (set-buffer gnus-article-buffer)
7394     (save-restriction
7395       (let* ((buffer-read-only nil)
7396              (inhibit-point-motion-hooks t)
7397              hidden e)
7398         (setq hidden
7399               (if (numberp arg)
7400                   (>= arg 0)
7401                 (save-restriction 
7402                   (article-narrow-to-head)
7403                   (gnus-article-hidden-text-p 'headers))))
7404         (goto-char (point-min))
7405         (when (search-forward "\n\n" nil t)
7406           (delete-region (point-min) (1- (point))))
7407         (goto-char (point-min))
7408         (save-excursion
7409           (set-buffer gnus-original-article-buffer)
7410           (goto-char (point-min))
7411           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7412         (insert-buffer-substring gnus-original-article-buffer 1 e)
7413         (save-restriction
7414           (narrow-to-region (point-min) (point))
7415           (article-decode-encoded-words)
7416           (if  hidden
7417               (let ((gnus-treat-hide-headers nil)
7418                     (gnus-treat-hide-boring-headers nil))
7419                 (setq gnus-article-wash-types
7420                       (delq 'headers gnus-article-wash-types))
7421                 (gnus-treat-article 'head))
7422             (gnus-treat-article 'head)))
7423         (gnus-set-mode-line 'article)))))
7424
7425 (defun gnus-summary-show-all-headers ()
7426   "Make all header lines visible."
7427   (interactive)
7428   (gnus-article-show-all-headers))
7429
7430 (defun gnus-summary-toggle-mime (&optional arg)
7431   "Toggle MIME processing.
7432 If ARG is a positive number, turn MIME processing on."
7433   (interactive "P")
7434   (setq gnus-show-mime
7435         (if (null arg)
7436             (not gnus-show-mime)
7437           (> (prefix-numeric-value arg) 0)))
7438   (gnus-summary-select-article t 'force))
7439
7440 (defun gnus-summary-caesar-message (&optional arg)
7441   "Caesar rotate the current article by 13.
7442 The numerical prefix specifies how many places to rotate each letter
7443 forward."
7444   (interactive "P")
7445   (gnus-summary-select-article)
7446   (let ((mail-header-separator ""))
7447     (gnus-eval-in-buffer-window gnus-article-buffer
7448       (save-restriction
7449         (widen)
7450         (let ((start (window-start))
7451               buffer-read-only)
7452           (message-caesar-buffer-body arg)
7453           (set-window-start (get-buffer-window (current-buffer)) start))))))
7454
7455 (defun gnus-summary-stop-page-breaking ()
7456   "Stop page breaking in the current article."
7457   (interactive)
7458   (gnus-summary-select-article)
7459   (gnus-eval-in-buffer-window gnus-article-buffer
7460     (widen)
7461     (when (gnus-visual-p 'page-marker)
7462       (let ((buffer-read-only nil))
7463         (gnus-remove-text-with-property 'gnus-prev)
7464         (gnus-remove-text-with-property 'gnus-next))
7465       (setq gnus-page-broken nil))))
7466
7467 (defun gnus-summary-move-article (&optional n to-newsgroup
7468                                             select-method action)
7469   "Move the current article to a different newsgroup.
7470 If N is a positive number, move the N next articles.
7471 If N is a negative number, move the N previous articles.
7472 If N is nil and any articles have been marked with the process mark,
7473 move those articles instead.
7474 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7475 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7476 re-spool using this method.
7477
7478 For this function to work, both the current newsgroup and the
7479 newsgroup that you want to move to have to support the `request-move'
7480 and `request-accept' functions.
7481
7482 ACTION can be either `move' (the default), `crosspost' or `copy'."
7483   (interactive "P")
7484   (unless action
7485     (setq action 'move))
7486   ;; Disable marking as read.
7487   (let (gnus-mark-article-hook)
7488     (save-window-excursion
7489       (gnus-summary-select-article)))
7490   ;; Check whether the source group supports the required functions.
7491   (cond ((and (eq action 'move)
7492               (not (gnus-check-backend-function
7493                     'request-move-article gnus-newsgroup-name)))
7494          (error "The current group does not support article moving"))
7495         ((and (eq action 'crosspost)
7496               (not (gnus-check-backend-function
7497                     'request-replace-article gnus-newsgroup-name)))
7498          (error "The current group does not support article editing")))
7499   (let ((articles (gnus-summary-work-articles n))
7500         (prefix (if (gnus-check-backend-function
7501                     'request-move-article gnus-newsgroup-name)
7502                     (gnus-group-real-prefix gnus-newsgroup-name)
7503                   ""))
7504         (names '((move "Move" "Moving")
7505                  (copy "Copy" "Copying")
7506                  (crosspost "Crosspost" "Crossposting")))
7507         (copy-buf (save-excursion
7508                     (nnheader-set-temp-buffer " *copy article*")))
7509         (default-marks gnus-article-mark-lists)
7510         (no-expire-marks (delete '(expirable . expire)
7511                                  (copy-sequence gnus-article-mark-lists)))
7512         art-group to-method new-xref article to-groups)
7513     (unless (assq action names)
7514       (error "Unknown action %s" action))
7515     ;; Read the newsgroup name.
7516     (when (and (not to-newsgroup)
7517                (not select-method))
7518       (setq to-newsgroup
7519             (gnus-read-move-group-name
7520              (cadr (assq action names))
7521              (symbol-value (intern (format "gnus-current-%s-group" action)))
7522              articles prefix))
7523       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7524     (setq to-method (or select-method
7525                         (gnus-server-to-method
7526                          (gnus-group-method to-newsgroup))))
7527     ;; Check the method we are to move this article to...
7528     (unless (gnus-check-backend-function
7529              'request-accept-article (car to-method))
7530       (error "%s does not support article copying" (car to-method)))
7531     (unless (gnus-check-server to-method)
7532       (error "Can't open server %s" (car to-method)))
7533     (gnus-message 6 "%s to %s: %s..."
7534                   (caddr (assq action names))
7535                   (or (car select-method) to-newsgroup) articles)
7536     (while articles
7537       (setq article (pop articles))
7538       (setq
7539        art-group
7540        (cond
7541         ;; Move the article.
7542         ((eq action 'move)
7543          ;; Remove this article from future suppression.
7544          (gnus-dup-unsuppress-article article)
7545          (gnus-request-move-article
7546           article                       ; Article to move
7547           gnus-newsgroup-name           ; From newsgroup
7548           (nth 1 (gnus-find-method-for-group
7549                   gnus-newsgroup-name)) ; Server
7550           (list 'gnus-request-accept-article
7551                 to-newsgroup (list 'quote select-method)
7552                 (not articles) t)       ; Accept form
7553           (not articles)))              ; Only save nov last time
7554         ;; Copy the article.
7555         ((eq action 'copy)
7556          (save-excursion
7557            (set-buffer copy-buf)
7558            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7559              (gnus-request-accept-article
7560               to-newsgroup select-method (not articles) t))))
7561         ;; Crosspost the article.
7562         ((eq action 'crosspost)
7563          (let ((xref (message-tokenize-header
7564                       (mail-header-xref (gnus-summary-article-header article))
7565                       " ")))
7566            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7567                                   ":" article))
7568            (unless xref
7569              (setq xref (list (system-name))))
7570            (setq new-xref
7571                  (concat
7572                   (mapconcat 'identity
7573                              (delete "Xref:" (delete new-xref xref))
7574                              " ")
7575                   " " new-xref))
7576            (save-excursion
7577              (set-buffer copy-buf)
7578              ;; First put the article in the destination group.
7579              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7580              (when (consp (setq art-group
7581                                 (gnus-request-accept-article
7582                                  to-newsgroup select-method (not articles))))
7583                (setq new-xref (concat new-xref " " (car art-group)
7584                                       ":" (cdr art-group)))
7585                ;; Now we have the new Xrefs header, so we insert
7586                ;; it and replace the new article.
7587                (nnheader-replace-header "Xref" new-xref)
7588                (gnus-request-replace-article
7589                 (cdr art-group) to-newsgroup (current-buffer))
7590                art-group))))))
7591       (cond
7592        ((not art-group)
7593         (gnus-message 1 "Couldn't %s article %s: %s"
7594                       (cadr (assq action names)) article
7595                       (nnheader-get-report (car to-method))))
7596        ((eq art-group 'junk)
7597         (when (eq action 'move)
7598           (gnus-summary-mark-article article gnus-canceled-mark)
7599           (gnus-message 4 "Deleted article %s" article)))
7600        (t
7601         (let* ((pto-group (gnus-group-prefixed-name
7602                            (car art-group) to-method))
7603                (entry
7604                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7605                (info (nth 2 entry))
7606                (to-group (gnus-info-group info))
7607                to-marks)
7608           ;; Update the group that has been moved to.
7609           (when (and info
7610                      (memq action '(move copy)))
7611             (unless (member to-group to-groups)
7612               (push to-group to-groups))
7613
7614             (unless (memq article gnus-newsgroup-unreads)
7615               (push 'read to-marks)
7616               (gnus-info-set-read
7617                info (gnus-add-to-range (gnus-info-read info)
7618                                        (list (cdr art-group)))))
7619
7620             ;; See whether the article is to be put in the cache.
7621             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7622                              default-marks
7623                            no-expire-marks))
7624                   (to-article (cdr art-group)))
7625
7626               ;; Enter the article into the cache in the new group,
7627               ;; if that is required.
7628               (when gnus-use-cache
7629                 (gnus-cache-possibly-enter-article
7630                  to-group to-article
7631                  (let ((header (copy-sequence
7632                                 (gnus-summary-article-header article))))
7633                    (mail-header-set-number header to-article)
7634                    header)
7635                  (memq article gnus-newsgroup-marked)
7636                  (memq article gnus-newsgroup-dormant)
7637                  (memq article gnus-newsgroup-unreads)))
7638
7639               (when gnus-preserve-marks 
7640                 ;; Copy any marks over to the new group.
7641                 (when (and (equal to-group gnus-newsgroup-name)
7642                            (not (memq article gnus-newsgroup-unreads)))
7643                   ;; Mark this article as read in this group.
7644                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7645                   (setcdr (gnus-active to-group) to-article)
7646                   (setcdr gnus-newsgroup-active to-article))
7647
7648                 (while marks
7649                   (when (memq article (symbol-value
7650                                        (intern (format "gnus-newsgroup-%s"
7651                                                        (caar marks)))))
7652                     (push (cdar marks) to-marks)
7653                     ;; If the other group is the same as this group,
7654                     ;; then we have to add the mark to the list.
7655                     (when (equal to-group gnus-newsgroup-name)
7656                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7657                            (cons to-article
7658                                  (symbol-value
7659                                   (intern (format "gnus-newsgroup-%s"
7660                                                   (caar marks)))))))
7661                     ;; Copy the marks to other group.
7662                     (gnus-add-marked-articles
7663                      to-group (cdar marks) (list to-article) info))
7664                   (setq marks (cdr marks)))
7665
7666                 (gnus-request-set-mark to-group (list (list (list to-article)
7667                                                             'set
7668                                                             to-marks))))
7669
7670               (gnus-dribble-enter
7671                (concat "(gnus-group-set-info '"
7672                        (gnus-prin1-to-string (gnus-get-info to-group))
7673                        ")"))))
7674
7675           ;; Update the Xref header in this article to point to
7676           ;; the new crossposted article we have just created.
7677           (when (eq action 'crosspost)
7678             (save-excursion
7679               (set-buffer copy-buf)
7680               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7681               (nnheader-replace-header "Xref" new-xref)
7682               (gnus-request-replace-article
7683                article gnus-newsgroup-name (current-buffer)))))
7684
7685         ;;;!!!Why is this necessary?
7686         (set-buffer gnus-summary-buffer)
7687
7688         (gnus-summary-goto-subject article)
7689         (when (eq action 'move)
7690           (gnus-summary-mark-article article gnus-canceled-mark))))
7691       (gnus-summary-remove-process-mark article))
7692     ;; Re-activate all groups that have been moved to.
7693     (while to-groups
7694       (save-excursion
7695         (set-buffer gnus-group-buffer)
7696         (when (gnus-group-goto-group (car to-groups) t)
7697           (gnus-group-get-new-news-this-group 1 t))
7698         (pop to-groups)))
7699
7700     (gnus-kill-buffer copy-buf)
7701     (gnus-summary-position-point)
7702     (gnus-set-mode-line 'summary)))
7703
7704 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7705   "Move the current article to a different newsgroup.
7706 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7707 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7708 re-spool using this method."
7709   (interactive "P")
7710   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7711
7712 (defun gnus-summary-crosspost-article (&optional n)
7713   "Crosspost the current article to some other group."
7714   (interactive "P")
7715   (gnus-summary-move-article n nil nil 'crosspost))
7716
7717 (defcustom gnus-summary-respool-default-method nil
7718   "Default method for respooling an article.
7719 If nil, use to the current newsgroup method."
7720   :type '(choice (gnus-select-method :value (nnml ""))
7721                  (const nil))
7722   :group 'gnus-summary-mail)
7723
7724 (defun gnus-summary-respool-article (&optional n method)
7725   "Respool the current article.
7726 The article will be squeezed through the mail spooling process again,
7727 which means that it will be put in some mail newsgroup or other
7728 depending on `nnmail-split-methods'.
7729 If N is a positive number, respool the N next articles.
7730 If N is a negative number, respool the N previous articles.
7731 If N is nil and any articles have been marked with the process mark,
7732 respool those articles instead.
7733
7734 Respooling can be done both from mail groups and \"real\" newsgroups.
7735 In the former case, the articles in question will be moved from the
7736 current group into whatever groups they are destined to.  In the
7737 latter case, they will be copied into the relevant groups."
7738   (interactive
7739    (list current-prefix-arg
7740          (let* ((methods (gnus-methods-using 'respool))
7741                 (methname
7742                  (symbol-name (or gnus-summary-respool-default-method
7743                                   (car (gnus-find-method-for-group
7744                                         gnus-newsgroup-name)))))
7745                 (method
7746                  (gnus-completing-read
7747                   methname "What backend do you want to use when respooling?"
7748                   methods nil t nil 'gnus-mail-method-history))
7749                 ms)
7750            (cond
7751             ((zerop (length (setq ms (gnus-servers-using-backend
7752                                       (intern method)))))
7753              (list (intern method) ""))
7754             ((= 1 (length ms))
7755              (car ms))
7756             (t
7757              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7758                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7759                            ms-alist))))))))
7760   (unless method
7761     (error "No method given for respooling"))
7762   (if (assoc (symbol-name
7763               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7764              (gnus-methods-using 'respool))
7765       (gnus-summary-move-article n nil method)
7766     (gnus-summary-copy-article n nil method)))
7767
7768 (defun gnus-summary-import-article (file)
7769   "Import an arbitrary file into a mail newsgroup."
7770   (interactive "fImport file: ")
7771   (let ((group gnus-newsgroup-name)
7772         (now (current-time))
7773         atts lines)
7774     (unless (gnus-check-backend-function 'request-accept-article group)
7775       (error "%s does not support article importing" group))
7776     (or (file-readable-p file)
7777         (not (file-regular-p file))
7778         (error "Can't read %s" file))
7779     (save-excursion
7780       (set-buffer (gnus-get-buffer-create " *import file*"))
7781       (erase-buffer)
7782       (nnheader-insert-file-contents file)
7783       (goto-char (point-min))
7784       (unless (nnheader-article-p)
7785         ;; This doesn't look like an article, so we fudge some headers.
7786         (setq atts (file-attributes file)
7787               lines (count-lines (point-min) (point-max)))
7788         (insert "From: " (read-string "From: ") "\n"
7789                 "Subject: " (read-string "Subject: ") "\n"
7790                 "Date: " (message-make-date (nth 5 atts))
7791                 "\n"
7792                 "Message-ID: " (message-make-message-id) "\n"
7793                 "Lines: " (int-to-string lines) "\n"
7794                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7795       (gnus-request-accept-article group nil t)
7796       (kill-buffer (current-buffer)))))
7797
7798 (defun gnus-summary-article-posted-p ()
7799   "Say whether the current (mail) article is available from news as well.
7800 This will be the case if the article has both been mailed and posted."
7801   (interactive)
7802   (let ((id (mail-header-references (gnus-summary-article-header)))
7803         (gnus-override-method (car (gnus-refer-article-methods))))
7804     (if (gnus-request-head id "")
7805         (gnus-message 2 "The current message was found on %s"
7806                       gnus-override-method)
7807       (gnus-message 2 "The current message couldn't be found on %s"
7808                     gnus-override-method)
7809       nil)))
7810
7811 (defun gnus-summary-expire-articles (&optional now)
7812   "Expire all articles that are marked as expirable in the current group."
7813   (interactive)
7814   (when (gnus-check-backend-function
7815          'request-expire-articles gnus-newsgroup-name)
7816     ;; This backend supports expiry.
7817     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7818            (expirable (if total
7819                           (progn
7820                             ;; We need to update the info for
7821                             ;; this group for `gnus-list-of-read-articles'
7822                             ;; to give us the right answer.
7823                             (gnus-run-hooks 'gnus-exit-group-hook)
7824                             (gnus-summary-update-info)
7825                             (gnus-list-of-read-articles gnus-newsgroup-name))
7826                         (setq gnus-newsgroup-expirable
7827                               (sort gnus-newsgroup-expirable '<))))
7828            (expiry-wait (if now 'immediate
7829                           (gnus-group-find-parameter
7830                            gnus-newsgroup-name 'expiry-wait)))
7831            es)
7832       (when expirable
7833         ;; There are expirable articles in this group, so we run them
7834         ;; through the expiry process.
7835         (gnus-message 6 "Expiring articles...")
7836         (unless (gnus-check-group gnus-newsgroup-name)
7837           (error "Can't open server for %s" gnus-newsgroup-name))
7838         ;; The list of articles that weren't expired is returned.
7839         (save-excursion
7840           (if expiry-wait
7841               (let ((nnmail-expiry-wait-function nil)
7842                     (nnmail-expiry-wait expiry-wait))
7843                 (setq es (gnus-request-expire-articles
7844                           expirable gnus-newsgroup-name)))
7845             (setq es (gnus-request-expire-articles
7846                       expirable gnus-newsgroup-name))))
7847         (unless total
7848           (setq gnus-newsgroup-expirable es))
7849         ;; We go through the old list of expirable, and mark all
7850         ;; really expired articles as nonexistent.
7851         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7852           (let ((gnus-use-cache nil))
7853             (while expirable
7854               (unless (memq (car expirable) es)
7855                 (when (gnus-data-find (car expirable))
7856                   (gnus-summary-mark-article
7857                    (car expirable) gnus-canceled-mark)))
7858               (setq expirable (cdr expirable)))))
7859         (gnus-message 6 "Expiring articles...done")))))
7860
7861 (defun gnus-summary-expire-articles-now ()
7862   "Expunge all expirable articles in the current group.
7863 This means that *all* articles that are marked as expirable will be
7864 deleted forever, right now."
7865   (interactive)
7866   (or gnus-expert-user
7867       (gnus-yes-or-no-p
7868        "Are you really, really, really sure you want to delete all these messages? ")
7869       (error "Phew!"))
7870   (gnus-summary-expire-articles t))
7871
7872 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7873 (defun gnus-summary-delete-article (&optional n)
7874   "Delete the N next (mail) articles.
7875 This command actually deletes articles.  This is not a marking
7876 command.  The article will disappear forever from your life, never to
7877 return.
7878 If N is negative, delete backwards.
7879 If N is nil and articles have been marked with the process mark,
7880 delete these instead."
7881   (interactive "P")
7882   (unless (gnus-check-backend-function 'request-expire-articles
7883                                        gnus-newsgroup-name)
7884     (error "The current newsgroup does not support article deletion"))
7885   ;; Compute the list of articles to delete.
7886   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7887         not-deleted)
7888     (if (and gnus-novice-user
7889              (not (gnus-yes-or-no-p
7890                    (format "Do you really want to delete %s forever? "
7891                            (if (> (length articles) 1)
7892                                (format "these %s articles" (length articles))
7893                              "this article")))))
7894         ()
7895       ;; Delete the articles.
7896       (setq not-deleted (gnus-request-expire-articles
7897                          articles gnus-newsgroup-name 'force))
7898       (while articles
7899         (gnus-summary-remove-process-mark (car articles))
7900         ;; The backend might not have been able to delete the article
7901         ;; after all.
7902         (unless (memq (car articles) not-deleted)
7903           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7904         (setq articles (cdr articles)))
7905       (when not-deleted
7906         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7907     (gnus-summary-position-point)
7908     (gnus-set-mode-line 'summary)
7909     not-deleted))
7910
7911 (defun gnus-summary-edit-article (&optional force)
7912   "Edit the current article.
7913 This will have permanent effect only in mail groups.
7914 If FORCE is non-nil, allow editing of articles even in read-only
7915 groups."
7916   (interactive "P")
7917   (save-excursion
7918     (set-buffer gnus-summary-buffer)
7919     (let ((mail-parse-charset gnus-newsgroup-charset)
7920           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7921       (gnus-set-global-variables)
7922       (when (and (not force)
7923                  (gnus-group-read-only-p))
7924         (error "The current newsgroup does not support article editing"))
7925       (gnus-summary-show-article t)
7926       (gnus-article-edit-article
7927        'ignore
7928        `(lambda (no-highlight)
7929           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7930                 (mail-parse-ignored-charsets
7931                  ',gnus-newsgroup-ignored-charsets))
7932             (gnus-summary-edit-article-done
7933              ,(or (mail-header-references gnus-current-headers) "")
7934              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7935
7936 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7937
7938 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7939                                                  no-highlight)
7940   "Make edits to the current article permanent."
7941   (interactive)
7942   ;; Replace the article.
7943   (let ((buf (current-buffer)))
7944     (with-temp-buffer
7945       (insert-buffer-substring buf)
7946       (if (and (not read-only)
7947                (not (gnus-request-replace-article
7948                      (cdr gnus-article-current) (car gnus-article-current)
7949                      (current-buffer) t)))
7950           (error "Couldn't replace article")
7951         ;; Update the summary buffer.
7952         (if (and references
7953                  (equal (message-tokenize-header references " ")
7954                         (message-tokenize-header
7955                          (or (message-fetch-field "references") "") " ")))
7956             ;; We only have to update this line.
7957             (save-excursion
7958               (save-restriction
7959                 (message-narrow-to-head)
7960                 (let ((head (buffer-string))
7961                       header)
7962                   (with-temp-buffer
7963                     (insert (format "211 %d Article retrieved.\n"
7964                                     (cdr gnus-article-current)))
7965                     (insert head)
7966                     (insert ".\n")
7967                     (let ((nntp-server-buffer (current-buffer)))
7968                       (setq header (car (gnus-get-newsgroup-headers
7969                                          (save-excursion
7970                                            (set-buffer gnus-summary-buffer)
7971                                            gnus-newsgroup-dependencies)
7972                                          t))))
7973                     (save-excursion
7974                       (set-buffer gnus-summary-buffer)
7975                       (gnus-data-set-header
7976                        (gnus-data-find (cdr gnus-article-current))
7977                        header)
7978                       (gnus-summary-update-article-line
7979                        (cdr gnus-article-current) header))))))
7980           ;; Update threads.
7981           (set-buffer (or buffer gnus-summary-buffer))
7982           (gnus-summary-update-article (cdr gnus-article-current)))
7983         ;; Prettify the article buffer again.
7984         (unless no-highlight
7985           (save-excursion
7986             (set-buffer gnus-article-buffer)
7987             ;;;!!! Fix this -- article should be rehighlighted.
7988             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7989             (set-buffer gnus-original-article-buffer)
7990             (gnus-request-article
7991              (cdr gnus-article-current)
7992              (car gnus-article-current) (current-buffer))))
7993         ;; Prettify the summary buffer line.
7994         (when (gnus-visual-p 'summary-highlight 'highlight)
7995           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7996
7997 (defun gnus-summary-edit-wash (key)
7998   "Perform editing command KEY in the article buffer."
7999   (interactive
8000    (list
8001     (progn
8002       (message "%s" (concat (this-command-keys) "- "))
8003       (read-char))))
8004   (message "")
8005   (gnus-summary-edit-article)
8006   (execute-kbd-macro (concat (this-command-keys) key))
8007   (gnus-article-edit-done))
8008
8009 ;;; Respooling
8010
8011 (defun gnus-summary-respool-query (&optional silent trace)
8012   "Query where the respool algorithm would put this article."
8013   (interactive)
8014   (let (gnus-mark-article-hook)
8015     (gnus-summary-select-article)
8016     (save-excursion
8017       (set-buffer gnus-original-article-buffer)
8018       (save-restriction
8019         (message-narrow-to-head)
8020         (let ((groups (nnmail-article-group 'identity trace)))
8021           (unless silent
8022             (if groups
8023                 (message "This message would go to %s"
8024                          (mapconcat 'car groups ", "))
8025               (message "This message would go to no groups"))
8026             groups))))))
8027
8028 (defun gnus-summary-respool-trace ()
8029   "Trace where the respool algorithm would put this article.
8030 Display a buffer showing all fancy splitting patterns which matched."
8031   (interactive)
8032   (gnus-summary-respool-query nil t))
8033
8034 ;; Summary marking commands.
8035
8036 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8037   "Mark articles which has the same subject as read, and then select the next.
8038 If UNMARK is positive, remove any kind of mark.
8039 If UNMARK is negative, tick articles."
8040   (interactive "P")
8041   (when unmark
8042     (setq unmark (prefix-numeric-value unmark)))
8043   (let ((count
8044          (gnus-summary-mark-same-subject
8045           (gnus-summary-article-subject) unmark)))
8046     ;; Select next unread article.  If auto-select-same mode, should
8047     ;; select the first unread article.
8048     (gnus-summary-next-article t (and gnus-auto-select-same
8049                                       (gnus-summary-article-subject)))
8050     (gnus-message 7 "%d article%s marked as %s"
8051                   count (if (= count 1) " is" "s are")
8052                   (if unmark "unread" "read"))))
8053
8054 (defun gnus-summary-kill-same-subject (&optional unmark)
8055   "Mark articles which has the same subject as read.
8056 If UNMARK is positive, remove any kind of mark.
8057 If UNMARK is negative, tick articles."
8058   (interactive "P")
8059   (when unmark
8060     (setq unmark (prefix-numeric-value unmark)))
8061   (let ((count
8062          (gnus-summary-mark-same-subject
8063           (gnus-summary-article-subject) unmark)))
8064     ;; If marked as read, go to next unread subject.
8065     (when (null unmark)
8066       ;; Go to next unread subject.
8067       (gnus-summary-next-subject 1 t))
8068     (gnus-message 7 "%d articles are marked as %s"
8069                   count (if unmark "unread" "read"))))
8070
8071 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8072   "Mark articles with same SUBJECT as read, and return marked number.
8073 If optional argument UNMARK is positive, remove any kinds of marks.
8074 If optional argument UNMARK is negative, mark articles as unread instead."
8075   (let ((count 1))
8076     (save-excursion
8077       (cond
8078        ((null unmark)                   ; Mark as read.
8079         (while (and
8080                 (progn
8081                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8082                   (gnus-summary-show-thread) t)
8083                 (gnus-summary-find-subject subject))
8084           (setq count (1+ count))))
8085        ((> unmark 0)                    ; Tick.
8086         (while (and
8087                 (progn
8088                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8089                   (gnus-summary-show-thread) t)
8090                 (gnus-summary-find-subject subject))
8091           (setq count (1+ count))))
8092        (t                               ; Mark as unread.
8093         (while (and
8094                 (progn
8095                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8096                   (gnus-summary-show-thread) t)
8097                 (gnus-summary-find-subject subject))
8098           (setq count (1+ count)))))
8099       (gnus-set-mode-line 'summary)
8100       ;; Return the number of marked articles.
8101       count)))
8102
8103 (defun gnus-summary-mark-as-processable (n &optional unmark)
8104   "Set the process mark on the next N articles.
8105 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8106 the process mark instead.  The difference between N and the actual
8107 number of articles marked is returned."
8108   (interactive "p")
8109   (let ((backward (< n 0))
8110         (n (abs n)))
8111     (while (and
8112             (> n 0)
8113             (if unmark
8114                 (gnus-summary-remove-process-mark
8115                  (gnus-summary-article-number))
8116               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8117             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8118       (setq n (1- n)))
8119     (when (/= 0 n)
8120       (gnus-message 7 "No more articles"))
8121     (gnus-summary-recenter)
8122     (gnus-summary-position-point)
8123     n))
8124
8125 (defun gnus-summary-unmark-as-processable (n)
8126   "Remove the process mark from the next N articles.
8127 If N is negative, unmark backward instead.  The difference between N and
8128 the actual number of articles unmarked is returned."
8129   (interactive "p")
8130   (gnus-summary-mark-as-processable n t))
8131
8132 (defun gnus-summary-unmark-all-processable ()
8133   "Remove the process mark from all articles."
8134   (interactive)
8135   (save-excursion
8136     (while gnus-newsgroup-processable
8137       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8138   (gnus-summary-position-point))
8139
8140 (defun gnus-summary-mark-as-expirable (n)
8141   "Mark N articles forward as expirable.
8142 If N is negative, mark backward instead.  The difference between N and
8143 the actual number of articles marked is returned."
8144   (interactive "p")
8145   (gnus-summary-mark-forward n gnus-expirable-mark))
8146
8147 (defun gnus-summary-mark-article-as-replied (article)
8148   "Mark ARTICLE replied and update the summary line."
8149   (push article gnus-newsgroup-replied)
8150   (let ((buffer-read-only nil))
8151     (when (gnus-summary-goto-subject article nil t)
8152       (gnus-summary-update-secondary-mark article))))
8153
8154 (defun gnus-summary-set-bookmark (article)
8155   "Set a bookmark in current article."
8156   (interactive (list (gnus-summary-article-number)))
8157   (when (or (not (get-buffer gnus-article-buffer))
8158             (not gnus-current-article)
8159             (not gnus-article-current)
8160             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8161     (error "No current article selected"))
8162   ;; Remove old bookmark, if one exists.
8163   (let ((old (assq article gnus-newsgroup-bookmarks)))
8164     (when old
8165       (setq gnus-newsgroup-bookmarks
8166             (delq old gnus-newsgroup-bookmarks))))
8167   ;; Set the new bookmark, which is on the form
8168   ;; (article-number . line-number-in-body).
8169   (push
8170    (cons article
8171          (save-excursion
8172            (set-buffer gnus-article-buffer)
8173            (count-lines
8174             (min (point)
8175                  (save-excursion
8176                    (goto-char (point-min))
8177                    (search-forward "\n\n" nil t)
8178                    (point)))
8179             (point))))
8180    gnus-newsgroup-bookmarks)
8181   (gnus-message 6 "A bookmark has been added to the current article."))
8182
8183 (defun gnus-summary-remove-bookmark (article)
8184   "Remove the bookmark from the current article."
8185   (interactive (list (gnus-summary-article-number)))
8186   ;; Remove old bookmark, if one exists.
8187   (let ((old (assq article gnus-newsgroup-bookmarks)))
8188     (if old
8189         (progn
8190           (setq gnus-newsgroup-bookmarks
8191                 (delq old gnus-newsgroup-bookmarks))
8192           (gnus-message 6 "Removed bookmark."))
8193       (gnus-message 6 "No bookmark in current article."))))
8194
8195 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8196 (defun gnus-summary-mark-as-dormant (n)
8197   "Mark N articles forward as dormant.
8198 If N is negative, mark backward instead.  The difference between N and
8199 the actual number of articles marked is returned."
8200   (interactive "p")
8201   (gnus-summary-mark-forward n gnus-dormant-mark))
8202
8203 (defun gnus-summary-set-process-mark (article)
8204   "Set the process mark on ARTICLE and update the summary line."
8205   (setq gnus-newsgroup-processable
8206         (cons article
8207               (delq article gnus-newsgroup-processable)))
8208   (when (gnus-summary-goto-subject article)
8209     (gnus-summary-show-thread)
8210     (gnus-summary-goto-subject article)
8211     (gnus-summary-update-secondary-mark article)))
8212
8213 (defun gnus-summary-remove-process-mark (article)
8214   "Remove the process mark from ARTICLE and update the summary line."
8215   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8216   (when (gnus-summary-goto-subject article)
8217     (gnus-summary-show-thread)
8218     (gnus-summary-goto-subject article)
8219     (gnus-summary-update-secondary-mark article)))
8220
8221 (defun gnus-summary-set-saved-mark (article)
8222   "Set the process mark on ARTICLE and update the summary line."
8223   (push article gnus-newsgroup-saved)
8224   (when (gnus-summary-goto-subject article)
8225     (gnus-summary-update-secondary-mark article)))
8226
8227 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8228   "Mark N articles as read forwards.
8229 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8230 The difference between N and the actual number of articles marked is
8231 returned.
8232 Iff NO-EXPIRE, auto-expiry will be inhibited."
8233   (interactive "p")
8234   (gnus-summary-show-thread)
8235   (let ((backward (< n 0))
8236         (gnus-summary-goto-unread
8237          (and gnus-summary-goto-unread
8238               (not (eq gnus-summary-goto-unread 'never))
8239               (not (memq mark (list gnus-unread-mark
8240                                     gnus-ticked-mark gnus-dormant-mark)))))
8241         (n (abs n))
8242         (mark (or mark gnus-del-mark)))
8243     (while (and (> n 0)
8244                 (gnus-summary-mark-article nil mark no-expire)
8245                 (zerop (gnus-summary-next-subject
8246                         (if backward -1 1)
8247                         (and gnus-summary-goto-unread
8248                              (not (eq gnus-summary-goto-unread 'never)))
8249                         t)))
8250       (setq n (1- n)))
8251     (when (/= 0 n)
8252       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8253     (gnus-summary-recenter)
8254     (gnus-summary-position-point)
8255     (gnus-set-mode-line 'summary)
8256     n))
8257
8258 (defun gnus-summary-mark-article-as-read (mark)
8259   "Mark the current article quickly as read with MARK."
8260   (let ((article (gnus-summary-article-number)))
8261     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8262     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8263     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8264     (push (cons article mark) gnus-newsgroup-reads)
8265     ;; Possibly remove from cache, if that is used.
8266     (when gnus-use-cache
8267       (gnus-cache-enter-remove-article article))
8268     ;; Allow the backend to change the mark.
8269     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8270     ;; Check for auto-expiry.
8271     (when (and gnus-newsgroup-auto-expire
8272                (memq mark gnus-auto-expirable-marks))
8273       (setq mark gnus-expirable-mark)
8274       ;; Let the backend know about the mark change.
8275       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8276       (push article gnus-newsgroup-expirable))
8277     ;; Set the mark in the buffer.
8278     (gnus-summary-update-mark mark 'unread)
8279     t))
8280
8281 (defun gnus-summary-mark-article-as-unread (mark)
8282   "Mark the current article quickly as unread with MARK."
8283   (let* ((article (gnus-summary-article-number))
8284          (old-mark (gnus-summary-article-mark article)))
8285     ;; Allow the backend to change the mark.
8286     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8287     (if (eq mark old-mark)
8288         t
8289       (if (<= article 0)
8290           (progn
8291             (gnus-error 1 "Can't mark negative article numbers")
8292             nil)
8293         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8294         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8295         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8296         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8297         (cond ((= mark gnus-ticked-mark)
8298                (push article gnus-newsgroup-marked))
8299               ((= mark gnus-dormant-mark)
8300                (push article gnus-newsgroup-dormant))
8301               (t
8302                (push article gnus-newsgroup-unreads)))
8303         (gnus-pull article gnus-newsgroup-reads)
8304
8305         ;; See whether the article is to be put in the cache.
8306         (and gnus-use-cache
8307              (vectorp (gnus-summary-article-header article))
8308              (save-excursion
8309                (gnus-cache-possibly-enter-article
8310                 gnus-newsgroup-name article
8311                 (gnus-summary-article-header article)
8312                 (= mark gnus-ticked-mark)
8313                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8314
8315         ;; Fix the mark.
8316         (gnus-summary-update-mark mark 'unread)
8317         t))))
8318
8319 (defun gnus-summary-mark-article (&optional article mark no-expire)
8320   "Mark ARTICLE with MARK.  MARK can be any character.
8321 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8322 `??' (dormant) and `?E' (expirable).
8323 If MARK is nil, then the default character `?r' is used.
8324 If ARTICLE is nil, then the article on the current line will be
8325 marked.
8326 Iff NO-EXPIRE, auto-expiry will be inhibited."
8327   ;; The mark might be a string.
8328   (when (stringp mark)
8329     (setq mark (aref mark 0)))
8330   ;; If no mark is given, then we check auto-expiring.
8331   (when (null mark)
8332     (setq mark gnus-del-mark))
8333   (when (and (not no-expire)
8334              gnus-newsgroup-auto-expire
8335              (memq mark gnus-auto-expirable-marks))
8336     (setq mark gnus-expirable-mark))
8337   (let ((article (or article (gnus-summary-article-number)))
8338         (old-mark (gnus-summary-article-mark article)))
8339     ;; Allow the backend to change the mark.
8340     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8341     (if (eq mark old-mark)
8342         t
8343       (unless article
8344         (error "No article on current line"))
8345       (if (not (if (or (= mark gnus-unread-mark)
8346                        (= mark gnus-ticked-mark)
8347                        (= mark gnus-dormant-mark))
8348                    (gnus-mark-article-as-unread article mark)
8349                  (gnus-mark-article-as-read article mark)))
8350           t
8351         ;; See whether the article is to be put in the cache.
8352         (and gnus-use-cache
8353              (not (= mark gnus-canceled-mark))
8354              (vectorp (gnus-summary-article-header article))
8355              (save-excursion
8356                (gnus-cache-possibly-enter-article
8357                 gnus-newsgroup-name article
8358                 (gnus-summary-article-header article)
8359                 (= mark gnus-ticked-mark)
8360                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8361
8362         (when (gnus-summary-goto-subject article nil t)
8363           (let ((buffer-read-only nil))
8364             (gnus-summary-show-thread)
8365             ;; Fix the mark.
8366             (gnus-summary-update-mark mark 'unread)
8367             t))))))
8368
8369 (defun gnus-summary-update-secondary-mark (article)
8370   "Update the secondary (read, process, cache) mark."
8371   (gnus-summary-update-mark
8372    (cond ((memq article gnus-newsgroup-processable)
8373           gnus-process-mark)
8374          ((memq article gnus-newsgroup-cached)
8375           gnus-cached-mark)
8376          ((memq article gnus-newsgroup-replied)
8377           gnus-replied-mark)
8378          ((memq article gnus-newsgroup-saved)
8379           gnus-saved-mark)
8380          (t gnus-unread-mark))
8381    'replied)
8382   (when (gnus-visual-p 'summary-highlight 'highlight)
8383     (gnus-run-hooks 'gnus-summary-update-hook))
8384   t)
8385
8386 (defun gnus-summary-update-mark (mark type)
8387   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8388         (buffer-read-only nil))
8389     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8390     (when forward
8391       (when (looking-at "\r")
8392         (incf forward))
8393       (when (<= (+ forward (point)) (point-max))
8394         ;; Go to the right position on the line.
8395         (goto-char (+ forward (point)))
8396         ;; Replace the old mark with the new mark.
8397         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8398         ;; Optionally update the marks by some user rule.
8399         (when (eq type 'unread)
8400           (gnus-data-set-mark
8401            (gnus-data-find (gnus-summary-article-number)) mark)
8402           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8403
8404 (defun gnus-mark-article-as-read (article &optional mark)
8405   "Enter ARTICLE in the pertinent lists and remove it from others."
8406   ;; Make the article expirable.
8407   (let ((mark (or mark gnus-del-mark)))
8408     (if (= mark gnus-expirable-mark)
8409         (push article gnus-newsgroup-expirable)
8410       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8411     ;; Remove from unread and marked lists.
8412     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8413     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8414     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8415     (push (cons article mark) gnus-newsgroup-reads)
8416     ;; Possibly remove from cache, if that is used.
8417     (when gnus-use-cache
8418       (gnus-cache-enter-remove-article article))
8419     t))
8420
8421 (defun gnus-mark-article-as-unread (article &optional mark)
8422   "Enter ARTICLE in the pertinent lists and remove it from others."
8423   (let ((mark (or mark gnus-ticked-mark)))
8424     (if (<= article 0)
8425         (progn
8426           (gnus-error 1 "Can't mark negative article numbers")
8427           nil)
8428       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8429             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8430             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8431             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8432
8433       ;; Unsuppress duplicates?
8434       (when gnus-suppress-duplicates
8435         (gnus-dup-unsuppress-article article))
8436
8437       (cond ((= mark gnus-ticked-mark)
8438              (push article gnus-newsgroup-marked))
8439             ((= mark gnus-dormant-mark)
8440              (push article gnus-newsgroup-dormant))
8441             (t
8442              (push article gnus-newsgroup-unreads)))
8443       (gnus-pull article gnus-newsgroup-reads)
8444       t)))
8445
8446 (defalias 'gnus-summary-mark-as-unread-forward
8447   'gnus-summary-tick-article-forward)
8448 (make-obsolete 'gnus-summary-mark-as-unread-forward
8449                'gnus-summary-tick-article-forward)
8450 (defun gnus-summary-tick-article-forward (n)
8451   "Tick N articles forwards.
8452 If N is negative, tick backwards instead.
8453 The difference between N and the number of articles ticked is returned."
8454   (interactive "p")
8455   (gnus-summary-mark-forward n gnus-ticked-mark))
8456
8457 (defalias 'gnus-summary-mark-as-unread-backward
8458   'gnus-summary-tick-article-backward)
8459 (make-obsolete 'gnus-summary-mark-as-unread-backward
8460                'gnus-summary-tick-article-backward)
8461 (defun gnus-summary-tick-article-backward (n)
8462   "Tick N articles backwards.
8463 The difference between N and the number of articles ticked is returned."
8464   (interactive "p")
8465   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8466
8467 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8468 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8469 (defun gnus-summary-tick-article (&optional article clear-mark)
8470   "Mark current article as unread.
8471 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8472 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8473   (interactive)
8474   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8475                                        gnus-ticked-mark)))
8476
8477 (defun gnus-summary-mark-as-read-forward (n)
8478   "Mark N articles as read forwards.
8479 If N is negative, mark backwards instead.
8480 The difference between N and the actual number of articles marked is
8481 returned."
8482   (interactive "p")
8483   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8484
8485 (defun gnus-summary-mark-as-read-backward (n)
8486   "Mark the N articles as read backwards.
8487 The difference between N and the actual number of articles marked is
8488 returned."
8489   (interactive "p")
8490   (gnus-summary-mark-forward
8491    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8492
8493 (defun gnus-summary-mark-as-read (&optional article mark)
8494   "Mark current article as read.
8495 ARTICLE specifies the article to be marked as read.
8496 MARK specifies a string to be inserted at the beginning of the line."
8497   (gnus-summary-mark-article article mark))
8498
8499 (defun gnus-summary-clear-mark-forward (n)
8500   "Clear marks from N articles forward.
8501 If N is negative, clear backward instead.
8502 The difference between N and the number of marks cleared is returned."
8503   (interactive "p")
8504   (gnus-summary-mark-forward n gnus-unread-mark))
8505
8506 (defun gnus-summary-clear-mark-backward (n)
8507   "Clear marks from N articles backward.
8508 The difference between N and the number of marks cleared is returned."
8509   (interactive "p")
8510   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8511
8512 (defun gnus-summary-mark-unread-as-read ()
8513   "Intended to be used by `gnus-summary-mark-article-hook'."
8514   (when (memq gnus-current-article gnus-newsgroup-unreads)
8515     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8516
8517 (defun gnus-summary-mark-read-and-unread-as-read ()
8518   "Intended to be used by `gnus-summary-mark-article-hook'."
8519   (let ((mark (gnus-summary-article-mark)))
8520     (when (or (gnus-unread-mark-p mark)
8521               (gnus-read-mark-p mark))
8522       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8523
8524 (defun gnus-summary-mark-region-as-read (point mark all)
8525   "Mark all unread articles between point and mark as read.
8526 If given a prefix, mark all articles between point and mark as read,
8527 even ticked and dormant ones."
8528   (interactive "r\nP")
8529   (save-excursion
8530     (let (article)
8531       (goto-char point)
8532       (beginning-of-line)
8533       (while (and
8534               (< (point) mark)
8535               (progn
8536                 (when (or all
8537                           (memq (setq article (gnus-summary-article-number))
8538                                 gnus-newsgroup-unreads))
8539                   (gnus-summary-mark-article article gnus-del-mark))
8540                 t)
8541               (gnus-summary-find-next))))))
8542
8543 (defun gnus-summary-mark-below (score mark)
8544   "Mark articles with score less than SCORE with MARK."
8545   (interactive "P\ncMark: ")
8546   (setq score (if score
8547                   (prefix-numeric-value score)
8548                 (or gnus-summary-default-score 0)))
8549   (save-excursion
8550     (set-buffer gnus-summary-buffer)
8551     (goto-char (point-min))
8552     (while
8553         (progn
8554           (and (< (gnus-summary-article-score) score)
8555                (gnus-summary-mark-article nil mark))
8556           (gnus-summary-find-next)))))
8557
8558 (defun gnus-summary-kill-below (&optional score)
8559   "Mark articles with score below SCORE as read."
8560   (interactive "P")
8561   (gnus-summary-mark-below score gnus-killed-mark))
8562
8563 (defun gnus-summary-clear-above (&optional score)
8564   "Clear all marks from articles with score above SCORE."
8565   (interactive "P")
8566   (gnus-summary-mark-above score gnus-unread-mark))
8567
8568 (defun gnus-summary-tick-above (&optional score)
8569   "Tick all articles with score above SCORE."
8570   (interactive "P")
8571   (gnus-summary-mark-above score gnus-ticked-mark))
8572
8573 (defun gnus-summary-mark-above (score mark)
8574   "Mark articles with score over SCORE with MARK."
8575   (interactive "P\ncMark: ")
8576   (setq score (if score
8577                   (prefix-numeric-value score)
8578                 (or gnus-summary-default-score 0)))
8579   (save-excursion
8580     (set-buffer gnus-summary-buffer)
8581     (goto-char (point-min))
8582     (while (and (progn
8583                   (when (> (gnus-summary-article-score) score)
8584                     (gnus-summary-mark-article nil mark))
8585                   t)
8586                 (gnus-summary-find-next)))))
8587
8588 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8589 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8590 (defun gnus-summary-limit-include-expunged (&optional no-error)
8591   "Display all the hidden articles that were expunged for low scores."
8592   (interactive)
8593   (let ((buffer-read-only nil))
8594     (let ((scored gnus-newsgroup-scored)
8595           headers h)
8596       (while scored
8597         (unless (gnus-summary-goto-subject (caar scored))
8598           (and (setq h (gnus-summary-article-header (caar scored)))
8599                (< (cdar scored) gnus-summary-expunge-below)
8600                (push h headers)))
8601         (setq scored (cdr scored)))
8602       (if (not headers)
8603           (when (not no-error)
8604             (error "No expunged articles hidden"))
8605         (goto-char (point-min))
8606         (gnus-summary-prepare-unthreaded (nreverse headers))
8607         (goto-char (point-min))
8608         (gnus-summary-position-point)
8609         t))))
8610
8611 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8612   "Mark all unread articles in this newsgroup as read.
8613 If prefix argument ALL is non-nil, ticked and dormant articles will
8614 also be marked as read.
8615 If QUIETLY is non-nil, no questions will be asked.
8616 If TO-HERE is non-nil, it should be a point in the buffer.  All
8617 articles before this point will be marked as read.
8618 Note that this function will only catch up the unread article
8619 in the current summary buffer limitation.
8620 The number of articles marked as read is returned."
8621   (interactive "P")
8622   (prog1
8623       (save-excursion
8624         (when (or quietly
8625                   (not gnus-interactive-catchup) ;Without confirmation?
8626                   gnus-expert-user
8627                   (gnus-y-or-n-p
8628                    (if all
8629                        "Mark absolutely all articles as read? "
8630                      "Mark all unread articles as read? ")))
8631           (if (and not-mark
8632                    (not gnus-newsgroup-adaptive)
8633                    (not gnus-newsgroup-auto-expire)
8634                    (not gnus-suppress-duplicates)
8635                    (or (not gnus-use-cache)
8636                        (eq gnus-use-cache 'passive)))
8637               (progn
8638                 (when all
8639                   (setq gnus-newsgroup-marked nil
8640                         gnus-newsgroup-dormant nil))
8641                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8642             ;; We actually mark all articles as canceled, which we
8643             ;; have to do when using auto-expiry or adaptive scoring.
8644             (gnus-summary-show-all-threads)
8645             (when (gnus-summary-first-subject (not all) t)
8646               (while (and
8647                       (if to-here (< (point) to-here) t)
8648                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8649                       (gnus-summary-find-next (not all) nil nil t))))
8650             (gnus-set-mode-line 'summary))
8651           t))
8652     (gnus-summary-position-point)))
8653
8654 (defun gnus-summary-catchup-to-here (&optional all)
8655   "Mark all unticked articles before the current one as read.
8656 If ALL is non-nil, also mark ticked and dormant articles as read."
8657   (interactive "P")
8658   (save-excursion
8659     (gnus-save-hidden-threads
8660       (let ((beg (point)))
8661         ;; We check that there are unread articles.
8662         (when (or all (gnus-summary-find-prev))
8663           (gnus-summary-catchup all t beg)))))
8664   (gnus-summary-position-point))
8665
8666 (defun gnus-summary-catchup-all (&optional quietly)
8667   "Mark all articles in this newsgroup as read."
8668   (interactive "P")
8669   (gnus-summary-catchup t quietly))
8670
8671 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8672   "Mark all unread articles in this group as read, then exit.
8673 If prefix argument ALL is non-nil, all articles are marked as read."
8674   (interactive "P")
8675   (when (gnus-summary-catchup all quietly nil 'fast)
8676     ;; Select next newsgroup or exit.
8677     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8678              (eq gnus-auto-select-next 'quietly))
8679         (gnus-summary-next-group nil)
8680       (gnus-summary-exit))))
8681
8682 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8683   "Mark all articles in this newsgroup as read, and then exit."
8684   (interactive "P")
8685   (gnus-summary-catchup-and-exit t quietly))
8686
8687 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8688   "Mark all articles in this group as read and select the next group.
8689 If given a prefix, mark all articles, unread as well as ticked, as
8690 read."
8691   (interactive "P")
8692   (save-excursion
8693     (gnus-summary-catchup all))
8694   (gnus-summary-next-group))
8695
8696 ;;;
8697 ;;; with article
8698 ;;;
8699
8700 (defmacro gnus-with-article (article &rest forms)
8701   "Select ARTICLE and perform FORMS in the original article buffer.
8702 Then replace the article with the result."
8703   `(progn
8704      ;; We don't want the article to be marked as read.
8705      (let (gnus-mark-article-hook)
8706        (gnus-summary-select-article t t nil ,article))
8707      (set-buffer gnus-original-article-buffer)
8708      ,@forms
8709      (if (not (gnus-check-backend-function
8710                'request-replace-article (car gnus-article-current)))
8711          (gnus-message 5 "Read-only group; not replacing")
8712        (unless (gnus-request-replace-article
8713                 ,article (car gnus-article-current)
8714                 (current-buffer) t)
8715          (error "Couldn't replace article")))
8716      ;; The cache and backlog have to be flushed somewhat.
8717      (when gnus-keep-backlog
8718        (gnus-backlog-remove-article
8719         (car gnus-article-current) (cdr gnus-article-current)))
8720      (when gnus-use-cache
8721        (gnus-cache-update-article
8722         (car gnus-article-current) (cdr gnus-article-current)))))
8723
8724 (put 'gnus-with-article 'lisp-indent-function 1)
8725 (put 'gnus-with-article 'edebug-form-spec '(form body))
8726
8727 ;; Thread-based commands.
8728
8729 (defun gnus-summary-articles-in-thread (&optional article)
8730   "Return a list of all articles in the current thread.
8731 If ARTICLE is non-nil, return all articles in the thread that starts
8732 with that article."
8733   (let* ((article (or article (gnus-summary-article-number)))
8734          (data (gnus-data-find-list article))
8735          (top-level (gnus-data-level (car data)))
8736          (top-subject
8737           (cond ((null gnus-thread-operation-ignore-subject)
8738                  (gnus-simplify-subject-re
8739                   (mail-header-subject (gnus-data-header (car data)))))
8740                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8741                  (gnus-simplify-subject-fuzzy
8742                   (mail-header-subject (gnus-data-header (car data)))))
8743                 (t nil)))
8744          (end-point (save-excursion
8745                       (if (gnus-summary-go-to-next-thread)
8746                           (point) (point-max))))
8747          articles)
8748     (while (and data
8749                 (< (gnus-data-pos (car data)) end-point))
8750       (when (or (not top-subject)
8751                 (string= top-subject
8752                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8753                              (gnus-simplify-subject-fuzzy
8754                               (mail-header-subject
8755                                (gnus-data-header (car data))))
8756                            (gnus-simplify-subject-re
8757                             (mail-header-subject
8758                              (gnus-data-header (car data)))))))
8759         (push (gnus-data-number (car data)) articles))
8760       (unless (and (setq data (cdr data))
8761                    (> (gnus-data-level (car data)) top-level))
8762         (setq data nil)))
8763     ;; Return the list of articles.
8764     (nreverse articles)))
8765
8766 (defun gnus-summary-rethread-current ()
8767   "Rethread the thread the current article is part of."
8768   (interactive)
8769   (let* ((gnus-show-threads t)
8770          (article (gnus-summary-article-number))
8771          (id (mail-header-id (gnus-summary-article-header)))
8772          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8773     (unless id
8774       (error "No article on the current line"))
8775     (gnus-rebuild-thread id)
8776     (gnus-summary-goto-subject article)))
8777
8778 (defun gnus-summary-reparent-thread ()
8779   "Make the current article child of the marked (or previous) article.
8780
8781 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8782 is non-nil or the Subject: of both articles are the same."
8783   (interactive)
8784   (unless (not (gnus-group-read-only-p))
8785     (error "The current newsgroup does not support article editing"))
8786   (unless (<= (length gnus-newsgroup-processable) 1)
8787     (error "No more than one article may be marked"))
8788   (save-window-excursion
8789     (let ((gnus-article-buffer " *reparent*")
8790           (current-article (gnus-summary-article-number))
8791           ;; First grab the marked article, otherwise one line up.
8792           (parent-article (if (not (null gnus-newsgroup-processable))
8793                               (car gnus-newsgroup-processable)
8794                             (save-excursion
8795                               (if (eq (forward-line -1) 0)
8796                                   (gnus-summary-article-number)
8797                                 (error "Beginning of summary buffer"))))))
8798       (unless (not (eq current-article parent-article))
8799         (error "An article may not be self-referential"))
8800       (let ((message-id (mail-header-id
8801                          (gnus-summary-article-header parent-article))))
8802         (unless (and message-id (not (equal message-id "")))
8803           (error "No message-id in desired parent"))
8804         (gnus-with-article current-article
8805           (goto-char (point-min))
8806           (if (re-search-forward "^References: " nil t)
8807               (progn
8808                 (re-search-forward "^[^ \t]" nil t)
8809                 (forward-line -1)
8810                 (end-of-line)
8811                 (insert " " message-id))
8812             (insert "References: " message-id "\n")))
8813         (set-buffer gnus-summary-buffer)
8814         (gnus-summary-unmark-all-processable)
8815         (gnus-summary-update-article current-article)
8816         (gnus-summary-rethread-current)
8817         (gnus-message 3 "Article %d is now the child of article %d"
8818                       current-article parent-article)))))
8819
8820 (defun gnus-summary-toggle-threads (&optional arg)
8821   "Toggle showing conversation threads.
8822 If ARG is positive number, turn showing conversation threads on."
8823   (interactive "P")
8824   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8825     (setq gnus-show-threads
8826           (if (null arg) (not gnus-show-threads)
8827             (> (prefix-numeric-value arg) 0)))
8828     (gnus-summary-prepare)
8829     (gnus-summary-goto-subject current)
8830     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8831     (gnus-summary-position-point)))
8832
8833 (defun gnus-summary-show-all-threads ()
8834   "Show all threads."
8835   (interactive)
8836   (save-excursion
8837     (let ((buffer-read-only nil))
8838       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8839   (gnus-summary-position-point))
8840
8841 (defun gnus-summary-show-thread ()
8842   "Show thread subtrees.
8843 Returns nil if no thread was there to be shown."
8844   (interactive)
8845   (let ((buffer-read-only nil)
8846         (orig (point))
8847         ;; first goto end then to beg, to have point at beg after let
8848         (end (progn (end-of-line) (point)))
8849         (beg (progn (beginning-of-line) (point))))
8850     (prog1
8851         ;; Any hidden lines here?
8852         (search-forward "\r" end t)
8853       (subst-char-in-region beg end ?\^M ?\n t)
8854       (goto-char orig)
8855       (gnus-summary-position-point))))
8856
8857 (defun gnus-summary-hide-all-threads ()
8858   "Hide all thread subtrees."
8859   (interactive)
8860   (save-excursion
8861     (goto-char (point-min))
8862     (gnus-summary-hide-thread)
8863     (while (zerop (gnus-summary-next-thread 1 t))
8864       (gnus-summary-hide-thread)))
8865   (gnus-summary-position-point))
8866
8867 (defun gnus-summary-hide-thread ()
8868   "Hide thread subtrees.
8869 Returns nil if no threads were there to be hidden."
8870   (interactive)
8871   (let ((buffer-read-only nil)
8872         (start (point))
8873         (article (gnus-summary-article-number)))
8874     (goto-char start)
8875     ;; Go forward until either the buffer ends or the subthread
8876     ;; ends.
8877     (when (and (not (eobp))
8878                (or (zerop (gnus-summary-next-thread 1 t))
8879                    (goto-char (point-max))))
8880       (prog1
8881           (if (and (> (point) start)
8882                    (search-backward "\n" start t))
8883               (progn
8884                 (subst-char-in-region start (point) ?\n ?\^M)
8885                 (gnus-summary-goto-subject article))
8886             (goto-char start)
8887             nil)))))
8888
8889 (defun gnus-summary-go-to-next-thread (&optional previous)
8890   "Go to the same level (or less) next thread.
8891 If PREVIOUS is non-nil, go to previous thread instead.
8892 Return the article number moved to, or nil if moving was impossible."
8893   (let ((level (gnus-summary-thread-level))
8894         (way (if previous -1 1))
8895         (beg (point)))
8896     (forward-line way)
8897     (while (and (not (eobp))
8898                 (< level (gnus-summary-thread-level)))
8899       (forward-line way))
8900     (if (eobp)
8901         (progn
8902           (goto-char beg)
8903           nil)
8904       (setq beg (point))
8905       (prog1
8906           (gnus-summary-article-number)
8907         (goto-char beg)))))
8908
8909 (defun gnus-summary-next-thread (n &optional silent)
8910   "Go to the same level next N'th thread.
8911 If N is negative, search backward instead.
8912 Returns the difference between N and the number of skips actually
8913 done.
8914
8915 If SILENT, don't output messages."
8916   (interactive "p")
8917   (let ((backward (< n 0))
8918         (n (abs n)))
8919     (while (and (> n 0)
8920                 (gnus-summary-go-to-next-thread backward))
8921       (decf n))
8922     (unless silent
8923       (gnus-summary-position-point))
8924     (when (and (not silent) (/= 0 n))
8925       (gnus-message 7 "No more threads"))
8926     n))
8927
8928 (defun gnus-summary-prev-thread (n)
8929   "Go to the same level previous N'th thread.
8930 Returns the difference between N and the number of skips actually
8931 done."
8932   (interactive "p")
8933   (gnus-summary-next-thread (- n)))
8934
8935 (defun gnus-summary-go-down-thread ()
8936   "Go down one level in the current thread."
8937   (let ((children (gnus-summary-article-children)))
8938     (when children
8939       (gnus-summary-goto-subject (car children)))))
8940
8941 (defun gnus-summary-go-up-thread ()
8942   "Go up one level in the current thread."
8943   (let ((parent (gnus-summary-article-parent)))
8944     (when parent
8945       (gnus-summary-goto-subject parent))))
8946
8947 (defun gnus-summary-down-thread (n)
8948   "Go down thread N steps.
8949 If N is negative, go up instead.
8950 Returns the difference between N and how many steps down that were
8951 taken."
8952   (interactive "p")
8953   (let ((up (< n 0))
8954         (n (abs n)))
8955     (while (and (> n 0)
8956                 (if up (gnus-summary-go-up-thread)
8957                   (gnus-summary-go-down-thread)))
8958       (setq n (1- n)))
8959     (gnus-summary-position-point)
8960     (when (/= 0 n)
8961       (gnus-message 7 "Can't go further"))
8962     n))
8963
8964 (defun gnus-summary-up-thread (n)
8965   "Go up thread N steps.
8966 If N is negative, go up instead.
8967 Returns the difference between N and how many steps down that were
8968 taken."
8969   (interactive "p")
8970   (gnus-summary-down-thread (- n)))
8971
8972 (defun gnus-summary-top-thread ()
8973   "Go to the top of the thread."
8974   (interactive)
8975   (while (gnus-summary-go-up-thread))
8976   (gnus-summary-article-number))
8977
8978 (defun gnus-summary-kill-thread (&optional unmark)
8979   "Mark articles under current thread as read.
8980 If the prefix argument is positive, remove any kinds of marks.
8981 If the prefix argument is negative, tick articles instead."
8982   (interactive "P")
8983   (when unmark
8984     (setq unmark (prefix-numeric-value unmark)))
8985   (let ((articles (gnus-summary-articles-in-thread)))
8986     (save-excursion
8987       ;; Expand the thread.
8988       (gnus-summary-show-thread)
8989       ;; Mark all the articles.
8990       (while articles
8991         (gnus-summary-goto-subject (car articles))
8992         (cond ((null unmark)
8993                (gnus-summary-mark-article-as-read gnus-killed-mark))
8994               ((> unmark 0)
8995                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8996               (t
8997                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8998         (setq articles (cdr articles))))
8999     ;; Hide killed subtrees.
9000     (and (null unmark)
9001          gnus-thread-hide-killed
9002          (gnus-summary-hide-thread))
9003     ;; If marked as read, go to next unread subject.
9004     (when (null unmark)
9005       ;; Go to next unread subject.
9006       (gnus-summary-next-subject 1 t)))
9007   (gnus-set-mode-line 'summary))
9008
9009 ;; Summary sorting commands
9010
9011 (defun gnus-summary-sort-by-number (&optional reverse)
9012   "Sort the summary buffer by article number.
9013 Argument REVERSE means reverse order."
9014   (interactive "P")
9015   (gnus-summary-sort 'number reverse))
9016
9017 (defun gnus-summary-sort-by-author (&optional reverse)
9018   "Sort the summary buffer by author name alphabetically.
9019 If case-fold-search is non-nil, case of letters is ignored.
9020 Argument REVERSE means reverse order."
9021   (interactive "P")
9022   (gnus-summary-sort 'author reverse))
9023
9024 (defun gnus-summary-sort-by-subject (&optional reverse)
9025   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9026 If case-fold-search is non-nil, case of letters is ignored.
9027 Argument REVERSE means reverse order."
9028   (interactive "P")
9029   (gnus-summary-sort 'subject reverse))
9030
9031 (defun gnus-summary-sort-by-date (&optional reverse)
9032   "Sort the summary buffer by date.
9033 Argument REVERSE means reverse order."
9034   (interactive "P")
9035   (gnus-summary-sort 'date reverse))
9036
9037 (defun gnus-summary-sort-by-score (&optional reverse)
9038   "Sort the summary buffer by score.
9039 Argument REVERSE means reverse order."
9040   (interactive "P")
9041   (gnus-summary-sort 'score reverse))
9042
9043 (defun gnus-summary-sort-by-lines (&optional reverse)
9044   "Sort the summary buffer by the number of lines.
9045 Argument REVERSE means reverse order."
9046   (interactive "P")
9047   (gnus-summary-sort 'lines reverse))
9048
9049 (defun gnus-summary-sort-by-chars (&optional reverse)
9050   "Sort the summary buffer by article length.
9051 Argument REVERSE means reverse order."
9052   (interactive "P")
9053   (gnus-summary-sort 'chars reverse))   
9054
9055 (defun gnus-summary-sort (predicate reverse)
9056   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9057   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9058          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9059          (gnus-thread-sort-functions
9060           (if (not reverse)
9061               thread
9062             `(lambda (t1 t2)
9063                (,thread t2 t1))))
9064          (gnus-article-sort-functions
9065           (if (not reverse)
9066               article
9067             `(lambda (t1 t2)
9068                (,article t2 t1))))
9069          (buffer-read-only)
9070          (gnus-summary-prepare-hook nil))
9071     ;; We do the sorting by regenerating the threads.
9072     (gnus-summary-prepare)
9073     ;; Hide subthreads if needed.
9074     (when (and gnus-show-threads gnus-thread-hide-subtree)
9075       (gnus-summary-hide-all-threads))))
9076
9077 ;; Summary saving commands.
9078
9079 (defun gnus-summary-save-article (&optional n not-saved)
9080   "Save the current article using the default saver function.
9081 If N is a positive number, save the N next articles.
9082 If N is a negative number, save the N previous articles.
9083 If N is nil and any articles have been marked with the process mark,
9084 save those articles instead.
9085 The variable `gnus-default-article-saver' specifies the saver function."
9086   (interactive "P")
9087   (let* ((articles (gnus-summary-work-articles n))
9088          (save-buffer (save-excursion
9089                         (nnheader-set-temp-buffer " *Gnus Save*")))
9090          (num (length articles))
9091          header file)
9092     (dolist (article articles)
9093       (setq header (gnus-summary-article-header article))
9094       (if (not (vectorp header))
9095           ;; This is a pseudo-article.
9096           (if (assq 'name header)
9097               (gnus-copy-file (cdr (assq 'name header)))
9098             (gnus-message 1 "Article %d is unsaveable" article))
9099         ;; This is a real article.
9100         (save-window-excursion
9101           (gnus-summary-select-article t nil nil article))
9102         (save-excursion
9103           (set-buffer save-buffer)
9104           (erase-buffer)
9105           (insert-buffer-substring gnus-original-article-buffer))
9106         (setq file (gnus-article-save save-buffer file num))
9107         (gnus-summary-remove-process-mark article)
9108         (unless not-saved
9109           (gnus-summary-set-saved-mark article))))
9110     (gnus-kill-buffer save-buffer)
9111     (gnus-summary-position-point)
9112     (gnus-set-mode-line 'summary)
9113     n))
9114
9115 (defun gnus-summary-pipe-output (&optional arg)
9116   "Pipe the current article to a subprocess.
9117 If N is a positive number, pipe the N next articles.
9118 If N is a negative number, pipe the N previous articles.
9119 If N is nil and any articles have been marked with the process mark,
9120 pipe those articles instead."
9121   (interactive "P")
9122   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9123     (gnus-summary-save-article arg t))
9124   (gnus-configure-windows 'pipe))
9125
9126 (defun gnus-summary-save-article-mail (&optional arg)
9127   "Append the current article to an mail file.
9128 If N is a positive number, save the N next articles.
9129 If N is a negative number, save the N previous articles.
9130 If N is nil and any articles have been marked with the process mark,
9131 save those articles instead."
9132   (interactive "P")
9133   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9134     (gnus-summary-save-article arg)))
9135
9136 (defun gnus-summary-save-article-rmail (&optional arg)
9137   "Append the current article to an rmail file.
9138 If N is a positive number, save the N next articles.
9139 If N is a negative number, save the N previous articles.
9140 If N is nil and any articles have been marked with the process mark,
9141 save those articles instead."
9142   (interactive "P")
9143   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9144     (gnus-summary-save-article arg)))
9145
9146 (defun gnus-summary-save-article-file (&optional arg)
9147   "Append the current article to a file.
9148 If N is a positive number, save the N next articles.
9149 If N is a negative number, save the N previous articles.
9150 If N is nil and any articles have been marked with the process mark,
9151 save those articles instead."
9152   (interactive "P")
9153   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9154     (gnus-summary-save-article arg)))
9155
9156 (defun gnus-summary-write-article-file (&optional arg)
9157   "Write the current article to a file, deleting the previous file.
9158 If N is a positive number, save the N next articles.
9159 If N is a negative number, save the N previous articles.
9160 If N is nil and any articles have been marked with the process mark,
9161 save those articles instead."
9162   (interactive "P")
9163   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9164     (gnus-summary-save-article arg)))
9165
9166 (defun gnus-summary-save-article-body-file (&optional arg)
9167   "Append the current article body to a file.
9168 If N is a positive number, save the N next articles.
9169 If N is a negative number, save the N previous articles.
9170 If N is nil and any articles have been marked with the process mark,
9171 save those articles instead."
9172   (interactive "P")
9173   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9174     (gnus-summary-save-article arg)))
9175
9176 (defun gnus-summary-pipe-message (program)
9177   "Pipe the current article through PROGRAM."
9178   (interactive "sProgram: ")
9179   (gnus-summary-select-article)
9180   (let ((mail-header-separator ""))
9181     (gnus-eval-in-buffer-window gnus-article-buffer
9182       (save-restriction
9183         (widen)
9184         (let ((start (window-start))
9185               buffer-read-only)
9186           (message-pipe-buffer-body program)
9187           (set-window-start (get-buffer-window (current-buffer)) start))))))
9188
9189 (defun gnus-get-split-value (methods)
9190   "Return a value based on the split METHODS."
9191   (let (split-name method result match)
9192     (when methods
9193       (save-excursion
9194         (set-buffer gnus-original-article-buffer)
9195         (save-restriction
9196           (nnheader-narrow-to-headers)
9197           (while methods
9198             (goto-char (point-min))
9199             (setq method (pop methods))
9200             (setq match (car method))
9201             (when (cond
9202                    ((stringp match)
9203                     ;; Regular expression.
9204                     (ignore-errors
9205                       (re-search-forward match nil t)))
9206                    ((gnus-functionp match)
9207                     ;; Function.
9208                     (save-restriction
9209                       (widen)
9210                       (setq result (funcall match gnus-newsgroup-name))))
9211                    ((consp match)
9212                     ;; Form.
9213                     (save-restriction
9214                       (widen)
9215                       (setq result (eval match)))))
9216               (setq split-name (append (cdr method) split-name))
9217               (cond ((stringp result)
9218                      (push (expand-file-name
9219                             result gnus-article-save-directory)
9220                            split-name))
9221                     ((consp result)
9222                      (setq split-name (append result split-name)))))))))
9223     (nreverse split-name)))
9224
9225 (defun gnus-valid-move-group-p (group)
9226   (and (boundp group)
9227        (symbol-name group)
9228        (symbol-value group)
9229        (gnus-get-function (gnus-find-method-for-group
9230                            (symbol-name group)) 'request-accept-article t)))
9231
9232 (defun gnus-read-move-group-name (prompt default articles prefix)
9233   "Read a group name."
9234   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9235          (minibuffer-confirm-incomplete nil) ; XEmacs
9236          (prom
9237           (format "%s %s to:"
9238                   prompt
9239                   (if (> (length articles) 1)
9240                       (format "these %d articles" (length articles))
9241                     "this article")))
9242          (to-newsgroup
9243           (cond
9244            ((null split-name)
9245             (gnus-completing-read default prom
9246                                   gnus-active-hashtb
9247                                   'gnus-valid-move-group-p
9248                                   nil prefix
9249                                   'gnus-group-history))
9250            ((= 1 (length split-name))
9251             (gnus-completing-read (car split-name) prom
9252                                   gnus-active-hashtb
9253                                   'gnus-valid-move-group-p
9254                                   nil nil
9255                                   'gnus-group-history))
9256            (t
9257             (gnus-completing-read nil prom
9258                                   (mapcar (lambda (el) (list el))
9259                                           (nreverse split-name))
9260                                   nil nil nil
9261                                   'gnus-group-history))))
9262          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9263     (when to-newsgroup
9264       (if (or (string= to-newsgroup "")
9265               (string= to-newsgroup prefix))
9266           (setq to-newsgroup default))
9267       (unless to-newsgroup
9268         (error "No group name entered"))
9269       (or (gnus-active to-newsgroup)
9270           (gnus-activate-group to-newsgroup nil nil to-method)
9271           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9272                                      to-newsgroup))
9273               (or (and (gnus-request-create-group to-newsgroup to-method)
9274                        (gnus-activate-group
9275                         to-newsgroup nil nil to-method)
9276                        (gnus-subscribe-group to-newsgroup))
9277                   (error "Couldn't create group %s" to-newsgroup)))
9278           (error "No such group: %s" to-newsgroup)))
9279     to-newsgroup))
9280
9281 (defun gnus-summary-save-parts (type dir n &optional reverse)
9282   "Save parts matching TYPE to DIR.
9283 If REVERSE, save parts that do not match TYPE."
9284   (interactive
9285    (list (read-string "Save parts of type: " "image/.*")
9286          (read-file-name "Save to directory: " nil nil t)
9287          current-prefix-arg))
9288   (gnus-summary-iterate n
9289     (let ((gnus-display-mime-function nil)
9290           (gnus-inhibit-treatment t))
9291       (gnus-summary-select-article))
9292     (save-excursion
9293       (set-buffer gnus-article-buffer)
9294       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9295         (when handles
9296           (gnus-summary-save-parts-1 type dir handles reverse)
9297           (mm-destroy-parts handles))))))
9298
9299 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9300   (if (stringp (car handle))
9301       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9302               (cdr handle))
9303     (when (if reverse
9304               (not (string-match type (mm-handle-media-type handle)))
9305             (string-match type (mm-handle-media-type handle)))
9306       (let ((file (expand-file-name
9307                    (file-name-nondirectory
9308                     (or
9309                      (mail-content-type-get
9310                       (mm-handle-disposition handle) 'filename)
9311                      (concat gnus-newsgroup-name "." gnus-current-article)))
9312                    dir)))
9313         (unless (file-exists-p file)
9314           (mm-save-part-to-file handle file))))))
9315
9316 ;; Summary extract commands
9317
9318 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9319   (let ((buffer-read-only nil)
9320         (article (gnus-summary-article-number))
9321         after-article b e)
9322     (unless (gnus-summary-goto-subject article)
9323       (error "No such article: %d" article))
9324     (gnus-summary-position-point)
9325     ;; If all commands are to be bunched up on one line, we collect
9326     ;; them here.
9327     (unless gnus-view-pseudos-separately
9328       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9329             files action)
9330         (while ps
9331           (setq action (cdr (assq 'action (car ps))))
9332           (setq files (list (cdr (assq 'name (car ps)))))
9333           (while (and ps (cdr ps)
9334                       (string= (or action "1")
9335                                (or (cdr (assq 'action (cadr ps))) "2")))
9336             (push (cdr (assq 'name (cadr ps))) files)
9337             (setcdr ps (cddr ps)))
9338           (when files
9339             (when (not (string-match "%s" action))
9340               (push " " files))
9341             (push " " files)
9342             (when (assq 'execute (car ps))
9343               (setcdr (assq 'execute (car ps))
9344                       (funcall (if (string-match "%s" action)
9345                                    'format 'concat)
9346                                action
9347                                (mapconcat
9348                                 (lambda (f)
9349                                   (if (equal f " ")
9350                                       f
9351                                     (gnus-quote-arg-for-sh-or-csh f)))
9352                                 files " ")))))
9353           (setq ps (cdr ps)))))
9354     (if (and gnus-view-pseudos (not not-view))
9355         (while pslist
9356           (when (assq 'execute (car pslist))
9357             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9358                                   (eq gnus-view-pseudos 'not-confirm)))
9359           (setq pslist (cdr pslist)))
9360       (save-excursion
9361         (while pslist
9362           (setq after-article (or (cdr (assq 'article (car pslist)))
9363                                   (gnus-summary-article-number)))
9364           (gnus-summary-goto-subject after-article)
9365           (forward-line 1)
9366           (setq b (point))
9367           (insert "    " (file-name-nondirectory
9368                           (cdr (assq 'name (car pslist))))
9369                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9370           (setq e (point))
9371           (forward-line -1)             ; back to `b'
9372           (gnus-add-text-properties
9373            b (1- e) (list 'gnus-number gnus-reffed-article-number
9374                           gnus-mouse-face-prop gnus-mouse-face))
9375           (gnus-data-enter
9376            after-article gnus-reffed-article-number
9377            gnus-unread-mark b (car pslist) 0 (- e b))
9378           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9379           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9380           (setq pslist (cdr pslist)))))))
9381
9382 (defun gnus-pseudos< (p1 p2)
9383   (let ((c1 (cdr (assq 'action p1)))
9384         (c2 (cdr (assq 'action p2))))
9385     (and c1 c2 (string< c1 c2))))
9386
9387 (defun gnus-request-pseudo-article (props)
9388   (cond ((assq 'execute props)
9389          (gnus-execute-command (cdr (assq 'execute props)))))
9390   (let ((gnus-current-article (gnus-summary-article-number)))
9391     (gnus-run-hooks 'gnus-mark-article-hook)))
9392
9393 (defun gnus-execute-command (command &optional automatic)
9394   (save-excursion
9395     (gnus-article-setup-buffer)
9396     (set-buffer gnus-article-buffer)
9397     (setq buffer-read-only nil)
9398     (let ((command (if automatic command
9399                      (read-string "Command: " (cons command 0)))))
9400       (erase-buffer)
9401       (insert "$ " command "\n\n")
9402       (if gnus-view-pseudo-asynchronously
9403           (start-process "gnus-execute" (current-buffer) shell-file-name
9404                          shell-command-switch command)
9405         (call-process shell-file-name nil t nil
9406                       shell-command-switch command)))))
9407
9408 ;; Summary kill commands.
9409
9410 (defun gnus-summary-edit-global-kill (article)
9411   "Edit the \"global\" kill file."
9412   (interactive (list (gnus-summary-article-number)))
9413   (gnus-group-edit-global-kill article))
9414
9415 (defun gnus-summary-edit-local-kill ()
9416   "Edit a local kill file applied to the current newsgroup."
9417   (interactive)
9418   (setq gnus-current-headers (gnus-summary-article-header))
9419   (gnus-group-edit-local-kill
9420    (gnus-summary-article-number) gnus-newsgroup-name))
9421
9422 ;;; Header reading.
9423
9424 (defun gnus-read-header (id &optional header)
9425   "Read the headers of article ID and enter them into the Gnus system."
9426   (let ((group gnus-newsgroup-name)
9427         (gnus-override-method
9428          (or
9429           gnus-override-method
9430           (and (gnus-news-group-p gnus-newsgroup-name)
9431                (car (gnus-refer-article-methods)))))
9432         where)
9433     ;; First we check to see whether the header in question is already
9434     ;; fetched.
9435     (if (stringp id)
9436         ;; This is a Message-ID.
9437         (setq header (or header (gnus-id-to-header id)))
9438       ;; This is an article number.
9439       (setq header (or header (gnus-summary-article-header id))))
9440     (if (and header
9441              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9442         ;; We have found the header.
9443         header
9444       ;; If this is a sparse article, we have to nix out its
9445       ;; previous entry in the thread hashtb.
9446       (when (and header
9447                  (gnus-summary-article-sparse-p (mail-header-number header)))
9448         (let* ((parent (gnus-parent-id (mail-header-references header)))
9449                (thread (and parent (gnus-id-to-thread parent))))
9450           (when thread
9451             (delq (assq header thread) thread))))
9452       ;; We have to really fetch the header to this article.
9453       (save-excursion
9454         (set-buffer nntp-server-buffer)
9455         (when (setq where (gnus-request-head id group))
9456           (nnheader-fold-continuation-lines)
9457           (goto-char (point-max))
9458           (insert ".\n")
9459           (goto-char (point-min))
9460           (insert "211 ")
9461           (princ (cond
9462                   ((numberp id) id)
9463                   ((cdr where) (cdr where))
9464                   (header (mail-header-number header))
9465                   (t gnus-reffed-article-number))
9466                  (current-buffer))
9467           (insert " Article retrieved.\n"))
9468         (if (or (not where)
9469                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9470             ()                          ; Malformed head.
9471           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9472             (when (and (stringp id)
9473                        (not (string= (gnus-group-real-name group)
9474                                      (car where))))
9475               ;; If we fetched by Message-ID and the article came
9476               ;; from a different group, we fudge some bogus article
9477               ;; numbers for this article.
9478               (mail-header-set-number header gnus-reffed-article-number))
9479             (save-excursion
9480               (set-buffer gnus-summary-buffer)
9481               (decf gnus-reffed-article-number)
9482               (gnus-remove-header (mail-header-number header))
9483               (push header gnus-newsgroup-headers)
9484               (setq gnus-current-headers header)
9485               (push (mail-header-number header) gnus-newsgroup-limit)))
9486           header)))))
9487
9488 (defun gnus-remove-header (number)
9489   "Remove header NUMBER from `gnus-newsgroup-headers'."
9490   (if (and gnus-newsgroup-headers
9491            (= number (mail-header-number (car gnus-newsgroup-headers))))
9492       (pop gnus-newsgroup-headers)
9493     (let ((headers gnus-newsgroup-headers))
9494       (while (and (cdr headers)
9495                   (not (= number (mail-header-number (cadr headers)))))
9496         (pop headers))
9497       (when (cdr headers)
9498         (setcdr headers (cddr headers))))))
9499
9500 ;;;
9501 ;;; summary highlights
9502 ;;;
9503
9504 (defun gnus-highlight-selected-summary ()
9505   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9506   ;; Highlight selected article in summary buffer
9507   (when gnus-summary-selected-face
9508     (save-excursion
9509       (let* ((beg (progn (beginning-of-line) (point)))
9510              (end (progn (end-of-line) (point)))
9511              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9512              (from (if (get-text-property beg gnus-mouse-face-prop)
9513                        beg
9514                      (or (next-single-property-change
9515                           beg gnus-mouse-face-prop nil end)
9516                          beg)))
9517              (to
9518               (if (= from end)
9519                   (- from 2)
9520                 (or (next-single-property-change
9521                      from gnus-mouse-face-prop nil end)
9522                     end))))
9523         ;; If no mouse-face prop on line we will have to = from = end,
9524         ;; so we highlight the entire line instead.
9525         (when (= (+ to 2) from)
9526           (setq from beg)
9527           (setq to end))
9528         (if gnus-newsgroup-selected-overlay
9529             ;; Move old overlay.
9530             (gnus-move-overlay
9531              gnus-newsgroup-selected-overlay from to (current-buffer))
9532           ;; Create new overlay.
9533           (gnus-overlay-put
9534            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9535            'face gnus-summary-selected-face))))))
9536
9537 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9538 (defun gnus-summary-highlight-line ()
9539   "Highlight current line according to `gnus-summary-highlight'."
9540   (let* ((list gnus-summary-highlight)
9541          (p (point))
9542          (end (progn (end-of-line) (point)))
9543          ;; now find out where the line starts and leave point there.
9544          (beg (progn (beginning-of-line) (point)))
9545          (article (gnus-summary-article-number))
9546          (score (or (cdr (assq (or article gnus-current-article)
9547                                gnus-newsgroup-scored))
9548                     gnus-summary-default-score 0))
9549          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9550          (inhibit-read-only t))
9551     ;; Eval the cars of the lists until we find a match.
9552     (let ((default gnus-summary-default-score))
9553       (while (and list
9554                   (not (eval (caar list))))
9555         (setq list (cdr list))))
9556     (let ((face (cdar list)))
9557       (unless (eq face (get-text-property beg 'face))
9558         (gnus-put-text-property-excluding-characters-with-faces
9559          beg end 'face
9560          (setq face (if (boundp face) (symbol-value face) face)))
9561         (when gnus-summary-highlight-line-function
9562           (funcall gnus-summary-highlight-line-function article face))))
9563     (goto-char p)))
9564
9565 (defun gnus-update-read-articles (group unread &optional compute)
9566   "Update the list of read articles in GROUP."
9567   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9568          (entry (gnus-gethash group gnus-newsrc-hashtb))
9569          (info (nth 2 entry))
9570          (prev 1)
9571          (unread (sort (copy-sequence unread) '<))
9572          read)
9573     (if (or (not info) (not active))
9574         ;; There is no info on this group if it was, in fact,
9575         ;; killed.  Gnus stores no information on killed groups, so
9576         ;; there's nothing to be done.
9577         ;; One could store the information somewhere temporarily,
9578         ;; perhaps...  Hmmm...
9579         ()
9580       ;; Remove any negative articles numbers.
9581       (while (and unread (< (car unread) 0))
9582         (setq unread (cdr unread)))
9583       ;; Remove any expired article numbers
9584       (while (and unread (< (car unread) (car active)))
9585         (setq unread (cdr unread)))
9586       ;; Compute the ranges of read articles by looking at the list of
9587       ;; unread articles.
9588       (while unread
9589         (when (/= (car unread) prev)
9590           (push (if (= prev (1- (car unread))) prev
9591                   (cons prev (1- (car unread))))
9592                 read))
9593         (setq prev (1+ (car unread)))
9594         (setq unread (cdr unread)))
9595       (when (<= prev (cdr active))
9596         (push (cons prev (cdr active)) read))
9597       (setq read (if (> (length read) 1) (nreverse read) read))
9598       (if compute
9599           read
9600         (save-excursion
9601           (set-buffer gnus-group-buffer)
9602           (gnus-undo-register
9603             `(progn
9604                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9605                (gnus-info-set-read ',info ',(gnus-info-read info))
9606                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9607                (gnus-group-update-group ,group t))))
9608         ;; Propagate the read marks to the backend.
9609         (if (gnus-check-backend-function 'request-set-mark group)
9610             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9611                   (add (gnus-remove-from-range read (gnus-info-read info))))
9612               (when (or add del)
9613                 (unless (gnus-check-group group)
9614                   (error "Can't open server for %s" group))
9615                 (gnus-request-set-mark
9616                  group (delq nil (list (if add (list add 'add '(read)))
9617                                        (if del (list del 'del '(read)))))))))
9618         ;; Enter this list into the group info.
9619         (gnus-info-set-read info read)
9620         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9621         (gnus-get-unread-articles-in-group info (gnus-active group))
9622         t))))
9623
9624 (defun gnus-offer-save-summaries ()
9625   "Offer to save all active summary buffers."
9626   (save-excursion
9627     (let ((buflist (buffer-list))
9628           buffers bufname)
9629       ;; Go through all buffers and find all summaries.
9630       (while buflist
9631         (and (setq bufname (buffer-name (car buflist)))
9632              (string-match "Summary" bufname)
9633              (save-excursion
9634                (set-buffer bufname)
9635                ;; We check that this is, indeed, a summary buffer.
9636                (and (eq major-mode 'gnus-summary-mode)
9637                     ;; Also make sure this isn't bogus.
9638                     gnus-newsgroup-prepared
9639                     ;; Also make sure that this isn't a dead summary buffer.
9640                     (not gnus-dead-summary-mode)))
9641              (push bufname buffers))
9642         (setq buflist (cdr buflist)))
9643       ;; Go through all these summary buffers and offer to save them.
9644       (when buffers
9645         (map-y-or-n-p
9646          "Update summary buffer %s? "
9647          (lambda (buf)
9648            (switch-to-buffer buf)
9649            (gnus-summary-exit))
9650          buffers)))))
9651
9652
9653 ;;; @ for mime-partial
9654 ;;;
9655
9656 (defun gnus-request-partial-message ()
9657   (save-excursion
9658     (let ((number (gnus-summary-article-number))
9659           (group gnus-newsgroup-name)
9660           (mother gnus-article-buffer))
9661       (set-buffer (get-buffer-create " *Partial Article*"))
9662       (erase-buffer)
9663       (setq mime-preview-buffer mother)
9664       (gnus-request-article-this-buffer number group)
9665       (mime-parse-buffer)
9666       )))
9667
9668 (autoload 'mime-combine-message/partial-pieces-automatically
9669   "mime-partial"
9670   "Internal method to combine message/partial messages automatically.")
9671
9672 (mime-add-condition
9673  'action '((type . message)(subtype . partial)
9674            (major-mode . gnus-original-article-mode)
9675            (method . mime-combine-message/partial-pieces-automatically)
9676            (summary-buffer-exp . gnus-summary-buffer)
9677            (request-partial-message-method . gnus-request-partial-message)
9678            ))
9679
9680
9681 ;;; @ for message/rfc822
9682 ;;;
9683
9684 (defun gnus-mime-extract-message/rfc822 (entity situation)
9685   (let (group article num cwin swin cur)
9686     (with-current-buffer (mime-entity-buffer entity)
9687       (save-restriction
9688         (narrow-to-region (mime-entity-body-start entity)
9689                           (mime-entity-body-end entity))
9690         (setq group (or (cdr (assq 'group situation))
9691                         (completing-read "Group: "
9692                                          gnus-active-hashtb
9693                                          nil
9694                                          (gnus-read-active-file-p)
9695                                          gnus-newsgroup-name))
9696               article (gnus-request-accept-article group)
9697               )
9698         ))
9699     (when (and (consp article)
9700                (numberp (setq article (cdr article))))
9701       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9702             cwin (get-buffer-window (current-buffer) t)
9703             )
9704       (save-window-excursion
9705         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9706             (select-window swin)
9707           (set-buffer gnus-summary-buffer)
9708           )
9709         (setq cur gnus-current-article)
9710         (forward-line num)
9711         (let (gnus-show-threads)
9712           (gnus-summary-goto-subject article t)
9713           )
9714         (gnus-summary-clear-mark-forward 1)
9715         (gnus-summary-goto-subject cur)
9716         )
9717       (when (and cwin (window-frame cwin))
9718         (select-frame (window-frame cwin))
9719         )
9720       (when (boundp 'mime-acting-situation-to-override)
9721         (set-alist 'mime-acting-situation-to-override
9722                    'group
9723                    group)
9724         (set-alist 'mime-acting-situation-to-override
9725                    'after-method
9726                    `(progn
9727                       (save-current-buffer
9728                         (set-buffer gnus-group-buffer)
9729                         (gnus-activate-group ,group)
9730                         )
9731                       (gnus-summary-goto-article ,cur
9732                                                  gnus-show-all-headers)
9733                       ))
9734         (set-alist 'mime-acting-situation-to-override
9735                    'number num)
9736         )
9737       )))
9738
9739 (mime-add-condition
9740  'action '((type . message)(subtype . rfc822)
9741            (major-mode . gnus-original-article-mode)
9742            (method . gnus-mime-extract-message/rfc822)
9743            (mode . "extract")
9744            ))
9745
9746 (mime-add-condition
9747  'action '((type . message)(subtype . news)
9748            (major-mode . gnus-original-article-mode)
9749            (method . gnus-mime-extract-message/rfc822)
9750            (mode . "extract")
9751            ))
9752
9753 (defun gnus-mime-extract-multipart (entity situation)
9754   (let ((children (mime-entity-children entity))
9755         mime-acting-situation-to-override
9756         f)
9757     (while children
9758       (mime-play-entity (car children)
9759                         (cons (assq 'mode situation)
9760                               mime-acting-situation-to-override))
9761       (setq children (cdr children)))
9762     (if (setq f (cdr (assq 'after-method
9763                            mime-acting-situation-to-override)))
9764         (eval f)
9765       )))  
9766
9767 (mime-add-condition
9768  'action '((type . multipart)
9769            (method . gnus-mime-extract-multipart)
9770            (mode . "extract")
9771            )
9772  'with-default)
9773
9774
9775 ;;; @ end
9776 ;;;
9777
9778 (defun gnus-summary-setup-default-charset ()
9779   "Setup newsgroup default charset."
9780   (if (equal gnus-newsgroup-name "nndraft:drafts")
9781       (setq gnus-newsgroup-charset nil)
9782     (let* ((name (and gnus-newsgroup-name
9783                       (gnus-group-real-name gnus-newsgroup-name)))
9784            (ignored-charsets 
9785             (or gnus-newsgroup-ephemeral-ignored-charsets
9786                 (append
9787                  (and gnus-newsgroup-name
9788                       (or (gnus-group-find-parameter gnus-newsgroup-name
9789                                                      'ignored-charsets t)
9790                           (let ((alist gnus-group-ignored-charsets-alist)
9791                                 elem (charsets nil))
9792                             (while (setq elem (pop alist))
9793                               (when (and name
9794                                          (string-match (car elem) name))
9795                                 (setq alist nil
9796                                       charsets (cdr elem))))
9797                             charsets)))
9798                  gnus-newsgroup-ignored-charsets))))
9799       (setq gnus-newsgroup-charset
9800             (or gnus-newsgroup-ephemeral-charset
9801                 (and gnus-newsgroup-name
9802                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9803                          (let ((alist gnus-group-charset-alist)
9804                                elem charset)
9805                            (while (setq elem (pop alist))
9806                              (when (and name
9807                                         (string-match (car elem) name))
9808                                (setq alist nil
9809                                      charset (cadr elem))))
9810                            charset)))
9811                 gnus-default-charset))
9812       (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9813            ignored-charsets))))
9814
9815 ;;;
9816 ;;; Mime Commands
9817 ;;;
9818
9819 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9820   "Display the current article buffer fully MIME-buttonized.
9821 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9822 treated as multipart/mixed."
9823   (interactive "P")
9824   (require 'gnus-art)
9825   (let ((gnus-unbuttonized-mime-types nil)
9826         (gnus-mime-display-multipart-as-mixed show-all-parts))
9827     (gnus-summary-show-article)))
9828
9829 (defun gnus-summary-repair-multipart (article)
9830   "Add a Content-Type header to a multipart article without one."
9831   (interactive (list (gnus-summary-article-number)))
9832   (gnus-with-article article
9833     (message-narrow-to-head)
9834     (goto-char (point-max))
9835     (widen)
9836     (when (search-forward "\n--" nil t)
9837       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9838         (message-narrow-to-head)
9839         (message-remove-header "Mime-Version")
9840         (message-remove-header "Content-Type")
9841         (goto-char (point-max))
9842         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9843                         separator))
9844         (insert "Mime-Version: 1.0\n")
9845         (widen))))
9846   (let (gnus-mark-article-hook)
9847     (gnus-summary-select-article t t nil article)))
9848
9849 (defun gnus-summary-toggle-display-buttonized ()
9850   "Toggle the buttonizing of the article buffer."
9851   (interactive)
9852   (require 'gnus-art)
9853   (if (setq gnus-inhibit-mime-unbuttonizing
9854             (not gnus-inhibit-mime-unbuttonizing))
9855       (let ((gnus-unbuttonized-mime-types nil))
9856         (gnus-summary-show-article))
9857     (gnus-summary-show-article)))
9858
9859 ;;;
9860 ;;; Intelli-mouse commmands
9861 ;;;
9862
9863 (defun gnus-wheel-summary-scroll (event)
9864   (interactive "e")
9865   (let ((amount (if (memq 'shift (event-modifiers event))
9866                     (car gnus-wheel-scroll-amount)
9867                   (cdr gnus-wheel-scroll-amount)))
9868         (direction (- (* (static-if (featurep 'xemacs)
9869                              (event-button event)
9870                            (cond ((eq 'mouse-4 (event-basic-type event))
9871                                   4)
9872                                  ((eq 'mouse-5 (event-basic-type event))
9873                                   5)))
9874                          2) 9))
9875         edge)
9876     (gnus-summary-scroll-up (* amount direction))
9877     (when (gnus-eval-in-buffer-window gnus-article-buffer
9878             (save-restriction
9879               (widen)
9880               (and (if (< 0 direction)
9881                        (gnus-article-next-page 0)
9882                      (gnus-article-prev-page 0)
9883                      (bobp))
9884                    (if (setq edge (get-text-property
9885                                    (point-min) 'gnus-wheel-edge))
9886                        (setq edge (* edge direction))
9887                      (setq edge -1))
9888                    (or (plusp edge)
9889                        (let ((buffer-read-only nil)
9890                              (inhibit-read-only t))
9891                          (put-text-property (point-min) (point-max)
9892                                             'gnus-wheel-edge direction)
9893                          nil))
9894                    (or (> edge gnus-wheel-edge-resistance)
9895                        (let ((buffer-read-only nil)
9896                              (inhibit-read-only t))
9897                          (put-text-property (point-min) (point-max)
9898                                             'gnus-wheel-edge
9899                                             (* (1+ edge) direction))
9900                          nil))
9901                    (eq last-command 'gnus-wheel-summary-scroll))))
9902       (gnus-summary-next-article nil nil (minusp direction)))))
9903
9904 (defun gnus-wheel-install ()
9905   "Enable mouse wheel support on summary window."
9906   (when gnus-use-wheel
9907     (let ((keys 
9908            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9909       (dolist (key keys)
9910         (define-key gnus-summary-mode-map key
9911           'gnus-wheel-summary-scroll)))))
9912
9913 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9914
9915 ;;;
9916 ;;; Traditional PGP commmands
9917 ;;;
9918
9919 (defun gnus-summary-decrypt-article (&optional force)
9920   "Decrypt the current article in traditional PGP way.
9921 This will have permanent effect only in mail groups.
9922 If FORCE is non-nil, allow editing of articles even in read-only
9923 groups."
9924   (interactive "P")
9925   (gnus-summary-select-article t)
9926   (gnus-eval-in-buffer-window gnus-article-buffer
9927     (save-excursion
9928       (save-restriction
9929         (widen)
9930         (goto-char (point-min))
9931         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
9932           (error "Not a traditional PGP message!"))
9933         (let ((armor-start (match-beginning 0)))
9934           (if (and (pgg-decrypt-region armor-start (point-max))
9935                    (or force (not (gnus-group-read-only-p))))
9936               (let ((inhibit-read-only t) 
9937                     buffer-read-only)
9938                 (delete-region armor-start
9939                                (progn 
9940                                  (re-search-forward "^-+END PGP" nil t)
9941                                  (beginning-of-line 2)
9942                                  (point)))
9943                 (insert-buffer-substring pgg-output-buffer))))))))
9944
9945 (defun gnus-summary-verify-article ()
9946   "Verify the current article in traditional PGP way."
9947   (interactive)
9948   (save-excursion
9949     (set-buffer gnus-original-article-buffer)
9950     (goto-char (point-min))
9951     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
9952       (error "Not a traditional PGP message!"))
9953     (re-search-forward "^-+END PGP" nil t)
9954     (beginning-of-line 2)
9955     (call-interactively (function pgg-verify-region))))
9956
9957 ;;;
9958 ;;; Generic summary marking commands
9959 ;;;
9960
9961 (defvar gnus-summary-marking-alist
9962   '((read gnus-del-mark "d")
9963     (unread gnus-unread-mark "u")
9964     (ticked gnus-ticked-mark "!")
9965     (dormant gnus-dormant-mark "?")
9966     (expirable gnus-expirable-mark "e"))
9967   "An alist of names/marks/keystrokes.")
9968
9969 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9970 (defvar gnus-summary-mark-map)
9971
9972 (defun gnus-summary-make-all-marking-commands ()
9973   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9974   (dolist (elem gnus-summary-marking-alist)
9975     (apply 'gnus-summary-make-marking-command elem)))
9976
9977 (defun gnus-summary-make-marking-command (name mark keystroke)
9978   (let ((map (make-sparse-keymap)))
9979     (define-key gnus-summary-generic-mark-map keystroke map)
9980     (dolist (lway `((next "next" next nil "n")
9981                     (next-unread "next unread" next t "N")
9982                     (prev "previous" prev nil "p")
9983                     (prev-unread "previous unread" prev t "P")
9984                     (nomove "" nil nil ,keystroke)))
9985       (let ((func (gnus-summary-make-marking-command-1
9986                    mark (car lway) lway name)))
9987         (setq func (eval func))
9988         (define-key map (nth 4 lway) func)))))
9989       
9990 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9991   `(defun ,(intern
9992             (format "gnus-summary-put-mark-as-%s%s"
9993                     name (if (eq way 'nomove)
9994                              ""
9995                            (concat "-" (symbol-name way)))))
9996      (n)
9997      ,(format
9998        "Mark the current article as %s%s.
9999 If N, the prefix, then repeat N times.
10000 If N is negative, move in reverse order.
10001 The difference between N and the actual number of articles marked is
10002 returned."
10003        name (car (cdr lway)))
10004      (interactive "p")
10005      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10006     
10007 (defun gnus-summary-generic-mark (n mark move unread)
10008   "Mark N articles with MARK."
10009   (unless (eq major-mode 'gnus-summary-mode)
10010     (error "This command can only be used in the summary buffer"))
10011   (gnus-summary-show-thread)
10012   (let ((nummove
10013          (cond
10014           ((eq move 'next) 1)
10015           ((eq move 'prev) -1)
10016           (t 0))))
10017     (if (zerop nummove)
10018         (setq n 1)
10019       (when (< n 0)
10020         (setq n (abs n)
10021               nummove (* -1 nummove))))
10022     (while (and (> n 0)
10023                 (gnus-summary-mark-article nil mark)
10024                 (zerop (gnus-summary-next-subject nummove unread t)))
10025       (setq n (1- n)))
10026     (when (/= 0 n)
10027       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10028     (gnus-summary-recenter)
10029     (gnus-summary-position-point)
10030     (gnus-set-mode-line 'summary)
10031     n))
10032
10033 (gnus-summary-make-all-marking-commands)
10034
10035 (gnus-ems-redefine)
10036
10037 (provide 'gnus-sum)
10038
10039 (run-hooks 'gnus-sum-load-hook)
10040
10041 ;;; gnus-sum.el ends here