b00facf6136d2a43a681d2c92d43e3b9b4a5c6db
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 (require 'nnoo)
42 (require 'mime-view)
43
44 (eval-when-compile
45   (require 'mime-play)
46   (require 'static))
47
48 (eval-and-compile
49   (autoload 'pgg-decrypt-region "pgg" nil t)
50   (autoload 'pgg-verify-region "pgg" nil t))
51
52 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
53 (autoload 'gnus-cache-write-active "gnus-cache")
54 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
55 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
56 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
57 (autoload 'mm-uu-dissect "mm-uu")
58
59 (defcustom gnus-kill-summary-on-exit t
60   "*If non-nil, kill the summary buffer when you exit from it.
61 If nil, the summary will become a \"*Dead Summary*\" buffer, and
62 it will be killed sometime later."
63   :group 'gnus-summary-exit
64   :type 'boolean)
65
66 (defcustom gnus-fetch-old-headers nil
67   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
68 If an unread article in the group refers to an older, already read (or
69 just marked as read) article, the old article will not normally be
70 displayed in the Summary buffer.  If this variable is non-nil, Gnus
71 will attempt to grab the headers to the old articles, and thereby
72 build complete threads.  If it has the value `some', only enough
73 headers to connect otherwise loose threads will be displayed.  This
74 variable can also be a number.  In that case, no more than that number
75 of old headers will be fetched.  If it has the value `invisible', all
76 old headers will be fetched, but none will be displayed.
77
78 The server has to support NOV for any of this to work."
79   :group 'gnus-thread
80   :type '(choice (const :tag "off" nil)
81                  (const some)
82                  number
83                  (sexp :menu-tag "other" t)))
84
85 (defcustom gnus-refer-thread-limit 200
86   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
87 If t, fetch all the available old headers."
88   :group 'gnus-thread
89   :type '(choice number
90                  (sexp :menu-tag "other" t)))
91
92 (defcustom gnus-summary-make-false-root 'adopt
93   "*nil means that Gnus won't gather loose threads.
94 If the root of a thread has expired or been read in a previous
95 session, the information necessary to build a complete thread has been
96 lost.  Instead of having many small sub-threads from this original thread
97 scattered all over the summary buffer, Gnus can gather them.
98
99 If non-nil, Gnus will try to gather all loose sub-threads from an
100 original thread into one large thread.
101
102 If this variable is non-nil, it should be one of `none', `adopt',
103 `dummy' or `empty'.
104
105 If this variable is `none', Gnus will not make a false root, but just
106 present the sub-threads after another.
107 If this variable is `dummy', Gnus will create a dummy root that will
108 have all the sub-threads as children.
109 If this variable is `adopt', Gnus will make one of the \"children\"
110 the parent and mark all the step-children as such.
111 If this variable is `empty', the \"children\" are printed with empty
112 subject fields.  (Or rather, they will be printed with a string
113 given by the `gnus-summary-same-subject' variable.)"
114   :group 'gnus-thread
115   :type '(choice (const :tag "off" nil)
116                  (const none)
117                  (const dummy)
118                  (const adopt)
119                  (const empty)))
120
121 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
122   "*A regexp to match subjects to be excluded from loose thread gathering.
123 As loose thread gathering is done on subjects only, that means that
124 there can be many false gatherings performed.  By rooting out certain
125 common subjects, gathering might become saner."
126   :group 'gnus-thread
127   :type 'regexp)
128
129 (defcustom gnus-summary-gather-subject-limit nil
130   "*Maximum length of subject comparisons when gathering loose threads.
131 Use nil to compare full subjects.  Setting this variable to a low
132 number will help gather threads that have been corrupted by
133 newsreaders chopping off subject lines, but it might also mean that
134 unrelated articles that have subject that happen to begin with the
135 same few characters will be incorrectly gathered.
136
137 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
138 comparing subjects."
139   :group 'gnus-thread
140   :type '(choice (const :tag "off" nil)
141                  (const fuzzy)
142                  (sexp :menu-tag "on" t)))
143
144 (defcustom gnus-simplify-subject-functions nil
145   "List of functions taking a string argument that simplify subjects.
146 The functions are applied recursively.
147
148 Useful functions to put in this list include: `gnus-simplify-subject-re',
149 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
150   :group 'gnus-thread
151   :type '(repeat function))
152
153 (defcustom gnus-simplify-ignored-prefixes nil
154   "*Regexp, matches for which are removed 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-dont-select-after-jump-to-other-group nil
322   "If non-nil, don't select the first unread article after entering the
323 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
324 it is depend on the value of `gnus-auto-select-first' whether to select
325 or not."
326   :group 'gnus-group-select
327   :type 'boolean)
328
329 (defcustom gnus-auto-select-next t
330   "*If non-nil, offer to go to the next group from the end of the previous.
331 If the value is t and the next newsgroup is empty, Gnus will exit
332 summary mode and go back to group mode.  If the value is neither nil
333 nor t, Gnus will select the following unread newsgroup.  In
334 particular, if the value is the symbol `quietly', the next unread
335 newsgroup will be selected without any confirmation, and if it is
336 `almost-quietly', the next group will be selected without any
337 confirmation if you are located on the last article in the group.
338 Finally, if this variable is `slightly-quietly', the `Z n' command
339 will go to the next group without confirmation."
340   :group 'gnus-summary-maneuvering
341   :type '(choice (const :tag "off" nil)
342                  (const quietly)
343                  (const almost-quietly)
344                  (const slightly-quietly)
345                  (sexp :menu-tag "on" t)))
346
347 (defcustom gnus-auto-select-same nil
348   "*If non-nil, select the next article with the same subject.
349 If there are no more articles with the same subject, go to
350 the first unread article."
351   :group 'gnus-summary-maneuvering
352   :type 'boolean)
353
354 (defcustom gnus-summary-check-current nil
355   "*If non-nil, consider the current article when moving.
356 The \"unread\" movement commands will stay on the same line if the
357 current article is unread."
358   :group 'gnus-summary-maneuvering
359   :type 'boolean)
360
361 (defcustom gnus-auto-center-summary t
362   "*If non-nil, always center the current summary buffer.
363 In particular, if `vertical' do only vertical recentering.  If non-nil
364 and non-`vertical', do both horizontal and vertical recentering."
365   :group 'gnus-summary-maneuvering
366   :type '(choice (const :tag "none" nil)
367                  (const vertical)
368                  (integer :tag "height")
369                  (sexp :menu-tag "both" t)))
370
371 (defcustom gnus-show-all-headers nil
372   "*If non-nil, don't hide any headers."
373   :group 'gnus-article-hiding
374   :group 'gnus-article-headers
375   :type 'boolean)
376
377 (defcustom gnus-summary-ignore-duplicates nil
378   "*If non-nil, ignore articles with identical Message-ID headers."
379   :group 'gnus-summary
380   :type 'boolean)
381
382 (defcustom gnus-single-article-buffer t
383   "*If non-nil, display all articles in the same buffer.
384 If nil, each group will get its own article buffer."
385   :group 'gnus-article-various
386   :type 'boolean)
387
388 (defcustom gnus-break-pages t
389   "*If non-nil, do page breaking on articles.
390 The page delimiter is specified by the `gnus-page-delimiter'
391 variable."
392   :group 'gnus-article-various
393   :type 'boolean)
394
395 (defcustom gnus-show-mime t
396   "*If non-nil, do mime processing of articles.
397 The articles will simply be fed to the function given by
398 `gnus-article-display-method-for-mime'."
399   :group 'gnus-article-mime
400   :type 'boolean)
401
402 (defcustom gnus-move-split-methods nil
403   "*Variable used to suggest where articles are to be moved to.
404 It uses the same syntax as the `gnus-split-methods' variable.
405 However, whereas `gnus-split-methods' specifies file names as targets,
406 this variable specifies group names."
407   :group 'gnus-summary-mail
408   :type '(repeat (choice (list :value (fun) function)
409                          (cons :value ("" "") regexp (repeat string))
410                          (sexp :value nil))))
411
412 (defcustom gnus-unread-mark ?\ ;;;Whitespace
413   "*Mark used for unread articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-ticked-mark ?!
418   "*Mark used for ticked articles."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-dormant-mark ??
423   "*Mark used for dormant articles."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-del-mark ?r
428   "*Mark used for del'd articles."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-read-mark ?R
433   "*Mark used for read articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-expirable-mark ?E
438   "*Mark used for expirable articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-killed-mark ?K
443   "*Mark used for killed articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-souped-mark ?F
448   "*Mark used for souped articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-kill-file-mark ?X
453   "*Mark used for articles killed by kill files."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-low-score-mark ?Y
458   "*Mark used for articles with a low score."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-catchup-mark ?C
463   "*Mark used for articles that are caught up."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-replied-mark ?A
468   "*Mark used for articles that have been replied to."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-forwarded-mark ?F
473   "*Mark used for articles that have been forwarded."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-recent-mark ?N
478   "*Mark used for articles that are recent."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-cached-mark ?*
483   "*Mark used for articles that are in the cache."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-saved-mark ?S
488   "*Mark used for articles that have been saved."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-unseen-mark ?.
493   "*Mark used for articles that haven't been seen."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-no-mark ?\ ;;;Whitespace
498   "*Mark used for articles that have no other secondary mark."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-ancient-mark ?O
503   "*Mark used for ancient articles."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-sparse-mark ?Q
508   "*Mark used for sparsely reffed articles."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-canceled-mark ?G
513   "*Mark used for canceled articles."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-duplicate-mark ?M
518   "*Mark used for duplicate articles."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-undownloaded-mark ?@
523   "*Mark used for articles that weren't downloaded."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-downloadable-mark ?%
528   "*Mark used for articles that are to be downloaded."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-unsendable-mark ?=
533   "*Mark used for articles that won't be sent."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-score-over-mark ?+
538   "*Score mark used for articles with high scores."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-score-below-mark ?-
543   "*Score mark used for articles with low scores."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
548   "*There is no thread under the article."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-not-empty-thread-mark ?=
553   "*There is a thread under the article."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-view-pseudo-asynchronously nil
558   "*If non-nil, Gnus will view pseudo-articles asynchronously."
559   :group 'gnus-extract-view
560   :type 'boolean)
561
562 (defcustom gnus-auto-expirable-marks
563   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
564         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
565         gnus-souped-mark gnus-duplicate-mark)
566   "*The list of marks converted into expiration if a group is auto-expirable."
567   :version "21.1"
568   :group 'gnus-summary
569   :type '(repeat character))
570
571 (defcustom gnus-inhibit-user-auto-expire t
572   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
573   :version "21.1"
574   :group 'gnus-summary
575   :type 'boolean)
576
577 (defcustom gnus-view-pseudos nil
578   "*If `automatic', pseudo-articles will be viewed automatically.
579 If `not-confirm', pseudos will be viewed automatically, and the user
580 will not be asked to confirm the command."
581   :group 'gnus-extract-view
582   :type '(choice (const :tag "off" nil)
583                  (const automatic)
584                  (const not-confirm)))
585
586 (defcustom gnus-view-pseudos-separately t
587   "*If non-nil, one pseudo-article will be created for each file to be viewed.
588 If nil, all files that use the same viewing command will be given as a
589 list of parameters to that command."
590   :group 'gnus-extract-view
591   :type 'boolean)
592
593 (defcustom gnus-insert-pseudo-articles t
594   "*If non-nil, insert pseudo-articles when decoding articles."
595   :group 'gnus-extract-view
596   :type 'boolean)
597
598 (defcustom gnus-summary-dummy-line-format
599   "  %(:                          :%) %S\n"
600   "*The format specification for the dummy roots in the summary buffer.
601 It works along the same lines as a normal formatting string,
602 with some simple extensions.
603
604 %S  The subject
605
606 General format specifiers can also be used.
607 See (gnus)Formatting Variables."
608   :link '(custom-manual "(gnus)Formatting Variables")
609   :group 'gnus-threading
610   :type 'string)
611
612 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
613   "*The format specification for the summary mode line.
614 It works along the same lines as a normal formatting string,
615 with some simple extensions:
616
617 %G  Group name
618 %p  Unprefixed group name
619 %A  Current article number
620 %z  Current article score
621 %V  Gnus version
622 %U  Number of unread articles in the group
623 %e  Number of unselected articles in the group
624 %Z  A string with unread/unselected article counts
625 %g  Shortish group name
626 %S  Subject of the current article
627 %u  User-defined spec
628 %s  Current score file name
629 %d  Number of dormant articles
630 %r  Number of articles that have been marked as read in this session
631 %E  Number of articles expunged by the score files"
632   :group 'gnus-summary-format
633   :type 'string)
634
635 (defcustom gnus-list-identifiers nil
636   "Regexp that matches list identifiers to be removed from subject.
637 This can also be a list of regexps."
638   :version "21.1"
639   :group 'gnus-summary-format
640   :group 'gnus-article-hiding
641   :type '(choice (const :tag "none" nil)
642                  (regexp :value ".*")
643                  (repeat :value (".*") regexp)))
644
645 (defcustom gnus-summary-mark-below 0
646   "*Mark all articles with a score below this variable as read.
647 This variable is local to each summary buffer and usually set by the
648 score file."
649   :group 'gnus-score-default
650   :type 'integer)
651
652 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
653   "*List of functions used for sorting articles in the summary buffer.
654
655 Each function takes two articles and returns non-nil if the first
656 article should be sorted before the other.  If you use more than one
657 function, the primary sort function should be the last.  You should
658 probably always include `gnus-article-sort-by-number' in the list of
659 sorting functions -- preferably first.  Also note that sorting by date
660 is often much slower than sorting by number, and the sorting order is
661 very similar.  (Sorting by date means sorting by the time the message
662 was sent, sorting by number means sorting by arrival time.)
663
664 Ready-made functions include `gnus-article-sort-by-number',
665 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
666 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
667
668 When threading is turned on, the variable `gnus-thread-sort-functions'
669 controls how articles are sorted."
670   :group 'gnus-summary-sort
671   :type '(repeat (choice (function-item gnus-article-sort-by-number)
672                          (function-item gnus-article-sort-by-author)
673                          (function-item gnus-article-sort-by-subject)
674                          (function-item gnus-article-sort-by-date)
675                          (function-item gnus-article-sort-by-score)
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', and
696 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
697
698 When threading is turned off, the variable
699 `gnus-article-sort-functions' controls how articles are sorted."
700   :group 'gnus-summary-sort
701   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
702                          (function-item gnus-thread-sort-by-author)
703                          (function-item gnus-thread-sort-by-subject)
704                          (function-item gnus-thread-sort-by-date)
705                          (function-item gnus-thread-sort-by-score)
706                          (function-item gnus-thread-sort-by-total-score)
707                          (function :tag "other"))))
708
709 (defcustom gnus-thread-score-function '+
710   "*Function used for calculating the total score of a thread.
711
712 The function is called with the scores of the article and each
713 subthread and should then return the score of the thread.
714
715 Some functions you can use are `+', `max', or `min'."
716   :group 'gnus-summary-sort
717   :type 'function)
718
719 (defcustom gnus-summary-expunge-below nil
720   "All articles that have a score less than this variable will be expunged.
721 This variable is local to the summary buffers."
722   :group 'gnus-score-default
723   :type '(choice (const :tag "off" nil)
724                  integer))
725
726 (defcustom gnus-thread-expunge-below nil
727   "All threads that have a total score less than this variable will be expunged.
728 See `gnus-thread-score-function' for en explanation of what a
729 \"thread score\" is.
730
731 This variable is local to the summary buffers."
732   :group 'gnus-threading
733   :group 'gnus-score-default
734   :type '(choice (const :tag "off" nil)
735                  integer))
736
737 (defcustom gnus-summary-mode-hook nil
738   "*A hook for Gnus summary mode.
739 This hook is run before any variables are set in the summary buffer."
740   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
741   :group 'gnus-summary-various
742   :type 'hook)
743
744 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
745 (when (featurep 'xemacs)
746   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
747   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
748   (add-hook 'gnus-summary-mode-hook
749             'gnus-xmas-switch-horizontal-scrollbar-off))
750
751 (defcustom gnus-summary-menu-hook nil
752   "*Hook run after the creation of the summary mode menu."
753   :group 'gnus-summary-visual
754   :type 'hook)
755
756 (defcustom gnus-summary-exit-hook nil
757   "*A hook called on exit from the summary buffer.
758 It will be called with point in the group buffer."
759   :group 'gnus-summary-exit
760   :type 'hook)
761
762 (defcustom gnus-summary-prepare-hook nil
763   "*A hook called after the summary buffer has been generated.
764 If you want to modify the summary buffer, you can use this hook."
765   :group 'gnus-summary-various
766   :type 'hook)
767
768 (defcustom gnus-summary-prepared-hook nil
769   "*A hook called as the last thing after the summary buffer has been generated."
770   :group 'gnus-summary-various
771   :type 'hook)
772
773 (defcustom gnus-summary-generate-hook nil
774   "*A hook run just before generating the summary buffer.
775 This hook is commonly used to customize threading variables and the
776 like."
777   :group 'gnus-summary-various
778   :type 'hook)
779
780 (defcustom gnus-select-group-hook nil
781   "*A hook called when a newsgroup is selected.
782
783 If you'd like to simplify subjects like the
784 `gnus-summary-next-same-subject' command does, you can use the
785 following hook:
786
787  (add-hook gnus-select-group-hook
788            (lambda ()
789              (mapcar (lambda (header)
790                        (mail-header-set-subject
791                         header
792                         (gnus-simplify-subject
793                          (mail-header-subject header) 're-only)))
794                      gnus-newsgroup-headers)))"
795   :group 'gnus-group-select
796   :type 'hook)
797
798 (defcustom gnus-select-article-hook nil
799   "*A hook called when an article is selected."
800   :group 'gnus-summary-choose
801   :type 'hook)
802
803 (defcustom gnus-visual-mark-article-hook
804   (list 'gnus-highlight-selected-summary)
805   "*Hook run after selecting an article in the summary buffer.
806 It is meant to be used for highlighting the article in some way.  It
807 is not run if `gnus-visual' is nil."
808   :group 'gnus-summary-visual
809   :type 'hook)
810
811 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
812   "*A hook called before parsing the headers."
813   :group 'gnus-various
814   :type 'hook)
815
816 (defcustom gnus-exit-group-hook nil
817   "*A hook called when exiting summary mode.
818 This hook is not called from the non-updating exit commands like `Q'."
819   :group 'gnus-various
820   :type 'hook)
821
822 (defcustom gnus-summary-update-hook
823   (list 'gnus-summary-highlight-line)
824   "*A hook called when a summary line is changed.
825 The hook will not be called if `gnus-visual' is nil.
826
827 The default function `gnus-summary-highlight-line' will
828 highlight the line according to the `gnus-summary-highlight'
829 variable."
830   :group 'gnus-summary-visual
831   :type 'hook)
832
833 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
834   "*A hook called when an article is selected for the first time.
835 The hook is intended to mark an article as read (or unread)
836 automatically when it is selected."
837   :group 'gnus-summary-choose
838   :type 'hook)
839
840 (defcustom gnus-group-no-more-groups-hook nil
841   "*A hook run when returning to group mode having no more (unread) groups."
842   :group 'gnus-group-select
843   :type 'hook)
844
845 (defcustom gnus-ps-print-hook nil
846   "*A hook run before ps-printing something from Gnus."
847   :group 'gnus-summary
848   :type 'hook)
849
850 (defcustom gnus-summary-display-arrow
851   (and (fboundp 'display-graphic-p)
852        (display-graphic-p))
853   "*If non-nil, display an arrow highlighting the current article."
854   :version "21.1"
855   :group 'gnus-summary
856   :type 'boolean)
857
858 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
859   "Face used for highlighting the current article in the summary buffer."
860   :group 'gnus-summary-visual
861   :type 'face)
862
863 (defcustom gnus-summary-highlight
864   '(((= mark gnus-canceled-mark)
865      . gnus-summary-cancelled-face)
866     ((and (> score default-high)
867           (or (= mark gnus-dormant-mark)
868               (= mark gnus-ticked-mark)))
869      . gnus-summary-high-ticked-face)
870     ((and (< score default-low)
871           (or (= mark gnus-dormant-mark)
872               (= mark gnus-ticked-mark)))
873      . gnus-summary-low-ticked-face)
874     ((or (= mark gnus-dormant-mark)
875          (= mark gnus-ticked-mark))
876      . gnus-summary-normal-ticked-face)
877     ((and (> score default-high) (= mark gnus-ancient-mark))
878      . gnus-summary-high-ancient-face)
879     ((and (< score default-low) (= mark gnus-ancient-mark))
880      . gnus-summary-low-ancient-face)
881     ((= mark gnus-ancient-mark)
882      . gnus-summary-normal-ancient-face)
883     ((and (> score default-high) (= mark gnus-unread-mark))
884      . gnus-summary-high-unread-face)
885     ((and (< score default-low) (= mark gnus-unread-mark))
886      . gnus-summary-low-unread-face)
887     ((= mark gnus-unread-mark)
888      . gnus-summary-normal-unread-face)
889     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
890                                                   gnus-undownloaded-mark)))
891      . gnus-summary-high-unread-face)
892     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
893                                                  gnus-undownloaded-mark)))
894      . gnus-summary-low-unread-face)
895     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
896           (memq article gnus-newsgroup-unreads))
897      . gnus-summary-normal-unread-face)
898     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
899      . gnus-summary-normal-read-face)
900     ((> score default-high)
901      . gnus-summary-high-read-face)
902     ((< score default-low)
903      . gnus-summary-low-read-face)
904     (t
905      . gnus-summary-normal-read-face))
906   "*Controls the highlighting of summary buffer lines.
907
908 A list of (FORM . FACE) pairs.  When deciding how a a particular
909 summary line should be displayed, each form is evaluated.  The content
910 of the face field after the first true form is used.  You can change
911 how those summary lines are displayed, by editing the face field.
912
913 You can use the following variables in the FORM field.
914
915 score:        The article's score
916 default:      The default article score.
917 default-high: The default score for high scored articles.
918 default-low:  The default score for low scored articles.
919 below:        The score below which articles are automatically marked as read.
920 mark:         The articles mark."
921   :group 'gnus-summary-visual
922   :type '(repeat (cons (sexp :tag "Form" nil)
923                        face)))
924
925 (defcustom gnus-alter-header-function nil
926   "Function called to allow alteration of article header structures.
927 The function is called with one parameter, the article header vector,
928 which it may alter in any way.")
929
930 (defvar gnus-decode-encoded-word-function
931   (mime-find-field-decoder 'From 'nov)
932   "Variable that says which function should be used to decode a string with encoded words.")
933
934 (defcustom gnus-extra-headers '(To Newsgroups)
935   "*Extra headers to parse."
936   :version "21.1"
937   :group 'gnus-summary
938   :type '(repeat symbol))
939
940 (defcustom gnus-ignored-from-addresses
941   (and user-mail-address (regexp-quote user-mail-address))
942   "*Regexp of From headers that may be suppressed in favor of To headers."
943   :version "21.1"
944   :group 'gnus-summary
945   :type 'regexp)
946
947 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
948   "List of charsets that should be ignored.
949 When these charsets are used in the \"charset\" parameter, the
950 default charset will be used instead."
951   :version "21.1"
952   :type '(repeat symbol)
953   :group 'gnus-charset)
954
955 (gnus-define-group-parameter
956  ignored-charsets
957  :type list
958  :function-document
959  "Return the ignored charsets of GROUP."
960  :variable gnus-group-ignored-charsets-alist
961  :variable-default
962  '(("alt\\.chinese\\.text" iso-8859-1))
963  :variable-document
964  "Alist of regexps (to match group names) and charsets that should be ignored.
965 When these charsets are used in the \"charset\" parameter, the
966 default charset will be used instead."
967  :variable-group gnus-charset
968  :variable-type '(repeat (cons (regexp :tag "Group")
969                                (repeat symbol)))
970  :parameter-type '(choice :tag "Ignored charsets"
971                           :value nil
972                           (repeat (symbol)))
973  :parameter-document       "\
974 List of charsets that should be ignored.
975
976 When these charsets are used in the \"charset\" parameter, the
977 default charset will be used instead.")
978
979 (defcustom gnus-group-highlight-words-alist nil
980   "Alist of group regexps and highlight regexps.
981 This variable uses the same syntax as `gnus-emphasis-alist'."
982   :version "21.1"
983   :type '(repeat (cons (regexp :tag "Group")
984                        (repeat (list (regexp :tag "Highlight regexp")
985                                      (number :tag "Group for entire word" 0)
986                                      (number :tag "Group for displayed part" 0)
987                                      (symbol :tag "Face"
988                                              gnus-emphasis-highlight-words)))))
989   :group 'gnus-summary-visual)
990
991 (defcustom gnus-use-wheel nil
992   "Use Intelli-mouse on summary movement"
993   :type 'boolean
994   :group 'gnus-summary-maneuvering)
995
996 (defcustom gnus-wheel-scroll-amount '(5 . 1)
997   "Amount to scroll messages by spinning the mouse wheel.
998 This is actually a cons cell, where the first item is the amount to scroll
999 on a normal wheel event, and the second is the amount to scroll when the
1000 wheel is moved with the shift key depressed."
1001   :type '(cons (integer :tag "Shift") integer)
1002   :group 'gnus-summary-maneuvering)
1003
1004 (defcustom gnus-wheel-edge-resistance 2
1005   "How hard it should be to change the current article
1006 by moving the mouse over the edge of the article window."
1007   :type 'integer
1008   :group 'gnus-summary-maneuvering)
1009
1010 (defcustom gnus-summary-show-article-charset-alist
1011   nil
1012   "Alist of number and charset.
1013 The article will be shown with the charset corresponding to the
1014 numbered argument.
1015 For example: ((1 . cn-gb-2312) (2 . big5))."
1016   :version "21.1"
1017   :type '(repeat (cons (number :tag "Argument" 1)
1018                        (symbol :tag "Charset")))
1019   :group 'gnus-charset)
1020
1021 (defcustom gnus-preserve-marks t
1022   "Whether marks are preserved when moving, copying and respooling messages."
1023   :version "21.1"
1024   :type 'boolean
1025   :group 'gnus-summary-marks)
1026
1027 (defcustom gnus-alter-articles-to-read-function nil
1028   "Function to be called to alter the list of articles to be selected."
1029   :type '(choice (const nil) function)
1030   :group 'gnus-summary)
1031
1032 (defcustom gnus-orphan-score nil
1033   "*All orphans get this score added.  Set in the score file."
1034   :group 'gnus-score-default
1035   :type '(choice (const nil)
1036                  integer))
1037
1038 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1039   "*A regexp to match MIME parts when saving multiple parts of a message
1040 with gnus-summary-save-parts (X m). This regexp will be used by default
1041 when prompting the user for which type of files to save."
1042   :group 'gnus-summary
1043   :type 'regexp)
1044
1045
1046 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1047   "*A regexp to match MIME parts when saving multiple parts of a message
1048 with gnus-summary-save-parts (X m). This regexp will be used by default
1049 when prompting the user for which type of files to save."
1050   :group 'gnus-summary
1051   :type 'regexp)
1052
1053 (defcustom gnus-read-all-available-headers nil
1054   "Whether Gnus should parse all headers made available to it.
1055 This is mostly relevant for slow backends where the user may
1056 wish to widen the summary buffer to include all headers
1057 that were fetched.  Say, for nnultimate groups."
1058   :group 'gnus-summary
1059   :type '(choice boolean regexp))
1060
1061 (defcustom gnus-summary-muttprint-program "muttprint"
1062   "Command (and optional arguments) used to run Muttprint."
1063   :group 'gnus-summary
1064   :type 'string)
1065
1066 ;;; Internal variables
1067
1068 (defvar gnus-summary-display-cache nil)
1069 (defvar gnus-article-mime-handles nil)
1070 (defvar gnus-article-decoded-p nil)
1071 (defvar gnus-article-charset nil)
1072 (defvar gnus-article-ignored-charsets nil)
1073 (defvar gnus-scores-exclude-files nil)
1074 (defvar gnus-page-broken nil)
1075 (defvar gnus-inhibit-mime-unbuttonizing nil)
1076
1077 (defvar gnus-original-article nil)
1078 (defvar gnus-article-internal-prepare-hook nil)
1079 (defvar gnus-newsgroup-process-stack nil)
1080
1081 (defvar gnus-thread-indent-array nil)
1082 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1083 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1084   "Function called to sort the articles within a thread after it has been gathered together.")
1085
1086 (defvar gnus-summary-save-parts-type-history nil)
1087 (defvar gnus-summary-save-parts-last-directory nil)
1088
1089 (defvar gnus-summary-save-parts-type-history nil)
1090 (defvar gnus-summary-save-parts-last-directory nil)
1091
1092 ;; Avoid highlighting in kill files.
1093 (defvar gnus-summary-inhibit-highlight nil)
1094 (defvar gnus-newsgroup-selected-overlay nil)
1095 (defvar gnus-inhibit-limiting nil)
1096 (defvar gnus-newsgroup-adaptive-score-file nil)
1097 (defvar gnus-current-score-file nil)
1098 (defvar gnus-current-move-group nil)
1099 (defvar gnus-current-copy-group nil)
1100 (defvar gnus-current-crosspost-group nil)
1101 (defvar gnus-newsgroup-display nil)
1102
1103 (defvar gnus-newsgroup-dependencies nil)
1104 (defvar gnus-newsgroup-adaptive nil)
1105 (defvar gnus-summary-display-article-function nil)
1106 (defvar gnus-summary-highlight-line-function nil
1107   "Function called after highlighting a summary line.")
1108
1109 (defvar gnus-summary-line-format-alist
1110   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1111     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1112     (?s gnus-tmp-subject-or-nil ?s)
1113     (?n gnus-tmp-name ?s)
1114     (?A (std11-address-string
1115          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1116     (?a (or (std11-full-name-string
1117              (car (mime-entity-read-field gnus-tmp-header 'From)))
1118             gnus-tmp-from) ?s)
1119     (?F gnus-tmp-from ?s)
1120     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1121     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1122     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1123     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1124     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1125     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1126     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1127     (?L gnus-tmp-lines ?s)
1128     (?I gnus-tmp-indentation ?s)
1129     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1130     (?R gnus-tmp-replied ?c)
1131     (?\[ gnus-tmp-opening-bracket ?c)
1132     (?\] gnus-tmp-closing-bracket ?c)
1133     (?\> (make-string gnus-tmp-level ? ) ?s)
1134     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1135     (?i gnus-tmp-score ?d)
1136     (?z gnus-tmp-score-char ?c)
1137     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1138     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1139     (?U gnus-tmp-unread ?c)
1140     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1141         ?s)
1142     (?t (gnus-summary-number-of-articles-in-thread
1143          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1144         ?d)
1145     (?e (gnus-summary-number-of-articles-in-thread
1146          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1147         ?c)
1148     (?u gnus-tmp-user-defined ?s)
1149     (?P (gnus-pick-line-number) ?d)
1150     (?B gnus-tmp-thread-tree-header-string ?s)
1151     (user-date (gnus-user-date
1152                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1153   "An alist of format specifications that can appear in summary lines.
1154 These are paired with what variables they correspond with, along with
1155 the type of the variable (string, integer, character, etc).")
1156
1157 (defvar gnus-summary-dummy-line-format-alist
1158   `((?S gnus-tmp-subject ?s)
1159     (?N gnus-tmp-number ?d)
1160     (?u gnus-tmp-user-defined ?s)))
1161
1162 (defvar gnus-summary-mode-line-format-alist
1163   `((?G gnus-tmp-group-name ?s)
1164     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1165     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1166     (?A gnus-tmp-article-number ?d)
1167     (?Z gnus-tmp-unread-and-unselected ?s)
1168     (?V gnus-version ?s)
1169     (?U gnus-tmp-unread-and-unticked ?d)
1170     (?S gnus-tmp-subject ?s)
1171     (?e gnus-tmp-unselected ?d)
1172     (?u gnus-tmp-user-defined ?s)
1173     (?d (length gnus-newsgroup-dormant) ?d)
1174     (?t (length gnus-newsgroup-marked) ?d)
1175     (?r (length gnus-newsgroup-reads) ?d)
1176     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1177     (?E gnus-newsgroup-expunged-tally ?d)
1178     (?s (gnus-current-score-file-nondirectory) ?s)))
1179
1180 (defvar gnus-last-search-regexp nil
1181   "Default regexp for article search command.")
1182
1183 (defvar gnus-summary-search-article-matched-data nil
1184   "Last matched data of article search command.  It is the local variable
1185 in `gnus-article-buffer' which consists of the list of start position,
1186 end position and text.")
1187
1188 (defvar gnus-last-shell-command nil
1189   "Default shell command on article.")
1190
1191 (defvar gnus-newsgroup-begin nil)
1192 (defvar gnus-newsgroup-end nil)
1193 (defvar gnus-newsgroup-last-rmail nil)
1194 (defvar gnus-newsgroup-last-mail nil)
1195 (defvar gnus-newsgroup-last-folder nil)
1196 (defvar gnus-newsgroup-last-file nil)
1197 (defvar gnus-newsgroup-auto-expire nil)
1198 (defvar gnus-newsgroup-active nil)
1199
1200 (defvar gnus-newsgroup-data nil)
1201 (defvar gnus-newsgroup-data-reverse nil)
1202 (defvar gnus-newsgroup-limit nil)
1203 (defvar gnus-newsgroup-limits nil)
1204
1205 (defvar gnus-newsgroup-unreads nil
1206   "List of unread articles in the current newsgroup.")
1207
1208 (defvar gnus-newsgroup-unselected nil
1209   "List of unselected unread articles in the current newsgroup.")
1210
1211 (defvar gnus-newsgroup-reads nil
1212   "Alist of read articles and article marks in the current newsgroup.")
1213
1214 (defvar gnus-newsgroup-expunged-tally nil)
1215
1216 (defvar gnus-newsgroup-marked nil
1217   "List of ticked articles in the current newsgroup (a subset of unread art).")
1218
1219 (defvar gnus-newsgroup-killed nil
1220   "List of ranges of articles that have been through the scoring process.")
1221
1222 (defvar gnus-newsgroup-cached nil
1223   "List of articles that come from the article cache.")
1224
1225 (defvar gnus-newsgroup-saved nil
1226   "List of articles that have been saved.")
1227
1228 (defvar gnus-newsgroup-kill-headers nil)
1229
1230 (defvar gnus-newsgroup-replied nil
1231   "List of articles that have been replied to in the current newsgroup.")
1232
1233 (defvar gnus-newsgroup-forwarded nil
1234   "List of articles that have been forwarded in the current newsgroup.")
1235
1236 (defvar gnus-newsgroup-recent nil
1237   "List of articles that have are recent in the current newsgroup.")
1238
1239 (defvar gnus-newsgroup-expirable nil
1240   "List of articles in the current newsgroup that can be expired.")
1241
1242 (defvar gnus-newsgroup-processable nil
1243   "List of articles in the current newsgroup that can be processed.")
1244
1245 (defvar gnus-newsgroup-downloadable nil
1246   "List of articles in the current newsgroup that can be processed.")
1247
1248 (defvar gnus-newsgroup-undownloaded nil
1249   "List of articles in the current newsgroup that haven't been downloaded..")
1250
1251 (defvar gnus-newsgroup-unsendable nil
1252   "List of articles in the current newsgroup that won't be sent.")
1253
1254 (defvar gnus-newsgroup-bookmarks nil
1255   "List of articles in the current newsgroup that have bookmarks.")
1256
1257 (defvar gnus-newsgroup-dormant nil
1258   "List of dormant articles in the current newsgroup.")
1259
1260 (defvar gnus-newsgroup-unseen nil
1261   "List of unseen articles in the current newsgroup.")
1262
1263 (defvar gnus-newsgroup-seen nil
1264   "Range of seen articles in the current newsgroup.")
1265
1266 (defvar gnus-newsgroup-articles nil
1267   "List of articles in the current newsgroup.")
1268
1269 (defvar gnus-newsgroup-scored nil
1270   "List of scored articles in the current newsgroup.")
1271
1272 (defvar gnus-newsgroup-incorporated nil
1273   "List of incorporated articles in the current newsgroup.")
1274
1275 (defvar gnus-newsgroup-headers nil
1276   "List of article headers in the current newsgroup.")
1277
1278 (defvar gnus-newsgroup-threads nil)
1279
1280 (defvar gnus-newsgroup-prepared nil
1281   "Whether the current group has been prepared properly.")
1282
1283 (defvar gnus-newsgroup-ancient nil
1284   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1285
1286 (defvar gnus-newsgroup-sparse nil)
1287
1288 (defvar gnus-current-article nil)
1289 (defvar gnus-article-current nil)
1290 (defvar gnus-current-headers nil)
1291 (defvar gnus-have-all-headers nil)
1292 (defvar gnus-last-article nil)
1293 (defvar gnus-newsgroup-history nil)
1294 (defvar gnus-newsgroup-charset nil)
1295 (defvar gnus-newsgroup-ephemeral-charset nil)
1296 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1297
1298 (defvar gnus-article-before-search nil)
1299
1300 (defconst gnus-summary-local-variables
1301   '(gnus-newsgroup-name
1302     gnus-newsgroup-begin gnus-newsgroup-end
1303     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1304     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1305     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1306     gnus-newsgroup-unselected gnus-newsgroup-marked
1307     gnus-newsgroup-reads gnus-newsgroup-saved
1308     gnus-newsgroup-replied gnus-newsgroup-forwarded
1309     gnus-newsgroup-recent
1310     gnus-newsgroup-expirable
1311     gnus-newsgroup-processable gnus-newsgroup-killed
1312     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1313     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1314     gnus-newsgroup-seen gnus-newsgroup-articles
1315     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1316     gnus-newsgroup-headers gnus-newsgroup-threads
1317     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1318     gnus-current-article gnus-current-headers gnus-have-all-headers
1319     gnus-last-article gnus-article-internal-prepare-hook
1320     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1321     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1322     gnus-thread-expunge-below
1323     gnus-score-alist gnus-current-score-file
1324     (gnus-summary-expunge-below . global)
1325     (gnus-summary-mark-below . global)
1326     (gnus-orphan-score . global)
1327     gnus-newsgroup-active gnus-scores-exclude-files
1328     gnus-newsgroup-history gnus-newsgroup-ancient
1329     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1330     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1331     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1332     (gnus-newsgroup-expunged-tally . 0)
1333     gnus-cache-removable-articles gnus-newsgroup-cached
1334     gnus-newsgroup-data gnus-newsgroup-data-reverse
1335     gnus-newsgroup-limit gnus-newsgroup-limits
1336     gnus-newsgroup-charset gnus-newsgroup-display
1337     gnus-newsgroup-incorporated)
1338   "Variables that are buffer-local to the summary buffers.")
1339
1340 (defvar gnus-newsgroup-variables nil
1341   "A list of variables that have separate values in different newsgroups.
1342 A list of newsgroup (summary buffer) local variables, or cons of
1343 variables and their default values (when the default values are not
1344 nil), that should be made global while the summary buffer is active.
1345 These variables can be used to set variables in the group parameters
1346 while still allowing them to affect operations done in other
1347 buffers. For example:
1348
1349 \(setq gnus-newsgroup-variables
1350      '(message-use-followup-to
1351        (gnus-visible-headers .
1352          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1353 ")
1354
1355 ;; Byte-compiler warning.
1356 (eval-when-compile (defvar gnus-article-mode-map))
1357
1358 ;; Subject simplification.
1359
1360 (defun gnus-simplify-whitespace (str)
1361   "Remove excessive whitespace from STR."
1362   (let ((mystr str))
1363     ;; Multiple spaces.
1364     (while (string-match "[ \t][ \t]+" mystr)
1365       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1366                           " "
1367                           (substring mystr (match-end 0)))))
1368     ;; Leading spaces.
1369     (when (string-match "^[ \t]+" mystr)
1370       (setq mystr (substring mystr (match-end 0))))
1371     ;; Trailing spaces.
1372     (when (string-match "[ \t]+$" mystr)
1373       (setq mystr (substring mystr 0 (match-beginning 0))))
1374     mystr))
1375
1376 (defsubst gnus-simplify-subject-re (subject)
1377   "Remove \"Re:\" from subject lines."
1378   (if (string-match message-subject-re-regexp subject)
1379       (substring subject (match-end 0))
1380     subject))
1381
1382 (defun gnus-simplify-subject (subject &optional re-only)
1383   "Remove `Re:' and words in parentheses.
1384 If RE-ONLY is non-nil, strip leading `Re:'s only."
1385   (let ((case-fold-search t))           ;Ignore case.
1386     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1387     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1388       (setq subject (substring subject (match-end 0))))
1389     ;; Remove uninteresting prefixes.
1390     (when (and (not re-only)
1391                gnus-simplify-ignored-prefixes
1392                (string-match gnus-simplify-ignored-prefixes subject))
1393       (setq subject (substring subject (match-end 0))))
1394     ;; Remove words in parentheses from end.
1395     (unless re-only
1396       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1397         (setq subject (substring subject 0 (match-beginning 0)))))
1398     ;; Return subject string.
1399     subject))
1400
1401 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1402 ;; all whitespace.
1403 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1404   (goto-char (point-min))
1405   (while (re-search-forward regexp nil t)
1406     (replace-match (or newtext ""))))
1407
1408 (defun gnus-simplify-buffer-fuzzy ()
1409   "Simplify string in the buffer fuzzily.
1410 The string in the accessible portion of the current buffer is simplified.
1411 It is assumed to be a single-line subject.
1412 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1413 matter is removed.  Additional things can be deleted by setting
1414 `gnus-simplify-subject-fuzzy-regexp'."
1415   (let ((case-fold-search t)
1416         (modified-tick))
1417     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1418
1419     (while (not (eq modified-tick (buffer-modified-tick)))
1420       (setq modified-tick (buffer-modified-tick))
1421       (cond
1422        ((listp gnus-simplify-subject-fuzzy-regexp)
1423         (mapcar 'gnus-simplify-buffer-fuzzy-step
1424                 gnus-simplify-subject-fuzzy-regexp))
1425        (gnus-simplify-subject-fuzzy-regexp
1426         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1427       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1428       (gnus-simplify-buffer-fuzzy-step
1429        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1430       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1431
1432     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1433     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1434     (gnus-simplify-buffer-fuzzy-step " $")
1435     (gnus-simplify-buffer-fuzzy-step "^ +")))
1436
1437 (defun gnus-simplify-subject-fuzzy (subject)
1438   "Simplify a subject string fuzzily.
1439 See `gnus-simplify-buffer-fuzzy' for details."
1440   (save-excursion
1441     (gnus-set-work-buffer)
1442     (let ((case-fold-search t))
1443       ;; Remove uninteresting prefixes.
1444       (when (and gnus-simplify-ignored-prefixes
1445                  (string-match gnus-simplify-ignored-prefixes subject))
1446         (setq subject (substring subject (match-end 0))))
1447       (insert subject)
1448       (inline (gnus-simplify-buffer-fuzzy))
1449       (buffer-string))))
1450
1451 (defsubst gnus-simplify-subject-fully (subject)
1452   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1453   (cond
1454    (gnus-simplify-subject-functions
1455     (gnus-map-function gnus-simplify-subject-functions subject))
1456    ((null gnus-summary-gather-subject-limit)
1457     (gnus-simplify-subject-re subject))
1458    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1459     (gnus-simplify-subject-fuzzy subject))
1460    ((numberp gnus-summary-gather-subject-limit)
1461     (gnus-limit-string (gnus-simplify-subject-re subject)
1462                        gnus-summary-gather-subject-limit))
1463    (t
1464     subject)))
1465
1466 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1467   "Check whether two subjects are equal.
1468 If optional argument simple-first is t, first argument is already
1469 simplified."
1470   (cond
1471    ((null simple-first)
1472     (equal (gnus-simplify-subject-fully s1)
1473            (gnus-simplify-subject-fully s2)))
1474    (t
1475     (equal s1
1476            (gnus-simplify-subject-fully s2)))))
1477
1478 (defun gnus-summary-bubble-group ()
1479   "Increase the score of the current group.
1480 This is a handy function to add to `gnus-summary-exit-hook' to
1481 increase the score of each group you read."
1482   (gnus-group-add-score gnus-newsgroup-name))
1483
1484 \f
1485 ;;;
1486 ;;; Gnus summary mode
1487 ;;;
1488
1489 (put 'gnus-summary-mode 'mode-class 'special)
1490
1491 (defvar gnus-article-commands-menu)
1492
1493 (when t
1494   ;; Non-orthogonal keys
1495
1496   (gnus-define-keys gnus-summary-mode-map
1497     " " gnus-summary-next-page
1498     "\177" gnus-summary-prev-page
1499     [delete] gnus-summary-prev-page
1500     [backspace] gnus-summary-prev-page
1501     "\r" gnus-summary-scroll-up
1502     "\M-\r" gnus-summary-scroll-down
1503     "n" gnus-summary-next-unread-article
1504     "p" gnus-summary-prev-unread-article
1505     "N" gnus-summary-next-article
1506     "P" gnus-summary-prev-article
1507     "\M-\C-n" gnus-summary-next-same-subject
1508     "\M-\C-p" gnus-summary-prev-same-subject
1509     "\M-n" gnus-summary-next-unread-subject
1510     "\M-p" gnus-summary-prev-unread-subject
1511     "." gnus-summary-first-unread-article
1512     "," gnus-summary-best-unread-article
1513     "\M-s" gnus-summary-search-article-forward
1514     "\M-r" gnus-summary-search-article-backward
1515     "<" gnus-summary-beginning-of-article
1516     ">" gnus-summary-end-of-article
1517     "j" gnus-summary-goto-article
1518     "^" gnus-summary-refer-parent-article
1519     "\M-^" gnus-summary-refer-article
1520     "u" gnus-summary-tick-article-forward
1521     "!" gnus-summary-tick-article-forward
1522     "U" gnus-summary-tick-article-backward
1523     "d" gnus-summary-mark-as-read-forward
1524     "D" gnus-summary-mark-as-read-backward
1525     "E" gnus-summary-mark-as-expirable
1526     "\M-u" gnus-summary-clear-mark-forward
1527     "\M-U" gnus-summary-clear-mark-backward
1528     "k" gnus-summary-kill-same-subject-and-select
1529     "\C-k" gnus-summary-kill-same-subject
1530     "\M-\C-k" gnus-summary-kill-thread
1531     "\M-\C-l" gnus-summary-lower-thread
1532     "e" gnus-summary-edit-article
1533     "#" gnus-summary-mark-as-processable
1534     "\M-#" gnus-summary-unmark-as-processable
1535     "\M-\C-t" gnus-summary-toggle-threads
1536     "\M-\C-s" gnus-summary-show-thread
1537     "\M-\C-h" gnus-summary-hide-thread
1538     "\M-\C-f" gnus-summary-next-thread
1539     "\M-\C-b" gnus-summary-prev-thread
1540     [(meta down)] gnus-summary-next-thread
1541     [(meta up)] gnus-summary-prev-thread
1542     "\M-\C-u" gnus-summary-up-thread
1543     "\M-\C-d" gnus-summary-down-thread
1544     "&" gnus-summary-execute-command
1545     "c" gnus-summary-catchup-and-exit
1546     "\C-w" gnus-summary-mark-region-as-read
1547     "\C-t" gnus-summary-toggle-truncation
1548     "?" gnus-summary-mark-as-dormant
1549     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1550     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1551     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1552     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1553     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1554     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1555     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1556     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1557     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1558     "=" gnus-summary-expand-window
1559     "\C-x\C-s" gnus-summary-reselect-current-group
1560     "\M-g" gnus-summary-rescan-group
1561     "w" gnus-summary-stop-page-breaking
1562     "\C-c\C-r" gnus-summary-caesar-message
1563     "\M-t" gnus-summary-toggle-mime
1564     "f" gnus-summary-followup
1565     "F" gnus-summary-followup-with-original
1566     "C" gnus-summary-cancel-article
1567     "r" gnus-summary-reply
1568     "R" gnus-summary-reply-with-original
1569     "\C-c\C-f" gnus-summary-mail-forward
1570     "o" gnus-summary-save-article
1571     "\C-o" gnus-summary-save-article-mail
1572     "|" gnus-summary-pipe-output
1573     "\M-k" gnus-summary-edit-local-kill
1574     "\M-K" gnus-summary-edit-global-kill
1575     ;; "V" gnus-version
1576     "\C-c\C-d" gnus-summary-describe-group
1577     "q" gnus-summary-exit
1578     "Q" gnus-summary-exit-no-update
1579     "\C-c\C-i" gnus-info-find-node
1580     gnus-mouse-2 gnus-mouse-pick-article
1581     "m" gnus-summary-mail-other-window
1582     "a" gnus-summary-post-news
1583     "i" gnus-summary-news-other-window
1584     "x" gnus-summary-limit-to-unread
1585     "s" gnus-summary-isearch-article
1586     "t" gnus-article-toggle-headers
1587     "g" gnus-summary-show-article
1588     "l" gnus-summary-goto-last-article
1589     "v" gnus-summary-preview-mime-message
1590     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1591     "\C-d" gnus-summary-enter-digest-group
1592     "\M-\C-d" gnus-summary-read-document
1593     "\M-\C-e" gnus-summary-edit-parameters
1594     "\M-\C-a" gnus-summary-customize-parameters
1595     "\C-c\C-b" gnus-bug
1596     "\C-c\C-n" gnus-namazu-search
1597     "*" gnus-cache-enter-article
1598     "\M-*" gnus-cache-remove-article
1599     "\M-&" gnus-summary-universal-argument
1600     "\C-l" gnus-recenter
1601     "I" gnus-summary-increase-score
1602     "L" gnus-summary-lower-score
1603     "\M-i" gnus-symbolic-argument
1604     "h" gnus-summary-select-article-buffer
1605
1606     "V" gnus-summary-score-map
1607     "X" gnus-uu-extract-map
1608     "S" gnus-summary-send-map)
1609
1610   ;; Sort of orthogonal keymap
1611   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1612     "t" gnus-summary-tick-article-forward
1613     "!" gnus-summary-tick-article-forward
1614     "d" gnus-summary-mark-as-read-forward
1615     "r" gnus-summary-mark-as-read-forward
1616     "c" gnus-summary-clear-mark-forward
1617     " " gnus-summary-clear-mark-forward
1618     "e" gnus-summary-mark-as-expirable
1619     "x" gnus-summary-mark-as-expirable
1620     "?" gnus-summary-mark-as-dormant
1621     "b" gnus-summary-set-bookmark
1622     "B" gnus-summary-remove-bookmark
1623     "#" gnus-summary-mark-as-processable
1624     "\M-#" gnus-summary-unmark-as-processable
1625     "S" gnus-summary-limit-include-expunged
1626     "C" gnus-summary-catchup
1627     "H" gnus-summary-catchup-to-here
1628     "h" gnus-summary-catchup-from-here
1629     "\C-c" gnus-summary-catchup-all
1630     "k" gnus-summary-kill-same-subject-and-select
1631     "K" gnus-summary-kill-same-subject
1632     "P" gnus-uu-mark-map)
1633
1634   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1635     "c" gnus-summary-clear-above
1636     "u" gnus-summary-tick-above
1637     "m" gnus-summary-mark-above
1638     "k" gnus-summary-kill-below)
1639
1640   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1641     "/" gnus-summary-limit-to-subject
1642     "n" gnus-summary-limit-to-articles
1643     "w" gnus-summary-pop-limit
1644     "s" gnus-summary-limit-to-subject
1645     "a" gnus-summary-limit-to-author
1646     "u" gnus-summary-limit-to-unread
1647     "m" gnus-summary-limit-to-marks
1648     "M" gnus-summary-limit-exclude-marks
1649     "v" gnus-summary-limit-to-score
1650     "*" gnus-summary-limit-include-cached
1651     "D" gnus-summary-limit-include-dormant
1652     "T" gnus-summary-limit-include-thread
1653     "d" gnus-summary-limit-exclude-dormant
1654     "t" gnus-summary-limit-to-age
1655     "x" gnus-summary-limit-to-extra
1656     "p" gnus-summary-limit-to-display-predicate
1657     "E" gnus-summary-limit-include-expunged
1658     "c" gnus-summary-limit-exclude-childless-dormant
1659     "C" gnus-summary-limit-mark-excluded-as-read
1660     "o" gnus-summary-insert-old-articles
1661     "N" gnus-summary-insert-new-articles)
1662
1663   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1664     "n" gnus-summary-next-unread-article
1665     "p" gnus-summary-prev-unread-article
1666     "N" gnus-summary-next-article
1667     "P" gnus-summary-prev-article
1668     "\C-n" gnus-summary-next-same-subject
1669     "\C-p" gnus-summary-prev-same-subject
1670     "\M-n" gnus-summary-next-unread-subject
1671     "\M-p" gnus-summary-prev-unread-subject
1672     "f" gnus-summary-first-unread-article
1673     "b" gnus-summary-best-unread-article
1674     "j" gnus-summary-goto-article
1675     "g" gnus-summary-goto-subject
1676     "l" gnus-summary-goto-last-article
1677     "o" gnus-summary-pop-article)
1678
1679   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1680     "k" gnus-summary-kill-thread
1681     "l" gnus-summary-lower-thread
1682     "i" gnus-summary-raise-thread
1683     "T" gnus-summary-toggle-threads
1684     "t" gnus-summary-rethread-current
1685     "^" gnus-summary-reparent-thread
1686     "s" gnus-summary-show-thread
1687     "S" gnus-summary-show-all-threads
1688     "h" gnus-summary-hide-thread
1689     "H" gnus-summary-hide-all-threads
1690     "n" gnus-summary-next-thread
1691     "p" gnus-summary-prev-thread
1692     "u" gnus-summary-up-thread
1693     "o" gnus-summary-top-thread
1694     "d" gnus-summary-down-thread
1695     "#" gnus-uu-mark-thread
1696     "\M-#" gnus-uu-unmark-thread)
1697
1698   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1699     "g" gnus-summary-prepare
1700     "c" gnus-summary-insert-cached-articles)
1701
1702   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1703     "c" gnus-summary-catchup-and-exit
1704     "C" gnus-summary-catchup-all-and-exit
1705     "E" gnus-summary-exit-no-update
1706     "J" gnus-summary-jump-to-other-group
1707     "Q" gnus-summary-exit
1708     "Z" gnus-summary-exit
1709     "n" gnus-summary-catchup-and-goto-next-group
1710     "R" gnus-summary-reselect-current-group
1711     "G" gnus-summary-rescan-group
1712     "N" gnus-summary-next-group
1713     "s" gnus-summary-save-newsrc
1714     "P" gnus-summary-prev-group)
1715
1716   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1717     " " gnus-summary-next-page
1718     "n" gnus-summary-next-page
1719     "\177" gnus-summary-prev-page
1720     [delete] gnus-summary-prev-page
1721     "p" gnus-summary-prev-page
1722     "\r" gnus-summary-scroll-up
1723     "\M-\r" gnus-summary-scroll-down
1724     "<" gnus-summary-beginning-of-article
1725     ">" gnus-summary-end-of-article
1726     "b" gnus-summary-beginning-of-article
1727     "e" gnus-summary-end-of-article
1728     "^" gnus-summary-refer-parent-article
1729     "r" gnus-summary-refer-parent-article
1730     "D" gnus-summary-enter-digest-group
1731     "R" gnus-summary-refer-references
1732     "T" gnus-summary-refer-thread
1733     "g" gnus-summary-show-article
1734     "s" gnus-summary-isearch-article
1735     "P" gnus-summary-print-article
1736     "M" gnus-mailing-list-insinuate
1737     "t" gnus-article-babel)
1738
1739   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1740     "b" gnus-article-add-buttons
1741     "B" gnus-article-add-buttons-to-head
1742     "o" gnus-article-treat-overstrike
1743     "e" gnus-article-emphasize
1744     "w" gnus-article-fill-cited-article
1745     "Q" gnus-article-fill-long-lines
1746     "C" gnus-article-capitalize-sentences
1747     "c" gnus-article-remove-cr
1748     "Z" gnus-article-decode-HZ
1749     "h" gnus-article-wash-html
1750     "f" gnus-article-display-x-face
1751     "l" gnus-summary-stop-page-breaking
1752     "r" gnus-summary-caesar-message
1753     "t" gnus-article-toggle-headers
1754     "g" gnus-treat-smiley
1755     "v" gnus-summary-verbose-headers
1756     "m" gnus-summary-toggle-mime
1757     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1758     "p" gnus-article-verify-x-pgp-sig
1759     "d" gnus-article-treat-dumbquotes)
1760
1761   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1762     "a" gnus-article-hide
1763     "h" gnus-article-toggle-headers
1764     "b" gnus-article-hide-boring-headers
1765     "s" gnus-article-hide-signature
1766     "c" gnus-article-hide-citation
1767     "C" gnus-article-hide-citation-in-followups
1768     "l" gnus-article-hide-list-identifiers
1769     "p" gnus-article-hide-pgp
1770     "B" gnus-article-strip-banner
1771     "P" gnus-article-hide-pem
1772     "\C-c" gnus-article-hide-citation-maybe)
1773
1774   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1775     "a" gnus-article-highlight
1776     "h" gnus-article-highlight-headers
1777     "c" gnus-article-highlight-citation
1778     "s" gnus-article-highlight-signature)
1779
1780   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1781     "f" gnus-article-treat-fold-headers
1782     "u" gnus-article-treat-unfold-headers
1783     "n" gnus-article-treat-fold-newsgroups)
1784
1785   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1786     "x" gnus-article-display-x-face
1787     "s" gnus-treat-smiley
1788     "D" gnus-article-remove-images
1789     "f" gnus-treat-from-picon
1790     "m" gnus-treat-mail-picon
1791     "n" gnus-treat-newsgroups-picon)
1792
1793   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1794     "z" gnus-article-date-ut
1795     "u" gnus-article-date-ut
1796     "l" gnus-article-date-local
1797     "p" gnus-article-date-english
1798     "e" gnus-article-date-lapsed
1799     "o" gnus-article-date-original
1800     "i" gnus-article-date-iso8601
1801     "s" gnus-article-date-user)
1802
1803   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1804     "t" gnus-article-remove-trailing-blank-lines
1805     "l" gnus-article-strip-leading-blank-lines
1806     "m" gnus-article-strip-multiple-blank-lines
1807     "a" gnus-article-strip-blank-lines
1808     "A" gnus-article-strip-all-blank-lines
1809     "s" gnus-article-strip-leading-space
1810     "e" gnus-article-strip-trailing-space
1811     "w" gnus-article-remove-leading-whitespace)
1812
1813   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1814     "v" gnus-version
1815     "f" gnus-summary-fetch-faq
1816     "d" gnus-summary-describe-group
1817     "h" gnus-summary-describe-briefly
1818     "i" gnus-info-find-node)
1819
1820   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1821     "e" gnus-summary-expire-articles
1822     "\M-\C-e" gnus-summary-expire-articles-now
1823     "\177" gnus-summary-delete-article
1824     [delete] gnus-summary-delete-article
1825     [backspace] gnus-summary-delete-article
1826     "m" gnus-summary-move-article
1827     "r" gnus-summary-respool-article
1828     "w" gnus-summary-edit-article
1829     "c" gnus-summary-copy-article
1830     "B" gnus-summary-crosspost-article
1831     "q" gnus-summary-respool-query
1832     "t" gnus-summary-respool-trace
1833     "i" gnus-summary-import-article
1834     "I" gnus-summary-create-article
1835     "p" gnus-summary-article-posted-p)
1836
1837   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1838     "o" gnus-summary-save-article
1839     "m" gnus-summary-save-article-mail
1840     "F" gnus-summary-write-article-file
1841     "r" gnus-summary-save-article-rmail
1842     "f" gnus-summary-save-article-file
1843     "b" gnus-summary-save-article-body-file
1844     "h" gnus-summary-save-article-folder
1845     "v" gnus-summary-save-article-vm
1846     "p" gnus-summary-pipe-output
1847     "P" gnus-summary-muttprint
1848     "s" gnus-soup-add-article)
1849
1850   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1851     "b" gnus-summary-display-buttonized
1852     "m" gnus-summary-repair-multipart
1853     "v" gnus-article-view-part
1854     "o" gnus-article-save-part
1855     "c" gnus-article-copy-part
1856     "C" gnus-article-view-part-as-charset
1857     "e" gnus-article-externalize-part
1858     "E" gnus-article-encrypt-body
1859     "i" gnus-article-inline-part
1860     "|" gnus-article-pipe-part))
1861
1862 (defvar gnus-article-post-menu nil)
1863
1864 (defun gnus-summary-make-menu-bar ()
1865   (gnus-turn-off-edit-menu 'summary)
1866
1867   (unless (boundp 'gnus-summary-misc-menu)
1868
1869     (easy-menu-define
1870      gnus-summary-kill-menu gnus-summary-mode-map ""
1871      (cons
1872       "Score"
1873       (nconc
1874        (list
1875         ["Customize" gnus-score-customize t])
1876        (gnus-make-score-map 'increase)
1877        (gnus-make-score-map 'lower)
1878        '(("Mark"
1879           ["Kill below" gnus-summary-kill-below t]
1880           ["Mark above" gnus-summary-mark-above t]
1881           ["Tick above" gnus-summary-tick-above t]
1882           ["Clear above" gnus-summary-clear-above t])
1883          ["Current score" gnus-summary-current-score t]
1884          ["Set score" gnus-summary-set-score t]
1885          ["Switch current score file..." gnus-score-change-score-file t]
1886          ["Set mark below..." gnus-score-set-mark-below t]
1887          ["Set expunge below..." gnus-score-set-expunge-below t]
1888          ["Edit current score file" gnus-score-edit-current-scores t]
1889          ["Edit score file" gnus-score-edit-file t]
1890          ["Trace score" gnus-score-find-trace t]
1891          ["Find words" gnus-score-find-favourite-words t]
1892          ["Rescore buffer" gnus-summary-rescore t]
1893          ["Increase score..." gnus-summary-increase-score t]
1894          ["Lower score..." gnus-summary-lower-score t]))))
1895
1896     ;; Define both the Article menu in the summary buffer and the
1897     ;; equivalent Commands menu in the article buffer here for
1898     ;; consistency.
1899     (let ((innards
1900            `(("Hide"
1901               ["All" gnus-article-hide t]
1902               ["Headers" gnus-article-toggle-headers t]
1903               ["Signature" gnus-article-hide-signature t]
1904               ["Citation" gnus-article-hide-citation t]
1905               ["List identifiers" gnus-article-hide-list-identifiers t]
1906               ["PGP" gnus-article-hide-pgp t]
1907               ["Banner" gnus-article-strip-banner t]
1908               ["Boring headers" gnus-article-hide-boring-headers t])
1909              ("Highlight"
1910               ["All" gnus-article-highlight t]
1911               ["Headers" gnus-article-highlight-headers t]
1912               ["Signature" gnus-article-highlight-signature t]
1913               ["Citation" gnus-article-highlight-citation t])
1914              ("Date"
1915               ["Local" gnus-article-date-local t]
1916               ["ISO8601" gnus-article-date-iso8601 t]
1917               ["UT" gnus-article-date-ut t]
1918               ["Original" gnus-article-date-original t]
1919               ["Lapsed" gnus-article-date-lapsed t]
1920               ["User-defined" gnus-article-date-user t])
1921              ("Display"
1922               ["Remove images" gnus-article-remove-images t]
1923               ["Toggle smiley" gnus-treat-smiley t]
1924               ["Show X-Face" gnus-article-display-x-face t]
1925               ["Show picons in From" gnus-treat-from-picon t]
1926               ["Show picons in mail headers" gnus-treat-mail-picon t]
1927               ["Show picons in news headers" gnus-treat-newsgroups-picon t])
1928              ("Washing"
1929               ("Remove Blanks"
1930                ["Leading" gnus-article-strip-leading-blank-lines t]
1931                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1932                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1933                ["All of the above" gnus-article-strip-blank-lines t]
1934                ["All" gnus-article-strip-all-blank-lines t]
1935                ["Leading space" gnus-article-strip-leading-space t]
1936                ["Trailing space" gnus-article-strip-trailing-space t]
1937                ["Leading space in headers"
1938                 gnus-article-remove-leading-whitespace t])
1939               ["Overstrike" gnus-article-treat-overstrike t]
1940               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1941               ["Emphasis" gnus-article-emphasize t]
1942               ["Word wrap" gnus-article-fill-cited-article t]
1943               ["Fill long lines" gnus-article-fill-long-lines t]
1944               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1945               ["CR" gnus-article-remove-cr t]
1946               ["Rot 13" gnus-summary-caesar-message
1947                ,@(if (featurep 'xemacs) '(t)
1948                    '(:help "\"Caesar rotate\" article by 13"))]
1949               ["Unix pipe" gnus-summary-pipe-message t]
1950               ["Add buttons" gnus-article-add-buttons t]
1951               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1952               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1953               ["Toggle MIME" gnus-summary-toggle-mime t]
1954               ["Verbose header" gnus-summary-verbose-headers t]
1955               ["Toggle header" gnus-summary-toggle-header t]
1956               ["Unfold headers" gnus-article-treat-unfold-headers t]
1957               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
1958               ["Html" gnus-article-wash-html t]
1959               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1960               ["HZ" gnus-article-decode-HZ t])
1961              ("Output"
1962               ["Save in default format" gnus-summary-save-article
1963                ,@(if (featurep 'xemacs) '(t)
1964                    '(:help "Save article using default method"))]
1965               ["Save in file" gnus-summary-save-article-file
1966                ,@(if (featurep 'xemacs) '(t)
1967                    '(:help "Save article in file"))]
1968               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1969               ["Save in MH folder" gnus-summary-save-article-folder t]
1970               ["Save in VM folder" gnus-summary-save-article-vm t]
1971               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1972               ["Save body in file" gnus-summary-save-article-body-file t]
1973               ["Pipe through a filter" gnus-summary-pipe-output t]
1974               ["Add to SOUP packet" gnus-soup-add-article t]
1975               ["Print with Muttprint" gnus-summary-muttprint t]
1976               ["Print" gnus-summary-print-article t])
1977              ("Backend"
1978               ["Respool article..." gnus-summary-respool-article t]
1979               ["Move article..." gnus-summary-move-article
1980                (gnus-check-backend-function
1981                 'request-move-article gnus-newsgroup-name)]
1982               ["Copy article..." gnus-summary-copy-article t]
1983               ["Crosspost article..." gnus-summary-crosspost-article
1984                (gnus-check-backend-function
1985                 'request-replace-article gnus-newsgroup-name)]
1986               ["Import file..." gnus-summary-import-article t]
1987               ["Create article..." gnus-summary-create-article t]
1988               ["Check if posted" gnus-summary-article-posted-p t]
1989               ["Edit article" gnus-summary-edit-article
1990                (not (gnus-group-read-only-p))]
1991               ["Delete article" gnus-summary-delete-article
1992                (gnus-check-backend-function
1993                 'request-expire-articles gnus-newsgroup-name)]
1994               ["Query respool" gnus-summary-respool-query t]
1995               ["Trace respool" gnus-summary-respool-trace t]
1996               ["Delete expirable articles" gnus-summary-expire-articles-now
1997                (gnus-check-backend-function
1998                 'request-expire-articles gnus-newsgroup-name)])
1999              ("Extract"
2000               ["Uudecode" gnus-uu-decode-uu
2001                ,@(if (featurep 'xemacs) '(t)
2002                    '(:help "Decode uuencoded article(s)"))]
2003               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2004               ["Unshar" gnus-uu-decode-unshar t]
2005               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2006               ["Save" gnus-uu-decode-save t]
2007               ["Binhex" gnus-uu-decode-binhex t]
2008               ["Postscript" gnus-uu-decode-postscript t])
2009              ("Cache"
2010               ["Enter article" gnus-cache-enter-article t]
2011               ["Remove article" gnus-cache-remove-article t])
2012              ["Translate" gnus-article-babel t]
2013              ["Select article buffer" gnus-summary-select-article-buffer t]
2014              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2015              ["Isearch article..." gnus-summary-isearch-article t]
2016              ["Beginning of the article" gnus-summary-beginning-of-article t]
2017              ["End of the article" gnus-summary-end-of-article t]
2018              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2019              ["Fetch referenced articles" gnus-summary-refer-references t]
2020              ["Fetch current thread" gnus-summary-refer-thread t]
2021              ["Fetch article with id..." gnus-summary-refer-article t]
2022              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2023              ["Redisplay" gnus-summary-show-article t]
2024              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2025       (easy-menu-define
2026        gnus-summary-article-menu gnus-summary-mode-map ""
2027        (cons "Article" innards))
2028
2029       (if (not (keymapp gnus-summary-article-menu))
2030           (easy-menu-define
2031            gnus-article-commands-menu gnus-article-mode-map ""
2032            (cons "Commands" innards))
2033         ;; in Emacs, don't share menu.
2034         (setq gnus-article-commands-menu
2035               (copy-keymap gnus-summary-article-menu))
2036         (define-key gnus-article-mode-map [menu-bar commands]
2037           (cons "Commands" gnus-article-commands-menu))))
2038
2039     (easy-menu-define
2040      gnus-summary-thread-menu gnus-summary-mode-map ""
2041      '("Threads"
2042        ["Toggle threading" gnus-summary-toggle-threads t]
2043        ["Hide threads" gnus-summary-hide-all-threads t]
2044        ["Show threads" gnus-summary-show-all-threads t]
2045        ["Hide thread" gnus-summary-hide-thread t]
2046        ["Show thread" gnus-summary-show-thread t]
2047        ["Go to next thread" gnus-summary-next-thread t]
2048        ["Go to previous thread" gnus-summary-prev-thread t]
2049        ["Go down thread" gnus-summary-down-thread t]
2050        ["Go up thread" gnus-summary-up-thread t]
2051        ["Top of thread" gnus-summary-top-thread t]
2052        ["Mark thread as read" gnus-summary-kill-thread t]
2053        ["Lower thread score" gnus-summary-lower-thread t]
2054        ["Raise thread score" gnus-summary-raise-thread t]
2055        ["Rethread current" gnus-summary-rethread-current t]))
2056
2057     (easy-menu-define
2058      gnus-summary-post-menu gnus-summary-mode-map ""
2059      `("Post"
2060        ["Send a message (mail or news)" gnus-summary-post-news
2061         ,@(if (featurep 'xemacs) '(t)
2062             '(:help "Post an article"))]
2063        ["Followup" gnus-summary-followup
2064         ,@(if (featurep 'xemacs) '(t)
2065             '(:help "Post followup to this article"))]
2066        ["Followup and yank" gnus-summary-followup-with-original
2067         ,@(if (featurep 'xemacs) '(t)
2068             '(:help "Post followup to this article, quoting its contents"))]
2069        ["Supersede article" gnus-summary-supersede-article t]
2070        ["Cancel article" gnus-summary-cancel-article
2071         ,@(if (featurep 'xemacs) '(t)
2072             '(:help "Cancel an article you posted"))]
2073        ["Reply" gnus-summary-reply t]
2074        ["Reply and yank" gnus-summary-reply-with-original t]
2075        ["Wide reply" gnus-summary-wide-reply t]
2076        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2077         ,@(if (featurep 'xemacs) '(t)
2078             '(:help "Mail a reply, quoting this article"))]
2079        ["Very wide reply" gnus-summary-very-wide-reply t]
2080        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2081         ,@(if (featurep 'xemacs) '(t)
2082             '(:help "Mail a very wide reply, quoting this article"))]
2083        ["Mail forward" gnus-summary-mail-forward t]
2084        ["Post forward" gnus-summary-post-forward t]
2085        ["Digest and mail" gnus-summary-digest-mail-forward t]
2086        ["Digest and post" gnus-summary-digest-post-forward t]
2087        ["Resend message" gnus-summary-resend-message t]
2088        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2089        ["Send a mail" gnus-summary-mail-other-window t]
2090        ["Create a local message" gnus-summary-news-other-window t]
2091        ["Uuencode and post" gnus-uu-post-news
2092         ,@(if (featurep 'xemacs) '(t)
2093             '(:help "Post a uuencoded article"))]
2094        ["Followup via news" gnus-summary-followup-to-mail t]
2095        ["Followup via news and yank"
2096         gnus-summary-followup-to-mail-with-original t]
2097        ;;("Draft"
2098        ;;["Send" gnus-summary-send-draft t]
2099        ;;["Send bounced" gnus-resend-bounced-mail t])
2100        ))
2101
2102     (cond
2103      ((not (keymapp gnus-summary-post-menu))
2104       (setq gnus-article-post-menu gnus-summary-post-menu))
2105      ((not gnus-article-post-menu)
2106       ;; Don't share post menu.
2107       (setq gnus-article-post-menu
2108             (copy-keymap gnus-summary-post-menu))))
2109     (define-key gnus-article-mode-map [menu-bar post]
2110       (cons "Post" gnus-article-post-menu))
2111
2112     (easy-menu-define
2113      gnus-summary-misc-menu gnus-summary-mode-map ""
2114      `("Gnus"
2115        ("Mark Read"
2116         ["Mark as read" gnus-summary-mark-as-read-forward t]
2117         ["Mark same subject and select"
2118          gnus-summary-kill-same-subject-and-select t]
2119         ["Mark same subject" gnus-summary-kill-same-subject t]
2120         ["Catchup" gnus-summary-catchup
2121          ,@(if (featurep 'xemacs) '(t)
2122              '(:help "Mark unread articles in this group as read"))]
2123         ["Catchup all" gnus-summary-catchup-all t]
2124         ["Catchup to here" gnus-summary-catchup-to-here t]
2125         ["Catchup from here" gnus-summary-catchup-from-here t]
2126         ["Catchup region" gnus-summary-mark-region-as-read t]
2127         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2128        ("Mark Various"
2129         ["Tick" gnus-summary-tick-article-forward t]
2130         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2131         ["Remove marks" gnus-summary-clear-mark-forward t]
2132         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2133         ["Set bookmark" gnus-summary-set-bookmark t]
2134         ["Remove bookmark" gnus-summary-remove-bookmark t])
2135        ("Limit to"
2136         ["Marks..." gnus-summary-limit-to-marks t]
2137         ["Subject..." gnus-summary-limit-to-subject t]
2138         ["Author..." gnus-summary-limit-to-author t]
2139         ["Age..." gnus-summary-limit-to-age t]
2140         ["Extra..." gnus-summary-limit-to-extra t]
2141         ["Score" gnus-summary-limit-to-score t]
2142         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2143         ["Unread" gnus-summary-limit-to-unread t]
2144         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2145         ["Articles" gnus-summary-limit-to-articles t]
2146         ["Pop limit" gnus-summary-pop-limit t]
2147         ["Show dormant" gnus-summary-limit-include-dormant t]
2148         ["Hide childless dormant"
2149          gnus-summary-limit-exclude-childless-dormant t]
2150         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2151         ["Hide marked" gnus-summary-limit-exclude-marks t]
2152         ["Show expunged" gnus-summary-limit-include-expunged t])
2153        ("Process Mark"
2154         ["Set mark" gnus-summary-mark-as-processable t]
2155         ["Remove mark" gnus-summary-unmark-as-processable t]
2156         ["Remove all marks" gnus-summary-unmark-all-processable t]
2157         ["Mark above" gnus-uu-mark-over t]
2158         ["Mark series" gnus-uu-mark-series t]
2159         ["Mark region" gnus-uu-mark-region t]
2160         ["Unmark region" gnus-uu-unmark-region t]
2161         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2162         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2163         ["Mark all" gnus-uu-mark-all t]
2164         ["Mark buffer" gnus-uu-mark-buffer t]
2165         ["Mark sparse" gnus-uu-mark-sparse t]
2166         ["Mark thread" gnus-uu-mark-thread t]
2167         ["Unmark thread" gnus-uu-unmark-thread t]
2168         ("Process Mark Sets"
2169          ["Kill" gnus-summary-kill-process-mark t]
2170          ["Yank" gnus-summary-yank-process-mark
2171           gnus-newsgroup-process-stack]
2172          ["Save" gnus-summary-save-process-mark t]))
2173        ("Scroll article"
2174         ["Page forward" gnus-summary-next-page
2175          ,@(if (featurep 'xemacs) '(t)
2176              '(:help "Show next page of article"))]
2177         ["Page backward" gnus-summary-prev-page
2178          ,@(if (featurep 'xemacs) '(t)
2179              '(:help "Show previous page of article"))]
2180         ["Line forward" gnus-summary-scroll-up t])
2181        ("Move"
2182         ["Next unread article" gnus-summary-next-unread-article t]
2183         ["Previous unread article" gnus-summary-prev-unread-article t]
2184         ["Next article" gnus-summary-next-article t]
2185         ["Previous article" gnus-summary-prev-article t]
2186         ["Next unread subject" gnus-summary-next-unread-subject t]
2187         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2188         ["Next article same subject" gnus-summary-next-same-subject t]
2189         ["Previous article same subject" gnus-summary-prev-same-subject t]
2190         ["First unread article" gnus-summary-first-unread-article t]
2191         ["Best unread article" gnus-summary-best-unread-article t]
2192         ["Go to subject number..." gnus-summary-goto-subject t]
2193         ["Go to article number..." gnus-summary-goto-article t]
2194         ["Go to the last article" gnus-summary-goto-last-article t]
2195         ["Pop article off history" gnus-summary-pop-article t])
2196        ("Sort"
2197         ["Sort by number" gnus-summary-sort-by-number t]
2198         ["Sort by author" gnus-summary-sort-by-author t]
2199         ["Sort by subject" gnus-summary-sort-by-subject t]
2200         ["Sort by date" gnus-summary-sort-by-date t]
2201         ["Sort by score" gnus-summary-sort-by-score t]
2202         ["Sort by lines" gnus-summary-sort-by-lines t]
2203         ["Sort by characters" gnus-summary-sort-by-chars t]
2204         ["Original sort" gnus-summary-sort-by-original t])
2205        ("Help"
2206         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2207         ["Describe group" gnus-summary-describe-group t]
2208         ["Read manual" gnus-info-find-node t])
2209        ("Modes"
2210         ["Pick and read" gnus-pick-mode t]
2211         ["Binary" gnus-binary-mode t])
2212        ("Regeneration"
2213         ["Regenerate" gnus-summary-prepare t]
2214         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2215         ["Toggle threading" gnus-summary-toggle-threads t])
2216        ["See old articles" gnus-summary-insert-old-articles t]
2217        ["See new articles" gnus-summary-insert-new-articles t]
2218        ["Filter articles..." gnus-summary-execute-command t]
2219        ["Run command on subjects..." gnus-summary-universal-argument t]
2220        ["Search articles forward..." gnus-summary-search-article-forward t]
2221        ["Search articles backward..." gnus-summary-search-article-backward t]
2222        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2223        ["Expand window" gnus-summary-expand-window t]
2224        ["Expire expirable articles" gnus-summary-expire-articles
2225         (gnus-check-backend-function
2226          'request-expire-articles gnus-newsgroup-name)]
2227        ["Edit local kill file" gnus-summary-edit-local-kill t]
2228        ["Edit main kill file" gnus-summary-edit-global-kill t]
2229        ["Edit group parameters" gnus-summary-edit-parameters t]
2230        ["Customize group parameters" gnus-summary-customize-parameters t]
2231        ["Send a bug report" gnus-bug t]
2232        ("Exit"
2233         ["Catchup and exit" gnus-summary-catchup-and-exit
2234          ,@(if (featurep 'xemacs) '(t)
2235              '(:help "Mark unread articles in this group as read, then exit"))]
2236         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2237         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2238         ["Exit group" gnus-summary-exit
2239          ,@(if (featurep 'xemacs) '(t)
2240              '(:help "Exit current group, return to group selection mode"))]
2241         ["Exit group without updating" gnus-summary-exit-no-update t]
2242         ["Exit and goto next group" gnus-summary-next-group t]
2243         ["Exit and goto prev group" gnus-summary-prev-group t]
2244         ["Reselect group" gnus-summary-reselect-current-group t]
2245         ["Rescan group" gnus-summary-rescan-group t]
2246         ["Update dribble" gnus-summary-save-newsrc t])))
2247
2248     (gnus-run-hooks 'gnus-summary-menu-hook)))
2249
2250 (defvar gnus-summary-tool-bar-map nil)
2251
2252 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2253 (defun gnus-summary-make-tool-bar ()
2254   (if (and (fboundp 'tool-bar-add-item-from-menu)
2255            (default-value 'tool-bar-mode)
2256            (not gnus-summary-tool-bar-map))
2257       (setq gnus-summary-tool-bar-map
2258             (let ((tool-bar-map (make-sparse-keymap))
2259                   (load-path (mm-image-load-path)))
2260               (tool-bar-add-item-from-menu
2261                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2262               (tool-bar-add-item-from-menu
2263                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2264               (tool-bar-add-item-from-menu
2265                'gnus-summary-post-news "post" gnus-summary-mode-map)
2266               (tool-bar-add-item-from-menu
2267                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2268               (tool-bar-add-item-from-menu
2269                'gnus-summary-followup "followup" gnus-summary-mode-map)
2270               (tool-bar-add-item-from-menu
2271                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2272               (tool-bar-add-item-from-menu
2273                'gnus-summary-reply "reply" gnus-summary-mode-map)
2274               (tool-bar-add-item-from-menu
2275                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2276               (tool-bar-add-item-from-menu
2277                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2278               (tool-bar-add-item-from-menu
2279                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2280               (tool-bar-add-item-from-menu
2281                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2282               (tool-bar-add-item-from-menu
2283                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2284               (tool-bar-add-item-from-menu
2285                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2286               (tool-bar-add-item-from-menu
2287                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2288               (tool-bar-add-item-from-menu
2289                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2290               tool-bar-map)))
2291   (if gnus-summary-tool-bar-map
2292       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2293
2294 (defun gnus-score-set-default (var value)
2295   "A version of set that updates the GNU Emacs menu-bar."
2296   (set var value)
2297   ;; It is the message that forces the active status to be updated.
2298   (message ""))
2299
2300 (defun gnus-make-score-map (type)
2301   "Make a summary score map of type TYPE."
2302   (if t
2303       nil
2304     (let ((headers '(("author" "from" string)
2305                      ("subject" "subject" string)
2306                      ("article body" "body" string)
2307                      ("article head" "head" string)
2308                      ("xref" "xref" string)
2309                      ("extra header" "extra" string)
2310                      ("lines" "lines" number)
2311                      ("followups to author" "followup" string)))
2312           (types '((number ("less than" <)
2313                            ("greater than" >)
2314                            ("equal" =))
2315                    (string ("substring" s)
2316                            ("exact string" e)
2317                            ("fuzzy string" f)
2318                            ("regexp" r))))
2319           (perms '(("temporary" (current-time-string))
2320                    ("permanent" nil)
2321                    ("immediate" now)))
2322           header)
2323       (list
2324        (apply
2325         'nconc
2326         (list
2327          (if (eq type 'lower)
2328              "Lower score"
2329            "Increase score"))
2330         (let (outh)
2331           (while headers
2332             (setq header (car headers))
2333             (setq outh
2334                   (cons
2335                    (apply
2336                     'nconc
2337                     (list (car header))
2338                     (let ((ts (cdr (assoc (nth 2 header) types)))
2339                           outt)
2340                       (while ts
2341                         (setq outt
2342                               (cons
2343                                (apply
2344                                 'nconc
2345                                 (list (caar ts))
2346                                 (let ((ps perms)
2347                                       outp)
2348                                   (while ps
2349                                     (setq outp
2350                                           (cons
2351                                            (vector
2352                                             (caar ps)
2353                                             (list
2354                                              'gnus-summary-score-entry
2355                                              (nth 1 header)
2356                                              (if (or (string= (nth 1 header)
2357                                                               "head")
2358                                                      (string= (nth 1 header)
2359                                                               "body"))
2360                                                  ""
2361                                                (list 'gnus-summary-header
2362                                                      (nth 1 header)))
2363                                              (list 'quote (nth 1 (car ts)))
2364                                              (list 'gnus-score-delta-default
2365                                                    nil)
2366                                              (nth 1 (car ps))
2367                                              t)
2368                                             t)
2369                                            outp))
2370                                     (setq ps (cdr ps)))
2371                                   (list (nreverse outp))))
2372                                outt))
2373                         (setq ts (cdr ts)))
2374                       (list (nreverse outt))))
2375                    outh))
2376             (setq headers (cdr headers)))
2377           (list (nreverse outh))))))))
2378
2379 \f
2380
2381 (defun gnus-summary-mode (&optional group)
2382   "Major mode for reading articles.
2383
2384 All normal editing commands are switched off.
2385 \\<gnus-summary-mode-map>
2386 Each line in this buffer represents one article.  To read an
2387 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2388 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2389 respectively.
2390
2391 You can also post articles and send mail from this buffer.  To
2392 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2393 of an article, type `\\[gnus-summary-reply]'.
2394
2395 There are approx. one gazillion commands you can execute in this
2396 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2397
2398 The following commands are available:
2399
2400 \\{gnus-summary-mode-map}"
2401   (interactive)
2402   (kill-all-local-variables)
2403   (when (gnus-visual-p 'summary-menu 'menu)
2404     (gnus-summary-make-menu-bar)
2405     (gnus-summary-make-tool-bar))
2406   (gnus-summary-make-local-variables)
2407   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2408     (gnus-summary-make-local-variables))
2409   (gnus-make-thread-indent-array)
2410   (gnus-simplify-mode-line)
2411   (setq major-mode 'gnus-summary-mode)
2412   (setq mode-name "Summary")
2413   (make-local-variable 'minor-mode-alist)
2414   (use-local-map gnus-summary-mode-map)
2415   (buffer-disable-undo)
2416   (setq buffer-read-only t)             ;Disable modification
2417   (setq truncate-lines t)
2418   (setq selective-display t)
2419   (setq selective-display-ellipses t)   ;Display `...'
2420   (gnus-summary-set-display-table)
2421   (gnus-set-default-directory)
2422   (setq gnus-newsgroup-name group)
2423   (unless (gnus-news-group-p group)
2424     (setq gnus-newsgroup-incorporated
2425           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2426   (make-local-variable 'gnus-summary-line-format)
2427   (make-local-variable 'gnus-summary-line-format-spec)
2428   (make-local-variable 'gnus-summary-dummy-line-format)
2429   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2430   (make-local-variable 'gnus-summary-mark-positions)
2431   (make-local-hook 'pre-command-hook)
2432   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2433   (gnus-run-hooks 'gnus-summary-mode-hook)
2434   (turn-on-gnus-mailing-list-mode)
2435   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2436   (gnus-update-summary-mark-positions))
2437
2438 (defun gnus-summary-make-local-variables ()
2439   "Make all the local summary buffer variables."
2440   (let (global)
2441     (dolist (local gnus-summary-local-variables)
2442       (if (consp local)
2443           (progn
2444             (if (eq (cdr local) 'global)
2445                 ;; Copy the global value of the variable.
2446                 (setq global (symbol-value (car local)))
2447               ;; Use the value from the list.
2448               (setq global (eval (cdr local))))
2449             (set (make-local-variable (car local)) global))
2450         ;; Simple nil-valued local variable.
2451         (set (make-local-variable local) nil)))))
2452
2453 (defun gnus-summary-clear-local-variables ()
2454   (let ((locals gnus-summary-local-variables))
2455     (while locals
2456       (if (consp (car locals))
2457           (and (vectorp (caar locals))
2458                (set (caar locals) nil))
2459         (and (vectorp (car locals))
2460              (set (car locals) nil)))
2461       (setq locals (cdr locals)))))
2462
2463 ;; Summary data functions.
2464
2465 (defmacro gnus-data-number (data)
2466   `(car ,data))
2467
2468 (defmacro gnus-data-set-number (data number)
2469   `(setcar ,data ,number))
2470
2471 (defmacro gnus-data-mark (data)
2472   `(nth 1 ,data))
2473
2474 (defmacro gnus-data-set-mark (data mark)
2475   `(setcar (nthcdr 1 ,data) ,mark))
2476
2477 (defmacro gnus-data-pos (data)
2478   `(nth 2 ,data))
2479
2480 (defmacro gnus-data-set-pos (data pos)
2481   `(setcar (nthcdr 2 ,data) ,pos))
2482
2483 (defmacro gnus-data-header (data)
2484   `(nth 3 ,data))
2485
2486 (defmacro gnus-data-set-header (data header)
2487   `(setcar (nthcdr 3 ,data) ,header))
2488
2489 (defmacro gnus-data-level (data)
2490   `(nth 4 ,data))
2491
2492 (defmacro gnus-data-unread-p (data)
2493   `(= (nth 1 ,data) gnus-unread-mark))
2494
2495 (defmacro gnus-data-read-p (data)
2496   `(/= (nth 1 ,data) gnus-unread-mark))
2497
2498 (defmacro gnus-data-pseudo-p (data)
2499   `(consp (nth 3 ,data)))
2500
2501 (defmacro gnus-data-find (number)
2502   `(assq ,number gnus-newsgroup-data))
2503
2504 (defmacro gnus-data-find-list (number &optional data)
2505   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2506      (memq (assq ,number bdata)
2507            bdata)))
2508
2509 (defmacro gnus-data-make (number mark pos header level)
2510   `(list ,number ,mark ,pos ,header ,level))
2511
2512 (defun gnus-data-enter (after-article number mark pos header level offset)
2513   (let ((data (gnus-data-find-list after-article)))
2514     (unless data
2515       (error "No such article: %d" after-article))
2516     (setcdr data (cons (gnus-data-make number mark pos header level)
2517                        (cdr data)))
2518     (setq gnus-newsgroup-data-reverse nil)
2519     (gnus-data-update-list (cddr data) offset)))
2520
2521 (defun gnus-data-enter-list (after-article list &optional offset)
2522   (when list
2523     (let ((data (and after-article (gnus-data-find-list after-article)))
2524           (ilist list))
2525       (if (not (or data
2526                    after-article))
2527           (let ((odata gnus-newsgroup-data))
2528             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2529             (when offset
2530               (gnus-data-update-list odata offset)))
2531         ;; Find the last element in the list to be spliced into the main
2532         ;; list.
2533         (while (cdr list)
2534           (setq list (cdr list)))
2535         (if (not data)
2536             (progn
2537               (setcdr list gnus-newsgroup-data)
2538               (setq gnus-newsgroup-data ilist)
2539               (when offset
2540                 (gnus-data-update-list (cdr list) offset)))
2541           (setcdr list (cdr data))
2542           (setcdr data ilist)
2543           (when offset
2544             (gnus-data-update-list (cdr list) offset))))
2545       (setq gnus-newsgroup-data-reverse nil))))
2546
2547 (defun gnus-data-remove (article &optional offset)
2548   (let ((data gnus-newsgroup-data))
2549     (if (= (gnus-data-number (car data)) article)
2550         (progn
2551           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2552                 gnus-newsgroup-data-reverse nil)
2553           (when offset
2554             (gnus-data-update-list gnus-newsgroup-data offset)))
2555       (while (cdr data)
2556         (when (= (gnus-data-number (cadr data)) article)
2557           (setcdr data (cddr data))
2558           (when offset
2559             (gnus-data-update-list (cdr data) offset))
2560           (setq data nil
2561                 gnus-newsgroup-data-reverse nil))
2562         (setq data (cdr data))))))
2563
2564 (defmacro gnus-data-list (backward)
2565   `(if ,backward
2566        (or gnus-newsgroup-data-reverse
2567            (setq gnus-newsgroup-data-reverse
2568                  (reverse gnus-newsgroup-data)))
2569      gnus-newsgroup-data))
2570
2571 (defun gnus-data-update-list (data offset)
2572   "Add OFFSET to the POS of all data entries in DATA."
2573   (setq gnus-newsgroup-data-reverse nil)
2574   (while data
2575     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2576     (setq data (cdr data))))
2577
2578 (defun gnus-summary-article-pseudo-p (article)
2579   "Say whether this article is a pseudo article or not."
2580   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2581
2582 (defmacro gnus-summary-article-sparse-p (article)
2583   "Say whether this article is a sparse article or not."
2584   `(memq ,article gnus-newsgroup-sparse))
2585
2586 (defmacro gnus-summary-article-ancient-p (article)
2587   "Say whether this article is a sparse article or not."
2588   `(memq ,article gnus-newsgroup-ancient))
2589
2590 (defun gnus-article-parent-p (number)
2591   "Say whether this article is a parent or not."
2592   (let ((data (gnus-data-find-list number)))
2593     (and (cdr data)                     ; There has to be an article after...
2594          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2595             (gnus-data-level (nth 1 data))))))
2596
2597 (defun gnus-article-children (number)
2598   "Return a list of all children to NUMBER."
2599   (let* ((data (gnus-data-find-list number))
2600          (level (gnus-data-level (car data)))
2601          children)
2602     (setq data (cdr data))
2603     (while (and data
2604                 (= (gnus-data-level (car data)) (1+ level)))
2605       (push (gnus-data-number (car data)) children)
2606       (setq data (cdr data)))
2607     children))
2608
2609 (defmacro gnus-summary-skip-intangible ()
2610   "If the current article is intangible, then jump to a different article."
2611   '(let ((to (get-text-property (point) 'gnus-intangible)))
2612      (and to (gnus-summary-goto-subject to))))
2613
2614 (defmacro gnus-summary-article-intangible-p ()
2615   "Say whether this article is intangible or not."
2616   '(get-text-property (point) 'gnus-intangible))
2617
2618 (defun gnus-article-read-p (article)
2619   "Say whether ARTICLE is read or not."
2620   (not (or (memq article gnus-newsgroup-marked)
2621            (memq article gnus-newsgroup-unreads)
2622            (memq article gnus-newsgroup-unselected)
2623            (memq article gnus-newsgroup-dormant))))
2624
2625 ;; Some summary mode macros.
2626
2627 (defmacro gnus-summary-article-number ()
2628   "The article number of the article on the current line.
2629 If there isn's an article number here, then we return the current
2630 article number."
2631   '(progn
2632      (gnus-summary-skip-intangible)
2633      (or (get-text-property (point) 'gnus-number)
2634          (gnus-summary-last-subject))))
2635
2636 (defmacro gnus-summary-article-header (&optional number)
2637   "Return the header of article NUMBER."
2638   `(gnus-data-header (gnus-data-find
2639                       ,(or number '(gnus-summary-article-number)))))
2640
2641 (defmacro gnus-summary-thread-level (&optional number)
2642   "Return the level of thread that starts with article NUMBER."
2643   `(if (and (eq gnus-summary-make-false-root 'dummy)
2644             (get-text-property (point) 'gnus-intangible))
2645        0
2646      (gnus-data-level (gnus-data-find
2647                        ,(or number '(gnus-summary-article-number))))))
2648
2649 (defmacro gnus-summary-article-mark (&optional number)
2650   "Return the mark of article NUMBER."
2651   `(gnus-data-mark (gnus-data-find
2652                     ,(or number '(gnus-summary-article-number)))))
2653
2654 (defmacro gnus-summary-article-pos (&optional number)
2655   "Return the position of the line of article NUMBER."
2656   `(gnus-data-pos (gnus-data-find
2657                    ,(or number '(gnus-summary-article-number)))))
2658
2659 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2660 (defmacro gnus-summary-article-subject (&optional number)
2661   "Return current subject string or nil if nothing."
2662   `(let ((headers
2663           ,(if number
2664                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2665              '(gnus-data-header (assq (gnus-summary-article-number)
2666                                       gnus-newsgroup-data)))))
2667      (and headers
2668           (vectorp headers)
2669           (mail-header-subject headers))))
2670
2671 (defmacro gnus-summary-article-score (&optional number)
2672   "Return current article score."
2673   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2674                   gnus-newsgroup-scored))
2675        gnus-summary-default-score 0))
2676
2677 (defun gnus-summary-article-children (&optional number)
2678   "Return a list of article numbers that are children of article NUMBER."
2679   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2680          (level (gnus-data-level (car data)))
2681          l children)
2682     (while (and (setq data (cdr data))
2683                 (> (setq l (gnus-data-level (car data))) level))
2684       (and (= (1+ level) l)
2685            (push (gnus-data-number (car data))
2686                  children)))
2687     (nreverse children)))
2688
2689 (defun gnus-summary-article-parent (&optional number)
2690   "Return the article number of the parent of article NUMBER."
2691   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2692                                     (gnus-data-list t)))
2693          (level (gnus-data-level (car data))))
2694     (if (zerop level)
2695         ()                              ; This is a root.
2696       ;; We search until we find an article with a level less than
2697       ;; this one.  That function has to be the parent.
2698       (while (and (setq data (cdr data))
2699                   (not (< (gnus-data-level (car data)) level))))
2700       (and data (gnus-data-number (car data))))))
2701
2702 (defun gnus-unread-mark-p (mark)
2703   "Say whether MARK is the unread mark."
2704   (= mark gnus-unread-mark))
2705
2706 (defun gnus-read-mark-p (mark)
2707   "Say whether MARK is one of the marks that mark as read.
2708 This is all marks except unread, ticked, dormant, and expirable."
2709   (not (or (= mark gnus-unread-mark)
2710            (= mark gnus-ticked-mark)
2711            (= mark gnus-dormant-mark)
2712            (= mark gnus-expirable-mark))))
2713
2714 (defmacro gnus-article-mark (number)
2715   "Return the MARK of article NUMBER.
2716 This macro should only be used when computing the mark the \"first\"
2717 time; i.e., when generating the summary lines.  After that,
2718 `gnus-summary-article-mark' should be used to examine the
2719 marks of articles."
2720   `(cond
2721     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2722     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2723     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2724     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2725     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2726     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2727     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2728     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2729            gnus-ancient-mark))))
2730
2731 ;; Saving hidden threads.
2732
2733 (defmacro gnus-save-hidden-threads (&rest forms)
2734   "Save hidden threads, eval FORMS, and restore the hidden threads."
2735   (let ((config (make-symbol "config")))
2736     `(let ((,config (gnus-hidden-threads-configuration)))
2737        (unwind-protect
2738            (save-excursion
2739              ,@forms)
2740          (gnus-restore-hidden-threads-configuration ,config)))))
2741 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2742 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2743
2744 (defun gnus-data-compute-positions ()
2745   "Compute the positions of all articles."
2746   (setq gnus-newsgroup-data-reverse nil)
2747   (let ((data gnus-newsgroup-data))
2748     (save-excursion
2749       (gnus-save-hidden-threads
2750         (gnus-summary-show-all-threads)
2751         (goto-char (point-min))
2752         (while data
2753           (while (get-text-property (point) 'gnus-intangible)
2754             (forward-line 1))
2755           (gnus-data-set-pos (car data) (+ (point) 3))
2756           (setq data (cdr data))
2757           (forward-line 1))))))
2758
2759 (defun gnus-hidden-threads-configuration ()
2760   "Return the current hidden threads configuration."
2761   (save-excursion
2762     (let (config)
2763       (goto-char (point-min))
2764       (while (search-forward "\r" nil t)
2765         (push (1- (point)) config))
2766       config)))
2767
2768 (defun gnus-restore-hidden-threads-configuration (config)
2769   "Restore hidden threads configuration from CONFIG."
2770   (save-excursion
2771     (let (point buffer-read-only)
2772       (while (setq point (pop config))
2773         (when (and (< point (point-max))
2774                    (goto-char point)
2775                    (eq (char-after) ?\n))
2776           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2777
2778 ;; Various summary mode internalish functions.
2779
2780 (defun gnus-mouse-pick-article (e)
2781   (interactive "e")
2782   (mouse-set-point e)
2783   (gnus-summary-next-page nil t))
2784
2785 (defun gnus-summary-set-display-table ()
2786   "Change the display table.
2787 Odd characters have a tendency to mess
2788 up nicely formatted displays - we make all possible glyphs
2789 display only a single character."
2790
2791   ;; We start from the standard display table, if any.
2792   (let ((table (or (copy-sequence standard-display-table)
2793                    (make-display-table)))
2794         (i 32))
2795     ;; Nix out all the control chars...
2796     (while (>= (setq i (1- i)) 0)
2797       (aset table i [??]))
2798     ;; ... but not newline and cr, of course.  (cr is necessary for the
2799     ;; selective display).
2800     (aset table ?\n nil)
2801     (aset table ?\r nil)
2802     ;; We keep TAB as well.
2803     (aset table ?\t nil)
2804     ;; We nix out any glyphs over 126 that are not set already.
2805     (let ((i 256))
2806       (while (>= (setq i (1- i)) 127)
2807         ;; Only modify if the entry is nil.
2808         (unless (aref table i)
2809           (aset table i [??]))))
2810     (setq buffer-display-table table)))
2811
2812 (defun gnus-summary-set-article-display-arrow (pos)
2813   "Update the overlay arrow to point to line at position POS."
2814   (when (and gnus-summary-display-arrow
2815              (boundp 'overlay-arrow-position)
2816              (boundp 'overlay-arrow-string))
2817     (save-excursion
2818       (goto-char pos)
2819       (beginning-of-line)
2820       (unless overlay-arrow-position
2821         (setq overlay-arrow-position (make-marker)))
2822       (setq overlay-arrow-string "=>"
2823             overlay-arrow-position (set-marker overlay-arrow-position
2824                                                (point)
2825                                                (current-buffer))))))
2826
2827 (defun gnus-summary-buffer-name (group)
2828   "Return the summary buffer name of GROUP."
2829   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2830
2831 (defun gnus-summary-setup-buffer (group)
2832   "Initialize summary buffer."
2833   (let ((buffer (gnus-summary-buffer-name group))
2834         (dead-name (concat "*Dead Summary "
2835                            (gnus-group-decoded-name group) "*")))
2836     ;; If a dead summary buffer exists, we kill it.
2837     (when (gnus-buffer-live-p dead-name)
2838       (gnus-kill-buffer dead-name))
2839     (if (get-buffer buffer)
2840         (progn
2841           (set-buffer buffer)
2842           (setq gnus-summary-buffer (current-buffer))
2843           (not gnus-newsgroup-prepared))
2844       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2845       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2846       (gnus-summary-mode group)
2847       (when gnus-carpal
2848         (gnus-carpal-setup-buffer 'summary))
2849       (unless gnus-single-article-buffer
2850         (make-local-variable 'gnus-article-buffer)
2851         (make-local-variable 'gnus-article-current)
2852         (make-local-variable 'gnus-original-article-buffer))
2853       (setq gnus-newsgroup-name group)
2854       ;; Set any local variables in the group parameters.
2855       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2856       t)))
2857
2858 (defun gnus-set-global-variables ()
2859   "Set the global equivalents of the buffer-local variables.
2860 They are set to the latest values they had.  These reflect the summary
2861 buffer that was in action when the last article was fetched."
2862   (when (eq major-mode 'gnus-summary-mode)
2863     (setq gnus-summary-buffer (current-buffer))
2864     (let ((name gnus-newsgroup-name)
2865           (marked gnus-newsgroup-marked)
2866           (unread gnus-newsgroup-unreads)
2867           (headers gnus-current-headers)
2868           (data gnus-newsgroup-data)
2869           (summary gnus-summary-buffer)
2870           (article-buffer gnus-article-buffer)
2871           (original gnus-original-article-buffer)
2872           (gac gnus-article-current)
2873           (reffed gnus-reffed-article-number)
2874           (score-file gnus-current-score-file)
2875           (default-charset gnus-newsgroup-charset)
2876           vlist)
2877       (let ((locals gnus-newsgroup-variables))
2878         (while locals
2879           (if (consp (car locals))
2880               (push (eval (caar locals)) vlist)
2881             (push (eval (car locals)) vlist))
2882           (setq locals (cdr locals)))
2883         (setq vlist (nreverse vlist)))
2884       (save-excursion
2885         (set-buffer gnus-group-buffer)
2886         (setq gnus-newsgroup-name name
2887               gnus-newsgroup-marked marked
2888               gnus-newsgroup-unreads unread
2889               gnus-current-headers headers
2890               gnus-newsgroup-data data
2891               gnus-article-current gac
2892               gnus-summary-buffer summary
2893               gnus-article-buffer article-buffer
2894               gnus-original-article-buffer original
2895               gnus-reffed-article-number reffed
2896               gnus-current-score-file score-file
2897               gnus-newsgroup-charset default-charset)
2898         (let ((locals gnus-newsgroup-variables))
2899           (while locals
2900             (if (consp (car locals))
2901                 (set (caar locals) (pop vlist))
2902               (set (car locals) (pop vlist)))
2903             (setq locals (cdr locals))))
2904         ;; The article buffer also has local variables.
2905         (when (gnus-buffer-live-p gnus-article-buffer)
2906           (set-buffer gnus-article-buffer)
2907           (setq gnus-summary-buffer summary))))))
2908
2909 (defun gnus-summary-article-unread-p (article)
2910   "Say whether ARTICLE is unread or not."
2911   (memq article gnus-newsgroup-unreads))
2912
2913 (defun gnus-summary-first-article-p (&optional article)
2914   "Return whether ARTICLE is the first article in the buffer."
2915   (if (not (setq article (or article (gnus-summary-article-number))))
2916       nil
2917     (eq article (caar gnus-newsgroup-data))))
2918
2919 (defun gnus-summary-last-article-p (&optional article)
2920   "Return whether ARTICLE is the last article in the buffer."
2921   (if (not (setq article (or article (gnus-summary-article-number))))
2922       ;; All non-existent numbers are the last article.  :-)
2923       t
2924     (not (cdr (gnus-data-find-list article)))))
2925
2926 (defun gnus-make-thread-indent-array ()
2927   (let ((n 200))
2928     (unless (and gnus-thread-indent-array
2929                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2930       (setq gnus-thread-indent-array (make-vector 201 "")
2931             gnus-thread-indent-array-level gnus-thread-indent-level)
2932       (while (>= n 0)
2933         (aset gnus-thread-indent-array n
2934               (make-string (* n gnus-thread-indent-level) ? ))
2935         (setq n (1- n))))))
2936
2937 (defun gnus-update-summary-mark-positions ()
2938   "Compute where the summary marks are to go."
2939   (save-excursion
2940     (when (gnus-buffer-exists-p gnus-summary-buffer)
2941       (set-buffer gnus-summary-buffer))
2942     (let ((gnus-replied-mark 129)
2943           (gnus-score-below-mark 130)
2944           (gnus-score-over-mark 130)
2945           (gnus-download-mark 131)
2946           (spec gnus-summary-line-format-spec)
2947           gnus-visual pos)
2948       (save-excursion
2949         (gnus-set-work-buffer)
2950         (let ((gnus-summary-line-format-spec spec)
2951               (gnus-newsgroup-downloadable '((0 . t))))
2952           (gnus-summary-insert-line
2953            (make-full-mail-header 0 "" "nobody"
2954                                   "05 Apr 2001 23:33:09 +0400"
2955                                   "" "" 0 0 "" nil)
2956            0 nil 128 t nil "" nil 1)
2957           (goto-char (point-min))
2958           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2959                                              (- (point) 2)))))
2960           (goto-char (point-min))
2961           (push (cons 'replied (and (search-forward "\201" nil t)
2962                                     (- (point) 2)))
2963                 pos)
2964           (goto-char (point-min))
2965           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2966                 pos)
2967           (goto-char (point-min))
2968           (push (cons 'download
2969                       (and (search-forward "\203" nil t) (- (point) 2)))
2970                 pos)))
2971       (setq gnus-summary-mark-positions pos))))
2972
2973 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2974   "Insert a dummy root in the summary buffer."
2975   (beginning-of-line)
2976   (gnus-add-text-properties
2977    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2978    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2979
2980 (defun gnus-summary-extract-address-component (from)
2981   (or (car (funcall gnus-extract-address-components from))
2982       from))
2983
2984 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
2985   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
2986                                 default-mime-charset)))
2987     ;; Is it really necessary to do this next part for each summary line?
2988     ;; Luckily, doesn't seem to slow things down much.
2989     (or
2990      (and gnus-ignored-from-addresses
2991           (string-match gnus-ignored-from-addresses gnus-tmp-from)
2992           (let ((extra-headers (mail-header-extra header))
2993                 to
2994                 newsgroups)
2995             (cond
2996              ((setq to (cdr (assq 'To extra-headers)))
2997               (concat "-> "
2998                       (inline
2999                         (gnus-summary-extract-address-component
3000                          (funcall gnus-decode-encoded-word-function to)))))
3001              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3002               (concat "=> " newsgroups)))))
3003      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3004
3005 (defun gnus-summary-insert-line (gnus-tmp-header
3006                                  gnus-tmp-level gnus-tmp-current
3007                                  gnus-tmp-unread gnus-tmp-replied
3008                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3009                                  &optional gnus-tmp-dummy gnus-tmp-score
3010                                  gnus-tmp-process)
3011   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3012          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3013          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3014          (gnus-tmp-score-char
3015           (if (or (null gnus-summary-default-score)
3016                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3017                       gnus-summary-zcore-fuzz))
3018               ?\ ;;;Whitespace
3019             (if (< gnus-tmp-score gnus-summary-default-score)
3020                 gnus-score-below-mark gnus-score-over-mark)))
3021          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3022          (gnus-tmp-replied
3023           (cond (gnus-tmp-process gnus-process-mark)
3024                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3025                  gnus-cached-mark)
3026                 (gnus-tmp-replied gnus-replied-mark)
3027                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3028                  gnus-forwarded-mark)
3029                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3030                  gnus-saved-mark)
3031                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3032                  gnus-recent-mark)
3033                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3034                  gnus-unseen-mark)
3035                 (t gnus-no-mark)))
3036          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3037          (gnus-tmp-name
3038           (cond
3039            ((string-match "<[^>]+> *$" gnus-tmp-from)
3040             (let ((beg (match-beginning 0)))
3041               (or (and (string-match "^\".+\"" gnus-tmp-from)
3042                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3043                   (substring gnus-tmp-from 0 beg))))
3044            ((string-match "(.+)" gnus-tmp-from)
3045             (substring gnus-tmp-from
3046                        (1+ (match-beginning 0)) (1- (match-end 0))))
3047            (t gnus-tmp-from)))
3048          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3049          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3050          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3051          (buffer-read-only nil))
3052     (when (string= gnus-tmp-name "")
3053       (setq gnus-tmp-name gnus-tmp-from))
3054     (unless (numberp gnus-tmp-lines)
3055       (setq gnus-tmp-lines -1))
3056     (if (= gnus-tmp-lines -1)
3057         (setq gnus-tmp-lines "?")
3058       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3059     (gnus-put-text-property-excluding-characters-with-faces
3060      (point)
3061      (progn (eval gnus-summary-line-format-spec) (point))
3062      'gnus-number gnus-tmp-number)
3063     (when (gnus-visual-p 'summary-highlight 'highlight)
3064       (forward-line -1)
3065       (gnus-run-hooks 'gnus-summary-update-hook)
3066       (forward-line 1))))
3067
3068 (defun gnus-summary-update-line (&optional dont-update)
3069   "Update summary line after change."
3070   (when (and gnus-summary-default-score
3071              (not gnus-summary-inhibit-highlight))
3072     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3073            (article (gnus-summary-article-number))
3074            (score (gnus-summary-article-score article)))
3075       (unless dont-update
3076         (if (and gnus-summary-mark-below
3077                  (< (gnus-summary-article-score)
3078                     gnus-summary-mark-below))
3079             ;; This article has a low score, so we mark it as read.
3080             (when (memq article gnus-newsgroup-unreads)
3081               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3082           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3083             ;; This article was previously marked as read on account
3084             ;; of a low score, but now it has risen, so we mark it as
3085             ;; unread.
3086             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3087         (gnus-summary-update-mark
3088          (if (or (null gnus-summary-default-score)
3089                  (<= (abs (- score gnus-summary-default-score))
3090                      gnus-summary-zcore-fuzz))
3091              ?\ ;;;Whitespace
3092            (if (< score gnus-summary-default-score)
3093                gnus-score-below-mark gnus-score-over-mark))
3094          'score))
3095       ;; Do visual highlighting.
3096       (when (gnus-visual-p 'summary-highlight 'highlight)
3097         (gnus-run-hooks 'gnus-summary-update-hook)))))
3098
3099 (defvar gnus-tmp-new-adopts nil)
3100
3101 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3102   "Return the number of articles in THREAD.
3103 This may be 0 in some cases -- if none of the articles in
3104 the thread are to be displayed."
3105   (let* ((number
3106           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3107           (cond
3108            ((not (listp thread))
3109             1)
3110            ((and (consp thread) (cdr thread))
3111             (apply
3112              '+ 1 (mapcar
3113                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3114            ((null thread)
3115             1)
3116            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3117             1)
3118            (t 0))))
3119     (when (and level (zerop level) gnus-tmp-new-adopts)
3120       (incf number
3121             (apply '+ (mapcar
3122                        'gnus-summary-number-of-articles-in-thread
3123                        gnus-tmp-new-adopts))))
3124     (if char
3125         (if (> number 1) gnus-not-empty-thread-mark
3126           gnus-empty-thread-mark)
3127       number)))
3128
3129 (defun gnus-summary-set-local-parameters (group)
3130   "Go through the local params of GROUP and set all variable specs in that list."
3131   (let ((params (gnus-group-find-parameter group))
3132         (vars '(quit-config))           ; Ignore quit-config.
3133         elem)
3134     (while params
3135       (setq elem (car params)
3136             params (cdr params))
3137       (and (consp elem)                 ; Has to be a cons.
3138            (consp (cdr elem))           ; The cdr has to be a list.
3139            (symbolp (car elem))         ; Has to be a symbol in there.
3140            (not (memq (car elem) vars))
3141            (ignore-errors               ; So we set it.
3142              (push (car elem) vars)
3143              (make-local-variable (car elem))
3144              (set (car elem) (eval (nth 1 elem))))))))
3145
3146 (defun gnus-summary-read-group (group &optional show-all no-article
3147                                       kill-buffer no-display backward
3148                                       select-articles)
3149   "Start reading news in newsgroup GROUP.
3150 If SHOW-ALL is non-nil, already read articles are also listed.
3151 If NO-ARTICLE is non-nil, no article is selected initially.
3152 If NO-DISPLAY, don't generate a summary buffer."
3153   (let (result)
3154     (while (and group
3155                 (null (setq result
3156                             (let ((gnus-auto-select-next nil))
3157                               (or (gnus-summary-read-group-1
3158                                    group show-all no-article
3159                                    kill-buffer no-display
3160                                    select-articles)
3161                                   (setq show-all nil
3162                                         select-articles nil)))))
3163                 (eq gnus-auto-select-next 'quietly))
3164       (set-buffer gnus-group-buffer)
3165       ;; The entry function called above goes to the next
3166       ;; group automatically, so we go two groups back
3167       ;; if we are searching for the previous group.
3168       (when backward
3169         (gnus-group-prev-unread-group 2))
3170       (if (not (equal group (gnus-group-group-name)))
3171           (setq group (gnus-group-group-name))
3172         (setq group nil)))
3173     result))
3174
3175 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3176   "Directly jump to the other GROUP from summary buffer.
3177 If SHOW-ALL is non-nil, already read articles are also listed."
3178   (interactive
3179    (if (eq gnus-summary-buffer (current-buffer))
3180        (list (completing-read
3181               "Group: " gnus-active-hashtb nil t
3182               (when (and gnus-newsgroup-name
3183                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3184                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3185               'gnus-group-history)
3186              current-prefix-arg)
3187      (error "%s must be invoked from a gnus summary buffer." this-command)))
3188   (unless (or (zerop (length group))
3189               (and gnus-newsgroup-name
3190                    (string-equal gnus-newsgroup-name group)))
3191     (gnus-summary-exit)
3192     (gnus-summary-read-group group show-all
3193                              gnus-dont-select-after-jump-to-other-group)))
3194
3195 (defun gnus-summary-read-group-1 (group show-all no-article
3196                                         kill-buffer no-display
3197                                         &optional select-articles)
3198   ;; Killed foreign groups can't be entered.
3199   ;;  (when (and (not (gnus-group-native-p group))
3200   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3201   ;;    (error "Dead non-native groups can't be entered"))
3202   (gnus-message 5 "Retrieving newsgroup: %s..."
3203                 (gnus-group-decoded-name group))
3204   (let* ((new-group (gnus-summary-setup-buffer group))
3205          (quit-config (gnus-group-quit-config group))
3206          (did-select (and new-group (gnus-select-newsgroup
3207                                      group show-all select-articles))))
3208     (cond
3209      ;; This summary buffer exists already, so we just select it.
3210      ((not new-group)
3211       (gnus-set-global-variables)
3212       (when kill-buffer
3213         (gnus-kill-or-deaden-summary kill-buffer))
3214       (gnus-configure-windows 'summary 'force)
3215       (gnus-set-mode-line 'summary)
3216       (gnus-summary-position-point)
3217       (message "")
3218       t)
3219      ;; We couldn't select this group.
3220      ((null did-select)
3221       (when (and (eq major-mode 'gnus-summary-mode)
3222                  (not (equal (current-buffer) kill-buffer)))
3223         (kill-buffer (current-buffer))
3224         (if (not quit-config)
3225             (progn
3226               ;; Update the info -- marks might need to be removed,
3227               ;; for instance.
3228               (gnus-summary-update-info)
3229               (set-buffer gnus-group-buffer)
3230               (gnus-group-jump-to-group group)
3231               (gnus-group-next-unread-group 1))
3232           (gnus-handle-ephemeral-exit quit-config)))
3233       (let ((grpinfo (gnus-get-info group)))
3234         (if (null (gnus-info-read grpinfo))
3235             (gnus-message 3 "Group %s contains no messages"
3236                           (gnus-group-decoded-name group))
3237           (gnus-message 3 "Can't select group")))
3238       nil)
3239      ;; The user did a `C-g' while prompting for number of articles,
3240      ;; so we exit this group.
3241      ((eq did-select 'quit)
3242       (and (eq major-mode 'gnus-summary-mode)
3243            (not (equal (current-buffer) kill-buffer))
3244            (kill-buffer (current-buffer)))
3245       (when kill-buffer
3246         (gnus-kill-or-deaden-summary kill-buffer))
3247       (if (not quit-config)
3248           (progn
3249             (set-buffer gnus-group-buffer)
3250             (gnus-group-jump-to-group group)
3251             (gnus-group-next-unread-group 1)
3252             (gnus-configure-windows 'group 'force))
3253         (gnus-handle-ephemeral-exit quit-config))
3254       ;; Finally signal the quit.
3255       (signal 'quit nil))
3256      ;; The group was successfully selected.
3257      (t
3258       (gnus-set-global-variables)
3259       ;; Save the active value in effect when the group was entered.
3260       (setq gnus-newsgroup-active
3261             (gnus-copy-sequence
3262              (gnus-active gnus-newsgroup-name)))
3263       ;; You can change the summary buffer in some way with this hook.
3264       (gnus-run-hooks 'gnus-select-group-hook)
3265       (gnus-update-format-specifications
3266        nil 'summary 'summary-mode 'summary-dummy)
3267       (gnus-update-summary-mark-positions)
3268       ;; Do score processing.
3269       (when gnus-use-scoring
3270         (gnus-possibly-score-headers))
3271       ;; Check whether to fill in the gaps in the threads.
3272       (when gnus-build-sparse-threads
3273         (gnus-build-sparse-threads))
3274       ;; Find the initial limit.
3275       (if gnus-show-threads
3276           (if show-all
3277               (let ((gnus-newsgroup-dormant nil))
3278                 (gnus-summary-initial-limit show-all))
3279             (gnus-summary-initial-limit show-all))
3280         ;; When untreaded, all articles are always shown.
3281         (setq gnus-newsgroup-limit
3282               (mapcar
3283                (lambda (header) (mail-header-number header))
3284                gnus-newsgroup-headers)))
3285       ;; Generate the summary buffer.
3286       (unless no-display
3287         (gnus-summary-prepare))
3288       (when gnus-use-trees
3289         (gnus-tree-open group)
3290         (setq gnus-summary-highlight-line-function
3291               'gnus-tree-highlight-article))
3292       ;; If the summary buffer is empty, but there are some low-scored
3293       ;; articles or some excluded dormants, we include these in the
3294       ;; buffer.
3295       (when (and (zerop (buffer-size))
3296                  (not no-display))
3297         (cond (gnus-newsgroup-dormant
3298                (gnus-summary-limit-include-dormant))
3299               ((and gnus-newsgroup-scored show-all)
3300                (gnus-summary-limit-include-expunged t))))
3301       ;; Function `gnus-apply-kill-file' must be called in this hook.
3302       (gnus-run-hooks 'gnus-apply-kill-hook)
3303       (if (and (zerop (buffer-size))
3304                (not no-display))
3305           (progn
3306             ;; This newsgroup is empty.
3307             (gnus-summary-catchup-and-exit nil t)
3308             (gnus-message 6 "No unread news")
3309             (when kill-buffer
3310               (gnus-kill-or-deaden-summary kill-buffer))
3311             ;; Return nil from this function.
3312             nil)
3313         ;; Hide conversation thread subtrees.  We cannot do this in
3314         ;; gnus-summary-prepare-hook since kill processing may not
3315         ;; work with hidden articles.
3316         (gnus-summary-maybe-hide-threads)
3317         (when kill-buffer
3318           (gnus-kill-or-deaden-summary kill-buffer))
3319         (gnus-summary-auto-select-subject)
3320         ;; Show first unread article if requested.
3321         (if (and (not no-article)
3322                  (not no-display)
3323                  gnus-newsgroup-unreads
3324                  gnus-auto-select-first)
3325             (progn
3326               (gnus-configure-windows 'summary)
3327               (let ((art (gnus-summary-article-number)))
3328                 (unless (or (memq art gnus-newsgroup-undownloaded)
3329                             (memq art gnus-newsgroup-downloadable))
3330                   (gnus-summary-goto-article art))))
3331           ;; Don't select any articles.
3332           (gnus-summary-position-point)
3333           (gnus-configure-windows 'summary 'force)
3334           (gnus-set-mode-line 'summary))
3335         (when (get-buffer-window gnus-group-buffer t)
3336           ;; Gotta use windows, because recenter does weird stuff if
3337           ;; the current buffer ain't the displayed window.
3338           (let ((owin (selected-window)))
3339             (select-window (get-buffer-window gnus-group-buffer t))
3340             (when (gnus-group-goto-group group)
3341               (recenter))
3342             (select-window owin)))
3343         ;; Mark this buffer as "prepared".
3344         (setq gnus-newsgroup-prepared t)
3345         (gnus-run-hooks 'gnus-summary-prepared-hook)
3346         t)))))
3347
3348 (defun gnus-summary-auto-select-subject ()
3349   "Select the subject line on initial group entry."
3350   (goto-char (point-min))
3351   (cond
3352    ((eq gnus-auto-select-subject 'best)
3353     (gnus-summary-best-unread-subject))
3354    ((eq gnus-auto-select-subject 'unread)
3355     (gnus-summary-first-unread-subject))
3356    ((eq gnus-auto-select-subject 'unseen)
3357     (gnus-summary-first-unseen-subject))
3358    ((eq gnus-auto-select-subject 'unseen-or-unread)
3359     (gnus-summary-first-unseen-or-unread-subject))
3360    ((eq gnus-auto-select-subject 'first)
3361     ;; Do nothing.
3362     )
3363    ((gnus-functionp gnus-auto-select-subject)
3364     (funcall gnus-auto-select-subject))))
3365
3366 (defun gnus-summary-prepare ()
3367   "Generate the summary buffer."
3368   (interactive)
3369   (let ((buffer-read-only nil))
3370     (erase-buffer)
3371     (setq gnus-newsgroup-data nil
3372           gnus-newsgroup-data-reverse nil)
3373     (gnus-run-hooks 'gnus-summary-generate-hook)
3374     ;; Generate the buffer, either with threads or without.
3375     (when gnus-newsgroup-headers
3376       (gnus-summary-prepare-threads
3377        (if gnus-show-threads
3378            (gnus-sort-gathered-threads
3379             (funcall gnus-summary-thread-gathering-function
3380                      (gnus-sort-threads
3381                       (gnus-cut-threads (gnus-make-threads)))))
3382          ;; Unthreaded display.
3383          (gnus-sort-articles gnus-newsgroup-headers))))
3384     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3385     ;; Call hooks for modifying summary buffer.
3386     (goto-char (point-min))
3387     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3388
3389 (defsubst gnus-general-simplify-subject (subject)
3390   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3391   (setq subject
3392         (cond
3393          ;; Truncate the subject.
3394          (gnus-simplify-subject-functions
3395           (gnus-map-function gnus-simplify-subject-functions subject))
3396          ((numberp gnus-summary-gather-subject-limit)
3397           (setq subject (gnus-simplify-subject-re subject))
3398           (if (> (length subject) gnus-summary-gather-subject-limit)
3399               (substring subject 0 gnus-summary-gather-subject-limit)
3400             subject))
3401          ;; Fuzzily simplify it.
3402          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3403           (gnus-simplify-subject-fuzzy subject))
3404          ;; Just remove the leading "Re:".
3405          (t
3406           (gnus-simplify-subject-re subject))))
3407
3408   (if (and gnus-summary-gather-exclude-subject
3409            (string-match gnus-summary-gather-exclude-subject subject))
3410       nil                               ; This article shouldn't be gathered
3411     subject))
3412
3413 (defun gnus-summary-simplify-subject-query ()
3414   "Query where the respool algorithm would put this article."
3415   (interactive)
3416   (gnus-summary-select-article)
3417   (message "%s"
3418            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3419
3420 (defun gnus-gather-threads-by-subject (threads)
3421   "Gather threads by looking at Subject headers."
3422   (if (not gnus-summary-make-false-root)
3423       threads
3424     (let ((hashtb (gnus-make-hashtable 1024))
3425           (prev threads)
3426           (result threads)
3427           subject hthread whole-subject)
3428       (while threads
3429         (setq subject (gnus-general-simplify-subject
3430                        (setq whole-subject (mail-header-subject
3431                                             (caar threads)))))
3432         (when subject
3433           (if (setq hthread (gnus-gethash subject hashtb))
3434               (progn
3435                 ;; We enter a dummy root into the thread, if we
3436                 ;; haven't done that already.
3437                 (unless (stringp (caar hthread))
3438                   (setcar hthread (list whole-subject (car hthread))))
3439                 ;; We add this new gathered thread to this gathered
3440                 ;; thread.
3441                 (setcdr (car hthread)
3442                         (nconc (cdar hthread) (list (car threads))))
3443                 ;; Remove it from the list of threads.
3444                 (setcdr prev (cdr threads))
3445                 (setq threads prev))
3446             ;; Enter this thread into the hash table.
3447             (gnus-sethash subject threads hashtb)))
3448         (setq prev threads)
3449         (setq threads (cdr threads)))
3450       result)))
3451
3452 (defun gnus-gather-threads-by-references (threads)
3453   "Gather threads by looking at References headers."
3454   (let ((idhashtb (gnus-make-hashtable 1024))
3455         (thhashtb (gnus-make-hashtable 1024))
3456         (prev threads)
3457         (result threads)
3458         ids references id gthread gid entered ref)
3459     (while threads
3460       (when (setq references (mail-header-references (caar threads)))
3461         (setq id (mail-header-id (caar threads))
3462               ids (inline (gnus-split-references references))
3463               entered nil)
3464         (while (setq ref (pop ids))
3465           (setq ids (delete ref ids))
3466           (if (not (setq gid (gnus-gethash ref idhashtb)))
3467               (progn
3468                 (gnus-sethash ref id idhashtb)
3469                 (gnus-sethash id threads thhashtb))
3470             (setq gthread (gnus-gethash gid thhashtb))
3471             (unless entered
3472               ;; We enter a dummy root into the thread, if we
3473               ;; haven't done that already.
3474               (unless (stringp (caar gthread))
3475                 (setcar gthread (list (mail-header-subject (caar gthread))
3476                                       (car gthread))))
3477               ;; We add this new gathered thread to this gathered
3478               ;; thread.
3479               (setcdr (car gthread)
3480                       (nconc (cdar gthread) (list (car threads)))))
3481             ;; Add it into the thread hash table.
3482             (gnus-sethash id gthread thhashtb)
3483             (setq entered t)
3484             ;; Remove it from the list of threads.
3485             (setcdr prev (cdr threads))
3486             (setq threads prev))))
3487       (setq prev threads)
3488       (setq threads (cdr threads)))
3489     result))
3490
3491 (defun gnus-sort-gathered-threads (threads)
3492   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3493   (let ((result threads))
3494     (while threads
3495       (when (stringp (caar threads))
3496         (setcdr (car threads)
3497                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3498       (setq threads (cdr threads)))
3499     result))
3500
3501 (defun gnus-thread-loop-p (root thread)
3502   "Say whether ROOT is in THREAD."
3503   (let ((stack (list thread))
3504         (infloop 0)
3505         th)
3506     (while (setq thread (pop stack))
3507       (setq th (cdr thread))
3508       (while (and th
3509                   (not (eq (caar th) root)))
3510         (pop th))
3511       (if th
3512           ;; We have found a loop.
3513           (let (ref-dep)
3514             (setcdr thread (delq (car th) (cdr thread)))
3515             (if (boundp (setq ref-dep (intern "none"
3516                                               gnus-newsgroup-dependencies)))
3517                 (setcdr (symbol-value ref-dep)
3518                         (nconc (cdr (symbol-value ref-dep))
3519                                (list (car th))))
3520               (set ref-dep (list nil (car th))))
3521             (setq infloop 1
3522                   stack nil))
3523         ;; Push all the subthreads onto the stack.
3524         (push (cdr thread) stack)))
3525     infloop))
3526
3527 (defun gnus-make-threads ()
3528   "Go through the dependency hashtb and find the roots.  Return all threads."
3529   (let (threads)
3530     (while (catch 'infloop
3531              (mapatoms
3532               (lambda (refs)
3533                 ;; Deal with self-referencing References loops.
3534                 (when (and (car (symbol-value refs))
3535                            (not (zerop
3536                                  (apply
3537                                   '+
3538                                   (mapcar
3539                                    (lambda (thread)
3540                                      (gnus-thread-loop-p
3541                                       (car (symbol-value refs)) thread))
3542                                    (cdr (symbol-value refs)))))))
3543                   (setq threads nil)
3544                   (throw 'infloop t))
3545                 (unless (car (symbol-value refs))
3546                   ;; These threads do not refer back to any other articles,
3547                   ;; so they're roots.
3548                   (setq threads (append (cdr (symbol-value refs)) threads))))
3549               gnus-newsgroup-dependencies)))
3550     threads))
3551
3552 ;; Build the thread tree.
3553 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3554   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3555
3556 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3557 if it was already present.
3558
3559 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3560 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3561 Message-IDs will be renamed be renamed to a unique Message-ID before
3562 being entered.
3563
3564 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3565   (let* ((id (mail-header-id header))
3566          (id-dep (and id (intern id dependencies)))
3567          ref ref-dep ref-header)
3568     ;; Enter this `header' in the `dependencies' table.
3569     (cond
3570      ((not id-dep)
3571       (setq header nil))
3572      ;; The first two cases do the normal part: enter a new `header'
3573      ;; in the `dependencies' table.
3574      ((not (boundp id-dep))
3575       (set id-dep (list header)))
3576      ((null (car (symbol-value id-dep)))
3577       (setcar (symbol-value id-dep) header))
3578
3579      ;; From here the `header' was already present in the
3580      ;; `dependencies' table.
3581      (force-new
3582       ;; Overrides an existing entry;
3583       ;; just set the header part of the entry.
3584       (setcar (symbol-value id-dep) header))
3585
3586      ;; Renames the existing `header' to a unique Message-ID.
3587      ((not gnus-summary-ignore-duplicates)
3588       ;; An article with this Message-ID has already been seen.
3589       ;; We rename the Message-ID.
3590       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3591            (list header))
3592       (mail-header-set-id header id))
3593
3594      ;; The last case ignores an existing entry, except it adds any
3595      ;; additional Xrefs (in case the two articles came from different
3596      ;; servers.
3597      ;; Also sets `header' to `nil' meaning that the `dependencies'
3598      ;; table was *not* modified.
3599      (t
3600       (mail-header-set-xref
3601        (car (symbol-value id-dep))
3602        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3603                    "")
3604                (or (mail-header-xref header) "")))
3605       (setq header nil)))
3606
3607     (when header
3608       ;; First check that we are not creating a References loop.
3609       (setq ref (gnus-parent-id (mail-header-references header)))
3610       (while (and ref
3611                   (setq ref-dep (intern-soft ref dependencies))
3612                   (boundp ref-dep)
3613                   (setq ref-header (car (symbol-value ref-dep))))
3614         (if (string= id ref)
3615             ;; Yuk!  This is a reference loop.  Make the article be a
3616             ;; root article.
3617             (progn
3618               (mail-header-set-references (car (symbol-value id-dep)) "none")
3619               (setq ref nil))
3620           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3621       (setq ref (gnus-parent-id (mail-header-references header)))
3622       (setq ref-dep (intern (or ref "none") dependencies))
3623       (if (boundp ref-dep)
3624           (setcdr (symbol-value ref-dep)
3625                   (nconc (cdr (symbol-value ref-dep))
3626                          (list (symbol-value id-dep))))
3627         (set ref-dep (list nil (symbol-value id-dep)))))
3628     header))
3629
3630 (defun gnus-extract-message-id-from-in-reply-to (string)
3631   (if (string-match "<[^>]+>" string)
3632       (substring string (match-beginning 0) (match-end 0))
3633     nil))
3634
3635 (defun gnus-build-sparse-threads ()
3636   (let ((headers gnus-newsgroup-headers)
3637         (mail-parse-charset gnus-newsgroup-charset)
3638         (gnus-summary-ignore-duplicates t)
3639         header references generation relations
3640         subject child end new-child date)
3641     ;; First we create an alist of generations/relations, where
3642     ;; generations is how much we trust the relation, and the relation
3643     ;; is parent/child.
3644     (gnus-message 7 "Making sparse threads...")
3645     (save-excursion
3646       (nnheader-set-temp-buffer " *gnus sparse threads*")
3647       (while (setq header (pop headers))
3648         (when (and (setq references (mail-header-references header))
3649                    (not (string= references "")))
3650           (insert references)
3651           (setq child (mail-header-id header)
3652                 subject (mail-header-subject header)
3653                 date (mail-header-date header)
3654                 generation 0)
3655           (while (search-backward ">" nil t)
3656             (setq end (1+ (point)))
3657             (when (search-backward "<" nil t)
3658               (setq new-child (buffer-substring (point) end))
3659               (push (list (incf generation)
3660                           child (setq child new-child)
3661                           subject date)
3662                     relations)))
3663           (when child
3664             (push (list (1+ generation) child nil subject) relations))
3665           (erase-buffer)))
3666       (kill-buffer (current-buffer)))
3667     ;; Sort over trustworthiness.
3668     (mapcar
3669      (lambda (relation)
3670        (when (gnus-dependencies-add-header
3671               (make-full-mail-header-from-decoded-header
3672                gnus-reffed-article-number
3673                (nth 3 relation) "" (or (nth 4 relation) "")
3674                (nth 1 relation)
3675                (or (nth 2 relation) "") 0 0 "")
3676               gnus-newsgroup-dependencies nil)
3677          (push gnus-reffed-article-number gnus-newsgroup-limit)
3678          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3679          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3680                gnus-newsgroup-reads)
3681          (decf gnus-reffed-article-number)))
3682      (sort relations 'car-less-than-car))
3683     (gnus-message 7 "Making sparse threads...done")))
3684
3685 (defun gnus-build-old-threads ()
3686   ;; Look at all the articles that refer back to old articles, and
3687   ;; fetch the headers for the articles that aren't there.  This will
3688   ;; build complete threads - if the roots haven't been expired by the
3689   ;; server, that is.
3690   (let ((mail-parse-charset gnus-newsgroup-charset)
3691         id heads)
3692     (mapatoms
3693      (lambda (refs)
3694        (when (not (car (symbol-value refs)))
3695          (setq heads (cdr (symbol-value refs)))
3696          (while heads
3697            (if (memq (mail-header-number (caar heads))
3698                      gnus-newsgroup-dormant)
3699                (setq heads (cdr heads))
3700              (setq id (symbol-name refs))
3701              (while (and (setq id (gnus-build-get-header id))
3702                          (not (car (gnus-id-to-thread id)))))
3703              (setq heads nil)))))
3704      gnus-newsgroup-dependencies)))
3705
3706 ;; This function has to be called with point after the article number
3707 ;; on the beginning of the line.
3708 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3709   (let ((eol (gnus-point-at-eol))
3710         (buffer (current-buffer))
3711         header references in-reply-to)
3712
3713     ;; overview: [num subject from date id refs chars lines misc]
3714     (unwind-protect
3715         (progn
3716           (narrow-to-region (point) eol)
3717           (unless (eobp)
3718             (forward-char))
3719
3720           (setq header
3721                 (make-full-mail-header
3722                  number                         ; number
3723                  (nnheader-nov-field)           ; subject
3724                  (nnheader-nov-field)           ; from
3725                  (nnheader-nov-field)           ; date
3726                  (nnheader-nov-read-message-id) ; id
3727                  (nnheader-nov-field)           ; refs
3728                  (nnheader-nov-read-integer)    ; chars
3729                  (nnheader-nov-read-integer)    ; lines
3730                  (unless (eobp)
3731                    (if (looking-at "Xref: ")
3732                        (goto-char (match-end 0)))
3733                    (nnheader-nov-field))        ; Xref
3734                  (nnheader-nov-parse-extra))))  ; extra
3735
3736       (widen))
3737
3738     (when (and (string= references "")
3739                (setq in-reply-to (mail-header-extra header))
3740                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3741       (mail-header-set-references
3742        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3743
3744     (when gnus-alter-header-function
3745       (funcall gnus-alter-header-function header))
3746     (gnus-dependencies-add-header header dependencies force-new)))
3747
3748 (defun gnus-build-get-header (id)
3749   "Look through the buffer of NOV lines and find the header to ID.
3750 Enter this line into the dependencies hash table, and return
3751 the id of the parent article (if any)."
3752   (let ((deps gnus-newsgroup-dependencies)
3753         found header)
3754     (prog1
3755         (save-excursion
3756           (set-buffer nntp-server-buffer)
3757           (let ((case-fold-search nil))
3758             (goto-char (point-min))
3759             (while (and (not found)
3760                         (search-forward id nil t))
3761               (beginning-of-line)
3762               (setq found (looking-at
3763                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3764                                    (regexp-quote id))))
3765               (or found (beginning-of-line 2)))
3766             (when found
3767               (beginning-of-line)
3768               (and
3769                (setq header (gnus-nov-parse-line
3770                              (read (current-buffer)) deps))
3771                (gnus-parent-id (mail-header-references header))))))
3772       (when header
3773         (let ((number (mail-header-number header)))
3774           (push number gnus-newsgroup-limit)
3775           (push header gnus-newsgroup-headers)
3776           (if (memq number gnus-newsgroup-unselected)
3777               (progn
3778                 (push number gnus-newsgroup-unreads)
3779                 (setq gnus-newsgroup-unselected
3780                       (delq number gnus-newsgroup-unselected)))
3781             (push number gnus-newsgroup-ancient)))))))
3782
3783 (defun gnus-build-all-threads ()
3784   "Read all the headers."
3785   (let ((gnus-summary-ignore-duplicates t)
3786         (mail-parse-charset gnus-newsgroup-charset)
3787         (dependencies gnus-newsgroup-dependencies)
3788         header article)
3789     (save-excursion
3790       (set-buffer nntp-server-buffer)
3791       (let ((case-fold-search nil))
3792         (goto-char (point-min))
3793         (while (not (eobp))
3794           (ignore-errors
3795             (setq article (read (current-buffer))
3796                   header (gnus-nov-parse-line article dependencies)))
3797           (when header
3798             (save-excursion
3799               (set-buffer gnus-summary-buffer)
3800               (push header gnus-newsgroup-headers)
3801               (if (memq (setq article (mail-header-number header))
3802                         gnus-newsgroup-unselected)
3803                   (progn
3804                     (push article gnus-newsgroup-unreads)
3805                     (setq gnus-newsgroup-unselected
3806                           (delq article gnus-newsgroup-unselected)))
3807                 (push article gnus-newsgroup-ancient)))
3808             (forward-line 1)))))))
3809
3810 (defun gnus-summary-update-article-line (article header)
3811   "Update the line for ARTICLE using HEADERS."
3812   (let* ((id (mail-header-id header))
3813          (thread (gnus-id-to-thread id)))
3814     (unless thread
3815       (error "Article in no thread"))
3816     ;; Update the thread.
3817     (setcar thread header)
3818     (gnus-summary-goto-subject article)
3819     (let* ((datal (gnus-data-find-list article))
3820            (data (car datal))
3821            (length (when (cdr datal)
3822                      (- (gnus-data-pos data)
3823                         (gnus-data-pos (cadr datal)))))
3824            (buffer-read-only nil)
3825            (level (gnus-summary-thread-level)))
3826       (gnus-delete-line)
3827       (gnus-summary-insert-line
3828        header level nil (gnus-article-mark article)
3829        (memq article gnus-newsgroup-replied)
3830        (memq article gnus-newsgroup-expirable)
3831        ;; Only insert the Subject string when it's different
3832        ;; from the previous Subject string.
3833        (if (and
3834             gnus-show-threads
3835             (gnus-subject-equal
3836              (condition-case ()
3837                  (mail-header-subject
3838                   (gnus-data-header
3839                    (cadr
3840                     (gnus-data-find-list
3841                      article
3842                      (gnus-data-list t)))))
3843                ;; Error on the side of excessive subjects.
3844                (error ""))
3845              (mail-header-subject header)))
3846            ""
3847          (mail-header-subject header))
3848        nil (cdr (assq article gnus-newsgroup-scored))
3849        (memq article gnus-newsgroup-processable))
3850       (when length
3851         (gnus-data-update-list
3852          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3853
3854 (defun gnus-summary-update-article (article &optional iheader)
3855   "Update ARTICLE in the summary buffer."
3856   (set-buffer gnus-summary-buffer)
3857   (let* ((header (gnus-summary-article-header article))
3858          (id (mail-header-id header))
3859          (data (gnus-data-find article))
3860          (thread (gnus-id-to-thread id))
3861          (references (mail-header-references header))
3862          (parent
3863           (gnus-id-to-thread
3864            (or (gnus-parent-id
3865                 (when (and references
3866                            (not (equal "" references)))
3867                   references))
3868                "none")))
3869          (buffer-read-only nil)
3870          (old (car thread)))
3871     (when thread
3872       (unless iheader
3873         (setcar thread nil)
3874         (when parent
3875           (delq thread parent)))
3876       (if (gnus-summary-insert-subject id header)
3877           ;; Set the (possibly) new article number in the data structure.
3878           (gnus-data-set-number data (gnus-id-to-article id))
3879         (setcar thread old)
3880         nil))))
3881
3882 (defun gnus-rebuild-thread (id &optional line)
3883   "Rebuild the thread containing ID.
3884 If LINE, insert the rebuilt thread starting on line LINE."
3885   (let ((buffer-read-only nil)
3886         old-pos current thread data)
3887     (if (not gnus-show-threads)
3888         (setq thread (list (car (gnus-id-to-thread id))))
3889       ;; Get the thread this article is part of.
3890       (setq thread (gnus-remove-thread id)))
3891     (setq old-pos (gnus-point-at-bol))
3892     (setq current (save-excursion
3893                     (and (re-search-backward "[\r\n]" nil t)
3894                          (gnus-summary-article-number))))
3895     ;; If this is a gathered thread, we have to go some re-gathering.
3896     (when (stringp (car thread))
3897       (let ((subject (car thread))
3898             roots thr)
3899         (setq thread (cdr thread))
3900         (while thread
3901           (unless (memq (setq thr (gnus-id-to-thread
3902                                    (gnus-root-id
3903                                     (mail-header-id (caar thread)))))
3904                         roots)
3905             (push thr roots))
3906           (setq thread (cdr thread)))
3907         ;; We now have all (unique) roots.
3908         (if (= (length roots) 1)
3909             ;; All the loose roots are now one solid root.
3910             (setq thread (car roots))
3911           (setq thread (cons subject (gnus-sort-threads roots))))))
3912     (let (threads)
3913       ;; We then insert this thread into the summary buffer.
3914       (when line
3915         (goto-char (point-min))
3916         (forward-line (1- line)))
3917       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3918         (if gnus-show-threads
3919             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3920           (gnus-summary-prepare-unthreaded thread))
3921         (setq data (nreverse gnus-newsgroup-data))
3922         (setq threads gnus-newsgroup-threads))
3923       ;; We splice the new data into the data structure.
3924       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3925       ;;!!! then we want to insert at the beginning of the buffer.
3926       ;;!!! That happens to be true with Gnus now, but that may
3927       ;;!!! change in the future.  Perhaps.
3928       (gnus-data-enter-list
3929        (if line nil current) data (- (point) old-pos))
3930       (setq gnus-newsgroup-threads
3931             (nconc threads gnus-newsgroup-threads))
3932       (gnus-data-compute-positions))))
3933
3934 (defun gnus-number-to-header (number)
3935   "Return the header for article NUMBER."
3936   (let ((headers gnus-newsgroup-headers))
3937     (while (and headers
3938                 (not (= number (mail-header-number (car headers)))))
3939       (pop headers))
3940     (when headers
3941       (car headers))))
3942
3943 (defun gnus-parent-headers (in-headers &optional generation)
3944   "Return the headers of the GENERATIONeth parent of HEADERS."
3945   (unless generation
3946     (setq generation 1))
3947   (let ((parent t)
3948         (headers in-headers)
3949         references)
3950     (while (and parent
3951                 (not (zerop generation))
3952                 (setq references (mail-header-references headers)))
3953       (setq headers (if (and references
3954                              (setq parent (gnus-parent-id references)))
3955                         (car (gnus-id-to-thread parent))
3956                       nil))
3957       (decf generation))
3958     (and (not (eq headers in-headers))
3959          headers)))
3960
3961 (defun gnus-id-to-thread (id)
3962   "Return the (sub-)thread where ID appears."
3963   (gnus-gethash id gnus-newsgroup-dependencies))
3964
3965 (defun gnus-id-to-article (id)
3966   "Return the article number of ID."
3967   (let ((thread (gnus-id-to-thread id)))
3968     (when (and thread
3969                (car thread))
3970       (mail-header-number (car thread)))))
3971
3972 (defun gnus-id-to-header (id)
3973   "Return the article headers of ID."
3974   (car (gnus-id-to-thread id)))
3975
3976 (defun gnus-article-displayed-root-p (article)
3977   "Say whether ARTICLE is a root(ish) article."
3978   (let ((level (gnus-summary-thread-level article))
3979         (refs (mail-header-references  (gnus-summary-article-header article)))
3980         particle)
3981     (cond
3982      ((null level) nil)
3983      ((zerop level) t)
3984      ((null refs) t)
3985      ((null (gnus-parent-id refs)) t)
3986      ((and (= 1 level)
3987            (null (setq particle (gnus-id-to-article
3988                                  (gnus-parent-id refs))))
3989            (null (gnus-summary-thread-level particle)))))))
3990
3991 (defun gnus-root-id (id)
3992   "Return the id of the root of the thread where ID appears."
3993   (let (last-id prev)
3994     (while (and id (setq prev (car (gnus-id-to-thread id))))
3995       (setq last-id id
3996             id (gnus-parent-id (mail-header-references prev))))
3997     last-id))
3998
3999 (defun gnus-articles-in-thread (thread)
4000   "Return the list of articles in THREAD."
4001   (cons (mail-header-number (car thread))
4002         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4003
4004 (defun gnus-remove-thread (id &optional dont-remove)
4005   "Remove the thread that has ID in it."
4006   (let (headers thread last-id)
4007     ;; First go up in this thread until we find the root.
4008     (setq last-id (gnus-root-id id)
4009           headers (message-flatten-list (gnus-id-to-thread last-id)))
4010     ;; We have now found the real root of this thread.  It might have
4011     ;; been gathered into some loose thread, so we have to search
4012     ;; through the threads to find the thread we wanted.
4013     (let ((threads gnus-newsgroup-threads)
4014           sub)
4015       (while threads
4016         (setq sub (car threads))
4017         (if (stringp (car sub))
4018             ;; This is a gathered thread, so we look at the roots
4019             ;; below it to find whether this article is in this
4020             ;; gathered root.
4021             (progn
4022               (setq sub (cdr sub))
4023               (while sub
4024                 (when (member (caar sub) headers)
4025                   (setq thread (car threads)
4026                         threads nil
4027                         sub nil))
4028                 (setq sub (cdr sub))))
4029           ;; It's an ordinary thread, so we check it.
4030           (when (eq (car sub) (car headers))
4031             (setq thread sub
4032                   threads nil)))
4033         (setq threads (cdr threads)))
4034       ;; If this article is in no thread, then it's a root.
4035       (if thread
4036           (unless dont-remove
4037             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4038         (setq thread (gnus-id-to-thread last-id)))
4039       (when thread
4040         (prog1
4041             thread                      ; We return this thread.
4042           (unless dont-remove
4043             (if (stringp (car thread))
4044                 (progn
4045                   ;; If we use dummy roots, then we have to remove the
4046                   ;; dummy root as well.
4047                   (when (eq gnus-summary-make-false-root 'dummy)
4048                     ;; We go to the dummy root by going to
4049                     ;; the first sub-"thread", and then one line up.
4050                     (gnus-summary-goto-article
4051                      (mail-header-number (caadr thread)))
4052                     (forward-line -1)
4053                     (gnus-delete-line)
4054                     (gnus-data-compute-positions))
4055                   (setq thread (cdr thread))
4056                   (while thread
4057                     (gnus-remove-thread-1 (car thread))
4058                     (setq thread (cdr thread))))
4059               (gnus-remove-thread-1 thread))))))))
4060
4061 (defun gnus-remove-thread-1 (thread)
4062   "Remove the thread THREAD recursively."
4063   (let ((number (mail-header-number (pop thread)))
4064         d)
4065     (setq thread (reverse thread))
4066     (while thread
4067       (gnus-remove-thread-1 (pop thread)))
4068     (when (setq d (gnus-data-find number))
4069       (goto-char (gnus-data-pos d))
4070       (gnus-summary-show-thread)
4071       (gnus-data-remove
4072        number
4073        (- (gnus-point-at-bol)
4074           (prog1
4075               (1+ (gnus-point-at-eol))
4076             (gnus-delete-line)))))))
4077
4078 (defun gnus-sort-threads-1 (threads func)
4079   (sort (mapcar (lambda (thread)
4080                   (cons (car thread)
4081                         (and (cdr thread)
4082                              (gnus-sort-threads-1 (cdr thread) func))))
4083                 threads) func))
4084
4085 (defun gnus-sort-threads (threads)
4086   "Sort THREADS."
4087   (if (not gnus-thread-sort-functions)
4088       threads
4089     (gnus-message 8 "Sorting threads...")
4090     (prog1
4091         (gnus-sort-threads-1
4092          threads
4093          (gnus-make-sort-function gnus-thread-sort-functions))
4094       (gnus-message 8 "Sorting threads...done"))))
4095
4096 (defun gnus-sort-articles (articles)
4097   "Sort ARTICLES."
4098   (when gnus-article-sort-functions
4099     (gnus-message 7 "Sorting articles...")
4100     (prog1
4101         (setq gnus-newsgroup-headers
4102               (sort articles (gnus-make-sort-function
4103                               gnus-article-sort-functions)))
4104       (gnus-message 7 "Sorting articles...done"))))
4105
4106 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4107 (defmacro gnus-thread-header (thread)
4108   "Return header of first article in THREAD.
4109 Note that THREAD must never, ever be anything else than a variable -
4110 using some other form will lead to serious barfage."
4111   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4112   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4113   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4114         (vector thread) 2))
4115
4116 (defsubst gnus-article-sort-by-number (h1 h2)
4117   "Sort articles by article number."
4118   (< (mail-header-number h1)
4119      (mail-header-number h2)))
4120
4121 (defun gnus-thread-sort-by-number (h1 h2)
4122   "Sort threads by root article number."
4123   (gnus-article-sort-by-number
4124    (gnus-thread-header h1) (gnus-thread-header h2)))
4125
4126 (defsubst gnus-article-sort-by-lines (h1 h2)
4127   "Sort articles by article Lines header."
4128   (< (mail-header-lines h1)
4129      (mail-header-lines h2)))
4130
4131 (defun gnus-thread-sort-by-lines (h1 h2)
4132   "Sort threads by root article Lines header."
4133   (gnus-article-sort-by-lines
4134    (gnus-thread-header h1) (gnus-thread-header h2)))
4135
4136 (defsubst gnus-article-sort-by-chars (h1 h2)
4137   "Sort articles by octet length."
4138   (< (mail-header-chars h1)
4139      (mail-header-chars h2)))
4140
4141 (defun gnus-thread-sort-by-chars (h1 h2)
4142   "Sort threads by root article octet length."
4143   (gnus-article-sort-by-chars
4144    (gnus-thread-header h1) (gnus-thread-header h2)))
4145
4146 (defsubst gnus-article-sort-by-author (h1 h2)
4147   "Sort articles by root author."
4148   (string-lessp
4149    (let ((addr (car (mime-entity-read-field h1 'From))))
4150      (or (std11-full-name-string addr)
4151          (std11-address-string addr)
4152          ""))
4153    (let ((addr (car (mime-entity-read-field h2 'From))))
4154      (or (std11-full-name-string addr)
4155          (std11-address-string addr)
4156          ""))
4157    ))
4158
4159 (defun gnus-thread-sort-by-author (h1 h2)
4160   "Sort threads by root author."
4161   (gnus-article-sort-by-author
4162    (gnus-thread-header h1)  (gnus-thread-header h2)))
4163
4164 (defsubst gnus-article-sort-by-subject (h1 h2)
4165   "Sort articles by root subject."
4166   (string-lessp
4167    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4168    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4169
4170 (defun gnus-thread-sort-by-subject (h1 h2)
4171   "Sort threads by root subject."
4172   (gnus-article-sort-by-subject
4173    (gnus-thread-header h1) (gnus-thread-header h2)))
4174
4175 (defsubst gnus-article-sort-by-date (h1 h2)
4176   "Sort articles by root article date."
4177   (time-less-p
4178    (gnus-date-get-time (mail-header-date h1))
4179    (gnus-date-get-time (mail-header-date h2))))
4180
4181 (defun gnus-thread-sort-by-date (h1 h2)
4182   "Sort threads by root article date."
4183   (gnus-article-sort-by-date
4184    (gnus-thread-header h1) (gnus-thread-header h2)))
4185
4186 (defsubst gnus-article-sort-by-score (h1 h2)
4187   "Sort articles by root article score.
4188 Unscored articles will be counted as having a score of zero."
4189   (> (or (cdr (assq (mail-header-number h1)
4190                     gnus-newsgroup-scored))
4191          gnus-summary-default-score 0)
4192      (or (cdr (assq (mail-header-number h2)
4193                     gnus-newsgroup-scored))
4194          gnus-summary-default-score 0)))
4195
4196 (defun gnus-thread-sort-by-score (h1 h2)
4197   "Sort threads by root article score."
4198   (gnus-article-sort-by-score
4199    (gnus-thread-header h1) (gnus-thread-header h2)))
4200
4201 (defun gnus-thread-sort-by-total-score (h1 h2)
4202   "Sort threads by the sum of all scores in the thread.
4203 Unscored articles will be counted as having a score of zero."
4204   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4205
4206 (defun gnus-thread-total-score (thread)
4207   ;; This function find the total score of THREAD.
4208   (cond
4209    ((null thread)
4210     0)
4211    ((consp thread)
4212     (if (stringp (car thread))
4213         (apply gnus-thread-score-function 0
4214                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4215       (gnus-thread-total-score-1 thread)))
4216    (t
4217     (gnus-thread-total-score-1 (list thread)))))
4218
4219 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4220   "Sort threads such that the thread with the most recently arrived article comes first."
4221   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4222
4223 (defun gnus-thread-highest-number (thread)
4224   "Return the highest article number in THREAD."
4225   (apply 'max (mapcar (lambda (header)
4226                         (mail-header-number header))
4227                       (message-flatten-list thread))))
4228
4229 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4230   "Sort threads such that the thread with the most recently dated article comes first."
4231   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4232
4233 (defun gnus-thread-latest-date (thread)
4234   "Return the highest article date in THREAD."
4235   (let ((previous-time 0))
4236     (apply 'max (mapcar
4237                  (lambda (header)
4238                    (setq previous-time
4239                          (time-to-seconds
4240                           (mail-header-parse-date
4241                            (condition-case ()
4242                                (mail-header-date header)
4243                              (error previous-time))))))
4244                  (sort
4245                   (message-flatten-list thread)
4246                   (lambda (h1 h2)
4247                     (< (mail-header-number h1)
4248                        (mail-header-number h2))))))))
4249
4250 (defun gnus-thread-total-score-1 (root)
4251   ;; This function find the total score of the thread below ROOT.
4252   (setq root (car root))
4253   (apply gnus-thread-score-function
4254          (or (append
4255               (mapcar 'gnus-thread-total-score
4256                       (cdr (gnus-id-to-thread (mail-header-id root))))
4257               (when (> (mail-header-number root) 0)
4258                 (list (or (cdr (assq (mail-header-number root)
4259                                      gnus-newsgroup-scored))
4260                           gnus-summary-default-score 0))))
4261              (list gnus-summary-default-score)
4262              '(0))))
4263
4264 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4265 (defvar gnus-tmp-prev-subject nil)
4266 (defvar gnus-tmp-false-parent nil)
4267 (defvar gnus-tmp-root-expunged nil)
4268 (defvar gnus-tmp-dummy-line nil)
4269
4270 (eval-when-compile (defvar gnus-tmp-header))
4271 (defun gnus-extra-header (type &optional header)
4272   "Return the extra header of TYPE."
4273   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4274       ""))
4275
4276 (defvar gnus-tmp-thread-tree-header-string "")
4277
4278 (defvar gnus-sum-thread-tree-root "> "
4279   "With %B spec, used for the root of a thread.
4280 If nil, use subject instead.")
4281 (defvar gnus-sum-thread-tree-single-indent ""
4282   "With %B spec, used for a thread with just one message.
4283 If nil, use subject instead.")
4284 (defvar gnus-sum-thread-tree-vertical "| "
4285   "With %B spec, used for drawing a vertical line.")
4286 (defvar gnus-sum-thread-tree-indent "  "
4287   "With %B spec, used for indenting.")
4288 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4289   "With %B spec, used for a leaf with brothers.")
4290 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4291   "With %B spec, used for a leaf without brothers.")
4292
4293 (defun gnus-summary-prepare-threads (threads)
4294   "Prepare summary buffer from THREADS and indentation LEVEL.
4295 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4296 or a straight list of headers."
4297   (gnus-message 7 "Generating summary...")
4298
4299   (setq gnus-newsgroup-threads threads)
4300   (beginning-of-line)
4301
4302   (let ((gnus-tmp-level 0)
4303         (default-score (or gnus-summary-default-score 0))
4304         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4305         thread number subject stack state gnus-tmp-gathered beg-match
4306         new-roots gnus-tmp-new-adopts thread-end
4307         gnus-tmp-header gnus-tmp-unread
4308         gnus-tmp-replied gnus-tmp-subject-or-nil
4309         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4310         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4311         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4312         tree-stack)
4313
4314     (setq gnus-tmp-prev-subject nil)
4315
4316     (if (vectorp (car threads))
4317         ;; If this is a straight (sic) list of headers, then a
4318         ;; threaded summary display isn't required, so we just create
4319         ;; an unthreaded one.
4320         (gnus-summary-prepare-unthreaded threads)
4321
4322       ;; Do the threaded display.
4323
4324       (while (or threads stack gnus-tmp-new-adopts new-roots)
4325
4326         (if (and (= gnus-tmp-level 0)
4327                  (or (not stack)
4328                      (= (caar stack) 0))
4329                  (not gnus-tmp-false-parent)
4330                  (or gnus-tmp-new-adopts new-roots))
4331             (if gnus-tmp-new-adopts
4332                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4333                       thread (list (car gnus-tmp-new-adopts))
4334                       gnus-tmp-header (caar thread)
4335                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4336               (when new-roots
4337                 (setq thread (list (car new-roots))
4338                       gnus-tmp-header (caar thread)
4339                       new-roots (cdr new-roots))))
4340
4341           (if threads
4342               ;; If there are some threads, we do them before the
4343               ;; threads on the stack.
4344               (setq thread threads
4345                     gnus-tmp-header (caar thread))
4346             ;; There were no current threads, so we pop something off
4347             ;; the stack.
4348             (setq state (car stack)
4349                   gnus-tmp-level (car state)
4350                   tree-stack (cadr state)
4351                   thread (caddr state)
4352                   stack (cdr stack)
4353                   gnus-tmp-header (caar thread))))
4354
4355         (setq gnus-tmp-false-parent nil)
4356         (setq gnus-tmp-root-expunged nil)
4357         (setq thread-end nil)
4358
4359         (if (stringp gnus-tmp-header)
4360             ;; The header is a dummy root.
4361             (cond
4362              ((eq gnus-summary-make-false-root 'adopt)
4363               ;; We let the first article adopt the rest.
4364               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4365                                                (cddar thread)))
4366               (setq gnus-tmp-gathered
4367                     (nconc (mapcar
4368                             (lambda (h) (mail-header-number (car h)))
4369                             (cddar thread))
4370                            gnus-tmp-gathered))
4371               (setq thread (cons (list (caar thread)
4372                                        (cadar thread))
4373                                  (cdr thread)))
4374               (setq gnus-tmp-level -1
4375                     gnus-tmp-false-parent t))
4376              ((eq gnus-summary-make-false-root 'empty)
4377               ;; We print adopted articles with empty subject fields.
4378               (setq gnus-tmp-gathered
4379                     (nconc (mapcar
4380                             (lambda (h) (mail-header-number (car h)))
4381                             (cddar thread))
4382                            gnus-tmp-gathered))
4383               (setq gnus-tmp-level -1))
4384              ((eq gnus-summary-make-false-root 'dummy)
4385               ;; We remember that we probably want to output a dummy
4386               ;; root.
4387               (setq gnus-tmp-dummy-line gnus-tmp-header)
4388               (setq gnus-tmp-prev-subject gnus-tmp-header))
4389              (t
4390               ;; We do not make a root for the gathered
4391               ;; sub-threads at all.
4392               (setq gnus-tmp-level -1)))
4393
4394           (setq number (mail-header-number gnus-tmp-header)
4395                 subject (mail-header-subject gnus-tmp-header))
4396
4397           (cond
4398            ;; If the thread has changed subject, we might want to make
4399            ;; this subthread into a root.
4400            ((and (null gnus-thread-ignore-subject)
4401                  (not (zerop gnus-tmp-level))
4402                  gnus-tmp-prev-subject
4403                  (not (inline
4404                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4405             (setq new-roots (nconc new-roots (list (car thread)))
4406                   thread-end t
4407                   gnus-tmp-header nil))
4408            ;; If the article lies outside the current limit,
4409            ;; then we do not display it.
4410            ((not (memq number gnus-newsgroup-limit))
4411             (setq gnus-tmp-gathered
4412                   (nconc (mapcar
4413                           (lambda (h) (mail-header-number (car h)))
4414                           (cdar thread))
4415                          gnus-tmp-gathered))
4416             (setq gnus-tmp-new-adopts (if (cdar thread)
4417                                           (append gnus-tmp-new-adopts
4418                                                   (cdar thread))
4419                                         gnus-tmp-new-adopts)
4420                   thread-end t
4421                   gnus-tmp-header nil)
4422             (when (zerop gnus-tmp-level)
4423               (setq gnus-tmp-root-expunged t)))
4424            ;; Perhaps this article is to be marked as read?
4425            ((and gnus-summary-mark-below
4426                  (< (or (cdr (assq number gnus-newsgroup-scored))
4427                         default-score)
4428                     gnus-summary-mark-below)
4429                  ;; Don't touch sparse articles.
4430                  (not (gnus-summary-article-sparse-p number))
4431                  (not (gnus-summary-article-ancient-p number)))
4432             (setq gnus-newsgroup-unreads
4433                   (delq number gnus-newsgroup-unreads))
4434             (if gnus-newsgroup-auto-expire
4435                 (push number gnus-newsgroup-expirable)
4436               (push (cons number gnus-low-score-mark)
4437                     gnus-newsgroup-reads))))
4438
4439           (when gnus-tmp-header
4440             ;; We may have an old dummy line to output before this
4441             ;; article.
4442             (when (and gnus-tmp-dummy-line
4443                        (gnus-subject-equal
4444                         gnus-tmp-dummy-line
4445                         (mail-header-subject gnus-tmp-header)))
4446               (gnus-summary-insert-dummy-line
4447                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4448               (setq gnus-tmp-dummy-line nil))
4449
4450             ;; Compute the mark.
4451             (setq gnus-tmp-unread (gnus-article-mark number))
4452
4453             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4454                                   gnus-tmp-header gnus-tmp-level)
4455                   gnus-newsgroup-data)
4456
4457             ;; Actually insert the line.
4458             (setq
4459              gnus-tmp-subject-or-nil
4460              (cond
4461               ((and gnus-thread-ignore-subject
4462                     gnus-tmp-prev-subject
4463                     (not (inline (gnus-subject-equal
4464                                   gnus-tmp-prev-subject subject))))
4465                subject)
4466               ((zerop gnus-tmp-level)
4467                (if (and (eq gnus-summary-make-false-root 'empty)
4468                         (memq number gnus-tmp-gathered)
4469                         gnus-tmp-prev-subject
4470                         (inline (gnus-subject-equal
4471                                  gnus-tmp-prev-subject subject)))
4472                    gnus-summary-same-subject
4473                  subject))
4474               (t gnus-summary-same-subject)))
4475             (if (and (eq gnus-summary-make-false-root 'adopt)
4476                      (= gnus-tmp-level 1)
4477                      (memq number gnus-tmp-gathered))
4478                 (setq gnus-tmp-opening-bracket ?\<
4479                       gnus-tmp-closing-bracket ?\>)
4480               (setq gnus-tmp-opening-bracket ?\[
4481                     gnus-tmp-closing-bracket ?\]))
4482             (setq
4483              gnus-tmp-indentation
4484              (aref gnus-thread-indent-array gnus-tmp-level)
4485              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4486              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4487                                 gnus-summary-default-score 0)
4488              gnus-tmp-score-char
4489              (if (or (null gnus-summary-default-score)
4490                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4491                          gnus-summary-zcore-fuzz))
4492                  ?\ ;;;Whitespace
4493                (if (< gnus-tmp-score gnus-summary-default-score)
4494                    gnus-score-below-mark gnus-score-over-mark))
4495              gnus-tmp-replied
4496              (cond ((memq number gnus-newsgroup-processable)
4497                     gnus-process-mark)
4498                    ((memq number gnus-newsgroup-cached)
4499                     gnus-cached-mark)
4500                    ((memq number gnus-newsgroup-replied)
4501                     gnus-replied-mark)
4502                    ((memq number gnus-newsgroup-forwarded)
4503                     gnus-forwarded-mark)
4504                    ((memq number gnus-newsgroup-saved)
4505                     gnus-saved-mark)
4506                    ((memq number gnus-newsgroup-recent)
4507                     gnus-recent-mark)
4508                    ((memq number gnus-newsgroup-unseen)
4509                     gnus-unseen-mark)
4510                    (t gnus-no-mark))
4511              gnus-tmp-from (mail-header-from gnus-tmp-header)
4512              gnus-tmp-name
4513              (cond
4514               ((string-match "<[^>]+> *$" gnus-tmp-from)
4515                (setq beg-match (match-beginning 0))
4516                (or (and (string-match "^\".+\"" gnus-tmp-from)
4517                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4518                    (substring gnus-tmp-from 0 beg-match)))
4519               ((string-match "(.+)" gnus-tmp-from)
4520                (substring gnus-tmp-from
4521                           (1+ (match-beginning 0)) (1- (match-end 0))))
4522               (t gnus-tmp-from))
4523              gnus-tmp-thread-tree-header-string
4524              (cond
4525               ((not gnus-show-threads) "")
4526               ((zerop gnus-tmp-level)
4527                (if (cdar thread)
4528                    (or gnus-sum-thread-tree-root subject)
4529                  (or gnus-sum-thread-tree-single-indent subject)))
4530               (t
4531                (concat (apply 'concat
4532                               (mapcar (lambda (item)
4533                                         (if (= item 1)
4534                                             gnus-sum-thread-tree-vertical
4535                                           gnus-sum-thread-tree-indent))
4536                                       (cdr (reverse tree-stack))))
4537                        (if (nth 1 thread)
4538                            gnus-sum-thread-tree-leaf-with-other
4539                          gnus-sum-thread-tree-single-leaf)))))
4540             (when (string= gnus-tmp-name "")
4541               (setq gnus-tmp-name gnus-tmp-from))
4542             (unless (numberp gnus-tmp-lines)
4543               (setq gnus-tmp-lines -1))
4544             (if (= gnus-tmp-lines -1)
4545                 (setq gnus-tmp-lines "?")
4546               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4547             (gnus-put-text-property
4548              (point)
4549              (progn (eval gnus-summary-line-format-spec) (point))
4550              'gnus-number number)
4551             (when gnus-visual-p
4552               (forward-line -1)
4553               (gnus-run-hooks 'gnus-summary-update-hook)
4554               (forward-line 1))
4555
4556             (setq gnus-tmp-prev-subject subject)))
4557
4558         (when (nth 1 thread)
4559           (push (list (max 0 gnus-tmp-level)
4560                       (copy-list tree-stack)
4561                       (nthcdr 1 thread))
4562                 stack))
4563         (push (if (nth 1 thread) 1 0) tree-stack)
4564         (incf gnus-tmp-level)
4565         (setq threads (if thread-end nil (cdar thread)))
4566         (unless threads
4567           (setq gnus-tmp-level 0)))))
4568   (gnus-message 7 "Generating summary...done"))
4569
4570 (defun gnus-summary-prepare-unthreaded (headers)
4571   "Generate an unthreaded summary buffer based on HEADERS."
4572   (let (header number mark)
4573
4574     (beginning-of-line)
4575
4576     (while headers
4577       ;; We may have to root out some bad articles...
4578       (when (memq (setq number (mail-header-number
4579                                 (setq header (pop headers))))
4580                   gnus-newsgroup-limit)
4581         ;; Mark article as read when it has a low score.
4582         (when (and gnus-summary-mark-below
4583                    (< (or (cdr (assq number gnus-newsgroup-scored))
4584                           gnus-summary-default-score 0)
4585                       gnus-summary-mark-below)
4586                    (not (gnus-summary-article-ancient-p number)))
4587           (setq gnus-newsgroup-unreads
4588                 (delq number gnus-newsgroup-unreads))
4589           (if gnus-newsgroup-auto-expire
4590               (push number gnus-newsgroup-expirable)
4591             (push (cons number gnus-low-score-mark)
4592                   gnus-newsgroup-reads)))
4593
4594         (setq mark (gnus-article-mark number))
4595         (push (gnus-data-make number mark (1+ (point)) header 0)
4596               gnus-newsgroup-data)
4597         (gnus-summary-insert-line
4598          header 0 number
4599          mark (memq number gnus-newsgroup-replied)
4600          (memq number gnus-newsgroup-expirable)
4601          (mail-header-subject header) nil
4602          (cdr (assq number gnus-newsgroup-scored))
4603          (memq number gnus-newsgroup-processable))))))
4604
4605 (defun gnus-summary-remove-list-identifiers ()
4606   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4607   (let ((regexp (if (consp gnus-list-identifiers)
4608                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4609                   gnus-list-identifiers))
4610         changed subject)
4611     (when regexp
4612       (dolist (header gnus-newsgroup-headers)
4613         (setq subject (mail-header-subject header)
4614               changed nil)
4615         (while (string-match
4616                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4617                 subject)
4618           (setq subject
4619                 (concat (substring subject 0 (match-beginning 2))
4620                         (substring subject (match-end 0)))
4621                 changed t))
4622         (when (and changed
4623                    (string-match
4624                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4625           (setq subject
4626                 (concat (substring subject 0 (match-beginning 1))
4627                         (substring subject (match-end 1)))))
4628         (when changed
4629           (mail-header-set-subject header subject))))))
4630
4631 (defun gnus-fetch-headers (articles)
4632   "Fetch headers of ARTICLES."
4633   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4634     (gnus-message 5 "Fetching headers for %s..." name)
4635     (prog1
4636         (if (eq 'nov
4637                 (setq gnus-headers-retrieved-by
4638                       (gnus-retrieve-headers
4639                        articles gnus-newsgroup-name
4640                        ;; We might want to fetch old headers, but
4641                        ;; not if there is only 1 article.
4642                        (and (or (and
4643                                  (not (eq gnus-fetch-old-headers 'some))
4644                                  (not (numberp gnus-fetch-old-headers)))
4645                                 (> (length articles) 1))
4646                             gnus-fetch-old-headers))))
4647             (gnus-get-newsgroup-headers-xover
4648              articles nil nil gnus-newsgroup-name t)
4649           (gnus-get-newsgroup-headers))
4650       (gnus-message 5 "Fetching headers for %s...done" name))))
4651
4652 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4653   "Select newsgroup GROUP.
4654 If READ-ALL is non-nil, all articles in the group are selected.
4655 If SELECT-ARTICLES, only select those articles from GROUP."
4656   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4657          ;;!!! Dirty hack; should be removed.
4658          (gnus-summary-ignore-duplicates
4659           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4660               t
4661             gnus-summary-ignore-duplicates))
4662          (info (nth 2 entry))
4663          articles fetched-articles cached)
4664
4665     (unless (gnus-check-server
4666              (set (make-local-variable 'gnus-current-select-method)
4667                   (gnus-find-method-for-group group)))
4668       (error "Couldn't open server"))
4669
4670     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4671         (gnus-activate-group group)     ; Or we can activate it...
4672         (progn                          ; Or we bug out.
4673           (when (equal major-mode 'gnus-summary-mode)
4674             (kill-buffer (current-buffer)))
4675           (error "Couldn't activate group %s: %s"
4676                  group (gnus-status-message group))))
4677
4678     (unless (gnus-request-group group t)
4679       (when (equal major-mode 'gnus-summary-mode)
4680         (kill-buffer (current-buffer)))
4681       (error "Couldn't request group %s: %s"
4682              group (gnus-status-message group)))
4683
4684     (setq gnus-newsgroup-name group
4685           gnus-newsgroup-unselected nil
4686           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4687
4688     (let ((display (gnus-group-find-parameter group 'display)))
4689       (setq gnus-newsgroup-display
4690             (cond
4691              ((not (zerop (or (car-safe read-all) 0)))
4692               ;; The user entered the group with C-u SPC/RET, let's show
4693               ;; all articles.
4694               'gnus-not-ignore)
4695              ((eq display 'all)
4696               'gnus-not-ignore)
4697              ((arrayp display)
4698               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4699              ((numberp display)
4700               ;; The following is probably the "correct" solution, but
4701               ;; it makes Gnus fetch all headers and then limit the
4702               ;; articles (which is slow), so instead we hack the
4703               ;; select-articles parameter instead. -- Simon Josefsson
4704               ;; <jas@kth.se>
4705               ;;
4706               ;; (gnus-byte-compile
4707               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4708               ;;                         display)))))
4709               (setq select-articles
4710                     (gnus-uncompress-range
4711                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4712                              (if (> tmp 0)
4713                                  tmp
4714                                1))
4715                            (cdr (gnus-active group)))))
4716               nil)
4717              (t
4718               nil))))
4719
4720     (gnus-summary-setup-default-charset)
4721
4722     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4723     (when (gnus-virtual-group-p group)
4724       (setq cached gnus-newsgroup-cached))
4725
4726     (setq gnus-newsgroup-unreads
4727           (gnus-set-difference
4728            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4729            gnus-newsgroup-dormant))
4730
4731     (setq gnus-newsgroup-processable nil)
4732
4733     (gnus-update-read-articles group gnus-newsgroup-unreads)
4734
4735     ;; Adjust and set lists of article marks.
4736     (when info
4737       (gnus-adjust-marked-articles info))
4738
4739     (if (setq articles select-articles)
4740         (setq gnus-newsgroup-unselected
4741               (gnus-sorted-intersection
4742                gnus-newsgroup-unreads
4743                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4744       (setq articles (gnus-articles-to-read group read-all)))
4745
4746     (cond
4747      ((null articles)
4748       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4749       'quit)
4750      ((eq articles 0) nil)
4751      (t
4752       ;; Init the dependencies hash table.
4753       (setq gnus-newsgroup-dependencies
4754             (gnus-make-hashtable (length articles)))
4755       (gnus-set-global-variables)
4756       ;; Retrieve the headers and read them in.
4757       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4758
4759       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4760       (when cached
4761         (setq gnus-newsgroup-cached cached))
4762
4763       ;; Suppress duplicates?
4764       (when gnus-suppress-duplicates
4765         (gnus-dup-suppress-articles))
4766
4767       ;; Set the initial limit.
4768       (setq gnus-newsgroup-limit (copy-sequence articles))
4769       ;; Remove canceled articles from the list of unread articles.
4770       (setq fetched-articles
4771             (mapcar (lambda (headers) (mail-header-number headers))
4772                     gnus-newsgroup-headers))
4773       (setq gnus-newsgroup-articles fetched-articles)
4774       (setq gnus-newsgroup-unreads
4775             (gnus-set-sorted-intersection
4776              gnus-newsgroup-unreads fetched-articles))
4777       (gnus-compute-unseen-list)
4778
4779       ;; Removed marked articles that do not exist.
4780       (gnus-update-missing-marks
4781        (gnus-sorted-complement fetched-articles articles))
4782       ;; We might want to build some more threads first.
4783       (when (and gnus-fetch-old-headers
4784                  (eq gnus-headers-retrieved-by 'nov))
4785         (if (eq gnus-fetch-old-headers 'invisible)
4786             (gnus-build-all-threads)
4787           (gnus-build-old-threads)))
4788       ;; Let the Gnus agent mark articles as read.
4789       (when gnus-agent
4790         (gnus-agent-get-undownloaded-list))
4791       ;; Remove list identifiers from subject
4792       (when gnus-list-identifiers
4793         (gnus-summary-remove-list-identifiers))
4794       ;; Check whether auto-expire is to be done in this group.
4795       (setq gnus-newsgroup-auto-expire
4796             (gnus-group-auto-expirable-p group))
4797       ;; Set up the article buffer now, if necessary.
4798       (unless gnus-single-article-buffer
4799         (gnus-article-setup-buffer))
4800       ;; First and last article in this newsgroup.
4801       (when gnus-newsgroup-headers
4802         (setq gnus-newsgroup-begin
4803               (mail-header-number (car gnus-newsgroup-headers))
4804               gnus-newsgroup-end
4805               (mail-header-number
4806                (gnus-last-element gnus-newsgroup-headers))))
4807       ;; GROUP is successfully selected.
4808       (or gnus-newsgroup-headers t)))))
4809
4810 (defun gnus-compute-unseen-list ()
4811   ;; The `seen' marks are treated specially.
4812   (if (not gnus-newsgroup-seen)
4813       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4814     (setq gnus-newsgroup-unseen
4815           (gnus-inverse-list-range-intersection
4816            gnus-newsgroup-articles gnus-newsgroup-seen))))
4817
4818 (defun gnus-summary-display-make-predicate (display)
4819   (require 'gnus-agent)
4820   (when (= (length display) 1)
4821     (setq display (car display)))
4822   (unless gnus-summary-display-cache
4823     (dolist (elem (append (list (cons 'read 'read)
4824                                 (cons 'unseen 'unseen))
4825                           gnus-article-mark-lists))
4826       (push (cons (cdr elem)
4827                   (gnus-byte-compile
4828                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4829             gnus-summary-display-cache)))
4830   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4831     (gnus-get-predicate display)))
4832
4833 ;; Uses the dynamically bound `number' variable.
4834 (defvar number)
4835 (defun gnus-article-marked-p (type &optional article)
4836   (let ((article (or article number)))
4837     (cond
4838      ((eq type 'tick)
4839       (memq article gnus-newsgroup-marked))
4840      ((eq type 'unsend)
4841       (memq article gnus-newsgroup-unsendable))
4842      ((eq type 'undownload)
4843       (memq article gnus-newsgroup-undownloaded))
4844      ((eq type 'download)
4845       (memq article gnus-newsgroup-downloadable))
4846      ((eq type 'unread)
4847       (memq article gnus-newsgroup-unreads))
4848      ((eq type 'read)
4849       (memq article gnus-newsgroup-reads))
4850      ((eq type 'dormant)
4851       (memq article gnus-newsgroup-dormant) )
4852      ((eq type 'expire)
4853       (memq article gnus-newsgroup-expirable))
4854      ((eq type 'reply)
4855       (memq article gnus-newsgroup-replied))
4856      ((eq type 'killed)
4857       (memq article gnus-newsgroup-killed))
4858      ((eq type 'bookmark)
4859       (assq article gnus-newsgroup-bookmarks))
4860      ((eq type 'score)
4861       (assq article gnus-newsgroup-scored))
4862      ((eq type 'save)
4863       (memq article gnus-newsgroup-saved))
4864      ((eq type 'cache)
4865       (memq article gnus-newsgroup-cached))
4866      ((eq type 'forward)
4867       (memq article gnus-newsgroup-forwarded))
4868      ((eq type 'seen)
4869       (not (memq article gnus-newsgroup-unseen)))
4870      ((eq type 'recent)
4871       (memq article gnus-newsgroup-recent))
4872      (t t))))
4873
4874 (defun gnus-articles-to-read (group &optional read-all)
4875   "Find out what articles the user wants to read."
4876   (let* ((articles
4877           ;; Select all articles if `read-all' is non-nil, or if there
4878           ;; are no unread articles.
4879           (if (or read-all
4880                   (and (zerop (length gnus-newsgroup-marked))
4881                        (zerop (length gnus-newsgroup-unreads)))
4882                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4883               ;; We want to select the headers for all the articles in
4884               ;; the group, so we select either all the active
4885               ;; articles in the group, or (if that's nil), the
4886               ;; articles in the cache.
4887               (or
4888                (gnus-uncompress-range (gnus-active group))
4889                (gnus-cache-articles-in-group group))
4890             ;; Select only the "normal" subset of articles.
4891             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4892                           (copy-sequence gnus-newsgroup-unreads))
4893                   '<)))
4894          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4895          (scored (length scored-list))
4896          (number (length articles))
4897          (marked (+ (length gnus-newsgroup-marked)
4898                     (length gnus-newsgroup-dormant)))
4899          (select
4900           (cond
4901            ((numberp read-all)
4902             read-all)
4903            (t
4904             (condition-case ()
4905                 (cond
4906                  ((and (or (<= scored marked) (= scored number))
4907                        (natnump gnus-large-newsgroup)
4908                        (> number gnus-large-newsgroup))
4909                   (let* ((cursor-in-echo-area nil)
4910                          (input
4911                           (read-from-minibuffer
4912                            (format
4913                             "How many articles from %s (max %d): "
4914                             (gnus-limit-string
4915                              (gnus-group-decoded-name gnus-newsgroup-name)
4916                              35)
4917                             number)
4918                            (cons (number-to-string gnus-large-newsgroup)
4919                                  0))))
4920                     (if (string-match "^[ \t]*$" input)
4921                         number
4922                       input)))
4923                  ((and (> scored marked) (< scored number)
4924                        (> (- scored number) 20))
4925                   (let ((input
4926                          (read-string
4927                           (format "%s %s (%d scored, %d total): "
4928                                   "How many articles from"
4929                                   (gnus-group-decoded-name group)
4930                                   scored number))))
4931                     (if (string-match "^[ \t]*$" input)
4932                         number input)))
4933                  (t number))
4934               (quit
4935                (message "Quit getting the articles to read")
4936                nil))))))
4937     (setq select (if (stringp select) (string-to-number select) select))
4938     (if (or (null select) (zerop select))
4939         select
4940       (if (and (not (zerop scored)) (<= (abs select) scored))
4941           (progn
4942             (setq articles (sort scored-list '<))
4943             (setq number (length articles)))
4944         (setq articles (copy-sequence articles)))
4945
4946       (when (< (abs select) number)
4947         (if (< select 0)
4948             ;; Select the N oldest articles.
4949             (setcdr (nthcdr (1- (abs select)) articles) nil)
4950           ;; Select the N most recent articles.
4951           (setq articles (nthcdr (- number select) articles))))
4952       (setq gnus-newsgroup-unselected
4953             (gnus-sorted-intersection
4954              gnus-newsgroup-unreads
4955              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4956       (when gnus-alter-articles-to-read-function
4957         (setq gnus-newsgroup-unreads
4958               (sort
4959                (funcall gnus-alter-articles-to-read-function
4960                         gnus-newsgroup-name gnus-newsgroup-unreads)
4961                '<)))
4962       articles)))
4963
4964 (defun gnus-killed-articles (killed articles)
4965   (let (out)
4966     (while articles
4967       (when (inline (gnus-member-of-range (car articles) killed))
4968         (push (car articles) out))
4969       (setq articles (cdr articles)))
4970     out))
4971
4972 (defun gnus-uncompress-marks (marks)
4973   "Uncompress the mark ranges in MARKS."
4974   (let ((uncompressed '(score bookmark))
4975         out)
4976     (while marks
4977       (if (memq (caar marks) uncompressed)
4978           (push (car marks) out)
4979         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4980       (setq marks (cdr marks)))
4981     out))
4982
4983 (defun gnus-article-mark-to-type (mark)
4984   "Return the type of MARK."
4985   (or (cadr (assq mark gnus-article-special-mark-lists))
4986       'list))
4987
4988 (defun gnus-article-unpropagatable-p (mark)
4989   "Return whether MARK should be propagated to backend."
4990   (memq mark gnus-article-unpropagated-mark-lists))
4991
4992 (defun gnus-adjust-marked-articles (info)
4993   "Set all article lists and remove all marks that are no longer valid."
4994   (let* ((marked-lists (gnus-info-marks info))
4995          (active (gnus-active (gnus-info-group info)))
4996          (min (car active))
4997          (max (cdr active))
4998          (types gnus-article-mark-lists)
4999          marks var articles article mark mark-type)
5000
5001     (dolist (marks marked-lists)
5002       (setq mark (car marks)
5003             mark-type (gnus-article-mark-to-type mark)
5004             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5005
5006       ;; We set the variable according to the type of the marks list,
5007       ;; and then adjust the marks to a subset of the active articles.
5008       (cond
5009        ;; Adjust "simple" lists.
5010        ((eq mark-type 'list)
5011         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5012         (when (memq mark '(tick dormant expire reply save))
5013           (while articles
5014             (when (or (< (setq article (pop articles)) min) (> article max))
5015               (set var (delq article (symbol-value var)))))))
5016        ;; Adjust assocs.
5017        ((eq mark-type 'tuple)
5018         (set var (setq articles (cdr marks)))
5019         (when (not (listp (cdr (symbol-value var))))
5020           (set var (list (symbol-value var))))
5021         (when (not (listp (cdr articles)))
5022           (setq articles (list articles)))
5023         (while articles
5024           (when (or (not (consp (setq article (pop articles))))
5025                     (< (car article) min)
5026                     (> (car article) max))
5027             (set var (delq article (symbol-value var))))))
5028        ;; Adjust ranges (sloppily).
5029        ((eq mark-type 'range)
5030         (cond
5031          ((eq mark 'seen)
5032           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5033           ;; It should be (seen (NUM1 . NUM2)).
5034           (when (numberp (cddr marks))
5035             (setcdr marks (list (cdr marks))))
5036           (setq articles (cdr marks))
5037           (while (and articles
5038                       (or (and (consp (car articles))
5039                                (> min (cdar articles)))
5040                           (and (numberp (car articles))
5041                                (> min (car articles)))))
5042             (pop articles))
5043           (set var articles))))))))
5044
5045 (defun gnus-update-missing-marks (missing)
5046   "Go through the list of MISSING articles and remove them from the mark lists."
5047   (when missing
5048     (let (var m)
5049       ;; Go through all types.
5050       (dolist (elem gnus-article-mark-lists)
5051         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5052           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5053           (when (symbol-value var)
5054             ;; This list has articles.  So we delete all missing
5055             ;; articles from it.
5056             (setq m missing)
5057             (while m
5058               (set var (delq (pop m) (symbol-value var))))))))))
5059
5060 (defun gnus-update-marks ()
5061   "Enter the various lists of marked articles into the newsgroup info list."
5062   (let ((types gnus-article-mark-lists)
5063         (info (gnus-get-info gnus-newsgroup-name))
5064         type list newmarked symbol delta-marks)
5065     (when info
5066       ;; Add all marks lists to the list of marks lists.
5067       (while (setq type (pop types))
5068         (setq list (symbol-value
5069                     (setq symbol
5070                           (intern (format "gnus-newsgroup-%s" (car type))))))
5071
5072         (when list
5073           ;; Get rid of the entries of the articles that have the
5074           ;; default score.
5075           (when (and (eq (cdr type) 'score)
5076                      gnus-save-score
5077                      list)
5078             (let* ((arts list)
5079                    (prev (cons nil list))
5080                    (all prev))
5081               (while arts
5082                 (if (or (not (consp (car arts)))
5083                         (= (cdar arts) gnus-summary-default-score))
5084                     (setcdr prev (cdr arts))
5085                   (setq prev arts))
5086                 (setq arts (cdr arts)))
5087               (setq list (cdr all)))))
5088
5089         (when (eq (cdr type) 'seen)
5090           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5091
5092         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5093           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5094
5095         (when (and (gnus-check-backend-function
5096                     'request-set-mark gnus-newsgroup-name)
5097                    (not (gnus-article-unpropagatable-p (cdr type))))
5098           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5099                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5100                  (add (gnus-remove-from-range
5101                        (gnus-copy-sequence list) old)))
5102             (when add
5103               (push (list add 'add (list (cdr type))) delta-marks))
5104             (when del
5105               (push (list del 'del (list (cdr type))) delta-marks))))
5106
5107         (when list
5108           (push (cons (cdr type) list) newmarked)))
5109
5110       (when delta-marks
5111         (unless (gnus-check-group gnus-newsgroup-name)
5112           (error "Can't open server for %s" gnus-newsgroup-name))
5113         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5114
5115       ;; Enter these new marks into the info of the group.
5116       (if (nthcdr 3 info)
5117           (setcar (nthcdr 3 info) newmarked)
5118         ;; Add the marks lists to the end of the info.
5119         (when newmarked
5120           (setcdr (nthcdr 2 info) (list newmarked))))
5121
5122       ;; Cut off the end of the info if there's nothing else there.
5123       (let ((i 5))
5124         (while (and (> i 2)
5125                     (not (nth i info)))
5126           (when (nthcdr (decf i) info)
5127             (setcdr (nthcdr i info) nil)))))))
5128
5129 (defun gnus-set-mode-line (where)
5130   "Set the mode line of the article or summary buffers.
5131 If WHERE is `summary', the summary mode line format will be used."
5132   ;; Is this mode line one we keep updated?
5133   (when (and (memq where gnus-updated-mode-lines)
5134              (symbol-value
5135               (intern (format "gnus-%s-mode-line-format-spec" where))))
5136     (let (mode-string)
5137       (save-excursion
5138         ;; We evaluate this in the summary buffer since these
5139         ;; variables are buffer-local to that buffer.
5140         (set-buffer gnus-summary-buffer)
5141         ;; We bind all these variables that are used in the `eval' form
5142         ;; below.
5143         (let* ((mformat (symbol-value
5144                          (intern
5145                           (format "gnus-%s-mode-line-format-spec" where))))
5146                (gnus-tmp-group-name (gnus-group-decoded-name
5147                                      gnus-newsgroup-name))
5148                (gnus-tmp-article-number (or gnus-current-article 0))
5149                (gnus-tmp-unread gnus-newsgroup-unreads)
5150                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5151                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5152                (gnus-tmp-unread-and-unselected
5153                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5154                             (zerop gnus-tmp-unselected))
5155                        "")
5156                       ((zerop gnus-tmp-unselected)
5157                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5158                       (t (format "{%d(+%d) more}"
5159                                  gnus-tmp-unread-and-unticked
5160                                  gnus-tmp-unselected))))
5161                (gnus-tmp-subject
5162                 (if (and gnus-current-headers
5163                          (vectorp gnus-current-headers))
5164                     (gnus-mode-string-quote
5165                      (mail-header-subject gnus-current-headers))
5166                   ""))
5167                bufname-length max-len
5168                gnus-tmp-header);; passed as argument to any user-format-funcs
5169           (setq mode-string (eval mformat))
5170           (setq bufname-length (if (string-match "%b" mode-string)
5171                                    (- (length
5172                                        (buffer-name
5173                                         (if (eq where 'summary)
5174                                             nil
5175                                           (get-buffer gnus-article-buffer))))
5176                                       2)
5177                                  0))
5178           (setq max-len (max 4 (if gnus-mode-non-string-length
5179                                    (- (window-width)
5180                                       gnus-mode-non-string-length
5181                                       bufname-length)
5182                                  (length mode-string))))
5183           ;; We might have to chop a bit of the string off...
5184           (when (> (length mode-string) max-len)
5185             (setq mode-string
5186                   (concat (gnus-truncate-string mode-string (- max-len 3))
5187                           "...")))
5188           ;; Pad the mode string a bit.
5189           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5190       ;; Update the mode line.
5191       (setq mode-line-buffer-identification
5192             (gnus-mode-line-buffer-identification (list mode-string)))
5193       (set-buffer-modified-p t))))
5194
5195 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5196   "Go through the HEADERS list and add all Xrefs to a hash table.
5197 The resulting hash table is returned, or nil if no Xrefs were found."
5198   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5199          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5200          (xref-hashtb (gnus-make-hashtable))
5201          start group entry number xrefs header)
5202     (while headers
5203       (setq header (pop headers))
5204       (when (and (setq xrefs (mail-header-xref header))
5205                  (not (memq (setq number (mail-header-number header))
5206                             unreads)))
5207         (setq start 0)
5208         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5209           (setq start (match-end 0))
5210           (setq group (if prefix
5211                           (concat prefix (substring xrefs (match-beginning 1)
5212                                                     (match-end 1)))
5213                         (substring xrefs (match-beginning 1) (match-end 1))))
5214           (setq number
5215                 (string-to-int (substring xrefs (match-beginning 2)
5216                                           (match-end 2))))
5217           (if (setq entry (gnus-gethash group xref-hashtb))
5218               (setcdr entry (cons number (cdr entry)))
5219             (gnus-sethash group (cons number nil) xref-hashtb)))))
5220     (and start xref-hashtb)))
5221
5222 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5223   "Look through all the headers and mark the Xrefs as read."
5224   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5225         name entry info xref-hashtb idlist method nth4)
5226     (save-excursion
5227       (set-buffer gnus-group-buffer)
5228       (when (setq xref-hashtb
5229                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5230         (mapatoms
5231          (lambda (group)
5232            (unless (string= from-newsgroup (setq name (symbol-name group)))
5233              (setq idlist (symbol-value group))
5234              ;; Dead groups are not updated.
5235              (and (prog1
5236                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5237                             info (nth 2 entry))
5238                     (when (stringp (setq nth4 (gnus-info-method info)))
5239                       (setq nth4 (gnus-server-to-method nth4))))
5240                   ;; Only do the xrefs if the group has the same
5241                   ;; select method as the group we have just read.
5242                   (or (gnus-methods-equal-p
5243                        nth4 (gnus-find-method-for-group from-newsgroup))
5244                       virtual
5245                       (equal nth4 (setq method (gnus-find-method-for-group
5246                                                 from-newsgroup)))
5247                       (and (equal (car nth4) (car method))
5248                            (equal (nth 1 nth4) (nth 1 method))))
5249                   gnus-use-cross-reference
5250                   (or (not (eq gnus-use-cross-reference t))
5251                       virtual
5252                       ;; Only do cross-references on subscribed
5253                       ;; groups, if that is what is wanted.
5254                       (<= (gnus-info-level info) gnus-level-subscribed))
5255                   (gnus-group-make-articles-read name idlist))))
5256          xref-hashtb)))))
5257
5258 (defun gnus-compute-read-articles (group articles)
5259   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5260          (info (nth 2 entry))
5261          (active (gnus-active group))
5262          ninfo)
5263     (when entry
5264       ;; First peel off all invalid article numbers.
5265       (when active
5266         (let ((ids articles)
5267               id first)
5268           (while (setq id (pop ids))
5269             (when (and first (> id (cdr active)))
5270               ;; We'll end up in this situation in one particular
5271               ;; obscure situation.  If you re-scan a group and get
5272               ;; a new article that is cross-posted to a different
5273               ;; group that has not been re-scanned, you might get
5274               ;; crossposted article that has a higher number than
5275               ;; Gnus believes possible.  So we re-activate this
5276               ;; group as well.  This might mean doing the
5277               ;; crossposting thingy will *increase* the number
5278               ;; of articles in some groups.  Tsk, tsk.
5279               (setq active (or (gnus-activate-group group) active)))
5280             (when (or (> id (cdr active))
5281                       (< id (car active)))
5282               (setq articles (delq id articles))))))
5283       ;; If the read list is nil, we init it.
5284       (if (and active
5285                (null (gnus-info-read info))
5286                (> (car active) 1))
5287           (setq ninfo (cons 1 (1- (car active))))
5288         (setq ninfo (gnus-info-read info)))
5289       ;; Then we add the read articles to the range.
5290       (gnus-add-to-range
5291        ninfo (setq articles (sort articles '<))))))
5292
5293 (defun gnus-group-make-articles-read (group articles)
5294   "Update the info of GROUP to say that ARTICLES are read."
5295   (let* ((num 0)
5296          (entry (gnus-gethash group gnus-newsrc-hashtb))
5297          (info (nth 2 entry))
5298          (active (gnus-active group))
5299          range)
5300     (when entry
5301       (setq range (gnus-compute-read-articles group articles))
5302       (save-excursion
5303         (set-buffer gnus-group-buffer)
5304         (gnus-undo-register
5305           `(progn
5306              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5307              (gnus-info-set-read ',info ',(gnus-info-read info))
5308              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5309              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5310              (gnus-group-update-group ,group t))))
5311       ;; Add the read articles to the range.
5312       (gnus-info-set-read info range)
5313       (gnus-request-set-mark group (list (list range 'add '(read))))
5314       ;; Then we have to re-compute how many unread
5315       ;; articles there are in this group.
5316       (when active
5317         (cond
5318          ((not range)
5319           (setq num (- (1+ (cdr active)) (car active))))
5320          ((not (listp (cdr range)))
5321           (setq num (- (cdr active) (- (1+ (cdr range))
5322                                        (car range)))))
5323          (t
5324           (while range
5325             (if (numberp (car range))
5326                 (setq num (1+ num))
5327               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5328             (setq range (cdr range)))
5329           (setq num (- (cdr active) num))))
5330         ;; Update the number of unread articles.
5331         (setcar entry num)
5332         ;; Update the group buffer.
5333         (gnus-group-update-group group t)))))
5334
5335 (defvar gnus-newsgroup-none-id 0)
5336
5337 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5338   (let ((cur nntp-server-buffer)
5339         (dependencies
5340          (or dependencies
5341              (save-excursion (set-buffer gnus-summary-buffer)
5342                              gnus-newsgroup-dependencies)))
5343         headers id end ref
5344         (mail-parse-charset gnus-newsgroup-charset)
5345         (mail-parse-ignored-charsets
5346          (save-excursion (condition-case nil
5347                              (set-buffer gnus-summary-buffer)
5348                            (error))
5349                          gnus-newsgroup-ignored-charsets)))
5350     (save-excursion
5351       (set-buffer nntp-server-buffer)
5352       ;; Translate all TAB characters into SPACE characters.
5353       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5354       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5355       (gnus-run-hooks 'gnus-parse-headers-hook)
5356       (let ((case-fold-search t)
5357             in-reply-to header p lines chars ctype)
5358         (goto-char (point-min))
5359         ;; Search to the beginning of the next header.  Error messages
5360         ;; do not begin with 2 or 3.
5361         (while (re-search-forward "^[23][0-9]+ " nil t)
5362           (setq id nil
5363                 ref nil)
5364           ;; This implementation of this function, with nine
5365           ;; search-forwards instead of the one re-search-forward and
5366           ;; a case (which basically was the old function) is actually
5367           ;; about twice as fast, even though it looks messier.  You
5368           ;; can't have everything, I guess.  Speed and elegance
5369           ;; doesn't always go hand in hand.
5370           (setq
5371            header
5372            (make-full-mail-header
5373             ;; Number.
5374             (prog1
5375                 (read cur)
5376               (end-of-line)
5377               (setq p (point))
5378               (narrow-to-region (point)
5379                                 (or (and (search-forward "\n.\n" nil t)
5380                                          (- (point) 2))
5381                                     (point))))
5382             ;; Subject.
5383             (progn
5384               (goto-char p)
5385               (if (search-forward "\nsubject:" nil t)
5386                   (nnheader-header-value)
5387                 "(none)"))
5388             ;; From.
5389             (progn
5390               (goto-char p)
5391               (if (search-forward "\nfrom:" nil t)
5392                   (nnheader-header-value)
5393                 "(nobody)"))
5394             ;; Date.
5395             (progn
5396               (goto-char p)
5397               (if (search-forward "\ndate:" nil t)
5398                   (nnheader-header-value) ""))
5399             ;; Message-ID.
5400             (progn
5401               (goto-char p)
5402               (setq id (if (re-search-forward
5403                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5404                            ;; We do it this way to make sure the Message-ID
5405                            ;; is (somewhat) syntactically valid.
5406                            (buffer-substring (match-beginning 1)
5407                                              (match-end 1))
5408                          ;; If there was no message-id, we just fake one
5409                          ;; to make subsequent routines simpler.
5410                          (nnheader-generate-fake-message-id))))
5411             ;; References.
5412             (progn
5413               (goto-char p)
5414               (if (search-forward "\nreferences:" nil t)
5415                   (progn
5416                     (setq end (point))
5417                     (prog1
5418                         (nnheader-header-value)
5419                       (setq ref
5420                             (buffer-substring
5421                              (progn
5422                                ;; (end-of-line)
5423                                (search-backward ">" end t)
5424                                (1+ (point)))
5425                              (progn
5426                                (search-backward "<" end t)
5427                                (point))))))
5428                 ;; Get the references from the in-reply-to header if there
5429                 ;; were no references and the in-reply-to header looks
5430                 ;; promising.
5431                 (if (and (search-forward "\nin-reply-to:" nil t)
5432                          (setq in-reply-to (nnheader-header-value))
5433                          (string-match "<[^>]+>" in-reply-to))
5434                     (let (ref2)
5435                       (setq ref (substring in-reply-to (match-beginning 0)
5436                                            (match-end 0)))
5437                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5438                         (setq ref2 (substring in-reply-to (match-beginning 0)
5439                                               (match-end 0)))
5440                         (when (> (length ref2) (length ref))
5441                           (setq ref ref2)))
5442                       ref)
5443                   (setq ref nil))))
5444             ;; Chars.
5445             (progn
5446               (goto-char p)
5447               (if (search-forward "\nchars: " nil t)
5448                   (if (numberp (setq chars (ignore-errors (read cur))))
5449                       chars -1)
5450                 -1))
5451             ;; Lines.
5452             (progn
5453               (goto-char p)
5454               (if (search-forward "\nlines: " nil t)
5455                   (if (numberp (setq lines (ignore-errors (read cur))))
5456                       lines -1)
5457                 -1))
5458             ;; Xref.
5459             (progn
5460               (goto-char p)
5461               (and (search-forward "\nxref:" nil t)
5462                    (nnheader-header-value)))
5463             ;; Extra.
5464             (when gnus-extra-headers
5465               (let ((extra gnus-extra-headers)
5466                     out)
5467                 (while extra
5468                   (goto-char p)
5469                   (when (search-forward
5470                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5471                     (push (cons (car extra) (nnheader-header-value)) out))
5472                   (pop extra))
5473                 out))))
5474           (goto-char p)
5475           (if (and (search-forward "\ncontent-type: " nil t)
5476                    (setq ctype (nnheader-header-value)))
5477               (mime-entity-set-content-type-internal
5478                header (mime-parse-Content-Type ctype)))
5479           (when (equal id ref)
5480             (setq ref nil))
5481
5482           (when gnus-alter-header-function
5483             (funcall gnus-alter-header-function header)
5484             (setq id (mail-header-id header)
5485                   ref (gnus-parent-id (mail-header-references header))))
5486
5487           (when (setq header
5488                       (gnus-dependencies-add-header
5489                        header dependencies force-new))
5490             (push header headers))
5491           (goto-char (point-max))
5492           (widen))
5493         (nreverse headers)))))
5494
5495 ;; Goes through the xover lines and returns a list of vectors
5496 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5497                                                   force-new dependencies
5498                                                   group also-fetch-heads)
5499   "Parse the news overview data in the server buffer.
5500 Return a list of headers that match SEQUENCE (see
5501 `nntp-retrieve-headers')."
5502   ;; Get the Xref when the users reads the articles since most/some
5503   ;; NNTP servers do not include Xrefs when using XOVER.
5504   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5505   (let ((mail-parse-charset gnus-newsgroup-charset)
5506         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5507         (cur nntp-server-buffer)
5508         (dependencies (or dependencies gnus-newsgroup-dependencies))
5509         (allp (cond
5510                ((eq gnus-read-all-available-headers t)
5511                 t)
5512                ((stringp gnus-read-all-available-headers)
5513                 (string-match gnus-read-all-available-headers group))
5514                (t
5515                 nil)))
5516         number headers header)
5517     (save-excursion
5518       (set-buffer nntp-server-buffer)
5519       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5520       ;; Allow the user to mangle the headers before parsing them.
5521       (gnus-run-hooks 'gnus-parse-headers-hook)
5522       (goto-char (point-min))
5523       (gnus-parse-without-error
5524         (while (and (or sequence allp)
5525                     (not (eobp)))
5526           (setq number (read cur))
5527           (when (not allp)
5528             (while (and sequence
5529                         (< (car sequence) number))
5530               (setq sequence (cdr sequence))))
5531           (when (and (or allp
5532                          (and sequence
5533                               (eq number (car sequence))))
5534                      (progn
5535                        (setq sequence (cdr sequence))
5536                        (setq header (inline
5537                                       (gnus-nov-parse-line
5538                                        number dependencies force-new)))))
5539             (push header headers))
5540           (forward-line 1)))
5541       ;; A common bug in inn is that if you have posted an article and
5542       ;; then retrieves the active file, it will answer correctly --
5543       ;; the new article is included.  However, a NOV entry for the
5544       ;; article may not have been generated yet, so this may fail.
5545       ;; We work around this problem by retrieving the last few
5546       ;; headers using HEAD.
5547       (if (or (not also-fetch-heads)
5548               (not sequence))
5549           ;; We (probably) got all the headers.
5550           (nreverse headers)
5551         (let ((gnus-nov-is-evil t))
5552           (nconc
5553            (nreverse headers)
5554            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5555              (gnus-get-newsgroup-headers))))))))
5556
5557 (defun gnus-article-get-xrefs ()
5558   "Fill in the Xref value in `gnus-current-headers', if necessary.
5559 This is meant to be called in `gnus-article-internal-prepare-hook'."
5560   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5561                                  gnus-current-headers)))
5562     (or (not gnus-use-cross-reference)
5563         (not headers)
5564         (and (mail-header-xref headers)
5565              (not (string= (mail-header-xref headers) "")))
5566         (let ((case-fold-search t)
5567               xref)
5568           (save-restriction
5569             (nnheader-narrow-to-headers)
5570             (goto-char (point-min))
5571             (when (or (and (not (eobp))
5572                            (eq (downcase (char-after)) ?x)
5573                            (looking-at "Xref:"))
5574                       (search-forward "\nXref:" nil t))
5575               (goto-char (1+ (match-end 0)))
5576               (setq xref (buffer-substring (point)
5577                                            (progn (end-of-line) (point))))
5578               (mail-header-set-xref headers xref)))))))
5579
5580 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5581   "Find article ID and insert the summary line for that article.
5582 OLD-HEADER can either be a header or a line number to insert
5583 the subject line on."
5584   (let* ((line (and (numberp old-header) old-header))
5585          (old-header (and (vectorp old-header) old-header))
5586          (header (cond ((and old-header use-old-header)
5587                         old-header)
5588                        ((and (numberp id)
5589                              (gnus-number-to-header id))
5590                         (gnus-number-to-header id))
5591                        (t
5592                         (gnus-read-header id))))
5593          (number (and (numberp id) id))
5594          d)
5595     (when header
5596       ;; Rebuild the thread that this article is part of and go to the
5597       ;; article we have fetched.
5598       (when (and (not gnus-show-threads)
5599                  old-header)
5600         (when (and number
5601                    (setq d (gnus-data-find (mail-header-number old-header))))
5602           (goto-char (gnus-data-pos d))
5603           (gnus-data-remove
5604            number
5605            (- (gnus-point-at-bol)
5606               (prog1
5607                   (1+ (gnus-point-at-eol))
5608                 (gnus-delete-line))))))
5609       (when old-header
5610         (mail-header-set-number header (mail-header-number old-header)))
5611       (setq gnus-newsgroup-sparse
5612             (delq (setq number (mail-header-number header))
5613                   gnus-newsgroup-sparse))
5614       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5615       (push number gnus-newsgroup-limit)
5616       (gnus-rebuild-thread (mail-header-id header) line)
5617       (gnus-summary-goto-subject number nil t))
5618     (when (and (numberp number)
5619                (> number 0))
5620       ;; We have to update the boundaries even if we can't fetch the
5621       ;; article if ID is a number -- so that the next `P' or `N'
5622       ;; command will fetch the previous (or next) article even
5623       ;; if the one we tried to fetch this time has been canceled.
5624       (when (> number gnus-newsgroup-end)
5625         (setq gnus-newsgroup-end number))
5626       (when (< number gnus-newsgroup-begin)
5627         (setq gnus-newsgroup-begin number))
5628       (setq gnus-newsgroup-unselected
5629             (delq number gnus-newsgroup-unselected)))
5630     ;; Report back a success?
5631     (and header (mail-header-number header))))
5632
5633 ;;; Process/prefix in the summary buffer
5634
5635 (defun gnus-summary-work-articles (n)
5636   "Return a list of articles to be worked upon.
5637 The prefix argument, the list of process marked articles, and the
5638 current article will be taken into consideration."
5639   (save-excursion
5640     (set-buffer gnus-summary-buffer)
5641     (cond
5642      (n
5643       ;; A numerical prefix has been given.
5644       (setq n (prefix-numeric-value n))
5645       (let ((backward (< n 0))
5646             (n (abs (prefix-numeric-value n)))
5647             articles article)
5648         (save-excursion
5649           (while
5650               (and (> n 0)
5651                    (push (setq article (gnus-summary-article-number))
5652                          articles)
5653                    (if backward
5654                        (gnus-summary-find-prev nil article)
5655                      (gnus-summary-find-next nil article)))
5656             (decf n)))
5657         (nreverse articles)))
5658      ((and (gnus-region-active-p) (mark))
5659       (message "region active")
5660       ;; Work on the region between point and mark.
5661       (let ((max (max (point) (mark)))
5662             articles article)
5663         (save-excursion
5664           (goto-char (min (point) (mark)))
5665           (while
5666               (and
5667                (push (setq article (gnus-summary-article-number)) articles)
5668                (gnus-summary-find-next nil article)
5669                (< (point) max)))
5670           (nreverse articles))))
5671      (gnus-newsgroup-processable
5672       ;; There are process-marked articles present.
5673       ;; Save current state.
5674       (gnus-summary-save-process-mark)
5675       ;; Return the list.
5676       (reverse gnus-newsgroup-processable))
5677      (t
5678       ;; Just return the current article.
5679       (list (gnus-summary-article-number))))))
5680
5681 (defmacro gnus-summary-iterate (arg &rest forms)
5682   "Iterate over the process/prefixed articles and do FORMS.
5683 ARG is the interactive prefix given to the command.  FORMS will be
5684 executed with point over the summary line of the articles."
5685   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5686     `(let ((,articles (gnus-summary-work-articles ,arg)))
5687        (while ,articles
5688          (gnus-summary-goto-subject (car ,articles))
5689          ,@forms
5690          (pop ,articles)))))
5691
5692 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5693 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5694
5695 (defun gnus-summary-save-process-mark ()
5696   "Push the current set of process marked articles on the stack."
5697   (interactive)
5698   (push (copy-sequence gnus-newsgroup-processable)
5699         gnus-newsgroup-process-stack))
5700
5701 (defun gnus-summary-kill-process-mark ()
5702   "Push the current set of process marked articles on the stack and unmark."
5703   (interactive)
5704   (gnus-summary-save-process-mark)
5705   (gnus-summary-unmark-all-processable))
5706
5707 (defun gnus-summary-yank-process-mark ()
5708   "Pop the last process mark state off the stack and restore it."
5709   (interactive)
5710   (unless gnus-newsgroup-process-stack
5711     (error "Empty mark stack"))
5712   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5713
5714 (defun gnus-summary-process-mark-set (set)
5715   "Make SET into the current process marked articles."
5716   (gnus-summary-unmark-all-processable)
5717   (while set
5718     (gnus-summary-set-process-mark (pop set))))
5719
5720 ;;; Searching and stuff
5721
5722 (defun gnus-summary-search-group (&optional backward use-level)
5723   "Search for next unread newsgroup.
5724 If optional argument BACKWARD is non-nil, search backward instead."
5725   (save-excursion
5726     (set-buffer gnus-group-buffer)
5727     (when (gnus-group-search-forward
5728            backward nil (if use-level (gnus-group-group-level) nil))
5729       (gnus-group-group-name))))
5730
5731 (defun gnus-summary-best-group (&optional exclude-group)
5732   "Find the name of the best unread group.
5733 If EXCLUDE-GROUP, do not go to this group."
5734   (save-excursion
5735     (set-buffer gnus-group-buffer)
5736     (save-excursion
5737       (gnus-group-best-unread-group exclude-group))))
5738
5739 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5740   (if backward (gnus-summary-find-prev)
5741     (let* ((dummy (gnus-summary-article-intangible-p))
5742            (article (or article (gnus-summary-article-number)))
5743            (arts (gnus-data-find-list article))
5744            result)
5745       (when (and (not dummy)
5746                  (or (not gnus-summary-check-current)
5747                      (not unread)
5748                      (not (gnus-data-unread-p (car arts)))))
5749         (setq arts (cdr arts)))
5750       (when (setq result
5751                   (if unread
5752                       (progn
5753                         (while arts
5754                           (when (or (and undownloaded
5755                                          (eq gnus-undownloaded-mark
5756                                              (gnus-data-mark (car arts))))
5757                                     (gnus-data-unread-p (car arts)))
5758                             (setq result (car arts)
5759                                   arts nil))
5760                           (setq arts (cdr arts)))
5761                         result)
5762                     (car arts)))
5763         (goto-char (gnus-data-pos result))
5764         (gnus-data-number result)))))
5765
5766 (defun gnus-summary-find-prev (&optional unread article)
5767   (let* ((eobp (eobp))
5768          (article (or article (gnus-summary-article-number)))
5769          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5770          result)
5771     (when (and (not eobp)
5772                (or (not gnus-summary-check-current)
5773                    (not unread)
5774                    (not (gnus-data-unread-p (car arts)))))
5775       (setq arts (cdr arts)))
5776     (when (setq result
5777                 (if unread
5778                     (progn
5779                       (while arts
5780                         (when (gnus-data-unread-p (car arts))
5781                           (setq result (car arts)
5782                                 arts nil))
5783                         (setq arts (cdr arts)))
5784                       result)
5785                   (car arts)))
5786       (goto-char (gnus-data-pos result))
5787       (gnus-data-number result))))
5788
5789 (defun gnus-summary-find-subject (subject &optional unread backward article)
5790   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5791          (article (or article (gnus-summary-article-number)))
5792          (articles (gnus-data-list backward))
5793          (arts (gnus-data-find-list article articles))
5794          result)
5795     (when (or (not gnus-summary-check-current)
5796               (not unread)
5797               (not (gnus-data-unread-p (car arts))))
5798       (setq arts (cdr arts)))
5799     (while arts
5800       (and (or (not unread)
5801                (gnus-data-unread-p (car arts)))
5802            (vectorp (gnus-data-header (car arts)))
5803            (gnus-subject-equal
5804             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5805            (setq result (car arts)
5806                  arts nil))
5807       (setq arts (cdr arts)))
5808     (and result
5809          (goto-char (gnus-data-pos result))
5810          (gnus-data-number result))))
5811
5812 (defun gnus-summary-search-forward (&optional unread subject backward)
5813   "Search forward for an article.
5814 If UNREAD, look for unread articles.  If SUBJECT, look for
5815 articles with that subject.  If BACKWARD, search backward instead."
5816   (cond (subject (gnus-summary-find-subject subject unread backward))
5817         (backward (gnus-summary-find-prev unread))
5818         (t (gnus-summary-find-next unread))))
5819
5820 (defun gnus-recenter (&optional n)
5821   "Center point in window and redisplay frame.
5822 Also do horizontal recentering."
5823   (interactive "P")
5824   (when (and gnus-auto-center-summary
5825              (not (eq gnus-auto-center-summary 'vertical)))
5826     (gnus-horizontal-recenter))
5827   (recenter n))
5828
5829 (defun gnus-summary-recenter ()
5830   "Center point in the summary window.
5831 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5832 displayed, no centering will be performed."
5833   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5834   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5835   (interactive)
5836   (let* ((top (cond ((< (window-height) 4) 0)
5837                     ((< (window-height) 7) 1)
5838                     (t (if (numberp gnus-auto-center-summary)
5839                            gnus-auto-center-summary
5840                          2))))
5841          (height (1- (window-height)))
5842          (bottom (save-excursion (goto-char (point-max))
5843                                  (forward-line (- height))
5844                                  (point)))
5845          (window (get-buffer-window (current-buffer))))
5846     ;; The user has to want it.
5847     (when gnus-auto-center-summary
5848       (when (get-buffer-window gnus-article-buffer)
5849         ;; Only do recentering when the article buffer is displayed,
5850         ;; Set the window start to either `bottom', which is the biggest
5851         ;; possible valid number, or the second line from the top,
5852         ;; whichever is the least.
5853         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5854           (if (> bottom top-pos)
5855               ;; Keep the second line from the top visible
5856               (set-window-start window top-pos t)
5857             ;; Try to keep the bottom line visible; if it's partially
5858             ;; obscured, either scroll one more line to make it fully
5859             ;; visible, or revert to using TOP-POS.
5860             (save-excursion
5861               (goto-char (point-max))
5862               (forward-line -1)
5863               (let ((last-line-start (point)))
5864                 (goto-char bottom)
5865                 (set-window-start window (point) t)
5866                 (when (not (pos-visible-in-window-p last-line-start window))
5867                   (forward-line 1)
5868                   (set-window-start window (min (point) top-pos) t)))))))
5869       ;; Do horizontal recentering while we're at it.
5870       (when (and (get-buffer-window (current-buffer) t)
5871                  (not (eq gnus-auto-center-summary 'vertical)))
5872         (let ((selected (selected-window)))
5873           (select-window (get-buffer-window (current-buffer) t))
5874           (gnus-summary-position-point)
5875           (gnus-horizontal-recenter)
5876           (select-window selected))))))
5877
5878 (defun gnus-summary-jump-to-group (newsgroup)
5879   "Move point to NEWSGROUP in group mode buffer."
5880   ;; Keep update point of group mode buffer if visible.
5881   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5882       (save-window-excursion
5883         ;; Take care of tree window mode.
5884         (when (get-buffer-window gnus-group-buffer)
5885           (pop-to-buffer gnus-group-buffer))
5886         (gnus-group-jump-to-group newsgroup))
5887     (save-excursion
5888       ;; Take care of tree window mode.
5889       (if (get-buffer-window gnus-group-buffer)
5890           (pop-to-buffer gnus-group-buffer)
5891         (set-buffer gnus-group-buffer))
5892       (gnus-group-jump-to-group newsgroup))))
5893
5894 ;; This function returns a list of article numbers based on the
5895 ;; difference between the ranges of read articles in this group and
5896 ;; the range of active articles.
5897 (defun gnus-list-of-unread-articles (group)
5898   (let* ((read (gnus-info-read (gnus-get-info group)))
5899          (active (or (gnus-active group) (gnus-activate-group group)))
5900          (last (cdr active))
5901          first nlast unread)
5902     ;; If none are read, then all are unread.
5903     (if (not read)
5904         (setq first (car active))
5905       ;; If the range of read articles is a single range, then the
5906       ;; first unread article is the article after the last read
5907       ;; article.  Sounds logical, doesn't it?
5908       (if (and (not (listp (cdr read)))
5909                (or (< (car read) (car active))
5910                    (progn (setq read (list read))
5911                           nil)))
5912           (setq first (max (car active) (1+ (cdr read))))
5913         ;; `read' is a list of ranges.
5914         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5915                                   (caar read)))
5916                   1)
5917           (setq first (car active)))
5918         (while read
5919           (when first
5920             (while (< first nlast)
5921               (push first unread)
5922               (setq first (1+ first))))
5923           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5924           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5925           (setq read (cdr read)))))
5926     ;; And add the last unread articles.
5927     (while (<= first last)
5928       (push first unread)
5929       (setq first (1+ first)))
5930     ;; Return the list of unread articles.
5931     (delq 0 (nreverse unread))))
5932
5933 (defun gnus-list-of-read-articles (group)
5934   "Return a list of unread, unticked and non-dormant articles."
5935   (let* ((info (gnus-get-info group))
5936          (marked (gnus-info-marks info))
5937          (active (gnus-active group)))
5938     (and info active
5939          (gnus-set-difference
5940           (gnus-sorted-complement
5941            (gnus-uncompress-range active)
5942            (gnus-list-of-unread-articles group))
5943           (append
5944            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5945            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5946
5947 ;; Various summary commands
5948
5949 (defun gnus-summary-select-article-buffer ()
5950   "Reconfigure windows to show article buffer."
5951   (interactive)
5952   (if (not (gnus-buffer-live-p gnus-article-buffer))
5953       (error "There is no article buffer for this summary buffer")
5954     (gnus-configure-windows 'article)
5955     (select-window (get-buffer-window gnus-article-buffer))))
5956
5957 (defun gnus-summary-universal-argument (arg)
5958   "Perform any operation on all articles that are process/prefixed."
5959   (interactive "P")
5960   (let ((articles (gnus-summary-work-articles arg))
5961         func article)
5962     (if (eq
5963          (setq
5964           func
5965           (key-binding
5966            (read-key-sequence
5967             (substitute-command-keys
5968              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5969          'undefined)
5970         (gnus-error 1 "Undefined key")
5971       (save-excursion
5972         (while articles
5973           (gnus-summary-goto-subject (setq article (pop articles)))
5974           (let (gnus-newsgroup-processable)
5975             (command-execute func))
5976           (gnus-summary-remove-process-mark article)))))
5977   (gnus-summary-position-point))
5978
5979 (defun gnus-summary-toggle-truncation (&optional arg)
5980   "Toggle truncation of summary lines.
5981 With arg, turn line truncation on iff arg is positive."
5982   (interactive "P")
5983   (setq truncate-lines
5984         (if (null arg) (not truncate-lines)
5985           (> (prefix-numeric-value arg) 0)))
5986   (redraw-display))
5987
5988 (defun gnus-summary-reselect-current-group (&optional all rescan)
5989   "Exit and then reselect the current newsgroup.
5990 The prefix argument ALL means to select all articles."
5991   (interactive "P")
5992   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5993     (error "Ephemeral groups can't be reselected"))
5994   (let ((current-subject (gnus-summary-article-number))
5995         (group gnus-newsgroup-name))
5996     (setq gnus-newsgroup-begin nil)
5997     (gnus-summary-exit)
5998     ;; We have to adjust the point of group mode buffer because
5999     ;; point was moved to the next unread newsgroup by exiting.
6000     (gnus-summary-jump-to-group group)
6001     (when rescan
6002       (save-excursion
6003         (save-window-excursion
6004           ;; Don't show group contents.
6005           (set-window-start (selected-window) (point-max))
6006           (gnus-group-get-new-news-this-group 1))))
6007     (gnus-group-read-group all t)
6008     (gnus-summary-goto-subject current-subject nil t)))
6009
6010 (defun gnus-summary-rescan-group (&optional all)
6011   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6012   (interactive "P")
6013   (gnus-summary-reselect-current-group all t))
6014
6015 (defun gnus-summary-update-info (&optional non-destructive)
6016   (save-excursion
6017     (let ((group gnus-newsgroup-name))
6018       (when group
6019         (when gnus-newsgroup-kill-headers
6020           (setq gnus-newsgroup-killed
6021                 (gnus-compress-sequence
6022                  (nconc
6023                   (gnus-set-sorted-intersection
6024                    (gnus-uncompress-range gnus-newsgroup-killed)
6025                    (setq gnus-newsgroup-unselected
6026                          (sort gnus-newsgroup-unselected '<)))
6027                   (setq gnus-newsgroup-unreads
6028                         (sort gnus-newsgroup-unreads '<)))
6029                  t)))
6030         (unless (listp (cdr gnus-newsgroup-killed))
6031           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6032         (let ((headers gnus-newsgroup-headers))
6033           ;; Set the new ranges of read articles.
6034           (save-excursion
6035             (set-buffer gnus-group-buffer)
6036             (gnus-undo-force-boundary))
6037           (gnus-update-read-articles
6038            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
6039           ;; Set the current article marks.
6040           (let ((gnus-newsgroup-scored
6041                  (if (and (not gnus-save-score)
6042                           (not non-destructive))
6043                      nil
6044                    gnus-newsgroup-scored)))
6045             (save-excursion
6046               (gnus-update-marks)))
6047           ;; Do the cross-ref thing.
6048           (when gnus-use-cross-reference
6049             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6050           ;; Do not switch windows but change the buffer to work.
6051           (set-buffer gnus-group-buffer)
6052           (unless (gnus-ephemeral-group-p group)
6053             (gnus-group-update-group group)))))))
6054
6055 (defun gnus-summary-save-newsrc (&optional force)
6056   "Save the current number of read/marked articles in the dribble buffer.
6057 The dribble buffer will then be saved.
6058 If FORCE (the prefix), also save the .newsrc file(s)."
6059   (interactive "P")
6060   (gnus-summary-update-info t)
6061   (if force
6062       (gnus-save-newsrc-file)
6063     (gnus-dribble-save)))
6064
6065 (defun gnus-summary-exit (&optional temporary)
6066   "Exit reading current newsgroup, and then return to group selection mode.
6067 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6068   (interactive)
6069   (gnus-set-global-variables)
6070   (gnus-kill-save-kill-buffer)
6071   (gnus-async-halt-prefetch)
6072   (let* ((group gnus-newsgroup-name)
6073          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6074          (mode major-mode)
6075          (group-point nil)
6076          (buf (current-buffer)))
6077     (unless quit-config
6078       ;; Do adaptive scoring, and possibly save score files.
6079       (when gnus-newsgroup-adaptive
6080         (gnus-score-adaptive))
6081       (when gnus-use-scoring
6082         (gnus-score-save)))
6083     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6084     ;; If we have several article buffers, we kill them at exit.
6085     (unless gnus-single-article-buffer
6086       (gnus-kill-buffer gnus-original-article-buffer)
6087       (setq gnus-article-current nil))
6088     (when gnus-use-cache
6089       (gnus-cache-possibly-remove-articles)
6090       (gnus-cache-save-buffers))
6091     (gnus-async-prefetch-remove-group group)
6092     (when gnus-suppress-duplicates
6093       (gnus-dup-enter-articles))
6094     (when gnus-use-trees
6095       (gnus-tree-close group))
6096     (when gnus-use-cache
6097       (gnus-cache-write-active))
6098     ;; Remove entries for this group.
6099     (nnmail-purge-split-history (gnus-group-real-name group))
6100     ;; Make all changes in this group permanent.
6101     (unless quit-config
6102       (gnus-run-hooks 'gnus-exit-group-hook)
6103       (gnus-summary-update-info))
6104     (gnus-close-group group)
6105     ;; Make sure where we were, and go to next newsgroup.
6106     (set-buffer gnus-group-buffer)
6107     (unless quit-config
6108       (gnus-group-jump-to-group group))
6109     (gnus-run-hooks 'gnus-summary-exit-hook)
6110     (unless (or quit-config
6111                 ;; If this group has disappeared from the summary
6112                 ;; buffer, don't skip forwards.
6113                 (not (string= group (gnus-group-group-name))))
6114       (gnus-group-next-unread-group 1))
6115     (setq group-point (point))
6116     (if temporary
6117         nil                             ;Nothing to do.
6118       ;; If we have several article buffers, we kill them at exit.
6119       (unless gnus-single-article-buffer
6120         (gnus-kill-buffer gnus-article-buffer)
6121         (gnus-kill-buffer gnus-original-article-buffer)
6122         (setq gnus-article-current nil))
6123       (set-buffer buf)
6124       (if (not gnus-kill-summary-on-exit)
6125           (progn
6126             (gnus-deaden-summary)
6127             (setq mode nil))
6128         ;; We set all buffer-local variables to nil.  It is unclear why
6129         ;; this is needed, but if we don't, buffer-local variables are
6130         ;; not garbage-collected, it seems.  This would the lead to en
6131         ;; ever-growing Emacs.
6132         (gnus-summary-clear-local-variables)
6133         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6134           (gnus-summary-clear-local-variables))
6135         (when (get-buffer gnus-article-buffer)
6136           (bury-buffer gnus-article-buffer))
6137         ;; We clear the global counterparts of the buffer-local
6138         ;; variables as well, just to be on the safe side.
6139         (set-buffer gnus-group-buffer)
6140         (gnus-summary-clear-local-variables)
6141         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6142           (gnus-summary-clear-local-variables)))
6143       (setq gnus-current-select-method gnus-select-method)
6144       (pop-to-buffer gnus-group-buffer)
6145       (if (not quit-config)
6146           (progn
6147             (goto-char group-point)
6148             (gnus-configure-windows 'group 'force)
6149             (unless (pos-visible-in-window-p)
6150               (forward-line (/ (static-if (featurep 'xemacs)
6151                                    (window-displayed-height)
6152                                  (1- (window-height)))
6153                                -2))
6154               (set-window-start (selected-window) (point))
6155               (goto-char group-point)))
6156         (gnus-handle-ephemeral-exit quit-config))
6157       ;; Return to group mode buffer.
6158       (when (eq mode 'gnus-summary-mode)
6159         (gnus-kill-buffer buf))
6160       ;; Clear the current group name.
6161       (unless quit-config
6162         (setq gnus-newsgroup-name nil)))))
6163
6164 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6165 (defun gnus-summary-exit-no-update (&optional no-questions)
6166   "Quit reading current newsgroup without updating read article info."
6167   (interactive)
6168   (let* ((group gnus-newsgroup-name)
6169          (quit-config (gnus-group-quit-config group)))
6170     (when (or no-questions
6171               gnus-expert-user
6172               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6173       (gnus-async-halt-prefetch)
6174       (mapcar 'funcall
6175               (delq 'gnus-summary-expire-articles
6176                     (copy-sequence gnus-summary-prepare-exit-hook)))
6177       ;; If we have several article buffers, we kill them at exit.
6178       (unless gnus-single-article-buffer
6179         (gnus-kill-buffer gnus-article-buffer)
6180         (gnus-kill-buffer gnus-original-article-buffer)
6181         (setq gnus-article-current nil))
6182       (if (not gnus-kill-summary-on-exit)
6183           (gnus-deaden-summary)
6184         (gnus-close-group group)
6185         (gnus-summary-clear-local-variables)
6186         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6187           (gnus-summary-clear-local-variables))
6188         (set-buffer gnus-group-buffer)
6189         (gnus-summary-clear-local-variables)
6190         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6191           (gnus-summary-clear-local-variables))
6192         (when (get-buffer gnus-summary-buffer)
6193           (kill-buffer gnus-summary-buffer)))
6194       (unless gnus-single-article-buffer
6195         (setq gnus-article-current nil))
6196       (when gnus-use-trees
6197         (gnus-tree-close group))
6198       (gnus-async-prefetch-remove-group group)
6199       (when (get-buffer gnus-article-buffer)
6200         (bury-buffer gnus-article-buffer))
6201       ;; Return to the group buffer.
6202       (gnus-configure-windows 'group 'force)
6203       ;; Clear the current group name.
6204       (setq gnus-newsgroup-name nil)
6205       (when (equal (gnus-group-group-name) group)
6206         (gnus-group-next-unread-group 1))
6207       (when quit-config
6208         (gnus-handle-ephemeral-exit quit-config)))))
6209
6210 (defun gnus-handle-ephemeral-exit (quit-config)
6211   "Handle movement when leaving an ephemeral group.
6212 The state which existed when entering the ephemeral is reset."
6213   (if (not (buffer-name (car quit-config)))
6214       (gnus-configure-windows 'group 'force)
6215     (set-buffer (car quit-config))
6216     (cond ((eq major-mode 'gnus-summary-mode)
6217            (gnus-set-global-variables))
6218           ((eq major-mode 'gnus-article-mode)
6219            (save-excursion
6220              ;; The `gnus-summary-buffer' variable may point
6221              ;; to the old summary buffer when using a single
6222              ;; article buffer.
6223              (unless (gnus-buffer-live-p gnus-summary-buffer)
6224                (set-buffer gnus-group-buffer))
6225              (set-buffer gnus-summary-buffer)
6226              (gnus-set-global-variables))))
6227     (if (or (eq (cdr quit-config) 'article)
6228             (eq (cdr quit-config) 'pick))
6229         (progn
6230           ;; The current article may be from the ephemeral group
6231           ;; thus it is best that we reload this article
6232           (gnus-summary-show-article)
6233           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6234               (gnus-configure-windows 'pick 'force)
6235             (gnus-configure-windows (cdr quit-config) 'force)))
6236       (gnus-configure-windows (cdr quit-config) 'force))
6237     (when (eq major-mode 'gnus-summary-mode)
6238       (gnus-summary-next-subject 1 nil t)
6239       (gnus-summary-recenter)
6240       (gnus-summary-position-point))))
6241
6242 (defun gnus-summary-preview-mime-message ()
6243   "MIME decode and play this message."
6244   (interactive)
6245   (let ((gnus-break-pages nil)
6246         (gnus-show-mime t))
6247     (gnus-summary-select-article gnus-show-all-headers t))
6248   (let ((w (get-buffer-window gnus-article-buffer)))
6249     (when w
6250       (select-window (get-buffer-window gnus-article-buffer)))))
6251
6252 ;;; Dead summaries.
6253
6254 (defvar gnus-dead-summary-mode-map nil)
6255
6256 (unless gnus-dead-summary-mode-map
6257   (setq gnus-dead-summary-mode-map (make-keymap))
6258   (suppress-keymap gnus-dead-summary-mode-map)
6259   (substitute-key-definition
6260    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6261   (dolist (key '("\C-d" "\r" "\177" [delete]))
6262     (define-key gnus-dead-summary-mode-map
6263       key 'gnus-summary-wake-up-the-dead))
6264   (dolist (key '("q" "Q"))
6265     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6266
6267 (defvar gnus-dead-summary-mode nil
6268   "Minor mode for Gnus summary buffers.")
6269
6270 (defun gnus-dead-summary-mode (&optional arg)
6271   "Minor mode for Gnus summary buffers."
6272   (interactive "P")
6273   (when (eq major-mode 'gnus-summary-mode)
6274     (make-local-variable 'gnus-dead-summary-mode)
6275     (setq gnus-dead-summary-mode
6276           (if (null arg) (not gnus-dead-summary-mode)
6277             (> (prefix-numeric-value arg) 0)))
6278     (when gnus-dead-summary-mode
6279       (gnus-add-minor-mode
6280        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6281
6282 (defun gnus-deaden-summary ()
6283   "Make the current summary buffer into a dead summary buffer."
6284   ;; Kill any previous dead summary buffer.
6285   (when (and gnus-dead-summary
6286              (buffer-name gnus-dead-summary))
6287     (save-excursion
6288       (set-buffer gnus-dead-summary)
6289       (when gnus-dead-summary-mode
6290         (kill-buffer (current-buffer)))))
6291   ;; Make this the current dead summary.
6292   (setq gnus-dead-summary (current-buffer))
6293   (gnus-dead-summary-mode 1)
6294   (let ((name (buffer-name)))
6295     (when (string-match "Summary" name)
6296       (rename-buffer
6297        (concat (substring name 0 (match-beginning 0)) "Dead "
6298                (substring name (match-beginning 0)))
6299        t)
6300       (bury-buffer))))
6301
6302 (defun gnus-kill-or-deaden-summary (buffer)
6303   "Kill or deaden the summary BUFFER."
6304   (save-excursion
6305     (when (and (buffer-name buffer)
6306                (not gnus-single-article-buffer))
6307       (save-excursion
6308         (set-buffer buffer)
6309         (gnus-kill-buffer gnus-article-buffer)
6310         (gnus-kill-buffer gnus-original-article-buffer)))
6311     (cond
6312      ;; Kill the buffer.
6313      (gnus-kill-summary-on-exit
6314       (when (and gnus-use-trees
6315                  (gnus-buffer-exists-p buffer))
6316         (save-excursion
6317           (set-buffer buffer)
6318           (gnus-tree-close gnus-newsgroup-name)))
6319       (gnus-kill-buffer buffer))
6320      ;; Deaden the buffer.
6321      ((gnus-buffer-exists-p buffer)
6322       (save-excursion
6323         (set-buffer buffer)
6324         (gnus-deaden-summary))))))
6325
6326 (defun gnus-summary-wake-up-the-dead (&rest args)
6327   "Wake up the dead summary buffer."
6328   (interactive)
6329   (gnus-dead-summary-mode -1)
6330   (let ((name (buffer-name)))
6331     (when (string-match "Dead " name)
6332       (rename-buffer
6333        (concat (substring name 0 (match-beginning 0))
6334                (substring name (match-end 0)))
6335        t)))
6336   (gnus-message 3 "This dead summary is now alive again"))
6337
6338 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6339 (defun gnus-summary-fetch-faq (&optional faq-dir)
6340   "Fetch the FAQ for the current group.
6341 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6342 in."
6343   (interactive
6344    (list
6345     (when current-prefix-arg
6346       (completing-read
6347        "Faq dir: " (and (listp gnus-group-faq-directory)
6348                         (mapcar (lambda (file) (list file))
6349                                 gnus-group-faq-directory))))))
6350   (let (gnus-faq-buffer)
6351     (when (setq gnus-faq-buffer
6352                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6353       (gnus-configure-windows 'summary-faq))))
6354
6355 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6356 (defun gnus-summary-describe-group (&optional force)
6357   "Describe the current newsgroup."
6358   (interactive "P")
6359   (gnus-group-describe-group force gnus-newsgroup-name))
6360
6361 (defun gnus-summary-describe-briefly ()
6362   "Describe summary mode commands briefly."
6363   (interactive)
6364   (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")))
6365
6366 ;; Walking around group mode buffer from summary mode.
6367
6368 (defun gnus-summary-next-group (&optional no-article target-group backward)
6369   "Exit current newsgroup and then select next unread newsgroup.
6370 If prefix argument NO-ARTICLE is non-nil, no article is selected
6371 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6372 previous group instead."
6373   (interactive "P")
6374   ;; Stop pre-fetching.
6375   (gnus-async-halt-prefetch)
6376   (let ((current-group gnus-newsgroup-name)
6377         (current-buffer (current-buffer))
6378         entered)
6379     ;; First we semi-exit this group to update Xrefs and all variables.
6380     ;; We can't do a real exit, because the window conf must remain
6381     ;; the same in case the user is prompted for info, and we don't
6382     ;; want the window conf to change before that...
6383     (gnus-summary-exit t)
6384     (while (not entered)
6385       ;; Then we find what group we are supposed to enter.
6386       (set-buffer gnus-group-buffer)
6387       (gnus-group-jump-to-group current-group)
6388       (setq target-group
6389             (or target-group
6390                 (if (eq gnus-keep-same-level 'best)
6391                     (gnus-summary-best-group gnus-newsgroup-name)
6392                   (gnus-summary-search-group backward gnus-keep-same-level))))
6393       (if (not target-group)
6394           ;; There are no further groups, so we return to the group
6395           ;; buffer.
6396           (progn
6397             (gnus-message 5 "Returning to the group buffer")
6398             (setq entered t)
6399             (when (gnus-buffer-live-p current-buffer)
6400               (set-buffer current-buffer)
6401               (gnus-summary-exit))
6402             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6403         ;; We try to enter the target group.
6404         (gnus-group-jump-to-group target-group)
6405         (let ((unreads (gnus-group-group-unread)))
6406           (if (and (or (eq t unreads)
6407                        (and unreads (not (zerop unreads))))
6408                    (gnus-summary-read-group
6409                     target-group nil no-article
6410                     (and (buffer-name current-buffer) current-buffer)
6411                     nil backward))
6412               (setq entered t)
6413             (setq current-group target-group
6414                   target-group nil)))))))
6415
6416 (defun gnus-summary-prev-group (&optional no-article)
6417   "Exit current newsgroup and then select previous unread newsgroup.
6418 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6419   (interactive "P")
6420   (gnus-summary-next-group no-article nil t))
6421
6422 ;; Walking around summary lines.
6423
6424 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6425   "Go to the first unread subject.
6426 If UNREAD is non-nil, go to the first unread article.
6427 Returns the article selected or nil if there are no unread articles."
6428   (interactive "P")
6429   (prog1
6430       (cond
6431        ;; Empty summary.
6432        ((null gnus-newsgroup-data)
6433         (gnus-message 3 "No articles in the group")
6434         nil)
6435        ;; Pick the first article.
6436        ((not unread)
6437         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6438         (gnus-data-number (car gnus-newsgroup-data)))
6439        ;; No unread articles.
6440        ((null gnus-newsgroup-unreads)
6441         (gnus-message 3 "No more unread articles")
6442         nil)
6443        ;; Find the first unread article.
6444        (t
6445         (let ((data gnus-newsgroup-data))
6446           (while (and data
6447                       (and (not (and undownloaded
6448                                      (eq gnus-undownloaded-mark
6449                                          (gnus-data-mark (car data)))))
6450                            (if unseen
6451                                (or (not (memq
6452                                          (gnus-data-number (car data))
6453                                          gnus-newsgroup-unseen))
6454                                    (not (gnus-data-unread-p (car data))))
6455                              (not (gnus-data-unread-p (car data))))))
6456             (setq data (cdr data)))
6457           (when data
6458             (goto-char (gnus-data-pos (car data)))
6459             (gnus-data-number (car data))))))
6460     (gnus-summary-position-point)))
6461
6462 (defun gnus-summary-next-subject (n &optional unread dont-display)
6463   "Go to next N'th summary line.
6464 If N is negative, go to the previous N'th subject line.
6465 If UNREAD is non-nil, only unread articles are selected.
6466 The difference between N and the actual number of steps taken is
6467 returned."
6468   (interactive "p")
6469   (let ((backward (< n 0))
6470         (n (abs n)))
6471     (while (and (> n 0)
6472                 (if backward
6473                     (gnus-summary-find-prev unread)
6474                   (gnus-summary-find-next unread)))
6475       (unless (zerop (setq n (1- n)))
6476         (gnus-summary-show-thread)))
6477     (when (/= 0 n)
6478       (gnus-message 7 "No more%s articles"
6479                     (if unread " unread" "")))
6480     (unless dont-display
6481       (gnus-summary-recenter)
6482       (gnus-summary-position-point))
6483     n))
6484
6485 (defun gnus-summary-next-unread-subject (n)
6486   "Go to next N'th unread summary line."
6487   (interactive "p")
6488   (gnus-summary-next-subject n t))
6489
6490 (defun gnus-summary-prev-subject (n &optional unread)
6491   "Go to previous N'th summary line.
6492 If optional argument UNREAD is non-nil, only unread article is selected."
6493   (interactive "p")
6494   (gnus-summary-next-subject (- n) unread))
6495
6496 (defun gnus-summary-prev-unread-subject (n)
6497   "Go to previous N'th unread summary line."
6498   (interactive "p")
6499   (gnus-summary-next-subject (- n) t))
6500
6501 (defun gnus-summary-goto-subject (article &optional force silent)
6502   "Go the subject line of ARTICLE.
6503 If FORCE, also allow jumping to articles not currently shown."
6504   (interactive "nArticle number: ")
6505   (unless (numberp article)
6506     (error "Article %s is not a number" article))
6507   (let ((b (point))
6508         (data (gnus-data-find article)))
6509     ;; We read in the article if we have to.
6510     (and (not data)
6511          force
6512          (gnus-summary-insert-subject
6513           article
6514           (if (or (numberp force) (vectorp force)) force)
6515           t)
6516          (setq data (gnus-data-find article)))
6517     (goto-char b)
6518     (if (not data)
6519         (progn
6520           (unless silent
6521             (gnus-message 3 "Can't find article %d" article))
6522           nil)
6523       (let ((pt (gnus-data-pos data)))
6524         (goto-char pt)
6525         (gnus-summary-set-article-display-arrow pt))
6526       (gnus-summary-position-point)
6527       article)))
6528
6529 ;; Walking around summary lines with displaying articles.
6530
6531 (defun gnus-summary-expand-window (&optional arg)
6532   "Make the summary buffer take up the entire Emacs frame.
6533 Given a prefix, will force an `article' buffer configuration."
6534   (interactive "P")
6535   (if arg
6536       (gnus-configure-windows 'article 'force)
6537     (gnus-configure-windows 'summary 'force)))
6538
6539 (defun gnus-summary-display-article (article &optional all-header)
6540   "Display ARTICLE in article buffer."
6541   (when (gnus-buffer-live-p gnus-article-buffer)
6542     (with-current-buffer gnus-article-buffer
6543       (set-buffer-multibyte t)))
6544   (gnus-set-global-variables)
6545   (when (gnus-buffer-live-p gnus-article-buffer)
6546     (with-current-buffer gnus-article-buffer
6547       (setq gnus-article-charset gnus-newsgroup-charset)
6548       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6549       (set-buffer-multibyte t)))
6550   (if (null article)
6551       nil
6552     (prog1
6553         (if gnus-summary-display-article-function
6554             (funcall gnus-summary-display-article-function article all-header)
6555           (gnus-article-prepare article all-header))
6556       (with-current-buffer gnus-article-buffer
6557         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6558              nil))
6559       (gnus-run-hooks 'gnus-select-article-hook)
6560       (when (and gnus-current-article
6561                  (not (zerop gnus-current-article)))
6562         (gnus-summary-goto-subject gnus-current-article))
6563       (gnus-summary-recenter)
6564       (when (and gnus-use-trees gnus-show-threads)
6565         (gnus-possibly-generate-tree article)
6566         (gnus-highlight-selected-tree article))
6567       ;; Successfully display article.
6568       (gnus-article-set-window-start
6569        (cdr (assq article gnus-newsgroup-bookmarks))))))
6570
6571 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6572   "Select the current article.
6573 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6574 non-nil, the article will be re-fetched even if it already present in
6575 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6576 be displayed."
6577   ;; Make sure we are in the summary buffer to work around bbdb bug.
6578   (unless (eq major-mode 'gnus-summary-mode)
6579     (set-buffer gnus-summary-buffer))
6580   (let ((article (or article (gnus-summary-article-number)))
6581         (all-headers (not (not all-headers))) ;Must be T or NIL.
6582         gnus-summary-display-article-function)
6583     (and (not pseudo)
6584          (gnus-summary-article-pseudo-p article)
6585          (error "This is a pseudo-article"))
6586     (save-excursion
6587       (set-buffer gnus-summary-buffer)
6588       (if (or (and gnus-single-article-buffer
6589                    (or (null gnus-current-article)
6590                        (null gnus-article-current)
6591                        (null (get-buffer gnus-article-buffer))
6592                        (not (eq article (cdr gnus-article-current)))
6593                        (not (equal (car gnus-article-current)
6594                                    gnus-newsgroup-name))))
6595               (and (not gnus-single-article-buffer)
6596                    (or (null gnus-current-article)
6597                        (not (eq gnus-current-article article))))
6598               force)
6599           ;; The requested article is different from the current article.
6600           (progn
6601             (gnus-summary-display-article article all-headers)
6602 ;;; Hidden headers are not hidden text any more.
6603 ;;          (when (or all-headers gnus-show-all-headers)
6604 ;;            (gnus-article-show-all-headers))
6605             (gnus-article-set-window-start
6606              (cdr (assq article gnus-newsgroup-bookmarks)))
6607             article)
6608 ;;      (when (or all-headers gnus-show-all-headers)
6609 ;;        (gnus-article-show-all-headers))
6610         'old))))
6611
6612 (defun gnus-summary-force-verify-and-decrypt ()
6613   (interactive)
6614   (let ((mm-verify-option 'known)
6615         (mm-decrypt-option 'known))
6616     (gnus-summary-select-article nil 'force)))
6617
6618 (defun gnus-summary-set-current-mark (&optional current-mark)
6619   "Obsolete function."
6620   nil)
6621
6622 (defun gnus-summary-next-article (&optional unread subject backward push)
6623   "Select the next article.
6624 If UNREAD, only unread articles are selected.
6625 If SUBJECT, only articles with SUBJECT are selected.
6626 If BACKWARD, the previous article is selected instead of the next."
6627   (interactive "P")
6628   (cond
6629    ;; Is there such an article?
6630    ((and (gnus-summary-search-forward unread subject backward)
6631          (or (gnus-summary-display-article (gnus-summary-article-number))
6632              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6633     (gnus-summary-position-point))
6634    ;; If not, we try the first unread, if that is wanted.
6635    ((and subject
6636          gnus-auto-select-same
6637          (gnus-summary-first-unread-article))
6638     (gnus-summary-position-point)
6639     (gnus-message 6 "Wrapped"))
6640    ;; Try to get next/previous article not displayed in this group.
6641    ((and gnus-auto-extend-newsgroup
6642          (not unread) (not subject))
6643     (gnus-summary-goto-article
6644      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6645      nil (count-lines (point-min) (point))))
6646    ;; Go to next/previous group.
6647    (t
6648     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6649       (gnus-summary-jump-to-group gnus-newsgroup-name))
6650     (let ((cmd last-command-char)
6651           (point
6652            (save-excursion
6653              (set-buffer gnus-group-buffer)
6654              (point)))
6655           (group
6656            (if (eq gnus-keep-same-level 'best)
6657                (gnus-summary-best-group gnus-newsgroup-name)
6658              (gnus-summary-search-group backward gnus-keep-same-level))))
6659       ;; For some reason, the group window gets selected.  We change
6660       ;; it back.
6661       (select-window (get-buffer-window (current-buffer)))
6662       ;; Select next unread newsgroup automagically.
6663       (cond
6664        ((or (not gnus-auto-select-next)
6665             (not cmd))
6666         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6667        ((or (eq gnus-auto-select-next 'quietly)
6668             (and (eq gnus-auto-select-next 'slightly-quietly)
6669                  push)
6670             (and (eq gnus-auto-select-next 'almost-quietly)
6671                  (gnus-summary-last-article-p)))
6672         ;; Select quietly.
6673         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6674             (gnus-summary-exit)
6675           (gnus-message 7 "No more%s articles (%s)..."
6676                         (if unread " unread" "")
6677                         (if group (concat "selecting " group)
6678                           "exiting"))
6679           (gnus-summary-next-group nil group backward)))
6680        (t
6681         (when (gnus-key-press-event-p last-input-event)
6682           (gnus-summary-walk-group-buffer
6683            gnus-newsgroup-name cmd unread backward point))))))))
6684
6685 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6686   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6687                       (?\C-p (gnus-group-prev-unread-group 1))))
6688         (cursor-in-echo-area t)
6689         keve key group ended)
6690     (save-excursion
6691       (set-buffer gnus-group-buffer)
6692       (goto-char start)
6693       (setq group
6694             (if (eq gnus-keep-same-level 'best)
6695                 (gnus-summary-best-group gnus-newsgroup-name)
6696               (gnus-summary-search-group backward gnus-keep-same-level))))
6697     (while (not ended)
6698       (gnus-message
6699        5 "No more%s articles%s" (if unread " unread" "")
6700        (if (and group
6701                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6702            (format " (Type %s for %s [%s])"
6703                    (single-key-description cmd) group
6704                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6705          (format " (Type %s to exit %s)"
6706                  (single-key-description cmd)
6707                  gnus-newsgroup-name)))
6708       ;; Confirm auto selection.
6709       (setq key (car (setq keve (gnus-read-event-char))))
6710       (setq ended t)
6711       (cond
6712        ((assq key keystrokes)
6713         (let ((obuf (current-buffer)))
6714           (switch-to-buffer gnus-group-buffer)
6715           (when group
6716             (gnus-group-jump-to-group group))
6717           (eval (cadr (assq key keystrokes)))
6718           (setq group (gnus-group-group-name))
6719           (switch-to-buffer obuf))
6720         (setq ended nil))
6721        ((equal key cmd)
6722         (if (or (not group)
6723                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6724             (gnus-summary-exit)
6725           (gnus-summary-next-group nil group backward)))
6726        (t
6727         (push (cdr keve) unread-command-events))))))
6728
6729 (defun gnus-summary-next-unread-article ()
6730   "Select unread article after current one."
6731   (interactive)
6732   (gnus-summary-next-article
6733    (or (not (eq gnus-summary-goto-unread 'never))
6734        (gnus-summary-last-article-p (gnus-summary-article-number)))
6735    (and gnus-auto-select-same
6736         (gnus-summary-article-subject))))
6737
6738 (defun gnus-summary-prev-article (&optional unread subject)
6739   "Select the article after the current one.
6740 If UNREAD is non-nil, only unread articles are selected."
6741   (interactive "P")
6742   (gnus-summary-next-article unread subject t))
6743
6744 (defun gnus-summary-prev-unread-article ()
6745   "Select unread article before current one."
6746   (interactive)
6747   (gnus-summary-prev-article
6748    (or (not (eq gnus-summary-goto-unread 'never))
6749        (gnus-summary-first-article-p (gnus-summary-article-number)))
6750    (and gnus-auto-select-same
6751         (gnus-summary-article-subject))))
6752
6753 (defun gnus-summary-next-page (&optional lines circular)
6754   "Show next page of the selected article.
6755 If at the end of the current article, select the next article.
6756 LINES says how many lines should be scrolled up.
6757
6758 If CIRCULAR is non-nil, go to the start of the article instead of
6759 selecting the next article when reaching the end of the current
6760 article."
6761   (interactive "P")
6762   (setq gnus-summary-buffer (current-buffer))
6763   (gnus-set-global-variables)
6764   (let ((article (gnus-summary-article-number))
6765         (article-window (get-buffer-window gnus-article-buffer t))
6766         endp)
6767     ;; If the buffer is empty, we have no article.
6768     (unless article
6769       (error "No article to select"))
6770     (gnus-configure-windows 'article)
6771     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6772         (if (and (eq gnus-summary-goto-unread 'never)
6773                  (not (gnus-summary-last-article-p article)))
6774             (gnus-summary-next-article)
6775           (gnus-summary-next-unread-article))
6776       (if (or (null gnus-current-article)
6777               (null gnus-article-current)
6778               (/= article (cdr gnus-article-current))
6779               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6780           ;; Selected subject is different from current article's.
6781           (gnus-summary-display-article article)
6782         (when article-window
6783           (gnus-eval-in-buffer-window gnus-article-buffer
6784             (setq endp (gnus-article-next-page lines)))
6785           (when endp
6786             (cond (circular
6787                    (gnus-summary-beginning-of-article))
6788                   (lines
6789                    (gnus-message 3 "End of message"))
6790                   ((null lines)
6791                    (if (and (eq gnus-summary-goto-unread 'never)
6792                             (not (gnus-summary-last-article-p article)))
6793                        (gnus-summary-next-article)
6794                      (gnus-summary-next-unread-article))))))))
6795     (gnus-summary-recenter)
6796     (gnus-summary-position-point)))
6797
6798 (defun gnus-summary-prev-page (&optional lines move)
6799   "Show previous page of selected article.
6800 Argument LINES specifies lines to be scrolled down.
6801 If MOVE, move to the previous unread article if point is at
6802 the beginning of the buffer."
6803   (interactive "P")
6804   (let ((article (gnus-summary-article-number))
6805         (article-window (get-buffer-window gnus-article-buffer t))
6806         endp)
6807     (gnus-configure-windows 'article)
6808     (if (or (null gnus-current-article)
6809             (null gnus-article-current)
6810             (/= article (cdr gnus-article-current))
6811             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6812         ;; Selected subject is different from current article's.
6813         (gnus-summary-display-article article)
6814       (gnus-summary-recenter)
6815       (when article-window
6816         (gnus-eval-in-buffer-window gnus-article-buffer
6817           (setq endp (gnus-article-prev-page lines)))
6818         (when (and move endp)
6819           (cond (lines
6820                  (gnus-message 3 "Beginning of message"))
6821                 ((null lines)
6822                  (if (and (eq gnus-summary-goto-unread 'never)
6823                           (not (gnus-summary-first-article-p article)))
6824                      (gnus-summary-prev-article)
6825                    (gnus-summary-prev-unread-article))))))))
6826   (gnus-summary-position-point))
6827
6828 (defun gnus-summary-prev-page-or-article (&optional lines)
6829   "Show previous page of selected article.
6830 Argument LINES specifies lines to be scrolled down.
6831 If at the beginning of the article, go to the next article."
6832   (interactive "P")
6833   (gnus-summary-prev-page lines t))
6834
6835 (defun gnus-summary-scroll-up (lines)
6836   "Scroll up (or down) one line current article.
6837 Argument LINES specifies lines to be scrolled up (or down if negative)."
6838   (interactive "p")
6839   (gnus-configure-windows 'article)
6840   (gnus-summary-show-thread)
6841   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6842     (gnus-eval-in-buffer-window gnus-article-buffer
6843       (cond ((> lines 0)
6844              (when (gnus-article-next-page lines)
6845                (gnus-message 3 "End of message")))
6846             ((< lines 0)
6847              (gnus-article-prev-page (- lines))))))
6848   (gnus-summary-recenter)
6849   (gnus-summary-position-point))
6850
6851 (defun gnus-summary-scroll-down (lines)
6852   "Scroll down (or up) one line current article.
6853 Argument LINES specifies lines to be scrolled down (or up if negative)."
6854   (interactive "p")
6855   (gnus-summary-scroll-up (- lines)))
6856
6857 (defun gnus-summary-next-same-subject ()
6858   "Select next article which has the same subject as current one."
6859   (interactive)
6860   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6861
6862 (defun gnus-summary-prev-same-subject ()
6863   "Select previous article which has the same subject as current one."
6864   (interactive)
6865   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6866
6867 (defun gnus-summary-next-unread-same-subject ()
6868   "Select next unread article which has the same subject as current one."
6869   (interactive)
6870   (gnus-summary-next-article t (gnus-summary-article-subject)))
6871
6872 (defun gnus-summary-prev-unread-same-subject ()
6873   "Select previous unread article which has the same subject as current one."
6874   (interactive)
6875   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6876
6877 (defun gnus-summary-first-unread-article ()
6878   "Select the first unread article.
6879 Return nil if there are no unread articles."
6880   (interactive)
6881   (prog1
6882       (when (gnus-summary-first-subject t)
6883         (gnus-summary-show-thread)
6884         (gnus-summary-first-subject t)
6885         (gnus-summary-display-article (gnus-summary-article-number)))
6886     (gnus-summary-position-point)))
6887
6888 (defun gnus-summary-first-unread-subject ()
6889   "Place the point on the subject line of the first unread article.
6890 Return nil if there are no unread articles."
6891   (interactive)
6892   (prog1
6893       (when (gnus-summary-first-subject t)
6894         (gnus-summary-show-thread)
6895         (gnus-summary-first-subject t))
6896     (gnus-summary-position-point)))
6897
6898 (defun gnus-summary-first-unseen-subject ()
6899   "Place the point on the subject line of the first unseen article.
6900 Return nil if there are no unseen articles."
6901   (interactive)
6902   (prog1
6903       (when (gnus-summary-first-subject t t t)
6904         (gnus-summary-show-thread)
6905         (gnus-summary-first-subject t t t))
6906     (gnus-summary-position-point)))
6907
6908 (defun gnus-summary-first-unseen-or-unread-subject ()
6909   "Place the point on the subject line of the first unseen article.
6910 Return nil if there are no unseen articles."
6911   (interactive)
6912   (prog1
6913       (unless (when (gnus-summary-first-subject t t t)
6914                 (gnus-summary-show-thread)
6915                 (gnus-summary-first-subject t t t))
6916         (when (gnus-summary-first-subject t)
6917           (gnus-summary-show-thread)
6918           (gnus-summary-first-subject t)))
6919     (gnus-summary-position-point)))
6920
6921 (defun gnus-summary-first-article ()
6922   "Select the first article.
6923 Return nil if there are no articles."
6924   (interactive)
6925   (prog1
6926       (when (gnus-summary-first-subject)
6927         (gnus-summary-show-thread)
6928         (gnus-summary-first-subject)
6929         (gnus-summary-display-article (gnus-summary-article-number)))
6930     (gnus-summary-position-point)))
6931
6932 (defun gnus-summary-best-unread-article (&optional arg)
6933   "Select the unread article with the highest score.
6934 If given a prefix argument, select the next unread article that has a
6935 score higher than the default score."
6936   (interactive "P")
6937   (let ((article (if arg
6938                      (gnus-summary-better-unread-subject)
6939                    (gnus-summary-best-unread-subject))))
6940     (if article
6941         (gnus-summary-goto-article article)
6942       (error "No unread articles"))))
6943
6944 (defun gnus-summary-best-unread-subject ()
6945   "Select the unread subject with the highest score."
6946   (interactive)
6947   (let ((best -1000000)
6948         (data gnus-newsgroup-data)
6949         article score)
6950     (while data
6951       (and (gnus-data-unread-p (car data))
6952            (> (setq score
6953                     (gnus-summary-article-score (gnus-data-number (car data))))
6954               best)
6955            (setq best score
6956                  article (gnus-data-number (car data))))
6957       (setq data (cdr data)))
6958     (when article
6959       (gnus-summary-goto-subject article))
6960     (gnus-summary-position-point)
6961     article))
6962
6963 (defun gnus-summary-better-unread-subject ()
6964   "Select the first unread subject that has a score over the default score."
6965   (interactive)
6966   (let ((data gnus-newsgroup-data)
6967         article score)
6968     (while (and (setq article (gnus-data-number (car data)))
6969                 (or (gnus-data-read-p (car data))
6970                     (not (> (gnus-summary-article-score article)
6971                             gnus-summary-default-score))))
6972       (setq data (cdr data)))
6973     (when article
6974       (gnus-summary-goto-subject article))
6975     (gnus-summary-position-point)
6976     article))
6977
6978 (defun gnus-summary-last-subject ()
6979   "Go to the last displayed subject line in the group."
6980   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6981     (when article
6982       (gnus-summary-goto-subject article))))
6983
6984 (defun gnus-summary-goto-article (article &optional all-headers force)
6985   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6986 If ALL-HEADERS is non-nil, no header lines are hidden.
6987 If FORCE, go to the article even if it isn't displayed.  If FORCE
6988 is a number, it is the line the article is to be displayed on."
6989   (interactive
6990    (list
6991     (completing-read
6992      "Article number or Message-ID: "
6993      (mapcar (lambda (number) (list (int-to-string number)))
6994              gnus-newsgroup-limit))
6995     current-prefix-arg
6996     t))
6997   (prog1
6998       (if (and (stringp article)
6999                (string-match "@" article))
7000           (gnus-summary-refer-article article)
7001         (when (stringp article)
7002           (setq article (string-to-number article)))
7003         (if (gnus-summary-goto-subject article force)
7004             (gnus-summary-display-article article all-headers)
7005           (gnus-message 4 "Couldn't go to article %s" article) nil))
7006     (gnus-summary-position-point)))
7007
7008 (defun gnus-summary-goto-last-article ()
7009   "Go to the previously read article."
7010   (interactive)
7011   (prog1
7012       (when gnus-last-article
7013         (gnus-summary-goto-article gnus-last-article nil t))
7014     (gnus-summary-position-point)))
7015
7016 (defun gnus-summary-pop-article (number)
7017   "Pop one article off the history and go to the previous.
7018 NUMBER articles will be popped off."
7019   (interactive "p")
7020   (let (to)
7021     (setq gnus-newsgroup-history
7022           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7023     (if to
7024         (gnus-summary-goto-article (car to) nil t)
7025       (error "Article history empty")))
7026   (gnus-summary-position-point))
7027
7028 ;; Summary commands and functions for limiting the summary buffer.
7029
7030 (defun gnus-summary-limit-to-articles (n)
7031   "Limit the summary buffer to the next N articles.
7032 If not given a prefix, use the process marked articles instead."
7033   (interactive "P")
7034   (prog1
7035       (let ((articles (gnus-summary-work-articles n)))
7036         (setq gnus-newsgroup-processable nil)
7037         (gnus-summary-limit articles))
7038     (gnus-summary-position-point)))
7039
7040 (defun gnus-summary-pop-limit (&optional total)
7041   "Restore the previous limit.
7042 If given a prefix, remove all limits."
7043   (interactive "P")
7044   (when total
7045     (setq gnus-newsgroup-limits
7046           (list (mapcar (lambda (h) (mail-header-number h))
7047                         gnus-newsgroup-headers))))
7048   (unless gnus-newsgroup-limits
7049     (error "No limit to pop"))
7050   (prog1
7051       (gnus-summary-limit nil 'pop)
7052     (gnus-summary-position-point)))
7053
7054 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7055   "Limit the summary buffer to articles that have subjects that match a regexp.
7056 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7057   (interactive
7058    (list (read-string (if current-prefix-arg
7059                           "Exclude subject (regexp): "
7060                         "Limit to subject (regexp): "))
7061          nil current-prefix-arg))
7062   (unless header
7063     (setq header "subject"))
7064   (when (not (equal "" subject))
7065     (prog1
7066         (let ((articles (gnus-summary-find-matching
7067                          (or header "subject") subject 'all nil nil
7068                          not-matching)))
7069           (unless articles
7070             (error "Found no matches for \"%s\"" subject))
7071           (gnus-summary-limit articles))
7072       (gnus-summary-position-point))))
7073
7074 (defun gnus-summary-limit-to-author (from &optional not-matching)
7075   "Limit the summary buffer to articles that have authors that match a regexp.
7076 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7077   (interactive
7078    (list (read-string (if current-prefix-arg
7079                           "Exclude author (regexp): "
7080                         "Limit to author (regexp): "))
7081          current-prefix-arg))
7082   (gnus-summary-limit-to-subject from "from" not-matching))
7083
7084 (defun gnus-summary-limit-to-age (age &optional younger-p)
7085   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7086 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7087 articles that are younger than AGE days."
7088   (interactive
7089    (let ((younger current-prefix-arg)
7090          (days-got nil)
7091          days)
7092      (while (not days-got)
7093        (setq days (if younger
7094                       (read-string "Limit to articles within (in days): ")
7095                     (read-string "Limit to articles older than (in days): ")))
7096        (when (> (length days) 0)
7097          (setq days (read days)))
7098        (if (numberp days)
7099            (progn
7100              (setq days-got t)
7101              (if (< days 0)
7102                  (progn
7103                    (setq younger (not younger))
7104                    (setq days (* days -1)))))
7105          (message "Please enter a number.")
7106          (sleep-for 1)))
7107      (list days younger)))
7108   (prog1
7109       (let ((data gnus-newsgroup-data)
7110             (cutoff (days-to-time age))
7111             articles d date is-younger)
7112         (while (setq d (pop data))
7113           (when (and (vectorp (gnus-data-header d))
7114                      (setq date (mail-header-date (gnus-data-header d))))
7115             (setq is-younger (time-less-p
7116                               (time-since (condition-case ()
7117                                               (date-to-time date)
7118                                             (error '(0 0))))
7119                               cutoff))
7120             (when (if younger-p
7121                       is-younger
7122                     (not is-younger))
7123               (push (gnus-data-number d) articles))))
7124         (gnus-summary-limit (nreverse articles)))
7125     (gnus-summary-position-point)))
7126
7127 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7128   "Limit the summary buffer to articles that match an 'extra' header."
7129   (interactive
7130    (let ((header
7131           (intern
7132            (gnus-completing-read-with-default
7133             (symbol-name (car gnus-extra-headers))
7134             (if current-prefix-arg
7135                 "Exclude extra header:"
7136               "Limit extra header:")
7137             (mapcar (lambda (x)
7138                       (cons (symbol-name x) x))
7139                     gnus-extra-headers)
7140             nil
7141             t))))
7142      (list header
7143            (read-string (format "%s header %s (regexp): "
7144                                 (if current-prefix-arg "Exclude" "Limit to")
7145                                 header))
7146            current-prefix-arg)))
7147   (when (not (equal "" regexp))
7148     (prog1
7149         (let ((articles (gnus-summary-find-matching
7150                          (cons 'extra header) regexp 'all nil nil
7151                          not-matching)))
7152           (unless articles
7153             (error "Found no matches for \"%s\"" regexp))
7154           (gnus-summary-limit articles))
7155       (gnus-summary-position-point))))
7156
7157 (defun gnus-summary-limit-to-display-predicate ()
7158   "Limit the summary buffer to the predicated in the `display' group parameter."
7159   (interactive)
7160   (unless gnus-newsgroup-display
7161     (error "There is no `display' group parameter"))
7162   (let (articles)
7163     (dolist (number gnus-newsgroup-articles)
7164       (when (funcall gnus-newsgroup-display)
7165         (push number articles)))
7166     (gnus-summary-limit articles))
7167   (gnus-summary-position-point))
7168
7169 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7170 (make-obsolete
7171  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7172
7173 (defun gnus-summary-limit-to-unread (&optional all)
7174   "Limit the summary buffer to articles that are not marked as read.
7175 If ALL is non-nil, limit strictly to unread articles."
7176   (interactive "P")
7177   (if all
7178       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7179     (gnus-summary-limit-to-marks
7180      ;; Concat all the marks that say that an article is read and have
7181      ;; those removed.
7182      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7183            gnus-killed-mark gnus-kill-file-mark
7184            gnus-low-score-mark gnus-expirable-mark
7185            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7186            gnus-duplicate-mark gnus-souped-mark)
7187      'reverse)))
7188
7189 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7190 (make-obsolete 'gnus-summary-delete-marked-with
7191                'gnus-summary-limit-exlude-marks)
7192
7193 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7194   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7195 If REVERSE, limit the summary buffer to articles that are marked
7196 with MARKS.  MARKS can either be a string of marks or a list of marks.
7197 Returns how many articles were removed."
7198   (interactive "sMarks: ")
7199   (gnus-summary-limit-to-marks marks t))
7200
7201 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7202   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7203 If REVERSE (the prefix), limit the summary buffer to articles that are
7204 not marked with MARKS.  MARKS can either be a string of marks or a
7205 list of marks.
7206 Returns how many articles were removed."
7207   (interactive "sMarks: \nP")
7208   (prog1
7209       (let ((data gnus-newsgroup-data)
7210             (marks (if (listp marks) marks
7211                      (append marks nil))) ; Transform to list.
7212             articles)
7213         (while data
7214           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7215                   (memq (gnus-data-mark (car data)) marks))
7216             (push (gnus-data-number (car data)) articles))
7217           (setq data (cdr data)))
7218         (gnus-summary-limit articles))
7219     (gnus-summary-position-point)))
7220
7221 (defun gnus-summary-limit-to-score (score)
7222   "Limit to articles with score at or above SCORE."
7223   (interactive "NLimit to articles with score of at least: ")
7224   (let ((data gnus-newsgroup-data)
7225         articles)
7226     (while data
7227       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7228                 score)
7229         (push (gnus-data-number (car data)) articles))
7230       (setq data (cdr data)))
7231     (prog1
7232         (gnus-summary-limit articles)
7233       (gnus-summary-position-point))))
7234
7235 (defun gnus-summary-limit-include-thread (id)
7236   "Display all the hidden articles that is in the thread with ID in it.
7237 When called interactively, ID is the Message-ID of the current
7238 article."
7239   (interactive (list (mail-header-id (gnus-summary-article-header))))
7240   (let ((articles (gnus-articles-in-thread
7241                    (gnus-id-to-thread (gnus-root-id id)))))
7242     (prog1
7243         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7244       (gnus-summary-limit-include-matching-articles
7245        "subject"
7246        (regexp-quote (gnus-simplify-subject-re
7247                       (mail-header-subject (gnus-id-to-header id)))))
7248       (gnus-summary-position-point))))
7249
7250 (defun gnus-summary-limit-include-matching-articles (header regexp)
7251   "Display all the hidden articles that have HEADERs that match REGEXP."
7252   (interactive (list (read-string "Match on header: ")
7253                      (read-string "Regexp: ")))
7254   (let ((articles (gnus-find-matching-articles header regexp)))
7255     (prog1
7256         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7257       (gnus-summary-position-point))))
7258
7259 (defun gnus-summary-limit-include-dormant ()
7260   "Display all the hidden articles that are marked as dormant.
7261 Note that this command only works on a subset of the articles currently
7262 fetched for this group."
7263   (interactive)
7264   (unless gnus-newsgroup-dormant
7265     (error "There are no dormant articles in this group"))
7266   (prog1
7267       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7268     (gnus-summary-position-point)))
7269
7270 (defun gnus-summary-limit-exclude-dormant ()
7271   "Hide all dormant articles."
7272   (interactive)
7273   (prog1
7274       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7275     (gnus-summary-position-point)))
7276
7277 (defun gnus-summary-limit-exclude-childless-dormant ()
7278   "Hide all dormant articles that have no children."
7279   (interactive)
7280   (let ((data (gnus-data-list t))
7281         articles d children)
7282     ;; Find all articles that are either not dormant or have
7283     ;; children.
7284     (while (setq d (pop data))
7285       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7286                 (and (setq children
7287                            (gnus-article-children (gnus-data-number d)))
7288                      (let (found)
7289                        (while children
7290                          (when (memq (car children) articles)
7291                            (setq children nil
7292                                  found t))
7293                          (pop children))
7294                        found)))
7295         (push (gnus-data-number d) articles)))
7296     ;; Do the limiting.
7297     (prog1
7298         (gnus-summary-limit articles)
7299       (gnus-summary-position-point))))
7300
7301 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7302   "Mark all unread excluded articles as read.
7303 If ALL, mark even excluded ticked and dormants as read."
7304   (interactive "P")
7305   (let ((articles (gnus-sorted-complement
7306                    (sort
7307                     (mapcar (lambda (h) (mail-header-number h))
7308                             gnus-newsgroup-headers)
7309                     '<)
7310                    (sort gnus-newsgroup-limit '<)))
7311         article)
7312     (setq gnus-newsgroup-unreads
7313           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7314     (if all
7315         (setq gnus-newsgroup-dormant nil
7316               gnus-newsgroup-marked nil
7317               gnus-newsgroup-reads
7318               (nconc
7319                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7320                gnus-newsgroup-reads))
7321       (while (setq article (pop articles))
7322         (unless (or (memq article gnus-newsgroup-dormant)
7323                     (memq article gnus-newsgroup-marked))
7324           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7325
7326 (defun gnus-summary-limit (articles &optional pop)
7327   (if pop
7328       ;; We pop the previous limit off the stack and use that.
7329       (setq articles (car gnus-newsgroup-limits)
7330             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7331     ;; We use the new limit, so we push the old limit on the stack.
7332     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7333   ;; Set the limit.
7334   (setq gnus-newsgroup-limit articles)
7335   (let ((total (length gnus-newsgroup-data))
7336         (data (gnus-data-find-list (gnus-summary-article-number)))
7337         (gnus-summary-mark-below nil)   ; Inhibit this.
7338         found)
7339     ;; This will do all the work of generating the new summary buffer
7340     ;; according to the new limit.
7341     (gnus-summary-prepare)
7342     ;; Hide any threads, possibly.
7343     (gnus-summary-maybe-hide-threads)
7344     ;; Try to return to the article you were at, or one in the
7345     ;; neighborhood.
7346     (when data
7347       ;; We try to find some article after the current one.
7348       (while data
7349         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7350           (setq data nil
7351                 found t))
7352         (setq data (cdr data))))
7353     (unless found
7354       ;; If there is no data, that means that we were after the last
7355       ;; article.  The same goes when we can't find any articles
7356       ;; after the current one.
7357       (goto-char (point-max))
7358       (gnus-summary-find-prev))
7359     (gnus-set-mode-line 'summary)
7360     ;; We return how many articles were removed from the summary
7361     ;; buffer as a result of the new limit.
7362     (- total (length gnus-newsgroup-data))))
7363
7364 (defsubst gnus-invisible-cut-children (threads)
7365   (let ((num 0))
7366     (while threads
7367       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7368         (incf num))
7369       (pop threads))
7370     (< num 2)))
7371
7372 (defsubst gnus-cut-thread (thread)
7373   "Go forwards in the thread until we find an article that we want to display."
7374   (when (or (eq gnus-fetch-old-headers 'some)
7375             (eq gnus-fetch-old-headers 'invisible)
7376             (numberp gnus-fetch-old-headers)
7377             (eq gnus-build-sparse-threads 'some)
7378             (eq gnus-build-sparse-threads 'more))
7379     ;; Deal with old-fetched headers and sparse threads.
7380     (while (and
7381             thread
7382             (or
7383              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7384              (gnus-summary-article-ancient-p
7385               (mail-header-number (car thread))))
7386             (if (or (<= (length (cdr thread)) 1)
7387                     (eq gnus-fetch-old-headers 'invisible))
7388                 (setq gnus-newsgroup-limit
7389                       (delq (mail-header-number (car thread))
7390                             gnus-newsgroup-limit)
7391                       thread (cadr thread))
7392               (when (gnus-invisible-cut-children (cdr thread))
7393                 (let ((th (cdr thread)))
7394                   (while th
7395                     (if (memq (mail-header-number (caar th))
7396                               gnus-newsgroup-limit)
7397                         (setq thread (car th)
7398                               th nil)
7399                       (setq th (cdr th))))))))))
7400   thread)
7401
7402 (defun gnus-cut-threads (threads)
7403   "Cut off all uninteresting articles from the beginning of threads."
7404   (when (or (eq gnus-fetch-old-headers 'some)
7405             (eq gnus-fetch-old-headers 'invisible)
7406             (numberp gnus-fetch-old-headers)
7407             (eq gnus-build-sparse-threads 'some)
7408             (eq gnus-build-sparse-threads 'more))
7409     (let ((th threads))
7410       (while th
7411         (setcar th (gnus-cut-thread (car th)))
7412         (setq th (cdr th)))))
7413   ;; Remove nixed out threads.
7414   (delq nil threads))
7415
7416 (defun gnus-summary-initial-limit (&optional show-if-empty)
7417   "Figure out what the initial limit is supposed to be on group entry.
7418 This entails weeding out unwanted dormants, low-scored articles,
7419 fetch-old-headers verbiage, and so on."
7420   ;; Most groups have nothing to remove.
7421   (if (or gnus-inhibit-limiting
7422           (and (null gnus-newsgroup-dormant)
7423                (eq gnus-newsgroup-display 'gnus-not-ignore)
7424                (not (eq gnus-fetch-old-headers 'some))
7425                (not (numberp gnus-fetch-old-headers))
7426                (not (eq gnus-fetch-old-headers 'invisible))
7427                (null gnus-summary-expunge-below)
7428                (not (eq gnus-build-sparse-threads 'some))
7429                (not (eq gnus-build-sparse-threads 'more))
7430                (null gnus-thread-expunge-below)
7431                (not gnus-use-nocem)))
7432       ()                                ; Do nothing.
7433     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7434     (setq gnus-newsgroup-limit nil)
7435     (mapatoms
7436      (lambda (node)
7437        (unless (car (symbol-value node))
7438          ;; These threads have no parents -- they are roots.
7439          (let ((nodes (cdr (symbol-value node)))
7440                thread)
7441            (while nodes
7442              (if (and gnus-thread-expunge-below
7443                       (< (gnus-thread-total-score (car nodes))
7444                          gnus-thread-expunge-below))
7445                  (gnus-expunge-thread (pop nodes))
7446                (setq thread (pop nodes))
7447                (gnus-summary-limit-children thread))))))
7448      gnus-newsgroup-dependencies)
7449     ;; If this limitation resulted in an empty group, we might
7450     ;; pop the previous limit and use it instead.
7451     (when (and (not gnus-newsgroup-limit)
7452                show-if-empty)
7453       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7454     gnus-newsgroup-limit))
7455
7456 (defun gnus-summary-limit-children (thread)
7457   "Return 1 if this subthread is visible and 0 if it is not."
7458   ;; First we get the number of visible children to this thread.  This
7459   ;; is done by recursing down the thread using this function, so this
7460   ;; will really go down to a leaf article first, before slowly
7461   ;; working its way up towards the root.
7462   (when thread
7463     (let ((children
7464            (if (cdr thread)
7465                (apply '+ (mapcar 'gnus-summary-limit-children
7466                                  (cdr thread)))
7467              0))
7468           (number (mail-header-number (car thread)))
7469           score)
7470       (if (and
7471            (not (memq number gnus-newsgroup-marked))
7472            (or
7473             ;; If this article is dormant and has absolutely no visible
7474             ;; children, then this article isn't visible.
7475             (and (memq number gnus-newsgroup-dormant)
7476                  (zerop children))
7477             ;; If this is "fetch-old-headered" and there is no
7478             ;; visible children, then we don't want this article.
7479             (and (or (eq gnus-fetch-old-headers 'some)
7480                      (numberp gnus-fetch-old-headers))
7481                  (gnus-summary-article-ancient-p number)
7482                  (zerop children))
7483             ;; If this is "fetch-old-headered" and `invisible', then
7484             ;; we don't want this article.
7485             (and (eq gnus-fetch-old-headers 'invisible)
7486                  (gnus-summary-article-ancient-p number))
7487             ;; If this is a sparsely inserted article with no children,
7488             ;; we don't want it.
7489             (and (eq gnus-build-sparse-threads 'some)
7490                  (gnus-summary-article-sparse-p number)
7491                  (zerop children))
7492             ;; If we use expunging, and this article is really
7493             ;; low-scored, then we don't want this article.
7494             (when (and gnus-summary-expunge-below
7495                        (< (setq score
7496                                 (or (cdr (assq number gnus-newsgroup-scored))
7497                                     gnus-summary-default-score))
7498                           gnus-summary-expunge-below))
7499               ;; We increase the expunge-tally here, but that has
7500               ;; nothing to do with the limits, really.
7501               (incf gnus-newsgroup-expunged-tally)
7502               ;; We also mark as read here, if that's wanted.
7503               (when (and gnus-summary-mark-below
7504                          (< score gnus-summary-mark-below))
7505                 (setq gnus-newsgroup-unreads
7506                       (delq number gnus-newsgroup-unreads))
7507                 (if gnus-newsgroup-auto-expire
7508                     (push number gnus-newsgroup-expirable)
7509                   (push (cons number gnus-low-score-mark)
7510                         gnus-newsgroup-reads)))
7511               t)
7512             ;; Do the `display' group parameter.
7513             (and gnus-newsgroup-display
7514                  (not (funcall gnus-newsgroup-display)))
7515             ;; Check NoCeM things.
7516             (if (and gnus-use-nocem
7517                      (gnus-nocem-unwanted-article-p
7518                       (mail-header-id (car thread))))
7519                 (progn
7520                   (setq gnus-newsgroup-unreads
7521                         (delq number gnus-newsgroup-unreads))
7522                   t))))
7523           ;; Nope, invisible article.
7524           0
7525         ;; Ok, this article is to be visible, so we add it to the limit
7526         ;; and return 1.
7527         (push number gnus-newsgroup-limit)
7528         1))))
7529
7530 (defun gnus-expunge-thread (thread)
7531   "Mark all articles in THREAD as read."
7532   (let* ((number (mail-header-number (car thread))))
7533     (incf gnus-newsgroup-expunged-tally)
7534     ;; We also mark as read here, if that's wanted.
7535     (setq gnus-newsgroup-unreads
7536           (delq number gnus-newsgroup-unreads))
7537     (if gnus-newsgroup-auto-expire
7538         (push number gnus-newsgroup-expirable)
7539       (push (cons number gnus-low-score-mark)
7540             gnus-newsgroup-reads)))
7541   ;; Go recursively through all subthreads.
7542   (mapcar 'gnus-expunge-thread (cdr thread)))
7543
7544 ;; Summary article oriented commands
7545
7546 (defun gnus-summary-refer-parent-article (n)
7547   "Refer parent article N times.
7548 If N is negative, go to ancestor -N instead.
7549 The difference between N and the number of articles fetched is returned."
7550   (interactive "p")
7551   (let ((skip 1)
7552         error header ref)
7553     (when (not (natnump n))
7554       (setq skip (abs n)
7555             n 1))
7556     (while (and (> n 0)
7557                 (not error))
7558       (setq header (gnus-summary-article-header))
7559       (if (and (eq (mail-header-number header)
7560                    (cdr gnus-article-current))
7561                (equal gnus-newsgroup-name
7562                       (car gnus-article-current)))
7563           ;; If we try to find the parent of the currently
7564           ;; displayed article, then we take a look at the actual
7565           ;; References header, since this is slightly more
7566           ;; reliable than the References field we got from the
7567           ;; server.
7568           (save-excursion
7569             (set-buffer gnus-original-article-buffer)
7570             (nnheader-narrow-to-headers)
7571             (unless (setq ref (message-fetch-field "references"))
7572               (setq ref (message-fetch-field "in-reply-to")))
7573             (widen))
7574         (setq ref
7575               ;; It's not the current article, so we take a bet on
7576               ;; the value we got from the server.
7577               (mail-header-references header)))
7578       (if (and ref
7579                (not (equal ref "")))
7580           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7581             (gnus-message 1 "Couldn't find parent"))
7582         (gnus-message 1 "No references in article %d"
7583                       (gnus-summary-article-number))
7584         (setq error t))
7585       (decf n))
7586     (gnus-summary-position-point)
7587     n))
7588
7589 (defun gnus-summary-refer-references ()
7590   "Fetch all articles mentioned in the References header.
7591 Return the number of articles fetched."
7592   (interactive)
7593   (let ((ref (mail-header-references (gnus-summary-article-header)))
7594         (current (gnus-summary-article-number))
7595         (n 0))
7596     (if (or (not ref)
7597             (equal ref ""))
7598         (error "No References in the current article")
7599       ;; For each Message-ID in the References header...
7600       (while (string-match "<[^>]*>" ref)
7601         (incf n)
7602         ;; ... fetch that article.
7603         (gnus-summary-refer-article
7604          (prog1 (match-string 0 ref)
7605            (setq ref (substring ref (match-end 0))))))
7606       (gnus-summary-goto-subject current)
7607       (gnus-summary-position-point)
7608       n)))
7609
7610 (defun gnus-summary-refer-thread (&optional limit)
7611   "Fetch all articles in the current thread.
7612 If LIMIT (the numerical prefix), fetch that many old headers instead
7613 of what's specified by the `gnus-refer-thread-limit' variable."
7614   (interactive "P")
7615   (let ((id (mail-header-id (gnus-summary-article-header)))
7616         (limit (if limit (prefix-numeric-value limit)
7617                  gnus-refer-thread-limit)))
7618     ;; We want to fetch LIMIT *old* headers, but we also have to
7619     ;; re-fetch all the headers in the current buffer, because many of
7620     ;; them may be undisplayed.  So we adjust LIMIT.
7621     (when (numberp limit)
7622       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7623     (unless (eq gnus-fetch-old-headers 'invisible)
7624       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7625       ;; Retrieve the headers and read them in.
7626       (if (eq (gnus-retrieve-headers
7627                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7628               'nov)
7629           (gnus-build-all-threads)
7630         (error "Can't fetch thread from backends that don't support NOV"))
7631       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7632     (gnus-summary-limit-include-thread id)))
7633
7634 (defun gnus-summary-refer-article (message-id)
7635   "Fetch an article specified by MESSAGE-ID."
7636   (interactive "sMessage-ID: ")
7637   (when (and (stringp message-id)
7638              (not (zerop (length message-id))))
7639     ;; Construct the correct Message-ID if necessary.
7640     ;; Suggested by tale@pawl.rpi.edu.
7641     (unless (string-match "^<" message-id)
7642       (setq message-id (concat "<" message-id)))
7643     (unless (string-match ">$" message-id)
7644       (setq message-id (concat message-id ">")))
7645     (let* ((header (gnus-id-to-header message-id))
7646            (sparse (and header
7647                         (gnus-summary-article-sparse-p
7648                          (mail-header-number header))
7649                         (memq (mail-header-number header)
7650                               gnus-newsgroup-limit)))
7651            number)
7652       (cond
7653        ;; If the article is present in the buffer we just go to it.
7654        ((and header
7655              (or (not (gnus-summary-article-sparse-p
7656                        (mail-header-number header)))
7657                  sparse))
7658         (prog1
7659             (gnus-summary-goto-article
7660              (mail-header-number header) nil t)
7661           (when sparse
7662             (gnus-summary-update-article (mail-header-number header)))))
7663        (t
7664         ;; We fetch the article.
7665         (catch 'found
7666           (dolist (gnus-override-method (gnus-refer-article-methods))
7667             (gnus-check-server gnus-override-method)
7668             ;; Fetch the header, and display the article.
7669             (when (setq number (gnus-summary-insert-subject message-id))
7670               (gnus-summary-select-article nil nil nil number)
7671               (throw 'found t)))
7672           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7673
7674 (defun gnus-refer-article-methods ()
7675   "Return a list of referrable methods."
7676   (cond
7677    ;; No method, so we default to current and native.
7678    ((null gnus-refer-article-method)
7679     (list gnus-current-select-method gnus-select-method))
7680    ;; Current.
7681    ((eq 'current gnus-refer-article-method)
7682     (list gnus-current-select-method))
7683    ;; List of select methods.
7684    ((not (and (symbolp (car gnus-refer-article-method))
7685               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7686     (let (out)
7687       (dolist (method gnus-refer-article-method)
7688         (push (if (eq 'current method)
7689                   gnus-current-select-method
7690                 method)
7691               out))
7692       (nreverse out)))
7693    ;; One single select method.
7694    (t
7695     (list gnus-refer-article-method))))
7696
7697 (defun gnus-summary-edit-parameters ()
7698   "Edit the group parameters of the current group."
7699   (interactive)
7700   (gnus-group-edit-group gnus-newsgroup-name 'params))
7701
7702 (defun gnus-summary-customize-parameters ()
7703   "Customize the group parameters of the current group."
7704   (interactive)
7705   (gnus-group-customize gnus-newsgroup-name))
7706
7707 (defun gnus-summary-enter-digest-group (&optional force)
7708   "Enter an nndoc group based on the current article.
7709 If FORCE, force a digest interpretation.  If not, try
7710 to guess what the document format is."
7711   (interactive "P")
7712   (let ((conf gnus-current-window-configuration))
7713     (save-excursion
7714       (gnus-summary-select-article))
7715     (setq gnus-current-window-configuration conf)
7716     (let* ((name (format "%s-%d"
7717                          (gnus-group-prefixed-name
7718                           gnus-newsgroup-name (list 'nndoc ""))
7719                          (save-excursion
7720                            (set-buffer gnus-summary-buffer)
7721                            gnus-current-article)))
7722            (ogroup gnus-newsgroup-name)
7723            (params (append (gnus-info-params (gnus-get-info ogroup))
7724                            (list (cons 'to-group ogroup))
7725                            (list (cons 'save-article-group ogroup))))
7726            (case-fold-search t)
7727            (buf (current-buffer))
7728            dig to-address)
7729       (save-excursion
7730         (set-buffer gnus-original-article-buffer)
7731         ;; Have the digest group inherit the main mail address of
7732         ;; the parent article.
7733         (when (setq to-address (or (message-fetch-field "reply-to")
7734                                    (message-fetch-field "from")))
7735           (setq params (append
7736                         (list (cons 'to-address
7737                                     (funcall gnus-decode-encoded-word-function
7738                                              to-address))))))
7739         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7740         (insert-buffer-substring gnus-original-article-buffer)
7741         ;; Remove lines that may lead nndoc to misinterpret the
7742         ;; document type.
7743         (narrow-to-region
7744          (goto-char (point-min))
7745          (or (search-forward "\n\n" nil t) (point)))
7746         (goto-char (point-min))
7747         (delete-matching-lines "^Path:\\|^From ")
7748         (widen))
7749       (unwind-protect
7750           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7751                     (gnus-newsgroup-ephemeral-ignored-charsets
7752                      gnus-newsgroup-ignored-charsets))
7753                 (gnus-group-read-ephemeral-group
7754                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7755                               (nndoc-article-type
7756                                ,(if force 'mbox 'guess)))
7757                  t nil nil nil
7758                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7759                                                         "ADAPT")))))
7760               ;; Make all postings to this group go to the parent group.
7761               (nconc (gnus-info-params (gnus-get-info name))
7762                      params)
7763             ;; Couldn't select this doc group.
7764             (switch-to-buffer buf)
7765             (gnus-set-global-variables)
7766             (gnus-configure-windows 'summary)
7767             (gnus-message 3 "Article couldn't be entered?"))
7768         (kill-buffer dig)))))
7769
7770 (defun gnus-summary-read-document (n)
7771   "Open a new group based on the current article(s).
7772 This will allow you to read digests and other similar
7773 documents as newsgroups.
7774 Obeys the standard process/prefix convention."
7775   (interactive "P")
7776   (let* ((articles (gnus-summary-work-articles n))
7777          (ogroup gnus-newsgroup-name)
7778          (params (append (gnus-info-params (gnus-get-info ogroup))
7779                          (list (cons 'to-group ogroup))))
7780          article group egroup groups vgroup)
7781     (while (setq article (pop articles))
7782       (setq group (format "%s-%d" gnus-newsgroup-name article))
7783       (gnus-summary-remove-process-mark article)
7784       (when (gnus-summary-display-article article)
7785         (save-excursion
7786           (with-temp-buffer
7787             (insert-buffer-substring gnus-original-article-buffer)
7788             ;; Remove some headers that may lead nndoc to make
7789             ;; the wrong guess.
7790             (message-narrow-to-head)
7791             (goto-char (point-min))
7792             (delete-matching-lines "^\\(Path\\):\\|^From ")
7793             (widen)
7794             (if (setq egroup
7795                       (gnus-group-read-ephemeral-group
7796                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7797                                      (nndoc-article-type guess))
7798                        t nil t))
7799                 (progn
7800                   ;; Make all postings to this group go to the parent group.
7801                   (nconc (gnus-info-params (gnus-get-info egroup))
7802                          params)
7803                   (push egroup groups))
7804               ;; Couldn't select this doc group.
7805               (gnus-error 3 "Article couldn't be entered"))))))
7806     ;; Now we have selected all the documents.
7807     (cond
7808      ((not groups)
7809       (error "None of the articles could be interpreted as documents"))
7810      ((gnus-group-read-ephemeral-group
7811        (setq vgroup (format
7812                      "nnvirtual:%s-%s" gnus-newsgroup-name
7813                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7814        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7815        t
7816        (cons (current-buffer) 'summary)))
7817      (t
7818       (error "Couldn't select virtual nndoc group")))))
7819
7820 (defun gnus-summary-isearch-article (&optional regexp-p)
7821   "Do incremental search forward on the current article.
7822 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7823   (interactive "P")
7824   (let* ((gnus-inhibit-treatment t)
7825          (old (gnus-summary-select-article)))
7826     (gnus-configure-windows 'article)
7827     (gnus-eval-in-buffer-window gnus-article-buffer
7828       (save-restriction
7829         (widen)
7830         (when (eq 'old old)
7831           (gnus-article-show-all-headers))
7832         (goto-char (point-min))
7833         (isearch-forward regexp-p)))))
7834
7835 (defun gnus-summary-search-article-forward (regexp &optional backward)
7836   "Search for an article containing REGEXP forward.
7837 If BACKWARD, search backward instead."
7838   (interactive
7839    (list (read-string
7840           (format "Search article %s (regexp%s): "
7841                   (if current-prefix-arg "backward" "forward")
7842                   (if gnus-last-search-regexp
7843                       (concat ", default " gnus-last-search-regexp)
7844                     "")))
7845          current-prefix-arg))
7846   (if (string-equal regexp "")
7847       (setq regexp (or gnus-last-search-regexp ""))
7848     (setq gnus-last-search-regexp regexp)
7849     (setq gnus-article-before-search gnus-current-article))
7850   ;; Intentionally set gnus-last-article.
7851   (setq gnus-last-article gnus-article-before-search)
7852   (let ((gnus-last-article gnus-last-article))
7853     (if (gnus-summary-search-article regexp backward)
7854         (gnus-summary-show-thread)
7855       (error "Search failed: \"%s\"" regexp))))
7856
7857 (defun gnus-summary-search-article-backward (regexp)
7858   "Search for an article containing REGEXP backward."
7859   (interactive
7860    (list (read-string
7861           (format "Search article backward (regexp%s): "
7862                   (if gnus-last-search-regexp
7863                       (concat ", default " gnus-last-search-regexp)
7864                     "")))))
7865   (gnus-summary-search-article-forward regexp 'backward))
7866
7867 (eval-when-compile
7868   (defmacro gnus-summary-search-article-position-point (regexp backward)
7869     "Dehighlight the last matched text and goto the beginning position."
7870     (` (if (and gnus-summary-search-article-matched-data
7871                 (let ((text (caddr gnus-summary-search-article-matched-data))
7872                       (inhibit-read-only t)
7873                       buffer-read-only)
7874                   (delete-region
7875                    (goto-char (car gnus-summary-search-article-matched-data))
7876                    (cadr gnus-summary-search-article-matched-data))
7877                   (insert text)
7878                   (string-match (, regexp) text)))
7879            (if (, backward) (beginning-of-line) (end-of-line))
7880          (goto-char (if (, backward) (point-max) (point-min))))))
7881
7882   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7883     "Place point where X-Face image is displayed."
7884     (if (featurep 'xemacs)
7885         (` (let ((end (if (search-forward "\n\n" nil t)
7886                           (goto-char (1- (point)))
7887                         (point-min)))
7888                  extent)
7889              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7890              (unless (and (re-search-forward "^From:" end t)
7891                           (setq extent (extent-at (point)))
7892                           (extent-begin-glyph extent))
7893                (goto-char (, opoint)))))
7894       (` (let ((end (if (search-forward "\n\n" nil t)
7895                         (goto-char (1- (point)))
7896                       (point-min)))
7897                (start (or (search-backward "\n\n" nil t) (point-min))))
7898            (goto-char
7899             (or (text-property-any start end 'x-face-image t);; x-face-e21
7900                 (text-property-any start end 'x-face-mule-bitmap-image t)
7901                 (, opoint)))))))
7902
7903   (defmacro gnus-summary-search-article-highlight-matched-text
7904     (backward treated x-face)
7905     "Highlight matched text in the function `gnus-summary-search-article'."
7906     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7907              (end (set-marker (make-marker) (match-end 0)))
7908              (inhibit-read-only t)
7909              buffer-read-only)
7910          (unless treated
7911            (let ((,@
7912                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7913                     (mapcar
7914                      (lambda (item) (setq items (delq item items)))
7915                      '(gnus-treat-buttonize
7916                        gnus-treat-fill-article
7917                        gnus-treat-fill-long-lines
7918                        gnus-treat-emphasize
7919                        gnus-treat-highlight-headers
7920                        gnus-treat-highlight-citation
7921                        gnus-treat-highlight-signature
7922                        gnus-treat-overstrike
7923                        gnus-treat-display-xface
7924                        gnus-treat-buttonize-head
7925                        gnus-treat-decode-article-as-default-mime-charset))
7926                     (static-if (featurep 'xemacs)
7927                         items
7928                       (cons '(x-face-mule-delete-x-face-field
7929                               (quote never))
7930                             items))))
7931                  (gnus-treat-display-xface
7932                   (when (, x-face) gnus-treat-display-xface)))
7933              (gnus-article-prepare-mime-display)))
7934          (goto-char (if (, backward) start end))
7935          (when (, x-face)
7936            (gnus-summary-search-article-highlight-goto-x-face (point)))
7937          (setq gnus-summary-search-article-matched-data
7938                (list start end (buffer-substring start end)))
7939          (unless (eq start end);; matched text has been deleted. :-<
7940            (put-text-property start end 'face
7941                               (or (find-face 'isearch)
7942                                   'secondary-selection))))))
7943   )
7944
7945 (defun gnus-summary-search-article (regexp &optional backward)
7946   "Search for an article containing REGEXP.
7947 Optional argument BACKWARD means do search for backward.
7948 `gnus-select-article-hook' is not called during the search."
7949   ;; We have to require this here to make sure that the following
7950   ;; dynamic binding isn't shadowed by autoloading.
7951   (require 'gnus-async)
7952   (require 'gnus-art)
7953   (let ((gnus-select-article-hook nil)  ;Disable hook.
7954         (gnus-article-prepare-hook nil)
7955         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7956         (gnus-use-article-prefetch nil)
7957         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7958         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7959         (sum (current-buffer))
7960         (found nil)
7961         point treated)
7962     (gnus-save-hidden-threads
7963       (static-if (featurep 'xemacs)
7964           (let ((gnus-inhibit-treatment t))
7965             (setq treated (eq 'old (gnus-summary-select-article)))
7966             (when (and treated
7967                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7968                                  (window-live-p (get-buffer-window
7969                                                  gnus-article-buffer t)))))
7970               (gnus-summary-select-article nil t)
7971               (setq treated nil)))
7972         (let ((gnus-inhibit-treatment t)
7973               (x-face-mule-delete-x-face-field 'never))
7974           (setq treated (eq 'old (gnus-summary-select-article)))
7975           (when (and treated
7976                      (not
7977                       (and (gnus-buffer-live-p gnus-article-buffer)
7978                            (window-live-p (get-buffer-window
7979                                            gnus-article-buffer t))
7980                            (or (not (string-match "^\\^X-Face:" regexp))
7981                                (with-current-buffer gnus-article-buffer
7982                                  gnus-summary-search-article-matched-data)))))
7983             (gnus-summary-select-article nil t)
7984             (setq treated nil))))
7985       (set-buffer gnus-article-buffer)
7986       (widen)
7987       (if treated
7988           (progn
7989             (gnus-article-show-all-headers)
7990             (gnus-summary-search-article-position-point regexp backward))
7991         (goto-char (if backward (point-max) (point-min))))
7992       (while (not found)
7993         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7994         (if (if backward
7995                 (re-search-backward regexp nil t)
7996               (re-search-forward regexp nil t))
7997             ;; We found the regexp.
7998             (progn
7999               (gnus-summary-search-article-highlight-matched-text
8000                backward treated (string-match "^\\^X-Face:" regexp))
8001               (setq found 'found)
8002               (forward-line
8003                (/ (- 2 (window-height
8004                         (get-buffer-window gnus-article-buffer t)))
8005                   2))
8006               (set-window-start
8007                (get-buffer-window (current-buffer))
8008                (point))
8009               (set-buffer sum)
8010               (setq point (point)))
8011           ;; We didn't find it, so we go to the next article.
8012           (set-buffer sum)
8013           (setq found 'not)
8014           (while (eq found 'not)
8015             (if (not (if backward (gnus-summary-find-prev)
8016                        (gnus-summary-find-next)))
8017                 ;; No more articles.
8018                 (setq found t)
8019               ;; Select the next article and adjust point.
8020               (unless (gnus-summary-article-sparse-p
8021                        (gnus-summary-article-number))
8022                 (setq found nil)
8023                 (let ((gnus-inhibit-treatment t))
8024                   (gnus-summary-select-article))
8025                 (setq treated nil)
8026                 (set-buffer gnus-article-buffer)
8027                 (widen)
8028                 (goto-char (if backward (point-max) (point-min))))))))
8029       (gnus-message 7 ""))
8030     ;; Return whether we found the regexp.
8031     (when (eq found 'found)
8032       (goto-char point)
8033       (gnus-summary-show-thread)
8034       (gnus-summary-goto-subject gnus-current-article)
8035       (gnus-summary-position-point)
8036       t)))
8037
8038 (defun gnus-find-matching-articles (header regexp)
8039   "Return a list of all articles that match REGEXP on HEADER.
8040 This search includes all articles in the current group that Gnus has
8041 fetched headers for, whether they are displayed or not."
8042   (let ((articles nil)
8043         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8044         (case-fold-search t))
8045     (dolist (header gnus-newsgroup-headers)
8046       (when (string-match regexp (funcall func header))
8047         (push (mail-header-number header) articles)))
8048     (nreverse articles)))
8049
8050 (defun gnus-summary-find-matching (header regexp &optional backward unread
8051                                           not-case-fold not-matching)
8052   "Return a list of all articles that match REGEXP on HEADER.
8053 The search stars on the current article and goes forwards unless
8054 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8055 If UNREAD is non-nil, only unread articles will
8056 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8057 in the comparisons. If NOT-MATCHING, return a list of all articles that
8058 not match REGEXP on HEADER."
8059   (let ((case-fold-search (not not-case-fold))
8060         articles d func)
8061     (if (consp header)
8062         (if (eq (car header) 'extra)
8063             (setq func
8064                   `(lambda (h)
8065                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8066                          "")))
8067           (error "%s is an invalid header" header))
8068       (unless (fboundp (intern (concat "mail-header-" header)))
8069         (error "%s is not a valid header" header))
8070       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8071     (dolist (d (if (eq backward 'all)
8072                    gnus-newsgroup-data
8073                  (gnus-data-find-list
8074                   (gnus-summary-article-number)
8075                   (gnus-data-list backward))))
8076       (when (and (or (not unread)       ; We want all articles...
8077                      (gnus-data-unread-p d)) ; Or just unreads.
8078                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8079                  (if not-matching
8080                      (not (string-match
8081                            regexp
8082                            (funcall func (gnus-data-header d))))
8083                    (string-match regexp
8084                                  (funcall func (gnus-data-header d)))))
8085         (push (gnus-data-number d) articles))) ; Success!
8086     (nreverse articles)))
8087
8088 (defun gnus-summary-execute-command (header regexp command &optional backward)
8089   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8090 If HEADER is an empty string (or nil), the match is done on the entire
8091 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8092   (interactive
8093    (list (let ((completion-ignore-case t))
8094            (completing-read
8095             "Header name: "
8096             (mapcar (lambda (header) (list (format "%s" header)))
8097                     (append
8098                      '("Number" "Subject" "From" "Lines" "Date"
8099                        "Message-ID" "Xref" "References" "Body")
8100                      gnus-extra-headers))
8101             nil 'require-match))
8102          (read-string "Regexp: ")
8103          (read-key-sequence "Command: ")
8104          current-prefix-arg))
8105   (when (equal header "Body")
8106     (setq header ""))
8107   ;; Hidden thread subtrees must be searched as well.
8108   (gnus-summary-show-all-threads)
8109   ;; We don't want to change current point nor window configuration.
8110   (save-excursion
8111     (save-window-excursion
8112       (gnus-message 6 "Executing %s..." (key-description command))
8113       ;; We'd like to execute COMMAND interactively so as to give arguments.
8114       (gnus-execute header regexp
8115                     `(call-interactively ',(key-binding command))
8116                     backward)
8117       (gnus-message 6 "Executing %s...done" (key-description command)))))
8118
8119 (defun gnus-summary-beginning-of-article ()
8120   "Scroll the article back to the beginning."
8121   (interactive)
8122   (gnus-summary-select-article)
8123   (gnus-configure-windows 'article)
8124   (gnus-eval-in-buffer-window gnus-article-buffer
8125     (widen)
8126     (goto-char (point-min))
8127     (when gnus-page-broken
8128       (gnus-narrow-to-page))))
8129
8130 (defun gnus-summary-end-of-article ()
8131   "Scroll to the end of the article."
8132   (interactive)
8133   (gnus-summary-select-article)
8134   (gnus-configure-windows 'article)
8135   (gnus-eval-in-buffer-window gnus-article-buffer
8136     (widen)
8137     (goto-char (point-max))
8138     (recenter -3)
8139     (when gnus-page-broken
8140       (gnus-narrow-to-page))))
8141
8142 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8143   "Truncate to LEN and quote all \"(\"'s in STRING."
8144   (gnus-replace-in-string (if (and len (> (length string) len))
8145                               (substring string 0 len)
8146                             string)
8147                           "[()]" "\\\\\\&"))
8148
8149 (defun gnus-summary-print-article (&optional filename n)
8150   "Generate and print a PostScript image of the N next (mail) articles.
8151
8152 If N is negative, print the N previous articles.  If N is nil and articles
8153 have been marked with the process mark, print these instead.
8154
8155 If the optional first argument FILENAME is nil, send the image to the
8156 printer.  If FILENAME is a string, save the PostScript image in a file with
8157 that name.  If FILENAME is a number, prompt the user for the name of the file
8158 to save in."
8159   (interactive (list (ps-print-preprint current-prefix-arg)))
8160   (dolist (article (gnus-summary-work-articles n))
8161     (gnus-summary-select-article nil nil 'pseudo article)
8162     (gnus-eval-in-buffer-window gnus-article-buffer
8163       (gnus-print-buffer))
8164     (gnus-summary-remove-process-mark article))
8165   (ps-despool filename))
8166
8167 (defun gnus-print-buffer ()
8168   (let ((buffer (generate-new-buffer " *print*")))
8169     (unwind-protect
8170         (progn
8171           (copy-to-buffer buffer (point-min) (point-max))
8172           (set-buffer buffer)
8173           (gnus-article-delete-invisible-text)
8174           (when (gnus-visual-p 'article-highlight 'highlight)
8175             ;; Copy-to-buffer doesn't copy overlay.  So redo
8176             ;; highlight.
8177             (let ((gnus-article-buffer buffer))
8178               (gnus-article-highlight-citation t)
8179               (gnus-article-highlight-signature)))
8180           (let ((ps-left-header
8181                  (list
8182                   (concat "("
8183                           (gnus-summary-print-truncate-and-quote
8184                            (mail-header-subject gnus-current-headers)
8185                            66) ")")
8186                   (concat "("
8187                           (gnus-summary-print-truncate-and-quote
8188                            (mail-header-from gnus-current-headers)
8189                            45) ")")))
8190                 (ps-right-header
8191                  (list
8192                   "/pagenumberstring load"
8193                   (concat "("
8194                           (mail-header-date gnus-current-headers) ")"))))
8195             (gnus-run-hooks 'gnus-ps-print-hook)
8196             (save-excursion
8197               (if window-system
8198                   (ps-spool-buffer-with-faces)
8199                 (ps-spool-buffer)))))
8200       (kill-buffer buffer))))
8201
8202 (defun gnus-summary-show-article (&optional arg)
8203   "Force redisplaying of the current article.
8204 If ARG (the prefix) is a number, show the article with the charset
8205 defined in `gnus-summary-show-article-charset-alist', or the charset
8206 input.
8207 If ARG (the prefix) is non-nil and not a number, show the raw article
8208 without any article massaging functions being run.  Normally, the key strokes
8209 are `C-u g'."
8210   (interactive "P")
8211   (cond
8212    ((numberp arg)
8213     (gnus-summary-show-article t)
8214     (let* ((gnus-newsgroup-charset
8215             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8216                 (mm-read-coding-system
8217                  "View as charset: " ;; actually it is coding system.
8218                  (save-excursion
8219                    (set-buffer gnus-article-buffer)
8220                    (mm-detect-coding-region (point) (point-max))))))
8221            (default-mime-charset gnus-newsgroup-charset)
8222            (gnus-newsgroup-ignored-charsets 'gnus-all))
8223       (gnus-summary-select-article nil 'force)
8224       (let ((deps gnus-newsgroup-dependencies)
8225             head header lines)
8226         (save-excursion
8227           (set-buffer gnus-original-article-buffer)
8228           (save-restriction
8229             (message-narrow-to-head)
8230             (setq head (buffer-string))
8231             (goto-char (point-min))
8232             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8233               (goto-char (point-max))
8234               (widen)
8235               (setq lines (1- (count-lines (point) (point-max))))))
8236           (with-temp-buffer
8237             (insert (format "211 %d Article retrieved.\n"
8238                             (cdr gnus-article-current)))
8239             (insert head)
8240             (if lines (insert (format "Lines: %d\n" lines)))
8241             (insert ".\n")
8242             (let ((nntp-server-buffer (current-buffer)))
8243               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8244         (gnus-data-set-header
8245          (gnus-data-find (cdr gnus-article-current))
8246          header)
8247         (gnus-summary-update-article-line
8248          (cdr gnus-article-current) header)
8249         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8250           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8251    ((not arg)
8252     ;; Select the article the normal way.
8253     (gnus-summary-select-article nil 'force))
8254    (t
8255     ;; We have to require this here to make sure that the following
8256     ;; dynamic binding isn't shadowed by autoloading.
8257     (require 'gnus-async)
8258     (require 'gnus-art)
8259     ;; Bind the article treatment functions to nil.
8260     (let ((gnus-have-all-headers t)
8261           gnus-article-prepare-hook
8262           gnus-article-decode-hook
8263           gnus-break-pages
8264           gnus-show-mime
8265           (gnus-inhibit-treatment t))
8266       (gnus-summary-select-article nil 'force))))
8267   (gnus-summary-goto-subject gnus-current-article)
8268   (gnus-summary-position-point))
8269
8270 (defun gnus-summary-show-raw-article ()
8271   "Show the raw article without any article massaging functions being run."
8272   (interactive)
8273   (gnus-summary-show-article t))
8274
8275 (defun gnus-summary-verbose-headers (&optional arg)
8276   "Toggle permanent full header display.
8277 If ARG is a positive number, turn header display on.
8278 If ARG is a negative number, turn header display off."
8279   (interactive "P")
8280   (setq gnus-show-all-headers
8281         (cond ((or (not (numberp arg))
8282                    (zerop arg))
8283                (not gnus-show-all-headers))
8284               ((natnump arg)
8285                t)))
8286   (gnus-summary-show-article))
8287
8288 (defun gnus-summary-toggle-header (&optional arg)
8289   "Show the headers if they are hidden, or hide them if they are shown.
8290 If ARG is a positive number, show the entire header.
8291 If ARG is a negative number, hide the unwanted header lines."
8292   (interactive "P")
8293   (save-excursion
8294     (set-buffer gnus-article-buffer)
8295     (save-restriction
8296       (let* ((buffer-read-only nil)
8297              (inhibit-point-motion-hooks t)
8298              hidden e)
8299         (setq hidden
8300               (if (numberp arg)
8301                   (>= arg 0)
8302                 (save-restriction
8303                   (article-narrow-to-head)
8304                   (gnus-article-hidden-text-p 'headers))))
8305         (goto-char (point-min))
8306         (when (search-forward "\n\n" nil t)
8307           (delete-region (point-min) (1- (point))))
8308         (goto-char (point-min))
8309         (save-excursion
8310           (set-buffer gnus-original-article-buffer)
8311           (goto-char (point-min))
8312           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8313         (insert-buffer-substring gnus-original-article-buffer 1 e)
8314         (save-restriction
8315           (narrow-to-region (point-min) (point))
8316           (article-decode-encoded-words)
8317           (if  hidden
8318               (let ((gnus-treat-hide-headers nil)
8319                     (gnus-treat-hide-boring-headers nil))
8320                 (gnus-delete-wash-type 'headers)
8321                 (gnus-treat-article 'head))
8322             (gnus-treat-article 'head)))
8323         (gnus-set-mode-line 'article)))))
8324
8325 (defun gnus-summary-show-all-headers ()
8326   "Make all header lines visible."
8327   (interactive)
8328   (gnus-summary-toggle-header 1))
8329
8330 (defun gnus-summary-toggle-mime (&optional arg)
8331   "Toggle MIME processing.
8332 If ARG is a positive number, turn MIME processing on."
8333   (interactive "P")
8334   (setq gnus-show-mime
8335         (if (null arg)
8336             (not gnus-show-mime)
8337           (> (prefix-numeric-value arg) 0)))
8338   (gnus-summary-select-article t 'force))
8339
8340 (defun gnus-summary-caesar-message (&optional arg)
8341   "Caesar rotate the current article by 13.
8342 The numerical prefix specifies how many places to rotate each letter
8343 forward."
8344   (interactive "P")
8345   (gnus-summary-select-article)
8346   (let ((mail-header-separator ""))
8347     (gnus-eval-in-buffer-window gnus-article-buffer
8348       (save-restriction
8349         (widen)
8350         (let ((start (window-start))
8351               buffer-read-only)
8352           (message-caesar-buffer-body arg)
8353           (set-window-start (get-buffer-window (current-buffer)) start))))))
8354
8355 (defun gnus-summary-stop-page-breaking ()
8356   "Stop page breaking in the current article."
8357   (interactive)
8358   (gnus-summary-select-article)
8359   (gnus-eval-in-buffer-window gnus-article-buffer
8360     (widen)
8361     (when (gnus-visual-p 'page-marker)
8362       (let ((buffer-read-only nil))
8363         (gnus-remove-text-with-property 'gnus-prev)
8364         (gnus-remove-text-with-property 'gnus-next))
8365       (setq gnus-page-broken nil))))
8366
8367 (defun gnus-summary-move-article (&optional n to-newsgroup
8368                                             select-method action)
8369   "Move the current article to a different newsgroup.
8370 If N is a positive number, move the N next articles.
8371 If N is a negative number, move the N previous articles.
8372 If N is nil and any articles have been marked with the process mark,
8373 move those articles instead.
8374 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8375 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8376 re-spool using this method.
8377
8378 For this function to work, both the current newsgroup and the
8379 newsgroup that you want to move to have to support the `request-move'
8380 and `request-accept' functions.
8381
8382 ACTION can be either `move' (the default), `crosspost' or `copy'."
8383   (interactive "P")
8384   (unless action
8385     (setq action 'move))
8386   ;; Check whether the source group supports the required functions.
8387   (cond ((and (eq action 'move)
8388               (not (gnus-check-backend-function
8389                     'request-move-article gnus-newsgroup-name)))
8390          (error "The current group does not support article moving"))
8391         ((and (eq action 'crosspost)
8392               (not (gnus-check-backend-function
8393                     'request-replace-article gnus-newsgroup-name)))
8394          (error "The current group does not support article editing")))
8395   (let ((articles (gnus-summary-work-articles n))
8396         (prefix (if (gnus-check-backend-function
8397                      'request-move-article gnus-newsgroup-name)
8398                     (gnus-group-real-prefix gnus-newsgroup-name)
8399                   ""))
8400         (names '((move "Move" "Moving")
8401                  (copy "Copy" "Copying")
8402                  (crosspost "Crosspost" "Crossposting")))
8403         (copy-buf (save-excursion
8404                     (nnheader-set-temp-buffer " *copy article*")))
8405         (default-marks gnus-article-mark-lists)
8406         (no-expire-marks (delete '(expirable . expire)
8407                                  (copy-sequence gnus-article-mark-lists)))
8408         art-group to-method new-xref article to-groups)
8409     (unless (assq action names)
8410       (error "Unknown action %s" action))
8411     ;; We have to select an article to give
8412     ;; `gnus-read-move-group-name' an opportunity to suggest an
8413     ;; appropriate default.
8414     (unless (gnus-buffer-live-p gnus-original-article-buffer)
8415       (let ((gnus-display-mime-function nil)
8416             (gnus-article-prepare-hook nil))
8417         (gnus-summary-select-article nil nil nil (car articles))))
8418     ;; Read the newsgroup name.
8419     (when (and (not to-newsgroup)
8420                (not select-method))
8421       (setq to-newsgroup
8422             (gnus-read-move-group-name
8423              (cadr (assq action names))
8424              (symbol-value (intern (format "gnus-current-%s-group" action)))
8425              articles prefix))
8426       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8427     (setq to-method (or select-method
8428                         (gnus-server-to-method
8429                          (gnus-group-method to-newsgroup))))
8430     ;; Check the method we are to move this article to...
8431     (unless (gnus-check-backend-function
8432              'request-accept-article (car to-method))
8433       (error "%s does not support article copying" (car to-method)))
8434     (unless (gnus-check-server to-method)
8435       (error "Can't open server %s" (car to-method)))
8436     (gnus-message 6 "%s to %s: %s..."
8437                   (caddr (assq action names))
8438                   (or (car select-method) to-newsgroup) articles)
8439     (while articles
8440       (setq article (pop articles))
8441       (setq
8442        art-group
8443        (cond
8444         ;; Move the article.
8445         ((eq action 'move)
8446          ;; Remove this article from future suppression.
8447          (gnus-dup-unsuppress-article article)
8448          (gnus-request-move-article
8449           article                       ; Article to move
8450           gnus-newsgroup-name           ; From newsgroup
8451           (nth 1 (gnus-find-method-for-group
8452                   gnus-newsgroup-name)) ; Server
8453           (list 'gnus-request-accept-article
8454                 to-newsgroup (list 'quote select-method)
8455                 (not articles) t)       ; Accept form
8456           (not articles)))              ; Only save nov last time
8457         ;; Copy the article.
8458         ((eq action 'copy)
8459          (save-excursion
8460            (set-buffer copy-buf)
8461            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8462              (gnus-request-accept-article
8463               to-newsgroup select-method (not articles) t))))
8464         ;; Crosspost the article.
8465         ((eq action 'crosspost)
8466          (let ((xref (message-tokenize-header
8467                       (mail-header-xref (gnus-summary-article-header article))
8468                       " ")))
8469            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8470                                   ":" (number-to-string article)))
8471            (unless xref
8472              (setq xref (list (system-name))))
8473            (setq new-xref
8474                  (concat
8475                   (mapconcat 'identity
8476                              (delete "Xref:" (delete new-xref xref))
8477                              " ")
8478                   " " new-xref))
8479            (save-excursion
8480              (set-buffer copy-buf)
8481              ;; First put the article in the destination group.
8482              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8483              (when (consp (setq art-group
8484                                 (gnus-request-accept-article
8485                                  to-newsgroup select-method (not articles))))
8486                (setq new-xref (concat new-xref " " (car art-group)
8487                                       ":"
8488                                       (number-to-string (cdr art-group))))
8489                ;; Now we have the new Xrefs header, so we insert
8490                ;; it and replace the new article.
8491                (nnheader-replace-header "Xref" new-xref)
8492                (gnus-request-replace-article
8493                 (cdr art-group) to-newsgroup (current-buffer))
8494                art-group))))))
8495       (cond
8496        ((not art-group)
8497         (gnus-message 1 "Couldn't %s article %s: %s"
8498                       (cadr (assq action names)) article
8499                       (nnheader-get-report (car to-method))))
8500        ((eq art-group 'junk)
8501         (when (eq action 'move)
8502           (gnus-summary-mark-article article gnus-canceled-mark)
8503           (gnus-message 4 "Deleted article %s" article)))
8504        (t
8505         (let* ((pto-group (gnus-group-prefixed-name
8506                            (car art-group) to-method))
8507                (entry
8508                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8509                (info (nth 2 entry))
8510                (to-group (gnus-info-group info))
8511                to-marks)
8512           ;; Update the group that has been moved to.
8513           (when (and info
8514                      (memq action '(move copy)))
8515             (unless (member to-group to-groups)
8516               (push to-group to-groups))
8517
8518             (unless (memq article gnus-newsgroup-unreads)
8519               (push 'read to-marks)
8520               (gnus-info-set-read
8521                info (gnus-add-to-range (gnus-info-read info)
8522                                        (list (cdr art-group)))))
8523
8524             ;; See whether the article is to be put in the cache.
8525             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8526                              default-marks
8527                            no-expire-marks))
8528                   (to-article (cdr art-group)))
8529
8530               ;; Enter the article into the cache in the new group,
8531               ;; if that is required.
8532               (when gnus-use-cache
8533                 (gnus-cache-possibly-enter-article
8534                  to-group to-article
8535                  (let ((header (copy-sequence
8536                                 (gnus-summary-article-header article))))
8537                    (mail-header-set-number header to-article)
8538                    header)
8539                  (memq article gnus-newsgroup-marked)
8540                  (memq article gnus-newsgroup-dormant)
8541                  (memq article gnus-newsgroup-unreads)))
8542
8543               (when gnus-preserve-marks
8544                 ;; Copy any marks over to the new group.
8545                 (when (and (equal to-group gnus-newsgroup-name)
8546                            (not (memq article gnus-newsgroup-unreads)))
8547                   ;; Mark this article as read in this group.
8548                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8549                   (setcdr (gnus-active to-group) to-article)
8550                   (setcdr gnus-newsgroup-active to-article))
8551
8552                 (while marks
8553                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8554                     (when (memq article (symbol-value
8555                                          (intern (format "gnus-newsgroup-%s"
8556                                                          (caar marks)))))
8557                       (push (cdar marks) to-marks)
8558                       ;; If the other group is the same as this group,
8559                       ;; then we have to add the mark to the list.
8560                       (when (equal to-group gnus-newsgroup-name)
8561                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8562                              (cons to-article
8563                                    (symbol-value
8564                                     (intern (format "gnus-newsgroup-%s"
8565                                                     (caar marks)))))))
8566                       ;; Copy the marks to other group.
8567                       (gnus-add-marked-articles
8568                        to-group (cdar marks) (list to-article) info)))
8569                   (setq marks (cdr marks)))
8570
8571                 (gnus-request-set-mark to-group (list (list (list to-article)
8572                                                             'add
8573                                                             to-marks))))
8574
8575               (gnus-dribble-enter
8576                (concat "(gnus-group-set-info '"
8577                        (gnus-prin1-to-string (gnus-get-info to-group))
8578                        ")"))))
8579
8580           ;; Update the Xref header in this article to point to
8581           ;; the new crossposted article we have just created.
8582           (when (eq action 'crosspost)
8583             (save-excursion
8584               (set-buffer copy-buf)
8585               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8586               (nnheader-replace-header "Xref" new-xref)
8587               (gnus-request-replace-article
8588                article gnus-newsgroup-name (current-buffer)))))
8589
8590         ;;;!!!Why is this necessary?
8591         (set-buffer gnus-summary-buffer)
8592
8593         (gnus-summary-goto-subject article)
8594         (when (eq action 'move)
8595           (gnus-summary-mark-article article gnus-canceled-mark))))
8596       (gnus-summary-remove-process-mark article))
8597     ;; Re-activate all groups that have been moved to.
8598     (save-excursion
8599       (set-buffer gnus-group-buffer)
8600       (let ((gnus-group-marked to-groups))
8601         (gnus-group-get-new-news-this-group nil t)))
8602
8603     (gnus-kill-buffer copy-buf)
8604     (gnus-summary-position-point)
8605     (gnus-set-mode-line 'summary)))
8606
8607 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8608   "Move the current article to a different newsgroup.
8609 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8610 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8611 re-spool using this method."
8612   (interactive "P")
8613   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8614
8615 (defun gnus-summary-crosspost-article (&optional n)
8616   "Crosspost the current article to some other group."
8617   (interactive "P")
8618   (gnus-summary-move-article n nil nil 'crosspost))
8619
8620 (defcustom gnus-summary-respool-default-method nil
8621   "Default method type for respooling an article.
8622 If nil, use to the current newsgroup method."
8623   :type 'symbol
8624   :group 'gnus-summary-mail)
8625
8626 (defun gnus-summary-respool-article (&optional n method)
8627   "Respool the current article.
8628 The article will be squeezed through the mail spooling process again,
8629 which means that it will be put in some mail newsgroup or other
8630 depending on `nnmail-split-methods'.
8631 If N is a positive number, respool the N next articles.
8632 If N is a negative number, respool the N previous articles.
8633 If N is nil and any articles have been marked with the process mark,
8634 respool those articles instead.
8635
8636 Respooling can be done both from mail groups and \"real\" newsgroups.
8637 In the former case, the articles in question will be moved from the
8638 current group into whatever groups they are destined to.  In the
8639 latter case, they will be copied into the relevant groups."
8640   (interactive
8641    (list current-prefix-arg
8642          (let* ((methods (gnus-methods-using 'respool))
8643                 (methname
8644                  (symbol-name (or gnus-summary-respool-default-method
8645                                   (car (gnus-find-method-for-group
8646                                         gnus-newsgroup-name)))))
8647                 (method
8648                  (gnus-completing-read-with-default
8649                   methname "What backend do you want to use when respooling?"
8650                   methods nil t nil 'gnus-mail-method-history))
8651                 ms)
8652            (cond
8653             ((zerop (length (setq ms (gnus-servers-using-backend
8654                                       (intern method)))))
8655              (list (intern method) ""))
8656             ((= 1 (length ms))
8657              (car ms))
8658             (t
8659              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8660                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8661                            ms-alist))))))))
8662   (unless method
8663     (error "No method given for respooling"))
8664   (if (assoc (symbol-name
8665               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8666              (gnus-methods-using 'respool))
8667       (gnus-summary-move-article n nil method)
8668     (gnus-summary-copy-article n nil method)))
8669
8670 (defun gnus-summary-import-article (file &optional edit)
8671   "Import an arbitrary file into a mail newsgroup."
8672   (interactive "fImport file: \nP")
8673   (let ((group gnus-newsgroup-name)
8674         (now (current-time))
8675         atts lines group-art)
8676     (unless (gnus-check-backend-function 'request-accept-article group)
8677       (error "%s does not support article importing" group))
8678     (or (file-readable-p file)
8679         (not (file-regular-p file))
8680         (error "Can't read %s" file))
8681     (save-excursion
8682       (set-buffer (gnus-get-buffer-create " *import file*"))
8683       (erase-buffer)
8684       (nnheader-insert-file-contents file)
8685       (goto-char (point-min))
8686       (if (nnheader-article-p)
8687           (save-restriction
8688             (goto-char (point-min))
8689             (search-forward "\n\n" nil t)
8690             (narrow-to-region (point-min) (1- (point)))
8691             (goto-char (point-min))
8692             (unless (re-search-forward "^date:" nil t)
8693               (goto-char (point-max))
8694               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8695         ;; This doesn't look like an article, so we fudge some headers.
8696         (setq atts (file-attributes file)
8697               lines (count-lines (point-min) (point-max)))
8698         (insert "From: " (read-string "From: ") "\n"
8699                 "Subject: " (read-string "Subject: ") "\n"
8700                 "Date: " (message-make-date (nth 5 atts)) "\n"
8701                 "Message-ID: " (message-make-message-id) "\n"
8702                 "Lines: " (int-to-string lines) "\n"
8703                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8704       (setq group-art (gnus-request-accept-article group nil t))
8705       (kill-buffer (current-buffer)))
8706     (setq gnus-newsgroup-active (gnus-activate-group group))
8707     (forward-line 1)
8708     (gnus-summary-goto-article (cdr group-art) nil t)
8709     (when edit
8710       (gnus-summary-edit-article))))
8711
8712 (defun gnus-summary-create-article ()
8713   "Create an article in a mail newsgroup."
8714   (interactive)
8715   (let ((group gnus-newsgroup-name)
8716         (now (current-time))
8717         group-art)
8718     (unless (gnus-check-backend-function 'request-accept-article group)
8719       (error "%s does not support article importing" group))
8720     (save-excursion
8721       (set-buffer (gnus-get-buffer-create " *import file*"))
8722       (erase-buffer)
8723       (goto-char (point-min))
8724       ;; This doesn't look like an article, so we fudge some headers.
8725       (insert "From: " (read-string "From: ") "\n"
8726               "Subject: " (read-string "Subject: ") "\n"
8727               "Date: " (message-make-date now) "\n"
8728               "Message-ID: " (message-make-message-id) "\n")
8729       (setq group-art (gnus-request-accept-article group nil t))
8730       (kill-buffer (current-buffer)))
8731     (setq gnus-newsgroup-active (gnus-activate-group group))
8732     (forward-line 1)
8733     (gnus-summary-goto-article (cdr group-art) nil t)
8734     (gnus-summary-edit-article)))
8735
8736 (defun gnus-summary-article-posted-p ()
8737   "Say whether the current (mail) article is available from news as well.
8738 This will be the case if the article has both been mailed and posted."
8739   (interactive)
8740   (let ((id (mail-header-references (gnus-summary-article-header)))
8741         (gnus-override-method (car (gnus-refer-article-methods))))
8742     (if (gnus-request-head id "")
8743         (gnus-message 2 "The current message was found on %s"
8744                       gnus-override-method)
8745       (gnus-message 2 "The current message couldn't be found on %s"
8746                     gnus-override-method)
8747       nil)))
8748
8749 (defun gnus-summary-expire-articles (&optional now)
8750   "Expire all articles that are marked as expirable in the current group."
8751   (interactive)
8752   (when (gnus-check-backend-function
8753          'request-expire-articles gnus-newsgroup-name)
8754     ;; This backend supports expiry.
8755     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8756            (expirable (if total
8757                           (progn
8758                             ;; We need to update the info for
8759                             ;; this group for `gnus-list-of-read-articles'
8760                             ;; to give us the right answer.
8761                             (gnus-run-hooks 'gnus-exit-group-hook)
8762                             (gnus-summary-update-info)
8763                             (gnus-list-of-read-articles gnus-newsgroup-name))
8764                         (setq gnus-newsgroup-expirable
8765                               (sort gnus-newsgroup-expirable '<))))
8766            (expiry-wait (if now 'immediate
8767                           (gnus-group-find-parameter
8768                            gnus-newsgroup-name 'expiry-wait)))
8769            (nnmail-expiry-target
8770             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8771                 nnmail-expiry-target))
8772            es)
8773       (when expirable
8774         ;; There are expirable articles in this group, so we run them
8775         ;; through the expiry process.
8776         (gnus-message 6 "Expiring articles...")
8777         (unless (gnus-check-group gnus-newsgroup-name)
8778           (error "Can't open server for %s" gnus-newsgroup-name))
8779         ;; The list of articles that weren't expired is returned.
8780         (save-excursion
8781           (if expiry-wait
8782               (let ((nnmail-expiry-wait-function nil)
8783                     (nnmail-expiry-wait expiry-wait))
8784                 (setq es (gnus-request-expire-articles
8785                           expirable gnus-newsgroup-name)))
8786             (setq es (gnus-request-expire-articles
8787                       expirable gnus-newsgroup-name)))
8788           (unless total
8789             (setq gnus-newsgroup-expirable es))
8790           ;; We go through the old list of expirable, and mark all
8791           ;; really expired articles as nonexistent.
8792           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8793             (let ((gnus-use-cache nil))
8794               (dolist (article expirable)
8795                 (when (and (not (memq article es))
8796                            (gnus-data-find article))
8797                   (gnus-summary-mark-article article gnus-canceled-mark))))))
8798         (gnus-message 6 "Expiring articles...done")))))
8799
8800 (defun gnus-summary-expire-articles-now ()
8801   "Expunge all expirable articles in the current group.
8802 This means that *all* articles that are marked as expirable will be
8803 deleted forever, right now."
8804   (interactive)
8805   (or gnus-expert-user
8806       (gnus-yes-or-no-p
8807        "Are you really, really, really sure you want to delete all these messages? ")
8808       (error "Phew!"))
8809   (gnus-summary-expire-articles t))
8810
8811 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8812 (defun gnus-summary-delete-article (&optional n)
8813   "Delete the N next (mail) articles.
8814 This command actually deletes articles.  This is not a marking
8815 command.  The article will disappear forever from your life, never to
8816 return.
8817 If N is negative, delete backwards.
8818 If N is nil and articles have been marked with the process mark,
8819 delete these instead."
8820   (interactive "P")
8821   (unless (gnus-check-backend-function 'request-expire-articles
8822                                        gnus-newsgroup-name)
8823     (error "The current newsgroup does not support article deletion"))
8824   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8825     (error "Couldn't open server"))
8826   ;; Compute the list of articles to delete.
8827   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8828         not-deleted)
8829     (if (and gnus-novice-user
8830              (not (gnus-yes-or-no-p
8831                    (format "Do you really want to delete %s forever? "
8832                            (if (> (length articles) 1)
8833                                (format "these %s articles" (length articles))
8834                              "this article")))))
8835         ()
8836       ;; Delete the articles.
8837       (setq not-deleted (gnus-request-expire-articles
8838                          articles gnus-newsgroup-name 'force))
8839       (while articles
8840         (gnus-summary-remove-process-mark (car articles))
8841         ;; The backend might not have been able to delete the article
8842         ;; after all.
8843         (unless (memq (car articles) not-deleted)
8844           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8845         (setq articles (cdr articles)))
8846       (when not-deleted
8847         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8848     (gnus-summary-position-point)
8849     (gnus-set-mode-line 'summary)
8850     not-deleted))
8851
8852 (defun gnus-summary-edit-article (&optional force)
8853   "Edit the current article.
8854 This will have permanent effect only in mail groups.
8855 If FORCE is non-nil, allow editing of articles even in read-only
8856 groups."
8857   (interactive "P")
8858   (save-excursion
8859     (set-buffer gnus-summary-buffer)
8860     (let ((mail-parse-charset gnus-newsgroup-charset)
8861           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8862       (gnus-set-global-variables)
8863       (when (and (not force)
8864                  (gnus-group-read-only-p))
8865         (error "The current newsgroup does not support article editing"))
8866       (gnus-summary-show-article t)
8867       (gnus-article-edit-article
8868        'ignore
8869        `(lambda (no-highlight)
8870           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8871                 (message-options message-options)
8872                 (message-options-set-recipient)
8873                 (mail-parse-ignored-charsets
8874                  ',gnus-newsgroup-ignored-charsets))
8875             (gnus-summary-edit-article-done
8876              ,(or (mail-header-references gnus-current-headers) "")
8877              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8878
8879 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8880
8881 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8882                                                  no-highlight)
8883   "Make edits to the current article permanent."
8884   (interactive)
8885   (save-excursion
8886     ;; The buffer restriction contains the entire article if it exists.
8887     (when (article-goto-body)
8888       (let ((lines (count-lines (point) (point-max)))
8889             (length (- (point-max) (point)))
8890             (case-fold-search t)
8891             (body (copy-marker (point))))
8892         (goto-char (point-min))
8893         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8894           (delete-region (match-beginning 1) (match-end 1))
8895           (insert (number-to-string length)))
8896         (goto-char (point-min))
8897         (when (re-search-forward
8898                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8899           (delete-region (match-beginning 1) (match-end 1))
8900           (insert (number-to-string length)))
8901         (goto-char (point-min))
8902         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8903           (delete-region (match-beginning 1) (match-end 1))
8904           (insert (number-to-string lines))))))
8905   ;; Replace the article.
8906   (let ((buf (current-buffer)))
8907     (with-temp-buffer
8908       (insert-buffer-substring buf)
8909
8910       (if (and (not read-only)
8911                (not (gnus-request-replace-article
8912                      (cdr gnus-article-current) (car gnus-article-current)
8913                      (current-buffer) t)))
8914           (error "Couldn't replace article")
8915         ;; Update the summary buffer.
8916         (if (and references
8917                  (equal (message-tokenize-header references " ")
8918                         (message-tokenize-header
8919                          (or (message-fetch-field "references") "") " ")))
8920             ;; We only have to update this line.
8921             (save-excursion
8922               (save-restriction
8923                 (message-narrow-to-head)
8924                 (let ((head (buffer-string))
8925                       header)
8926                   (with-temp-buffer
8927                     (insert (format "211 %d Article retrieved.\n"
8928                                     (cdr gnus-article-current)))
8929                     (insert head)
8930                     (insert ".\n")
8931                     (let ((nntp-server-buffer (current-buffer)))
8932                       (setq header (car (gnus-get-newsgroup-headers
8933                                          (save-excursion
8934                                            (set-buffer gnus-summary-buffer)
8935                                            gnus-newsgroup-dependencies)
8936                                          t))))
8937                     (save-excursion
8938                       (set-buffer gnus-summary-buffer)
8939                       (gnus-data-set-header
8940                        (gnus-data-find (cdr gnus-article-current))
8941                        header)
8942                       (gnus-summary-update-article-line
8943                        (cdr gnus-article-current) header)
8944                       (if (gnus-summary-goto-subject
8945                            (cdr gnus-article-current) nil t)
8946                           (gnus-summary-update-secondary-mark
8947                            (cdr gnus-article-current))))))))
8948           ;; Update threads.
8949           (set-buffer (or buffer gnus-summary-buffer))
8950           (gnus-summary-update-article (cdr gnus-article-current))
8951           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8952               (gnus-summary-update-secondary-mark
8953                (cdr gnus-article-current))))
8954         ;; Prettify the article buffer again.
8955         (unless no-highlight
8956           (save-excursion
8957             (set-buffer gnus-article-buffer)
8958             ;;;!!! Fix this -- article should be rehighlighted.
8959             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8960             (set-buffer gnus-original-article-buffer)
8961             (gnus-request-article
8962              (cdr gnus-article-current)
8963              (car gnus-article-current) (current-buffer))))
8964         ;; Prettify the summary buffer line.
8965         (when (gnus-visual-p 'summary-highlight 'highlight)
8966           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8967
8968 (defun gnus-summary-edit-wash (key)
8969   "Perform editing command KEY in the article buffer."
8970   (interactive
8971    (list
8972     (progn
8973       (message "%s" (concat (this-command-keys) "- "))
8974       (read-char))))
8975   (message "")
8976   (gnus-summary-edit-article)
8977   (execute-kbd-macro (concat (this-command-keys) key))
8978   (gnus-article-edit-done))
8979
8980 ;;; Respooling
8981
8982 (defun gnus-summary-respool-query (&optional silent trace)
8983   "Query where the respool algorithm would put this article."
8984   (interactive)
8985   (let (gnus-mark-article-hook)
8986     (gnus-summary-select-article)
8987     (save-excursion
8988       (set-buffer gnus-original-article-buffer)
8989       (save-restriction
8990         (message-narrow-to-head)
8991         (let ((groups (nnmail-article-group 'identity trace)))
8992           (unless silent
8993             (if groups
8994                 (message "This message would go to %s"
8995                          (mapconcat 'car groups ", "))
8996               (message "This message would go to no groups"))
8997             groups))))))
8998
8999 (defun gnus-summary-respool-trace ()
9000   "Trace where the respool algorithm would put this article.
9001 Display a buffer showing all fancy splitting patterns which matched."
9002   (interactive)
9003   (gnus-summary-respool-query nil t))
9004
9005 ;; Summary marking commands.
9006
9007 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9008   "Mark articles which has the same subject as read, and then select the next.
9009 If UNMARK is positive, remove any kind of mark.
9010 If UNMARK is negative, tick articles."
9011   (interactive "P")
9012   (when unmark
9013     (setq unmark (prefix-numeric-value unmark)))
9014   (let ((count
9015          (gnus-summary-mark-same-subject
9016           (gnus-summary-article-subject) unmark)))
9017     ;; Select next unread article.  If auto-select-same mode, should
9018     ;; select the first unread article.
9019     (gnus-summary-next-article t (and gnus-auto-select-same
9020                                       (gnus-summary-article-subject)))
9021     (gnus-message 7 "%d article%s marked as %s"
9022                   count (if (= count 1) " is" "s are")
9023                   (if unmark "unread" "read"))))
9024
9025 (defun gnus-summary-kill-same-subject (&optional unmark)
9026   "Mark articles which has the same subject as read.
9027 If UNMARK is positive, remove any kind of mark.
9028 If UNMARK is negative, tick articles."
9029   (interactive "P")
9030   (when unmark
9031     (setq unmark (prefix-numeric-value unmark)))
9032   (let ((count
9033          (gnus-summary-mark-same-subject
9034           (gnus-summary-article-subject) unmark)))
9035     ;; If marked as read, go to next unread subject.
9036     (when (null unmark)
9037       ;; Go to next unread subject.
9038       (gnus-summary-next-subject 1 t))
9039     (gnus-message 7 "%d articles are marked as %s"
9040                   count (if unmark "unread" "read"))))
9041
9042 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9043   "Mark articles with same SUBJECT as read, and return marked number.
9044 If optional argument UNMARK is positive, remove any kinds of marks.
9045 If optional argument UNMARK is negative, mark articles as unread instead."
9046   (let ((count 1))
9047     (save-excursion
9048       (cond
9049        ((null unmark)                   ; Mark as read.
9050         (while (and
9051                 (progn
9052                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9053                   (gnus-summary-show-thread) t)
9054                 (gnus-summary-find-subject subject))
9055           (setq count (1+ count))))
9056        ((> unmark 0)                    ; Tick.
9057         (while (and
9058                 (progn
9059                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9060                   (gnus-summary-show-thread) t)
9061                 (gnus-summary-find-subject subject))
9062           (setq count (1+ count))))
9063        (t                               ; Mark as unread.
9064         (while (and
9065                 (progn
9066                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9067                   (gnus-summary-show-thread) t)
9068                 (gnus-summary-find-subject subject))
9069           (setq count (1+ count)))))
9070       (gnus-set-mode-line 'summary)
9071       ;; Return the number of marked articles.
9072       count)))
9073
9074 (defun gnus-summary-mark-as-processable (n &optional unmark)
9075   "Set the process mark on the next N articles.
9076 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9077 the process mark instead.  The difference between N and the actual
9078 number of articles marked is returned."
9079   (interactive "P")
9080   (if (and (null n) (gnus-region-active-p))
9081       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9082     (setq n (prefix-numeric-value n))
9083     (let ((backward (< n 0))
9084           (n (abs n)))
9085       (while (and
9086               (> n 0)
9087               (if unmark
9088                   (gnus-summary-remove-process-mark
9089                    (gnus-summary-article-number))
9090                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9091               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9092         (setq n (1- n)))
9093       (when (/= 0 n)
9094         (gnus-message 7 "No more articles"))
9095       (gnus-summary-recenter)
9096       (gnus-summary-position-point)
9097       n)))
9098
9099 (defun gnus-summary-unmark-as-processable (n)
9100   "Remove the process mark from the next N articles.
9101 If N is negative, unmark backward instead.  The difference between N and
9102 the actual number of articles unmarked is returned."
9103   (interactive "P")
9104   (gnus-summary-mark-as-processable n t))
9105
9106 (defun gnus-summary-unmark-all-processable ()
9107   "Remove the process mark from all articles."
9108   (interactive)
9109   (save-excursion
9110     (while gnus-newsgroup-processable
9111       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9112   (gnus-summary-position-point))
9113
9114 (defun gnus-summary-add-mark (article type)
9115   "Mark ARTICLE with a mark of TYPE."
9116   (let ((vtype (car (assq type gnus-article-mark-lists)))
9117         var)
9118     (if (not vtype)
9119         (error "No such mark type: %s" type)
9120       (setq var (intern (format "gnus-newsgroup-%s" type)))
9121       (set var (cons article (symbol-value var)))
9122       (if (memq type '(processable cached replied forwarded recent saved))
9123           (gnus-summary-update-secondary-mark article)
9124         ;;; !!! This is bogus.  We should find out what primary
9125         ;;; !!! mark we want to set.
9126         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9127
9128 (defun gnus-summary-mark-as-expirable (n)
9129   "Mark N articles forward as expirable.
9130 If N is negative, mark backward instead.  The difference between N and
9131 the actual number of articles marked is returned."
9132   (interactive "p")
9133   (gnus-summary-mark-forward n gnus-expirable-mark))
9134
9135 (defun gnus-summary-mark-article-as-replied (article)
9136   "Mark ARTICLE as replied to and update the summary line.
9137 ARTICLE can also be a list of articles."
9138   (interactive (list (gnus-summary-article-number)))
9139   (let ((articles (if (listp article) article (list article))))
9140     (dolist (article articles)
9141       (push article gnus-newsgroup-replied)
9142       (let ((buffer-read-only nil))
9143         (when (gnus-summary-goto-subject article nil t)
9144           (gnus-summary-update-secondary-mark article))))))
9145
9146 (defun gnus-summary-mark-article-as-forwarded (article)
9147   "Mark ARTICLE as forwarded and update the summary line.
9148 ARTICLE can also be a list of articles."
9149   (let ((articles (if (listp article) article (list article))))
9150     (dolist (article articles)
9151       (push article gnus-newsgroup-forwarded)
9152       (let ((buffer-read-only nil))
9153         (when (gnus-summary-goto-subject article nil t)
9154           (gnus-summary-update-secondary-mark article))))))
9155
9156 (defun gnus-summary-set-bookmark (article)
9157   "Set a bookmark in current article."
9158   (interactive (list (gnus-summary-article-number)))
9159   (when (or (not (get-buffer gnus-article-buffer))
9160             (not gnus-current-article)
9161             (not gnus-article-current)
9162             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9163     (error "No current article selected"))
9164   ;; Remove old bookmark, if one exists.
9165   (let ((old (assq article gnus-newsgroup-bookmarks)))
9166     (when old
9167       (setq gnus-newsgroup-bookmarks
9168             (delq old gnus-newsgroup-bookmarks))))
9169   ;; Set the new bookmark, which is on the form
9170   ;; (article-number . line-number-in-body).
9171   (push
9172    (cons article
9173          (save-excursion
9174            (set-buffer gnus-article-buffer)
9175            (count-lines
9176             (min (point)
9177                  (save-excursion
9178                    (goto-char (point-min))
9179                    (search-forward "\n\n" nil t)
9180                    (point)))
9181             (point))))
9182    gnus-newsgroup-bookmarks)
9183   (gnus-message 6 "A bookmark has been added to the current article."))
9184
9185 (defun gnus-summary-remove-bookmark (article)
9186   "Remove the bookmark from the current article."
9187   (interactive (list (gnus-summary-article-number)))
9188   ;; Remove old bookmark, if one exists.
9189   (let ((old (assq article gnus-newsgroup-bookmarks)))
9190     (if old
9191         (progn
9192           (setq gnus-newsgroup-bookmarks
9193                 (delq old gnus-newsgroup-bookmarks))
9194           (gnus-message 6 "Removed bookmark."))
9195       (gnus-message 6 "No bookmark in current article."))))
9196
9197 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9198 (defun gnus-summary-mark-as-dormant (n)
9199   "Mark N articles forward as dormant.
9200 If N is negative, mark backward instead.  The difference between N and
9201 the actual number of articles marked is returned."
9202   (interactive "p")
9203   (gnus-summary-mark-forward n gnus-dormant-mark))
9204
9205 (defun gnus-summary-set-process-mark (article)
9206   "Set the process mark on ARTICLE and update the summary line."
9207   (setq gnus-newsgroup-processable
9208         (cons article
9209               (delq article gnus-newsgroup-processable)))
9210   (when (gnus-summary-goto-subject article)
9211     (gnus-summary-show-thread)
9212     (gnus-summary-goto-subject article)
9213     (gnus-summary-update-secondary-mark article)))
9214
9215 (defun gnus-summary-remove-process-mark (article)
9216   "Remove the process mark from ARTICLE and update the summary line."
9217   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9218   (when (gnus-summary-goto-subject article)
9219     (gnus-summary-show-thread)
9220     (gnus-summary-goto-subject article)
9221     (gnus-summary-update-secondary-mark article)))
9222
9223 (defun gnus-summary-set-saved-mark (article)
9224   "Set the process mark on ARTICLE and update the summary line."
9225   (push article gnus-newsgroup-saved)
9226   (when (gnus-summary-goto-subject article)
9227     (gnus-summary-update-secondary-mark article)))
9228
9229 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9230   "Mark N articles as read forwards.
9231 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9232 The difference between N and the actual number of articles marked is
9233 returned.
9234 Iff NO-EXPIRE, auto-expiry will be inhibited."
9235   (interactive "p")
9236   (gnus-summary-show-thread)
9237   (let ((backward (< n 0))
9238         (gnus-summary-goto-unread
9239          (and gnus-summary-goto-unread
9240               (not (eq gnus-summary-goto-unread 'never))
9241               (not (memq mark (list gnus-unread-mark
9242                                     gnus-ticked-mark gnus-dormant-mark)))))
9243         (n (abs n))
9244         (mark (or mark gnus-del-mark)))
9245     (while (and (> n 0)
9246                 (gnus-summary-mark-article nil mark no-expire)
9247                 (zerop (gnus-summary-next-subject
9248                         (if backward -1 1)
9249                         (and gnus-summary-goto-unread
9250                              (not (eq gnus-summary-goto-unread 'never)))
9251                         t)))
9252       (setq n (1- n)))
9253     (when (/= 0 n)
9254       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9255     (gnus-summary-recenter)
9256     (gnus-summary-position-point)
9257     (gnus-set-mode-line 'summary)
9258     n))
9259
9260 (defun gnus-summary-mark-article-as-read (mark)
9261   "Mark the current article quickly as read with MARK."
9262   (let ((article (gnus-summary-article-number)))
9263     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9264     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9265     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9266     (push (cons article mark) gnus-newsgroup-reads)
9267     ;; Possibly remove from cache, if that is used.
9268     (when gnus-use-cache
9269       (gnus-cache-enter-remove-article article))
9270     ;; Allow the backend to change the mark.
9271     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9272     ;; Check for auto-expiry.
9273     (when (and gnus-newsgroup-auto-expire
9274                (memq mark gnus-auto-expirable-marks))
9275       (setq mark gnus-expirable-mark)
9276       ;; Let the backend know about the mark change.
9277       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9278       (push article gnus-newsgroup-expirable))
9279     ;; Set the mark in the buffer.
9280     (gnus-summary-update-mark mark 'unread)
9281     t))
9282
9283 (defun gnus-summary-mark-article-as-unread (mark)
9284   "Mark the current article quickly as unread with MARK."
9285   (let* ((article (gnus-summary-article-number))
9286          (old-mark (gnus-summary-article-mark article)))
9287     ;; Allow the backend to change the mark.
9288     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9289     (if (eq mark old-mark)
9290         t
9291       (if (<= article 0)
9292           (progn
9293             (gnus-error 1 "Can't mark negative article numbers")
9294             nil)
9295         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9296         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9297         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9298         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9299         (cond ((= mark gnus-ticked-mark)
9300                (push article gnus-newsgroup-marked))
9301               ((= mark gnus-dormant-mark)
9302                (push article gnus-newsgroup-dormant))
9303               (t
9304                (push article gnus-newsgroup-unreads)))
9305         (gnus-pull article gnus-newsgroup-reads)
9306
9307         ;; See whether the article is to be put in the cache.
9308         (and gnus-use-cache
9309              (vectorp (gnus-summary-article-header article))
9310              (save-excursion
9311                (gnus-cache-possibly-enter-article
9312                 gnus-newsgroup-name article
9313                 (gnus-summary-article-header article)
9314                 (= mark gnus-ticked-mark)
9315                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9316
9317         ;; Fix the mark.
9318         (gnus-summary-update-mark mark 'unread)
9319         t))))
9320
9321 (defun gnus-summary-mark-article (&optional article mark no-expire)
9322   "Mark ARTICLE with MARK.  MARK can be any character.
9323 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9324 `??' (dormant) and `?E' (expirable).
9325 If MARK is nil, then the default character `?r' is used.
9326 If ARTICLE is nil, then the article on the current line will be
9327 marked.
9328 Iff NO-EXPIRE, auto-expiry will be inhibited."
9329   ;; The mark might be a string.
9330   (when (stringp mark)
9331     (setq mark (aref mark 0)))
9332   ;; If no mark is given, then we check auto-expiring.
9333   (when (null mark)
9334     (setq mark gnus-del-mark))
9335   (when (and (not no-expire)
9336              gnus-newsgroup-auto-expire
9337              (memq mark gnus-auto-expirable-marks))
9338     (setq mark gnus-expirable-mark))
9339   (let ((article (or article (gnus-summary-article-number)))
9340         (old-mark (gnus-summary-article-mark article)))
9341     ;; Allow the backend to change the mark.
9342     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9343     (if (eq mark old-mark)
9344         t
9345       (unless article
9346         (error "No article on current line"))
9347       (if (not (if (or (= mark gnus-unread-mark)
9348                        (= mark gnus-ticked-mark)
9349                        (= mark gnus-dormant-mark))
9350                    (gnus-mark-article-as-unread article mark)
9351                  (gnus-mark-article-as-read article mark)))
9352           t
9353         ;; See whether the article is to be put in the cache.
9354         (and gnus-use-cache
9355              (not (= mark gnus-canceled-mark))
9356              (vectorp (gnus-summary-article-header article))
9357              (save-excursion
9358                (gnus-cache-possibly-enter-article
9359                 gnus-newsgroup-name article
9360                 (gnus-summary-article-header article)
9361                 (= mark gnus-ticked-mark)
9362                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9363
9364         (when (gnus-summary-goto-subject article nil t)
9365           (let ((buffer-read-only nil))
9366             (gnus-summary-show-thread)
9367             ;; Fix the mark.
9368             (gnus-summary-update-mark mark 'unread)
9369             t))))))
9370
9371 (defun gnus-summary-update-secondary-mark (article)
9372   "Update the secondary (read, process, cache) mark."
9373   (gnus-summary-update-mark
9374    (cond ((memq article gnus-newsgroup-processable)
9375           gnus-process-mark)
9376          ((memq article gnus-newsgroup-cached)
9377           gnus-cached-mark)
9378          ((memq article gnus-newsgroup-replied)
9379           gnus-replied-mark)
9380          ((memq article gnus-newsgroup-forwarded)
9381           gnus-forwarded-mark)
9382          ((memq article gnus-newsgroup-saved)
9383           gnus-saved-mark)
9384          ((memq article gnus-newsgroup-recent)
9385           gnus-recent-mark)
9386          ((memq article gnus-newsgroup-unseen)
9387           gnus-unseen-mark)
9388          (t gnus-no-mark))
9389    'replied)
9390   (when (gnus-visual-p 'summary-highlight 'highlight)
9391     (gnus-run-hooks 'gnus-summary-update-hook))
9392   t)
9393
9394 (defun gnus-summary-update-mark (mark type)
9395   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9396         (buffer-read-only nil))
9397     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9398     (when forward
9399       (when (looking-at "\r")
9400         (incf forward))
9401       (when (<= (+ forward (point)) (point-max))
9402         ;; Go to the right position on the line.
9403         (goto-char (+ forward (point)))
9404         ;; Replace the old mark with the new mark.
9405         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9406         ;; Optionally update the marks by some user rule.
9407         (when (eq type 'unread)
9408           (gnus-data-set-mark
9409            (gnus-data-find (gnus-summary-article-number)) mark)
9410           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9411
9412 (defun gnus-mark-article-as-read (article &optional mark)
9413   "Enter ARTICLE in the pertinent lists and remove it from others."
9414   ;; Make the article expirable.
9415   (let ((mark (or mark gnus-del-mark)))
9416     (if (= mark gnus-expirable-mark)
9417         (push article gnus-newsgroup-expirable)
9418       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9419     ;; Remove from unread and marked lists.
9420     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9421     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9422     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9423     (push (cons article mark) gnus-newsgroup-reads)
9424     ;; Possibly remove from cache, if that is used.
9425     (when gnus-use-cache
9426       (gnus-cache-enter-remove-article article))
9427     t))
9428
9429 (defun gnus-mark-article-as-unread (article &optional mark)
9430   "Enter ARTICLE in the pertinent lists and remove it from others."
9431   (let ((mark (or mark gnus-ticked-mark)))
9432     (if (<= article 0)
9433         (progn
9434           (gnus-error 1 "Can't mark negative article numbers")
9435           nil)
9436       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9437             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9438             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9439             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9440
9441       ;; Unsuppress duplicates?
9442       (when gnus-suppress-duplicates
9443         (gnus-dup-unsuppress-article article))
9444
9445       (cond ((= mark gnus-ticked-mark)
9446              (push article gnus-newsgroup-marked))
9447             ((= mark gnus-dormant-mark)
9448              (push article gnus-newsgroup-dormant))
9449             (t
9450              (push article gnus-newsgroup-unreads)))
9451       (gnus-pull article gnus-newsgroup-reads)
9452       t)))
9453
9454 (defalias 'gnus-summary-mark-as-unread-forward
9455   'gnus-summary-tick-article-forward)
9456 (make-obsolete 'gnus-summary-mark-as-unread-forward
9457                'gnus-summary-tick-article-forward)
9458 (defun gnus-summary-tick-article-forward (n)
9459   "Tick N articles forwards.
9460 If N is negative, tick backwards instead.
9461 The difference between N and the number of articles ticked is returned."
9462   (interactive "p")
9463   (gnus-summary-mark-forward n gnus-ticked-mark))
9464
9465 (defalias 'gnus-summary-mark-as-unread-backward
9466   'gnus-summary-tick-article-backward)
9467 (make-obsolete 'gnus-summary-mark-as-unread-backward
9468                'gnus-summary-tick-article-backward)
9469 (defun gnus-summary-tick-article-backward (n)
9470   "Tick N articles backwards.
9471 The difference between N and the number of articles ticked is returned."
9472   (interactive "p")
9473   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9474
9475 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9476 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9477 (defun gnus-summary-tick-article (&optional article clear-mark)
9478   "Mark current article as unread.
9479 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9480 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9481   (interactive)
9482   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9483                                        gnus-ticked-mark)))
9484
9485 (defun gnus-summary-mark-as-read-forward (n)
9486   "Mark N articles as read forwards.
9487 If N is negative, mark backwards instead.
9488 The difference between N and the actual number of articles marked is
9489 returned."
9490   (interactive "p")
9491   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9492
9493 (defun gnus-summary-mark-as-read-backward (n)
9494   "Mark the N articles as read backwards.
9495 The difference between N and the actual number of articles marked is
9496 returned."
9497   (interactive "p")
9498   (gnus-summary-mark-forward
9499    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9500
9501 (defun gnus-summary-mark-as-read (&optional article mark)
9502   "Mark current article as read.
9503 ARTICLE specifies the article to be marked as read.
9504 MARK specifies a string to be inserted at the beginning of the line."
9505   (gnus-summary-mark-article article mark))
9506
9507 (defun gnus-summary-clear-mark-forward (n)
9508   "Clear marks from N articles forward.
9509 If N is negative, clear backward instead.
9510 The difference between N and the number of marks cleared is returned."
9511   (interactive "p")
9512   (gnus-summary-mark-forward n gnus-unread-mark))
9513
9514 (defun gnus-summary-clear-mark-backward (n)
9515   "Clear marks from N articles backward.
9516 The difference between N and the number of marks cleared is returned."
9517   (interactive "p")
9518   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9519
9520 (defun gnus-summary-mark-unread-as-read ()
9521   "Intended to be used by `gnus-summary-mark-article-hook'."
9522   (when (memq gnus-current-article gnus-newsgroup-unreads)
9523     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9524
9525 (defun gnus-summary-mark-read-and-unread-as-read ()
9526   "Intended to be used by `gnus-summary-mark-article-hook'."
9527   (let ((mark (gnus-summary-article-mark)))
9528     (when (or (gnus-unread-mark-p mark)
9529               (gnus-read-mark-p mark))
9530       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9531
9532 (defun gnus-summary-mark-unread-as-ticked ()
9533   "Intended to be used by `gnus-summary-mark-article-hook'."
9534   (when (memq gnus-current-article gnus-newsgroup-unreads)
9535     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9536
9537 (defun gnus-summary-mark-region-as-read (point mark all)
9538   "Mark all unread articles between point and mark as read.
9539 If given a prefix, mark all articles between point and mark as read,
9540 even ticked and dormant ones."
9541   (interactive "r\nP")
9542   (save-excursion
9543     (let (article)
9544       (goto-char point)
9545       (beginning-of-line)
9546       (while (and
9547               (< (point) mark)
9548               (progn
9549                 (when (or all
9550                           (memq (setq article (gnus-summary-article-number))
9551                                 gnus-newsgroup-unreads))
9552                   (gnus-summary-mark-article article gnus-del-mark))
9553                 t)
9554               (gnus-summary-find-next))))))
9555
9556 (defun gnus-summary-mark-below (score mark)
9557   "Mark articles with score less than SCORE with MARK."
9558   (interactive "P\ncMark: ")
9559   (setq score (if score
9560                   (prefix-numeric-value score)
9561                 (or gnus-summary-default-score 0)))
9562   (save-excursion
9563     (set-buffer gnus-summary-buffer)
9564     (goto-char (point-min))
9565     (while
9566         (progn
9567           (and (< (gnus-summary-article-score) score)
9568                (gnus-summary-mark-article nil mark))
9569           (gnus-summary-find-next)))))
9570
9571 (defun gnus-summary-kill-below (&optional score)
9572   "Mark articles with score below SCORE as read."
9573   (interactive "P")
9574   (gnus-summary-mark-below score gnus-killed-mark))
9575
9576 (defun gnus-summary-clear-above (&optional score)
9577   "Clear all marks from articles with score above SCORE."
9578   (interactive "P")
9579   (gnus-summary-mark-above score gnus-unread-mark))
9580
9581 (defun gnus-summary-tick-above (&optional score)
9582   "Tick all articles with score above SCORE."
9583   (interactive "P")
9584   (gnus-summary-mark-above score gnus-ticked-mark))
9585
9586 (defun gnus-summary-mark-above (score mark)
9587   "Mark articles with score over SCORE with MARK."
9588   (interactive "P\ncMark: ")
9589   (setq score (if score
9590                   (prefix-numeric-value score)
9591                 (or gnus-summary-default-score 0)))
9592   (save-excursion
9593     (set-buffer gnus-summary-buffer)
9594     (goto-char (point-min))
9595     (while (and (progn
9596                   (when (> (gnus-summary-article-score) score)
9597                     (gnus-summary-mark-article nil mark))
9598                   t)
9599                 (gnus-summary-find-next)))))
9600
9601 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9602 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9603 (defun gnus-summary-limit-include-expunged (&optional no-error)
9604   "Display all the hidden articles that were expunged for low scores."
9605   (interactive)
9606   (let ((buffer-read-only nil))
9607     (let ((scored gnus-newsgroup-scored)
9608           headers h)
9609       (while scored
9610         (unless (gnus-summary-article-header (caar scored))
9611           (and (setq h (gnus-number-to-header (caar scored)))
9612                (< (cdar scored) gnus-summary-expunge-below)
9613                (push h headers)))
9614         (setq scored (cdr scored)))
9615       (if (not headers)
9616           (when (not no-error)
9617             (error "No expunged articles hidden"))
9618         (goto-char (point-min))
9619         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9620         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9621         (mapcar (lambda (x) (push (mail-header-number x)
9622                                   gnus-newsgroup-limit))
9623                 headers)
9624         (gnus-summary-prepare-unthreaded (nreverse headers))
9625         (goto-char (point-min))
9626         (gnus-summary-position-point)
9627         t))))
9628
9629 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9630   "Mark all unread articles in this newsgroup as read.
9631 If prefix argument ALL is non-nil, ticked and dormant articles will
9632 also be marked as read.
9633 If QUIETLY is non-nil, no questions will be asked.
9634 If TO-HERE is non-nil, it should be a point in the buffer.  All
9635 articles before (after, if REVERSE is set) this point will be marked as read.
9636 Note that this function will only catch up the unread article
9637 in the current summary buffer limitation.
9638 The number of articles marked as read is returned."
9639   (interactive "P")
9640   (prog1
9641       (save-excursion
9642         (when (or quietly
9643                   (not gnus-interactive-catchup) ;Without confirmation?
9644                   gnus-expert-user
9645                   (gnus-y-or-n-p
9646                    (if all
9647                        "Mark absolutely all articles as read? "
9648                      "Mark all unread articles as read? ")))
9649           (if (and not-mark
9650                    (not gnus-newsgroup-adaptive)
9651                    (not gnus-newsgroup-auto-expire)
9652                    (not gnus-suppress-duplicates)
9653                    (or (not gnus-use-cache)
9654                        (eq gnus-use-cache 'passive)))
9655               (progn
9656                 (when all
9657                   (setq gnus-newsgroup-marked nil
9658                         gnus-newsgroup-dormant nil))
9659                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9660             ;; We actually mark all articles as canceled, which we
9661             ;; have to do when using auto-expiry or adaptive scoring.
9662             (gnus-summary-show-all-threads)
9663             (if (and to-here reverse)
9664                 (progn
9665                   (goto-char to-here)
9666                   (while (and
9667                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9668                           (gnus-summary-find-next (not all) nil nil t))))
9669               (when (gnus-summary-first-subject (not all) t)
9670                 (while (and
9671                         (if to-here (< (point) to-here) t)
9672                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9673                         (gnus-summary-find-next (not all) nil nil t)))))
9674             (gnus-set-mode-line 'summary))
9675           t))
9676     (gnus-summary-position-point)))
9677
9678 (defun gnus-summary-catchup-to-here (&optional all)
9679   "Mark all unticked articles before the current one as read.
9680 If ALL is non-nil, also mark ticked and dormant articles as read."
9681   (interactive "P")
9682   (save-excursion
9683     (gnus-save-hidden-threads
9684       (let ((beg (point)))
9685         ;; We check that there are unread articles.
9686         (when (or all (gnus-summary-find-prev))
9687           (gnus-summary-catchup all t beg)))))
9688   (gnus-summary-position-point))
9689
9690 (defun gnus-summary-catchup-from-here (&optional all)
9691   "Mark all unticked articles after the current one as read.
9692 If ALL is non-nil, also mark ticked and dormant articles as read."
9693   (interactive "P")
9694   (save-excursion
9695     (gnus-save-hidden-threads
9696       (let ((beg (point)))
9697         ;; We check that there are unread articles.
9698         (when (or all (gnus-summary-find-next))
9699           (gnus-summary-catchup all t beg nil t)))))
9700   (gnus-summary-position-point))
9701
9702 (defun gnus-summary-catchup-all (&optional quietly)
9703   "Mark all articles in this newsgroup as read."
9704   (interactive "P")
9705   (gnus-summary-catchup t quietly))
9706
9707 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9708   "Mark all unread articles in this group as read, then exit.
9709 If prefix argument ALL is non-nil, all articles are marked as read.
9710 If QUIETLY is non-nil, no questions will be asked."
9711   (interactive "P")
9712   (when (gnus-summary-catchup all quietly nil 'fast)
9713     ;; Select next newsgroup or exit.
9714     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9715              (eq gnus-auto-select-next 'quietly))
9716         (gnus-summary-next-group nil)
9717       (gnus-summary-exit))))
9718
9719 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9720   "Mark all articles in this newsgroup as read, and then exit."
9721   (interactive "P")
9722   (gnus-summary-catchup-and-exit t quietly))
9723
9724 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9725   "Mark all articles in this group as read and select the next group.
9726 If given a prefix, mark all articles, unread as well as ticked, as
9727 read."
9728   (interactive "P")
9729   (save-excursion
9730     (gnus-summary-catchup all))
9731   (gnus-summary-next-group))
9732
9733 ;;;
9734 ;;; with article
9735 ;;;
9736
9737 (defmacro gnus-with-article (article &rest forms)
9738   "Select ARTICLE and perform FORMS in the original article buffer.
9739 Then replace the article with the result."
9740   `(progn
9741      ;; We don't want the article to be marked as read.
9742      (let (gnus-mark-article-hook)
9743        (gnus-summary-select-article t t nil ,article))
9744      (set-buffer gnus-original-article-buffer)
9745      ,@forms
9746      (if (not (gnus-check-backend-function
9747                'request-replace-article (car gnus-article-current)))
9748          (gnus-message 5 "Read-only group; not replacing")
9749        (unless (gnus-request-replace-article
9750                 ,article (car gnus-article-current)
9751                 (current-buffer) t)
9752          (error "Couldn't replace article")))
9753      ;; The cache and backlog have to be flushed somewhat.
9754      (when gnus-keep-backlog
9755        (gnus-backlog-remove-article
9756         (car gnus-article-current) (cdr gnus-article-current)))
9757      (when gnus-use-cache
9758        (gnus-cache-update-article
9759         (car gnus-article-current) (cdr gnus-article-current)))))
9760
9761 (put 'gnus-with-article 'lisp-indent-function 1)
9762 (put 'gnus-with-article 'edebug-form-spec '(form body))
9763
9764 ;; Thread-based commands.
9765
9766 (defun gnus-summary-articles-in-thread (&optional article)
9767   "Return a list of all articles in the current thread.
9768 If ARTICLE is non-nil, return all articles in the thread that starts
9769 with that article."
9770   (let* ((article (or article (gnus-summary-article-number)))
9771          (data (gnus-data-find-list article))
9772          (top-level (gnus-data-level (car data)))
9773          (top-subject
9774           (cond ((null gnus-thread-operation-ignore-subject)
9775                  (gnus-simplify-subject-re
9776                   (mail-header-subject (gnus-data-header (car data)))))
9777                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9778                  (gnus-simplify-subject-fuzzy
9779                   (mail-header-subject (gnus-data-header (car data)))))
9780                 (t nil)))
9781          (end-point (save-excursion
9782                       (if (gnus-summary-go-to-next-thread)
9783                           (point) (point-max))))
9784          articles)
9785     (while (and data
9786                 (< (gnus-data-pos (car data)) end-point))
9787       (when (or (not top-subject)
9788                 (string= top-subject
9789                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9790                              (gnus-simplify-subject-fuzzy
9791                               (mail-header-subject
9792                                (gnus-data-header (car data))))
9793                            (gnus-simplify-subject-re
9794                             (mail-header-subject
9795                              (gnus-data-header (car data)))))))
9796         (push (gnus-data-number (car data)) articles))
9797       (unless (and (setq data (cdr data))
9798                    (> (gnus-data-level (car data)) top-level))
9799         (setq data nil)))
9800     ;; Return the list of articles.
9801     (nreverse articles)))
9802
9803 (defun gnus-summary-rethread-current ()
9804   "Rethread the thread the current article is part of."
9805   (interactive)
9806   (let* ((gnus-show-threads t)
9807          (article (gnus-summary-article-number))
9808          (id (mail-header-id (gnus-summary-article-header)))
9809          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9810     (unless id
9811       (error "No article on the current line"))
9812     (gnus-rebuild-thread id)
9813     (gnus-summary-goto-subject article)))
9814
9815 (defun gnus-summary-reparent-thread ()
9816   "Make the current article child of the marked (or previous) article.
9817
9818 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9819 is non-nil or the Subject: of both articles are the same."
9820   (interactive)
9821   (unless (not (gnus-group-read-only-p))
9822     (error "The current newsgroup does not support article editing"))
9823   (unless (<= (length gnus-newsgroup-processable) 1)
9824     (error "No more than one article may be marked"))
9825   (save-window-excursion
9826     (let ((gnus-article-buffer " *reparent*")
9827           (current-article (gnus-summary-article-number))
9828           ;; First grab the marked article, otherwise one line up.
9829           (parent-article (if (not (null gnus-newsgroup-processable))
9830                               (car gnus-newsgroup-processable)
9831                             (save-excursion
9832                               (if (eq (forward-line -1) 0)
9833                                   (gnus-summary-article-number)
9834                                 (error "Beginning of summary buffer"))))))
9835       (unless (not (eq current-article parent-article))
9836         (error "An article may not be self-referential"))
9837       (let ((message-id (mail-header-id
9838                          (gnus-summary-article-header parent-article))))
9839         (unless (and message-id (not (equal message-id "")))
9840           (error "No message-id in desired parent"))
9841         (gnus-with-article current-article
9842           (save-restriction
9843             (goto-char (point-min))
9844             (message-narrow-to-head)
9845             (if (re-search-forward "^References: " nil t)
9846                 (progn
9847                   (re-search-forward "^[^ \t]" nil t)
9848                   (forward-line -1)
9849                   (end-of-line)
9850                   (insert " " message-id))
9851               (insert "References: " message-id "\n"))))
9852         (set-buffer gnus-summary-buffer)
9853         (gnus-summary-unmark-all-processable)
9854         (gnus-summary-update-article current-article)
9855         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9856             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9857         (gnus-summary-rethread-current)
9858         (gnus-message 3 "Article %d is now the child of article %d"
9859                       current-article parent-article)))))
9860
9861 (defun gnus-summary-toggle-threads (&optional arg)
9862   "Toggle showing conversation threads.
9863 If ARG is positive number, turn showing conversation threads on."
9864   (interactive "P")
9865   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9866     (setq gnus-show-threads
9867           (if (null arg) (not gnus-show-threads)
9868             (> (prefix-numeric-value arg) 0)))
9869     (gnus-summary-prepare)
9870     (gnus-summary-goto-subject current)
9871     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9872     (gnus-summary-position-point)))
9873
9874 (defun gnus-summary-show-all-threads ()
9875   "Show all threads."
9876   (interactive)
9877   (save-excursion
9878     (let ((buffer-read-only nil))
9879       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9880   (gnus-summary-position-point))
9881
9882 (defun gnus-summary-show-thread ()
9883   "Show thread subtrees.
9884 Returns nil if no thread was there to be shown."
9885   (interactive)
9886   (let ((buffer-read-only nil)
9887         (orig (point))
9888         ;; first goto end then to beg, to have point at beg after let
9889         (end (progn (end-of-line) (point)))
9890         (beg (progn (beginning-of-line) (point))))
9891     (prog1
9892         ;; Any hidden lines here?
9893         (search-forward "\r" end t)
9894       (subst-char-in-region beg end ?\^M ?\n t)
9895       (goto-char orig)
9896       (gnus-summary-position-point))))
9897
9898 (defun gnus-summary-maybe-hide-threads ()
9899   "If requested, hide the threads that should be hidden."
9900   (when (and gnus-show-threads
9901              gnus-thread-hide-subtree)
9902     (gnus-summary-hide-all-threads
9903      (if (or (consp gnus-thread-hide-subtree)
9904              (gnus-functionp gnus-thread-hide-subtree))
9905          (gnus-make-predicate gnus-thread-hide-subtree)
9906        nil))))
9907
9908 ;;; Hiding predicates.
9909
9910 (defun gnus-article-unread-p (header)
9911   (memq (mail-header-number header) gnus-newsgroup-unreads))
9912
9913 (defun gnus-article-unseen-p (header)
9914   (memq (mail-header-number header) gnus-newsgroup-unseen))
9915
9916 (defun gnus-map-articles (predicate articles)
9917   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
9918   (apply 'gnus-or (mapcar predicate
9919                           (mapcar 'gnus-summary-article-header articles))))
9920
9921 (defun gnus-summary-hide-all-threads (&optional predicate)
9922   "Hide all thread subtrees.
9923 If PREDICATE is supplied, threads that satisfy this predicate
9924 will not be hidden."
9925   (interactive)
9926   (save-excursion
9927     (goto-char (point-min))
9928     (let ((end nil))
9929       (while (not end)
9930         (when (or (not predicate)
9931                   (gnus-map-articles
9932                    predicate (gnus-summary-article-children)))
9933             (gnus-summary-hide-thread))
9934         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
9935   (gnus-summary-position-point))
9936
9937 (defun gnus-summary-hide-thread ()
9938   "Hide thread subtrees.
9939 If PREDICATE is supplied, threads that satisfy this predicate
9940 will not be hidden.
9941 Returns nil if no threads were there to be hidden."
9942   (interactive)
9943   (let ((buffer-read-only nil)
9944         (start (point))
9945         (article (gnus-summary-article-number)))
9946     (goto-char start)
9947     ;; Go forward until either the buffer ends or the subthread
9948     ;; ends.
9949     (when (and (not (eobp))
9950                (or (zerop (gnus-summary-next-thread 1 t))
9951                    (goto-char (point-max))))
9952       (prog1
9953           (if (and (> (point) start)
9954                    (search-backward "\n" start t))
9955               (progn
9956                 (subst-char-in-region start (point) ?\n ?\^M)
9957                 (gnus-summary-goto-subject article))
9958             (goto-char start)
9959             nil)))))
9960
9961 (defun gnus-summary-go-to-next-thread (&optional previous)
9962   "Go to the same level (or less) next thread.
9963 If PREVIOUS is non-nil, go to previous thread instead.
9964 Return the article number moved to, or nil if moving was impossible."
9965   (let ((level (gnus-summary-thread-level))
9966         (way (if previous -1 1))
9967         (beg (point)))
9968     (forward-line way)
9969     (while (and (not (eobp))
9970                 (< level (gnus-summary-thread-level)))
9971       (forward-line way))
9972     (if (eobp)
9973         (progn
9974           (goto-char beg)
9975           nil)
9976       (setq beg (point))
9977       (prog1
9978           (gnus-summary-article-number)
9979         (goto-char beg)))))
9980
9981 (defun gnus-summary-next-thread (n &optional silent)
9982   "Go to the same level next N'th thread.
9983 If N is negative, search backward instead.
9984 Returns the difference between N and the number of skips actually
9985 done.
9986
9987 If SILENT, don't output messages."
9988   (interactive "p")
9989   (let ((backward (< n 0))
9990         (n (abs n)))
9991     (while (and (> n 0)
9992                 (gnus-summary-go-to-next-thread backward))
9993       (decf n))
9994     (unless silent
9995       (gnus-summary-position-point))
9996     (when (and (not silent) (/= 0 n))
9997       (gnus-message 7 "No more threads"))
9998     n))
9999
10000 (defun gnus-summary-prev-thread (n)
10001   "Go to the same level previous N'th thread.
10002 Returns the difference between N and the number of skips actually
10003 done."
10004   (interactive "p")
10005   (gnus-summary-next-thread (- n)))
10006
10007 (defun gnus-summary-go-down-thread ()
10008   "Go down one level in the current thread."
10009   (let ((children (gnus-summary-article-children)))
10010     (when children
10011       (gnus-summary-goto-subject (car children)))))
10012
10013 (defun gnus-summary-go-up-thread ()
10014   "Go up one level in the current thread."
10015   (let ((parent (gnus-summary-article-parent)))
10016     (when parent
10017       (gnus-summary-goto-subject parent))))
10018
10019 (defun gnus-summary-down-thread (n)
10020   "Go down thread N steps.
10021 If N is negative, go up instead.
10022 Returns the difference between N and how many steps down that were
10023 taken."
10024   (interactive "p")
10025   (let ((up (< n 0))
10026         (n (abs n)))
10027     (while (and (> n 0)
10028                 (if up (gnus-summary-go-up-thread)
10029                   (gnus-summary-go-down-thread)))
10030       (setq n (1- n)))
10031     (gnus-summary-position-point)
10032     (when (/= 0 n)
10033       (gnus-message 7 "Can't go further"))
10034     n))
10035
10036 (defun gnus-summary-up-thread (n)
10037   "Go up thread N steps.
10038 If N is negative, go down instead.
10039 Returns the difference between N and how many steps down that were
10040 taken."
10041   (interactive "p")
10042   (gnus-summary-down-thread (- n)))
10043
10044 (defun gnus-summary-top-thread ()
10045   "Go to the top of the thread."
10046   (interactive)
10047   (while (gnus-summary-go-up-thread))
10048   (gnus-summary-article-number))
10049
10050 (defun gnus-summary-kill-thread (&optional unmark)
10051   "Mark articles under current thread as read.
10052 If the prefix argument is positive, remove any kinds of marks.
10053 If the prefix argument is negative, tick articles instead."
10054   (interactive "P")
10055   (when unmark
10056     (setq unmark (prefix-numeric-value unmark)))
10057   (let ((articles (gnus-summary-articles-in-thread)))
10058     (save-excursion
10059       ;; Expand the thread.
10060       (gnus-summary-show-thread)
10061       ;; Mark all the articles.
10062       (while articles
10063         (gnus-summary-goto-subject (car articles))
10064         (cond ((null unmark)
10065                (gnus-summary-mark-article-as-read gnus-killed-mark))
10066               ((> unmark 0)
10067                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10068               (t
10069                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10070         (setq articles (cdr articles))))
10071     ;; Hide killed subtrees.
10072     (and (null unmark)
10073          gnus-thread-hide-killed
10074          (gnus-summary-hide-thread))
10075     ;; If marked as read, go to next unread subject.
10076     (when (null unmark)
10077       ;; Go to next unread subject.
10078       (gnus-summary-next-subject 1 t)))
10079   (gnus-set-mode-line 'summary))
10080
10081 ;; Summary sorting commands
10082
10083 (defun gnus-summary-sort-by-number (&optional reverse)
10084   "Sort the summary buffer by article number.
10085 Argument REVERSE means reverse order."
10086   (interactive "P")
10087   (gnus-summary-sort 'number reverse))
10088
10089 (defun gnus-summary-sort-by-author (&optional reverse)
10090   "Sort the summary buffer by author name alphabetically.
10091 If `case-fold-search' is non-nil, case of letters is ignored.
10092 Argument REVERSE means reverse order."
10093   (interactive "P")
10094   (gnus-summary-sort 'author reverse))
10095
10096 (defun gnus-summary-sort-by-subject (&optional reverse)
10097   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10098 If `case-fold-search' is non-nil, case of letters is ignored.
10099 Argument REVERSE means reverse order."
10100   (interactive "P")
10101   (gnus-summary-sort 'subject reverse))
10102
10103 (defun gnus-summary-sort-by-date (&optional reverse)
10104   "Sort the summary buffer by date.
10105 Argument REVERSE means reverse order."
10106   (interactive "P")
10107   (gnus-summary-sort 'date reverse))
10108
10109 (defun gnus-summary-sort-by-score (&optional reverse)
10110   "Sort the summary buffer by score.
10111 Argument REVERSE means reverse order."
10112   (interactive "P")
10113   (gnus-summary-sort 'score reverse))
10114
10115 (defun gnus-summary-sort-by-lines (&optional reverse)
10116   "Sort the summary buffer by the number of lines.
10117 Argument REVERSE means reverse order."
10118   (interactive "P")
10119   (gnus-summary-sort 'lines reverse))
10120
10121 (defun gnus-summary-sort-by-chars (&optional reverse)
10122   "Sort the summary buffer by article length.
10123 Argument REVERSE means reverse order."
10124   (interactive "P")
10125   (gnus-summary-sort 'chars reverse))
10126
10127 (defun gnus-summary-sort-by-original (&optional reverse)
10128   "Sort the summary buffer using the default sorting method.
10129 Argument REVERSE means reverse order."
10130   (interactive "P")
10131   (let* ((buffer-read-only)
10132          (gnus-summary-prepare-hook nil))
10133     ;; We do the sorting by regenerating the threads.
10134     (gnus-summary-prepare)
10135     ;; Hide subthreads if needed.
10136     (gnus-summary-maybe-hide-threads)))
10137
10138 (defun gnus-summary-sort (predicate reverse)
10139   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10140   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10141          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10142          (gnus-thread-sort-functions
10143           (if (not reverse)
10144               thread
10145             `(lambda (t1 t2)
10146                (,thread t2 t1))))
10147          (gnus-sort-gathered-threads-function
10148           gnus-thread-sort-functions)
10149          (gnus-article-sort-functions
10150           (if (not reverse)
10151               article
10152             `(lambda (t1 t2)
10153                (,article t2 t1))))
10154          (buffer-read-only)
10155          (gnus-summary-prepare-hook nil))
10156     ;; We do the sorting by regenerating the threads.
10157     (gnus-summary-prepare)
10158     ;; Hide subthreads if needed.
10159     (gnus-summary-maybe-hide-threads)))
10160
10161 ;; Summary saving commands.
10162
10163 (defun gnus-summary-save-article (&optional n not-saved)
10164   "Save the current article using the default saver function.
10165 If N is a positive number, save the N next articles.
10166 If N is a negative number, save the N previous articles.
10167 If N is nil and any articles have been marked with the process mark,
10168 save those articles instead.
10169 The variable `gnus-default-article-saver' specifies the saver function."
10170   (interactive "P")
10171   (let* ((articles (gnus-summary-work-articles n))
10172          (save-buffer (save-excursion
10173                         (nnheader-set-temp-buffer " *Gnus Save*")))
10174          (num (length articles))
10175          header file)
10176     (dolist (article articles)
10177       (setq header (gnus-summary-article-header article))
10178       (if (not (vectorp header))
10179           ;; This is a pseudo-article.
10180           (if (assq 'name header)
10181               (gnus-copy-file (cdr (assq 'name header)))
10182             (gnus-message 1 "Article %d is unsaveable" article))
10183         ;; This is a real article.
10184         (save-window-excursion
10185           (let ((gnus-display-mime-function nil)
10186                 (gnus-article-prepare-hook nil))
10187             (gnus-summary-select-article t nil nil article)))
10188         (save-excursion
10189           (set-buffer save-buffer)
10190           (erase-buffer)
10191           (insert-buffer-substring gnus-original-article-buffer))
10192         (setq file (gnus-article-save save-buffer file num))
10193         (gnus-summary-remove-process-mark article)
10194         (unless not-saved
10195           (gnus-summary-set-saved-mark article))))
10196     (gnus-kill-buffer save-buffer)
10197     (gnus-summary-position-point)
10198     (gnus-set-mode-line 'summary)
10199     n))
10200
10201 (defun gnus-summary-pipe-output (&optional arg)
10202   "Pipe the current article to a subprocess.
10203 If N is a positive number, pipe the N next articles.
10204 If N is a negative number, pipe the N previous articles.
10205 If N is nil and any articles have been marked with the process mark,
10206 pipe those articles instead."
10207   (interactive "P")
10208   (require 'gnus-art)
10209   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10210     (gnus-summary-save-article arg t))
10211   (let ((buffer (get-buffer "*Shell Command Output*")))
10212     (if (and buffer
10213              (with-current-buffer buffer (> (point-max) (point-min))))
10214         (gnus-configure-windows 'pipe))))
10215
10216 (defun gnus-summary-save-article-mail (&optional arg)
10217   "Append the current article to an mail file.
10218 If N is a positive number, save the N next articles.
10219 If N is a negative number, save the N previous articles.
10220 If N is nil and any articles have been marked with the process mark,
10221 save those articles instead."
10222   (interactive "P")
10223   (require 'gnus-art)
10224   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10225     (gnus-summary-save-article arg)))
10226
10227 (defun gnus-summary-save-article-rmail (&optional arg)
10228   "Append the current article to an rmail file.
10229 If N is a positive number, save the N next articles.
10230 If N is a negative number, save the N previous articles.
10231 If N is nil and any articles have been marked with the process mark,
10232 save those articles instead."
10233   (interactive "P")
10234   (require 'gnus-art)
10235   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10236     (gnus-summary-save-article arg)))
10237
10238 (defun gnus-summary-save-article-file (&optional arg)
10239   "Append the current article to a file.
10240 If N is a positive number, save the N next articles.
10241 If N is a negative number, save the N previous articles.
10242 If N is nil and any articles have been marked with the process mark,
10243 save those articles instead."
10244   (interactive "P")
10245   (require 'gnus-art)
10246   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10247     (gnus-summary-save-article arg)))
10248
10249 (defun gnus-summary-write-article-file (&optional arg)
10250   "Write the current article to a file, deleting the previous file.
10251 If N is a positive number, save the N next articles.
10252 If N is a negative number, save the N previous articles.
10253 If N is nil and any articles have been marked with the process mark,
10254 save those articles instead."
10255   (interactive "P")
10256   (require 'gnus-art)
10257   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10258     (gnus-summary-save-article arg)))
10259
10260 (defun gnus-summary-save-article-body-file (&optional arg)
10261   "Append the current article body to a file.
10262 If N is a positive number, save the N next articles.
10263 If N is a negative number, save the N previous articles.
10264 If N is nil and any articles have been marked with the process mark,
10265 save those articles instead."
10266   (interactive "P")
10267   (require 'gnus-art)
10268   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10269     (gnus-summary-save-article arg)))
10270
10271 (defun gnus-summary-muttprint (&optional arg)
10272   "Print the current article using Muttprint.
10273 If N is a positive number, save the N next articles.
10274 If N is a negative number, save the N previous articles.
10275 If N is nil and any articles have been marked with the process mark,
10276 save those articles instead."
10277   (interactive "P")
10278   (require 'gnus-art)
10279   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10280     (gnus-summary-save-article arg t)))
10281
10282 (defun gnus-summary-pipe-message (program)
10283   "Pipe the current article through PROGRAM."
10284   (interactive "sProgram: ")
10285   (gnus-summary-select-article)
10286   (let ((mail-header-separator ""))
10287     (gnus-eval-in-buffer-window gnus-article-buffer
10288       (save-restriction
10289         (widen)
10290         (let ((start (window-start))
10291               buffer-read-only)
10292           (message-pipe-buffer-body program)
10293           (set-window-start (get-buffer-window (current-buffer)) start))))))
10294
10295 (defun gnus-get-split-value (methods)
10296   "Return a value based on the split METHODS."
10297   (let (split-name method result match)
10298     (when methods
10299       (save-excursion
10300         (set-buffer gnus-original-article-buffer)
10301         (save-restriction
10302           (nnheader-narrow-to-headers)
10303           (while (and methods (not split-name))
10304             (goto-char (point-min))
10305             (setq method (pop methods))
10306             (setq match (car method))
10307             (when (cond
10308                    ((stringp match)
10309                     ;; Regular expression.
10310                     (ignore-errors
10311                       (re-search-forward match nil t)))
10312                    ((gnus-functionp match)
10313                     ;; Function.
10314                     (save-restriction
10315                       (widen)
10316                       (setq result (funcall match gnus-newsgroup-name))))
10317                    ((consp match)
10318                     ;; Form.
10319                     (save-restriction
10320                       (widen)
10321                       (setq result (eval match)))))
10322               (setq split-name (cdr method))
10323               (cond ((stringp result)
10324                      (push (expand-file-name
10325                             result gnus-article-save-directory)
10326                            split-name))
10327                     ((consp result)
10328                      (setq split-name (append result split-name)))))))))
10329     (nreverse split-name)))
10330
10331 (defun gnus-valid-move-group-p (group)
10332   (and (boundp group)
10333        (symbol-name group)
10334        (symbol-value group)
10335        (gnus-get-function (gnus-find-method-for-group
10336                            (symbol-name group)) 'request-accept-article t)))
10337
10338 (defun gnus-read-move-group-name (prompt default articles prefix)
10339   "Read a group name."
10340   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10341          (minibuffer-confirm-incomplete nil) ; XEmacs
10342          (prom
10343           (format "%s %s to:"
10344                   prompt
10345                   (if (> (length articles) 1)
10346                       (format "these %d articles" (length articles))
10347                     "this article")))
10348          (to-newsgroup
10349           (cond
10350            ((null split-name)
10351             (gnus-completing-read-with-default
10352              default prom
10353              gnus-active-hashtb
10354              'gnus-valid-move-group-p
10355              nil prefix
10356              'gnus-group-history))
10357            ((= 1 (length split-name))
10358             (gnus-completing-read-with-default
10359              (car split-name) prom
10360              gnus-active-hashtb
10361              'gnus-valid-move-group-p
10362              nil nil
10363              'gnus-group-history))
10364            (t
10365             (gnus-completing-read-with-default
10366              nil prom
10367              (mapcar (lambda (el) (list el))
10368                      (nreverse split-name))
10369              nil nil nil
10370              'gnus-group-history))))
10371          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10372     (when to-newsgroup
10373       (if (or (string= to-newsgroup "")
10374               (string= to-newsgroup prefix))
10375           (setq to-newsgroup default))
10376       (unless to-newsgroup
10377         (error "No group name entered"))
10378       (or (gnus-active to-newsgroup)
10379           (gnus-activate-group to-newsgroup nil nil to-method)
10380           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10381                                      to-newsgroup))
10382               (or (and (gnus-request-create-group to-newsgroup to-method)
10383                        (gnus-activate-group
10384                         to-newsgroup nil nil to-method)
10385                        (gnus-subscribe-group to-newsgroup))
10386                   (error "Couldn't create group %s" to-newsgroup)))
10387           (error "No such group: %s" to-newsgroup)))
10388     to-newsgroup))
10389
10390 (defun gnus-summary-save-parts (type dir n &optional reverse)
10391   "Save parts matching TYPE to DIR.
10392 If REVERSE, save parts that do not match TYPE."
10393   (interactive
10394    (list (read-string "Save parts of type: "
10395                       (or (car gnus-summary-save-parts-type-history)
10396                           gnus-summary-save-parts-default-mime)
10397                       'gnus-summary-save-parts-type-history)
10398          (setq gnus-summary-save-parts-last-directory
10399                (read-file-name "Save to directory: "
10400                                gnus-summary-save-parts-last-directory
10401                                nil t))
10402          current-prefix-arg))
10403   (gnus-summary-iterate n
10404     (let ((gnus-display-mime-function nil)
10405           (gnus-inhibit-treatment t))
10406       (gnus-summary-select-article))
10407     (save-excursion
10408       (set-buffer gnus-article-buffer)
10409       (let ((handles (or gnus-article-mime-handles
10410                          (mm-dissect-buffer) (mm-uu-dissect))))
10411         (when handles
10412           (gnus-summary-save-parts-1 type dir handles reverse)
10413           (unless gnus-article-mime-handles ;; Don't destroy this case.
10414             (mm-destroy-parts handles)))))))
10415
10416 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10417   (if (stringp (car handle))
10418       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10419               (cdr handle))
10420     (when (if reverse
10421               (not (string-match type (mm-handle-media-type handle)))
10422             (string-match type (mm-handle-media-type handle)))
10423       (let ((file (expand-file-name
10424                    (file-name-nondirectory
10425                     (or
10426                      (mail-content-type-get
10427                       (mm-handle-disposition handle) 'filename)
10428                      (concat gnus-newsgroup-name
10429                              "." (number-to-string
10430                                   (cdr gnus-article-current)))))
10431                    dir)))
10432         (unless (file-exists-p file)
10433           (mm-save-part-to-file handle file))))))
10434
10435 ;; Summary extract commands
10436
10437 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10438   (let ((buffer-read-only nil)
10439         (article (gnus-summary-article-number))
10440         after-article b e)
10441     (unless (gnus-summary-goto-subject article)
10442       (error "No such article: %d" article))
10443     (gnus-summary-position-point)
10444     ;; If all commands are to be bunched up on one line, we collect
10445     ;; them here.
10446     (unless gnus-view-pseudos-separately
10447       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10448             files action)
10449         (while ps
10450           (setq action (cdr (assq 'action (car ps))))
10451           (setq files (list (cdr (assq 'name (car ps)))))
10452           (while (and ps (cdr ps)
10453                       (string= (or action "1")
10454                                (or (cdr (assq 'action (cadr ps))) "2")))
10455             (push (cdr (assq 'name (cadr ps))) files)
10456             (setcdr ps (cddr ps)))
10457           (when files
10458             (when (not (string-match "%s" action))
10459               (push " " files))
10460             (push " " files)
10461             (when (assq 'execute (car ps))
10462               (setcdr (assq 'execute (car ps))
10463                       (funcall (if (string-match "%s" action)
10464                                    'format 'concat)
10465                                action
10466                                (mapconcat
10467                                 (lambda (f)
10468                                   (if (equal f " ")
10469                                       f
10470                                     (gnus-quote-arg-for-sh-or-csh f)))
10471                                 files " ")))))
10472           (setq ps (cdr ps)))))
10473     (if (and gnus-view-pseudos (not not-view))
10474         (while pslist
10475           (when (assq 'execute (car pslist))
10476             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10477                                   (eq gnus-view-pseudos 'not-confirm)))
10478           (setq pslist (cdr pslist)))
10479       (save-excursion
10480         (while pslist
10481           (setq after-article (or (cdr (assq 'article (car pslist)))
10482                                   (gnus-summary-article-number)))
10483           (gnus-summary-goto-subject after-article)
10484           (forward-line 1)
10485           (setq b (point))
10486           (insert "    " (file-name-nondirectory
10487                           (cdr (assq 'name (car pslist))))
10488                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10489           (setq e (point))
10490           (forward-line -1)             ; back to `b'
10491           (gnus-add-text-properties
10492            b (1- e) (list 'gnus-number gnus-reffed-article-number
10493                           gnus-mouse-face-prop gnus-mouse-face))
10494           (gnus-data-enter
10495            after-article gnus-reffed-article-number
10496            gnus-unread-mark b (car pslist) 0 (- e b))
10497           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10498           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10499           (setq pslist (cdr pslist)))))))
10500
10501 (defun gnus-pseudos< (p1 p2)
10502   (let ((c1 (cdr (assq 'action p1)))
10503         (c2 (cdr (assq 'action p2))))
10504     (and c1 c2 (string< c1 c2))))
10505
10506 (defun gnus-request-pseudo-article (props)
10507   (cond ((assq 'execute props)
10508          (gnus-execute-command (cdr (assq 'execute props)))))
10509   (let ((gnus-current-article (gnus-summary-article-number)))
10510     (gnus-run-hooks 'gnus-mark-article-hook)))
10511
10512 (defun gnus-execute-command (command &optional automatic)
10513   (save-excursion
10514     (gnus-article-setup-buffer)
10515     (set-buffer gnus-article-buffer)
10516     (setq buffer-read-only nil)
10517     (let ((command (if automatic command
10518                      (read-string "Command: " (cons command 0)))))
10519       (erase-buffer)
10520       (insert "$ " command "\n\n")
10521       (if gnus-view-pseudo-asynchronously
10522           (start-process "gnus-execute" (current-buffer) shell-file-name
10523                          shell-command-switch command)
10524         (call-process shell-file-name nil t nil
10525                       shell-command-switch command)))))
10526
10527 ;; Summary kill commands.
10528
10529 (defun gnus-summary-edit-global-kill (article)
10530   "Edit the \"global\" kill file."
10531   (interactive (list (gnus-summary-article-number)))
10532   (gnus-group-edit-global-kill article))
10533
10534 (defun gnus-summary-edit-local-kill ()
10535   "Edit a local kill file applied to the current newsgroup."
10536   (interactive)
10537   (setq gnus-current-headers (gnus-summary-article-header))
10538   (gnus-group-edit-local-kill
10539    (gnus-summary-article-number) gnus-newsgroup-name))
10540
10541 ;;; Header reading.
10542
10543 (defun gnus-read-header (id &optional header)
10544   "Read the headers of article ID and enter them into the Gnus system."
10545   (let ((group gnus-newsgroup-name)
10546         (gnus-override-method
10547          (or
10548           gnus-override-method
10549           (and (gnus-news-group-p gnus-newsgroup-name)
10550                (car (gnus-refer-article-methods)))))
10551         where)
10552     ;; First we check to see whether the header in question is already
10553     ;; fetched.
10554     (if (stringp id)
10555         ;; This is a Message-ID.
10556         (setq header (or header (gnus-id-to-header id)))
10557       ;; This is an article number.
10558       (setq header (or header (gnus-summary-article-header id))))
10559     (if (and header
10560              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10561         ;; We have found the header.
10562         header
10563       ;; If this is a sparse article, we have to nix out its
10564       ;; previous entry in the thread hashtb.
10565       (when (and header
10566                  (gnus-summary-article-sparse-p (mail-header-number header)))
10567         (let* ((parent (gnus-parent-id (mail-header-references header)))
10568                (thread (and parent (gnus-id-to-thread parent))))
10569           (when thread
10570             (delq (assq header thread) thread))))
10571       ;; We have to really fetch the header to this article.
10572       (save-excursion
10573         (set-buffer nntp-server-buffer)
10574         (when (setq where (gnus-request-head id group))
10575           (nnheader-fold-continuation-lines)
10576           (goto-char (point-max))
10577           (insert ".\n")
10578           (goto-char (point-min))
10579           (insert "211 ")
10580           (princ (cond
10581                   ((numberp id) id)
10582                   ((cdr where) (cdr where))
10583                   (header (mail-header-number header))
10584                   (t gnus-reffed-article-number))
10585                  (current-buffer))
10586           (insert " Article retrieved.\n"))
10587         (if (or (not where)
10588                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10589             ()                          ; Malformed head.
10590           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10591             (when (and (stringp id)
10592                        (not (string= (gnus-group-real-name group)
10593                                      (car where))))
10594               ;; If we fetched by Message-ID and the article came
10595               ;; from a different group, we fudge some bogus article
10596               ;; numbers for this article.
10597               (mail-header-set-number header gnus-reffed-article-number))
10598             (save-excursion
10599               (set-buffer gnus-summary-buffer)
10600               (decf gnus-reffed-article-number)
10601               (gnus-remove-header (mail-header-number header))
10602               (push header gnus-newsgroup-headers)
10603               (setq gnus-current-headers header)
10604               (push (mail-header-number header) gnus-newsgroup-limit)))
10605           header)))))
10606
10607 (defun gnus-remove-header (number)
10608   "Remove header NUMBER from `gnus-newsgroup-headers'."
10609   (if (and gnus-newsgroup-headers
10610            (= number (mail-header-number (car gnus-newsgroup-headers))))
10611       (pop gnus-newsgroup-headers)
10612     (let ((headers gnus-newsgroup-headers))
10613       (while (and (cdr headers)
10614                   (not (= number (mail-header-number (cadr headers)))))
10615         (pop headers))
10616       (when (cdr headers)
10617         (setcdr headers (cddr headers))))))
10618
10619 ;;;
10620 ;;; summary highlights
10621 ;;;
10622
10623 (defun gnus-highlight-selected-summary ()
10624   "Highlight selected article in summary buffer."
10625   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10626   (when gnus-summary-selected-face
10627     (save-excursion
10628       (let* ((beg (progn (beginning-of-line) (point)))
10629              (end (progn (end-of-line) (point)))
10630              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10631              (from (if (get-text-property beg gnus-mouse-face-prop)
10632                        beg
10633                      (or (next-single-property-change
10634                           beg gnus-mouse-face-prop nil end)
10635                          beg)))
10636              (to
10637               (if (= from end)
10638                   (- from 2)
10639                 (or (next-single-property-change
10640                      from gnus-mouse-face-prop nil end)
10641                     end))))
10642         ;; If no mouse-face prop on line we will have to = from = end,
10643         ;; so we highlight the entire line instead.
10644         (when (= (+ to 2) from)
10645           (setq from beg)
10646           (setq to end))
10647         (if gnus-newsgroup-selected-overlay
10648             ;; Move old overlay.
10649             (gnus-move-overlay
10650              gnus-newsgroup-selected-overlay from to (current-buffer))
10651           ;; Create new overlay.
10652           (gnus-overlay-put
10653            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10654            'face gnus-summary-selected-face))))))
10655
10656 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10657 (defun gnus-summary-highlight-line ()
10658   "Highlight current line according to `gnus-summary-highlight'."
10659   (let* ((list gnus-summary-highlight)
10660          (p (point))
10661          (end (progn (end-of-line) (point)))
10662          ;; now find out where the line starts and leave point there.
10663          (beg (progn (beginning-of-line) (point)))
10664          (article (gnus-summary-article-number))
10665          (score (or (cdr (assq (or article gnus-current-article)
10666                                gnus-newsgroup-scored))
10667                     gnus-summary-default-score 0))
10668          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10669          (inhibit-read-only t))
10670     ;; Eval the cars of the lists until we find a match.
10671     (let ((default gnus-summary-default-score)
10672           (default-high gnus-summary-default-high-score)
10673           (default-low gnus-summary-default-low-score))
10674       (while (and list
10675                   (not (eval (caar list))))
10676         (setq list (cdr list))))
10677     (let ((face (cdar list)))
10678       (unless (eq face (get-text-property beg 'face))
10679         (gnus-put-text-property-excluding-characters-with-faces
10680          beg end 'face
10681          (setq face (if (boundp face) (symbol-value face) face)))
10682         (when gnus-summary-highlight-line-function
10683           (funcall gnus-summary-highlight-line-function article face))))
10684     (goto-char p)))
10685
10686 (defun gnus-update-read-articles (group unread &optional compute)
10687   "Update the list of read articles in GROUP."
10688   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10689          (entry (gnus-gethash group gnus-newsrc-hashtb))
10690          (info (nth 2 entry))
10691          (prev 1)
10692          (unread (sort (copy-sequence unread) '<))
10693          read)
10694     (if (or (not info) (not active))
10695         ;; There is no info on this group if it was, in fact,
10696         ;; killed.  Gnus stores no information on killed groups, so
10697         ;; there's nothing to be done.
10698         ;; One could store the information somewhere temporarily,
10699         ;; perhaps...  Hmmm...
10700         ()
10701       ;; Remove any negative articles numbers.
10702       (while (and unread (< (car unread) 0))
10703         (setq unread (cdr unread)))
10704       ;; Remove any expired article numbers
10705       (while (and unread (< (car unread) (car active)))
10706         (setq unread (cdr unread)))
10707       ;; Compute the ranges of read articles by looking at the list of
10708       ;; unread articles.
10709       (while unread
10710         (when (/= (car unread) prev)
10711           (push (if (= prev (1- (car unread))) prev
10712                   (cons prev (1- (car unread))))
10713                 read))
10714         (setq prev (1+ (car unread)))
10715         (setq unread (cdr unread)))
10716       (when (<= prev (cdr active))
10717         (push (cons prev (cdr active)) read))
10718       (setq read (if (> (length read) 1) (nreverse read) read))
10719       (if compute
10720           read
10721         (save-excursion
10722           (let (setmarkundo)
10723             ;; Propagate the read marks to the backend.
10724             (when (gnus-check-backend-function 'request-set-mark group)
10725               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10726                     (add (gnus-remove-from-range read (gnus-info-read info))))
10727                 (when (or add del)
10728                   (unless (gnus-check-group group)
10729                     (error "Can't open server for %s" group))
10730                   (gnus-request-set-mark
10731                    group (delq nil (list (if add (list add 'add '(read)))
10732                                          (if del (list del 'del '(read))))))
10733                   (setq setmarkundo
10734                         `(gnus-request-set-mark
10735                           ,group
10736                           ',(delq nil (list
10737                                        (if del (list del 'add '(read)))
10738                                        (if add (list add 'del '(read))))))))))
10739             (set-buffer gnus-group-buffer)
10740             (gnus-undo-register
10741               `(progn
10742                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10743                  (gnus-info-set-read ',info ',(gnus-info-read info))
10744                  (gnus-get-unread-articles-in-group ',info
10745                                                     (gnus-active ,group))
10746                  (gnus-group-update-group ,group t)
10747                  ,setmarkundo))))
10748         ;; Enter this list into the group info.
10749         (gnus-info-set-read info read)
10750         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10751         (gnus-get-unread-articles-in-group info (gnus-active group))
10752         t))))
10753
10754 (defun gnus-offer-save-summaries ()
10755   "Offer to save all active summary buffers."
10756   (let (buffers)
10757     ;; Go through all buffers and find all summaries.
10758     (dolist (buffer (buffer-list))
10759       (when (and (setq buffer (buffer-name buffer))
10760                  (string-match "Summary" buffer)
10761                  (save-excursion
10762                    (set-buffer buffer)
10763                    ;; We check that this is, indeed, a summary buffer.
10764                    (and (eq major-mode 'gnus-summary-mode)
10765                         ;; Also make sure this isn't bogus.
10766                         gnus-newsgroup-prepared
10767                         ;; Also make sure that this isn't a
10768                         ;; dead summary buffer.
10769                         (not gnus-dead-summary-mode))))
10770         (push buffer buffers)))
10771     ;; Go through all these summary buffers and offer to save them.
10772     (when buffers
10773       (save-excursion
10774         (map-y-or-n-p
10775          "Update summary buffer %s? "
10776          (lambda (buf)
10777            (switch-to-buffer buf)
10778            (gnus-summary-exit))
10779          buffers)))))
10780
10781
10782 ;;; @ for mime-partial
10783 ;;;
10784
10785 (defun gnus-request-partial-message ()
10786   (save-excursion
10787     (let ((number (gnus-summary-article-number))
10788           (group gnus-newsgroup-name)
10789           (mother gnus-article-buffer))
10790       (set-buffer (get-buffer-create " *Partial Article*"))
10791       (erase-buffer)
10792       (setq mime-preview-buffer mother)
10793       (gnus-request-article-this-buffer number group)
10794       (mime-parse-buffer)
10795       )))
10796
10797 (autoload 'mime-combine-message/partial-pieces-automatically
10798   "mime-partial"
10799   "Internal method to combine message/partial messages automatically.")
10800
10801 (mime-add-condition
10802  'action '((type . message)(subtype . partial)
10803            (major-mode . gnus-original-article-mode)
10804            (method . mime-combine-message/partial-pieces-automatically)
10805            (summary-buffer-exp . gnus-summary-buffer)
10806            (request-partial-message-method . gnus-request-partial-message)
10807            ))
10808
10809
10810 ;;; @ for message/rfc822
10811 ;;;
10812
10813 (defun gnus-mime-extract-message/rfc822 (entity situation)
10814   "Burst a forwarded article."
10815   (save-excursion
10816     (set-buffer gnus-summary-buffer)
10817     (let* ((group (completing-read "Group: " gnus-active-hashtb
10818                                    nil (gnus-read-active-file-p)
10819                                    gnus-newsgroup-name 'gnus-group-history))
10820            (old (cdr (gnus-active group)))
10821            article summary buffers)
10822       (gnus-summary-goto-subject gnus-current-article)
10823       (gnus-summary-copy-article 1 group)
10824       (setq article (cdr (gnus-active group)))
10825       (unless (> article old)
10826         (error "Something wrong on bursting; check articles in %s" group))
10827       (with-temp-buffer
10828         (mime-insert-entity-content entity)
10829         (gnus-request-replace-article article group (current-buffer) t))
10830       (when (string-equal group gnus-newsgroup-name)
10831         (gnus-summary-goto-subject gnus-current-article)
10832         (forward-line 1)
10833         (let (gnus-show-threads)
10834           (gnus-summary-goto-subject article t))
10835         (gnus-summary-clear-mark-forward 1)))))
10836
10837 (mime-add-condition
10838  'action '((type . message)(subtype . rfc822)
10839            (major-mode . gnus-original-article-mode)
10840            (method . gnus-mime-extract-message/rfc822)
10841            (mode . "extract")
10842            ))
10843
10844 (mime-add-condition
10845  'action '((type . message)(subtype . news)
10846            (major-mode . gnus-original-article-mode)
10847            (method . gnus-mime-extract-message/rfc822)
10848            (mode . "extract")
10849            ))
10850
10851 (defun gnus-mime-extract-multipart (entity situation)
10852   (let ((children (mime-entity-children entity))
10853         mime-acting-situation-to-override
10854         f)
10855     (while children
10856       (mime-play-entity (car children)
10857                         (cons (assq 'mode situation)
10858                               mime-acting-situation-to-override))
10859       (setq children (cdr children)))
10860     (if (setq f (cdr (assq 'after-method
10861                            mime-acting-situation-to-override)))
10862         (eval f)
10863       )))
10864
10865 (mime-add-condition
10866  'action '((type . multipart)
10867            (method . gnus-mime-extract-multipart)
10868            (mode . "extract")
10869            )
10870  'with-default)
10871
10872
10873 ;;; @ end
10874 ;;;
10875
10876 (defun gnus-summary-setup-default-charset ()
10877   "Setup newsgroup default charset."
10878   (if (equal gnus-newsgroup-name "nndraft:drafts")
10879       (setq gnus-newsgroup-charset nil)
10880     (let* ((ignored-charsets
10881             (or gnus-newsgroup-ephemeral-ignored-charsets
10882                 (append
10883                  (and gnus-newsgroup-name
10884                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10885                  gnus-newsgroup-ignored-charsets))))
10886       (setq gnus-newsgroup-charset
10887             (or gnus-newsgroup-ephemeral-charset
10888                 (and gnus-newsgroup-name
10889                      (gnus-parameter-charset gnus-newsgroup-name))
10890                 gnus-default-charset))
10891       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10892            ignored-charsets))))
10893
10894 ;;;
10895 ;;; Mime Commands
10896 ;;;
10897
10898 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10899   "Display the current article buffer fully MIME-buttonized.
10900 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10901 treated as multipart/mixed."
10902   (interactive "P")
10903   (require 'gnus-art)
10904   (let ((gnus-unbuttonized-mime-types nil)
10905         (gnus-mime-display-multipart-as-mixed show-all-parts))
10906     (gnus-summary-show-article)))
10907
10908 (defun gnus-summary-repair-multipart (article)
10909   "Add a Content-Type header to a multipart article without one."
10910   (interactive (list (gnus-summary-article-number)))
10911   (gnus-with-article article
10912     (message-narrow-to-head)
10913     (message-remove-header "Mime-Version")
10914     (goto-char (point-max))
10915     (insert "Mime-Version: 1.0\n")
10916     (widen)
10917     (when (search-forward "\n--" nil t)
10918       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10919         (message-narrow-to-head)
10920         (message-remove-header "Content-Type")
10921         (goto-char (point-max))
10922         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10923                         separator))
10924         (widen))))
10925   (let (gnus-mark-article-hook)
10926     (gnus-summary-select-article t t nil article)))
10927
10928 (defun gnus-summary-toggle-display-buttonized ()
10929   "Toggle the buttonizing of the article buffer."
10930   (interactive)
10931   (require 'gnus-art)
10932   (if (setq gnus-inhibit-mime-unbuttonizing
10933             (not gnus-inhibit-mime-unbuttonizing))
10934       (let ((gnus-unbuttonized-mime-types nil))
10935         (gnus-summary-show-article))
10936     (gnus-summary-show-article)))
10937
10938 ;;;
10939 ;;; Intelli-mouse commmands
10940 ;;;
10941
10942 (defun gnus-wheel-summary-scroll (event)
10943   (interactive "e")
10944   (let ((amount (if (memq 'shift (event-modifiers event))
10945                     (car gnus-wheel-scroll-amount)
10946                   (cdr gnus-wheel-scroll-amount)))
10947         (direction (- (* (static-if (featurep 'xemacs)
10948                              (event-button event)
10949                            (cond ((eq 'mouse-4 (event-basic-type event))
10950                                   4)
10951                                  ((eq 'mouse-5 (event-basic-type event))
10952                                   5)))
10953                          2) 9))
10954         edge)
10955     (gnus-summary-scroll-up (* amount direction))
10956     (when (gnus-eval-in-buffer-window gnus-article-buffer
10957             (save-restriction
10958               (widen)
10959               (and (if (< 0 direction)
10960                        (gnus-article-next-page 0)
10961                      (gnus-article-prev-page 0)
10962                      (bobp))
10963                    (if (setq edge (get-text-property
10964                                    (point-min) 'gnus-wheel-edge))
10965                        (setq edge (* edge direction))
10966                      (setq edge -1))
10967                    (or (plusp edge)
10968                        (let ((buffer-read-only nil)
10969                              (inhibit-read-only t))
10970                          (put-text-property (point-min) (point-max)
10971                                             'gnus-wheel-edge direction)
10972                          nil))
10973                    (or (> edge gnus-wheel-edge-resistance)
10974                        (let ((buffer-read-only nil)
10975                              (inhibit-read-only t))
10976                          (put-text-property (point-min) (point-max)
10977                                             'gnus-wheel-edge
10978                                             (* (1+ edge) direction))
10979                          nil))
10980                    (eq last-command 'gnus-wheel-summary-scroll))))
10981       (gnus-summary-next-article nil nil (minusp direction)))))
10982
10983 (defun gnus-wheel-install ()
10984   "Enable mouse wheel support on summary window."
10985   (when gnus-use-wheel
10986     (let ((keys
10987            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10988       (dolist (key keys)
10989         (define-key gnus-summary-mode-map key
10990           'gnus-wheel-summary-scroll)))))
10991
10992 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10993
10994 ;;;
10995 ;;; Traditional PGP commmands
10996 ;;;
10997
10998 (defun gnus-summary-decrypt-article (&optional force)
10999   "Decrypt the current article in traditional PGP way.
11000 This will have permanent effect only in mail groups.
11001 If FORCE is non-nil, allow editing of articles even in read-only
11002 groups."
11003   (interactive "P")
11004   (gnus-summary-select-article t)
11005   (gnus-eval-in-buffer-window gnus-article-buffer
11006     (save-excursion
11007       (save-restriction
11008         (widen)
11009         (goto-char (point-min))
11010         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11011           (error "Not a traditional PGP message!"))
11012         (let ((armor-start (match-beginning 0)))
11013           (if (and (pgg-decrypt-region armor-start (point-max))
11014                    (or force (not (gnus-group-read-only-p))))
11015               (let ((inhibit-read-only t)
11016                     buffer-read-only)
11017                 (delete-region armor-start
11018                                (progn
11019                                  (re-search-forward "^-+END PGP" nil t)
11020                                  (beginning-of-line 2)
11021                                  (point)))
11022                 (insert-buffer-substring pgg-output-buffer))))))))
11023
11024 (defun gnus-summary-verify-article ()
11025   "Verify the current article in traditional PGP way."
11026   (interactive)
11027   (save-excursion
11028     (set-buffer gnus-original-article-buffer)
11029     (goto-char (point-min))
11030     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11031       (error "Not a traditional PGP message!"))
11032     (re-search-forward "^-+END PGP" nil t)
11033     (beginning-of-line 2)
11034     (call-interactively (function pgg-verify-region))))
11035
11036 ;;;
11037 ;;; Generic summary marking commands
11038 ;;;
11039
11040 (defvar gnus-summary-marking-alist
11041   '((read gnus-del-mark "d")
11042     (unread gnus-unread-mark "u")
11043     (ticked gnus-ticked-mark "!")
11044     (dormant gnus-dormant-mark "?")
11045     (expirable gnus-expirable-mark "e"))
11046   "An alist of names/marks/keystrokes.")
11047
11048 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11049 (defvar gnus-summary-mark-map)
11050
11051 (defun gnus-summary-make-all-marking-commands ()
11052   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11053   (dolist (elem gnus-summary-marking-alist)
11054     (apply 'gnus-summary-make-marking-command elem)))
11055
11056 (defun gnus-summary-make-marking-command (name mark keystroke)
11057   (let ((map (make-sparse-keymap)))
11058     (define-key gnus-summary-generic-mark-map keystroke map)
11059     (dolist (lway `((next "next" next nil "n")
11060                     (next-unread "next unread" next t "N")
11061                     (prev "previous" prev nil "p")
11062                     (prev-unread "previous unread" prev t "P")
11063                     (nomove "" nil nil ,keystroke)))
11064       (let ((func (gnus-summary-make-marking-command-1
11065                    mark (car lway) lway name)))
11066         (setq func (eval func))
11067         (define-key map (nth 4 lway) func)))))
11068
11069 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11070   `(defun ,(intern
11071             (format "gnus-summary-put-mark-as-%s%s"
11072                     name (if (eq way 'nomove)
11073                              ""
11074                            (concat "-" (symbol-name way)))))
11075      (n)
11076      ,(format
11077        "Mark the current article as %s%s.
11078 If N, the prefix, then repeat N times.
11079 If N is negative, move in reverse order.
11080 The difference between N and the actual number of articles marked is
11081 returned."
11082        name (car (cdr lway)))
11083      (interactive "p")
11084      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11085
11086 (defun gnus-summary-generic-mark (n mark move unread)
11087   "Mark N articles with MARK."
11088   (unless (eq major-mode 'gnus-summary-mode)
11089     (error "This command can only be used in the summary buffer"))
11090   (gnus-summary-show-thread)
11091   (let ((nummove
11092          (cond
11093           ((eq move 'next) 1)
11094           ((eq move 'prev) -1)
11095           (t 0))))
11096     (if (zerop nummove)
11097         (setq n 1)
11098       (when (< n 0)
11099         (setq n (abs n)
11100               nummove (* -1 nummove))))
11101     (while (and (> n 0)
11102                 (gnus-summary-mark-article nil mark)
11103                 (zerop (gnus-summary-next-subject nummove unread t)))
11104       (setq n (1- n)))
11105     (when (/= 0 n)
11106       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11107     (gnus-summary-recenter)
11108     (gnus-summary-position-point)
11109     (gnus-set-mode-line 'summary)
11110     n))
11111
11112 (defun gnus-summary-insert-articles (articles)
11113   (when (setq articles
11114               (gnus-set-difference articles
11115                                    (mapcar (lambda (h) (mail-header-number h))
11116                                            gnus-newsgroup-headers)))
11117     (setq gnus-newsgroup-headers
11118           (merge 'list
11119                  gnus-newsgroup-headers
11120                  (gnus-fetch-headers articles)
11121                  'gnus-article-sort-by-number))
11122     ;; Suppress duplicates?
11123     (when gnus-suppress-duplicates
11124       (gnus-dup-suppress-articles))
11125
11126     ;; We might want to build some more threads first.
11127     (when (and gnus-fetch-old-headers
11128                (eq gnus-headers-retrieved-by 'nov))
11129       (if (eq gnus-fetch-old-headers 'invisible)
11130           (gnus-build-all-threads)
11131         (gnus-build-old-threads)))
11132     ;; Let the Gnus agent mark articles as read.
11133     (when gnus-agent
11134       (gnus-agent-get-undownloaded-list))
11135     ;; Remove list identifiers from subject
11136     (when gnus-list-identifiers
11137       (gnus-summary-remove-list-identifiers))
11138     ;; First and last article in this newsgroup.
11139     (when gnus-newsgroup-headers
11140       (setq gnus-newsgroup-begin
11141             (mail-header-number (car gnus-newsgroup-headers))
11142             gnus-newsgroup-end
11143             (mail-header-number
11144              (gnus-last-element gnus-newsgroup-headers))))
11145     (when gnus-use-scoring
11146       (gnus-possibly-score-headers))))
11147
11148 (defun gnus-summary-insert-old-articles (&optional all)
11149   "Insert all old articles in this group.
11150 If ALL is non-nil, already read articles become readable.
11151 If ALL is a number, fetch this number of articles."
11152   (interactive "P")
11153   (prog1
11154       (let ((old (mapcar 'car gnus-newsgroup-data))
11155             (i (car gnus-newsgroup-active))
11156             older len)
11157         (while (<= i (cdr gnus-newsgroup-active))
11158           (or (memq i old) (push i older))
11159           (incf i))
11160         (setq len (length older))
11161         (cond
11162          ((null older) nil)
11163          ((numberp all)
11164           (if (< all len)
11165               (setq older (subseq older 0 all))))
11166          (all nil)
11167          (t
11168           (if (and (numberp gnus-large-newsgroup)
11169                    (> len gnus-large-newsgroup))
11170               (let ((input
11171                      (read-string
11172                       (format
11173                        "How many articles from %s (default %d): "
11174                        (gnus-limit-string
11175                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
11176                        len))))
11177                 (unless (string-match "^[ \t]*$" input)
11178                   (setq all (string-to-number input))
11179                   (if (< all len)
11180                       (setq older (subseq older 0 all))))))))
11181         (if (not older)
11182             (message "No old news.")
11183           (let ((gnus-fetch-old-headers t))
11184             (gnus-summary-insert-articles older))
11185           (gnus-summary-limit (gnus-union older old))))
11186     (gnus-summary-position-point)))
11187
11188 (defun gnus-summary-insert-new-articles ()
11189   "Insert all new articles in this group."
11190   (interactive)
11191   (prog1
11192       (let ((old (mapcar 'car gnus-newsgroup-data))
11193             (old-active gnus-newsgroup-active)
11194             (nnmail-fetched-sources (list t))
11195             i new)
11196         (setq gnus-newsgroup-active
11197               (gnus-activate-group gnus-newsgroup-name 'scan))
11198         (setq i (1+ (cdr old-active)))
11199         (while (<= i (cdr gnus-newsgroup-active))
11200           (push i new)
11201           (incf i))
11202         (if (not new)
11203             (message "No gnus is bad news.")
11204           (setq new (nreverse new))
11205           (gnus-summary-insert-articles new)
11206           (setq gnus-newsgroup-unreads
11207                 (append gnus-newsgroup-unreads new))
11208           (gnus-summary-limit (gnus-union old new))))
11209     (gnus-summary-position-point)))
11210
11211 (gnus-summary-make-all-marking-commands)
11212
11213 (gnus-ems-redefine)
11214
11215 (provide 'gnus-sum)
11216
11217 (run-hooks 'gnus-sum-load-hook)
11218
11219 ;;; gnus-sum.el ends here