* gnus-group.el (gnus-group-is-exiting-without-update-p): New.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
50 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
51 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
52
53 (defcustom gnus-kill-summary-on-exit t
54   "*If non-nil, kill the summary buffer when you exit from it.
55 If nil, the summary will become a \"*Dead Summary*\" buffer, and
56 it will be killed sometime later."
57   :group 'gnus-summary-exit
58   :type 'boolean)
59
60 (defcustom gnus-fetch-old-headers nil
61   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
62 If an unread article in the group refers to an older, already read (or
63 just marked as read) article, the old article will not normally be
64 displayed in the Summary buffer.  If this variable is non-nil, Gnus
65 will attempt to grab the headers to the old articles, and thereby
66 build complete threads.  If it has the value `some', only enough
67 headers to connect otherwise loose threads will be displayed.  This
68 variable can also be a number.  In that case, no more than that number
69 of old headers will be fetched.  If it has the value `invisible', all
70 old headers will be fetched, but none will be displayed.
71
72 The server has to support NOV for any of this to work."
73   :group 'gnus-thread
74   :type '(choice (const :tag "off" nil)
75                  (const some)
76                  number
77                  (sexp :menu-tag "other" t)))
78
79 (defcustom gnus-refer-thread-limit 200
80   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
81 If t, fetch all the available old headers."
82   :group 'gnus-thread
83   :type '(choice number
84                  (sexp :menu-tag "other" t)))
85
86 (defcustom gnus-summary-make-false-root 'adopt
87   "*nil means that Gnus won't gather loose threads.
88 If the root of a thread has expired or been read in a previous
89 session, the information necessary to build a complete thread has been
90 lost.  Instead of having many small sub-threads from this original thread
91 scattered all over the summary buffer, Gnus can gather them.
92
93 If non-nil, Gnus will try to gather all loose sub-threads from an
94 original thread into one large thread.
95
96 If this variable is non-nil, it should be one of `none', `adopt',
97 `dummy' or `empty'.
98
99 If this variable is `none', Gnus will not make a false root, but just
100 present the sub-threads after another.
101 If this variable is `dummy', Gnus will create a dummy root that will
102 have all the sub-threads as children.
103 If this variable is `adopt', Gnus will make one of the \"children\"
104 the parent and mark all the step-children as such.
105 If this variable is `empty', the \"children\" are printed with empty
106 subject fields.  (Or rather, they will be printed with a string
107 given by the `gnus-summary-same-subject' variable.)"
108   :group 'gnus-thread
109   :type '(choice (const :tag "off" nil)
110                  (const none)
111                  (const dummy)
112                  (const adopt)
113                  (const empty)))
114
115 (defcustom gnus-summary-make-false-root-always nil
116   "Always make a false dummy root."
117   :group 'gnus-thread
118   :type 'boolean)
119
120 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
121   "*A regexp to match subjects to be excluded from loose thread gathering.
122 As loose thread gathering is done on subjects only, that means that
123 there can be many false gatherings performed.  By rooting out certain
124 common subjects, gathering might become saner."
125   :group 'gnus-thread
126   :type 'regexp)
127
128 (defcustom gnus-summary-gather-subject-limit nil
129   "*Maximum length of subject comparisons when gathering loose threads.
130 Use nil to compare full subjects.  Setting this variable to a low
131 number will help gather threads that have been corrupted by
132 newsreaders chopping off subject lines, but it might also mean that
133 unrelated articles that have subject that happen to begin with the
134 same few characters will be incorrectly gathered.
135
136 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
137 comparing subjects."
138   :group 'gnus-thread
139   :type '(choice (const :tag "off" nil)
140                  (const fuzzy)
141                  (sexp :menu-tag "on" t)))
142
143 (defcustom gnus-simplify-subject-functions nil
144   "List of functions taking a string argument that simplify subjects.
145 The functions are applied recursively.
146
147 Useful functions to put in this list include:
148 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
149 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
150   :group 'gnus-thread
151   :type '(repeat function))
152
153 (defcustom gnus-simplify-ignored-prefixes nil
154   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
155   :group 'gnus-thread
156   :type '(choice (const :tag "off" nil)
157                  regexp))
158
159 (defcustom gnus-build-sparse-threads nil
160   "*If non-nil, fill in the gaps in threads.
161 If `some', only fill in the gaps that are needed to tie loose threads
162 together.  If `more', fill in all leaf nodes that Gnus can find.  If
163 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
164   :group 'gnus-thread
165   :type '(choice (const :tag "off" nil)
166                  (const some)
167                  (const more)
168                  (sexp :menu-tag "all" t)))
169
170 (defcustom gnus-summary-thread-gathering-function
171   'gnus-gather-threads-by-subject
172   "*Function used for gathering loose threads.
173 There are two pre-defined functions: `gnus-gather-threads-by-subject',
174 which only takes Subjects into consideration; and
175 `gnus-gather-threads-by-references', which compared the References
176 headers of the articles to find matches."
177   :group 'gnus-thread
178   :type '(radio (function-item gnus-gather-threads-by-subject)
179                 (function-item gnus-gather-threads-by-references)
180                 (function :tag "other")))
181
182 (defcustom gnus-summary-same-subject ""
183   "*String indicating that the current article has the same subject as the previous.
184 This variable will only be used if the value of
185 `gnus-summary-make-false-root' is `empty'."
186   :group 'gnus-summary-format
187   :type 'string)
188
189 (defcustom gnus-summary-goto-unread t
190   "*If t, many commands will go to the next unread article.
191 This applies to marking commands as well as other commands that
192 \"naturally\" select the next article, like, for instance, `SPC' at
193 the end of an article.
194
195 If nil, the marking commands do NOT go to the next unread article
196 \(they go to the next article instead).  If `never', commands that
197 usually go to the next unread article, will go to the next article,
198 whether it is read or not."
199   :group 'gnus-summary-marks
200   :link '(custom-manual "(gnus)Setting Marks")
201   :type '(choice (const :tag "off" nil)
202                  (const never)
203                  (sexp :menu-tag "on" t)))
204
205 (defcustom gnus-summary-default-score 0
206   "*Default article score level.
207 All scores generated by the score files will be added to this score.
208 If this variable is nil, scoring will be disabled."
209   :group 'gnus-score-default
210   :type '(choice (const :tag "disable")
211                  integer))
212
213 (defcustom gnus-summary-default-high-score 0
214   "*Default threshold for a high scored article.
215 An article will be highlighted as high scored if its score is greater
216 than this score."
217   :group 'gnus-score-default
218   :type 'integer)
219
220 (defcustom gnus-summary-default-low-score 0
221   "*Default threshold for a low scored article.
222 An article will be highlighted as low scored if its score is smaller
223 than this score."
224   :group 'gnus-score-default
225   :type 'integer)
226
227 (defcustom gnus-summary-zcore-fuzz 0
228   "*Fuzziness factor for the zcore in the summary buffer.
229 Articles with scores closer than this to `gnus-summary-default-score'
230 will not be marked."
231   :group 'gnus-summary-format
232   :type 'integer)
233
234 (defcustom gnus-simplify-subject-fuzzy-regexp nil
235   "*Strings to be removed when doing fuzzy matches.
236 This can either be a regular expression or list of regular expressions
237 that will be removed from subject strings if fuzzy subject
238 simplification is selected."
239   :group 'gnus-thread
240   :type '(repeat regexp))
241
242 (defcustom gnus-show-threads t
243   "*If non-nil, display threads in summary mode."
244   :group 'gnus-thread
245   :type 'boolean)
246
247 (defcustom gnus-thread-hide-subtree nil
248   "*If non-nil, hide all threads initially.
249 This can be a predicate specifier which says which threads to hide.
250 If threads are hidden, you have to run the command
251 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
252 to expose hidden threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-hide-killed t
257   "*If non-nil, hide killed threads automatically."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-ignore-subject t
262   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
263 If nil, articles that have different subjects from their parents will
264 start separate threads."
265   :group 'gnus-thread
266   :type 'boolean)
267
268 (defcustom gnus-thread-operation-ignore-subject t
269   "*If non-nil, subjects will be ignored when doing thread commands.
270 This affects commands like `gnus-summary-kill-thread' and
271 `gnus-summary-lower-thread'.
272
273 If this variable is nil, articles in the same thread with different
274 subjects will not be included in the operation in question.  If this
275 variable is `fuzzy', only articles that have subjects that are fuzzily
276 equal will be included."
277   :group 'gnus-thread
278   :type '(choice (const :tag "off" nil)
279                  (const fuzzy)
280                  (sexp :tag "on" t)))
281
282 (defcustom gnus-thread-indent-level 4
283   "*Number that says how much each sub-thread should be indented."
284   :group 'gnus-thread
285   :type 'integer)
286
287 (defcustom gnus-auto-extend-newsgroup t
288   "*If non-nil, extend newsgroup forward and backward when requested."
289   :group 'gnus-summary-choose
290   :type 'boolean)
291
292 (defcustom gnus-auto-select-first t
293   "*If non-nil, select the article under point.
294 Which article this is is controlled by the `gnus-auto-select-subject'
295 variable.
296
297 If you want to prevent automatic selection of articles in some
298 newsgroups, set the variable to nil in `gnus-select-group-hook'."
299   :group 'gnus-group-select
300   :type '(choice (const :tag "none" nil)
301                  (sexp :menu-tag "first" t)))
302
303 (defcustom gnus-auto-select-subject 'unread
304   "*Says what subject to place under point when entering a group.
305
306 This variable can either be the symbols `first' (place point on the
307 first subject), `unread' (place point on the subject line of the first
308 unread article), `best' (place point on the subject line of the
309 higest-scored article), `unseen' (place point on the subject line of
310 the first unseen article), 'unseen-or-unread' (place point on the subject
311 line of the first unseen article or, if all article have been seen, on the
312 subject line of the first unread article), or a function to be called to
313 place point on some subject line."
314   :group 'gnus-group-select
315   :type '(choice (const best)
316                  (const unread)
317                  (const first)
318                  (const unseen)
319                  (const unseen-or-unread)))
320
321 (defcustom gnus-auto-select-next t
322   "*If non-nil, offer to go to the next group from the end of the previous.
323 If the value is t and the next newsgroup is empty, Gnus will exit
324 summary mode and go back to group mode.  If the value is neither nil
325 nor t, Gnus will select the following unread newsgroup.  In
326 particular, if the value is the symbol `quietly', the next unread
327 newsgroup will be selected without any confirmation, and if it is
328 `almost-quietly', the next group will be selected without any
329 confirmation if you are located on the last article in the group.
330 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
331 will go to the next group without confirmation."
332   :group 'gnus-summary-maneuvering
333   :type '(choice (const :tag "off" nil)
334                  (const quietly)
335                  (const almost-quietly)
336                  (const slightly-quietly)
337                  (sexp :menu-tag "on" t)))
338
339 (defcustom gnus-auto-select-same nil
340   "*If non-nil, select the next article with the same subject.
341 If there are no more articles with the same subject, go to
342 the first unread article."
343   :group 'gnus-summary-maneuvering
344   :type 'boolean)
345
346 (defcustom gnus-summary-check-current nil
347   "*If non-nil, consider the current article when moving.
348 The \"unread\" movement commands will stay on the same line if the
349 current article is unread."
350   :group 'gnus-summary-maneuvering
351   :type 'boolean)
352
353 (defcustom gnus-auto-center-summary t
354   "*If non-nil, always center the current summary buffer.
355 In particular, if `vertical' do only vertical recentering.  If non-nil
356 and non-`vertical', do both horizontal and vertical recentering."
357   :group 'gnus-summary-maneuvering
358   :type '(choice (const :tag "none" nil)
359                  (const vertical)
360                  (integer :tag "height")
361                  (sexp :menu-tag "both" t)))
362
363 (defvar gnus-auto-center-group t
364   "*If non-nil, always center the group buffer.")
365
366 (defcustom gnus-show-all-headers nil
367   "*If non-nil, don't hide any headers."
368   :group 'gnus-article-hiding
369   :group 'gnus-article-headers
370   :type 'boolean)
371
372 (defcustom gnus-summary-ignore-duplicates nil
373   "*If non-nil, ignore articles with identical Message-ID headers."
374   :group 'gnus-summary
375   :type 'boolean)
376
377 (defcustom gnus-single-article-buffer t
378   "*If non-nil, display all articles in the same buffer.
379 If nil, each group will get its own article buffer."
380   :group 'gnus-article-various
381   :type 'boolean)
382
383 (defcustom gnus-break-pages t
384   "*If non-nil, do page breaking on articles.
385 The page delimiter is specified by the `gnus-page-delimiter'
386 variable."
387   :group 'gnus-article-various
388   :type 'boolean)
389
390 (defcustom gnus-move-split-methods nil
391   "*Variable used to suggest where articles are to be moved to.
392 It uses the same syntax as the `gnus-split-methods' variable.
393 However, whereas `gnus-split-methods' specifies file names as targets,
394 this variable specifies group names."
395   :group 'gnus-summary-mail
396   :type '(repeat (choice (list :value (fun) function)
397                          (cons :value ("" "") regexp (repeat string))
398                          (sexp :value nil))))
399
400 (defcustom gnus-unread-mark ?           ;Whitespace
401   "*Mark used for unread articles."
402   :group 'gnus-summary-marks
403   :type 'character)
404
405 (defcustom gnus-ticked-mark ?!
406   "*Mark used for ticked articles."
407   :group 'gnus-summary-marks
408   :type 'character)
409
410 (defcustom gnus-dormant-mark ??
411   "*Mark used for dormant articles."
412   :group 'gnus-summary-marks
413   :type 'character)
414
415 (defcustom gnus-del-mark ?r
416   "*Mark used for del'd articles."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-read-mark ?R
421   "*Mark used for read articles."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-expirable-mark ?E
426   "*Mark used for expirable articles."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-killed-mark ?K
431   "*Mark used for killed articles."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-spam-mark ?$
436   "*Mark used for spam articles."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-souped-mark ?F
441   "*Mark used for souped articles."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-kill-file-mark ?X
446   "*Mark used for articles killed by kill files."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-low-score-mark ?Y
451   "*Mark used for articles with a low score."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-catchup-mark ?C
456   "*Mark used for articles that are caught up."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-replied-mark ?A
461   "*Mark used for articles that have been replied to."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-forwarded-mark ?F
466   "*Mark used for articles that have been forwarded."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-recent-mark ?N
471   "*Mark used for articles that are recent."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-cached-mark ?*
476   "*Mark used for articles that are in the cache."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-saved-mark ?S
481   "*Mark used for articles that have been saved."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-unseen-mark ?.
486   "*Mark used for articles that haven't been seen."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-no-mark ?               ;Whitespace
491   "*Mark used for articles that have no other secondary mark."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-ancient-mark ?O
496   "*Mark used for ancient articles."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-sparse-mark ?Q
501   "*Mark used for sparsely reffed articles."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-canceled-mark ?G
506   "*Mark used for canceled articles."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-duplicate-mark ?M
511   "*Mark used for duplicate articles."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-undownloaded-mark ?-
516   "*Mark used for articles that weren't downloaded."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-downloaded-mark ?+
521   "*Mark used for articles that were downloaded."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-downloadable-mark ?%
526   "*Mark used for articles that are to be downloaded."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-unsendable-mark ?=
531   "*Mark used for articles that won't be sent."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-score-over-mark ?+
536   "*Score mark used for articles with high scores."
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-score-below-mark ?-
541   "*Score mark used for articles with low scores."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-empty-thread-mark ?     ;Whitespace
546   "*There is no thread under the article."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-not-empty-thread-mark ?=
551   "*There is a thread under the article."
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-view-pseudo-asynchronously nil
556   "*If non-nil, Gnus will view pseudo-articles asynchronously."
557   :group 'gnus-extract-view
558   :type 'boolean)
559
560 (defcustom gnus-auto-expirable-marks
561   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
562         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
563         gnus-souped-mark gnus-duplicate-mark)
564   "*The list of marks converted into expiration if a group is auto-expirable."
565   :version "21.1"
566   :group 'gnus-summary
567   :type '(repeat character))
568
569 (defcustom gnus-inhibit-user-auto-expire t
570   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
571   :version "21.1"
572   :group 'gnus-summary
573   :type 'boolean)
574
575 (defcustom gnus-view-pseudos nil
576   "*If `automatic', pseudo-articles will be viewed automatically.
577 If `not-confirm', pseudos will be viewed automatically, and the user
578 will not be asked to confirm the command."
579   :group 'gnus-extract-view
580   :type '(choice (const :tag "off" nil)
581                  (const automatic)
582                  (const not-confirm)))
583
584 (defcustom gnus-view-pseudos-separately t
585   "*If non-nil, one pseudo-article will be created for each file to be viewed.
586 If nil, all files that use the same viewing command will be given as a
587 list of parameters to that command."
588   :group 'gnus-extract-view
589   :type 'boolean)
590
591 (defcustom gnus-insert-pseudo-articles t
592   "*If non-nil, insert pseudo-articles when decoding articles."
593   :group 'gnus-extract-view
594   :type 'boolean)
595
596 (defcustom gnus-summary-dummy-line-format
597   "   %(:                             :%) %S\n"
598   "*The format specification for the dummy roots in the summary buffer.
599 It works along the same lines as a normal formatting string,
600 with some simple extensions.
601
602 %S  The subject
603
604 General format specifiers can also be used.
605 See `(gnus)Formatting Variables'."
606   :link '(custom-manual "(gnus)Formatting Variables")
607   :group 'gnus-threading
608   :type 'string)
609
610 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
611   "*The format specification for the summary mode line.
612 It works along the same lines as a normal formatting string,
613 with some simple extensions:
614
615 %G  Group name
616 %p  Unprefixed group name
617 %A  Current article number
618 %z  Current article score
619 %V  Gnus version
620 %U  Number of unread articles in the group
621 %e  Number of unselected articles in the group
622 %Z  A string with unread/unselected article counts
623 %g  Shortish group name
624 %S  Subject of the current article
625 %u  User-defined spec
626 %s  Current score file name
627 %d  Number of dormant articles
628 %r  Number of articles that have been marked as read in this session
629 %E  Number of articles expunged by the score files"
630   :group 'gnus-summary-format
631   :type 'string)
632
633 (defcustom gnus-list-identifiers nil
634   "Regexp that matches list identifiers to be removed from subject.
635 This can also be a list of regexps."
636   :version "21.1"
637   :group 'gnus-summary-format
638   :group 'gnus-article-hiding
639   :type '(choice (const :tag "none" nil)
640                  (regexp :value ".*")
641                  (repeat :value (".*") regexp)))
642
643 (defcustom gnus-summary-mark-below 0
644   "*Mark all articles with a score below this variable as read.
645 This variable is local to each summary buffer and usually set by the
646 score file."
647   :group 'gnus-score-default
648   :type 'integer)
649
650 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
651   "*List of functions used for sorting articles in the summary buffer.
652
653 Each function takes two articles and returns non-nil if the first
654 article should be sorted before the other.  If you use more than one
655 function, the primary sort function should be the last.  You should
656 probably always include `gnus-article-sort-by-number' in the list of
657 sorting functions -- preferably first.  Also note that sorting by date
658 is often much slower than sorting by number, and the sorting order is
659 very similar.  (Sorting by date means sorting by the time the message
660 was sent, sorting by number means sorting by arrival time.)
661
662 Ready-made functions include `gnus-article-sort-by-number',
663 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
664 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
665 and `gnus-article-sort-by-score'.
666
667 When threading is turned on, the variable `gnus-thread-sort-functions'
668 controls how articles are sorted."
669   :group 'gnus-summary-sort
670   :type '(repeat (choice (function-item gnus-article-sort-by-number)
671                          (function-item gnus-article-sort-by-author)
672                          (function-item gnus-article-sort-by-subject)
673                          (function-item gnus-article-sort-by-date)
674                          (function-item gnus-article-sort-by-score)
675                          (function-item gnus-article-sort-by-random)
676                          (function :tag "other"))))
677
678 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
679   "*List of functions used for sorting threads in the summary buffer.
680 By default, threads are sorted by article number.
681
682 Each function takes two threads and returns non-nil if the first
683 thread should be sorted before the other.  If you use more than one
684 function, the primary sort function should be the last.  You should
685 probably always include `gnus-thread-sort-by-number' in the list of
686 sorting functions -- preferably first.  Also note that sorting by date
687 is often much slower than sorting by number, and the sorting order is
688 very similar.  (Sorting by date means sorting by the time the message
689 was sent, sorting by number means sorting by arrival time.)
690
691 Ready-made functions include `gnus-thread-sort-by-number',
692 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
693 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
694 `gnus-thread-sort-by-most-recent-number',
695 `gnus-thread-sort-by-most-recent-date',
696 `gnus-thread-sort-by-random', and
697 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
698
699 When threading is turned off, the variable
700 `gnus-article-sort-functions' controls how articles are sorted."
701   :group 'gnus-summary-sort
702   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
703                          (function-item gnus-thread-sort-by-author)
704                          (function-item gnus-thread-sort-by-subject)
705                          (function-item gnus-thread-sort-by-date)
706                          (function-item gnus-thread-sort-by-score)
707                          (function-item gnus-thread-sort-by-total-score)
708                          (function-item gnus-thread-sort-by-random)
709                          (function :tag "other"))))
710
711 (defcustom gnus-thread-score-function '+
712   "*Function used for calculating the total score of a thread.
713
714 The function is called with the scores of the article and each
715 subthread and should then return the score of the thread.
716
717 Some functions you can use are `+', `max', or `min'."
718   :group 'gnus-summary-sort
719   :type 'function)
720
721 (defcustom gnus-summary-expunge-below nil
722   "All articles that have a score less than this variable will be expunged.
723 This variable is local to the summary buffers."
724   :group 'gnus-score-default
725   :type '(choice (const :tag "off" nil)
726                  integer))
727
728 (defcustom gnus-thread-expunge-below nil
729   "All threads that have a total score less than this variable will be expunged.
730 See `gnus-thread-score-function' for en explanation of what a
731 \"thread score\" is.
732
733 This variable is local to the summary buffers."
734   :group 'gnus-threading
735   :group 'gnus-score-default
736   :type '(choice (const :tag "off" nil)
737                  integer))
738
739 (defcustom gnus-summary-mode-hook nil
740   "*A hook for Gnus summary mode.
741 This hook is run before any variables are set in the summary buffer."
742   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
743   :group 'gnus-summary-various
744   :type 'hook)
745
746 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
747 (when (featurep 'xemacs)
748   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
749   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
750   (add-hook 'gnus-summary-mode-hook
751             'gnus-xmas-switch-horizontal-scrollbar-off))
752
753 (defcustom gnus-summary-menu-hook nil
754   "*Hook run after the creation of the summary mode menu."
755   :group 'gnus-summary-visual
756   :type 'hook)
757
758 (defcustom gnus-summary-exit-hook nil
759   "*A hook called on exit from the summary buffer.
760 It will be called with point in the group buffer."
761   :group 'gnus-summary-exit
762   :type 'hook)
763
764 (defcustom gnus-summary-prepare-hook nil
765   "*A hook called after the summary buffer has been generated.
766 If you want to modify the summary buffer, you can use this hook."
767   :group 'gnus-summary-various
768   :type 'hook)
769
770 (defcustom gnus-summary-prepared-hook nil
771   "*A hook called as the last thing after the summary buffer has been generated."
772   :group 'gnus-summary-various
773   :type 'hook)
774
775 (defcustom gnus-summary-generate-hook nil
776   "*A hook run just before generating the summary buffer.
777 This hook is commonly used to customize threading variables and the
778 like."
779   :group 'gnus-summary-various
780   :type 'hook)
781
782 (defcustom gnus-select-group-hook nil
783   "*A hook called when a newsgroup is selected.
784
785 If you'd like to simplify subjects like the
786 `gnus-summary-next-same-subject' command does, you can use the
787 following hook:
788
789  (add-hook gnus-select-group-hook
790            (lambda ()
791              (mapcar (lambda (header)
792                        (mail-header-set-subject
793                         header
794                         (gnus-simplify-subject
795                          (mail-header-subject header) 're-only)))
796                      gnus-newsgroup-headers)))"
797   :group 'gnus-group-select
798   :type 'hook)
799
800 (defcustom gnus-select-article-hook nil
801   "*A hook called when an article is selected."
802   :group 'gnus-summary-choose
803   :options '(gnus-agent-fetch-selected-article)
804   :type 'hook)
805
806 (defcustom gnus-visual-mark-article-hook
807   (list 'gnus-highlight-selected-summary)
808   "*Hook run after selecting an article in the summary buffer.
809 It is meant to be used for highlighting the article in some way.  It
810 is not run if `gnus-visual' is nil."
811   :group 'gnus-summary-visual
812   :type 'hook)
813
814 (defcustom gnus-parse-headers-hook nil
815   "*A hook called before parsing the headers."
816   :group 'gnus-various
817   :type 'hook)
818
819 (defcustom gnus-exit-group-hook nil
820   "*A hook called when exiting summary mode.
821 This hook is not called from the non-updating exit commands like `Q'."
822   :group 'gnus-various
823   :type 'hook)
824
825 (defcustom gnus-summary-update-hook
826   (list 'gnus-summary-highlight-line)
827   "*A hook called when a summary line is changed.
828 The hook will not be called if `gnus-visual' is nil.
829
830 The default function `gnus-summary-highlight-line' will
831 highlight the line according to the `gnus-summary-highlight'
832 variable."
833   :group 'gnus-summary-visual
834   :type 'hook)
835
836 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
837   "*A hook called when an article is selected for the first time.
838 The hook is intended to mark an article as read (or unread)
839 automatically when it is selected."
840   :group 'gnus-summary-choose
841   :type 'hook)
842
843 (defcustom gnus-group-no-more-groups-hook nil
844   "*A hook run when returning to group mode having no more (unread) groups."
845   :group 'gnus-group-select
846   :type 'hook)
847
848 (defcustom gnus-ps-print-hook nil
849   "*A hook run before ps-printing something from Gnus."
850   :group 'gnus-summary
851   :type 'hook)
852
853 (defcustom gnus-summary-article-move-hook nil
854   "*A hook called after an article is moved, copied, respooled, or crossposted."
855   :group 'gnus-summary
856   :type 'hook)
857
858 (defcustom gnus-summary-article-delete-hook nil
859   "*A hook called after an article is deleted."
860   :group 'gnus-summary
861   :type 'hook)
862
863 (defcustom gnus-summary-article-expire-hook nil
864   "*A hook called after an article is expired."
865   :group 'gnus-summary
866   :type 'hook)
867
868 (defcustom gnus-summary-display-arrow
869   (and (fboundp 'display-graphic-p)
870        (display-graphic-p))
871   "*If non-nil, display an arrow highlighting the current article."
872   :version "21.1"
873   :group 'gnus-summary
874   :type 'boolean)
875
876 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
877   "Face used for highlighting the current article in the summary buffer."
878   :group 'gnus-summary-visual
879   :type 'face)
880
881 (defvar gnus-tmp-downloaded nil)
882
883 (defcustom gnus-summary-highlight
884   '(((eq mark gnus-canceled-mark)
885      . gnus-summary-cancelled-face)
886     ((and uncached (> score default-high))
887      . gnus-summary-high-undownloaded-face)
888     ((and uncached (< score default-low))
889      . gnus-summary-low-undownloaded-face)
890     (uncached
891      . gnus-summary-normal-undownloaded-face)
892     ((and (> score default-high)
893           (or (eq mark gnus-dormant-mark)
894               (eq mark gnus-ticked-mark)))
895      . gnus-summary-high-ticked-face)
896     ((and (< score default-low)
897           (or (eq mark gnus-dormant-mark)
898               (eq mark gnus-ticked-mark)))
899      . gnus-summary-low-ticked-face)
900     ((or (eq mark gnus-dormant-mark)
901          (eq mark gnus-ticked-mark))
902      . gnus-summary-normal-ticked-face)
903     ((and (> score default-high) (eq mark gnus-ancient-mark))
904      . gnus-summary-high-ancient-face)
905     ((and (< score default-low) (eq mark gnus-ancient-mark))
906      . gnus-summary-low-ancient-face)
907     ((eq mark gnus-ancient-mark)
908      . gnus-summary-normal-ancient-face)
909     ((and (> score default-high) (eq mark gnus-unread-mark))
910      . gnus-summary-high-unread-face)
911     ((and (< score default-low) (eq mark gnus-unread-mark))
912      . gnus-summary-low-unread-face)
913     ((eq mark gnus-unread-mark)
914      . gnus-summary-normal-unread-face)
915     ((> score default-high)
916      . gnus-summary-high-read-face)
917     ((< score default-low)
918      . gnus-summary-low-read-face)
919     (t
920      . gnus-summary-normal-read-face))
921   "*Controls the highlighting of summary buffer lines.
922
923 A list of (FORM . FACE) pairs.  When deciding how a a particular
924 summary line should be displayed, each form is evaluated.  The content
925 of the face field after the first true form is used.  You can change
926 how those summary lines are displayed, by editing the face field.
927
928 You can use the following variables in the FORM field.
929
930 score:        The article's score
931 default:      The default article score.
932 default-high: The default score for high scored articles.
933 default-low:  The default score for low scored articles.
934 below:        The score below which articles are automatically marked as read.
935 mark:         The articles mark."
936   :group 'gnus-summary-visual
937   :type '(repeat (cons (sexp :tag "Form" nil)
938                        face)))
939
940 (defcustom gnus-alter-header-function nil
941   "Function called to allow alteration of article header structures.
942 The function is called with one parameter, the article header vector,
943 which it may alter in any way."
944   :type '(choice (const :tag "None" nil)
945                  function)
946   :group 'gnus-summary)
947
948 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
949   "Variable that says which function should be used to decode a string with encoded words.")
950
951 (defcustom gnus-extra-headers '(To Newsgroups)
952   "*Extra headers to parse."
953   :version "21.1"
954   :group 'gnus-summary
955   :type '(repeat symbol))
956
957 (defcustom gnus-ignored-from-addresses
958   (and user-mail-address (regexp-quote user-mail-address))
959   "*Regexp of From headers that may be suppressed in favor of To headers."
960   :version "21.1"
961   :group 'gnus-summary
962   :type 'regexp)
963
964 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
965   "List of charsets that should be ignored.
966 When these charsets are used in the \"charset\" parameter, the
967 default charset will be used instead."
968   :version "21.1"
969   :type '(repeat symbol)
970   :group 'gnus-charset)
971
972 (gnus-define-group-parameter
973  ignored-charsets
974  :type list
975  :function-document
976  "Return the ignored charsets of GROUP."
977  :variable gnus-group-ignored-charsets-alist
978  :variable-default
979  '(("alt\\.chinese\\.text" iso-8859-1))
980  :variable-document
981  "Alist of regexps (to match group names) and charsets that should be ignored.
982 When these charsets are used in the \"charset\" parameter, the
983 default charset will be used instead."
984  :variable-group gnus-charset
985  :variable-type '(repeat (cons (regexp :tag "Group")
986                                (repeat symbol)))
987  :parameter-type '(choice :tag "Ignored charsets"
988                           :value nil
989                           (repeat (symbol)))
990  :parameter-document       "\
991 List of charsets that should be ignored.
992
993 When these charsets are used in the \"charset\" parameter, the
994 default charset will be used instead.")
995
996 (defcustom gnus-group-highlight-words-alist nil
997   "Alist of group regexps and highlight regexps.
998 This variable uses the same syntax as `gnus-emphasis-alist'."
999   :version "21.1"
1000   :type '(repeat (cons (regexp :tag "Group")
1001                        (repeat (list (regexp :tag "Highlight regexp")
1002                                      (number :tag "Group for entire word" 0)
1003                                      (number :tag "Group for displayed part" 0)
1004                                      (symbol :tag "Face"
1005                                              gnus-emphasis-highlight-words)))))
1006   :group 'gnus-summary-visual)
1007
1008 (defcustom gnus-summary-show-article-charset-alist
1009   nil
1010   "Alist of number and charset.
1011 The article will be shown with the charset corresponding to the
1012 numbered argument.
1013 For example: ((1 . cn-gb-2312) (2 . big5))."
1014   :version "21.1"
1015   :type '(repeat (cons (number :tag "Argument" 1)
1016                        (symbol :tag "Charset")))
1017   :group 'gnus-charset)
1018
1019 (defcustom gnus-preserve-marks t
1020   "Whether marks are preserved when moving, copying and respooling messages."
1021   :version "21.1"
1022   :type 'boolean
1023   :group 'gnus-summary-marks)
1024
1025 (defcustom gnus-alter-articles-to-read-function nil
1026   "Function to be called to alter the list of articles to be selected."
1027   :type '(choice (const nil) function)
1028   :group 'gnus-summary)
1029
1030 (defcustom gnus-orphan-score nil
1031   "*All orphans get this score added.  Set in the score file."
1032   :group 'gnus-score-default
1033   :type '(choice (const nil)
1034                  integer))
1035
1036 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1037   "*A regexp to match MIME parts when saving multiple parts of a
1038 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1039 This regexp will be used by default when prompting the user for which
1040 type of files to save."
1041   :group 'gnus-summary
1042   :type 'regexp)
1043
1044 (defcustom gnus-read-all-available-headers nil
1045   "Whether Gnus should parse all headers made available to it.
1046 This is mostly relevant for slow backends where the user may
1047 wish to widen the summary buffer to include all headers
1048 that were fetched.  Say, for nnultimate groups."
1049   :group 'gnus-summary
1050   :type '(choice boolean regexp))
1051
1052 (defcustom gnus-summary-muttprint-program "muttprint"
1053   "Command (and optional arguments) used to run Muttprint."
1054   :version "21.3"
1055   :group 'gnus-summary
1056   :type 'string)
1057
1058 (defcustom gnus-article-loose-mime nil
1059   "If non-nil, don't require MIME-Version header.
1060 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1061 supply the MIME-Version header or deliberately strip it From the mail.
1062 Set it to non-nil, Gnus will treat some articles as MIME even if
1063 the MIME-Version header is missed."
1064   :version "21.3"
1065   :type 'boolean
1066   :group 'gnus-article)
1067
1068 (defcustom gnus-article-emulate-mime t
1069   "If non-nil, use MIME emulation for uuencode and the like.
1070 This means that Gnus will search message bodies for text that look
1071 like uuencoded bits, yEncoded bits, and so on, and present that using
1072 the normal Gnus MIME machinery."
1073   :type 'boolean
1074   :group 'gnus-article)
1075
1076 ;;; Internal variables
1077
1078 (defvar gnus-summary-display-cache nil)
1079 (defvar gnus-article-mime-handles nil)
1080 (defvar gnus-article-decoded-p nil)
1081 (defvar gnus-article-charset nil)
1082 (defvar gnus-article-ignored-charsets nil)
1083 (defvar gnus-scores-exclude-files nil)
1084 (defvar gnus-page-broken nil)
1085
1086 (defvar gnus-original-article nil)
1087 (defvar gnus-article-internal-prepare-hook nil)
1088 (defvar gnus-newsgroup-process-stack nil)
1089
1090 (defvar gnus-thread-indent-array nil)
1091 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1092 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1093   "Function called to sort the articles within a thread after it has been gathered together.")
1094
1095 (defvar gnus-summary-save-parts-type-history nil)
1096 (defvar gnus-summary-save-parts-last-directory nil)
1097
1098 ;; Avoid highlighting in kill files.
1099 (defvar gnus-summary-inhibit-highlight nil)
1100 (defvar gnus-newsgroup-selected-overlay nil)
1101 (defvar gnus-inhibit-limiting nil)
1102 (defvar gnus-newsgroup-adaptive-score-file nil)
1103 (defvar gnus-current-score-file nil)
1104 (defvar gnus-current-move-group nil)
1105 (defvar gnus-current-copy-group nil)
1106 (defvar gnus-current-crosspost-group nil)
1107 (defvar gnus-newsgroup-display nil)
1108
1109 (defvar gnus-newsgroup-dependencies nil)
1110 (defvar gnus-newsgroup-adaptive nil)
1111 (defvar gnus-summary-display-article-function nil)
1112 (defvar gnus-summary-highlight-line-function nil
1113   "Function called after highlighting a summary line.")
1114
1115 (defvar gnus-summary-line-format-alist
1116   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1117     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1118     (?s gnus-tmp-subject-or-nil ?s)
1119     (?n gnus-tmp-name ?s)
1120     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1121         ?s)
1122     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1123             gnus-tmp-from) ?s)
1124     (?F gnus-tmp-from ?s)
1125     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1126     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1127     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1128     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1129     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1130     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1131     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1132     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1133     (?L gnus-tmp-lines ?s)
1134     (?O gnus-tmp-downloaded ?c)
1135     (?I gnus-tmp-indentation ?s)
1136     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1137     (?R gnus-tmp-replied ?c)
1138     (?\[ gnus-tmp-opening-bracket ?c)
1139     (?\] gnus-tmp-closing-bracket ?c)
1140     (?\> (make-string gnus-tmp-level ? ) ?s)
1141     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1142     (?i gnus-tmp-score ?d)
1143     (?z gnus-tmp-score-char ?c)
1144     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1145     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1146     (?U gnus-tmp-unread ?c)
1147     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1148         ?s)
1149     (?t (gnus-summary-number-of-articles-in-thread
1150          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1151         ?d)
1152     (?e (gnus-summary-number-of-articles-in-thread
1153          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1154         ?c)
1155     (?u gnus-tmp-user-defined ?s)
1156     (?P (gnus-pick-line-number) ?d)
1157     (?B gnus-tmp-thread-tree-header-string ?s)
1158     (user-date (gnus-user-date
1159                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1160   "An alist of format specifications that can appear in summary lines.
1161 These are paired with what variables they correspond with, along with
1162 the type of the variable (string, integer, character, etc).")
1163
1164 (defvar gnus-summary-dummy-line-format-alist
1165   `((?S gnus-tmp-subject ?s)
1166     (?N gnus-tmp-number ?d)
1167     (?u gnus-tmp-user-defined ?s)))
1168
1169 (defvar gnus-summary-mode-line-format-alist
1170   `((?G gnus-tmp-group-name ?s)
1171     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1172     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1173     (?A gnus-tmp-article-number ?d)
1174     (?Z gnus-tmp-unread-and-unselected ?s)
1175     (?V gnus-version ?s)
1176     (?U gnus-tmp-unread-and-unticked ?d)
1177     (?S gnus-tmp-subject ?s)
1178     (?e gnus-tmp-unselected ?d)
1179     (?u gnus-tmp-user-defined ?s)
1180     (?d (length gnus-newsgroup-dormant) ?d)
1181     (?t (length gnus-newsgroup-marked) ?d)
1182     (?h (length gnus-newsgroup-spam-marked) ?d)
1183     (?r (length gnus-newsgroup-reads) ?d)
1184     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1185     (?E gnus-newsgroup-expunged-tally ?d)
1186     (?s (gnus-current-score-file-nondirectory) ?s)))
1187
1188 (defvar gnus-last-search-regexp nil
1189   "Default regexp for article search command.")
1190
1191 (defvar gnus-last-shell-command nil
1192   "Default shell command on article.")
1193
1194 (defvar gnus-newsgroup-agentized nil
1195   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1196 (defvar gnus-newsgroup-begin nil)
1197 (defvar gnus-newsgroup-end nil)
1198 (defvar gnus-newsgroup-last-rmail nil)
1199 (defvar gnus-newsgroup-last-mail nil)
1200 (defvar gnus-newsgroup-last-folder nil)
1201 (defvar gnus-newsgroup-last-file nil)
1202 (defvar gnus-newsgroup-auto-expire nil)
1203 (defvar gnus-newsgroup-active nil)
1204
1205 (defvar gnus-newsgroup-data nil)
1206 (defvar gnus-newsgroup-data-reverse nil)
1207 (defvar gnus-newsgroup-limit nil)
1208 (defvar gnus-newsgroup-limits nil)
1209
1210 (defvar gnus-newsgroup-unreads nil
1211   "Sorted list of unread articles in the current newsgroup.")
1212
1213 (defvar gnus-newsgroup-unselected nil
1214   "Sorted list of unselected unread articles in the current newsgroup.")
1215
1216 (defvar gnus-newsgroup-reads nil
1217   "Alist of read articles and article marks in the current newsgroup.")
1218
1219 (defvar gnus-newsgroup-expunged-tally nil)
1220
1221 (defvar gnus-newsgroup-marked nil
1222   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1223
1224 (defvar gnus-newsgroup-spam-marked nil
1225   "List of ranges of articles that have been marked as spam.")
1226
1227 (defvar gnus-newsgroup-killed nil
1228   "List of ranges of articles that have been through the scoring process.")
1229
1230 (defvar gnus-newsgroup-cached nil
1231   "Sorted list of articles that come from the article cache.")
1232
1233 (defvar gnus-newsgroup-saved nil
1234   "List of articles that have been saved.")
1235
1236 (defvar gnus-newsgroup-kill-headers nil)
1237
1238 (defvar gnus-newsgroup-replied nil
1239   "List of articles that have been replied to in the current newsgroup.")
1240
1241 (defvar gnus-newsgroup-forwarded nil
1242   "List of articles that have been forwarded in the current newsgroup.")
1243
1244 (defvar gnus-newsgroup-recent nil
1245   "List of articles that have are recent in the current newsgroup.")
1246
1247 (defvar gnus-newsgroup-expirable nil
1248   "Sorted list of articles in the current newsgroup that can be expired.")
1249
1250 (defvar gnus-newsgroup-processable nil
1251   "List of articles in the current newsgroup that can be processed.")
1252
1253 (defvar gnus-newsgroup-downloadable nil
1254   "Sorted list of articles in the current newsgroup that can be processed.")
1255
1256 (defvar gnus-newsgroup-unfetched nil
1257   "Sorted list of articles in the current newsgroup whose headers have not been fetched into the agent.")
1258
1259 (defvar gnus-newsgroup-undownloaded nil
1260   "List of articles in the current newsgroup that haven't been downloaded.")
1261
1262 (defvar gnus-newsgroup-unsendable nil
1263   "List of articles in the current newsgroup that won't be sent.")
1264
1265 (defvar gnus-newsgroup-bookmarks nil
1266   "List of articles in the current newsgroup that have bookmarks.")
1267
1268 (defvar gnus-newsgroup-dormant nil
1269   "Sorted list of dormant articles in the current newsgroup.")
1270
1271 (defvar gnus-newsgroup-unseen nil
1272   "List of unseen articles in the current newsgroup.")
1273
1274 (defvar gnus-newsgroup-seen nil
1275   "Range of seen articles in the current newsgroup.")
1276
1277 (defvar gnus-newsgroup-articles nil
1278   "List of articles in the current newsgroup.")
1279
1280 (defvar gnus-newsgroup-scored nil
1281   "List of scored articles in the current newsgroup.")
1282
1283 (defvar gnus-newsgroup-headers nil
1284   "List of article headers in the current newsgroup.")
1285
1286 (defvar gnus-newsgroup-threads nil)
1287
1288 (defvar gnus-newsgroup-prepared nil
1289   "Whether the current group has been prepared properly.")
1290
1291 (defvar gnus-newsgroup-ancient nil
1292   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1293
1294 (defvar gnus-newsgroup-sparse nil)
1295
1296 (defvar gnus-current-article nil)
1297 (defvar gnus-article-current nil)
1298 (defvar gnus-current-headers nil)
1299 (defvar gnus-have-all-headers nil)
1300 (defvar gnus-last-article nil)
1301 (defvar gnus-newsgroup-history nil)
1302 (defvar gnus-newsgroup-charset nil)
1303 (defvar gnus-newsgroup-ephemeral-charset nil)
1304 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1305
1306 (defvar gnus-article-before-search nil)
1307
1308 (defconst gnus-summary-local-variables
1309   '(gnus-newsgroup-name
1310     gnus-newsgroup-begin gnus-newsgroup-end
1311     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1312     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1313     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1314     gnus-newsgroup-unselected gnus-newsgroup-marked
1315     gnus-newsgroup-spam-marked
1316     gnus-newsgroup-reads gnus-newsgroup-saved
1317     gnus-newsgroup-replied gnus-newsgroup-forwarded
1318     gnus-newsgroup-recent
1319     gnus-newsgroup-expirable
1320     gnus-newsgroup-processable gnus-newsgroup-killed
1321     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1322     gnus-newsgroup-unfetched
1323     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1324     gnus-newsgroup-seen gnus-newsgroup-articles
1325     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1326     gnus-newsgroup-headers gnus-newsgroup-threads
1327     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1328     gnus-current-article gnus-current-headers gnus-have-all-headers
1329     gnus-last-article gnus-article-internal-prepare-hook
1330     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1331     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1332     gnus-thread-expunge-below
1333     gnus-score-alist gnus-current-score-file
1334     (gnus-summary-expunge-below . global)
1335     (gnus-summary-mark-below . global)
1336     (gnus-orphan-score . global)
1337     gnus-newsgroup-active gnus-scores-exclude-files
1338     gnus-newsgroup-history gnus-newsgroup-ancient
1339     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1340     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1341     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1342     (gnus-newsgroup-expunged-tally . 0)
1343     gnus-cache-removable-articles gnus-newsgroup-cached
1344     gnus-newsgroup-data gnus-newsgroup-data-reverse
1345     gnus-newsgroup-limit gnus-newsgroup-limits
1346     gnus-newsgroup-charset gnus-newsgroup-display)
1347   "Variables that are buffer-local to the summary buffers.")
1348
1349 (defvar gnus-newsgroup-variables nil
1350   "A list of variables that have separate values in different newsgroups.
1351 A list of newsgroup (summary buffer) local variables, or cons of
1352 variables and their default values (when the default values are not
1353 nil), that should be made global while the summary buffer is active.
1354 These variables can be used to set variables in the group parameters
1355 while still allowing them to affect operations done in other
1356 buffers. For example:
1357
1358 \(setq gnus-newsgroup-variables
1359      '(message-use-followup-to
1360        (gnus-visible-headers .
1361          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1362 ")
1363
1364 ;; Byte-compiler warning.
1365 ;(eval-when-compile (defvar gnus-article-mode-map))
1366 (eval-when-compile
1367   (let ((features (cons 'gnus-sum features)))
1368     (require 'gnus)
1369     (require 'gnus-agent)
1370     (require 'gnus-art)))
1371
1372 ;; MIME stuff.
1373
1374 (defvar gnus-decode-encoded-word-methods
1375   '(mail-decode-encoded-word-string)
1376   "List of methods used to decode encoded words.
1377
1378 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1379 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1380 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1381 whose names match REGEXP.
1382
1383 For example:
1384 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1385  mail-decode-encoded-word-string
1386  (\"chinese\" . rfc1843-decode-string))")
1387
1388 (defvar gnus-decode-encoded-word-methods-cache nil)
1389
1390 (defun gnus-multi-decode-encoded-word-string (string)
1391   "Apply the functions from `gnus-encoded-word-methods' that match."
1392   (unless (and gnus-decode-encoded-word-methods-cache
1393                (eq gnus-newsgroup-name
1394                    (car gnus-decode-encoded-word-methods-cache)))
1395     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1396     (mapcar (lambda (x)
1397               (if (symbolp x)
1398                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1399                 (if (and gnus-newsgroup-name
1400                          (string-match (car x) gnus-newsgroup-name))
1401                     (nconc gnus-decode-encoded-word-methods-cache
1402                            (list (cdr x))))))
1403             gnus-decode-encoded-word-methods))
1404   (let ((xlist gnus-decode-encoded-word-methods-cache))
1405     (pop xlist)
1406     (while xlist
1407       (setq string (funcall (pop xlist) string))))
1408   string)
1409
1410 ;; Subject simplification.
1411
1412 (defun gnus-simplify-whitespace (str)
1413   "Remove excessive whitespace from STR."
1414   ;; Multiple spaces.
1415   (while (string-match "[ \t][ \t]+" str)
1416     (setq str (concat (substring str 0 (match-beginning 0))
1417                         " "
1418                         (substring str (match-end 0)))))
1419   ;; Leading spaces.
1420   (when (string-match "^[ \t]+" str)
1421     (setq str (substring str (match-end 0))))
1422   ;; Trailing spaces.
1423   (when (string-match "[ \t]+$" str)
1424     (setq str (substring str 0 (match-beginning 0))))
1425   str)
1426
1427 (defun gnus-simplify-all-whitespace (str)
1428   "Remove all whitespace from STR."
1429   (while (string-match "[ \t\n]+" str)
1430     (setq str (replace-match "" nil nil str)))
1431   str)
1432
1433 (defsubst gnus-simplify-subject-re (subject)
1434   "Remove \"Re:\" from subject lines."
1435   (if (string-match message-subject-re-regexp subject)
1436       (substring subject (match-end 0))
1437     subject))
1438
1439 (defun gnus-simplify-subject (subject &optional re-only)
1440   "Remove `Re:' and words in parentheses.
1441 If RE-ONLY is non-nil, strip leading `Re:'s only."
1442   (let ((case-fold-search t))           ;Ignore case.
1443     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1444     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1445       (setq subject (substring subject (match-end 0))))
1446     ;; Remove uninteresting prefixes.
1447     (when (and (not re-only)
1448                gnus-simplify-ignored-prefixes
1449                (string-match gnus-simplify-ignored-prefixes subject))
1450       (setq subject (substring subject (match-end 0))))
1451     ;; Remove words in parentheses from end.
1452     (unless re-only
1453       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1454         (setq subject (substring subject 0 (match-beginning 0)))))
1455     ;; Return subject string.
1456     subject))
1457
1458 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1459 ;; all whitespace.
1460 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1461   (goto-char (point-min))
1462   (while (re-search-forward regexp nil t)
1463     (replace-match (or newtext ""))))
1464
1465 (defun gnus-simplify-buffer-fuzzy ()
1466   "Simplify string in the buffer fuzzily.
1467 The string in the accessible portion of the current buffer is simplified.
1468 It is assumed to be a single-line subject.
1469 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1470 matter is removed.  Additional things can be deleted by setting
1471 `gnus-simplify-subject-fuzzy-regexp'."
1472   (let ((case-fold-search t)
1473         (modified-tick))
1474     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1475
1476     (while (not (eq modified-tick (buffer-modified-tick)))
1477       (setq modified-tick (buffer-modified-tick))
1478       (cond
1479        ((listp gnus-simplify-subject-fuzzy-regexp)
1480         (mapcar 'gnus-simplify-buffer-fuzzy-step
1481                 gnus-simplify-subject-fuzzy-regexp))
1482        (gnus-simplify-subject-fuzzy-regexp
1483         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1484       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1485       (gnus-simplify-buffer-fuzzy-step
1486        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1487       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1488
1489     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1490     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1491     (gnus-simplify-buffer-fuzzy-step " $")
1492     (gnus-simplify-buffer-fuzzy-step "^ +")))
1493
1494 (defun gnus-simplify-subject-fuzzy (subject)
1495   "Simplify a subject string fuzzily.
1496 See `gnus-simplify-buffer-fuzzy' for details."
1497   (save-excursion
1498     (gnus-set-work-buffer)
1499     (let ((case-fold-search t))
1500       ;; Remove uninteresting prefixes.
1501       (when (and gnus-simplify-ignored-prefixes
1502                  (string-match gnus-simplify-ignored-prefixes subject))
1503         (setq subject (substring subject (match-end 0))))
1504       (insert subject)
1505       (inline (gnus-simplify-buffer-fuzzy))
1506       (buffer-string))))
1507
1508 (defsubst gnus-simplify-subject-fully (subject)
1509   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1510   (cond
1511    (gnus-simplify-subject-functions
1512     (gnus-map-function gnus-simplify-subject-functions subject))
1513    ((null gnus-summary-gather-subject-limit)
1514     (gnus-simplify-subject-re subject))
1515    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1516     (gnus-simplify-subject-fuzzy subject))
1517    ((numberp gnus-summary-gather-subject-limit)
1518     (gnus-limit-string (gnus-simplify-subject-re subject)
1519                        gnus-summary-gather-subject-limit))
1520    (t
1521     subject)))
1522
1523 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1524   "Check whether two subjects are equal.
1525 If optional argument SIMPLE-FIRST is t, first argument is already
1526 simplified."
1527   (cond
1528    ((null simple-first)
1529     (equal (gnus-simplify-subject-fully s1)
1530            (gnus-simplify-subject-fully s2)))
1531    (t
1532     (equal s1
1533            (gnus-simplify-subject-fully s2)))))
1534
1535 (defun gnus-summary-bubble-group ()
1536   "Increase the score of the current group.
1537 This is a handy function to add to `gnus-summary-exit-hook' to
1538 increase the score of each group you read."
1539   (gnus-group-add-score gnus-newsgroup-name))
1540
1541 \f
1542 ;;;
1543 ;;; Gnus summary mode
1544 ;;;
1545
1546 (put 'gnus-summary-mode 'mode-class 'special)
1547
1548 (defvar gnus-article-commands-menu)
1549
1550 ;; Non-orthogonal keys
1551
1552 (gnus-define-keys gnus-summary-mode-map
1553   " " gnus-summary-next-page
1554   "\177" gnus-summary-prev-page
1555   [delete] gnus-summary-prev-page
1556   [backspace] gnus-summary-prev-page
1557   "\r" gnus-summary-scroll-up
1558   "\M-\r" gnus-summary-scroll-down
1559   "n" gnus-summary-next-unread-article
1560   "p" gnus-summary-prev-unread-article
1561   "N" gnus-summary-next-article
1562   "P" gnus-summary-prev-article
1563   "\M-\C-n" gnus-summary-next-same-subject
1564   "\M-\C-p" gnus-summary-prev-same-subject
1565   "\M-n" gnus-summary-next-unread-subject
1566   "\M-p" gnus-summary-prev-unread-subject
1567   "." gnus-summary-first-unread-article
1568   "," gnus-summary-best-unread-article
1569   "\M-s" gnus-summary-search-article-forward
1570   "\M-r" gnus-summary-search-article-backward
1571   "<" gnus-summary-beginning-of-article
1572   ">" gnus-summary-end-of-article
1573   "j" gnus-summary-goto-article
1574   "^" gnus-summary-refer-parent-article
1575   "\M-^" gnus-summary-refer-article
1576   "u" gnus-summary-tick-article-forward
1577   "!" gnus-summary-tick-article-forward
1578   "U" gnus-summary-tick-article-backward
1579   "d" gnus-summary-mark-as-read-forward
1580   "D" gnus-summary-mark-as-read-backward
1581   "E" gnus-summary-mark-as-expirable
1582   "\M-u" gnus-summary-clear-mark-forward
1583   "\M-U" gnus-summary-clear-mark-backward
1584   "k" gnus-summary-kill-same-subject-and-select
1585   "\C-k" gnus-summary-kill-same-subject
1586   "\M-\C-k" gnus-summary-kill-thread
1587   "\M-\C-l" gnus-summary-lower-thread
1588   "e" gnus-summary-edit-article
1589   "#" gnus-summary-mark-as-processable
1590   "\M-#" gnus-summary-unmark-as-processable
1591   "\M-\C-t" gnus-summary-toggle-threads
1592   "\M-\C-s" gnus-summary-show-thread
1593   "\M-\C-h" gnus-summary-hide-thread
1594   "\M-\C-f" gnus-summary-next-thread
1595   "\M-\C-b" gnus-summary-prev-thread
1596   [(meta down)] gnus-summary-next-thread
1597   [(meta up)] gnus-summary-prev-thread
1598   "\M-\C-u" gnus-summary-up-thread
1599   "\M-\C-d" gnus-summary-down-thread
1600   "&" gnus-summary-execute-command
1601   "c" gnus-summary-catchup-and-exit
1602   "\C-w" gnus-summary-mark-region-as-read
1603   "\C-t" gnus-summary-toggle-truncation
1604   "?" gnus-summary-mark-as-dormant
1605   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1606   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1607   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1608   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1609   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1610   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1611   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1612   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1613   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1614   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1615   "=" gnus-summary-expand-window
1616   "\C-x\C-s" gnus-summary-reselect-current-group
1617   "\M-g" gnus-summary-rescan-group
1618   "w" gnus-summary-stop-page-breaking
1619   "\C-c\C-r" gnus-summary-caesar-message
1620   "f" gnus-summary-followup
1621   "F" gnus-summary-followup-with-original
1622   "C" gnus-summary-cancel-article
1623   "r" gnus-summary-reply
1624   "R" gnus-summary-reply-with-original
1625   "\C-c\C-f" gnus-summary-mail-forward
1626   "o" gnus-summary-save-article
1627   "\C-o" gnus-summary-save-article-mail
1628   "|" gnus-summary-pipe-output
1629   "\M-k" gnus-summary-edit-local-kill
1630   "\M-K" gnus-summary-edit-global-kill
1631   ;; "V" gnus-version
1632   "\C-c\C-d" gnus-summary-describe-group
1633   "q" gnus-summary-exit
1634   "Q" gnus-summary-exit-no-update
1635   "\C-c\C-i" gnus-info-find-node
1636   gnus-mouse-2 gnus-mouse-pick-article
1637   "m" gnus-summary-mail-other-window
1638   "a" gnus-summary-post-news
1639   "i" gnus-summary-news-other-window
1640   "x" gnus-summary-limit-to-unread
1641   "s" gnus-summary-isearch-article
1642   "t" gnus-summary-toggle-header
1643   "g" gnus-summary-show-article
1644   "l" gnus-summary-goto-last-article
1645   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1646   "\C-d" gnus-summary-enter-digest-group
1647   "\M-\C-d" gnus-summary-read-document
1648   "\M-\C-e" gnus-summary-edit-parameters
1649   "\M-\C-a" gnus-summary-customize-parameters
1650   "\C-c\C-b" gnus-bug
1651   "*" gnus-cache-enter-article
1652   "\M-*" gnus-cache-remove-article
1653   "\M-&" gnus-summary-universal-argument
1654   "\C-l" gnus-recenter
1655   "I" gnus-summary-increase-score
1656   "L" gnus-summary-lower-score
1657   "\M-i" gnus-symbolic-argument
1658   "h" gnus-summary-select-article-buffer
1659
1660   "b" gnus-article-view-part
1661   "\M-t" gnus-summary-toggle-display-buttonized
1662
1663   "V" gnus-summary-score-map
1664   "X" gnus-uu-extract-map
1665   "S" gnus-summary-send-map)
1666
1667 ;; Sort of orthogonal keymap
1668 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1669   "t" gnus-summary-tick-article-forward
1670   "!" gnus-summary-tick-article-forward
1671   "d" gnus-summary-mark-as-read-forward
1672   "r" gnus-summary-mark-as-read-forward
1673   "c" gnus-summary-clear-mark-forward
1674   " " gnus-summary-clear-mark-forward
1675   "e" gnus-summary-mark-as-expirable
1676   "x" gnus-summary-mark-as-expirable
1677   "?" gnus-summary-mark-as-dormant
1678   "b" gnus-summary-set-bookmark
1679   "B" gnus-summary-remove-bookmark
1680   "#" gnus-summary-mark-as-processable
1681   "\M-#" gnus-summary-unmark-as-processable
1682   "S" gnus-summary-limit-include-expunged
1683   "C" gnus-summary-catchup
1684   "H" gnus-summary-catchup-to-here
1685   "h" gnus-summary-catchup-from-here
1686   "\C-c" gnus-summary-catchup-all
1687   "k" gnus-summary-kill-same-subject-and-select
1688   "K" gnus-summary-kill-same-subject
1689   "P" gnus-uu-mark-map)
1690
1691 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1692   "c" gnus-summary-clear-above
1693   "u" gnus-summary-tick-above
1694   "m" gnus-summary-mark-above
1695   "k" gnus-summary-kill-below)
1696
1697 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1698   "/" gnus-summary-limit-to-subject
1699   "n" gnus-summary-limit-to-articles
1700   "w" gnus-summary-pop-limit
1701   "s" gnus-summary-limit-to-subject
1702   "a" gnus-summary-limit-to-author
1703   "u" gnus-summary-limit-to-unread
1704   "m" gnus-summary-limit-to-marks
1705   "M" gnus-summary-limit-exclude-marks
1706   "v" gnus-summary-limit-to-score
1707   "*" gnus-summary-limit-include-cached
1708   "D" gnus-summary-limit-include-dormant
1709   "T" gnus-summary-limit-include-thread
1710   "d" gnus-summary-limit-exclude-dormant
1711   "t" gnus-summary-limit-to-age
1712   "." gnus-summary-limit-to-unseen
1713   "x" gnus-summary-limit-to-extra
1714   "p" gnus-summary-limit-to-display-predicate
1715   "E" gnus-summary-limit-include-expunged
1716   "c" gnus-summary-limit-exclude-childless-dormant
1717   "C" gnus-summary-limit-mark-excluded-as-read
1718   "o" gnus-summary-insert-old-articles
1719   "N" gnus-summary-insert-new-articles)
1720
1721 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1722   "n" gnus-summary-next-unread-article
1723   "p" gnus-summary-prev-unread-article
1724   "N" gnus-summary-next-article
1725   "P" gnus-summary-prev-article
1726   "\C-n" gnus-summary-next-same-subject
1727   "\C-p" gnus-summary-prev-same-subject
1728   "\M-n" gnus-summary-next-unread-subject
1729   "\M-p" gnus-summary-prev-unread-subject
1730   "f" gnus-summary-first-unread-article
1731   "b" gnus-summary-best-unread-article
1732   "j" gnus-summary-goto-article
1733   "g" gnus-summary-goto-subject
1734   "l" gnus-summary-goto-last-article
1735   "o" gnus-summary-pop-article)
1736
1737 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1738   "k" gnus-summary-kill-thread
1739   "l" gnus-summary-lower-thread
1740   "i" gnus-summary-raise-thread
1741   "T" gnus-summary-toggle-threads
1742   "t" gnus-summary-rethread-current
1743   "^" gnus-summary-reparent-thread
1744   "s" gnus-summary-show-thread
1745   "S" gnus-summary-show-all-threads
1746   "h" gnus-summary-hide-thread
1747   "H" gnus-summary-hide-all-threads
1748   "n" gnus-summary-next-thread
1749   "p" gnus-summary-prev-thread
1750   "u" gnus-summary-up-thread
1751   "o" gnus-summary-top-thread
1752   "d" gnus-summary-down-thread
1753   "#" gnus-uu-mark-thread
1754   "\M-#" gnus-uu-unmark-thread)
1755
1756 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1757   "g" gnus-summary-prepare
1758   "c" gnus-summary-insert-cached-articles
1759   "d" gnus-summary-insert-dormant-articles)
1760
1761 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1762   "c" gnus-summary-catchup-and-exit
1763   "C" gnus-summary-catchup-all-and-exit
1764   "E" gnus-summary-exit-no-update
1765   "Q" gnus-summary-exit
1766   "Z" gnus-summary-exit
1767   "n" gnus-summary-catchup-and-goto-next-group
1768   "R" gnus-summary-reselect-current-group
1769   "G" gnus-summary-rescan-group
1770   "N" gnus-summary-next-group
1771   "s" gnus-summary-save-newsrc
1772   "P" gnus-summary-prev-group)
1773
1774 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1775   " " gnus-summary-next-page
1776   "n" gnus-summary-next-page
1777   "\177" gnus-summary-prev-page
1778   [delete] gnus-summary-prev-page
1779   "p" gnus-summary-prev-page
1780   "\r" gnus-summary-scroll-up
1781   "\M-\r" gnus-summary-scroll-down
1782   "<" gnus-summary-beginning-of-article
1783   ">" gnus-summary-end-of-article
1784   "b" gnus-summary-beginning-of-article
1785   "e" gnus-summary-end-of-article
1786   "^" gnus-summary-refer-parent-article
1787   "r" gnus-summary-refer-parent-article
1788   "D" gnus-summary-enter-digest-group
1789   "R" gnus-summary-refer-references
1790   "T" gnus-summary-refer-thread
1791   "g" gnus-summary-show-article
1792   "s" gnus-summary-isearch-article
1793   "P" gnus-summary-print-article
1794   "M" gnus-mailing-list-insinuate
1795   "t" gnus-article-babel)
1796
1797 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1798   "b" gnus-article-add-buttons
1799   "B" gnus-article-add-buttons-to-head
1800   "o" gnus-article-treat-overstrike
1801   "e" gnus-article-emphasize
1802   "w" gnus-article-fill-cited-article
1803   "Q" gnus-article-fill-long-lines
1804   "C" gnus-article-capitalize-sentences
1805   "c" gnus-article-remove-cr
1806   "q" gnus-article-de-quoted-unreadable
1807   "6" gnus-article-de-base64-unreadable
1808   "Z" gnus-article-decode-HZ
1809   "h" gnus-article-wash-html
1810   "u" gnus-article-unsplit-urls
1811   "s" gnus-summary-force-verify-and-decrypt
1812   "f" gnus-article-display-x-face
1813   "l" gnus-summary-stop-page-breaking
1814   "r" gnus-summary-caesar-message
1815   "m" gnus-summary-morse-message
1816   "t" gnus-summary-toggle-header
1817   "g" gnus-treat-smiley
1818   "v" gnus-summary-verbose-headers
1819   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1820   "p" gnus-article-verify-x-pgp-sig
1821   "d" gnus-article-treat-dumbquotes)
1822
1823 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1824   ;; mnemonic: deuglif*Y*
1825   "u" gnus-article-outlook-unwrap-lines
1826   "a" gnus-article-outlook-repair-attribution
1827   "c" gnus-article-outlook-rearrange-citation
1828   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1829
1830 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1831   "a" gnus-article-hide
1832   "h" gnus-article-hide-headers
1833   "b" gnus-article-hide-boring-headers
1834   "s" gnus-article-hide-signature
1835   "c" gnus-article-hide-citation
1836   "C" gnus-article-hide-citation-in-followups
1837   "l" gnus-article-hide-list-identifiers
1838   "B" gnus-article-strip-banner
1839   "P" gnus-article-hide-pem
1840   "\C-c" gnus-article-hide-citation-maybe)
1841
1842 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1843   "a" gnus-article-highlight
1844   "h" gnus-article-highlight-headers
1845   "c" gnus-article-highlight-citation
1846   "s" gnus-article-highlight-signature)
1847
1848 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1849   "f" gnus-article-treat-fold-headers
1850   "u" gnus-article-treat-unfold-headers
1851   "n" gnus-article-treat-fold-newsgroups)
1852
1853 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1854   "x" gnus-article-display-x-face
1855   "d" gnus-article-display-face
1856   "s" gnus-treat-smiley
1857   "D" gnus-article-remove-images
1858   "f" gnus-treat-from-picon
1859   "m" gnus-treat-mail-picon
1860   "n" gnus-treat-newsgroups-picon)
1861
1862 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1863   "w" gnus-article-decode-mime-words
1864   "c" gnus-article-decode-charset
1865   "v" gnus-mime-view-all-parts
1866   "b" gnus-article-view-part)
1867
1868 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1869   "z" gnus-article-date-ut
1870   "u" gnus-article-date-ut
1871   "l" gnus-article-date-local
1872   "p" gnus-article-date-english
1873   "e" gnus-article-date-lapsed
1874   "o" gnus-article-date-original
1875   "i" gnus-article-date-iso8601
1876   "s" gnus-article-date-user)
1877
1878 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1879   "t" gnus-article-remove-trailing-blank-lines
1880   "l" gnus-article-strip-leading-blank-lines
1881   "m" gnus-article-strip-multiple-blank-lines
1882   "a" gnus-article-strip-blank-lines
1883   "A" gnus-article-strip-all-blank-lines
1884   "s" gnus-article-strip-leading-space
1885   "e" gnus-article-strip-trailing-space
1886   "w" gnus-article-remove-leading-whitespace)
1887
1888 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1889   "v" gnus-version
1890   "f" gnus-summary-fetch-faq
1891   "d" gnus-summary-describe-group
1892   "h" gnus-summary-describe-briefly
1893   "i" gnus-info-find-node
1894   "c" gnus-group-fetch-charter
1895   "C" gnus-group-fetch-control)
1896
1897 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1898   "e" gnus-summary-expire-articles
1899   "\M-\C-e" gnus-summary-expire-articles-now
1900   "\177" gnus-summary-delete-article
1901   [delete] gnus-summary-delete-article
1902   [backspace] gnus-summary-delete-article
1903   "m" gnus-summary-move-article
1904   "r" gnus-summary-respool-article
1905   "w" gnus-summary-edit-article
1906   "c" gnus-summary-copy-article
1907   "B" gnus-summary-crosspost-article
1908   "q" gnus-summary-respool-query
1909   "t" gnus-summary-respool-trace
1910   "i" gnus-summary-import-article
1911   "I" gnus-summary-create-article
1912   "p" gnus-summary-article-posted-p)
1913
1914 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1915   "o" gnus-summary-save-article
1916   "m" gnus-summary-save-article-mail
1917   "F" gnus-summary-write-article-file
1918   "r" gnus-summary-save-article-rmail
1919   "f" gnus-summary-save-article-file
1920   "b" gnus-summary-save-article-body-file
1921   "h" gnus-summary-save-article-folder
1922   "v" gnus-summary-save-article-vm
1923   "p" gnus-summary-pipe-output
1924   "P" gnus-summary-muttprint
1925   "s" gnus-soup-add-article)
1926
1927 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1928   "b" gnus-summary-display-buttonized
1929   "m" gnus-summary-repair-multipart
1930   "v" gnus-article-view-part
1931   "o" gnus-article-save-part
1932   "c" gnus-article-copy-part
1933   "C" gnus-article-view-part-as-charset
1934   "e" gnus-article-view-part-externally
1935   "E" gnus-article-encrypt-body
1936   "i" gnus-article-inline-part
1937   "|" gnus-article-pipe-part)
1938
1939 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1940   "p" gnus-summary-mark-as-processable
1941   "u" gnus-summary-unmark-as-processable
1942   "U" gnus-summary-unmark-all-processable
1943   "v" gnus-uu-mark-over
1944   "s" gnus-uu-mark-series
1945   "r" gnus-uu-mark-region
1946   "g" gnus-uu-unmark-region
1947   "R" gnus-uu-mark-by-regexp
1948   "G" gnus-uu-unmark-by-regexp
1949   "t" gnus-uu-mark-thread
1950   "T" gnus-uu-unmark-thread
1951   "a" gnus-uu-mark-all
1952   "b" gnus-uu-mark-buffer
1953   "S" gnus-uu-mark-sparse
1954   "k" gnus-summary-kill-process-mark
1955   "y" gnus-summary-yank-process-mark
1956   "w" gnus-summary-save-process-mark
1957   "i" gnus-uu-invert-processable)
1958
1959 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1960   ;;"x" gnus-uu-extract-any
1961   "m" gnus-summary-save-parts
1962   "u" gnus-uu-decode-uu
1963   "U" gnus-uu-decode-uu-and-save
1964   "s" gnus-uu-decode-unshar
1965   "S" gnus-uu-decode-unshar-and-save
1966   "o" gnus-uu-decode-save
1967   "O" gnus-uu-decode-save
1968   "b" gnus-uu-decode-binhex
1969   "B" gnus-uu-decode-binhex
1970   "p" gnus-uu-decode-postscript
1971   "P" gnus-uu-decode-postscript-and-save)
1972
1973 (gnus-define-keys
1974     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1975   "u" gnus-uu-decode-uu-view
1976   "U" gnus-uu-decode-uu-and-save-view
1977   "s" gnus-uu-decode-unshar-view
1978   "S" gnus-uu-decode-unshar-and-save-view
1979   "o" gnus-uu-decode-save-view
1980   "O" gnus-uu-decode-save-view
1981   "b" gnus-uu-decode-binhex-view
1982   "B" gnus-uu-decode-binhex-view
1983   "p" gnus-uu-decode-postscript-view
1984   "P" gnus-uu-decode-postscript-and-save-view)
1985
1986 (defvar gnus-article-post-menu nil)
1987
1988 (defconst gnus-summary-menu-maxlen 20)
1989
1990 (defun gnus-summary-menu-split (menu)
1991   ;; If we have lots of elements, divide them into groups of 20
1992   ;; and make a pane (or submenu) for each one.
1993   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1994       (let ((menu menu) sublists next
1995             (i 1))
1996         (while menu
1997           ;; Pull off the next gnus-summary-menu-maxlen elements
1998           ;; and make them the next element of sublist.
1999           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2000           (if next
2001               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2002                       nil))
2003           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2004                                              (aref (car (last menu)) 0)) menu)
2005                                sublists))
2006           (setq i (1+ i))
2007           (setq menu next))
2008         (nreverse sublists))
2009     ;; Few elements--put them all in one pane.
2010     menu))
2011
2012 (defun gnus-summary-make-menu-bar ()
2013   (gnus-turn-off-edit-menu 'summary)
2014
2015   (unless (boundp 'gnus-summary-misc-menu)
2016
2017     (easy-menu-define
2018       gnus-summary-kill-menu gnus-summary-mode-map ""
2019       (cons
2020        "Score"
2021        (nconc
2022         (list
2023          ["Customize" gnus-score-customize t])
2024         (gnus-make-score-map 'increase)
2025         (gnus-make-score-map 'lower)
2026         '(("Mark"
2027            ["Kill below" gnus-summary-kill-below t]
2028            ["Mark above" gnus-summary-mark-above t]
2029            ["Tick above" gnus-summary-tick-above t]
2030            ["Clear above" gnus-summary-clear-above t])
2031           ["Current score" gnus-summary-current-score t]
2032           ["Set score" gnus-summary-set-score t]
2033           ["Switch current score file..." gnus-score-change-score-file t]
2034           ["Set mark below..." gnus-score-set-mark-below t]
2035           ["Set expunge below..." gnus-score-set-expunge-below t]
2036           ["Edit current score file" gnus-score-edit-current-scores t]
2037           ["Edit score file" gnus-score-edit-file t]
2038           ["Trace score" gnus-score-find-trace t]
2039           ["Find words" gnus-score-find-favourite-words t]
2040           ["Rescore buffer" gnus-summary-rescore t]
2041           ["Increase score..." gnus-summary-increase-score t]
2042           ["Lower score..." gnus-summary-lower-score t]))))
2043
2044     ;; Define both the Article menu in the summary buffer and the
2045     ;; equivalent Commands menu in the article buffer here for
2046     ;; consistency.
2047     (let ((innards
2048            `(("Hide"
2049               ["All" gnus-article-hide t]
2050               ["Headers" gnus-article-hide-headers t]
2051               ["Signature" gnus-article-hide-signature t]
2052               ["Citation" gnus-article-hide-citation t]
2053               ["List identifiers" gnus-article-hide-list-identifiers t]
2054               ["Banner" gnus-article-strip-banner t]
2055               ["Boring headers" gnus-article-hide-boring-headers t])
2056              ("Highlight"
2057               ["All" gnus-article-highlight t]
2058               ["Headers" gnus-article-highlight-headers t]
2059               ["Signature" gnus-article-highlight-signature t]
2060               ["Citation" gnus-article-highlight-citation t])
2061              ("MIME"
2062               ["Words" gnus-article-decode-mime-words t]
2063               ["Charset" gnus-article-decode-charset t]
2064               ["QP" gnus-article-de-quoted-unreadable t]
2065               ["Base64" gnus-article-de-base64-unreadable t]
2066               ["View MIME buttons" gnus-summary-display-buttonized t]
2067               ["View all" gnus-mime-view-all-parts t]
2068               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2069               ["Encrypt body" gnus-article-encrypt-body t]
2070               ["Extract all parts" gnus-summary-save-parts t]
2071               ("Multipart"
2072                ["Repair multipart" gnus-summary-repair-multipart t]
2073                ["Add buttons" gnus-summary-display-buttonized t]
2074                ["Pipe part" gnus-article-pipe-part t]
2075                ["Inline part" gnus-article-inline-part t]
2076                ["Encrypt body" gnus-article-encrypt-body t]
2077                ["View part externally" gnus-article-view-part-externally t]
2078                ["View part with charset" gnus-article-view-part-as-charset t]
2079                ["Copy part" gnus-article-copy-part t]
2080                ["Save part" gnus-article-save-part t]
2081                ["View part" gnus-article-view-part t]))
2082              ("Date"
2083               ["Local" gnus-article-date-local t]
2084               ["ISO8601" gnus-article-date-iso8601 t]
2085               ["UT" gnus-article-date-ut t]
2086               ["Original" gnus-article-date-original t]
2087               ["Lapsed" gnus-article-date-lapsed t]
2088               ["User-defined" gnus-article-date-user t])
2089              ("Display"
2090               ["Remove images" gnus-article-remove-images t]
2091               ["Toggle smiley" gnus-treat-smiley t]
2092               ["Show X-Face" gnus-article-display-x-face t]
2093               ["Show picons in From" gnus-treat-from-picon t]
2094               ["Show picons in mail headers" gnus-treat-mail-picon t]
2095               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2096               ("View as different encoding"
2097                ,@(gnus-summary-menu-split
2098                   (mapcar
2099                    (lambda (cs)
2100                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2101                      ;; forms for menu commands, we should provide intern'ed
2102                      ;; function symbols.
2103                      (let ((command (intern (format "\
2104 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2105                        (fset command
2106                              `(lambda ()
2107                                 (interactive)
2108                                 (let ((gnus-summary-show-article-charset-alist
2109                                        '((1 . ,cs))))
2110                                   (gnus-summary-show-article 1))))
2111                        `[,(symbol-name cs) ,command t]))
2112                    (sort (if (fboundp 'coding-system-list)
2113                              (coding-system-list)
2114                            (mapcar 'car mm-mime-mule-charset-alist))
2115                          'string<)))))
2116              ("Washing"
2117               ("Remove Blanks"
2118                ["Leading" gnus-article-strip-leading-blank-lines t]
2119                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2120                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2121                ["All of the above" gnus-article-strip-blank-lines t]
2122                ["All" gnus-article-strip-all-blank-lines t]
2123                ["Leading space" gnus-article-strip-leading-space t]
2124                ["Trailing space" gnus-article-strip-trailing-space t]
2125                ["Leading space in headers"
2126                 gnus-article-remove-leading-whitespace t])
2127               ["Overstrike" gnus-article-treat-overstrike t]
2128               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2129               ["Emphasis" gnus-article-emphasize t]
2130               ["Word wrap" gnus-article-fill-cited-article t]
2131               ["Fill long lines" gnus-article-fill-long-lines t]
2132               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2133               ["CR" gnus-article-remove-cr t]
2134               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2135               ["Base64" gnus-article-de-base64-unreadable t]
2136               ["Rot 13" gnus-summary-caesar-message
2137                ,@(if (featurep 'xemacs) '(t)
2138                    '(:help "\"Caesar rotate\" article by 13"))]
2139               ["Morse decode" gnus-summary-morse-message t]
2140               ["Unix pipe..." gnus-summary-pipe-message t]
2141               ["Add buttons" gnus-article-add-buttons t]
2142               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2143               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2144               ["Verbose header" gnus-summary-verbose-headers t]
2145               ["Toggle header" gnus-summary-toggle-header t]
2146               ["Unfold headers" gnus-article-treat-unfold-headers t]
2147               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2148               ["Html" gnus-article-wash-html t]
2149               ["URLs" gnus-article-unsplit-urls t]
2150               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2151               ["HZ" gnus-article-decode-HZ t]
2152               ("(Outlook) Deuglify"
2153                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2154                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2155                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2156                ["Full (Outlook) deuglify"
2157                 gnus-article-outlook-deuglify-article t])
2158               )
2159              ("Output"
2160               ["Save in default format" gnus-summary-save-article
2161                ,@(if (featurep 'xemacs) '(t)
2162                    '(:help "Save article using default method"))]
2163               ["Save in file" gnus-summary-save-article-file
2164                ,@(if (featurep 'xemacs) '(t)
2165                    '(:help "Save article in file"))]
2166               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2167               ["Save in MH folder" gnus-summary-save-article-folder t]
2168               ["Save in VM folder" gnus-summary-save-article-vm t]
2169               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2170               ["Save body in file" gnus-summary-save-article-body-file t]
2171               ["Pipe through a filter" gnus-summary-pipe-output t]
2172               ["Add to SOUP packet" gnus-soup-add-article t]
2173               ["Print with Muttprint" gnus-summary-muttprint t]
2174               ["Print" gnus-summary-print-article t])
2175              ("Backend"
2176               ["Respool article..." gnus-summary-respool-article t]
2177               ["Move article..." gnus-summary-move-article
2178                (gnus-check-backend-function
2179                 'request-move-article gnus-newsgroup-name)]
2180               ["Copy article..." gnus-summary-copy-article t]
2181               ["Crosspost article..." gnus-summary-crosspost-article
2182                (gnus-check-backend-function
2183                 'request-replace-article gnus-newsgroup-name)]
2184               ["Import file..." gnus-summary-import-article t]
2185               ["Create article..." gnus-summary-create-article t]
2186               ["Check if posted" gnus-summary-article-posted-p t]
2187               ["Edit article" gnus-summary-edit-article
2188                (not (gnus-group-read-only-p))]
2189               ["Delete article" gnus-summary-delete-article
2190                (gnus-check-backend-function
2191                 'request-expire-articles gnus-newsgroup-name)]
2192               ["Query respool" gnus-summary-respool-query t]
2193               ["Trace respool" gnus-summary-respool-trace t]
2194               ["Delete expirable articles" gnus-summary-expire-articles-now
2195                (gnus-check-backend-function
2196                 'request-expire-articles gnus-newsgroup-name)])
2197              ("Extract"
2198               ["Uudecode" gnus-uu-decode-uu
2199                ,@(if (featurep 'xemacs) '(t)
2200                    '(:help "Decode uuencoded article(s)"))]
2201               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2202               ["Unshar" gnus-uu-decode-unshar t]
2203               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2204               ["Save" gnus-uu-decode-save t]
2205               ["Binhex" gnus-uu-decode-binhex t]
2206               ["Postscript" gnus-uu-decode-postscript t]
2207               ["All MIME parts" gnus-summary-save-parts t])
2208              ("Cache"
2209               ["Enter article" gnus-cache-enter-article t]
2210               ["Remove article" gnus-cache-remove-article t])
2211              ["Translate" gnus-article-babel t]
2212              ["Select article buffer" gnus-summary-select-article-buffer t]
2213              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2214              ["Isearch article..." gnus-summary-isearch-article t]
2215              ["Beginning of the article" gnus-summary-beginning-of-article t]
2216              ["End of the article" gnus-summary-end-of-article t]
2217              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2218              ["Fetch referenced articles" gnus-summary-refer-references t]
2219              ["Fetch current thread" gnus-summary-refer-thread t]
2220              ["Fetch article with id..." gnus-summary-refer-article t]
2221              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2222              ["Redisplay" gnus-summary-show-article t]
2223              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2224       (easy-menu-define
2225         gnus-summary-article-menu gnus-summary-mode-map ""
2226         (cons "Article" innards))
2227
2228       (if (not (keymapp gnus-summary-article-menu))
2229           (easy-menu-define
2230             gnus-article-commands-menu gnus-article-mode-map ""
2231             (cons "Commands" innards))
2232         ;; in Emacs, don't share menu.
2233         (setq gnus-article-commands-menu
2234               (copy-keymap gnus-summary-article-menu))
2235         (define-key gnus-article-mode-map [menu-bar commands]
2236           (cons "Commands" gnus-article-commands-menu))))
2237
2238     (easy-menu-define
2239       gnus-summary-thread-menu gnus-summary-mode-map ""
2240       '("Threads"
2241         ["Find all messages in thread" gnus-summary-refer-thread t]
2242         ["Toggle threading" gnus-summary-toggle-threads t]
2243         ["Hide threads" gnus-summary-hide-all-threads t]
2244         ["Show threads" gnus-summary-show-all-threads t]
2245         ["Hide thread" gnus-summary-hide-thread t]
2246         ["Show thread" gnus-summary-show-thread t]
2247         ["Go to next thread" gnus-summary-next-thread t]
2248         ["Go to previous thread" gnus-summary-prev-thread t]
2249         ["Go down thread" gnus-summary-down-thread t]
2250         ["Go up thread" gnus-summary-up-thread t]
2251         ["Top of thread" gnus-summary-top-thread t]
2252         ["Mark thread as read" gnus-summary-kill-thread t]
2253         ["Lower thread score" gnus-summary-lower-thread t]
2254         ["Raise thread score" gnus-summary-raise-thread t]
2255         ["Rethread current" gnus-summary-rethread-current t]))
2256
2257     (easy-menu-define
2258       gnus-summary-post-menu gnus-summary-mode-map ""
2259       `("Post"
2260         ["Send a message (mail or news)" gnus-summary-post-news
2261          ,@(if (featurep 'xemacs) '(t)
2262              '(:help "Post an article"))]
2263         ["Followup" gnus-summary-followup
2264          ,@(if (featurep 'xemacs) '(t)
2265              '(:help "Post followup to this article"))]
2266         ["Followup and yank" gnus-summary-followup-with-original
2267          ,@(if (featurep 'xemacs) '(t)
2268              '(:help "Post followup to this article, quoting its contents"))]
2269         ["Supersede article" gnus-summary-supersede-article t]
2270         ["Cancel article" gnus-summary-cancel-article
2271          ,@(if (featurep 'xemacs) '(t)
2272              '(:help "Cancel an article you posted"))]
2273         ["Reply" gnus-summary-reply t]
2274         ["Reply and yank" gnus-summary-reply-with-original t]
2275         ["Wide reply" gnus-summary-wide-reply t]
2276         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2277          ,@(if (featurep 'xemacs) '(t)
2278              '(:help "Mail a reply, quoting this article"))]
2279         ["Very wide reply" gnus-summary-very-wide-reply t]
2280         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2281          ,@(if (featurep 'xemacs) '(t)
2282              '(:help "Mail a very wide reply, quoting this article"))]
2283         ["Mail forward" gnus-summary-mail-forward t]
2284         ["Post forward" gnus-summary-post-forward t]
2285         ["Digest and mail" gnus-uu-digest-mail-forward t]
2286         ["Digest and post" gnus-uu-digest-post-forward t]
2287         ["Resend message" gnus-summary-resend-message t]
2288         ["Resend message edit" gnus-summary-resend-message-edit t]
2289         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2290         ["Send a mail" gnus-summary-mail-other-window t]
2291         ["Create a local message" gnus-summary-news-other-window t]
2292         ["Uuencode and post" gnus-uu-post-news
2293          ,@(if (featurep 'xemacs) '(t)
2294              '(:help "Post a uuencoded article"))]
2295         ["Followup via news" gnus-summary-followup-to-mail t]
2296         ["Followup via news and yank"
2297          gnus-summary-followup-to-mail-with-original t]
2298         ;;("Draft"
2299         ;;["Send" gnus-summary-send-draft t]
2300         ;;["Send bounced" gnus-resend-bounced-mail t])
2301         ))
2302
2303     (cond
2304      ((not (keymapp gnus-summary-post-menu))
2305       (setq gnus-article-post-menu gnus-summary-post-menu))
2306      ((not gnus-article-post-menu)
2307       ;; Don't share post menu.
2308       (setq gnus-article-post-menu
2309             (copy-keymap gnus-summary-post-menu))))
2310     (define-key gnus-article-mode-map [menu-bar post]
2311       (cons "Post" gnus-article-post-menu))
2312
2313     (easy-menu-define
2314       gnus-summary-misc-menu gnus-summary-mode-map ""
2315       `("Gnus"
2316         ("Mark Read"
2317          ["Mark as read" gnus-summary-mark-as-read-forward t]
2318          ["Mark same subject and select"
2319           gnus-summary-kill-same-subject-and-select t]
2320          ["Mark same subject" gnus-summary-kill-same-subject t]
2321          ["Catchup" gnus-summary-catchup
2322           ,@(if (featurep 'xemacs) '(t)
2323               '(:help "Mark unread articles in this group as read"))]
2324          ["Catchup all" gnus-summary-catchup-all t]
2325          ["Catchup to here" gnus-summary-catchup-to-here t]
2326          ["Catchup from here" gnus-summary-catchup-from-here t]
2327          ["Catchup region" gnus-summary-mark-region-as-read 
2328           (gnus-mark-active-p)]
2329          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2330         ("Mark Various"
2331          ["Tick" gnus-summary-tick-article-forward t]
2332          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2333          ["Remove marks" gnus-summary-clear-mark-forward t]
2334          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2335          ["Set bookmark" gnus-summary-set-bookmark t]
2336          ["Remove bookmark" gnus-summary-remove-bookmark t])
2337         ("Limit to"
2338          ["Marks..." gnus-summary-limit-to-marks t]
2339          ["Subject..." gnus-summary-limit-to-subject t]
2340          ["Author..." gnus-summary-limit-to-author t]
2341          ["Age..." gnus-summary-limit-to-age t]
2342          ["Extra..." gnus-summary-limit-to-extra t]
2343          ["Score..." gnus-summary-limit-to-score t]
2344          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2345          ["Unread" gnus-summary-limit-to-unread t]
2346          ["Unseen" gnus-summary-limit-to-unseen t]
2347          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2348          ["Next articles" gnus-summary-limit-to-articles t]
2349          ["Pop limit" gnus-summary-pop-limit t]
2350          ["Show dormant" gnus-summary-limit-include-dormant t]
2351          ["Hide childless dormant"
2352           gnus-summary-limit-exclude-childless-dormant t]
2353          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2354          ["Hide marked" gnus-summary-limit-exclude-marks t]
2355          ["Show expunged" gnus-summary-limit-include-expunged t])
2356         ("Process Mark"
2357          ["Set mark" gnus-summary-mark-as-processable t]
2358          ["Remove mark" gnus-summary-unmark-as-processable t]
2359          ["Remove all marks" gnus-summary-unmark-all-processable t]
2360          ["Mark above" gnus-uu-mark-over t]
2361          ["Mark series" gnus-uu-mark-series t]
2362          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2363          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2364          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2365          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2366          ["Mark all" gnus-uu-mark-all t]
2367          ["Mark buffer" gnus-uu-mark-buffer t]
2368          ["Mark sparse" gnus-uu-mark-sparse t]
2369          ["Mark thread" gnus-uu-mark-thread t]
2370          ["Unmark thread" gnus-uu-unmark-thread t]
2371          ("Process Mark Sets"
2372           ["Kill" gnus-summary-kill-process-mark t]
2373           ["Yank" gnus-summary-yank-process-mark
2374            gnus-newsgroup-process-stack]
2375           ["Save" gnus-summary-save-process-mark t]
2376           ["Run command on marked..." gnus-summary-universal-argument t]))
2377         ("Scroll article"
2378          ["Page forward" gnus-summary-next-page
2379           ,@(if (featurep 'xemacs) '(t)
2380               '(:help "Show next page of article"))]
2381          ["Page backward" gnus-summary-prev-page
2382           ,@(if (featurep 'xemacs) '(t)
2383               '(:help "Show previous page of article"))]
2384          ["Line forward" gnus-summary-scroll-up t])
2385         ("Move"
2386          ["Next unread article" gnus-summary-next-unread-article t]
2387          ["Previous unread article" gnus-summary-prev-unread-article t]
2388          ["Next article" gnus-summary-next-article t]
2389          ["Previous article" gnus-summary-prev-article t]
2390          ["Next unread subject" gnus-summary-next-unread-subject t]
2391          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2392          ["Next article same subject" gnus-summary-next-same-subject t]
2393          ["Previous article same subject" gnus-summary-prev-same-subject t]
2394          ["First unread article" gnus-summary-first-unread-article t]
2395          ["Best unread article" gnus-summary-best-unread-article t]
2396          ["Go to subject number..." gnus-summary-goto-subject t]
2397          ["Go to article number..." gnus-summary-goto-article t]
2398          ["Go to the last article" gnus-summary-goto-last-article t]
2399          ["Pop article off history" gnus-summary-pop-article t])
2400         ("Sort"
2401          ["Sort by number" gnus-summary-sort-by-number t]
2402          ["Sort by author" gnus-summary-sort-by-author t]
2403          ["Sort by subject" gnus-summary-sort-by-subject t]
2404          ["Sort by date" gnus-summary-sort-by-date t]
2405          ["Sort by score" gnus-summary-sort-by-score t]
2406          ["Sort by lines" gnus-summary-sort-by-lines t]
2407          ["Sort by characters" gnus-summary-sort-by-chars t]
2408          ["Randomize" gnus-summary-sort-by-random t]
2409          ["Original sort" gnus-summary-sort-by-original t])
2410         ("Help"
2411          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2412          ["Describe group" gnus-summary-describe-group t]
2413          ["Fetch charter" gnus-group-fetch-charter
2414           ,@(if (featurep 'xemacs) nil
2415               '(:help "Display the charter of the current group"))]
2416          ["Fetch control message" gnus-group-fetch-control
2417           ,@(if (featurep 'xemacs) nil
2418               '(:help "Display the archived control message for the current group"))]
2419          ["Read manual" gnus-info-find-node t])
2420         ("Modes"
2421          ["Pick and read" gnus-pick-mode t]
2422          ["Binary" gnus-binary-mode t])
2423         ("Regeneration"
2424          ["Regenerate" gnus-summary-prepare t]
2425          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2426          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2427          ["Toggle threading" gnus-summary-toggle-threads t])
2428         ["See old articles" gnus-summary-insert-old-articles t]
2429         ["See new articles" gnus-summary-insert-new-articles t]
2430         ["Filter articles..." gnus-summary-execute-command t]
2431         ["Run command on articles..." gnus-summary-universal-argument t]
2432         ["Search articles forward..." gnus-summary-search-article-forward t]
2433         ["Search articles backward..." gnus-summary-search-article-backward t]
2434         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2435         ["Expand window" gnus-summary-expand-window t]
2436         ["Expire expirable articles" gnus-summary-expire-articles
2437          (gnus-check-backend-function
2438           'request-expire-articles gnus-newsgroup-name)]
2439         ["Edit local kill file" gnus-summary-edit-local-kill t]
2440         ["Edit main kill file" gnus-summary-edit-global-kill t]
2441         ["Edit group parameters" gnus-summary-edit-parameters t]
2442         ["Customize group parameters" gnus-summary-customize-parameters t]
2443         ["Send a bug report" gnus-bug t]
2444         ("Exit"
2445          ["Catchup and exit" gnus-summary-catchup-and-exit
2446           ,@(if (featurep 'xemacs) '(t)
2447               '(:help "Mark unread articles in this group as read, then exit"))]
2448          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2449          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2450          ["Exit group" gnus-summary-exit
2451           ,@(if (featurep 'xemacs) '(t)
2452               '(:help "Exit current group, return to group selection mode"))]
2453          ["Exit group without updating" gnus-summary-exit-no-update t]
2454          ["Exit and goto next group" gnus-summary-next-group t]
2455          ["Exit and goto prev group" gnus-summary-prev-group t]
2456          ["Reselect group" gnus-summary-reselect-current-group t]
2457          ["Rescan group" gnus-summary-rescan-group t]
2458          ["Update dribble" gnus-summary-save-newsrc t])))
2459
2460     (gnus-run-hooks 'gnus-summary-menu-hook)))
2461
2462 (defvar gnus-summary-tool-bar-map nil)
2463
2464 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2465 (defun gnus-summary-make-tool-bar ()
2466   (if (and (fboundp 'tool-bar-add-item-from-menu)
2467            (default-value 'tool-bar-mode)
2468            (not gnus-summary-tool-bar-map))
2469       (setq gnus-summary-tool-bar-map
2470             (let ((tool-bar-map (make-sparse-keymap))
2471                   (load-path (mm-image-load-path)))
2472               (tool-bar-add-item-from-menu
2473                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2474               (tool-bar-add-item-from-menu
2475                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2476               (tool-bar-add-item-from-menu
2477                'gnus-summary-post-news "post" gnus-summary-mode-map)
2478               (tool-bar-add-item-from-menu
2479                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2480               (tool-bar-add-item-from-menu
2481                'gnus-summary-followup "followup" gnus-summary-mode-map)
2482               (tool-bar-add-item-from-menu
2483                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2484               (tool-bar-add-item-from-menu
2485                'gnus-summary-reply "reply" gnus-summary-mode-map)
2486               (tool-bar-add-item-from-menu
2487                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2488               (tool-bar-add-item-from-menu
2489                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2490               (tool-bar-add-item-from-menu
2491                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2492               (tool-bar-add-item-from-menu
2493                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2494               (tool-bar-add-item-from-menu
2495                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2496               (tool-bar-add-item-from-menu
2497                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2498               (tool-bar-add-item-from-menu
2499                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2500               (tool-bar-add-item-from-menu
2501                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2502               tool-bar-map)))
2503   (if gnus-summary-tool-bar-map
2504       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2505
2506 (defun gnus-score-set-default (var value)
2507   "A version of set that updates the GNU Emacs menu-bar."
2508   (set var value)
2509   ;; It is the message that forces the active status to be updated.
2510   (message ""))
2511
2512 (defun gnus-make-score-map (type)
2513   "Make a summary score map of type TYPE."
2514   (if t
2515       nil
2516     (let ((headers '(("author" "from" string)
2517                      ("subject" "subject" string)
2518                      ("article body" "body" string)
2519                      ("article head" "head" string)
2520                      ("xref" "xref" string)
2521                      ("extra header" "extra" string)
2522                      ("lines" "lines" number)
2523                      ("followups to author" "followup" string)))
2524           (types '((number ("less than" <)
2525                            ("greater than" >)
2526                            ("equal" =))
2527                    (string ("substring" s)
2528                            ("exact string" e)
2529                            ("fuzzy string" f)
2530                            ("regexp" r))))
2531           (perms '(("temporary" (current-time-string))
2532                    ("permanent" nil)
2533                    ("immediate" now)))
2534           header)
2535       (list
2536        (apply
2537         'nconc
2538         (list
2539          (if (eq type 'lower)
2540              "Lower score"
2541            "Increase score"))
2542         (let (outh)
2543           (while headers
2544             (setq header (car headers))
2545             (setq outh
2546                   (cons
2547                    (apply
2548                     'nconc
2549                     (list (car header))
2550                     (let ((ts (cdr (assoc (nth 2 header) types)))
2551                           outt)
2552                       (while ts
2553                         (setq outt
2554                               (cons
2555                                (apply
2556                                 'nconc
2557                                 (list (caar ts))
2558                                 (let ((ps perms)
2559                                       outp)
2560                                   (while ps
2561                                     (setq outp
2562                                           (cons
2563                                            (vector
2564                                             (caar ps)
2565                                             (list
2566                                              'gnus-summary-score-entry
2567                                              (nth 1 header)
2568                                              (if (or (string= (nth 1 header)
2569                                                               "head")
2570                                                      (string= (nth 1 header)
2571                                                               "body"))
2572                                                  ""
2573                                                (list 'gnus-summary-header
2574                                                      (nth 1 header)))
2575                                              (list 'quote (nth 1 (car ts)))
2576                                              (list 'gnus-score-delta-default
2577                                                    nil)
2578                                              (nth 1 (car ps))
2579                                              t)
2580                                             t)
2581                                            outp))
2582                                     (setq ps (cdr ps)))
2583                                   (list (nreverse outp))))
2584                                outt))
2585                         (setq ts (cdr ts)))
2586                       (list (nreverse outt))))
2587                    outh))
2588             (setq headers (cdr headers)))
2589           (list (nreverse outh))))))))
2590
2591 \f
2592
2593 (defun gnus-summary-mode (&optional group)
2594   "Major mode for reading articles.
2595
2596 All normal editing commands are switched off.
2597 \\<gnus-summary-mode-map>
2598 Each line in this buffer represents one article.  To read an
2599 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2600 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2601 respectively.
2602
2603 You can also post articles and send mail from this buffer.  To
2604 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2605 of an article, type `\\[gnus-summary-reply]'.
2606
2607 There are approx. one gazillion commands you can execute in this
2608 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2609
2610 The following commands are available:
2611
2612 \\{gnus-summary-mode-map}"
2613   (interactive)
2614   (kill-all-local-variables)
2615   (when (gnus-visual-p 'summary-menu 'menu)
2616     (gnus-summary-make-menu-bar)
2617     (gnus-summary-make-tool-bar))
2618   (gnus-summary-make-local-variables)
2619   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2620     (gnus-summary-make-local-variables))
2621   (gnus-make-thread-indent-array)
2622   (gnus-simplify-mode-line)
2623   (setq major-mode 'gnus-summary-mode)
2624   (setq mode-name "Summary")
2625   (make-local-variable 'minor-mode-alist)
2626   (use-local-map gnus-summary-mode-map)
2627   (buffer-disable-undo)
2628   (setq buffer-read-only t)             ;Disable modification
2629   (setq truncate-lines t)
2630   (setq selective-display t)
2631   (setq selective-display-ellipses t)   ;Display `...'
2632   (gnus-summary-set-display-table)
2633   (gnus-set-default-directory)
2634   (setq gnus-newsgroup-name group)
2635   (make-local-variable 'gnus-summary-line-format)
2636   (make-local-variable 'gnus-summary-line-format-spec)
2637   (make-local-variable 'gnus-summary-dummy-line-format)
2638   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2639   (make-local-variable 'gnus-summary-mark-positions)
2640   (make-local-hook 'pre-command-hook)
2641   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2642   (gnus-run-hooks 'gnus-summary-mode-hook)
2643   (turn-on-gnus-mailing-list-mode)
2644   (mm-enable-multibyte)
2645   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2646   (gnus-update-summary-mark-positions))
2647
2648 (defun gnus-summary-make-local-variables ()
2649   "Make all the local summary buffer variables."
2650   (let (global)
2651     (dolist (local gnus-summary-local-variables)
2652       (if (consp local)
2653           (progn
2654             (if (eq (cdr local) 'global)
2655                 ;; Copy the global value of the variable.
2656                 (setq global (symbol-value (car local)))
2657               ;; Use the value from the list.
2658               (setq global (eval (cdr local))))
2659             (set (make-local-variable (car local)) global))
2660         ;; Simple nil-valued local variable.
2661         (set (make-local-variable local) nil)))))
2662
2663 (defun gnus-summary-clear-local-variables ()
2664   (let ((locals gnus-summary-local-variables))
2665     (while locals
2666       (if (consp (car locals))
2667           (and (vectorp (caar locals))
2668                (set (caar locals) nil))
2669         (and (vectorp (car locals))
2670              (set (car locals) nil)))
2671       (setq locals (cdr locals)))))
2672
2673 ;; Summary data functions.
2674
2675 (defmacro gnus-data-number (data)
2676   `(car ,data))
2677
2678 (defmacro gnus-data-set-number (data number)
2679   `(setcar ,data ,number))
2680
2681 (defmacro gnus-data-mark (data)
2682   `(nth 1 ,data))
2683
2684 (defmacro gnus-data-set-mark (data mark)
2685   `(setcar (nthcdr 1 ,data) ,mark))
2686
2687 (defmacro gnus-data-pos (data)
2688   `(nth 2 ,data))
2689
2690 (defmacro gnus-data-set-pos (data pos)
2691   `(setcar (nthcdr 2 ,data) ,pos))
2692
2693 (defmacro gnus-data-header (data)
2694   `(nth 3 ,data))
2695
2696 (defmacro gnus-data-set-header (data header)
2697   `(setf (nth 3 ,data) ,header))
2698
2699 (defmacro gnus-data-level (data)
2700   `(nth 4 ,data))
2701
2702 (defmacro gnus-data-unread-p (data)
2703   `(= (nth 1 ,data) gnus-unread-mark))
2704
2705 (defmacro gnus-data-read-p (data)
2706   `(/= (nth 1 ,data) gnus-unread-mark))
2707
2708 (defmacro gnus-data-pseudo-p (data)
2709   `(consp (nth 3 ,data)))
2710
2711 (defmacro gnus-data-find (number)
2712   `(assq ,number gnus-newsgroup-data))
2713
2714 (defmacro gnus-data-find-list (number &optional data)
2715   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2716      (memq (assq ,number bdata)
2717            bdata)))
2718
2719 (defmacro gnus-data-make (number mark pos header level)
2720   `(list ,number ,mark ,pos ,header ,level))
2721
2722 (defun gnus-data-enter (after-article number mark pos header level offset)
2723   (let ((data (gnus-data-find-list after-article)))
2724     (unless data
2725       (error "No such article: %d" after-article))
2726     (setcdr data (cons (gnus-data-make number mark pos header level)
2727                        (cdr data)))
2728     (setq gnus-newsgroup-data-reverse nil)
2729     (gnus-data-update-list (cddr data) offset)))
2730
2731 (defun gnus-data-enter-list (after-article list &optional offset)
2732   (when list
2733     (let ((data (and after-article (gnus-data-find-list after-article)))
2734           (ilist list))
2735       (if (not (or data
2736                    after-article))
2737           (let ((odata gnus-newsgroup-data))
2738             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2739             (when offset
2740               (gnus-data-update-list odata offset)))
2741       ;; Find the last element in the list to be spliced into the main
2742         ;; list.
2743         (while (cdr list)
2744           (setq list (cdr list)))
2745         (if (not data)
2746             (progn
2747               (setcdr list gnus-newsgroup-data)
2748               (setq gnus-newsgroup-data ilist)
2749               (when offset
2750                 (gnus-data-update-list (cdr list) offset)))
2751           (setcdr list (cdr data))
2752           (setcdr data ilist)
2753           (when offset
2754             (gnus-data-update-list (cdr list) offset))))
2755       (setq gnus-newsgroup-data-reverse nil))))
2756
2757 (defun gnus-data-remove (article &optional offset)
2758   (let ((data gnus-newsgroup-data))
2759     (if (= (gnus-data-number (car data)) article)
2760         (progn
2761           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2762                 gnus-newsgroup-data-reverse nil)
2763           (when offset
2764             (gnus-data-update-list gnus-newsgroup-data offset)))
2765       (while (cdr data)
2766         (when (= (gnus-data-number (cadr data)) article)
2767           (setcdr data (cddr data))
2768           (when offset
2769             (gnus-data-update-list (cdr data) offset))
2770           (setq data nil
2771                 gnus-newsgroup-data-reverse nil))
2772         (setq data (cdr data))))))
2773
2774 (defmacro gnus-data-list (backward)
2775   `(if ,backward
2776        (or gnus-newsgroup-data-reverse
2777            (setq gnus-newsgroup-data-reverse
2778                  (reverse gnus-newsgroup-data)))
2779      gnus-newsgroup-data))
2780
2781 (defun gnus-data-update-list (data offset)
2782   "Add OFFSET to the POS of all data entries in DATA."
2783   (setq gnus-newsgroup-data-reverse nil)
2784   (while data
2785     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2786     (setq data (cdr data))))
2787
2788 (defun gnus-summary-article-pseudo-p (article)
2789   "Say whether this article is a pseudo article or not."
2790   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2791
2792 (defmacro gnus-summary-article-sparse-p (article)
2793   "Say whether this article is a sparse article or not."
2794   `(memq ,article gnus-newsgroup-sparse))
2795
2796 (defmacro gnus-summary-article-ancient-p (article)
2797   "Say whether this article is a sparse article or not."
2798   `(memq ,article gnus-newsgroup-ancient))
2799
2800 (defun gnus-article-parent-p (number)
2801   "Say whether this article is a parent or not."
2802   (let ((data (gnus-data-find-list number)))
2803     (and (cdr data)              ; There has to be an article after...
2804          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2805             (gnus-data-level (nth 1 data))))))
2806
2807 (defun gnus-article-children (number)
2808   "Return a list of all children to NUMBER."
2809   (let* ((data (gnus-data-find-list number))
2810          (level (gnus-data-level (car data)))
2811          children)
2812     (setq data (cdr data))
2813     (while (and data
2814                 (= (gnus-data-level (car data)) (1+ level)))
2815       (push (gnus-data-number (car data)) children)
2816       (setq data (cdr data)))
2817     children))
2818
2819 (defmacro gnus-summary-skip-intangible ()
2820   "If the current article is intangible, then jump to a different article."
2821   '(let ((to (get-text-property (point) 'gnus-intangible)))
2822      (and to (gnus-summary-goto-subject to))))
2823
2824 (defmacro gnus-summary-article-intangible-p ()
2825   "Say whether this article is intangible or not."
2826   '(get-text-property (point) 'gnus-intangible))
2827
2828 (defun gnus-article-read-p (article)
2829   "Say whether ARTICLE is read or not."
2830   (not (or (memq article gnus-newsgroup-marked)
2831            (memq article gnus-newsgroup-spam-marked)
2832            (memq article gnus-newsgroup-unreads)
2833            (memq article gnus-newsgroup-unselected)
2834            (memq article gnus-newsgroup-dormant))))
2835
2836 ;; Some summary mode macros.
2837
2838 (defmacro gnus-summary-article-number ()
2839   "The article number of the article on the current line.
2840 If there isn't an article number here, then we return the current
2841 article number."
2842   '(progn
2843      (gnus-summary-skip-intangible)
2844      (or (get-text-property (point) 'gnus-number)
2845          (gnus-summary-last-subject))))
2846
2847 (defmacro gnus-summary-article-header (&optional number)
2848   "Return the header of article NUMBER."
2849   `(gnus-data-header (gnus-data-find
2850                       ,(or number '(gnus-summary-article-number)))))
2851
2852 (defmacro gnus-summary-thread-level (&optional number)
2853   "Return the level of thread that starts with article NUMBER."
2854   `(if (and (eq gnus-summary-make-false-root 'dummy)
2855             (get-text-property (point) 'gnus-intangible))
2856        0
2857      (gnus-data-level (gnus-data-find
2858                        ,(or number '(gnus-summary-article-number))))))
2859
2860 (defmacro gnus-summary-article-mark (&optional number)
2861   "Return the mark of article NUMBER."
2862   `(gnus-data-mark (gnus-data-find
2863                     ,(or number '(gnus-summary-article-number)))))
2864
2865 (defmacro gnus-summary-article-pos (&optional number)
2866   "Return the position of the line of article NUMBER."
2867   `(gnus-data-pos (gnus-data-find
2868                    ,(or number '(gnus-summary-article-number)))))
2869
2870 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2871 (defmacro gnus-summary-article-subject (&optional number)
2872   "Return current subject string or nil if nothing."
2873   `(let ((headers
2874           ,(if number
2875                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2876              '(gnus-data-header (assq (gnus-summary-article-number)
2877                                       gnus-newsgroup-data)))))
2878      (and headers
2879           (vectorp headers)
2880           (mail-header-subject headers))))
2881
2882 (defmacro gnus-summary-article-score (&optional number)
2883   "Return current article score."
2884   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2885                   gnus-newsgroup-scored))
2886        gnus-summary-default-score 0))
2887
2888 (defun gnus-summary-article-children (&optional number)
2889   "Return a list of article numbers that are children of article NUMBER."
2890   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2891          (level (gnus-data-level (car data)))
2892          l children)
2893     (while (and (setq data (cdr data))
2894                 (> (setq l (gnus-data-level (car data))) level))
2895       (and (= (1+ level) l)
2896            (push (gnus-data-number (car data))
2897                  children)))
2898     (nreverse children)))
2899
2900 (defun gnus-summary-article-parent (&optional number)
2901   "Return the article number of the parent of article NUMBER."
2902   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2903                                     (gnus-data-list t)))
2904          (level (gnus-data-level (car data))))
2905     (if (zerop level)
2906         ()                              ; This is a root.
2907       ;; We search until we find an article with a level less than
2908       ;; this one.  That function has to be the parent.
2909       (while (and (setq data (cdr data))
2910                   (not (< (gnus-data-level (car data)) level))))
2911       (and data (gnus-data-number (car data))))))
2912
2913 (defun gnus-unread-mark-p (mark)
2914   "Say whether MARK is the unread mark."
2915   (= mark gnus-unread-mark))
2916
2917 (defun gnus-read-mark-p (mark)
2918   "Say whether MARK is one of the marks that mark as read.
2919 This is all marks except unread, ticked, dormant, and expirable."
2920   (not (or (= mark gnus-unread-mark)
2921            (= mark gnus-ticked-mark)
2922            (= mark gnus-spam-mark)
2923            (= mark gnus-dormant-mark)
2924            (= mark gnus-expirable-mark))))
2925
2926 (defmacro gnus-article-mark (number)
2927   "Return the MARK of article NUMBER.
2928 This macro should only be used when computing the mark the \"first\"
2929 time; i.e., when generating the summary lines.  After that,
2930 `gnus-summary-article-mark' should be used to examine the
2931 marks of articles."
2932   `(cond
2933     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2934     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2935     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2936     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2937     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2938     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2939     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2940     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2941            gnus-ancient-mark))))
2942
2943 ;; Saving hidden threads.
2944
2945 (defmacro gnus-save-hidden-threads (&rest forms)
2946   "Save hidden threads, eval FORMS, and restore the hidden threads."
2947   (let ((config (make-symbol "config")))
2948     `(let ((,config (gnus-hidden-threads-configuration)))
2949        (unwind-protect
2950            (save-excursion
2951              ,@forms)
2952          (gnus-restore-hidden-threads-configuration ,config)))))
2953 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2954 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2955
2956 (defun gnus-data-compute-positions ()
2957   "Compute the positions of all articles."
2958   (setq gnus-newsgroup-data-reverse nil)
2959   (let ((data gnus-newsgroup-data))
2960     (save-excursion
2961       (gnus-save-hidden-threads
2962         (gnus-summary-show-all-threads)
2963         (goto-char (point-min))
2964         (while data
2965           (while (get-text-property (point) 'gnus-intangible)
2966             (forward-line 1))
2967           (gnus-data-set-pos (car data) (+ (point) 3))
2968           (setq data (cdr data))
2969           (forward-line 1))))))
2970
2971 (defun gnus-hidden-threads-configuration ()
2972   "Return the current hidden threads configuration."
2973   (save-excursion
2974     (let (config)
2975       (goto-char (point-min))
2976       (while (search-forward "\r" nil t)
2977         (push (1- (point)) config))
2978       config)))
2979
2980 (defun gnus-restore-hidden-threads-configuration (config)
2981   "Restore hidden threads configuration from CONFIG."
2982   (save-excursion
2983     (let (point buffer-read-only)
2984       (while (setq point (pop config))
2985         (when (and (< point (point-max))
2986                    (goto-char point)
2987                    (eq (char-after) ?\n))
2988           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2989
2990 ;; Various summary mode internalish functions.
2991
2992 (defun gnus-mouse-pick-article (e)
2993   (interactive "e")
2994   (mouse-set-point e)
2995   (gnus-summary-next-page nil t))
2996
2997 (defun gnus-summary-set-display-table ()
2998   "Change the display table.
2999 Odd characters have a tendency to mess
3000 up nicely formatted displays - we make all possible glyphs
3001 display only a single character."
3002
3003   ;; We start from the standard display table, if any.
3004   (let ((table (or (copy-sequence standard-display-table)
3005                    (make-display-table)))
3006         (i 32))
3007     ;; Nix out all the control chars...
3008     (while (>= (setq i (1- i)) 0)
3009       (aset table i [??]))
3010    ;; ... but not newline and cr, of course.  (cr is necessary for the
3011     ;; selective display).
3012     (aset table ?\n nil)
3013     (aset table ?\r nil)
3014     ;; We keep TAB as well.
3015     (aset table ?\t nil)
3016     ;; We nix out any glyphs over 126 that are not set already.
3017     (let ((i 256))
3018       (while (>= (setq i (1- i)) 127)
3019         ;; Only modify if the entry is nil.
3020         (unless (aref table i)
3021           (aset table i [??]))))
3022     (setq buffer-display-table table)))
3023
3024 (defun gnus-summary-set-article-display-arrow (pos)
3025   "Update the overlay arrow to point to line at position POS."
3026   (when (and gnus-summary-display-arrow
3027              (boundp 'overlay-arrow-position)
3028              (boundp 'overlay-arrow-string))
3029     (save-excursion
3030       (goto-char pos)
3031       (beginning-of-line)
3032       (unless overlay-arrow-position
3033         (setq overlay-arrow-position (make-marker)))
3034       (setq overlay-arrow-string "=>"
3035             overlay-arrow-position (set-marker overlay-arrow-position
3036                                                (point)
3037                                                (current-buffer))))))
3038
3039 (defun gnus-summary-setup-buffer (group)
3040   "Initialize summary buffer."
3041   (let ((buffer (gnus-summary-buffer-name group))
3042         (dead-name (concat "*Dead Summary "
3043                            (gnus-group-decoded-name group) "*")))
3044     ;; If a dead summary buffer exists, we kill it.
3045     (when (gnus-buffer-live-p dead-name)
3046       (gnus-kill-buffer dead-name))
3047     (if (get-buffer buffer)
3048         (progn
3049           (set-buffer buffer)
3050           (setq gnus-summary-buffer (current-buffer))
3051           (not gnus-newsgroup-prepared))
3052       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3053       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3054       (gnus-summary-mode group)
3055       (when gnus-carpal
3056         (gnus-carpal-setup-buffer 'summary))
3057       (unless gnus-single-article-buffer
3058         (make-local-variable 'gnus-article-buffer)
3059         (make-local-variable 'gnus-article-current)
3060         (make-local-variable 'gnus-original-article-buffer))
3061       (setq gnus-newsgroup-name group)
3062       ;; Set any local variables in the group parameters.
3063       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3064       t)))
3065
3066 (defun gnus-set-global-variables ()
3067   "Set the global equivalents of the buffer-local variables.
3068 They are set to the latest values they had.  These reflect the summary
3069 buffer that was in action when the last article was fetched."
3070   (when (eq major-mode 'gnus-summary-mode)
3071     (setq gnus-summary-buffer (current-buffer))
3072     (let ((name gnus-newsgroup-name)
3073           (marked gnus-newsgroup-marked)
3074           (spam gnus-newsgroup-spam-marked)
3075           (unread gnus-newsgroup-unreads)
3076           (headers gnus-current-headers)
3077           (data gnus-newsgroup-data)
3078           (summary gnus-summary-buffer)
3079           (article-buffer gnus-article-buffer)
3080           (original gnus-original-article-buffer)
3081           (gac gnus-article-current)
3082           (reffed gnus-reffed-article-number)
3083           (score-file gnus-current-score-file)
3084           (default-charset gnus-newsgroup-charset)
3085           vlist)
3086       (let ((locals gnus-newsgroup-variables))
3087         (while locals
3088           (if (consp (car locals))
3089               (push (eval (caar locals)) vlist)
3090             (push (eval (car locals)) vlist))
3091           (setq locals (cdr locals)))
3092         (setq vlist (nreverse vlist)))
3093       (save-excursion
3094         (set-buffer gnus-group-buffer)
3095         (setq gnus-newsgroup-name name
3096               gnus-newsgroup-marked marked
3097               gnus-newsgroup-spam-marked spam
3098               gnus-newsgroup-unreads unread
3099               gnus-current-headers headers
3100               gnus-newsgroup-data data
3101               gnus-article-current gac
3102               gnus-summary-buffer summary
3103               gnus-article-buffer article-buffer
3104               gnus-original-article-buffer original
3105               gnus-reffed-article-number reffed
3106               gnus-current-score-file score-file
3107               gnus-newsgroup-charset default-charset)
3108         (let ((locals gnus-newsgroup-variables))
3109           (while locals
3110             (if (consp (car locals))
3111                 (set (caar locals) (pop vlist))
3112               (set (car locals) (pop vlist)))
3113             (setq locals (cdr locals))))
3114         ;; The article buffer also has local variables.
3115         (when (gnus-buffer-live-p gnus-article-buffer)
3116           (set-buffer gnus-article-buffer)
3117           (setq gnus-summary-buffer summary))))))
3118
3119 (defun gnus-summary-article-unread-p (article)
3120   "Say whether ARTICLE is unread or not."
3121   (memq article gnus-newsgroup-unreads))
3122
3123 (defun gnus-summary-first-article-p (&optional article)
3124   "Return whether ARTICLE is the first article in the buffer."
3125   (if (not (setq article (or article (gnus-summary-article-number))))
3126       nil
3127     (eq article (caar gnus-newsgroup-data))))
3128
3129 (defun gnus-summary-last-article-p (&optional article)
3130   "Return whether ARTICLE is the last article in the buffer."
3131   (if (not (setq article (or article (gnus-summary-article-number))))
3132       ;; All non-existent numbers are the last article.  :-)
3133       t
3134     (not (cdr (gnus-data-find-list article)))))
3135
3136 (defun gnus-make-thread-indent-array ()
3137   (let ((n 200))
3138     (unless (and gnus-thread-indent-array
3139                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3140       (setq gnus-thread-indent-array (make-vector 201 "")
3141             gnus-thread-indent-array-level gnus-thread-indent-level)
3142       (while (>= n 0)
3143         (aset gnus-thread-indent-array n
3144               (make-string (* n gnus-thread-indent-level) ? ))
3145         (setq n (1- n))))))
3146
3147 (defun gnus-update-summary-mark-positions ()
3148   "Compute where the summary marks are to go."
3149   (save-excursion
3150     (when (gnus-buffer-exists-p gnus-summary-buffer)
3151       (set-buffer gnus-summary-buffer))
3152     (let ((gnus-replied-mark 129)
3153           (gnus-score-below-mark 130)
3154           (gnus-score-over-mark 130)
3155           (gnus-undownloaded-mark 131)
3156           (spec gnus-summary-line-format-spec)
3157           gnus-visual pos)
3158       (save-excursion
3159         (gnus-set-work-buffer)
3160         (let ((gnus-summary-line-format-spec spec)
3161               (gnus-newsgroup-downloadable '(0)))
3162           (gnus-summary-insert-line
3163            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3164            0 nil t 128 t nil "" nil 1)
3165           (goto-char (point-min))
3166           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3167                                              (- (point) (point-min) 1)))))
3168           (goto-char (point-min))
3169           (push (cons 'replied (and (search-forward "\201" nil t)
3170                                     (- (point) (point-min) 1)))
3171                 pos)
3172           (goto-char (point-min))
3173           (push (cons 'score (and (search-forward "\202" nil t)
3174                                   (- (point) (point-min) 1)))
3175                 pos)
3176           (goto-char (point-min))
3177           (push (cons 'download
3178                       (and (search-forward "\203" nil t)
3179                            (- (point) (point-min) 1)))
3180                 pos)))
3181       (setq gnus-summary-mark-positions pos))))
3182
3183 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3184   "Insert a dummy root in the summary buffer."
3185   (beginning-of-line)
3186   (gnus-add-text-properties
3187    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3188    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3189
3190 (defun gnus-summary-extract-address-component (from)
3191   (or (car (funcall gnus-extract-address-components from))
3192       from))
3193
3194 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3195   (let ((mail-parse-charset gnus-newsgroup-charset)
3196         ; Is it really necessary to do this next part for each summary line?
3197         ; Luckily, doesn't seem to slow things down much.
3198         (mail-parse-ignored-charsets
3199          (save-excursion (set-buffer gnus-summary-buffer)
3200                          gnus-newsgroup-ignored-charsets)))
3201     (or
3202      (and gnus-ignored-from-addresses
3203           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3204           (let ((extra-headers (mail-header-extra header))
3205                 to
3206                 newsgroups)
3207             (cond
3208              ((setq to (cdr (assq 'To extra-headers)))
3209               (concat "-> "
3210                       (inline
3211                         (gnus-summary-extract-address-component
3212                          (funcall gnus-decode-encoded-word-function to)))))
3213              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3214               (concat "=> " newsgroups)))))
3215      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3216
3217 (defun gnus-summary-insert-line (gnus-tmp-header
3218                                  gnus-tmp-level gnus-tmp-current
3219                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3220                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3221                                  &optional gnus-tmp-dummy gnus-tmp-score
3222                                  gnus-tmp-process)
3223   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3224          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3225          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3226          (gnus-tmp-score-char
3227           (if (or (null gnus-summary-default-score)
3228                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3229                       gnus-summary-zcore-fuzz))
3230               ?                         ;Whitespace
3231             (if (< gnus-tmp-score gnus-summary-default-score)
3232                 gnus-score-below-mark gnus-score-over-mark)))
3233          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3234          (gnus-tmp-replied
3235           (cond (gnus-tmp-process gnus-process-mark)
3236                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3237                  gnus-cached-mark)
3238                 (gnus-tmp-replied gnus-replied-mark)
3239                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3240                  gnus-forwarded-mark)
3241                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3242                  gnus-saved-mark)
3243                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3244                  gnus-recent-mark)
3245                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3246                  gnus-unseen-mark)
3247                 (t gnus-no-mark)))
3248          (gnus-tmp-downloaded
3249           (cond (undownloaded 
3250                  gnus-undownloaded-mark)
3251                 (gnus-newsgroup-agentized
3252                  gnus-downloaded-mark)
3253                 (t
3254                  gnus-no-mark)))
3255          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3256          (gnus-tmp-name
3257           (cond
3258            ((string-match "<[^>]+> *$" gnus-tmp-from)
3259             (let ((beg (match-beginning 0)))
3260               (or (and (string-match "^\".+\"" gnus-tmp-from)
3261                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3262                   (substring gnus-tmp-from 0 beg))))
3263            ((string-match "(.+)" gnus-tmp-from)
3264             (substring gnus-tmp-from
3265                        (1+ (match-beginning 0)) (1- (match-end 0))))
3266            (t gnus-tmp-from)))
3267          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3268          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3269          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3270          (buffer-read-only nil))
3271     (when (string= gnus-tmp-name "")
3272       (setq gnus-tmp-name gnus-tmp-from))
3273     (unless (numberp gnus-tmp-lines)
3274       (setq gnus-tmp-lines -1))
3275     (if (= gnus-tmp-lines -1)
3276         (setq gnus-tmp-lines "?")
3277       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines))) 
3278       (gnus-put-text-property
3279      (point)
3280      (progn (eval gnus-summary-line-format-spec) (point))
3281        'gnus-number gnus-tmp-number)
3282     (when (gnus-visual-p 'summary-highlight 'highlight)
3283       (forward-line -1)
3284       (gnus-run-hooks 'gnus-summary-update-hook)
3285       (forward-line 1))))
3286
3287 (defun gnus-summary-update-line (&optional dont-update)
3288   "Update summary line after change."
3289   (when (and gnus-summary-default-score
3290              (not gnus-summary-inhibit-highlight))
3291     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3292            (article (gnus-summary-article-number))
3293            (score (gnus-summary-article-score article)))
3294       (unless dont-update
3295         (if (and gnus-summary-mark-below
3296                  (< (gnus-summary-article-score)
3297                     gnus-summary-mark-below))
3298             ;; This article has a low score, so we mark it as read.
3299             (when (memq article gnus-newsgroup-unreads)
3300               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3301           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3302             ;; This article was previously marked as read on account
3303             ;; of a low score, but now it has risen, so we mark it as
3304             ;; unread.
3305             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3306         (gnus-summary-update-mark
3307          (if (or (null gnus-summary-default-score)
3308                  (<= (abs (- score gnus-summary-default-score))
3309                      gnus-summary-zcore-fuzz))
3310              ?                          ;Whitespace
3311            (if (< score gnus-summary-default-score)
3312                gnus-score-below-mark gnus-score-over-mark))
3313          'score))
3314       ;; Do visual highlighting.
3315       (when (gnus-visual-p 'summary-highlight 'highlight)
3316         (gnus-run-hooks 'gnus-summary-update-hook)))))
3317
3318 (defvar gnus-tmp-new-adopts nil)
3319
3320 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3321   "Return the number of articles in THREAD.
3322 This may be 0 in some cases -- if none of the articles in
3323 the thread are to be displayed."
3324   (let* ((number
3325          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3326           (cond
3327            ((not (listp thread))
3328             1)
3329            ((and (consp thread) (cdr thread))
3330             (apply
3331              '+ 1 (mapcar
3332                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3333            ((null thread)
3334             1)
3335            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3336             1)
3337            (t 0))))
3338     (when (and level (zerop level) gnus-tmp-new-adopts)
3339       (incf number
3340             (apply '+ (mapcar
3341                        'gnus-summary-number-of-articles-in-thread
3342                        gnus-tmp-new-adopts))))
3343     (if char
3344         (if (> number 1) gnus-not-empty-thread-mark
3345           gnus-empty-thread-mark)
3346       number)))
3347
3348 (defsubst gnus-summary-line-message-size (head)
3349   "Return pretty-printed version of message size.
3350 This function is intended to be used in
3351 `gnus-summary-line-format-alist', which see."
3352   (let ((c (or (mail-header-chars head) -1)))
3353     (cond ((< c 0) "n/a")               ; chars not available
3354           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3355           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3356           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3357           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3358
3359
3360 (defun gnus-summary-set-local-parameters (group)
3361   "Go through the local params of GROUP and set all variable specs in that list."
3362   (let ((params (gnus-group-find-parameter group))
3363         (vars '(quit-config))           ; Ignore quit-config.
3364         elem)
3365     (while params
3366       (setq elem (car params)
3367             params (cdr params))
3368       (and (consp elem)                 ; Has to be a cons.
3369            (consp (cdr elem))           ; The cdr has to be a list.
3370            (symbolp (car elem))         ; Has to be a symbol in there.
3371            (not (memq (car elem) vars))
3372            (ignore-errors               ; So we set it.
3373              (push (car elem) vars)
3374              (make-local-variable (car elem))
3375              (set (car elem) (eval (nth 1 elem))))))))
3376
3377 (defun gnus-summary-read-group (group &optional show-all no-article
3378                                       kill-buffer no-display backward
3379                                       select-articles)
3380   "Start reading news in newsgroup GROUP.
3381 If SHOW-ALL is non-nil, already read articles are also listed.
3382 If NO-ARTICLE is non-nil, no article is selected initially.
3383 If NO-DISPLAY, don't generate a summary buffer."
3384   (let (result)
3385     (while (and group
3386                 (null (setq result
3387                             (let ((gnus-auto-select-next nil))
3388                               (or (gnus-summary-read-group-1
3389                                    group show-all no-article
3390                                    kill-buffer no-display
3391                                    select-articles)
3392                                   (setq show-all nil
3393                                         select-articles nil)))))
3394                 (eq gnus-auto-select-next 'quietly))
3395       (set-buffer gnus-group-buffer)
3396       ;; The entry function called above goes to the next
3397       ;; group automatically, so we go two groups back
3398       ;; if we are searching for the previous group.
3399       (when backward
3400         (gnus-group-prev-unread-group 2))
3401       (if (not (equal group (gnus-group-group-name)))
3402           (setq group (gnus-group-group-name))
3403         (setq group nil)))
3404     result))
3405
3406 (defun gnus-summary-read-group-1 (group show-all no-article
3407                                         kill-buffer no-display
3408                                         &optional select-articles)
3409   ;; Killed foreign groups can't be entered.
3410   ;;  (when (and (not (gnus-group-native-p group))
3411   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3412   ;;    (error "Dead non-native groups can't be entered"))
3413   (gnus-message 5 "Retrieving newsgroup: %s..."
3414                 (gnus-group-decoded-name group))
3415   (let* ((new-group (gnus-summary-setup-buffer group))
3416          (quit-config (gnus-group-quit-config group))
3417          (did-select (and new-group (gnus-select-newsgroup
3418                                      group show-all select-articles))))
3419     (cond
3420      ;; This summary buffer exists already, so we just select it.
3421      ((not new-group)
3422       (gnus-set-global-variables)
3423       (when kill-buffer
3424         (gnus-kill-or-deaden-summary kill-buffer))
3425       (gnus-configure-windows 'summary 'force)
3426       (gnus-set-mode-line 'summary)
3427       (gnus-summary-position-point)
3428       (message "")
3429       t)
3430      ;; We couldn't select this group.
3431      ((null did-select)
3432       (when (and (eq major-mode 'gnus-summary-mode)
3433                  (not (equal (current-buffer) kill-buffer)))
3434         (kill-buffer (current-buffer))
3435         (if (not quit-config)
3436             (progn
3437               ;; Update the info -- marks might need to be removed,
3438               ;; for instance.
3439               (gnus-summary-update-info)
3440               (set-buffer gnus-group-buffer)
3441               (gnus-group-jump-to-group group)
3442               (gnus-group-next-unread-group 1))
3443           (gnus-handle-ephemeral-exit quit-config)))
3444       (let ((grpinfo (gnus-get-info group)))
3445         (if (null (gnus-info-read grpinfo))
3446             (gnus-message 3 "Group %s contains no messages"
3447                           (gnus-group-decoded-name group))
3448           (gnus-message 3 "Can't select group")))
3449       nil)
3450      ;; The user did a `C-g' while prompting for number of articles,
3451      ;; so we exit this group.
3452      ((eq did-select 'quit)
3453       (and (eq major-mode 'gnus-summary-mode)
3454            (not (equal (current-buffer) kill-buffer))
3455            (kill-buffer (current-buffer)))
3456       (when kill-buffer
3457         (gnus-kill-or-deaden-summary kill-buffer))
3458       (if (not quit-config)
3459           (progn
3460             (set-buffer gnus-group-buffer)
3461             (gnus-group-jump-to-group group)
3462             (gnus-group-next-unread-group 1)
3463             (gnus-configure-windows 'group 'force))
3464         (gnus-handle-ephemeral-exit quit-config))
3465       ;; Finally signal the quit.
3466       (signal 'quit nil))
3467      ;; The group was successfully selected.
3468      (t
3469       (gnus-set-global-variables)
3470       ;; Save the active value in effect when the group was entered.
3471       (setq gnus-newsgroup-active
3472             (gnus-copy-sequence
3473              (gnus-active gnus-newsgroup-name)))
3474       ;; You can change the summary buffer in some way with this hook.
3475       (gnus-run-hooks 'gnus-select-group-hook)
3476       (gnus-update-format-specifications
3477        nil 'summary 'summary-mode 'summary-dummy)
3478       (gnus-update-summary-mark-positions)
3479       ;; Do score processing.
3480       (when gnus-use-scoring
3481         (gnus-possibly-score-headers))
3482       ;; Check whether to fill in the gaps in the threads.
3483       (when gnus-build-sparse-threads
3484         (gnus-build-sparse-threads))
3485       ;; Find the initial limit.
3486       (if gnus-show-threads
3487           (if show-all
3488               (let ((gnus-newsgroup-dormant nil))
3489                 (gnus-summary-initial-limit show-all))
3490             (gnus-summary-initial-limit show-all))
3491         ;; When unthreaded, all articles are always shown.
3492         (setq gnus-newsgroup-limit
3493               (mapcar
3494                (lambda (header) (mail-header-number header))
3495                gnus-newsgroup-headers)))
3496       ;; Generate the summary buffer.
3497       (unless no-display
3498         (gnus-summary-prepare))
3499       (when gnus-use-trees
3500         (gnus-tree-open group)
3501         (setq gnus-summary-highlight-line-function
3502               'gnus-tree-highlight-article))
3503       ;; If the summary buffer is empty, but there are some low-scored
3504       ;; articles or some excluded dormants, we include these in the
3505       ;; buffer.
3506       (when (and (zerop (buffer-size))
3507                  (not no-display))
3508         (cond (gnus-newsgroup-dormant
3509                (gnus-summary-limit-include-dormant))
3510               ((and gnus-newsgroup-scored show-all)
3511                (gnus-summary-limit-include-expunged t))))
3512       ;; Function `gnus-apply-kill-file' must be called in this hook.
3513       (gnus-run-hooks 'gnus-apply-kill-hook)
3514       (if (and (zerop (buffer-size))
3515                (not no-display))
3516           (progn
3517             ;; This newsgroup is empty.
3518             (gnus-summary-catchup-and-exit nil t)
3519             (gnus-message 6 "No unread news")
3520             (when kill-buffer
3521               (gnus-kill-or-deaden-summary kill-buffer))
3522             ;; Return nil from this function.
3523             nil)
3524         ;; Hide conversation thread subtrees.  We cannot do this in
3525         ;; gnus-summary-prepare-hook since kill processing may not
3526         ;; work with hidden articles.
3527         (gnus-summary-maybe-hide-threads)
3528         (when kill-buffer
3529           (gnus-kill-or-deaden-summary kill-buffer))
3530         (gnus-summary-auto-select-subject)
3531         ;; Show first unread article if requested.
3532         (if (and (not no-article)
3533                  (not no-display)
3534                  gnus-newsgroup-unreads
3535                  gnus-auto-select-first)
3536             (progn
3537               (gnus-configure-windows 'summary)
3538               (let ((art (gnus-summary-article-number)))
3539                 (unless (and (not gnus-plugged)
3540                              (or (memq art gnus-newsgroup-undownloaded)
3541                                  (memq art gnus-newsgroup-downloadable)))
3542                   (gnus-summary-goto-article art))))
3543           ;; Don't select any articles.
3544           (gnus-summary-position-point)
3545           (gnus-configure-windows 'summary 'force)
3546           (gnus-set-mode-line 'summary))
3547         (when (and gnus-auto-center-group
3548                    (get-buffer-window gnus-group-buffer t))
3549           ;; Gotta use windows, because recenter does weird stuff if
3550           ;; the current buffer ain't the displayed window.
3551           (let ((owin (selected-window)))
3552             (select-window (get-buffer-window gnus-group-buffer t))
3553             (when (gnus-group-goto-group group)
3554               (recenter))
3555             (select-window owin)))
3556         ;; Mark this buffer as "prepared".
3557         (setq gnus-newsgroup-prepared t)
3558         (gnus-run-hooks 'gnus-summary-prepared-hook)
3559         (unless (gnus-ephemeral-group-p group)
3560           (gnus-group-update-group group))
3561         t)))))
3562
3563 (defun gnus-summary-auto-select-subject ()
3564   "Select the subject line on initial group entry."
3565   (goto-char (point-min))
3566   (cond
3567    ((eq gnus-auto-select-subject 'best)
3568     (gnus-summary-best-unread-subject))
3569    ((eq gnus-auto-select-subject 'unread)
3570     (gnus-summary-first-unread-subject))
3571    ((eq gnus-auto-select-subject 'unseen)
3572     (gnus-summary-first-unseen-subject))
3573    ((eq gnus-auto-select-subject 'unseen-or-unread)
3574     (gnus-summary-first-unseen-or-unread-subject))
3575    ((eq gnus-auto-select-subject 'first)
3576     ;; Do nothing.
3577     )
3578    ((gnus-functionp gnus-auto-select-subject)
3579     (funcall gnus-auto-select-subject))))
3580
3581 (defun gnus-summary-prepare ()
3582   "Generate the summary buffer."
3583   (interactive)
3584   (let ((buffer-read-only nil))
3585     (erase-buffer)
3586     (setq gnus-newsgroup-data nil
3587           gnus-newsgroup-data-reverse nil)
3588     (gnus-run-hooks 'gnus-summary-generate-hook)
3589     ;; Generate the buffer, either with threads or without.
3590     (when gnus-newsgroup-headers
3591       (gnus-summary-prepare-threads
3592        (if gnus-show-threads
3593            (gnus-sort-gathered-threads
3594             (funcall gnus-summary-thread-gathering-function
3595                      (gnus-sort-threads
3596                       (gnus-cut-threads (gnus-make-threads)))))
3597          ;; Unthreaded display.
3598          (gnus-sort-articles gnus-newsgroup-headers))))
3599     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3600     ;; Call hooks for modifying summary buffer.
3601     (goto-char (point-min))
3602     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3603
3604 (defsubst gnus-general-simplify-subject (subject)
3605   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3606   (setq subject
3607         (cond
3608          ;; Truncate the subject.
3609          (gnus-simplify-subject-functions
3610           (gnus-map-function gnus-simplify-subject-functions subject))
3611          ((numberp gnus-summary-gather-subject-limit)
3612           (setq subject (gnus-simplify-subject-re subject))
3613           (if (> (length subject) gnus-summary-gather-subject-limit)
3614               (substring subject 0 gnus-summary-gather-subject-limit)
3615             subject))
3616          ;; Fuzzily simplify it.
3617          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3618           (gnus-simplify-subject-fuzzy subject))
3619          ;; Just remove the leading "Re:".
3620          (t
3621           (gnus-simplify-subject-re subject))))
3622
3623   (if (and gnus-summary-gather-exclude-subject
3624            (string-match gnus-summary-gather-exclude-subject subject))
3625       nil                         ; This article shouldn't be gathered
3626     subject))
3627
3628 (defun gnus-summary-simplify-subject-query ()
3629   "Query where the respool algorithm would put this article."
3630   (interactive)
3631   (gnus-summary-select-article)
3632   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3633
3634 (defun gnus-gather-threads-by-subject (threads)
3635   "Gather threads by looking at Subject headers."
3636   (if (not gnus-summary-make-false-root)
3637       threads
3638     (let ((hashtb (gnus-make-hashtable 1024))
3639           (prev threads)
3640           (result threads)
3641           subject hthread whole-subject)
3642       (while threads
3643         (setq subject (gnus-general-simplify-subject
3644                        (setq whole-subject (mail-header-subject
3645                                             (caar threads)))))
3646         (when subject
3647           (if (setq hthread (gnus-gethash subject hashtb))
3648               (progn
3649                 ;; We enter a dummy root into the thread, if we
3650                 ;; haven't done that already.
3651                 (unless (stringp (caar hthread))
3652                   (setcar hthread (list whole-subject (car hthread))))
3653                 ;; We add this new gathered thread to this gathered
3654                 ;; thread.
3655                 (setcdr (car hthread)
3656                         (nconc (cdar hthread) (list (car threads))))
3657                 ;; Remove it from the list of threads.
3658                 (setcdr prev (cdr threads))
3659                 (setq threads prev))
3660             ;; Enter this thread into the hash table.
3661             (gnus-sethash subject
3662                           (if gnus-summary-make-false-root-always
3663                               (progn
3664                                 ;; If you want a dummy root above all
3665                                 ;; threads...
3666                                 (setcar threads (list whole-subject
3667                                                       (car threads)))
3668                                 threads)
3669                             threads)
3670                           hashtb)))
3671         (setq prev threads)
3672         (setq threads (cdr threads)))
3673       result)))
3674
3675 (defun gnus-gather-threads-by-references (threads)
3676   "Gather threads by looking at References headers."
3677   (let ((idhashtb (gnus-make-hashtable 1024))
3678         (thhashtb (gnus-make-hashtable 1024))
3679         (prev threads)
3680         (result threads)
3681         ids references id gthread gid entered ref)
3682     (while threads
3683       (when (setq references (mail-header-references (caar threads)))
3684         (setq id (mail-header-id (caar threads))
3685               ids (inline (gnus-split-references references))
3686               entered nil)
3687         (while (setq ref (pop ids))
3688           (setq ids (delete ref ids))
3689           (if (not (setq gid (gnus-gethash ref idhashtb)))
3690               (progn
3691                 (gnus-sethash ref id idhashtb)
3692                 (gnus-sethash id threads thhashtb))
3693             (setq gthread (gnus-gethash gid thhashtb))
3694             (unless entered
3695               ;; We enter a dummy root into the thread, if we
3696               ;; haven't done that already.
3697               (unless (stringp (caar gthread))
3698                 (setcar gthread (list (mail-header-subject (caar gthread))
3699                                       (car gthread))))
3700               ;; We add this new gathered thread to this gathered
3701               ;; thread.
3702               (setcdr (car gthread)
3703                       (nconc (cdar gthread) (list (car threads)))))
3704             ;; Add it into the thread hash table.
3705             (gnus-sethash id gthread thhashtb)
3706             (setq entered t)
3707             ;; Remove it from the list of threads.
3708             (setcdr prev (cdr threads))
3709             (setq threads prev))))
3710       (setq prev threads)
3711       (setq threads (cdr threads)))
3712     result))
3713
3714 (defun gnus-sort-gathered-threads (threads)
3715   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3716   (let ((result threads))
3717     (while threads
3718       (when (stringp (caar threads))
3719         (setcdr (car threads)
3720                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3721       (setq threads (cdr threads)))
3722     result))
3723
3724 (defun gnus-thread-loop-p (root thread)
3725   "Say whether ROOT is in THREAD."
3726   (let ((stack (list thread))
3727         (infloop 0)
3728         th)
3729     (while (setq thread (pop stack))
3730       (setq th (cdr thread))
3731       (while (and th
3732                   (not (eq (caar th) root)))
3733         (pop th))
3734       (if th
3735           ;; We have found a loop.
3736           (let (ref-dep)
3737             (setcdr thread (delq (car th) (cdr thread)))
3738             (if (boundp (setq ref-dep (intern "none"
3739                                               gnus-newsgroup-dependencies)))
3740                 (setcdr (symbol-value ref-dep)
3741                         (nconc (cdr (symbol-value ref-dep))
3742                                (list (car th))))
3743               (set ref-dep (list nil (car th))))
3744             (setq infloop 1
3745                   stack nil))
3746         ;; Push all the subthreads onto the stack.
3747         (push (cdr thread) stack)))
3748     infloop))
3749
3750 (defun gnus-make-threads ()
3751   "Go through the dependency hashtb and find the roots.  Return all threads."
3752   (let (threads)
3753     (while (catch 'infloop
3754              (mapatoms
3755               (lambda (refs)
3756                 ;; Deal with self-referencing References loops.
3757                 (when (and (car (symbol-value refs))
3758                            (not (zerop
3759                                  (apply
3760                                   '+
3761                                   (mapcar
3762                                    (lambda (thread)
3763                                      (gnus-thread-loop-p
3764                                       (car (symbol-value refs)) thread))
3765                                    (cdr (symbol-value refs)))))))
3766                   (setq threads nil)
3767                   (throw 'infloop t))
3768                 (unless (car (symbol-value refs))
3769                   ;; These threads do not refer back to any other
3770                   ;; articles, so they're roots.
3771                   (setq threads (append (cdr (symbol-value refs)) threads))))
3772               gnus-newsgroup-dependencies)))
3773     threads))
3774
3775 ;; Build the thread tree.
3776 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3777   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3778
3779 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3780 if it was already present.
3781
3782 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3783 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3784 Message-IDs will be renamed to a unique Message-ID before being
3785 entered.
3786
3787 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3788   (let* ((id (mail-header-id header))
3789          (id-dep (and id (intern id dependencies)))
3790          parent-id ref ref-dep ref-header replaced)
3791     ;; Enter this `header' in the `dependencies' table.
3792     (cond
3793      ((not id-dep)
3794       (setq header nil))
3795      ;; The first two cases do the normal part: enter a new `header'
3796      ;; in the `dependencies' table.
3797      ((not (boundp id-dep))
3798       (set id-dep (list header)))
3799      ((null (car (symbol-value id-dep)))
3800       (setcar (symbol-value id-dep) header))
3801
3802      ;; From here the `header' was already present in the
3803      ;; `dependencies' table.
3804      (force-new
3805       ;; Overrides an existing entry;
3806       ;; just set the header part of the entry.
3807       (setcar (symbol-value id-dep) header)
3808       (setq replaced t))
3809
3810      ;; Renames the existing `header' to a unique Message-ID.
3811      ((not gnus-summary-ignore-duplicates)
3812       ;; An article with this Message-ID has already been seen.
3813       ;; We rename the Message-ID.
3814       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3815            (list header))
3816       (mail-header-set-id header id))
3817
3818      ;; The last case ignores an existing entry, except it adds any
3819      ;; additional Xrefs (in case the two articles came from different
3820      ;; servers.
3821      ;; Also sets `header' to `nil' meaning that the `dependencies'
3822      ;; table was *not* modified.
3823      (t
3824       (mail-header-set-xref
3825        (car (symbol-value id-dep))
3826        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3827                    "")
3828                (or (mail-header-xref header) "")))
3829       (setq header nil)))
3830
3831     (when (and header (not replaced))
3832       ;; First check that we are not creating a References loop.
3833       (setq parent-id (gnus-parent-id (mail-header-references header)))
3834       (setq ref parent-id)
3835       (while (and ref
3836                   (setq ref-dep (intern-soft ref dependencies))
3837                   (boundp ref-dep)
3838                   (setq ref-header (car (symbol-value ref-dep))))
3839         (if (string= id ref)
3840             ;; Yuk!  This is a reference loop.  Make the article be a
3841             ;; root article.
3842             (progn
3843               (mail-header-set-references (car (symbol-value id-dep)) "none")
3844               (setq ref nil)
3845               (setq parent-id nil))
3846           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3847       (setq ref-dep (intern (or parent-id "none") dependencies))
3848       (if (boundp ref-dep)
3849           (setcdr (symbol-value ref-dep)
3850                   (nconc (cdr (symbol-value ref-dep))
3851                          (list (symbol-value id-dep))))
3852         (set ref-dep (list nil (symbol-value id-dep)))))
3853     header))
3854
3855 (defun gnus-extract-message-id-from-in-reply-to (string)
3856   (if (string-match "<[^>]+>" string)
3857       (substring string (match-beginning 0) (match-end 0))
3858     nil))
3859
3860 (defun gnus-build-sparse-threads ()
3861   (let ((headers gnus-newsgroup-headers)
3862         (mail-parse-charset gnus-newsgroup-charset)
3863         (gnus-summary-ignore-duplicates t)
3864         header references generation relations
3865         subject child end new-child date)
3866     ;; First we create an alist of generations/relations, where
3867     ;; generations is how much we trust the relation, and the relation
3868     ;; is parent/child.
3869     (gnus-message 7 "Making sparse threads...")
3870     (save-excursion
3871       (nnheader-set-temp-buffer " *gnus sparse threads*")
3872       (while (setq header (pop headers))
3873         (when (and (setq references (mail-header-references header))
3874                    (not (string= references "")))
3875           (insert references)
3876           (setq child (mail-header-id header)
3877                 subject (mail-header-subject header)
3878                 date (mail-header-date header)
3879                 generation 0)
3880           (while (search-backward ">" nil t)
3881             (setq end (1+ (point)))
3882             (when (search-backward "<" nil t)
3883               (setq new-child (buffer-substring (point) end))
3884               (push (list (incf generation)
3885                           child (setq child new-child)
3886                           subject date)
3887                     relations)))
3888           (when child
3889             (push (list (1+ generation) child nil subject) relations))
3890           (erase-buffer)))
3891       (kill-buffer (current-buffer)))
3892     ;; Sort over trustworthiness.
3893     (mapcar
3894      (lambda (relation)
3895        (when (gnus-dependencies-add-header
3896               (make-full-mail-header
3897                gnus-reffed-article-number
3898                (nth 3 relation) "" (or (nth 4 relation) "")
3899                (nth 1 relation)
3900                (or (nth 2 relation) "") 0 0 "")
3901               gnus-newsgroup-dependencies nil)
3902          (push gnus-reffed-article-number gnus-newsgroup-limit)
3903          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3904          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3905                gnus-newsgroup-reads)
3906          (decf gnus-reffed-article-number)))
3907      (sort relations 'car-less-than-car))
3908     (gnus-message 7 "Making sparse threads...done")))
3909
3910 (defun gnus-build-old-threads ()
3911   ;; Look at all the articles that refer back to old articles, and
3912   ;; fetch the headers for the articles that aren't there.  This will
3913   ;; build complete threads - if the roots haven't been expired by the
3914   ;; server, that is.
3915   (let ((mail-parse-charset gnus-newsgroup-charset)
3916         id heads)
3917     (mapatoms
3918      (lambda (refs)
3919        (when (not (car (symbol-value refs)))
3920          (setq heads (cdr (symbol-value refs)))
3921          (while heads
3922            (if (memq (mail-header-number (caar heads))
3923                      gnus-newsgroup-dormant)
3924                (setq heads (cdr heads))
3925              (setq id (symbol-name refs))
3926              (while (and (setq id (gnus-build-get-header id))
3927                          (not (car (gnus-id-to-thread id)))))
3928              (setq heads nil)))))
3929      gnus-newsgroup-dependencies)))
3930
3931 ;; This function has to be called with point after the article number
3932 ;; on the beginning of the line.
3933 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3934   (let ((eol (gnus-point-at-eol))
3935         (buffer (current-buffer))
3936         header references in-reply-to)
3937
3938     ;; overview: [num subject from date id refs chars lines misc]
3939     (unwind-protect
3940         (let (x)
3941           (narrow-to-region (point) eol)
3942           (unless (eobp)
3943             (forward-char))
3944
3945           (setq header
3946                 (make-full-mail-header
3947                  number                 ; number
3948                  (condition-case ()     ; subject
3949                      (funcall gnus-decode-encoded-word-function
3950                               (setq x (nnheader-nov-field)))
3951                    (error x))
3952                  (condition-case ()     ; from
3953                      (funcall gnus-decode-encoded-word-function
3954                               (setq x (nnheader-nov-field)))
3955                    (error x))
3956                  (nnheader-nov-field)   ; date
3957                  (nnheader-nov-read-message-id) ; id
3958                  (setq references (nnheader-nov-field)) ; refs
3959                  (nnheader-nov-read-integer) ; chars
3960                  (nnheader-nov-read-integer) ; lines
3961                  (unless (eobp)
3962                    (if (looking-at "Xref: ")
3963                        (goto-char (match-end 0)))
3964                    (nnheader-nov-field)) ; Xref
3965                  (nnheader-nov-parse-extra)))) ; extra
3966
3967       (widen))
3968
3969     (when (and (string= references "")
3970                (setq in-reply-to (mail-header-extra header))
3971                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3972       (mail-header-set-references
3973        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3974
3975     (when gnus-alter-header-function
3976       (funcall gnus-alter-header-function header))
3977     (gnus-dependencies-add-header header dependencies force-new)))
3978
3979 (defun gnus-build-get-header (id)
3980   "Look through the buffer of NOV lines and find the header to ID.
3981 Enter this line into the dependencies hash table, and return
3982 the id of the parent article (if any)."
3983   (let ((deps gnus-newsgroup-dependencies)
3984         found header)
3985     (prog1
3986         (save-excursion
3987           (set-buffer nntp-server-buffer)
3988           (let ((case-fold-search nil))
3989             (goto-char (point-min))
3990             (while (and (not found)
3991                         (search-forward id nil t))
3992               (beginning-of-line)
3993               (setq found (looking-at
3994                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3995                                    (regexp-quote id))))
3996               (or found (beginning-of-line 2)))
3997             (when found
3998               (beginning-of-line)
3999               (and
4000                (setq header (gnus-nov-parse-line
4001                              (read (current-buffer)) deps))
4002                (gnus-parent-id (mail-header-references header))))))
4003       (when header
4004         (let ((number (mail-header-number header)))
4005           (push number gnus-newsgroup-limit)
4006           (push header gnus-newsgroup-headers)
4007           (if (memq number gnus-newsgroup-unselected)
4008               (progn
4009                 (setq gnus-newsgroup-unreads
4010                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4011                                                number))
4012                 (setq gnus-newsgroup-unselected
4013                       (delq number gnus-newsgroup-unselected)))
4014             (push number gnus-newsgroup-ancient)))))))
4015
4016 (defun gnus-build-all-threads ()
4017   "Read all the headers."
4018   (let ((gnus-summary-ignore-duplicates t)
4019         (mail-parse-charset gnus-newsgroup-charset)
4020         (dependencies gnus-newsgroup-dependencies)
4021         header article)
4022     (save-excursion
4023       (set-buffer nntp-server-buffer)
4024       (let ((case-fold-search nil))
4025         (goto-char (point-min))
4026         (while (not (eobp))
4027           (ignore-errors
4028             (setq article (read (current-buffer))
4029                   header (gnus-nov-parse-line article dependencies)))
4030           (when header
4031             (save-excursion
4032               (set-buffer gnus-summary-buffer)
4033               (push header gnus-newsgroup-headers)
4034               (if (memq (setq article (mail-header-number header))
4035                         gnus-newsgroup-unselected)
4036                   (progn
4037                     (setq gnus-newsgroup-unreads
4038                           (gnus-add-to-sorted-list
4039                            gnus-newsgroup-unreads article))
4040                     (setq gnus-newsgroup-unselected
4041                           (delq article gnus-newsgroup-unselected)))
4042                 (push article gnus-newsgroup-ancient)))
4043             (forward-line 1)))))))
4044
4045 (defun gnus-summary-update-article-line (article header)
4046   "Update the line for ARTICLE using HEADERS."
4047   (let* ((id (mail-header-id header))
4048          (thread (gnus-id-to-thread id)))
4049     (unless thread
4050       (error "Article in no thread"))
4051     ;; Update the thread.
4052     (setcar thread header)
4053     (gnus-summary-goto-subject article)
4054     (let* ((datal (gnus-data-find-list article))
4055            (data (car datal))
4056            (buffer-read-only nil)
4057            (level (gnus-summary-thread-level)))
4058       (gnus-delete-line)
4059       (let ((inserted (- (point)
4060                          (progn
4061                            (gnus-summary-insert-line
4062                             header level nil 
4063                             (memq article gnus-newsgroup-undownloaded)
4064                             (gnus-article-mark article)
4065                             (memq article gnus-newsgroup-replied)
4066                             (memq article gnus-newsgroup-expirable)
4067                             ;; Only insert the Subject string when it's different
4068                             ;; from the previous Subject string.
4069                             (if (and
4070                                  gnus-show-threads
4071                                  (gnus-subject-equal
4072                                   (condition-case ()
4073                                       (mail-header-subject
4074                                        (gnus-data-header
4075                                         (cadr
4076                                          (gnus-data-find-list
4077                                           article
4078                                           (gnus-data-list t)))))
4079                                     ;; Error on the side of excessive subjects.
4080                                     (error ""))
4081                                   (mail-header-subject header)))
4082                                 ""
4083                               (mail-header-subject header))
4084                             nil (cdr (assq article gnus-newsgroup-scored))
4085                             (memq article gnus-newsgroup-processable))
4086                            (point)))))
4087         (when (cdr datal)
4088           (gnus-data-update-list
4089            (cdr datal) 
4090            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4091
4092 (defun gnus-summary-update-article (article &optional iheader)
4093   "Update ARTICLE in the summary buffer."
4094   (set-buffer gnus-summary-buffer)
4095   (let* ((header (gnus-summary-article-header article))
4096          (id (mail-header-id header))
4097          (data (gnus-data-find article))
4098          (thread (gnus-id-to-thread id))
4099          (references (mail-header-references header))
4100          (parent
4101           (gnus-id-to-thread
4102            (or (gnus-parent-id
4103                 (when (and references
4104                            (not (equal "" references)))
4105                   references))
4106                "none")))
4107          (buffer-read-only nil)
4108          (old (car thread)))
4109     (when thread
4110       (unless iheader
4111         (setcar thread nil)
4112         (when parent
4113           (delq thread parent)))
4114       (if (gnus-summary-insert-subject id header)
4115           ;; Set the (possibly) new article number in the data structure.
4116           (gnus-data-set-number data (gnus-id-to-article id))
4117         (setcar thread old)
4118         nil))))
4119
4120 (defun gnus-rebuild-thread (id &optional line)
4121   "Rebuild the thread containing ID.
4122 If LINE, insert the rebuilt thread starting on line LINE."
4123   (let ((buffer-read-only nil)
4124         old-pos current thread data)
4125     (if (not gnus-show-threads)
4126         (setq thread (list (car (gnus-id-to-thread id))))
4127       ;; Get the thread this article is part of.
4128       (setq thread (gnus-remove-thread id)))
4129     (setq old-pos (gnus-point-at-bol))
4130     (setq current (save-excursion
4131                     (and (re-search-backward "[\r\n]" nil t)
4132                          (gnus-summary-article-number))))
4133     ;; If this is a gathered thread, we have to go some re-gathering.
4134     (when (stringp (car thread))
4135       (let ((subject (car thread))
4136             roots thr)
4137         (setq thread (cdr thread))
4138         (while thread
4139           (unless (memq (setq thr (gnus-id-to-thread
4140                                    (gnus-root-id
4141                                     (mail-header-id (caar thread)))))
4142                         roots)
4143             (push thr roots))
4144           (setq thread (cdr thread)))
4145         ;; We now have all (unique) roots.
4146         (if (= (length roots) 1)
4147             ;; All the loose roots are now one solid root.
4148             (setq thread (car roots))
4149           (setq thread (cons subject (gnus-sort-threads roots))))))
4150     (let (threads)
4151       ;; We then insert this thread into the summary buffer.
4152       (when line
4153         (goto-char (point-min))
4154         (forward-line (1- line)))
4155       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4156         (if gnus-show-threads
4157             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4158           (gnus-summary-prepare-unthreaded thread))
4159         (setq data (nreverse gnus-newsgroup-data))
4160         (setq threads gnus-newsgroup-threads))
4161       ;; We splice the new data into the data structure.
4162       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4163       ;;!!! then we want to insert at the beginning of the buffer.
4164       ;;!!! That happens to be true with Gnus now, but that may
4165       ;;!!! change in the future.  Perhaps.
4166       (gnus-data-enter-list
4167        (if line nil current) data (- (point) old-pos))
4168       (setq gnus-newsgroup-threads
4169             (nconc threads gnus-newsgroup-threads))
4170       (gnus-data-compute-positions))))
4171
4172 (defun gnus-number-to-header (number)
4173   "Return the header for article NUMBER."
4174   (let ((headers gnus-newsgroup-headers))
4175     (while (and headers
4176                 (not (= number (mail-header-number (car headers)))))
4177       (pop headers))
4178     (when headers
4179       (car headers))))
4180
4181 (defun gnus-parent-headers (in-headers &optional generation)
4182   "Return the headers of the GENERATIONeth parent of HEADERS."
4183   (unless generation
4184     (setq generation 1))
4185   (let ((parent t)
4186         (headers in-headers)
4187         references)
4188     (while (and parent
4189                 (not (zerop generation))
4190                 (setq references (mail-header-references headers)))
4191       (setq headers (if (and references
4192                              (setq parent (gnus-parent-id references)))
4193                         (car (gnus-id-to-thread parent))
4194                       nil))
4195       (decf generation))
4196     (and (not (eq headers in-headers))
4197          headers)))
4198
4199 (defun gnus-id-to-thread (id)
4200   "Return the (sub-)thread where ID appears."
4201   (gnus-gethash id gnus-newsgroup-dependencies))
4202
4203 (defun gnus-id-to-article (id)
4204   "Return the article number of ID."
4205   (let ((thread (gnus-id-to-thread id)))
4206     (when (and thread
4207                (car thread))
4208       (mail-header-number (car thread)))))
4209
4210 (defun gnus-id-to-header (id)
4211   "Return the article headers of ID."
4212   (car (gnus-id-to-thread id)))
4213
4214 (defun gnus-article-displayed-root-p (article)
4215   "Say whether ARTICLE is a root(ish) article."
4216   (let ((level (gnus-summary-thread-level article))
4217         (refs (mail-header-references  (gnus-summary-article-header article)))
4218         particle)
4219     (cond
4220      ((null level) nil)
4221      ((zerop level) t)
4222      ((null refs) t)
4223      ((null (gnus-parent-id refs)) t)
4224      ((and (= 1 level)
4225            (null (setq particle (gnus-id-to-article
4226                                  (gnus-parent-id refs))))
4227            (null (gnus-summary-thread-level particle)))))))
4228
4229 (defun gnus-root-id (id)
4230   "Return the id of the root of the thread where ID appears."
4231   (let (last-id prev)
4232     (while (and id (setq prev (car (gnus-id-to-thread id))))
4233       (setq last-id id
4234             id (gnus-parent-id (mail-header-references prev))))
4235     last-id))
4236
4237 (defun gnus-articles-in-thread (thread)
4238   "Return the list of articles in THREAD."
4239   (cons (mail-header-number (car thread))
4240         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4241
4242 (defun gnus-remove-thread (id &optional dont-remove)
4243   "Remove the thread that has ID in it."
4244   (let (headers thread last-id)
4245     ;; First go up in this thread until we find the root.
4246     (setq last-id (gnus-root-id id)
4247           headers (message-flatten-list (gnus-id-to-thread last-id)))
4248     ;; We have now found the real root of this thread.  It might have
4249     ;; been gathered into some loose thread, so we have to search
4250     ;; through the threads to find the thread we wanted.
4251     (let ((threads gnus-newsgroup-threads)
4252           sub)
4253       (while threads
4254         (setq sub (car threads))
4255         (if (stringp (car sub))
4256             ;; This is a gathered thread, so we look at the roots
4257             ;; below it to find whether this article is in this
4258             ;; gathered root.
4259             (progn
4260               (setq sub (cdr sub))
4261               (while sub
4262                 (when (member (caar sub) headers)
4263                   (setq thread (car threads)
4264                         threads nil
4265                         sub nil))
4266                 (setq sub (cdr sub))))
4267           ;; It's an ordinary thread, so we check it.
4268           (when (eq (car sub) (car headers))
4269             (setq thread sub
4270                   threads nil)))
4271         (setq threads (cdr threads)))
4272       ;; If this article is in no thread, then it's a root.
4273       (if thread
4274           (unless dont-remove
4275             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4276         (setq thread (gnus-id-to-thread last-id)))
4277       (when thread
4278         (prog1
4279             thread                      ; We return this thread.
4280           (unless dont-remove
4281             (if (stringp (car thread))
4282                 (progn
4283                   ;; If we use dummy roots, then we have to remove the
4284                   ;; dummy root as well.
4285                   (when (eq gnus-summary-make-false-root 'dummy)
4286                     ;; We go to the dummy root by going to
4287                     ;; the first sub-"thread", and then one line up.
4288                     (gnus-summary-goto-article
4289                      (mail-header-number (caadr thread)))
4290                     (forward-line -1)
4291                     (gnus-delete-line)
4292                     (gnus-data-compute-positions))
4293                   (setq thread (cdr thread))
4294                   (while thread
4295                     (gnus-remove-thread-1 (car thread))
4296                     (setq thread (cdr thread))))
4297               (gnus-remove-thread-1 thread))))))))
4298
4299 (defun gnus-remove-thread-1 (thread)
4300   "Remove the thread THREAD recursively."
4301   (let ((number (mail-header-number (pop thread)))
4302         d)
4303     (setq thread (reverse thread))
4304     (while thread
4305       (gnus-remove-thread-1 (pop thread)))
4306     (when (setq d (gnus-data-find number))
4307       (goto-char (gnus-data-pos d))
4308       (gnus-summary-show-thread)
4309       (gnus-data-remove
4310        number
4311        (- (gnus-point-at-bol)
4312           (prog1
4313               (1+ (gnus-point-at-eol))
4314             (gnus-delete-line)))))))
4315
4316 (defun gnus-sort-threads-1 (threads func)
4317   (sort (mapcar (lambda (thread)
4318                   (cons (car thread)
4319                         (and (cdr thread)
4320                              (gnus-sort-threads-1 (cdr thread) func))))
4321                 threads) func))
4322
4323 (defun gnus-sort-threads (threads)
4324   "Sort THREADS."
4325   (if (not gnus-thread-sort-functions)
4326       threads
4327     (gnus-message 8 "Sorting threads...")
4328     (let ((max-lisp-eval-depth 5000))
4329       (prog1 (gnus-sort-threads-1
4330          threads
4331          (gnus-make-sort-function gnus-thread-sort-functions))
4332         (gnus-message 8 "Sorting threads...done")))))
4333
4334 (defun gnus-sort-articles (articles)
4335   "Sort ARTICLES."
4336   (when gnus-article-sort-functions
4337     (gnus-message 7 "Sorting articles...")
4338     (prog1
4339         (setq gnus-newsgroup-headers
4340               (sort articles (gnus-make-sort-function
4341                               gnus-article-sort-functions)))
4342       (gnus-message 7 "Sorting articles...done"))))
4343
4344 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4345 (defmacro gnus-thread-header (thread)
4346   "Return header of first article in THREAD.
4347 Note that THREAD must never, ever be anything else than a variable -
4348 using some other form will lead to serious barfage."
4349   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4350   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4351   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4352         (vector thread) 2))
4353
4354 (defsubst gnus-article-sort-by-number (h1 h2)
4355   "Sort articles by article number."
4356   (< (mail-header-number h1)
4357      (mail-header-number h2)))
4358
4359 (defun gnus-thread-sort-by-number (h1 h2)
4360   "Sort threads by root article number."
4361   (gnus-article-sort-by-number
4362    (gnus-thread-header h1) (gnus-thread-header h2)))
4363
4364 (defsubst gnus-article-sort-by-random (h1 h2)
4365   "Sort articles by article number."
4366   (zerop (random 2)))
4367
4368 (defun gnus-thread-sort-by-random (h1 h2)
4369   "Sort threads by root article number."
4370   (gnus-article-sort-by-random
4371    (gnus-thread-header h1) (gnus-thread-header h2)))
4372
4373 (defsubst gnus-article-sort-by-lines (h1 h2)
4374   "Sort articles by article Lines header."
4375   (< (mail-header-lines h1)
4376      (mail-header-lines h2)))
4377
4378 (defun gnus-thread-sort-by-lines (h1 h2)
4379   "Sort threads by root article Lines header."
4380   (gnus-article-sort-by-lines
4381    (gnus-thread-header h1) (gnus-thread-header h2)))
4382
4383 (defsubst gnus-article-sort-by-chars (h1 h2)
4384   "Sort articles by octet length."
4385   (< (mail-header-chars h1)
4386      (mail-header-chars h2)))
4387
4388 (defun gnus-thread-sort-by-chars (h1 h2)
4389   "Sort threads by root article octet length."
4390   (gnus-article-sort-by-chars
4391    (gnus-thread-header h1) (gnus-thread-header h2)))
4392
4393 (defsubst gnus-article-sort-by-author (h1 h2)
4394   "Sort articles by root author."
4395   (string-lessp
4396    (let ((extract (funcall
4397                    gnus-extract-address-components
4398                    (mail-header-from h1))))
4399      (or (car extract) (cadr extract) ""))
4400    (let ((extract (funcall
4401                    gnus-extract-address-components
4402                    (mail-header-from h2))))
4403      (or (car extract) (cadr extract) ""))))
4404
4405 (defun gnus-thread-sort-by-author (h1 h2)
4406   "Sort threads by root author."
4407   (gnus-article-sort-by-author
4408    (gnus-thread-header h1)  (gnus-thread-header h2)))
4409
4410 (defsubst gnus-article-sort-by-subject (h1 h2)
4411   "Sort articles by root subject."
4412   (string-lessp
4413    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4414    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4415
4416 (defun gnus-thread-sort-by-subject (h1 h2)
4417   "Sort threads by root subject."
4418   (gnus-article-sort-by-subject
4419    (gnus-thread-header h1) (gnus-thread-header h2)))
4420
4421 (defsubst gnus-article-sort-by-date (h1 h2)
4422   "Sort articles by root article date."
4423   (time-less-p
4424    (gnus-date-get-time (mail-header-date h1))
4425    (gnus-date-get-time (mail-header-date h2))))
4426
4427 (defun gnus-thread-sort-by-date (h1 h2)
4428   "Sort threads by root article date."
4429   (gnus-article-sort-by-date
4430    (gnus-thread-header h1) (gnus-thread-header h2)))
4431
4432 (defsubst gnus-article-sort-by-score (h1 h2)
4433   "Sort articles by root article score.
4434 Unscored articles will be counted as having a score of zero."
4435   (> (or (cdr (assq (mail-header-number h1)
4436                     gnus-newsgroup-scored))
4437          gnus-summary-default-score 0)
4438      (or (cdr (assq (mail-header-number h2)
4439                     gnus-newsgroup-scored))
4440          gnus-summary-default-score 0)))
4441
4442 (defun gnus-thread-sort-by-score (h1 h2)
4443   "Sort threads by root article score."
4444   (gnus-article-sort-by-score
4445    (gnus-thread-header h1) (gnus-thread-header h2)))
4446
4447 (defun gnus-thread-sort-by-total-score (h1 h2)
4448   "Sort threads by the sum of all scores in the thread.
4449 Unscored articles will be counted as having a score of zero."
4450   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4451
4452 (defun gnus-thread-total-score (thread)
4453   ;; This function find the total score of THREAD.
4454   (cond
4455    ((null thread)
4456     0)
4457    ((consp thread)
4458     (if (stringp (car thread))
4459         (apply gnus-thread-score-function 0
4460                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4461       (gnus-thread-total-score-1 thread)))
4462    (t
4463     (gnus-thread-total-score-1 (list thread)))))
4464
4465 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4466   "Sort threads such that the thread with the most recently arrived article comes first."
4467   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4468
4469 (defun gnus-thread-highest-number (thread)
4470   "Return the highest article number in THREAD."
4471   (apply 'max (mapcar (lambda (header)
4472                         (mail-header-number header))
4473                       (message-flatten-list thread))))
4474
4475 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4476   "Sort threads such that the thread with the most recently dated article comes first."
4477   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4478
4479 (defun gnus-thread-latest-date (thread)
4480   "Return the highest article date in THREAD."
4481   (let ((previous-time 0))
4482     (apply 'max
4483            (mapcar
4484             (lambda (header)
4485               (setq previous-time
4486                     (time-to-seconds
4487                      (condition-case ()
4488                          (mail-header-parse-date (mail-header-date header))
4489                        (error previous-time)))))
4490             (sort
4491              (message-flatten-list thread)
4492              (lambda (h1 h2)
4493                (< (mail-header-number h1)
4494                   (mail-header-number h2))))))))
4495
4496 (defun gnus-thread-total-score-1 (root)
4497   ;; This function find the total score of the thread below ROOT.
4498   (setq root (car root))
4499   (apply gnus-thread-score-function
4500          (or (append
4501               (mapcar 'gnus-thread-total-score
4502                       (cdr (gnus-id-to-thread (mail-header-id root))))
4503               (when (> (mail-header-number root) 0)
4504                 (list (or (cdr (assq (mail-header-number root)
4505                                      gnus-newsgroup-scored))
4506                           gnus-summary-default-score 0))))
4507              (list gnus-summary-default-score)
4508              '(0))))
4509
4510 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4511 (defvar gnus-tmp-prev-subject nil)
4512 (defvar gnus-tmp-false-parent nil)
4513 (defvar gnus-tmp-root-expunged nil)
4514 (defvar gnus-tmp-dummy-line nil)
4515
4516 (eval-when-compile (defvar gnus-tmp-header))
4517 (defun gnus-extra-header (type &optional header)
4518   "Return the extra header of TYPE."
4519   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4520       ""))
4521
4522 (defvar gnus-tmp-thread-tree-header-string "")
4523
4524 (defcustom gnus-sum-thread-tree-root "> "
4525   "With %B spec, used for the root of a thread.
4526 If nil, use subject instead."
4527   :type 'string
4528   :group 'gnus-thread)
4529 (defcustom gnus-sum-thread-tree-single-indent ""
4530   "With %B spec, used for a thread with just one message.
4531 If nil, use subject instead."
4532   :type 'string
4533   :group 'gnus-thread)
4534 (defcustom gnus-sum-thread-tree-vertical "| "
4535   "With %B spec, used for drawing a vertical line."
4536   :type 'string
4537   :group 'gnus-thread)
4538 (defcustom gnus-sum-thread-tree-indent "  "
4539   "With %B spec, used for indenting."
4540   :type 'string
4541   :group 'gnus-thread)
4542 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4543   "With %B spec, used for a leaf with brothers."
4544   :type 'string
4545   :group 'gnus-thread)
4546 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4547   "With %B spec, used for a leaf without brothers."
4548   :type 'string
4549   :group 'gnus-thread)
4550
4551 (defun gnus-summary-prepare-threads (threads)
4552   "Prepare summary buffer from THREADS and indentation LEVEL.
4553 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4554 or a straight list of headers."
4555   (gnus-message 7 "Generating summary...")
4556
4557   (setq gnus-newsgroup-threads threads)
4558   (beginning-of-line)
4559
4560   (let ((gnus-tmp-level 0)
4561         (default-score (or gnus-summary-default-score 0))
4562         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4563         (building-line-count gnus-summary-display-while-building)
4564         (building-count (integerp gnus-summary-display-while-building))
4565         thread number subject stack state gnus-tmp-gathered beg-match
4566         new-roots gnus-tmp-new-adopts thread-end simp-subject
4567         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4568         gnus-tmp-replied gnus-tmp-subject-or-nil
4569         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4570         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4571         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4572         tree-stack)
4573
4574     (setq gnus-tmp-prev-subject nil
4575           gnus-tmp-thread-tree-header-string "")
4576
4577     (if (vectorp (car threads))
4578         ;; If this is a straight (sic) list of headers, then a
4579         ;; threaded summary display isn't required, so we just create
4580         ;; an unthreaded one.
4581         (gnus-summary-prepare-unthreaded threads)
4582
4583       ;; Do the threaded display.
4584
4585       (if gnus-summary-display-while-building
4586           (switch-to-buffer (buffer-name)))
4587       (while (or threads stack gnus-tmp-new-adopts new-roots)
4588
4589         (if (and (= gnus-tmp-level 0)
4590                  (or (not stack)
4591                      (= (caar stack) 0))
4592                  (not gnus-tmp-false-parent)
4593                  (or gnus-tmp-new-adopts new-roots))
4594             (if gnus-tmp-new-adopts
4595                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4596                       thread (list (car gnus-tmp-new-adopts))
4597                       gnus-tmp-header (caar thread)
4598                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4599               (when new-roots
4600                 (setq thread (list (car new-roots))
4601                       gnus-tmp-header (caar thread)
4602                       new-roots (cdr new-roots))))
4603
4604           (if threads
4605               ;; If there are some threads, we do them before the
4606               ;; threads on the stack.
4607               (setq thread threads
4608                     gnus-tmp-header (caar thread))
4609             ;; There were no current threads, so we pop something off
4610             ;; the stack.
4611             (setq state (car stack)
4612                   gnus-tmp-level (car state)
4613                   tree-stack (cadr state)
4614                   thread (caddr state)
4615                   stack (cdr stack)
4616                   gnus-tmp-header (caar thread))))
4617
4618         (setq gnus-tmp-false-parent nil)
4619         (setq gnus-tmp-root-expunged nil)
4620         (setq thread-end nil)
4621
4622         (if (stringp gnus-tmp-header)
4623             ;; The header is a dummy root.
4624             (cond
4625              ((eq gnus-summary-make-false-root 'adopt)
4626               ;; We let the first article adopt the rest.
4627               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4628                                                (cddar thread)))
4629               (setq gnus-tmp-gathered
4630                     (nconc (mapcar
4631                             (lambda (h) (mail-header-number (car h)))
4632                             (cddar thread))
4633                            gnus-tmp-gathered))
4634               (setq thread (cons (list (caar thread)
4635                                        (cadar thread))
4636                                  (cdr thread)))
4637               (setq gnus-tmp-level -1
4638                     gnus-tmp-false-parent t))
4639              ((eq gnus-summary-make-false-root 'empty)
4640               ;; We print adopted articles with empty subject fields.
4641               (setq gnus-tmp-gathered
4642                     (nconc (mapcar
4643                             (lambda (h) (mail-header-number (car h)))
4644                             (cddar thread))
4645                            gnus-tmp-gathered))
4646               (setq gnus-tmp-level -1))
4647              ((eq gnus-summary-make-false-root 'dummy)
4648               ;; We remember that we probably want to output a dummy
4649               ;; root.
4650               (setq gnus-tmp-dummy-line gnus-tmp-header)
4651               (setq gnus-tmp-prev-subject gnus-tmp-header))
4652              (t
4653               ;; We do not make a root for the gathered
4654               ;; sub-threads at all.
4655               (setq gnus-tmp-level -1)))
4656
4657           (setq number (mail-header-number gnus-tmp-header)
4658                 subject (mail-header-subject gnus-tmp-header)
4659                 simp-subject (gnus-simplify-subject-fully subject))
4660
4661           (cond
4662            ;; If the thread has changed subject, we might want to make
4663            ;; this subthread into a root.
4664            ((and (null gnus-thread-ignore-subject)
4665                  (not (zerop gnus-tmp-level))
4666                  gnus-tmp-prev-subject
4667                  (not (string= gnus-tmp-prev-subject simp-subject)))
4668             (setq new-roots (nconc new-roots (list (car thread)))
4669                   thread-end t
4670                   gnus-tmp-header nil))
4671            ;; If the article lies outside the current limit,
4672            ;; then we do not display it.
4673            ((not (memq number gnus-newsgroup-limit))
4674             (setq gnus-tmp-gathered
4675                   (nconc (mapcar
4676                           (lambda (h) (mail-header-number (car h)))
4677                           (cdar thread))
4678                          gnus-tmp-gathered))
4679             (setq gnus-tmp-new-adopts (if (cdar thread)
4680                                           (append gnus-tmp-new-adopts
4681                                                   (cdar thread))
4682                                         gnus-tmp-new-adopts)
4683                   thread-end t
4684                   gnus-tmp-header nil)
4685             (when (zerop gnus-tmp-level)
4686               (setq gnus-tmp-root-expunged t)))
4687            ;; Perhaps this article is to be marked as read?
4688            ((and gnus-summary-mark-below
4689                  (< (or (cdr (assq number gnus-newsgroup-scored))
4690                         default-score)
4691                     gnus-summary-mark-below)
4692                  ;; Don't touch sparse articles.
4693                  (not (gnus-summary-article-sparse-p number))
4694                  (not (gnus-summary-article-ancient-p number)))
4695             (setq gnus-newsgroup-unreads
4696                   (delq number gnus-newsgroup-unreads))
4697             (if gnus-newsgroup-auto-expire
4698                 (setq gnus-newsgroup-expirable
4699                       (gnus-add-to-sorted-list
4700                        gnus-newsgroup-expirable number))
4701               (push (cons number gnus-low-score-mark)
4702                     gnus-newsgroup-reads))))
4703
4704           (when gnus-tmp-header
4705             ;; We may have an old dummy line to output before this
4706             ;; article.
4707             (when (and gnus-tmp-dummy-line
4708                        (gnus-subject-equal
4709                         gnus-tmp-dummy-line
4710                         (mail-header-subject gnus-tmp-header)))
4711               (gnus-summary-insert-dummy-line
4712                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4713               (setq gnus-tmp-dummy-line nil))
4714
4715             ;; Compute the mark.
4716             (setq gnus-tmp-unread (gnus-article-mark number))
4717
4718             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4719                                   gnus-tmp-header gnus-tmp-level)
4720                   gnus-newsgroup-data)
4721
4722             ;; Actually insert the line.
4723             (setq
4724              gnus-tmp-subject-or-nil
4725              (cond
4726               ((and gnus-thread-ignore-subject
4727                     gnus-tmp-prev-subject
4728                     (not (string= gnus-tmp-prev-subject simp-subject)))
4729                subject)
4730               ((zerop gnus-tmp-level)
4731                (if (and (eq gnus-summary-make-false-root 'empty)
4732                         (memq number gnus-tmp-gathered)
4733                         gnus-tmp-prev-subject
4734                         (string= gnus-tmp-prev-subject simp-subject))
4735                    gnus-summary-same-subject
4736                  subject))
4737               (t gnus-summary-same-subject)))
4738             (if (and (eq gnus-summary-make-false-root 'adopt)
4739                      (= gnus-tmp-level 1)
4740                      (memq number gnus-tmp-gathered))
4741                 (setq gnus-tmp-opening-bracket ?\<
4742                       gnus-tmp-closing-bracket ?\>)
4743               (setq gnus-tmp-opening-bracket ?\[
4744                     gnus-tmp-closing-bracket ?\]))
4745             (setq
4746              gnus-tmp-indentation
4747              (aref gnus-thread-indent-array gnus-tmp-level)
4748              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4749              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4750                                 gnus-summary-default-score 0)
4751              gnus-tmp-score-char
4752              (if (or (null gnus-summary-default-score)
4753                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4754                          gnus-summary-zcore-fuzz))
4755                  ?                      ;Whitespace
4756                (if (< gnus-tmp-score gnus-summary-default-score)
4757                    gnus-score-below-mark gnus-score-over-mark))
4758              gnus-tmp-replied
4759              (cond ((memq number gnus-newsgroup-processable)
4760                     gnus-process-mark)
4761                    ((memq number gnus-newsgroup-cached)
4762                     gnus-cached-mark)
4763                    ((memq number gnus-newsgroup-replied)
4764                     gnus-replied-mark)
4765                    ((memq number gnus-newsgroup-forwarded)
4766                     gnus-forwarded-mark)
4767                    ((memq number gnus-newsgroup-saved)
4768                     gnus-saved-mark)
4769                    ((memq number gnus-newsgroup-recent)
4770                     gnus-recent-mark)
4771                    ((memq number gnus-newsgroup-unseen)
4772                     gnus-unseen-mark)
4773                    (t gnus-no-mark))
4774              gnus-tmp-downloaded
4775              (cond ((memq number gnus-newsgroup-undownloaded) 
4776                     gnus-undownloaded-mark)
4777                    (gnus-newsgroup-agentized
4778                     gnus-downloaded-mark)
4779                    (t
4780                     gnus-no-mark))
4781              gnus-tmp-from (mail-header-from gnus-tmp-header)
4782              gnus-tmp-name
4783              (cond
4784               ((string-match "<[^>]+> *$" gnus-tmp-from)
4785                (setq beg-match (match-beginning 0))
4786                (or (and (string-match "^\".+\"" gnus-tmp-from)
4787                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4788                    (substring gnus-tmp-from 0 beg-match)))
4789               ((string-match "(.+)" gnus-tmp-from)
4790                (substring gnus-tmp-from
4791                           (1+ (match-beginning 0)) (1- (match-end 0))))
4792               (t gnus-tmp-from))
4793              gnus-tmp-thread-tree-header-string
4794              (cond
4795               ((not gnus-show-threads) "")
4796               ((zerop gnus-tmp-level)
4797                (if (cdar thread)
4798                    (or gnus-sum-thread-tree-root subject)
4799                  (or gnus-sum-thread-tree-single-indent subject)))
4800               (t
4801                (concat (apply 'concat
4802                               (mapcar (lambda (item)
4803                                         (if (= item 1)
4804                                             gnus-sum-thread-tree-vertical
4805                                           gnus-sum-thread-tree-indent))
4806                                       (cdr (reverse tree-stack))))
4807                        (if (nth 1 thread)
4808                            gnus-sum-thread-tree-leaf-with-other
4809                          gnus-sum-thread-tree-single-leaf)))))
4810             (when (string= gnus-tmp-name "")
4811               (setq gnus-tmp-name gnus-tmp-from))
4812             (unless (numberp gnus-tmp-lines)
4813               (setq gnus-tmp-lines -1))
4814             (if (= gnus-tmp-lines -1)
4815                 (setq gnus-tmp-lines "?")
4816               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4817               (gnus-put-text-property
4818              (point)
4819              (progn (eval gnus-summary-line-format-spec) (point))
4820                'gnus-number number)
4821             (when gnus-visual-p
4822               (forward-line -1)
4823               (gnus-run-hooks 'gnus-summary-update-hook)
4824               (forward-line 1))
4825
4826             (setq gnus-tmp-prev-subject simp-subject)))
4827
4828         (when (nth 1 thread)
4829           (push (list (max 0 gnus-tmp-level)
4830                       (copy-list tree-stack)
4831                       (nthcdr 1 thread))
4832                 stack))
4833         (push (if (nth 1 thread) 1 0) tree-stack)
4834         (incf gnus-tmp-level)
4835         (setq threads (if thread-end nil (cdar thread)))
4836         (if gnus-summary-display-while-building
4837             (if building-count
4838                 (progn
4839                   ;; use a set frequency
4840                   (setq building-line-count (1- building-line-count))
4841                   (when (= building-line-count 0)
4842                     (sit-for 0)
4843                     (setq building-line-count
4844                           gnus-summary-display-while-building)))
4845               ;; always
4846               (sit-for 0)))
4847         (unless threads
4848           (setq gnus-tmp-level 0)))))
4849   (gnus-message 7 "Generating summary...done"))
4850
4851 (defun gnus-summary-prepare-unthreaded (headers)
4852   "Generate an unthreaded summary buffer based on HEADERS."
4853   (let (header number mark)
4854
4855     (beginning-of-line)
4856
4857     (while headers
4858       ;; We may have to root out some bad articles...
4859       (when (memq (setq number (mail-header-number
4860                                 (setq header (pop headers))))
4861                   gnus-newsgroup-limit)
4862         ;; Mark article as read when it has a low score.
4863         (when (and gnus-summary-mark-below
4864                    (< (or (cdr (assq number gnus-newsgroup-scored))
4865                           gnus-summary-default-score 0)
4866                       gnus-summary-mark-below)
4867                    (not (gnus-summary-article-ancient-p number)))
4868           (setq gnus-newsgroup-unreads
4869                 (delq number gnus-newsgroup-unreads))
4870           (if gnus-newsgroup-auto-expire
4871               (push number gnus-newsgroup-expirable)
4872             (push (cons number gnus-low-score-mark)
4873                   gnus-newsgroup-reads)))
4874
4875         (setq mark (gnus-article-mark number))
4876         (push (gnus-data-make number mark (1+ (point)) header 0)
4877               gnus-newsgroup-data)
4878         (gnus-summary-insert-line
4879          header 0 number
4880          (memq number gnus-newsgroup-undownloaded)
4881          mark (memq number gnus-newsgroup-replied)
4882          (memq number gnus-newsgroup-expirable)
4883          (mail-header-subject header) nil
4884          (cdr (assq number gnus-newsgroup-scored))
4885          (memq number gnus-newsgroup-processable))))))
4886
4887 (defun gnus-summary-remove-list-identifiers ()
4888   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4889   (let ((regexp (if (consp gnus-list-identifiers)
4890                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4891                   gnus-list-identifiers))
4892         changed subject)
4893     (when regexp
4894       (dolist (header gnus-newsgroup-headers)
4895         (setq subject (mail-header-subject header)
4896               changed nil)
4897         (while (string-match
4898                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4899                 subject)
4900           (setq subject
4901                 (concat (substring subject 0 (match-beginning 2))
4902                         (substring subject (match-end 0)))
4903                 changed t))
4904         (when (and changed
4905                    (string-match
4906                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4907           (setq subject
4908                 (concat (substring subject 0 (match-beginning 1))
4909                         (substring subject (match-end 1)))))
4910         (when changed
4911           (mail-header-set-subject header subject))))))
4912
4913 (defun gnus-fetch-headers (articles)
4914   "Fetch headers of ARTICLES."
4915   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4916     (gnus-message 5 "Fetching headers for %s..." name)
4917     (prog1
4918         (if (eq 'nov
4919                 (setq gnus-headers-retrieved-by
4920                       (gnus-retrieve-headers
4921                        articles gnus-newsgroup-name
4922                        ;; We might want to fetch old headers, but
4923                        ;; not if there is only 1 article.
4924                        (and (or (and
4925                                  (not (eq gnus-fetch-old-headers 'some))
4926                                  (not (numberp gnus-fetch-old-headers)))
4927                                 (> (length articles) 1))
4928                             gnus-fetch-old-headers))))
4929             (gnus-get-newsgroup-headers-xover
4930              articles nil nil gnus-newsgroup-name t)
4931           (gnus-get-newsgroup-headers))
4932       (gnus-message 5 "Fetching headers for %s...done" name))))
4933
4934 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4935   "Select newsgroup GROUP.
4936 If READ-ALL is non-nil, all articles in the group are selected.
4937 If SELECT-ARTICLES, only select those articles from GROUP."
4938   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4939          ;;!!! Dirty hack; should be removed.
4940          (gnus-summary-ignore-duplicates
4941           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4942               t
4943             gnus-summary-ignore-duplicates))
4944          (info (nth 2 entry))
4945          articles fetched-articles cached)
4946
4947     (unless (gnus-check-server
4948              (set (make-local-variable 'gnus-current-select-method)
4949                   (gnus-find-method-for-group group)))
4950       (error "Couldn't open server"))
4951
4952     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4953         (gnus-activate-group group)     ; Or we can activate it...
4954         (progn                          ; Or we bug out.
4955           (when (equal major-mode 'gnus-summary-mode)
4956             (gnus-kill-buffer (current-buffer)))
4957           (error "Couldn't activate group %s: %s"
4958                  group (gnus-status-message group))))
4959
4960     (unless (gnus-request-group group t)
4961       (when (equal major-mode 'gnus-summary-mode)
4962         (gnus-kill-buffer (current-buffer)))
4963       (error "Couldn't request group %s: %s"
4964              group (gnus-status-message group)))
4965
4966     (setq gnus-newsgroup-name group
4967           gnus-newsgroup-unselected nil
4968           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4969
4970     (let ((display (gnus-group-find-parameter group 'display)))
4971       (setq gnus-newsgroup-display
4972             (cond
4973              ((not (zerop (or (car-safe read-all) 0)))
4974               ;; The user entered the group with C-u SPC/RET, let's show
4975               ;; all articles.
4976               'gnus-not-ignore)
4977              ((eq display 'all)
4978               'gnus-not-ignore)
4979              ((arrayp display)
4980               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4981              ((numberp display)
4982               ;; The following is probably the "correct" solution, but
4983               ;; it makes Gnus fetch all headers and then limit the
4984               ;; articles (which is slow), so instead we hack the
4985               ;; select-articles parameter instead. -- Simon Josefsson
4986               ;; <jas@kth.se>
4987               ;;
4988               ;; (gnus-byte-compile
4989               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4990               ;;                         display)))))
4991               (setq select-articles
4992                     (gnus-uncompress-range
4993                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4994                              (if (> tmp 0)
4995                                  tmp
4996                                1))
4997                            (cdr (gnus-active group)))))
4998               nil)
4999              (t
5000               nil))))
5001
5002     (gnus-summary-setup-default-charset)
5003
5004     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5005     (when (gnus-virtual-group-p group)
5006       (setq cached gnus-newsgroup-cached))
5007
5008     (setq gnus-newsgroup-unreads
5009           (gnus-sorted-ndifference
5010            (gnus-sorted-ndifference gnus-newsgroup-unreads
5011                                     gnus-newsgroup-marked)
5012            gnus-newsgroup-dormant))
5013
5014     (setq gnus-newsgroup-processable nil)
5015
5016     (gnus-update-read-articles group gnus-newsgroup-unreads)
5017
5018     ;; Adjust and set lists of article marks.
5019     (when info
5020       (gnus-adjust-marked-articles info))
5021     (if (setq articles select-articles)
5022         (setq gnus-newsgroup-unselected
5023               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5024       (setq articles (gnus-articles-to-read group read-all)))
5025
5026     (cond
5027      ((null articles)
5028       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5029       'quit)
5030      ((eq articles 0) nil)
5031      (t
5032       ;; Init the dependencies hash table.
5033       (setq gnus-newsgroup-dependencies
5034             (gnus-make-hashtable (length articles)))
5035       (gnus-set-global-variables)
5036       ;; Retrieve the headers and read them in.
5037
5038       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5039
5040       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5041       (when cached
5042         (setq gnus-newsgroup-cached cached))
5043
5044       ;; Suppress duplicates?
5045       (when gnus-suppress-duplicates
5046         (gnus-dup-suppress-articles))
5047
5048       ;; Set the initial limit.
5049       (setq gnus-newsgroup-limit (copy-sequence articles))
5050       ;; Remove canceled articles from the list of unread articles.
5051       (setq fetched-articles
5052             (mapcar (lambda (headers) (mail-header-number headers))
5053                     gnus-newsgroup-headers))
5054       (setq gnus-newsgroup-articles fetched-articles)
5055       (setq gnus-newsgroup-unreads
5056             (gnus-sorted-nintersection
5057              gnus-newsgroup-unreads fetched-articles))
5058       (gnus-compute-unseen-list)
5059
5060       ;; Removed marked articles that do not exist.
5061       (gnus-update-missing-marks
5062        (gnus-sorted-difference articles fetched-articles))
5063       ;; We might want to build some more threads first.
5064       (when (and gnus-fetch-old-headers
5065                  (eq gnus-headers-retrieved-by 'nov))
5066         (if (eq gnus-fetch-old-headers 'invisible)
5067             (gnus-build-all-threads)
5068           (gnus-build-old-threads)))
5069       ;; Let the Gnus agent mark articles as read.
5070       (when gnus-agent
5071         (gnus-agent-get-undownloaded-list))
5072       ;; Remove list identifiers from subject
5073       (when gnus-list-identifiers
5074         (gnus-summary-remove-list-identifiers))
5075       ;; Check whether auto-expire is to be done in this group.
5076       (setq gnus-newsgroup-auto-expire
5077             (gnus-group-auto-expirable-p group))
5078       ;; Set up the article buffer now, if necessary.
5079       (unless gnus-single-article-buffer
5080         (gnus-article-setup-buffer))
5081       ;; First and last article in this newsgroup.
5082       (when gnus-newsgroup-headers
5083         (setq gnus-newsgroup-begin
5084               (mail-header-number (car gnus-newsgroup-headers))
5085               gnus-newsgroup-end
5086               (mail-header-number
5087                (gnus-last-element gnus-newsgroup-headers))))
5088       ;; GROUP is successfully selected.
5089       (or gnus-newsgroup-headers t)))))
5090
5091 (defun gnus-compute-unseen-list ()
5092   ;; The `seen' marks are treated specially.
5093   (if (not gnus-newsgroup-seen)
5094       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5095     (setq gnus-newsgroup-unseen
5096           (gnus-inverse-list-range-intersection
5097            gnus-newsgroup-articles gnus-newsgroup-seen))))
5098
5099 (defun gnus-summary-display-make-predicate (display)
5100   (require 'gnus-agent)
5101   (when (= (length display) 1)
5102     (setq display (car display)))
5103   (unless gnus-summary-display-cache
5104     (dolist (elem (append '((unread . unread)
5105                             (read . read)
5106                             (unseen . unseen))
5107                           gnus-article-mark-lists))
5108       (push (cons (cdr elem)
5109                   (gnus-byte-compile
5110                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5111             gnus-summary-display-cache)))
5112   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5113         (gnus-category-predicate-cache gnus-summary-display-cache))
5114     (gnus-get-predicate display)))
5115
5116 ;; Uses the dynamically bound `number' variable.
5117 (eval-when-compile
5118   (defvar number))
5119 (defun gnus-article-marked-p (type &optional article)
5120   (let ((article (or article number)))
5121     (cond
5122      ((eq type 'tick)
5123       (memq article gnus-newsgroup-marked))
5124      ((eq type 'spam)
5125       (memq article gnus-newsgroup-spam-marked))
5126      ((eq type 'unsend)
5127       (memq article gnus-newsgroup-unsendable))
5128      ((eq type 'undownload)
5129       (memq article gnus-newsgroup-undownloaded))
5130      ((eq type 'download)
5131       (memq article gnus-newsgroup-downloadable))
5132      ((eq type 'unread)
5133       (memq article gnus-newsgroup-unreads))
5134      ((eq type 'read)
5135       (memq article gnus-newsgroup-reads))
5136      ((eq type 'dormant)
5137       (memq article gnus-newsgroup-dormant) )
5138      ((eq type 'expire)
5139       (memq article gnus-newsgroup-expirable))
5140      ((eq type 'reply)
5141       (memq article gnus-newsgroup-replied))
5142      ((eq type 'killed)
5143       (memq article gnus-newsgroup-killed))
5144      ((eq type 'bookmark)
5145       (assq article gnus-newsgroup-bookmarks))
5146      ((eq type 'score)
5147       (assq article gnus-newsgroup-scored))
5148      ((eq type 'save)
5149       (memq article gnus-newsgroup-saved))
5150      ((eq type 'cache)
5151       (memq article gnus-newsgroup-cached))
5152      ((eq type 'forward)
5153       (memq article gnus-newsgroup-forwarded))
5154      ((eq type 'seen)
5155       (not (memq article gnus-newsgroup-unseen)))
5156      ((eq type 'recent)
5157       (memq article gnus-newsgroup-recent))
5158      (t t))))
5159
5160 (defun gnus-articles-to-read (group &optional read-all)
5161   "Find out what articles the user wants to read."
5162   (let* ((display (gnus-group-find-parameter group 'display))
5163          (articles
5164           ;; Select all articles if `read-all' is non-nil, or if there
5165           ;; are no unread articles.
5166           (if (or read-all
5167                   (and (zerop (length gnus-newsgroup-marked))
5168                        (zerop (length gnus-newsgroup-unreads)))
5169                   ;; Fetch all if the predicate is non-nil.
5170                   gnus-newsgroup-display)
5171               ;; We want to select the headers for all the articles in
5172               ;; the group, so we select either all the active
5173               ;; articles in the group, or (if that's nil), the
5174               ;; articles in the cache.
5175               (or
5176                (gnus-uncompress-range (gnus-active group))
5177                (gnus-cache-articles-in-group group))
5178             ;; Select only the "normal" subset of articles.
5179             (gnus-sorted-nunion
5180              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5181              gnus-newsgroup-unreads)))
5182          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5183          (scored (length scored-list))
5184          (number (length articles))
5185          (marked (+ (length gnus-newsgroup-marked)
5186                     (length gnus-newsgroup-dormant)))
5187          (select
5188           (cond
5189            ((numberp read-all)
5190             read-all)
5191            ((numberp gnus-newsgroup-display)
5192             gnus-newsgroup-display)
5193            (t
5194             (condition-case ()
5195                 (cond
5196                  ((and (or (<= scored marked) (= scored number))
5197                        (numberp gnus-large-newsgroup)
5198                        (> number gnus-large-newsgroup))
5199                   (let* ((cursor-in-echo-area nil)
5200                          (initial (gnus-parameter-large-newsgroup-initial
5201                                    gnus-newsgroup-name))
5202                          (input
5203                           (read-string
5204                            (format
5205                             "How many articles from %s (%s %d): "
5206                             (gnus-limit-string
5207                              (gnus-group-decoded-name gnus-newsgroup-name)
5208                              35)
5209                             (if initial "max" "default")
5210                             number)
5211                            (if initial
5212                                (cons (number-to-string initial)
5213                                      0)))))
5214                     (if (string-match "^[ \t]*$" input) number input)))
5215                  ((and (> scored marked) (< scored number)
5216                        (> (- scored number) 20))
5217                   (let ((input
5218                          (read-string
5219                           (format "%s %s (%d scored, %d total): "
5220                                   "How many articles from"
5221                                   (gnus-group-decoded-name group)
5222                                   scored number))))
5223                     (if (string-match "^[ \t]*$" input)
5224                         number input)))
5225                  (t number))
5226               (quit
5227                (message "Quit getting the articles to read")
5228                nil))))))
5229     (setq select (if (stringp select) (string-to-number select) select))
5230     (if (or (null select) (zerop select))
5231         select
5232       (if (and (not (zerop scored)) (<= (abs select) scored))
5233           (progn
5234             (setq articles (sort scored-list '<))
5235             (setq number (length articles)))
5236         (setq articles (copy-sequence articles)))
5237
5238       (when (< (abs select) number)
5239         (if (< select 0)
5240             ;; Select the N oldest articles.
5241             (setcdr (nthcdr (1- (abs select)) articles) nil)
5242           ;; Select the N most recent articles.
5243           (setq articles (nthcdr (- number select) articles))))
5244       (setq gnus-newsgroup-unselected
5245             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5246       (when gnus-alter-articles-to-read-function
5247         (setq articles
5248               (sort
5249                (funcall gnus-alter-articles-to-read-function
5250                         gnus-newsgroup-name articles)
5251                '<)))
5252       articles)))
5253
5254 (defun gnus-killed-articles (killed articles)
5255   (let (out)
5256     (while articles
5257       (when (inline (gnus-member-of-range (car articles) killed))
5258         (push (car articles) out))
5259       (setq articles (cdr articles)))
5260     out))
5261
5262 (defun gnus-uncompress-marks (marks)
5263   "Uncompress the mark ranges in MARKS."
5264   (let ((uncompressed '(score bookmark))
5265         out)
5266     (while marks
5267       (if (memq (caar marks) uncompressed)
5268           (push (car marks) out)
5269         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5270       (setq marks (cdr marks)))
5271     out))
5272
5273 (defun gnus-article-mark-to-type (mark)
5274   "Return the type of MARK."
5275   (or (cadr (assq mark gnus-article-special-mark-lists))
5276       'list))
5277
5278 (defun gnus-article-unpropagatable-p (mark)
5279   "Return whether MARK should be propagated to backend."
5280   (memq mark gnus-article-unpropagated-mark-lists))
5281
5282 (defun gnus-adjust-marked-articles (info)
5283   "Set all article lists and remove all marks that are no longer valid."
5284   (let* ((marked-lists (gnus-info-marks info))
5285          (active (gnus-active (gnus-info-group info)))
5286          (min (car active))
5287          (max (cdr active))
5288          (types gnus-article-mark-lists)
5289          marks var articles article mark mark-type)
5290
5291     (dolist (marks marked-lists)
5292       (setq mark (car marks)
5293             mark-type (gnus-article-mark-to-type mark)
5294             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5295
5296       ;; We set the variable according to the type of the marks list,
5297       ;; and then adjust the marks to a subset of the active articles.
5298       (cond
5299        ;; Adjust "simple" lists.
5300        ((eq mark-type 'list)
5301         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5302         (when (memq mark '(tick dormant expire reply save))
5303           (while articles
5304             (when (or (< (setq article (pop articles)) min) (> article max))
5305               (set var (delq article (symbol-value var)))))))
5306        ;; Adjust assocs.
5307        ((eq mark-type 'tuple)
5308         (set var (setq articles (cdr marks)))
5309         (when (not (listp (cdr (symbol-value var))))
5310           (set var (list (symbol-value var))))
5311         (when (not (listp (cdr articles)))
5312           (setq articles (list articles)))
5313         (while articles
5314           (when (or (not (consp (setq article (pop articles))))
5315                     (< (car article) min)
5316                     (> (car article) max))
5317             (set var (delq article (symbol-value var))))))
5318        ;; Adjust ranges (sloppily).
5319        ((eq mark-type 'range)
5320         (cond
5321          ((eq mark 'seen)
5322           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5323           ;; It should be (seen (NUM1 . NUM2)).
5324           (when (numberp (cddr marks))
5325             (setcdr marks (list (cdr marks))))
5326           (setq articles (cdr marks))
5327           (while (and articles
5328                       (or (and (consp (car articles))
5329                                (> min (cdar articles)))
5330                           (and (numberp (car articles))
5331                                (> min (car articles)))))
5332             (pop articles))
5333           (set var articles))))))))
5334
5335 (defun gnus-update-missing-marks (missing)
5336   "Go through the list of MISSING articles and remove them from the mark lists."
5337   (when missing
5338     (let (var m)
5339       ;; Go through all types.
5340       (dolist (elem gnus-article-mark-lists)
5341         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5342           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5343           (when (symbol-value var)
5344             ;; This list has articles.  So we delete all missing
5345             ;; articles from it.
5346             (setq m missing)
5347             (while m
5348               (set var (delq (pop m) (symbol-value var))))))))))
5349
5350 (defun gnus-update-marks ()
5351   "Enter the various lists of marked articles into the newsgroup info list."
5352   (let ((types gnus-article-mark-lists)
5353         (info (gnus-get-info gnus-newsgroup-name))
5354         type list newmarked symbol delta-marks)
5355     (when info
5356       ;; Add all marks lists to the list of marks lists.
5357       (while (setq type (pop types))
5358         (setq list (symbol-value
5359                     (setq symbol
5360                           (intern (format "gnus-newsgroup-%s" (car type))))))
5361
5362         (when list
5363           ;; Get rid of the entries of the articles that have the
5364           ;; default score.
5365           (when (and (eq (cdr type) 'score)
5366                      gnus-save-score
5367                      list)
5368             (let* ((arts list)
5369                    (prev (cons nil list))
5370                    (all prev))
5371               (while arts
5372                 (if (or (not (consp (car arts)))
5373                         (= (cdar arts) gnus-summary-default-score))
5374                     (setcdr prev (cdr arts))
5375                   (setq prev arts))
5376                 (setq arts (cdr arts)))
5377               (setq list (cdr all)))))
5378
5379         (when (eq (cdr type) 'seen)
5380           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5381
5382         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5383           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5384
5385         (when (and (gnus-check-backend-function
5386                     'request-set-mark gnus-newsgroup-name)
5387                    (not (gnus-article-unpropagatable-p (cdr type))))
5388           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5389                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5390                  (add (gnus-remove-from-range
5391                        (gnus-copy-sequence list) old)))
5392             (when add
5393               (push (list add 'add (list (cdr type))) delta-marks))
5394             (when del
5395               (push (list del 'del (list (cdr type))) delta-marks))))
5396
5397         (when list
5398           (push (cons (cdr type) list) newmarked)))
5399
5400       (when delta-marks
5401         (unless (gnus-check-group gnus-newsgroup-name)
5402           (error "Can't open server for %s" gnus-newsgroup-name))
5403         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5404
5405       ;; Enter these new marks into the info of the group.
5406       (if (nthcdr 3 info)
5407           (setcar (nthcdr 3 info) newmarked)
5408         ;; Add the marks lists to the end of the info.
5409         (when newmarked
5410           (setcdr (nthcdr 2 info) (list newmarked))))
5411
5412       ;; Cut off the end of the info if there's nothing else there.
5413       (let ((i 5))
5414         (while (and (> i 2)
5415                     (not (nth i info)))
5416           (when (nthcdr (decf i) info)
5417             (setcdr (nthcdr i info) nil)))))))
5418
5419 (defun gnus-set-mode-line (where)
5420   "Set the mode line of the article or summary buffers.
5421 If WHERE is `summary', the summary mode line format will be used."
5422   ;; Is this mode line one we keep updated?
5423   (when (and (memq where gnus-updated-mode-lines)
5424              (symbol-value
5425               (intern (format "gnus-%s-mode-line-format-spec" where))))
5426     (let (mode-string)
5427       (save-excursion
5428         ;; We evaluate this in the summary buffer since these
5429         ;; variables are buffer-local to that buffer.
5430         (set-buffer gnus-summary-buffer)
5431        ;; We bind all these variables that are used in the `eval' form
5432         ;; below.
5433         (let* ((mformat (symbol-value
5434                          (intern
5435                           (format "gnus-%s-mode-line-format-spec" where))))
5436                (gnus-tmp-group-name (gnus-group-decoded-name
5437                                      gnus-newsgroup-name))
5438                (gnus-tmp-article-number (or gnus-current-article 0))
5439                (gnus-tmp-unread gnus-newsgroup-unreads)
5440                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5441                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5442                (gnus-tmp-unread-and-unselected
5443                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5444                             (zerop gnus-tmp-unselected))
5445                        "")
5446                       ((zerop gnus-tmp-unselected)
5447                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5448                       (t (format "{%d(+%d) more}"
5449                                  gnus-tmp-unread-and-unticked
5450                                  gnus-tmp-unselected))))
5451                (gnus-tmp-subject
5452                 (if (and gnus-current-headers
5453                          (vectorp gnus-current-headers))
5454                     (gnus-mode-string-quote
5455                      (mail-header-subject gnus-current-headers))
5456                   ""))
5457                bufname-length max-len
5458                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5459           (setq mode-string (eval mformat))
5460           (setq bufname-length (if (string-match "%b" mode-string)
5461                                    (- (length
5462                                        (buffer-name
5463                                         (if (eq where 'summary)
5464                                             nil
5465                                           (get-buffer gnus-article-buffer))))
5466                                       2)
5467                                  0))
5468           (setq max-len (max 4 (if gnus-mode-non-string-length
5469                                    (- (window-width)
5470                                       gnus-mode-non-string-length
5471                                       bufname-length)
5472                                  (length mode-string))))
5473           ;; We might have to chop a bit of the string off...
5474           (when (> (length mode-string) max-len)
5475             (setq mode-string
5476                   (concat (truncate-string-to-width mode-string (- max-len 3))
5477                           "...")))
5478           ;; Pad the mode string a bit.
5479           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5480       ;; Update the mode line.
5481       (setq mode-line-buffer-identification
5482             (gnus-mode-line-buffer-identification (list mode-string)))
5483       (set-buffer-modified-p t))))
5484
5485 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5486   "Go through the HEADERS list and add all Xrefs to a hash table.
5487 The resulting hash table is returned, or nil if no Xrefs were found."
5488   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5489          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5490          (xref-hashtb (gnus-make-hashtable))
5491          start group entry number xrefs header)
5492     (while headers
5493       (setq header (pop headers))
5494       (when (and (setq xrefs (mail-header-xref header))
5495                  (not (memq (setq number (mail-header-number header))
5496                             unreads)))
5497         (setq start 0)
5498         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5499           (setq start (match-end 0))
5500           (setq group (if prefix
5501                           (concat prefix (substring xrefs (match-beginning 1)
5502                                                     (match-end 1)))
5503                         (substring xrefs (match-beginning 1) (match-end 1))))
5504           (setq number
5505                 (string-to-int (substring xrefs (match-beginning 2)
5506                                           (match-end 2))))
5507           (if (setq entry (gnus-gethash group xref-hashtb))
5508               (setcdr entry (cons number (cdr entry)))
5509             (gnus-sethash group (cons number nil) xref-hashtb)))))
5510     (and start xref-hashtb)))
5511
5512 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5513   "Look through all the headers and mark the Xrefs as read."
5514   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5515         name entry info xref-hashtb idlist method nth4)
5516     (save-excursion
5517       (set-buffer gnus-group-buffer)
5518       (when (setq xref-hashtb
5519                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5520         (mapatoms
5521          (lambda (group)
5522            (unless (string= from-newsgroup (setq name (symbol-name group)))
5523              (setq idlist (symbol-value group))
5524              ;; Dead groups are not updated.
5525              (and (prog1
5526                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5527                             info (nth 2 entry))
5528                     (when (stringp (setq nth4 (gnus-info-method info)))
5529                       (setq nth4 (gnus-server-to-method nth4))))
5530                   ;; Only do the xrefs if the group has the same
5531                   ;; select method as the group we have just read.
5532                   (or (gnus-methods-equal-p
5533                        nth4 (gnus-find-method-for-group from-newsgroup))
5534                       virtual
5535                       (equal nth4 (setq method (gnus-find-method-for-group
5536                                                 from-newsgroup)))
5537                       (and (equal (car nth4) (car method))
5538                            (equal (nth 1 nth4) (nth 1 method))))
5539                   gnus-use-cross-reference
5540                   (or (not (eq gnus-use-cross-reference t))
5541                       virtual
5542                       ;; Only do cross-references on subscribed
5543                       ;; groups, if that is what is wanted.
5544                       (<= (gnus-info-level info) gnus-level-subscribed))
5545                   (gnus-group-make-articles-read name idlist))))
5546          xref-hashtb)))))
5547
5548 (defun gnus-compute-read-articles (group articles)
5549   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5550          (info (nth 2 entry))
5551          (active (gnus-active group))
5552          ninfo)
5553     (when entry
5554       ;; First peel off all invalid article numbers.
5555       (when active
5556         (let ((ids articles)
5557               id first)
5558           (while (setq id (pop ids))
5559             (when (and first (> id (cdr active)))
5560               ;; We'll end up in this situation in one particular
5561               ;; obscure situation.  If you re-scan a group and get
5562               ;; a new article that is cross-posted to a different
5563               ;; group that has not been re-scanned, you might get
5564               ;; crossposted article that has a higher number than
5565               ;; Gnus believes possible.  So we re-activate this
5566               ;; group as well.  This might mean doing the
5567               ;; crossposting thingy will *increase* the number
5568               ;; of articles in some groups.  Tsk, tsk.
5569               (setq active (or (gnus-activate-group group) active)))
5570             (when (or (> id (cdr active))
5571                       (< id (car active)))
5572               (setq articles (delq id articles))))))
5573       ;; If the read list is nil, we init it.
5574       (if (and active
5575                (null (gnus-info-read info))
5576                (> (car active) 1))
5577           (setq ninfo (cons 1 (1- (car active))))
5578         (setq ninfo (gnus-info-read info)))
5579       ;; Then we add the read articles to the range.
5580       (gnus-add-to-range
5581        ninfo (setq articles (sort articles '<))))))
5582
5583 (defun gnus-group-make-articles-read (group articles)
5584   "Update the info of GROUP to say that ARTICLES are read."
5585   (let* ((num 0)
5586          (entry (gnus-gethash group gnus-newsrc-hashtb))
5587          (info (nth 2 entry))
5588          (active (gnus-active group))
5589          range)
5590     (when entry
5591       (setq range (gnus-compute-read-articles group articles))
5592       (save-excursion
5593         (set-buffer gnus-group-buffer)
5594         (gnus-undo-register
5595           `(progn
5596              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5597              (gnus-info-set-read ',info ',(gnus-info-read info))
5598              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5599              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5600              (gnus-group-update-group ,group t))))
5601       ;; Add the read articles to the range.
5602       (gnus-info-set-read info range)
5603       (gnus-request-set-mark group (list (list range 'add '(read))))
5604       ;; Then we have to re-compute how many unread
5605       ;; articles there are in this group.
5606       (when active
5607         (cond
5608          ((not range)
5609           (setq num (- (1+ (cdr active)) (car active))))
5610          ((not (listp (cdr range)))
5611           (setq num (- (cdr active) (- (1+ (cdr range))
5612                                        (car range)))))
5613          (t
5614           (while range
5615             (if (numberp (car range))
5616                 (setq num (1+ num))
5617               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5618             (setq range (cdr range)))
5619           (setq num (- (cdr active) num))))
5620         ;; Update the number of unread articles.
5621         (setcar entry num)
5622         ;; Update the group buffer.
5623         (unless (gnus-ephemeral-group-p group)
5624           (gnus-group-update-group group t))))))
5625
5626 (defvar gnus-newsgroup-none-id 0)
5627
5628 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5629   (let ((cur nntp-server-buffer)
5630         (dependencies
5631          (or dependencies
5632              (save-excursion (set-buffer gnus-summary-buffer)
5633                              gnus-newsgroup-dependencies)))
5634         headers id end ref
5635         (mail-parse-charset gnus-newsgroup-charset)
5636         (mail-parse-ignored-charsets
5637          (save-excursion (condition-case nil
5638                              (set-buffer gnus-summary-buffer)
5639                            (error))
5640                          gnus-newsgroup-ignored-charsets)))
5641     (save-excursion
5642       (set-buffer nntp-server-buffer)
5643       ;; Translate all TAB characters into SPACE characters.
5644       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5645       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5646       (gnus-run-hooks 'gnus-parse-headers-hook)
5647       (let ((case-fold-search t)
5648             in-reply-to header p lines chars)
5649         (goto-char (point-min))
5650         ;; Search to the beginning of the next header.  Error messages
5651         ;; do not begin with 2 or 3.
5652         (while (re-search-forward "^[23][0-9]+ " nil t)
5653           (setq id nil
5654                 ref nil)
5655           ;; This implementation of this function, with nine
5656           ;; search-forwards instead of the one re-search-forward and
5657           ;; a case (which basically was the old function) is actually
5658           ;; about twice as fast, even though it looks messier.  You
5659           ;; can't have everything, I guess.  Speed and elegance
5660           ;; doesn't always go hand in hand.
5661           (setq
5662            header
5663            (vector
5664             ;; Number.
5665             (prog1
5666                 (read cur)
5667               (end-of-line)
5668               (setq p (point))
5669               (narrow-to-region (point)
5670                                 (or (and (search-forward "\n.\n" nil t)
5671                                          (- (point) 2))
5672                                     (point))))
5673             ;; Subject.
5674             (progn
5675               (goto-char p)
5676               (if (search-forward "\nsubject:" nil t)
5677                   (funcall gnus-decode-encoded-word-function
5678                            (nnheader-header-value))
5679                 "(none)"))
5680             ;; From.
5681             (progn
5682               (goto-char p)
5683               (if (search-forward "\nfrom:" nil t)
5684                   (funcall gnus-decode-encoded-word-function
5685                            (nnheader-header-value))
5686                 "(nobody)"))
5687             ;; Date.
5688             (progn
5689               (goto-char p)
5690               (if (search-forward "\ndate:" nil t)
5691                   (nnheader-header-value) ""))
5692             ;; Message-ID.
5693             (progn
5694               (goto-char p)
5695               (setq id (if (re-search-forward
5696                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5697                            ;; We do it this way to make sure the Message-ID
5698                            ;; is (somewhat) syntactically valid.
5699                            (buffer-substring (match-beginning 1)
5700                                              (match-end 1))
5701                          ;; If there was no message-id, we just fake one
5702                          ;; to make subsequent routines simpler.
5703                          (nnheader-generate-fake-message-id))))
5704             ;; References.
5705             (progn
5706               (goto-char p)
5707               (if (search-forward "\nreferences:" nil t)
5708                   (progn
5709                     (setq end (point))
5710                     (prog1
5711                         (nnheader-header-value)
5712                       (setq ref
5713                             (buffer-substring
5714                              (progn
5715                                (end-of-line)
5716                                (search-backward ">" end t)
5717                                (1+ (point)))
5718                              (progn
5719                                (search-backward "<" end t)
5720                                (point))))))
5721                 ;; Get the references from the in-reply-to header if there
5722                 ;; were no references and the in-reply-to header looks
5723                 ;; promising.
5724                 (if (and (search-forward "\nin-reply-to:" nil t)
5725                          (setq in-reply-to (nnheader-header-value))
5726                          (string-match "<[^>]+>" in-reply-to))
5727                     (let (ref2)
5728                       (setq ref (substring in-reply-to (match-beginning 0)
5729                                            (match-end 0)))
5730                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5731                         (setq ref2 (substring in-reply-to (match-beginning 0)
5732                                               (match-end 0)))
5733                         (when (> (length ref2) (length ref))
5734                           (setq ref ref2)))
5735                       ref)
5736                   (setq ref nil))))
5737             ;; Chars.
5738             (progn
5739               (goto-char p)
5740               (if (search-forward "\nchars: " nil t)
5741                   (if (numberp (setq chars (ignore-errors (read cur))))
5742                       chars -1)
5743                 -1))
5744             ;; Lines.
5745             (progn
5746               (goto-char p)
5747               (if (search-forward "\nlines: " nil t)
5748                   (if (numberp (setq lines (ignore-errors (read cur))))
5749                       lines -1)
5750                 -1))
5751             ;; Xref.
5752             (progn
5753               (goto-char p)
5754               (and (search-forward "\nxref:" nil t)
5755                    (nnheader-header-value)))
5756             ;; Extra.
5757             (when gnus-extra-headers
5758               (let ((extra gnus-extra-headers)
5759                     out)
5760                 (while extra
5761                   (goto-char p)
5762                   (when (search-forward
5763                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5764                     (push (cons (car extra) (nnheader-header-value))
5765                           out))
5766                   (pop extra))
5767                 out))))
5768           (when (equal id ref)
5769             (setq ref nil))
5770
5771           (when gnus-alter-header-function
5772             (funcall gnus-alter-header-function header)
5773             (setq id (mail-header-id header)
5774                   ref (gnus-parent-id (mail-header-references header))))
5775
5776           (when (setq header
5777                       (gnus-dependencies-add-header
5778                        header dependencies force-new))
5779             (push header headers))
5780           (goto-char (point-max))
5781           (widen))
5782         (nreverse headers)))))
5783
5784 ;; Goes through the xover lines and returns a list of vectors
5785 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5786                                                   force-new dependencies
5787                                                   group also-fetch-heads)
5788   "Parse the news overview data in the server buffer.
5789 Return a list of headers that match SEQUENCE (see
5790 `nntp-retrieve-headers')."
5791   ;; Get the Xref when the users reads the articles since most/some
5792   ;; NNTP servers do not include Xrefs when using XOVER.
5793   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5794   (let ((mail-parse-charset gnus-newsgroup-charset)
5795         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5796         (cur nntp-server-buffer)
5797         (dependencies (or dependencies gnus-newsgroup-dependencies))
5798         (allp (cond
5799                ((eq gnus-read-all-available-headers t)
5800                 t)
5801                ((stringp gnus-read-all-available-headers)
5802                 (string-match gnus-read-all-available-headers group))
5803                (t
5804                 nil)))
5805         number headers header)
5806     (save-excursion
5807       (set-buffer nntp-server-buffer)
5808       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5809       ;; Allow the user to mangle the headers before parsing them.
5810       (gnus-run-hooks 'gnus-parse-headers-hook)
5811       (goto-char (point-min))
5812       (gnus-parse-without-error
5813         (while (and (or sequence allp)
5814                     (not (eobp)))
5815           (setq number (read cur))
5816           (when (not allp)
5817             (while (and sequence
5818                         (< (car sequence) number))
5819               (setq sequence (cdr sequence))))
5820           (when (and (or allp
5821                          (and sequence
5822                               (eq number (car sequence))))
5823                      (progn
5824                        (setq sequence (cdr sequence))
5825                        (setq header (inline
5826                                       (gnus-nov-parse-line
5827                                        number dependencies force-new)))))
5828             (push header headers))
5829           (forward-line 1)))
5830       ;; A common bug in inn is that if you have posted an article and
5831       ;; then retrieves the active file, it will answer correctly --
5832       ;; the new article is included.  However, a NOV entry for the
5833       ;; article may not have been generated yet, so this may fail.
5834       ;; We work around this problem by retrieving the last few
5835       ;; headers using HEAD.
5836       (if (or (not also-fetch-heads)
5837               (not sequence))
5838           ;; We (probably) got all the headers.
5839           (nreverse headers)
5840         (let ((gnus-nov-is-evil t))
5841           (nconc
5842            (nreverse headers)
5843            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5844              (gnus-get-newsgroup-headers))))))))
5845
5846 (defun gnus-article-get-xrefs ()
5847   "Fill in the Xref value in `gnus-current-headers', if necessary.
5848 This is meant to be called in `gnus-article-internal-prepare-hook'."
5849   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5850                                  gnus-current-headers)))
5851     (or (not gnus-use-cross-reference)
5852         (not headers)
5853         (and (mail-header-xref headers)
5854              (not (string= (mail-header-xref headers) "")))
5855         (let ((case-fold-search t)
5856               xref)
5857           (save-restriction
5858             (nnheader-narrow-to-headers)
5859             (goto-char (point-min))
5860             (when (or (and (not (eobp))
5861                            (eq (downcase (char-after)) ?x)
5862                            (looking-at "Xref:"))
5863                       (search-forward "\nXref:" nil t))
5864               (goto-char (1+ (match-end 0)))
5865               (setq xref (buffer-substring (point)
5866                                            (progn (end-of-line) (point))))
5867               (mail-header-set-xref headers xref)))))))
5868
5869 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5870   "Find article ID and insert the summary line for that article.
5871 OLD-HEADER can either be a header or a line number to insert
5872 the subject line on."
5873   (let* ((line (and (numberp old-header) old-header))
5874          (old-header (and (vectorp old-header) old-header))
5875          (header (cond ((and old-header use-old-header)
5876                         old-header)
5877                        ((and (numberp id)
5878                              (gnus-number-to-header id))
5879                         (gnus-number-to-header id))
5880                        (t
5881                         (gnus-read-header id))))
5882          (number (and (numberp id) id))
5883          d)
5884     (when header
5885       ;; Rebuild the thread that this article is part of and go to the
5886       ;; article we have fetched.
5887       (when (and (not gnus-show-threads)
5888                  old-header)
5889         (when (and number
5890                    (setq d (gnus-data-find (mail-header-number old-header))))
5891           (goto-char (gnus-data-pos d))
5892           (gnus-data-remove
5893            number
5894            (- (gnus-point-at-bol)
5895               (prog1
5896                   (1+ (gnus-point-at-eol))
5897                 (gnus-delete-line))))))
5898       (when old-header
5899         (mail-header-set-number header (mail-header-number old-header)))
5900       (setq gnus-newsgroup-sparse
5901             (delq (setq number (mail-header-number header))
5902                   gnus-newsgroup-sparse))
5903       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5904       (push number gnus-newsgroup-limit)
5905       (gnus-rebuild-thread (mail-header-id header) line)
5906       (gnus-summary-goto-subject number nil t))
5907     (when (and (numberp number)
5908                (> number 0))
5909       ;; We have to update the boundaries even if we can't fetch the
5910       ;; article if ID is a number -- so that the next `P' or `N'
5911       ;; command will fetch the previous (or next) article even
5912       ;; if the one we tried to fetch this time has been canceled.
5913       (when (> number gnus-newsgroup-end)
5914         (setq gnus-newsgroup-end number))
5915       (when (< number gnus-newsgroup-begin)
5916         (setq gnus-newsgroup-begin number))
5917       (setq gnus-newsgroup-unselected
5918             (delq number gnus-newsgroup-unselected)))
5919     ;; Report back a success?
5920     (and header (mail-header-number header))))
5921
5922 ;;; Process/prefix in the summary buffer
5923
5924 (defun gnus-summary-work-articles (n)
5925   "Return a list of articles to be worked upon.
5926 The prefix argument, the list of process marked articles, and the
5927 current article will be taken into consideration."
5928   (save-excursion
5929     (set-buffer gnus-summary-buffer)
5930     (cond
5931      (n
5932       ;; A numerical prefix has been given.
5933       (setq n (prefix-numeric-value n))
5934       (let ((backward (< n 0))
5935             (n (abs (prefix-numeric-value n)))
5936             articles article)
5937         (save-excursion
5938           (while
5939               (and (> n 0)
5940                    (push (setq article (gnus-summary-article-number))
5941                          articles)
5942                    (if backward
5943                        (gnus-summary-find-prev nil article)
5944                      (gnus-summary-find-next nil article)))
5945             (decf n)))
5946         (nreverse articles)))
5947      ((and (gnus-region-active-p) (mark))
5948       (message "region active")
5949       ;; Work on the region between point and mark.
5950       (let ((max (max (point) (mark)))
5951             articles article)
5952         (save-excursion
5953           (goto-char (min (min (point) (mark))))
5954           (while
5955               (and
5956                (push (setq article (gnus-summary-article-number)) articles)
5957                (gnus-summary-find-next nil article)
5958                (< (point) max)))
5959           (nreverse articles))))
5960      (gnus-newsgroup-processable
5961       ;; There are process-marked articles present.
5962       ;; Save current state.
5963       (gnus-summary-save-process-mark)
5964       ;; Return the list.
5965       (reverse gnus-newsgroup-processable))
5966      (t
5967       ;; Just return the current article.
5968       (list (gnus-summary-article-number))))))
5969
5970 (defmacro gnus-summary-iterate (arg &rest forms)
5971   "Iterate over the process/prefixed articles and do FORMS.
5972 ARG is the interactive prefix given to the command.  FORMS will be
5973 executed with point over the summary line of the articles."
5974   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5975     `(let ((,articles (gnus-summary-work-articles ,arg)))
5976        (while ,articles
5977          (gnus-summary-goto-subject (car ,articles))
5978          ,@forms
5979          (pop ,articles)))))
5980
5981 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5982 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5983
5984 (defun gnus-summary-save-process-mark ()
5985   "Push the current set of process marked articles on the stack."
5986   (interactive)
5987   (push (copy-sequence gnus-newsgroup-processable)
5988         gnus-newsgroup-process-stack))
5989
5990 (defun gnus-summary-kill-process-mark ()
5991   "Push the current set of process marked articles on the stack and unmark."
5992   (interactive)
5993   (gnus-summary-save-process-mark)
5994   (gnus-summary-unmark-all-processable))
5995
5996 (defun gnus-summary-yank-process-mark ()
5997   "Pop the last process mark state off the stack and restore it."
5998   (interactive)
5999   (unless gnus-newsgroup-process-stack
6000     (error "Empty mark stack"))
6001   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6002
6003 (defun gnus-summary-process-mark-set (set)
6004   "Make SET into the current process marked articles."
6005   (gnus-summary-unmark-all-processable)
6006   (while set
6007     (gnus-summary-set-process-mark (pop set))))
6008
6009 ;;; Searching and stuff
6010
6011 (defun gnus-summary-search-group (&optional backward use-level)
6012   "Search for next unread newsgroup.
6013 If optional argument BACKWARD is non-nil, search backward instead."
6014   (save-excursion
6015     (set-buffer gnus-group-buffer)
6016     (when (gnus-group-search-forward
6017            backward nil (if use-level (gnus-group-group-level) nil))
6018       (gnus-group-group-name))))
6019
6020 (defun gnus-summary-best-group (&optional exclude-group)
6021   "Find the name of the best unread group.
6022 If EXCLUDE-GROUP, do not go to this group."
6023   (save-excursion
6024     (set-buffer gnus-group-buffer)
6025     (save-excursion
6026       (gnus-group-best-unread-group exclude-group))))
6027
6028 (defun gnus-summary-find-next (&optional unread article backward)
6029   (if backward (gnus-summary-find-prev)
6030     (let* ((dummy (gnus-summary-article-intangible-p))
6031            (article (or article (gnus-summary-article-number)))
6032            (data (gnus-data-find-list article))
6033            result)
6034       (when (and (not dummy)
6035                  (or (not gnus-summary-check-current)
6036                      (not unread)
6037                      (not (gnus-data-unread-p (car data)))))
6038         (setq data (cdr data)))
6039       (when (setq result
6040                   (if unread
6041                       (progn
6042                         (while data
6043                           (unless (memq (gnus-data-number (car data)) 
6044                                         gnus-newsgroup-unfetched)
6045                             (when (gnus-data-unread-p (car data))
6046                               (setq result (car data)
6047                                     data nil)))
6048                           (setq data (cdr data)))
6049                         result)
6050                     (car data)))
6051         (goto-char (gnus-data-pos result))
6052         (gnus-data-number result)))))
6053
6054 (defun gnus-summary-find-prev (&optional unread article)
6055   (let* ((eobp (eobp))
6056          (article (or article (gnus-summary-article-number)))
6057          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6058          result)
6059     (when (and (not eobp)
6060                (or (not gnus-summary-check-current)
6061                    (not unread)
6062                    (not (gnus-data-unread-p (car data)))))
6063       (setq data (cdr data)))
6064     (when (setq result
6065                 (if unread
6066                     (progn
6067                       (while data
6068                         (unless (memq (gnus-data-number (car data)) gnus-newsgroup-unfetched)
6069                           (when (gnus-data-unread-p (car data))
6070                             (setq result (car data)
6071                                   data nil)))
6072                         (setq data (cdr data)))
6073                       result)
6074                   (car data)))
6075       (goto-char (gnus-data-pos result))
6076       (gnus-data-number result))))
6077
6078 (defun gnus-summary-find-subject (subject &optional unread backward article)
6079   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6080          (article (or article (gnus-summary-article-number)))
6081          (articles (gnus-data-list backward))
6082          (arts (gnus-data-find-list article articles))
6083          result)
6084     (when (or (not gnus-summary-check-current)
6085               (not unread)
6086               (not (gnus-data-unread-p (car arts))))
6087       (setq arts (cdr arts)))
6088     (while arts
6089       (and (or (not unread)
6090                (gnus-data-unread-p (car arts)))
6091            (vectorp (gnus-data-header (car arts)))
6092            (gnus-subject-equal
6093             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6094            (setq result (car arts)
6095                  arts nil))
6096       (setq arts (cdr arts)))
6097     (and result
6098          (goto-char (gnus-data-pos result))
6099          (gnus-data-number result))))
6100
6101 (defun gnus-summary-search-forward (&optional unread subject backward)
6102   "Search forward for an article.
6103 If UNREAD, look for unread articles.  If SUBJECT, look for
6104 articles with that subject.  If BACKWARD, search backward instead."
6105   (cond (subject (gnus-summary-find-subject subject unread backward))
6106         (backward (gnus-summary-find-prev unread))
6107         (t (gnus-summary-find-next unread))))
6108
6109 (defun gnus-recenter (&optional n)
6110   "Center point in window and redisplay frame.
6111 Also do horizontal recentering."
6112   (interactive "P")
6113   (when (and gnus-auto-center-summary
6114              (not (eq gnus-auto-center-summary 'vertical)))
6115     (gnus-horizontal-recenter))
6116   (recenter n))
6117
6118 (defun gnus-summary-recenter ()
6119   "Center point in the summary window.
6120 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6121 displayed, no centering will be performed."
6122   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6123   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6124   (interactive)
6125   ;; The user has to want it.
6126   (when gnus-auto-center-summary
6127     (let* ((top (cond ((< (window-height) 4) 0)
6128                       ((< (window-height) 7) 1)
6129                       (t (if (numberp gnus-auto-center-summary)
6130                              gnus-auto-center-summary
6131                            2))))
6132            (height (1- (window-height)))
6133            (bottom (save-excursion (goto-char (point-max))
6134                                    (forward-line (- height))
6135                                    (point)))
6136            (window (get-buffer-window (current-buffer))))
6137       (when (get-buffer-window gnus-article-buffer)
6138         ;; Only do recentering when the article buffer is displayed,
6139         ;; Set the window start to either `bottom', which is the biggest
6140         ;; possible valid number, or the second line from the top,
6141         ;; whichever is the least.
6142         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6143           (if (> bottom top-pos)
6144               ;; Keep the second line from the top visible
6145               (set-window-start window top-pos t)
6146             ;; Try to keep the bottom line visible; if it's partially
6147             ;; obscured, either scroll one more line to make it fully
6148             ;; visible, or revert to using TOP-POS.
6149             (save-excursion
6150               (goto-char (point-max))
6151               (forward-line -1)
6152               (let ((last-line-start (point)))
6153                 (goto-char bottom)
6154                 (set-window-start window (point) t)
6155                 (when (not (pos-visible-in-window-p last-line-start window))
6156                   (forward-line 1)
6157                   (set-window-start window (min (point) top-pos) t)))))))
6158       ;; Do horizontal recentering while we're at it.
6159       (when (and (get-buffer-window (current-buffer) t)
6160                  (not (eq gnus-auto-center-summary 'vertical)))
6161         (let ((selected (selected-window)))
6162           (select-window (get-buffer-window (current-buffer) t))
6163           (gnus-summary-position-point)
6164           (gnus-horizontal-recenter)
6165           (select-window selected))))))
6166
6167 (defun gnus-summary-jump-to-group (newsgroup)
6168   "Move point to NEWSGROUP in group mode buffer."
6169   ;; Keep update point of group mode buffer if visible.
6170   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6171       (save-window-excursion
6172         ;; Take care of tree window mode.
6173         (when (get-buffer-window gnus-group-buffer)
6174           (pop-to-buffer gnus-group-buffer))
6175         (gnus-group-jump-to-group newsgroup))
6176     (save-excursion
6177       ;; Take care of tree window mode.
6178       (if (get-buffer-window gnus-group-buffer)
6179           (pop-to-buffer gnus-group-buffer)
6180         (set-buffer gnus-group-buffer))
6181       (gnus-group-jump-to-group newsgroup))))
6182
6183 ;; This function returns a list of article numbers based on the
6184 ;; difference between the ranges of read articles in this group and
6185 ;; the range of active articles.
6186 (defun gnus-list-of-unread-articles (group)
6187   (let* ((read (gnus-info-read (gnus-get-info group)))
6188          (active (or (gnus-active group) (gnus-activate-group group)))
6189          (last (cdr active))
6190          first nlast unread)
6191     ;; If none are read, then all are unread.
6192     (if (not read)
6193         (setq first (car active))
6194       ;; If the range of read articles is a single range, then the
6195       ;; first unread article is the article after the last read
6196       ;; article.  Sounds logical, doesn't it?
6197       (if (and (not (listp (cdr read)))
6198                (or (< (car read) (car active))
6199                    (progn (setq read (list read))
6200                           nil)))
6201           (setq first (max (car active) (1+ (cdr read))))
6202         ;; `read' is a list of ranges.
6203         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6204                                   (caar read)))
6205                   1)
6206           (setq first (car active)))
6207         (while read
6208           (when first
6209             (while (< first nlast)
6210               (push first unread)
6211               (setq first (1+ first))))
6212           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6213           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6214           (setq read (cdr read)))))
6215     ;; And add the last unread articles.
6216     (while (<= first last)
6217       (push first unread)
6218       (setq first (1+ first)))
6219     ;; Return the list of unread articles.
6220     (delq 0 (nreverse unread))))
6221
6222 (defun gnus-list-of-read-articles (group)
6223   "Return a list of unread, unticked and non-dormant articles."
6224   (let* ((info (gnus-get-info group))
6225          (marked (gnus-info-marks info))
6226          (active (gnus-active group)))
6227     (and info active
6228          (gnus-list-range-difference
6229           (gnus-list-range-difference
6230            (gnus-sorted-complement
6231             (gnus-uncompress-range active)
6232             (gnus-list-of-unread-articles group))
6233            (cdr (assq 'dormant marked)))
6234           (cdr (assq 'tick marked))))))
6235
6236 ;; Various summary commands
6237
6238 (defun gnus-summary-select-article-buffer ()
6239   "Reconfigure windows to show article buffer."
6240   (interactive)
6241   (if (not (gnus-buffer-live-p gnus-article-buffer))
6242       (error "There is no article buffer for this summary buffer")
6243     (gnus-configure-windows 'article)
6244     (select-window (get-buffer-window gnus-article-buffer))))
6245
6246 (defun gnus-summary-universal-argument (arg)
6247   "Perform any operation on all articles that are process/prefixed."
6248   (interactive "P")
6249   (let ((articles (gnus-summary-work-articles arg))
6250         func article)
6251     (if (eq
6252          (setq
6253           func
6254           (key-binding
6255            (read-key-sequence
6256             (substitute-command-keys
6257              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6258          'undefined)
6259         (gnus-error 1 "Undefined key")
6260       (save-excursion
6261         (while articles
6262           (gnus-summary-goto-subject (setq article (pop articles)))
6263           (let (gnus-newsgroup-processable)
6264             (command-execute func))
6265           (gnus-summary-remove-process-mark article)))))
6266   (gnus-summary-position-point))
6267
6268 (defun gnus-summary-toggle-truncation (&optional arg)
6269   "Toggle truncation of summary lines.
6270 With arg, turn line truncation on if arg is positive."
6271   (interactive "P")
6272   (setq truncate-lines
6273         (if (null arg) (not truncate-lines)
6274           (> (prefix-numeric-value arg) 0)))
6275   (redraw-display))
6276
6277 (defun gnus-summary-find-uncancelled ()
6278   "Return the number of an uncancelled article.
6279 The current article is considered, then following articles, then previous
6280 articles.  If all articles are cancelled then return a dummy 0."
6281   (let (found)
6282     (dolist (rev '(nil t))
6283       (unless found      ; don't demand the reverse list if we don't need it
6284         (let ((data (gnus-data-find-list
6285                      (gnus-summary-article-number) (gnus-data-list rev))))
6286           (while (and data (not found))
6287             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6288                 (setq found (gnus-data-number (car data))))
6289             (setq data (cdr data))))))
6290     (or found 0)))
6291
6292 (defun gnus-summary-reselect-current-group (&optional all rescan)
6293   "Exit and then reselect the current newsgroup.
6294 The prefix argument ALL means to select all articles."
6295   (interactive "P")
6296   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6297     (error "Ephemeral groups can't be reselected"))
6298   (let ((current-subject (gnus-summary-find-uncancelled))
6299         (group gnus-newsgroup-name))
6300     (setq gnus-newsgroup-begin nil)
6301     (gnus-summary-exit)
6302     ;; We have to adjust the point of group mode buffer because
6303     ;; point was moved to the next unread newsgroup by exiting.
6304     (gnus-summary-jump-to-group group)
6305     (when rescan
6306       (save-excursion
6307         (gnus-group-get-new-news-this-group 1)))
6308     (gnus-group-read-group all t)
6309     (gnus-summary-goto-subject current-subject nil t)))
6310
6311 (defun gnus-summary-rescan-group (&optional all)
6312   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6313   (interactive "P")
6314   (gnus-summary-reselect-current-group all t))
6315
6316 (defun gnus-summary-update-info (&optional non-destructive)
6317   (save-excursion
6318     (let ((group gnus-newsgroup-name))
6319       (when group
6320         (when gnus-newsgroup-kill-headers
6321           (setq gnus-newsgroup-killed
6322                 (gnus-compress-sequence
6323                  (gnus-sorted-union
6324                   (gnus-list-range-intersection
6325                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6326                   gnus-newsgroup-unreads)
6327                  t)))
6328         (unless (listp (cdr gnus-newsgroup-killed))
6329           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6330         (let ((headers gnus-newsgroup-headers))
6331           ;; Set the new ranges of read articles.
6332           (save-excursion
6333             (set-buffer gnus-group-buffer)
6334             (gnus-undo-force-boundary))
6335           (gnus-update-read-articles
6336            group (gnus-sorted-union
6337                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6338           ;; Set the current article marks.
6339           (let ((gnus-newsgroup-scored
6340                  (if (and (not gnus-save-score)
6341                           (not non-destructive))
6342                      nil
6343                    gnus-newsgroup-scored)))
6344             (save-excursion
6345               (gnus-update-marks)))
6346           ;; Do the cross-ref thing.
6347           (when gnus-use-cross-reference
6348             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6349           ;; Do not switch windows but change the buffer to work.
6350           (set-buffer gnus-group-buffer)
6351           (unless (gnus-ephemeral-group-p group)
6352             (gnus-group-update-group group)))))))
6353
6354 (defun gnus-summary-save-newsrc (&optional force)
6355   "Save the current number of read/marked articles in the dribble buffer.
6356 The dribble buffer will then be saved.
6357 If FORCE (the prefix), also save the .newsrc file(s)."
6358   (interactive "P")
6359   (gnus-summary-update-info t)
6360   (if force
6361       (gnus-save-newsrc-file)
6362     (gnus-dribble-save)))
6363
6364 (defun gnus-summary-exit (&optional temporary)
6365   "Exit reading current newsgroup, and then return to group selection mode.
6366 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6367   (interactive)
6368   (gnus-set-global-variables)
6369   (when (gnus-buffer-live-p gnus-article-buffer)
6370     (save-excursion
6371       (set-buffer gnus-article-buffer)
6372       (mm-destroy-parts gnus-article-mime-handles)
6373       ;; Set it to nil for safety reason.
6374       (setq gnus-article-mime-handle-alist nil)
6375       (setq gnus-article-mime-handles nil)))
6376   (gnus-kill-save-kill-buffer)
6377   (gnus-async-halt-prefetch)
6378   (let* ((group gnus-newsgroup-name)
6379          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6380          (gnus-group-is-exiting-p t)
6381          (mode major-mode)
6382          (group-point nil)
6383          (buf (current-buffer)))
6384     (unless quit-config
6385       ;; Do adaptive scoring, and possibly save score files.
6386       (when gnus-newsgroup-adaptive
6387         (gnus-score-adaptive))
6388       (when gnus-use-scoring
6389         (gnus-score-save)))
6390     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6391     ;; If we have several article buffers, we kill them at exit.
6392     (unless gnus-single-article-buffer
6393       (gnus-kill-buffer gnus-original-article-buffer)
6394       (setq gnus-article-current nil))
6395     (when gnus-use-cache
6396       (gnus-cache-possibly-remove-articles)
6397       (gnus-cache-save-buffers))
6398     (gnus-async-prefetch-remove-group group)
6399     (when gnus-suppress-duplicates
6400       (gnus-dup-enter-articles))
6401     (when gnus-use-trees
6402       (gnus-tree-close group))
6403     (when gnus-use-cache
6404       (gnus-cache-write-active))
6405     ;; Remove entries for this group.
6406     (nnmail-purge-split-history (gnus-group-real-name group))
6407     ;; Make all changes in this group permanent.
6408     (unless quit-config
6409       (gnus-run-hooks 'gnus-exit-group-hook)
6410       (gnus-summary-update-info))
6411     (gnus-close-group group)
6412     ;; Make sure where we were, and go to next newsgroup.
6413     (set-buffer gnus-group-buffer)
6414     (unless quit-config
6415       (gnus-group-jump-to-group group))
6416     (gnus-run-hooks 'gnus-summary-exit-hook)
6417     (unless (or quit-config
6418                 ;; If this group has disappeared from the summary
6419                 ;; buffer, don't skip forwards.
6420                 (not (string= group (gnus-group-group-name))))
6421       (gnus-group-next-unread-group 1))
6422     (setq group-point (point))
6423     (if temporary
6424         nil                             ;Nothing to do.
6425       ;; If we have several article buffers, we kill them at exit.
6426       (unless gnus-single-article-buffer
6427         (gnus-kill-buffer gnus-article-buffer)
6428         (gnus-kill-buffer gnus-original-article-buffer)
6429         (setq gnus-article-current nil))
6430       (set-buffer buf)
6431       (if (not gnus-kill-summary-on-exit)
6432           (progn
6433             (gnus-deaden-summary)
6434             (setq mode nil))
6435         ;; We set all buffer-local variables to nil.  It is unclear why
6436         ;; this is needed, but if we don't, buffer-local variables are
6437         ;; not garbage-collected, it seems.  This would the lead to en
6438         ;; ever-growing Emacs.
6439         (gnus-summary-clear-local-variables)
6440         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6441           (gnus-summary-clear-local-variables))
6442         (when (get-buffer gnus-article-buffer)
6443           (bury-buffer gnus-article-buffer))
6444         ;; We clear the global counterparts of the buffer-local
6445         ;; variables as well, just to be on the safe side.
6446         (set-buffer gnus-group-buffer)
6447         (gnus-summary-clear-local-variables)
6448         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6449           (gnus-summary-clear-local-variables))
6450         ;; Return to group mode buffer.
6451         (when (eq mode 'gnus-summary-mode)
6452           (gnus-kill-buffer buf)))
6453       (setq gnus-current-select-method gnus-select-method)
6454       (pop-to-buffer gnus-group-buffer)
6455       (if (not quit-config)
6456           (progn
6457             (goto-char group-point)
6458             (gnus-configure-windows 'group 'force))
6459         (gnus-handle-ephemeral-exit quit-config))
6460       ;; Clear the current group name.
6461       (unless quit-config
6462         (setq gnus-newsgroup-name nil)))))
6463
6464 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6465 (defun gnus-summary-exit-no-update (&optional no-questions)
6466   "Quit reading current newsgroup without updating read article info."
6467   (interactive)
6468   (let* ((group gnus-newsgroup-name)
6469          (gnus-group-is-exiting-p t)
6470          (gnus-group-is-exiting-without-update-p t)
6471          (quit-config (gnus-group-quit-config group)))
6472     (when (or no-questions
6473               gnus-expert-user
6474               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6475       (gnus-async-halt-prefetch)
6476       (run-hooks 'gnus-summary-prepare-exit-hook)
6477       (when (gnus-buffer-live-p gnus-article-buffer)
6478         (save-excursion
6479           (set-buffer gnus-article-buffer)
6480           (mm-destroy-parts gnus-article-mime-handles)
6481           ;; Set it to nil for safety reason.
6482           (setq gnus-article-mime-handle-alist nil)
6483           (setq gnus-article-mime-handles nil)))
6484       ;; If we have several article buffers, we kill them at exit.
6485       (unless gnus-single-article-buffer
6486         (gnus-kill-buffer gnus-article-buffer)
6487         (gnus-kill-buffer gnus-original-article-buffer)
6488         (setq gnus-article-current nil))
6489       (if (not gnus-kill-summary-on-exit)
6490           (gnus-deaden-summary)
6491         (gnus-close-group group)
6492         (gnus-summary-clear-local-variables)
6493         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6494           (gnus-summary-clear-local-variables))
6495         (set-buffer gnus-group-buffer)
6496         (gnus-summary-clear-local-variables)
6497         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6498           (gnus-summary-clear-local-variables))
6499         (gnus-kill-buffer gnus-summary-buffer))
6500       (unless gnus-single-article-buffer
6501         (setq gnus-article-current nil))
6502       (when gnus-use-trees
6503         (gnus-tree-close group))
6504       (gnus-async-prefetch-remove-group group)
6505       (when (get-buffer gnus-article-buffer)
6506         (bury-buffer gnus-article-buffer))
6507       ;; Return to the group buffer.
6508       (gnus-configure-windows 'group 'force)
6509       ;; Clear the current group name.
6510       (setq gnus-newsgroup-name nil)
6511       (unless (gnus-ephemeral-group-p group)
6512         (gnus-group-update-group group))
6513       (when (equal (gnus-group-group-name) group)
6514         (gnus-group-next-unread-group 1))
6515       (when quit-config
6516         (gnus-handle-ephemeral-exit quit-config)))))
6517
6518 (defun gnus-handle-ephemeral-exit (quit-config)
6519   "Handle movement when leaving an ephemeral group.
6520 The state which existed when entering the ephemeral is reset."
6521   (if (not (buffer-name (car quit-config)))
6522       (gnus-configure-windows 'group 'force)
6523     (set-buffer (car quit-config))
6524     (cond ((eq major-mode 'gnus-summary-mode)
6525            (gnus-set-global-variables))
6526           ((eq major-mode 'gnus-article-mode)
6527            (save-excursion
6528              ;; The `gnus-summary-buffer' variable may point
6529              ;; to the old summary buffer when using a single
6530              ;; article buffer.
6531              (unless (gnus-buffer-live-p gnus-summary-buffer)
6532                (set-buffer gnus-group-buffer))
6533              (set-buffer gnus-summary-buffer)
6534              (gnus-set-global-variables))))
6535     (if (or (eq (cdr quit-config) 'article)
6536             (eq (cdr quit-config) 'pick))
6537         (progn
6538           ;; The current article may be from the ephemeral group
6539           ;; thus it is best that we reload this article
6540           (gnus-summary-show-article)
6541           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6542               (gnus-configure-windows 'pick 'force)
6543             (gnus-configure-windows (cdr quit-config) 'force)))
6544       (gnus-configure-windows (cdr quit-config) 'force))
6545     (when (eq major-mode 'gnus-summary-mode)
6546       (gnus-summary-next-subject 1 nil t)
6547       (gnus-summary-recenter)
6548       (gnus-summary-position-point))))
6549
6550 ;;; Dead summaries.
6551
6552 (defvar gnus-dead-summary-mode-map nil)
6553
6554 (unless gnus-dead-summary-mode-map
6555   (setq gnus-dead-summary-mode-map (make-keymap))
6556   (suppress-keymap gnus-dead-summary-mode-map)
6557   (substitute-key-definition
6558    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6559   (dolist (key '("\C-d" "\r" "\177" [delete]))
6560     (define-key gnus-dead-summary-mode-map
6561       key 'gnus-summary-wake-up-the-dead))
6562   (dolist (key '("q" "Q"))
6563     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6564
6565 (defvar gnus-dead-summary-mode nil
6566   "Minor mode for Gnus summary buffers.")
6567
6568 (defun gnus-dead-summary-mode (&optional arg)
6569   "Minor mode for Gnus summary buffers."
6570   (interactive "P")
6571   (when (eq major-mode 'gnus-summary-mode)
6572     (make-local-variable 'gnus-dead-summary-mode)
6573     (setq gnus-dead-summary-mode
6574           (if (null arg) (not gnus-dead-summary-mode)
6575             (> (prefix-numeric-value arg) 0)))
6576     (when gnus-dead-summary-mode
6577       (gnus-add-minor-mode
6578        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6579
6580 (defun gnus-deaden-summary ()
6581   "Make the current summary buffer into a dead summary buffer."
6582   ;; Kill any previous dead summary buffer.
6583   (when (and gnus-dead-summary
6584              (buffer-name gnus-dead-summary))
6585     (save-excursion
6586       (set-buffer gnus-dead-summary)
6587       (when gnus-dead-summary-mode
6588         (kill-buffer (current-buffer)))))
6589   ;; Make this the current dead summary.
6590   (setq gnus-dead-summary (current-buffer))
6591   (gnus-dead-summary-mode 1)
6592   (let ((name (buffer-name)))
6593     (when (string-match "Summary" name)
6594       (rename-buffer
6595        (concat (substring name 0 (match-beginning 0)) "Dead "
6596                (substring name (match-beginning 0)))
6597        t)
6598       (bury-buffer))))
6599
6600 (defun gnus-kill-or-deaden-summary (buffer)
6601   "Kill or deaden the summary BUFFER."
6602   (save-excursion
6603     (when (and (buffer-name buffer)
6604                (not gnus-single-article-buffer))
6605       (save-excursion
6606         (set-buffer buffer)
6607         (gnus-kill-buffer gnus-article-buffer)
6608         (gnus-kill-buffer gnus-original-article-buffer)))
6609     (cond
6610      ;; Kill the buffer.
6611      (gnus-kill-summary-on-exit
6612       (when (and gnus-use-trees
6613                  (gnus-buffer-exists-p buffer))
6614         (save-excursion
6615           (set-buffer buffer)
6616           (gnus-tree-close gnus-newsgroup-name)))
6617       (gnus-kill-buffer buffer))
6618      ;; Deaden the buffer.
6619      ((gnus-buffer-exists-p buffer)
6620       (save-excursion
6621         (set-buffer buffer)
6622         (gnus-deaden-summary))))))
6623
6624 (defun gnus-summary-wake-up-the-dead (&rest args)
6625   "Wake up the dead summary buffer."
6626   (interactive)
6627   (gnus-dead-summary-mode -1)
6628   (let ((name (buffer-name)))
6629     (when (string-match "Dead " name)
6630       (rename-buffer
6631        (concat (substring name 0 (match-beginning 0))
6632                (substring name (match-end 0)))
6633        t)))
6634   (gnus-message 3 "This dead summary is now alive again"))
6635
6636 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6637 (defun gnus-summary-fetch-faq (&optional faq-dir)
6638   "Fetch the FAQ for the current group.
6639 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6640 in."
6641   (interactive
6642    (list
6643     (when current-prefix-arg
6644       (completing-read
6645        "FAQ dir: " (and (listp gnus-group-faq-directory)
6646                         (mapcar (lambda (file) (list file))
6647                                 gnus-group-faq-directory))))))
6648   (let (gnus-faq-buffer)
6649     (when (setq gnus-faq-buffer
6650                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6651       (gnus-configure-windows 'summary-faq))))
6652
6653 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6654 (defun gnus-summary-describe-group (&optional force)
6655   "Describe the current newsgroup."
6656   (interactive "P")
6657   (gnus-group-describe-group force gnus-newsgroup-name))
6658
6659 (defun gnus-summary-describe-briefly ()
6660   "Describe summary mode commands briefly."
6661   (interactive)
6662   (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")))
6663
6664 ;; Walking around group mode buffer from summary mode.
6665
6666 (defun gnus-summary-next-group (&optional no-article target-group backward)
6667   "Exit current newsgroup and then select next unread newsgroup.
6668 If prefix argument NO-ARTICLE is non-nil, no article is selected
6669 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6670 previous group instead."
6671   (interactive "P")
6672   ;; Stop pre-fetching.
6673   (gnus-async-halt-prefetch)
6674   (let ((current-group gnus-newsgroup-name)
6675         (current-buffer (current-buffer))
6676         entered)
6677     (while (not entered)
6678       ;; Then we find what group we are supposed to enter.
6679       (set-buffer gnus-group-buffer)
6680       (gnus-group-jump-to-group current-group)
6681       (setq target-group
6682             (or target-group
6683                 (if (eq gnus-keep-same-level 'best)
6684                     (gnus-summary-best-group gnus-newsgroup-name)
6685                   (gnus-summary-search-group backward gnus-keep-same-level))))
6686       (if (not target-group)
6687           ;; There are no further groups, so we return to the group
6688           ;; buffer.
6689           (progn
6690             (gnus-message 5 "Returning to the group buffer")
6691             (setq entered t)
6692             (when (gnus-buffer-live-p current-buffer)
6693               (set-buffer current-buffer)
6694               (gnus-summary-exit))
6695             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6696         ;; We try to enter the target group.
6697         (gnus-group-jump-to-group target-group)
6698         (let ((unreads (gnus-group-group-unread)))
6699           (if (and (or (eq t unreads)
6700                        (and unreads (not (zerop unreads))))
6701                    (progn
6702                      ;; Now we semi-exit this group to update Xrefs
6703                      ;; and all variables.  We can't do a real exit,
6704                      ;; because the window conf must remain the same
6705                      ;; in case the user is prompted for info, and we
6706                      ;; don't want the window conf to change before
6707                      ;; that...
6708                      (when (gnus-buffer-live-p current-buffer)
6709                        (set-buffer current-buffer)
6710                        (gnus-summary-exit t))
6711                      (gnus-summary-read-group
6712                       target-group nil no-article
6713                       (and (buffer-name current-buffer) current-buffer)
6714                       nil backward)))
6715               (setq entered t)
6716             (setq current-group target-group
6717                   target-group nil)))))))
6718
6719 (defun gnus-summary-prev-group (&optional no-article)
6720   "Exit current newsgroup and then select previous unread newsgroup.
6721 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6722   (interactive "P")
6723   (gnus-summary-next-group no-article nil t))
6724
6725 ;; Walking around summary lines.
6726
6727 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6728   "Go to the first subject satisfying any non-nil constraint.
6729 If UNREAD is non-nil, the article should be unread.
6730 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6731 If UNSEED is non-nil, the article should be unseen.
6732 Returns the article selected or nil if there are no matching articles."
6733   (interactive "P")
6734   (cond
6735    ;; Empty summary.
6736    ((null gnus-newsgroup-data)
6737     (gnus-message 3 "No articles in the group")
6738     nil)
6739    ;; Pick the first article.
6740    ((not (or unread undownloaded unseen))
6741     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6742     (gnus-data-number (car gnus-newsgroup-data)))
6743    ;; Find the first unread article.
6744    (t
6745     (let ((data gnus-newsgroup-data))
6746       (while (and data
6747                   (let ((num (gnus-data-number (car data))))
6748                     (or (memq num gnus-newsgroup-unfetched)
6749                         (not (or (and unread
6750                                       (memq num gnus-newsgroup-unreads))
6751                                  (and undownloaded
6752                                       (memq num gnus-newsgroup-undownloaded))
6753                                  (and unseen
6754                                       (memq num gnus-newsgroup-unseen)))))))
6755         (setq data (cdr data)))
6756       (prog1 
6757           (if data
6758               (progn
6759                 (goto-char (gnus-data-pos (car data)))
6760                 (gnus-data-number (car data)))
6761             (gnus-message 3 "No more%s articles"
6762                           (let* ((r (when unread " unread"))
6763                                  (d (when undownloaded " undownloaded"))
6764                                  (s (when unseen " unseen"))
6765                                  (l (delq nil (list r d s))))
6766                             (cond ((= 3 (length l))
6767                                    (concat r "," d ", or" s))
6768                                   ((= 2 (length l))
6769                                    (concat (car l) ", or" (cadr l)))
6770                                   ((= 1 (length l))
6771                                    (car l))
6772                                   (t
6773                                    ""))))
6774             nil
6775             )
6776         (gnus-summary-position-point))))))
6777
6778 (defun gnus-summary-next-subject (n &optional unread dont-display)
6779   "Go to next N'th summary line.
6780 If N is negative, go to the previous N'th subject line.
6781 If UNREAD is non-nil, only unread articles are selected.
6782 The difference between N and the actual number of steps taken is
6783 returned."
6784   (interactive "p")
6785   (let ((backward (< n 0))
6786         (n (abs n)))
6787     (while (and (> n 0)
6788                 (if backward
6789                     (gnus-summary-find-prev unread)
6790                   (gnus-summary-find-next unread)))
6791       (unless (zerop (setq n (1- n)))
6792         (gnus-summary-show-thread)))
6793     (when (/= 0 n)
6794       (gnus-message 7 "No more%s articles"
6795                     (if unread " unread" "")))
6796     (unless dont-display
6797       (gnus-summary-recenter)
6798       (gnus-summary-position-point))
6799     n))
6800
6801 (defun gnus-summary-next-unread-subject (n)
6802   "Go to next N'th unread summary line."
6803   (interactive "p")
6804   (gnus-summary-next-subject n t))
6805
6806 (defun gnus-summary-prev-subject (n &optional unread)
6807   "Go to previous N'th summary line.
6808 If optional argument UNREAD is non-nil, only unread article is selected."
6809   (interactive "p")
6810   (gnus-summary-next-subject (- n) unread))
6811
6812 (defun gnus-summary-prev-unread-subject (n)
6813   "Go to previous N'th unread summary line."
6814   (interactive "p")
6815   (gnus-summary-next-subject (- n) t))
6816
6817 (defun gnus-summary-goto-subjects (articles)
6818   "Insert the subject header for ARTICLES in the current buffer."
6819   (save-excursion
6820     (dolist (article articles)
6821       (gnus-summary-goto-subject article t)))
6822   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6823   (gnus-summary-position-point))
6824  
6825 (defun gnus-summary-goto-subject (article &optional force silent)
6826   "Go the subject line of ARTICLE.
6827 If FORCE, also allow jumping to articles not currently shown."
6828   (interactive "nArticle number: ")
6829   (unless (numberp article)
6830     (error "Article %s is not a number" article))
6831   (let ((b (point))
6832         (data (gnus-data-find article)))
6833     ;; We read in the article if we have to.
6834     (and (not data)
6835          force
6836          (gnus-summary-insert-subject
6837           article
6838           (if (or (numberp force) (vectorp force)) force)
6839           t)
6840          (setq data (gnus-data-find article)))
6841     (goto-char b)
6842     (if (not data)
6843         (progn
6844           (unless silent
6845             (gnus-message 3 "Can't find article %d" article))
6846           nil)
6847       (let ((pt (gnus-data-pos data)))
6848         (goto-char pt)
6849         (gnus-summary-set-article-display-arrow pt))
6850       (gnus-summary-position-point)
6851       article)))
6852
6853 ;; Walking around summary lines with displaying articles.
6854
6855 (defun gnus-summary-expand-window (&optional arg)
6856   "Make the summary buffer take up the entire Emacs frame.
6857 Given a prefix, will force an `article' buffer configuration."
6858   (interactive "P")
6859   (if arg
6860       (gnus-configure-windows 'article 'force)
6861     (gnus-configure-windows 'summary 'force)))
6862
6863 (defun gnus-summary-display-article (article &optional all-header)
6864   "Display ARTICLE in article buffer."
6865   (when (gnus-buffer-live-p gnus-article-buffer)
6866     (with-current-buffer gnus-article-buffer
6867       (mm-enable-multibyte)))
6868   (gnus-set-global-variables)
6869   (when (gnus-buffer-live-p gnus-article-buffer)
6870     (with-current-buffer gnus-article-buffer
6871       (setq gnus-article-charset gnus-newsgroup-charset)
6872       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6873       (mm-enable-multibyte)))
6874   (if (null article)
6875       nil
6876     (prog1
6877         (if gnus-summary-display-article-function
6878             (funcall gnus-summary-display-article-function article all-header)
6879           (gnus-article-prepare article all-header))
6880       (gnus-run-hooks 'gnus-select-article-hook)
6881       (when (and gnus-current-article
6882                  (not (zerop gnus-current-article)))
6883         (gnus-summary-goto-subject gnus-current-article))
6884       (gnus-summary-recenter)
6885       (when (and gnus-use-trees gnus-show-threads)
6886         (gnus-possibly-generate-tree article)
6887         (gnus-highlight-selected-tree article))
6888       ;; Successfully display article.
6889       (gnus-article-set-window-start
6890        (cdr (assq article gnus-newsgroup-bookmarks))))))
6891
6892 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6893   "Select the current article.
6894 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6895 non-nil, the article will be re-fetched even if it already present in
6896 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6897 be displayed."
6898   ;; Make sure we are in the summary buffer to work around bbdb bug.
6899   (unless (eq major-mode 'gnus-summary-mode)
6900     (set-buffer gnus-summary-buffer))
6901   (let ((article (or article (gnus-summary-article-number)))
6902         (all-headers (not (not all-headers))) ;Must be t or nil.
6903         gnus-summary-display-article-function)
6904     (and (not pseudo)
6905          (gnus-summary-article-pseudo-p article)
6906          (error "This is a pseudo-article"))
6907     (save-excursion
6908       (set-buffer gnus-summary-buffer)
6909       (if (or (and gnus-single-article-buffer
6910                    (or (null gnus-current-article)
6911                        (null gnus-article-current)
6912                        (null (get-buffer gnus-article-buffer))
6913                        (not (eq article (cdr gnus-article-current)))
6914                        (not (equal (car gnus-article-current)
6915                                    gnus-newsgroup-name))))
6916               (and (not gnus-single-article-buffer)
6917                    (or (null gnus-current-article)
6918                        (not (eq gnus-current-article article))))
6919               force)
6920           ;; The requested article is different from the current article.
6921           (progn
6922             (gnus-summary-display-article article all-headers)
6923             (when (gnus-buffer-live-p gnus-article-buffer)
6924               (with-current-buffer gnus-article-buffer
6925                 (if (not gnus-article-decoded-p) ;; a local variable
6926                     (mm-disable-multibyte))))
6927             (gnus-article-set-window-start
6928              (cdr (assq article gnus-newsgroup-bookmarks)))
6929             article)
6930         'old))))
6931
6932 (defun gnus-summary-force-verify-and-decrypt ()
6933   "Display buttons for signed/encrypted parts and verify/decrypt them."
6934   (interactive)
6935   (let ((mm-verify-option 'known)
6936         (mm-decrypt-option 'known)
6937         (gnus-buttonized-mime-types (append (list "multipart/signed"
6938                                                   "multipart/encrypted")
6939                                             gnus-buttonized-mime-types)))
6940     (gnus-summary-select-article nil 'force)))
6941
6942 (defun gnus-summary-set-current-mark (&optional current-mark)
6943   "Obsolete function."
6944   nil)
6945
6946 (defun gnus-summary-next-article (&optional unread subject backward push)
6947   "Select the next article.
6948 If UNREAD, only unread articles are selected.
6949 If SUBJECT, only articles with SUBJECT are selected.
6950 If BACKWARD, the previous article is selected instead of the next."
6951   (interactive "P")
6952   (cond
6953    ;; Is there such an article?
6954    ((and (gnus-summary-search-forward unread subject backward)
6955          (or (gnus-summary-display-article (gnus-summary-article-number))
6956              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6957     (gnus-summary-position-point))
6958    ;; If not, we try the first unread, if that is wanted.
6959    ((and subject
6960          gnus-auto-select-same
6961          (gnus-summary-first-unread-article))
6962     (gnus-summary-position-point)
6963     (gnus-message 6 "Wrapped"))
6964    ;; Try to get next/previous article not displayed in this group.
6965    ((and gnus-auto-extend-newsgroup
6966          (not unread) (not subject))
6967     (gnus-summary-goto-article
6968      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6969      nil (count-lines (point-min) (point))))
6970    ;; Go to next/previous group.
6971    (t
6972     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6973       (gnus-summary-jump-to-group gnus-newsgroup-name))
6974     (let ((cmd last-command-char)
6975           (point
6976            (save-excursion
6977              (set-buffer gnus-group-buffer)
6978              (point)))
6979           (group
6980            (if (eq gnus-keep-same-level 'best)
6981                (gnus-summary-best-group gnus-newsgroup-name)
6982              (gnus-summary-search-group backward gnus-keep-same-level))))
6983       ;; For some reason, the group window gets selected.  We change
6984       ;; it back.
6985       (select-window (get-buffer-window (current-buffer)))
6986       ;; Select next unread newsgroup automagically.
6987       (cond
6988        ((or (not gnus-auto-select-next)
6989             (not cmd))
6990         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6991        ((or (eq gnus-auto-select-next 'quietly)
6992             (and (eq gnus-auto-select-next 'slightly-quietly)
6993                  push)
6994             (and (eq gnus-auto-select-next 'almost-quietly)
6995                  (gnus-summary-last-article-p)))
6996         ;; Select quietly.
6997         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6998             (gnus-summary-exit)
6999           (gnus-message 7 "No more%s articles (%s)..."
7000                         (if unread " unread" "")
7001                         (if group (concat "selecting " group)
7002                           "exiting"))
7003           (gnus-summary-next-group nil group backward)))
7004        (t
7005         (when (gnus-key-press-event-p last-input-event)
7006           (gnus-summary-walk-group-buffer
7007            gnus-newsgroup-name cmd unread backward point))))))))
7008
7009 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7010   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7011                       (?\C-p (gnus-group-prev-unread-group 1))))
7012         (cursor-in-echo-area t)
7013         keve key group ended prompt)
7014     (save-excursion
7015       (set-buffer gnus-group-buffer)
7016       (goto-char start)
7017       (setq group
7018             (if (eq gnus-keep-same-level 'best)
7019                 (gnus-summary-best-group gnus-newsgroup-name)
7020               (gnus-summary-search-group backward gnus-keep-same-level))))
7021     (while (not ended)
7022       (setq prompt
7023             (format
7024              "No more%s articles%s " (if unread " unread" "")
7025              (if (and group
7026                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7027                  (format " (Type %s for %s [%s])"
7028                          (single-key-description cmd) group
7029                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7030                (format " (Type %s to exit %s)"
7031                        (single-key-description cmd)
7032                        gnus-newsgroup-name))))
7033       ;; Confirm auto selection.
7034       (setq key (car (setq keve (gnus-read-event-char prompt)))
7035             ended t)
7036       (cond
7037        ((assq key keystrokes)
7038         (let ((obuf (current-buffer)))
7039           (switch-to-buffer gnus-group-buffer)
7040           (when group
7041             (gnus-group-jump-to-group group))
7042           (eval (cadr (assq key keystrokes)))
7043           (setq group (gnus-group-group-name))
7044           (switch-to-buffer obuf))
7045         (setq ended nil))
7046        ((equal key cmd)
7047         (if (or (not group)
7048                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7049             (gnus-summary-exit)
7050           (gnus-summary-next-group nil group backward)))
7051        (t
7052         (push (cdr keve) unread-command-events))))))
7053
7054 (defun gnus-summary-next-unread-article ()
7055   "Select unread article after current one."
7056   (interactive)
7057   (gnus-summary-next-article
7058    (or (not (eq gnus-summary-goto-unread 'never))
7059        (gnus-summary-last-article-p (gnus-summary-article-number)))
7060    (and gnus-auto-select-same
7061         (gnus-summary-article-subject))))
7062
7063 (defun gnus-summary-prev-article (&optional unread subject)
7064   "Select the article after the current one.
7065 If UNREAD is non-nil, only unread articles are selected."
7066   (interactive "P")
7067   (gnus-summary-next-article unread subject t))
7068
7069 (defun gnus-summary-prev-unread-article ()
7070   "Select unread article before current one."
7071   (interactive)
7072   (gnus-summary-prev-article
7073    (or (not (eq gnus-summary-goto-unread 'never))
7074        (gnus-summary-first-article-p (gnus-summary-article-number)))
7075    (and gnus-auto-select-same
7076         (gnus-summary-article-subject))))
7077
7078 (defun gnus-summary-next-page (&optional lines circular stop)
7079   "Show next page of the selected article.
7080 If at the end of the current article, select the next article.
7081 LINES says how many lines should be scrolled up.
7082
7083 If CIRCULAR is non-nil, go to the start of the article instead of
7084 selecting the next article when reaching the end of the current
7085 article.
7086
7087 If STOP is non-nil, just stop when reaching the end of the message."
7088   (interactive "P")
7089   (setq gnus-summary-buffer (current-buffer))
7090   (gnus-set-global-variables)
7091   (let ((article (gnus-summary-article-number))
7092         (article-window (get-buffer-window gnus-article-buffer t))
7093         endp)
7094     ;; If the buffer is empty, we have no article.
7095     (unless article
7096       (error "No article to select"))
7097     (gnus-configure-windows 'article)
7098     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7099         (if (and (eq gnus-summary-goto-unread 'never)
7100                  (not (gnus-summary-last-article-p article)))
7101             (gnus-summary-next-article)
7102           (gnus-summary-next-unread-article))
7103       (if (or (null gnus-current-article)
7104               (null gnus-article-current)
7105               (/= article (cdr gnus-article-current))
7106               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7107           ;; Selected subject is different from current article's.
7108           (gnus-summary-display-article article)
7109         (when article-window
7110           (gnus-eval-in-buffer-window gnus-article-buffer
7111             (setq endp (or (gnus-article-next-page lines)
7112                            (gnus-article-only-boring-p))))
7113           (when endp
7114             (cond (stop
7115                    (gnus-message 3 "End of message"))
7116                   (circular
7117                    (gnus-summary-beginning-of-article))
7118                   (lines
7119                    (gnus-message 3 "End of message"))
7120                   ((null lines)
7121                    (if (and (eq gnus-summary-goto-unread 'never)
7122                             (not (gnus-summary-last-article-p article)))
7123                        (gnus-summary-next-article)
7124                      (gnus-summary-next-unread-article))))))))
7125     (gnus-summary-recenter)
7126     (gnus-summary-position-point)))
7127
7128 (defun gnus-summary-prev-page (&optional lines move)
7129   "Show previous page of selected article.
7130 Argument LINES specifies lines to be scrolled down.
7131 If MOVE, move to the previous unread article if point is at
7132 the beginning of the buffer."
7133   (interactive "P")
7134   (let ((article (gnus-summary-article-number))
7135         (article-window (get-buffer-window gnus-article-buffer t))
7136         endp)
7137     (gnus-configure-windows 'article)
7138     (if (or (null gnus-current-article)
7139             (null gnus-article-current)
7140             (/= article (cdr gnus-article-current))
7141             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7142         ;; Selected subject is different from current article's.
7143         (gnus-summary-display-article article)
7144       (gnus-summary-recenter)
7145       (when article-window
7146         (gnus-eval-in-buffer-window gnus-article-buffer
7147           (setq endp (gnus-article-prev-page lines)))
7148         (when (and move endp)
7149           (cond (lines
7150                  (gnus-message 3 "Beginning of message"))
7151                 ((null lines)
7152                  (if (and (eq gnus-summary-goto-unread 'never)
7153                           (not (gnus-summary-first-article-p article)))
7154                      (gnus-summary-prev-article)
7155                    (gnus-summary-prev-unread-article))))))))
7156   (gnus-summary-position-point))
7157
7158 (defun gnus-summary-prev-page-or-article (&optional lines)
7159   "Show previous page of selected article.
7160 Argument LINES specifies lines to be scrolled down.
7161 If at the beginning of the article, go to the next article."
7162   (interactive "P")
7163   (gnus-summary-prev-page lines t))
7164
7165 (defun gnus-summary-scroll-up (lines)
7166   "Scroll up (or down) one line current article.
7167 Argument LINES specifies lines to be scrolled up (or down if negative)."
7168   (interactive "p")
7169   (gnus-configure-windows 'article)
7170   (gnus-summary-show-thread)
7171   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7172     (gnus-eval-in-buffer-window gnus-article-buffer
7173       (cond ((> lines 0)
7174              (when (gnus-article-next-page lines)
7175                (gnus-message 3 "End of message")))
7176             ((< lines 0)
7177              (gnus-article-prev-page (- lines))))))
7178   (gnus-summary-recenter)
7179   (gnus-summary-position-point))
7180
7181 (defun gnus-summary-scroll-down (lines)
7182   "Scroll down (or up) one line current article.
7183 Argument LINES specifies lines to be scrolled down (or up if negative)."
7184   (interactive "p")
7185   (gnus-summary-scroll-up (- lines)))
7186
7187 (defun gnus-summary-next-same-subject ()
7188   "Select next article which has the same subject as current one."
7189   (interactive)
7190   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7191
7192 (defun gnus-summary-prev-same-subject ()
7193   "Select previous article which has the same subject as current one."
7194   (interactive)
7195   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7196
7197 (defun gnus-summary-next-unread-same-subject ()
7198   "Select next unread article which has the same subject as current one."
7199   (interactive)
7200   (gnus-summary-next-article t (gnus-summary-article-subject)))
7201
7202 (defun gnus-summary-prev-unread-same-subject ()
7203   "Select previous unread article which has the same subject as current one."
7204   (interactive)
7205   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7206
7207 (defun gnus-summary-first-unread-article ()
7208   "Select the first unread article.
7209 Return nil if there are no unread articles."
7210   (interactive)
7211   (prog1
7212       (when (gnus-summary-first-subject t)
7213         (gnus-summary-show-thread)
7214         (gnus-summary-first-subject t)
7215         (gnus-summary-display-article (gnus-summary-article-number)))
7216     (gnus-summary-position-point)))
7217
7218 (defun gnus-summary-first-unread-subject ()
7219   "Place the point on the subject line of the first unread article.
7220 Return nil if there are no unread articles."
7221   (interactive)
7222   (prog1
7223       (when (gnus-summary-first-subject t)
7224         (gnus-summary-show-thread)
7225         (gnus-summary-first-subject t))
7226     (gnus-summary-position-point)))
7227
7228 (defun gnus-summary-first-unseen-subject ()
7229   "Place the point on the subject line of the first unseen article.
7230 Return nil if there are no unseen articles."
7231   (interactive)
7232   (prog1
7233       (when (gnus-summary-first-subject nil nil t)
7234         (gnus-summary-show-thread)
7235         (gnus-summary-first-subject nil nil t))
7236     (gnus-summary-position-point)))
7237
7238 (defun gnus-summary-first-unseen-or-unread-subject ()
7239   "Place the point on the subject line of the first unseen article or,
7240 if all article have been seen, on the subject line of the first unread
7241 article."
7242   (interactive)
7243   (prog1
7244       (unless (when (gnus-summary-first-subject nil nil t)
7245                 (gnus-summary-show-thread)
7246                 (gnus-summary-first-subject nil nil t))
7247         (when (gnus-summary-first-subject t)
7248           (gnus-summary-show-thread)
7249           (gnus-summary-first-subject t)))
7250     (gnus-summary-position-point)))
7251
7252 (defun gnus-summary-first-article ()
7253   "Select the first article.
7254 Return nil if there are no articles."
7255   (interactive)
7256   (prog1
7257       (when (gnus-summary-first-subject)
7258         (gnus-summary-show-thread)
7259         (gnus-summary-first-subject)
7260         (gnus-summary-display-article (gnus-summary-article-number)))
7261     (gnus-summary-position-point)))
7262
7263 (defun gnus-summary-best-unread-article (&optional arg)
7264   "Select the unread article with the highest score.
7265 If given a prefix argument, select the next unread article that has a
7266 score higher than the default score."
7267   (interactive "P")
7268   (let ((article (if arg
7269                      (gnus-summary-better-unread-subject)
7270                    (gnus-summary-best-unread-subject))))
7271     (if article
7272         (gnus-summary-goto-article article)
7273       (error "No unread articles"))))
7274
7275 (defun gnus-summary-best-unread-subject ()
7276   "Select the unread subject with the highest score."
7277   (interactive)
7278   (let ((best -1000000)
7279         (data gnus-newsgroup-data)
7280         article score)
7281     (while data
7282       (and (gnus-data-unread-p (car data))
7283            (> (setq score
7284                     (gnus-summary-article-score (gnus-data-number (car data))))
7285               best)
7286            (setq best score
7287                  article (gnus-data-number (car data))))
7288       (setq data (cdr data)))
7289     (when article
7290       (gnus-summary-goto-subject article))
7291     (gnus-summary-position-point)
7292     article))
7293
7294 (defun gnus-summary-better-unread-subject ()
7295   "Select the first unread subject that has a score over the default score."
7296   (interactive)
7297   (let ((data gnus-newsgroup-data)
7298         article score)
7299     (while (and (setq article (gnus-data-number (car data)))
7300                 (or (gnus-data-read-p (car data))
7301                     (not (> (gnus-summary-article-score article)
7302                             gnus-summary-default-score))))
7303       (setq data (cdr data)))
7304     (when article
7305       (gnus-summary-goto-subject article))
7306     (gnus-summary-position-point)
7307     article))
7308
7309 (defun gnus-summary-last-subject ()
7310   "Go to the last displayed subject line in the group."
7311   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7312     (when article
7313       (gnus-summary-goto-subject article))))
7314
7315 (defun gnus-summary-goto-article (article &optional all-headers force)
7316   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7317 If ALL-HEADERS is non-nil, no header lines are hidden.
7318 If FORCE, go to the article even if it isn't displayed.  If FORCE
7319 is a number, it is the line the article is to be displayed on."
7320   (interactive
7321    (list
7322     (completing-read
7323      "Article number or Message-ID: "
7324      (mapcar (lambda (number) (list (int-to-string number)))
7325              gnus-newsgroup-limit))
7326     current-prefix-arg
7327     t))
7328   (prog1
7329       (if (and (stringp article)
7330                (string-match "@" article))
7331           (gnus-summary-refer-article article)
7332         (when (stringp article)
7333           (setq article (string-to-number article)))
7334         (if (gnus-summary-goto-subject article force)
7335             (gnus-summary-display-article article all-headers)
7336           (gnus-message 4 "Couldn't go to article %s" article) nil))
7337     (gnus-summary-position-point)))
7338
7339 (defun gnus-summary-goto-last-article ()
7340   "Go to the previously read article."
7341   (interactive)
7342   (prog1
7343       (when gnus-last-article
7344         (gnus-summary-goto-article gnus-last-article nil t))
7345     (gnus-summary-position-point)))
7346
7347 (defun gnus-summary-pop-article (number)
7348   "Pop one article off the history and go to the previous.
7349 NUMBER articles will be popped off."
7350   (interactive "p")
7351   (let (to)
7352     (setq gnus-newsgroup-history
7353           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7354     (if to
7355         (gnus-summary-goto-article (car to) nil t)
7356       (error "Article history empty")))
7357   (gnus-summary-position-point))
7358
7359 ;; Summary commands and functions for limiting the summary buffer.
7360
7361 (defun gnus-summary-limit-to-articles (n)
7362   "Limit the summary buffer to the next N articles.
7363 If not given a prefix, use the process marked articles instead."
7364   (interactive "P")
7365   (prog1
7366       (let ((articles (gnus-summary-work-articles n)))
7367         (setq gnus-newsgroup-processable nil)
7368         (gnus-summary-limit articles))
7369     (gnus-summary-position-point)))
7370
7371 (defun gnus-summary-pop-limit (&optional total)
7372   "Restore the previous limit.
7373 If given a prefix, remove all limits."
7374   (interactive "P")
7375   (when total
7376     (setq gnus-newsgroup-limits
7377           (list (mapcar (lambda (h) (mail-header-number h))
7378                         gnus-newsgroup-headers))))
7379   (unless gnus-newsgroup-limits
7380     (error "No limit to pop"))
7381   (prog1
7382       (gnus-summary-limit nil 'pop)
7383     (gnus-summary-position-point)))
7384
7385 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7386   "Limit the summary buffer to articles that have subjects that match a regexp.
7387 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7388   (interactive
7389    (list (read-string (if current-prefix-arg
7390                           "Exclude subject (regexp): "
7391                         "Limit to subject (regexp): "))
7392          nil current-prefix-arg))
7393   (unless header
7394     (setq header "subject"))
7395   (when (not (equal "" subject))
7396     (prog1
7397         (let ((articles (gnus-summary-find-matching
7398                          (or header "subject") subject 'all nil nil
7399                          not-matching)))
7400           (unless articles
7401             (error "Found no matches for \"%s\"" subject))
7402           (gnus-summary-limit articles))
7403       (gnus-summary-position-point))))
7404
7405 (defun gnus-summary-limit-to-author (from &optional not-matching)
7406   "Limit the summary buffer to articles that have authors that match a regexp.
7407 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7408   (interactive
7409    (list (read-string (if current-prefix-arg
7410                           "Exclude author (regexp): "
7411                         "Limit to author (regexp): "))
7412          current-prefix-arg))
7413   (gnus-summary-limit-to-subject from "from" not-matching))
7414
7415 (defun gnus-summary-limit-to-age (age &optional younger-p)
7416   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7417 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7418 articles that are younger than AGE days."
7419   (interactive
7420    (let ((younger current-prefix-arg)
7421          (days-got nil)
7422          days)
7423      (while (not days-got)
7424        (setq days (if younger
7425                       (read-string "Limit to articles younger than (in days, older when negative): ")
7426                     (read-string
7427                      "Limit to articles older than (in days, younger when negative): ")))
7428        (when (> (length days) 0)
7429          (setq days (read days)))
7430        (if (numberp days)
7431            (progn
7432              (setq days-got t)
7433              (if (< days 0)
7434                  (progn
7435                    (setq younger (not younger))
7436                    (setq days (* days -1)))))
7437          (message "Please enter a number.")
7438          (sleep-for 1)))
7439      (list days younger)))
7440   (prog1
7441       (let ((data gnus-newsgroup-data)
7442             (cutoff (days-to-time age))
7443             articles d date is-younger)
7444         (while (setq d (pop data))
7445           (when (and (vectorp (gnus-data-header d))
7446                      (setq date (mail-header-date (gnus-data-header d))))
7447             (setq is-younger (time-less-p
7448                               (time-since (condition-case ()
7449                                               (date-to-time date)
7450                                             (error '(0 0))))
7451                               cutoff))
7452             (when (if younger-p
7453                       is-younger
7454                     (not is-younger))
7455               (push (gnus-data-number d) articles))))
7456         (gnus-summary-limit (nreverse articles)))
7457     (gnus-summary-position-point)))
7458
7459 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7460   "Limit the summary buffer to articles that match an 'extra' header."
7461   (interactive
7462    (let ((header
7463           (intern
7464            (gnus-completing-read-with-default
7465             (symbol-name (car gnus-extra-headers))
7466             (if current-prefix-arg
7467                 "Exclude extra header:"
7468               "Limit extra header:")
7469             (mapcar (lambda (x)
7470                       (cons (symbol-name x) x))
7471                     gnus-extra-headers)
7472             nil
7473             t))))
7474      (list header
7475            (read-string (format "%s header %s (regexp): "
7476                                 (if current-prefix-arg "Exclude" "Limit to")
7477                                 header))
7478            current-prefix-arg)))
7479   (when (not (equal "" regexp))
7480     (prog1
7481         (let ((articles (gnus-summary-find-matching
7482                          (cons 'extra header) regexp 'all nil nil
7483                          not-matching)))
7484           (unless articles
7485             (error "Found no matches for \"%s\"" regexp))
7486           (gnus-summary-limit articles))
7487       (gnus-summary-position-point))))
7488
7489 (defun gnus-summary-limit-to-display-predicate ()
7490   "Limit the summary buffer to the predicated in the `display' group parameter."
7491   (interactive)
7492   (unless gnus-newsgroup-display
7493     (error "There is no `display' group parameter"))
7494   (let (articles)
7495     (dolist (number gnus-newsgroup-articles)
7496       (when (funcall gnus-newsgroup-display)
7497         (push number articles)))
7498     (gnus-summary-limit articles))
7499   (gnus-summary-position-point))
7500
7501 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7502 (make-obsolete
7503  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7504
7505 (defun gnus-summary-limit-to-unread (&optional all)
7506   "Limit the summary buffer to articles that are not marked as read.
7507 If ALL is non-nil, limit strictly to unread articles."
7508   (interactive "P")
7509   (if all
7510       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7511     (gnus-summary-limit-to-marks
7512      ;; Concat all the marks that say that an article is read and have
7513      ;; those removed.
7514      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7515            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7516            gnus-low-score-mark gnus-expirable-mark
7517            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7518            gnus-duplicate-mark gnus-souped-mark)
7519      'reverse)))
7520
7521 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7522 (make-obsolete 'gnus-summary-delete-marked-with
7523                'gnus-summary-limit-exclude-marks)
7524
7525 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7526   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7527 If REVERSE, limit the summary buffer to articles that are marked
7528 with MARKS.  MARKS can either be a string of marks or a list of marks.
7529 Returns how many articles were removed."
7530   (interactive "sMarks: ")
7531   (gnus-summary-limit-to-marks marks t))
7532
7533 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7534   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7535 If REVERSE (the prefix), limit the summary buffer to articles that are
7536 not marked with MARKS.  MARKS can either be a string of marks or a
7537 list of marks.
7538 Returns how many articles were removed."
7539   (interactive "sMarks: \nP")
7540   (prog1
7541       (let ((data gnus-newsgroup-data)
7542             (marks (if (listp marks) marks
7543                      (append marks nil))) ; Transform to list.
7544             articles)
7545         (while data
7546           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7547                   (memq (gnus-data-mark (car data)) marks))
7548             (push (gnus-data-number (car data)) articles))
7549           (setq data (cdr data)))
7550         (gnus-summary-limit articles))
7551     (gnus-summary-position-point)))
7552
7553 (defun gnus-summary-limit-to-score (score)
7554   "Limit to articles with score at or above SCORE."
7555   (interactive "NLimit to articles with score of at least: ")
7556   (let ((data gnus-newsgroup-data)
7557         articles)
7558     (while data
7559       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7560                 score)
7561         (push (gnus-data-number (car data)) articles))
7562       (setq data (cdr data)))
7563     (prog1
7564         (gnus-summary-limit articles)
7565       (gnus-summary-position-point))))
7566
7567 (defun gnus-summary-limit-to-unseen ()
7568   "Limit to unseen articles."
7569   (interactive)
7570   (prog1
7571       (gnus-summary-limit gnus-newsgroup-unseen)
7572     (gnus-summary-position-point)))
7573
7574 (defun gnus-summary-limit-include-thread (id)
7575   "Display all the hidden articles that is in the thread with ID in it.
7576 When called interactively, ID is the Message-ID of the current
7577 article."
7578   (interactive (list (mail-header-id (gnus-summary-article-header))))
7579   (let ((articles (gnus-articles-in-thread
7580                    (gnus-id-to-thread (gnus-root-id id)))))
7581     (prog1
7582         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7583       (gnus-summary-limit-include-matching-articles
7584        "subject"
7585        (regexp-quote (gnus-simplify-subject-re
7586                       (mail-header-subject (gnus-id-to-header id)))))
7587       (gnus-summary-position-point))))
7588
7589 (defun gnus-summary-limit-include-matching-articles (header regexp)
7590   "Display all the hidden articles that have HEADERs that match REGEXP."
7591   (interactive (list (read-string "Match on header: ")
7592                      (read-string "Regexp: ")))
7593   (let ((articles (gnus-find-matching-articles header regexp)))
7594     (prog1
7595         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7596       (gnus-summary-position-point))))
7597
7598 (defun gnus-summary-insert-dormant-articles ()
7599   "Insert all the dormat articles for this group into the current buffer."
7600   (interactive)
7601   (let ((gnus-verbose (max 6 gnus-verbose)))
7602     (if (not gnus-newsgroup-dormant)
7603         (gnus-message 3 "No cached articles for this group")
7604       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7605
7606 (defun gnus-summary-limit-include-dormant ()
7607   "Display all the hidden articles that are marked as dormant.
7608 Note that this command only works on a subset of the articles currently
7609 fetched for this group."
7610   (interactive)
7611   (unless gnus-newsgroup-dormant
7612     (error "There are no dormant articles in this group"))
7613   (prog1
7614       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7615     (gnus-summary-position-point)))
7616
7617 (defun gnus-summary-limit-exclude-dormant ()
7618   "Hide all dormant articles."
7619   (interactive)
7620   (prog1
7621       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7622     (gnus-summary-position-point)))
7623
7624 (defun gnus-summary-limit-exclude-childless-dormant ()
7625   "Hide all dormant articles that have no children."
7626   (interactive)
7627   (let ((data (gnus-data-list t))
7628         articles d children)
7629     ;; Find all articles that are either not dormant or have
7630     ;; children.
7631     (while (setq d (pop data))
7632       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7633                 (and (setq children
7634                            (gnus-article-children (gnus-data-number d)))
7635                      (let (found)
7636                        (while children
7637                          (when (memq (car children) articles)
7638                            (setq children nil
7639                                  found t))
7640                          (pop children))
7641                        found)))
7642         (push (gnus-data-number d) articles)))
7643     ;; Do the limiting.
7644     (prog1
7645         (gnus-summary-limit articles)
7646       (gnus-summary-position-point))))
7647
7648 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7649   "Mark all unread excluded articles as read.
7650 If ALL, mark even excluded ticked and dormants as read."
7651   (interactive "P")
7652   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7653   (let ((articles (gnus-sorted-ndifference
7654                    (sort
7655                     (mapcar (lambda (h) (mail-header-number h))
7656                             gnus-newsgroup-headers)
7657                     '<)
7658                    gnus-newsgroup-limit))
7659         article)
7660     (setq gnus-newsgroup-unreads
7661           (gnus-sorted-intersection gnus-newsgroup-unreads
7662                                     gnus-newsgroup-limit))
7663     (if all
7664         (setq gnus-newsgroup-dormant nil
7665               gnus-newsgroup-marked nil
7666               gnus-newsgroup-reads
7667               (nconc
7668                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7669                gnus-newsgroup-reads))
7670       (while (setq article (pop articles))
7671         (unless (or (memq article gnus-newsgroup-dormant)
7672                     (memq article gnus-newsgroup-marked))
7673           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7674
7675 (defun gnus-summary-limit (articles &optional pop)
7676   (if pop
7677       ;; We pop the previous limit off the stack and use that.
7678       (setq articles (car gnus-newsgroup-limits)
7679             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7680     ;; We use the new limit, so we push the old limit on the stack.
7681     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7682   ;; Set the limit.
7683   (setq gnus-newsgroup-limit articles)
7684   (let ((total (length gnus-newsgroup-data))
7685         (data (gnus-data-find-list (gnus-summary-article-number)))
7686         (gnus-summary-mark-below nil)   ; Inhibit this.
7687         found)
7688     ;; This will do all the work of generating the new summary buffer
7689     ;; according to the new limit.
7690     (gnus-summary-prepare)
7691     ;; Hide any threads, possibly.
7692     (gnus-summary-maybe-hide-threads)
7693     ;; Try to return to the article you were at, or one in the
7694     ;; neighborhood.
7695     (when data
7696       ;; We try to find some article after the current one.
7697       (while data
7698         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7699           (setq data nil
7700                 found t))
7701         (setq data (cdr data))))
7702     (unless found
7703       ;; If there is no data, that means that we were after the last
7704       ;; article.  The same goes when we can't find any articles
7705       ;; after the current one.
7706       (goto-char (point-max))
7707       (gnus-summary-find-prev))
7708     (gnus-set-mode-line 'summary)
7709     ;; We return how many articles were removed from the summary
7710     ;; buffer as a result of the new limit.
7711     (- total (length gnus-newsgroup-data))))
7712
7713 (defsubst gnus-invisible-cut-children (threads)
7714   (let ((num 0))
7715     (while threads
7716       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7717         (incf num))
7718       (pop threads))
7719     (< num 2)))
7720
7721 (defsubst gnus-cut-thread (thread)
7722   "Go forwards in the thread until we find an article that we want to display."
7723   (when (or (eq gnus-fetch-old-headers 'some)
7724             (eq gnus-fetch-old-headers 'invisible)
7725             (numberp gnus-fetch-old-headers)
7726             (eq gnus-build-sparse-threads 'some)
7727             (eq gnus-build-sparse-threads 'more))
7728     ;; Deal with old-fetched headers and sparse threads.
7729     (while (and
7730             thread
7731             (or
7732              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7733              (gnus-summary-article-ancient-p
7734               (mail-header-number (car thread))))
7735             (if (or (<= (length (cdr thread)) 1)
7736                     (eq gnus-fetch-old-headers 'invisible))
7737                 (setq gnus-newsgroup-limit
7738                       (delq (mail-header-number (car thread))
7739                             gnus-newsgroup-limit)
7740                       thread (cadr thread))
7741               (when (gnus-invisible-cut-children (cdr thread))
7742                 (let ((th (cdr thread)))
7743                   (while th
7744                     (if (memq (mail-header-number (caar th))
7745                               gnus-newsgroup-limit)
7746                         (setq thread (car th)
7747                               th nil)
7748                       (setq th (cdr th))))))))))
7749   thread)
7750
7751 (defun gnus-cut-threads (threads)
7752   "Cut off all uninteresting articles from the beginning of threads."
7753   (when (or (eq gnus-fetch-old-headers 'some)
7754             (eq gnus-fetch-old-headers 'invisible)
7755             (numberp gnus-fetch-old-headers)
7756             (eq gnus-build-sparse-threads 'some)
7757             (eq gnus-build-sparse-threads 'more))
7758     (let ((th threads))
7759       (while th
7760         (setcar th (gnus-cut-thread (car th)))
7761         (setq th (cdr th)))))
7762   ;; Remove nixed out threads.
7763   (delq nil threads))
7764
7765 (defun gnus-summary-initial-limit (&optional show-if-empty)
7766   "Figure out what the initial limit is supposed to be on group entry.
7767 This entails weeding out unwanted dormants, low-scored articles,
7768 fetch-old-headers verbiage, and so on."
7769   ;; Most groups have nothing to remove.
7770   (if (or gnus-inhibit-limiting
7771           (and (null gnus-newsgroup-dormant)
7772                (eq gnus-newsgroup-display 'gnus-not-ignore)
7773                (not (eq gnus-fetch-old-headers 'some))
7774                (not (numberp gnus-fetch-old-headers))
7775                (not (eq gnus-fetch-old-headers 'invisible))
7776                (null gnus-summary-expunge-below)
7777                (not (eq gnus-build-sparse-threads 'some))
7778                (not (eq gnus-build-sparse-threads 'more))
7779                (null gnus-thread-expunge-below)
7780                (not gnus-use-nocem)))
7781       ()                                ; Do nothing.
7782     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7783     (setq gnus-newsgroup-limit nil)
7784     (mapatoms
7785      (lambda (node)
7786        (unless (car (symbol-value node))
7787          ;; These threads have no parents -- they are roots.
7788          (let ((nodes (cdr (symbol-value node)))
7789                thread)
7790            (while nodes
7791              (if (and gnus-thread-expunge-below
7792                       (< (gnus-thread-total-score (car nodes))
7793                          gnus-thread-expunge-below))
7794                  (gnus-expunge-thread (pop nodes))
7795                (setq thread (pop nodes))
7796                (gnus-summary-limit-children thread))))))
7797      gnus-newsgroup-dependencies)
7798     ;; If this limitation resulted in an empty group, we might
7799     ;; pop the previous limit and use it instead.
7800     (when (and (not gnus-newsgroup-limit)
7801                show-if-empty)
7802       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7803     gnus-newsgroup-limit))
7804
7805 (defun gnus-summary-limit-children (thread)
7806   "Return 1 if this subthread is visible and 0 if it is not."
7807   ;; First we get the number of visible children to this thread.  This
7808   ;; is done by recursing down the thread using this function, so this
7809   ;; will really go down to a leaf article first, before slowly
7810   ;; working its way up towards the root.
7811   (when thread
7812     (let* ((max-lisp-eval-depth 5000)
7813            (children
7814            (if (cdr thread)
7815                (apply '+ (mapcar 'gnus-summary-limit-children
7816                                  (cdr thread)))
7817              0))
7818           (number (mail-header-number (car thread)))
7819           score)
7820       (if (and
7821            (not (memq number gnus-newsgroup-marked))
7822            (or
7823             ;; If this article is dormant and has absolutely no visible
7824             ;; children, then this article isn't visible.
7825             (and (memq number gnus-newsgroup-dormant)
7826                  (zerop children))
7827             ;; If this is "fetch-old-headered" and there is no
7828             ;; visible children, then we don't want this article.
7829             (and (or (eq gnus-fetch-old-headers 'some)
7830                      (numberp gnus-fetch-old-headers))
7831                  (gnus-summary-article-ancient-p number)
7832                  (zerop children))
7833             ;; If this is "fetch-old-headered" and `invisible', then
7834             ;; we don't want this article.
7835             (and (eq gnus-fetch-old-headers 'invisible)
7836                  (gnus-summary-article-ancient-p number))
7837             ;; If this is a sparsely inserted article with no children,
7838             ;; we don't want it.
7839             (and (eq gnus-build-sparse-threads 'some)
7840                  (gnus-summary-article-sparse-p number)
7841                  (zerop children))
7842             ;; If we use expunging, and this article is really
7843             ;; low-scored, then we don't want this article.
7844             (when (and gnus-summary-expunge-below
7845                        (< (setq score
7846                                 (or (cdr (assq number gnus-newsgroup-scored))
7847                                     gnus-summary-default-score))
7848                           gnus-summary-expunge-below))
7849               ;; We increase the expunge-tally here, but that has
7850               ;; nothing to do with the limits, really.
7851               (incf gnus-newsgroup-expunged-tally)
7852               ;; We also mark as read here, if that's wanted.
7853               (when (and gnus-summary-mark-below
7854                          (< score gnus-summary-mark-below))
7855                 (setq gnus-newsgroup-unreads
7856                       (delq number gnus-newsgroup-unreads))
7857                 (if gnus-newsgroup-auto-expire
7858                     (push number gnus-newsgroup-expirable)
7859                   (push (cons number gnus-low-score-mark)
7860                         gnus-newsgroup-reads)))
7861               t)
7862             ;; Do the `display' group parameter.
7863             (and gnus-newsgroup-display
7864                  (not (funcall gnus-newsgroup-display)))
7865             ;; Check NoCeM things.
7866             (if (and gnus-use-nocem
7867                      (gnus-nocem-unwanted-article-p
7868                       (mail-header-id (car thread))))
7869                 (progn
7870                   (setq gnus-newsgroup-unreads
7871                         (delq number gnus-newsgroup-unreads))
7872                   t))))
7873           ;; Nope, invisible article.
7874           0
7875         ;; Ok, this article is to be visible, so we add it to the limit
7876         ;; and return 1.
7877         (push number gnus-newsgroup-limit)
7878         1))))
7879
7880 (defun gnus-expunge-thread (thread)
7881   "Mark all articles in THREAD as read."
7882   (let* ((number (mail-header-number (car thread))))
7883     (incf gnus-newsgroup-expunged-tally)
7884     ;; We also mark as read here, if that's wanted.
7885     (setq gnus-newsgroup-unreads
7886           (delq number gnus-newsgroup-unreads))
7887     (if gnus-newsgroup-auto-expire
7888         (push number gnus-newsgroup-expirable)
7889       (push (cons number gnus-low-score-mark)
7890             gnus-newsgroup-reads)))
7891   ;; Go recursively through all subthreads.
7892   (mapcar 'gnus-expunge-thread (cdr thread)))
7893
7894 ;; Summary article oriented commands
7895
7896 (defun gnus-summary-refer-parent-article (n)
7897   "Refer parent article N times.
7898 If N is negative, go to ancestor -N instead.
7899 The difference between N and the number of articles fetched is returned."
7900   (interactive "p")
7901   (let ((skip 1)
7902         error header ref)
7903     (when (not (natnump n))
7904       (setq skip (abs n)
7905             n 1))
7906     (while (and (> n 0)
7907                 (not error))
7908       (setq header (gnus-summary-article-header))
7909       (if (and (eq (mail-header-number header)
7910                    (cdr gnus-article-current))
7911                (equal gnus-newsgroup-name
7912                       (car gnus-article-current)))
7913           ;; If we try to find the parent of the currently
7914           ;; displayed article, then we take a look at the actual
7915           ;; References header, since this is slightly more
7916           ;; reliable than the References field we got from the
7917           ;; server.
7918           (save-excursion
7919             (set-buffer gnus-original-article-buffer)
7920             (nnheader-narrow-to-headers)
7921             (unless (setq ref (message-fetch-field "references"))
7922               (setq ref (message-fetch-field "in-reply-to")))
7923             (widen))
7924         (setq ref
7925               ;; It's not the current article, so we take a bet on
7926               ;; the value we got from the server.
7927               (mail-header-references header)))
7928       (if (and ref
7929                (not (equal ref "")))
7930           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7931             (gnus-message 1 "Couldn't find parent"))
7932         (gnus-message 1 "No references in article %d"
7933                       (gnus-summary-article-number))
7934         (setq error t))
7935       (decf n))
7936     (gnus-summary-position-point)
7937     n))
7938
7939 (defun gnus-summary-refer-references ()
7940   "Fetch all articles mentioned in the References header.
7941 Return the number of articles fetched."
7942   (interactive)
7943   (let ((ref (mail-header-references (gnus-summary-article-header)))
7944         (current (gnus-summary-article-number))
7945         (n 0))
7946     (if (or (not ref)
7947             (equal ref ""))
7948         (error "No References in the current article")
7949       ;; For each Message-ID in the References header...
7950       (while (string-match "<[^>]*>" ref)
7951         (incf n)
7952         ;; ... fetch that article.
7953         (gnus-summary-refer-article
7954          (prog1 (match-string 0 ref)
7955            (setq ref (substring ref (match-end 0))))))
7956       (gnus-summary-goto-subject current)
7957       (gnus-summary-position-point)
7958       n)))
7959
7960 (defun gnus-summary-refer-thread (&optional limit)
7961   "Fetch all articles in the current thread.
7962 If LIMIT (the numerical prefix), fetch that many old headers instead
7963 of what's specified by the `gnus-refer-thread-limit' variable."
7964   (interactive "P")
7965   (let ((id (mail-header-id (gnus-summary-article-header)))
7966         (limit (if limit (prefix-numeric-value limit)
7967                  gnus-refer-thread-limit)))
7968     (unless (eq gnus-fetch-old-headers 'invisible)
7969       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7970       ;; Retrieve the headers and read them in.
7971       (if (eq (if (numberp limit)
7972                   (gnus-retrieve-headers
7973                    (list (min
7974                           (+ (mail-header-number
7975                               (gnus-summary-article-header))
7976                              limit)
7977                           gnus-newsgroup-end))
7978                    gnus-newsgroup-name (* limit 2))
7979                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
7980                 ;; headers.
7981                 (gnus-retrieve-headers (list gnus-newsgroup-end)
7982                                        gnus-newsgroup-name limit))
7983               'nov)
7984           (gnus-build-all-threads)
7985         (error "Can't fetch thread from backends that don't support NOV"))
7986       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7987     (gnus-summary-limit-include-thread id)))
7988
7989 (defun gnus-summary-refer-article (message-id)
7990   "Fetch an article specified by MESSAGE-ID."
7991   (interactive "sMessage-ID: ")
7992   (when (and (stringp message-id)
7993              (not (zerop (length message-id))))
7994     ;; Construct the correct Message-ID if necessary.
7995     ;; Suggested by tale@pawl.rpi.edu.
7996     (unless (string-match "^<" message-id)
7997       (setq message-id (concat "<" message-id)))
7998     (unless (string-match ">$" message-id)
7999       (setq message-id (concat message-id ">")))
8000     (let* ((header (gnus-id-to-header message-id))
8001            (sparse (and header
8002                         (gnus-summary-article-sparse-p
8003                          (mail-header-number header))
8004                         (memq (mail-header-number header)
8005                               gnus-newsgroup-limit)))
8006            number)
8007       (cond
8008        ;; If the article is present in the buffer we just go to it.
8009        ((and header
8010              (or (not (gnus-summary-article-sparse-p
8011                        (mail-header-number header)))
8012                  sparse))
8013         (prog1
8014             (gnus-summary-goto-article
8015              (mail-header-number header) nil t)
8016           (when sparse
8017             (gnus-summary-update-article (mail-header-number header)))))
8018        (t
8019         ;; We fetch the article.
8020         (catch 'found
8021           (dolist (gnus-override-method (gnus-refer-article-methods))
8022             (when (and (gnus-check-server gnus-override-method)
8023                        ;; Fetch the header,
8024                        (setq number (gnus-summary-insert-subject message-id)))
8025               ;; and display the article.
8026               (gnus-summary-select-article nil nil nil number)
8027               (throw 'found t)))
8028           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8029
8030 (defun gnus-refer-article-methods ()
8031   "Return a list of referable methods."
8032   (cond
8033    ;; No method, so we default to current and native.
8034    ((null gnus-refer-article-method)
8035     (list gnus-current-select-method gnus-select-method))
8036    ;; Current.
8037    ((eq 'current gnus-refer-article-method)
8038     (list gnus-current-select-method))
8039    ;; List of select methods.
8040    ((not (and (symbolp (car gnus-refer-article-method))
8041               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8042     (let (out)
8043       (dolist (method gnus-refer-article-method)
8044         (push (if (eq 'current method)
8045                   gnus-current-select-method
8046                 method)
8047               out))
8048       (nreverse out)))
8049    ;; One single select method.
8050    (t
8051     (list gnus-refer-article-method))))
8052
8053 (defun gnus-summary-edit-parameters ()
8054   "Edit the group parameters of the current group."
8055   (interactive)
8056   (gnus-group-edit-group gnus-newsgroup-name 'params))
8057
8058 (defun gnus-summary-customize-parameters ()
8059   "Customize the group parameters of the current group."
8060   (interactive)
8061   (gnus-group-customize gnus-newsgroup-name))
8062
8063 (defun gnus-summary-enter-digest-group (&optional force)
8064   "Enter an nndoc group based on the current article.
8065 If FORCE, force a digest interpretation.  If not, try
8066 to guess what the document format is."
8067   (interactive "P")
8068   (let ((conf gnus-current-window-configuration))
8069     (save-excursion
8070       (gnus-summary-select-article))
8071     (setq gnus-current-window-configuration conf)
8072     (let* ((name (format "%s-%d"
8073                          (gnus-group-prefixed-name
8074                           gnus-newsgroup-name (list 'nndoc ""))
8075                          (save-excursion
8076                            (set-buffer gnus-summary-buffer)
8077                            gnus-current-article)))
8078            (ogroup gnus-newsgroup-name)
8079            (params (append (gnus-info-params (gnus-get-info ogroup))
8080                            (list (cons 'to-group ogroup))
8081                            (list (cons 'save-article-group ogroup))))
8082            (case-fold-search t)
8083            (buf (current-buffer))
8084            dig to-address)
8085       (save-excursion
8086         (set-buffer gnus-original-article-buffer)
8087         ;; Have the digest group inherit the main mail address of
8088         ;; the parent article.
8089         (when (setq to-address (or (gnus-fetch-field "reply-to")
8090                                    (gnus-fetch-field "from")))
8091           (setq params (append
8092                         (list (cons 'to-address
8093                                     (funcall gnus-decode-encoded-word-function
8094                                              to-address))))))
8095         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8096         (insert-buffer-substring gnus-original-article-buffer)
8097         ;; Remove lines that may lead nndoc to misinterpret the
8098         ;; document type.
8099         (narrow-to-region
8100          (goto-char (point-min))
8101          (or (search-forward "\n\n" nil t) (point)))
8102         (goto-char (point-min))
8103         (delete-matching-lines "^Path:\\|^From ")
8104         (widen))
8105       (unwind-protect
8106           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8107                     (gnus-newsgroup-ephemeral-ignored-charsets
8108                      gnus-newsgroup-ignored-charsets))
8109                 (gnus-group-read-ephemeral-group
8110                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8111                               (nndoc-article-type
8112                                ,(if force 'mbox 'guess)))
8113                  t nil nil nil
8114                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8115                                                         "ADAPT")))))
8116               ;; Make all postings to this group go to the parent group.
8117               (nconc (gnus-info-params (gnus-get-info name))
8118                      params)
8119             ;; Couldn't select this doc group.
8120             (switch-to-buffer buf)
8121             (gnus-set-global-variables)
8122             (gnus-configure-windows 'summary)
8123             (gnus-message 3 "Article couldn't be entered?"))
8124         (kill-buffer dig)))))
8125
8126 (defun gnus-summary-read-document (n)
8127   "Open a new group based on the current article(s).
8128 This will allow you to read digests and other similar
8129 documents as newsgroups.
8130 Obeys the standard process/prefix convention."
8131   (interactive "P")
8132   (let* ((articles (gnus-summary-work-articles n))
8133          (ogroup gnus-newsgroup-name)
8134          (params (append (gnus-info-params (gnus-get-info ogroup))
8135                          (list (cons 'to-group ogroup))))
8136          article group egroup groups vgroup)
8137     (while (setq article (pop articles))
8138       (setq group (format "%s-%d" gnus-newsgroup-name article))
8139       (gnus-summary-remove-process-mark article)
8140       (when (gnus-summary-display-article article)
8141         (save-excursion
8142           (with-temp-buffer
8143             (insert-buffer-substring gnus-original-article-buffer)
8144             ;; Remove some headers that may lead nndoc to make
8145             ;; the wrong guess.
8146             (message-narrow-to-head)
8147             (goto-char (point-min))
8148             (delete-matching-lines "^\\(Path\\):\\|^From ")
8149             (widen)
8150             (if (setq egroup
8151                       (gnus-group-read-ephemeral-group
8152                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8153                                      (nndoc-article-type guess))
8154                        t nil t))
8155                 (progn
8156             ;; Make all postings to this group go to the parent group.
8157                   (nconc (gnus-info-params (gnus-get-info egroup))
8158                          params)
8159                   (push egroup groups))
8160               ;; Couldn't select this doc group.
8161               (gnus-error 3 "Article couldn't be entered"))))))
8162     ;; Now we have selected all the documents.
8163     (cond
8164      ((not groups)
8165       (error "None of the articles could be interpreted as documents"))
8166      ((gnus-group-read-ephemeral-group
8167        (setq vgroup (format
8168                      "nnvirtual:%s-%s" gnus-newsgroup-name
8169                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8170        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8171        t
8172        (cons (current-buffer) 'summary)))
8173      (t
8174       (error "Couldn't select virtual nndoc group")))))
8175
8176 (defun gnus-summary-isearch-article (&optional regexp-p)
8177   "Do incremental search forward on the current article.
8178 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8179   (interactive "P")
8180   (gnus-summary-select-article)
8181   (gnus-configure-windows 'article)
8182   (gnus-eval-in-buffer-window gnus-article-buffer
8183     (save-restriction
8184       (widen)
8185       (isearch-forward regexp-p))))
8186
8187 (defun gnus-summary-search-article-forward (regexp &optional backward)
8188   "Search for an article containing REGEXP forward.
8189 If BACKWARD, search backward instead."
8190   (interactive
8191    (list (read-string
8192           (format "Search article %s (regexp%s): "
8193                   (if current-prefix-arg "backward" "forward")
8194                   (if gnus-last-search-regexp
8195                       (concat ", default " gnus-last-search-regexp)
8196                     "")))
8197          current-prefix-arg))
8198   (if (string-equal regexp "")
8199       (setq regexp (or gnus-last-search-regexp ""))
8200     (setq gnus-last-search-regexp regexp)
8201     (setq gnus-article-before-search gnus-current-article))
8202   ;; Intentionally set gnus-last-article.
8203   (setq gnus-last-article gnus-article-before-search)
8204   (let ((gnus-last-article gnus-last-article))
8205     (if (gnus-summary-search-article regexp backward)
8206         (gnus-summary-show-thread)
8207       (error "Search failed: \"%s\"" regexp))))
8208
8209 (defun gnus-summary-search-article-backward (regexp)
8210   "Search for an article containing REGEXP backward."
8211   (interactive
8212    (list (read-string
8213           (format "Search article backward (regexp%s): "
8214                   (if gnus-last-search-regexp
8215                       (concat ", default " gnus-last-search-regexp)
8216                     "")))))
8217   (gnus-summary-search-article-forward regexp 'backward))
8218
8219 (defun gnus-summary-search-article (regexp &optional backward)
8220   "Search for an article containing REGEXP.
8221 Optional argument BACKWARD means do search for backward.
8222 `gnus-select-article-hook' is not called during the search."
8223   ;; We have to require this here to make sure that the following
8224   ;; dynamic binding isn't shadowed by autoloading.
8225   (require 'gnus-async)
8226   (require 'gnus-art)
8227   (let ((gnus-select-article-hook nil)  ;Disable hook.
8228         (gnus-article-prepare-hook nil)
8229         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8230         (gnus-use-article-prefetch nil)
8231         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8232         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8233         (gnus-visual nil)
8234         (gnus-keep-backlog nil)
8235         (gnus-break-pages nil)
8236         (gnus-summary-display-arrow nil)
8237         (gnus-updated-mode-lines nil)
8238         (gnus-auto-center-summary nil)
8239         (sum (current-buffer))
8240         (gnus-display-mime-function nil)
8241         (found nil)
8242         point)
8243     (gnus-save-hidden-threads
8244       (gnus-summary-select-article)
8245       (set-buffer gnus-article-buffer)
8246       (goto-char (window-point (get-buffer-window (current-buffer))))
8247       (when backward
8248         (forward-line -1))
8249       (while (not found)
8250         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8251         (if (if backward
8252                 (re-search-backward regexp nil t)
8253               (re-search-forward regexp nil t))
8254             ;; We found the regexp.
8255             (progn
8256               (setq found 'found)
8257               (beginning-of-line)
8258               (set-window-start
8259                (get-buffer-window (current-buffer))
8260                (point))
8261               (forward-line 1)
8262               (set-window-point
8263                (get-buffer-window (current-buffer))
8264                (point))
8265               (set-buffer sum)
8266               (setq point (point)))
8267           ;; We didn't find it, so we go to the next article.
8268           (set-buffer sum)
8269           (setq found 'not)
8270           (while (eq found 'not)
8271             (if (not (if backward (gnus-summary-find-prev)
8272                        (gnus-summary-find-next)))
8273                 ;; No more articles.
8274                 (setq found t)
8275               ;; Select the next article and adjust point.
8276               (unless (gnus-summary-article-sparse-p
8277                        (gnus-summary-article-number))
8278                 (setq found nil)
8279                 (gnus-summary-select-article)
8280                 (set-buffer gnus-article-buffer)
8281                 (widen)
8282                 (goto-char (if backward (point-max) (point-min))))))))
8283       (gnus-message 7 ""))
8284     ;; Return whether we found the regexp.
8285     (when (eq found 'found)
8286       (goto-char point)
8287       (gnus-summary-show-thread)
8288       (gnus-summary-goto-subject gnus-current-article)
8289       (gnus-summary-position-point)
8290       t)))
8291
8292 (defun gnus-find-matching-articles (header regexp)
8293   "Return a list of all articles that match REGEXP on HEADER.
8294 This search includes all articles in the current group that Gnus has
8295 fetched headers for, whether they are displayed or not."
8296   (let ((articles nil)
8297         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8298         (case-fold-search t))
8299     (dolist (header gnus-newsgroup-headers)
8300       (when (string-match regexp (funcall func header))
8301         (push (mail-header-number header) articles)))
8302     (nreverse articles)))
8303
8304 (defun gnus-summary-find-matching (header regexp &optional backward unread
8305                                           not-case-fold not-matching)
8306   "Return a list of all articles that match REGEXP on HEADER.
8307 The search stars on the current article and goes forwards unless
8308 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8309 If UNREAD is non-nil, only unread articles will
8310 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8311 in the comparisons. If NOT-MATCHING, return a list of all articles that
8312 not match REGEXP on HEADER."
8313   (let ((case-fold-search (not not-case-fold))
8314         articles d func)
8315     (if (consp header)
8316         (if (eq (car header) 'extra)
8317             (setq func
8318                   `(lambda (h)
8319                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8320                          "")))
8321           (error "%s is an invalid header" header))
8322       (unless (fboundp (intern (concat "mail-header-" header)))
8323         (error "%s is not a valid header" header))
8324       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8325     (dolist (d (if (eq backward 'all)
8326                    gnus-newsgroup-data
8327                  (gnus-data-find-list
8328                   (gnus-summary-article-number)
8329                   (gnus-data-list backward))))
8330       (when (and (or (not unread)       ; We want all articles...
8331                      (gnus-data-unread-p d)) ; Or just unreads.
8332                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8333                  (if not-matching
8334                      (not (string-match
8335                            regexp
8336                            (funcall func (gnus-data-header d))))
8337                    (string-match regexp
8338                                  (funcall func (gnus-data-header d)))))
8339         (push (gnus-data-number d) articles))) ; Success!
8340     (nreverse articles)))
8341
8342 (defun gnus-summary-execute-command (header regexp command &optional backward)
8343   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8344 If HEADER is an empty string (or nil), the match is done on the entire
8345 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8346   (interactive
8347    (list (let ((completion-ignore-case t))
8348            (completing-read
8349             "Header name: "
8350             (mapcar (lambda (header) (list (format "%s" header)))
8351                     (append
8352                      '("Number" "Subject" "From" "Lines" "Date"
8353                        "Message-ID" "Xref" "References" "Body")
8354                      gnus-extra-headers))
8355             nil 'require-match))
8356          (read-string "Regexp: ")
8357          (read-key-sequence "Command: ")
8358          current-prefix-arg))
8359   (when (equal header "Body")
8360     (setq header ""))
8361   ;; Hidden thread subtrees must be searched as well.
8362   (gnus-summary-show-all-threads)
8363   ;; We don't want to change current point nor window configuration.
8364   (save-excursion
8365     (save-window-excursion
8366       (let (gnus-visual
8367             gnus-treat-strip-trailing-blank-lines
8368             gnus-treat-strip-leading-blank-lines
8369             gnus-treat-strip-multiple-blank-lines
8370             gnus-treat-hide-boring-headers
8371             gnus-treat-fold-newsgroups
8372             gnus-article-prepare-hook)
8373         (gnus-message 6 "Executing %s..." (key-description command))
8374         ;; We'd like to execute COMMAND interactively so as to give arguments.
8375         (gnus-execute header regexp
8376                       `(call-interactively ',(key-binding command))
8377                       backward)
8378         (gnus-message 6 "Executing %s...done" (key-description command))))))
8379
8380 (defun gnus-summary-beginning-of-article ()
8381   "Scroll the article back to the beginning."
8382   (interactive)
8383   (gnus-summary-select-article)
8384   (gnus-configure-windows 'article)
8385   (gnus-eval-in-buffer-window gnus-article-buffer
8386     (widen)
8387     (goto-char (point-min))
8388     (when gnus-page-broken
8389       (gnus-narrow-to-page))))
8390
8391 (defun gnus-summary-end-of-article ()
8392   "Scroll to the end of the article."
8393   (interactive)
8394   (gnus-summary-select-article)
8395   (gnus-configure-windows 'article)
8396   (gnus-eval-in-buffer-window gnus-article-buffer
8397     (widen)
8398     (goto-char (point-max))
8399     (recenter -3)
8400     (when gnus-page-broken
8401       (gnus-narrow-to-page))))
8402
8403 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8404   "Truncate to LEN and quote all \"(\"'s in STRING."
8405   (gnus-replace-in-string (if (and len (> (length string) len))
8406                               (substring string 0 len)
8407                             string)
8408                           "[()]" "\\\\\\&"))
8409
8410 (defun gnus-summary-print-article (&optional filename n)
8411   "Generate and print a PostScript image of the N next (mail) articles.
8412
8413 If N is negative, print the N previous articles.  If N is nil and articles
8414 have been marked with the process mark, print these instead.
8415
8416 If the optional first argument FILENAME is nil, send the image to the
8417 printer.  If FILENAME is a string, save the PostScript image in a file with
8418 that name.  If FILENAME is a number, prompt the user for the name of the file
8419 to save in."
8420   (interactive (list (ps-print-preprint current-prefix-arg)))
8421   (dolist (article (gnus-summary-work-articles n))
8422     (gnus-summary-select-article nil nil 'pseudo article)
8423     (gnus-eval-in-buffer-window gnus-article-buffer
8424       (gnus-print-buffer))
8425     (gnus-summary-remove-process-mark article))
8426   (ps-despool filename))
8427
8428 (defun gnus-print-buffer ()
8429   (let ((buffer (generate-new-buffer " *print*")))
8430     (unwind-protect
8431         (progn
8432           (copy-to-buffer buffer (point-min) (point-max))
8433           (set-buffer buffer)
8434           (gnus-article-delete-invisible-text)
8435           (gnus-remove-text-with-property 'gnus-decoration)
8436           (when (gnus-visual-p 'article-highlight 'highlight)
8437             ;; Copy-to-buffer doesn't copy overlay.  So redo
8438             ;; highlight.
8439             (let ((gnus-article-buffer buffer))
8440               (gnus-article-highlight-citation t)
8441               (gnus-article-highlight-signature)))
8442           (let ((ps-left-header
8443                  (list
8444                   (concat "("
8445                           (gnus-summary-print-truncate-and-quote
8446                            (mail-header-subject gnus-current-headers)
8447                            66) ")")
8448                   (concat "("
8449                           (gnus-summary-print-truncate-and-quote
8450                            (mail-header-from gnus-current-headers)
8451                            45) ")")))
8452                 (ps-right-header
8453                  (list
8454                   "/pagenumberstring load"
8455                   (concat "("
8456                           (mail-header-date gnus-current-headers) ")"))))
8457             (gnus-run-hooks 'gnus-ps-print-hook)
8458             (save-excursion
8459               (if window-system
8460                   (ps-spool-buffer-with-faces)
8461                 (ps-spool-buffer)))))
8462       (kill-buffer buffer))))
8463
8464 (defun gnus-summary-show-article (&optional arg)
8465   "Force redisplaying of the current article.
8466 If ARG (the prefix) is a number, show the article with the charset
8467 defined in `gnus-summary-show-article-charset-alist', or the charset
8468 input.
8469 If ARG (the prefix) is non-nil and not a number, show the raw article
8470 without any article massaging functions being run.  Normally, the key strokes
8471 are `C-u g'."
8472   (interactive "P")
8473   (cond
8474    ((numberp arg)
8475     (gnus-summary-show-article t)
8476     (let ((gnus-newsgroup-charset
8477            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8478                (mm-read-coding-system
8479                 "View as charset: " ;; actually it is coding system.
8480                 (save-excursion
8481                   (set-buffer gnus-article-buffer)
8482                   (mm-detect-coding-region (point) (point-max))))))
8483           (gnus-newsgroup-ignored-charsets 'gnus-all))
8484       (gnus-summary-select-article nil 'force)
8485       (let ((deps gnus-newsgroup-dependencies)
8486             head header lines)
8487         (save-excursion
8488           (set-buffer gnus-original-article-buffer)
8489           (save-restriction
8490             (message-narrow-to-head)
8491             (setq head (buffer-string))
8492             (goto-char (point-min))
8493             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8494               (goto-char (point-max))
8495               (widen)
8496               (setq lines (1- (count-lines (point) (point-max))))))
8497           (with-temp-buffer
8498             (insert (format "211 %d Article retrieved.\n"
8499                             (cdr gnus-article-current)))
8500             (insert head)
8501             (if lines (insert (format "Lines: %d\n" lines)))
8502             (insert ".\n")
8503             (let ((nntp-server-buffer (current-buffer)))
8504               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8505         (gnus-data-set-header
8506          (gnus-data-find (cdr gnus-article-current))
8507          header)
8508         (gnus-summary-update-article-line
8509          (cdr gnus-article-current) header)
8510         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8511           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8512    ((not arg)
8513     ;; Select the article the normal way.
8514     (gnus-summary-select-article nil 'force))
8515    (t
8516     ;; We have to require this here to make sure that the following
8517     ;; dynamic binding isn't shadowed by autoloading.
8518     (require 'gnus-async)
8519     (require 'gnus-art)
8520     ;; Bind the article treatment functions to nil.
8521     (let ((gnus-have-all-headers t)
8522           gnus-article-prepare-hook
8523           gnus-article-decode-hook
8524           gnus-display-mime-function
8525           gnus-break-pages)
8526       ;; Destroy any MIME parts.
8527       (when (gnus-buffer-live-p gnus-article-buffer)
8528         (save-excursion
8529           (set-buffer gnus-article-buffer)
8530           (mm-destroy-parts gnus-article-mime-handles)
8531           ;; Set it to nil for safety reason.
8532           (setq gnus-article-mime-handle-alist nil)
8533           (setq gnus-article-mime-handles nil)))
8534       (gnus-summary-select-article nil 'force))))
8535   (gnus-summary-goto-subject gnus-current-article)
8536   (gnus-summary-position-point))
8537
8538 (defun gnus-summary-show-raw-article ()
8539   "Show the raw article without any article massaging functions being run."
8540   (interactive)
8541   (gnus-summary-show-article t))
8542
8543 (defun gnus-summary-verbose-headers (&optional arg)
8544   "Toggle permanent full header display.
8545 If ARG is a positive number, turn header display on.
8546 If ARG is a negative number, turn header display off."
8547   (interactive "P")
8548   (setq gnus-show-all-headers
8549         (cond ((or (not (numberp arg))
8550                    (zerop arg))
8551                (not gnus-show-all-headers))
8552               ((natnump arg)
8553                t)))
8554   (gnus-summary-show-article))
8555
8556 (defun gnus-summary-toggle-header (&optional arg)
8557   "Show the headers if they are hidden, or hide them if they are shown.
8558 If ARG is a positive number, show the entire header.
8559 If ARG is a negative number, hide the unwanted header lines."
8560   (interactive "P")
8561   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8562                      (get-buffer-window gnus-article-buffer t))))
8563     (with-current-buffer gnus-article-buffer
8564       (widen)
8565       (article-narrow-to-head)
8566       (let* ((buffer-read-only nil)
8567              (inhibit-point-motion-hooks t)
8568              (hidden (if (numberp arg)
8569                          (>= arg 0)
8570                        (gnus-article-hidden-text-p 'headers)))
8571              s e)
8572         (delete-region (point-min) (point-max))
8573         (with-current-buffer gnus-original-article-buffer
8574           (goto-char (setq s (point-min)))
8575           (setq e (if (search-forward "\n\n" nil t)
8576                       (1- (point))
8577                     (point-max))))
8578         (insert-buffer-substring gnus-original-article-buffer s e)
8579         (article-decode-encoded-words)
8580         (if hidden
8581             (let ((gnus-treat-hide-headers nil)
8582                   (gnus-treat-hide-boring-headers nil))
8583               (gnus-delete-wash-type 'headers)
8584               (gnus-treat-article 'head))
8585           (gnus-treat-article 'head))
8586         (widen)
8587         (if window
8588             (set-window-start window (goto-char (point-min))))
8589         (setq gnus-page-broken
8590               (when gnus-break-pages
8591                 (gnus-narrow-to-page)
8592                 t))
8593         (gnus-set-mode-line 'article)))))
8594
8595 (defun gnus-summary-show-all-headers ()
8596   "Make all header lines visible."
8597   (interactive)
8598   (gnus-summary-toggle-header 1))
8599
8600 (defun gnus-summary-caesar-message (&optional arg)
8601   "Caesar rotate the current article by 13.
8602 The numerical prefix specifies how many places to rotate each letter
8603 forward."
8604   (interactive "P")
8605   (gnus-summary-select-article)
8606   (let ((mail-header-separator ""))
8607     (gnus-eval-in-buffer-window gnus-article-buffer
8608       (save-restriction
8609         (widen)
8610         (let ((start (window-start))
8611               buffer-read-only)
8612           (message-caesar-buffer-body arg)
8613           (set-window-start (get-buffer-window (current-buffer)) start))))))
8614
8615 (autoload 'unmorse-region "morse"
8616   "Convert morse coded text in region to ordinary ASCII text."
8617   t)
8618
8619 (defun gnus-summary-morse-message (&optional arg)
8620   "Morse decode the current article."
8621   (interactive "P")
8622   (gnus-summary-select-article)
8623   (let ((mail-header-separator ""))
8624     (gnus-eval-in-buffer-window gnus-article-buffer
8625       (save-excursion
8626         (save-restriction
8627           (widen)
8628           (let ((pos (window-start))
8629                 buffer-read-only)
8630             (goto-char (point-min))
8631             (when (message-goto-body)
8632               (gnus-narrow-to-body))
8633             (goto-char (point-min))
8634             (while (re-search-forward "·" (point-max) t)
8635               (replace-match "."))
8636             (unmorse-region (point-min) (point-max))
8637             (widen)
8638             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8639
8640 (defun gnus-summary-stop-page-breaking ()
8641   "Stop page breaking in the current article."
8642   (interactive)
8643   (gnus-summary-select-article)
8644   (gnus-eval-in-buffer-window gnus-article-buffer
8645     (widen)
8646     (when (gnus-visual-p 'page-marker)
8647       (let ((buffer-read-only nil))
8648         (gnus-remove-text-with-property 'gnus-prev)
8649         (gnus-remove-text-with-property 'gnus-next))
8650       (setq gnus-page-broken nil))))
8651
8652 (defun gnus-summary-move-article (&optional n to-newsgroup
8653                                             select-method action)
8654   "Move the current article to a different newsgroup.
8655 If N is a positive number, move the N next articles.
8656 If N is a negative number, move the N previous articles.
8657 If N is nil and any articles have been marked with the process mark,
8658 move those articles instead.
8659 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8660 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8661 re-spool using this method.
8662
8663 When called interactively with TO-NEWSGROUP being nil, the value of
8664 the variable `gnus-move-split-methods' is used for finding a default
8665 for the target newsgroup.
8666
8667 For this function to work, both the current newsgroup and the
8668 newsgroup that you want to move to have to support the `request-move'
8669 and `request-accept' functions.
8670
8671 ACTION can be either `move' (the default), `crosspost' or `copy'."
8672   (interactive "P")
8673   (unless action
8674     (setq action 'move))
8675   ;; Check whether the source group supports the required functions.
8676   (cond ((and (eq action 'move)
8677               (not (gnus-check-backend-function
8678                     'request-move-article gnus-newsgroup-name)))
8679          (error "The current group does not support article moving"))
8680         ((and (eq action 'crosspost)
8681               (not (gnus-check-backend-function
8682                     'request-replace-article gnus-newsgroup-name)))
8683          (error "The current group does not support article editing")))
8684   (let ((articles (gnus-summary-work-articles n))
8685         (prefix (if (gnus-check-backend-function
8686                      'request-move-article gnus-newsgroup-name)
8687                     (gnus-group-real-prefix gnus-newsgroup-name)
8688                   ""))
8689         (names '((move "Move" "Moving")
8690                  (copy "Copy" "Copying")
8691                  (crosspost "Crosspost" "Crossposting")))
8692         (copy-buf (save-excursion
8693                     (nnheader-set-temp-buffer " *copy article*")))
8694         art-group to-method new-xref article to-groups)
8695     (unless (assq action names)
8696       (error "Unknown action %s" action))
8697     ;; Read the newsgroup name.
8698     (when (and (not to-newsgroup)
8699                (not select-method))
8700       (if (and gnus-move-split-methods
8701                (not
8702                 (and (memq gnus-current-article articles)
8703                      (gnus-buffer-live-p gnus-original-article-buffer))))
8704           ;; When `gnus-move-split-methods' is non-nil, we have to
8705           ;; select an article to give `gnus-read-move-group-name' an
8706           ;; opportunity to suggest an appropriate default.  However,
8707           ;; we needn't render or mark the article.
8708           (let ((gnus-display-mime-function nil)
8709                 (gnus-article-prepare-hook nil)
8710                 (gnus-mark-article-hook nil))
8711             (gnus-summary-select-article nil nil nil (car articles))))
8712       (setq to-newsgroup
8713             (gnus-read-move-group-name
8714              (cadr (assq action names))
8715              (symbol-value (intern (format "gnus-current-%s-group" action)))
8716              articles prefix))
8717       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8718     (setq to-method (or select-method
8719                         (gnus-server-to-method
8720                          (gnus-group-method to-newsgroup))))
8721     ;; Check the method we are to move this article to...
8722     (unless (gnus-check-backend-function
8723              'request-accept-article (car to-method))
8724       (error "%s does not support article copying" (car to-method)))
8725     (unless (gnus-check-server to-method)
8726       (error "Can't open server %s" (car to-method)))
8727     (gnus-message 6 "%s to %s: %s..."
8728                   (caddr (assq action names))
8729                   (or (car select-method) to-newsgroup) articles)
8730     (while articles
8731       (setq article (pop articles))
8732       (setq
8733        art-group
8734        (cond
8735         ;; Move the article.
8736         ((eq action 'move)
8737          ;; Remove this article from future suppression.
8738          (gnus-dup-unsuppress-article article)
8739          (gnus-request-move-article
8740           article                       ; Article to move
8741           gnus-newsgroup-name           ; From newsgroup
8742           (nth 1 (gnus-find-method-for-group
8743                   gnus-newsgroup-name)) ; Server
8744           (list 'gnus-request-accept-article
8745                 to-newsgroup (list 'quote select-method)
8746                 (not articles) t)       ; Accept form
8747           (not articles)))              ; Only save nov last time
8748         ;; Copy the article.
8749         ((eq action 'copy)
8750          (save-excursion
8751            (set-buffer copy-buf)
8752            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8753              (gnus-request-accept-article
8754               to-newsgroup select-method (not articles) t))))
8755         ;; Crosspost the article.
8756         ((eq action 'crosspost)
8757          (let ((xref (message-tokenize-header
8758                       (mail-header-xref (gnus-summary-article-header article))
8759                       " ")))
8760            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8761                                   ":" (number-to-string article)))
8762            (unless xref
8763              (setq xref (list (system-name))))
8764            (setq new-xref
8765                  (concat
8766                   (mapconcat 'identity
8767                              (delete "Xref:" (delete new-xref xref))
8768                              " ")
8769                   " " new-xref))
8770            (save-excursion
8771              (set-buffer copy-buf)
8772              ;; First put the article in the destination group.
8773              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8774              (when (consp (setq art-group
8775                                 (gnus-request-accept-article
8776                                  to-newsgroup select-method (not articles))))
8777                (setq new-xref (concat new-xref " " (car art-group)
8778                                       ":"
8779                                       (number-to-string (cdr art-group))))
8780                ;; Now we have the new Xrefs header, so we insert
8781                ;; it and replace the new article.
8782                (nnheader-replace-header "Xref" new-xref)
8783                (gnus-request-replace-article
8784                 (cdr art-group) to-newsgroup (current-buffer))
8785                art-group))))))
8786       (cond
8787        ((not art-group)
8788         (gnus-message 1 "Couldn't %s article %s: %s"
8789                       (cadr (assq action names)) article
8790                       (nnheader-get-report (car to-method))))
8791        ((eq art-group 'junk)
8792         (when (eq action 'move)
8793           (gnus-summary-mark-article article gnus-canceled-mark)
8794           (gnus-message 4 "Deleted article %s" article)
8795           ;; run the delete hook
8796           (run-hook-with-args 'gnus-summary-article-delete-hook
8797                               action
8798                               (gnus-data-header
8799                                (assoc article (gnus-data-list nil)))
8800                               gnus-newsgroup-name nil
8801                               select-method)))
8802        (t
8803         (let* ((pto-group (gnus-group-prefixed-name
8804                            (car art-group) to-method))
8805                (entry
8806                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8807                (info (nth 2 entry))
8808                (to-group (gnus-info-group info))
8809                to-marks)
8810           ;; Update the group that has been moved to.
8811           (when (and info
8812                      (memq action '(move copy)))
8813             (unless (member to-group to-groups)
8814               (push to-group to-groups))
8815
8816             (unless (memq article gnus-newsgroup-unreads)
8817               (push 'read to-marks)
8818               (gnus-info-set-read
8819                info (gnus-add-to-range (gnus-info-read info)
8820                                        (list (cdr art-group)))))
8821
8822             ;; See whether the article is to be put in the cache.
8823             (let ((marks gnus-article-mark-lists)
8824                   (to-article (cdr art-group)))
8825
8826               ;; Enter the article into the cache in the new group,
8827               ;; if that is required.
8828               (when gnus-use-cache
8829                 (gnus-cache-possibly-enter-article
8830                  to-group to-article
8831                  (memq article gnus-newsgroup-marked)
8832                  (memq article gnus-newsgroup-dormant)
8833                  (memq article gnus-newsgroup-unreads)))
8834
8835               (when gnus-preserve-marks
8836                 ;; Copy any marks over to the new group.
8837                 (when (and (equal to-group gnus-newsgroup-name)
8838                            (not (memq article gnus-newsgroup-unreads)))
8839                   ;; Mark this article as read in this group.
8840                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8841                   (setcdr (gnus-active to-group) to-article)
8842                   (setcdr gnus-newsgroup-active to-article))
8843
8844                 (while marks
8845                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8846                     (when (memq article (symbol-value
8847                                          (intern (format "gnus-newsgroup-%s"
8848                                                          (caar marks)))))
8849                       (push (cdar marks) to-marks)
8850                       ;; If the other group is the same as this group,
8851                       ;; then we have to add the mark to the list.
8852                       (when (equal to-group gnus-newsgroup-name)
8853                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8854                              (cons to-article
8855                                    (symbol-value
8856                                     (intern (format "gnus-newsgroup-%s"
8857                                                     (caar marks)))))))
8858                       ;; Copy the marks to other group.
8859                       (gnus-add-marked-articles
8860                        to-group (cdar marks) (list to-article) info)))
8861                   (setq marks (cdr marks)))
8862
8863                 (gnus-request-set-mark
8864                  to-group (list (list (list to-article) 'add to-marks))))
8865
8866               (gnus-dribble-enter
8867                (concat "(gnus-group-set-info '"
8868                        (gnus-prin1-to-string (gnus-get-info to-group))
8869                        ")"))))
8870
8871           ;; Update the Xref header in this article to point to
8872           ;; the new crossposted article we have just created.
8873           (when (eq action 'crosspost)
8874             (save-excursion
8875               (set-buffer copy-buf)
8876               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8877               (nnheader-replace-header "Xref" new-xref)
8878               (gnus-request-replace-article
8879                article gnus-newsgroup-name (current-buffer))))
8880
8881           ;; run the move/copy/crosspost/respool hook
8882           (run-hook-with-args 'gnus-summary-article-move-hook 
8883                               action
8884                               (gnus-data-header 
8885                                (assoc article (gnus-data-list nil)))
8886                               gnus-newsgroup-name
8887                               to-newsgroup
8888                               select-method))
8889
8890         ;;;!!!Why is this necessary?
8891         (set-buffer gnus-summary-buffer)
8892         
8893         (gnus-summary-goto-subject article)
8894         (when (eq action 'move)
8895           (gnus-summary-mark-article article gnus-canceled-mark))))
8896       (gnus-summary-remove-process-mark article))
8897     ;; Re-activate all groups that have been moved to.
8898     (save-excursion
8899       (set-buffer gnus-group-buffer)
8900       (let ((gnus-group-marked to-groups))
8901         (gnus-group-get-new-news-this-group nil t)))
8902
8903     (gnus-kill-buffer copy-buf)
8904     (gnus-summary-position-point)
8905     (gnus-set-mode-line 'summary)))
8906
8907 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8908   "Move the current article to a different newsgroup.
8909 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8910 When called interactively, if TO-NEWSGROUP is nil, use the value of
8911 the variable `gnus-move-split-methods' for finding a default target
8912 newsgroup.
8913 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8914 re-spool using this method."
8915   (interactive "P")
8916   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8917
8918 (defun gnus-summary-crosspost-article (&optional n)
8919   "Crosspost the current article to some other group."
8920   (interactive "P")
8921   (gnus-summary-move-article n nil nil 'crosspost))
8922
8923 (defcustom gnus-summary-respool-default-method nil
8924   "Default method type for respooling an article.
8925 If nil, use to the current newsgroup method."
8926   :type 'symbol
8927   :group 'gnus-summary-mail)
8928
8929 (defcustom gnus-summary-display-while-building nil
8930   "If not-nil, show and update the summary buffer as it's being built.
8931 If the value is t, update the buffer after every line is inserted.  If
8932 the value is an integer (N), update the display every N lines."
8933   :group 'gnus-thread
8934   :type '(choice (const :tag "off" nil)
8935                  number
8936                  (const :tag "frequently" t)))
8937
8938 (defun gnus-summary-respool-article (&optional n method)
8939   "Respool the current article.
8940 The article will be squeezed through the mail spooling process again,
8941 which means that it will be put in some mail newsgroup or other
8942 depending on `nnmail-split-methods'.
8943 If N is a positive number, respool the N next articles.
8944 If N is a negative number, respool the N previous articles.
8945 If N is nil and any articles have been marked with the process mark,
8946 respool those articles instead.
8947
8948 Respooling can be done both from mail groups and \"real\" newsgroups.
8949 In the former case, the articles in question will be moved from the
8950 current group into whatever groups they are destined to.  In the
8951 latter case, they will be copied into the relevant groups."
8952   (interactive
8953    (list current-prefix-arg
8954          (let* ((methods (gnus-methods-using 'respool))
8955                 (methname
8956                  (symbol-name (or gnus-summary-respool-default-method
8957                                   (car (gnus-find-method-for-group
8958                                         gnus-newsgroup-name)))))
8959                 (method
8960                  (gnus-completing-read-with-default
8961                   methname "What backend do you want to use when respooling?"
8962                   methods nil t nil 'gnus-mail-method-history))
8963                 ms)
8964            (cond
8965             ((zerop (length (setq ms (gnus-servers-using-backend
8966                                       (intern method)))))
8967              (list (intern method) ""))
8968             ((= 1 (length ms))
8969              (car ms))
8970             (t
8971              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8972                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8973                            ms-alist))))))))
8974   (unless method
8975     (error "No method given for respooling"))
8976   (if (assoc (symbol-name
8977               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8978              (gnus-methods-using 'respool))
8979       (gnus-summary-move-article n nil method)
8980     (gnus-summary-copy-article n nil method)))
8981
8982 (defun gnus-summary-import-article (file &optional edit)
8983   "Import an arbitrary file into a mail newsgroup."
8984   (interactive "fImport file: \nP")
8985   (let ((group gnus-newsgroup-name)
8986         (now (current-time))
8987         atts lines group-art)
8988     (unless (gnus-check-backend-function 'request-accept-article group)
8989       (error "%s does not support article importing" group))
8990     (or (file-readable-p file)
8991         (not (file-regular-p file))
8992         (error "Can't read %s" file))
8993     (save-excursion
8994       (set-buffer (gnus-get-buffer-create " *import file*"))
8995       (erase-buffer)
8996       (nnheader-insert-file-contents file)
8997       (goto-char (point-min))
8998       (if (nnheader-article-p)
8999           (save-restriction
9000             (goto-char (point-min))
9001             (search-forward "\n\n" nil t)
9002             (narrow-to-region (point-min) (1- (point)))
9003             (goto-char (point-min))
9004             (unless (re-search-forward "^date:" nil t)
9005               (goto-char (point-max))
9006               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9007        ;; This doesn't look like an article, so we fudge some headers.
9008         (setq atts (file-attributes file)
9009               lines (count-lines (point-min) (point-max)))
9010         (insert "From: " (read-string "From: ") "\n"
9011                 "Subject: " (read-string "Subject: ") "\n"
9012                 "Date: " (message-make-date (nth 5 atts)) "\n"
9013                 "Message-ID: " (message-make-message-id) "\n"
9014                 "Lines: " (int-to-string lines) "\n"
9015                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9016       (setq group-art (gnus-request-accept-article group nil t))
9017       (kill-buffer (current-buffer)))
9018     (setq gnus-newsgroup-active (gnus-activate-group group))
9019     (forward-line 1)
9020     (gnus-summary-goto-article (cdr group-art) nil t)
9021     (when edit
9022       (gnus-summary-edit-article))))
9023
9024 (defun gnus-summary-create-article ()
9025   "Create an article in a mail newsgroup."
9026   (interactive)
9027   (let ((group gnus-newsgroup-name)
9028         (now (current-time))
9029         group-art)
9030     (unless (gnus-check-backend-function 'request-accept-article group)
9031       (error "%s does not support article importing" group))
9032     (save-excursion
9033       (set-buffer (gnus-get-buffer-create " *import file*"))
9034       (erase-buffer)
9035       (goto-char (point-min))
9036       ;; This doesn't look like an article, so we fudge some headers.
9037       (insert "From: " (read-string "From: ") "\n"
9038               "Subject: " (read-string "Subject: ") "\n"
9039               "Date: " (message-make-date now) "\n"
9040               "Message-ID: " (message-make-message-id) "\n")
9041       (setq group-art (gnus-request-accept-article group nil t))
9042       (kill-buffer (current-buffer)))
9043     (setq gnus-newsgroup-active (gnus-activate-group group))
9044     (forward-line 1)
9045     (gnus-summary-goto-article (cdr group-art) nil t)
9046     (gnus-summary-edit-article)))
9047
9048 (defun gnus-summary-article-posted-p ()
9049   "Say whether the current (mail) article is available from news as well.
9050 This will be the case if the article has both been mailed and posted."
9051   (interactive)
9052   (let ((id (mail-header-references (gnus-summary-article-header)))
9053         (gnus-override-method (car (gnus-refer-article-methods))))
9054     (if (gnus-request-head id "")
9055         (gnus-message 2 "The current message was found on %s"
9056                       gnus-override-method)
9057       (gnus-message 2 "The current message couldn't be found on %s"
9058                     gnus-override-method)
9059       nil)))
9060
9061 (defun gnus-summary-expire-articles (&optional now)
9062   "Expire all articles that are marked as expirable in the current group."
9063   (interactive)
9064   (when (and (not gnus-group-is-exiting-without-update-p)
9065              (gnus-check-backend-function
9066               'request-expire-articles gnus-newsgroup-name))
9067     ;; This backend supports expiry.
9068     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9069            (expirable (if total
9070                           (progn
9071                             ;; We need to update the info for
9072                             ;; this group for `gnus-list-of-read-articles'
9073                             ;; to give us the right answer.
9074                             (gnus-run-hooks 'gnus-exit-group-hook)
9075                             (gnus-summary-update-info)
9076                             (gnus-list-of-read-articles gnus-newsgroup-name))
9077                         (setq gnus-newsgroup-expirable
9078                               (sort gnus-newsgroup-expirable '<))))
9079            (expiry-wait (if now 'immediate
9080                           (gnus-group-find-parameter
9081                            gnus-newsgroup-name 'expiry-wait)))
9082            (nnmail-expiry-target
9083             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9084                 nnmail-expiry-target))
9085            es)
9086       (when expirable
9087         ;; There are expirable articles in this group, so we run them
9088         ;; through the expiry process.
9089         (gnus-message 6 "Expiring articles...")
9090         (unless (gnus-check-group gnus-newsgroup-name)
9091           (error "Can't open server for %s" gnus-newsgroup-name))
9092         ;; The list of articles that weren't expired is returned.
9093         (save-excursion
9094           (if expiry-wait
9095               (let ((nnmail-expiry-wait-function nil)
9096                     (nnmail-expiry-wait expiry-wait))
9097                 (setq es (gnus-request-expire-articles
9098                           expirable gnus-newsgroup-name)))
9099             (setq es (gnus-request-expire-articles
9100                       expirable gnus-newsgroup-name)))
9101           (unless total
9102             (setq gnus-newsgroup-expirable es))
9103           ;; We go through the old list of expirable, and mark all
9104           ;; really expired articles as nonexistent.
9105           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9106             (let ((gnus-use-cache nil))
9107               (dolist (article expirable)
9108                 (when (and (not (memq article es))
9109                            (gnus-data-find article))
9110                   (gnus-summary-mark-article article gnus-canceled-mark)
9111                   (run-hook-with-args 'gnus-summary-article-expire-hook
9112                                       'delete
9113                                       (gnus-data-header
9114                                        (assoc article (gnus-data-list nil)))
9115                                       gnus-newsgroup-name
9116                                       nil
9117                                       nil))))))
9118         (gnus-message 6 "Expiring articles...done")))))
9119
9120 (defun gnus-summary-expire-articles-now ()
9121   "Expunge all expirable articles in the current group.
9122 This means that *all* articles that are marked as expirable will be
9123 deleted forever, right now."
9124   (interactive)
9125   (or gnus-expert-user
9126       (gnus-yes-or-no-p
9127        "Are you really, really, really sure you want to delete all these messages? ")
9128       (error "Phew!"))
9129   (gnus-summary-expire-articles t))
9130
9131 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9132 (defun gnus-summary-delete-article (&optional n)
9133   "Delete the N next (mail) articles.
9134 This command actually deletes articles.  This is not a marking
9135 command.  The article will disappear forever from your life, never to
9136 return.
9137 If N is negative, delete backwards.
9138 If N is nil and articles have been marked with the process mark,
9139 delete these instead."
9140   (interactive "P")
9141   (unless (gnus-check-backend-function 'request-expire-articles
9142                                        gnus-newsgroup-name)
9143     (error "The current newsgroup does not support article deletion"))
9144   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9145     (error "Couldn't open server"))
9146   ;; Compute the list of articles to delete.
9147   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9148         (nnmail-expiry-target 'delete)
9149         not-deleted)
9150     (if (and gnus-novice-user
9151              (not (gnus-yes-or-no-p
9152                    (format "Do you really want to delete %s forever? "
9153                            (if (> (length articles) 1)
9154                                (format "these %s articles" (length articles))
9155                              "this article")))))
9156         ()
9157       ;; Delete the articles.
9158       (setq not-deleted (gnus-request-expire-articles
9159                          articles gnus-newsgroup-name 'force))
9160       (while articles
9161         (gnus-summary-remove-process-mark (car articles))
9162         ;; The backend might not have been able to delete the article
9163         ;; after all.
9164         (unless (memq (car articles) not-deleted)
9165           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9166         (let* ((article (car articles))
9167                (id (mail-header-id (gnus-data-header 
9168                                     (assoc article (gnus-data-list nil))))))
9169           (run-hook-with-args 'gnus-summary-article-delete-hook
9170                               'delete id gnus-newsgroup-name nil
9171                               nil))
9172         (setq articles (cdr articles)))
9173       (when not-deleted
9174         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9175     (gnus-summary-position-point)
9176     (gnus-set-mode-line 'summary)
9177     not-deleted))
9178
9179 (defun gnus-summary-edit-article (&optional arg)
9180   "Edit the current article.
9181 This will have permanent effect only in mail groups.
9182 If ARG is nil, edit the decoded articles.
9183 If ARG is 1, edit the raw articles.
9184 If ARG is 2, edit the raw articles even in read-only groups.
9185 If ARG is 3, edit the articles with the current handles.
9186 Otherwise, allow editing of articles even in read-only
9187 groups."
9188   (interactive "P")
9189   (let (force raw current-handles)
9190     (cond
9191      ((null arg))
9192      ((eq arg 1)
9193       (setq raw t))
9194      ((eq arg 2)
9195       (setq raw t
9196             force t))
9197      ((eq arg 3)
9198       (setq current-handles
9199             (and (gnus-buffer-live-p gnus-article-buffer)
9200                  (with-current-buffer gnus-article-buffer
9201                    (prog1
9202                        gnus-article-mime-handles
9203                      (setq gnus-article-mime-handles nil))))))
9204      (t
9205       (setq force t)))
9206     (when (and raw (not force)
9207                (member gnus-newsgroup-name '("nndraft:delayed"
9208                                              "nndraft:drafts"
9209                                              "nndraft:queue")))
9210       (error "Can't edit the raw article in group %s"
9211              gnus-newsgroup-name))
9212     (save-excursion
9213       (set-buffer gnus-summary-buffer)
9214       (let ((mail-parse-charset gnus-newsgroup-charset)
9215             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9216         (gnus-set-global-variables)
9217         (when (and (not force)
9218                    (gnus-group-read-only-p))
9219           (error "The current newsgroup does not support article editing"))
9220         (gnus-summary-show-article t)
9221         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9222           (with-current-buffer gnus-article-buffer
9223             (mm-enable-multibyte)))
9224         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9225             (setq raw t))
9226         (gnus-article-edit-article
9227          (if raw 'ignore
9228            `(lambda ()
9229               (let ((mbl mml-buffer-list))
9230                 (setq mml-buffer-list nil)
9231                 (mime-to-mml ,'current-handles)
9232                 (let ((mbl1 mml-buffer-list))
9233                   (setq mml-buffer-list mbl)
9234                   (set (make-local-variable 'mml-buffer-list) mbl1))
9235                 (make-local-hook 'kill-buffer-hook)
9236                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9237          `(lambda (no-highlight)
9238             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9239                   (message-options message-options)
9240                   (message-options-set-recipient)
9241                   (mail-parse-ignored-charsets
9242                    ',gnus-newsgroup-ignored-charsets))
9243               ,(if (not raw) '(progn
9244                                 (mml-to-mime)
9245                                 (mml-destroy-buffers)
9246                                 (remove-hook 'kill-buffer-hook
9247                                              'mml-destroy-buffers t)
9248                                 (kill-local-variable 'mml-buffer-list)))
9249               (gnus-summary-edit-article-done
9250                ,(or (mail-header-references gnus-current-headers) "")
9251                ,(gnus-group-read-only-p)
9252                ,gnus-summary-buffer no-highlight))))))))
9253
9254 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9255
9256 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9257                                                  no-highlight)
9258   "Make edits to the current article permanent."
9259   (interactive)
9260   (save-excursion
9261    ;; The buffer restriction contains the entire article if it exists.
9262     (when (article-goto-body)
9263       (let ((lines (count-lines (point) (point-max)))
9264             (length (- (point-max) (point)))
9265             (case-fold-search t)
9266             (body (copy-marker (point))))
9267         (goto-char (point-min))
9268         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9269           (delete-region (match-beginning 1) (match-end 1))
9270           (insert (number-to-string length)))
9271         (goto-char (point-min))
9272         (when (re-search-forward
9273                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9274           (delete-region (match-beginning 1) (match-end 1))
9275           (insert (number-to-string length)))
9276         (goto-char (point-min))
9277         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9278           (delete-region (match-beginning 1) (match-end 1))
9279           (insert (number-to-string lines))))))
9280   ;; Replace the article.
9281   (let ((buf (current-buffer)))
9282     (with-temp-buffer
9283       (insert-buffer-substring buf)
9284
9285       (if (and (not read-only)
9286                (not (gnus-request-replace-article
9287                      (cdr gnus-article-current) (car gnus-article-current)
9288                      (current-buffer) t)))
9289           (error "Couldn't replace article")
9290         ;; Update the summary buffer.
9291         (if (and references
9292                  (equal (message-tokenize-header references " ")
9293                         (message-tokenize-header
9294                          (or (message-fetch-field "references") "") " ")))
9295             ;; We only have to update this line.
9296             (save-excursion
9297               (save-restriction
9298                 (message-narrow-to-head)
9299                 (let ((head (buffer-string))
9300                       header)
9301                   (with-temp-buffer
9302                     (insert (format "211 %d Article retrieved.\n"
9303                                     (cdr gnus-article-current)))
9304                     (insert head)
9305                     (insert ".\n")
9306                     (let ((nntp-server-buffer (current-buffer)))
9307                       (setq header (car (gnus-get-newsgroup-headers
9308                                          nil t))))
9309                     (save-excursion
9310                       (set-buffer gnus-summary-buffer)
9311                       (gnus-data-set-header
9312                        (gnus-data-find (cdr gnus-article-current))
9313                        header)
9314                       (gnus-summary-update-article-line
9315                        (cdr gnus-article-current) header)
9316                       (if (gnus-summary-goto-subject
9317                            (cdr gnus-article-current) nil t)
9318                           (gnus-summary-update-secondary-mark
9319                            (cdr gnus-article-current))))))))
9320           ;; Update threads.
9321           (set-buffer (or buffer gnus-summary-buffer))
9322           (gnus-summary-update-article (cdr gnus-article-current))
9323           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9324               (gnus-summary-update-secondary-mark
9325                (cdr gnus-article-current))))
9326         ;; Prettify the article buffer again.
9327         (unless no-highlight
9328           (save-excursion
9329             (set-buffer gnus-article-buffer)
9330             ;;;!!! Fix this -- article should be rehighlighted.
9331             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9332             (set-buffer gnus-original-article-buffer)
9333             (gnus-request-article
9334              (cdr gnus-article-current)
9335              (car gnus-article-current) (current-buffer))))
9336         ;; Prettify the summary buffer line.
9337         (when (gnus-visual-p 'summary-highlight 'highlight)
9338           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9339
9340 (defun gnus-summary-edit-wash (key)
9341   "Perform editing command KEY in the article buffer."
9342   (interactive
9343    (list
9344     (progn
9345       (message "%s" (concat (this-command-keys) "- "))
9346       (read-char))))
9347   (message "")
9348   (gnus-summary-edit-article)
9349   (execute-kbd-macro (concat (this-command-keys) key))
9350   (gnus-article-edit-done))
9351
9352 ;;; Respooling
9353
9354 (defun gnus-summary-respool-query (&optional silent trace)
9355   "Query where the respool algorithm would put this article."
9356   (interactive)
9357   (let (gnus-mark-article-hook)
9358     (gnus-summary-select-article)
9359     (save-excursion
9360       (set-buffer gnus-original-article-buffer)
9361       (save-restriction
9362         (message-narrow-to-head)
9363         (let ((groups (nnmail-article-group 'identity trace)))
9364           (unless silent
9365             (if groups
9366                 (message "This message would go to %s"
9367                          (mapconcat 'car groups ", "))
9368               (message "This message would go to no groups"))
9369             groups))))))
9370
9371 (defun gnus-summary-respool-trace ()
9372   "Trace where the respool algorithm would put this article.
9373 Display a buffer showing all fancy splitting patterns which matched."
9374   (interactive)
9375   (gnus-summary-respool-query nil t))
9376
9377 ;; Summary marking commands.
9378
9379 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9380   "Mark articles which has the same subject as read, and then select the next.
9381 If UNMARK is positive, remove any kind of mark.
9382 If UNMARK is negative, tick articles."
9383   (interactive "P")
9384   (when unmark
9385     (setq unmark (prefix-numeric-value unmark)))
9386   (let ((count
9387          (gnus-summary-mark-same-subject
9388           (gnus-summary-article-subject) unmark)))
9389     ;; Select next unread article.  If auto-select-same mode, should
9390     ;; select the first unread article.
9391     (gnus-summary-next-article t (and gnus-auto-select-same
9392                                       (gnus-summary-article-subject)))
9393     (gnus-message 7 "%d article%s marked as %s"
9394                   count (if (= count 1) " is" "s are")
9395                   (if unmark "unread" "read"))))
9396
9397 (defun gnus-summary-kill-same-subject (&optional unmark)
9398   "Mark articles which has the same subject as read.
9399 If UNMARK is positive, remove any kind of mark.
9400 If UNMARK is negative, tick articles."
9401   (interactive "P")
9402   (when unmark
9403     (setq unmark (prefix-numeric-value unmark)))
9404   (let ((count
9405          (gnus-summary-mark-same-subject
9406           (gnus-summary-article-subject) unmark)))
9407     ;; If marked as read, go to next unread subject.
9408     (when (null unmark)
9409       ;; Go to next unread subject.
9410       (gnus-summary-next-subject 1 t))
9411     (gnus-message 7 "%d articles are marked as %s"
9412                   count (if unmark "unread" "read"))))
9413
9414 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9415   "Mark articles with same SUBJECT as read, and return marked number.
9416 If optional argument UNMARK is positive, remove any kinds of marks.
9417 If optional argument UNMARK is negative, mark articles as unread instead."
9418   (let ((count 1))
9419     (save-excursion
9420       (cond
9421        ((null unmark)                   ; Mark as read.
9422         (while (and
9423                 (progn
9424                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9425                   (gnus-summary-show-thread) t)
9426                 (gnus-summary-find-subject subject))
9427           (setq count (1+ count))))
9428        ((> unmark 0)                    ; Tick.
9429         (while (and
9430                 (progn
9431                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9432                   (gnus-summary-show-thread) t)
9433                 (gnus-summary-find-subject subject))
9434           (setq count (1+ count))))
9435        (t                               ; Mark as unread.
9436         (while (and
9437                 (progn
9438                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9439                   (gnus-summary-show-thread) t)
9440                 (gnus-summary-find-subject subject))
9441           (setq count (1+ count)))))
9442       (gnus-set-mode-line 'summary)
9443       ;; Return the number of marked articles.
9444       count)))
9445
9446 (defun gnus-summary-mark-as-processable (n &optional unmark)
9447   "Set the process mark on the next N articles.
9448 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9449 the process mark instead.  The difference between N and the actual
9450 number of articles marked is returned."
9451   (interactive "P")
9452   (if (and (null n) (gnus-region-active-p))
9453       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9454     (setq n (prefix-numeric-value n))
9455     (let ((backward (< n 0))
9456           (n (abs n)))
9457       (while (and
9458               (> n 0)
9459               (if unmark
9460                   (gnus-summary-remove-process-mark
9461                    (gnus-summary-article-number))
9462                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9463               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9464         (setq n (1- n)))
9465       (when (/= 0 n)
9466         (gnus-message 7 "No more articles"))
9467       (gnus-summary-recenter)
9468       (gnus-summary-position-point)
9469       n)))
9470
9471 (defun gnus-summary-unmark-as-processable (n)
9472   "Remove the process mark from the next N articles.
9473 If N is negative, unmark backward instead.  The difference between N and
9474 the actual number of articles unmarked is returned."
9475   (interactive "P")
9476   (gnus-summary-mark-as-processable n t))
9477
9478 (defun gnus-summary-unmark-all-processable ()
9479   "Remove the process mark from all articles."
9480   (interactive)
9481   (save-excursion
9482     (while gnus-newsgroup-processable
9483       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9484   (gnus-summary-position-point))
9485
9486 (defun gnus-summary-add-mark (article type)
9487   "Mark ARTICLE with a mark of TYPE."
9488   (let ((vtype (car (assq type gnus-article-mark-lists)))
9489         var)
9490     (if (not vtype)
9491         (error "No such mark type: %s" type)
9492       (setq var (intern (format "gnus-newsgroup-%s" type)))
9493       (set var (cons article (symbol-value var)))
9494       (if (memq type '(processable cached replied forwarded recent saved))
9495           (gnus-summary-update-secondary-mark article)
9496         ;;; !!! This is bogus.  We should find out what primary
9497         ;;; !!! mark we want to set.
9498         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9499
9500 (defun gnus-summary-mark-as-expirable (n)
9501   "Mark N articles forward as expirable.
9502 If N is negative, mark backward instead.  The difference between N and
9503 the actual number of articles marked is returned."
9504   (interactive "p")
9505   (gnus-summary-mark-forward n gnus-expirable-mark))
9506
9507 (defun gnus-summary-mark-as-spam (n)
9508   "Mark N articles forward as spam.
9509 If N is negative, mark backward instead.  The difference between N and
9510 the actual number of articles marked is returned."
9511   (interactive "p")
9512   (gnus-summary-mark-forward n gnus-spam-mark))
9513
9514 (defun gnus-summary-mark-article-as-replied (article)
9515   "Mark ARTICLE as replied to and update the summary line.
9516 ARTICLE can also be a list of articles."
9517   (interactive (list (gnus-summary-article-number)))
9518   (let ((articles (if (listp article) article (list article))))
9519     (dolist (article articles)
9520       (unless (numberp article)
9521         (error "%s is not a number" article))
9522       (push article gnus-newsgroup-replied)
9523       (let ((buffer-read-only nil))
9524         (when (gnus-summary-goto-subject article nil t)
9525           (gnus-summary-update-secondary-mark article))))))
9526
9527 (defun gnus-summary-mark-article-as-forwarded (article)
9528   "Mark ARTICLE as forwarded and update the summary line.
9529 ARTICLE can also be a list of articles."
9530   (let ((articles (if (listp article) article (list article))))
9531     (dolist (article articles)
9532       (push article gnus-newsgroup-forwarded)
9533       (let ((buffer-read-only nil))
9534         (when (gnus-summary-goto-subject article nil t)
9535           (gnus-summary-update-secondary-mark article))))))
9536
9537 (defun gnus-summary-set-bookmark (article)
9538   "Set a bookmark in current article."
9539   (interactive (list (gnus-summary-article-number)))
9540   (when (or (not (get-buffer gnus-article-buffer))
9541             (not gnus-current-article)
9542             (not gnus-article-current)
9543             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9544     (error "No current article selected"))
9545   ;; Remove old bookmark, if one exists.
9546   (gnus-pull article gnus-newsgroup-bookmarks)
9547   ;; Set the new bookmark, which is on the form
9548   ;; (article-number . line-number-in-body).
9549   (push
9550    (cons article
9551          (save-excursion
9552            (set-buffer gnus-article-buffer)
9553            (count-lines
9554             (min (point)
9555                  (save-excursion
9556                    (article-goto-body)
9557                    (point)))
9558             (point))))
9559    gnus-newsgroup-bookmarks)
9560   (gnus-message 6 "A bookmark has been added to the current article."))
9561
9562 (defun gnus-summary-remove-bookmark (article)
9563   "Remove the bookmark from the current article."
9564   (interactive (list (gnus-summary-article-number)))
9565   ;; Remove old bookmark, if one exists.
9566   (if (not (assq article gnus-newsgroup-bookmarks))
9567       (gnus-message 6 "No bookmark in current article.")
9568     (gnus-pull article gnus-newsgroup-bookmarks)
9569     (gnus-message 6 "Removed bookmark.")))
9570
9571 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9572 (defun gnus-summary-mark-as-dormant (n)
9573   "Mark N articles forward as dormant.
9574 If N is negative, mark backward instead.  The difference between N and
9575 the actual number of articles marked is returned."
9576   (interactive "p")
9577   (gnus-summary-mark-forward n gnus-dormant-mark))
9578
9579 (defun gnus-summary-set-process-mark (article)
9580   "Set the process mark on ARTICLE and update the summary line."
9581   (setq gnus-newsgroup-processable
9582         (cons article
9583               (delq article gnus-newsgroup-processable)))
9584   (when (gnus-summary-goto-subject article)
9585     (gnus-summary-show-thread)
9586     (gnus-summary-goto-subject article)
9587     (gnus-summary-update-secondary-mark article)))
9588
9589 (defun gnus-summary-remove-process-mark (article)
9590   "Remove the process mark from ARTICLE and update the summary line."
9591   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9592   (when (gnus-summary-goto-subject article)
9593     (gnus-summary-show-thread)
9594     (gnus-summary-goto-subject article)
9595     (gnus-summary-update-secondary-mark article)))
9596
9597 (defun gnus-summary-set-saved-mark (article)
9598   "Set the process mark on ARTICLE and update the summary line."
9599   (push article gnus-newsgroup-saved)
9600   (when (gnus-summary-goto-subject article)
9601     (gnus-summary-update-secondary-mark article)))
9602
9603 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9604   "Mark N articles as read forwards.
9605 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9606 The difference between N and the actual number of articles marked is
9607 returned.
9608 If NO-EXPIRE, auto-expiry will be inhibited."
9609   (interactive "p")
9610   (gnus-summary-show-thread)
9611   (let ((backward (< n 0))
9612         (gnus-summary-goto-unread
9613          (and gnus-summary-goto-unread
9614               (not (eq gnus-summary-goto-unread 'never))
9615               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9616                                     gnus-ticked-mark gnus-dormant-mark)))))
9617         (n (abs n))
9618         (mark (or mark gnus-del-mark)))
9619     (while (and (> n 0)
9620                 (gnus-summary-mark-article nil mark no-expire)
9621                 (zerop (gnus-summary-next-subject
9622                         (if backward -1 1)
9623                         (and gnus-summary-goto-unread
9624                              (not (eq gnus-summary-goto-unread 'never)))
9625                         t)))
9626       (setq n (1- n)))
9627     (when (/= 0 n)
9628       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9629     (gnus-summary-recenter)
9630     (gnus-summary-position-point)
9631     (gnus-set-mode-line 'summary)
9632     n))
9633
9634 (defun gnus-summary-mark-article-as-read (mark)
9635   "Mark the current article quickly as read with MARK."
9636   (let ((article (gnus-summary-article-number)))
9637     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9638     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9639     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9640     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9641     (push (cons article mark) gnus-newsgroup-reads)
9642     ;; Possibly remove from cache, if that is used.
9643     (when gnus-use-cache
9644       (gnus-cache-enter-remove-article article))
9645     ;; Allow the backend to change the mark.
9646     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9647     ;; Check for auto-expiry.
9648     (when (and gnus-newsgroup-auto-expire
9649                (memq mark gnus-auto-expirable-marks))
9650       (setq mark gnus-expirable-mark)
9651       ;; Let the backend know about the mark change.
9652       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9653       (push article gnus-newsgroup-expirable))
9654     ;; Set the mark in the buffer.
9655     (gnus-summary-update-mark mark 'unread)
9656     t))
9657
9658 (defun gnus-summary-mark-article-as-unread (mark)
9659   "Mark the current article quickly as unread with MARK."
9660   (let* ((article (gnus-summary-article-number))
9661          (old-mark (gnus-summary-article-mark article)))
9662     ;; Allow the backend to change the mark.
9663     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9664     (if (eq mark old-mark)
9665         t
9666       (if (<= article 0)
9667           (progn
9668             (gnus-error 1 "Can't mark negative article numbers")
9669             nil)
9670         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9671         (setq gnus-newsgroup-spam-marked
9672               (delq article gnus-newsgroup-spam-marked))
9673         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9674         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9675         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9676         (cond ((= mark gnus-ticked-mark)
9677                (setq gnus-newsgroup-marked
9678                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9679                                               article)))
9680               ((= mark gnus-spam-mark)
9681                (setq gnus-newsgroup-spam-marked
9682                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9683                                               article)))
9684               ((= mark gnus-dormant-mark)
9685                (setq gnus-newsgroup-dormant
9686                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9687                                               article)))
9688               (t
9689                (setq gnus-newsgroup-unreads
9690                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9691                                               article))))
9692         (gnus-pull article gnus-newsgroup-reads)
9693
9694         ;; See whether the article is to be put in the cache.
9695         (and gnus-use-cache
9696              (vectorp (gnus-summary-article-header article))
9697              (save-excursion
9698                (gnus-cache-possibly-enter-article
9699                 gnus-newsgroup-name article
9700                 (= mark gnus-ticked-mark)
9701                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9702
9703         ;; Fix the mark.
9704         (gnus-summary-update-mark mark 'unread)
9705         t))))
9706
9707 (defun gnus-summary-mark-article (&optional article mark no-expire)
9708   "Mark ARTICLE with MARK.  MARK can be any character.
9709 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9710 `??' (dormant) and `?E' (expirable).
9711 If MARK is nil, then the default character `?r' is used.
9712 If ARTICLE is nil, then the article on the current line will be
9713 marked.
9714 If NO-EXPIRE, auto-expiry will be inhibited."
9715   ;; The mark might be a string.
9716   (when (stringp mark)
9717     (setq mark (aref mark 0)))
9718   ;; If no mark is given, then we check auto-expiring.
9719   (when (null mark)
9720     (setq mark gnus-del-mark))
9721   (when (and (not no-expire)
9722              gnus-newsgroup-auto-expire
9723              (memq mark gnus-auto-expirable-marks))
9724     (setq mark gnus-expirable-mark))
9725   (let ((article (or article (gnus-summary-article-number)))
9726         (old-mark (gnus-summary-article-mark article)))
9727     ;; Allow the backend to change the mark.
9728     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9729     (if (eq mark old-mark)
9730         t
9731       (unless article
9732         (error "No article on current line"))
9733       (if (not (if (or (= mark gnus-unread-mark)
9734                        (= mark gnus-ticked-mark)
9735                        (= mark gnus-spam-mark)
9736                        (= mark gnus-dormant-mark))
9737                    (gnus-mark-article-as-unread article mark)
9738                  (gnus-mark-article-as-read article mark)))
9739           t
9740         ;; See whether the article is to be put in the cache.
9741         (and gnus-use-cache
9742              (not (= mark gnus-canceled-mark))
9743              (vectorp (gnus-summary-article-header article))
9744              (save-excursion
9745                (gnus-cache-possibly-enter-article
9746                 gnus-newsgroup-name article
9747                 (= mark gnus-ticked-mark)
9748                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9749
9750         (when (gnus-summary-goto-subject article nil t)
9751           (let ((buffer-read-only nil))
9752             (gnus-summary-show-thread)
9753             ;; Fix the mark.
9754             (gnus-summary-update-mark mark 'unread)
9755             t))))))
9756
9757 (defun gnus-summary-update-secondary-mark (article)
9758   "Update the secondary (read, process, cache) mark."
9759   (gnus-summary-update-mark
9760    (cond ((memq article gnus-newsgroup-processable)
9761           gnus-process-mark)
9762          ((memq article gnus-newsgroup-cached)
9763           gnus-cached-mark)
9764          ((memq article gnus-newsgroup-replied)
9765           gnus-replied-mark)
9766          ((memq article gnus-newsgroup-forwarded)
9767           gnus-forwarded-mark)
9768          ((memq article gnus-newsgroup-saved)
9769           gnus-saved-mark)
9770          ((memq article gnus-newsgroup-recent)
9771           gnus-recent-mark)
9772          ((memq article gnus-newsgroup-unseen)
9773           gnus-unseen-mark)
9774          (t gnus-no-mark))
9775    'replied)
9776   (when (gnus-visual-p 'summary-highlight 'highlight)
9777     (gnus-run-hooks 'gnus-summary-update-hook))
9778   t)
9779
9780 (defun gnus-summary-update-download-mark (article)
9781   "Update the secondary (read, process, cache) mark."
9782   (gnus-summary-update-mark
9783    (cond ((memq article gnus-newsgroup-undownloaded) 
9784           gnus-undownloaded-mark)
9785          (gnus-newsgroup-agentized
9786           gnus-downloaded-mark)
9787          (t
9788           gnus-no-mark))
9789    'download)
9790   (gnus-summary-update-line t)
9791   t)
9792
9793 (defun gnus-summary-update-mark (mark type)
9794   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9795         (buffer-read-only nil))
9796     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9797     (when forward
9798       (when (looking-at "\r")
9799         (incf forward))
9800       (when (<= (+ forward (point)) (point-max))
9801         ;; Go to the right position on the line.
9802         (goto-char (+ forward (point)))
9803         ;; Replace the old mark with the new mark.
9804         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9805         ;; Optionally update the marks by some user rule.
9806         (when (eq type 'unread)
9807           (gnus-data-set-mark
9808            (gnus-data-find (gnus-summary-article-number)) mark)
9809           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9810
9811 (defun gnus-mark-article-as-read (article &optional mark)
9812   "Enter ARTICLE in the pertinent lists and remove it from others."
9813   ;; Make the article expirable.
9814   (let ((mark (or mark gnus-del-mark)))
9815     (setq gnus-newsgroup-expirable
9816           (if (= mark gnus-expirable-mark)
9817               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9818             (delq article gnus-newsgroup-expirable)))
9819     ;; Remove from unread and marked lists.
9820     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9821     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9822     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9823     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9824     (push (cons article mark) gnus-newsgroup-reads)
9825     ;; Possibly remove from cache, if that is used.
9826     (when gnus-use-cache
9827       (gnus-cache-enter-remove-article article))
9828     t))
9829
9830 (defun gnus-mark-article-as-unread (article &optional mark)
9831   "Enter ARTICLE in the pertinent lists and remove it from others."
9832   (let ((mark (or mark gnus-ticked-mark)))
9833     (if (<= article 0)
9834         (progn
9835           (gnus-error 1 "Can't mark negative article numbers")
9836           nil)
9837       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9838             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9839             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9840             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9841             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9842
9843       ;; Unsuppress duplicates?
9844       (when gnus-suppress-duplicates
9845         (gnus-dup-unsuppress-article article))
9846
9847       (cond ((= mark gnus-ticked-mark)
9848              (setq gnus-newsgroup-marked
9849                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9850             ((= mark gnus-spam-mark)
9851              (setq gnus-newsgroup-spam-marked
9852                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9853                                             article)))
9854             ((= mark gnus-dormant-mark)
9855              (setq gnus-newsgroup-dormant
9856                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9857             (t
9858              (setq gnus-newsgroup-unreads
9859                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9860       (gnus-pull article gnus-newsgroup-reads)
9861       t)))
9862
9863 (defalias 'gnus-summary-mark-as-unread-forward
9864   'gnus-summary-tick-article-forward)
9865 (make-obsolete 'gnus-summary-mark-as-unread-forward
9866                'gnus-summary-tick-article-forward)
9867 (defun gnus-summary-tick-article-forward (n)
9868   "Tick N articles forwards.
9869 If N is negative, tick backwards instead.
9870 The difference between N and the number of articles ticked is returned."
9871   (interactive "p")
9872   (gnus-summary-mark-forward n gnus-ticked-mark))
9873
9874 (defalias 'gnus-summary-mark-as-unread-backward
9875   'gnus-summary-tick-article-backward)
9876 (make-obsolete 'gnus-summary-mark-as-unread-backward
9877                'gnus-summary-tick-article-backward)
9878 (defun gnus-summary-tick-article-backward (n)
9879   "Tick N articles backwards.
9880 The difference between N and the number of articles ticked is returned."
9881   (interactive "p")
9882   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9883
9884 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9885 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9886 (defun gnus-summary-tick-article (&optional article clear-mark)
9887   "Mark current article as unread.
9888 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9889 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9890   (interactive)
9891   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9892                                        gnus-ticked-mark)))
9893
9894 (defun gnus-summary-mark-as-read-forward (n)
9895   "Mark N articles as read forwards.
9896 If N is negative, mark backwards instead.
9897 The difference between N and the actual number of articles marked is
9898 returned."
9899   (interactive "p")
9900   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9901
9902 (defun gnus-summary-mark-as-read-backward (n)
9903   "Mark the N articles as read backwards.
9904 The difference between N and the actual number of articles marked is
9905 returned."
9906   (interactive "p")
9907   (gnus-summary-mark-forward
9908    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9909
9910 (defun gnus-summary-mark-as-read (&optional article mark)
9911   "Mark current article as read.
9912 ARTICLE specifies the article to be marked as read.
9913 MARK specifies a string to be inserted at the beginning of the line."
9914   (gnus-summary-mark-article article mark))
9915
9916 (defun gnus-summary-clear-mark-forward (n)
9917   "Clear marks from N articles forward.
9918 If N is negative, clear backward instead.
9919 The difference between N and the number of marks cleared is returned."
9920   (interactive "p")
9921   (gnus-summary-mark-forward n gnus-unread-mark))
9922
9923 (defun gnus-summary-clear-mark-backward (n)
9924   "Clear marks from N articles backward.
9925 The difference between N and the number of marks cleared is returned."
9926   (interactive "p")
9927   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9928
9929 (defun gnus-summary-mark-unread-as-read ()
9930   "Intended to be used by `gnus-summary-mark-article-hook'."
9931   (when (memq gnus-current-article gnus-newsgroup-unreads)
9932     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9933
9934 (defun gnus-summary-mark-read-and-unread-as-read ()
9935   "Intended to be used by `gnus-summary-mark-article-hook'."
9936   (let ((mark (gnus-summary-article-mark)))
9937     (when (or (gnus-unread-mark-p mark)
9938               (gnus-read-mark-p mark))
9939       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9940
9941 (defun gnus-summary-mark-unread-as-ticked ()
9942   "Intended to be used by `gnus-summary-mark-article-hook'."
9943   (when (memq gnus-current-article gnus-newsgroup-unreads)
9944     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9945
9946 (defun gnus-summary-mark-region-as-read (point mark all)
9947   "Mark all unread articles between point and mark as read.
9948 If given a prefix, mark all articles between point and mark as read,
9949 even ticked and dormant ones."
9950   (interactive "r\nP")
9951   (save-excursion
9952     (let (article)
9953       (goto-char point)
9954       (beginning-of-line)
9955       (while (and
9956               (< (point) mark)
9957               (progn
9958                 (when (or all
9959                           (memq (setq article (gnus-summary-article-number))
9960                                 gnus-newsgroup-unreads))
9961                   (gnus-summary-mark-article article gnus-del-mark))
9962                 t)
9963               (gnus-summary-find-next))))))
9964
9965 (defun gnus-summary-mark-below (score mark)
9966   "Mark articles with score less than SCORE with MARK."
9967   (interactive "P\ncMark: ")
9968   (setq score (if score
9969                   (prefix-numeric-value score)
9970                 (or gnus-summary-default-score 0)))
9971   (save-excursion
9972     (set-buffer gnus-summary-buffer)
9973     (goto-char (point-min))
9974     (while
9975         (progn
9976           (and (< (gnus-summary-article-score) score)
9977                (gnus-summary-mark-article nil mark))
9978           (gnus-summary-find-next)))))
9979
9980 (defun gnus-summary-kill-below (&optional score)
9981   "Mark articles with score below SCORE as read."
9982   (interactive "P")
9983   (gnus-summary-mark-below score gnus-killed-mark))
9984
9985 (defun gnus-summary-clear-above (&optional score)
9986   "Clear all marks from articles with score above SCORE."
9987   (interactive "P")
9988   (gnus-summary-mark-above score gnus-unread-mark))
9989
9990 (defun gnus-summary-tick-above (&optional score)
9991   "Tick all articles with score above SCORE."
9992   (interactive "P")
9993   (gnus-summary-mark-above score gnus-ticked-mark))
9994
9995 (defun gnus-summary-mark-above (score mark)
9996   "Mark articles with score over SCORE with MARK."
9997   (interactive "P\ncMark: ")
9998   (setq score (if score
9999                   (prefix-numeric-value score)
10000                 (or gnus-summary-default-score 0)))
10001   (save-excursion
10002     (set-buffer gnus-summary-buffer)
10003     (goto-char (point-min))
10004     (while (and (progn
10005                   (when (> (gnus-summary-article-score) score)
10006                     (gnus-summary-mark-article nil mark))
10007                   t)
10008                 (gnus-summary-find-next)))))
10009
10010 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10011 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10012 (defun gnus-summary-limit-include-expunged (&optional no-error)
10013   "Display all the hidden articles that were expunged for low scores."
10014   (interactive)
10015   (let ((buffer-read-only nil))
10016     (let ((scored gnus-newsgroup-scored)
10017           headers h)
10018       (while scored
10019         (unless (gnus-summary-article-header (caar scored))
10020           (and (setq h (gnus-number-to-header (caar scored)))
10021                (< (cdar scored) gnus-summary-expunge-below)
10022                (push h headers)))
10023         (setq scored (cdr scored)))
10024       (if (not headers)
10025           (when (not no-error)
10026             (error "No expunged articles hidden"))
10027         (goto-char (point-min))
10028         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10029         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10030         (mapcar (lambda (x) (push (mail-header-number x)
10031                                   gnus-newsgroup-limit))
10032                 headers)
10033         (gnus-summary-prepare-unthreaded (nreverse headers))
10034         (goto-char (point-min))
10035         (gnus-summary-position-point)
10036         t))))
10037
10038 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10039   "Mark all unread articles in this newsgroup as read.
10040 If prefix argument ALL is non-nil, ticked and dormant articles will
10041 also be marked as read.
10042 If QUIETLY is non-nil, no questions will be asked.
10043 If TO-HERE is non-nil, it should be a point in the buffer.  All
10044 articles before (after, if REVERSE is set) this point will be marked as read.
10045 Note that this function will only catch up the unread article
10046 in the current summary buffer limitation.
10047 The number of articles marked as read is returned."
10048   (interactive "P")
10049   (prog1
10050       (save-excursion
10051         (when (or quietly
10052                   (not gnus-interactive-catchup) ;Without confirmation?
10053                   gnus-expert-user
10054                   (gnus-y-or-n-p
10055                    (if all
10056                        "Mark absolutely all articles as read? "
10057                      "Mark all unread articles as read? ")))
10058           (if (and not-mark
10059                    (not gnus-newsgroup-adaptive)
10060                    (not gnus-newsgroup-auto-expire)
10061                    (not gnus-suppress-duplicates)
10062                    (or (not gnus-use-cache)
10063                        (eq gnus-use-cache 'passive)))
10064               (progn
10065                 (when all
10066                   (setq gnus-newsgroup-marked nil
10067                         gnus-newsgroup-spam-marked nil
10068                         gnus-newsgroup-dormant nil))
10069                 (setq gnus-newsgroup-unreads
10070                       (gnus-sorted-nunion
10071                        (gnus-intersection gnus-newsgroup-unreads
10072                                           gnus-newsgroup-downloadable)
10073                        gnus-newsgroup-unfetched)))
10074             ;; We actually mark all articles as canceled, which we
10075             ;; have to do when using auto-expiry or adaptive scoring.
10076             (gnus-summary-show-all-threads)
10077             (if (and to-here reverse)
10078                 (progn
10079                   (goto-char to-here)
10080                   (while (and
10081                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
10082                           (gnus-summary-find-next (not all)))))
10083               (when (gnus-summary-first-subject (not all))
10084                 (while (and
10085                         (if to-here (< (point) to-here) t)
10086                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10087                         (gnus-summary-find-next (not all))))))
10088             (gnus-set-mode-line 'summary))
10089           t))
10090     (gnus-summary-position-point)))
10091
10092 (defun gnus-summary-catchup-to-here (&optional all)
10093   "Mark all unticked articles before the current one as read.
10094 If ALL is non-nil, also mark ticked and dormant articles as read."
10095   (interactive "P")
10096   (save-excursion
10097     (gnus-save-hidden-threads
10098       (let ((beg (point)))
10099         ;; We check that there are unread articles.
10100         (when (or all (gnus-summary-find-prev))
10101           (gnus-summary-catchup all t beg)))))
10102   (gnus-summary-position-point))
10103
10104 (defun gnus-summary-catchup-from-here (&optional all)
10105   "Mark all unticked articles after the current one as read.
10106 If ALL is non-nil, also mark ticked and dormant articles as read."
10107   (interactive "P")
10108   (save-excursion
10109     (gnus-save-hidden-threads
10110       (let ((beg (point)))
10111         ;; We check that there are unread articles.
10112         (when (or all (gnus-summary-find-next))
10113           (gnus-summary-catchup all t beg nil t)))))
10114
10115   (gnus-summary-position-point))
10116 (defun gnus-summary-catchup-all (&optional quietly)
10117   "Mark all articles in this newsgroup as read.
10118 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10119 instead, which marks only unread articles as read."
10120   (interactive "P")
10121   (gnus-summary-catchup t quietly))
10122
10123 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10124   "Mark all unread articles in this group as read, then exit.
10125 If prefix argument ALL is non-nil, all articles are marked as read.
10126 If QUIETLY is non-nil, no questions will be asked."
10127   (interactive "P")
10128   (when (gnus-summary-catchup all quietly nil 'fast)
10129     ;; Select next newsgroup or exit.
10130     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10131              (eq gnus-auto-select-next 'quietly))
10132         (gnus-summary-next-group nil)
10133       (gnus-summary-exit))))
10134
10135 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10136   "Mark all articles in this newsgroup as read, and then exit.
10137 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10138 instead, which marks only unread articles as read."
10139   (interactive "P")
10140   (gnus-summary-catchup-and-exit t quietly))
10141
10142 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10143   "Mark all articles in this group as read and select the next group.
10144 If given a prefix, mark all articles, unread as well as ticked, as
10145 read."
10146   (interactive "P")
10147   (save-excursion
10148     (gnus-summary-catchup all))
10149   (gnus-summary-next-group))
10150
10151 ;;;
10152 ;;; with article
10153 ;;;
10154
10155 (defmacro gnus-with-article (article &rest forms)
10156   "Select ARTICLE and perform FORMS in the original article buffer.
10157 Then replace the article with the result."
10158   `(progn
10159      ;; We don't want the article to be marked as read.
10160      (let (gnus-mark-article-hook)
10161        (gnus-summary-select-article t t nil ,article))
10162      (set-buffer gnus-original-article-buffer)
10163      ,@forms
10164      (if (not (gnus-check-backend-function
10165                'request-replace-article (car gnus-article-current)))
10166          (gnus-message 5 "Read-only group; not replacing")
10167        (unless (gnus-request-replace-article
10168                 ,article (car gnus-article-current)
10169                 (current-buffer) t)
10170          (error "Couldn't replace article")))
10171      ;; The cache and backlog have to be flushed somewhat.
10172      (when gnus-keep-backlog
10173        (gnus-backlog-remove-article
10174         (car gnus-article-current) (cdr gnus-article-current)))
10175      (when gnus-use-cache
10176        (gnus-cache-update-article
10177         (car gnus-article-current) (cdr gnus-article-current)))))
10178
10179 (put 'gnus-with-article 'lisp-indent-function 1)
10180 (put 'gnus-with-article 'edebug-form-spec '(form body))
10181
10182 ;; Thread-based commands.
10183
10184 (defun gnus-summary-articles-in-thread (&optional article)
10185   "Return a list of all articles in the current thread.
10186 If ARTICLE is non-nil, return all articles in the thread that starts
10187 with that article."
10188   (let* ((article (or article (gnus-summary-article-number)))
10189          (data (gnus-data-find-list article))
10190          (top-level (gnus-data-level (car data)))
10191          (top-subject
10192           (cond ((null gnus-thread-operation-ignore-subject)
10193                  (gnus-simplify-subject-re
10194                   (mail-header-subject (gnus-data-header (car data)))))
10195                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10196                  (gnus-simplify-subject-fuzzy
10197                   (mail-header-subject (gnus-data-header (car data)))))
10198                 (t nil)))
10199          (end-point (save-excursion
10200                       (if (gnus-summary-go-to-next-thread)
10201                           (point) (point-max))))
10202          articles)
10203     (while (and data
10204                 (< (gnus-data-pos (car data)) end-point))
10205       (when (or (not top-subject)
10206                 (string= top-subject
10207                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10208                              (gnus-simplify-subject-fuzzy
10209                               (mail-header-subject
10210                                (gnus-data-header (car data))))
10211                            (gnus-simplify-subject-re
10212                             (mail-header-subject
10213                              (gnus-data-header (car data)))))))
10214         (push (gnus-data-number (car data)) articles))
10215       (unless (and (setq data (cdr data))
10216                    (> (gnus-data-level (car data)) top-level))
10217         (setq data nil)))
10218     ;; Return the list of articles.
10219     (nreverse articles)))
10220
10221 (defun gnus-summary-rethread-current ()
10222   "Rethread the thread the current article is part of."
10223   (interactive)
10224   (let* ((gnus-show-threads t)
10225          (article (gnus-summary-article-number))
10226          (id (mail-header-id (gnus-summary-article-header)))
10227          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10228     (unless id
10229       (error "No article on the current line"))
10230     (gnus-rebuild-thread id)
10231     (gnus-summary-goto-subject article)))
10232
10233 (defun gnus-summary-reparent-thread ()
10234   "Make the current article child of the marked (or previous) article.
10235
10236 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10237 is non-nil or the Subject: of both articles are the same."
10238   (interactive)
10239   (unless (not (gnus-group-read-only-p))
10240     (error "The current newsgroup does not support article editing"))
10241   (unless (<= (length gnus-newsgroup-processable) 1)
10242     (error "No more than one article may be marked"))
10243   (save-window-excursion
10244     (let ((gnus-article-buffer " *reparent*")
10245           (current-article (gnus-summary-article-number))
10246           ;; First grab the marked article, otherwise one line up.
10247           (parent-article (if (not (null gnus-newsgroup-processable))
10248                               (car gnus-newsgroup-processable)
10249                             (save-excursion
10250                               (if (eq (forward-line -1) 0)
10251                                   (gnus-summary-article-number)
10252                                 (error "Beginning of summary buffer"))))))
10253       (unless (not (eq current-article parent-article))
10254         (error "An article may not be self-referential"))
10255       (let ((message-id (mail-header-id
10256                          (gnus-summary-article-header parent-article))))
10257         (unless (and message-id (not (equal message-id "")))
10258           (error "No message-id in desired parent"))
10259         (gnus-with-article current-article
10260           (save-restriction
10261             (goto-char (point-min))
10262             (message-narrow-to-head)
10263             (if (re-search-forward "^References: " nil t)
10264                 (progn
10265                   (re-search-forward "^[^ \t]" nil t)
10266                   (forward-line -1)
10267                   (end-of-line)
10268                   (insert " " message-id))
10269               (insert "References: " message-id "\n"))))
10270         (set-buffer gnus-summary-buffer)
10271         (gnus-summary-unmark-all-processable)
10272         (gnus-summary-update-article current-article)
10273         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10274             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10275         (gnus-summary-rethread-current)
10276         (gnus-message 3 "Article %d is now the child of article %d"
10277                       current-article parent-article)))))
10278
10279 (defun gnus-summary-toggle-threads (&optional arg)
10280   "Toggle showing conversation threads.
10281 If ARG is positive number, turn showing conversation threads on."
10282   (interactive "P")
10283   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10284     (setq gnus-show-threads
10285           (if (null arg) (not gnus-show-threads)
10286             (> (prefix-numeric-value arg) 0)))
10287     (gnus-summary-prepare)
10288     (gnus-summary-goto-subject current)
10289     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10290     (gnus-summary-position-point)))
10291
10292 (defun gnus-summary-show-all-threads ()
10293   "Show all threads."
10294   (interactive)
10295   (save-excursion
10296     (let ((buffer-read-only nil))
10297       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10298   (gnus-summary-position-point))
10299
10300 (defun gnus-summary-show-thread ()
10301   "Show thread subtrees.
10302 Returns nil if no thread was there to be shown."
10303   (interactive)
10304   (let ((buffer-read-only nil)
10305         (orig (point))
10306         ;; first goto end then to beg, to have point at beg after let
10307         (end (progn (end-of-line) (point)))
10308         (beg (progn (beginning-of-line) (point))))
10309     (prog1
10310         ;; Any hidden lines here?
10311         (search-forward "\r" end t)
10312       (subst-char-in-region beg end ?\^M ?\n t)
10313       (goto-char orig)
10314       (gnus-summary-position-point))))
10315
10316 (defun gnus-summary-maybe-hide-threads ()
10317   "If requested, hide the threads that should be hidden."
10318   (when (and gnus-show-threads
10319              gnus-thread-hide-subtree)
10320     (gnus-summary-hide-all-threads
10321      (if (or (consp gnus-thread-hide-subtree)
10322              (gnus-functionp gnus-thread-hide-subtree))
10323          (gnus-make-predicate gnus-thread-hide-subtree)
10324        nil))))
10325
10326 ;;; Hiding predicates.
10327
10328 (defun gnus-article-unread-p (header)
10329   (memq (mail-header-number header) gnus-newsgroup-unreads))
10330
10331 (defun gnus-article-unseen-p (header)
10332   (memq (mail-header-number header) gnus-newsgroup-unseen))
10333
10334 (defun gnus-map-articles (predicate articles)
10335   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10336   (apply 'gnus-or (mapcar predicate
10337                           (mapcar 'gnus-summary-article-header articles))))
10338
10339 (defun gnus-summary-hide-all-threads (&optional predicate)
10340   "Hide all thread subtrees.
10341 If PREDICATE is supplied, threads that satisfy this predicate
10342 will not be hidden."
10343   (interactive)
10344   (save-excursion
10345     (goto-char (point-min))
10346     (let ((end nil))
10347       (while (not end)
10348         (when (or (not predicate)
10349                   (gnus-map-articles
10350                    predicate (gnus-summary-article-children)))
10351             (gnus-summary-hide-thread))
10352         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10353   (gnus-summary-position-point))
10354
10355 (defun gnus-summary-hide-thread ()
10356   "Hide thread subtrees.
10357 If PREDICATE is supplied, threads that satisfy this predicate
10358 will not be hidden.
10359 Returns nil if no threads were there to be hidden."
10360   (interactive)
10361   (let ((buffer-read-only nil)
10362         (start (point))
10363         (article (gnus-summary-article-number)))
10364     (goto-char start)
10365     ;; Go forward until either the buffer ends or the subthread
10366     ;; ends.
10367     (when (and (not (eobp))
10368                (or (zerop (gnus-summary-next-thread 1 t))
10369                    (goto-char (point-max))))
10370       (prog1
10371           (if (and (> (point) start)
10372                    (search-backward "\n" start t))
10373               (progn
10374                 (subst-char-in-region start (point) ?\n ?\^M)
10375                 (gnus-summary-goto-subject article))
10376             (goto-char start)
10377             nil)))))
10378
10379 (defun gnus-summary-go-to-next-thread (&optional previous)
10380   "Go to the same level (or less) next thread.
10381 If PREVIOUS is non-nil, go to previous thread instead.
10382 Return the article number moved to, or nil if moving was impossible."
10383   (let ((level (gnus-summary-thread-level))
10384         (way (if previous -1 1))
10385         (beg (point)))
10386     (forward-line way)
10387     (while (and (not (eobp))
10388                 (< level (gnus-summary-thread-level)))
10389       (forward-line way))
10390     (if (eobp)
10391         (progn
10392           (goto-char beg)
10393           nil)
10394       (setq beg (point))
10395       (prog1
10396           (gnus-summary-article-number)
10397         (goto-char beg)))))
10398
10399 (defun gnus-summary-next-thread (n &optional silent)
10400   "Go to the same level next N'th thread.
10401 If N is negative, search backward instead.
10402 Returns the difference between N and the number of skips actually
10403 done.
10404
10405 If SILENT, don't output messages."
10406   (interactive "p")
10407   (let ((backward (< n 0))
10408         (n (abs n)))
10409     (while (and (> n 0)
10410                 (gnus-summary-go-to-next-thread backward))
10411       (decf n))
10412     (unless silent
10413       (gnus-summary-position-point))
10414     (when (and (not silent) (/= 0 n))
10415       (gnus-message 7 "No more threads"))
10416     n))
10417
10418 (defun gnus-summary-prev-thread (n)
10419   "Go to the same level previous N'th thread.
10420 Returns the difference between N and the number of skips actually
10421 done."
10422   (interactive "p")
10423   (gnus-summary-next-thread (- n)))
10424
10425 (defun gnus-summary-go-down-thread ()
10426   "Go down one level in the current thread."
10427   (let ((children (gnus-summary-article-children)))
10428     (when children
10429       (gnus-summary-goto-subject (car children)))))
10430
10431 (defun gnus-summary-go-up-thread ()
10432   "Go up one level in the current thread."
10433   (let ((parent (gnus-summary-article-parent)))
10434     (when parent
10435       (gnus-summary-goto-subject parent))))
10436
10437 (defun gnus-summary-down-thread (n)
10438   "Go down thread N steps.
10439 If N is negative, go up instead.
10440 Returns the difference between N and how many steps down that were
10441 taken."
10442   (interactive "p")
10443   (let ((up (< n 0))
10444         (n (abs n)))
10445     (while (and (> n 0)
10446                 (if up (gnus-summary-go-up-thread)
10447                   (gnus-summary-go-down-thread)))
10448       (setq n (1- n)))
10449     (gnus-summary-position-point)
10450     (when (/= 0 n)
10451       (gnus-message 7 "Can't go further"))
10452     n))
10453
10454 (defun gnus-summary-up-thread (n)
10455   "Go up thread N steps.
10456 If N is negative, go down instead.
10457 Returns the difference between N and how many steps down that were
10458 taken."
10459   (interactive "p")
10460   (gnus-summary-down-thread (- n)))
10461
10462 (defun gnus-summary-top-thread ()
10463   "Go to the top of the thread."
10464   (interactive)
10465   (while (gnus-summary-go-up-thread))
10466   (gnus-summary-article-number))
10467
10468 (defun gnus-summary-kill-thread (&optional unmark)
10469   "Mark articles under current thread as read.
10470 If the prefix argument is positive, remove any kinds of marks.
10471 If the prefix argument is negative, tick articles instead."
10472   (interactive "P")
10473   (when unmark
10474     (setq unmark (prefix-numeric-value unmark)))
10475   (let ((articles (gnus-summary-articles-in-thread)))
10476     (save-excursion
10477       ;; Expand the thread.
10478       (gnus-summary-show-thread)
10479       ;; Mark all the articles.
10480       (while articles
10481         (gnus-summary-goto-subject (car articles))
10482         (cond ((null unmark)
10483                (gnus-summary-mark-article-as-read gnus-killed-mark))
10484               ((> unmark 0)
10485                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10486               (t
10487                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10488         (setq articles (cdr articles))))
10489     ;; Hide killed subtrees.
10490     (and (null unmark)
10491          gnus-thread-hide-killed
10492          (gnus-summary-hide-thread))
10493     ;; If marked as read, go to next unread subject.
10494     (when (null unmark)
10495       ;; Go to next unread subject.
10496       (gnus-summary-next-subject 1 t)))
10497   (gnus-set-mode-line 'summary))
10498
10499 ;; Summary sorting commands
10500
10501 (defun gnus-summary-sort-by-number (&optional reverse)
10502   "Sort the summary buffer by article number.
10503 Argument REVERSE means reverse order."
10504   (interactive "P")
10505   (gnus-summary-sort 'number reverse))
10506
10507 (defun gnus-summary-sort-by-random (&optional reverse)
10508   "Randomize the order in the summary buffer.
10509 Argument REVERSE means to randomize in reverse order."
10510   (interactive "P")
10511   (gnus-summary-sort 'random reverse))
10512
10513 (defun gnus-summary-sort-by-author (&optional reverse)
10514   "Sort the summary buffer by author name alphabetically.
10515 If `case-fold-search' is non-nil, case of letters is ignored.
10516 Argument REVERSE means reverse order."
10517   (interactive "P")
10518   (gnus-summary-sort 'author reverse))
10519
10520 (defun gnus-summary-sort-by-subject (&optional reverse)
10521   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10522 If `case-fold-search' is non-nil, case of letters is ignored.
10523 Argument REVERSE means reverse order."
10524   (interactive "P")
10525   (gnus-summary-sort 'subject reverse))
10526
10527 (defun gnus-summary-sort-by-date (&optional reverse)
10528   "Sort the summary buffer by date.
10529 Argument REVERSE means reverse order."
10530   (interactive "P")
10531   (gnus-summary-sort 'date reverse))
10532
10533 (defun gnus-summary-sort-by-score (&optional reverse)
10534   "Sort the summary buffer by score.
10535 Argument REVERSE means reverse order."
10536   (interactive "P")
10537   (gnus-summary-sort 'score reverse))
10538
10539 (defun gnus-summary-sort-by-lines (&optional reverse)
10540   "Sort the summary buffer by the number of lines.
10541 Argument REVERSE means reverse order."
10542   (interactive "P")
10543   (gnus-summary-sort 'lines reverse))
10544
10545 (defun gnus-summary-sort-by-chars (&optional reverse)
10546   "Sort the summary buffer by article length.
10547 Argument REVERSE means reverse order."
10548   (interactive "P")
10549   (gnus-summary-sort 'chars reverse))
10550
10551 (defun gnus-summary-sort-by-original (&optional reverse)
10552   "Sort the summary buffer using the default sorting method.
10553 Argument REVERSE means reverse order."
10554   (interactive "P")
10555   (let* ((buffer-read-only)
10556          (gnus-summary-prepare-hook nil))
10557     ;; We do the sorting by regenerating the threads.
10558     (gnus-summary-prepare)
10559     ;; Hide subthreads if needed.
10560     (gnus-summary-maybe-hide-threads)))
10561
10562 (defun gnus-summary-sort (predicate reverse)
10563   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10564   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10565          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10566          (gnus-thread-sort-functions
10567           (if (not reverse)
10568               thread
10569             `(lambda (t1 t2)
10570                (,thread t2 t1))))
10571          (gnus-sort-gathered-threads-function
10572           gnus-thread-sort-functions)
10573          (gnus-article-sort-functions
10574           (if (not reverse)
10575               article
10576             `(lambda (t1 t2)
10577                (,article t2 t1))))
10578          (buffer-read-only)
10579          (gnus-summary-prepare-hook nil))
10580     ;; We do the sorting by regenerating the threads.
10581     (gnus-summary-prepare)
10582     ;; Hide subthreads if needed.
10583     (gnus-summary-maybe-hide-threads)))
10584
10585 ;; Summary saving commands.
10586
10587 (defun gnus-summary-save-article (&optional n not-saved)
10588   "Save the current article using the default saver function.
10589 If N is a positive number, save the N next articles.
10590 If N is a negative number, save the N previous articles.
10591 If N is nil and any articles have been marked with the process mark,
10592 save those articles instead.
10593 The variable `gnus-default-article-saver' specifies the saver function."
10594   (interactive "P")
10595   (let* ((articles (gnus-summary-work-articles n))
10596          (save-buffer (save-excursion
10597                         (nnheader-set-temp-buffer " *Gnus Save*")))
10598          (num (length articles))
10599          header file)
10600     (dolist (article articles)
10601       (setq header (gnus-summary-article-header article))
10602       (if (not (vectorp header))
10603           ;; This is a pseudo-article.
10604           (if (assq 'name header)
10605               (gnus-copy-file (cdr (assq 'name header)))
10606             (gnus-message 1 "Article %d is unsaveable" article))
10607         ;; This is a real article.
10608         (save-window-excursion
10609           (let ((gnus-display-mime-function nil)
10610                 (gnus-article-prepare-hook nil))
10611             (gnus-summary-select-article t nil nil article)))
10612         (save-excursion
10613           (set-buffer save-buffer)
10614           (erase-buffer)
10615           (insert-buffer-substring gnus-original-article-buffer))
10616         (setq file (gnus-article-save save-buffer file num))
10617         (gnus-summary-remove-process-mark article)
10618         (unless not-saved
10619           (gnus-summary-set-saved-mark article))))
10620     (gnus-kill-buffer save-buffer)
10621     (gnus-summary-position-point)
10622     (gnus-set-mode-line 'summary)
10623     n))
10624
10625 (defun gnus-summary-pipe-output (&optional arg headers)
10626   "Pipe the current article to a subprocess.
10627 If N is a positive number, pipe the N next articles.
10628 If N is a negative number, pipe the N previous articles.
10629 If N is nil and any articles have been marked with the process mark,
10630 pipe those articles instead.
10631 If HEADERS (the symbolic prefix), include the headers, too."
10632   (interactive (gnus-interactive "P\ny"))
10633   (require 'gnus-art)
10634   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10635         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10636     (gnus-summary-save-article arg t))
10637   (let ((buffer (get-buffer "*Shell Command Output*")))
10638     (when (and buffer
10639                (not (zerop (buffer-size buffer))))
10640       (gnus-configure-windows 'pipe))))
10641
10642 (defun gnus-summary-save-article-mail (&optional arg)
10643   "Append the current article to an mail file.
10644 If N is a positive number, save the N next articles.
10645 If N is a negative number, save the N previous articles.
10646 If N is nil and any articles have been marked with the process mark,
10647 save those articles instead."
10648   (interactive "P")
10649   (require 'gnus-art)
10650   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10651     (gnus-summary-save-article arg)))
10652
10653 (defun gnus-summary-save-article-rmail (&optional arg)
10654   "Append the current article to an rmail file.
10655 If N is a positive number, save the N next articles.
10656 If N is a negative number, save the N previous articles.
10657 If N is nil and any articles have been marked with the process mark,
10658 save those articles instead."
10659   (interactive "P")
10660   (require 'gnus-art)
10661   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10662     (gnus-summary-save-article arg)))
10663
10664 (defun gnus-summary-save-article-file (&optional arg)
10665   "Append the current article to a file.
10666 If N is a positive number, save the N next articles.
10667 If N is a negative number, save the N previous articles.
10668 If N is nil and any articles have been marked with the process mark,
10669 save those articles instead."
10670   (interactive "P")
10671   (require 'gnus-art)
10672   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10673     (gnus-summary-save-article arg)))
10674
10675 (defun gnus-summary-write-article-file (&optional arg)
10676   "Write the current article to a file, deleting the previous file.
10677 If N is a positive number, save the N next articles.
10678 If N is a negative number, save the N previous articles.
10679 If N is nil and any articles have been marked with the process mark,
10680 save those articles instead."
10681   (interactive "P")
10682   (require 'gnus-art)
10683   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10684     (gnus-summary-save-article arg)))
10685
10686 (defun gnus-summary-save-article-body-file (&optional arg)
10687   "Append the current article body to a file.
10688 If N is a positive number, save the N next articles.
10689 If N is a negative number, save the N previous articles.
10690 If N is nil and any articles have been marked with the process mark,
10691 save those articles instead."
10692   (interactive "P")
10693   (require 'gnus-art)
10694   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10695     (gnus-summary-save-article arg)))
10696
10697 (defun gnus-summary-muttprint (&optional arg)
10698   "Print the current article using Muttprint.
10699 If N is a positive number, save the N next articles.
10700 If N is a negative number, save the N previous articles.
10701 If N is nil and any articles have been marked with the process mark,
10702 save those articles instead."
10703   (interactive "P")
10704   (require 'gnus-art)
10705   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10706     (gnus-summary-save-article arg t)))
10707
10708 (defun gnus-summary-pipe-message (program)
10709   "Pipe the current article through PROGRAM."
10710   (interactive "sProgram: ")
10711   (gnus-summary-select-article)
10712   (let ((mail-header-separator ""))
10713     (gnus-eval-in-buffer-window gnus-article-buffer
10714       (save-restriction
10715         (widen)
10716         (let ((start (window-start))
10717               buffer-read-only)
10718           (message-pipe-buffer-body program)
10719           (set-window-start (get-buffer-window (current-buffer)) start))))))
10720
10721 (defun gnus-get-split-value (methods)
10722   "Return a value based on the split METHODS."
10723   (let (split-name method result match)
10724     (when methods
10725       (save-excursion
10726         (set-buffer gnus-original-article-buffer)
10727         (save-restriction
10728           (nnheader-narrow-to-headers)
10729           (while (and methods (not split-name))
10730             (goto-char (point-min))
10731             (setq method (pop methods))
10732             (setq match (car method))
10733             (when (cond
10734                    ((stringp match)
10735                     ;; Regular expression.
10736                     (ignore-errors
10737                       (re-search-forward match nil t)))
10738                    ((gnus-functionp match)
10739                     ;; Function.
10740                     (save-restriction
10741                       (widen)
10742                       (setq result (funcall match gnus-newsgroup-name))))
10743                    ((consp match)
10744                     ;; Form.
10745                     (save-restriction
10746                       (widen)
10747                       (setq result (eval match)))))
10748               (setq split-name (cdr method))
10749               (cond ((stringp result)
10750                      (push (expand-file-name
10751                             result gnus-article-save-directory)
10752                            split-name))
10753                     ((consp result)
10754                      (setq split-name (append result split-name)))))))))
10755     (nreverse split-name)))
10756
10757 (defun gnus-valid-move-group-p (group)
10758   (and (boundp group)
10759        (symbol-name group)
10760        (symbol-value group)
10761        (gnus-get-function (gnus-find-method-for-group
10762                            (symbol-name group)) 'request-accept-article t)))
10763
10764 (defun gnus-read-move-group-name (prompt default articles prefix)
10765   "Read a group name."
10766   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10767          (minibuffer-confirm-incomplete nil) ; XEmacs
10768          (prom
10769           (format "%s %s to:"
10770                   prompt
10771                   (if (> (length articles) 1)
10772                       (format "these %d articles" (length articles))
10773                     "this article")))
10774          (to-newsgroup
10775           (cond
10776            ((null split-name)
10777             (gnus-completing-read-with-default
10778              default prom
10779              gnus-active-hashtb
10780              'gnus-valid-move-group-p
10781              nil prefix
10782              'gnus-group-history))
10783            ((= 1 (length split-name))
10784             (gnus-completing-read-with-default
10785              (car split-name) prom
10786              gnus-active-hashtb
10787              'gnus-valid-move-group-p
10788              nil nil
10789              'gnus-group-history))
10790            (t
10791             (gnus-completing-read-with-default
10792              nil prom
10793              (mapcar (lambda (el) (list el))
10794                      (nreverse split-name))
10795              nil nil nil
10796              'gnus-group-history))))
10797          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10798     (when to-newsgroup
10799       (if (or (string= to-newsgroup "")
10800               (string= to-newsgroup prefix))
10801           (setq to-newsgroup default))
10802       (unless to-newsgroup
10803         (error "No group name entered"))
10804       (or (gnus-active to-newsgroup)
10805           (gnus-activate-group to-newsgroup nil nil to-method)
10806           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10807                                      to-newsgroup))
10808               (or (and (gnus-request-create-group to-newsgroup to-method)
10809                        (gnus-activate-group
10810                         to-newsgroup nil nil to-method)
10811                        (gnus-subscribe-group to-newsgroup))
10812                   (error "Couldn't create group %s" to-newsgroup)))
10813           (error "No such group: %s" to-newsgroup)))
10814     to-newsgroup))
10815
10816 (defun gnus-summary-save-parts (type dir n &optional reverse)
10817   "Save parts matching TYPE to DIR.
10818 If REVERSE, save parts that do not match TYPE."
10819   (interactive
10820    (list (read-string "Save parts of type: "
10821                       (or (car gnus-summary-save-parts-type-history)
10822                           gnus-summary-save-parts-default-mime)
10823                       'gnus-summary-save-parts-type-history)
10824          (setq gnus-summary-save-parts-last-directory
10825                (read-file-name "Save to directory: "
10826                                gnus-summary-save-parts-last-directory
10827                                nil t))
10828          current-prefix-arg))
10829   (gnus-summary-iterate n
10830     (let ((gnus-display-mime-function nil)
10831           (gnus-inhibit-treatment t))
10832       (gnus-summary-select-article))
10833     (save-excursion
10834       (set-buffer gnus-article-buffer)
10835       (let ((handles (or gnus-article-mime-handles
10836                          (mm-dissect-buffer nil gnus-article-loose-mime)
10837                          (and gnus-article-emulate-mime
10838                               (mm-uu-dissect)))))
10839         (when handles
10840           (gnus-summary-save-parts-1 type dir handles reverse)
10841           (unless gnus-article-mime-handles ;; Don't destroy this case.
10842             (mm-destroy-parts handles)))))))
10843
10844 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10845   (if (stringp (car handle))
10846       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10847               (cdr handle))
10848     (when (if reverse
10849               (not (string-match type (mm-handle-media-type handle)))
10850             (string-match type (mm-handle-media-type handle)))
10851       (let ((file (expand-file-name
10852                    (file-name-nondirectory
10853                     (or
10854                      (mail-content-type-get
10855                       (mm-handle-disposition handle) 'filename)
10856                      (concat gnus-newsgroup-name
10857                              "." (number-to-string
10858                                   (cdr gnus-article-current)))))
10859                    dir)))
10860         (unless (file-exists-p file)
10861           (mm-save-part-to-file handle file))))))
10862
10863 ;; Summary extract commands
10864
10865 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10866   (let ((buffer-read-only nil)
10867         (article (gnus-summary-article-number))
10868         after-article b e)
10869     (unless (gnus-summary-goto-subject article)
10870       (error "No such article: %d" article))
10871     (gnus-summary-position-point)
10872     ;; If all commands are to be bunched up on one line, we collect
10873     ;; them here.
10874     (unless gnus-view-pseudos-separately
10875       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10876             files action)
10877         (while ps
10878           (setq action (cdr (assq 'action (car ps))))
10879           (setq files (list (cdr (assq 'name (car ps)))))
10880           (while (and ps (cdr ps)
10881                       (string= (or action "1")
10882                                (or (cdr (assq 'action (cadr ps))) "2")))
10883             (push (cdr (assq 'name (cadr ps))) files)
10884             (setcdr ps (cddr ps)))
10885           (when files
10886             (when (not (string-match "%s" action))
10887               (push " " files))
10888             (push " " files)
10889             (when (assq 'execute (car ps))
10890               (setcdr (assq 'execute (car ps))
10891                       (funcall (if (string-match "%s" action)
10892                                    'format 'concat)
10893                                action
10894                                (mapconcat
10895                                 (lambda (f)
10896                                   (if (equal f " ")
10897                                       f
10898                                     (mm-quote-arg f)))
10899                                 files " ")))))
10900           (setq ps (cdr ps)))))
10901     (if (and gnus-view-pseudos (not not-view))
10902         (while pslist
10903           (when (assq 'execute (car pslist))
10904             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10905                                   (eq gnus-view-pseudos 'not-confirm)))
10906           (setq pslist (cdr pslist)))
10907       (save-excursion
10908         (while pslist
10909           (setq after-article (or (cdr (assq 'article (car pslist)))
10910                                   (gnus-summary-article-number)))
10911           (gnus-summary-goto-subject after-article)
10912           (forward-line 1)
10913           (setq b (point))
10914           (insert "    " (file-name-nondirectory
10915                           (cdr (assq 'name (car pslist))))
10916                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10917           (setq e (point))
10918           (forward-line -1)             ; back to `b'
10919           (gnus-add-text-properties
10920            b (1- e) (list 'gnus-number gnus-reffed-article-number
10921                           gnus-mouse-face-prop gnus-mouse-face))
10922           (gnus-data-enter
10923            after-article gnus-reffed-article-number
10924            gnus-unread-mark b (car pslist) 0 (- e b))
10925           (setq gnus-newsgroup-unreads
10926                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10927                                          gnus-reffed-article-number))
10928           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10929           (setq pslist (cdr pslist)))))))
10930
10931 (defun gnus-pseudos< (p1 p2)
10932   (let ((c1 (cdr (assq 'action p1)))
10933         (c2 (cdr (assq 'action p2))))
10934     (and c1 c2 (string< c1 c2))))
10935
10936 (defun gnus-request-pseudo-article (props)
10937   (cond ((assq 'execute props)
10938          (gnus-execute-command (cdr (assq 'execute props)))))
10939   (let ((gnus-current-article (gnus-summary-article-number)))
10940     (gnus-run-hooks 'gnus-mark-article-hook)))
10941
10942 (defun gnus-execute-command (command &optional automatic)
10943   (save-excursion
10944     (gnus-article-setup-buffer)
10945     (set-buffer gnus-article-buffer)
10946     (setq buffer-read-only nil)
10947     (let ((command (if automatic command
10948                      (read-string "Command: " (cons command 0)))))
10949       (erase-buffer)
10950       (insert "$ " command "\n\n")
10951       (if gnus-view-pseudo-asynchronously
10952           (start-process "gnus-execute" (current-buffer) shell-file-name
10953                          shell-command-switch command)
10954         (call-process shell-file-name nil t nil
10955                       shell-command-switch command)))))
10956
10957 ;; Summary kill commands.
10958
10959 (defun gnus-summary-edit-global-kill (article)
10960   "Edit the \"global\" kill file."
10961   (interactive (list (gnus-summary-article-number)))
10962   (gnus-group-edit-global-kill article))
10963
10964 (defun gnus-summary-edit-local-kill ()
10965   "Edit a local kill file applied to the current newsgroup."
10966   (interactive)
10967   (setq gnus-current-headers (gnus-summary-article-header))
10968   (gnus-group-edit-local-kill
10969    (gnus-summary-article-number) gnus-newsgroup-name))
10970
10971 ;;; Header reading.
10972
10973 (defun gnus-read-header (id &optional header)
10974   "Read the headers of article ID and enter them into the Gnus system."
10975   (let ((group gnus-newsgroup-name)
10976         (gnus-override-method
10977          (or
10978           gnus-override-method
10979           (and (gnus-news-group-p gnus-newsgroup-name)
10980                (car (gnus-refer-article-methods)))))
10981         where)
10982     ;; First we check to see whether the header in question is already
10983     ;; fetched.
10984     (if (stringp id)
10985         ;; This is a Message-ID.
10986         (setq header (or header (gnus-id-to-header id)))
10987       ;; This is an article number.
10988       (setq header (or header (gnus-summary-article-header id))))
10989     (if (and header
10990              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10991         ;; We have found the header.
10992         header
10993       ;; If this is a sparse article, we have to nix out its
10994       ;; previous entry in the thread hashtb.
10995       (when (and header
10996                  (gnus-summary-article-sparse-p (mail-header-number header)))
10997         (let* ((parent (gnus-parent-id (mail-header-references header)))
10998                (thread (and parent (gnus-id-to-thread parent))))
10999           (when thread
11000             (delq (assq header thread) thread))))
11001       ;; We have to really fetch the header to this article.
11002       (save-excursion
11003         (set-buffer nntp-server-buffer)
11004         (when (setq where (gnus-request-head id group))
11005           (nnheader-fold-continuation-lines)
11006           (goto-char (point-max))
11007           (insert ".\n")
11008           (goto-char (point-min))
11009           (insert "211 ")
11010           (princ (cond
11011                   ((numberp id) id)
11012                   ((cdr where) (cdr where))
11013                   (header (mail-header-number header))
11014                   (t gnus-reffed-article-number))
11015                  (current-buffer))
11016           (insert " Article retrieved.\n"))
11017         (if (or (not where)
11018                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11019             ()                          ; Malformed head.
11020           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11021             (when (and (stringp id)
11022                        (not (string= (gnus-group-real-name group)
11023                                      (car where))))
11024               ;; If we fetched by Message-ID and the article came
11025               ;; from a different group, we fudge some bogus article
11026               ;; numbers for this article.
11027               (mail-header-set-number header gnus-reffed-article-number))
11028             (save-excursion
11029               (set-buffer gnus-summary-buffer)
11030               (decf gnus-reffed-article-number)
11031               (gnus-remove-header (mail-header-number header))
11032               (push header gnus-newsgroup-headers)
11033               (setq gnus-current-headers header)
11034               (push (mail-header-number header) gnus-newsgroup-limit)))
11035           header)))))
11036
11037 (defun gnus-remove-header (number)
11038   "Remove header NUMBER from `gnus-newsgroup-headers'."
11039   (if (and gnus-newsgroup-headers
11040            (= number (mail-header-number (car gnus-newsgroup-headers))))
11041       (pop gnus-newsgroup-headers)
11042     (let ((headers gnus-newsgroup-headers))
11043       (while (and (cdr headers)
11044                   (not (= number (mail-header-number (cadr headers)))))
11045         (pop headers))
11046       (when (cdr headers)
11047         (setcdr headers (cddr headers))))))
11048
11049 ;;;
11050 ;;; summary highlights
11051 ;;;
11052
11053 (defun gnus-highlight-selected-summary ()
11054   "Highlight selected article in summary buffer."
11055   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11056   (when gnus-summary-selected-face
11057     (save-excursion
11058       (let* ((beg (progn (beginning-of-line) (point)))
11059              (end (progn (end-of-line) (point)))
11060              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11061              (from (if (get-text-property beg gnus-mouse-face-prop)
11062                        beg
11063                      (or (next-single-property-change
11064                           beg gnus-mouse-face-prop nil end)
11065                          beg)))
11066              (to
11067               (if (= from end)
11068                   (- from 2)
11069                 (or (next-single-property-change
11070                      from gnus-mouse-face-prop nil end)
11071                     end))))
11072         ;; If no mouse-face prop on line we will have to = from = end,
11073         ;; so we highlight the entire line instead.
11074         (when (= (+ to 2) from)
11075           (setq from beg)
11076           (setq to end))
11077         (if gnus-newsgroup-selected-overlay
11078             ;; Move old overlay.
11079             (gnus-move-overlay
11080              gnus-newsgroup-selected-overlay from to (current-buffer))
11081           ;; Create new overlay.
11082           (gnus-overlay-put
11083            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11084            'face gnus-summary-selected-face))))))
11085
11086 (defvar gnus-summary-highlight-line-cached nil)
11087 (defvar gnus-summary-highlight-line-trigger nil)
11088
11089 (defun gnus-summary-highlight-line-0 ()
11090   (if (and (eq gnus-summary-highlight-line-trigger 
11091                gnus-summary-highlight)
11092            gnus-summary-highlight-line-cached)
11093       gnus-summary-highlight-line-cached
11094     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11095           gnus-summary-highlight-line-cached
11096           (let* ((cond (list 'cond))
11097                  (c cond)
11098                  (list gnus-summary-highlight))
11099             (while list
11100               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11101               (setq c (cdr c)
11102                     list (cdr list)))
11103             (gnus-byte-compile (list 'lambda nil cond))))))
11104
11105 (defun gnus-summary-highlight-line ()
11106   "Highlight current line according to `gnus-summary-highlight'."
11107   (let* ((beg (gnus-point-at-bol))
11108          (article (or (gnus-summary-article-number) gnus-current-article))
11109          (score (or (cdr (assq article
11110                                gnus-newsgroup-scored))
11111                     gnus-summary-default-score 0))
11112          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11113          (inhibit-read-only t)
11114          (default gnus-summary-default-score)
11115          (default-high gnus-summary-default-high-score)
11116          (default-low gnus-summary-default-low-score)
11117          (uncached (memq article gnus-newsgroup-undownloaded))
11118          (downloaded (not uncached)))
11119     (let ((face (funcall (gnus-summary-highlight-line-0))))
11120       (unless (eq face (get-text-property beg 'face))
11121         (gnus-put-text-property-excluding-characters-with-faces
11122          beg (gnus-point-at-eol) 'face
11123          (setq face (if (boundp face) (symbol-value face) face)))
11124         (when gnus-summary-highlight-line-function
11125           (funcall gnus-summary-highlight-line-function article face))))))
11126
11127 (defun gnus-update-read-articles (group unread &optional compute)
11128   "Update the list of read articles in GROUP.
11129 UNREAD is a sorted list."
11130   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11131          (entry (gnus-gethash group gnus-newsrc-hashtb))
11132          (info (nth 2 entry))
11133          (prev 1)
11134          read)
11135     (if (or (not info) (not active))
11136         ;; There is no info on this group if it was, in fact,
11137         ;; killed.  Gnus stores no information on killed groups, so
11138         ;; there's nothing to be done.
11139         ;; One could store the information somewhere temporarily,
11140         ;; perhaps...  Hmmm...
11141         ()
11142       ;; Remove any negative articles numbers.
11143       (while (and unread (< (car unread) 0))
11144         (setq unread (cdr unread)))
11145       ;; Remove any expired article numbers
11146       (while (and unread (< (car unread) (car active)))
11147         (setq unread (cdr unread)))
11148       ;; Compute the ranges of read articles by looking at the list of
11149       ;; unread articles.
11150       (while unread
11151         (when (/= (car unread) prev)
11152           (push (if (= prev (1- (car unread))) prev
11153                   (cons prev (1- (car unread))))
11154                 read))
11155         (setq prev (1+ (car unread)))
11156         (setq unread (cdr unread)))
11157       (when (<= prev (cdr active))
11158         (push (cons prev (cdr active)) read))
11159       (setq read (if (> (length read) 1) (nreverse read) read))
11160       (if compute
11161           read
11162         (save-excursion
11163           (let (setmarkundo)
11164             ;; Propagate the read marks to the backend.
11165             (when (gnus-check-backend-function 'request-set-mark group)
11166               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11167                     (add (gnus-remove-from-range read (gnus-info-read info))))
11168                 (when (or add del)
11169                   (unless (gnus-check-group group)
11170                     (error "Can't open server for %s" group))
11171                   (gnus-request-set-mark
11172                    group (delq nil (list (if add (list add 'add '(read)))
11173                                          (if del (list del 'del '(read))))))
11174                   (setq setmarkundo
11175                         `(gnus-request-set-mark
11176                           ,group
11177                           ',(delq nil (list
11178                                        (if del (list del 'add '(read)))
11179                                        (if add (list add 'del '(read))))))))))
11180             (set-buffer gnus-group-buffer)
11181             (gnus-undo-register
11182               `(progn
11183                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11184                  (gnus-info-set-read ',info ',(gnus-info-read info))
11185                  (gnus-get-unread-articles-in-group ',info
11186                                                     (gnus-active ,group))
11187                  (gnus-group-update-group ,group t)
11188                  ,setmarkundo))))
11189         ;; Enter this list into the group info.
11190         (gnus-info-set-read info read)
11191         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11192         (gnus-get-unread-articles-in-group info (gnus-active group))
11193         t))))
11194
11195 (defun gnus-offer-save-summaries ()
11196   "Offer to save all active summary buffers."
11197   (let (buffers)
11198     ;; Go through all buffers and find all summaries.
11199     (dolist (buffer (buffer-list))
11200       (when (and (setq buffer (buffer-name buffer))
11201                  (string-match "Summary" buffer)
11202                  (save-excursion
11203                    (set-buffer buffer)
11204                    ;; We check that this is, indeed, a summary buffer.
11205                    (and (eq major-mode 'gnus-summary-mode)
11206                         ;; Also make sure this isn't bogus.
11207                         gnus-newsgroup-prepared
11208                         ;; Also make sure that this isn't a
11209                         ;; dead summary buffer.
11210                         (not gnus-dead-summary-mode))))
11211         (push buffer buffers)))
11212     ;; Go through all these summary buffers and offer to save them.
11213     (when buffers
11214       (save-excursion
11215         (map-y-or-n-p
11216          "Update summary buffer %s? "
11217          (lambda (buf)
11218            (switch-to-buffer buf)
11219            (gnus-summary-exit))
11220          buffers)))))
11221
11222 (defun gnus-summary-setup-default-charset ()
11223   "Setup newsgroup default charset."
11224   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11225       (setq gnus-newsgroup-charset nil)
11226     (let* ((ignored-charsets
11227             (or gnus-newsgroup-ephemeral-ignored-charsets
11228                 (append
11229                  (and gnus-newsgroup-name
11230                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11231                  gnus-newsgroup-ignored-charsets))))
11232       (setq gnus-newsgroup-charset
11233             (or gnus-newsgroup-ephemeral-charset
11234                 (and gnus-newsgroup-name
11235                      (gnus-parameter-charset gnus-newsgroup-name))
11236                 gnus-default-charset))
11237       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11238            ignored-charsets))))
11239
11240 ;;;
11241 ;;; Mime Commands
11242 ;;;
11243
11244 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11245   "Display the current article buffer fully MIME-buttonized.
11246 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11247 treated as multipart/mixed."
11248   (interactive "P")
11249   (require 'gnus-art)
11250   (let ((gnus-unbuttonized-mime-types nil)
11251         (gnus-mime-display-multipart-as-mixed show-all-parts))
11252     (gnus-summary-show-article)))
11253
11254 (defun gnus-summary-repair-multipart (article)
11255   "Add a Content-Type header to a multipart article without one."
11256   (interactive (list (gnus-summary-article-number)))
11257   (gnus-with-article article
11258     (message-narrow-to-head)
11259     (message-remove-header "Mime-Version")
11260     (goto-char (point-max))
11261     (insert "Mime-Version: 1.0\n")
11262     (widen)
11263     (when (search-forward "\n--" nil t)
11264       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11265         (message-narrow-to-head)
11266         (message-remove-header "Content-Type")
11267         (goto-char (point-max))
11268         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11269                         separator))
11270         (widen))))
11271   (let (gnus-mark-article-hook)
11272     (gnus-summary-select-article t t nil article)))
11273
11274 (defun gnus-summary-toggle-display-buttonized ()
11275   "Toggle the buttonizing of the article buffer."
11276   (interactive)
11277   (require 'gnus-art)
11278   (if (setq gnus-inhibit-mime-unbuttonizing
11279             (not gnus-inhibit-mime-unbuttonizing))
11280       (let ((gnus-unbuttonized-mime-types nil))
11281         (gnus-summary-show-article))
11282     (gnus-summary-show-article)))
11283
11284 ;;;
11285 ;;; Generic summary marking commands
11286 ;;;
11287
11288 (defvar gnus-summary-marking-alist
11289   '((read gnus-del-mark "d")
11290     (unread gnus-unread-mark "u")
11291     (ticked gnus-ticked-mark "!")
11292     (dormant gnus-dormant-mark "?")
11293     (expirable gnus-expirable-mark "e"))
11294   "An alist of names/marks/keystrokes.")
11295
11296 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11297 (defvar gnus-summary-mark-map)
11298
11299 (defun gnus-summary-make-all-marking-commands ()
11300   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11301   (dolist (elem gnus-summary-marking-alist)
11302     (apply 'gnus-summary-make-marking-command elem)))
11303
11304 (defun gnus-summary-make-marking-command (name mark keystroke)
11305   (let ((map (make-sparse-keymap)))
11306     (define-key gnus-summary-generic-mark-map keystroke map)
11307     (dolist (lway `((next "next" next nil "n")
11308                     (next-unread "next unread" next t "N")
11309                     (prev "previous" prev nil "p")
11310                     (prev-unread "previous unread" prev t "P")
11311                     (nomove "" nil nil ,keystroke)))
11312       (let ((func (gnus-summary-make-marking-command-1
11313                    mark (car lway) lway name)))
11314         (setq func (eval func))
11315         (define-key map (nth 4 lway) func)))))
11316
11317 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11318   `(defun ,(intern
11319             (format "gnus-summary-put-mark-as-%s%s"
11320                     name (if (eq way 'nomove)
11321                              ""
11322                            (concat "-" (symbol-name way)))))
11323      (n)
11324      ,(format
11325        "Mark the current article as %s%s.
11326 If N, the prefix, then repeat N times.
11327 If N is negative, move in reverse order.
11328 The difference between N and the actual number of articles marked is
11329 returned."
11330        name (cadr lway))
11331      (interactive "p")
11332      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11333
11334 (defun gnus-summary-generic-mark (n mark move unread)
11335   "Mark N articles with MARK."
11336   (unless (eq major-mode 'gnus-summary-mode)
11337     (error "This command can only be used in the summary buffer"))
11338   (gnus-summary-show-thread)
11339   (let ((nummove
11340          (cond
11341           ((eq move 'next) 1)
11342           ((eq move 'prev) -1)
11343           (t 0))))
11344     (if (zerop nummove)
11345         (setq n 1)
11346       (when (< n 0)
11347         (setq n (abs n)
11348               nummove (* -1 nummove))))
11349     (while (and (> n 0)
11350                 (gnus-summary-mark-article nil mark)
11351                 (zerop (gnus-summary-next-subject nummove unread t)))
11352       (setq n (1- n)))
11353     (when (/= 0 n)
11354       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11355     (gnus-summary-recenter)
11356     (gnus-summary-position-point)
11357     (gnus-set-mode-line 'summary)
11358     n))
11359
11360 (defun gnus-summary-insert-articles (articles)
11361   (when (setq articles
11362               (gnus-sorted-difference articles
11363                                       (mapcar (lambda (h)
11364                                                 (mail-header-number h))
11365                                               gnus-newsgroup-headers)))
11366     (setq gnus-newsgroup-headers
11367           (merge 'list
11368                  gnus-newsgroup-headers
11369                  (gnus-fetch-headers articles)
11370                  'gnus-article-sort-by-number))
11371     ;; Suppress duplicates?
11372     (when gnus-suppress-duplicates
11373       (gnus-dup-suppress-articles))
11374
11375     ;; We might want to build some more threads first.
11376     (when (and gnus-fetch-old-headers
11377                (eq gnus-headers-retrieved-by 'nov))
11378       (if (eq gnus-fetch-old-headers 'invisible)
11379           (gnus-build-all-threads)
11380         (gnus-build-old-threads)))
11381     ;; Let the Gnus agent mark articles as read.
11382     (when gnus-agent
11383       (gnus-agent-get-undownloaded-list))
11384     ;; Remove list identifiers from subject
11385     (when gnus-list-identifiers
11386       (gnus-summary-remove-list-identifiers))
11387     ;; First and last article in this newsgroup.
11388     (when gnus-newsgroup-headers
11389       (setq gnus-newsgroup-begin
11390             (mail-header-number (car gnus-newsgroup-headers))
11391             gnus-newsgroup-end
11392             (mail-header-number
11393              (gnus-last-element gnus-newsgroup-headers))))
11394     (when gnus-use-scoring
11395       (gnus-possibly-score-headers))))
11396
11397 (defun gnus-summary-insert-old-articles (&optional all)
11398   "Insert all old articles in this group.
11399 If ALL is non-nil, already read articles become readable.
11400 If ALL is a number, fetch this number of articles."
11401   (interactive "P")
11402   (prog1
11403       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11404             older len)
11405         (setq older
11406               ;; Some nntp servers lie about their active range.  When
11407               ;; this happens, the active range can be in the millions.
11408               ;; Use a compressed range to avoid creating a huge list.
11409               (gnus-range-difference (list gnus-newsgroup-active) old))
11410         (setq len (gnus-range-length older))
11411         (cond
11412          ((null older) nil)
11413          ((numberp all)
11414           (if (< all len)
11415               (let ((older-range (nreverse older)))
11416                 (setq older nil)
11417
11418                 (while (> all 0)
11419                   (let* ((r (pop older-range))
11420                          (min (if (numberp r) r (car r)))
11421                          (max (if (numberp r) r (cdr r))))
11422                     (while (and (<= min max)
11423                                 (> all 0))
11424                       (push max older)
11425                       (setq all (1- all)
11426                             max (1- max))))))
11427             (setq older (gnus-uncompress-range older))))
11428          (all
11429           (setq older (gnus-uncompress-range older)))
11430          (t
11431           (when (and (numberp gnus-large-newsgroup)
11432                    (> len gnus-large-newsgroup))
11433               (let* ((cursor-in-echo-area nil)
11434                      (initial (gnus-parameter-large-newsgroup-initial
11435                                gnus-newsgroup-name))
11436                      (input
11437                       (read-string
11438                        (format
11439                         "How many articles from %s (%s %d): "
11440                         (gnus-limit-string
11441                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11442                         (if initial "max" "default")
11443                         len)
11444                        (if initial
11445                            (cons (number-to-string initial)
11446                                  0)))))
11447                 (unless (string-match "^[ \t]*$" input)
11448                   (setq all (string-to-number input))
11449                   (if (< all len)
11450                       (let ((older-range (nreverse older)))
11451                         (setq older nil)
11452
11453                         (while (> all 0)
11454                           (let* ((r (pop older-range))
11455                                  (min (if (numberp r) r (car r)))
11456                                  (max (if (numberp r) r (cdr r))))
11457                             (while (and (<= min max)
11458                                         (> all 0))
11459                               (push max older)
11460                               (setq all (1- all)
11461                                     max (1- max))))))))))
11462           (setq older (gnus-uncompress-range older))))
11463         (if (not older)
11464             (message "No old news.")
11465           (gnus-summary-insert-articles older)
11466           (gnus-summary-limit (gnus-sorted-nunion old older))))
11467     (gnus-summary-position-point)))
11468
11469 (defun gnus-summary-insert-new-articles ()
11470   "Insert all new articles in this group."
11471   (interactive)
11472   (prog1
11473       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11474             (old-active gnus-newsgroup-active)
11475             (nnmail-fetched-sources (list t))
11476             i new)
11477         (setq gnus-newsgroup-active
11478               (gnus-activate-group gnus-newsgroup-name 'scan))
11479         (setq i (cdr gnus-newsgroup-active))
11480         (while (> i (cdr old-active))
11481           (push i new)
11482           (decf i))
11483         (if (not new)
11484             (message "No gnus is bad news.")
11485           (gnus-summary-insert-articles new)
11486           (setq gnus-newsgroup-unreads
11487                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11488           (gnus-summary-limit (gnus-sorted-nunion old new))))
11489     (gnus-summary-position-point)))
11490
11491 (gnus-summary-make-all-marking-commands)
11492
11493 (gnus-ems-redefine)
11494
11495 (provide 'gnus-sum)
11496
11497 (run-hooks 'gnus-sum-load-hook)
11498
11499 ;;; gnus-sum.el ends here